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.

227 lines
5.4 KiB

  1. /*****************************************************************************
  2. *
  3. * diqfind.c
  4. *
  5. * The dialog box that tests IDirectInput2::FindDevice.
  6. *
  7. *****************************************************************************/
  8. #include "diquick.h"
  9. #include "dinputd.h"
  10. /*****************************************************************************
  11. *
  12. * Find dialog instance data
  13. *
  14. * Instance data for FindDevice dialog box.
  15. *
  16. *****************************************************************************/
  17. typedef struct FINDDLGINFO {
  18. HWND hdlgOwner; /* Owner window */
  19. BOOL fOle; /* Should we create via OLE? */
  20. UINT flCreate; /* Flags */
  21. IDirectInput2 *pdi2; /* The thing we created */
  22. } FINDDLGINFO, *PFINDDLGINFO;
  23. /*****************************************************************************
  24. *
  25. * Find_OnInitDialog
  26. *
  27. *****************************************************************************/
  28. BOOL INTERNAL
  29. Find_OnInitDialog(HWND hdlg, LPARAM lp)
  30. {
  31. PFINDDLGINFO pfind = (PV)lp;
  32. HWND hwnd;
  33. SetDialogPtr(hdlg, pfind);
  34. hwnd = GetDlgItem(hdlg, IDC_FIND_NAME);
  35. Edit_LimitText(hwnd, MAX_PATH);
  36. return 1;
  37. }
  38. /*****************************************************************************
  39. *
  40. * Find_OnInitDialog
  41. *
  42. *****************************************************************************/
  43. BOOL INTERNAL
  44. Find_OnFind(HWND hdlg)
  45. {
  46. PFINDDLGINFO pfind = GetDialogPtr(hdlg);
  47. GUID guid;
  48. HRESULT hres;
  49. TCHAR tsz[MAX_PATH];
  50. union {
  51. CHAR sz[MAX_PATH];
  52. WCHAR wsz[MAX_PATH];
  53. } u;
  54. /*
  55. * tsz must be a separate buffer because
  56. * WideCharToMultiByte / MultiByteToWideChar
  57. * don't support converting to/from the same buffer...
  58. */
  59. GetDlgItemText(hdlg, IDC_FIND_NAME, tsz, cA(tsz));
  60. UnconvertString(pfind->flCreate & CDIFL_UNICODE,
  61. tsz, &u, MAX_PATH);
  62. hres = IDirectInput2_FindDevice(pfind->pdi2, &GUID_HIDClass,
  63. (LPVOID)&u, &guid);
  64. if (SUCCEEDED(hres)) {
  65. StringFromGuid(tsz, &guid);
  66. } else {
  67. wsprintf(tsz, TEXT("Error %08x"), hres);
  68. }
  69. SetDlgItemText(hdlg, IDC_FIND_GUID, tsz);
  70. return 1;
  71. }
  72. /*****************************************************************************
  73. *
  74. * Find_OnCommand
  75. *
  76. *****************************************************************************/
  77. BOOL INTERNAL
  78. Find_OnCommand(HWND hdlg, int id, UINT cmd)
  79. {
  80. switch (id) {
  81. case IDC_FIND_FIND: return Find_OnFind(hdlg);
  82. }
  83. return 0;
  84. }
  85. /*****************************************************************************
  86. *
  87. * Find_DlgProc
  88. *
  89. *****************************************************************************/
  90. INT_PTR INTERNAL
  91. Find_DlgProc(HWND hdlg, UINT wm, WPARAM wp, LPARAM lp)
  92. {
  93. switch (wm) {
  94. case WM_INITDIALOG:
  95. return Find_OnInitDialog(hdlg, lp);
  96. case WM_DESTROY:
  97. /*
  98. * Find_ThreadStart will do the cleanup for us.
  99. */
  100. break;
  101. case WM_COMMAND:
  102. return Find_OnCommand(hdlg,
  103. (int)GET_WM_COMMAND_ID(wp, lp),
  104. (UINT)GET_WM_COMMAND_CMD(wp, lp));
  105. case WM_CLOSE:
  106. DestroyWindow(hdlg);
  107. return TRUE;
  108. }
  109. return 0;
  110. }
  111. /*****************************************************************************
  112. *
  113. * Find_DoFind
  114. *
  115. *****************************************************************************/
  116. void INLINE
  117. Find_DoFind(PFINDDLGINFO pfind)
  118. {
  119. SendNotifyMessage(pfind->hdlgOwner, WM_THREADSTARTED, 0, 0);
  120. /*
  121. * This function also sends the WM_CHILDEXIT.
  122. */
  123. SemimodalDialogBoxParam(IDD_FIND, pfind->hdlgOwner, Find_DlgProc,
  124. (LPARAM)pfind);
  125. }
  126. /*****************************************************************************
  127. *
  128. * Find_ThreadStart
  129. *
  130. * Runs on the new thread. Creates the object and spins the dialog
  131. * box to control it.
  132. *
  133. *****************************************************************************/
  134. DWORD WINAPI
  135. Find_ThreadStart(PFINDDLGINFO pfind)
  136. {
  137. HRESULT hres;
  138. hres = CoInitialize(0);
  139. if (SUCCEEDED(hres)) {
  140. hres = CreateDI(pfind->fOle, CDIFL_DI2 | pfind->flCreate,
  141. (PPV)&pfind->pdi2);
  142. if (SUCCEEDED(hres)) {
  143. Find_DoFind(pfind);
  144. IDirectInput_Release(pfind->pdi2);
  145. } else {
  146. ThreadFailHres(pfind->hdlgOwner, IDS_ERR_CREATEOBJ, hres);
  147. }
  148. CoUninitialize();
  149. } else {
  150. ThreadFailHres(pfind->hdlgOwner, IDS_ERR_COINIT, hres);
  151. }
  152. LocalFree(pfind);
  153. return 0;
  154. }
  155. /*****************************************************************************
  156. *
  157. * Find_Create
  158. *
  159. * Spin a thread to create a DirectInput device interface.
  160. *
  161. *****************************************************************************/
  162. INT_PTR EXTERNAL
  163. Find_Create(HWND hdlg, BOOL fOle, UINT flCreate)
  164. {
  165. PFINDDLGINFO pfind = LocalAlloc(LPTR, cbX(FINDDLGINFO));
  166. if (pfind) {
  167. DWORD id;
  168. HANDLE h;
  169. pfind->hdlgOwner = hdlg ;
  170. pfind->fOle = fOle ;
  171. pfind->flCreate = flCreate ;
  172. h = CreateThread(0, 0, Find_ThreadStart, pfind, 0, &id);
  173. if (h) {
  174. } else {
  175. LocalFree(pfind);
  176. pfind = 0;
  177. }
  178. }
  179. return (INT_PTR)pfind;
  180. }