2
0

SDL_hid.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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. #include "SDL_hid.h"
  20. HidD_GetAttributes_t SDL_HidD_GetAttributes;
  21. HidD_GetString_t SDL_HidD_GetManufacturerString;
  22. HidD_GetString_t SDL_HidD_GetProductString;
  23. HidP_GetCaps_t SDL_HidP_GetCaps;
  24. HidP_GetButtonCaps_t SDL_HidP_GetButtonCaps;
  25. HidP_GetValueCaps_t SDL_HidP_GetValueCaps;
  26. HidP_MaxDataListLength_t SDL_HidP_MaxDataListLength;
  27. HidP_GetData_t SDL_HidP_GetData;
  28. static HMODULE s_pHIDDLL = 0;
  29. static int s_HIDDLLRefCount = 0;
  30. bool WIN_LoadHIDDLL(void)
  31. {
  32. if (s_pHIDDLL) {
  33. SDL_assert(s_HIDDLLRefCount > 0);
  34. s_HIDDLLRefCount++;
  35. return true; // already loaded
  36. }
  37. s_pHIDDLL = LoadLibrary(TEXT("hid.dll"));
  38. if (!s_pHIDDLL) {
  39. return false;
  40. }
  41. SDL_assert(s_HIDDLLRefCount == 0);
  42. s_HIDDLLRefCount = 1;
  43. SDL_HidD_GetAttributes = (HidD_GetAttributes_t)GetProcAddress(s_pHIDDLL, "HidD_GetAttributes");
  44. SDL_HidD_GetManufacturerString = (HidD_GetString_t)GetProcAddress(s_pHIDDLL, "HidD_GetManufacturerString");
  45. SDL_HidD_GetProductString = (HidD_GetString_t)GetProcAddress(s_pHIDDLL, "HidD_GetProductString");
  46. SDL_HidP_GetCaps = (HidP_GetCaps_t)GetProcAddress(s_pHIDDLL, "HidP_GetCaps");
  47. SDL_HidP_GetButtonCaps = (HidP_GetButtonCaps_t)GetProcAddress(s_pHIDDLL, "HidP_GetButtonCaps");
  48. SDL_HidP_GetValueCaps = (HidP_GetValueCaps_t)GetProcAddress(s_pHIDDLL, "HidP_GetValueCaps");
  49. SDL_HidP_MaxDataListLength = (HidP_MaxDataListLength_t)GetProcAddress(s_pHIDDLL, "HidP_MaxDataListLength");
  50. SDL_HidP_GetData = (HidP_GetData_t)GetProcAddress(s_pHIDDLL, "HidP_GetData");
  51. if (!SDL_HidD_GetManufacturerString || !SDL_HidD_GetProductString ||
  52. !SDL_HidP_GetCaps || !SDL_HidP_GetButtonCaps ||
  53. !SDL_HidP_GetValueCaps || !SDL_HidP_MaxDataListLength || !SDL_HidP_GetData) {
  54. WIN_UnloadHIDDLL();
  55. return false;
  56. }
  57. return true;
  58. }
  59. void WIN_UnloadHIDDLL(void)
  60. {
  61. if (s_pHIDDLL) {
  62. SDL_assert(s_HIDDLLRefCount > 0);
  63. if (--s_HIDDLLRefCount == 0) {
  64. FreeLibrary(s_pHIDDLL);
  65. s_pHIDDLL = NULL;
  66. }
  67. } else {
  68. SDL_assert(s_HIDDLLRefCount == 0);
  69. }
  70. }
  71. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  72. // CM_Register_Notification definitions
  73. #define CR_SUCCESS 0
  74. DECLARE_HANDLE(HCMNOTIFICATION);
  75. typedef HCMNOTIFICATION *PHCMNOTIFICATION;
  76. typedef enum _CM_NOTIFY_FILTER_TYPE
  77. {
  78. CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE = 0,
  79. CM_NOTIFY_FILTER_TYPE_DEVICEHANDLE,
  80. CM_NOTIFY_FILTER_TYPE_DEVICEINSTANCE,
  81. CM_NOTIFY_FILTER_TYPE_MAX
  82. } CM_NOTIFY_FILTER_TYPE, *PCM_NOTIFY_FILTER_TYPE;
  83. typedef struct _CM_NOTIFY_FILTER
  84. {
  85. DWORD cbSize;
  86. DWORD Flags;
  87. CM_NOTIFY_FILTER_TYPE FilterType;
  88. DWORD Reserved;
  89. union
  90. {
  91. struct
  92. {
  93. GUID ClassGuid;
  94. } DeviceInterface;
  95. struct
  96. {
  97. HANDLE hTarget;
  98. } DeviceHandle;
  99. struct
  100. {
  101. WCHAR InstanceId[200];
  102. } DeviceInstance;
  103. } u;
  104. } CM_NOTIFY_FILTER, *PCM_NOTIFY_FILTER;
  105. typedef enum _CM_NOTIFY_ACTION
  106. {
  107. CM_NOTIFY_ACTION_DEVICEINTERFACEARRIVAL = 0,
  108. CM_NOTIFY_ACTION_DEVICEINTERFACEREMOVAL,
  109. CM_NOTIFY_ACTION_DEVICEQUERYREMOVE,
  110. CM_NOTIFY_ACTION_DEVICEQUERYREMOVEFAILED,
  111. CM_NOTIFY_ACTION_DEVICEREMOVEPENDING,
  112. CM_NOTIFY_ACTION_DEVICEREMOVECOMPLETE,
  113. CM_NOTIFY_ACTION_DEVICECUSTOMEVENT,
  114. CM_NOTIFY_ACTION_DEVICEINSTANCEENUMERATED,
  115. CM_NOTIFY_ACTION_DEVICEINSTANCESTARTED,
  116. CM_NOTIFY_ACTION_DEVICEINSTANCEREMOVED,
  117. CM_NOTIFY_ACTION_MAX
  118. } CM_NOTIFY_ACTION, *PCM_NOTIFY_ACTION;
  119. typedef struct _CM_NOTIFY_EVENT_DATA
  120. {
  121. CM_NOTIFY_FILTER_TYPE FilterType;
  122. DWORD Reserved;
  123. union
  124. {
  125. struct
  126. {
  127. GUID ClassGuid;
  128. WCHAR SymbolicLink[ANYSIZE_ARRAY];
  129. } DeviceInterface;
  130. struct
  131. {
  132. GUID EventGuid;
  133. LONG NameOffset;
  134. DWORD DataSize;
  135. BYTE Data[ANYSIZE_ARRAY];
  136. } DeviceHandle;
  137. struct
  138. {
  139. WCHAR InstanceId[ANYSIZE_ARRAY];
  140. } DeviceInstance;
  141. } u;
  142. } CM_NOTIFY_EVENT_DATA, *PCM_NOTIFY_EVENT_DATA;
  143. typedef DWORD (CALLBACK *PCM_NOTIFY_CALLBACK)(HCMNOTIFICATION hNotify, PVOID Context, CM_NOTIFY_ACTION Action, PCM_NOTIFY_EVENT_DATA EventData, DWORD EventDataSize);
  144. typedef DWORD (WINAPI *CM_Register_NotificationFunc)(PCM_NOTIFY_FILTER pFilter, PVOID pContext, PCM_NOTIFY_CALLBACK pCallback, PHCMNOTIFICATION pNotifyContext);
  145. typedef DWORD (WINAPI *CM_Unregister_NotificationFunc)(HCMNOTIFICATION NotifyContext);
  146. static GUID GUID_DEVINTERFACE_HID = { 0x4D1E55B2L, 0xF16F, 0x11CF, { 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 } };
  147. static int s_DeviceNotificationsRequested;
  148. static HMODULE cfgmgr32_lib_handle;
  149. static CM_Register_NotificationFunc CM_Register_Notification;
  150. static CM_Unregister_NotificationFunc CM_Unregister_Notification;
  151. static HCMNOTIFICATION s_DeviceNotificationFuncHandle;
  152. static Uint64 s_LastDeviceNotification = 1;
  153. static DWORD CALLBACK SDL_DeviceNotificationFunc(HCMNOTIFICATION hNotify, PVOID context, CM_NOTIFY_ACTION action, PCM_NOTIFY_EVENT_DATA eventData, DWORD event_data_size)
  154. {
  155. if (action == CM_NOTIFY_ACTION_DEVICEINTERFACEARRIVAL ||
  156. action == CM_NOTIFY_ACTION_DEVICEINTERFACEREMOVAL) {
  157. s_LastDeviceNotification = SDL_GetTicksNS();
  158. }
  159. return ERROR_SUCCESS;
  160. }
  161. void WIN_InitDeviceNotification(void)
  162. {
  163. ++s_DeviceNotificationsRequested;
  164. if (s_DeviceNotificationsRequested > 1) {
  165. return;
  166. }
  167. cfgmgr32_lib_handle = LoadLibraryA("cfgmgr32.dll");
  168. if (cfgmgr32_lib_handle) {
  169. CM_Register_Notification = (CM_Register_NotificationFunc)GetProcAddress(cfgmgr32_lib_handle, "CM_Register_Notification");
  170. CM_Unregister_Notification = (CM_Unregister_NotificationFunc)GetProcAddress(cfgmgr32_lib_handle, "CM_Unregister_Notification");
  171. if (CM_Register_Notification && CM_Unregister_Notification) {
  172. CM_NOTIFY_FILTER notify_filter;
  173. SDL_zero(notify_filter);
  174. notify_filter.cbSize = sizeof(notify_filter);
  175. notify_filter.FilterType = CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE;
  176. notify_filter.u.DeviceInterface.ClassGuid = GUID_DEVINTERFACE_HID;
  177. if (CM_Register_Notification(&notify_filter, NULL, SDL_DeviceNotificationFunc, &s_DeviceNotificationFuncHandle) == CR_SUCCESS) {
  178. return;
  179. }
  180. }
  181. }
  182. // FIXME: Should we log errors?
  183. }
  184. Uint64 WIN_GetLastDeviceNotification(void)
  185. {
  186. return s_LastDeviceNotification;
  187. }
  188. void WIN_QuitDeviceNotification(void)
  189. {
  190. if (--s_DeviceNotificationsRequested > 0) {
  191. return;
  192. }
  193. // Make sure we have balanced calls to init/quit
  194. SDL_assert(s_DeviceNotificationsRequested == 0);
  195. if (cfgmgr32_lib_handle) {
  196. if (s_DeviceNotificationFuncHandle && CM_Unregister_Notification) {
  197. CM_Unregister_Notification(s_DeviceNotificationFuncHandle);
  198. s_DeviceNotificationFuncHandle = NULL;
  199. }
  200. FreeLibrary(cfgmgr32_lib_handle);
  201. cfgmgr32_lib_handle = NULL;
  202. }
  203. }
  204. #else
  205. void WIN_InitDeviceNotification(void)
  206. {
  207. }
  208. Uint64 WIN_GetLastDeviceNotification( void )
  209. {
  210. return 0;
  211. }
  212. void WIN_QuitDeviceNotification(void)
  213. {
  214. }
  215. #endif // !SDL_PLATFORM_XBOXONE && !SDL_PLATFORM_XBOXSERIES