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.

1993 lines
60 KiB

  1. /*******************************************************************************
  2. **
  3. ** MODULE: "DT.C".
  4. **
  5. **
  6. ** DESCRIPTION: iPostit Notes.
  7. **
  8. **
  9. ** AUTHOR: Daniel Dean.
  10. **
  11. **
  12. **
  13. ** CREATED: 12/18/1995.
  14. **
  15. ** HISTORY:
  16. **
  17. ** Date Author Reason
  18. ** ----------------------------------------------------------------------
  19. ** 12/18/95 Daniel Dean (a-Danld) Created
  20. ** 02/29/96 John Pierce (johnpi) Took over dev from Dan
  21. **
  22. **
  23. ** (C) C O P Y R I G H T M I C R O S O F T C O R P 1 9 9 5 - 1 9 9 6.
  24. **
  25. ** A L L R I G H T S R E S E R V E D .
  26. **
  27. *******************************************************************************/
  28. #include <windows.h>
  29. #include <commctrl.h> // for spin control
  30. #include <string.h>
  31. #include <stdio.h> // for sscanf
  32. #define DEFINE_GLOBALS
  33. #include "..\include\DT.H"
  34. #undef DEFINE_GLOBALS
  35. #include "..\include\dtrc.h"
  36. #include "..\include\public.h" // include file for test driver test.sys
  37. #define VK_A 0x41
  38. #define VK_F 0x46
  39. #define VK_Z 0x5A
  40. //
  41. // Uncomment this line to build a version of the app which does not
  42. // have the SendDescriptor button. This is for versions that ship
  43. // to the public
  44. #define EXTERNAL_BUILD
  45. #define KEYBORAD_MESSAGE (Message.message == WM_KEYDOWN || Message.message == WM_CHAR || Message.message == WM_KEYUP)
  46. //#define LISTSTYLE (LBS_WANTKEYBOARDINPUT | LBS_EXTENDEDSEL | LBS_NOTIFY | WS_BORDER | WS_VSCROLL | WS_VISIBLE | WS_CHILD)
  47. #define LISTSTYLE (LBS_WANTKEYBOARDINPUT | LBS_HASSTRINGS | LBS_NOTIFY | WS_BORDER | WS_VSCROLL | WS_VISIBLE | WS_CHILD )
  48. #define DESCSTYLE (LBS_WANTKEYBOARDINPUT | LBS_USETABSTOPS | LBS_NOTIFY | WS_BORDER | WS_VSCROLL | WS_VISIBLE | WS_CHILD)
  49. #define HEXSTYLE (LBS_WANTKEYBOARDINPUT | LBS_NOTIFY | WS_BORDER | WS_VSCROLL | WS_VISIBLE | WS_CHILD)
  50. //#define EDIT_STYLE (WS_CHILD | WS_VISIBLE | WS_BORDER | ES_UPPERCASE)
  51. #define SPACERTOP 10
  52. #define SPACERBOTTOM 5
  53. #define TEXTSIZE 12
  54. // Entity list box positions
  55. #define ENTITY_TEXT_X 10
  56. #define ENTITY_TEXT_Y SPACERTOP
  57. #define ENTITY_X ENTITY_TEXT_X
  58. #define ENTITY_Y ENTITY_TEXT_Y + SPACERBOTTOM + TEXTSIZE
  59. #define ENTITY_SIZE_X 193
  60. #define ENTITY_SIZE_Y 315
  61. // Descriptor list box positions
  62. // replacing Hex listbox with one tabbed Descr listbox
  63. #define DESC_TEXT_X ENTITY_X + ENTITY_SIZE_X + SPACERTOP
  64. #define DESC_TEXT_Y SPACERTOP
  65. #define DESC_X DESC_TEXT_X
  66. #define DESC_Y ENTITY_Y
  67. #define DESC_SIZE_X ENTITY_SIZE_X * 2 + SPACERTOP
  68. #define DESC_SIZE_Y ENTITY_SIZE_Y
  69. // Manual Data Entry button positions
  70. #define MANUAL_X CLEAR_X - CLEAR_SIZE_X - SPACERTOP//ENTITY_TEXT_X + ((ENTITY_SIZE_X - MANUAL_SIZE_X)/2)
  71. #define MANUAL_Y ENTITY_Y + ENTITY_SIZE_Y + SPACERBOTTOM
  72. #define MANUAL_SIZE_X 150
  73. #define MANUAL_SIZE_Y 25
  74. // Clear Descriptor button positions
  75. #define CLEAR_X (((ENTITY_SIZE_X * 3)+(SPACERTOP*5))/2) - (CLEAR_SIZE_X/2)
  76. #define CLEAR_Y DESC_Y + DESC_SIZE_Y + SPACERBOTTOM
  77. #define CLEAR_SIZE_X 150
  78. #define CLEAR_SIZE_Y 25
  79. // Send Descriptor button positions
  80. #define BUTTON_X CLEAR_X + CLEAR_SIZE_X + SPACERTOP // + (((DESC_SIZE_X/2) - BUTTON_SIZE_X) / 2)
  81. #define BUTTON_Y DESC_Y + DESC_SIZE_Y + SPACERBOTTOM
  82. #define BUTTON_SIZE_X 150
  83. #define BUTTON_SIZE_Y 25
  84. //
  85. // Child window ID's
  86. #define ID_ENTITY 0
  87. HANDLE hInst;
  88. #define NUMBER_WINDOWS 5
  89. HWND hWindow[NUMBER_WINDOWS];
  90. #define ENTITY_WINDOW hWindow[0]
  91. #define DESC_WINDOW hWindow[1]
  92. #define SEND_BUTTON hWindow[2]
  93. #define MANUAL_BUTTON hWindow[3]
  94. #define CLEAR_BUTTON hWindow[4]
  95. ULONG Focus = 0;
  96. //
  97. // Storage for address of original ListBox window proc
  98. //
  99. WNDPROC gOrgListBoxProc=NULL;
  100. //
  101. // Machinery for getting ItemTag values from users
  102. //
  103. #define USAGEP_FUNC 0
  104. #define EDIT_SIGNED_FUNC 1
  105. #define EDIT_UNSIGNED_FUNC 2
  106. #define COLL_FUNC 3
  107. #define END_COLL_FUNC 4
  108. #define INPUT_FUNC 5
  109. #define OUTPUT_FUNC 6
  110. #define FEAT_FUNC 7
  111. #define EXP_FUNC 8
  112. #define UNIT_FUNC 9
  113. #define PUSH_FUNC 10
  114. #define POP_FUNC 11
  115. #define DELIMIT_FUNC 12
  116. #define BOGUS_FUNC 13
  117. //
  118. // Array of pointers to input functions taking
  119. // a Handles and an int and returning void
  120. //
  121. void (*arGetItemVal[])(HANDLE,int) = {
  122. GetUsagePageVal,
  123. GetInputFromEditBoxSigned,
  124. GetInputFromEditBoxUnSigned,
  125. GetCollectionVal,
  126. GetEndCollectionVal,
  127. GetInputVal,
  128. GetOutputVal,
  129. GetFeatVal,
  130. GetExponentVal,
  131. GetUnitsVal,
  132. GetPushVal,
  133. GetPopVal,
  134. GetSetDelimiterVal,
  135. GetBogusVal
  136. };
  137. //
  138. // An array of offsets into the arGetItemVal[] function
  139. // pointer array. This array is arranged in the same
  140. // order that the items appear in the Entity ListBox.
  141. // So, when we click on an item we can programatically
  142. // call the input routine associated with it.
  143. UCHAR EntityInputIndex[] = {
  144. EDIT_UNSIGNED_FUNC, //USAGE,
  145. USAGEP_FUNC, //USAGE_PAGE,
  146. EDIT_UNSIGNED_FUNC, //USAGE_MIN,
  147. EDIT_UNSIGNED_FUNC, //USAGE_MAX,
  148. EDIT_UNSIGNED_FUNC, //DESIGNATOR_INDEX,
  149. EDIT_UNSIGNED_FUNC, //DESIGNATOR_MIN,
  150. EDIT_UNSIGNED_FUNC, //DESIGNATOR_MAX,
  151. EDIT_UNSIGNED_FUNC, //STRING_INDEX,
  152. EDIT_UNSIGNED_FUNC, //STRING_MIN,
  153. EDIT_UNSIGNED_FUNC, //STRING_MAX,
  154. COLL_FUNC, //COLLECTION,
  155. END_COLL_FUNC, //END_COLLECTION,
  156. INPUT_FUNC, //INPUT,
  157. OUTPUT_FUNC, //OUTPUT,
  158. FEAT_FUNC, //FEATURE,
  159. EDIT_SIGNED_FUNC, //LOGICAL_EXTENT_MIN,
  160. EDIT_SIGNED_FUNC, //LOGICAL_EXTENT_MAX,
  161. EDIT_SIGNED_FUNC, //PHYSICAL_EXTENT_MIN,
  162. EDIT_SIGNED_FUNC, //PHYSICAL_EXTENT_MAX,
  163. EXP_FUNC, //UNIT_EXPONENT,
  164. UNIT_FUNC, //UNIT
  165. EDIT_UNSIGNED_FUNC, //REPORT_SIZE,
  166. EDIT_SIGNED_FUNC, //REPORT_ID,
  167. EDIT_UNSIGNED_FUNC, //REPORT_COUNT,
  168. PUSH_FUNC, //PUSH,
  169. POP_FUNC, //POP
  170. DELIMIT_FUNC, //SET_DELIMITER
  171. BOGUS_FUNC //BOGUS
  172. };
  173. //
  174. // Misc. Globals
  175. //
  176. char gszFileName[MAX_PATH];
  177. ULONG InitializationFLAG = 1;
  178. HFONT hFontControl;
  179. HFONT hFontText;
  180. /*******************************************************************************
  181. **
  182. ** WinMain.
  183. **
  184. ** DESCRIPTION:
  185. **
  186. **
  187. ** PARAMETERS: HANDLE hInstance, Current Instance handle.
  188. ** HANDLE hPrevInstance, Last instance handle.
  189. ** LPSTR lpszCmParam, Comand line parameter.
  190. ** INT WindowState, Show window state.
  191. **
  192. ** RETURNS:
  193. **
  194. *******************************************************************************/
  195. INT PASCAL WinMain(HANDLE hInstance,
  196. HANDLE hPrevInstance,
  197. LPSTR lpszCmParam,
  198. INT WindowState)
  199. {
  200. MSG Message;
  201. HWND hWnd;
  202. hInst = hInstance;
  203. if(WindowRegistration(hInstance, hPrevInstance))
  204. return ERROR;
  205. // Main Window
  206. hWnd = CreateWindowEx(0,
  207. (LPCSTR) APPCLASS,
  208. (LPCSTR) APPTITLE,
  209. WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU |
  210. WS_DLGFRAME | CS_DBLCLKS,
  211. CW_USEDEFAULT,
  212. CW_USEDEFAULT,
  213. (ENTITY_SIZE_X * 3) + (SPACERTOP *5),
  214. 450,
  215. (HWND) NULL,
  216. LoadMenu(hInstance,MAKEINTRESOURCE(IDM_MAIN_MENU)),
  217. hInstance,
  218. (LPSTR) NULL);
  219. if(!hWnd)
  220. return ERROR;
  221. //
  222. // Save the instance in a global
  223. ghInst = hInstance;
  224. ShowWindow(hWnd, WindowState);
  225. UpdateWindow(hWnd);
  226. while(GetMessage(&Message, NULL, 0, 0))
  227. {
  228. TranslateMessage(&Message);
  229. DispatchMessage(&Message);
  230. }
  231. return Message.lParam;
  232. }
  233. /*******************************************************************************
  234. **
  235. ** WindowProc.
  236. **
  237. ** DESCRIPTION:
  238. **
  239. **
  240. ** PARAMETERS: HWND hWnd, Window handle.
  241. ** UINT Message, Window Message.
  242. ** UINT uParam, Unsigned message parameter.
  243. ** LPARAM lParam, Signed message parameter.
  244. **
  245. ** RETURNS:
  246. **
  247. *******************************************************************************/
  248. LPARAM WINAPI WindowProc(HWND hWnd, UINT Message, UINT uParam, LPARAM lParam)
  249. {
  250. //
  251. // Save the MainWindow handle in a global
  252. //
  253. ghWnd = hWnd;
  254. switch(Message)
  255. {
  256. case WM_VKEYTOITEM:
  257. switch(LOWORD(uParam))
  258. {
  259. case VK_DELETE:
  260. if(lParam == (LPARAM) DESC_WINDOW)
  261. {
  262. int Number,nBytes;
  263. PITEM_INFO pII;
  264. Number = SendMessage(DESC_WINDOW, LB_GETCURSEL, 0, 0);
  265. pII = (PITEM_INFO)SendMessage(DESC_WINDOW,LB_GETITEMDATA,Number,0);
  266. if(pII)
  267. {
  268. if( (nBytes = (pII->bTag & ITEM_SIZE_MASK)) == 0x03)
  269. gwReportDescLen -= 5; // 4 data bytes + 1 Tag byte
  270. else
  271. gwReportDescLen -= nBytes + 1;
  272. GlobalFree(pII);
  273. }
  274. SendMessage(DESC_WINDOW, LB_DELETESTRING, Number, 0);
  275. SendMessage(DESC_WINDOW, LB_SETCURSEL, Number, 0);
  276. }
  277. break;
  278. case VK_RETURN:
  279. if(lParam == (LPARAM) ENTITY_WINDOW)
  280. SendMessage(hWnd,WM_COMMAND,(WPARAM)MAKEWPARAM(0,LBN_DBLCLK),(LPARAM)ENTITY_WINDOW);
  281. break;
  282. case VK_TAB:
  283. if(lParam == (LPARAM) DESC_WINDOW)
  284. Focus = 0;
  285. if(lParam == (LPARAM) ENTITY_WINDOW)
  286. Focus = 1;
  287. SetFocus(hWindow[Focus]);
  288. break;
  289. }
  290. return -1;
  291. case WM_SETFOCUS:
  292. SetFocus(hWindow[Focus]);
  293. return SUCCESS;
  294. case WM_KEYDOWN:
  295. case WM_KEYUP:
  296. case WM_CHAR: return WMKey(hWnd, Message, uParam, lParam);
  297. case WM_RESTORE:
  298. {
  299. ULONG Index;
  300. Focus = 0;
  301. for(Index=0; Index<5; Index++)
  302. ShowWindow(hWindow[Index], SW_SHOW);
  303. return SUCCESS;
  304. }
  305. case WM_CREATE: return WMCreate(hWnd, lParam);
  306. case WM_SIZE: return WMSize(hWnd, lParam);
  307. case WM_PAINT: return WMPaint(hWnd, Message, uParam, lParam);
  308. case WM_COMMAND: return WMCommand(hWnd, Message, uParam, lParam);
  309. //case WM_SYSCOMMAND: return WMSysCommand(hWnd, Message, uParam, lParam);
  310. case WM_CLOSE: return WMClose(hWnd);
  311. case WM_DESTROY: return WMClose(hWnd);
  312. default: return DefWindowProc(hWnd, Message, uParam, lParam);
  313. }
  314. }
  315. /*******************************************************************************
  316. **
  317. ** WMCommand.
  318. **
  319. ** DESCRIPTION:
  320. **
  321. **
  322. ** PARAMETERS: HWND hWnd, Window handle.
  323. ** UINT Message, Window Message.
  324. ** UINT uParam, Unsigned message parameter.
  325. ** LPARAM lParam, Signed message parameter.
  326. **
  327. **
  328. ** RETURNS:
  329. **
  330. *******************************************************************************/
  331. LPARAM WMCommand(HWND hWnd, UINT Message, UINT uParam, LPARAM lParam)
  332. {
  333. #define MAX_STRING 64
  334. switch( uParam )
  335. {
  336. case IDM_OPEN:
  337. DoFileOpen(hWnd);
  338. return SUCCESS;
  339. case IDM_SAVE:
  340. DoFileSave(hWnd);
  341. return SUCCESS;
  342. case IDM_PRINT:
  343. DoFilePrint(hWnd);
  344. return SUCCESS;
  345. case IDM_EXIT:
  346. SendMessage(ghWnd,WM_CLOSE,0,0);
  347. return SUCCESS;
  348. case IDM_COPY:
  349. DoCopyDescriptor(hWnd);
  350. return SUCCESS;
  351. case IDM_PARSE:
  352. DoParseDescriptor(hWnd);
  353. return SUCCESS;
  354. }
  355. //////////////////////////////////////////////////
  356. //
  357. // Clear Descriptor button was pressed
  358. //
  359. if(lParam == (LPARAM) CLEAR_BUTTON && uParam == 0)
  360. {
  361. PITEM_INFO pItemInfo;
  362. LRESULT Index,Count;
  363. Count = SendMessage(DESC_WINDOW,LB_GETCOUNT,0,0);
  364. for( Index = Count-1; Index >= 0; Index-- )
  365. {
  366. pItemInfo = (PITEM_INFO) SendMessage(DESC_WINDOW,LB_GETITEMDATA,0,0);
  367. if( pItemInfo )
  368. GlobalFree(pItemInfo);
  369. SendMessage(DESC_WINDOW,LB_DELETESTRING,Index,0);
  370. }
  371. gwReportDescLen = 0;
  372. return SUCCESS;
  373. }
  374. //////////////////////////////////////////////////
  375. //
  376. // Mouse was DBL_CLICKED in the Entity window
  377. //
  378. if( (lParam == (LPARAM) ENTITY_WINDOW) && (HIWORD(uParam) == LBN_DBLCLK) )
  379. {
  380. ULONG Item;
  381. Item = SendMessage(ENTITY_WINDOW,LB_GETCURSEL,0,0);
  382. (*arGetItemVal[EntityInputIndex[Item]])( DESC_WINDOW, Item );
  383. return SUCCESS;
  384. }
  385. //////////////////////////////////////////////////
  386. //
  387. // Mouse was DBL_CLICKED in the Descriptor Window
  388. //
  389. if( (lParam == (LPARAM) DESC_WINDOW) && (HIWORD(uParam) == LBN_DBLCLK) )
  390. {
  391. ULONG Item;
  392. Item = SendMessage(DESC_WINDOW,LB_GETCURSEL,0,0);
  393. return SUCCESS;
  394. }
  395. //////////////////////////////////////////////////
  396. //
  397. // Manual Entry button was pressed
  398. //
  399. if(lParam == (LPARAM) MANUAL_BUTTON && uParam == 0)
  400. {
  401. PITEM_INFO pItemInfo;
  402. char szBuff[128],szTmp[3];
  403. int Index,rc,i;
  404. BYTE *pb;
  405. rc = DialogBox( ghInst,"EditBoxDlg",ghWnd , EditBoxDlgProc);
  406. if( rc == TRUE )
  407. {
  408. Index = SendMessage(ENTITY_WINDOW,LB_GETCURSEL,0,0);
  409. if( pItemInfo = (PITEM_INFO) GlobalAlloc(GPTR,sizeof(ITEM_INFO)) )
  410. {
  411. int NumBytes=0;
  412. pItemInfo->bTag = Entity[Index];
  413. if( gEditVal < 0 ) // Handle negative numbers differently
  414. {
  415. if( gEditVal > -0x100 )
  416. {
  417. pItemInfo->bTag |= 0x01;
  418. NumBytes = 1;
  419. }
  420. else if (gEditVal > -0x10000 )
  421. {
  422. pItemInfo->bTag |= 0x02;
  423. NumBytes = 2;
  424. }
  425. else
  426. {
  427. pItemInfo->bTag |= 0x03;
  428. NumBytes = 4;
  429. }
  430. }
  431. else // Number is not negative
  432. {
  433. if( (DWORD)gEditVal < 0x80 )
  434. {
  435. pItemInfo->bTag |= 0x01;
  436. NumBytes = 1;
  437. }
  438. else if ( (DWORD)gEditVal < 0x8000 )
  439. {
  440. pItemInfo->bTag |= 0x02;
  441. NumBytes = 2;
  442. }
  443. else
  444. {
  445. pItemInfo->bTag |= 0x03;
  446. NumBytes = 4;
  447. }
  448. }
  449. wsprintf(szBuff,"%s (%d)\t%02X ",szEntity[Index],gEditVal,pItemInfo->bTag);
  450. pb = (BYTE *) &gEditVal;
  451. for( i=0;i<NumBytes;i++ )
  452. {
  453. pItemInfo->bParam[i] = *pb++;
  454. wsprintf(szTmp,"%02X ",pItemInfo->bParam[i]);
  455. strcat(szBuff,szTmp);
  456. }
  457. Index = AddString(DESC_WINDOW,szBuff);
  458. rc = SendMessage(DESC_WINDOW,LB_SETITEMDATA,Index,(LPARAM) pItemInfo);
  459. }
  460. }
  461. return SUCCESS;
  462. }
  463. //////////////////////////////////////////////////
  464. //
  465. // Send Descriptor button was pressed
  466. //
  467. if(lParam == (LPARAM) SEND_BUTTON && uParam == 0)
  468. {
  469. ULONG Number;
  470. ULONG DescLBIndex;
  471. ULONG BufferIndex = 0;
  472. PUCHAR pBuffer;
  473. DWORD dwDevID; // Device ID returned from SendHIDDescriptor
  474. ULONG hDevHandle; // Device handle returned from SendHIDDescriptor
  475. PITEM_INFO pItemInfo;
  476. PDEVICEINFO pdiDlgInstanceInfo; // Instance info for SendData dialogs
  477. int rc;
  478. static int nDialogIndex=0;
  479. // Get number of Items in Descriptor list box
  480. Number = SendMessage(DESC_WINDOW, LB_GETCOUNT, 0, 0);
  481. // Descriptor must have at least 2 items
  482. if( Number < 2 )
  483. return FAILURE;
  484. if( (pBuffer = (PUCHAR) GlobalAlloc(GPTR, Number*5*sizeof(UCHAR)) ) )
  485. {
  486. // Read through Descriptor
  487. for(DescLBIndex=0; DescLBIndex<Number; DescLBIndex++)
  488. {
  489. // Get Pointer to Descriptor Info struct
  490. pItemInfo = (PITEM_INFO) SendMessage(DESC_WINDOW, LB_GETITEMDATA, DescLBIndex, 0 );
  491. if( !pItemInfo )
  492. {
  493. GlobalFree(pBuffer);
  494. return FAILURE;
  495. }
  496. pBuffer[BufferIndex++] = pItemInfo->bTag;
  497. switch( pItemInfo->bTag & DATA_SIZE_MASK )
  498. {
  499. case 0:
  500. break;
  501. case 1:
  502. pBuffer[BufferIndex++] = pItemInfo->bParam[0];
  503. break;
  504. case 2:
  505. pBuffer[BufferIndex++] = pItemInfo->bParam[0];
  506. pBuffer[BufferIndex++] = pItemInfo->bParam[1];
  507. break;
  508. case 3:
  509. pBuffer[BufferIndex++] = pItemInfo->bParam[0];
  510. pBuffer[BufferIndex++] = pItemInfo->bParam[1];
  511. pBuffer[BufferIndex++] = pItemInfo->bParam[2];
  512. pBuffer[BufferIndex++] = pItemInfo->bParam[3];
  513. break;
  514. }
  515. }
  516. //
  517. // Send the descriptor to the HID driver.
  518. // SendHIDDescriptor returns 0 if called worked
  519. // or ERR_CREATEDEVICE if device creation failed
  520. //
  521. rc = SendHIDDescriptor(pBuffer, &BufferIndex, &dwDevID, &hDevHandle);
  522. GlobalFree(pBuffer);
  523. //
  524. // If the descriptor was not sent OK or there was an error
  525. // creating the device, inform the user.
  526. //
  527. if( !rc )
  528. {
  529. MessageBox(hWnd,"Error Creating Device","DT Error",MB_OK);
  530. return 0;
  531. }
  532. //
  533. // Else, create modeless dialog for the device
  534. //
  535. pdiDlgInstanceInfo = (PDEVICEINFO) GlobalAlloc(GPTR,sizeof(DEVICEINFO));
  536. pdiDlgInstanceInfo->nDeviceID = dwDevID;
  537. pdiDlgInstanceInfo->hDevice = hDevHandle;
  538. CreateDialogParam(ghInst,"SendData",hWnd,SendDataDlgProc,(LONG)pdiDlgInstanceInfo);
  539. }
  540. else
  541. MessageBox(hWnd, "Cant allocate memory!", "ERROR", MB_OK);
  542. }
  543. return 0;
  544. }
  545. /*******************************************************************************
  546. **
  547. ** WMCreate.
  548. **
  549. ** DESCRIPTION:
  550. **
  551. **
  552. ** PARAMETERS: HWND hWnd, Window handle.
  553. ** UINT uParam, Unsigned message parameter.
  554. **
  555. ** RETURNS:
  556. **
  557. *******************************************************************************/
  558. LPARAM WMCreate(HWND hWnd, UINT uParam)
  559. {
  560. ULONG Index;
  561. static LOGFONT LogFont;
  562. //
  563. // Create a font for the ListBoxes and buttons
  564. LogFont.lfHeight = -14;
  565. strcpy(LogFont.lfFaceName,"Lucida Console");
  566. hFontControl = CreateFontIndirect(&LogFont);
  567. // Create ListBox of Entity Descriptor Tags
  568. ENTITY_WINDOW = CreateWindow("LISTBOX",
  569. NULL,
  570. LISTSTYLE,
  571. ENTITY_X,
  572. ENTITY_Y,
  573. ENTITY_SIZE_X,
  574. ENTITY_SIZE_Y,
  575. hWnd,
  576. ID_ENTITY, //NULL
  577. hInst,
  578. NULL);
  579. if(!ENTITY_WINDOW)
  580. return EXITERROR;
  581. for(Index=0; Index < ENTITY_INDEX; Index++)
  582. SendMessage(ENTITY_WINDOW, LB_ADDSTRING, 0, (LPARAM) szEntity[Index]);
  583. SendMessage(ENTITY_WINDOW, LB_SETSEL, TRUE, 0);
  584. SendMessage(ENTITY_WINDOW, WM_SETFONT,(WPARAM)hFontControl,TRUE);
  585. //
  586. // Create a list box to display Item tags and their values
  587. DESC_WINDOW = CreateWindow("LISTBOX",
  588. NULL,
  589. DESCSTYLE,
  590. DESC_X,
  591. DESC_Y,
  592. DESC_SIZE_X,
  593. DESC_SIZE_Y,
  594. hWnd,
  595. NULL,
  596. hInst,
  597. NULL);
  598. if(!DESC_WINDOW)
  599. return EXITERROR;
  600. //
  601. // Subclass this window!
  602. //
  603. gOrgListBoxProc = (WNDPROC)GetWindowLong( DESC_WINDOW, GWL_WNDPROC );
  604. SetWindowLong( DESC_WINDOW, GWL_WNDPROC,(LONG) ListBoxWindowProc);
  605. SendMessage(DESC_WINDOW, WM_SETFONT,(WPARAM)hFontControl,TRUE);
  606. {
  607. int TabStops[1] = {138};
  608. SendMessage(DESC_WINDOW, LB_SETTABSTOPS,1,(LPARAM)TabStops);
  609. }
  610. //
  611. //Create a button for manually entering item data
  612. //
  613. MANUAL_BUTTON = CreateWindow("BUTTON",
  614. NULL,
  615. WS_CHILD | WS_VISIBLE | WS_BORDER,
  616. MANUAL_X,
  617. MANUAL_Y,
  618. MANUAL_SIZE_X,
  619. MANUAL_SIZE_Y,
  620. hWnd,
  621. NULL,
  622. hInst,
  623. NULL);
  624. if(!MANUAL_BUTTON)
  625. return EXITERROR;
  626. SendMessage(MANUAL_BUTTON, WM_SETTEXT, TRUE, (LPARAM) "Manual Entry");
  627. SendMessage(MANUAL_BUTTON, WM_SETFONT,(WPARAM)hFontControl,TRUE);
  628. //
  629. //Create a button for clearing Descriptor entries
  630. //
  631. CLEAR_BUTTON = CreateWindow("BUTTON",
  632. NULL,
  633. WS_CHILD | WS_VISIBLE | WS_BORDER,
  634. CLEAR_X,
  635. CLEAR_Y,
  636. CLEAR_SIZE_X,
  637. CLEAR_SIZE_Y,
  638. hWnd,
  639. NULL,
  640. hInst,
  641. NULL);
  642. if(!CLEAR_BUTTON)
  643. return EXITERROR;
  644. SendMessage(CLEAR_BUTTON, WM_SETTEXT, TRUE, (LPARAM) "Clear Descriptor");
  645. SendMessage(CLEAR_BUTTON, WM_SETFONT,(WPARAM)hFontControl,TRUE);
  646. #ifndef EXTERNAL_BUILD
  647. //
  648. // Create a button for sending descriptor to HID driver
  649. SEND_BUTTON = CreateWindow("BUTTON",
  650. NULL,
  651. WS_CHILD | WS_VISIBLE | WS_BORDER,
  652. BUTTON_X,
  653. BUTTON_Y,
  654. BUTTON_SIZE_X,
  655. BUTTON_SIZE_Y,
  656. hWnd,
  657. NULL,
  658. hInst,
  659. NULL);
  660. if(!SEND_BUTTON)
  661. return EXITERROR;
  662. SendMessage(SEND_BUTTON, WM_SETTEXT, TRUE, (LPARAM) "Send Descriptor");
  663. SendMessage(SEND_BUTTON, WM_SETFONT,(WPARAM)hFontControl,TRUE);
  664. #endif
  665. return SUCCESS;
  666. }
  667. /*******************************************************************************
  668. **
  669. ** WMClose.
  670. **
  671. ** DESCRIPTION:
  672. **
  673. **
  674. ** PARAMETERS: HWND hWnd, Window handle.
  675. **
  676. **
  677. ** RETURNS:
  678. **
  679. *******************************************************************************/
  680. LPARAM WMClose(HWND hWnd)
  681. {
  682. ULONG Index;
  683. ULONG Number;
  684. PITEM_INFO pItemInfo;
  685. Number = SendMessage(DESC_WINDOW, LB_GETCOUNT, 0, 0);
  686. // Free all the memory pointed to by LB_GETITEMDATA
  687. for(Index=0; Index<Number; Index++)
  688. {
  689. // Get Pointer to Descriptor Info struct
  690. pItemInfo = (PITEM_INFO) SendMessage(DESC_WINDOW, LB_GETITEMDATA, Index, 0 );
  691. GlobalFree(pItemInfo);
  692. }
  693. DeleteObject(hFontControl);
  694. PostQuitMessage(0);
  695. return SUCCESS;
  696. }
  697. /*******************************************************************************
  698. **
  699. ** WMSize.
  700. **
  701. ** DESCRIPTION:
  702. **
  703. **
  704. ** PARAMETERS: HWND hWnd, Window handle.
  705. ** LPARAM lParam, Signed message parameter.
  706. **
  707. **
  708. ** RETURNS:
  709. **
  710. *******************************************************************************/
  711. LPARAM WMSize(HWND hWnd, LPARAM lParam)
  712. {
  713. return SUCCESS;
  714. }
  715. /*******************************************************************************
  716. **
  717. ** WMPaint.
  718. **
  719. ** DESCRIPTION:
  720. **
  721. **
  722. ** PARAMETERS: HWND hWnd, Window handle.
  723. ** UINT Message, Window Message.
  724. ** UINT uParam, Unsigned message parameter.
  725. ** LPARAM lParam, Signed message parameter.
  726. **
  727. ** RETURNS:
  728. **
  729. *******************************************************************************/
  730. LPARAM WMPaint(HWND hWnd, UINT Message, UINT uParam, LPARAM lParam)
  731. {
  732. HDC hDC;
  733. PAINTSTRUCT PaintS;
  734. static LOGFONT LF;
  735. hDC=BeginPaint(hWnd, &PaintS);
  736. LF.lfHeight = -12;
  737. hFontText = CreateFontIndirect(&LF);
  738. SelectObject(hDC,hFontText);
  739. // Set Window Texts
  740. //SetBkColor(hDC, BACKGROUND_BRUSH);
  741. SetBkMode(hDC,TRANSPARENT);
  742. TextOut(hDC, ENTITY_TEXT_X, ENTITY_TEXT_Y, "Items",sizeof("Items")-1);
  743. TextOut(hDC, DESC_TEXT_X, DESC_TEXT_Y, "Report Descriptor", sizeof("Report Descriptor")-1);
  744. //TextOut(hDC, HEX_TEXT_X, HEX_TEXT_Y, "HID Descriptor (Binary)", 23);
  745. DeleteObject(hFontText);
  746. EndPaint(hWnd, &PaintS);
  747. return SUCCESS;
  748. }
  749. /*******************************************************************************
  750. **
  751. ** WMKey.
  752. **
  753. ** DESCRIPTION:
  754. **
  755. **
  756. ** PARAMETERS: HWND hWnd, Window handle.
  757. **
  758. **
  759. ** RETURNS:
  760. **
  761. *******************************************************************************/
  762. INT WMKey(HWND hWnd, UINT Message, UINT uParam, LPARAM lParam)
  763. {
  764. // Do some checking for ALPHA NUMERIC
  765. if(uParam > VK_F && uParam <= VK_Z)
  766. return SUCCESS;
  767. if(uParam == VK_RETURN)
  768. Focus = 0;
  769. else if(uParam == VK_TAB)
  770. Focus = 2;
  771. // else
  772. // return SendMessage(DATA_WINDOW, Message, uParam, lParam);
  773. if(Message == WM_KEYUP || Message == WM_CHAR)
  774. return SUCCESS;
  775. SetFocus(hWindow[Focus]);
  776. return SUCCESS;
  777. }
  778. /*******************************************************************************
  779. **
  780. ** WindowRegistration.
  781. **
  782. ** DESCRIPTION:
  783. **
  784. **
  785. ** PARAMETERS: HANDLE hInstance, Current Instance handle.
  786. ** HANDLE hPrevInstance, Last instance handle.
  787. **
  788. ** RETURNS:
  789. **
  790. *******************************************************************************/
  791. BOOL WindowRegistration(HANDLE hInstance, HANDLE hPrevInstance)
  792. {
  793. WNDCLASS WndClass;
  794. if(!hPrevInstance)
  795. {
  796. WndClass.style = CS_DBLCLKS | CS_PARENTDC | CS_HREDRAW | CS_VREDRAW | CS_SAVEBITS | CS_BYTEALIGNCLIENT | CS_BYTEALIGNWINDOW;
  797. WndClass.lpfnWndProc = WindowProc;
  798. WndClass.cbClsExtra = 0;
  799. WndClass.cbWndExtra = 0;
  800. WndClass.hInstance = hInstance;
  801. WndClass.hIcon = LoadIcon(hInstance,MAIN_ICON);
  802. WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  803. WndClass.hbrBackground = (HBRUSH) (COLOR_MENU+1);
  804. WndClass.lpszMenuName = MAKEINTRESOURCE(IDM_MAIN_MENU);
  805. WndClass.lpszClassName = (LPSTR) APPCLASS;
  806. if(!RegisterClass(&WndClass))
  807. return FAILURE;
  808. }
  809. return SUCCESS;
  810. }
  811. /***************************************************************************************************
  812. **
  813. ** DoParseDescriptor()
  814. **
  815. ** DESCRIPTION: Parses the current Descriptor
  816. **
  817. ** PARAMETERS: HANDLE - handle the main window
  818. **
  819. ** RETURNS:
  820. **
  821. ***************************************************************************************************/
  822. void DoParseDescriptor(HANDLE hWnd)
  823. {
  824. LRESULT rc;
  825. LRESULT lines;
  826. HGLOBAL hTextBuff;
  827. char * pTextBuff;
  828. int i;
  829. char tmpBuff[128];
  830. //
  831. // Create a buffer big enough for all the Item strings
  832. lines = SendMessage(DESC_WINDOW,LB_GETCOUNT,0,0);
  833. if( !lines )
  834. return;
  835. hTextBuff = GlobalAlloc(GHND, lines * MAX_DESC_ENTRY );
  836. //
  837. // Copy the current entry into the buffer after appending
  838. // a new line to it.
  839. //
  840. pTextBuff = (char *)GlobalLock(hTextBuff);
  841. for(i=0;i<lines;i++)
  842. {
  843. SendMessage(DESC_WINDOW,LB_GETTEXT,i,(LPARAM)tmpBuff);
  844. SendMessage(DESC_WINDOW,LB_GETTEXTLEN,i,0);
  845. strcat(tmpBuff,"\n");
  846. strcat(pTextBuff,tmpBuff);
  847. }
  848. strcat(pTextBuff,"END\0\0");
  849. GlobalUnlock(hTextBuff);
  850. rc = yy_scan_string( pTextBuff );
  851. rc = yyparse();
  852. GlobalFree(hTextBuff);
  853. }
  854. /***************************************************************************************************
  855. **
  856. ** DoCopyDescriptor()
  857. **
  858. ** DESCRIPTION: Copies the current descriptor to the Clip Board
  859. **
  860. ** PARAMETERS:
  861. **
  862. ** RETURNS:
  863. **
  864. ***************************************************************************************************/
  865. void DoCopyDescriptor(HANDLE hWnd)
  866. {
  867. HGLOBAL hTextBuff;
  868. char * pTextBuff;
  869. LRESULT lines,len;
  870. char tmpBuff[MAX_DESC_ENTRY];
  871. int i;
  872. //
  873. // Calculate the size of, and create a memory block for
  874. // transfering the descriptor to the ClipBoard
  875. //
  876. lines = SendMessage(DESC_WINDOW,LB_GETCOUNT,0,0);
  877. hTextBuff = GlobalAlloc(GHND, lines * MAX_DESC_ENTRY );
  878. //
  879. // Copy the current entry into the buffer after appending
  880. // a new line to it.
  881. //
  882. pTextBuff = (char *)GlobalLock(hTextBuff);
  883. for(i=0;i<lines;i++)
  884. {
  885. SendMessage(DESC_WINDOW,LB_GETTEXT,i,(LPARAM)tmpBuff);
  886. len = SendMessage(DESC_WINDOW,LB_GETTEXTLEN,i,0);
  887. strcat(tmpBuff,"\n");
  888. strcat(pTextBuff,tmpBuff);
  889. }
  890. GlobalUnlock(hTextBuff);
  891. //
  892. // Copy the text to the Clipboard
  893. //
  894. OpenClipboard(hWnd);
  895. EmptyClipboard();
  896. SetClipboardData(CF_TEXT,hTextBuff);
  897. CloseClipboard();
  898. }
  899. /***************************************************************************************************
  900. **
  901. ** DoFileOPen()
  902. **
  903. ** DESCRIPTION: Opens file specified by user from common dialog FileOpen, and the fills in
  904. ** the descriptor listbox with saved values
  905. **
  906. ** PARAMETERS:
  907. **
  908. ** RETURNS:
  909. **
  910. ***************************************************************************************************/
  911. void DoFileOpen(HANDLE hwnd)
  912. {
  913. static OPENFILENAME of;
  914. HANDLE hOpenFile;
  915. UINT nItems;
  916. DWORD dwBytesRead;
  917. LRESULT nIndex;
  918. BYTE bTag,bNumBytes;
  919. PITEM_INFO pItemInfo;
  920. DWORD rc;
  921. of.lStructSize = sizeof(OPENFILENAME);
  922. of.hwndOwner = hwnd;
  923. of.hInstance = NULL;
  924. of.lpstrFilter = "HID Descriptor Files(*.hid)\0*.hid\0\0";
  925. of.lpstrCustomFilter = NULL;
  926. of.nMaxCustFilter = 0;
  927. of.nFilterIndex = 1;
  928. of.lpstrFile = gszFileName;
  929. of.nMaxFile = MAX_PATH;
  930. of.lpstrFileTitle = NULL;
  931. of.lpstrInitialDir = NULL;
  932. of.lpstrTitle = NULL;
  933. of.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  934. of.lpstrDefExt = "*.hid";
  935. of.lpTemplateName = NULL;
  936. if(rc = GetOpenFileName(&of) )
  937. {
  938. hOpenFile = CreateFile(gszFileName,GENERIC_READ,0,NULL,
  939. OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
  940. if( hOpenFile )
  941. {
  942. char szBuff[128],szTmp[64];
  943. int i;
  944. // Clear any current descriptor
  945. SendMessage(hwnd,WM_COMMAND,0,(LPARAM) CLEAR_BUTTON);
  946. // Get number of items in file from Header
  947. ReadFile(hOpenFile,&nItems,sizeof(DWORD),&dwBytesRead,NULL);
  948. // Get the Items
  949. while( nItems-- )
  950. {
  951. if( pItemInfo = (PITEM_INFO) GlobalAlloc(GPTR,sizeof(ITEM_INFO)) )
  952. {
  953. ReadFile(hOpenFile,pItemInfo,sizeof(ITEM_INFO),&dwBytesRead,NULL);
  954. //
  955. // Now read in the data
  956. //
  957. // Mask off all but the tag bits
  958. //
  959. bTag = pItemInfo->bTag & ITEM_TAG_MASK;
  960. gwReportDescLen++;
  961. //
  962. // Locate the tag in the Entity array. nIndex will equall offset
  963. // into the szEntity string array when found
  964. for( nIndex =0; nIndex < ENTITY_INDEX; nIndex++ )
  965. if( Entity[nIndex] == bTag )
  966. break;
  967. //
  968. // Create the textual representation of the Item
  969. wsprintf(szBuff,"%s (%d)\t%02X ",szEntity[nIndex],(DWORD)pItemInfo->bParam[0],pItemInfo->bTag);
  970. //
  971. // Create the Hex portion of the display
  972. // Mask off all but number of bytes bits
  973. bNumBytes = pItemInfo->bTag & ITEM_SIZE_MASK;
  974. // a bit value of 3 (11) means we have 4 bytes of data
  975. if( bNumBytes == 3 )
  976. bNumBytes = 4;
  977. for( i=0;i<bNumBytes;i++)
  978. {
  979. wsprintf(szTmp,"%02X ",pItemInfo->bParam[i]);
  980. strcat(szBuff,szTmp);
  981. }
  982. gwReportDescLen += bNumBytes;
  983. nIndex = AddString(DESC_WINDOW,szBuff);
  984. SendMessage(DESC_WINDOW,LB_SETITEMDATA,nIndex,(LPARAM) pItemInfo);
  985. }
  986. }
  987. CloseHandle(hOpenFile);
  988. }//end if( hOpenFile )
  989. }//end If(GetOpenFileName)
  990. else
  991. rc = CommDlgExtendedError();
  992. }
  993. /***************************************************************************************************
  994. **
  995. ** DoFileSave()
  996. **
  997. ** DESCRIPTION: Saves values from the descriptor list box to a file specified by the user from
  998. ** common dialog FileSave
  999. **
  1000. ** PARAMETERS:
  1001. **
  1002. ** RETURNS:
  1003. **
  1004. ***************************************************************************************************/
  1005. #define HID_FILE 1
  1006. #define LAVA_FILE 2
  1007. #define TXT_FILE 3
  1008. void DoFileSave(HANDLE hwnd)
  1009. {
  1010. static OPENFILENAME of;
  1011. HANDLE hSaveFile;
  1012. PITEM_INFO pIF;
  1013. DWORD dwBytesWritten;
  1014. UINT nItems=0,nIndex=0;
  1015. of.lStructSize = sizeof(OPENFILENAME);
  1016. of.hwndOwner = hwnd;
  1017. of.hInstance = NULL;
  1018. of.lpstrFilter = "HID Descriptor File\0*.hid\0Intel LAVA Data File\0*.dcd\0Text File\0*.txt\0\0";
  1019. of.lpstrCustomFilter = NULL;
  1020. of.nMaxCustFilter = 0;
  1021. of.nFilterIndex = 1;
  1022. of.lpstrFile = gszFileName;
  1023. of.nMaxFile = MAX_PATH;
  1024. of.lpstrFileTitle = NULL;
  1025. of.lpstrInitialDir = NULL;
  1026. of.lpstrTitle = NULL;
  1027. of.Flags = OFN_SHOWHELP ;//| OFN_PATHMUSTEXIST; //| OFN_FILEMUSTEXIST;
  1028. of.lpstrDefExt = "*.hid";
  1029. of.lpTemplateName = NULL;
  1030. if( GetSaveFileName(&of) )
  1031. {
  1032. hSaveFile = CreateFile(gszFileName,GENERIC_WRITE,0,NULL,
  1033. CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  1034. if( hSaveFile != INVALID_HANDLE_VALUE )
  1035. {
  1036. if(nItems = SendMessage(DESC_WINDOW,LB_GETCOUNT,0,0))
  1037. {
  1038. switch( of.nFilterIndex )
  1039. {
  1040. case HID_FILE:
  1041. // Header
  1042. WriteFile(hSaveFile,&nItems,sizeof(UINT),&dwBytesWritten,NULL);
  1043. // Data
  1044. for(nIndex=0; nIndex < nItems; nIndex++)
  1045. {
  1046. pIF = (PITEM_INFO) SendMessage(DESC_WINDOW,LB_GETITEMDATA,nIndex,0);
  1047. WriteFile(hSaveFile,pIF,sizeof(ITEM_INFO),&dwBytesWritten,NULL);
  1048. }
  1049. break;
  1050. case LAVA_FILE:
  1051. WriteLavaConfigFile(hSaveFile,nItems);
  1052. break;
  1053. case TXT_FILE:
  1054. {
  1055. char buff[80]; // width of a page
  1056. for(nIndex=0; nIndex < nItems; nIndex++)
  1057. {
  1058. SendMessage(DESC_WINDOW,LB_GETTEXT,nIndex,(LPARAM)buff);
  1059. strcat(buff,"\n");
  1060. WriteFile(hSaveFile,buff,strlen(buff),&dwBytesWritten,NULL);
  1061. }
  1062. break;
  1063. }
  1064. }// end switch()
  1065. }// end if(nItems=)
  1066. CloseHandle(hSaveFile);
  1067. }//end if( hSaveFile )
  1068. //
  1069. // CreatFile failed form some reason, find out why
  1070. else
  1071. {
  1072. LPVOID lpErrorMessage;
  1073. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  1074. NULL,GetLastError(),
  1075. MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
  1076. (LPSTR) &lpErrorMessage,
  1077. 0,NULL );
  1078. MessageBox(NULL,lpErrorMessage,"File Save Error",MB_OK);
  1079. }
  1080. }//end If(GetOpenFileName)
  1081. }
  1082. /***************************************************************************************************
  1083. **
  1084. ** WriteLavaConfigFile(HANDLE hSaveFile, int nItems)
  1085. **
  1086. ** DESCRIPTION: Writes the Report desriptor in the Intle Lava .cfg format. This is done
  1087. ** in a seperate routine so that we can use it in more than one place.
  1088. **
  1089. ** PARAMETERS: HANDLE - Handle to the file to write to
  1090. ** int - Number of items in the list box
  1091. **
  1092. ** RETURNS:
  1093. **
  1094. ***************************************************************************************************/
  1095. char szLAVAHEADER[] = "00 00 DC DC 00 00 00 00 01 00 00 00\n";
  1096. void WriteLavaConfigFile(HANDLE hSaveFile, UINT nItems)
  1097. {
  1098. PITEM_INFO pIF;
  1099. char szBuff[30],szTmp[4];
  1100. UINT bNumBytes,i;
  1101. DWORD dwBytesWritten;
  1102. UINT nIndex=0;
  1103. int nByteCount=0;
  1104. int remainder;
  1105. //
  1106. // Write out the header info.
  1107. //
  1108. // First line is FileIndex line. Static at this time
  1109. //
  1110. WriteFile(hSaveFile,szLAVAHEADER,strlen(szLAVAHEADER), &dwBytesWritten,NULL);
  1111. //
  1112. // Second line is Lava ConfigurationIndex. The only non static items (in this
  1113. // release) will be the ReportDescriptor size.
  1114. //
  1115. wsprintf(szBuff,"14 00 09 00 %02X %02X 00 00\n",LOBYTE(gwReportDescLen),HIBYTE(gwReportDescLen));
  1116. WriteFile(hSaveFile,szBuff,strlen(szBuff),&dwBytesWritten,NULL);
  1117. //
  1118. // Third line is the HIDDescriptor. The only item that will change is the ReportDescriptor
  1119. // length field. All the other fields should be the same for MOST devices.
  1120. //
  1121. wsprintf(szBuff,"09 01 00 01 00 01 02 %02X %02X\n",LOBYTE(gwReportDescLen),HIBYTE(gwReportDescLen));
  1122. WriteFile(hSaveFile,szBuff,strlen(szBuff),&dwBytesWritten,NULL);
  1123. //
  1124. // Rest of the file is the ReportDescriptor
  1125. //
  1126. for(nIndex=0; nIndex < nItems; nIndex++)
  1127. {
  1128. pIF = (PITEM_INFO) SendMessage(DESC_WINDOW,LB_GETITEMDATA,nIndex,0);
  1129. wsprintf(szBuff,"%02X ",pIF->bTag);
  1130. nByteCount++;
  1131. if( (remainder = nByteCount % 16) == 0 )
  1132. strcat(szBuff,"\n");
  1133. // Mask off all but number of bytes bits
  1134. bNumBytes = pIF->bTag & ITEM_SIZE_MASK;
  1135. // a bit value of 3 (11 binary) means we have 4 bytes of data
  1136. if( bNumBytes == 3 )
  1137. bNumBytes = 4;
  1138. for( i=0;i<bNumBytes;i++)
  1139. {
  1140. nByteCount++;
  1141. wsprintf(szTmp,"%02X ",pIF->bParam[i]);
  1142. strcat(szBuff,szTmp);
  1143. if((remainder = nByteCount % 16)==0)
  1144. strcat(szBuff,"\n");
  1145. }
  1146. //if((remainder = nByteCount % 16)==0)
  1147. // strcat(szBuff,"\n");
  1148. strcat(szBuff,"\0");
  1149. WriteFile(hSaveFile,szBuff,strlen(szBuff),&dwBytesWritten,NULL);
  1150. }
  1151. }
  1152. /***************************************************************************************************
  1153. **
  1154. ** DoFilePrint()
  1155. **
  1156. ** DESCRIPTION: Prints the current contents of the DESC_WINDOW list box
  1157. **
  1158. ** PARAMETERS:
  1159. **
  1160. ** RETURNS:
  1161. **
  1162. ***************************************************************************************************/
  1163. void DoFilePrint(HANDLE hwnd)
  1164. {
  1165. PRINTDLG pd;
  1166. DOCINFO di;
  1167. TEXTMETRIC tm;
  1168. int nNumItems,i,y;
  1169. int nLineHeight, nDescColumn, nHexColumn;
  1170. char szDescBuff[64],szTextBuff[64];
  1171. pd.lStructSize = sizeof(PRINTDLG);
  1172. pd.hwndOwner = hwnd;
  1173. pd.hDevMode = NULL;
  1174. pd.hDevNames = NULL;
  1175. pd.hDC = NULL;
  1176. pd.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
  1177. pd.nCopies = 1;
  1178. PrintDlg(&pd);
  1179. GetTextMetrics(pd.hDC,&tm);
  1180. nLineHeight = tm.tmHeight + tm.tmExternalLeading;
  1181. nHexColumn = 40 * tm.tmAveCharWidth;
  1182. nDescColumn = 5 * tm.tmAveCharWidth;
  1183. di.cbSize = sizeof(DOCINFO);
  1184. di.lpszDocName = "HID";
  1185. di.lpszOutput = NULL;
  1186. StartDoc(pd.hDC,&di);
  1187. StartPage(pd.hDC);
  1188. nNumItems = SendMessage(DESC_WINDOW,LB_GETCOUNT,0,0);
  1189. for(i=0; i< nNumItems; i++)
  1190. {
  1191. SendMessage(DESC_WINDOW,LB_GETTEXT,i,(LPARAM)szDescBuff);
  1192. y = i*nLineHeight;
  1193. strcpy(szTextBuff,strtok(szDescBuff,"\t"));
  1194. TextOut(pd.hDC,nDescColumn,y+40,szTextBuff,strlen(szTextBuff));
  1195. strcpy(szTextBuff,strtok(NULL,"\t"));
  1196. TextOut(pd.hDC,nHexColumn,y+40,szTextBuff,strlen(szTextBuff));
  1197. }
  1198. EndPage (pd.hDC);
  1199. EndDoc (pd.hDC);
  1200. DeleteDC (pd.hDC);
  1201. }
  1202. /*******************************************************************************
  1203. **
  1204. ** SendHIDDescriptor.
  1205. **
  1206. ** DESCRIPTION: Sends the current HID descriptor to the HID driver.
  1207. **
  1208. **
  1209. ** PARAMETERS: pHID - Pointer to a memory block which contains the HID
  1210. ** descriptor bytes
  1211. ** pSize - Size of the memory block (in bytes)
  1212. **
  1213. ** pDevID - Variable to recievce device ID
  1214. **
  1215. ** pDevHandle - Variable to recieve device handle
  1216. **
  1217. ** RETURNS: int - Ordinal of device HID driver created
  1218. ** or ERR_CREATEDEVICE if device could not
  1219. ** be created
  1220. **
  1221. *******************************************************************************/
  1222. int SendHIDDescriptor(PUCHAR pHID, PULONG pSize, DWORD *pDevID, ULONG *pDevHandle)
  1223. {
  1224. ULONG Status = FALSE;
  1225. HANDLE hHIDDevice;
  1226. ULONG dwDeviceID_and_Handle[2];
  1227. // TESTING TESTING TESTING !!!!
  1228. static DWORD i=1;
  1229. static DWORD handle;
  1230. *pDevID = i++;
  1231. *pDevHandle = (HANDLE) handle++;
  1232. return TRUE;
  1233. // TESTING TESTING TESTING !!!!
  1234. dwDeviceID_and_Handle[0]=0;
  1235. dwDeviceID_and_Handle[1]=0;
  1236. //
  1237. // Open the HID pump Device
  1238. //
  1239. hHIDDevice = CreateFile(TEST_DEVICE,
  1240. 0,
  1241. FILE_SHARE_READ | FILE_SHARE_WRITE,
  1242. NULL,
  1243. OPEN_EXISTING,
  1244. 0,
  1245. NULL);
  1246. if( hHIDDevice == INVALID_HANDLE_VALUE )
  1247. MessageBox(NULL,"CreatFile(HID_DEVICE) Failed!","DT Error",MB_ICONEXCLAMATION | MB_OK);
  1248. //
  1249. // Send the descriptor
  1250. //
  1251. else
  1252. {
  1253. Status = DeviceIoControl(hHIDDevice,
  1254. (ULONG)IOCTL_ADD_DEVICE,
  1255. pHID,
  1256. *pSize,
  1257. &dwDeviceID_and_Handle,
  1258. sizeof(dwDeviceID_and_Handle),
  1259. pSize,
  1260. (LPOVERLAPPED) NULL);
  1261. if( !Status )
  1262. {
  1263. char buff[128];
  1264. wsprintf(buff, "DeviceIoControl(IOCTL_ADD_DEVICE) Failed! Error: %d",GetLastError() );
  1265. MessageBox(NULL,buff,"DT Error",MB_ICONEXCLAMATION | MB_OK);
  1266. }
  1267. //
  1268. // Save the DeviceID and Handle of the device created by the HID.
  1269. //
  1270. else
  1271. {
  1272. char buff[128];
  1273. *pDevID = dwDeviceID_and_Handle[0];
  1274. *pDevHandle = dwDeviceID_and_Handle[1];
  1275. //wsprintf(buff,"DevID = %d DevObj=%08X",dwDeviceID_and_Handle[0],dwDeviceID_and_Handle[1]);
  1276. //MessageBox(NULL,buff,"SendHIDDescriptor()",MB_OK);
  1277. }
  1278. CloseHandle(hHIDDevice);
  1279. }
  1280. return Status;
  1281. }
  1282. /*******************************************************************************
  1283. **
  1284. ** SendHIDData(BYTE *pPacket, ULONG SizeIn, PULONG pSizeOut)
  1285. **
  1286. ** DESCRIPTION: Send Data to the HID driver via IOCTL's
  1287. **
  1288. ** PARAMETERS: pPacket - Pointer to the data
  1289. ** SizeIn - Number of bytes in the packet
  1290. ** pSizeOut- Varaible to receive number of bytes actually sent
  1291. ** hDevice - Handle to device data is meant for
  1292. **
  1293. ** RETURNS:
  1294. **
  1295. *******************************************************************************/
  1296. ULONG SendHIDData(SENDDATA_PACKET *pPacket, ULONG SizeIn, DWORD *pSizeOut)
  1297. {
  1298. ULONG Status = TRUE;
  1299. HANDLE hHIDDevice;
  1300. //TESTING!!!!!!!!
  1301. return TRUE;
  1302. //TESTING!!!!!!!!
  1303. hHIDDevice = CreateFile(TEST_DEVICE,
  1304. 0,
  1305. FILE_SHARE_READ | FILE_SHARE_WRITE,
  1306. NULL,
  1307. OPEN_EXISTING,
  1308. 0,
  1309. NULL);
  1310. if( hHIDDevice == INVALID_HANDLE_VALUE )
  1311. {
  1312. MessageBox(NULL,"CreatFile(HID_DEVICE) Failed!","DT Error",MB_ICONEXCLAMATION | MB_OK);
  1313. return FALSE;
  1314. }
  1315. if(hHIDDevice)
  1316. {
  1317. Status = DeviceIoControl(hHIDDevice,
  1318. (ULONG)IOCTL_SET_DATA,
  1319. pPacket,
  1320. SizeIn,
  1321. NULL,
  1322. 0,
  1323. pSizeOut,
  1324. (LPOVERLAPPED) NULL);
  1325. if( !Status )
  1326. MessageBox(NULL,"DeviceIoControl(IOCTL_SET_DATA) Failed!","DT Error",MB_ICONEXCLAMATION | MB_OK);
  1327. CloseHandle(hHIDDevice);
  1328. }
  1329. return Status;
  1330. }
  1331. /*******************************************************************************
  1332. **
  1333. ** KillHIDDevice(int nDeviceID)
  1334. **
  1335. ** DESCRIPTION: Kill a HID device
  1336. **
  1337. ** PARAMETERS: hDevice - Handle to device to kill
  1338. **
  1339. ** RETURNS: TRUE - All went as planned
  1340. ** FALSE - Error occured
  1341. **
  1342. *******************************************************************************/
  1343. ULONG KillHIDDevice(SENDDATA_PACKET *pPacket)
  1344. {
  1345. ULONG Status = TRUE;
  1346. HANDLE hHIDDevice;
  1347. DWORD dwBytesReturned;
  1348. //TESTING!!!!!!!
  1349. return TRUE;
  1350. hHIDDevice = CreateFile(TEST_DEVICE,
  1351. 0,
  1352. FILE_SHARE_READ | FILE_SHARE_WRITE,
  1353. NULL,
  1354. OPEN_EXISTING,
  1355. 0,
  1356. NULL);
  1357. if( hHIDDevice == INVALID_HANDLE_VALUE )
  1358. {
  1359. MessageBox(NULL,"CreatFile(HID_DEVICE) Failed!","DT Error",MB_ICONEXCLAMATION | MB_OK);
  1360. return FALSE;
  1361. }
  1362. if(hHIDDevice)
  1363. {
  1364. Status = DeviceIoControl(hHIDDevice,
  1365. (ULONG)IOCTL_DELETE_DEVICE,
  1366. pPacket,
  1367. sizeof(SENDDATA_PACKET),
  1368. NULL,
  1369. 0,
  1370. &dwBytesReturned,
  1371. (LPOVERLAPPED) NULL);
  1372. if( !Status )
  1373. MessageBox(NULL,"DeviceIoControl(IOCTL_DELETE_DEVICE) Failed!","DT Error",MB_ICONEXCLAMATION | MB_OK);
  1374. CloseHandle(hHIDDevice);
  1375. }
  1376. return Status;
  1377. }
  1378. /*******************************************************************************
  1379. **
  1380. ** SendDataDlgProc
  1381. **
  1382. ** DESCRIPTION: Dialog procedure for the Device Data dialogs
  1383. **
  1384. **
  1385. ** PARAMETERS: lParam - Pointer to a DeviceInfo structure.
  1386. **
  1387. **
  1388. ** RETURNS:
  1389. **
  1390. *******************************************************************************/
  1391. BOOL WINAPI SendDataDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  1392. {
  1393. PDEVICEINFO pdiInstance;
  1394. switch( msg )
  1395. {
  1396. case WM_INITDIALOG:
  1397. {
  1398. char szBuff[128];
  1399. int i;
  1400. //
  1401. // Save instance information
  1402. pdiInstance = (PDEVICEINFO) lParam;
  1403. SetWindowLong(hDlg, DWL_USER, lParam);
  1404. //
  1405. // Set the Window title
  1406. wsprintf( szBuff,"Device%d Handle=%08X",pdiInstance->nDeviceID,pdiInstance->hDevice);
  1407. SetWindowText(hDlg,szBuff);
  1408. //
  1409. // Set up the spin control
  1410. SendMessage(GetDlgItem(hDlg,IDC_BYTESLIDER),UDM_SETRANGE,0,MAKELONG(10,1));
  1411. SetWindowText(GetDlgItem(hDlg,IDC_NUMBYTES),"1");
  1412. //
  1413. // Set all of the edit controls to 0
  1414. // and limit text to two hex characters
  1415. for(i=IDC_BYTE0;i<=IDC_BYTE9;i++)
  1416. {
  1417. HWND hwndEdit;
  1418. hwndEdit = GetDlgItem(hDlg,i);
  1419. SetWindowText(hwndEdit,"0");
  1420. SendMessage(hwndEdit,EM_LIMITTEXT,2,0);
  1421. }
  1422. return TRUE;
  1423. }
  1424. case WM_NOTIFY:
  1425. {
  1426. //
  1427. // Depending on whether or not the UP or DOWN arrow of
  1428. // the UPDOWN control was pressed, set the attribute
  1429. // of the byte entry EditWindows to Read only or Read/Write.
  1430. //
  1431. case IDC_BYTESLIDER:
  1432. {
  1433. NM_UPDOWN *nmud = (NM_UPDOWN *) lParam;
  1434. ///
  1435. // If the DOWN arrow was pressed set the attribute of the
  1436. // EditWindow who's ID corresponds to the base EditWindow ID (IDC_BYTE0)
  1437. // plus the current position of the UPDOWN control - 1 to Read only.
  1438. //
  1439. if( nmud->iDelta < 0 )
  1440. {
  1441. //
  1442. // Only deal with Edit Controls greater than 1. We always want to
  1443. // be able to send at least 1 byte.
  1444. if( nmud->iPos > 1 )
  1445. {
  1446. SendMessage(GetDlgItem(hDlg,IDC_BYTE0+nmud->iPos-1),EM_SETREADONLY,TRUE,0);
  1447. SetWindowText(GetDlgItem(hDlg,IDC_BYTE0+nmud->iPos-1),"0");
  1448. }
  1449. }
  1450. //
  1451. // Else UP arrow was pressed, so set the EditWindow who's ID corresponds to the base
  1452. // EditWindow ID (IDC_BYTE0) plus the current position of the UPDOWN control to
  1453. // Read/Write.
  1454. else
  1455. SendMessage(GetDlgItem(hDlg,IDC_BYTE0+nmud->iPos),EM_SETREADONLY,FALSE,0);
  1456. return TRUE;
  1457. }
  1458. } // end WM_NOTIFY
  1459. case WM_COMMAND:
  1460. {
  1461. PDEVICEINFO pdiInfo;
  1462. pdiInfo = (PDEVICEINFO) GetWindowLong(hDlg,DWL_USER);
  1463. switch( LOWORD(wParam))
  1464. {
  1465. case IDC_KILL:
  1466. {
  1467. SENDDATA_PACKET HIDDataPacket;
  1468. // Kill the device
  1469. HIDDataPacket.hDevice = pdiInfo->hDevice;
  1470. memset(HIDDataPacket.bData,0,MAX_DATA);
  1471. KillHIDDevice(&HIDDataPacket);
  1472. // Free the instance info
  1473. GlobalFree((HANDLE)GetWindowLong(hDlg,DWL_USER));
  1474. DestroyWindow(hDlg);
  1475. return TRUE;
  1476. }
  1477. case IDC_SEND:
  1478. {
  1479. int i;
  1480. char szHexNumber[4];
  1481. DWORD Sent;
  1482. SENDDATA_PACKET SendDataPacket;
  1483. //TESTING!!!!
  1484. char buff[128];
  1485. //
  1486. // The first element in the packet is the DeviceObject handle.
  1487. //
  1488. SendDataPacket.hDevice = pdiInfo->hDevice;
  1489. //
  1490. // Get the bytes from the Edit windows
  1491. //
  1492. for(i=0;i<MAX_DATA;i++)
  1493. {
  1494. GetDlgItemText(hDlg,IDC_BYTE0+i,szHexNumber,sizeof(szHexNumber));
  1495. // Convert from ASCII HEX to binary
  1496. sscanf(szHexNumber,"%2x",(unsigned)&SendDataPacket.bData[i]);
  1497. }
  1498. //
  1499. //Send it
  1500. //
  1501. SendHIDData(&SendDataPacket,sizeof(SENDDATA_PACKET),&Sent);
  1502. //wsprintf(buff,"DeviceID: %d PacketSize: %d",pdiInfo->nDeviceID,i);
  1503. //MessageBox(NULL,buff,"Testing",MB_OK);
  1504. return TRUE;
  1505. }
  1506. case IDC_GETDATA:
  1507. {
  1508. BYTE bData[MAX_DATA];
  1509. ULONG Status = TRUE;
  1510. DWORD SizeOut = MAX_DATA;
  1511. HANDLE hHIDDevice;
  1512. ULONG hDevice;
  1513. int nPacketSize;
  1514. SENDDATA_PACKET HIDDataPacket;
  1515. SENDDATA_PACKET *pHIDDataPacket;
  1516. pHIDDataPacket = &HIDDataPacket;
  1517. //
  1518. // Get the handle to the DeviceObject
  1519. HIDDataPacket.hDevice = pdiInfo->hDevice;
  1520. memset(HIDDataPacket.bData,0,MAX_DATA);
  1521. //
  1522. // Get the number of bytes in a packet
  1523. //
  1524. nPacketSize = SendMessage(GetDlgItem(hDlg,IDC_BYTESLIDER),UDM_GETPOS,0,0);
  1525. hHIDDevice = CreateFile(TEST_DEVICE,
  1526. 0,
  1527. FILE_SHARE_READ | FILE_SHARE_WRITE,
  1528. NULL,
  1529. OPEN_EXISTING,
  1530. 0,
  1531. NULL);
  1532. if( hHIDDevice == INVALID_HANDLE_VALUE )
  1533. {
  1534. MessageBox(NULL,"CreatFile(HID_DEVICE) Failed!","DT Error",MB_ICONEXCLAMATION | MB_OK);
  1535. return FALSE;
  1536. }
  1537. else //if(hHIDDevice)
  1538. {
  1539. Status = DeviceIoControl(hHIDDevice,
  1540. (ULONG)IOCTL_GET_DATA,
  1541. pHIDDataPacket,
  1542. sizeof(SENDDATA_PACKET),
  1543. &bData,
  1544. nPacketSize,
  1545. &SizeOut,
  1546. (LPOVERLAPPED) NULL);
  1547. if( !Status )
  1548. MessageBox(NULL,"DeviceIoControl(IOCTL_GET_DATA) Failed!","DT Error",MB_ICONEXCLAMATION | MB_OK);
  1549. CloseHandle(hHIDDevice);
  1550. if( Status )
  1551. {
  1552. char Buff[(MAX_DATA*3)+1];
  1553. char tmpbuff[4];
  1554. DWORD i;
  1555. memset(Buff,'\0',sizeof(Buff));
  1556. for(i=1;i<SizeOut;i++)
  1557. {
  1558. wsprintf(tmpbuff,"%02X ",bData[i]);
  1559. strcat(Buff,tmpbuff);
  1560. }
  1561. SetWindowText(GetDlgItem(hDlg,IDC_GETDATA_TEXT),Buff);
  1562. }
  1563. }
  1564. return Status;
  1565. }//edn CASE IDC_GETDATA
  1566. }// end switch(lParam)
  1567. }// end WM_COMMAND
  1568. }// end switch(msg)
  1569. return FALSE;
  1570. }
  1571. /*******************************************************************************
  1572. **
  1573. ** ListBoxWindowProc.
  1574. **
  1575. ** DESCRIPTION: Subclass window procedure for the HID Descriptor Text window.
  1576. ** We want to able to recognize right mouse button events when the
  1577. ** mouse is over this particular window.
  1578. **
  1579. **
  1580. ** PARAMETERS: HWND hWnd, Window handle.
  1581. ** UINT Message, Window Message.
  1582. ** UINT uParam, Unsigned message parameter.
  1583. ** LPARAM lParam, Signed message parameter.
  1584. **
  1585. ** RETURNS:
  1586. **
  1587. *******************************************************************************/
  1588. LPARAM WINAPI ListBoxWindowProc(HWND hWnd, UINT Message, UINT uParam, LPARAM lParam)
  1589. {
  1590. POINT p;
  1591. LRESULT item;
  1592. static HMENU hMenu;
  1593. static BOOL fMenuOpen=FALSE;
  1594. switch(Message)
  1595. {
  1596. //
  1597. // On RightButton Down, set the focus to the
  1598. // item under the cursor, and display a popup menu
  1599. case WM_RBUTTONDOWN:
  1600. {
  1601. p.x = LOWORD(lParam);
  1602. p.y = HIWORD(lParam);
  1603. item = SendMessage(DESC_WINDOW,LB_ITEMFROMPOINT,0,MAKELPARAM(p.x,p.y));
  1604. SendMessage(DESC_WINDOW,LB_SETCURSEL,item,0);
  1605. ClientToScreen(ghWnd,&p);
  1606. hMenu = CreatePopupMenu();
  1607. fMenuOpen = TRUE;
  1608. //
  1609. // Set the Insert menu item to be checked or unchecked depending
  1610. // on the state of gfInsert
  1611. if( !gfInsert )
  1612. AppendMenu(hMenu,MF_ENABLED,IDM_INSERT,"Insert Mode");
  1613. else
  1614. AppendMenu(hMenu,MF_CHECKED,IDM_INSERT,"Insert Mode");
  1615. AppendMenu(hMenu,MF_ENABLED,IDM_DELETE,"Delete Item");
  1616. TrackPopupMenu(hMenu,
  1617. TPM_LEFTBUTTON | TPM_RIGHTBUTTON,
  1618. p.x+DESC_X,
  1619. p.y+DESC_Y,
  1620. 0,
  1621. hWnd,
  1622. NULL);
  1623. break;
  1624. }
  1625. case WM_COMMAND:
  1626. switch(LOWORD(uParam))
  1627. {
  1628. case IDM_INSERT:
  1629. gfInsert ^= TRUE; // Toggle the state of the gfInsert flag
  1630. DestroyMenu(hMenu);
  1631. fMenuOpen = FALSE;
  1632. hMenu = NULL;
  1633. return TRUE;
  1634. case IDM_DELETE:
  1635. PostMessage(ghWnd,WM_VKEYTOITEM,VK_DELETE,(LPARAM)hWnd);
  1636. DestroyMenu(hMenu);
  1637. fMenuOpen = FALSE;
  1638. hMenu = NULL;
  1639. return TRUE;
  1640. }
  1641. break;
  1642. }
  1643. return( CallWindowProc(gOrgListBoxProc, hWnd,Message, uParam, lParam));
  1644. }