SDL_windowsjoystick.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
  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_events.h"
  31. #include "SDL_hints.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__) && !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  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 "SDL_rawinputjoystick_c.h"
  46. #include "../../haptic/windows/SDL_dinputhaptic_c.h" /* For haptic hot plugging */
  47. #include "../../haptic/windows/SDL_xinputhaptic_c.h" /* For haptic hot plugging */
  48. #ifndef DEVICE_NOTIFY_WINDOW_HANDLE
  49. #define DEVICE_NOTIFY_WINDOW_HANDLE 0x00000000
  50. #endif
  51. /* CM_Register_Notification definitions */
  52. #define CR_SUCCESS (0x00000000)
  53. /* Set up for C function definitions, even when using C++ */
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. DECLARE_HANDLE(HCMNOTIFICATION);
  58. typedef HCMNOTIFICATION* PHCMNOTIFICATION;
  59. typedef enum _CM_NOTIFY_FILTER_TYPE {
  60. CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE = 0,
  61. CM_NOTIFY_FILTER_TYPE_DEVICEHANDLE,
  62. CM_NOTIFY_FILTER_TYPE_DEVICEINSTANCE,
  63. CM_NOTIFY_FILTER_TYPE_MAX
  64. } CM_NOTIFY_FILTER_TYPE, * PCM_NOTIFY_FILTER_TYPE;
  65. typedef struct _CM_NOTIFY_FILTER {
  66. DWORD cbSize;
  67. DWORD Flags;
  68. CM_NOTIFY_FILTER_TYPE FilterType;
  69. DWORD Reserved;
  70. union {
  71. struct {
  72. GUID ClassGuid;
  73. } DeviceInterface;
  74. struct {
  75. HANDLE hTarget;
  76. } DeviceHandle;
  77. struct {
  78. WCHAR InstanceId[200];
  79. } DeviceInstance;
  80. } u;
  81. } CM_NOTIFY_FILTER, * PCM_NOTIFY_FILTER;
  82. typedef enum _CM_NOTIFY_ACTION {
  83. CM_NOTIFY_ACTION_DEVICEINTERFACEARRIVAL = 0,
  84. CM_NOTIFY_ACTION_DEVICEINTERFACEREMOVAL,
  85. CM_NOTIFY_ACTION_DEVICEQUERYREMOVE,
  86. CM_NOTIFY_ACTION_DEVICEQUERYREMOVEFAILED,
  87. CM_NOTIFY_ACTION_DEVICEREMOVEPENDING,
  88. CM_NOTIFY_ACTION_DEVICEREMOVECOMPLETE,
  89. CM_NOTIFY_ACTION_DEVICECUSTOMEVENT,
  90. CM_NOTIFY_ACTION_DEVICEINSTANCEENUMERATED,
  91. CM_NOTIFY_ACTION_DEVICEINSTANCESTARTED,
  92. CM_NOTIFY_ACTION_DEVICEINSTANCEREMOVED,
  93. CM_NOTIFY_ACTION_MAX
  94. } CM_NOTIFY_ACTION, * PCM_NOTIFY_ACTION;
  95. typedef struct _CM_NOTIFY_EVENT_DATA {
  96. CM_NOTIFY_FILTER_TYPE FilterType;
  97. DWORD Reserved;
  98. union {
  99. struct {
  100. GUID ClassGuid;
  101. WCHAR SymbolicLink[ANYSIZE_ARRAY];
  102. } DeviceInterface;
  103. struct {
  104. GUID EventGuid;
  105. LONG NameOffset;
  106. DWORD DataSize;
  107. BYTE Data[ANYSIZE_ARRAY];
  108. } DeviceHandle;
  109. struct {
  110. WCHAR InstanceId[ANYSIZE_ARRAY];
  111. } DeviceInstance;
  112. } u;
  113. } CM_NOTIFY_EVENT_DATA, * PCM_NOTIFY_EVENT_DATA;
  114. typedef DWORD (CALLBACK *PCM_NOTIFY_CALLBACK)(HCMNOTIFICATION hNotify, PVOID Context, CM_NOTIFY_ACTION Action, PCM_NOTIFY_EVENT_DATA EventData, DWORD EventDataSize);
  115. typedef DWORD (WINAPI *CM_Register_NotificationFunc)(PCM_NOTIFY_FILTER pFilter, PVOID pContext, PCM_NOTIFY_CALLBACK pCallback, PHCMNOTIFICATION pNotifyContext);
  116. typedef DWORD (WINAPI *CM_Unregister_NotificationFunc)(HCMNOTIFICATION NotifyContext);
  117. /* local variables */
  118. static SDL_bool s_bJoystickThread = SDL_FALSE;
  119. static SDL_bool s_bWindowsDeviceChanged = SDL_FALSE;
  120. static SDL_cond *s_condJoystickThread = NULL;
  121. static SDL_mutex *s_mutexJoyStickEnum = NULL;
  122. static SDL_Thread *s_joystickThread = NULL;
  123. static SDL_bool s_bJoystickThreadQuit = SDL_FALSE;
  124. static GUID GUID_DEVINTERFACE_HID = { 0x4D1E55B2L, 0xF16F, 0x11CF, { 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 } };
  125. JoyStick_DeviceData *SYS_Joystick; /* array to hold joystick ID values */
  126. #if !defined(__WINRT__) && !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  127. static HMODULE cfgmgr32_lib_handle;
  128. static CM_Register_NotificationFunc CM_Register_Notification;
  129. static CM_Unregister_NotificationFunc CM_Unregister_Notification;
  130. static HCMNOTIFICATION s_DeviceNotificationFuncHandle;
  131. static DWORD CALLBACK
  132. SDL_DeviceNotificationFunc(HCMNOTIFICATION hNotify, PVOID context, CM_NOTIFY_ACTION action, PCM_NOTIFY_EVENT_DATA eventData, DWORD event_data_size)
  133. {
  134. if (action == CM_NOTIFY_ACTION_DEVICEINTERFACEARRIVAL ||
  135. action == CM_NOTIFY_ACTION_DEVICEINTERFACEREMOVAL) {
  136. s_bWindowsDeviceChanged = SDL_TRUE;
  137. }
  138. return ERROR_SUCCESS;
  139. }
  140. static void
  141. SDL_CleanupDeviceNotificationFunc(void)
  142. {
  143. if (cfgmgr32_lib_handle) {
  144. if (s_DeviceNotificationFuncHandle) {
  145. CM_Unregister_Notification(s_DeviceNotificationFuncHandle);
  146. s_DeviceNotificationFuncHandle = NULL;
  147. }
  148. FreeLibrary(cfgmgr32_lib_handle);
  149. cfgmgr32_lib_handle = NULL;
  150. }
  151. }
  152. static SDL_bool
  153. SDL_CreateDeviceNotificationFunc(void)
  154. {
  155. cfgmgr32_lib_handle = LoadLibraryA("cfgmgr32.dll");
  156. if (cfgmgr32_lib_handle) {
  157. CM_Register_Notification = (CM_Register_NotificationFunc)GetProcAddress(cfgmgr32_lib_handle, "CM_Register_Notification");
  158. CM_Unregister_Notification = (CM_Unregister_NotificationFunc)GetProcAddress(cfgmgr32_lib_handle, "CM_Unregister_Notification");
  159. if (CM_Register_Notification && CM_Unregister_Notification) {
  160. CM_NOTIFY_FILTER notify_filter;
  161. SDL_zero(notify_filter);
  162. notify_filter.cbSize = sizeof(notify_filter);
  163. notify_filter.FilterType = CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE;
  164. notify_filter.u.DeviceInterface.ClassGuid = GUID_DEVINTERFACE_HID;
  165. if (CM_Register_Notification(&notify_filter, NULL, SDL_DeviceNotificationFunc, &s_DeviceNotificationFuncHandle) == CR_SUCCESS) {
  166. return SDL_TRUE;
  167. }
  168. }
  169. }
  170. SDL_CleanupDeviceNotificationFunc();
  171. return SDL_FALSE;
  172. }
  173. typedef struct
  174. {
  175. HRESULT coinitialized;
  176. WNDCLASSEX wincl;
  177. HWND messageWindow;
  178. HDEVNOTIFY hNotify;
  179. } SDL_DeviceNotificationData;
  180. #define IDT_SDL_DEVICE_CHANGE_TIMER_1 1200
  181. #define IDT_SDL_DEVICE_CHANGE_TIMER_2 1201
  182. /* windowproc for our joystick detect thread message only window, to detect any USB device addition/removal */
  183. static LRESULT CALLBACK
  184. SDL_PrivateJoystickDetectProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  185. {
  186. switch (msg) {
  187. case WM_DEVICECHANGE:
  188. switch (wParam) {
  189. case DBT_DEVICEARRIVAL:
  190. case DBT_DEVICEREMOVECOMPLETE:
  191. if (((DEV_BROADCAST_HDR*)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
  192. /* notify 300ms and 2 seconds later to ensure all APIs have updated status */
  193. SetTimer(hwnd, IDT_SDL_DEVICE_CHANGE_TIMER_1, 300, NULL);
  194. SetTimer(hwnd, IDT_SDL_DEVICE_CHANGE_TIMER_2, 2000, NULL);
  195. }
  196. break;
  197. }
  198. return 0;
  199. case WM_TIMER:
  200. if (wParam == IDT_SDL_DEVICE_CHANGE_TIMER_1 ||
  201. wParam == IDT_SDL_DEVICE_CHANGE_TIMER_2) {
  202. KillTimer(hwnd, wParam);
  203. s_bWindowsDeviceChanged = SDL_TRUE;
  204. return 0;
  205. }
  206. break;
  207. }
  208. #if SDL_JOYSTICK_RAWINPUT
  209. return CallWindowProc(RAWINPUT_WindowProc, hwnd, msg, wParam, lParam);
  210. #else
  211. return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
  212. #endif
  213. }
  214. static void
  215. SDL_CleanupDeviceNotification(SDL_DeviceNotificationData *data)
  216. {
  217. #if SDL_JOYSTICK_RAWINPUT
  218. RAWINPUT_UnregisterNotifications();
  219. #endif
  220. if (data->hNotify)
  221. UnregisterDeviceNotification(data->hNotify);
  222. if (data->messageWindow)
  223. DestroyWindow(data->messageWindow);
  224. UnregisterClass(data->wincl.lpszClassName, data->wincl.hInstance);
  225. if (data->coinitialized == S_OK) {
  226. WIN_CoUninitialize();
  227. }
  228. }
  229. static int
  230. SDL_CreateDeviceNotification(SDL_DeviceNotificationData *data)
  231. {
  232. DEV_BROADCAST_DEVICEINTERFACE dbh;
  233. SDL_zerop(data);
  234. data->coinitialized = WIN_CoInitialize();
  235. data->wincl.hInstance = GetModuleHandle(NULL);
  236. data->wincl.lpszClassName = TEXT("Message");
  237. data->wincl.lpfnWndProc = SDL_PrivateJoystickDetectProc; /* This function is called by windows */
  238. data->wincl.cbSize = sizeof (WNDCLASSEX);
  239. if (!RegisterClassEx(&data->wincl)) {
  240. WIN_SetError("Failed to create register class for joystick autodetect");
  241. SDL_CleanupDeviceNotification(data);
  242. return -1;
  243. }
  244. data->messageWindow = (HWND)CreateWindowEx(0, TEXT("Message"), NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
  245. if (!data->messageWindow) {
  246. WIN_SetError("Failed to create message window for joystick autodetect");
  247. SDL_CleanupDeviceNotification(data);
  248. return -1;
  249. }
  250. SDL_zero(dbh);
  251. dbh.dbcc_size = sizeof(dbh);
  252. dbh.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  253. dbh.dbcc_classguid = GUID_DEVINTERFACE_HID;
  254. data->hNotify = RegisterDeviceNotification(data->messageWindow, &dbh, DEVICE_NOTIFY_WINDOW_HANDLE);
  255. if (!data->hNotify) {
  256. WIN_SetError("Failed to create notify device for joystick autodetect");
  257. SDL_CleanupDeviceNotification(data);
  258. return -1;
  259. }
  260. #if SDL_JOYSTICK_RAWINPUT
  261. RAWINPUT_RegisterNotifications(data->messageWindow);
  262. #endif
  263. return 0;
  264. }
  265. static SDL_bool
  266. SDL_WaitForDeviceNotification(SDL_DeviceNotificationData *data, SDL_mutex *mutex)
  267. {
  268. MSG msg;
  269. int lastret = 1;
  270. if (!data->messageWindow) {
  271. return SDL_FALSE; /* device notifications require a window */
  272. }
  273. SDL_UnlockMutex(mutex);
  274. while (lastret > 0 && s_bWindowsDeviceChanged == SDL_FALSE) {
  275. lastret = GetMessage(&msg, NULL, 0, 0); /* WM_QUIT causes return value of 0 */
  276. if (lastret > 0) {
  277. TranslateMessage(&msg);
  278. DispatchMessage(&msg);
  279. }
  280. }
  281. SDL_LockMutex(mutex);
  282. return (lastret != -1) ? SDL_TRUE : SDL_FALSE;
  283. }
  284. #endif /* !defined(__WINRT__) && !defined(__XBOXONE__) && !defined(__XBOXSERIES__) */
  285. #if !defined(__WINRT__)
  286. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  287. static SDL_DeviceNotificationData s_notification_data;
  288. #endif
  289. /* Function/thread to scan the system for joysticks. */
  290. static int SDLCALL
  291. SDL_JoystickThread(void *_data)
  292. {
  293. #if SDL_JOYSTICK_XINPUT
  294. SDL_bool bOpenedXInputDevices[XUSER_MAX_COUNT];
  295. SDL_zeroa(bOpenedXInputDevices);
  296. #endif
  297. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  298. if (SDL_CreateDeviceNotification(&s_notification_data) < 0) {
  299. return -1;
  300. }
  301. #endif
  302. SDL_LockMutex(s_mutexJoyStickEnum);
  303. while (s_bJoystickThreadQuit == SDL_FALSE) {
  304. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  305. if (SDL_WaitForDeviceNotification(&s_notification_data, s_mutexJoyStickEnum) == SDL_FALSE) {
  306. #else
  307. {
  308. #endif
  309. #if SDL_JOYSTICK_XINPUT
  310. /* WM_DEVICECHANGE not working, poll for new XINPUT controllers */
  311. SDL_CondWaitTimeout(s_condJoystickThread, s_mutexJoyStickEnum, 1000);
  312. if (SDL_XINPUT_Enabled() && XINPUTGETCAPABILITIES) {
  313. /* scan for any change in XInput devices */
  314. Uint8 userId;
  315. for (userId = 0; userId < XUSER_MAX_COUNT; userId++) {
  316. XINPUT_CAPABILITIES capabilities;
  317. const DWORD result = XINPUTGETCAPABILITIES(userId, XINPUT_FLAG_GAMEPAD, &capabilities);
  318. const SDL_bool available = (result == ERROR_SUCCESS) ? SDL_TRUE : SDL_FALSE;
  319. if (bOpenedXInputDevices[userId] != available) {
  320. s_bWindowsDeviceChanged = SDL_TRUE;
  321. bOpenedXInputDevices[userId] = available;
  322. }
  323. }
  324. }
  325. #else
  326. /* WM_DEVICECHANGE not working, no XINPUT, no point in keeping thread alive */
  327. break;
  328. #endif /* SDL_JOYSTICK_XINPUT */
  329. }
  330. }
  331. SDL_UnlockMutex(s_mutexJoyStickEnum);
  332. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  333. SDL_CleanupDeviceNotification(&s_notification_data);
  334. #endif
  335. return 1;
  336. }
  337. /* spin up the thread to detect hotplug of devices */
  338. static int
  339. SDL_StartJoystickThread(void)
  340. {
  341. s_mutexJoyStickEnum = SDL_CreateMutex();
  342. if (!s_mutexJoyStickEnum) {
  343. return -1;
  344. }
  345. s_condJoystickThread = SDL_CreateCond();
  346. if (!s_condJoystickThread) {
  347. return -1;
  348. }
  349. s_bJoystickThreadQuit = SDL_FALSE;
  350. s_joystickThread = SDL_CreateThreadInternal(SDL_JoystickThread, "SDL_joystick", 64 * 1024, NULL);
  351. if (!s_joystickThread) {
  352. return -1;
  353. }
  354. return 0;
  355. }
  356. static void
  357. SDL_StopJoystickThread(void)
  358. {
  359. if (!s_joystickThread) {
  360. return;
  361. }
  362. SDL_LockMutex(s_mutexJoyStickEnum);
  363. s_bJoystickThreadQuit = SDL_TRUE;
  364. SDL_CondBroadcast(s_condJoystickThread); /* signal the joystick thread to quit */
  365. SDL_UnlockMutex(s_mutexJoyStickEnum);
  366. PostThreadMessage(SDL_GetThreadID(s_joystickThread), WM_QUIT, 0, 0);
  367. SDL_WaitThread(s_joystickThread, NULL); /* wait for it to bugger off */
  368. SDL_DestroyCond(s_condJoystickThread);
  369. s_condJoystickThread = NULL;
  370. SDL_DestroyMutex(s_mutexJoyStickEnum);
  371. s_mutexJoyStickEnum = NULL;
  372. s_joystickThread = NULL;
  373. }
  374. #endif /* !defined(__WINRT__) */
  375. void WINDOWS_AddJoystickDevice(JoyStick_DeviceData *device)
  376. {
  377. device->send_add_event = SDL_TRUE;
  378. device->nInstanceID = SDL_GetNextJoystickInstanceID();
  379. device->pNext = SYS_Joystick;
  380. SYS_Joystick = device;
  381. }
  382. static void WINDOWS_JoystickDetect(void);
  383. static void WINDOWS_JoystickQuit(void);
  384. /* Function to scan the system for joysticks.
  385. * Joystick 0 should be the system default joystick.
  386. * It should return 0, or -1 on an unrecoverable fatal error.
  387. */
  388. static int
  389. WINDOWS_JoystickInit(void)
  390. {
  391. if (SDL_DINPUT_JoystickInit() < 0) {
  392. WINDOWS_JoystickQuit();
  393. return -1;
  394. }
  395. if (SDL_XINPUT_JoystickInit() < 0) {
  396. WINDOWS_JoystickQuit();
  397. return -1;
  398. }
  399. s_bWindowsDeviceChanged = SDL_TRUE; /* force a scan of the system for joysticks this first time */
  400. WINDOWS_JoystickDetect();
  401. #if !defined(__WINRT__) && !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  402. SDL_CreateDeviceNotificationFunc();
  403. s_bJoystickThread = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_THREAD, SDL_FALSE);
  404. if (s_bJoystickThread) {
  405. if (SDL_StartJoystickThread() < 0) {
  406. return -1;
  407. }
  408. } else {
  409. if (SDL_CreateDeviceNotification(&s_notification_data) < 0) {
  410. return -1;
  411. }
  412. }
  413. #endif
  414. #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
  415. /* On Xbox, force create the joystick thread for device detection (since other methods don't work */
  416. s_bJoystickThread = SDL_TRUE;
  417. if (SDL_StartJoystickThread() < 0) {
  418. return -1;
  419. }
  420. #endif
  421. return 0;
  422. }
  423. /* return the number of joysticks that are connected right now */
  424. static int
  425. WINDOWS_JoystickGetCount(void)
  426. {
  427. int nJoysticks = 0;
  428. JoyStick_DeviceData *device = SYS_Joystick;
  429. while (device) {
  430. nJoysticks++;
  431. device = device->pNext;
  432. }
  433. return nJoysticks;
  434. }
  435. /* detect any new joysticks being inserted into the system */
  436. static void
  437. WINDOWS_JoystickDetect(void)
  438. {
  439. int device_index = 0;
  440. JoyStick_DeviceData *pCurList = NULL;
  441. /* only enum the devices if the joystick thread told us something changed */
  442. if (!s_bWindowsDeviceChanged) {
  443. return; /* thread hasn't signaled, nothing to do right now. */
  444. }
  445. if (s_mutexJoyStickEnum) {
  446. SDL_LockMutex(s_mutexJoyStickEnum);
  447. }
  448. s_bWindowsDeviceChanged = SDL_FALSE;
  449. pCurList = SYS_Joystick;
  450. SYS_Joystick = NULL;
  451. /* Look for DirectInput joysticks, wheels, head trackers, gamepads, etc.. */
  452. SDL_DINPUT_JoystickDetect(&pCurList);
  453. /* Look for XInput devices. Do this last, so they're first in the final list. */
  454. SDL_XINPUT_JoystickDetect(&pCurList);
  455. if (s_mutexJoyStickEnum) {
  456. SDL_UnlockMutex(s_mutexJoyStickEnum);
  457. }
  458. while (pCurList) {
  459. JoyStick_DeviceData *pListNext = NULL;
  460. if (pCurList->bXInputDevice) {
  461. #if SDL_HAPTIC_XINPUT
  462. SDL_XINPUT_HapticMaybeRemoveDevice(pCurList->XInputUserId);
  463. #endif
  464. } else {
  465. #if SDL_HAPTIC_DINPUT
  466. SDL_DINPUT_HapticMaybeRemoveDevice(&pCurList->dxdevice);
  467. #endif
  468. }
  469. SDL_PrivateJoystickRemoved(pCurList->nInstanceID);
  470. pListNext = pCurList->pNext;
  471. SDL_free(pCurList->joystickname);
  472. SDL_free(pCurList);
  473. pCurList = pListNext;
  474. }
  475. for (device_index = 0, pCurList = SYS_Joystick; pCurList; ++device_index, pCurList = pCurList->pNext) {
  476. if (pCurList->send_add_event) {
  477. if (pCurList->bXInputDevice) {
  478. #if SDL_HAPTIC_XINPUT
  479. SDL_XINPUT_HapticMaybeAddDevice(pCurList->XInputUserId);
  480. #endif
  481. } else {
  482. #if SDL_HAPTIC_DINPUT
  483. SDL_DINPUT_HapticMaybeAddDevice(&pCurList->dxdevice);
  484. #endif
  485. }
  486. SDL_PrivateJoystickAdded(pCurList->nInstanceID);
  487. pCurList->send_add_event = SDL_FALSE;
  488. }
  489. }
  490. }
  491. static const char *
  492. WINDOWS_JoystickGetDeviceName(int device_index)
  493. {
  494. JoyStick_DeviceData *device = SYS_Joystick;
  495. int index;
  496. for (index = device_index; index > 0; index--)
  497. device = device->pNext;
  498. return device->joystickname;
  499. }
  500. static const char *
  501. WINDOWS_JoystickGetDevicePath(int device_index)
  502. {
  503. JoyStick_DeviceData *device = SYS_Joystick;
  504. int index;
  505. for (index = device_index; index > 0; index--)
  506. device = device->pNext;
  507. return device->path;
  508. }
  509. static int
  510. WINDOWS_JoystickGetDevicePlayerIndex(int device_index)
  511. {
  512. JoyStick_DeviceData *device = SYS_Joystick;
  513. int index;
  514. for (index = device_index; index > 0; index--)
  515. device = device->pNext;
  516. return device->bXInputDevice ? (int)device->XInputUserId : -1;
  517. }
  518. static void
  519. WINDOWS_JoystickSetDevicePlayerIndex(int device_index, int player_index)
  520. {
  521. }
  522. /* return the stable device guid for this device index */
  523. static SDL_JoystickGUID
  524. WINDOWS_JoystickGetDeviceGUID(int device_index)
  525. {
  526. JoyStick_DeviceData *device = SYS_Joystick;
  527. int index;
  528. for (index = device_index; index > 0; index--)
  529. device = device->pNext;
  530. return device->guid;
  531. }
  532. /* Function to perform the mapping between current device instance and this joysticks instance id */
  533. static SDL_JoystickID
  534. WINDOWS_JoystickGetDeviceInstanceID(int device_index)
  535. {
  536. JoyStick_DeviceData *device = SYS_Joystick;
  537. int index;
  538. for (index = device_index; index > 0; index--)
  539. device = device->pNext;
  540. return device->nInstanceID;
  541. }
  542. /* Function to open a joystick for use.
  543. The joystick to open is specified by the device index.
  544. This should fill the nbuttons and naxes fields of the joystick structure.
  545. It returns 0, or -1 if there is an error.
  546. */
  547. static int
  548. WINDOWS_JoystickOpen(SDL_Joystick *joystick, int device_index)
  549. {
  550. JoyStick_DeviceData *device = SYS_Joystick;
  551. int index;
  552. for (index = device_index; index > 0; index--)
  553. device = device->pNext;
  554. /* allocate memory for system specific hardware data */
  555. joystick->instance_id = device->nInstanceID;
  556. joystick->hwdata =
  557. (struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata));
  558. if (joystick->hwdata == NULL) {
  559. return SDL_OutOfMemory();
  560. }
  561. SDL_zerop(joystick->hwdata);
  562. joystick->hwdata->guid = device->guid;
  563. if (device->bXInputDevice) {
  564. return SDL_XINPUT_JoystickOpen(joystick, device);
  565. } else {
  566. return SDL_DINPUT_JoystickOpen(joystick, device);
  567. }
  568. }
  569. static int
  570. WINDOWS_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  571. {
  572. if (joystick->hwdata->bXInputDevice) {
  573. return SDL_XINPUT_JoystickRumble(joystick, low_frequency_rumble, high_frequency_rumble);
  574. } else {
  575. return SDL_DINPUT_JoystickRumble(joystick, low_frequency_rumble, high_frequency_rumble);
  576. }
  577. }
  578. static int
  579. WINDOWS_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  580. {
  581. return SDL_Unsupported();
  582. }
  583. static Uint32
  584. WINDOWS_JoystickGetCapabilities(SDL_Joystick *joystick)
  585. {
  586. if (joystick->hwdata->bXInputDevice) {
  587. return SDL_XINPUT_JoystickGetCapabilities(joystick);
  588. } else {
  589. return SDL_DINPUT_JoystickGetCapabilities(joystick);
  590. }
  591. }
  592. static int
  593. WINDOWS_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  594. {
  595. return SDL_Unsupported();
  596. }
  597. static int
  598. WINDOWS_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
  599. {
  600. return SDL_Unsupported();
  601. }
  602. static int
  603. WINDOWS_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
  604. {
  605. return SDL_Unsupported();
  606. }
  607. static void
  608. WINDOWS_JoystickUpdate(SDL_Joystick *joystick)
  609. {
  610. if (!joystick->hwdata) {
  611. return;
  612. }
  613. if (joystick->hwdata->bXInputDevice) {
  614. SDL_XINPUT_JoystickUpdate(joystick);
  615. } else {
  616. SDL_DINPUT_JoystickUpdate(joystick);
  617. }
  618. }
  619. /* Function to close a joystick after use */
  620. static void
  621. WINDOWS_JoystickClose(SDL_Joystick *joystick)
  622. {
  623. if (joystick->hwdata->bXInputDevice) {
  624. SDL_XINPUT_JoystickClose(joystick);
  625. } else {
  626. SDL_DINPUT_JoystickClose(joystick);
  627. }
  628. SDL_free(joystick->hwdata);
  629. }
  630. /* Function to perform any system-specific joystick related cleanup */
  631. static void
  632. WINDOWS_JoystickQuit(void)
  633. {
  634. JoyStick_DeviceData *device = SYS_Joystick;
  635. while (device) {
  636. JoyStick_DeviceData *device_next = device->pNext;
  637. SDL_free(device->joystickname);
  638. SDL_free(device);
  639. device = device_next;
  640. }
  641. SYS_Joystick = NULL;
  642. #if !defined(__WINRT__) && !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  643. if (s_bJoystickThread) {
  644. SDL_StopJoystickThread();
  645. } else {
  646. SDL_CleanupDeviceNotification(&s_notification_data);
  647. }
  648. SDL_CleanupDeviceNotificationFunc();
  649. #endif
  650. #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
  651. if (s_bJoystickThread) {
  652. SDL_StopJoystickThread();
  653. }
  654. #endif
  655. SDL_DINPUT_JoystickQuit();
  656. SDL_XINPUT_JoystickQuit();
  657. s_bWindowsDeviceChanged = SDL_FALSE;
  658. }
  659. static SDL_bool
  660. WINDOWS_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
  661. {
  662. return SDL_FALSE;
  663. }
  664. SDL_JoystickDriver SDL_WINDOWS_JoystickDriver =
  665. {
  666. WINDOWS_JoystickInit,
  667. WINDOWS_JoystickGetCount,
  668. WINDOWS_JoystickDetect,
  669. WINDOWS_JoystickGetDeviceName,
  670. WINDOWS_JoystickGetDevicePath,
  671. WINDOWS_JoystickGetDevicePlayerIndex,
  672. WINDOWS_JoystickSetDevicePlayerIndex,
  673. WINDOWS_JoystickGetDeviceGUID,
  674. WINDOWS_JoystickGetDeviceInstanceID,
  675. WINDOWS_JoystickOpen,
  676. WINDOWS_JoystickRumble,
  677. WINDOWS_JoystickRumbleTriggers,
  678. WINDOWS_JoystickGetCapabilities,
  679. WINDOWS_JoystickSetLED,
  680. WINDOWS_JoystickSendEffect,
  681. WINDOWS_JoystickSetSensorsEnabled,
  682. WINDOWS_JoystickUpdate,
  683. WINDOWS_JoystickClose,
  684. WINDOWS_JoystickQuit,
  685. WINDOWS_JoystickGetGamepadMapping
  686. };
  687. /* Ends C function definitions when using C++ */
  688. #ifdef __cplusplus
  689. }
  690. #endif
  691. #else
  692. #if SDL_JOYSTICK_RAWINPUT
  693. /* The RAWINPUT driver needs the device notification setup above */
  694. #error SDL_JOYSTICK_RAWINPUT requires SDL_JOYSTICK_DINPUT || SDL_JOYSTICK_XINPUT
  695. #endif
  696. #endif /* SDL_JOYSTICK_DINPUT || SDL_JOYSTICK_XINPUT */
  697. /* vi: set ts=4 sw=4 expandtab: */