Path: blob/master/thirdparty/sdl/hidapi/SDL_hidapi.c
10279 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/2021/* Original hybrid wrapper for Linux by Valve Software. Their original notes:22*23* The libusb version doesn't support Bluetooth, but not all Linux24* distributions allow access to /dev/hidraw*25*26* This merges the two, at a small performance cost, until distributions27* have granted access to /dev/hidraw*28*/2930#include "SDL_internal.h"3132#include "SDL_hidapi_c.h"33#include "../joystick/usb_ids.h"34#include "../SDL_hints_c.h"3536// Initial type declarations37#define HID_API_NO_EXPORT_DEFINE // do not export hidapi procedures38#include "hidapi/hidapi.h"3940#ifndef SDL_HIDAPI_DISABLED4142#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)43#include "../core/windows/SDL_windows.h"44#endif4546#ifdef SDL_PLATFORM_MACOS47#include <CoreFoundation/CoreFoundation.h>48#include <mach/mach.h>49#include <IOKit/IOKitLib.h>50#include <IOKit/hid/IOHIDDevice.h>51#include <IOKit/usb/USBSpec.h>52#include <AvailabilityMacros.h>53// Things named "Master" were renamed to "Main" in macOS 12.0's SDK.54#if MAC_OS_X_VERSION_MIN_REQUIRED < 12000055#define kIOMainPortDefault kIOMasterPortDefault56#endif57#endif5859#include "../core/linux/SDL_udev.h"60#ifdef SDL_USE_LIBUDEV61#include <poll.h>62#endif6364#ifdef HAVE_INOTIFY65#include <string.h> // strerror66#include <errno.h> // errno67#include <fcntl.h>68#include <limits.h> // For the definition of NAME_MAX69#include <sys/inotify.h>70#endif7172#if defined(SDL_USE_LIBUDEV) || defined(HAVE_INOTIFY)73#include <unistd.h>74#endif7576#ifdef SDL_USE_LIBUDEV77typedef enum78{79ENUMERATION_UNSET,80ENUMERATION_LIBUDEV,81ENUMERATION_FALLBACK82} LinuxEnumerationMethod;8384static LinuxEnumerationMethod linux_enumeration_method = ENUMERATION_UNSET;85#endif8687#ifdef HAVE_INOTIFY88static int inotify_fd = -1;89#endif9091#ifdef SDL_USE_LIBUDEV92static const SDL_UDEV_Symbols *usyms = NULL;93#endif9495static struct96{97bool m_bInitialized;98Uint32 m_unDeviceChangeCounter;99bool m_bCanGetNotifications;100Uint64 m_unLastDetect;101102#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)103SDL_ThreadID m_nThreadID;104WNDCLASSEXA m_wndClass;105HWND m_hwndMsg;106HDEVNOTIFY m_hNotify;107double m_flLastWin32MessageCheck;108#endif109110#ifdef SDL_PLATFORM_MACOS111IONotificationPortRef m_notificationPort;112mach_port_t m_notificationMach;113#endif114115#ifdef SDL_USE_LIBUDEV116struct udev *m_pUdev;117struct udev_monitor *m_pUdevMonitor;118int m_nUdevFd;119#endif120} SDL_HIDAPI_discovery;121122#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)123struct _DEV_BROADCAST_HDR124{125DWORD dbch_size;126DWORD dbch_devicetype;127DWORD dbch_reserved;128};129130typedef struct _DEV_BROADCAST_DEVICEINTERFACE_A131{132DWORD dbcc_size;133DWORD dbcc_devicetype;134DWORD dbcc_reserved;135GUID dbcc_classguid;136char dbcc_name[1];137} DEV_BROADCAST_DEVICEINTERFACE_A, *PDEV_BROADCAST_DEVICEINTERFACE_A;138139typedef struct _DEV_BROADCAST_HDR DEV_BROADCAST_HDR;140#define DBT_DEVICEARRIVAL 0x8000 // system detected a new device141#define DBT_DEVICEREMOVECOMPLETE 0x8004 // device was removed from the system142#define DBT_DEVTYP_DEVICEINTERFACE 0x00000005 // device interface class143#define DBT_DEVNODES_CHANGED 0x0007144#define DBT_CONFIGCHANGED 0x0018145#define DBT_DEVICETYPESPECIFIC 0x8005 // type specific event146#define DBT_DEVINSTSTARTED 0x8008 // device installed and started147148#include <initguid.h>149DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);150151static LRESULT CALLBACK ControllerWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)152{153switch (message) {154case WM_DEVICECHANGE:155switch (wParam) {156case DBT_DEVICEARRIVAL:157case DBT_DEVICEREMOVECOMPLETE:158if (((DEV_BROADCAST_HDR *)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {159++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;160}161break;162}163return TRUE;164}165166return DefWindowProc(hwnd, message, wParam, lParam);167}168#endif // defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)169170#ifdef SDL_PLATFORM_MACOS171static void CallbackIOServiceFunc(void *context, io_iterator_t portIterator)172{173// Must drain the iterator, or we won't receive new notifications174io_object_t entry;175while ((entry = IOIteratorNext(portIterator)) != 0) {176IOObjectRelease(entry);177++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;178}179}180#endif // SDL_PLATFORM_MACOS181182#ifdef HAVE_INOTIFY183#ifdef HAVE_INOTIFY_INIT1184static int SDL_inotify_init1(void)185{186return inotify_init1(IN_NONBLOCK | IN_CLOEXEC);187}188#else189static int SDL_inotify_init1(void)190{191int fd = inotify_init();192if (fd < 0) {193return -1;194}195fcntl(fd, F_SETFL, O_NONBLOCK);196fcntl(fd, F_SETFD, FD_CLOEXEC);197return fd;198}199#endif200201static int StrHasPrefix(const char *string, const char *prefix)202{203return SDL_strncmp(string, prefix, SDL_strlen(prefix)) == 0;204}205206static int StrIsInteger(const char *string)207{208const char *p;209210if (*string == '\0') {211return 0;212}213214for (p = string; *p != '\0'; p++) {215if (*p < '0' || *p > '9') {216return 0;217}218}219220return 1;221}222#endif // HAVE_INOTIFY223224static void HIDAPI_InitializeDiscovery(void)225{226SDL_HIDAPI_discovery.m_bInitialized = true;227SDL_HIDAPI_discovery.m_unDeviceChangeCounter = 1;228SDL_HIDAPI_discovery.m_bCanGetNotifications = false;229SDL_HIDAPI_discovery.m_unLastDetect = 0;230231#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)232SDL_HIDAPI_discovery.m_nThreadID = SDL_GetCurrentThreadID();233234SDL_zero(SDL_HIDAPI_discovery.m_wndClass);235SDL_HIDAPI_discovery.m_wndClass.hInstance = GetModuleHandle(NULL);236SDL_HIDAPI_discovery.m_wndClass.lpszClassName = "SDL_HIDAPI_DEVICE_DETECTION";237SDL_HIDAPI_discovery.m_wndClass.lpfnWndProc = ControllerWndProc; // This function is called by windows238SDL_HIDAPI_discovery.m_wndClass.cbSize = sizeof(WNDCLASSEX);239240RegisterClassExA(&SDL_HIDAPI_discovery.m_wndClass);241SDL_HIDAPI_discovery.m_hwndMsg = CreateWindowExA(0, "SDL_HIDAPI_DEVICE_DETECTION", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);242243{244DEV_BROADCAST_DEVICEINTERFACE_A devBroadcast;245246SDL_zero(devBroadcast);247devBroadcast.dbcc_size = sizeof(devBroadcast);248devBroadcast.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;249devBroadcast.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;250251/* DEVICE_NOTIFY_ALL_INTERFACE_CLASSES is important, makes GUID_DEVINTERFACE_USB_DEVICE ignored,252* but that seems to be necessary to get a notice after each individual usb input device actually253* installs, rather than just as the composite device is seen.254*/255SDL_HIDAPI_discovery.m_hNotify = RegisterDeviceNotification(SDL_HIDAPI_discovery.m_hwndMsg, &devBroadcast, DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);256SDL_HIDAPI_discovery.m_bCanGetNotifications = (SDL_HIDAPI_discovery.m_hNotify != 0);257}258#endif // defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)259260#ifdef SDL_PLATFORM_MACOS261SDL_HIDAPI_discovery.m_notificationPort = IONotificationPortCreate(kIOMainPortDefault);262if (SDL_HIDAPI_discovery.m_notificationPort) {263{264io_iterator_t portIterator = 0;265io_object_t entry;266IOReturn result = IOServiceAddMatchingNotification(267SDL_HIDAPI_discovery.m_notificationPort,268kIOFirstMatchNotification,269IOServiceMatching(kIOHIDDeviceKey),270CallbackIOServiceFunc, NULL, &portIterator);271272if (result == 0) {273// Must drain the existing iterator, or we won't receive new notifications274while ((entry = IOIteratorNext(portIterator)) != 0) {275IOObjectRelease(entry);276}277} else {278IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);279SDL_HIDAPI_discovery.m_notificationPort = nil;280}281}282{283io_iterator_t portIterator = 0;284io_object_t entry;285IOReturn result = IOServiceAddMatchingNotification(286SDL_HIDAPI_discovery.m_notificationPort,287kIOTerminatedNotification,288IOServiceMatching(kIOHIDDeviceKey),289CallbackIOServiceFunc, NULL, &portIterator);290291if (result == 0) {292// Must drain the existing iterator, or we won't receive new notifications293while ((entry = IOIteratorNext(portIterator)) != 0) {294IOObjectRelease(entry);295}296} else {297IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);298SDL_HIDAPI_discovery.m_notificationPort = nil;299}300}301}302303SDL_HIDAPI_discovery.m_notificationMach = MACH_PORT_NULL;304if (SDL_HIDAPI_discovery.m_notificationPort) {305SDL_HIDAPI_discovery.m_notificationMach = IONotificationPortGetMachPort(SDL_HIDAPI_discovery.m_notificationPort);306}307308SDL_HIDAPI_discovery.m_bCanGetNotifications = (SDL_HIDAPI_discovery.m_notificationMach != MACH_PORT_NULL);309310#endif // SDL_PLATFORM_MACOS311312#ifdef SDL_USE_LIBUDEV313if (linux_enumeration_method == ENUMERATION_LIBUDEV) {314SDL_HIDAPI_discovery.m_pUdev = NULL;315SDL_HIDAPI_discovery.m_pUdevMonitor = NULL;316SDL_HIDAPI_discovery.m_nUdevFd = -1;317318usyms = SDL_UDEV_GetUdevSyms();319if (usyms != NULL) {320SDL_HIDAPI_discovery.m_pUdev = usyms->udev_new();321if (SDL_HIDAPI_discovery.m_pUdev != NULL) {322SDL_HIDAPI_discovery.m_pUdevMonitor = usyms->udev_monitor_new_from_netlink(SDL_HIDAPI_discovery.m_pUdev, "udev");323if (SDL_HIDAPI_discovery.m_pUdevMonitor != NULL) {324usyms->udev_monitor_enable_receiving(SDL_HIDAPI_discovery.m_pUdevMonitor);325SDL_HIDAPI_discovery.m_nUdevFd = usyms->udev_monitor_get_fd(SDL_HIDAPI_discovery.m_pUdevMonitor);326SDL_HIDAPI_discovery.m_bCanGetNotifications = true;327}328}329}330} else331#endif // SDL_USE_LIBUDEV332{333#ifdef HAVE_INOTIFY334inotify_fd = SDL_inotify_init1();335336if (inotify_fd < 0) {337SDL_LogWarn(SDL_LOG_CATEGORY_INPUT,338"Unable to initialize inotify, falling back to polling: %s",339strerror(errno));340return;341}342343/* We need to watch for attribute changes in addition to344* creation, because when a device is first created, it has345* permissions that we can't read. When udev chmods it to346* something that we maybe *can* read, we'll get an347* IN_ATTRIB event to tell us. */348if (inotify_add_watch(inotify_fd, "/dev",349IN_CREATE | IN_DELETE | IN_MOVE | IN_ATTRIB) < 0) {350close(inotify_fd);351inotify_fd = -1;352SDL_LogWarn(SDL_LOG_CATEGORY_INPUT,353"Unable to add inotify watch, falling back to polling: %s",354strerror(errno));355return;356}357358SDL_HIDAPI_discovery.m_bCanGetNotifications = true;359#endif // HAVE_INOTIFY360}361}362363static void HIDAPI_UpdateDiscovery(void)364{365if (!SDL_HIDAPI_discovery.m_bInitialized) {366HIDAPI_InitializeDiscovery();367}368369if (!SDL_HIDAPI_discovery.m_bCanGetNotifications) {370const Uint32 SDL_HIDAPI_DETECT_INTERVAL_MS = 3000; // Update every 3 seconds371Uint64 now = SDL_GetTicks();372if (!SDL_HIDAPI_discovery.m_unLastDetect || now >= (SDL_HIDAPI_discovery.m_unLastDetect + SDL_HIDAPI_DETECT_INTERVAL_MS)) {373++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;374SDL_HIDAPI_discovery.m_unLastDetect = now;375}376return;377}378379#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)380#if 0 // just let the usual SDL_PumpEvents loop dispatch these, fixing bug 4286. --ryan.381// We'll only get messages on the same thread that created the window382if (SDL_GetCurrentThreadID() == SDL_HIDAPI_discovery.m_nThreadID) {383MSG msg;384while (PeekMessage(&msg, SDL_HIDAPI_discovery.m_hwndMsg, 0, 0, PM_NOREMOVE)) {385if (GetMessageA(&msg, SDL_HIDAPI_discovery.m_hwndMsg, 0, 0) != 0) {386TranslateMessage(&msg);387DispatchMessage(&msg);388}389}390}391#endif392#endif // defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)393394#ifdef SDL_PLATFORM_MACOS395if (SDL_HIDAPI_discovery.m_notificationPort) {396struct397{398mach_msg_header_t hdr;399char payload[4096];400} msg;401while (mach_msg(&msg.hdr, MACH_RCV_MSG | MACH_RCV_TIMEOUT, 0, sizeof(msg), SDL_HIDAPI_discovery.m_notificationMach, 0, MACH_PORT_NULL) == KERN_SUCCESS) {402IODispatchCalloutFromMessage(NULL, &msg.hdr, SDL_HIDAPI_discovery.m_notificationPort);403}404}405#endif406407#ifdef SDL_USE_LIBUDEV408if (linux_enumeration_method == ENUMERATION_LIBUDEV) {409if (SDL_HIDAPI_discovery.m_nUdevFd >= 0) {410/* Drain all notification events.411* We don't expect a lot of device notifications so just412* do a new discovery on any kind or number of notifications.413* This could be made more restrictive if necessary.414*/415for (;;) {416struct pollfd PollUdev;417struct udev_device *pUdevDevice;418419PollUdev.fd = SDL_HIDAPI_discovery.m_nUdevFd;420PollUdev.events = POLLIN;421if (poll(&PollUdev, 1, 0) != 1) {422break;423}424425pUdevDevice = usyms->udev_monitor_receive_device(SDL_HIDAPI_discovery.m_pUdevMonitor);426if (pUdevDevice) {427const char *action = NULL;428action = usyms->udev_device_get_action(pUdevDevice);429if (action == NULL || SDL_strcmp(action, "add") == 0 || SDL_strcmp(action, "remove") == 0) {430++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;431}432usyms->udev_device_unref(pUdevDevice);433}434}435}436} else437#endif // SDL_USE_LIBUDEV438{439#ifdef HAVE_INOTIFY440if (inotify_fd >= 0) {441union442{443struct inotify_event event;444char storage[4096];445char enough_for_inotify[sizeof(struct inotify_event) + NAME_MAX + 1];446} buf;447ssize_t bytes;448size_t remain = 0;449size_t len;450451bytes = read(inotify_fd, &buf, sizeof(buf));452453if (bytes > 0) {454remain = (size_t)bytes;455}456457while (remain > 0) {458if (buf.event.len > 0) {459if (StrHasPrefix(buf.event.name, "hidraw") &&460StrIsInteger(buf.event.name + SDL_strlen("hidraw"))) {461++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;462/* We found an hidraw change. We still continue to463* drain the inotify fd to avoid leaving old464* notifications in the queue. */465}466}467468len = sizeof(struct inotify_event) + buf.event.len;469remain -= len;470471if (remain != 0) {472SDL_memmove(&buf.storage[0], &buf.storage[len], remain);473}474}475}476#endif // HAVE_INOTIFY477}478}479480static void HIDAPI_ShutdownDiscovery(void)481{482if (!SDL_HIDAPI_discovery.m_bInitialized) {483return;484}485486#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)487if (SDL_HIDAPI_discovery.m_hNotify) {488UnregisterDeviceNotification(SDL_HIDAPI_discovery.m_hNotify);489}490491if (SDL_HIDAPI_discovery.m_hwndMsg) {492DestroyWindow(SDL_HIDAPI_discovery.m_hwndMsg);493}494495UnregisterClassA(SDL_HIDAPI_discovery.m_wndClass.lpszClassName, SDL_HIDAPI_discovery.m_wndClass.hInstance);496#endif497498#ifdef SDL_PLATFORM_MACOS499if (SDL_HIDAPI_discovery.m_notificationPort) {500IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);501}502#endif503504#ifdef SDL_USE_LIBUDEV505if (linux_enumeration_method == ENUMERATION_LIBUDEV) {506if (usyms) {507if (SDL_HIDAPI_discovery.m_pUdevMonitor) {508usyms->udev_monitor_unref(SDL_HIDAPI_discovery.m_pUdevMonitor);509}510if (SDL_HIDAPI_discovery.m_pUdev) {511usyms->udev_unref(SDL_HIDAPI_discovery.m_pUdev);512}513SDL_UDEV_ReleaseUdevSyms();514usyms = NULL;515}516} else517#endif // SDL_USE_LIBUDEV518{519#ifdef HAVE_INOTIFY520if (inotify_fd >= 0) {521close(inotify_fd);522inotify_fd = -1;523}524#endif525}526527SDL_HIDAPI_discovery.m_bInitialized = false;528}529530// Platform HIDAPI Implementation531532#define HIDAPI_USING_SDL_RUNTIME533#define HIDAPI_IGNORE_DEVICE(BUS, VID, PID, USAGE_PAGE, USAGE) \534SDL_HIDAPI_ShouldIgnoreDevice(BUS, VID, PID, USAGE_PAGE, USAGE)535536struct PLATFORM_hid_device_;537typedef struct PLATFORM_hid_device_ PLATFORM_hid_device;538539#define api_version PLATFORM_api_version540#define create_device_info_for_device PLATFORM_create_device_info_for_device541#define free_hid_device PLATFORM_free_hid_device542#define hid_close PLATFORM_hid_close543#define hid_device PLATFORM_hid_device544#define hid_device_ PLATFORM_hid_device_545#define hid_enumerate PLATFORM_hid_enumerate546#define hid_error PLATFORM_hid_error547#define hid_exit PLATFORM_hid_exit548#define hid_free_enumeration PLATFORM_hid_free_enumeration549#define hid_get_device_info PLATFORM_hid_get_device_info550#define hid_get_feature_report PLATFORM_hid_get_feature_report551#define hid_get_indexed_string PLATFORM_hid_get_indexed_string552#define hid_get_input_report PLATFORM_hid_get_input_report553#define hid_get_manufacturer_string PLATFORM_hid_get_manufacturer_string554#define hid_get_product_string PLATFORM_hid_get_product_string555#define hid_get_report_descriptor PLATFORM_hid_get_report_descriptor556#define hid_get_serial_number_string PLATFORM_hid_get_serial_number_string557#define hid_init PLATFORM_hid_init558#define hid_open_path PLATFORM_hid_open_path559#define hid_open PLATFORM_hid_open560#define hid_read PLATFORM_hid_read561#define hid_read_timeout PLATFORM_hid_read_timeout562#define hid_send_feature_report PLATFORM_hid_send_feature_report563#define hid_set_nonblocking PLATFORM_hid_set_nonblocking564#define hid_version PLATFORM_hid_version565#define hid_version_str PLATFORM_hid_version_str566#define hid_write PLATFORM_hid_write567#define input_report PLATFORM_input_report568#define make_path PLATFORM_make_path569#define new_hid_device PLATFORM_new_hid_device570#define read_thread PLATFORM_read_thread571#define return_data PLATFORM_return_data572573#ifdef SDL_PLATFORM_LINUX574#include "SDL_hidapi_linux.h"575#elif defined(SDL_PLATFORM_NETBSD)576#include "SDL_hidapi_netbsd.h"577#elif defined(SDL_PLATFORM_MACOS)578#include "SDL_hidapi_mac.h"579#elif defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)580#include "SDL_hidapi_windows.h"581#elif defined(SDL_PLATFORM_ANDROID)582#include "SDL_hidapi_android.h"583#elif defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS)584#include "SDL_hidapi_ios.h"585#endif586587#undef api_version588#undef create_device_info_for_device589#undef free_hid_device590#undef hid_close591#undef hid_device592#undef hid_device_593#undef hid_enumerate594#undef hid_error595#undef hid_exit596#undef hid_free_enumeration597#undef hid_get_device_info598#undef hid_get_feature_report599#undef hid_get_indexed_string600#undef hid_get_input_report601#undef hid_get_manufacturer_string602#undef hid_get_product_string603#undef hid_get_report_descriptor604#undef hid_get_serial_number_string605#undef hid_init606#undef hid_open607#undef hid_open_path608#undef hid_read609#undef hid_read_timeout610#undef hid_send_feature_report611#undef hid_set_nonblocking612#undef hid_version613#undef hid_version_str614#undef hid_write615#undef input_report616#undef make_path617#undef new_hid_device618#undef read_thread619#undef return_data620621#ifdef SDL_JOYSTICK_HIDAPI_STEAMXBOX622#define HAVE_DRIVER_BACKEND 1623#endif624625#ifdef HAVE_DRIVER_BACKEND626627// DRIVER HIDAPI Implementation628629struct DRIVER_hid_device_;630typedef struct DRIVER_hid_device_ DRIVER_hid_device;631632#define hid_close DRIVER_hid_close633#define hid_device DRIVER_hid_device634#define hid_device_ DRIVER_hid_device_635#define hid_enumerate DRIVER_hid_enumerate636#define hid_error DRIVER_hid_error637#define hid_exit DRIVER_hid_exit638#define hid_free_enumeration DRIVER_hid_free_enumeration639#define hid_get_device_info DRIVER_hid_get_device_info640#define hid_get_feature_report DRIVER_hid_get_feature_report641#define hid_get_indexed_string DRIVER_hid_get_indexed_string642#define hid_get_input_report DRIVER_hid_get_input_report643#define hid_get_manufacturer_string DRIVER_hid_get_manufacturer_string644#define hid_get_product_string DRIVER_hid_get_product_string645#define hid_get_report_descriptor DRIVER_hid_get_report_descriptor646#define hid_get_serial_number_string DRIVER_hid_get_serial_number_string647#define hid_init DRIVER_hid_init648#define hid_open DRIVER_hid_open649#define hid_open_path DRIVER_hid_open_path650#define hid_read DRIVER_hid_read651#define hid_read_timeout DRIVER_hid_read_timeout652#define hid_send_feature_report DRIVER_hid_send_feature_report653#define hid_set_nonblocking DRIVER_hid_set_nonblocking654#define hid_write DRIVER_hid_write655656#ifdef SDL_JOYSTICK_HIDAPI_STEAMXBOX657#include "SDL_hidapi_steamxbox.h"658#else659#error Need a driver hid.c for this platform!660#endif661662#undef hid_close663#undef hid_device664#undef hid_device_665#undef hid_enumerate666#undef hid_error667#undef hid_exit668#undef hid_free_enumeration669#undef hid_get_device_info670#undef hid_get_feature_report671#undef hid_get_indexed_string672#undef hid_get_input_report673#undef hid_get_manufacturer_string674#undef hid_get_product_string675#undef hid_get_report_descriptor676#undef hid_get_serial_number_string677#undef hid_init678#undef hid_open679#undef hid_open_path680#undef hid_read681#undef hid_read_timeout682#undef hid_send_feature_report683#undef hid_set_nonblocking684#undef hid_write685686#endif // HAVE_DRIVER_BACKEND687688#ifdef HAVE_LIBUSB689// libusb HIDAPI Implementation690691// Include this now, for our dynamically-loaded libusb context692#include <libusb.h>693694static struct695{696SDL_SharedObject *libhandle;697698/* *INDENT-OFF* */ // clang-format off699int (LIBUSB_CALL *init)(libusb_context **ctx);700void (LIBUSB_CALL *exit)(libusb_context *ctx);701ssize_t (LIBUSB_CALL *get_device_list)(libusb_context *ctx, libusb_device ***list);702void (LIBUSB_CALL *free_device_list)(libusb_device **list, int unref_devices);703int (LIBUSB_CALL *get_device_descriptor)(libusb_device *dev, struct libusb_device_descriptor *desc);704int (LIBUSB_CALL *get_active_config_descriptor)(libusb_device *dev, struct libusb_config_descriptor **config);705int (LIBUSB_CALL *get_config_descriptor)(706libusb_device *dev,707uint8_t config_index,708struct libusb_config_descriptor **config709);710void (LIBUSB_CALL *free_config_descriptor)(struct libusb_config_descriptor *config);711uint8_t (LIBUSB_CALL *get_bus_number)(libusb_device *dev);712int (LIBUSB_CALL *get_port_numbers)(libusb_device *dev, uint8_t *port_numbers, int port_numbers_len);713uint8_t (LIBUSB_CALL *get_device_address)(libusb_device *dev);714int (LIBUSB_CALL *open)(libusb_device *dev, libusb_device_handle **dev_handle);715void (LIBUSB_CALL *close)(libusb_device_handle *dev_handle);716libusb_device *(LIBUSB_CALL *get_device)(libusb_device_handle *dev_handle);717int (LIBUSB_CALL *claim_interface)(libusb_device_handle *dev_handle, int interface_number);718int (LIBUSB_CALL *release_interface)(libusb_device_handle *dev_handle, int interface_number);719int (LIBUSB_CALL *kernel_driver_active)(libusb_device_handle *dev_handle, int interface_number);720int (LIBUSB_CALL *detach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number);721int (LIBUSB_CALL *attach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number);722int (LIBUSB_CALL *set_interface_alt_setting)(libusb_device_handle *dev, int interface_number, int alternate_setting);723struct libusb_transfer * (LIBUSB_CALL *alloc_transfer)(int iso_packets);724int (LIBUSB_CALL *submit_transfer)(struct libusb_transfer *transfer);725int (LIBUSB_CALL *cancel_transfer)(struct libusb_transfer *transfer);726void (LIBUSB_CALL *free_transfer)(struct libusb_transfer *transfer);727int (LIBUSB_CALL *control_transfer)(728libusb_device_handle *dev_handle,729uint8_t request_type,730uint8_t bRequest,731uint16_t wValue,732uint16_t wIndex,733unsigned char *data,734uint16_t wLength,735unsigned int timeout736);737int (LIBUSB_CALL *interrupt_transfer)(738libusb_device_handle *dev_handle,739unsigned char endpoint,740unsigned char *data,741int length,742int *actual_length,743unsigned int timeout744);745int (LIBUSB_CALL *handle_events)(libusb_context *ctx);746int (LIBUSB_CALL *handle_events_completed)(libusb_context *ctx, int *completed);747const char * (LIBUSB_CALL *error_name)(int errcode);748/* *INDENT-ON* */ // clang-format on749750} libusb_ctx;751752#define libusb_init libusb_ctx.init753#define libusb_exit libusb_ctx.exit754#define libusb_get_device_list libusb_ctx.get_device_list755#define libusb_free_device_list libusb_ctx.free_device_list756#define libusb_get_device_descriptor libusb_ctx.get_device_descriptor757#define libusb_get_active_config_descriptor libusb_ctx.get_active_config_descriptor758#define libusb_get_config_descriptor libusb_ctx.get_config_descriptor759#define libusb_free_config_descriptor libusb_ctx.free_config_descriptor760#define libusb_get_bus_number libusb_ctx.get_bus_number761#define libusb_get_port_numbers libusb_ctx.get_port_numbers762#define libusb_get_device_address libusb_ctx.get_device_address763#define libusb_open libusb_ctx.open764#define libusb_close libusb_ctx.close765#define libusb_get_device libusb_ctx.get_device766#define libusb_claim_interface libusb_ctx.claim_interface767#define libusb_release_interface libusb_ctx.release_interface768#define libusb_kernel_driver_active libusb_ctx.kernel_driver_active769#define libusb_detach_kernel_driver libusb_ctx.detach_kernel_driver770#define libusb_attach_kernel_driver libusb_ctx.attach_kernel_driver771#define libusb_set_interface_alt_setting libusb_ctx.set_interface_alt_setting772#define libusb_alloc_transfer libusb_ctx.alloc_transfer773#define libusb_submit_transfer libusb_ctx.submit_transfer774#define libusb_cancel_transfer libusb_ctx.cancel_transfer775#define libusb_free_transfer libusb_ctx.free_transfer776#define libusb_control_transfer libusb_ctx.control_transfer777#define libusb_interrupt_transfer libusb_ctx.interrupt_transfer778#define libusb_handle_events libusb_ctx.handle_events779#define libusb_handle_events_completed libusb_ctx.handle_events_completed780#define libusb_error_name libusb_ctx.error_name781782struct LIBUSB_hid_device_;783typedef struct LIBUSB_hid_device_ LIBUSB_hid_device;784785#define free_hid_device LIBUSB_free_hid_device786#define hid_close LIBUSB_hid_close787#define hid_device LIBUSB_hid_device788#define hid_device_ LIBUSB_hid_device_789#define hid_enumerate LIBUSB_hid_enumerate790#define hid_error LIBUSB_hid_error791#define hid_exit LIBUSB_hid_exit792#define hid_free_enumeration LIBUSB_hid_free_enumeration793#define hid_get_device_info LIBUSB_hid_get_device_info794#define hid_get_feature_report LIBUSB_hid_get_feature_report795#define hid_get_indexed_string LIBUSB_hid_get_indexed_string796#define hid_get_input_report LIBUSB_hid_get_input_report797#define hid_get_manufacturer_string LIBUSB_hid_get_manufacturer_string798#define hid_get_product_string LIBUSB_hid_get_product_string799#define hid_get_report_descriptor LIBUSB_hid_get_report_descriptor800#define hid_get_serial_number_string LIBUSB_hid_get_serial_number_string801#define hid_init LIBUSB_hid_init802#define hid_open LIBUSB_hid_open803#define hid_open_path LIBUSB_hid_open_path804#define hid_read LIBUSB_hid_read805#define hid_read_timeout LIBUSB_hid_read_timeout806#define hid_send_feature_report LIBUSB_hid_send_feature_report807#define hid_set_nonblocking LIBUSB_hid_set_nonblocking808#define hid_write LIBUSB_hid_write809#define hid_version LIBUSB_hid_version810#define hid_version_str LIBUSB_hid_version_str811#define input_report LIBUSB_input_report812#define make_path LIBUSB_make_path813#define new_hid_device LIBUSB_new_hid_device814#define read_thread LIBUSB_read_thread815#define return_data LIBUSB_return_data816817#include "SDL_hidapi_libusb.h"818819#undef libusb_init820#undef libusb_exit821#undef libusb_get_device_list822#undef libusb_free_device_list823#undef libusb_get_device_descriptor824#undef libusb_get_active_config_descriptor825#undef libusb_get_config_descriptor826#undef libusb_free_config_descriptor827#undef libusb_get_bus_number828#undef libusb_get_port_numbers829#undef libusb_get_device_address830#undef libusb_open831#undef libusb_close832#undef libusb_get_device833#undef libusb_claim_interface834#undef libusb_release_interface835#undef libusb_kernel_driver_active836#undef libusb_detach_kernel_driver837#undef libusb_attach_kernel_driver838#undef libusb_set_interface_alt_setting839#undef libusb_alloc_transfer840#undef libusb_submit_transfer841#undef libusb_cancel_transfer842#undef libusb_free_transfer843#undef libusb_control_transfer844#undef libusb_interrupt_transfer845#undef libusb_handle_events846#undef libusb_handle_events_completed847#undef libusb_error_name848849#undef free_hid_device850#undef hid_close851#undef hid_device852#undef hid_device_853#undef hid_enumerate854#undef hid_error855#undef hid_exit856#undef hid_free_enumeration857#undef hid_get_device_info858#undef hid_get_feature_report859#undef hid_get_indexed_string860#undef hid_get_input_report861#undef hid_get_manufacturer_string862#undef hid_get_product_string863#undef hid_get_report_descriptor864#undef hid_get_serial_number_string865#undef hid_init866#undef hid_open867#undef hid_open_path868#undef hid_read869#undef hid_read_timeout870#undef hid_send_feature_report871#undef hid_set_nonblocking872#undef hid_write873#undef input_report874#undef make_path875#undef new_hid_device876#undef read_thread877#undef return_data878879/* If the platform has any backend other than libusb, try to avoid using880* libusb as the main backend for devices, since it detaches drivers and881* therefore makes devices inaccessible to the rest of the OS.882*883* We do this by whitelisting devices we know to be accessible _exclusively_884* via libusb; these are typically devices that look like HIDs but have a885* quirk that requires direct access to the hardware.886*/887static const struct {888Uint16 vendor;889Uint16 product;890} SDL_libusb_whitelist[] = {891{ 0x057e, 0x0337 } // Nintendo WUP-028, Wii U/Switch GameCube Adapter892};893894static bool IsInWhitelist(Uint16 vendor, Uint16 product)895{896int i;897for (i = 0; i < SDL_arraysize(SDL_libusb_whitelist); i += 1) {898if (vendor == SDL_libusb_whitelist[i].vendor &&899product == SDL_libusb_whitelist[i].product) {900return true;901}902}903return false;904}905906#endif // HAVE_LIBUSB907908#endif // !SDL_HIDAPI_DISABLED909910#if defined(HAVE_PLATFORM_BACKEND) || defined(HAVE_DRIVER_BACKEND)911// We have another way to get HID devices, so use the whitelist to get devices where libusb is preferred912#define SDL_HINT_HIDAPI_LIBUSB_WHITELIST_DEFAULT true913#else914// libusb is the only way to get HID devices, so don't use the whitelist, get them all915#define SDL_HINT_HIDAPI_LIBUSB_WHITELIST_DEFAULT false916#endif // HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND917918static bool use_libusb_whitelist = SDL_HINT_HIDAPI_LIBUSB_WHITELIST_DEFAULT;919920// Shared HIDAPI Implementation921922struct hidapi_backend923{924int (*hid_write)(void *device, const unsigned char *data, size_t length);925int (*hid_read_timeout)(void *device, unsigned char *data, size_t length, int milliseconds);926int (*hid_read)(void *device, unsigned char *data, size_t length);927int (*hid_set_nonblocking)(void *device, int nonblock);928int (*hid_send_feature_report)(void *device, const unsigned char *data, size_t length);929int (*hid_get_feature_report)(void *device, unsigned char *data, size_t length);930int (*hid_get_input_report)(void *device, unsigned char *data, size_t length);931void (*hid_close)(void *device);932int (*hid_get_manufacturer_string)(void *device, wchar_t *string, size_t maxlen);933int (*hid_get_product_string)(void *device, wchar_t *string, size_t maxlen);934int (*hid_get_serial_number_string)(void *device, wchar_t *string, size_t maxlen);935int (*hid_get_indexed_string)(void *device, int string_index, wchar_t *string, size_t maxlen);936struct hid_device_info *(*hid_get_device_info)(void *device);937int (*hid_get_report_descriptor)(void *device, unsigned char *buf, size_t buf_size);938const wchar_t *(*hid_error)(void *device);939};940941#ifdef HAVE_PLATFORM_BACKEND942static const struct hidapi_backend PLATFORM_Backend = {943(void *)PLATFORM_hid_write,944(void *)PLATFORM_hid_read_timeout,945(void *)PLATFORM_hid_read,946(void *)PLATFORM_hid_set_nonblocking,947(void *)PLATFORM_hid_send_feature_report,948(void *)PLATFORM_hid_get_feature_report,949(void *)PLATFORM_hid_get_input_report,950(void *)PLATFORM_hid_close,951(void *)PLATFORM_hid_get_manufacturer_string,952(void *)PLATFORM_hid_get_product_string,953(void *)PLATFORM_hid_get_serial_number_string,954(void *)PLATFORM_hid_get_indexed_string,955(void *)PLATFORM_hid_get_device_info,956(void *)PLATFORM_hid_get_report_descriptor,957(void *)PLATFORM_hid_error958};959#endif // HAVE_PLATFORM_BACKEND960961#ifdef HAVE_DRIVER_BACKEND962static const struct hidapi_backend DRIVER_Backend = {963(void *)DRIVER_hid_write,964(void *)DRIVER_hid_read_timeout,965(void *)DRIVER_hid_read,966(void *)DRIVER_hid_set_nonblocking,967(void *)DRIVER_hid_send_feature_report,968(void *)DRIVER_hid_get_feature_report,969(void *)DRIVER_hid_get_input_report,970(void *)DRIVER_hid_close,971(void *)DRIVER_hid_get_manufacturer_string,972(void *)DRIVER_hid_get_product_string,973(void *)DRIVER_hid_get_serial_number_string,974(void *)DRIVER_hid_get_indexed_string,975(void *)DRIVER_hid_get_device_info,976(void *)DRIVER_hid_get_report_descriptor,977(void *)DRIVER_hid_error978};979#endif // HAVE_DRIVER_BACKEND980981#ifdef HAVE_LIBUSB982static const struct hidapi_backend LIBUSB_Backend = {983(void *)LIBUSB_hid_write,984(void *)LIBUSB_hid_read_timeout,985(void *)LIBUSB_hid_read,986(void *)LIBUSB_hid_set_nonblocking,987(void *)LIBUSB_hid_send_feature_report,988(void *)LIBUSB_hid_get_feature_report,989(void *)LIBUSB_hid_get_input_report,990(void *)LIBUSB_hid_close,991(void *)LIBUSB_hid_get_manufacturer_string,992(void *)LIBUSB_hid_get_product_string,993(void *)LIBUSB_hid_get_serial_number_string,994(void *)LIBUSB_hid_get_indexed_string,995(void *)LIBUSB_hid_get_device_info,996(void *)LIBUSB_hid_get_report_descriptor,997(void *)LIBUSB_hid_error998};999#endif // HAVE_LIBUSB10001001struct SDL_hid_device1002{1003void *device;1004const struct hidapi_backend *backend;1005SDL_hid_device_info info;1006};10071008#if defined(HAVE_PLATFORM_BACKEND) || defined(HAVE_DRIVER_BACKEND) || defined(HAVE_LIBUSB)10091010static SDL_hid_device *CreateHIDDeviceWrapper(void *device, const struct hidapi_backend *backend)1011{1012SDL_hid_device *wrapper = (SDL_hid_device *)SDL_malloc(sizeof(*wrapper));1013SDL_SetObjectValid(wrapper, SDL_OBJECT_TYPE_HIDAPI_DEVICE, true);1014wrapper->device = device;1015wrapper->backend = backend;1016SDL_zero(wrapper->info);1017return wrapper;1018}10191020#endif // HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB10211022static void DeleteHIDDeviceWrapper(SDL_hid_device *wrapper)1023{1024SDL_SetObjectValid(wrapper, SDL_OBJECT_TYPE_HIDAPI_DEVICE, false);1025SDL_free(wrapper->info.path);1026SDL_free(wrapper->info.serial_number);1027SDL_free(wrapper->info.manufacturer_string);1028SDL_free(wrapper->info.product_string);1029SDL_free(wrapper);1030}10311032#define CHECK_DEVICE_MAGIC(device, result) \1033if (!SDL_ObjectValid(device, SDL_OBJECT_TYPE_HIDAPI_DEVICE)) { \1034SDL_SetError("Invalid device"); \1035return result; \1036}10371038#define COPY_IF_EXISTS(var) \1039if (pSrc->var != NULL) { \1040pDst->var = SDL_strdup(pSrc->var); \1041} else { \1042pDst->var = NULL; \1043}1044#define WCOPY_IF_EXISTS(var) \1045if (pSrc->var != NULL) { \1046pDst->var = SDL_wcsdup(pSrc->var); \1047} else { \1048pDst->var = NULL; \1049}10501051static void CopyHIDDeviceInfo(struct hid_device_info *pSrc, struct SDL_hid_device_info *pDst)1052{1053COPY_IF_EXISTS(path)1054pDst->vendor_id = pSrc->vendor_id;1055pDst->product_id = pSrc->product_id;1056WCOPY_IF_EXISTS(serial_number)1057pDst->release_number = pSrc->release_number;1058WCOPY_IF_EXISTS(manufacturer_string)1059WCOPY_IF_EXISTS(product_string)1060pDst->usage_page = pSrc->usage_page;1061pDst->usage = pSrc->usage;1062pDst->interface_number = pSrc->interface_number;1063pDst->interface_class = pSrc->interface_class;1064pDst->interface_subclass = pSrc->interface_subclass;1065pDst->interface_protocol = pSrc->interface_protocol;1066pDst->bus_type = (SDL_hid_bus_type)pSrc->bus_type;1067pDst->next = NULL;1068}10691070#undef COPY_IF_EXISTS1071#undef WCOPY_IF_EXISTS10721073static int SDL_hidapi_refcount = 0;1074static bool SDL_hidapi_only_controllers;1075static char *SDL_hidapi_ignored_devices = NULL;10761077static void SDLCALL OnlyControllersChanged(void *userdata, const char *name, const char *oldValue, const char *hint)1078{1079SDL_hidapi_only_controllers = SDL_GetStringBoolean(hint, true);1080}10811082static void SDLCALL IgnoredDevicesChanged(void *userdata, const char *name, const char *oldValue, const char *hint)1083{1084if (SDL_hidapi_ignored_devices) {1085SDL_free(SDL_hidapi_ignored_devices);1086}1087if (hint && *hint) {1088SDL_hidapi_ignored_devices = SDL_strdup(hint);1089} else {1090SDL_hidapi_ignored_devices = NULL;1091}1092}10931094bool SDL_HIDAPI_ShouldIgnoreDevice(int bus, Uint16 vendor_id, Uint16 product_id, Uint16 usage_page, Uint16 usage)1095{1096// See if there are any devices we should skip in enumeration1097if (SDL_hidapi_only_controllers && usage_page) {1098if (vendor_id == USB_VENDOR_VALVE) {1099// Ignore the mouse/keyboard interface on Steam Controllers1100if (1101#ifdef SDL_PLATFORM_WIN321102// Check the usage page and usage on both USB and Bluetooth1103#else1104// Only check the usage page and usage on USB1105bus == HID_API_BUS_USB &&1106#endif1107usage_page == USB_USAGEPAGE_GENERIC_DESKTOP &&1108(usage == USB_USAGE_GENERIC_KEYBOARD || usage == USB_USAGE_GENERIC_MOUSE)) {1109return true;1110}1111} else if (usage_page == USB_USAGEPAGE_GENERIC_DESKTOP &&1112(usage == USB_USAGE_GENERIC_JOYSTICK || usage == USB_USAGE_GENERIC_GAMEPAD || usage == USB_USAGE_GENERIC_MULTIAXISCONTROLLER)) {1113// This is a controller1114} else {1115return true;1116}1117}1118if (SDL_hidapi_ignored_devices) {1119char vendor_match[16], product_match[16];1120SDL_snprintf(vendor_match, sizeof(vendor_match), "0x%.4x/0x0000", vendor_id);1121SDL_snprintf(product_match, sizeof(product_match), "0x%.4x/0x%.4x", vendor_id, product_id);1122if (SDL_strcasestr(SDL_hidapi_ignored_devices, vendor_match) ||1123SDL_strcasestr(SDL_hidapi_ignored_devices, product_match)) {1124return true;1125}1126}1127return false;1128}11291130int SDL_hid_init(void)1131{1132int attempts = 0, success = 0;11331134if (SDL_hidapi_refcount > 0) {1135++SDL_hidapi_refcount;1136return 0;1137}11381139SDL_AddHintCallback(SDL_HINT_HIDAPI_ENUMERATE_ONLY_CONTROLLERS, OnlyControllersChanged, NULL);1140SDL_AddHintCallback(SDL_HINT_HIDAPI_IGNORE_DEVICES, IgnoredDevicesChanged, NULL);11411142#ifdef SDL_USE_LIBUDEV1143if (!SDL_GetHintBoolean(SDL_HINT_HIDAPI_UDEV, true)) {1144SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,1145"udev disabled by SDL_HINT_HIDAPI_UDEV");1146linux_enumeration_method = ENUMERATION_FALLBACK;1147} else if (SDL_GetSandbox() != SDL_SANDBOX_NONE) {1148SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,1149"Container detected, disabling HIDAPI udev integration");1150linux_enumeration_method = ENUMERATION_FALLBACK;1151} else {1152SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,1153"Using udev for HIDAPI joystick device discovery");1154linux_enumeration_method = ENUMERATION_LIBUDEV;1155}1156#endif11571158use_libusb_whitelist = SDL_GetHintBoolean(SDL_HINT_HIDAPI_LIBUSB_WHITELIST,1159SDL_HINT_HIDAPI_LIBUSB_WHITELIST_DEFAULT);1160#ifdef HAVE_LIBUSB1161if (!SDL_GetHintBoolean(SDL_HINT_HIDAPI_LIBUSB, true)) {1162SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,1163"libusb disabled with SDL_HINT_HIDAPI_LIBUSB");1164libusb_ctx.libhandle = NULL;1165} else {1166++attempts;1167#ifdef SDL_LIBUSB_DYNAMIC1168libusb_ctx.libhandle = SDL_LoadObject(SDL_LIBUSB_DYNAMIC);1169#else1170libusb_ctx.libhandle = (void *)1;1171#endif1172if (libusb_ctx.libhandle != NULL) {1173bool loaded = true;1174#ifdef SDL_LIBUSB_DYNAMIC1175#define LOAD_LIBUSB_SYMBOL(type, func) \1176if (!(libusb_ctx.func = (type)SDL_LoadFunction(libusb_ctx.libhandle, "libusb_" #func))) { \1177loaded = false; \1178}1179#else1180#define LOAD_LIBUSB_SYMBOL(type, func) \1181libusb_ctx.func = libusb_##func;1182#endif1183LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context **), init)1184LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_context *), exit)1185LOAD_LIBUSB_SYMBOL(ssize_t (LIBUSB_CALL *)(libusb_context *, libusb_device ***), get_device_list)1186LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_device **, int), free_device_list)1187LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, struct libusb_device_descriptor *), get_device_descriptor)1188LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, struct libusb_config_descriptor **), get_active_config_descriptor)1189LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, uint8_t, struct libusb_config_descriptor **), get_config_descriptor)1190LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(struct libusb_config_descriptor *), free_config_descriptor)1191LOAD_LIBUSB_SYMBOL(uint8_t (LIBUSB_CALL *)(libusb_device *), get_bus_number)1192LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *dev, uint8_t *port_numbers, int port_numbers_len), get_port_numbers)1193LOAD_LIBUSB_SYMBOL(uint8_t (LIBUSB_CALL *)(libusb_device *), get_device_address)1194LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, libusb_device_handle **), open)1195LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_device_handle *), close)1196LOAD_LIBUSB_SYMBOL(libusb_device * (LIBUSB_CALL *)(libusb_device_handle *dev_handle), get_device)1197LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), claim_interface)1198LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), release_interface)1199LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), kernel_driver_active)1200LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), detach_kernel_driver)1201LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), attach_kernel_driver)1202LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int, int), set_interface_alt_setting)1203LOAD_LIBUSB_SYMBOL(struct libusb_transfer * (LIBUSB_CALL *)(int), alloc_transfer)1204LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(struct libusb_transfer *), submit_transfer)1205LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(struct libusb_transfer *), cancel_transfer)1206LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(struct libusb_transfer *), free_transfer)1207LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, uint8_t, uint8_t, uint16_t, uint16_t, unsigned char *, uint16_t, unsigned int), control_transfer)1208LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, unsigned char, unsigned char *, int, int *, unsigned int), interrupt_transfer)1209LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *), handle_events)1210LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *, int *), handle_events_completed)1211LOAD_LIBUSB_SYMBOL(const char * (LIBUSB_CALL *)(int), error_name)1212#undef LOAD_LIBUSB_SYMBOL12131214if (!loaded) {1215#ifdef SDL_LIBUSB_DYNAMIC1216SDL_UnloadObject(libusb_ctx.libhandle);1217#endif1218libusb_ctx.libhandle = NULL;1219// SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, SDL_LIBUSB_DYNAMIC " found but could not load function");1220} else if (LIBUSB_hid_init() < 0) {1221#ifdef SDL_LIBUSB_DYNAMIC1222SDL_UnloadObject(libusb_ctx.libhandle);1223#endif1224libusb_ctx.libhandle = NULL;1225} else {1226++success;1227}1228}1229}1230#endif // HAVE_LIBUSB12311232#ifdef HAVE_PLATFORM_BACKEND1233++attempts;1234#ifdef SDL_PLATFORM_LINUX1235udev_ctx = SDL_UDEV_GetUdevSyms();1236#endif // __LINUX __1237if (udev_ctx && PLATFORM_hid_init() == 0) {1238++success;1239}1240#endif // HAVE_PLATFORM_BACKEND12411242if (attempts > 0 && success == 0) {1243return -1;1244}12451246#if defined(SDL_PLATFORM_MACOS) && !defined(SDL_HIDAPI_DISABLED)1247hid_darwin_set_open_exclusive(0);1248#endif12491250++SDL_hidapi_refcount;1251return 0;1252}12531254int SDL_hid_exit(void)1255{1256int result = 0;12571258if (SDL_hidapi_refcount == 0) {1259return 0;1260}1261--SDL_hidapi_refcount;1262if (SDL_hidapi_refcount > 0) {1263return 0;1264}1265SDL_hidapi_refcount = 0;12661267#ifndef SDL_HIDAPI_DISABLED1268HIDAPI_ShutdownDiscovery();1269#endif12701271#ifdef HAVE_PLATFORM_BACKEND1272if (udev_ctx) {1273result |= PLATFORM_hid_exit();1274}1275#ifdef SDL_PLATFORM_LINUX1276SDL_UDEV_ReleaseUdevSyms();1277#endif // __LINUX __1278#endif // HAVE_PLATFORM_BACKEND12791280#ifdef HAVE_LIBUSB1281if (libusb_ctx.libhandle) {1282result |= LIBUSB_hid_exit();1283#ifdef SDL_LIBUSB_DYNAMIC1284SDL_UnloadObject(libusb_ctx.libhandle);1285#endif1286libusb_ctx.libhandle = NULL;1287}1288#endif // HAVE_LIBUSB12891290SDL_RemoveHintCallback(SDL_HINT_HIDAPI_ENUMERATE_ONLY_CONTROLLERS, OnlyControllersChanged, NULL);1291SDL_RemoveHintCallback(SDL_HINT_HIDAPI_IGNORE_DEVICES, IgnoredDevicesChanged, NULL);12921293if (SDL_hidapi_ignored_devices) {1294SDL_free(SDL_hidapi_ignored_devices);1295SDL_hidapi_ignored_devices = NULL;1296}12971298return result;1299}13001301Uint32 SDL_hid_device_change_count(void)1302{1303Uint32 counter = 0;13041305#ifndef SDL_HIDAPI_DISABLED1306if (SDL_hidapi_refcount == 0 && SDL_hid_init() < 0) {1307return 0;1308}13091310HIDAPI_UpdateDiscovery();13111312if (SDL_HIDAPI_discovery.m_unDeviceChangeCounter == 0) {1313// Counter wrapped!1314++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;1315}1316counter = SDL_HIDAPI_discovery.m_unDeviceChangeCounter;13171318#endif // !SDL_HIDAPI_DISABLED13191320return counter;1321}13221323static void AddDeviceToEnumeration(const char *driver_name, struct hid_device_info *dev, struct SDL_hid_device_info **devs, struct SDL_hid_device_info **last)1324{1325struct SDL_hid_device_info *new_dev;13261327#ifdef DEBUG_HIDAPI1328SDL_Log("Adding %s device to enumeration: %ls %ls 0x%.4hx/0x%.4hx/%d",1329driver_name, dev->manufacturer_string, dev->product_string, dev->vendor_id, dev->product_id, dev->interface_number);1330#else1331(void)driver_name;1332#endif13331334new_dev = (struct SDL_hid_device_info *)SDL_malloc(sizeof(struct SDL_hid_device_info));1335if (new_dev == NULL) {1336// Don't bother returning an error, get as many devices as possible1337return;1338}1339CopyHIDDeviceInfo(dev, new_dev);13401341if ((*last) != NULL) {1342(*last)->next = new_dev;1343} else {1344*devs = new_dev;1345}1346*last = new_dev;1347}13481349#if defined(HAVE_LIBUSB) || defined(HAVE_PLATFORM_BACKEND)1350static void RemoveDeviceFromEnumeration(const char *driver_name, struct hid_device_info *dev, struct hid_device_info **devs, void (*free_device_info)(struct hid_device_info *))1351{1352struct hid_device_info *last = NULL, *curr, *next;13531354for (curr = *devs; curr; curr = next) {1355next = curr->next;13561357if (dev->vendor_id == curr->vendor_id &&1358dev->product_id == curr->product_id &&1359(dev->interface_number < 0 || curr->interface_number < 0 || dev->interface_number == curr->interface_number)) {1360#ifdef DEBUG_HIDAPI1361SDL_Log("Skipping %s device: %ls %ls 0x%.4hx/0x%.4hx/%d",1362driver_name, curr->manufacturer_string, curr->product_string, curr->vendor_id, curr->product_id, curr->interface_number);1363#else1364(void)driver_name;1365#endif1366if (last) {1367last->next = next;1368} else {1369*devs = next;1370}13711372curr->next = NULL;1373free_device_info(curr);1374continue;1375}1376last = curr;1377}1378}1379#endif // HAVE_LIBUSB || HAVE_PLATFORM_BACKEND13801381#ifdef HAVE_LIBUSB1382static void RemoveNonWhitelistedDevicesFromEnumeration(struct hid_device_info **devs, void (*free_device_info)(struct hid_device_info *))1383{1384struct hid_device_info *last = NULL, *curr, *next;13851386for (curr = *devs; curr; curr = next) {1387next = curr->next;13881389if (!IsInWhitelist(curr->vendor_id, curr->product_id)) {1390#ifdef DEBUG_HIDAPI1391SDL_Log("Device was not in libusb whitelist, skipping: %ls %ls 0x%.4hx/0x%.4hx/%d",1392curr->manufacturer_string, curr->product_string, curr->vendor_id, curr->product_id, curr->interface_number);1393#endif1394if (last) {1395last->next = next;1396} else {1397*devs = next;1398}13991400curr->next = NULL;1401free_device_info(curr);1402continue;1403}1404last = curr;1405}1406}1407#endif // HAVE_LIBUSB14081409struct SDL_hid_device_info *SDL_hid_enumerate(unsigned short vendor_id, unsigned short product_id)1410{1411struct hid_device_info *driver_devs = NULL;1412struct hid_device_info *usb_devs = NULL;1413struct hid_device_info *raw_devs = NULL;1414struct hid_device_info *dev;1415struct SDL_hid_device_info *devs = NULL, *last = NULL;14161417if (SDL_hidapi_refcount == 0 && SDL_hid_init() < 0) {1418return NULL;1419}14201421// Collect the available devices1422#ifdef HAVE_DRIVER_BACKEND1423driver_devs = DRIVER_hid_enumerate(vendor_id, product_id);1424#endif14251426#ifdef HAVE_LIBUSB1427if (libusb_ctx.libhandle) {1428usb_devs = LIBUSB_hid_enumerate(vendor_id, product_id);14291430if (use_libusb_whitelist) {1431RemoveNonWhitelistedDevicesFromEnumeration(&usb_devs, LIBUSB_hid_free_enumeration);1432}1433}1434#endif // HAVE_LIBUSB14351436#ifdef HAVE_PLATFORM_BACKEND1437if (udev_ctx) {1438raw_devs = PLATFORM_hid_enumerate(vendor_id, product_id);1439}1440#endif14411442// Highest priority are custom driver devices1443for (dev = driver_devs; dev; dev = dev->next) {1444AddDeviceToEnumeration("driver", dev, &devs, &last);1445#ifdef HAVE_LIBUSB1446RemoveDeviceFromEnumeration("libusb", dev, &usb_devs, LIBUSB_hid_free_enumeration);1447#endif1448#ifdef HAVE_PLATFORM_BACKEND1449RemoveDeviceFromEnumeration("raw", dev, &raw_devs, PLATFORM_hid_free_enumeration);1450#endif1451}14521453// If whitelist is in effect, libusb has priority, otherwise raw devices do1454if (use_libusb_whitelist) {1455for (dev = usb_devs; dev; dev = dev->next) {1456AddDeviceToEnumeration("libusb", dev, &devs, &last);1457#ifdef HAVE_PLATFORM_BACKEND1458RemoveDeviceFromEnumeration("raw", dev, &raw_devs, PLATFORM_hid_free_enumeration);1459#endif1460}1461for (dev = raw_devs; dev; dev = dev->next) {1462AddDeviceToEnumeration("platform", dev, &devs, &last);1463}1464} else {1465for (dev = raw_devs; dev; dev = dev->next) {1466AddDeviceToEnumeration("raw", dev, &devs, &last);1467#ifdef HAVE_LIBUSB1468RemoveDeviceFromEnumeration("libusb", dev, &usb_devs, LIBUSB_hid_free_enumeration);1469#endif1470}1471for (dev = usb_devs; dev; dev = dev->next) {1472AddDeviceToEnumeration("libusb", dev, &devs, &last);1473}1474}14751476#ifdef HAVE_DRIVER_BACKEND1477DRIVER_hid_free_enumeration(driver_devs);1478#endif1479#ifdef HAVE_LIBUSB1480LIBUSB_hid_free_enumeration(usb_devs);1481#endif1482#ifdef HAVE_PLATFORM_BACKEND1483PLATFORM_hid_free_enumeration(raw_devs);1484#endif14851486return devs;1487}14881489void SDL_hid_free_enumeration(struct SDL_hid_device_info *devs)1490{1491while (devs) {1492struct SDL_hid_device_info *next = devs->next;1493SDL_free(devs->path);1494SDL_free(devs->serial_number);1495SDL_free(devs->manufacturer_string);1496SDL_free(devs->product_string);1497SDL_free(devs);1498devs = next;1499}1500}15011502SDL_hid_device *SDL_hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)1503{1504#if defined(HAVE_PLATFORM_BACKEND) || defined(HAVE_DRIVER_BACKEND) || defined(HAVE_LIBUSB)1505void *pDevice = NULL;15061507if (SDL_hidapi_refcount == 0 && SDL_hid_init() < 0) {1508return NULL;1509}15101511#ifdef HAVE_PLATFORM_BACKEND1512if (udev_ctx) {1513pDevice = PLATFORM_hid_open(vendor_id, product_id, serial_number);1514if (pDevice != NULL) {1515return CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);1516}1517}1518#endif // HAVE_PLATFORM_BACKEND15191520#ifdef HAVE_DRIVER_BACKEND1521pDevice = DRIVER_hid_open(vendor_id, product_id, serial_number);1522if (pDevice != NULL) {1523return CreateHIDDeviceWrapper(pDevice, &DRIVER_Backend);1524}1525#endif // HAVE_DRIVER_BACKEND15261527#ifdef HAVE_LIBUSB1528if (libusb_ctx.libhandle != NULL) {1529pDevice = LIBUSB_hid_open(vendor_id, product_id, serial_number);1530if (pDevice != NULL) {1531return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);1532}1533}1534#endif // HAVE_LIBUSB15351536#endif // HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB15371538return NULL;1539}15401541SDL_hid_device *SDL_hid_open_path(const char *path)1542{1543#if defined(HAVE_PLATFORM_BACKEND) || defined(HAVE_DRIVER_BACKEND) || defined(HAVE_LIBUSB)1544void *pDevice = NULL;15451546if (SDL_hidapi_refcount == 0 && SDL_hid_init() < 0) {1547return NULL;1548}15491550#ifdef HAVE_PLATFORM_BACKEND1551if (udev_ctx) {1552pDevice = PLATFORM_hid_open_path(path);1553if (pDevice != NULL) {1554return CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);1555}1556}1557#endif // HAVE_PLATFORM_BACKEND15581559#ifdef HAVE_DRIVER_BACKEND1560pDevice = DRIVER_hid_open_path(path);1561if (pDevice != NULL) {1562return CreateHIDDeviceWrapper(pDevice, &DRIVER_Backend);1563}1564#endif // HAVE_DRIVER_BACKEND15651566#ifdef HAVE_LIBUSB1567if (libusb_ctx.libhandle != NULL) {1568pDevice = LIBUSB_hid_open_path(path);1569if (pDevice != NULL) {1570return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);1571}1572}1573#endif // HAVE_LIBUSB15741575#endif // HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB15761577return NULL;1578}15791580int SDL_hid_write(SDL_hid_device *device, const unsigned char *data, size_t length)1581{1582CHECK_DEVICE_MAGIC(device, -1);15831584return device->backend->hid_write(device->device, data, length);1585}15861587int SDL_hid_read_timeout(SDL_hid_device *device, unsigned char *data, size_t length, int milliseconds)1588{1589CHECK_DEVICE_MAGIC(device, -1);15901591return device->backend->hid_read_timeout(device->device, data, length, milliseconds);1592}15931594int SDL_hid_read(SDL_hid_device *device, unsigned char *data, size_t length)1595{1596CHECK_DEVICE_MAGIC(device, -1);15971598return device->backend->hid_read(device->device, data, length);1599}16001601int SDL_hid_set_nonblocking(SDL_hid_device *device, int nonblock)1602{1603CHECK_DEVICE_MAGIC(device, -1);16041605return device->backend->hid_set_nonblocking(device->device, nonblock);1606}16071608int SDL_hid_send_feature_report(SDL_hid_device *device, const unsigned char *data, size_t length)1609{1610CHECK_DEVICE_MAGIC(device, -1);16111612return device->backend->hid_send_feature_report(device->device, data, length);1613}16141615int SDL_hid_get_feature_report(SDL_hid_device *device, unsigned char *data, size_t length)1616{1617CHECK_DEVICE_MAGIC(device, -1);16181619return device->backend->hid_get_feature_report(device->device, data, length);1620}16211622int SDL_hid_get_input_report(SDL_hid_device *device, unsigned char *data, size_t length)1623{1624CHECK_DEVICE_MAGIC(device, -1);16251626return device->backend->hid_get_input_report(device->device, data, length);1627}16281629int SDL_hid_close(SDL_hid_device *device)1630{1631CHECK_DEVICE_MAGIC(device, -1);16321633device->backend->hid_close(device->device);1634DeleteHIDDeviceWrapper(device);1635return 0;1636}16371638int SDL_hid_get_manufacturer_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)1639{1640CHECK_DEVICE_MAGIC(device, -1);16411642return device->backend->hid_get_manufacturer_string(device->device, string, maxlen);1643}16441645int SDL_hid_get_product_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)1646{1647CHECK_DEVICE_MAGIC(device, -1);16481649return device->backend->hid_get_product_string(device->device, string, maxlen);1650}16511652int SDL_hid_get_serial_number_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)1653{1654CHECK_DEVICE_MAGIC(device, -1);16551656return device->backend->hid_get_serial_number_string(device->device, string, maxlen);1657}16581659int SDL_hid_get_indexed_string(SDL_hid_device *device, int string_index, wchar_t *string, size_t maxlen)1660{1661CHECK_DEVICE_MAGIC(device, -1);16621663return device->backend->hid_get_indexed_string(device->device, string_index, string, maxlen);1664}16651666SDL_hid_device_info *SDL_hid_get_device_info(SDL_hid_device *device)1667{1668struct hid_device_info *info;16691670CHECK_DEVICE_MAGIC(device, NULL);16711672info = device->backend->hid_get_device_info(device->device);1673if (info) {1674CopyHIDDeviceInfo(info, &device->info);1675return &device->info;1676} else {1677return NULL;1678}1679}16801681int SDL_hid_get_report_descriptor(SDL_hid_device *device, unsigned char *buf, size_t buf_size)1682{1683CHECK_DEVICE_MAGIC(device, -1);16841685return device->backend->hid_get_report_descriptor(device->device, buf, buf_size);1686}16871688void SDL_hid_ble_scan(bool active)1689{1690#if !defined(SDL_HIDAPI_DISABLED) && (defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS))1691extern void hid_ble_scan(int bStart);1692hid_ble_scan(active);1693#endif1694}16951696#ifdef HAVE_ENABLE_GAMECUBE_ADAPTORS1697// This is needed to enable input for Nyko and EVORETRO GameCube adaptors1698void SDL_EnableGameCubeAdaptors(void)1699{1700#ifdef HAVE_LIBUSB1701libusb_context *context = NULL;1702libusb_device **devs = NULL;1703libusb_device_handle *handle = NULL;1704struct libusb_device_descriptor desc;1705ssize_t i, num_devs;1706int kernel_detached = 0;17071708if (libusb_ctx.libhandle == NULL) {1709return;1710}17111712if (libusb_ctx.init(&context) == 0) {1713num_devs = libusb_ctx.get_device_list(context, &devs);1714for (i = 0; i < num_devs; ++i) {1715if (libusb_ctx.get_device_descriptor(devs[i], &desc) != 0) {1716continue;1717}17181719if (desc.idVendor != 0x057e || desc.idProduct != 0x0337) {1720continue;1721}17221723if (libusb_ctx.open(devs[i], &handle) != 0) {1724continue;1725}17261727if (libusb_ctx.kernel_driver_active(handle, 0)) {1728if (libusb_ctx.detach_kernel_driver(handle, 0) == 0) {1729kernel_detached = 1;1730}1731}17321733if (libusb_ctx.claim_interface(handle, 0) == 0) {1734libusb_ctx.control_transfer(handle, 0x21, 11, 0x0001, 0, NULL, 0, 1000);1735libusb_ctx.release_interface(handle, 0);1736}17371738if (kernel_detached) {1739libusb_ctx.attach_kernel_driver(handle, 0);1740}17411742libusb_ctx.close(handle);1743}17441745libusb_ctx.free_device_list(devs, 1);17461747libusb_ctx.exit(context);1748}1749#endif // HAVE_LIBUSB1750}1751#endif // HAVE_ENABLE_GAMECUBE_ADAPTORS175217531754