SDL  2.0
hidapi.h
Go to the documentation of this file.
1 /*******************************************************
2  HIDAPI - Multi-Platform library for
3  communication with HID devices.
4 
5  Alan Ott
6  Signal 11 Software
7 
8  8/22/2009
9 
10  Copyright 2009, All Rights Reserved.
11 
12  At the discretion of the user of this library,
13  this software may be licensed under the terms of the
14  GNU General Public License v3, a BSD-Style license, or the
15  original HIDAPI license as outlined in the LICENSE.txt,
16  LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
17  files located at the root of the source distribution.
18  These files may also be found in the public source
19  code repository located at:
20  http://github.com/signal11/hidapi .
21 ********************************************************/
22 
23 /** @file
24  * @defgroup API hidapi API
25  */
26 
27 #ifndef HIDAPI_H__
28 #define HIDAPI_H__
29 
30 #include <wchar.h>
31 
32 #if defined(_WIN32) && !defined(NAMESPACE) && (0) /* SDL: don't export hidapi syms */
33  #define HID_API_EXPORT __declspec(dllexport)
34  #define HID_API_CALL
35 #else
36  #define HID_API_EXPORT /**< API export macro */
37  #define HID_API_CALL /**< API call macro */
38 #endif
39 
40 #define HID_API_EXPORT_CALL HID_API_EXPORT HID_API_CALL /**< API export and call macro*/
41 
42 #if defined(__cplusplus) && !defined(NAMESPACE)
43 extern "C" {
44 #endif
45 #ifdef NAMESPACE
46 namespace NAMESPACE {
47 #endif
48 
49  struct hid_device_;
50  typedef struct hid_device_ hid_device; /**< opaque hidapi structure */
51 
52  /** hidapi info structure */
53  struct hid_device_info {
54  /** Platform-specific device path */
55  char *path;
56  /** Device Vendor ID */
57  unsigned short vendor_id;
58  /** Device Product ID */
59  unsigned short product_id;
60  /** Serial Number */
61  wchar_t *serial_number;
62  /** Device Release Number in binary-coded decimal,
63  also known as Device Version Number */
64  unsigned short release_number;
65  /** Manufacturer String */
67  /** Product string */
68  wchar_t *product_string;
69  /** Usage Page for this Device/Interface
70  (Windows/Mac only). */
71  unsigned short usage_page;
72  /** Usage for this Device/Interface
73  (Windows/Mac only).*/
74  unsigned short usage;
75  /** The USB interface which this logical device
76  represents. Valid on both Linux implementations
77  in all cases, and valid on the Windows implementation
78  only if the device contains more than one interface. */
80 
81  /** Pointer to the next device */
83  };
84 
85 
86  /** @brief Initialize the HIDAPI library.
87 
88  This function initializes the HIDAPI library. Calling it is not
89  strictly necessary, as it will be called automatically by
90  hid_enumerate() and any of the hid_open_*() functions if it is
91  needed. This function should be called at the beginning of
92  execution however, if there is a chance of HIDAPI handles
93  being opened by different threads simultaneously.
94 
95  @ingroup API
96 
97  @returns
98  This function returns 0 on success and -1 on error.
99  */
101 
102  /** @brief Finalize the HIDAPI library.
103 
104  This function frees all of the static data associated with
105  HIDAPI. It should be called at the end of execution to avoid
106  memory leaks.
107 
108  @ingroup API
109 
110  @returns
111  This function returns 0 on success and -1 on error.
112  */
114 
115  /** @brief Enumerate the HID Devices.
116 
117  This function returns a linked list of all the HID devices
118  attached to the system which match vendor_id and product_id.
119  If @p vendor_id is set to 0 then any vendor matches.
120  If @p product_id is set to 0 then any product matches.
121  If @p vendor_id and @p product_id are both set to 0, then
122  all HID devices will be returned.
123 
124  @ingroup API
125  @param vendor_id The Vendor ID (VID) of the types of device
126  to open.
127  @param product_id The Product ID (PID) of the types of
128  device to open.
129 
130  @returns
131  This function returns a pointer to a linked list of type
132  struct #hid_device, containing information about the HID devices
133  attached to the system, or NULL in the case of failure. Free
134  this linked list by calling hid_free_enumeration().
135  */
136  struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id);
137 
138  /** @brief Free an enumeration Linked List
139 
140  This function frees a linked list created by hid_enumerate().
141 
142  @ingroup API
143  @param devs Pointer to a list of struct_device returned from
144  hid_enumerate().
145  */
147 
148  /** @brief Open a HID device using a Vendor ID (VID), Product ID
149  (PID) and optionally a serial number.
150 
151  If @p serial_number is NULL, the first device with the
152  specified VID and PID is opened.
153 
154  @ingroup API
155  @param vendor_id The Vendor ID (VID) of the device to open.
156  @param product_id The Product ID (PID) of the device to open.
157  @param serial_number The Serial Number of the device to open
158  (Optionally NULL).
159 
160  @returns
161  This function returns a pointer to a #hid_device object on
162  success or NULL on failure.
163  */
164  HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number);
165 
166  /** @brief Open a HID device by its path name.
167 
168  The path name be determined by calling hid_enumerate(), or a
169  platform-specific path name can be used (eg: /dev/hidraw0 on
170  Linux).
171 
172  @ingroup API
173  @param path The path name of the device to open
174 
175  @returns
176  This function returns a pointer to a #hid_device object on
177  success or NULL on failure.
178  */
179  HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path, int bExclusive /* = false */);
180 
181  /** @brief Write an Output report to a HID device.
182 
183  The first byte of @p data[] must contain the Report ID. For
184  devices which only support a single report, this must be set
185  to 0x0. The remaining bytes contain the report data. Since
186  the Report ID is mandatory, calls to hid_write() will always
187  contain one more byte than the report contains. For example,
188  if a hid report is 16 bytes long, 17 bytes must be passed to
189  hid_write(), the Report ID (or 0x0, for devices with a
190  single report), followed by the report data (16 bytes). In
191  this example, the length passed in would be 17.
192 
193  hid_write() will send the data on the first OUT endpoint, if
194  one exists. If it does not, it will send the data through
195  the Control Endpoint (Endpoint 0).
196 
197  @ingroup API
198  @param device A device handle returned from hid_open().
199  @param data The data to send, including the report number as
200  the first byte.
201  @param length The length in bytes of the data to send.
202 
203  @returns
204  This function returns the actual number of bytes written and
205  -1 on error.
206  */
207  int HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned char *data, size_t length);
208 
209  /** @brief Read an Input report from a HID device with timeout.
210 
211  Input reports are returned
212  to the host through the INTERRUPT IN endpoint. The first byte will
213  contain the Report number if the device uses numbered reports.
214 
215  @ingroup API
216  @param device A device handle returned from hid_open().
217  @param data A buffer to put the read data into.
218  @param length The number of bytes to read. For devices with
219  multiple reports, make sure to read an extra byte for
220  the report number.
221  @param milliseconds timeout in milliseconds or -1 for blocking wait.
222 
223  @returns
224  This function returns the actual number of bytes read and
225  -1 on error. If no packet was available to be read within
226  the timeout period, this function returns 0.
227  */
228  int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *device, unsigned char *data, size_t length, int milliseconds);
229 
230  /** @brief Read an Input report from a HID device.
231 
232  Input reports are returned
233  to the host through the INTERRUPT IN endpoint. The first byte will
234  contain the Report number if the device uses numbered reports.
235 
236  @ingroup API
237  @param device A device handle returned from hid_open().
238  @param data A buffer to put the read data into.
239  @param length The number of bytes to read. For devices with
240  multiple reports, make sure to read an extra byte for
241  the report number.
242 
243  @returns
244  This function returns the actual number of bytes read and
245  -1 on error. If no packet was available to be read and
246  the handle is in non-blocking mode, this function returns 0.
247  */
248  int HID_API_EXPORT HID_API_CALL hid_read(hid_device *device, unsigned char *data, size_t length);
249 
250  /** @brief Set the device handle to be non-blocking.
251 
252  In non-blocking mode calls to hid_read() will return
253  immediately with a value of 0 if there is no data to be
254  read. In blocking mode, hid_read() will wait (block) until
255  there is data to read before returning.
256 
257  Nonblocking can be turned on and off at any time.
258 
259  @ingroup API
260  @param device A device handle returned from hid_open().
261  @param nonblock enable or not the nonblocking reads
262  - 1 to enable nonblocking
263  - 0 to disable nonblocking.
264 
265  @returns
266  This function returns 0 on success and -1 on error.
267  */
269 
270  /** @brief Send a Feature report to the device.
271 
272  Feature reports are sent over the Control endpoint as a
273  Set_Report transfer. The first byte of @p data[] must
274  contain the Report ID. For devices which only support a
275  single report, this must be set to 0x0. The remaining bytes
276  contain the report data. Since the Report ID is mandatory,
277  calls to hid_send_feature_report() will always contain one
278  more byte than the report contains. For example, if a hid
279  report is 16 bytes long, 17 bytes must be passed to
280  hid_send_feature_report(): the Report ID (or 0x0, for
281  devices which do not use numbered reports), followed by the
282  report data (16 bytes). In this example, the length passed
283  in would be 17.
284 
285  @ingroup API
286  @param device A device handle returned from hid_open().
287  @param data The data to send, including the report number as
288  the first byte.
289  @param length The length in bytes of the data to send, including
290  the report number.
291 
292  @returns
293  This function returns the actual number of bytes written and
294  -1 on error.
295  */
296  int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *device, const unsigned char *data, size_t length);
297 
298  /** @brief Get a feature report from a HID device.
299 
300  Set the first byte of @p data[] to the Report ID of the
301  report to be read. Make sure to allow space for this
302  extra byte in @p data[]. Upon return, the first byte will
303  still contain the Report ID, and the report data will
304  start in data[1].
305 
306  @ingroup API
307  @param device A device handle returned from hid_open().
308  @param data A buffer to put the read data into, including
309  the Report ID. Set the first byte of @p data[] to the
310  Report ID of the report to be read, or set it to zero
311  if your device does not use numbered reports.
312  @param length The number of bytes to read, including an
313  extra byte for the report ID. The buffer can be longer
314  than the actual report.
315 
316  @returns
317  This function returns the number of bytes read plus
318  one for the report ID (which is still in the first
319  byte), or -1 on error.
320  */
322 
323  /** @brief Close a HID device.
324 
325  @ingroup API
326  @param device A device handle returned from hid_open().
327  */
329 
330  /** @brief Get The Manufacturer String from a HID device.
331 
332  @ingroup API
333  @param device A device handle returned from hid_open().
334  @param string A wide string buffer to put the data into.
335  @param maxlen The length of the buffer in multiples of wchar_t.
336 
337  @returns
338  This function returns 0 on success and -1 on error.
339  */
340  int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen);
341 
342  /** @brief Get The Product String from a HID device.
343 
344  @ingroup API
345  @param device A device handle returned from hid_open().
346  @param string A wide string buffer to put the data into.
347  @param maxlen The length of the buffer in multiples of wchar_t.
348 
349  @returns
350  This function returns 0 on success and -1 on error.
351  */
352  int HID_API_EXPORT_CALL hid_get_product_string(hid_device *device, wchar_t *string, size_t maxlen);
353 
354  /** @brief Get The Serial Number String from a HID device.
355 
356  @ingroup API
357  @param device A device handle returned from hid_open().
358  @param string A wide string buffer to put the data into.
359  @param maxlen The length of the buffer in multiples of wchar_t.
360 
361  @returns
362  This function returns 0 on success and -1 on error.
363  */
364  int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *device, wchar_t *string, size_t maxlen);
365 
366  /** @brief Get a string from a HID device, based on its string index.
367 
368  @ingroup API
369  @param device A device handle returned from hid_open().
370  @param string_index The index of the string to get.
371  @param string A wide string buffer to put the data into.
372  @param maxlen The length of the buffer in multiples of wchar_t.
373 
374  @returns
375  This function returns 0 on success and -1 on error.
376  */
377  int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *device, int string_index, wchar_t *string, size_t maxlen);
378 
379  /** @brief Get a string describing the last error which occurred.
380 
381  @ingroup API
382  @param device A device handle returned from hid_open().
383 
384  @returns
385  This function returns a string containing the last error
386  which occurred or NULL if none has occurred.
387  */
389 
390 #if defined(__cplusplus) && !defined(NAMESPACE)
391 }
392 #endif
393 #ifdef NAMESPACE
394 }
395 #endif
396 
397 #endif
398 
hid_error
const HID_API_EXPORT wchar_t *HID_API_CALL hid_error(hid_device *device)
Get a string describing the last error which occurred.
hid_device_info::interface_number
int interface_number
Definition: hidapi.h:79
hid_write
int HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned char *data, size_t length)
Write an Output report to a HID device.
hid_send_feature_report
int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *device, const unsigned char *data, size_t length)
Send a Feature report to the device.
hid_exit
int HID_API_EXPORT HID_API_CALL hid_exit(void)
Finalize the HIDAPI library.
hid_close
void HID_API_EXPORT HID_API_CALL hid_close(hid_device *device)
Close a HID device.
hid_free_enumeration
void HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hid_device_info *devs)
Free an enumeration Linked List.
hid_init
int HID_API_EXPORT HID_API_CALL hid_init(void)
Initialize the HIDAPI library.
HID_API_EXPORT_CALL
#define HID_API_EXPORT_CALL
Definition: hidapi.h:40
hid_get_indexed_string
int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *device, int string_index, wchar_t *string, size_t maxlen)
Get a string from a HID device, based on its string index.
path
GLsizei const GLchar *const * path
Definition: SDL_opengl_glext.h:3730
hid_device_info::manufacturer_string
wchar_t * manufacturer_string
Definition: hidapi.h:66
length
GLuint GLsizei GLsizei * length
Definition: SDL_opengl_glext.h:669
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.
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
hid_device_info::next
struct hid_device_info * next
Definition: hidapi.h:82
hid_device_info
Definition: hidapi.h:53
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.
hid_device_info::product_id
unsigned short product_id
Definition: hidapi.h:59
hid_read_timeout
int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *device, unsigned char *data, size_t length, int milliseconds)
Read an Input report from a HID device with timeout.
hid_device_info::serial_number
wchar_t * serial_number
Definition: hidapi.h:61
hid_device_info::release_number
unsigned short release_number
Definition: hidapi.h:64
hid_open_path
HID_API_EXPORT hid_device *HID_API_CALL hid_open_path(const char *path, int bExclusive)
Open a HID device by its path name.
hid_device_info::usage
unsigned short usage
Definition: hidapi.h:74
hid_get_serial_number_string
int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *device, wchar_t *string, size_t maxlen)
Get The Serial Number String from a HID device.
HID_API_CALL
#define HID_API_CALL
Definition: hidapi.h:37
hid_device_info::usage_page
unsigned short usage_page
Definition: hidapi.h:71
hid_get_feature_report
int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *device, unsigned char *data, size_t length)
Get a feature report from a HID device.
hid_set_nonblocking
int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *device, int nonblock)
Set the device handle to be non-blocking.
hid_read
int HID_API_EXPORT HID_API_CALL hid_read(hid_device *device, unsigned char *data, size_t length)
Read an Input report from a HID device.
hid_get_product_string
int HID_API_EXPORT_CALL hid_get_product_string(hid_device *device, wchar_t *string, size_t maxlen)
Get The Product String from a HID device.
hid_device
struct hid_device_ hid_device
Definition: hidapi.h:50
NAMESPACE
#define NAMESPACE
Definition: hidusb.cpp:2
hid_device_info::path
char * path
Definition: hidapi.h:55
device
static SDL_AudioDeviceID device
Definition: loopwave.c:37
hid_get_manufacturer_string
int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen)
Get The Manufacturer String from a HID device.
hid_device_info::vendor_id
unsigned short vendor_id
Definition: hidapi.h:57
hid_device_info::product_string
wchar_t * product_string
Definition: hidapi.h:68
HID_API_EXPORT
#define HID_API_EXPORT
Definition: hidapi.h:36