hidapi
Loading...
Searching...
No Matches
hidapi API

Macros

#define HID_API_VERSION_MAJOR   0
 Static/compile-time major version of the library.
#define HID_API_VERSION_MINOR   16
 Static/compile-time minor version of the library.
#define HID_API_VERSION_PATCH   0
 Static/compile-time patch version of the library.
#define HID_API_MAKE_VERSION(mj, mn, p)
 Coverts a version as Major/Minor/Patch into a number: <8 bit major><16 bit minor><8 bit patch>.
#define HID_API_VERSION   HID_API_MAKE_VERSION(HID_API_VERSION_MAJOR, HID_API_VERSION_MINOR, HID_API_VERSION_PATCH)
 Static/compile-time version of the library.
#define HID_API_VERSION_STR   HID_API_TO_VERSION_STR(HID_API_VERSION_MAJOR, HID_API_VERSION_MINOR, HID_API_VERSION_PATCH)
 Static/compile-time string version of the library.
#define HID_API_MAX_REPORT_DESCRIPTOR_SIZE   4096
 Maximum expected HID Report descriptor size in bytes.

Enumerations

enum  hid_bus_type {
  HID_API_BUS_UNKNOWN = 0x00 , HID_API_BUS_USB = 0x01 , HID_API_BUS_BLUETOOTH = 0x02 , HID_API_BUS_I2C = 0x03 ,
  HID_API_BUS_SPI = 0x04 , HID_API_BUS_VIRTUAL = 0x05
}
 HID underlying bus types. More...

Functions

int HID_API_EXPORT HID_API_CALL hid_init (void)
 Initialize the HIDAPI library.
int HID_API_EXPORT HID_API_CALL hid_exit (void)
 Finalize the HIDAPI library.
struct hid_device_info HID_API_EXPORT *HID_API_CALL hid_enumerate (unsigned short vendor_id, unsigned short product_id)
 Enumerate the HID Devices.
void HID_API_EXPORT HID_API_CALL hid_free_enumeration (struct hid_device_info *devs)
 Free an enumeration Linked List.
