SDL_windowsjoystick.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2017 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_JOYSTICK_DINPUT || SDL_JOYSTICK_XINPUT
  20. /* DirectInput joystick driver; written by Glenn Maynard, based on Andrei de
  21. * A. Formiga's WINMM driver.
  22. *
  23. * Hats and sliders are completely untested; the app I'm writing this for mostly
  24. * doesn't use them and I don't own any joysticks with them.
  25. *
  26. * We don't bother to use event notification here. It doesn't seem to work
  27. * with polled devices, and it's fine to call IDirectInputDevice8_GetDeviceData and
  28. * let it return 0 events. */
  29. #include "SDL_error.h"
  30. #include "SDL_assert.h"
  31. #include "SDL_events.h"
  32. #include "SDL_timer.h"
  33. #include "SDL_mutex.h"
  34. #include "SDL_joystick.h"
  35. #include "../SDL_sysjoystick.h"
  36. #include "../../thread/SDL_systhread.h"
  37. #include "../../core/windows/SDL_windows.h"
  38. #if !defined(__WINRT__)
  39. #include <dbt.h>
  40. #endif
  41. #define INITGUID /* Only set here, if set twice will cause mingw32 to break. */
  42. #include "SDL_windowsjoystick_c.h"
  43. #include "SDL_dinputjoystick_c.h"
  44. #include "SDL_xinputjoystick_c.h"
  45. #include "../../haptic/windows/SDL_dinputhaptic_c.h" /* For haptic hot plugging */
  46. #include "../../haptic/windows/SDL_xinputhaptic_c.h" /* For haptic hot plugging */
  47. #ifndef DEVICE_NOTIFY_WINDOW_HANDLE
  48. #define DEVICE_NOTIFY_WINDOW_HANDLE 0x00000000
  49. #endif
  50. /* local variables */
  51. static SDL_bool s_bDeviceAdded = SDL_FALSE;
  52. static SDL_bool s_bDeviceRemoved = SDL_FALSE;
  53. static SDL_JoystickID s_nInstanceID = -1;
  54. static SDL_cond *s_condJoystickThread = NULL;
  55. static SDL_mutex *s_mutexJoyStickEnum = NULL;
  56. static SDL_Thread *s_threadJoystick = NULL;
  57. static SDL_bool s_bJoystickThreadQuit = SDL_FALSE;
  58. JoyStick_DeviceData *SYS_Joystick; /* array to hold joystick ID values */
  59. static SDL_bool s_bWindowsDeviceChanged = SDL_FALSE;
  60. #ifdef __WINRT__
  61. typedef struct
  62. {
  63. int unused;
  64. } SDL_DeviceNotificationData;
  65. static void
  66. SDL_CleanupDeviceNotification(SDL_DeviceNotificationData *data)
  67. {
  68. }
  69. static int
  70. SDL_CreateDeviceNotification(SDL_DeviceNotificationData *data)
  71. {
  72. return 0;
  73. }
  74. static void
  75. SDL_CheckDeviceNotification(SDL_DeviceNotificationData *data)
  76. {
  77. }
  78. #else /* !__WINRT__ */
  79. typedef struct
  80. {
  81. HRESULT coinitialized;
  82. WNDCLASSEX wincl;
  83. HWND messageWindow;
  84. HDEVNOTIFY hNotify;
  85. } SDL_DeviceNotificationData;
  86. /* windowproc for our joystick detect thread message only window, to detect any USB device addition/removal */
  87. static LRESULT CALLBACK
  88. SDL_PrivateJoystickDetectProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  89. {
  90. switch (message) {
  91. case WM_DEVICECHANGE:
  92. switch (wParam) {
  93. case DBT_DEVICEARRIVAL:
  94. if (((DEV_BROADCAST_HDR*)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
  95. s_bWindowsDeviceChanged = SDL_TRUE;
  96. }
  97. break;
  98. case DBT_DEVICEREMOVECOMPLETE:
  99. if (((DEV_BROADCAST_HDR*)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
  100. s_bWindowsDeviceChanged = SDL_TRUE;
  101. }
  102. break;
  103. }
  104. return 0;
  105. }
  106. return DefWindowProc (hwnd, message, wParam, lParam);
  107. }
  108. static void
  109. SDL_CleanupDeviceNotification(SDL_DeviceNotificationData *data)
  110. {
  111. if (data->hNotify)
  112. UnregisterDeviceNotification(data->hNotify);
  113. if (data->messageWindow)
  114. DestroyWindow(data->messageWindow);
  115. UnregisterClass(data->wincl.lpszClassName, data->wincl.hInstance);
  116. if (data->coinitialized == S_OK) {
  117. WIN_CoUninitialize();
  118. }
  119. }
  120. static int
  121. SDL_CreateDeviceNotification(SDL_DeviceNotificationData *data)
  122. {
  123. DEV_BROADCAST_DEVICEINTERFACE dbh;
  124. GUID GUID_DEVINTERFACE_HID = { 0x4D1E55B2L, 0xF16F, 0x11CF, { 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 } };
  125. SDL_zerop(data);
  126. data->coinitialized = WIN_CoInitialize();
  127. data->wincl.hInstance = GetModuleHandle(NULL);
  128. data->wincl.lpszClassName = L"Message";
  129. data->wincl.lpfnWndProc = SDL_PrivateJoystickDetectProc; /* This function is called by windows */
  130. data->wincl.cbSize = sizeof (WNDCLASSEX);
  131. if (!RegisterClassEx(&data->wincl)) {
  132. WIN_SetError("Failed to create register class for joystick autodetect");
  133. SDL_CleanupDeviceNotification(data);
  134. return -1;
  135. }
  136. data->messageWindow = (HWND)CreateWindowEx(0, L"Message", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
  137. if (!data->messageWindow) {
  138. WIN_SetError("Failed to create message window for joystick autodetect");
  139. SDL_CleanupDeviceNotification(data);
  140. return -1;
  141. }
  142. SDL_zero(dbh);
  143. dbh.dbcc_size = sizeof(dbh);
  144. dbh.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  145. dbh.dbcc_classguid = GUID_DEVINTERFACE_HID;
  146. data->hNotify = RegisterDeviceNotification(data->messageWindow, &dbh, DEVICE_NOTIFY_WINDOW_HANDLE);
  147. if (!data->hNotify) {
  148. WIN_SetError("Failed to create notify device for joystick autodetect");
  149. SDL_CleanupDeviceNotification(data);
  150. return -1;
  151. }
  152. return 0;
  153. }
  154. static void
  155. SDL_CheckDeviceNotification(SDL_DeviceNotificationData *data)
  156. {
  157. MSG msg;
  158. if (!data->messageWindow) {
  159. return;
  160. }
  161. while (PeekMessage(&msg, data->messageWindow, 0, 0, PM_NOREMOVE)) {
  162. if (GetMessage(&msg, data->messageWindow, 0, 0) != 0) {
  163. TranslateMessage(&msg);
  164. DispatchMessage(&msg);
  165. }
  166. }
  167. }
  168. #endif /* __WINRT__ */
  169. /* Function/thread to scan the system for joysticks. */
  170. static int
  171. SDL_JoystickThread(void *_data)
  172. {
  173. SDL_DeviceNotificationData notification_data;
  174. #if SDL_JOYSTICK_XINPUT
  175. SDL_bool bOpenedXInputDevices[XUSER_MAX_COUNT];
  176. SDL_zero(bOpenedXInputDevices);
  177. #endif
  178. if (SDL_CreateDeviceNotification(&notification_data) < 0) {
  179. return -1;
  180. }
  181. SDL_LockMutex(s_mutexJoyStickEnum);
  182. while (s_bJoystickThreadQuit == SDL_FALSE) {
  183. SDL_bool bXInputChanged = SDL_FALSE;
  184. SDL_CondWaitTimeout(s_condJoystickThread, s_mutexJoyStickEnum, 300);
  185. SDL_CheckDeviceNotification(&notification_data);
  186. #if SDL_JOYSTICK_XINPUT
  187. if (SDL_XINPUT_Enabled() && XINPUTGETCAPABILITIES) {
  188. /* scan for any change in XInput devices */
  189. Uint8 userId;
  190. for (userId = 0; userId < XUSER_MAX_COUNT; userId++) {
  191. XINPUT_CAPABILITIES capabilities;
  192. const DWORD result = XINPUTGETCAPABILITIES(userId, XINPUT_FLAG_GAMEPAD, &capabilities);
  193. const SDL_bool available = (result == ERROR_SUCCESS);
  194. if (bOpenedXInputDevices[userId] != available) {
  195. bXInputChanged = SDL_TRUE;
  196. bOpenedXInputDevices[userId] = available;
  197. }
  198. }
  199. }
  200. #endif /* SDL_JOYSTICK_XINPUT */
  201. if (s_bWindowsDeviceChanged || bXInputChanged) {
  202. SDL_UnlockMutex(s_mutexJoyStickEnum); /* let main thread go while we SDL_Delay(). */
  203. SDL_Delay(300); /* wait for direct input to find out about this device */
  204. SDL_LockMutex(s_mutexJoyStickEnum);
  205. s_bDeviceRemoved = SDL_TRUE;
  206. s_bDeviceAdded = SDL_TRUE;
  207. s_bWindowsDeviceChanged = SDL_FALSE;
  208. }
  209. }
  210. SDL_UnlockMutex(s_mutexJoyStickEnum);
  211. SDL_CleanupDeviceNotification(&notification_data);
  212. return 1;
  213. }
  214. void SDL_SYS_AddJoystickDevice(JoyStick_DeviceData *device)
  215. {
  216. device->send_add_event = SDL_TRUE;
  217. device->nInstanceID = ++s_nInstanceID;
  218. device->pNext = SYS_Joystick;
  219. SYS_Joystick = device;
  220. s_bDeviceAdded = SDL_TRUE;
  221. }
  222. /* Function to scan the system for joysticks.
  223. * Joystick 0 should be the system default joystick.
  224. * It should return 0, or -1 on an unrecoverable fatal error.
  225. */
  226. int
  227. SDL_SYS_JoystickInit(void)
  228. {
  229. if (SDL_DINPUT_JoystickInit() < 0) {
  230. SDL_SYS_JoystickQuit();
  231. return -1;
  232. }
  233. if (SDL_XINPUT_JoystickInit() < 0) {
  234. SDL_SYS_JoystickQuit();
  235. return -1;
  236. }
  237. s_mutexJoyStickEnum = SDL_CreateMutex();
  238. s_condJoystickThread = SDL_CreateCond();
  239. s_bDeviceAdded = SDL_TRUE; /* force a scan of the system for joysticks this first time */
  240. SDL_SYS_JoystickDetect();
  241. if (!s_threadJoystick) {
  242. /* spin up the thread to detect hotplug of devices */
  243. s_bJoystickThreadQuit = SDL_FALSE;
  244. s_threadJoystick = SDL_CreateThreadInternal(SDL_JoystickThread, "SDL_joystick", 64 * 1024, NULL);
  245. }
  246. return SDL_SYS_NumJoysticks();
  247. }
  248. /* return the number of joysticks that are connected right now */
  249. int
  250. SDL_SYS_NumJoysticks(void)
  251. {
  252. int nJoysticks = 0;
  253. JoyStick_DeviceData *device = SYS_Joystick;
  254. while (device) {
  255. nJoysticks++;
  256. device = device->pNext;
  257. }
  258. return nJoysticks;
  259. }
  260. /* detect any new joysticks being inserted into the system */
  261. void
  262. SDL_SYS_JoystickDetect(void)
  263. {
  264. JoyStick_DeviceData *pCurList = NULL;
  265. /* only enum the devices if the joystick thread told us something changed */
  266. if (!s_bDeviceAdded && !s_bDeviceRemoved) {
  267. return; /* thread hasn't signaled, nothing to do right now. */
  268. }
  269. SDL_LockMutex(s_mutexJoyStickEnum);
  270. s_bDeviceAdded = SDL_FALSE;
  271. s_bDeviceRemoved = SDL_FALSE;
  272. pCurList = SYS_Joystick;
  273. SYS_Joystick = NULL;
  274. /* Look for DirectInput joysticks, wheels, head trackers, gamepads, etc.. */
  275. SDL_DINPUT_JoystickDetect(&pCurList);
  276. /* Look for XInput devices. Do this last, so they're first in the final list. */
  277. SDL_XINPUT_JoystickDetect(&pCurList);
  278. SDL_UnlockMutex(s_mutexJoyStickEnum);
  279. while (pCurList) {
  280. JoyStick_DeviceData *pListNext = NULL;
  281. if (pCurList->bXInputDevice) {
  282. SDL_XINPUT_MaybeRemoveDevice(pCurList->XInputUserId);
  283. } else {
  284. SDL_DINPUT_MaybeRemoveDevice(&pCurList->dxdevice);
  285. }
  286. SDL_PrivateJoystickRemoved(pCurList->nInstanceID);
  287. pListNext = pCurList->pNext;
  288. SDL_free(pCurList->joystickname);
  289. SDL_free(pCurList);
  290. pCurList = pListNext;
  291. }
  292. if (s_bDeviceAdded) {
  293. JoyStick_DeviceData *pNewJoystick;
  294. int device_index = 0;
  295. s_bDeviceAdded = SDL_FALSE;
  296. pNewJoystick = SYS_Joystick;
  297. while (pNewJoystick) {
  298. if (pNewJoystick->send_add_event) {
  299. if (pNewJoystick->bXInputDevice) {
  300. SDL_XINPUT_MaybeAddDevice(pNewJoystick->XInputUserId);
  301. } else {
  302. SDL_DINPUT_MaybeAddDevice(&pNewJoystick->dxdevice);
  303. }
  304. SDL_PrivateJoystickAdded(device_index);
  305. pNewJoystick->send_add_event = SDL_FALSE;
  306. }
  307. device_index++;
  308. pNewJoystick = pNewJoystick->pNext;
  309. }
  310. }
  311. }
  312. /* Function to get the device-dependent name of a joystick */
  313. const char *
  314. SDL_SYS_JoystickNameForDeviceIndex(int device_index)
  315. {
  316. JoyStick_DeviceData *device = SYS_Joystick;
  317. for (; device_index > 0; device_index--)
  318. device = device->pNext;
  319. return device->joystickname;
  320. }
  321. /* Function to perform the mapping between current device instance and this joysticks instance id */
  322. SDL_JoystickID
  323. SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
  324. {
  325. JoyStick_DeviceData *device = SYS_Joystick;
  326. int index;
  327. for (index = device_index; index > 0; index--)
  328. device = device->pNext;
  329. return device->nInstanceID;
  330. }
  331. /* Function to open a joystick for use.
  332. The joystick to open is specified by the device index.
  333. This should fill the nbuttons and naxes fields of the joystick structure.
  334. It returns 0, or -1 if there is an error.
  335. */
  336. int
  337. SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
  338. {
  339. JoyStick_DeviceData *joystickdevice = SYS_Joystick;
  340. for (; device_index > 0; device_index--)
  341. joystickdevice = joystickdevice->pNext;
  342. /* allocate memory for system specific hardware data */
  343. joystick->instance_id = joystickdevice->nInstanceID;
  344. joystick->hwdata =
  345. (struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata));
  346. if (joystick->hwdata == NULL) {
  347. return SDL_OutOfMemory();
  348. }
  349. SDL_zerop(joystick->hwdata);
  350. joystick->hwdata->guid = joystickdevice->guid;
  351. if (joystickdevice->bXInputDevice) {
  352. return SDL_XINPUT_JoystickOpen(joystick, joystickdevice);
  353. } else {
  354. return SDL_DINPUT_JoystickOpen(joystick, joystickdevice);
  355. }
  356. }
  357. /* return true if this joystick is plugged in right now */
  358. SDL_bool
  359. SDL_SYS_JoystickAttached(SDL_Joystick * joystick)
  360. {
  361. return joystick->hwdata && !joystick->hwdata->removed;
  362. }
  363. void
  364. SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
  365. {
  366. if (!joystick->hwdata || joystick->hwdata->removed) {
  367. return;
  368. }
  369. if (joystick->hwdata->bXInputDevice) {
  370. SDL_XINPUT_JoystickUpdate(joystick);
  371. } else {
  372. SDL_DINPUT_JoystickUpdate(joystick);
  373. }
  374. if (joystick->hwdata->removed) {
  375. joystick->force_recentering = SDL_TRUE;
  376. }
  377. }
  378. /* Function to close a joystick after use */
  379. void
  380. SDL_SYS_JoystickClose(SDL_Joystick * joystick)
  381. {
  382. if (joystick->hwdata->bXInputDevice) {
  383. SDL_XINPUT_JoystickClose(joystick);
  384. } else {
  385. SDL_DINPUT_JoystickClose(joystick);
  386. }
  387. SDL_free(joystick->hwdata);
  388. }
  389. /* Function to perform any system-specific joystick related cleanup */
  390. void
  391. SDL_SYS_JoystickQuit(void)
  392. {
  393. JoyStick_DeviceData *device = SYS_Joystick;
  394. while (device) {
  395. JoyStick_DeviceData *device_next = device->pNext;
  396. SDL_free(device->joystickname);
  397. SDL_free(device);
  398. device = device_next;
  399. }
  400. SYS_Joystick = NULL;
  401. if (s_threadJoystick) {
  402. SDL_LockMutex(s_mutexJoyStickEnum);
  403. s_bJoystickThreadQuit = SDL_TRUE;
  404. SDL_CondBroadcast(s_condJoystickThread); /* signal the joystick thread to quit */
  405. SDL_UnlockMutex(s_mutexJoyStickEnum);
  406. SDL_WaitThread(s_threadJoystick, NULL); /* wait for it to bugger off */
  407. SDL_DestroyMutex(s_mutexJoyStickEnum);
  408. SDL_DestroyCond(s_condJoystickThread);
  409. s_condJoystickThread= NULL;
  410. s_mutexJoyStickEnum = NULL;
  411. s_threadJoystick = NULL;
  412. }
  413. SDL_DINPUT_JoystickQuit();
  414. SDL_XINPUT_JoystickQuit();
  415. s_bDeviceAdded = SDL_FALSE;
  416. s_bDeviceRemoved = SDL_FALSE;
  417. }
  418. /* return the stable device guid for this device index */
  419. SDL_JoystickGUID
  420. SDL_SYS_JoystickGetDeviceGUID(int device_index)
  421. {
  422. JoyStick_DeviceData *device = SYS_Joystick;
  423. int index;
  424. for (index = device_index; index > 0; index--)
  425. device = device->pNext;
  426. return device->guid;
  427. }
  428. SDL_JoystickGUID
  429. SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
  430. {
  431. return joystick->hwdata->guid;
  432. }
  433. #endif /* SDL_JOYSTICK_DINPUT || SDL_JOYSTICK_XINPUT */
  434. /* vi: set ts=4 sw=4 expandtab: */