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.

337 lines
7.9 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Raw Input API sample showing joystick support
  4. //
  5. // Author: Alexander B�cken
  6. // Date: 04/22/2011
  7. //
  8. // Copyright 2011 Alexander B�cken
  9. //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include <Windows.h>
  12. #include <tchar.h>
  13. #define _USE_MATH_DEFINES
  14. #ifndef M_E
  15. #include <math.h>
  16. #endif
  17. #include <hidsdi.h>
  18. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  19. #define WC_MAINFRAME TEXT("MainFrame")
  20. #define MAX_BUTTONS 128
  21. #define CHECK(exp) { if(!(exp)) goto Error; }
  22. #define SAFE_FREE(p) { if(p) { HeapFree(hHeap, 0, p); (p) = NULL; } }
  23. //
  24. // Global variables
  25. //
  26. BOOL bButtonStates[MAX_BUTTONS];
  27. LONG lAxisX;
  28. LONG lAxisY;
  29. LONG lAxisZ;
  30. LONG lAxisRz;
  31. LONG lHat;
  32. INT g_NumberOfButtons;
  33. void ParseRawInput(PRAWINPUT pRawInput)
  34. {
  35. PHIDP_PREPARSED_DATA pPreparsedData;
  36. HIDP_CAPS Caps;
  37. PHIDP_BUTTON_CAPS pButtonCaps;
  38. PHIDP_VALUE_CAPS pValueCaps;
  39. USHORT capsLength;
  40. UINT bufferSize;
  41. HANDLE hHeap;
  42. USAGE usage[MAX_BUTTONS];
  43. ULONG i, usageLength, value;
  44. pPreparsedData = NULL;
  45. pButtonCaps = NULL;
  46. pValueCaps = NULL;
  47. hHeap = GetProcessHeap();
  48. //
  49. // Get the preparsed data block
  50. //
  51. CHECK( GetRawInputDeviceInfo(pRawInput->header.hDevice, RIDI_PREPARSEDDATA, NULL, &bufferSize) == 0 );
  52. CHECK( pPreparsedData = (PHIDP_PREPARSED_DATA)HeapAlloc(hHeap, 0, bufferSize) );
  53. CHECK( (int)GetRawInputDeviceInfo(pRawInput->header.hDevice, RIDI_PREPARSEDDATA, pPreparsedData, &bufferSize) >= 0 );
  54. //
  55. // Get the joystick's capabilities
  56. //
  57. // Button caps
  58. CHECK( HidP_GetCaps(pPreparsedData, &Caps) == HIDP_STATUS_SUCCESS )
  59. CHECK( pButtonCaps = (PHIDP_BUTTON_CAPS)HeapAlloc(hHeap, 0, sizeof(HIDP_BUTTON_CAPS) * Caps.NumberInputButtonCaps) );
  60. capsLength = Caps.NumberInputButtonCaps;
  61. CHECK( HidP_GetButtonCaps(HidP_Input, pButtonCaps, &capsLength, pPreparsedData) == HIDP_STATUS_SUCCESS )
  62. g_NumberOfButtons = pButtonCaps->Range.UsageMax - pButtonCaps->Range.UsageMin + 1;
  63. // Value caps
  64. CHECK( pValueCaps = (PHIDP_VALUE_CAPS)HeapAlloc(hHeap, 0, sizeof(HIDP_VALUE_CAPS) * Caps.NumberInputValueCaps) );
  65. capsLength = Caps.NumberInputValueCaps;
  66. CHECK( HidP_GetValueCaps(HidP_Input, pValueCaps, &capsLength, pPreparsedData) == HIDP_STATUS_SUCCESS )
  67. //
  68. // Get the pressed buttons
  69. //
  70. usageLength = g_NumberOfButtons;
  71. CHECK(
  72. HidP_GetUsages(
  73. HidP_Input, pButtonCaps->UsagePage, 0, usage, &usageLength, pPreparsedData,
  74. (PCHAR)pRawInput->data.hid.bRawData, pRawInput->data.hid.dwSizeHid
  75. ) == HIDP_STATUS_SUCCESS );
  76. ZeroMemory(bButtonStates, sizeof(bButtonStates));
  77. for(i = 0; i < usageLength; i++)
  78. bButtonStates[usage[i] - pButtonCaps->Range.UsageMin] = TRUE;
  79. //
  80. // Get the state of discrete-valued-controls
  81. //
  82. for(i = 0; i < Caps.NumberInputValueCaps; i++)
  83. {
  84. CHECK(
  85. HidP_GetUsageValue(
  86. HidP_Input, pValueCaps[i].UsagePage, 0, pValueCaps[i].Range.UsageMin, &value, pPreparsedData,
  87. (PCHAR)pRawInput->data.hid.bRawData, pRawInput->data.hid.dwSizeHid
  88. ) == HIDP_STATUS_SUCCESS );
  89. switch(pValueCaps[i].Range.UsageMin)
  90. {
  91. case 0x30: // X-axis
  92. lAxisX = (LONG)value - 128;
  93. break;
  94. case 0x31: // Y-axis
  95. lAxisY = (LONG)value - 128;
  96. break;
  97. case 0x32: // Z-axis
  98. lAxisZ = (LONG)value - 128;
  99. break;
  100. case 0x35: // Rotate-Z
  101. lAxisRz = (LONG)value - 128;
  102. break;
  103. case 0x39: // Hat Switch
  104. lHat = value;
  105. break;
  106. }
  107. }
  108. //
  109. // Clean up
  110. //
  111. Error:
  112. SAFE_FREE(pPreparsedData);
  113. SAFE_FREE(pButtonCaps);
  114. SAFE_FREE(pValueCaps);
  115. }
  116. void DrawButton(HDC hDC, int i, int x, int y, BOOL bPressed)
  117. {
  118. HBRUSH hOldBrush, hBr;
  119. TCHAR sz[4];
  120. RECT rc;
  121. if(bPressed)
  122. {
  123. hBr = CreateSolidBrush(RGB(192, 0, 0));
  124. hOldBrush = (HBRUSH)SelectObject(hDC, hBr);
  125. }
  126. rc.left = x;
  127. rc.top = y;
  128. rc.right = x + 30;
  129. rc.bottom = y + 30;
  130. Ellipse(hDC, rc.left, rc.top, rc.right, rc.bottom);
  131. _stprintf_s(sz, ARRAY_SIZE(sz), TEXT("%d"), i);
  132. DrawText(hDC, sz, -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  133. if(bPressed)
  134. {
  135. SelectObject(hDC, hOldBrush);
  136. DeleteObject(hBr);
  137. }
  138. }
  139. void DrawCrosshair(HDC hDC, int x, int y, LONG xVal, LONG yVal)
  140. {
  141. Rectangle(hDC, x, y, x + 256, y + 256);
  142. MoveToEx(hDC, x + xVal - 5 + 128, y + yVal + 128, NULL);
  143. LineTo(hDC, x + xVal + 5 + 128, y + yVal + 128);
  144. MoveToEx(hDC, x + xVal + 128, y + yVal - 5 + 128, NULL);
  145. LineTo(hDC, x + xVal + 128, y + yVal + 5 + 128);
  146. }
  147. void DrawDPad(HDC hDC, int x, int y, LONG value)
  148. {
  149. LONG i;
  150. for(i = 0; i < 8; i++)
  151. {
  152. HBRUSH hOldBrush;
  153. HBRUSH hBr;
  154. int xPos = (int)(sin(-2 * M_PI * i / 8 + M_PI) * 80.0) + 80;
  155. int yPos = (int)(cos(2 * M_PI * i / 8 + M_PI) * 80.0) + 80;
  156. if(value == i)
  157. {
  158. hBr = CreateSolidBrush(RGB(192, 0, 0));
  159. hOldBrush = (HBRUSH)SelectObject(hDC, hBr);
  160. }
  161. Ellipse(hDC, x + xPos, y + yPos, x + xPos + 20, y + yPos + 20);
  162. if(value == i)
  163. {
  164. SelectObject(hDC, hOldBrush);
  165. DeleteObject(hBr);
  166. }
  167. }
  168. }
  169. LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  170. {
  171. switch(msg)
  172. {
  173. case WM_CREATE:
  174. {
  175. //
  176. // Register for joystick devices
  177. //
  178. RAWINPUTDEVICE rid;
  179. rid.usUsagePage = 1;
  180. rid.usUsage = 4; // Joystick
  181. rid.dwFlags = 0;
  182. rid.hwndTarget = hWnd;
  183. if(!RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE)))
  184. return -1;
  185. }
  186. return 0;
  187. case WM_INPUT:
  188. {
  189. //
  190. // Get the pointer to the raw device data, process it and update the window
  191. //
  192. PRAWINPUT pRawInput;
  193. UINT bufferSize;
  194. HANDLE hHeap;
  195. GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &bufferSize, sizeof(RAWINPUTHEADER));
  196. hHeap = GetProcessHeap();
  197. pRawInput = (PRAWINPUT)HeapAlloc(hHeap, 0, bufferSize);
  198. if(!pRawInput)
  199. return 0;
  200. GetRawInputData((HRAWINPUT)lParam, RID_INPUT, pRawInput, &bufferSize, sizeof(RAWINPUTHEADER));
  201. ParseRawInput(pRawInput);
  202. HeapFree(hHeap, 0, pRawInput);
  203. InvalidateRect(hWnd, NULL, TRUE);
  204. UpdateWindow(hWnd);
  205. }
  206. return 0;
  207. case WM_PAINT:
  208. {
  209. //
  210. // Draw the buttons and axis-values
  211. //
  212. PAINTSTRUCT ps;
  213. HDC hDC;
  214. int i;
  215. hDC = BeginPaint(hWnd, &ps);
  216. SetBkMode(hDC, TRANSPARENT);
  217. for(i = 0; i < g_NumberOfButtons; i++)
  218. DrawButton(hDC, i+1, 20 + i * 40, 20, bButtonStates[i]);
  219. DrawCrosshair(hDC, 20, 100, lAxisX, lAxisY);
  220. DrawCrosshair(hDC, 296, 100, lAxisZ, lAxisRz);
  221. DrawDPad(hDC, 600, 140, lHat);
  222. EndPaint(hWnd, &ps);
  223. }
  224. return 0;
  225. case WM_DESTROY:
  226. PostQuitMessage(0);
  227. return 0;
  228. }
  229. return DefWindowProc(hWnd, msg, wParam, lParam);
  230. }
  231. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  232. {
  233. HWND hWnd;
  234. MSG msg;
  235. WNDCLASSEX wcex;
  236. //
  237. // Register window class
  238. //
  239. wcex.cbSize = sizeof(WNDCLASSEX);
  240. wcex.cbClsExtra = 0;
  241. wcex.cbWndExtra = 0;
  242. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  243. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  244. wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  245. wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  246. wcex.hInstance = hInstance;
  247. wcex.lpfnWndProc = WindowProc;
  248. wcex.lpszClassName = WC_MAINFRAME;
  249. wcex.lpszMenuName = NULL;
  250. wcex.style = CS_HREDRAW | CS_VREDRAW;
  251. if(!RegisterClassEx(&wcex))
  252. return -1;
  253. //
  254. // Create window
  255. //
  256. hWnd = CreateWindow(WC_MAINFRAME, TEXT("Joystick using Raw Input API"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
  257. ShowWindow(hWnd, nShowCmd);
  258. UpdateWindow(hWnd);
  259. //
  260. // Message loop
  261. //
  262. while(GetMessage(&msg, NULL, 0, 0))
  263. {
  264. TranslateMessage(&msg);
  265. DispatchMessage(&msg);
  266. }
  267. return (int)msg.wParam;
  268. }