HID_API_EXPORT hid_device *HID_API_CALL hid_open (unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
 Open a HID device using a Vendor ID (VID), Product ID (PID) and optionally a serial number.
HID_API_EXPORT hid_device *HID_API_CALL hid_open_path (const char *path)
 Open a HID device by its path name.
int HID_API_EXPORT HID_API_CALL hid_write (hid_device *dev, const unsigned char *data, size_t length)
 Write an Output report to a HID device.
int HID_API_EXPORT HID_API_CALL hid_read_timeout (hid_device *dev, unsigned char *data, size_t length, int milliseconds)
 Read an Input report from a HID device with timeout.
int HID_API_EXPORT HID_API_CALL hid_read (hid_device *dev, unsigned char *data, size_t length)
 Read an Input report from a HID device.
HID_API_EXPORT const wchar_t *HID_API_CALL hid_read_error (hid_device *dev)
 Get a string describing the last error which occurred during hid_read/hid_read_timeout.
int HID_API_EXPORT HID_API_CALL hid_set_nonblocking (hid_device *dev, int nonblock)
 Set the device handle to be non-blocking.
int HID_API_EXPORT HID_API_CALL hid_set_num_input_buffers (hid_device *dev, int num_buffers)
 Set the number of input report buffers queued per device.
void HID_API_EXPORT HID_API_CALL hid_read_interrupt (hid_device *dev)
 Asynchronously interrupt blocking hid_read/hid_read_timeout call.
int HID_API_EXPORT HID_API_CALL hid_is_read_interrupted (hid_device *dev)
 Query whether the read pipeline is currently interrupted.
void HID_API_EXPORT HID_API_CALL hid_read_clear_interrupt (hid_device *dev)
 Clear the read-interrupt state, allowing reads to resume.
int HID_API_EXPORT HID_API_CALL hid_send_feature_report (hid_device *dev, const unsigned char *data, size_t length)
 Send a Feature report to the device.
int HID_API_EXPORT HID_API_CALL hid_get_feature_report (hid_device *dev, unsigned char *data, size_t length)
 Get a feature report from a HID device.
int HID_API_EXPORT HID_API_CALL hid_send_output_report (hid_device *dev, const unsigned char *data, size_t length)
 Send a Output report to the device.
int HID_API_EXPORT HID_API_CALL hid_get_input_report (hid_device *dev, unsigned char *data, size_t length)
 Get a input report from a HID device.
void HID_API_EXPORT HID_API_CALL hid_close (hid_device *dev)
 Close a HID device.
int HID_API_EXPORT_CALL hid_get_manufacturer_string (hid_device *dev, wchar_t *string, size_t maxlen)
 Get The Manufacturer String from a HID device.
int HID_API_EXPORT_CALL hid_get_product_string (hid_device *dev, wchar_t *string, size_t maxlen)
 Get The Product String from a HID device.
int HID_API_EXPORT_CALL hid_get_serial_number_string (hid_device *dev, wchar_t *string, size_t maxlen)
 Get The Serial Number String from a HID device.
struct hid_device_info HID_API_EXPORT *HID_API_CALL hid_get_device_info (hid_device *dev)
 Get The struct hid_device_info from a HID device.
int HID_API_EXPORT_CALL hid_get_indexed_string (hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
 Get a string from a HID device, based on its string index.
int HID_API_EXPORT_CALL hid_get_report_descriptor (hid_device *dev, unsigned char *buf, size_t buf_size)
 Get a report descriptor from a HID device.
HID_API_EXPORT const wchar_t *HID_API_CALL hid_error (hid_device *dev)
 Get a string describing the last error which occurred.
HID_API_EXPORT const struct hid_api_version *HID_API_CALL hid_version (void)
 Get a runtime version of the library.
HID_API_EXPORT const char *HID_API_CALL hid_version_str (void)
 Get a runtime version string of the library.
HID_API_EXPORT hid_device *HID_API_CALL hid_libusb_wrap_sys_device (intptr_t sys_dev, int interface_num)
 Open a HID device using libusb_wrap_sys_device. See https://libusb.sourceforge.io/api-1.0/group__libusb__dev.html#ga98f783e115ceff4eaf88a60e6439563c, for details on libusb_wrap_sys_device.
HID_API_EXPORT int HID_API_CALL hid_libusb_error (hid_device *dev)
 Similar to hid_error but gives a libusb error code.
HID_API_EXPORT void HID_API_CALL hid_libusb_set_write_timeout (hid_device *dev, unsigned int timeout)
 Sets the timeout for hid_write operation.
HID_API_EXPORT void HID_API_CALL hid_libusb_set_send_output_report_timeout (hid_device *dev, unsigned int timeout)
 Sets the timeout for hid_send_output_report operation.
HID_API_EXPORT void HID_API_CALL hid_libusb_set_send_feature_report_timeout (hid_device *dev, unsigned int timeout)
 Sets the timeout for hid_send_feature_report operation.
int HID_API_EXPORT_CALL hid_darwin_get_location_id (hid_device *dev, uint32_t *location_id)
 Get the location ID for a HID device.
void HID_API_EXPORT_CALL hid_darwin_set_open_exclusive (int open_exclusive)
 Changes the behavior of all further calls to hid_open or hid_open_path.
int HID_API_EXPORT_CALL hid_darwin_get_open_exclusive (void)
 Getter for option set by hid_darwin_set_open_exclusive.
int HID_API_EXPORT_CALL hid_darwin_is_device_open_exclusive (hid_device *dev)
 Check how the device was opened.
int HID_API_EXPORT_CALL hid_winapi_get_container_id (hid_device *dev, GUID *container_id)
 Get the container ID for a HID device.
int HID_API_EXPORT_CALL hid_winapi_descriptor_reconstruct_pp_data (void *hidp_preparsed_data, unsigned char *buf, size_t buf_size)
 Reconstructs a HID Report Descriptor from a Win32 HIDP_PREPARSED_DATA structure. This reconstructed report descriptor is logical identical to the real report descriptor, but not byte wise identical.
void HID_API_EXPORT_CALL hid_winapi_set_write_timeout (hid_device *dev, unsigned long timeout)
 Sets the timeout for hid_write operation.

Detailed Description

Since version 0.11.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 11, 0).

Since version 0.12.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)

Macro Definition Documentation

◆ HID_API_MAKE_VERSION

#define HID_API_MAKE_VERSION ( mj,
mn,
p )
Value:
(((mj) << 24) | ((mn) << 8) | (p))

Coverts a version as Major/Minor/Patch into a number: <8 bit major><16 bit minor><8 bit patch>.

This macro was added in version 0.12.0.

Convenient function to be used for compile-time checks, like:

#if HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)
Examples
test.c.

◆ HID_API_MAX_REPORT_DESCRIPTOR_SIZE

#define HID_API_MAX_REPORT_DESCRIPTOR_SIZE   4096

Maximum expected HID Report descriptor size in bytes.

Since version 0.13.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 13, 0)

Examples
test.c.

◆ HID_API_VERSION

Static/compile-time version of the library.

This macro was added in version 0.12.0.

See also
HID_API_MAKE_VERSION.
Examples
test.c.

Enumeration Type Documentation

◆ hid_bus_type

HID underlying bus types.

Enumerator
HID_API_BUS_UNKNOWN 

Unknown bus type

HID_API_BUS_USB 

USB bus Specifications: https://usb.org/hid

HID_API_BUS_BLUETOOTH 

Bluetooth or Bluetooth LE bus Specifications: https://www.bluetooth.com/specifications/specs/human-interface-device-profile-1-1-1/ https://www.bluetooth.com/specifications/specs/hid-service-1-0/ https://www.bluetooth.com/specifications/specs/hid-over-gatt-profile-1-0/

