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.

380 lines
9.6 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. if (CB_ERR == iNewIndex)
  144. {
  145. return;
  146. }
  147. iNewIndex = (INT) SendMessage(pBufferDisplay -> hBufferComboBox,
  148. CB_GETITEMDATA,
  149. iNewIndex,
  150. 0);
  151. if (CB_ERR == iNewIndex)
  152. {
  153. return;
  154. }
  155. pBufferDisplay -> iCurrSelectionIndex = iNewIndex;
  156. BufferDisplay_OutputBuffer(pBufferDisplay -> hBufferEditBox,
  157. &(pBufferDisplay -> ReportBuffers[iNewIndex]));
  158. return;
  159. }
  160. VOID
  161. BufferDisplay_OutputBuffer(
  162. HWND hEditBox,
  163. PREPORT_BUFFER pReportBuffer
  164. )
  165. /*++
  166. Routine Description:
  167. This routine outputs to hEditBox a byte representation of pReportBuffer
  168. --*/
  169. {
  170. PCHAR BufferString;
  171. if (0 == pReportBuffer -> iBufferSize || NULL == pReportBuffer -> pBuffer)
  172. {
  173. SetWindowText(hEditBox, "");
  174. }
  175. else
  176. {
  177. /*
  178. // Create a buffer string the size of the buffer and display
  179. // as bytes
  180. */
  181. Strings_CreateDataBufferString(pReportBuffer -> pBuffer,
  182. pReportBuffer -> iBufferSize,
  183. pReportBuffer -> iBufferSize,
  184. 1,
  185. &BufferString);
  186. if (NULL == BufferString)
  187. {
  188. SetWindowText(hEditBox, "");
  189. }
  190. else
  191. {
  192. SetWindowText(hEditBox, BufferString);
  193. free(BufferString);
  194. }
  195. }
  196. return;
  197. }
  198. BOOLEAN
  199. BufferDisplay_UpdateBuffer(
  200. IN PBUFFER_DISPLAY pBufferDisplay,
  201. IN PCHAR pNewBuffer
  202. )
  203. /*++
  204. Routine Description:
  205. This routine changes the data of the currently active report buffer for the
  206. given buffer display structure.
  207. It returns FALSE if it needed to allocate a new buffer and the memory allocation
  208. failed.
  209. --*/
  210. {
  211. PREPORT_BUFFER pCurrentReport;
  212. pCurrentReport = CURRENT_REPORT(pBufferDisplay);
  213. if (NULL == pCurrentReport -> pBuffer)
  214. {
  215. pCurrentReport -> pBuffer = malloc(pBufferDisplay -> iBufferSize);
  216. if ((NULL == pCurrentReport) || (NULL == pCurrentReport -> pBuffer))
  217. {
  218. return (FALSE);
  219. }
  220. pCurrentReport -> iBufferSize = pBufferDisplay -> iBufferSize;
  221. }
  222. memmove (pCurrentReport -> pBuffer, pNewBuffer, pCurrentReport -> iBufferSize);
  223. BufferDisplay_OutputBuffer(pBufferDisplay -> hBufferEditBox, pCurrentReport);
  224. return (TRUE);
  225. }
  226. INT
  227. BufferDisplay_GetBufferSize(
  228. IN PBUFFER_DISPLAY pBufferDisplay
  229. )
  230. /*++
  231. Routine Description:
  232. This routine simply returns the size of the given buffer
  233. --*/
  234. {
  235. return (pBufferDisplay -> iBufferSize);
  236. }
  237. VOID
  238. BufferDisplay_CopyCurrentBuffer(
  239. IN PBUFFER_DISPLAY pBufferDisplay,
  240. OUT PCHAR pCopyBuffer
  241. )
  242. /*++
  243. Routine Description:
  244. This routine copies the currently active buffer for the given buffer display
  245. into the buffer passed in by the caller.
  246. It is the caller's responsibility to allocate a buffer of the appropriate size
  247. The appropriate size can be obtain by calling BufferDisplay_GetBufferSize
  248. --*/
  249. {
  250. PREPORT_BUFFER pCurrentReport;
  251. pCurrentReport = CURRENT_REPORT(pBufferDisplay);
  252. if (NULL == pCurrentReport -> pBuffer)
  253. {
  254. memset(pCopyBuffer, 0x0, pBufferDisplay -> iBufferSize);
  255. }
  256. else
  257. {
  258. memcpy(pCopyBuffer, pCurrentReport -> pBuffer, pCurrentReport -> iBufferSize);
  259. }
  260. return;
  261. }
  262. INT
  263. BufferDisplay_GetCurrentBufferNumber(
  264. IN PBUFFER_DISPLAY pBufferDisplay
  265. )
  266. /*++
  267. Routine Description:
  268. This routine returns the buffer number of the current buffer selection
  269. --*/
  270. {
  271. return (pBufferDisplay -> iCurrSelectionIndex);
  272. }
  273. UCHAR
  274. BufferDisplay_GetCurrentReportID(
  275. IN PBUFFER_DISPLAY pBufferDisplay
  276. )
  277. /*++
  278. Routine Description:
  279. This routine returns the report ID of the current buffer selection
  280. --*/
  281. {
  282. PREPORT_BUFFER pCurrentReport;
  283. pCurrentReport = CURRENT_REPORT(pBufferDisplay);
  284. return (pCurrentReport -> ucReportID);
  285. }
  286. VOID
  287. BufferDisplay_ClearBuffer(
  288. IN PBUFFER_DISPLAY pBufferDisplay
  289. )
  290. /*++
  291. Routine Description:
  292. This routine frees the current report buffer and set's it to NULL
  293. --*/
  294. {
  295. PREPORT_BUFFER pCurrentReport;
  296. pCurrentReport = CURRENT_REPORT(pBufferDisplay);
  297. if (NULL != pCurrentReport -> pBuffer)
  298. {
  299. free(pCurrentReport -> pBuffer);
  300. pCurrentReport -> iBufferSize = 0;
  301. pCurrentReport -> ucReportID = 0;
  302. pCurrentReport -> pBuffer = NULL;
  303. }
  304. BufferDisplay_OutputBuffer(pBufferDisplay -> hBufferEditBox,
  305. pCurrentReport);
  306. return;
  307. }