Path: blob/master/thirdparty/sdl/include/SDL3/SDL_hidapi.h
9912 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/* WIKI CATEGORY: HIDAPI */2223/**24* # CategoryHIDAPI25*26* Header file for SDL HIDAPI functions.27*28* This is an adaptation of the original HIDAPI interface by Alan Ott, and29* includes source code licensed under the following license:30*31* ```32* HIDAPI - Multi-Platform library for33* communication with HID devices.34*35* Copyright 2009, Alan Ott, Signal 11 Software.36* All Rights Reserved.37*38* This software may be used by anyone for any reason so39* long as the copyright notice in the source files40* remains intact.41* ```42*43* (Note that this license is the same as item three of SDL's zlib license, so44* it adds no new requirements on the user.)45*46* If you would like a version of SDL without this code, you can build SDL47* with SDL_HIDAPI_DISABLED defined to 1. You might want to do this for48* example on iOS or tvOS to avoid a dependency on the CoreBluetooth49* framework.50*/5152#ifndef SDL_hidapi_h_53#define SDL_hidapi_h_5455#include <SDL3/SDL_stdinc.h>56#include <SDL3/SDL_error.h>5758#include <SDL3/SDL_begin_code.h>59/* Set up for C function definitions, even when using C++ */60#ifdef __cplusplus61extern "C" {62#endif6364/**65* An opaque handle representing an open HID device.66*67* \since This struct is available since SDL 3.2.0.68*/69typedef struct SDL_hid_device SDL_hid_device;7071/**72* HID underlying bus types.73*74* \since This enum is available since SDL 3.2.0.75*/76typedef enum SDL_hid_bus_type {77/** Unknown bus type */78SDL_HID_API_BUS_UNKNOWN = 0x00,7980/** USB bus81Specifications:82https://usb.org/hid */83SDL_HID_API_BUS_USB = 0x01,8485/** Bluetooth or Bluetooth LE bus86Specifications:87https://www.bluetooth.com/specifications/specs/human-interface-device-profile-1-1-1/88https://www.bluetooth.com/specifications/specs/hid-service-1-0/89https://www.bluetooth.com/specifications/specs/hid-over-gatt-profile-1-0/ */90SDL_HID_API_BUS_BLUETOOTH = 0x02,9192/** I2C bus93Specifications:94https://docs.microsoft.com/previous-versions/windows/hardware/design/dn642101(v=vs.85) */95SDL_HID_API_BUS_I2C = 0x03,9697/** SPI bus98Specifications:99https://www.microsoft.com/download/details.aspx?id=103325 */100SDL_HID_API_BUS_SPI = 0x04101102} SDL_hid_bus_type;103104/** hidapi info structure */105106/**107* Information about a connected HID device108*109* \since This struct is available since SDL 3.2.0.110*/111typedef struct SDL_hid_device_info112{113/** Platform-specific device path */114char *path;115/** Device Vendor ID */116unsigned short vendor_id;117/** Device Product ID */118unsigned short product_id;119/** Serial Number */120wchar_t *serial_number;121/** Device Release Number in binary-coded decimal,122also known as Device Version Number */123unsigned short release_number;124/** Manufacturer String */125wchar_t *manufacturer_string;126/** Product string */127wchar_t *product_string;128/** Usage Page for this Device/Interface129(Windows/Mac/hidraw only) */130unsigned short usage_page;131/** Usage for this Device/Interface132(Windows/Mac/hidraw only) */133unsigned short usage;134/** The USB interface which this logical device135represents.136137Valid only if the device is a USB HID device.138Set to -1 in all other cases.139*/140int interface_number;141142/** Additional information about the USB interface.143Valid on libusb and Android implementations. */144int interface_class;145int interface_subclass;146int interface_protocol;147148/** Underlying bus type */149SDL_hid_bus_type bus_type;150151/** Pointer to the next device */152struct SDL_hid_device_info *next;153154} SDL_hid_device_info;155156157/**158* Initialize the HIDAPI library.159*160* This function initializes the HIDAPI library. Calling it is not strictly161* necessary, as it will be called automatically by SDL_hid_enumerate() and162* any of the SDL_hid_open_*() functions if it is needed. This function should163* be called at the beginning of execution however, if there is a chance of164* HIDAPI handles being opened by different threads simultaneously.165*166* Each call to this function should have a matching call to SDL_hid_exit()167*168* \returns 0 on success or a negative error code on failure; call169* SDL_GetError() for more information.170*171* \since This function is available since SDL 3.2.0.172*173* \sa SDL_hid_exit174*/175extern SDL_DECLSPEC int SDLCALL SDL_hid_init(void);176177/**178* Finalize the HIDAPI library.179*180* This function frees all of the static data associated with HIDAPI. It181* should be called at the end of execution to avoid memory leaks.182*183* \returns 0 on success or a negative error code on failure; call184* SDL_GetError() for more information.185*186* \since This function is available since SDL 3.2.0.187*188* \sa SDL_hid_init189*/190extern SDL_DECLSPEC int SDLCALL SDL_hid_exit(void);191192/**193* Check to see if devices may have been added or removed.194*195* Enumerating the HID devices is an expensive operation, so you can call this196* to see if there have been any system device changes since the last call to197* this function. A change in the counter returned doesn't necessarily mean198* that anything has changed, but you can call SDL_hid_enumerate() to get an199* updated device list.200*201* Calling this function for the first time may cause a thread or other system202* resource to be allocated to track device change notifications.203*204* \returns a change counter that is incremented with each potential device205* change, or 0 if device change detection isn't available.206*207* \since This function is available since SDL 3.2.0.208*209* \sa SDL_hid_enumerate210*/211extern SDL_DECLSPEC Uint32 SDLCALL SDL_hid_device_change_count(void);212213/**214* Enumerate the HID Devices.215*216* This function returns a linked list of all the HID devices attached to the217* system which match vendor_id and product_id. If `vendor_id` is set to 0218* then any vendor matches. If `product_id` is set to 0 then any product219* matches. If `vendor_id` and `product_id` are both set to 0, then all HID220* devices will be returned.221*222* By default SDL will only enumerate controllers, to reduce risk of hanging223* or crashing on bad drivers, but SDL_HINT_HIDAPI_ENUMERATE_ONLY_CONTROLLERS224* can be set to "0" to enumerate all HID devices.225*226* \param vendor_id the Vendor ID (VID) of the types of device to open, or 0227* to match any vendor.228* \param product_id the Product ID (PID) of the types of device to open, or 0229* to match any product.230* \returns a pointer to a linked list of type SDL_hid_device_info, containing231* information about the HID devices attached to the system, or NULL232* in the case of failure. Free this linked list by calling233* SDL_hid_free_enumeration().234*235* \since This function is available since SDL 3.2.0.236*237* \sa SDL_hid_device_change_count238*/239extern SDL_DECLSPEC SDL_hid_device_info * SDLCALL SDL_hid_enumerate(unsigned short vendor_id, unsigned short product_id);240241/**242* Free an enumeration linked list.243*244* This function frees a linked list created by SDL_hid_enumerate().245*246* \param devs pointer to a list of struct_device returned from247* SDL_hid_enumerate().248*249* \since This function is available since SDL 3.2.0.250*/251extern SDL_DECLSPEC void SDLCALL SDL_hid_free_enumeration(SDL_hid_device_info *devs);252253/**254* Open a HID device using a Vendor ID (VID), Product ID (PID) and optionally255* a serial number.256*257* If `serial_number` is NULL, the first device with the specified VID and PID258* is opened.259*260* \param vendor_id the Vendor ID (VID) of the device to open.261* \param product_id the Product ID (PID) of the device to open.262* \param serial_number the Serial Number of the device to open (Optionally263* NULL).264* \returns a pointer to a SDL_hid_device object on success or NULL on265* failure; call SDL_GetError() for more information.266*267* \since This function is available since SDL 3.2.0.268*/269extern SDL_DECLSPEC SDL_hid_device * SDLCALL SDL_hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number);270271/**272* Open a HID device by its path name.273*274* The path name be determined by calling SDL_hid_enumerate(), or a275* platform-specific path name can be used (eg: /dev/hidraw0 on Linux).276*277* \param path the path name of the device to open.278* \returns a pointer to a SDL_hid_device object on success or NULL on279* failure; call SDL_GetError() for more information.280*281* \since This function is available since SDL 3.2.0.282*/283extern SDL_DECLSPEC SDL_hid_device * SDLCALL SDL_hid_open_path(const char *path);284285/**286* Write an Output report to a HID device.287*288* The first byte of `data` must contain the Report ID. For devices which only289* support a single report, this must be set to 0x0. The remaining bytes290* contain the report data. Since the Report ID is mandatory, calls to291* SDL_hid_write() will always contain one more byte than the report contains.292* For example, if a hid report is 16 bytes long, 17 bytes must be passed to293* SDL_hid_write(), the Report ID (or 0x0, for devices with a single report),294* followed by the report data (16 bytes). In this example, the length passed295* in would be 17.296*297* SDL_hid_write() will send the data on the first OUT endpoint, if one298* exists. If it does not, it will send the data through the Control Endpoint299* (Endpoint 0).300*301* \param dev a device handle returned from SDL_hid_open().302* \param data the data to send, including the report number as the first303* byte.304* \param length the length in bytes of the data to send.305* \returns the actual number of bytes written and -1 on on failure; call306* SDL_GetError() for more information.307*308* \since This function is available since SDL 3.2.0.309*/310extern SDL_DECLSPEC int SDLCALL SDL_hid_write(SDL_hid_device *dev, const unsigned char *data, size_t length);311312/**313* Read an Input report from a HID device with timeout.314*315* Input reports are returned to the host through the INTERRUPT IN endpoint.316* The first byte will contain the Report number if the device uses numbered317* reports.318*319* \param dev a device handle returned from SDL_hid_open().320* \param data a buffer to put the read data into.321* \param length the number of bytes to read. For devices with multiple322* reports, make sure to read an extra byte for the report323* number.324* \param milliseconds timeout in milliseconds or -1 for blocking wait.325* \returns the actual number of bytes read and -1 on on failure; call326* SDL_GetError() for more information. If no packet was available to327* be read within the timeout period, this function returns 0.328*329* \since This function is available since SDL 3.2.0.330*/331extern SDL_DECLSPEC int SDLCALL SDL_hid_read_timeout(SDL_hid_device *dev, unsigned char *data, size_t length, int milliseconds);332333/**334* Read an Input report from a HID device.335*336* Input reports are returned to the host through the INTERRUPT IN endpoint.337* The first byte will contain the Report number if the device uses numbered338* reports.339*340* \param dev a device handle returned from SDL_hid_open().341* \param data a buffer to put the read data into.342* \param length the number of bytes to read. For devices with multiple343* reports, make sure to read an extra byte for the report344* number.345* \returns the actual number of bytes read and -1 on failure; call346* SDL_GetError() for more information. If no packet was available to347* be read and the handle is in non-blocking mode, this function348* returns 0.349*350* \since This function is available since SDL 3.2.0.351*/352extern SDL_DECLSPEC int SDLCALL SDL_hid_read(SDL_hid_device *dev, unsigned char *data, size_t length);353354/**355* Set the device handle to be non-blocking.356*357* In non-blocking mode calls to SDL_hid_read() will return immediately with a358* value of 0 if there is no data to be read. In blocking mode, SDL_hid_read()359* will wait (block) until there is data to read before returning.360*361* Nonblocking can be turned on and off at any time.362*363* \param dev a device handle returned from SDL_hid_open().364* \param nonblock enable or not the nonblocking reads - 1 to enable365* nonblocking - 0 to disable nonblocking.366* \returns 0 on success or a negative error code on failure; call367* SDL_GetError() for more information.368*369* \since This function is available since SDL 3.2.0.370*/371extern SDL_DECLSPEC int SDLCALL SDL_hid_set_nonblocking(SDL_hid_device *dev, int nonblock);372373/**374* Send a Feature report to the device.375*376* Feature reports are sent over the Control endpoint as a Set_Report377* transfer. The first byte of `data` must contain the Report ID. For devices378* which only support a single report, this must be set to 0x0. The remaining379* bytes contain the report data. Since the Report ID is mandatory, calls to380* SDL_hid_send_feature_report() will always contain one more byte than the381* report contains. For example, if a hid report is 16 bytes long, 17 bytes382* must be passed to SDL_hid_send_feature_report(): the Report ID (or 0x0, for383* devices which do not use numbered reports), followed by the report data (16384* bytes). In this example, the length passed in would be 17.385*386* \param dev a device handle returned from SDL_hid_open().387* \param data the data to send, including the report number as the first388* byte.389* \param length the length in bytes of the data to send, including the report390* number.391* \returns the actual number of bytes written and -1 on failure; call392* SDL_GetError() for more information.393*394* \since This function is available since SDL 3.2.0.395*/396extern SDL_DECLSPEC int SDLCALL SDL_hid_send_feature_report(SDL_hid_device *dev, const unsigned char *data, size_t length);397398/**399* Get a feature report from a HID device.400*401* Set the first byte of `data` to the Report ID of the report to be read.402* Make sure to allow space for this extra byte in `data`. Upon return, the403* first byte will still contain the Report ID, and the report data will start404* in data[1].405*406* \param dev a device handle returned from SDL_hid_open().407* \param data a buffer to put the read data into, including the Report ID.408* Set the first byte of `data` to the Report ID of the report to409* be read, or set it to zero if your device does not use numbered410* reports.411* \param length the number of bytes to read, including an extra byte for the412* report ID. The buffer can be longer than the actual report.413* \returns the number of bytes read plus one for the report ID (which is414* still in the first byte), or -1 on on failure; call SDL_GetError()415* for more information.416*417* \since This function is available since SDL 3.2.0.418*/419extern SDL_DECLSPEC int SDLCALL SDL_hid_get_feature_report(SDL_hid_device *dev, unsigned char *data, size_t length);420421/**422* Get an input report from a HID device.423*424* Set the first byte of `data` to the Report ID of the report to be read.425* Make sure to allow space for this extra byte in `data`. Upon return, the426* first byte will still contain the Report ID, and the report data will start427* in data[1].428*429* \param dev a device handle returned from SDL_hid_open().430* \param data a buffer to put the read data into, including the Report ID.431* Set the first byte of `data` to the Report ID of the report to432* be read, or set it to zero if your device does not use numbered433* reports.434* \param length the number of bytes to read, including an extra byte for the435* report ID. The buffer can be longer than the actual report.436* \returns the number of bytes read plus one for the report ID (which is437* still in the first byte), or -1 on on failure; call SDL_GetError()438* for more information.439*440* \since This function is available since SDL 3.2.0.441*/442extern SDL_DECLSPEC int SDLCALL SDL_hid_get_input_report(SDL_hid_device *dev, unsigned char *data, size_t length);443444/**445* Close a HID device.446*447* \param dev a device handle returned from SDL_hid_open().448* \returns 0 on success or a negative error code on failure; call449* SDL_GetError() for more information.450*451* \since This function is available since SDL 3.2.0.452*/453extern SDL_DECLSPEC int SDLCALL SDL_hid_close(SDL_hid_device *dev);454455/**456* Get The Manufacturer String from a HID device.457*458* \param dev a device handle returned from SDL_hid_open().459* \param string a wide string buffer to put the data into.460* \param maxlen the length of the buffer in multiples of wchar_t.461* \returns 0 on success or a negative error code on failure; call462* SDL_GetError() for more information.463*464* \since This function is available since SDL 3.2.0.465*/466extern SDL_DECLSPEC int SDLCALL SDL_hid_get_manufacturer_string(SDL_hid_device *dev, wchar_t *string, size_t maxlen);467468/**469* Get The Product String from a HID device.470*471* \param dev a device handle returned from SDL_hid_open().472* \param string a wide string buffer to put the data into.473* \param maxlen the length of the buffer in multiples of wchar_t.474* \returns 0 on success or a negative error code on failure; call475* SDL_GetError() for more information.476*477* \since This function is available since SDL 3.2.0.478*/479extern SDL_DECLSPEC int SDLCALL SDL_hid_get_product_string(SDL_hid_device *dev, wchar_t *string, size_t maxlen);480481/**482* Get The Serial Number String from a HID device.483*484* \param dev a device handle returned from SDL_hid_open().485* \param string a wide string buffer to put the data into.486* \param maxlen the length of the buffer in multiples of wchar_t.487* \returns 0 on success or a negative error code on failure; call488* SDL_GetError() for more information.489*490* \since This function is available since SDL 3.2.0.491*/492extern SDL_DECLSPEC int SDLCALL SDL_hid_get_serial_number_string(SDL_hid_device *dev, wchar_t *string, size_t maxlen);493494/**495* Get a string from a HID device, based on its string index.496*497* \param dev a device handle returned from SDL_hid_open().498* \param string_index the index of the string to get.499* \param string a wide string buffer to put the data into.500* \param maxlen the length of the buffer in multiples of wchar_t.501* \returns 0 on success or a negative error code on failure; call502* SDL_GetError() for more information.503*504* \since This function is available since SDL 3.2.0.505*/506extern SDL_DECLSPEC int SDLCALL SDL_hid_get_indexed_string(SDL_hid_device *dev, int string_index, wchar_t *string, size_t maxlen);507508/**509* Get the device info from a HID device.510*511* \param dev a device handle returned from SDL_hid_open().512* \returns a pointer to the SDL_hid_device_info for this hid_device or NULL513* on failure; call SDL_GetError() for more information. This struct514* is valid until the device is closed with SDL_hid_close().515*516* \since This function is available since SDL 3.2.0.517*/518extern SDL_DECLSPEC SDL_hid_device_info * SDLCALL SDL_hid_get_device_info(SDL_hid_device *dev);519520/**521* Get a report descriptor from a HID device.522*523* User has to provide a preallocated buffer where descriptor will be copied524* to. The recommended size for a preallocated buffer is 4096 bytes.525*526* \param dev a device handle returned from SDL_hid_open().527* \param buf the buffer to copy descriptor into.528* \param buf_size the size of the buffer in bytes.529* \returns the number of bytes actually copied or -1 on failure; call530* SDL_GetError() for more information.531*532* \since This function is available since SDL 3.2.0.533*/534extern SDL_DECLSPEC int SDLCALL SDL_hid_get_report_descriptor(SDL_hid_device *dev, unsigned char *buf, size_t buf_size);535536/**537* Start or stop a BLE scan on iOS and tvOS to pair Steam Controllers.538*539* \param active true to start the scan, false to stop the scan.540*541* \since This function is available since SDL 3.2.0.542*/543extern SDL_DECLSPEC void SDLCALL SDL_hid_ble_scan(bool active);544545/* Ends C function definitions when using C++ */546#ifdef __cplusplus547}548#endif549#include <SDL3/SDL_close_code.h>550551#endif /* SDL_hidapi_h_ */552553554