HID_API_BUS_I2C 

I2C bus Specifications: https://docs.microsoft.com/previous-versions/windows/hardware/design/dn642101(v=vs.85)

HID_API_BUS_SPI 

SPI bus Specifications: https://www.microsoft.com/download/details.aspx?id=103325

HID_API_BUS_VIRTUAL 

Virtual device E.g.: https://elixir.bootlin.com/linux/v4.0/source/include/uapi/linux/input.h#L955

Since version 0.16.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 16, 0)

Examples
test.c.

Function Documentation

◆ hid_close()

void HID_API_EXPORT HID_API_CALL hid_close ( hid_device * dev)

Close a HID device.

Parameters
devA device handle returned from hid_open().
Examples
test.c.

◆ hid_darwin_get_location_id()

int HID_API_EXPORT_CALL hid_darwin_get_location_id ( hid_device * dev,
uint32_t * location_id )

Get the location ID for a HID device.

Since version 0.12.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)

Parameters
devA device handle returned from hid_open().
location_idThe device's location ID on return.
Returns
This function returns 0 on success and -1 on error.

◆ hid_darwin_get_open_exclusive()

int HID_API_EXPORT_CALL hid_darwin_get_open_exclusive ( void )

Getter for option set by hid_darwin_set_open_exclusive.

Since version 0.12.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)

Returns
1 if all further devices will be opened in exclusive mode.
Note
Value returned by this function before calling to hid_init or after hid_exit is not reliable.

◆ hid_darwin_is_device_open_exclusive()

int HID_API_EXPORT_CALL hid_darwin_is_device_open_exclusive ( hid_device * dev)

Check how the device was opened.

Since version 0.12.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)

Parameters
devA device to get property from.
Returns
1 if the device is opened in exclusive mode, 0 - opened in non-exclusive, -1 - if dev is invalid.

◆ hid_darwin_set_open_exclusive()

void HID_API_EXPORT_CALL hid_darwin_set_open_exclusive ( int open_exclusive)

Changes the behavior of all further calls to hid_open or hid_open_path.

By default on Darwin platform all devices opened by HIDAPI with hid_open or hid_open_path are opened in exclusive mode (see kIOHIDOptionsTypeSeizeDevice).

Since version 0.12.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)

Parameters
open_exclusiveWhen set to 0 - all further devices will be opened in non-exclusive mode. Otherwise - all further devices will be opened in exclusive mode.
Note
During the initialisation by hid_init - this property is set to 1 (TRUE). This is done to preserve full backward compatibility with previous behavior.
Calling this function before hid_init or after hid_exit has no effect.
Examples
test.c.

◆ hid_enumerate()

struct hid_device_info HID_API_EXPORT *HID_API_CALL hid_enumerate ( unsigned short vendor_id,
unsigned short product_id )

Enumerate the HID Devices.

This function returns a linked list of all the HID devices attached to the system which match vendor_id and product_id. If vendor_id is set to 0 then any vendor matches. If product_id is set to 0 then any product matches. If vendor_id and product_id are both set to 0, then all HID devices will be returned.

Parameters
vendor_idThe Vendor ID (VID) of the types of device to open.
product_idThe Product ID (PID) of the types of device to open.
Returns
This function returns a pointer to a linked list of type struct hid_device_info, containing information about the HID devices attached to the system, or NULL in the case of failure or if no HID devices present in the system. Call hid_error(NULL) to get the failure reason.
Note
The returned value by this function must to be freed by calling hid_free_enumeration(), when not needed anymore.
Examples
test.c.

◆ hid_error()

HID_API_EXPORT const wchar_t *HID_API_CALL hid_error ( hid_device * dev)

Get a string describing the last error which occurred.

This function is intended for logging/debugging purposes.

This function guarantees to never return NULL. If there was no error in the last function call - the returned string clearly indicates that.

Any HIDAPI function that can explicitly indicate an execution failure (e.g. by an error code, or by returning NULL) - may set the error string, to be returned by this function.

Strings returned from hid_error() must not be freed by the user, i.e. owned by HIDAPI library. Device-specific error string may remain allocated at most until hid_close() is called. Global error string may remain allocated at most until hid_exit() is called.

Parameters
devA device handle returned from hid_open(), or NULL to get the last non-device-specific error (e.g. for errors in hid_open() or hid_enumerate()).
Returns
A string describing the last error (if any).
Examples
test.c.

◆ hid_exit()

int HID_API_EXPORT HID_API_CALL hid_exit ( void )

Finalize the HIDAPI library.

This function frees all of the static data associated with HIDAPI. It should be called at the end of execution to avoid memory leaks.

Returns
This function returns 0 on success and -1 on error.
Examples
test.c.

◆ hid_free_enumeration()

void HID_API_EXPORT HID_API_CALL hid_free_enumeration ( struct hid_device_info * devs)

