Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

694 lines
19 KiB

  1. //===========================================================================
  2. // dmtwrite.cpp
  3. //
  4. // File / code creation functionality
  5. //
  6. // Functions:
  7. // dmtwriteBrowse
  8. // dmtwriteWriteFileHeader
  9. // dmtwriteReadMappingFile
  10. // dmtwriteWriteDIHeader
  11. // dmtwriteWriteDeviceHeader
  12. // dmtwriteWriteObjectSection
  13. // dmtwriteWriteAllObjectSections
  14. // dmtwriteWriteGenreSection
  15. // dmtwriteWriteAllGenreSections
  16. // dmtwriteCreateDeviceShorthand
  17. // dmtwriteDisplaySaveDialog
  18. //
  19. // History:
  20. // 08/20/1999 - davidkl - created
  21. //===========================================================================
  22. #include "dimaptst.h"
  23. #include "commdlg.h"
  24. #include "cderr.h"
  25. #include "dmtinput.h"
  26. #include "dmtwrite.h"
  27. //---------------------------------------------------------------------------
  28. //===========================================================================
  29. // dmtwriteWriteFileHeader
  30. //
  31. // Writes the semantic mapping file for the provided device
  32. //
  33. // Parameters:
  34. //
  35. // Returns: HRESULT
  36. //
  37. // History:
  38. // 10/11/1999 - davidkl - stubbed
  39. // 10/14/1999 - davidkl - renamed and tweaked
  40. // 11/04/1999 - davidkl - reduced parameter list
  41. // 12/01/1999 - davidkl - now registers file HERE
  42. //===========================================================================
  43. HRESULT dmtwriteWriteFileHeader(HWND hwnd,
  44. DMTDEVICE_NODE *pDevice)
  45. {
  46. HRESULT hRes = S_OK;
  47. DWORD dwGenres = 0;
  48. HANDLE hDoesFileExist = NULL;
  49. // validate pDevice
  50. if(IsBadReadPtr((void*)pDevice, sizeof(DMTDEVICE_NODE)))
  51. {
  52. return E_POINTER;
  53. }
  54. __try
  55. {
  56. // prompt the user for where to save
  57. //
  58. // if we are handed a non-empty filename
  59. // (not == ""), skip this step
  60. // if(!lstrcmpA("", pDevice->szFilename))
  61. {
  62. // display the save dialog
  63. hRes = dmtwriteDisplaySaveDialog(hwnd, pDevice);
  64. if(FAILED(hRes))
  65. {
  66. __leave;
  67. }
  68. if(S_FALSE == hRes)
  69. {
  70. //user canceled
  71. __leave;
  72. }
  73. }
  74. // generate the device shorthand string
  75. lstrcpyA(pDevice->szShorthandName, pDevice->szName);
  76. /*
  77. //02/21/2000 - taking this out for now
  78. hRes = dmtwriteCreateDeviceShorthand(pDevice->szName,
  79. pDevice->szShorthandName);
  80. if(FAILED(hRes))
  81. {
  82. __leave;
  83. }
  84. */
  85. //JT - Fix for 38829 added create to check if file exists prior to writing all the header info back to the file.
  86. hDoesFileExist = CreateFile(pDevice->szFilename,GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
  87. if (INVALID_HANDLE_VALUE == hDoesFileExist)
  88. {
  89. DPF(0,"This file doesn't exist so we will write the header");
  90. // write the DirectInput header
  91. hRes = dmtwriteWriteDIHeader(pDevice->szFilename,
  92. pDevice->szShorthandName,
  93. dwGenres);
  94. if(FAILED(hRes))
  95. {
  96. __leave;
  97. }
  98. // write the device header
  99. hRes = dmtwriteWriteDeviceHeader(pDevice);
  100. if(FAILED(hRes))
  101. {
  102. __leave;
  103. }
  104. // write the device object sections
  105. hRes = dmtwriteWriteAllObjectSections(pDevice->szFilename,
  106. pDevice->szShorthandName,
  107. pDevice->pObjectList);
  108. if(FAILED(hRes))
  109. {
  110. __leave;
  111. }
  112. }
  113. else
  114. {
  115. //Otherwise the file does exist and we have to close the handle
  116. CloseHandle(hDoesFileExist);
  117. }
  118. // update the registry
  119. //
  120. // this is needed so that dinput can find our new file
  121. hRes = dmtinputRegisterMapFile(hwnd,
  122. pDevice);
  123. if(FAILED(hRes))
  124. {
  125. __leave;
  126. }
  127. }
  128. __finally
  129. {
  130. // general cleanup
  131. // nothing to do... yet
  132. }
  133. // done
  134. return hRes;
  135. } //*** end dmtwriteWriteFileHeader()
  136. //===========================================================================
  137. // dmtwriteWriteDIHeader
  138. //
  139. // Writes the DirectInput section of the device mapping ini file.
  140. //
  141. // Parameters:
  142. //
  143. // Returns:
  144. //
  145. // History:
  146. // 10/12/1999 - davidkl - created
  147. // 10/15/1999 - davidkl - tweaked section entries
  148. //===========================================================================
  149. HRESULT dmtwriteWriteDIHeader(PSTR szFilename,
  150. PSTR szDeviceShorthand,
  151. DWORD dwGenres)
  152. {
  153. HRESULT hRes = S_OK;
  154. __try
  155. {
  156. // * di version
  157. if(!WritePrivateProfileStringA("DirectInput",
  158. "DirectXVersion",
  159. DMT_DI_STRING_VER,
  160. szFilename))
  161. {
  162. hRes = DMT_E_FILE_WRITE_FAILED;
  163. __leave;
  164. }
  165. // * device
  166. // ISSUE-2001/03/29-timgill Need to read original value and support multiple devices
  167. if(!WritePrivateProfileStringA("DirectInput",
  168. "Devices",
  169. szDeviceShorthand,
  170. szFilename))
  171. {
  172. hRes = DMT_E_FILE_WRITE_FAILED;
  173. __leave;
  174. }
  175. }
  176. __finally
  177. {
  178. // cleanup
  179. // nothing to do... yet
  180. }
  181. // done
  182. return hRes;
  183. } //*** end dmtwriteWriteDIHeader()
  184. //===========================================================================
  185. // dmtwriteWriteDeviceHeader
  186. //
  187. // Writes the device summary section of the device mapping ini file.
  188. //
  189. // Parameters:
  190. //
  191. // Returns:
  192. //
  193. // History:
  194. // 10/12/1999 - davidkl - created
  195. // 11/01/1999 - davidkl - file size reduction changes
  196. // 11/04/1999 - davidkl - reduced parameter list
  197. //===========================================================================
  198. HRESULT dmtwriteWriteDeviceHeader(DMTDEVICE_NODE *pDevice)
  199. {
  200. HRESULT hRes = S_OK;
  201. DMTDEVICEOBJECT_NODE *pObjNode = NULL;
  202. UINT uAxes = 0;
  203. UINT uBtns = 0;
  204. UINT uPovs = 0;
  205. char szBuf[MAX_PATH];
  206. // validate pDevice
  207. if(IsBadReadPtr((void*)pDevice, sizeof(DMTDEVICE_NODE)))
  208. {
  209. return E_POINTER;
  210. }
  211. __try
  212. {
  213. // vendor id
  214. //
  215. // only write this if the vid is non-zero
  216. if(0 != pDevice->wVendorId)
  217. {
  218. wsprintfA(szBuf, "%d", pDevice->wVendorId);
  219. if(!WritePrivateProfileStringA(pDevice->szShorthandName,
  220. "VID",
  221. szBuf,
  222. pDevice->szFilename))
  223. {
  224. hRes = DMT_E_FILE_WRITE_FAILED;
  225. __leave;
  226. }
  227. }
  228. // product id
  229. //
  230. // only write this if the pid is non-zero
  231. if(0 != pDevice->wProductId)
  232. {
  233. wsprintfA(szBuf, "%d", pDevice->wProductId);
  234. if(!WritePrivateProfileStringA(pDevice->szShorthandName,
  235. "PID",
  236. szBuf,
  237. pDevice->szFilename))
  238. {
  239. hRes = DMT_E_FILE_WRITE_FAILED;
  240. __leave;
  241. }
  242. }
  243. // name
  244. //
  245. if(!WritePrivateProfileStringA(pDevice->szShorthandName,
  246. "Name",
  247. pDevice->szName,
  248. pDevice->szFilename))
  249. {
  250. hRes = DMT_E_FILE_WRITE_FAILED;
  251. __leave;
  252. }
  253. // control list
  254. lstrcpyA(szBuf, "");
  255. pObjNode = pDevice->pObjectList;
  256. while(pObjNode)
  257. {
  258. DPF(0, "dmtwriteWriteDeviceHeader - pObjNode == %016Xh", pObjNode);
  259. DPF(0, "dmtwriteWriteDeviceHeader - pObjNode->szName == %s", pObjNode->szName);
  260. wsprintfA(szBuf, "%s%s,", szBuf, pObjNode->szName);
  261. DPF(0, "dmtwriteWriteDeviceHeader - szBuf == %s", szBuf);
  262. // next object
  263. pObjNode = pObjNode->pNext;
  264. }
  265. if(!WritePrivateProfileStringA(pDevice->szShorthandName,
  266. "Controls",
  267. szBuf,
  268. pDevice->szFilename))
  269. {
  270. DPF(0, "dmtwriteWriteDeviceHeader - writing controls == %s", szBuf);
  271. hRes = DMT_E_FILE_WRITE_FAILED;
  272. __leave;
  273. }
  274. }
  275. __finally
  276. {
  277. // cleanup
  278. // nothing to do... yet
  279. }
  280. // done
  281. return hRes;
  282. } //*** end dmtwriteWriteDeviceHeader()
  283. //===========================================================================
  284. // dmtwriteWriteObjectSection
  285. //
  286. // Writes an individual object section of the device mapping ini file.
  287. //
  288. // Parameters:
  289. //
  290. // Returns:
  291. //
  292. // History:
  293. // 10/12/1999 - davidkl - stubbed
  294. // 10/13/1999 - davidkl - initial implementation
  295. // 10/15/1999 - davidkl - added name to section
  296. // 11/01/1999 - davidkl - file size reduction changes
  297. //===========================================================================
  298. HRESULT dmtwriteWriteObjectSection(PSTR szFilename,
  299. PSTR szDeviceShorthand,
  300. PSTR szObjectName,
  301. WORD wUsagePage,
  302. WORD wUsage)
  303. {
  304. HRESULT hRes = S_OK;
  305. char szBuf[MAX_PATH];
  306. char szSection[MAX_PATH];
  307. // construct section name
  308. /*
  309. wsprintfA(szSection, "%s.%s",
  310. szDeviceShorthand,
  311. */
  312. wsprintfA(szSection, "%s",
  313. szObjectName);
  314. // usage page
  315. //
  316. // only write this if it is non-zero
  317. if(0 != wUsagePage)
  318. {
  319. wsprintfA(szBuf, "%d", wUsagePage);
  320. if(!WritePrivateProfileStringA(szSection,
  321. "UsagePage",
  322. szBuf,
  323. szFilename))
  324. {
  325. return DMT_E_FILE_WRITE_FAILED;
  326. }
  327. }
  328. // usage
  329. //
  330. // only write this if it is non-zero
  331. if(0 != wUsage)
  332. {
  333. wsprintfA(szBuf, "%d", wUsage);
  334. if(!WritePrivateProfileStringA(szSection,
  335. "Usage",
  336. szBuf,
  337. szFilename))
  338. {
  339. return DMT_E_FILE_WRITE_FAILED;
  340. }
  341. }
  342. // name
  343. //
  344. // only write this if >both< wUsagePage and wUsage are zero
  345. if((0 == wUsagePage) && (0 == wUsage))
  346. {
  347. if(!WritePrivateProfileStringA(szSection,
  348. "Name",
  349. szObjectName,
  350. szFilename))
  351. {
  352. return DMT_E_FILE_WRITE_FAILED;
  353. }
  354. }
  355. // done
  356. return S_OK;
  357. } //*** end dmtwriteWriteObjectSection()
  358. //===========================================================================
  359. // dmtwriteWriteAllObjectSections
  360. //
  361. // Writes all object sections of the device mapping ini file.
  362. //
  363. // Parameters:
  364. //
  365. // Returns:
  366. //
  367. // History:
  368. // 10/12/1999 - davidkl - stubbed
  369. // 10/13/1999 - davidkl - initial implementation
  370. //===========================================================================
  371. HRESULT dmtwriteWriteAllObjectSections(PSTR szFilename,
  372. PSTR szDeviceShorthand,
  373. DMTDEVICEOBJECT_NODE *pObjectList)
  374. {
  375. HRESULT hRes = S_OK;
  376. DMTDEVICEOBJECT_NODE *pObject = NULL;
  377. // validate pObjectList
  378. if(IsBadReadPtr((void*)pObjectList, sizeof(DMTDEVICEOBJECT_NODE)))
  379. {
  380. return E_POINTER;
  381. }
  382. pObject = pObjectList;
  383. while(pObject)
  384. {
  385. hRes = dmtwriteWriteObjectSection(szFilename,
  386. szDeviceShorthand,
  387. pObject->szName,
  388. pObject->wUsagePage,
  389. pObject->wUsage);
  390. if(FAILED(hRes))
  391. {
  392. break;
  393. }
  394. // next object
  395. pObject = pObject->pNext;
  396. }
  397. // done
  398. return hRes;
  399. } //*** end dmtwriteWriteAllObjectSections()
  400. //===========================================================================
  401. // dmtwriteDisplaySaveDialog
  402. //
  403. // Displays Save (As) dialog box promting the user for the filename
  404. //
  405. // Parameters:
  406. // HWND hwnd - handle to owner of save dialog
  407. // PSTR szFilename - receives selected filename (incl. drive & path)
  408. // int cchFilename - count of characters in szFilename buffer
  409. //
  410. // Returns: HRESULT
  411. //
  412. // History:
  413. // 10/14/1999 - davidkl - created
  414. //===========================================================================
  415. HRESULT dmtwriteDisplaySaveDialog(HWND hwnd,
  416. DMTDEVICE_NODE *pDevice)
  417. {
  418. HRESULT hRes = S_OK;
  419. USHORT nOffsetFilename = 0;
  420. USHORT nOffsetExt = 0;
  421. DWORD dw = 0;
  422. OPENFILENAMEA ofn;
  423. char szTitle[MAX_PATH];
  424. // initialize Title Text
  425. lstrcpyA(szTitle, "Select DirectInput(TM) Mapping File");
  426. lstrcatA(szTitle, " for ");
  427. lstrcatA(szTitle, pDevice->szName);
  428. // initialize the ofn struct
  429. ZeroMemory((void*)&ofn, sizeof(OPENFILENAMEA));
  430. ofn.lStructSize = sizeof(OPENFILENAMEA);
  431. ofn.hwndOwner = hwnd;
  432. ofn.hInstance = (HINSTANCE)NULL; // not using dlg template
  433. ofn.lpstrFilter = "DirectInput(TM) Mapping Files\0*.ini\0";
  434. ofn.lpstrCustomFilter = (LPSTR)NULL; // don't save custom
  435. ofn.nMaxCustFilter = 0; // ignored based on above
  436. ofn.nFilterIndex = 1; // display first filter
  437. ofn.lpstrFile = pDevice->szFilename; // filename w/ path
  438. ofn.nMaxFile = MAX_PATH;
  439. ofn.lpstrFileTitle = (LPSTR)NULL; // filename w/o path
  440. ofn.nMaxFileTitle = 0;
  441. ofn.lpstrInitialDir = (LPSTR)NULL; // use default initial dir
  442. ofn.lpstrTitle = szTitle;
  443. ofn.Flags = OFN_CREATEPROMPT |
  444. OFN_OVERWRITEPROMPT |
  445. OFN_HIDEREADONLY |
  446. OFN_NOREADONLYRETURN |
  447. OFN_NOTESTFILECREATE;
  448. ofn.nFileOffset = (WORD)nOffsetFilename;
  449. ofn.nFileExtension = (WORD)nOffsetExt;
  450. ofn.lpstrDefExt = "ini";
  451. ofn.lCustData = NULL;
  452. ofn.lpfnHook = NULL;
  453. ofn.lpTemplateName = NULL;
  454. // display the save dialog
  455. if(!GetOpenFileNameA(&ofn))
  456. {
  457. // either something failed, or the user canceled
  458. //
  459. // find out which
  460. dw = CommDlgExtendedError();
  461. if( 0 == dw )
  462. {
  463. // user canceled
  464. DPF(2, "dmtwriteDisplaySaveDialog - user canceled");
  465. hRes = S_FALSE;
  466. }
  467. else
  468. {
  469. // failure
  470. DPF(2, "dmtwriteDisplaySaveDialog - GetSaveFileNameA failed (%d)", dw);
  471. hRes = E_UNEXPECTED;
  472. }
  473. }
  474. // done
  475. return hRes;
  476. } //*** end dmtwriteDisplaySaveDialog()
  477. //===========================================================================
  478. // dmtwriteSaveConfDlgProc
  479. //
  480. // Save confirmation dialog processing function
  481. //
  482. // Parameters: (see SDK help for parameter details)
  483. // HWND hwnd
  484. // UINT uMsg
  485. // WPARAM wparam
  486. // LPARAM lparam
  487. //
  488. // Returns: (see SDK help for return value details)
  489. // BOOL
  490. //
  491. // History:
  492. // 10/18/1999 - davidkl - created
  493. //===========================================================================
  494. INT_PTR WINAPI CALLBACK dmtwriteSaveConfDlgProc(HWND hwnd,
  495. UINT uMsg,
  496. WPARAM wparam,
  497. LPARAM lparam)
  498. {
  499. switch(uMsg)
  500. {
  501. case WM_INITDIALOG:
  502. return dmtwriteSaveConfOnInitDialog(hwnd,
  503. (HWND)wparam,
  504. lparam);
  505. case WM_COMMAND:
  506. return dmtwriteSaveConfOnCommand(hwnd,
  507. LOWORD(wparam),
  508. (HWND)lparam,
  509. HIWORD(wparam));
  510. }
  511. return FALSE;
  512. } //*** end dmtwriteSaveConfDlgProc()
  513. //===========================================================================
  514. // dmtwriteSaveConfOnInitDialog
  515. //
  516. // Handle WM_INITDIALOG processing for the save confirmation box
  517. //
  518. // Parameters:
  519. // HWND hwnd - handle to property page
  520. // HWND hwndFocus - handle of ctrl with focus
  521. // LPARAM lparam - user data (in this case, PROPSHEETPAGE*)
  522. //
  523. // Returns: BOOL
  524. //
  525. // History:
  526. // 10/18/1999 - davidkl - created
  527. //===========================================================================
  528. BOOL dmtwriteSaveConfOnInitDialog(HWND hwnd,
  529. HWND hwndFocus,
  530. LPARAM lparam)
  531. {
  532. char szBuf[MAX_PATH];
  533. wsprintfA(szBuf,
  534. "Save genre group %s action map?",
  535. (PSTR)lparam);
  536. SetWindowTextA(hwnd, szBuf);
  537. SetDlgItemTextA(hwnd,
  538. IDC_GENRE_GROUP,
  539. (PSTR)lparam);
  540. // done
  541. return TRUE;
  542. } //*** end dmtwriteSaveConfOnInitDialog()
  543. //===========================================================================
  544. // dmtwriteSaveConfOnCommand
  545. //
  546. // Handle WM_COMMAND processing for the save confirmation box
  547. //
  548. // Parameters:
  549. // HWND hwnd - handle to property page
  550. // WORD wId - control identifier (LOWORD(wparam))
  551. // HWND hwndCtrl - handle to control ((HWND)lparam)
  552. // WORD wNotifyCode - notification code (HIWORD(wparam))
  553. //
  554. // Returns: BOOL
  555. //
  556. // History:
  557. // 10/18/1999 - davidkl - created
  558. //===========================================================================
  559. BOOL dmtwriteSaveConfOnCommand(HWND hwnd,
  560. WORD wId,
  561. HWND hwndCtrl,
  562. WORD wNotifyCode)
  563. {
  564. int nRet = -1;
  565. switch(wId)
  566. {
  567. case IDOK:
  568. EndDialog(hwnd, (int)IDYES);
  569. break;
  570. case IDC_DONT_SAVE:
  571. EndDialog(hwnd, (int)IDNO);
  572. break;
  573. case IDCANCEL:
  574. EndDialog(hwnd, (int)IDCANCEL);
  575. break;
  576. }
  577. // done
  578. return FALSE;
  579. } //*** end dmtwriteSaveConfOnCommand()
  580. //===========================================================================
  581. //===========================================================================
  582. //===========================================================================
  583. //===========================================================================
  584. //===========================================================================
  585. //===========================================================================
  586. //===========================================================================
  587. //===========================================================================