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.

370 lines
9.1 KiB

  1. /*++
  2. Copyright (c) Microsoft 1998, All Rights Reserved
  3. Module Name:
  4. buffers.c
  5. Abstract:
  6. This module contains the code for handling the display of HID report
  7. buffers for the extended calls dialog box.
  8. Environment:
  9. User mode
  10. Revision History:
  11. May-98 : Created
  12. --*/
  13. #include <windows.h>
  14. #include <malloc.h>
  15. #include <setupapi.h>
  16. #include "hidsdi.h"
  17. #include "hidpi.h"
  18. #include "buffers.h"
  19. #include "strings.h"
  20. #define CURRENT_REPORT(pDisp) (pDisp -> ReportBuffers + pDisp -> iCurrSelectionIndex)
  21. BOOLEAN
  22. BufferDisplay_Init(
  23. IN HWND hCB,
  24. IN HWND hEB,
  25. IN INT nBuffers,
  26. IN INT iBufferSize,
  27. IN HIDP_REPORT_TYPE RType,
  28. OUT PBUFFER_DISPLAY *ppBufferDisplay
  29. )
  30. /*++
  31. Routine Description:
  32. This routine initializes the buffer display mechanism for a given report type
  33. The display mechanism maintains a list of list of nBuffers, where is each
  34. is buffer is of iBufferSize and hCB and hEB are handles to the combo box and
  35. edit box for displaying the buffer.
  36. The variable ppBufferDisplay is allocated block which is passed into other
  37. buffer routines and contains information about the buffer for the other
  38. routines.
  39. This function will return FALSE if there was a problem allocating memory
  40. --*/
  41. {
  42. PBUFFER_DISPLAY pNewDisplay;
  43. CHAR *pszBufferHeader;
  44. CHAR szBufferName[24];
  45. INT iIndex;
  46. INT iCBIndex;
  47. pNewDisplay = (PBUFFER_DISPLAY) malloc(sizeof(BUFFER_DISPLAY));
  48. *ppBufferDisplay = NULL;
  49. if (NULL == pNewDisplay)
  50. {
  51. return (FALSE);
  52. }
  53. pNewDisplay -> ReportBuffers = (PREPORT_BUFFER) malloc(sizeof(REPORT_BUFFER) * nBuffers);
  54. if (NULL == pNewDisplay -> ReportBuffers)
  55. {
  56. free(pNewDisplay);
  57. return (FALSE);
  58. }
  59. memset(pNewDisplay -> ReportBuffers, 0x00, sizeof(REPORT_BUFFER) * nBuffers);
  60. pNewDisplay -> hBufferComboBox = hCB;
  61. pNewDisplay -> hBufferEditBox = hEB;
  62. pNewDisplay -> nReportBuffers = nBuffers;
  63. pNewDisplay -> iBufferSize = iBufferSize;
  64. pNewDisplay -> ReportType = RType;
  65. switch (pNewDisplay -> ReportType)
  66. {
  67. case HidP_Input:
  68. pszBufferHeader = "Input";
  69. break;
  70. case HidP_Output:
  71. pszBufferHeader = "Output";
  72. break;
  73. case HidP_Feature:
  74. pszBufferHeader = "Feature";
  75. break;
  76. default:
  77. pszBufferHeader = "Other";
  78. break;
  79. }
  80. for (iIndex = 0; iIndex < pNewDisplay -> nReportBuffers; iIndex++)
  81. {
  82. wsprintf(szBufferName, "%s Buffer #%d", pszBufferHeader, iIndex);
  83. iCBIndex = (INT) SendMessage(pNewDisplay -> hBufferComboBox,
  84. CB_ADDSTRING,
  85. 0,
  86. (LPARAM) szBufferName);
  87. if (CB_ERR == iCBIndex || CB_ERRSPACE == iCBIndex)
  88. {
  89. BufferDisplay_Destroy(pNewDisplay);
  90. return (FALSE);
  91. }
  92. iCBIndex = (INT) SendMessage(pNewDisplay -> hBufferComboBox,
  93. CB_SETITEMDATA,
  94. iCBIndex,
  95. iIndex);
  96. if (CB_ERR == iCBIndex || CB_ERRSPACE == iCBIndex)
  97. {
  98. BufferDisplay_Destroy(pNewDisplay);
  99. return (FALSE);
  100. }
  101. }
  102. SendMessage(pNewDisplay -> hBufferComboBox, CB_SETCURSEL, 0, 0);
  103. BufferDisplay_ChangeSelection(pNewDisplay);
  104. *ppBufferDisplay = pNewDisplay;
  105. return (TRUE);
  106. }
  107. VOID
  108. BufferDisplay_Destroy(
  109. IN PBUFFER_DISPLAY pBufferDisplay
  110. )
  111. /*++
  112. Routine Description:
  113. This routine cleans up the buffer display variable that was allocated by
  114. the initialize routine
  115. --*/
  116. {
  117. INT iIndex;
  118. for (iIndex = 0; iIndex < pBufferDisplay -> nReportBuffers; iIndex++)
  119. {
  120. if (NULL != pBufferDisplay -> ReportBuffers[iIndex].pBuffer)
  121. {
  122. free(pBufferDisplay -> ReportBuffers[iIndex].pBuffer);
  123. }
  124. }
  125. free(pBufferDisplay -> ReportBuffers);
  126. free(pBufferDisplay);
  127. return;
  128. }
  129. VOID
  130. BufferDisplay_ChangeSelection(
  131. IN PBUFFER_DISPLAY pBufferDisplay
  132. )
  133. /*++
  134. Routine Description:
  135. This routine has the selection of a buffer to display via the combo box
  136. --*/
  137. {
  138. INT iNewIndex;
  139. iNewIndex = (INT) SendMessage(pBufferDisplay -> hBufferComboBox,
  140. CB_GETCURSEL,
  141. 0,
  142. 0);
  143. iNewIndex = (INT) SendMessage(pBufferDisplay -> hBufferComboBox,
  144. CB_GETITEMDATA,
  145. iNewIndex,
  146. 0);
  147. pBufferDisplay -> iCurrSelectionIndex = iNewIndex;
  148. BufferDisplay_OutputBuffer(pBufferDisplay -> hBufferEditBox,
  149. &(pBufferDisplay -> ReportBuffers[iNewIndex]));
  150. return;
  151. }
  152. VOID
  153. BufferDisplay_OutputBuffer(
  154. HWND hEditBox,
  155. PREPORT_BUFFER pReportBuffer
  156. )
  157. /*++
  158. Routine Description:
  159. This routine outputs to hEditBox a byte representation of pReportBuffer
  160. --*/
  161. {
  162. PCHAR BufferString;
  163. if (0 == pReportBuffer -> iBufferSize || NULL == pReportBuffer -> pBuffer)
  164. {
  165. SetWindowText(hEditBox, "");
  166. }
  167. else
  168. {
  169. /*
  170. // Create a buffer string the size of the buffer and display
  171. // as bytes
  172. */
  173. Strings_CreateDataBufferString(pReportBuffer -> pBuffer,
  174. pReportBuffer -> iBufferSize,
  175. pReportBuffer -> iBufferSize,
  176. 1,
  177. &BufferString);
  178. if (NULL == BufferString)
  179. {
  180. SetWindowText(hEditBox, "");
  181. }
  182. else
  183. {
  184. SetWindowText(hEditBox, BufferString);
  185. free(BufferString);
  186. }
  187. }
  188. return;
  189. }
  190. BOOLEAN
  191. BufferDisplay_UpdateBuffer(
  192. IN PBUFFER_DISPLAY pBufferDisplay,
  193. IN PCHAR pNewBuffer
  194. )
  195. /*++
  196. Routine Description:
  197. This routine changes the data of the currently active report buffer for the
  198. given buffer display structure.
  199. It returns FALSE if it needed to allocate a new buffer and the memory allocation
  200. failed.
  201. --*/
  202. {
  203. PREPORT_BUFFER pCurrentReport;
  204. pCurrentReport = CURRENT_REPORT(pBufferDisplay);
  205. if (NULL == pCurrentReport -> pBuffer)
  206. {
  207. pCurrentReport -> pBuffer = malloc(pBufferDisplay -> iBufferSize);
  208. if ((NULL == pCurrentReport) || (NULL == pCurrentReport -> pBuffer))
  209. {
  210. return (FALSE);
  211. }
  212. pCurrentReport -> iBufferSize = pBufferDisplay -> iBufferSize;
  213. }
  214. memmove (pCurrentReport -> pBuffer, pNewBuffer, pCurrentReport -> iBufferSize);
  215. BufferDisplay_OutputBuffer(pBufferDisplay -> hBufferEditBox, pCurrentReport);
  216. return (TRUE);
  217. }
  218. INT
  219. BufferDisplay_GetBufferSize(
  220. IN PBUFFER_DISPLAY pBufferDisplay
  221. )
  222. /*++
  223. Routine Description:
  224. This routine simply returns the size of the given buffer
  225. --*/
  226. {
  227. return (pBufferDisplay -> iBufferSize);
  228. }
  229. VOID
  230. BufferDisplay_CopyCurrentBuffer(
  231. IN PBUFFER_DISPLAY pBufferDisplay,
  232. OUT PCHAR pCopyBuffer
  233. )
  234. /*++
  235. Routine Description:
  236. This routine copies the currently active buffer for the given buffer display
  237. into the buffer passed in by the caller.
  238. It is the caller's responsibility to allocate a buffer of the appropriate size
  239. The appropriate size can be obtain by calling BufferDisplay_GetBufferSize
  240. --*/
  241. {
  242. PREPORT_BUFFER pCurrentReport;
  243. pCurrentReport = CURRENT_REPORT(pBufferDisplay);
  244. if (NULL == pCurrentReport -> pBuffer)
  245. {
  246. memset(pCopyBuffer, 0x0, pBufferDisplay -> iBufferSize);
  247. }
  248. else
  249. {
  250. memcpy(pCopyBuffer, pCurrentReport -> pBuffer, pCurrentReport -> iBufferSize);
  251. }
  252. return;
  253. }
  254. INT
  255. BufferDisplay_GetCurrentBufferNumber(
  256. IN PBUFFER_DISPLAY pBufferDisplay
  257. )
  258. /*++
  259. Routine Description:
  260. This routine returns the buffer number of the current buffer selection
  261. --*/
  262. {
  263. return (pBufferDisplay -> iCurrSelectionIndex);
  264. }
  265. UCHAR
  266. BufferDisplay_GetCurrentReportID(
  267. IN PBUFFER_DISPLAY pBufferDisplay
  268. )
  269. /*++
  270. Routine Description:
  271. This routine returns the report ID of the current buffer selection
  272. --*/
  273. {
  274. PREPORT_BUFFER pCurrentReport;
  275. pCurrentReport = CURRENT_REPORT(pBufferDisplay);
  276. return (pCurrentReport -> ucReportID);
  277. }
  278. VOID
  279. BufferDisplay_ClearBuffer(
  280. IN PBUFFER_DISPLAY pBufferDisplay
  281. )
  282. /*++
  283. Routine Description:
  284. This routine frees the current report buffer and set's it to NULL
  285. --*/
  286. {
  287. PREPORT_BUFFER pCurrentReport;
  288. pCurrentReport = CURRENT_REPORT(pBufferDisplay);
  289. if (NULL != pCurrentReport -> pBuffer)
  290. {
  291. free(pCurrentReport -> pBuffer);
  292. pCurrentReport -> iBufferSize = 0;
  293. pCurrentReport -> ucReportID = 0;
  294. pCurrentReport -> pBuffer = NULL;
  295. }
  296. BufferDisplay_OutputBuffer(pBufferDisplay -> hBufferEditBox,
  297. pCurrentReport);
  298. return;
  299. }