Free an enumeration Linked List.

This function frees a linked list created by hid_enumerate().

Parameters
devsPointer to a list of struct_device returned from hid_enumerate().
Examples
test.c.

◆ hid_get_device_info()

struct hid_device_info HID_API_EXPORT *HID_API_CALL hid_get_device_info ( hid_device * dev)

Get The struct hid_device_info from a HID device.

Since version 0.13.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 13, 0)

Parameters
devA device handle returned from hid_open().
Returns
This function returns a pointer to the struct hid_device_info for this hid_device, or NULL in the case of failure. Call hid_error(dev) to get the failure reason. This struct is valid until the device is closed with hid_close().
Note
The returned object is owned by the dev, and SHOULD NOT be freed by the user.
Examples
test.c.

◆ hid_get_feature_report()

int HID_API_EXPORT HID_API_CALL hid_get_feature_report ( hid_device * dev,
unsigned char * data,
size_t length )

Get a feature report from a HID device.

Set the first byte of data[] to the Report ID of the report to be read. Make sure to allow space for this extra byte in data[]. Upon return, the first byte will still contain the Report ID, and the report data will start in data[1].

Parameters
devA device handle returned from hid_open().
dataA buffer to put the read data into, including the Report ID. Set the first byte of data[] to the Report ID of the report to be read, or set it to zero if your device does not use numbered reports.
lengthThe number of bytes to read, including an extra byte for the report ID. The buffer can be longer than the actual report.
Returns
This function returns the number of bytes read plus one for the report ID (which is still in the first byte), or -1 on error. Call hid_error(dev) to get the failure reason.
Examples
test.c.

◆ hid_get_indexed_string()

int HID_API_EXPORT_CALL hid_get_indexed_string ( hid_device * dev,
int string_index,
wchar_t * string,
size_t maxlen )

Get a string from a HID device, based on its string index.

Parameters
devA device handle returned from hid_open().
string_indexThe index of the string to get.
stringA wide string buffer to put the data into.
maxlenThe length of the buffer in multiples of wchar_t.
Returns
This function returns 0 on success and -1 on error. Call hid_error(dev) to get the failure reason.
Examples
test.c.

◆ hid_get_input_report()

int HID_API_EXPORT HID_API_CALL hid_get_input_report ( hid_device * dev,
unsigned char * data,
size_t length )

Get a input report from a HID device.

Since version 0.10.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 10, 0)

Set the first byte of data[] to the Report ID of the report to be read. Make sure to allow space for this extra byte in data[]. Upon return, the first byte will still contain the Report ID, and the report data will start in data[1].

Parameters
devA device handle returned from hid_open().
dataA buffer to put the read data into, including the Report ID. Set the first byte of data[] to the Report ID of the report to be read, or set it to zero if your device does not use numbered reports.
lengthThe number of bytes to read, including an extra byte for the report ID. The buffer can be longer than the actual report.
Returns
This function returns the number of bytes read plus one for the report ID (which is still in the first byte), or -1 on error. Call hid_error(dev) to get the failure reason.
Examples
test.c.

◆ hid_get_manufacturer_string()

int HID_API_EXPORT_CALL hid_get_manufacturer_string ( hid_device * dev,
wchar_t * string,
size_t maxlen )

Get The Manufacturer String from a HID device.

Parameters
devA device handle returned from hid_open().
stringA wide string buffer to put the data into.
maxlenThe length of the buffer in multiples of wchar_t.
Returns
This function returns 0 on success and -1 on error. Call hid_error(dev) to get the failure reason.
Examples
test.c.

◆ hid_get_product_string()

int HID_API_EXPORT_CALL hid_get_product_string ( hid_device * dev,
wchar_t * string,
size_t maxlen )

Get The Product String from a HID device.

Parameters
devA device handle returned from hid_open().
stringA wide string buffer to put the data into.
maxlenThe length of the buffer in multiples of wchar_t.
Returns
This function returns 0 on success and -1 on error. Call hid_error(dev) to get the failure reason.
Examples
test.c.

◆ hid_get_report_descriptor()

int HID_API_EXPORT_CALL hid_get_report_descriptor ( hid_device * dev,
unsigned char * buf,
size_t buf_size )

Get a report descriptor from a HID device.

Since version 0.14.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 14, 0)

User has to provide a preallocated buffer where descriptor will be copied to. The recommended size for preallocated buffer is HID_API_MAX_REPORT_DESCRIPTOR_SIZE bytes.

Parameters
devA device handle returned from hid_open().
bufThe buffer to copy descriptor into.
buf_sizeThe size of the buffer in bytes.
Returns
This function returns non-negative number of bytes actually copied, or -1 on error.
Examples
test.c.

◆ hid_get_serial_number_string()

int HID_API_EXPORT_CALL hid_get_serial_number_string ( hid_device * dev,
wchar_t * string,
size_t maxlen )

