SDL_windowsjoystick.c 24 KB

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