Leaked source code of windows server 2003
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.

3001 lines
101 KiB

  1. /*++
  2. Copyright (c) Microsoft 1998, All Rights Reserved
  3. Module Name:
  4. hclient.c
  5. Abstract:
  6. This module contains the code for handling HClient's main dialog box and
  7. for performing/calling the appropriate other routines.
  8. Environment:
  9. User mode
  10. Revision History:
  11. Nov-97 : Created
  12. --*/
  13. #define __HCLIENT_C__
  14. #define LOG_FILE_NAME NULL
  15. //****************************************************************************
  16. // HClient include files
  17. //****************************************************************************
  18. #include <windows.h>
  19. #include <stdlib.h>
  20. #include <wtypes.h>
  21. #include <math.h>
  22. #include <assert.h>
  23. #include <dbt.h>
  24. #include "hidsdi.h"
  25. #include "hid.h"
  26. #include "resource.h"
  27. #include "hclient.h"
  28. #include "buffers.h"
  29. #include "ecdisp.h"
  30. #include "list.h"
  31. #include <strsafe.h>
  32. //****************************************************************************
  33. // Local display macro definitions
  34. //****************************************************************************
  35. #define INPUT_BUTTON 1
  36. #define INPUT_VALUE 2
  37. #define OUTPUT_BUTTON 3
  38. #define OUTPUT_VALUE 4
  39. #define FEATURE_BUTTON 5
  40. #define FEATURE_VALUE 6
  41. #define HID_CAPS 7
  42. #define DEVICE_ATTRIBUTES 8
  43. #define MAX_LB_ITEMS 200
  44. #define MAX_WRITE_ELEMENTS 100
  45. #define MAX_OUTPUT_ELEMENTS 50
  46. #define CONTROL_COUNT 9
  47. #define MAX_LABEL 128
  48. #define MAX_VALUE 128
  49. #define SMALL_BUFF 128
  50. //****************************************************************************
  51. // Macro definition to get device block from the main dialog box procedure
  52. //****************************************************************************
  53. #define GET_CURRENT_DEVICE(hDlg, pDevice) \
  54. { \
  55. pDevice = NULL; \
  56. iIndex = (INT) SendDlgItemMessage(hDlg, \
  57. IDC_DEVICES, \
  58. CB_GETCURSEL, \
  59. 0, \
  60. 0); \
  61. if (CB_ERR != iIndex) { \
  62. pDevice = (PHID_DEVICE) SendDlgItemMessage(hDlg, \
  63. IDC_DEVICES, \
  64. CB_GETITEMDATA, \
  65. iIndex, \
  66. 0); \
  67. } \
  68. }
  69. //****************************************************************************
  70. // Data types local to the HClient display routines
  71. //****************************************************************************
  72. typedef struct rWriteDataStruct_type
  73. {
  74. char szLabel[MAX_LABEL];
  75. char szValue[MAX_VALUE];
  76. } rWriteDataStruct, *prWriteDataStruct;
  77. typedef struct rGetWriteDataParams_type
  78. {
  79. prWriteDataStruct prItems;
  80. int iCount;
  81. } rGetWriteDataParams, *prGetWriteDataParams;
  82. typedef struct _DEVICE_LIST_NODE
  83. {
  84. LIST_NODE_HDR Hdr;
  85. HDEVNOTIFY NotificationHandle;
  86. HID_DEVICE HidDeviceInfo;
  87. BOOL DeviceOpened;
  88. } DEVICE_LIST_NODE, *PDEVICE_LIST_NODE;
  89. //****************************************************************************
  90. // Global program variables
  91. //****************************************************************************
  92. //
  93. // Pointers to the HID.DLL functions that were added into the Win98 OSR and
  94. // Windows 2000 but we're not included in the original implementation of
  95. // HID.DLL in Windows 98. By getting pointers to these functions instead of
  96. // statically linking with them, we can avoid the link error that would
  97. // occur when this runs on Windows 98. The typedefs to make this easier to
  98. // declare are also included below.
  99. //
  100. PGETEXTATTRIB pfnHidP_GetExtendedAttributes = NULL;
  101. PINITREPORT pfnHidP_InitializeReportForID = NULL;
  102. //****************************************************************************
  103. // Global module variables
  104. //****************************************************************************
  105. static HINSTANCE hGInstance; //global application instance handle
  106. static HANDLE HIDDLLModuleHandle;
  107. //
  108. // Variables for handling the two different types of devices that can be loaded
  109. // into the system. PhysicalDeviceList contains all the actual HID devices
  110. // attached via the USB bus.
  111. //
  112. static LIST PhysicalDeviceList;
  113. //****************************************************************************
  114. // Local data routine declarations
  115. //****************************************************************************
  116. VOID
  117. vReadDataFromControls(
  118. HWND hDlg,
  119. prWriteDataStruct prData,
  120. int iOffset,
  121. int iCount
  122. );
  123. INT_PTR CALLBACK
  124. bGetDataDlgProc(
  125. HWND hDlg,
  126. UINT message,
  127. WPARAM wParam,
  128. LPARAM lParam
  129. );
  130. INT_PTR CALLBACK
  131. bMainDlgProc(
  132. HWND hDlg,
  133. UINT message,
  134. WPARAM wParam,
  135. LPARAM lParam
  136. );
  137. INT_PTR CALLBACK
  138. bFeatureDlgProc(
  139. HWND hDlg,
  140. UINT message,
  141. WPARAM wParam,
  142. LPARAM lParam
  143. );
  144. INT_PTR CALLBACK
  145. bReadDlgProc(
  146. HWND hDlg,
  147. UINT message,
  148. WPARAM wParam,
  149. LPARAM lParam
  150. );
  151. VOID
  152. vLoadItemTypes(
  153. HWND hItemTypes
  154. );
  155. BOOL
  156. bGetData(
  157. prWriteDataStruct,
  158. int iCount,
  159. HWND hParent,
  160. char *pszDialogName
  161. );
  162. VOID
  163. vLoadDevices(
  164. HWND hDeviceCombo
  165. );
  166. VOID
  167. vFreeDeviceList(
  168. PHID_DEVICE DeviceList,
  169. ULONG nDevices
  170. );
  171. VOID
  172. vDisplayInputButtons(
  173. PHID_DEVICE pDevice,
  174. HWND hControl
  175. );
  176. VOID
  177. vDisplayInputValues(
  178. PHID_DEVICE pDevice,
  179. HWND hControl
  180. );
  181. VOID
  182. vDisplayOutputButtons(
  183. PHID_DEVICE pDevice,
  184. HWND hControl
  185. );
  186. VOID
  187. vDisplayOutputValues(
  188. PHID_DEVICE pDevice,
  189. HWND hControl
  190. );
  191. VOID
  192. vDisplayFeatureButtons(
  193. PHID_DEVICE pDevice,
  194. HWND hControl
  195. );
  196. VOID
  197. vDisplayFeatureValues(
  198. PHID_DEVICE pDevice,
  199. HWND hControl
  200. );
  201. VOID
  202. vWriteDataToControls(
  203. HWND hDlg,
  204. prWriteDataStruct prData,
  205. int iOffset,
  206. int iCount
  207. );
  208. int
  209. iPrepareDataFields(
  210. PHID_DATA pData,
  211. ULONG ulDataLength,
  212. rWriteDataStruct rWriteData[],
  213. int iMaxElements
  214. );
  215. BOOL
  216. bParseData(
  217. PHID_DATA pData,
  218. rWriteDataStruct rWriteData[],
  219. INT iCount,
  220. INT *piErrorLine
  221. );
  222. BOOL
  223. bSetButtonUsages(
  224. PHID_DATA pCap,
  225. PCHAR pszInputString
  226. );
  227. VOID
  228. BuildReportIDList(
  229. IN PHIDP_BUTTON_CAPS phidButtonCaps,
  230. IN USHORT nButtonCaps,
  231. IN PHIDP_VALUE_CAPS phidValueCaps,
  232. IN USHORT nValueCaps,
  233. OUT UCHAR **ppReportIDList,
  234. OUT INT *nReportIDs
  235. );
  236. VOID
  237. ReportToString(
  238. PHID_DATA pData,
  239. PCHAR szBuff,
  240. UINT iBuffSize
  241. );
  242. BOOL
  243. RegisterHidDevice(
  244. IN HWND WindowHandle,
  245. IN PDEVICE_LIST_NODE DeviceNode
  246. );
  247. VOID
  248. DestroyDeviceListCallback(
  249. IN PLIST_NODE_HDR ListNode
  250. );
  251. //****************************************************************************
  252. // Function Definitions
  253. //****************************************************************************
  254. /*******************************
  255. *WinMain: Windows Entry point *
  256. *******************************/
  257. int PASCAL
  258. WinMain(
  259. HINSTANCE hInstance,
  260. HINSTANCE hPrevInstance,
  261. LPSTR lpCmdLine,
  262. int nCmdShow
  263. )
  264. {
  265. //
  266. // Save instance of the application for further reference
  267. //
  268. hGInstance = hInstance;
  269. //
  270. // Attempt to load HID.DLL...This should already be loaded due to the
  271. // static linking of HID.DLL to this app on compilation. However,
  272. // to insure that this application runs on Windows 98 gold, we cannot
  273. // directly reference the new functions HidP_GetExtendedAttributes and
  274. // HidP_InitializeReportForID so to use them, we'll get pointers to their
  275. // functions instead.
  276. //
  277. HIDDLLModuleHandle = LoadLibrary("HID.DLL");
  278. if (NULL == HIDDLLModuleHandle)
  279. {
  280. //
  281. // Something really bad happened here...Throw up and error dialog
  282. // and bolt.
  283. //
  284. MessageBox(NULL,
  285. "Unable to open HID.DLL\n"
  286. "This should never occur",
  287. HCLIENT_ERROR,
  288. MB_ICONSTOP);
  289. return (0);
  290. }
  291. //
  292. // Get the function pointers,
  293. //
  294. pfnHidP_GetExtendedAttributes = (PGETEXTATTRIB) GetProcAddress(HIDDLLModuleHandle,
  295. "HidP_GetExtendedAttributes");
  296. pfnHidP_InitializeReportForID = (PINITREPORT) GetProcAddress(HIDDLLModuleHandle,
  297. "HidP_InitializeReportForID");
  298. //
  299. // Try to create the main dialog box. Cannot do much else if it fails
  300. // so we'll throw up a message box and then exit the app
  301. //
  302. if (-1 == DialogBox(hInstance, "MAIN_DIALOG", NULL, bMainDlgProc))
  303. {
  304. MessageBox(NULL,
  305. "Unable to create root dialog!",
  306. "DialogBox failure",
  307. MB_ICONSTOP);
  308. }
  309. FreeLibrary (HIDDLLModuleHandle);
  310. return (0);
  311. }
  312. /*************************************************
  313. * Main Dialog proc *
  314. *************************************************/
  315. //
  316. // This the dialog box procedure for the main dialog display.
  317. //
  318. INT_PTR CALLBACK
  319. bMainDlgProc(
  320. HWND hDlg,
  321. UINT message,
  322. WPARAM wParam,
  323. LPARAM lParam
  324. )
  325. {
  326. static HWND hComboCtrl;
  327. static rWriteDataStruct rWriteData[MAX_OUTPUT_ELEMENTS];
  328. static HDEVNOTIFY diNotifyHandle;
  329. INT iIndex;
  330. INT iCount;
  331. CHAR szTempBuff[SMALL_BUFF];
  332. PHID_DEVICE pDevice;
  333. PHIDP_BUTTON_CAPS pButtonCaps;
  334. PHIDP_VALUE_CAPS pValueCaps;
  335. INT iErrorLine;
  336. INT iItemType;
  337. PHID_DEVICE tempDeviceList;
  338. ULONG numberDevices;
  339. PDEVICE_LIST_NODE listNode;
  340. DEV_BROADCAST_DEVICEINTERFACE broadcastInterface;
  341. HID_DEVICE writeDevice;
  342. BOOL status;
  343. HRESULT stringReturn;
  344. switch (message)
  345. {
  346. case WM_INITDIALOG:
  347. //
  348. // Initialize the device list.
  349. // -- PhysicalDeviceList is for devices that are actually attached
  350. // to the HID bus
  351. //
  352. InitializeList(&PhysicalDeviceList);
  353. //
  354. // Begin by finding all the Physical HID devices currently attached to
  355. // the system. If that fails, exit the dialog box.
  356. //
  357. if (!FindKnownHidDevices(&tempDeviceList, &numberDevices))
  358. {
  359. EndDialog(hDlg, 0);
  360. return FALSE;
  361. }
  362. //
  363. // For each device in the newly acquired list, create a device list
  364. // node and add it the the list of physical device on the system
  365. //
  366. pDevice = tempDeviceList;
  367. for (iIndex = 0; (ULONG) iIndex < numberDevices; iIndex++, pDevice++)
  368. {
  369. listNode = malloc(sizeof(DEVICE_LIST_NODE));
  370. if (NULL == listNode) {
  371. //
  372. // When freeing up the device list, we need to kill those
  373. // already in the Physical Device List and close
  374. // that have not been added yet in the enumerated list
  375. //
  376. DestroyListWithCallback(&PhysicalDeviceList, DestroyDeviceListCallback);
  377. CloseHidDevices(pDevice, numberDevices - iIndex);
  378. free(tempDeviceList);
  379. EndDialog(hDlg, 0);
  380. return FALSE;
  381. }
  382. listNode -> HidDeviceInfo = *pDevice;
  383. listNode -> DeviceOpened = TRUE;
  384. //
  385. // Register this device node with the PnP system so the dialog
  386. // window can recieve notification if this device is unplugged.
  387. //
  388. if (!RegisterHidDevice(hDlg, listNode))
  389. {
  390. DestroyListWithCallback(&PhysicalDeviceList, DestroyDeviceListCallback);
  391. CloseHidDevices(pDevice, numberDevices - iIndex);
  392. free(tempDeviceList);
  393. free(listNode);
  394. EndDialog(hDlg, 0);
  395. return FALSE;
  396. }
  397. InsertTail(&PhysicalDeviceList, listNode);
  398. }
  399. //
  400. // Free the temporary device list...It is no longer needed
  401. //
  402. free(tempDeviceList);
  403. //
  404. // Register for notification from the HidDevice class. Doing so
  405. // allows the dialog box to receive device change notifications
  406. // whenever a new HID device is added to the system
  407. //
  408. broadcastInterface.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
  409. broadcastInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  410. HidD_GetHidGuid(&broadcastInterface.dbcc_classguid);
  411. diNotifyHandle = RegisterDeviceNotification(hDlg,
  412. &broadcastInterface,
  413. DEVICE_NOTIFY_WINDOW_HANDLE
  414. );
  415. if (NULL == diNotifyHandle)
  416. {
  417. DestroyListWithCallback(&PhysicalDeviceList, DestroyDeviceListCallback);
  418. EndDialog(hDlg, 0);
  419. return FALSE;
  420. }
  421. //
  422. // Update the device list box...
  423. //
  424. //
  425. vLoadDevices(GetDlgItem(hDlg, IDC_DEVICES));
  426. //
  427. // Load the types box
  428. //
  429. vLoadItemTypes(GetDlgItem(hDlg, IDC_TYPE));
  430. //
  431. // Post a message that the device changed so the appropriate
  432. // data for the first device in the system can be displayed
  433. //
  434. PostMessage(hDlg,
  435. WM_COMMAND,
  436. IDC_DEVICES + (CBN_SELCHANGE<<16),
  437. (LPARAM) GetDlgItem(hDlg, IDC_DEVICES));
  438. break; // end WM_INITDIALOG case
  439. case WM_COMMAND:
  440. switch(LOWORD(wParam))
  441. {
  442. //
  443. // For a read, simply get the current device instance
  444. // from the DEVICES combo box and call the read procedure
  445. // with the HID_DEVICE block
  446. //
  447. case IDC_READ:
  448. GET_CURRENT_DEVICE(hDlg, pDevice);
  449. if (NULL != pDevice)
  450. {
  451. iIndex = (INT) DialogBoxParam(hGInstance,
  452. "READDATA",
  453. hDlg,
  454. bReadDlgProc,
  455. (LPARAM) pDevice);
  456. }
  457. break;
  458. //
  459. // For a write, the following steps are performed:
  460. // 1) Get the current device data from the combo box
  461. // 2) Prepare the data fields for display based on the data
  462. // output data stored in the device data
  463. // 3) Retrieve the data the from the user that is to be sent
  464. // to the device
  465. // 4) If all goes well and the data parses correctly, send the
  466. // the new data values to the device
  467. //
  468. case IDC_WRITE:
  469. GET_CURRENT_DEVICE(hDlg, pDevice);
  470. if (NULL != pDevice)
  471. {
  472. //
  473. // In order to write to the device, need to get a
  474. // writable handle to the device. In this case, the
  475. // write will be a synchronous write. Begin by
  476. // trying to open a second instance of this device with
  477. // write access
  478. //
  479. status = OpenHidDevice(pDevice -> DevicePath,
  480. FALSE,
  481. TRUE,
  482. FALSE,
  483. FALSE,
  484. &writeDevice);
  485. if (!status)
  486. {
  487. MessageBox(hDlg,
  488. "Couldn't open device for write access",
  489. HCLIENT_ERROR,
  490. MB_ICONEXCLAMATION);
  491. }
  492. else
  493. {
  494. iCount = iPrepareDataFields(writeDevice.OutputData,
  495. writeDevice.OutputDataLength,
  496. rWriteData,
  497. MAX_OUTPUT_ELEMENTS);
  498. if (bGetData(rWriteData, iCount, hDlg, "WRITEDATA"))
  499. {
  500. if (bParseData(writeDevice.OutputData, rWriteData, iCount, &iErrorLine))
  501. {
  502. Write(&writeDevice);
  503. }
  504. else
  505. {
  506. stringReturn = StringCbPrintf(szTempBuff,
  507. SMALL_BUFF,
  508. "Unable to parse line %x of output data",
  509. iErrorLine);
  510. MessageBox(hDlg,
  511. szTempBuff,
  512. HCLIENT_ERROR,
  513. MB_ICONEXCLAMATION);
  514. }
  515. }
  516. CloseHidDevice(&writeDevice);
  517. }
  518. }
  519. break; //end case IDC_WRITE//
  520. //
  521. // For processing features, get the current device data and call
  522. // the Features dialog box, This dialog box will deal with
  523. // sending and retrieving the features.
  524. //
  525. case IDC_FEATURES:
  526. GET_CURRENT_DEVICE(hDlg, pDevice);
  527. if (NULL != pDevice)
  528. {
  529. iIndex = (INT) DialogBoxParam(hGInstance,
  530. "FEATURES",
  531. hDlg,
  532. bFeatureDlgProc,
  533. (LPARAM) pDevice);
  534. }
  535. break;
  536. //
  537. // Likewise with extended calls dialog box. This procedure
  538. // passes the address to the device data structure and lets
  539. // the dialog box procedure manipulate the data however it
  540. // wants to.
  541. //
  542. case IDC_EXTCALLS:
  543. GET_CURRENT_DEVICE(hDlg, pDevice);
  544. if (NULL != pDevice)
  545. {
  546. iIndex = (INT) DialogBoxParam(hGInstance,
  547. "EXTCALLS",
  548. hDlg,
  549. bExtCallDlgProc,
  550. (LPARAM) pDevice);
  551. }
  552. break;
  553. //
  554. // If there was a device change, issue an IDC_TYPE
  555. // change to insure that the currently displayed types are
  556. // updated to reflect the values of the device that has
  557. // been selected
  558. //
  559. case IDC_DEVICES:
  560. switch (HIWORD(wParam))
  561. {
  562. case CBN_SELCHANGE:
  563. GET_CURRENT_DEVICE(hDlg, pDevice);
  564. EnableWindow(GetDlgItem(hDlg, IDC_READ),
  565. (pDevice != NULL) &&
  566. (pDevice -> Caps.InputReportByteLength));
  567. EnableWindow(GetDlgItem(hDlg, IDC_WRITE),
  568. (pDevice != NULL) &&
  569. (pDevice -> Caps.OutputReportByteLength));
  570. EnableWindow(GetDlgItem(hDlg, IDC_FEATURES),
  571. (pDevice != NULL) &&
  572. (pDevice -> Caps.FeatureReportByteLength));
  573. PostMessage(hDlg,
  574. WM_COMMAND,
  575. IDC_TYPE + (CBN_SELCHANGE<<16),
  576. (LPARAM) GetDlgItem(hDlg,IDC_TYPE));
  577. break;
  578. }
  579. break;
  580. //
  581. // On a type change, retrieve the currently active device
  582. // from the IDC_DEVICES box and display the data that
  583. // corresponds to the item just selected
  584. //
  585. case IDC_TYPE:
  586. switch (HIWORD(wParam))
  587. {
  588. case CBN_SELCHANGE:
  589. GET_CURRENT_DEVICE(hDlg, pDevice);
  590. SendDlgItemMessage(hDlg,
  591. IDC_ITEMS,
  592. LB_RESETCONTENT,
  593. 0,
  594. 0);
  595. SendDlgItemMessage(hDlg,
  596. IDC_ATTRIBUTES,
  597. LB_RESETCONTENT,
  598. 0,
  599. 0);
  600. if (NULL != pDevice)
  601. {
  602. iIndex = (INT) SendDlgItemMessage(hDlg,
  603. IDC_TYPE,
  604. CB_GETCURSEL,
  605. 0,
  606. 0);
  607. iItemType = (INT) SendDlgItemMessage(hDlg,
  608. IDC_TYPE,
  609. CB_GETITEMDATA,
  610. iIndex,
  611. 0);
  612. switch(iItemType)
  613. {
  614. case INPUT_BUTTON:
  615. vDisplayInputButtons(pDevice,GetDlgItem(hDlg,IDC_ITEMS));
  616. break;
  617. case INPUT_VALUE:
  618. vDisplayInputValues(pDevice,GetDlgItem(hDlg,IDC_ITEMS));
  619. break;
  620. case OUTPUT_BUTTON:
  621. vDisplayOutputButtons(pDevice,GetDlgItem(hDlg,IDC_ITEMS));
  622. break;
  623. case OUTPUT_VALUE:
  624. vDisplayOutputValues(pDevice,GetDlgItem(hDlg,IDC_ITEMS));
  625. break;
  626. case FEATURE_BUTTON:
  627. vDisplayFeatureButtons(pDevice,GetDlgItem(hDlg,IDC_ITEMS));
  628. break;
  629. case FEATURE_VALUE:
  630. vDisplayFeatureValues(pDevice,GetDlgItem(hDlg,IDC_ITEMS));
  631. break;
  632. }
  633. PostMessage(hDlg,
  634. WM_COMMAND,
  635. IDC_ITEMS + (LBN_SELCHANGE << 16),
  636. (LPARAM) GetDlgItem(hDlg,IDC_ITEMS));
  637. }
  638. break; // case CBN_SELCHANGE
  639. } //end switch HIWORD wParam
  640. break; //case IDC_TYPE control
  641. case IDC_ITEMS:
  642. switch(HIWORD(wParam))
  643. {
  644. case LBN_SELCHANGE:
  645. iItemType = 0;
  646. iIndex = (INT) SendDlgItemMessage(hDlg,
  647. IDC_TYPE,
  648. CB_GETCURSEL,
  649. 0,
  650. 0);
  651. if (-1 != iIndex)
  652. {
  653. iItemType = (INT) SendDlgItemMessage(hDlg,
  654. IDC_TYPE,
  655. CB_GETITEMDATA,
  656. iIndex,
  657. 0);
  658. }
  659. iIndex = (INT) SendDlgItemMessage(hDlg,
  660. IDC_ITEMS,
  661. LB_GETCURSEL,
  662. 0,
  663. 0);
  664. switch (iItemType)
  665. {
  666. case INPUT_BUTTON:
  667. case OUTPUT_BUTTON:
  668. case FEATURE_BUTTON:
  669. pButtonCaps = NULL;
  670. if (-1 != iIndex)
  671. {
  672. pButtonCaps = (PHIDP_BUTTON_CAPS) SendDlgItemMessage(hDlg,
  673. IDC_ITEMS,
  674. LB_GETITEMDATA,
  675. iIndex,
  676. 0);
  677. }
  678. SendDlgItemMessage(hDlg, IDC_ATTRIBUTES, LB_RESETCONTENT, 0, 0);
  679. if (NULL != pButtonCaps)
  680. {
  681. vDisplayButtonAttributes(pButtonCaps, GetDlgItem(hDlg,IDC_ATTRIBUTES));
  682. }
  683. break;
  684. case INPUT_VALUE:
  685. case OUTPUT_VALUE:
  686. case FEATURE_VALUE:
  687. pValueCaps = NULL;
  688. if (-1 != iIndex)
  689. {
  690. pValueCaps = (PHIDP_VALUE_CAPS) SendDlgItemMessage(hDlg,
  691. IDC_ITEMS,
  692. LB_GETITEMDATA,
  693. iIndex,
  694. 0);
  695. }
  696. SendDlgItemMessage(hDlg, IDC_ATTRIBUTES, LB_RESETCONTENT, 0, 0);
  697. if (NULL != pValueCaps)
  698. {
  699. vDisplayValueAttributes(pValueCaps,GetDlgItem(hDlg,IDC_ATTRIBUTES));
  700. }
  701. break;
  702. case HID_CAPS:
  703. GET_CURRENT_DEVICE(hDlg, pDevice);
  704. if (NULL != pDevice)
  705. {
  706. vDisplayDeviceCaps(&(pDevice -> Caps),GetDlgItem(hDlg,IDC_ATTRIBUTES));
  707. }
  708. break;
  709. case DEVICE_ATTRIBUTES:
  710. GET_CURRENT_DEVICE(hDlg, pDevice);
  711. if (NULL != pDevice)
  712. {
  713. SendDlgItemMessage(hDlg, IDC_ATTRIBUTES, LB_RESETCONTENT, 0, 0);
  714. vDisplayDeviceAttributes(&(pDevice -> Attributes) ,GetDlgItem(hDlg,IDC_ATTRIBUTES));
  715. }
  716. break;
  717. } //end switch iItemType//
  718. break; //end case LBN_SELCHANGE in IDC_ITEMS//
  719. } //end switch HIWORD wParam//
  720. break; //case IDC_ITEMS//
  721. case IDC_ABOUT:
  722. MessageBox(hDlg,
  723. "Sample HID client Application. Microsoft Corp \nCopyright (C) 1997",
  724. "About HClient",
  725. MB_ICONINFORMATION);
  726. break;
  727. case IDOK:
  728. case IDCANCEL:
  729. //
  730. // Destroy the physical device list for exit
  731. //
  732. DestroyListWithCallback(&PhysicalDeviceList, DestroyDeviceListCallback);
  733. EndDialog(hDlg,0);
  734. break;
  735. } //end switch wParam//
  736. break;
  737. //
  738. // For a device change message, we are only concerned about the
  739. // DBT_DEVICEREMOVECOMPLETE and DBT_DEVICEARRIVAL events. I have
  740. // yet to determine how to process the device change message
  741. // only for HID devices. Therefore, there are two problems
  742. // with the below implementation. First of all, we must reload
  743. // the device list any time a device is added to the system.
  744. // Secondly, at least two DEVICEARRIVAL messages are received
  745. // per HID. One corresponds to the physical device. The second
  746. // change and any more correspond to each collection on the
  747. // physical device so a system that has one HID device with
  748. // two top level collections (a keyboard and a mouse) will receive
  749. // three DEVICEARRIVAL/REMOVALs causing the program to reload it's
  750. // device list more than once.
  751. //
  752. //
  753. // To handle dynamic changing of devices, we have already registered
  754. // notification for both HID class changes and for notification
  755. // for our open file objects. Since we are only concerned about
  756. // arrival/removal of devices, we only need to process those wParam.
  757. // lParam points to some sort of DEV_BROADCAST_HDR struct. For device
  758. // arrival, we only deal with the message if that struct is a
  759. // DEV_BROADCAST_DEVICEINTERFACE structure. For device removal, we're
  760. // only concerned if the struct is a DEV_BROADCAST_HANDLE structure.
  761. //
  762. case WM_DEVICECHANGE:
  763. switch (wParam)
  764. {
  765. PDEV_BROADCAST_HDR broadcastHdr;
  766. case DBT_DEVICEARRIVAL:
  767. broadcastHdr = (PDEV_BROADCAST_HDR) lParam;
  768. if (DBT_DEVTYP_DEVICEINTERFACE == broadcastHdr -> dbch_devicetype)
  769. {
  770. PDEV_BROADCAST_DEVICEINTERFACE pbroadcastInterface;
  771. PDEVICE_LIST_NODE currNode, lastNode;
  772. pbroadcastInterface = (PDEV_BROADCAST_DEVICEINTERFACE) lParam;
  773. //
  774. // Search for a previous instance of this device
  775. // in the device list...In some cases, multiple
  776. // messages are received for the same device. We
  777. // obviously only want one instance of the device
  778. // showing up in the dialog box.
  779. //
  780. if (!IsListEmpty(&PhysicalDeviceList))
  781. {
  782. currNode = (PDEVICE_LIST_NODE) GetListHead(&PhysicalDeviceList);
  783. lastNode = (PDEVICE_LIST_NODE) GetListTail(&PhysicalDeviceList);
  784. //
  785. // This loop should always terminate since the device
  786. // handle should be somewhere in the physical device list
  787. //
  788. while (1)
  789. {
  790. if (0 == strcmp(currNode -> HidDeviceInfo.DevicePath,
  791. pbroadcastInterface -> dbcc_name))
  792. {
  793. return (TRUE);
  794. }
  795. if (currNode == lastNode)
  796. {
  797. break;
  798. }
  799. currNode = (PDEVICE_LIST_NODE) GetNextEntry(currNode);
  800. }
  801. }
  802. //
  803. // In this structure, we are given the name of the device
  804. // to open. So all that needs to be done is open
  805. // a new hid device with the string
  806. //
  807. listNode = (PDEVICE_LIST_NODE) malloc(sizeof(DEVICE_LIST_NODE));
  808. if (NULL == listNode)
  809. {
  810. MessageBox(hDlg,
  811. "Error -- Couldn't allocate memory for new device list node",
  812. HCLIENT_ERROR,
  813. MB_ICONEXCLAMATION | MB_OK | MB_TASKMODAL);
  814. break;
  815. }
  816. //
  817. // Open the hid device for query access
  818. //
  819. if (!OpenHidDevice (pbroadcastInterface -> dbcc_name,
  820. FALSE,
  821. FALSE,
  822. FALSE,
  823. FALSE,
  824. &(listNode -> HidDeviceInfo)))
  825. {
  826. MessageBox(hDlg,
  827. "Error -- Couldn't open HID device",
  828. HCLIENT_ERROR,
  829. MB_ICONEXCLAMATION | MB_OK | MB_TASKMODAL);
  830. free(listNode);
  831. break;
  832. }
  833. if (!RegisterHidDevice(hDlg, listNode))
  834. {
  835. MessageBox(hDlg,
  836. "Error -- Couldn't register handle notification",
  837. HCLIENT_ERROR,
  838. MB_ICONEXCLAMATION | MB_OK | MB_TASKMODAL);
  839. CloseHidDevice(&(listNode -> HidDeviceInfo));
  840. free(listNode);
  841. break;
  842. }
  843. listNode -> DeviceOpened = TRUE;
  844. InsertTail(&PhysicalDeviceList, listNode);
  845. vLoadDevices(GetDlgItem(hDlg,IDC_DEVICES));
  846. PostMessage(hDlg,
  847. WM_COMMAND,
  848. IDC_DEVICES + (CBN_SELCHANGE << 16),
  849. (LPARAM) GetDlgItem(hDlg,IDC_DEVICES));
  850. }
  851. break;
  852. case DBT_DEVICEQUERYREMOVE:
  853. //
  854. // If this message is received, the device is either
  855. // being disabled or removed through device manager.
  856. // To properly handle this request, we need to close
  857. // the handle to the device.
  858. //
  859. broadcastHdr = (PDEV_BROADCAST_HDR) lParam;
  860. if (DBT_DEVTYP_HANDLE == broadcastHdr -> dbch_devicetype)
  861. {
  862. PDEV_BROADCAST_HANDLE broadcastHandle;
  863. PDEVICE_LIST_NODE currNode;
  864. HANDLE deviceHandle;
  865. broadcastHandle = (PDEV_BROADCAST_HANDLE) lParam;
  866. //
  867. // Get the file handle of the device that was removed
  868. // from the system
  869. //
  870. deviceHandle = (HANDLE) broadcastHandle -> dbch_handle;
  871. //
  872. // Search the physical device list for the handle that
  873. // was removed...
  874. //
  875. currNode = (PDEVICE_LIST_NODE) GetListHead(&PhysicalDeviceList);
  876. //
  877. // This loop should always terminate since the device
  878. // handle should be somewhere in the physical device list
  879. //
  880. while (currNode -> HidDeviceInfo.HidDevice != deviceHandle)
  881. {
  882. currNode = (PDEVICE_LIST_NODE) GetNextEntry(currNode);
  883. }
  884. CloseHidDevice(&(currNode -> HidDeviceInfo));
  885. currNode -> DeviceOpened = FALSE;
  886. }
  887. return (TRUE);
  888. case DBT_DEVICEREMOVEPENDING:
  889. case DBT_DEVICEREMOVECOMPLETE:
  890. //
  891. // Do the same steps for DBT_DEVICEREMOVEPENDING and
  892. // DBT_DEVICEREMOVECOMPLETE. We do not receive the
  893. // remove complete request for a device if it is
  894. // disabled or removed via Device Manager. However,
  895. // in that case will receive the remove pending.
  896. // We remove the device from our currently displayed
  897. // list of devices and unregister notification.
  898. //
  899. broadcastHdr = (PDEV_BROADCAST_HDR) lParam;
  900. if (DBT_DEVTYP_HANDLE == broadcastHdr -> dbch_devicetype)
  901. {
  902. PDEV_BROADCAST_HANDLE broadcastHandle;
  903. PDEVICE_LIST_NODE currNode;
  904. HANDLE deviceHandle;
  905. broadcastHandle = (PDEV_BROADCAST_HANDLE) lParam;
  906. //
  907. // Get the file handle of the device that was removed
  908. // from the system
  909. //
  910. deviceHandle = (HANDLE) broadcastHandle -> dbch_handle;
  911. //
  912. // Search the physical device list for the handle that
  913. // was removed...
  914. //
  915. currNode = (PDEVICE_LIST_NODE) GetListHead(&PhysicalDeviceList);
  916. //
  917. // This loop should always terminate since the device
  918. // handle should be somewhere in the physical device list
  919. //
  920. while (currNode -> HidDeviceInfo.HidDevice != deviceHandle)
  921. {
  922. currNode = (PDEVICE_LIST_NODE) GetNextEntry(currNode);
  923. }
  924. //
  925. // Node in PhysicalDeviceList has been found, do:
  926. // 1) Unregister notification
  927. // 2) Close the hid device
  928. // 3) Remove the entry from the list
  929. // 4) Free the memory for the entry
  930. //
  931. //
  932. PostMessage(hDlg,
  933. WM_UNREGISTER_HANDLE,
  934. 0,
  935. (LPARAM) currNode -> NotificationHandle);
  936. //
  937. // Close the device if still opened...This would
  938. // occur on surprise removal.
  939. //
  940. if (currNode -> DeviceOpened)
  941. {
  942. CloseHidDevice(&(currNode -> HidDeviceInfo));
  943. }
  944. RemoveNode(currNode);
  945. free(currNode);
  946. //
  947. // Reload the device list
  948. //
  949. vLoadDevices(GetDlgItem(hDlg,IDC_DEVICES));
  950. PostMessage(hDlg,
  951. WM_COMMAND,
  952. IDC_DEVICES + (CBN_SELCHANGE << 16),
  953. (LPARAM) GetDlgItem(hDlg,IDC_DEVICES));
  954. }
  955. break;
  956. default:
  957. break;
  958. }
  959. break;
  960. //
  961. // Application specific message used to defer the unregistering of a
  962. // file object for device change notification. This separte message
  963. // is sent when a WM_DEVICECHANGE (DBT_DEVICEREMOVECOMPLETE) has been
  964. // received. The Unregistering of the notification must be deferred
  965. // until after the WM_DEVICECHANGE message has been processed or the
  966. // system will deadlock. The handle that is to be freed will be passed
  967. // in as lParam for this message
  968. //
  969. case WM_UNREGISTER_HANDLE:
  970. UnregisterDeviceNotification ( (HDEVNOTIFY) lParam );
  971. break;
  972. } // end switch message
  973. return FALSE;
  974. } // end MainDlgProc
  975. BOOL
  976. bParseData(
  977. PHID_DATA pData,
  978. rWriteDataStruct rWriteData[],
  979. int iCount,
  980. int *piErrorLine
  981. )
  982. {
  983. INT iCap;
  984. PHID_DATA pWalk;
  985. BOOL noError = TRUE;
  986. pWalk = pData;
  987. for (iCap = 0; (iCap < iCount) && noError; iCap++)
  988. {
  989. //
  990. // Check to see if our data is a value cap or not
  991. //
  992. if (!pWalk->IsButtonData)
  993. {
  994. pWalk -> ValueData.Value = atol(rWriteData[iCap].szValue);
  995. }
  996. else
  997. {
  998. if (!bSetButtonUsages(pWalk, rWriteData[iCap].szValue) )
  999. {
  1000. *piErrorLine = iCap;
  1001. noError = FALSE;
  1002. }
  1003. }
  1004. pWalk++;
  1005. }
  1006. return (noError);
  1007. }
  1008. BOOL
  1009. bSetButtonUsages(
  1010. PHID_DATA pCap,
  1011. PCHAR pszInputString
  1012. )
  1013. {
  1014. CHAR szTempString[SMALL_BUFF];
  1015. CHAR pszDelimiter[] = " ";
  1016. PCHAR pszToken;
  1017. INT iLoop;
  1018. PUSAGE pUsageWalk;
  1019. BOOL bNoError=TRUE;
  1020. HRESULT stringReturn;
  1021. stringReturn = StringCbCopy(szTempString, SMALL_BUFF, pszInputString);
  1022. pszToken = strtok(szTempString, pszDelimiter);
  1023. pUsageWalk = pCap -> ButtonData.Usages;
  1024. memset(pUsageWalk, 0, pCap->ButtonData.MaxUsageLength * sizeof(USAGE));
  1025. for (iLoop = 0; ((ULONG) iLoop < pCap->ButtonData.MaxUsageLength) && (pszToken != NULL) && bNoError; iLoop++)
  1026. {
  1027. *pUsageWalk = (USAGE) atoi(pszToken);
  1028. pszToken = strtok(NULL, pszDelimiter);
  1029. pUsageWalk++;
  1030. }
  1031. return bNoError;
  1032. } //end function bSetButtonUsages//
  1033. INT
  1034. iPrepareDataFields(
  1035. PHID_DATA pData,
  1036. ULONG ulDataLength,
  1037. rWriteDataStruct rWriteData[],
  1038. int iMaxElements
  1039. )
  1040. {
  1041. INT i;
  1042. PHID_DATA pWalk;
  1043. HRESULT stringReturn;
  1044. pWalk = pData;
  1045. for (i = 0; (i < iMaxElements) && ((unsigned) i < ulDataLength); i++)
  1046. {
  1047. if (!pWalk->IsButtonData)
  1048. {
  1049. stringReturn = StringCbPrintf(rWriteData[i].szLabel,
  1050. MAX_LABEL,
  1051. "ValueCap; ReportID: 0x%x, UsagePage=0x%x, Usage=0x%x",
  1052. pWalk->ReportID,
  1053. pWalk->UsagePage,
  1054. pWalk->ValueData.Usage);
  1055. }
  1056. else
  1057. {
  1058. stringReturn = StringCbPrintf(rWriteData[i].szLabel,
  1059. MAX_LABEL,
  1060. "Button; ReportID: 0x%x, UsagePage=0x%x, UsageMin: 0x%x, UsageMax: 0x%x",
  1061. pWalk->ReportID,
  1062. pWalk->UsagePage,
  1063. pWalk->ButtonData.UsageMin,
  1064. pWalk->ButtonData.UsageMax);
  1065. }
  1066. pWalk++;
  1067. }
  1068. return i;
  1069. } //end function iPrepareDataFields//
  1070. INT_PTR CALLBACK
  1071. bReadDlgProc(
  1072. HWND hDlg,
  1073. UINT message,
  1074. WPARAM wParam,
  1075. LPARAM lParam
  1076. )
  1077. {
  1078. static INT iLbCounter;
  1079. static CHAR szTempBuff[1024];
  1080. static READ_THREAD_CONTEXT readContext;
  1081. static HANDLE readThread;
  1082. static HID_DEVICE syncDevice;
  1083. static HID_DEVICE asyncDevice;
  1084. static BOOL doAsyncReads;
  1085. static BOOL doSyncReads;
  1086. PHID_DEVICE pDevice;
  1087. DWORD threadID;
  1088. INT iIndex;
  1089. PHID_DATA pData;
  1090. UINT uLoop;
  1091. switch(message)
  1092. {
  1093. case WM_INITDIALOG:
  1094. //
  1095. // Initialize the list box counter, the readThread, and the
  1096. // readContext.DisplayEvent.
  1097. //
  1098. iLbCounter = 0;
  1099. readThread = NULL;
  1100. readContext.DisplayEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  1101. if (NULL == readContext.DisplayEvent)
  1102. {
  1103. EndDialog(hDlg, 0);
  1104. }
  1105. //
  1106. // Get the opened device information for the device to perform
  1107. // reads upon
  1108. //
  1109. pDevice = (PHID_DEVICE) lParam;
  1110. //
  1111. // To do sync and async reads requires file handles with different
  1112. // attributes (ie. an async must be opened with the OVERLAPPED flag
  1113. // set). The device node that was passed in the context parameter
  1114. // was not opened for reading. Therefore, two more devices will
  1115. // be opened, one for async reads and one for sync reads.
  1116. //
  1117. doSyncReads = OpenHidDevice(pDevice -> DevicePath,
  1118. TRUE,
  1119. FALSE,
  1120. FALSE,
  1121. FALSE,
  1122. &syncDevice);
  1123. if (!doSyncReads)
  1124. {
  1125. MessageBox(hDlg,
  1126. "Unable to open device for synchronous reading",
  1127. HCLIENT_ERROR,
  1128. MB_ICONEXCLAMATION);
  1129. }
  1130. //
  1131. // For asynchronous read, default to using the same information
  1132. // passed in as the lParam. This is because data related to
  1133. // Ppd and such cannot be retrieved using the standard HidD_
  1134. // functions. However, it is necessary to parse future reports.
  1135. //
  1136. doAsyncReads = OpenHidDevice(pDevice -> DevicePath,
  1137. TRUE,
  1138. FALSE,
  1139. TRUE,
  1140. FALSE,
  1141. &asyncDevice);
  1142. if (!doAsyncReads)
  1143. {
  1144. MessageBox(hDlg,
  1145. "Unable to open device for asynchronous reading",
  1146. HCLIENT_ERROR,
  1147. MB_ICONEXCLAMATION);
  1148. }
  1149. PostMessage(hDlg, WM_READ_DONE, 0, 0);
  1150. break;
  1151. case WM_DISPLAY_READ_DATA:
  1152. //
  1153. // LParam is the device that was read from
  1154. //
  1155. pDevice = (PHID_DEVICE) lParam;
  1156. //
  1157. // Display all the data stored in the Input data field for the device
  1158. //
  1159. pData = pDevice -> InputData;
  1160. SendDlgItemMessage(hDlg,
  1161. IDC_OUTPUT,
  1162. LB_ADDSTRING,
  1163. 0,
  1164. (LPARAM)"-------------------------------------------");
  1165. iLbCounter++;
  1166. if (iLbCounter > MAX_LB_ITEMS)
  1167. {
  1168. SendDlgItemMessage(hDlg,
  1169. IDC_OUTPUT,
  1170. LB_DELETESTRING,
  1171. 0,
  1172. 0);
  1173. }
  1174. for (uLoop = 0; uLoop < pDevice->InputDataLength; uLoop++)
  1175. {
  1176. ReportToString(pData, szTempBuff, sizeof(szTempBuff));
  1177. iIndex = (INT) SendDlgItemMessage(hDlg,
  1178. IDC_OUTPUT,
  1179. LB_ADDSTRING,
  1180. 0,
  1181. (LPARAM) szTempBuff);
  1182. SendDlgItemMessage(hDlg,
  1183. IDC_OUTPUT,
  1184. LB_SETCURSEL,
  1185. iIndex,
  1186. 0);
  1187. iLbCounter++;
  1188. if (iLbCounter > MAX_LB_ITEMS)
  1189. {
  1190. SendDlgItemMessage(hDlg,
  1191. IDC_OUTPUT,
  1192. LB_DELETESTRING,
  1193. 0,
  1194. 0);
  1195. }
  1196. pData++;
  1197. }
  1198. SetEvent( readContext.DisplayEvent );
  1199. break;
  1200. case WM_READ_DONE:
  1201. EnableWindow(GetDlgItem(hDlg, IDOK), TRUE);
  1202. EnableWindow(GetDlgItem(hDlg, IDC_READ_SYNCH), doSyncReads);
  1203. EnableWindow(GetDlgItem(hDlg, IDC_READ_ASYNCH_ONCE), doAsyncReads);
  1204. EnableWindow(GetDlgItem(hDlg, IDC_READ_ASYNCH_CONT), doAsyncReads);
  1205. SetWindowText(GetDlgItem(hDlg, IDC_READ_ASYNCH_ONCE),
  1206. "One Asynchronous Read");
  1207. SetWindowText(GetDlgItem(hDlg, IDC_READ_ASYNCH_CONT),
  1208. "Continuous Asynchronous Read");
  1209. readThread = NULL;
  1210. break;
  1211. case WM_COMMAND:
  1212. switch(LOWORD(wParam))
  1213. {
  1214. case IDC_READ_SYNCH:
  1215. EnableWindow(GetDlgItem(hDlg, IDOK), FALSE);
  1216. EnableWindow(GetDlgItem(hDlg, IDC_READ_SYNCH), FALSE);
  1217. EnableWindow(GetDlgItem(hDlg, IDC_READ_ASYNCH_ONCE), FALSE);
  1218. EnableWindow(GetDlgItem(hDlg, IDC_READ_ASYNCH_CONT), FALSE);
  1219. Read(&syncDevice);
  1220. PostMessage(hDlg, WM_DISPLAY_READ_DATA, 0, (LPARAM) &syncDevice);
  1221. PostMessage(hDlg, WM_READ_DONE, 0, 0);
  1222. break;
  1223. case IDC_READ_ASYNCH_ONCE:
  1224. case IDC_READ_ASYNCH_CONT:
  1225. //
  1226. // When these buttons are pushed there are two options:
  1227. // 1) Start a new asynch read thread (readThread == NULL)
  1228. // 2) Stop a previous asych read thread
  1229. //
  1230. if (NULL == readThread)
  1231. {
  1232. //
  1233. // Start a new read thread
  1234. //
  1235. readContext.HidDevice = &asyncDevice;
  1236. readContext.TerminateThread = FALSE;
  1237. readContext.DoOneRead = (IDC_READ_ASYNCH_ONCE == LOWORD(wParam));
  1238. readContext.DisplayWindow = hDlg;
  1239. readThread = CreateThread( NULL,
  1240. 0,
  1241. AsynchReadThreadProc,
  1242. &readContext,
  1243. 0,
  1244. &threadID);
  1245. if (NULL == readThread)
  1246. {
  1247. MessageBox(hDlg,
  1248. "Unable to create read thread",
  1249. HCLIENT_ERROR,
  1250. MB_ICONEXCLAMATION);
  1251. }
  1252. else
  1253. {
  1254. EnableWindow(GetDlgItem(hDlg, IDOK), FALSE);
  1255. EnableWindow(GetDlgItem(hDlg, IDC_READ_SYNCH), FALSE);
  1256. EnableWindow(GetDlgItem(hDlg, IDC_READ_ASYNCH_ONCE),
  1257. IDC_READ_ASYNCH_ONCE == LOWORD(wParam));
  1258. EnableWindow(GetDlgItem(hDlg, IDC_READ_ASYNCH_CONT),
  1259. IDC_READ_ASYNCH_CONT == LOWORD(wParam));
  1260. SetWindowText(GetDlgItem(hDlg, LOWORD(wParam)),
  1261. "Stop Asynchronous Read");
  1262. }
  1263. }
  1264. else
  1265. {
  1266. //
  1267. // Signal the terminate thread variable and
  1268. // wait for the read thread to complete.
  1269. //
  1270. readContext.TerminateThread = TRUE;
  1271. WaitForSingleObject(readThread, INFINITE);
  1272. }
  1273. break;
  1274. case IDCANCEL:
  1275. readContext.TerminateThread = TRUE;
  1276. WaitForSingleObject(readThread, INFINITE);
  1277. //Fall through!!!
  1278. case IDOK:
  1279. CloseHidDevice(&asyncDevice);
  1280. EndDialog(hDlg,0);
  1281. break;
  1282. }
  1283. break;
  1284. } // end switch message
  1285. return FALSE;
  1286. } // end bReadDlgProc
  1287. VOID
  1288. ReportToString(
  1289. PHID_DATA pData,
  1290. PCHAR szBuff,
  1291. UINT iBuffSize
  1292. )
  1293. {
  1294. PCHAR pszWalk;
  1295. PUSAGE pUsage;
  1296. ULONG i;
  1297. UINT iRemainingBuffer;
  1298. UINT iStringLength;
  1299. HRESULT stringReturn;
  1300. //
  1301. // For button data, all the usages in the usage list are to be displayed
  1302. //
  1303. if (pData -> IsButtonData)
  1304. {
  1305. stringReturn = StringCbPrintf (szBuff,
  1306. iBuffSize,
  1307. "Usage Page: 0x%x, Usages: ",
  1308. pData -> UsagePage);
  1309. iRemainingBuffer = 0;
  1310. iStringLength = strlen(szBuff);
  1311. pszWalk = szBuff + iStringLength;
  1312. if (iStringLength < iBuffSize)
  1313. {
  1314. iRemainingBuffer = iBuffSize - iStringLength;
  1315. }
  1316. for (i = 0, pUsage = pData -> ButtonData.Usages;
  1317. i < pData -> ButtonData.MaxUsageLength;
  1318. i++, pUsage++)
  1319. {
  1320. if (0 == *pUsage)
  1321. {
  1322. break; // A usage of zero is a non button.
  1323. }
  1324. stringReturn = StringCbPrintf (pszWalk, iRemainingBuffer, " 0x%x", *pUsage);
  1325. iRemainingBuffer -= strlen(pszWalk);
  1326. pszWalk += strlen(pszWalk);
  1327. }
  1328. }
  1329. else
  1330. {
  1331. stringReturn = StringCbPrintf (szBuff,
  1332. iBuffSize,
  1333. "Usage Page: 0x%x, Usage: 0x%x, Scaled: %d Value: %d",
  1334. pData->UsagePage,
  1335. pData->ValueData.Usage,
  1336. pData->ValueData.ScaledValue,
  1337. pData->ValueData.Value);
  1338. }
  1339. }
  1340. INT_PTR CALLBACK
  1341. bFeatureDlgProc(
  1342. HWND hDlg,
  1343. UINT message,
  1344. WPARAM wParam,
  1345. LPARAM lParam
  1346. )
  1347. {
  1348. static PHID_DEVICE pDevice;
  1349. static INT iLbCounter;
  1350. static rWriteDataStruct rWriteData[MAX_WRITE_ELEMENTS];
  1351. static CHAR szTempBuff[1024];
  1352. INT iIndex;
  1353. INT iCount;
  1354. INT iErrorLine;
  1355. PHID_DATA pData;
  1356. UINT uLoop;
  1357. HRESULT stringReturn;
  1358. switch(message)
  1359. {
  1360. case WM_INITDIALOG:
  1361. iLbCounter = 0;
  1362. pDevice = (PHID_DEVICE) lParam;
  1363. break;
  1364. case WM_COMMAND:
  1365. switch(LOWORD(wParam))
  1366. {
  1367. case IDC_READ:
  1368. GetFeature(pDevice);
  1369. pData = pDevice -> FeatureData;
  1370. SendDlgItemMessage(hDlg,
  1371. IDC_OUTPUT,
  1372. LB_ADDSTRING,
  1373. 0,
  1374. (LPARAM)"------------ Read Features ---------------");
  1375. iLbCounter++;
  1376. if (iLbCounter > MAX_LB_ITEMS)
  1377. {
  1378. SendDlgItemMessage(hDlg,
  1379. IDC_OUTPUT,
  1380. LB_DELETESTRING,
  1381. 0,
  1382. 0);
  1383. }
  1384. for (uLoop = 0; uLoop < pDevice -> FeatureDataLength; uLoop++)
  1385. {
  1386. ReportToString(pData, szTempBuff, sizeof(szTempBuff));
  1387. iIndex = (INT) SendDlgItemMessage(hDlg,
  1388. IDC_OUTPUT,
  1389. LB_ADDSTRING,
  1390. 0,
  1391. (LPARAM) szTempBuff);
  1392. SendDlgItemMessage(hDlg,
  1393. IDC_OUTPUT,
  1394. LB_SETCURSEL,
  1395. iIndex,
  1396. (LPARAM) 0);
  1397. iLbCounter++;
  1398. if (iLbCounter > MAX_LB_ITEMS)
  1399. {
  1400. SendDlgItemMessage(hDlg,
  1401. IDC_OUTPUT,
  1402. LB_DELETESTRING,
  1403. 0,
  1404. 0);
  1405. }
  1406. pData++;
  1407. }
  1408. break;
  1409. case IDC_WRITE:
  1410. iCount = iPrepareDataFields(pDevice -> FeatureData,
  1411. pDevice -> FeatureDataLength,
  1412. rWriteData,
  1413. MAX_OUTPUT_ELEMENTS);
  1414. if (bGetData(rWriteData, iCount, hDlg, "WRITEFEATURE"))
  1415. {
  1416. if (!bParseData(pDevice -> FeatureData, rWriteData,iCount, &iErrorLine))
  1417. {
  1418. stringReturn = StringCbPrintf(szTempBuff,
  1419. sizeof(szTempBuff),
  1420. "Unable to parse line %x of output data",
  1421. iErrorLine);
  1422. MessageBox(hDlg,
  1423. szTempBuff,
  1424. HCLIENT_ERROR,
  1425. MB_ICONEXCLAMATION);
  1426. }
  1427. else
  1428. {
  1429. if ( SetFeature(pDevice) )
  1430. {
  1431. SendDlgItemMessage(hDlg,
  1432. IDC_OUTPUT,
  1433. LB_ADDSTRING,
  1434. 0,
  1435. (LPARAM)"------------ Write Feature ---------------");
  1436. }
  1437. else
  1438. {
  1439. SendDlgItemMessage(hDlg,
  1440. IDC_OUTPUT,
  1441. LB_ADDSTRING,
  1442. 0,
  1443. (LPARAM)"------------ Write Feature Error ---------------");
  1444. }
  1445. }
  1446. }
  1447. break;
  1448. case IDOK:
  1449. case IDCANCEL:
  1450. EndDialog(hDlg,0);
  1451. break;
  1452. }
  1453. break;
  1454. } //end switch message//
  1455. return FALSE;
  1456. } //end bReadDlgProc//
  1457. VOID
  1458. vDisplayDeviceCaps(
  1459. IN PHIDP_CAPS pCaps,
  1460. IN HWND hControl
  1461. )
  1462. {
  1463. static CHAR szTempBuff[SMALL_BUFF];
  1464. HRESULT stringReturn;
  1465. SendMessage(hControl, LB_RESETCONTENT, 0, 0);
  1466. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Usage Page: 0x%x", pCaps -> UsagePage);
  1467. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1468. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Usage: 0x%x",pCaps -> Usage);
  1469. SendMessage(hControl,LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1470. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Input report byte length: %d",pCaps -> InputReportByteLength);
  1471. SendMessage(hControl,LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1472. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Output report byte length: %d",pCaps -> OutputReportByteLength);
  1473. SendMessage(hControl,LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1474. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Feature report byte length: %d",pCaps -> FeatureReportByteLength);
  1475. SendMessage(hControl,LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1476. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Number of collection nodes %d: ", pCaps -> NumberLinkCollectionNodes);
  1477. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1478. return;
  1479. }
  1480. VOID
  1481. vDisplayDeviceAttributes(
  1482. PHIDD_ATTRIBUTES pAttrib,
  1483. HWND hControl
  1484. )
  1485. {
  1486. static CHAR szTempBuff[SMALL_BUFF];
  1487. HRESULT stringReturn;
  1488. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Vendor ID: 0x%x", pAttrib -> VendorID);
  1489. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1490. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Product ID: 0x%x", pAttrib -> ProductID);
  1491. SendMessage(hControl, LB_ADDSTRING, 0,(LPARAM) szTempBuff);
  1492. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Version Number 0x%x", pAttrib -> VersionNumber);
  1493. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1494. return;
  1495. }
  1496. VOID
  1497. vDisplayDataAttributes(
  1498. PHIDP_DATA pData,
  1499. BOOL IsButton,
  1500. HWND hControl
  1501. )
  1502. {
  1503. static CHAR szTempBuff[SMALL_BUFF];
  1504. HRESULT stringReturn;
  1505. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) "================");
  1506. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Index: 0x%x", pData -> DataIndex);
  1507. SendMessage(hControl,LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1508. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "IsButton: %s", IsButton ? "TRUE" : "FALSE");
  1509. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1510. if (IsButton)
  1511. {
  1512. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Button pressed: %s", pData -> On ? "TRUE" : "FALSE");
  1513. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1514. }
  1515. else
  1516. {
  1517. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Data value: 0x%x", pData -> RawValue);
  1518. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1519. }
  1520. }
  1521. VOID
  1522. vDisplayButtonAttributes(
  1523. IN PHIDP_BUTTON_CAPS pButton,
  1524. IN HWND hControl
  1525. )
  1526. {
  1527. static CHAR szTempBuff[SMALL_BUFF];
  1528. HRESULT stringReturn;
  1529. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Report ID: 0x%x", pButton->ReportID);
  1530. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1531. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Usage Page: 0x%x", pButton->UsagePage);
  1532. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1533. stringReturn = StringCbPrintf(szTempBuff,
  1534. SMALL_BUFF,
  1535. "Alias: %s",
  1536. pButton -> IsAlias ? "TRUE" : "FALSE");
  1537. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1538. stringReturn = StringCbPrintf(szTempBuff,
  1539. SMALL_BUFF,
  1540. "Link Collection: %hu",
  1541. pButton -> LinkCollection);
  1542. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1543. stringReturn = StringCbPrintf(szTempBuff,
  1544. SMALL_BUFF,
  1545. "Link Usage Page: 0x%x",
  1546. pButton -> LinkUsagePage);
  1547. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1548. stringReturn = StringCbPrintf(szTempBuff,
  1549. SMALL_BUFF,
  1550. "Link Usage: 0x%x",
  1551. pButton -> LinkUsage);
  1552. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1553. if (pButton->IsRange)
  1554. {
  1555. stringReturn = StringCbPrintf(szTempBuff,
  1556. SMALL_BUFF,
  1557. "Usage Min: 0x%x, Usage Max: 0x%x",
  1558. pButton->Range.UsageMin,
  1559. pButton->Range.UsageMax);
  1560. }
  1561. else
  1562. {
  1563. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Usage: 0x%x",pButton->NotRange.Usage);
  1564. }
  1565. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1566. if (pButton->IsRange)
  1567. {
  1568. stringReturn = StringCbPrintf(szTempBuff,
  1569. SMALL_BUFF,
  1570. "Data Index Min: 0x%x, Data Index Max: 0x%x",
  1571. pButton->Range.DataIndexMin,
  1572. pButton->Range.DataIndexMax);
  1573. }
  1574. else
  1575. {
  1576. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "DataIndex: 0x%x",pButton->NotRange.DataIndex);
  1577. }
  1578. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1579. if (pButton->IsStringRange)
  1580. {
  1581. stringReturn = StringCbPrintf(szTempBuff,
  1582. SMALL_BUFF,
  1583. "String Min: 0x%x, String Max: 0x%x",
  1584. pButton->Range.StringMin,
  1585. pButton->Range.StringMax);
  1586. }
  1587. else
  1588. {
  1589. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "String Index: 0x%x",pButton->NotRange.StringIndex);
  1590. }
  1591. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1592. if (pButton->IsDesignatorRange)
  1593. {
  1594. stringReturn = StringCbPrintf(szTempBuff,
  1595. SMALL_BUFF,
  1596. "Designator Min: 0x%x, Designator Max: 0x%x",
  1597. pButton->Range.DesignatorMin,
  1598. pButton->Range.DesignatorMax);
  1599. }
  1600. else
  1601. {
  1602. stringReturn = StringCbPrintf(szTempBuff,
  1603. SMALL_BUFF,
  1604. "Designator Index: 0x%x",
  1605. pButton->NotRange.DesignatorIndex);
  1606. }
  1607. SendMessage(hControl, LB_ADDSTRING, 0,(LPARAM) szTempBuff);
  1608. if (pButton->IsAbsolute)
  1609. {
  1610. SendMessage(hControl, LB_ADDSTRING,0, (LPARAM) "Absolute: Yes");
  1611. }
  1612. else
  1613. {
  1614. SendMessage(hControl, LB_ADDSTRING,0, (LPARAM) "Absolute: No");
  1615. }
  1616. return;
  1617. }
  1618. VOID
  1619. vDisplayValueAttributes(
  1620. IN PHIDP_VALUE_CAPS pValue,
  1621. HWND hControl
  1622. )
  1623. {
  1624. static CHAR szTempBuff[SMALL_BUFF];
  1625. HRESULT stringReturn;
  1626. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Report ID 0x%x", pValue->ReportID);
  1627. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1628. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Usage Page: 0x%x", pValue->UsagePage);
  1629. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1630. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Bit size: 0x%x", pValue->BitSize);
  1631. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1632. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Report Count: 0x%x", pValue->ReportCount);
  1633. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1634. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Unit Exponent: 0x%x", pValue->UnitsExp);
  1635. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1636. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Has Null: 0x%x", pValue->HasNull);
  1637. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1638. if (pValue->IsAlias)
  1639. {
  1640. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Alias");
  1641. }
  1642. else
  1643. {
  1644. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "=====");
  1645. }
  1646. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1647. if (pValue->IsRange)
  1648. {
  1649. stringReturn = StringCbPrintf(szTempBuff,
  1650. SMALL_BUFF,
  1651. "Usage Min: 0x%x, Usage Max 0x%x",
  1652. pValue->Range.UsageMin,
  1653. pValue->Range.UsageMax);
  1654. }
  1655. else
  1656. {
  1657. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Usage: 0x%x", pValue -> NotRange.Usage);
  1658. }
  1659. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1660. if (pValue->IsRange)
  1661. {
  1662. stringReturn = StringCbPrintf(szTempBuff,
  1663. SMALL_BUFF,
  1664. "Data Index Min: 0x%x, Data Index Max: 0x%x",
  1665. pValue->Range.DataIndexMin,
  1666. pValue->Range.DataIndexMax);
  1667. }
  1668. else
  1669. {
  1670. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "DataIndex: 0x%x", pValue->NotRange.DataIndex);
  1671. }
  1672. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1673. stringReturn = StringCbPrintf(szTempBuff,
  1674. SMALL_BUFF,
  1675. "Physical Minimum: %d, Physical Maximum: %d",
  1676. pValue->PhysicalMin,
  1677. pValue->PhysicalMax);
  1678. SendMessage(hControl, LB_ADDSTRING, 0,(LPARAM) szTempBuff);
  1679. stringReturn = StringCbPrintf(szTempBuff,
  1680. SMALL_BUFF,
  1681. "Logical Minimum: 0x%x, Logical Maximum: 0x%x",
  1682. pValue->LogicalMin,
  1683. pValue->LogicalMax);
  1684. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1685. if (pValue->IsStringRange)
  1686. {
  1687. stringReturn = StringCbPrintf(szTempBuff,
  1688. SMALL_BUFF,
  1689. "String Min: 0x%x String Max 0x%x",
  1690. pValue->Range.StringMin,
  1691. pValue->Range.StringMax);
  1692. }
  1693. else
  1694. {
  1695. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "String Index: 0x%x",pValue->NotRange.StringIndex);
  1696. }
  1697. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1698. if (pValue->IsDesignatorRange)
  1699. {
  1700. stringReturn = StringCbPrintf(szTempBuff,
  1701. SMALL_BUFF,
  1702. "Designator Minimum: 0x%x, Max: 0x%x",
  1703. pValue->Range.DesignatorMin,
  1704. pValue->Range.DesignatorMax);
  1705. }
  1706. else
  1707. {
  1708. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Designator Index: 0x%x",pValue->NotRange.DesignatorIndex);
  1709. }
  1710. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1711. if (pValue->IsAbsolute)
  1712. {
  1713. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) "Absolute: Yes");
  1714. }
  1715. else
  1716. {
  1717. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) "Absolute: No");
  1718. }
  1719. return;
  1720. }
  1721. VOID
  1722. vDisplayInputButtons(
  1723. IN PHID_DEVICE pDevice,
  1724. IN HWND hControl
  1725. )
  1726. {
  1727. INT iLoop;
  1728. PHIDP_BUTTON_CAPS pButtonCaps;
  1729. static CHAR szTempBuff[SMALL_BUFF];
  1730. INT iIndex;
  1731. HRESULT stringReturn;
  1732. SendMessage(hControl, LB_RESETCONTENT, 0, (LPARAM) 0);
  1733. pButtonCaps = pDevice->InputButtonCaps;
  1734. for (iLoop = 0; iLoop < pDevice->Caps.NumberInputButtonCaps; iLoop++)
  1735. {
  1736. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Input button cap # %d", iLoop);
  1737. iIndex = (INT) SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1738. if (-1 != iIndex)
  1739. {
  1740. SendMessage(hControl, LB_SETITEMDATA, iIndex,(LPARAM) pButtonCaps);
  1741. }
  1742. pButtonCaps++;
  1743. }
  1744. SendMessage(hControl, LB_SETCURSEL, 0, 0 );
  1745. }
  1746. VOID
  1747. vDisplayOutputButtons(
  1748. IN PHID_DEVICE pDevice,
  1749. IN HWND hControl
  1750. )
  1751. {
  1752. INT iLoop;
  1753. static CHAR szTempBuff[SMALL_BUFF];
  1754. INT iIndex;
  1755. PHIDP_BUTTON_CAPS pButtonCaps;
  1756. HRESULT stringReturn;
  1757. SendMessage(hControl, LB_RESETCONTENT, 0, (LPARAM) 0);
  1758. pButtonCaps = pDevice -> OutputButtonCaps;
  1759. for (iLoop = 0; iLoop < pDevice->Caps.NumberOutputButtonCaps; iLoop++)
  1760. {
  1761. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Output button cap # %d", iLoop);
  1762. iIndex = (INT) SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1763. if (-1 != iIndex)
  1764. {
  1765. SendMessage(hControl, LB_SETITEMDATA, iIndex, (LPARAM) pButtonCaps);
  1766. }
  1767. pButtonCaps++;
  1768. }
  1769. SendMessage(hControl, LB_SETCURSEL, 0, 0);
  1770. return;
  1771. }
  1772. VOID
  1773. vDisplayInputValues(
  1774. IN PHID_DEVICE pDevice,
  1775. IN HWND hControl
  1776. )
  1777. {
  1778. INT iLoop;
  1779. static CHAR szTempBuff[SMALL_BUFF];
  1780. INT iIndex;
  1781. PHIDP_VALUE_CAPS pValueCaps;
  1782. HRESULT stringReturn;
  1783. SendMessage(hControl, LB_RESETCONTENT, 0, 0);
  1784. pValueCaps = pDevice -> InputValueCaps;
  1785. for (iLoop=0; iLoop < pDevice->Caps.NumberInputValueCaps; iLoop++)
  1786. {
  1787. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Input value cap # %d",iLoop);
  1788. iIndex = (INT) SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1789. if (-1 != iIndex)
  1790. {
  1791. SendMessage(hControl, LB_SETITEMDATA, iIndex,(LPARAM) pValueCaps);
  1792. }
  1793. pValueCaps++;
  1794. }
  1795. SendMessage(hControl, LB_SETCURSEL, 0, 0);
  1796. return;
  1797. }
  1798. VOID
  1799. vDisplayOutputValues(
  1800. IN PHID_DEVICE pDevice,
  1801. IN HWND hControl)
  1802. {
  1803. INT iLoop;
  1804. static CHAR szTempBuff[SMALL_BUFF];
  1805. INT iIndex;
  1806. PHIDP_VALUE_CAPS pValueCaps;
  1807. HRESULT stringReturn;
  1808. SendMessage(hControl, LB_RESETCONTENT, 0, 0);
  1809. pValueCaps = pDevice -> OutputValueCaps;
  1810. for (iLoop = 0; iLoop < pDevice->Caps.NumberOutputValueCaps; iLoop++)
  1811. {
  1812. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Output value cap # %d", iLoop);
  1813. iIndex = (INT) SendMessage(hControl,
  1814. LB_ADDSTRING,
  1815. 0,
  1816. (LPARAM) szTempBuff);
  1817. if (-1 != iIndex)
  1818. {
  1819. SendMessage(hControl, LB_SETITEMDATA, iIndex, (LPARAM) pValueCaps);
  1820. }
  1821. pValueCaps++;
  1822. }
  1823. SendMessage(hControl, LB_SETCURSEL, 0, 0);
  1824. return;
  1825. }
  1826. VOID
  1827. vDisplayFeatureButtons(
  1828. IN PHID_DEVICE pDevice,
  1829. IN HWND hControl
  1830. )
  1831. {
  1832. INT iLoop;
  1833. static CHAR szTempBuff[SMALL_BUFF];
  1834. INT iIndex;
  1835. PHIDP_BUTTON_CAPS pButtonCaps;
  1836. HRESULT stringReturn;
  1837. SendMessage(hControl, LB_RESETCONTENT, 0, 0);
  1838. pButtonCaps = pDevice -> FeatureButtonCaps;
  1839. for (iLoop = 0; iLoop < pDevice->Caps.NumberFeatureButtonCaps; iLoop++)
  1840. {
  1841. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Feature button cap # %d", iLoop);
  1842. iIndex = (INT) SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1843. if (-1 != iIndex)
  1844. {
  1845. SendMessage(hControl, LB_SETITEMDATA, iIndex, (LPARAM) pButtonCaps);
  1846. }
  1847. pButtonCaps++;
  1848. }
  1849. SendMessage(hControl, LB_SETCURSEL, 0, 0);
  1850. return;
  1851. }
  1852. VOID
  1853. vDisplayFeatureValues(
  1854. IN PHID_DEVICE pDevice,
  1855. IN HWND hControl
  1856. )
  1857. {
  1858. INT iLoop;
  1859. static CHAR szTempBuff[SMALL_BUFF];
  1860. INT iIndex;
  1861. PHIDP_VALUE_CAPS pValueCaps;
  1862. HRESULT stringReturn;
  1863. SendMessage(hControl, LB_RESETCONTENT, 0, 0);
  1864. pValueCaps = pDevice ->FeatureValueCaps;
  1865. for (iLoop = 0; iLoop < pDevice->Caps.NumberFeatureValueCaps; iLoop++)
  1866. {
  1867. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Feature value cap # %d", iLoop);
  1868. iIndex = (INT) SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1869. if (-1 != iIndex)
  1870. {
  1871. SendMessage(hControl, LB_SETITEMDATA, iIndex, (LPARAM) pValueCaps);
  1872. }
  1873. pValueCaps++;
  1874. }
  1875. SendMessage(hControl, LB_SETCURSEL, 0, 0);
  1876. return;
  1877. }
  1878. VOID
  1879. vLoadItemTypes(
  1880. IN HWND hItemTypes
  1881. )
  1882. {
  1883. INT iIndex;
  1884. iIndex = (INT) SendMessage(hItemTypes, CB_ADDSTRING, 0, (LPARAM) "INPUT BUTTON");
  1885. if (-1 != iIndex)
  1886. {
  1887. SendMessage(hItemTypes, CB_SETITEMDATA, iIndex, INPUT_BUTTON);
  1888. iIndex = (INT) SendMessage(hItemTypes, CB_ADDSTRING, 0 ,(LPARAM) "INPUT VALUE");
  1889. if (-1 != iIndex)
  1890. {
  1891. SendMessage(hItemTypes, CB_SETITEMDATA, iIndex, INPUT_VALUE);
  1892. }
  1893. iIndex = (INT) SendMessage(hItemTypes, CB_ADDSTRING, 0, (LPARAM) "OUTPUT BUTTON");
  1894. if (-1 != iIndex)
  1895. {
  1896. SendMessage(hItemTypes,CB_SETITEMDATA,iIndex,OUTPUT_BUTTON);
  1897. }
  1898. iIndex = (INT) SendMessage(hItemTypes, CB_ADDSTRING, 0, (LPARAM) "OUTPUT VALUE");
  1899. if (-1 != iIndex)
  1900. {
  1901. SendMessage(hItemTypes, CB_SETITEMDATA, iIndex, OUTPUT_VALUE);
  1902. }
  1903. iIndex = (INT) SendMessage(hItemTypes, CB_ADDSTRING, 0, (LPARAM) "FEATURE BUTTON");
  1904. if (-1 != iIndex)
  1905. {
  1906. SendMessage(hItemTypes, CB_SETITEMDATA, iIndex, FEATURE_BUTTON);
  1907. }
  1908. iIndex = (INT) SendMessage(hItemTypes, CB_ADDSTRING, 0, (LPARAM) "FEATURE VALUE");
  1909. if (-1 != iIndex)
  1910. {
  1911. SendMessage(hItemTypes, CB_SETITEMDATA, iIndex, FEATURE_VALUE);
  1912. }
  1913. iIndex = (INT) SendMessage(hItemTypes, CB_ADDSTRING, 0, (LPARAM) "HID CAPS");
  1914. if (-1 != iIndex )
  1915. {
  1916. SendMessage(hItemTypes, CB_SETITEMDATA, iIndex, HID_CAPS);
  1917. }
  1918. iIndex = (INT) SendMessage(hItemTypes, CB_ADDSTRING, 0, (LPARAM) "DEVICE ATTRIBUTES");
  1919. if (-1 != iIndex)
  1920. {
  1921. SendMessage(hItemTypes, CB_SETITEMDATA, iIndex, DEVICE_ATTRIBUTES);
  1922. }
  1923. SendMessage(hItemTypes, CB_SETCURSEL, 0, 0);
  1924. }
  1925. }
  1926. VOID vLoadDevices(
  1927. HWND hDeviceCombo
  1928. )
  1929. {
  1930. PDEVICE_LIST_NODE currNode;
  1931. static CHAR szTempBuff[SMALL_BUFF];
  1932. INT iIndex;
  1933. HRESULT stringReturn;
  1934. //
  1935. // Reset the content of the device list box.
  1936. //
  1937. SendMessage(hDeviceCombo, CB_RESETCONTENT, 0, 0);
  1938. if (!IsListEmpty(&PhysicalDeviceList))
  1939. {
  1940. currNode = (PDEVICE_LIST_NODE) GetListHead(&PhysicalDeviceList);
  1941. do
  1942. {
  1943. stringReturn = StringCbPrintf(szTempBuff,
  1944. SMALL_BUFF,
  1945. "Device %d, UsagePage 0%x, Usage 0%x",
  1946. HandleToULong(currNode -> HidDeviceInfo.HidDevice),
  1947. currNode -> HidDeviceInfo.Caps.UsagePage,
  1948. currNode -> HidDeviceInfo.Caps.Usage);
  1949. iIndex = (INT) SendMessage(hDeviceCombo, CB_ADDSTRING, 0, (LPARAM) szTempBuff);
  1950. if (CB_ERR != iIndex)
  1951. {
  1952. SendMessage(hDeviceCombo, CB_SETITEMDATA, iIndex, (LPARAM) &(currNode -> HidDeviceInfo));
  1953. }
  1954. currNode = (PDEVICE_LIST_NODE) GetNextEntry(currNode);
  1955. } while ((PLIST) currNode != &PhysicalDeviceList);
  1956. }
  1957. SendMessage(hDeviceCombo, CB_SETCURSEL, 0, 0);
  1958. return;
  1959. }
  1960. BOOL
  1961. bGetData(
  1962. prWriteDataStruct pItems,
  1963. INT iCount,
  1964. HWND hParent,
  1965. PCHAR pszDialogName
  1966. )
  1967. {
  1968. rGetWriteDataParams rParams;
  1969. static rWriteDataStruct arTempItems[MAX_WRITE_ELEMENTS];
  1970. INT iResult;
  1971. if (iCount > MAX_WRITE_ELEMENTS)
  1972. {
  1973. iCount = MAX_WRITE_ELEMENTS;
  1974. }
  1975. memcpy( &(arTempItems[0]), pItems, sizeof(rWriteDataStruct)*iCount);
  1976. rParams.iCount = iCount;
  1977. rParams.prItems = &(arTempItems[0]);
  1978. iResult = (INT) DialogBoxParam(hGInstance,
  1979. pszDialogName,
  1980. hParent,
  1981. bGetDataDlgProc,
  1982. (LPARAM) &rParams);
  1983. if (iResult)
  1984. {
  1985. memcpy(pItems, arTempItems, sizeof(rWriteDataStruct)*iCount);
  1986. }
  1987. return iResult;
  1988. }
  1989. INT_PTR CALLBACK
  1990. bGetDataDlgProc(
  1991. HWND hDlg,
  1992. UINT message,
  1993. WPARAM wParam,
  1994. LPARAM lParam
  1995. )
  1996. {
  1997. static prWriteDataStruct prData;
  1998. static prGetWriteDataParams pParams;
  1999. static INT iDisplayCount;
  2000. static INT iScrollRange;
  2001. static INT iCurrentScrollPos=0;
  2002. static HWND hScrollBar;
  2003. INT iTemp;
  2004. SCROLLINFO rScrollInfo;
  2005. INT iReturn;
  2006. switch(message)
  2007. {
  2008. case WM_INITDIALOG:
  2009. pParams = (prGetWriteDataParams) lParam;
  2010. prData = pParams -> prItems;
  2011. hScrollBar = GetDlgItem(hDlg, IDC_SCROLLBAR);
  2012. if (pParams -> iCount > CONTROL_COUNT)
  2013. {
  2014. iDisplayCount = CONTROL_COUNT;
  2015. iScrollRange = pParams -> iCount - CONTROL_COUNT;
  2016. rScrollInfo.fMask = SIF_RANGE | SIF_POS;
  2017. rScrollInfo.nPos = 0;
  2018. rScrollInfo.nMin = 0;
  2019. rScrollInfo.nMax = iScrollRange;
  2020. rScrollInfo.cbSize = sizeof(rScrollInfo);
  2021. rScrollInfo.nPage = CONTROL_COUNT;
  2022. iReturn = SetScrollInfo(hScrollBar,SB_CTL,&rScrollInfo,TRUE);
  2023. }
  2024. else
  2025. {
  2026. iDisplayCount=pParams->iCount;
  2027. EnableWindow(hScrollBar,FALSE);
  2028. }
  2029. vWriteDataToControls(hDlg, prData, 0, pParams->iCount);
  2030. break;
  2031. case WM_COMMAND:
  2032. switch(LOWORD(wParam))
  2033. {
  2034. case IDOK:
  2035. case ID_SEND:
  2036. vReadDataFromControls(hDlg, prData, iCurrentScrollPos, iDisplayCount);
  2037. EndDialog(hDlg,1);
  2038. break;
  2039. case IDCANCEL:
  2040. EndDialog(hDlg,0);
  2041. break;
  2042. }
  2043. break;
  2044. case WM_VSCROLL:
  2045. vReadDataFromControls(hDlg, prData, iCurrentScrollPos, iDisplayCount);
  2046. switch(LOWORD(wParam))
  2047. {
  2048. case SB_LINEDOWN:
  2049. ++iCurrentScrollPos;
  2050. break;
  2051. case SB_LINEUP:
  2052. --iCurrentScrollPos;
  2053. break;
  2054. case SB_THUMBPOSITION:
  2055. iCurrentScrollPos = HIWORD(wParam);
  2056. case SB_PAGEUP:
  2057. iCurrentScrollPos -= CONTROL_COUNT;
  2058. break;
  2059. case SB_PAGEDOWN:
  2060. iCurrentScrollPos += CONTROL_COUNT;
  2061. break;
  2062. }
  2063. if (iCurrentScrollPos < 0)
  2064. {
  2065. iCurrentScrollPos = 0;
  2066. }
  2067. if (iCurrentScrollPos > iScrollRange)
  2068. {
  2069. iCurrentScrollPos = iScrollRange;
  2070. }
  2071. SendMessage(hScrollBar, SBM_SETPOS, iCurrentScrollPos, TRUE);
  2072. iTemp = LOWORD(wParam);
  2073. if ( (iTemp == SB_LINEDOWN) || (iTemp == SB_LINEUP) || (iTemp == SB_THUMBPOSITION)|| (iTemp == SB_PAGEUP) || (iTemp==SB_PAGEDOWN) )
  2074. {
  2075. vWriteDataToControls(hDlg, prData, iCurrentScrollPos, iDisplayCount);
  2076. }
  2077. break;
  2078. }
  2079. return FALSE;
  2080. } //end function bGetDataDlgProc//
  2081. VOID
  2082. vReadDataFromControls(
  2083. HWND hDlg,
  2084. prWriteDataStruct prData,
  2085. INT iOffset,
  2086. INT iCount
  2087. )
  2088. {
  2089. INT iLoop;
  2090. INT iValueControlID = IDC_OUT_EDIT1;
  2091. prWriteDataStruct pDataWalk;
  2092. HWND hValueWnd;
  2093. pDataWalk = prData + iOffset;
  2094. for (iLoop = 0; (iLoop < iCount) && (iLoop < CONTROL_COUNT); iLoop++)
  2095. {
  2096. hValueWnd = GetDlgItem(hDlg, iValueControlID);
  2097. GetWindowText(hValueWnd, pDataWalk -> szValue, MAX_VALUE);
  2098. iValueControlID++;
  2099. pDataWalk++;
  2100. }
  2101. return;
  2102. }
  2103. VOID
  2104. vWriteDataToControls(
  2105. HWND hDlg,
  2106. prWriteDataStruct prData,
  2107. INT iOffset,
  2108. INT iCount
  2109. )
  2110. {
  2111. INT iLoop;
  2112. INT iLabelControlID = IDC_OUT_LABEL1;
  2113. INT iValueControlID = IDC_OUT_EDIT1;
  2114. HWND hLabelWnd, hValueWnd;
  2115. prWriteDataStruct pDataWalk;
  2116. pDataWalk = prData + iOffset;
  2117. for (iLoop = 0; (iLoop < iCount) && (iLoop < CONTROL_COUNT); iLoop++)
  2118. {
  2119. hLabelWnd = GetDlgItem(hDlg, iLabelControlID);
  2120. hValueWnd = GetDlgItem(hDlg, iValueControlID);
  2121. ShowWindow(hLabelWnd, SW_SHOW);
  2122. ShowWindow(hValueWnd, SW_SHOW);
  2123. SetWindowText(hLabelWnd, pDataWalk -> szLabel);
  2124. SetWindowText(hValueWnd, pDataWalk -> szValue);
  2125. iLabelControlID++;
  2126. iValueControlID++;
  2127. pDataWalk++;
  2128. }
  2129. //
  2130. // Hide the controls
  2131. //
  2132. for (; iLoop < CONTROL_COUNT; iLoop++)
  2133. {
  2134. hLabelWnd = GetDlgItem(hDlg,iLabelControlID);
  2135. hValueWnd = GetDlgItem(hDlg,iValueControlID);
  2136. ShowWindow(hLabelWnd,SW_HIDE);
  2137. ShowWindow(hValueWnd,SW_HIDE);
  2138. iLabelControlID++;
  2139. iValueControlID++;
  2140. }
  2141. }
  2142. VOID
  2143. vCreateUsageString(
  2144. IN PUSAGE pUsageList,
  2145. OUT CHAR szString[]
  2146. )
  2147. {
  2148. HRESULT stringReturn;
  2149. stringReturn = StringCbPrintf(szString,
  2150. SMALL_BUFF,
  2151. "Usage: %#04x",
  2152. *pUsageList);
  2153. return;
  2154. }
  2155. VOID
  2156. vCreateUsageAndPageString(
  2157. IN PUSAGE_AND_PAGE pUsageList,
  2158. OUT CHAR szString[]
  2159. )
  2160. {
  2161. HRESULT stringReturn;
  2162. stringReturn = StringCbPrintf(szString,
  2163. SMALL_BUFF,
  2164. "Usage Page: %#04x Usage: %#04x",
  2165. pUsageList -> UsagePage,
  2166. pUsageList -> Usage);
  2167. return;
  2168. }
  2169. VOID
  2170. vDisplayLinkCollectionNode(
  2171. IN PHIDP_LINK_COLLECTION_NODE pLCNode,
  2172. IN ULONG ulLinkIndex,
  2173. IN HWND hControl
  2174. )
  2175. {
  2176. static CHAR szTempBuff[SMALL_BUFF];
  2177. HRESULT stringReturn;
  2178. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Index: 0x%x", ulLinkIndex);
  2179. SendMessage(hControl, LB_ADDSTRING, 0, (LPARAM) szTempBuff);
  2180. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Usage Page: 0x%x", pLCNode -> LinkUsagePage);
  2181. SendMessage(hControl, LB_ADDSTRING,0, (LPARAM)szTempBuff);
  2182. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Usage: 0x%x", pLCNode -> LinkUsage);
  2183. SendMessage(hControl, LB_ADDSTRING,0, (LPARAM) szTempBuff);
  2184. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Parent Index: 0x%x", pLCNode -> Parent);
  2185. SendMessage(hControl, LB_ADDSTRING,0, (LPARAM) szTempBuff);
  2186. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Number of Children: 0x%x", pLCNode -> NumberOfChildren);
  2187. SendMessage(hControl, LB_ADDSTRING,0, (LPARAM) szTempBuff);
  2188. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "Next Sibling: 0x%x", pLCNode -> NextSibling);
  2189. SendMessage(hControl, LB_ADDSTRING,0, (LPARAM) szTempBuff);
  2190. stringReturn = StringCbPrintf(szTempBuff, SMALL_BUFF, "First Child: 0x%x", pLCNode -> FirstChild);
  2191. SendMessage(hControl, LB_ADDSTRING,0, (LPARAM) szTempBuff);
  2192. return;
  2193. }
  2194. VOID
  2195. vCreateUsageValueStringFromArray(
  2196. PCHAR pBuffer,
  2197. USHORT BitSize,
  2198. USHORT UsageIndex,
  2199. CHAR szString[]
  2200. )
  2201. /*++
  2202. Routine Description:
  2203. Given a report buffer, pBuffer, this routine extracts the given usage
  2204. at UsageIndex from the array and outputs to szString the string
  2205. representation of that value. The input parameter BitSize specifies
  2206. the number of bits representing that value in the array. This is
  2207. useful for extracting individual members of a UsageValueArray.
  2208. --*/
  2209. {
  2210. INT iByteIndex;
  2211. INT iByteOffset;
  2212. UCHAR ucLeftoverBits;
  2213. ULONG ulMask;
  2214. ULONG ulValue;
  2215. HRESULT stringReturn;
  2216. //
  2217. // Calculate the byte and byte offset into the buffer for the given
  2218. // index value
  2219. //
  2220. iByteIndex = (UsageIndex * BitSize) >> 3;
  2221. iByteOffset = (UsageIndex * BitSize) & 7;
  2222. //
  2223. // Extract the 32-bit value beginning at ByteIndex. This value
  2224. // will contain some or all of the value we are attempting to retrieve
  2225. //
  2226. ulValue = *(PULONG) (pBuffer + iByteIndex);
  2227. //
  2228. // Shift that value to the right by our byte offset..
  2229. //
  2230. ulValue = ulValue >> iByteOffset;
  2231. //
  2232. // At this point, ulValue contains the first 32-iByteOffset bits beginning
  2233. // the appropriate offset in the buffer. There are now two cases to
  2234. // look at:
  2235. //
  2236. // 1) BitSize > 32-iByteOffset -- In which case, we need to extract
  2237. // iByteOffset bits from the next
  2238. // byte in the array and OR them as
  2239. // the MSBs of ulValue
  2240. //
  2241. // 2) BitSize < 32-iByteOffset -- Need to get only the BitSize LSBs
  2242. //
  2243. //
  2244. //
  2245. // Case #1
  2246. //
  2247. if (BitSize > sizeof(ULONG)*8 - iByteOffset)
  2248. {
  2249. //
  2250. // Get the next byte of the report following the four bytes we
  2251. // retrieved earlier for ulValue
  2252. //
  2253. ucLeftoverBits = *(pBuffer+iByteIndex+4);
  2254. //
  2255. // Shift those bits to the left for anding to our previous value
  2256. //
  2257. ulMask = ucLeftoverBits << (24 + (8 - iByteOffset));
  2258. ulValue |= ulMask;
  2259. }
  2260. else if (BitSize < sizeof(ULONG)*8 - iByteOffset)
  2261. {
  2262. //
  2263. // Need to mask the most significant bits that are part of another
  2264. // value(s), not the one we are currently working with.
  2265. //
  2266. ulMask = (1 << BitSize) - 1;
  2267. ulValue &= ulMask;
  2268. }
  2269. //
  2270. // We've now got the correct value, now output to the string
  2271. //
  2272. stringReturn = StringCbPrintf(szString, SMALL_BUFF, "Usage value: %lu", ulValue);
  2273. return;
  2274. }
  2275. BOOL
  2276. RegisterHidDevice(
  2277. IN HWND WindowHandle,
  2278. IN PDEVICE_LIST_NODE DeviceNode
  2279. )
  2280. {
  2281. DEV_BROADCAST_HANDLE broadcastHandle;
  2282. broadcastHandle.dbch_size = sizeof(DEV_BROADCAST_HANDLE);
  2283. broadcastHandle.dbch_devicetype = DBT_DEVTYP_HANDLE;
  2284. broadcastHandle.dbch_handle = DeviceNode -> HidDeviceInfo.HidDevice;
  2285. DeviceNode -> NotificationHandle = RegisterDeviceNotification(
  2286. WindowHandle,
  2287. &broadcastHandle,
  2288. DEVICE_NOTIFY_WINDOW_HANDLE);
  2289. return (NULL != DeviceNode -> NotificationHandle);
  2290. }
  2291. VOID
  2292. DestroyDeviceListCallback(
  2293. PLIST_NODE_HDR ListNode
  2294. )
  2295. {
  2296. PDEVICE_LIST_NODE deviceNode;
  2297. deviceNode = (PDEVICE_LIST_NODE) ListNode;
  2298. //
  2299. // The callback function needs to do the following steps...
  2300. // 1) Close the HidDevice
  2301. // 2) Unregister device notification (if registered)
  2302. // 3) Free the allocated memory block
  2303. //
  2304. CloseHidDevice(&(deviceNode -> HidDeviceInfo));
  2305. if (NULL != deviceNode -> NotificationHandle)
  2306. {
  2307. UnregisterDeviceNotification(deviceNode -> NotificationHandle);
  2308. }
  2309. free (deviceNode);
  2310. return;
  2311. }
  2312. DWORD WINAPI
  2313. AsynchReadThreadProc(
  2314. PREAD_THREAD_CONTEXT Context
  2315. )
  2316. {
  2317. HANDLE completionEvent;
  2318. BOOL readStatus;
  2319. DWORD waitStatus;
  2320. //
  2321. // Create the completion event to send to the the OverlappedRead routine
  2322. //
  2323. completionEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  2324. //
  2325. // If NULL returned, then we cannot proceed any farther so we just exit the
  2326. // the thread
  2327. //
  2328. if (NULL == completionEvent)
  2329. {
  2330. goto AsyncRead_End;
  2331. }
  2332. //
  2333. // Now we enter the main read loop, which does the following:
  2334. // 1) Calls ReadOverlapped()
  2335. // 2) Waits for read completion with a timeout just to check if
  2336. // the main thread wants us to terminate our the read request
  2337. // 3) If the read fails, we simply break out of the loop
  2338. // and exit the thread
  2339. // 4) If the read succeeds, we call UnpackReport to get the relevant
  2340. // info and then post a message to main thread to indicate that
  2341. // there is new data to display.
  2342. // 5) We then block on the display event until the main thread says
  2343. // it has properly displayed the new data
  2344. // 6) Look to repeat this loop if we are doing more than one read
  2345. // and the main thread has yet to want us to terminate
  2346. //
  2347. do
  2348. {
  2349. //
  2350. // Call ReadOverlapped() and if the return status is TRUE, the ReadFile
  2351. // succeeded so we need to block on completionEvent, otherwise, we just
  2352. // exit
  2353. //
  2354. readStatus = ReadOverlapped( Context -> HidDevice, completionEvent );
  2355. if (!readStatus)
  2356. {
  2357. break;
  2358. }
  2359. while (!Context -> TerminateThread)
  2360. {
  2361. //
  2362. // Wait for the completion event to be signaled or a timeout
  2363. //
  2364. waitStatus = WaitForSingleObject (completionEvent, READ_THREAD_TIMEOUT );
  2365. //
  2366. // If completionEvent was signaled, then a read just completed
  2367. // so let's leave this loop and process the data
  2368. //
  2369. if ( WAIT_OBJECT_0 == waitStatus)
  2370. {
  2371. break;
  2372. }
  2373. }
  2374. //
  2375. // Check the TerminateThread again...If it is not set, then data has
  2376. // been read. In this case, we want to Unpack the report into our
  2377. // input info and then send a message to the main thread to display
  2378. // the new data.
  2379. //
  2380. if (!Context -> TerminateThread)
  2381. {
  2382. UnpackReport(Context -> HidDevice -> InputReportBuffer,
  2383. Context -> HidDevice -> Caps.InputReportByteLength,
  2384. HidP_Input,
  2385. Context -> HidDevice -> InputData,
  2386. Context -> HidDevice -> InputDataLength,
  2387. Context -> HidDevice -> Ppd);
  2388. if (NULL != Context -> DisplayEvent)
  2389. {
  2390. PostMessage(Context -> DisplayWindow,
  2391. WM_DISPLAY_READ_DATA,
  2392. 0,
  2393. (LPARAM) Context -> HidDevice);
  2394. WaitForSingleObject( Context -> DisplayEvent, INFINITE );
  2395. }
  2396. }
  2397. } while ( !Context -> TerminateThread && !Context -> DoOneRead );
  2398. AsyncRead_End:
  2399. PostMessage( Context -> DisplayWindow, WM_READ_DONE, 0, 0);
  2400. ExitThread(0);
  2401. return (0);
  2402. }
  2403. DWORD WINAPI
  2404. SynchReadThreadProc(
  2405. PREAD_THREAD_CONTEXT Context
  2406. )
  2407. {
  2408. do
  2409. {
  2410. Read(Context -> HidDevice);
  2411. UnpackReport(Context -> HidDevice -> InputReportBuffer,
  2412. Context -> HidDevice -> Caps.InputReportByteLength,
  2413. HidP_Input,
  2414. Context -> HidDevice -> InputData,
  2415. Context -> HidDevice -> InputDataLength,
  2416. Context -> HidDevice -> Ppd);
  2417. if (NULL != Context -> DisplayEvent)
  2418. {
  2419. PostMessage(Context -> DisplayWindow,
  2420. WM_DISPLAY_READ_DATA,
  2421. 0,
  2422. (LPARAM) Context -> HidDevice);
  2423. WaitForSingleObject( Context -> DisplayEvent, INFINITE );
  2424. }
  2425. } while ( !Context -> TerminateThread && !Context -> DoOneRead );
  2426. PostMessage( Context -> DisplayWindow, WM_READ_DONE, 0, 0);
  2427. ExitThread(0);
  2428. return (0);
  2429. }