Get The Serial Number String from a HID device.

Parameters
devA device handle returned from hid_open().
stringA wide string buffer to put the data into.
maxlenThe length of the buffer in multiples of wchar_t.
Returns
This function returns 0 on success and -1 on error. Call hid_error(dev) to get the failure reason.
Examples
test.c.

◆ hid_init()

int HID_API_EXPORT HID_API_CALL hid_init ( void )

Initialize the HIDAPI library.

This function initializes the HIDAPI library. Calling it is not strictly necessary, as it will be called automatically by hid_enumerate() and any of the hid_open_*() functions if it is needed. This function should be called at the beginning of execution however, if there is a chance of HIDAPI handles being opened by different threads simultaneously.

Returns
This function returns 0 on success and -1 on error. Call hid_error(NULL) to get the failure reason.
Examples
test.c.

◆ hid_is_read_interrupted()

int HID_API_EXPORT HID_API_CALL hid_is_read_interrupted ( hid_device * dev)

Query whether the read pipeline is currently interrupted.

Since version 0.16.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 16, 0)

Returns the current interrupt state set by hid_read_interrupt and hid_read_clear_interrupt. This function may run alongside device I/O, but not hid_close() or hid_exit(). The device handle must remain valid until the call returns.

Calls to hid_read_interrupt(), hid_is_read_interrupted(), and hid_read_clear_interrupt() on the same device must be serialized against one another, including calls to the same function. Otherwise, behavior is undefined.

Parameters
devA device handle returned from hid_open().
Returns
1 if the read pipeline is interrupted, 0 if not.

◆ hid_libusb_error()

HID_API_EXPORT int HID_API_CALL hid_libusb_error ( hid_device * dev)

Similar to hid_error but gives a libusb error code.

Since version 0.16.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 16, 0)

If the error occurred is not immediately caused by a libusb function call, the returned value is 1. hid_error would still contain a valid and meaningful error message.

Parameters
devA device handle returned from hid_open(), or NULL to get the last non-device-specific error (e.g. for errors in hid_open() or hid_enumerate()).
Returns
enum libusb_error value representing last error code.

◆ hid_libusb_set_send_feature_report_timeout()

HID_API_EXPORT void HID_API_CALL hid_libusb_set_send_feature_report_timeout ( hid_device * dev,
unsigned int timeout )

Sets the timeout for hid_send_feature_report operation.

Since version 0.16.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 16, 0)

The default timeout is 1sec for libusb backend.

In case if 1sec is not enough, on in case of multi-platform development, the recommended value is 5sec, e.g. to match (unconfigurable) 5sec timeout set for hidraw (linux kernel) implementation.

When the timeout is set to 0, the function will not exit, until the write operation is performed or an error occurred.

Parameters
devA device handle returned from hid_open().
timeoutNew timeout value in milliseconds.

◆ hid_libusb_set_send_output_report_timeout()

HID_API_EXPORT void HID_API_CALL hid_libusb_set_send_output_report_timeout ( hid_device * dev,
unsigned int timeout )

Sets the timeout for hid_send_output_report operation.

Since version 0.16.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 16, 0)

The default timeout is 1sec for libusb backend.

In case if 1sec is not enough, on in case of multi-platform development, the recommended value is 5sec, e.g. to match (unconfigurable) 5sec timeout set for hidraw (linux kernel) implementation.

When the timeout is set to 0, the function will not exit, until the write operation is performed or an error occurred.

Parameters
devA device handle returned from hid_open().
timeoutNew timeout value in milliseconds.

◆ hid_libusb_set_write_timeout()

HID_API_EXPORT void HID_API_CALL hid_libusb_set_write_timeout ( hid_device * dev,
unsigned int timeout )

Sets the timeout for hid_write operation.

Since version 0.16.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 16, 0)

The default timeout is 1sec for libusb backend.

In case if 1sec is not enough, on in case of multi-platform development, the recommended value is 5sec, e.g. to match (unconfigurable) 5sec timeout set for hidraw (linux kernel) implementation.

When the timeout is set to 0, the function will not exit, until the write operation is performed or an error occurred.

Parameters
devA device handle returned from hid_open().
timeoutNew timeout value in milliseconds.

◆ hid_libusb_wrap_sys_device()

HID_API_EXPORT hid_device *HID_API_CALL hid_libusb_wrap_sys_device ( intptr_t sys_dev,
int interface_num )

Open a HID device using libusb_wrap_sys_device. See https://libusb.sourceforge.io/api-1.0/group__libusb__dev.html#ga98f783e115ceff4eaf88a60e6439563c, for details on libusb_wrap_sys_device.

Parameters
sys_devPlatform-specific file descriptor that can be recognized by libusb.
interface_numUSB interface number of the device to be used as HID interface. Pass -1 to select first HID interface of the device.
Returns
This function returns a pointer to a hid_device object on success or NULL on failure.

◆ hid_open()

