Atlas - SDL_hidapi.c
Home / ext / SDL / src / hidapi Lines: 1 | Size: 55838 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2026 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21 22/* Original hybrid wrapper for Linux by Valve Software. Their original notes: 23 * 24 * The libusb version doesn't support Bluetooth, but not all Linux 25 * distributions allow access to /dev/hidraw* 26 * 27 * This merges the two, at a small performance cost, until distributions 28 * have granted access to /dev/hidraw* 29 */ 30 31#include "SDL_internal.h" 32 33#include "SDL_hidapi_c.h" 34#include "../joystick/usb_ids.h" 35#include "../joystick/SDL_joystick_c.h" 36#include "../SDL_hints_c.h" 37 38// Initial type declarations 39#define HID_API_NO_EXPORT_DEFINE // do not export hidapi procedures 40#include "hidapi/hidapi.h" 41 42#ifndef SDL_HIDAPI_DISABLED 43 44#ifdef SDL_LIBUSB_DYNAMIC 45SDL_ELF_NOTE_DLOPEN( 46 "hidabi-libusb", 47 "Support for joysticks through libusb", 48 SDL_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED, 49 SDL_LIBUSB_DYNAMIC 50) 51#endif 52 53#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK) 54#include "../core/windows/SDL_windows.h" 55#endif 56 57#ifdef SDL_PLATFORM_MACOS 58#include <CoreFoundation/CoreFoundation.h> 59#include <mach/mach.h> 60#include <IOKit/IOKitLib.h> 61#include <IOKit/hid/IOHIDDevice.h> 62#include <IOKit/usb/USBSpec.h> 63#include <AvailabilityMacros.h> 64// Things named "Master" were renamed to "Main" in macOS 12.0's SDK. 65#if MAC_OS_X_VERSION_MIN_REQUIRED < 120000 66#define kIOMainPortDefault kIOMasterPortDefault 67#endif 68#endif 69 70#include "../core/linux/SDL_udev.h" 71#ifdef SDL_USE_LIBUDEV 72#include <poll.h> 73#endif 74 75#ifdef HAVE_INOTIFY 76#include <string.h> // strerror 77#include <errno.h> // errno 78#include <fcntl.h> 79#include <limits.h> // For the definition of NAME_MAX 80#include <sys/inotify.h> 81#endif 82 83#if defined(SDL_USE_LIBUDEV) || defined(HAVE_INOTIFY) 84#include <unistd.h> 85#endif 86 87#ifdef SDL_USE_LIBUDEV 88typedef enum 89{ 90 ENUMERATION_UNSET, 91 ENUMERATION_LIBUDEV, 92 ENUMERATION_FALLBACK 93} LinuxEnumerationMethod; 94 95static LinuxEnumerationMethod linux_enumeration_method = ENUMERATION_UNSET; 96#endif 97 98#ifdef HAVE_INOTIFY 99static int inotify_fd = -1; 100#endif 101 102#ifdef SDL_USE_LIBUDEV 103static const SDL_UDEV_Symbols *usyms = NULL; 104#endif 105 106static struct 107{ 108 bool m_bInitialized; 109 Uint32 m_unDeviceChangeCounter; 110 bool m_bCanGetNotifications; 111 Uint64 m_unLastDetect; 112 113#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK) 114 SDL_ThreadID m_nThreadID; 115 WNDCLASSEXA m_wndClass; 116 HWND m_hwndMsg; 117 HDEVNOTIFY m_hNotify; 118 double m_flLastWin32MessageCheck; 119#endif 120 121#ifdef SDL_PLATFORM_MACOS 122 IONotificationPortRef m_notificationPort; 123 mach_port_t m_notificationMach; 124#endif 125 126#ifdef SDL_USE_LIBUDEV 127 struct udev *m_pUdev; 128 struct udev_monitor *m_pUdevMonitor; 129 int m_nUdevFd; 130#endif 131} SDL_HIDAPI_discovery; 132 133#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK) 134struct _DEV_BROADCAST_HDR 135{ 136 DWORD dbch_size; 137 DWORD dbch_devicetype; 138 DWORD dbch_reserved; 139}; 140 141typedef struct _DEV_BROADCAST_DEVICEINTERFACE_A 142{ 143 DWORD dbcc_size; 144 DWORD dbcc_devicetype; 145 DWORD dbcc_reserved; 146 GUID dbcc_classguid; 147 char dbcc_name[1]; 148} DEV_BROADCAST_DEVICEINTERFACE_A, *PDEV_BROADCAST_DEVICEINTERFACE_A; 149 150typedef struct _DEV_BROADCAST_HDR DEV_BROADCAST_HDR; 151#define DBT_DEVICEARRIVAL 0x8000 // system detected a new device 152#define DBT_DEVICEREMOVECOMPLETE 0x8004 // device was removed from the system 153#define DBT_DEVTYP_DEVICEINTERFACE 0x00000005 // device interface class 154#define DBT_DEVNODES_CHANGED 0x0007 155#define DBT_CONFIGCHANGED 0x0018 156#define DBT_DEVICETYPESPECIFIC 0x8005 // type specific event 157#define DBT_DEVINSTSTARTED 0x8008 // device installed and started 158 159#include <initguid.h> 160DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED); 161 162static LRESULT CALLBACK ControllerWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 163{ 164 switch (message) { 165 case WM_DEVICECHANGE: 166 switch (wParam) { 167 case DBT_DEVICEARRIVAL: 168 case DBT_DEVICEREMOVECOMPLETE: 169 if (((DEV_BROADCAST_HDR *)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) { 170 ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter; 171 } 172 break; 173 } 174 return TRUE; 175 } 176 177 return DefWindowProc(hwnd, message, wParam, lParam); 178} 179#endif // defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK) 180 181#ifdef SDL_PLATFORM_MACOS 182static void CallbackIOServiceFunc(void *context, io_iterator_t portIterator) 183{ 184 // Must drain the iterator, or we won't receive new notifications 185 io_object_t entry; 186 while ((entry = IOIteratorNext(portIterator)) != 0) { 187 IOObjectRelease(entry); 188 ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter; 189 } 190} 191#endif // SDL_PLATFORM_MACOS 192 193#ifdef HAVE_INOTIFY 194#ifdef HAVE_INOTIFY_INIT1 195static int SDL_inotify_init1(void) 196{ 197 return inotify_init1(IN_NONBLOCK | IN_CLOEXEC); 198} 199#else 200static int SDL_inotify_init1(void) 201{ 202 int fd = inotify_init(); 203 if (fd < 0) { 204 return -1; 205 } 206 fcntl(fd, F_SETFL, O_NONBLOCK); 207 fcntl(fd, F_SETFD, FD_CLOEXEC); 208 return fd; 209} 210#endif 211 212static int StrHasPrefix(const char *string, const char *prefix) 213{ 214 return SDL_strncmp(string, prefix, SDL_strlen(prefix)) == 0; 215} 216 217static int StrIsInteger(const char *string) 218{ 219 const char *p; 220 221 if (*string == '\0') { 222 return 0; 223 } 224 225 for (p = string; *p != '\0'; p++) { 226 if (*p < '0' || *p > '9') { 227 return 0; 228 } 229 } 230 231 return 1; 232} 233#endif // HAVE_INOTIFY 234 235static void HIDAPI_InitializeDiscovery(void) 236{ 237 SDL_HIDAPI_discovery.m_bInitialized = true; 238 SDL_HIDAPI_discovery.m_unDeviceChangeCounter = 1; 239 SDL_HIDAPI_discovery.m_bCanGetNotifications = false; 240 SDL_HIDAPI_discovery.m_unLastDetect = 0; 241 242#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK) 243 SDL_HIDAPI_discovery.m_nThreadID = SDL_GetCurrentThreadID(); 244 245 SDL_zero(SDL_HIDAPI_discovery.m_wndClass); 246 SDL_HIDAPI_discovery.m_wndClass.hInstance = GetModuleHandle(NULL); 247 SDL_HIDAPI_discovery.m_wndClass.lpszClassName = "SDL_HIDAPI_DEVICE_DETECTION"; 248 SDL_HIDAPI_discovery.m_wndClass.lpfnWndProc = ControllerWndProc; // This function is called by windows 249 SDL_HIDAPI_discovery.m_wndClass.cbSize = sizeof(WNDCLASSEX); 250 251 RegisterClassExA(&SDL_HIDAPI_discovery.m_wndClass); 252 SDL_HIDAPI_discovery.m_hwndMsg = CreateWindowExA(0, "SDL_HIDAPI_DEVICE_DETECTION", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL); 253 254 { 255 DEV_BROADCAST_DEVICEINTERFACE_A devBroadcast; 256 257 SDL_zero(devBroadcast); 258 devBroadcast.dbcc_size = sizeof(devBroadcast); 259 devBroadcast.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; 260 devBroadcast.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE; 261 262 /* DEVICE_NOTIFY_ALL_INTERFACE_CLASSES is important, makes GUID_DEVINTERFACE_USB_DEVICE ignored, 263 * but that seems to be necessary to get a notice after each individual usb input device actually 264 * installs, rather than just as the composite device is seen. 265 */ 266 SDL_HIDAPI_discovery.m_hNotify = RegisterDeviceNotification(SDL_HIDAPI_discovery.m_hwndMsg, &devBroadcast, DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES); 267 SDL_HIDAPI_discovery.m_bCanGetNotifications = (SDL_HIDAPI_discovery.m_hNotify != 0); 268 } 269#endif // defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK) 270 271#ifdef SDL_PLATFORM_MACOS 272 SDL_HIDAPI_discovery.m_notificationPort = IONotificationPortCreate(kIOMainPortDefault); 273 if (SDL_HIDAPI_discovery.m_notificationPort) { 274 { 275 io_iterator_t portIterator = 0; 276 io_object_t entry; 277 IOReturn result = IOServiceAddMatchingNotification( 278 SDL_HIDAPI_discovery.m_notificationPort, 279 kIOFirstMatchNotification, 280 IOServiceMatching(kIOHIDDeviceKey), 281 CallbackIOServiceFunc, NULL, &portIterator); 282 283 if (result == 0) { 284 // Must drain the existing iterator, or we won't receive new notifications 285 while ((entry = IOIteratorNext(portIterator)) != 0) { 286 IOObjectRelease(entry); 287 } 288 } else { 289 IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort); 290 SDL_HIDAPI_discovery.m_notificationPort = nil; 291 } 292 } 293 { 294 io_iterator_t portIterator = 0; 295 io_object_t entry; 296 IOReturn result = IOServiceAddMatchingNotification( 297 SDL_HIDAPI_discovery.m_notificationPort, 298 kIOTerminatedNotification, 299 IOServiceMatching(kIOHIDDeviceKey), 300 CallbackIOServiceFunc, NULL, &portIterator); 301 302 if (result == 0) { 303 // Must drain the existing iterator, or we won't receive new notifications 304 while ((entry = IOIteratorNext(portIterator)) != 0) { 305 IOObjectRelease(entry); 306 } 307 } else { 308 IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort); 309 SDL_HIDAPI_discovery.m_notificationPort = nil; 310 } 311 } 312 } 313 314 SDL_HIDAPI_discovery.m_notificationMach = MACH_PORT_NULL; 315 if (SDL_HIDAPI_discovery.m_notificationPort) { 316 SDL_HIDAPI_discovery.m_notificationMach = IONotificationPortGetMachPort(SDL_HIDAPI_discovery.m_notificationPort); 317 } 318 319 SDL_HIDAPI_discovery.m_bCanGetNotifications = (SDL_HIDAPI_discovery.m_notificationMach != MACH_PORT_NULL); 320 321#endif // SDL_PLATFORM_MACOS 322 323#ifdef SDL_USE_LIBUDEV 324 if (linux_enumeration_method == ENUMERATION_LIBUDEV) { 325 SDL_HIDAPI_discovery.m_pUdev = NULL; 326 SDL_HIDAPI_discovery.m_pUdevMonitor = NULL; 327 SDL_HIDAPI_discovery.m_nUdevFd = -1; 328 329 usyms = SDL_UDEV_GetUdevSyms(); 330 if (usyms != NULL) { 331 SDL_HIDAPI_discovery.m_pUdev = usyms->udev_new(); 332 if (SDL_HIDAPI_discovery.m_pUdev != NULL) { 333 SDL_HIDAPI_discovery.m_pUdevMonitor = usyms->udev_monitor_new_from_netlink(SDL_HIDAPI_discovery.m_pUdev, "udev"); 334 if (SDL_HIDAPI_discovery.m_pUdevMonitor != NULL) { 335 usyms->udev_monitor_enable_receiving(SDL_HIDAPI_discovery.m_pUdevMonitor); 336 SDL_HIDAPI_discovery.m_nUdevFd = usyms->udev_monitor_get_fd(SDL_HIDAPI_discovery.m_pUdevMonitor); 337 SDL_HIDAPI_discovery.m_bCanGetNotifications = true; 338 } 339 } 340 } 341 } else 342#endif // SDL_USE_LIBUDEV 343 { 344#ifdef HAVE_INOTIFY 345 inotify_fd = SDL_inotify_init1(); 346 347 if (inotify_fd < 0) { 348 SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, 349 "Unable to initialize inotify, falling back to polling: %s", 350 strerror(errno)); 351 return; 352 } 353 354 /* We need to watch for attribute changes in addition to 355 * creation, because when a device is first created, it has 356 * permissions that we can't read. When udev chmods it to 357 * something that we maybe *can* read, we'll get an 358 * IN_ATTRIB event to tell us. */ 359 if (inotify_add_watch(inotify_fd, "/dev", 360 IN_CREATE | IN_DELETE | IN_MOVE | IN_ATTRIB) < 0) { 361 close(inotify_fd); 362 inotify_fd = -1; 363 SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, 364 "Unable to add inotify watch, falling back to polling: %s", 365 strerror(errno)); 366 return; 367 } 368 369 SDL_HIDAPI_discovery.m_bCanGetNotifications = true; 370#endif // HAVE_INOTIFY 371 } 372} 373 374static void HIDAPI_UpdateDiscovery(void) 375{ 376 if (!SDL_HIDAPI_discovery.m_bInitialized) { 377 HIDAPI_InitializeDiscovery(); 378 } 379 380 if (!SDL_HIDAPI_discovery.m_bCanGetNotifications) { 381 const Uint32 SDL_HIDAPI_DETECT_INTERVAL_MS = 3000; // Update every 3 seconds 382 Uint64 now = SDL_GetTicks(); 383 if (!SDL_HIDAPI_discovery.m_unLastDetect || now >= (SDL_HIDAPI_discovery.m_unLastDetect + SDL_HIDAPI_DETECT_INTERVAL_MS)) { 384 ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter; 385 SDL_HIDAPI_discovery.m_unLastDetect = now; 386 } 387 return; 388 } 389 390#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK) 391 if (SDL_IsVideoThread()) { 392 // just let the usual SDL_PumpEvents loop dispatch these, fixing bug 2998. --ryan. 393 } else { 394 // We'll only get messages on the same thread that created the window 395 if (SDL_GetCurrentThreadID() == SDL_HIDAPI_discovery.m_nThreadID) { 396 MSG msg; 397 while (PeekMessage(&msg, SDL_HIDAPI_discovery.m_hwndMsg, 0, 0, PM_NOREMOVE)) { 398 if (GetMessageA(&msg, SDL_HIDAPI_discovery.m_hwndMsg, 0, 0) != 0) { 399 TranslateMessage(&msg); 400 DispatchMessage(&msg); 401 } 402 } 403 } 404 } 405#endif // defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK) 406 407#ifdef SDL_PLATFORM_MACOS 408 if (SDL_HIDAPI_discovery.m_notificationPort) { 409 struct 410 { 411 mach_msg_header_t hdr; 412 char payload[4096]; 413 } msg; 414 while (mach_msg(&msg.hdr, MACH_RCV_MSG | MACH_RCV_TIMEOUT, 0, sizeof(msg), SDL_HIDAPI_discovery.m_notificationMach, 0, MACH_PORT_NULL) == KERN_SUCCESS) { 415 IODispatchCalloutFromMessage(NULL, &msg.hdr, SDL_HIDAPI_discovery.m_notificationPort); 416 } 417 } 418#endif 419 420#ifdef SDL_USE_LIBUDEV 421 if (linux_enumeration_method == ENUMERATION_LIBUDEV) { 422 if (SDL_HIDAPI_discovery.m_nUdevFd >= 0) { 423 /* Drain all notification events. 424 * We don't expect a lot of device notifications so just 425 * do a new discovery on any kind or number of notifications. 426 * This could be made more restrictive if necessary. 427 */ 428 for (;;) { 429 struct pollfd PollUdev; 430 struct udev_device *pUdevDevice; 431 432 PollUdev.fd = SDL_HIDAPI_discovery.m_nUdevFd; 433 PollUdev.events = POLLIN; 434 if (poll(&PollUdev, 1, 0) != 1) { 435 break; 436 } 437 438 pUdevDevice = usyms->udev_monitor_receive_device(SDL_HIDAPI_discovery.m_pUdevMonitor); 439 if (pUdevDevice) { 440 const char *action = NULL; 441 action = usyms->udev_device_get_action(pUdevDevice); 442 if (action == NULL || SDL_strcmp(action, "add") == 0 || SDL_strcmp(action, "remove") == 0) { 443 ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter; 444 } 445 usyms->udev_device_unref(pUdevDevice); 446 } 447 } 448 } 449 } else 450#endif // SDL_USE_LIBUDEV 451 { 452#ifdef HAVE_INOTIFY 453 if (inotify_fd >= 0) { 454 union 455 { 456 struct inotify_event event; 457 char storage[4096]; 458 char enough_for_inotify[sizeof(struct inotify_event) + NAME_MAX + 1]; 459 } buf; 460 ssize_t bytes; 461 size_t remain = 0; 462 size_t len; 463 464 bytes = read(inotify_fd, &buf, sizeof(buf)); 465 466 if (bytes > 0) { 467 remain = (size_t)bytes; 468 } 469 470 while (remain > 0) { 471 if (buf.event.len > 0) { 472 if (StrHasPrefix(buf.event.name, "hidraw") && 473 StrIsInteger(buf.event.name + SDL_strlen("hidraw"))) { 474 ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter; 475 /* We found an hidraw change. We still continue to 476 * drain the inotify fd to avoid leaving old 477 * notifications in the queue. */ 478 } 479 } 480 481 len = sizeof(struct inotify_event) + buf.event.len; 482 remain -= len; 483 484 if (remain != 0) { 485 SDL_memmove(&buf.storage[0], &buf.storage[len], remain); 486 } 487 } 488 } 489#endif // HAVE_INOTIFY 490 } 491} 492 493static void HIDAPI_ShutdownDiscovery(void) 494{ 495 if (!SDL_HIDAPI_discovery.m_bInitialized) { 496 return; 497 } 498 499#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK) 500 if (SDL_HIDAPI_discovery.m_hNotify) { 501 UnregisterDeviceNotification(SDL_HIDAPI_discovery.m_hNotify); 502 } 503 504 if (SDL_HIDAPI_discovery.m_hwndMsg) { 505 DestroyWindow(SDL_HIDAPI_discovery.m_hwndMsg); 506 } 507 508 UnregisterClassA(SDL_HIDAPI_discovery.m_wndClass.lpszClassName, SDL_HIDAPI_discovery.m_wndClass.hInstance); 509#endif 510 511#ifdef SDL_PLATFORM_MACOS 512 if (SDL_HIDAPI_discovery.m_notificationPort) { 513 IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort); 514 } 515#endif 516 517#ifdef SDL_USE_LIBUDEV 518 if (linux_enumeration_method == ENUMERATION_LIBUDEV) { 519 if (usyms) { 520 if (SDL_HIDAPI_discovery.m_pUdevMonitor) { 521 usyms->udev_monitor_unref(SDL_HIDAPI_discovery.m_pUdevMonitor); 522 } 523 if (SDL_HIDAPI_discovery.m_pUdev) { 524 usyms->udev_unref(SDL_HIDAPI_discovery.m_pUdev); 525 } 526 SDL_UDEV_ReleaseUdevSyms(); 527 usyms = NULL; 528 } 529 } else 530#endif // SDL_USE_LIBUDEV 531 { 532#ifdef HAVE_INOTIFY 533 if (inotify_fd >= 0) { 534 close(inotify_fd); 535 inotify_fd = -1; 536 } 537#endif 538 } 539 540 SDL_HIDAPI_discovery.m_bInitialized = false; 541} 542 543// Platform HIDAPI Implementation 544 545#define HIDAPI_USING_SDL_RUNTIME 546#define HIDAPI_IGNORE_DEVICE(BUS, VID, PID, USAGE_PAGE, USAGE, LIBUSB) \ 547 SDL_HIDAPI_ShouldIgnoreDevice(BUS, VID, PID, USAGE_PAGE, USAGE, LIBUSB) 548 549struct PLATFORM_hid_device_; 550typedef struct PLATFORM_hid_device_ PLATFORM_hid_device; 551 552#define api_version PLATFORM_api_version 553#define create_device_info_for_device PLATFORM_create_device_info_for_device 554#define free_hid_device PLATFORM_free_hid_device 555#define hid_close PLATFORM_hid_close 556#define hid_device PLATFORM_hid_device 557#define hid_device_ PLATFORM_hid_device_ 558#define hid_enumerate PLATFORM_hid_enumerate 559#define hid_error PLATFORM_hid_error 560#define hid_exit PLATFORM_hid_exit 561#define hid_free_enumeration PLATFORM_hid_free_enumeration 562#define hid_get_device_info PLATFORM_hid_get_device_info 563#define hid_get_feature_report PLATFORM_hid_get_feature_report 564#define hid_get_indexed_string PLATFORM_hid_get_indexed_string 565#define hid_get_input_report PLATFORM_hid_get_input_report 566#define hid_get_manufacturer_string PLATFORM_hid_get_manufacturer_string 567#define hid_get_product_string PLATFORM_hid_get_product_string 568#define hid_get_report_descriptor PLATFORM_hid_get_report_descriptor 569#define hid_get_serial_number_string PLATFORM_hid_get_serial_number_string 570#define hid_init PLATFORM_hid_init 571#define hid_open_path PLATFORM_hid_open_path 572#define hid_open PLATFORM_hid_open 573#define hid_read PLATFORM_hid_read 574#define hid_read_timeout PLATFORM_hid_read_timeout 575#define hid_send_feature_report PLATFORM_hid_send_feature_report 576#define hid_set_nonblocking PLATFORM_hid_set_nonblocking 577#define hid_version PLATFORM_hid_version 578#define hid_version_str PLATFORM_hid_version_str 579#define hid_write PLATFORM_hid_write 580#define input_report PLATFORM_input_report 581#define make_path PLATFORM_make_path 582#define new_hid_device PLATFORM_new_hid_device 583#define read_thread PLATFORM_read_thread 584#define return_data PLATFORM_return_data 585 586#ifdef SDL_PLATFORM_LINUX 587#include "SDL_hidapi_linux.h" 588#elif defined(SDL_PLATFORM_NETBSD) 589#include "SDL_hidapi_netbsd.h" 590#elif defined(SDL_PLATFORM_MACOS) 591#include "SDL_hidapi_mac.h" 592#elif defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK) 593#include "SDL_hidapi_windows.h" 594#elif defined(SDL_PLATFORM_ANDROID) 595#include "SDL_hidapi_android.h" 596#elif defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS) 597#include "SDL_hidapi_ios.h" 598#endif 599 600#undef api_version 601#undef create_device_info_for_device 602#undef free_hid_device 603#undef hid_close 604#undef hid_device 605#undef hid_device_ 606#undef hid_enumerate 607#undef hid_error 608#undef hid_exit 609#undef hid_free_enumeration 610#undef hid_get_device_info 611#undef hid_get_feature_report 612#undef hid_get_indexed_string 613#undef hid_get_input_report 614#undef hid_get_manufacturer_string 615#undef hid_get_product_string 616#undef hid_get_report_descriptor 617#undef hid_get_serial_number_string 618#undef hid_init 619#undef hid_open 620#undef hid_open_path 621#undef hid_read 622#undef hid_read_timeout 623#undef hid_send_feature_report 624#undef hid_set_nonblocking 625#undef hid_version 626#undef hid_version_str 627#undef hid_write 628#undef input_report 629#undef make_path 630#undef new_hid_device 631#undef read_thread 632#undef return_data 633 634#ifdef SDL_JOYSTICK_HIDAPI_STEAMXBOX 635#define HAVE_DRIVER_BACKEND 1 636#endif 637 638#ifdef HAVE_DRIVER_BACKEND 639 640// DRIVER HIDAPI Implementation 641 642struct DRIVER_hid_device_; 643typedef struct DRIVER_hid_device_ DRIVER_hid_device; 644 645#define hid_close DRIVER_hid_close 646#define hid_device DRIVER_hid_device 647#define hid_device_ DRIVER_hid_device_ 648#define hid_enumerate DRIVER_hid_enumerate 649#define hid_error DRIVER_hid_error 650#define hid_exit DRIVER_hid_exit 651#define hid_free_enumeration DRIVER_hid_free_enumeration 652#define hid_get_device_info DRIVER_hid_get_device_info 653#define hid_get_feature_report DRIVER_hid_get_feature_report 654#define hid_get_indexed_string DRIVER_hid_get_indexed_string 655#define hid_get_input_report DRIVER_hid_get_input_report 656#define hid_get_manufacturer_string DRIVER_hid_get_manufacturer_string 657#define hid_get_product_string DRIVER_hid_get_product_string 658#define hid_get_report_descriptor DRIVER_hid_get_report_descriptor 659#define hid_get_serial_number_string DRIVER_hid_get_serial_number_string 660#define hid_init DRIVER_hid_init 661#define hid_open DRIVER_hid_open 662#define hid_open_path DRIVER_hid_open_path 663#define hid_read DRIVER_hid_read 664#define hid_read_timeout DRIVER_hid_read_timeout 665#define hid_send_feature_report DRIVER_hid_send_feature_report 666#define hid_set_nonblocking DRIVER_hid_set_nonblocking 667#define hid_write DRIVER_hid_write 668 669#ifdef SDL_JOYSTICK_HIDAPI_STEAMXBOX 670#include "SDL_hidapi_steamxbox.h" 671#else 672#error Need a driver hid.c for this platform! 673#endif 674 675#undef hid_close 676#undef hid_device 677#undef hid_device_ 678#undef hid_enumerate 679#undef hid_error 680#undef hid_exit 681#undef hid_free_enumeration 682#undef hid_get_device_info 683#undef hid_get_feature_report 684#undef hid_get_indexed_string 685#undef hid_get_input_report 686#undef hid_get_manufacturer_string 687#undef hid_get_product_string 688#undef hid_get_report_descriptor 689#undef hid_get_serial_number_string 690#undef hid_init 691#undef hid_open 692#undef hid_open_path 693#undef hid_read 694#undef hid_read_timeout 695#undef hid_send_feature_report 696#undef hid_set_nonblocking 697#undef hid_write 698 699#endif // HAVE_DRIVER_BACKEND 700 701#ifdef HAVE_LIBUSB 702// libusb HIDAPI Implementation 703 704#include "../misc/SDL_libusb.h" 705 706static SDL_LibUSBContext *libusb_ctx; 707 708#define libusb_init libusb_ctx->init 709#define libusb_exit libusb_ctx->exit 710#define libusb_get_device_list libusb_ctx->get_device_list 711#define libusb_free_device_list libusb_ctx->free_device_list 712#define libusb_get_device_descriptor libusb_ctx->get_device_descriptor 713#define libusb_get_active_config_descriptor libusb_ctx->get_active_config_descriptor 714#define libusb_get_config_descriptor libusb_ctx->get_config_descriptor 715#define libusb_free_config_descriptor libusb_ctx->free_config_descriptor 716#define libusb_get_bus_number libusb_ctx->get_bus_number 717#define libusb_get_port_numbers libusb_ctx->get_port_numbers 718#define libusb_get_device_address libusb_ctx->get_device_address 719#define libusb_open libusb_ctx->open 720#define libusb_close libusb_ctx->close 721#define libusb_get_device libusb_ctx->get_device 722#define libusb_claim_interface libusb_ctx->claim_interface 723#define libusb_release_interface libusb_ctx->release_interface 724#define libusb_kernel_driver_active libusb_ctx->kernel_driver_active 725#define libusb_detach_kernel_driver libusb_ctx->detach_kernel_driver 726#define libusb_attach_kernel_driver libusb_ctx->attach_kernel_driver 727#define libusb_set_interface_alt_setting libusb_ctx->set_interface_alt_setting 728#define libusb_alloc_transfer libusb_ctx->alloc_transfer 729#define libusb_submit_transfer libusb_ctx->submit_transfer 730#define libusb_cancel_transfer libusb_ctx->cancel_transfer 731#define libusb_free_transfer libusb_ctx->free_transfer 732#define libusb_control_transfer libusb_ctx->control_transfer 733#define libusb_interrupt_transfer libusb_ctx->interrupt_transfer 734#define libusb_bulk_transfer libusb_ctx->bulk_transfer 735#define libusb_handle_events libusb_ctx->handle_events 736#define libusb_handle_events_completed libusb_ctx->handle_events_completed 737#define libusb_error_name libusb_ctx->error_name 738 739struct LIBUSB_hid_device_; 740typedef struct LIBUSB_hid_device_ LIBUSB_hid_device; 741 742#define free_hid_device LIBUSB_free_hid_device 743#define get_usb_code_for_current_locale LIBUSB_get_usb_code_for_current_locale 744#define hid_close LIBUSB_hid_close 745#define hid_device LIBUSB_hid_device 746#define hid_device_ LIBUSB_hid_device_ 747#define hid_enumerate LIBUSB_hid_enumerate 748#define hid_error LIBUSB_hid_error 749#define hid_exit LIBUSB_hid_exit 750#define hid_free_enumeration LIBUSB_hid_free_enumeration 751#define hid_get_device_info LIBUSB_hid_get_device_info 752#define hid_get_feature_report LIBUSB_hid_get_feature_report 753#define hid_get_indexed_string LIBUSB_hid_get_indexed_string 754#define hid_get_input_report LIBUSB_hid_get_input_report 755#define hid_get_manufacturer_string LIBUSB_hid_get_manufacturer_string 756#define hid_get_product_string LIBUSB_hid_get_product_string 757#define hid_get_report_descriptor LIBUSB_hid_get_report_descriptor 758#define hid_get_serial_number_string LIBUSB_hid_get_serial_number_string 759#define hid_init LIBUSB_hid_init 760#define hid_open LIBUSB_hid_open 761#define hid_open_path LIBUSB_hid_open_path 762#define hid_read LIBUSB_hid_read 763#define hid_read_timeout LIBUSB_hid_read_timeout 764#define hid_send_feature_report LIBUSB_hid_send_feature_report 765#define hid_set_nonblocking LIBUSB_hid_set_nonblocking 766#define hid_write LIBUSB_hid_write 767#define hid_libusb_wrap_sys_device LIBUSB_hid_libusb_wrap_sys_device 768#define hid_version LIBUSB_hid_version 769#define hid_version_str LIBUSB_hid_version_str 770#define input_report LIBUSB_input_report 771#define make_path LIBUSB_make_path 772#define new_hid_device LIBUSB_new_hid_device 773#define read_thread LIBUSB_read_thread 774#define return_data LIBUSB_return_data 775 776#include "SDL_hidapi_libusb.h" 777 778#undef libusb_init 779#undef libusb_exit 780#undef libusb_get_device_list 781#undef libusb_free_device_list 782#undef libusb_get_device_descriptor 783#undef libusb_get_active_config_descriptor 784#undef libusb_get_config_descriptor 785#undef libusb_free_config_descriptor 786#undef libusb_get_bus_number 787#undef libusb_get_port_numbers 788#undef libusb_get_device_address 789#undef libusb_open 790#undef libusb_close 791#undef libusb_get_device 792#undef libusb_claim_interface 793#undef libusb_release_interface 794#undef libusb_kernel_driver_active 795#undef libusb_detach_kernel_driver 796#undef libusb_attach_kernel_driver 797#undef libusb_set_interface_alt_setting 798#undef libusb_alloc_transfer 799#undef libusb_submit_transfer 800#undef libusb_cancel_transfer 801#undef libusb_free_transfer 802#undef libusb_control_transfer 803#undef libusb_interrupt_transfer 804#undef libusb_bulk_transfer 805#undef libusb_handle_events 806#undef libusb_handle_events_completed 807#undef libusb_error_name 808 809#undef free_hid_device 810#undef hid_close 811#undef hid_device 812#undef hid_device_ 813#undef hid_enumerate 814#undef hid_error 815#undef hid_exit 816#undef hid_free_enumeration 817#undef hid_get_device_info 818#undef hid_get_feature_report 819#undef hid_get_indexed_string 820#undef hid_get_input_report 821#undef hid_get_manufacturer_string 822#undef hid_get_product_string 823#undef hid_get_report_descriptor 824#undef hid_get_serial_number_string 825#undef hid_init 826#undef hid_open 827#undef hid_open_path 828#undef hid_read 829#undef hid_read_timeout 830#undef hid_send_feature_report 831#undef hid_set_nonblocking 832#undef hid_write 833#undef input_report 834#undef make_path 835#undef new_hid_device 836#undef read_thread 837#undef return_data 838 839#endif // HAVE_LIBUSB 840 841#endif // !SDL_HIDAPI_DISABLED 842 843/* If the platform has any backend other than libusb, try to avoid using 844 * libusb as the main backend for devices, since it detaches drivers and 845 * therefore makes devices inaccessible to the rest of the OS. 846 * 847 * We do this by whitelisting devices we know to be accessible _exclusively_ 848 * via libusb; these are typically devices that look like HIDs but have a 849 * quirk that requires direct access to the hardware. 850 */ 851static const struct { 852 Uint16 vendor; 853 Uint16 product; 854} SDL_libusb_required[] = { 855 { USB_VENDOR_NINTENDO, USB_PRODUCT_NINTENDO_GAMECUBE_ADAPTER }, 856 { USB_VENDOR_NINTENDO, USB_PRODUCT_NINTENDO_SWITCH2_GAMECUBE_CONTROLLER }, 857 { USB_VENDOR_NINTENDO, USB_PRODUCT_NINTENDO_SWITCH2_JOYCON_LEFT }, 858 { USB_VENDOR_NINTENDO, USB_PRODUCT_NINTENDO_SWITCH2_JOYCON_RIGHT }, 859 { USB_VENDOR_NINTENDO, USB_PRODUCT_NINTENDO_SWITCH2_PRO }, 860}; 861 862static bool RequiresLibUSB(Uint16 vendor, Uint16 product) 863{ 864 for (int i = 0; i < SDL_arraysize(SDL_libusb_required); ++i) { 865 if (vendor == SDL_libusb_required[i].vendor && 866 product == SDL_libusb_required[i].product) { 867 return true; 868 } 869 } 870 return false; 871} 872 873#if defined(HAVE_PLATFORM_BACKEND) || defined(HAVE_DRIVER_BACKEND) 874// We have another way to get HID devices, so use the whitelist to get devices where libusb is preferred 875#define SDL_HINT_HIDAPI_LIBUSB_WHITELIST_DEFAULT true 876#else 877// libusb is the only way to get HID devices, so don't use the whitelist, get them all 878#define SDL_HINT_HIDAPI_LIBUSB_WHITELIST_DEFAULT false 879#endif // HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND 880 881static bool use_libusb_whitelist = SDL_HINT_HIDAPI_LIBUSB_WHITELIST_DEFAULT; 882static bool use_libusb_gamecube = true; 883 884// Shared HIDAPI Implementation 885 886struct hidapi_backend 887{ 888 int (*hid_write)(void *device, const unsigned char *data, size_t length); 889 int (*hid_read_timeout)(void *device, unsigned char *data, size_t length, int milliseconds); 890 int (*hid_read)(void *device, unsigned char *data, size_t length); 891 int (*hid_set_nonblocking)(void *device, int nonblock); 892 int (*hid_send_feature_report)(void *device, const unsigned char *data, size_t length); 893 int (*hid_get_feature_report)(void *device, unsigned char *data, size_t length); 894 int (*hid_get_input_report)(void *device, unsigned char *data, size_t length); 895 void (*hid_close)(void *device); 896 int (*hid_get_manufacturer_string)(void *device, wchar_t *string, size_t maxlen); 897 int (*hid_get_product_string)(void *device, wchar_t *string, size_t maxlen); 898 int (*hid_get_serial_number_string)(void *device, wchar_t *string, size_t maxlen); 899 int (*hid_get_indexed_string)(void *device, int string_index, wchar_t *string, size_t maxlen); 900 struct hid_device_info *(*hid_get_device_info)(void *device); 901 int (*hid_get_report_descriptor)(void *device, unsigned char *buf, size_t buf_size); 902 const wchar_t *(*hid_error)(void *device); 903}; 904 905#ifdef HAVE_PLATFORM_BACKEND 906static const struct hidapi_backend PLATFORM_Backend = { 907 (void *)PLATFORM_hid_write, 908 (void *)PLATFORM_hid_read_timeout, 909 (void *)PLATFORM_hid_read, 910 (void *)PLATFORM_hid_set_nonblocking, 911 (void *)PLATFORM_hid_send_feature_report, 912 (void *)PLATFORM_hid_get_feature_report, 913 (void *)PLATFORM_hid_get_input_report, 914 (void *)PLATFORM_hid_close, 915 (void *)PLATFORM_hid_get_manufacturer_string, 916 (void *)PLATFORM_hid_get_product_string, 917 (void *)PLATFORM_hid_get_serial_number_string, 918 (void *)PLATFORM_hid_get_indexed_string, 919 (void *)PLATFORM_hid_get_device_info, 920 (void *)PLATFORM_hid_get_report_descriptor, 921 (void *)PLATFORM_hid_error 922}; 923#endif // HAVE_PLATFORM_BACKEND 924 925#ifdef HAVE_DRIVER_BACKEND 926static const struct hidapi_backend DRIVER_Backend = { 927 (void *)DRIVER_hid_write, 928 (void *)DRIVER_hid_read_timeout, 929 (void *)DRIVER_hid_read, 930 (void *)DRIVER_hid_set_nonblocking, 931 (void *)DRIVER_hid_send_feature_report, 932 (void *)DRIVER_hid_get_feature_report, 933 (void *)DRIVER_hid_get_input_report, 934 (void *)DRIVER_hid_close, 935 (void *)DRIVER_hid_get_manufacturer_string, 936 (void *)DRIVER_hid_get_product_string, 937 (void *)DRIVER_hid_get_serial_number_string, 938 (void *)DRIVER_hid_get_indexed_string, 939 (void *)DRIVER_hid_get_device_info, 940 (void *)DRIVER_hid_get_report_descriptor, 941 (void *)DRIVER_hid_error 942}; 943#endif // HAVE_DRIVER_BACKEND 944 945#ifdef HAVE_LIBUSB 946static const struct hidapi_backend LIBUSB_Backend = { 947 (void *)LIBUSB_hid_write, 948 (void *)LIBUSB_hid_read_timeout, 949 (void *)LIBUSB_hid_read, 950 (void *)LIBUSB_hid_set_nonblocking, 951 (void *)LIBUSB_hid_send_feature_report, 952 (void *)LIBUSB_hid_get_feature_report, 953 (void *)LIBUSB_hid_get_input_report, 954 (void *)LIBUSB_hid_close, 955 (void *)LIBUSB_hid_get_manufacturer_string, 956 (void *)LIBUSB_hid_get_product_string, 957 (void *)LIBUSB_hid_get_serial_number_string, 958 (void *)LIBUSB_hid_get_indexed_string, 959 (void *)LIBUSB_hid_get_device_info, 960 (void *)LIBUSB_hid_get_report_descriptor, 961 (void *)LIBUSB_hid_error 962}; 963#endif // HAVE_LIBUSB 964 965struct SDL_hid_device 966{ 967 void *device; 968 const struct hidapi_backend *backend; 969 SDL_hid_device_info info; 970 SDL_PropertiesID props; 971}; 972 973#if defined(HAVE_PLATFORM_BACKEND) || defined(HAVE_DRIVER_BACKEND) || defined(HAVE_LIBUSB) 974 975static SDL_hid_device *CreateHIDDeviceWrapper(void *device, const struct hidapi_backend *backend) 976{ 977 SDL_hid_device *wrapper = (SDL_hid_device *)SDL_calloc(1, sizeof(*wrapper)); 978 SDL_SetObjectValid(wrapper, SDL_OBJECT_TYPE_HIDAPI_DEVICE, true); 979 wrapper->device = device; 980 wrapper->backend = backend; 981 SDL_zero(wrapper->info); 982 return wrapper; 983} 984 985#endif // HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB 986 987static void DeleteHIDDeviceWrapper(SDL_hid_device *wrapper) 988{ 989 SDL_SetObjectValid(wrapper, SDL_OBJECT_TYPE_HIDAPI_DEVICE, false); 990 SDL_free(wrapper->info.path); 991 SDL_free(wrapper->info.serial_number); 992 SDL_free(wrapper->info.manufacturer_string); 993 SDL_free(wrapper->info.product_string); 994 SDL_free(wrapper); 995} 996 997#define CHECK_DEVICE_MAGIC(device, result) \ 998 if (!SDL_ObjectValid(device, SDL_OBJECT_TYPE_HIDAPI_DEVICE)) { \ 999 SDL_SetError("Invalid device"); \ 1000 return result; \ 1001 } 1002 1003#define COPY_IF_EXISTS(var) \ 1004 if (pSrc->var != NULL) { \ 1005 pDst->var = SDL_strdup(pSrc->var); \ 1006 } else { \ 1007 pDst->var = NULL; \ 1008 } 1009#define WCOPY_IF_EXISTS(var) \ 1010 if (pSrc->var != NULL) { \ 1011 pDst->var = SDL_wcsdup(pSrc->var); \ 1012 } else { \ 1013 pDst->var = NULL; \ 1014 } 1015 1016static void CopyHIDDeviceInfo(struct hid_device_info *pSrc, struct SDL_hid_device_info *pDst) 1017{ 1018 COPY_IF_EXISTS(path) 1019 pDst->vendor_id = pSrc->vendor_id; 1020 pDst->product_id = pSrc->product_id; 1021 WCOPY_IF_EXISTS(serial_number) 1022 pDst->release_number = pSrc->release_number; 1023 WCOPY_IF_EXISTS(manufacturer_string) 1024 WCOPY_IF_EXISTS(product_string) 1025 pDst->usage_page = pSrc->usage_page; 1026 pDst->usage = pSrc->usage; 1027 pDst->interface_number = pSrc->interface_number; 1028 pDst->interface_class = pSrc->interface_class; 1029 pDst->interface_subclass = pSrc->interface_subclass; 1030 pDst->interface_protocol = pSrc->interface_protocol; 1031 pDst->bus_type = (SDL_hid_bus_type)pSrc->bus_type; 1032 pDst->next = NULL; 1033} 1034 1035#undef COPY_IF_EXISTS 1036#undef WCOPY_IF_EXISTS 1037 1038static int SDL_hidapi_refcount = 0; 1039static bool SDL_hidapi_only_controllers; 1040static char *SDL_hidapi_ignored_devices = NULL; 1041 1042static void SDLCALL OnlyControllersChanged(void *userdata, const char *name, const char *oldValue, const char *hint) 1043{ 1044 SDL_hidapi_only_controllers = SDL_GetStringBoolean(hint, true); 1045} 1046 1047static void SDLCALL IgnoredDevicesChanged(void *userdata, const char *name, const char *oldValue, const char *hint) 1048{ 1049 SDL_free(SDL_hidapi_ignored_devices); 1050 if (hint && *hint) { 1051 SDL_hidapi_ignored_devices = SDL_strdup(hint); 1052 } else { 1053 SDL_hidapi_ignored_devices = NULL; 1054 } 1055} 1056 1057bool SDL_HIDAPI_ShouldIgnoreDevice(int bus, Uint16 vendor_id, Uint16 product_id, Uint16 usage_page, Uint16 usage, bool libusb) 1058{ 1059 if (libusb) { 1060 if (use_libusb_whitelist && !RequiresLibUSB(vendor_id, product_id)) { 1061 return true; 1062 } 1063 if (!use_libusb_gamecube && 1064 vendor_id == USB_VENDOR_NINTENDO && product_id == USB_PRODUCT_NINTENDO_GAMECUBE_ADAPTER) { 1065 return true; 1066 } 1067 } else { 1068 if (RequiresLibUSB(vendor_id, product_id)) { 1069 return true; 1070 } 1071 } 1072 1073 // See if there are any devices we should skip in enumeration 1074 if (SDL_hidapi_only_controllers && usage_page) { 1075 if (vendor_id == USB_VENDOR_VALVE) { 1076 // Ignore the mouse/keyboard interface on Steam Controllers 1077 if ( 1078#ifdef SDL_PLATFORM_WIN32 1079 // Check the usage page and usage on both USB and Bluetooth 1080#else 1081 // Only check the usage page and usage on USB 1082 bus == HID_API_BUS_USB && 1083#endif 1084 usage_page == USB_USAGEPAGE_GENERIC_DESKTOP && 1085 (usage == USB_USAGE_GENERIC_KEYBOARD || usage == USB_USAGE_GENERIC_MOUSE)) { 1086 return true; 1087 } 1088 } else if (vendor_id == USB_VENDOR_FLYDIGI_V1 && product_id == USB_PRODUCT_FLYDIGI_V1_GAMEPAD) { 1089 if (usage_page == USB_USAGEPAGE_VENDOR_FLYDIGI) { 1090 return false; 1091 } 1092 return true; 1093 } else if (vendor_id == USB_VENDOR_FLYDIGI_V2 && 1094 (product_id == USB_PRODUCT_FLYDIGI_V2_APEX || product_id == USB_PRODUCT_FLYDIGI_V2_VADER)) { 1095 if (usage_page == USB_USAGEPAGE_VENDOR_FLYDIGI) { 1096 return false; 1097 } 1098 return true; 1099 } else if (usage_page == USB_USAGEPAGE_GENERIC_DESKTOP && 1100 (usage == USB_USAGE_GENERIC_JOYSTICK || usage == USB_USAGE_GENERIC_GAMEPAD || usage == USB_USAGE_GENERIC_MULTIAXISCONTROLLER)) { 1101 // This is a controller 1102 } else { 1103 return true; 1104 } 1105 } 1106 if (SDL_hidapi_ignored_devices) { 1107 char vendor_match[16], product_match[16]; 1108 SDL_snprintf(vendor_match, sizeof(vendor_match), "0x%.4x/0x0000", vendor_id); 1109 SDL_snprintf(product_match, sizeof(product_match), "0x%.4x/0x%.4x", vendor_id, product_id); 1110 if (SDL_strcasestr(SDL_hidapi_ignored_devices, vendor_match) || 1111 SDL_strcasestr(SDL_hidapi_ignored_devices, product_match)) { 1112 return true; 1113 } 1114 } 1115 return false; 1116} 1117 1118int SDL_hid_init(void) 1119{ 1120 int attempts = 0, success = 0; 1121 1122 if (SDL_hidapi_refcount > 0) { 1123 ++SDL_hidapi_refcount; 1124 return 0; 1125 } 1126 1127 SDL_AddHintCallback(SDL_HINT_HIDAPI_ENUMERATE_ONLY_CONTROLLERS, OnlyControllersChanged, NULL); 1128 SDL_AddHintCallback(SDL_HINT_HIDAPI_IGNORE_DEVICES, IgnoredDevicesChanged, NULL); 1129 1130#ifdef SDL_USE_LIBUDEV 1131 if (!SDL_GetHintBoolean(SDL_HINT_HIDAPI_UDEV, true)) { 1132 SDL_LogDebug(SDL_LOG_CATEGORY_INPUT, 1133 "udev disabled by SDL_HINT_HIDAPI_UDEV"); 1134 linux_enumeration_method = ENUMERATION_FALLBACK; 1135 } else if (SDL_GetSandbox() != SDL_SANDBOX_NONE) { 1136 SDL_LogDebug(SDL_LOG_CATEGORY_INPUT, 1137 "Container detected, disabling HIDAPI udev integration"); 1138 linux_enumeration_method = ENUMERATION_FALLBACK; 1139 } else { 1140 SDL_LogDebug(SDL_LOG_CATEGORY_INPUT, 1141 "Using udev for HIDAPI joystick device discovery"); 1142 linux_enumeration_method = ENUMERATION_LIBUDEV; 1143 } 1144#endif 1145 1146 use_libusb_whitelist = SDL_GetHintBoolean(SDL_HINT_HIDAPI_LIBUSB_WHITELIST, 1147 SDL_HINT_HIDAPI_LIBUSB_WHITELIST_DEFAULT); 1148 use_libusb_gamecube = SDL_GetHintBoolean(SDL_HINT_HIDAPI_LIBUSB_GAMECUBE, true); 1149#ifdef HAVE_LIBUSB 1150 if (!SDL_GetHintBoolean(SDL_HINT_HIDAPI_LIBUSB, true)) { 1151 SDL_LogDebug(SDL_LOG_CATEGORY_INPUT, 1152 "libusb disabled with SDL_HINT_HIDAPI_LIBUSB"); 1153 } else { 1154 ++attempts; 1155 if (!SDL_InitLibUSB(&libusb_ctx)) { 1156 SDL_LogDebug(SDL_LOG_CATEGORY_INPUT, "Couldn't load libusb"); 1157 } else if (LIBUSB_hid_init() < 0) { 1158 SDL_QuitLibUSB(); 1159 libusb_ctx = NULL; 1160 } else { 1161 ++success; 1162 } 1163 } 1164#endif // HAVE_LIBUSB 1165 1166#ifdef HAVE_PLATFORM_BACKEND 1167 ++attempts; 1168#ifdef SDL_PLATFORM_LINUX 1169 udev_ctx = SDL_UDEV_GetUdevSyms(); 1170#endif // __LINUX __ 1171 if (udev_ctx && PLATFORM_hid_init() == 0) { 1172 ++success; 1173 } 1174#endif // HAVE_PLATFORM_BACKEND 1175 1176 if (attempts > 0 && success == 0) { 1177 return -1; 1178 } 1179 1180#if defined(SDL_PLATFORM_MACOS) && !defined(SDL_HIDAPI_DISABLED) 1181 hid_darwin_set_open_exclusive(0); 1182#endif 1183 1184 ++SDL_hidapi_refcount; 1185 return 0; 1186} 1187 1188int SDL_hid_exit(void) 1189{ 1190 int result = 0; 1191 1192 if (SDL_hidapi_refcount == 0) { 1193 return 0; 1194 } 1195 --SDL_hidapi_refcount; 1196 if (SDL_hidapi_refcount > 0) { 1197 return 0; 1198 } 1199 SDL_hidapi_refcount = 0; 1200 1201#ifndef SDL_HIDAPI_DISABLED 1202 HIDAPI_ShutdownDiscovery(); 1203#endif 1204 1205#ifdef HAVE_PLATFORM_BACKEND 1206 if (udev_ctx) { 1207 result |= PLATFORM_hid_exit(); 1208 } 1209#ifdef SDL_PLATFORM_LINUX 1210 SDL_UDEV_ReleaseUdevSyms(); 1211#endif // __LINUX __ 1212#endif // HAVE_PLATFORM_BACKEND 1213 1214#ifdef HAVE_LIBUSB 1215 if (libusb_ctx) { 1216 result |= LIBUSB_hid_exit(); 1217 SDL_QuitLibUSB(); 1218 libusb_ctx = NULL; 1219 } 1220#endif // HAVE_LIBUSB 1221 1222 SDL_RemoveHintCallback(SDL_HINT_HIDAPI_ENUMERATE_ONLY_CONTROLLERS, OnlyControllersChanged, NULL); 1223 SDL_RemoveHintCallback(SDL_HINT_HIDAPI_IGNORE_DEVICES, IgnoredDevicesChanged, NULL); 1224 1225 if (SDL_hidapi_ignored_devices) { 1226 SDL_free(SDL_hidapi_ignored_devices); 1227 SDL_hidapi_ignored_devices = NULL; 1228 } 1229 1230 return result; 1231} 1232 1233Uint32 SDL_hid_device_change_count(void) 1234{ 1235 Uint32 counter = 0; 1236 1237#ifndef SDL_HIDAPI_DISABLED 1238 if (SDL_hidapi_refcount == 0 && SDL_hid_init() < 0) { 1239 return 0; 1240 } 1241 1242 HIDAPI_UpdateDiscovery(); 1243 1244 if (SDL_HIDAPI_discovery.m_unDeviceChangeCounter == 0) { 1245 // Counter wrapped! 1246 ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter; 1247 } 1248 counter = SDL_HIDAPI_discovery.m_unDeviceChangeCounter; 1249 1250#endif // !SDL_HIDAPI_DISABLED 1251 1252 return counter; 1253} 1254 1255static void AddDeviceToEnumeration(const char *driver_name, struct hid_device_info *dev, struct SDL_hid_device_info **devs, struct SDL_hid_device_info **last) 1256{ 1257 struct SDL_hid_device_info *new_dev; 1258 1259#ifdef DEBUG_HIDAPI 1260 SDL_Log("Adding %s device to enumeration: %ls %ls 0x%.4hx/0x%.4hx/%d", 1261 driver_name, dev->manufacturer_string, dev->product_string, dev->vendor_id, dev->product_id, dev->interface_number); 1262#else 1263 (void)driver_name; 1264#endif 1265 1266 new_dev = (struct SDL_hid_device_info *)SDL_malloc(sizeof(struct SDL_hid_device_info)); 1267 if (new_dev == NULL) { 1268 // Don't bother returning an error, get as many devices as possible 1269 return; 1270 } 1271 CopyHIDDeviceInfo(dev, new_dev); 1272 1273 if ((*last) != NULL) { 1274 (*last)->next = new_dev; 1275 } else { 1276 *devs = new_dev; 1277 } 1278 *last = new_dev; 1279} 1280 1281#if defined(HAVE_LIBUSB) || defined(HAVE_PLATFORM_BACKEND) 1282static void RemoveDeviceFromEnumeration(const char *driver_name, struct hid_device_info *dev, struct hid_device_info **devs, void (*free_device_info)(struct hid_device_info *)) 1283{ 1284 struct hid_device_info *last = NULL, *curr, *next; 1285 1286 for (curr = *devs; curr; curr = next) { 1287 next = curr->next; 1288 1289 if (dev->vendor_id == curr->vendor_id && 1290 dev->product_id == curr->product_id && 1291 (dev->interface_number < 0 || curr->interface_number < 0 || dev->interface_number == curr->interface_number)) { 1292#ifdef DEBUG_HIDAPI 1293 SDL_Log("Skipping %s device: %ls %ls 0x%.4hx/0x%.4hx/%d", 1294 driver_name, curr->manufacturer_string, curr->product_string, curr->vendor_id, curr->product_id, curr->interface_number); 1295#else 1296 (void)driver_name; 1297#endif 1298 if (last) { 1299 last->next = next; 1300 } else { 1301 *devs = next; 1302 } 1303 1304 curr->next = NULL; 1305 free_device_info(curr); 1306 continue; 1307 } 1308 last = curr; 1309 } 1310} 1311#endif // HAVE_LIBUSB || HAVE_PLATFORM_BACKEND 1312 1313struct SDL_hid_device_info *SDL_hid_enumerate(unsigned short vendor_id, unsigned short product_id) 1314{ 1315 struct hid_device_info *driver_devs = NULL; 1316 struct hid_device_info *usb_devs = NULL; 1317 struct hid_device_info *raw_devs = NULL; 1318 struct hid_device_info *dev; 1319 struct SDL_hid_device_info *devs = NULL, *last = NULL; 1320 1321 if (SDL_hidapi_refcount == 0 && SDL_hid_init() < 0) { 1322 return NULL; 1323 } 1324 1325 // Collect the available devices 1326#ifdef HAVE_DRIVER_BACKEND 1327 driver_devs = DRIVER_hid_enumerate(vendor_id, product_id); 1328#endif 1329 1330#ifdef HAVE_LIBUSB 1331 if (libusb_ctx) { 1332 usb_devs = LIBUSB_hid_enumerate(vendor_id, product_id); 1333 } 1334#endif // HAVE_LIBUSB 1335 1336#ifdef HAVE_PLATFORM_BACKEND 1337 if (udev_ctx) { 1338 raw_devs = PLATFORM_hid_enumerate(vendor_id, product_id); 1339 } 1340#endif 1341 1342 // Highest priority are custom driver devices 1343 for (dev = driver_devs; dev; dev = dev->next) { 1344 AddDeviceToEnumeration("driver", dev, &devs, &last); 1345#ifdef HAVE_LIBUSB 1346 RemoveDeviceFromEnumeration("libusb", dev, &usb_devs, LIBUSB_hid_free_enumeration); 1347#endif 1348#ifdef HAVE_PLATFORM_BACKEND 1349 RemoveDeviceFromEnumeration("raw", dev, &raw_devs, PLATFORM_hid_free_enumeration); 1350#endif 1351 } 1352 1353 // If whitelist is in effect, libusb has priority, otherwise raw devices do 1354 if (use_libusb_whitelist) { 1355 for (dev = usb_devs; dev; dev = dev->next) { 1356 AddDeviceToEnumeration("libusb", dev, &devs, &last); 1357#ifdef HAVE_PLATFORM_BACKEND 1358 RemoveDeviceFromEnumeration("raw", dev, &raw_devs, PLATFORM_hid_free_enumeration); 1359#endif 1360 } 1361 for (dev = raw_devs; dev; dev = dev->next) { 1362 AddDeviceToEnumeration("platform", dev, &devs, &last); 1363 } 1364 } else { 1365 for (dev = raw_devs; dev; dev = dev->next) { 1366 AddDeviceToEnumeration("raw", dev, &devs, &last); 1367#ifdef HAVE_LIBUSB 1368 RemoveDeviceFromEnumeration("libusb", dev, &usb_devs, LIBUSB_hid_free_enumeration); 1369#endif 1370 } 1371 for (dev = usb_devs; dev; dev = dev->next) { 1372 AddDeviceToEnumeration("libusb", dev, &devs, &last); 1373 } 1374 } 1375 1376#ifdef HAVE_DRIVER_BACKEND 1377 DRIVER_hid_free_enumeration(driver_devs); 1378#endif 1379#ifdef HAVE_LIBUSB 1380 LIBUSB_hid_free_enumeration(usb_devs); 1381#endif 1382#ifdef HAVE_PLATFORM_BACKEND 1383 PLATFORM_hid_free_enumeration(raw_devs); 1384#endif 1385 1386 return devs; 1387} 1388 1389void SDL_hid_free_enumeration(struct SDL_hid_device_info *devs) 1390{ 1391 while (devs) { 1392 struct SDL_hid_device_info *next = devs->next; 1393 SDL_free(devs->path); 1394 SDL_free(devs->serial_number); 1395 SDL_free(devs->manufacturer_string); 1396 SDL_free(devs->product_string); 1397 SDL_free(devs); 1398 devs = next; 1399 } 1400} 1401 1402SDL_hid_device *SDL_hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number) 1403{ 1404#if defined(HAVE_PLATFORM_BACKEND) || defined(HAVE_DRIVER_BACKEND) || defined(HAVE_LIBUSB) 1405 void *pDevice = NULL; 1406 1407 if (SDL_hidapi_refcount == 0 && SDL_hid_init() < 0) { 1408 return NULL; 1409 } 1410 1411#ifdef HAVE_PLATFORM_BACKEND 1412 if (udev_ctx) { 1413 pDevice = PLATFORM_hid_open(vendor_id, product_id, serial_number); 1414 if (pDevice != NULL) { 1415 return CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend); 1416 } 1417 } 1418#endif // HAVE_PLATFORM_BACKEND 1419 1420#ifdef HAVE_DRIVER_BACKEND 1421 pDevice = DRIVER_hid_open(vendor_id, product_id, serial_number); 1422 if (pDevice != NULL) { 1423 return CreateHIDDeviceWrapper(pDevice, &DRIVER_Backend); 1424 } 1425#endif // HAVE_DRIVER_BACKEND 1426 1427#ifdef HAVE_LIBUSB 1428 if (libusb_ctx) { 1429 pDevice = LIBUSB_hid_open(vendor_id, product_id, serial_number); 1430 if (pDevice != NULL) { 1431 SDL_hid_device *dev = CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend); 1432 SDL_SetPointerProperty(SDL_hid_get_properties(dev), SDL_PROP_HIDAPI_LIBUSB_DEVICE_HANDLE_POINTER, ((LIBUSB_hid_device *)pDevice)->device_handle); 1433 return dev; 1434 } 1435 } 1436#endif // HAVE_LIBUSB 1437 1438#endif // HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB 1439 1440 return NULL; 1441} 1442 1443SDL_hid_device *SDL_hid_open_path(const char *path) 1444{ 1445#if defined(HAVE_PLATFORM_BACKEND) || defined(HAVE_DRIVER_BACKEND) || defined(HAVE_LIBUSB) 1446 void *pDevice = NULL; 1447 1448 if (SDL_hidapi_refcount == 0 && SDL_hid_init() < 0) { 1449 return NULL; 1450 } 1451 1452#ifdef HAVE_PLATFORM_BACKEND 1453 if (udev_ctx) { 1454 pDevice = PLATFORM_hid_open_path(path); 1455 if (pDevice != NULL) { 1456 return CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend); 1457 } 1458 } 1459#endif // HAVE_PLATFORM_BACKEND 1460 1461#ifdef HAVE_DRIVER_BACKEND 1462 pDevice = DRIVER_hid_open_path(path); 1463 if (pDevice != NULL) { 1464 return CreateHIDDeviceWrapper(pDevice, &DRIVER_Backend); 1465 } 1466#endif // HAVE_DRIVER_BACKEND 1467 1468#ifdef HAVE_LIBUSB 1469 if (libusb_ctx) { 1470 pDevice = LIBUSB_hid_open_path(path); 1471 if (pDevice != NULL) { 1472 SDL_hid_device *dev = CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend); 1473 SDL_SetPointerProperty(SDL_hid_get_properties(dev), SDL_PROP_HIDAPI_LIBUSB_DEVICE_HANDLE_POINTER, ((LIBUSB_hid_device *)pDevice)->device_handle); 1474 return dev; 1475 } 1476 } 1477#endif // HAVE_LIBUSB 1478 1479#endif // HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB 1480 1481 return NULL; 1482} 1483 1484SDL_PropertiesID SDL_hid_get_properties(SDL_hid_device *device) 1485{ 1486 CHECK_DEVICE_MAGIC(device, 0); 1487 1488 if (!device->props) { 1489 device->props = SDL_CreateProperties(); 1490 } 1491 return device->props; 1492} 1493 1494int SDL_hid_write(SDL_hid_device *device, const unsigned char *data, size_t length) 1495{ 1496 CHECK_DEVICE_MAGIC(device, -1); 1497 1498 return device->backend->hid_write(device->device, data, length); 1499} 1500 1501int SDL_hid_read_timeout(SDL_hid_device *device, unsigned char *data, size_t length, int milliseconds) 1502{ 1503 CHECK_DEVICE_MAGIC(device, -1); 1504 1505 return device->backend->hid_read_timeout(device->device, data, length, milliseconds); 1506} 1507 1508int SDL_hid_read(SDL_hid_device *device, unsigned char *data, size_t length) 1509{ 1510 CHECK_DEVICE_MAGIC(device, -1); 1511 1512 return device->backend->hid_read(device->device, data, length); 1513} 1514 1515int SDL_hid_set_nonblocking(SDL_hid_device *device, int nonblock) 1516{ 1517 CHECK_DEVICE_MAGIC(device, -1); 1518 1519 return device->backend->hid_set_nonblocking(device->device, nonblock); 1520} 1521 1522int SDL_hid_send_feature_report(SDL_hid_device *device, const unsigned char *data, size_t length) 1523{ 1524 CHECK_DEVICE_MAGIC(device, -1); 1525 1526 return device->backend->hid_send_feature_report(device->device, data, length); 1527} 1528 1529int SDL_hid_get_feature_report(SDL_hid_device *device, unsigned char *data, size_t length) 1530{ 1531 CHECK_DEVICE_MAGIC(device, -1); 1532 1533 return device->backend->hid_get_feature_report(device->device, data, length); 1534} 1535 1536int SDL_hid_get_input_report(SDL_hid_device *device, unsigned char *data, size_t length) 1537{ 1538 CHECK_DEVICE_MAGIC(device, -1); 1539 1540 return device->backend->hid_get_input_report(device->device, data, length); 1541} 1542 1543int SDL_hid_close(SDL_hid_device *device) 1544{ 1545 CHECK_DEVICE_MAGIC(device, -1); 1546 1547 device->backend->hid_close(device->device); 1548 SDL_DestroyProperties(device->props); 1549 DeleteHIDDeviceWrapper(device); 1550 return 0; 1551} 1552 1553int SDL_hid_get_manufacturer_string(SDL_hid_device *device, wchar_t *string, size_t maxlen) 1554{ 1555 CHECK_DEVICE_MAGIC(device, -1); 1556 1557 return device->backend->hid_get_manufacturer_string(device->device, string, maxlen); 1558} 1559 1560int SDL_hid_get_product_string(SDL_hid_device *device, wchar_t *string, size_t maxlen) 1561{ 1562 CHECK_DEVICE_MAGIC(device, -1); 1563 1564 return device->backend->hid_get_product_string(device->device, string, maxlen); 1565} 1566 1567int SDL_hid_get_serial_number_string(SDL_hid_device *device, wchar_t *string, size_t maxlen) 1568{ 1569 CHECK_DEVICE_MAGIC(device, -1); 1570 1571 return device->backend->hid_get_serial_number_string(device->device, string, maxlen); 1572} 1573 1574int SDL_hid_get_indexed_string(SDL_hid_device *device, int string_index, wchar_t *string, size_t maxlen) 1575{ 1576 CHECK_DEVICE_MAGIC(device, -1); 1577 1578 return device->backend->hid_get_indexed_string(device->device, string_index, string, maxlen); 1579} 1580 1581SDL_hid_device_info *SDL_hid_get_device_info(SDL_hid_device *device) 1582{ 1583 struct hid_device_info *info; 1584 1585 CHECK_DEVICE_MAGIC(device, NULL); 1586 1587 info = device->backend->hid_get_device_info(device->device); 1588 if (info) { 1589 CopyHIDDeviceInfo(info, &device->info); 1590 return &device->info; 1591 } else { 1592 return NULL; 1593 } 1594} 1595 1596int SDL_hid_get_report_descriptor(SDL_hid_device *device, unsigned char *buf, size_t buf_size) 1597{ 1598 CHECK_DEVICE_MAGIC(device, -1); 1599 1600 return device->backend->hid_get_report_descriptor(device->device, buf, buf_size); 1601} 1602 1603void SDL_hid_ble_scan(bool active) 1604{ 1605#if !defined(SDL_HIDAPI_DISABLED) && (defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS)) 1606 extern void hid_ble_scan(int bStart); 1607 hid_ble_scan(active); 1608#endif 1609} 1610[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.