HID_API_EXPORT hid_device *HID_API_CALL hid_open ( unsigned short vendor_id,
unsigned short product_id,
const wchar_t * serial_number )

Open a HID device using a Vendor ID (VID), Product ID (PID) and optionally a serial number.

If serial_number is NULL, the first device with the specified VID and PID is opened.

Parameters
vendor_idThe Vendor ID (VID) of the device to open.
product_idThe Product ID (PID) of the device to open.
serial_numberThe Serial Number of the device to open (Optionally NULL).
Returns
This function returns a pointer to a hid_device object on success or NULL on failure. Call hid_error(NULL) to get the failure reason.
Note
The returned object must be freed by calling hid_close(), when not needed anymore.
Examples
test.c.

◆ hid_open_path()

HID_API_EXPORT hid_device *HID_API_CALL hid_open_path ( const char * path)

Open a HID device by its path name.

The path name be determined by calling hid_enumerate(), or a platform-specific path name can be used (eg: /dev/hidraw0 on Linux).

Parameters
pathThe path name of the device to open
Returns
This function returns a pointer to a hid_device object on success or NULL on failure. Call hid_error(NULL) to get the failure reason.
Note
The returned object must be freed by calling hid_close(), when not needed anymore.
Examples
test.c.

◆ hid_read()

int HID_API_EXPORT HID_API_CALL hid_read ( hid_device * dev,
unsigned char * data,
size_t length )

Read an Input report from a HID device.

Input reports are returned to the host through the INTERRUPT IN endpoint. The first byte will contain the Report number if the device uses numbered reports.

Parameters
devA device handle returned from hid_open().
dataA buffer to put the read data into.
lengthThe number of bytes to read. For devices with multiple reports, make sure to read an extra byte for the report number.
Returns
This function returns the actual number of bytes read and -1 on error. Call hid_read_error(dev) to get the failure reason. If no packet was available to be read and the handle is in non-blocking mode, this function returns 0.
Note
This function doesn't change the buffer returned by the hid_error(dev).
Examples
test.c.

◆ hid_read_clear_interrupt()

void HID_API_EXPORT HID_API_CALL hid_read_clear_interrupt ( hid_device * dev)

Clear the read-interrupt state, allowing reads to resume.

Since version 0.16.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 16, 0)

Clears the sticky state set by hid_read_interrupt. After this function returns, hid_is_read_interrupted() returns 0 and reads entered afterwards operate normally. Calling it when the read pipeline is not interrupted has no effect.

Any data that the device buffered during the interrupted period may be returned by subsequent reads, subject to a (backend-specific) buffer capacity. For a fresh-start behavior, the caller may drain the buffered data with a loop of hid_read_timeout(dev, ..., 0) calls before resuming the normal read loop.

Recommended use: call only when no hid_read*() call is in flight on dev. A read that is in flight while this function runs may either fail as interrupted or continue normally; which of the two happens is indeterminate. Do not overlap this function with hid_close() or hid_exit(); the device handle must remain valid until the call returns.

Calls to hid_read_interrupt(), hid_is_read_interrupted(), and hid_read_clear_interrupt() on the same device must be serialized against one another, including calls to the same function. Otherwise, behavior is undefined.

Parameters
devA device handle returned from hid_open().

◆ hid_read_error()

HID_API_EXPORT const wchar_t *HID_API_CALL hid_read_error ( hid_device * dev)

Get a string describing the last error which occurred during hid_read/hid_read_timeout.

Since version 0.15.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 15, 0)

This function is intended for logging/debugging purposes.

This function guarantees to never return NULL for a valid dev. If there was no error in the last call to hid_read/hid_read_error - the returned string clearly indicates that.

Strings returned from hid_read_error() must not be freed by the user, i.e. owned by HIDAPI library. Device-specific error string may remain allocated at most until hid_close() is called.

Parameters
devA device handle. Shall never be NULL.
Returns
A string describing the hid_read/hid_read_timeout error (if any).
Examples
test.c.

◆ hid_read_interrupt()

void HID_API_EXPORT HID_API_CALL hid_read_interrupt ( hid_device * dev)

Asynchronously interrupt blocking hid_read/hid_read_timeout call.

Since version 0.16.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 16, 0)

Puts the read pipeline of dev into a sticky interrupted state and wakes a hid_read()/hid_read_timeout() call that is waiting for input. An interrupted read returns -1; hid_read_error(dev) returns a string indicating the read was interrupted.

A read already in progress when this function is called may still return data that was already buffered or queued. Every read entered after this function returns fails immediately with -1, until hid_read_clear_interrupt is called. Queued input is not discarded; it remains readable after the interrupt is cleared.

Only the read pipeline is affected: hid_write(), hid_get_input_report(), feature/output report functions, and all other operations on dev continue to work normally.

This function may run concurrently with device I/O, including a blocked read, but not with hid_close() or hid_exit(). The device handle must remain valid until all calls using it have returned. The call is idempotent and is safe when no read is in flight.

Calls to hid_read_interrupt(), hid_is_read_interrupted(), and hid_read_clear_interrupt() on the same device must be serialized against one another, including calls to the same function. Otherwise, behavior is undefined.

After this function returns, hid_is_read_interrupted() returns 1 until the interrupt is cleared. This function has no failure result; it relies on the operating system and device driver to support read interruption.

Recommended usage to cleanly shut down a dedicated read thread:

// join the read thread
hid_close(dev);
void HID_API_EXPORT HID_API_CALL hid_read_interrupt(hid_device *dev)
Asynchronously interrupt blocking hid_read/hid_read_timeout call.
void HID_API_EXPORT HID_API_CALL hid_close(hid_device *dev)
Close a HID device.
Parameters
devA device handle returned from hid_open().

◆ hid_read_timeout()

int HID_API_EXPORT HID_API_CALL hid_read_timeout ( hid_device * dev,
unsigned char * data,
size_t length,
int milliseconds )

Read an Input report from a HID device with timeout.

Input reports are returned to the host through the INTERRUPT IN endpoint. The first byte will contain the Report number if the device uses numbered reports.

Parameters
devA device handle returned from hid_open().
dataA buffer to put the read data into.
lengthThe number of bytes to read. For devices with multiple reports, make sure to read an extra byte for the report number.
millisecondstimeout in milliseconds or -1 for blocking wait.
Returns
This function returns the actual number of bytes read and -1 on error. Call hid_read_error(dev) to get the failure reason. If no packet was available to be read within the timeout period, this function returns 0.
Note
This function doesn't change the buffer returned by the hid_error(dev).
Examples
test.c.

◆ hid_send_feature_report()

int HID_API_EXPORT HID_API_CALL hid_send_feature_report ( hid_device * dev,
const unsigned char * data,
size_t length )

Send a Feature report to the device.

Feature reports are sent over the Control endpoint as a Set_Report transfer. The first byte of data[] must contain the Report ID. For devices which only support a single report, this must be set to 0x0. The remaining bytes contain the report data. Since the Report ID is mandatory, calls to hid_send_feature_report() will always contain one more byte than the report contains. For example, if a hid report is 16 bytes long, 17 bytes must be passed to hid_send_feature_report(): the Report ID (or 0x0, for devices which do not use numbered reports), followed by the report data (16 bytes). In this example, the length passed in would be 17.

Parameters
devA device handle returned from hid_open().
dataThe data to send, including the report number as the first byte.
lengthThe length in bytes of the data to send, including the report number.
Returns
This function returns the actual number of bytes written and -1 on error. Call hid_error(dev) to get the failure reason.
Examples
test.c.

◆ hid_send_output_report()

int HID_API_EXPORT HID_API_CALL hid_send_output_report ( hid_device * dev,
const unsigned char * data,
size_t length )

Send a Output report to the device.

Since version 0.15.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 15, 0)

Output reports are sent over the Control endpoint as a Set_Report transfer. The first byte of data[] must contain the Report ID. For devices which only support a single report, this must be set to 0x0. The remaining bytes contain the report data. Since the Report ID is mandatory, calls to hid_send_output_report() will always contain one more byte than the report contains. For example, if a hid report is 16 bytes long, 17 bytes must be passed to hid_send_output_report(): the Report ID (or 0x0, for devices which do not use numbered reports), followed by the report data (16 bytes). In this example, the length passed in would be 17.

This function sets the return value of hid_error().

Parameters
devA device handle returned from hid_open().
dataThe data to send, including the report number as the first byte.
lengthThe length in bytes of the data to send, including the report number.
Returns
This function returns the actual number of bytes written and -1 on error.
See also
hid_write
Examples
test.c.

◆ hid_set_nonblocking()

int HID_API_EXPORT HID_API_CALL hid_set_nonblocking ( hid_device * dev,
int nonblock )

Set the device handle to be non-blocking.

In non-blocking mode calls to hid_read() will return immediately with a value of 0 if there is no data to be read. In blocking mode, hid_read() will wait (block) until there is data to read before returning.

Nonblocking can be turned on and off at any time.

Parameters
devA device handle returned from hid_open().
nonblockenable or not the nonblocking reads
  • 1 to enable nonblocking
  • 0 to disable nonblocking.
Returns
This function returns 0 on success and -1 on error. Call hid_error(dev) to get the failure reason.
Examples
test.c.

◆ hid_set_num_input_buffers()

int HID_API_EXPORT HID_API_CALL hid_set_num_input_buffers ( hid_device * dev,
int num_buffers )

Set the number of input report buffers queued per device.

Since version 0.16.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 16, 0)

Some HID devices emit input reports in bursts at rates that exceed the default internal queue capacity, causing silent report drops on macOS and the libusb backend. This function allows callers to change how many input report buffers are retained per device.

Call after hid_open() and before the first hid_read() to avoid losing reports buffered at open time.

Note
Per-backend behavior:
  • **macOS (IOKit)** and **libusb**: resizes the userspace input-report queue. Default: 30 reports. A full queue drops the oldest report. Shrinking below the queued count keeps the newest reports in FIFO order.
  • **Windows**: forwards to HidD_SetNumInputBuffers(), which resizes the kernel HID ring buffer. The kernel accepts values in the range [2, 512]; requests outside this range return -1. Default: 64 reports.
  • **Linux/FreeBSD hidraw** and **NetBSD uhid**: the call is accepted (returns 0) and validated against HID_API_MAX_NUM_INPUT_BUFFERS, but has no effect (no-op) — these kernels manage the input report buffer internally and expose no userspace resize.
Parameters
devA device handle returned from hid_open().
num_buffersThe desired number of input report buffers. Must be in range [1, HID_API_MAX_NUM_INPUT_BUFFERS].
Returns
0 on success, -1 on failure (invalid parameters or backend-specific error). Call hid_error(dev) for details where supported.
Examples
test.c.

◆ hid_version()

HID_API_EXPORT const struct hid_api_version *HID_API_CALL hid_version ( void )

Get a runtime version of the library.

This function is thread-safe.

Returns
Pointer to statically allocated struct, that contains version.
Examples
test.c.

◆ hid_version_str()

HID_API_EXPORT const char *HID_API_CALL hid_version_str ( void )

Get a runtime version string of the library.

This function is thread-safe.

Returns
Pointer to statically allocated string, that contains version string.
Examples
test.c.

◆ hid_winapi_descriptor_reconstruct_pp_data()

int HID_API_EXPORT_CALL hid_winapi_descriptor_reconstruct_pp_data ( void * hidp_preparsed_data,
unsigned char * buf,
size_t buf_size )

Reconstructs a HID Report Descriptor from a Win32 HIDP_PREPARSED_DATA structure. This reconstructed report descriptor is logical identical to the real report descriptor, but not byte wise identical.

Parameters
[in]hidp_preparsed_dataPointer to the HIDP_PREPARSED_DATA to read, i.e.: the value of PHIDP_PREPARSED_DATA, as returned by HidD_GetPreparsedData WinAPI function.
bufPointer to the buffer where the report descriptor should be stored.
[in]buf_sizeSize of the buffer. The recommended size for the buffer is HID_API_MAX_REPORT_DESCRIPTOR_SIZE bytes.
Returns
Returns size of reconstructed report descriptor if successful, -1 for error.

◆ hid_winapi_get_container_id()

int HID_API_EXPORT_CALL hid_winapi_get_container_id ( hid_device * dev,
GUID * container_id )

Get the container ID for a HID device.

Since version 0.12.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)

This function returns the `DEVPKEY_Device_ContainerId` property of the given device. This can be used to correlate different interfaces/ports on the same hardware device.

Parameters
devA device handle returned from hid_open().
container_idThe device's container ID on return.
Returns
This function returns 0 on success and -1 on error.

◆ hid_winapi_set_write_timeout()

void HID_API_EXPORT_CALL hid_winapi_set_write_timeout ( hid_device * dev,
unsigned long timeout )

Sets the timeout for hid_write operation.

Since version 0.15.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 15, 0)

The default timeout is 1sec for winapi backend.

In case if 1sec is not enough, on in case of multi-platform development, the recommended value is 5sec, e.g. to match (unconfigurable) 5sec timeout set for hidraw (linux kernel) implementation.

When the timeout is set to 0, hid_write function becomes non-blocking and would exit immediately. When the timeout is set to INFINITE ((DWORD)-1), the function will not exit, until the write operation is performed or an error occurred. See dwMilliseconds parameter documentation of WaitForSingleObject function.

Parameters
devA device handle returned from hid_open().
timeoutNew timeout value in milliseconds.
Examples
test.c.

◆ hid_write()

int HID_API_EXPORT HID_API_CALL hid_write ( hid_device * dev,
const unsigned char * data,
size_t length )

Write an Output report to a HID device.

The first byte of data[] must contain the Report ID. For devices which only support a single report, this must be set to 0x0. The remaining bytes contain the report data. Since the Report ID is mandatory, calls to hid_write() will always contain one more byte than the report contains. For example, if a hid report is 16 bytes long, 17 bytes must be passed to hid_write(), the Report ID (or 0x0, for devices with a single report), followed by the report data (16 bytes). In this example, the length passed in would be 17.

hid_write() will send the data on the first interrupt OUT endpoint, if one exists. If it does not the behaviour is as hid_send_output_report

Parameters
devA device handle returned from hid_open().
dataThe data to send, including the report number as the first byte.
lengthThe length in bytes of the data to send.
Returns
This function returns the actual number of bytes written and -1 on error. Call hid_error(dev) to get the failure reason.
Examples
test.c.