SDL  2.0
hid.c
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  Linux Version - 6/2/2009
10 
11  Copyright 2009, All Rights Reserved.
12 
13  At the discretion of the user of this library,
14  this software may be licensed under the terms of the
15  GNU General Public License v3, a BSD-Style license, or the
16  original HIDAPI license as outlined in the LICENSE.txt,
17  LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
18  files located at the root of the source distribution.
19  These files may also be found in the public source
20  code repository located at:
21  http://github.com/signal11/hidapi .
22 ********************************************************/
23 #include "../../SDL_internal.h"
24 
25 #ifdef SDL_JOYSTICK_HIDAPI
26 
27 #ifndef _GNU_SOURCE
28 #define _GNU_SOURCE /* needed for wcsdup() before glibc 2.10 */
29 #endif
30 
31 /* C */
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <locale.h>
36 #include <errno.h>
37 
38 /* Unix */
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <sys/ioctl.h>
43 #include <sys/utsname.h>
44 #include <fcntl.h>
45 #include <poll.h>
46 
47 /* Linux */
48 #include <linux/hidraw.h>
49 #include <linux/version.h>
50 #include <linux/input.h>
51 #include <libudev.h>
52 
53 #include "hidapi.h"
54 
55 #ifdef NAMESPACE
56 namespace NAMESPACE
57 {
58 #endif
59 
60 /* Definitions from linux/hidraw.h. Since these are new, some distros
61  may not have header files which contain them. */
62 #ifndef HIDIOCSFEATURE
63 #define HIDIOCSFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x06, len)
64 #endif
65 #ifndef HIDIOCGFEATURE
66 #define HIDIOCGFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x07, len)
67 #endif
68 
69 /* USB HID device property names */
70 const char *device_string_names[] = {
71  "manufacturer",
72  "product",
73  "serial",
74 };
75 
76 /* Symbolic names for the properties above */
77 enum device_string_id {
78  DEVICE_STRING_MANUFACTURER,
79  DEVICE_STRING_PRODUCT,
80  DEVICE_STRING_SERIAL,
81 
82  DEVICE_STRING_COUNT,
83 };
84 
85 struct hid_device_ {
86  int device_handle;
87  int blocking;
88  int uses_numbered_reports;
89  int is_bluetooth;
90 };
91 
92 
93 static __u32 kernel_version = 0;
94 
95 static __u32 detect_kernel_version(void)
96 {
97  struct utsname name;
98  int major, minor, release;
99  int ret;
100 
101  uname(&name);
102  ret = sscanf(name.release, "%d.%d.%d", &major, &minor, &release);
103  if (ret == 3) {
104  return KERNEL_VERSION(major, minor, release);
105  }
106 
107  ret = sscanf(name.release, "%d.%d", &major, &minor);
108  if (ret == 2) {
109  return KERNEL_VERSION(major, minor, 0);
110  }
111 
112  printf("Couldn't determine kernel version from version string \"%s\"\n", name.release);
113  return 0;
114 }
115 
116 static hid_device *new_hid_device(void)
117 {
118  hid_device *dev = (hid_device *)calloc(1, sizeof(hid_device));
119  dev->device_handle = -1;
120  dev->blocking = 1;
121  dev->uses_numbered_reports = 0;
122  dev->is_bluetooth = 0;
123 
124  return dev;
125 }
126 
127 
128 /* The caller must free the returned string with free(). */
129 static wchar_t *utf8_to_wchar_t(const char *utf8)
130 {
131  wchar_t *ret = NULL;
132 
133  if (utf8) {
134  size_t wlen = mbstowcs(NULL, utf8, 0);
135  if ((size_t) -1 == wlen) {
136  return wcsdup(L"");
137  }
138  ret = (wchar_t *)calloc(wlen+1, sizeof(wchar_t));
139  mbstowcs(ret, utf8, wlen+1);
140  ret[wlen] = 0x0000;
141  }
142 
143  return ret;
144 }
145 
146 /* Get an attribute value from a udev_device and return it as a whar_t
147  string. The returned string must be freed with free() when done.*/
148 static wchar_t *copy_udev_string(struct udev_device *dev, const char *udev_name)
149 {
150  return utf8_to_wchar_t(udev_device_get_sysattr_value(dev, udev_name));
151 }
152 
153 /* uses_numbered_reports() returns 1 if report_descriptor describes a device
154  which contains numbered reports. */
155 static int uses_numbered_reports(__u8 *report_descriptor, __u32 size) {
156  unsigned int i = 0;
157  int size_code;
158  int data_len, key_size;
159 
160  while (i < size) {
161  int key = report_descriptor[i];
162 
163  /* Check for the Report ID key */
164  if (key == 0x85/*Report ID*/) {
165  /* This device has a Report ID, which means it uses
166  numbered reports. */
167  return 1;
168  }
169 
170  //printf("key: %02hhx\n", key);
171 
172  if ((key & 0xf0) == 0xf0) {
173  /* This is a Long Item. The next byte contains the
174  length of the data section (value) for this key.
175  See the HID specification, version 1.11, section
176  6.2.2.3, titled "Long Items." */
177  if (i+1 < size)
178  data_len = report_descriptor[i+1];
179  else
180  data_len = 0; /* malformed report */
181  key_size = 3;
182  }
183  else {
184  /* This is a Short Item. The bottom two bits of the
185  key contain the size code for the data section
186  (value) for this key. Refer to the HID
187  specification, version 1.11, section 6.2.2.2,
188  titled "Short Items." */
189  size_code = key & 0x3;
190  switch (size_code) {
191  case 0:
192  case 1:
193  case 2:
194  data_len = size_code;
195  break;
196  case 3:
197  data_len = 4;
198  break;
199  default:
200  /* Can't ever happen since size_code is & 0x3 */
201  data_len = 0;
202  break;
203  };
204  key_size = 1;
205  }
206 
207  /* Skip over this key and it's associated data */
208  i += data_len + key_size;
209  }
210 
211  /* Didn't find a Report ID key. Device doesn't use numbered reports. */
212  return 0;
213 }
214 
215 /*
216  * The caller is responsible for free()ing the (newly-allocated) character
217  * strings pointed to by serial_number_utf8 and product_name_utf8 after use.
218  */
219 static int
220 parse_uevent_info(const char *uevent, int *bus_type,
221  unsigned short *vendor_id, unsigned short *product_id,
222  char **serial_number_utf8, char **product_name_utf8)
223 {
224  char *tmp = strdup(uevent);
225  char *saveptr = NULL;
226  char *line;
227  char *key;
228  char *value;
229 
230  int found_id = 0;
231  int found_serial = 0;
232  int found_name = 0;
233 
234  line = strtok_r(tmp, "\n", &saveptr);
235  while (line != NULL) {
236  /* line: "KEY=value" */
237  key = line;
238  value = strchr(line, '=');
239  if (!value) {
240  goto next_line;
241  }
242  *value = '\0';
243  value++;
244 
245  if (strcmp(key, "HID_ID") == 0) {
246  /**
247  * type vendor product
248  * HID_ID=0003:000005AC:00008242
249  **/
250  int ret = sscanf(value, "%x:%hx:%hx", bus_type, vendor_id, product_id);
251  if (ret == 3) {
252  found_id = 1;
253  }
254  } else if (strcmp(key, "HID_NAME") == 0) {
255  /* The caller has to free the product name */
256  *product_name_utf8 = strdup(value);
257  found_name = 1;
258  } else if (strcmp(key, "HID_UNIQ") == 0) {
259  /* The caller has to free the serial number */
260  *serial_number_utf8 = strdup(value);
261  found_serial = 1;
262  }
263 
264 next_line:
265  line = strtok_r(NULL, "\n", &saveptr);
266  }
267 
268  free(tmp);
269  return (found_id && found_name && found_serial);
270 }
271 
272 static int is_bluetooth(hid_device *dev)
273 {
274  struct udev *udev;
275  struct udev_device *udev_dev, *hid_dev;
276  struct stat s;
277  int ret = -1;
278 
279  /* Create the udev object */
280  udev = udev_new();
281  if (!udev) {
282  printf("Can't create udev\n");
283  return -1;
284  }
285 
286  /* Get the dev_t (major/minor numbers) from the file handle. */
287  ret = fstat(dev->device_handle, &s);
288  if (-1 == ret) {
289  udev_unref(udev);
290  return ret;
291  }
292 
293  /* Open a udev device from the dev_t. 'c' means character device. */
294  udev_dev = udev_device_new_from_devnum(udev, 'c', s.st_rdev);
295  if (udev_dev) {
296  hid_dev = udev_device_get_parent_with_subsystem_devtype(
297  udev_dev,
298  "hid",
299  NULL);
300  if (hid_dev) {
301  unsigned short dev_vid;
302  unsigned short dev_pid;
303  int bus_type;
304  char *serial_number_utf8 = NULL;
305  char *product_name_utf8 = NULL;
306 
307  ret = parse_uevent_info(
308  udev_device_get_sysattr_value(hid_dev, "uevent"),
309  &bus_type,
310  &dev_vid,
311  &dev_pid,
312  &serial_number_utf8,
313  &product_name_utf8);
314  free(serial_number_utf8);
315  free(product_name_utf8);
316 
317  ret = (bus_type == BUS_BLUETOOTH);
318 
319  /* hid_dev doesn't need to be (and can't be) unref'd.
320  I'm not sure why, but it'll throw double-free() errors. */
321  }
322  udev_device_unref(udev_dev);
323  }
324 
325  udev_unref(udev);
326 
327  return ret;
328 }
329 
330 
331 static int get_device_string(hid_device *dev, enum device_string_id key, wchar_t *string, size_t maxlen)
332 {
333  struct udev *udev;
334  struct udev_device *udev_dev, *parent, *hid_dev;
335  struct stat s;
336  int ret = -1;
337  char *serial_number_utf8 = NULL;
338  char *product_name_utf8 = NULL;
339  char *tmp;
340 
341  /* Create the udev object */
342  udev = udev_new();
343  if (!udev) {
344  printf("Can't create udev\n");
345  return -1;
346  }
347 
348  /* Get the dev_t (major/minor numbers) from the file handle. */
349  ret = fstat(dev->device_handle, &s);
350  if (-1 == ret) {
351  udev_unref(udev);
352  return ret;
353  }
354  /* Open a udev device from the dev_t. 'c' means character device. */
355  udev_dev = udev_device_new_from_devnum(udev, 'c', s.st_rdev);
356  if (udev_dev) {
357  hid_dev = udev_device_get_parent_with_subsystem_devtype(
358  udev_dev,
359  "hid",
360  NULL);
361  if (hid_dev) {
362  unsigned short dev_vid;
363  unsigned short dev_pid;
364  int bus_type;
365  size_t retm;
366 
367  ret = parse_uevent_info(
368  udev_device_get_sysattr_value(hid_dev, "uevent"),
369  &bus_type,
370  &dev_vid,
371  &dev_pid,
372  &serial_number_utf8,
373  &product_name_utf8);
374 
375  if (bus_type == BUS_BLUETOOTH) {
376  switch (key) {
377  case DEVICE_STRING_MANUFACTURER:
378  wcsncpy(string, L"", maxlen);
379  ret = 0;
380  break;
381  case DEVICE_STRING_PRODUCT:
382  retm = mbstowcs(string, product_name_utf8, maxlen);
383  ret = (retm == (size_t)-1)? -1: 0;
384  break;
385  case DEVICE_STRING_SERIAL:
386  /* Bluetooth serial numbers are often the bluetooth device address
387  and we want that with the colons stripped out, which is the correct
388  serial number for PS4 controllers
389  */
390  while ((tmp = strchr(serial_number_utf8, ':')) != NULL) {
391  memmove(tmp, tmp+1, strlen(tmp));
392  }
393  retm = mbstowcs(string, serial_number_utf8, maxlen);
394  ret = (retm == (size_t)-1)? -1: 0;
395  break;
396  case DEVICE_STRING_COUNT:
397  default:
398  ret = -1;
399  break;
400  }
401  }
402  else {
403  /* This is a USB device. Find its parent USB Device node. */
404  parent = udev_device_get_parent_with_subsystem_devtype(
405  udev_dev,
406  "usb",
407  "usb_device");
408  if (parent) {
409  const char *str;
410  const char *key_str = NULL;
411 
412  if (key >= 0 && key < DEVICE_STRING_COUNT) {
413  key_str = device_string_names[key];
414  } else {
415  ret = -1;
416  goto end;
417  }
418 
419  str = udev_device_get_sysattr_value(parent, key_str);
420  if (str) {
421  /* Convert the string from UTF-8 to wchar_t */
422  retm = mbstowcs(string, str, maxlen);
423  ret = (retm == (size_t)-1)? -1: 0;
424  goto end;
425  }
426  }
427  }
428  }
429  }
430 
431 end:
432  free(serial_number_utf8);
433  free(product_name_utf8);
434 
435  udev_device_unref(udev_dev);
436  /* parent and hid_dev don't need to be (and can't be) unref'd.
437  I'm not sure why, but they'll throw double-free() errors. */
438  udev_unref(udev);
439 
440  return ret;
441 }
442 
443 int HID_API_EXPORT hid_init(void)
444 {
445  const char *locale;
446 
447  /* Set the locale if it's not set. */
448  locale = setlocale(LC_CTYPE, NULL);
449  if (!locale)
450  setlocale(LC_CTYPE, "");
451 
452  kernel_version = detect_kernel_version();
453 
454  return 0;
455 }
456 
457 int HID_API_EXPORT hid_exit(void)
458 {
459  /* Nothing to do for this in the Linux/hidraw implementation. */
460  return 0;
461 }
462 
463 
464 struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, unsigned short product_id)
465 {
466  struct udev *udev;
467  struct udev_enumerate *enumerate;
468  struct udev_list_entry *devices, *dev_list_entry;
469 
470  struct hid_device_info *root = NULL; /* return object */
471  struct hid_device_info *cur_dev = NULL;
472  struct hid_device_info *prev_dev = NULL; /* previous device */
473 
474  hid_init();
475 
476  /* Create the udev object */
477  udev = udev_new();
478  if (!udev) {
479  printf("Can't create udev\n");
480  return NULL;
481  }
482 
483  /* Create a list of the devices in the 'hidraw' subsystem. */
484  enumerate = udev_enumerate_new(udev);
485  udev_enumerate_add_match_subsystem(enumerate, "hidraw");
486  udev_enumerate_scan_devices(enumerate);
487  devices = udev_enumerate_get_list_entry(enumerate);
488  /* For each item, see if it matches the vid/pid, and if so
489  create a udev_device record for it */
490  udev_list_entry_foreach(dev_list_entry, devices) {
491  const char *sysfs_path;
492  const char *dev_path;
493  const char *str;
494  struct udev_device *raw_dev; /* The device's hidraw udev node. */
495  struct udev_device *hid_dev; /* The device's HID udev node. */
496  struct udev_device *usb_dev; /* The device's USB udev node. */
497  struct udev_device *intf_dev; /* The device's interface (in the USB sense). */
498  unsigned short dev_vid;
499  unsigned short dev_pid;
500  char *serial_number_utf8 = NULL;
501  char *product_name_utf8 = NULL;
502  int bus_type;
503  int result;
504 
505  /* Get the filename of the /sys entry for the device
506  and create a udev_device object (dev) representing it */
507  sysfs_path = udev_list_entry_get_name(dev_list_entry);
508  raw_dev = udev_device_new_from_syspath(udev, sysfs_path);
509  dev_path = udev_device_get_devnode(raw_dev);
510 
511  hid_dev = udev_device_get_parent_with_subsystem_devtype(
512  raw_dev,
513  "hid",
514  NULL);
515 
516  if (!hid_dev) {
517  /* Unable to find parent hid device. */
518  goto next;
519  }
520 
521  result = parse_uevent_info(
522  udev_device_get_sysattr_value(hid_dev, "uevent"),
523  &bus_type,
524  &dev_vid,
525  &dev_pid,
526  &serial_number_utf8,
527  &product_name_utf8);
528 
529  if (!result) {
530  /* parse_uevent_info() failed for at least one field. */
531  goto next;
532  }
533 
534  if (bus_type != BUS_USB && bus_type != BUS_BLUETOOTH) {
535  /* We only know how to handle USB and BT devices. */
536  goto next;
537  }
538 
539  if (access(dev_path, R_OK|W_OK) != 0) {
540  /* We can't open this device, ignore it */
541  goto next;
542  }
543 
544  /* Check the VID/PID against the arguments */
545  if ((vendor_id == 0x0 || vendor_id == dev_vid) &&
546  (product_id == 0x0 || product_id == dev_pid)) {
547  struct hid_device_info *tmp;
548 
549  /* VID/PID match. Create the record. */
550  tmp = (struct hid_device_info *)malloc(sizeof(struct hid_device_info));
551  if (cur_dev) {
552  cur_dev->next = tmp;
553  }
554  else {
555  root = tmp;
556  }
557  prev_dev = cur_dev;
558  cur_dev = tmp;
559 
560  /* Fill out the record */
561  cur_dev->next = NULL;
562  cur_dev->path = dev_path? strdup(dev_path): NULL;
563 
564  /* VID/PID */
565  cur_dev->vendor_id = dev_vid;
566  cur_dev->product_id = dev_pid;
567 
568  /* Serial Number */
569  cur_dev->serial_number = utf8_to_wchar_t(serial_number_utf8);
570 
571  /* Release Number */
572  cur_dev->release_number = 0x0;
573 
574  /* Interface Number */
575  cur_dev->interface_number = -1;
576 
577  switch (bus_type) {
578  case BUS_USB:
579  /* The device pointed to by raw_dev contains information about
580  the hidraw device. In order to get information about the
581  USB device, get the parent device with the
582  subsystem/devtype pair of "usb"/"usb_device". This will
583  be several levels up the tree, but the function will find
584  it. */
585  usb_dev = udev_device_get_parent_with_subsystem_devtype(
586  raw_dev,
587  "usb",
588  "usb_device");
589 
590  if (!usb_dev) {
591  /* Free this device */
592  free(cur_dev->serial_number);
593  free(cur_dev->path);
594  free(cur_dev);
595 
596  /* Take it off the device list. */
597  if (prev_dev) {
598  prev_dev->next = NULL;
599  cur_dev = prev_dev;
600  }
601  else {
602  cur_dev = root = NULL;
603  }
604 
605  goto next;
606  }
607 
608  /* Manufacturer and Product strings */
609  cur_dev->manufacturer_string = copy_udev_string(usb_dev, device_string_names[DEVICE_STRING_MANUFACTURER]);
610  cur_dev->product_string = copy_udev_string(usb_dev, device_string_names[DEVICE_STRING_PRODUCT]);
611 
612  /* Release Number */
613  str = udev_device_get_sysattr_value(usb_dev, "bcdDevice");
614  cur_dev->release_number = (str)? strtol(str, NULL, 16): 0x0;
615 
616  /* Get a handle to the interface's udev node. */
617  intf_dev = udev_device_get_parent_with_subsystem_devtype(
618  raw_dev,
619  "usb",
620  "usb_interface");
621  if (intf_dev) {
622  str = udev_device_get_sysattr_value(intf_dev, "bInterfaceNumber");
623  cur_dev->interface_number = (str)? strtol(str, NULL, 16): -1;
624  }
625 
626  break;
627 
628  case BUS_BLUETOOTH:
629  /* Manufacturer and Product strings */
630  cur_dev->manufacturer_string = wcsdup(L"");
631  cur_dev->product_string = utf8_to_wchar_t(product_name_utf8);
632 
633  break;
634 
635  default:
636  /* Unknown device type - this should never happen, as we
637  * check for USB and Bluetooth devices above */
638  break;
639  }
640  }
641 
642  next:
643  free(serial_number_utf8);
644  free(product_name_utf8);
645  udev_device_unref(raw_dev);
646  /* hid_dev, usb_dev and intf_dev don't need to be (and can't be)
647  unref()d. It will cause a double-free() error. I'm not
648  sure why. */
649  }
650  /* Free the enumerator and udev objects. */
651  udev_enumerate_unref(enumerate);
652  udev_unref(udev);
653 
654  return root;
655 }
656 
658 {
659  struct hid_device_info *d = devs;
660  while (d) {
661  struct hid_device_info *next = d->next;
662  free(d->path);
663  free(d->serial_number);
665  free(d->product_string);
666  free(d);
667  d = next;
668  }
669 }
670 
671 hid_device * hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
672 {
673  struct hid_device_info *devs, *cur_dev;
674  const char *path_to_open = NULL;
675  hid_device *handle = NULL;
676 
677  devs = hid_enumerate(vendor_id, product_id);
678  cur_dev = devs;
679  while (cur_dev) {
680  if (cur_dev->vendor_id == vendor_id &&
681  cur_dev->product_id == product_id) {
682  if (serial_number) {
683  if (wcscmp(serial_number, cur_dev->serial_number) == 0) {
684  path_to_open = cur_dev->path;
685  break;
686  }
687  }
688  else {
689  path_to_open = cur_dev->path;
690  break;
691  }
692  }
693  cur_dev = cur_dev->next;
694  }
695 
696  if (path_to_open) {
697  /* Open the device */
698  handle = hid_open_path(path_to_open, 0);
699  }
700 
701  hid_free_enumeration(devs);
702 
703  return handle;
704 }
705 
706 hid_device * HID_API_EXPORT hid_open_path(const char *path, int bExclusive)
707 {
708  hid_device *dev = NULL;
709 
710  hid_init();
711 
712  dev = new_hid_device();
713 
714  /* OPEN HERE */
715  dev->device_handle = open(path, O_RDWR);
716 
717  /* If we have a good handle, return it. */
718  if (dev->device_handle > 0) {
719 
720  /* Get the report descriptor */
721  int res, desc_size = 0;
722  struct hidraw_report_descriptor rpt_desc;
723 
724  memset(&rpt_desc, 0x0, sizeof(rpt_desc));
725 
726  /* Get Report Descriptor Size */
727  res = ioctl(dev->device_handle, HIDIOCGRDESCSIZE, &desc_size);
728  if (res < 0)
729  perror("HIDIOCGRDESCSIZE");
730 
731 
732  /* Get Report Descriptor */
733  rpt_desc.size = desc_size;
734  res = ioctl(dev->device_handle, HIDIOCGRDESC, &rpt_desc);
735  if (res < 0) {
736  perror("HIDIOCGRDESC");
737  } else {
738  /* Determine if this device uses numbered reports. */
739  dev->uses_numbered_reports =
740  uses_numbered_reports(rpt_desc.value,
741  rpt_desc.size);
742  }
743 
744  dev->is_bluetooth = (is_bluetooth(dev) == 1);
745 
746  return dev;
747  }
748  else {
749  /* Unable to open any devices. */
750  free(dev);
751  return NULL;
752  }
753 }
754 
755 
756 int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length)
757 {
758  int bytes_written;
759 
760  bytes_written = write(dev->device_handle, data, length);
761 
762  return bytes_written;
763 }
764 
765 
766 int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
767 {
768  int bytes_read;
769 
770  if (milliseconds >= 0) {
771  /* Milliseconds is either 0 (non-blocking) or > 0 (contains
772  a valid timeout). In both cases we want to call poll()
773  and wait for data to arrive. Don't rely on non-blocking
774  operation (O_NONBLOCK) since some kernels don't seem to
775  properly report device disconnection through read() when
776  in non-blocking mode. */
777  int ret;
778  struct pollfd fds;
779 
780  fds.fd = dev->device_handle;
781  fds.events = POLLIN;
782  fds.revents = 0;
783  ret = poll(&fds, 1, milliseconds);
784  if (ret == -1 || ret == 0) {
785  /* Error or timeout */
786  return ret;
787  }
788  else {
789  /* Check for errors on the file descriptor. This will
790  indicate a device disconnection. */
791  if (fds.revents & (POLLERR | POLLHUP | POLLNVAL))
792  return -1;
793  }
794  }
795 
796  bytes_read = read(dev->device_handle, data, length);
797  if (bytes_read < 0 && (errno == EAGAIN || errno == EINPROGRESS))
798  bytes_read = 0;
799 
800  if (bytes_read >= 0 &&
801  kernel_version != 0 &&
802  kernel_version < KERNEL_VERSION(2,6,34) &&
803  dev->uses_numbered_reports) {
804  /* Work around a kernel bug. Chop off the first byte. */
805  memmove(data, data+1, bytes_read);
806  bytes_read--;
807  }
808 
809  return bytes_read;
810 }
811 
812 int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
813 {
814  return hid_read_timeout(dev, data, length, (dev->blocking)? -1: 0);
815 }
816 
817 int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
818 {
819  /* Do all non-blocking in userspace using poll(), since it looks
820  like there's a bug in the kernel in some versions where
821  read() will not return -1 on disconnection of the USB device */
822 
823  dev->blocking = !nonblock;
824  return 0; /* Success */
825 }
826 
827 
828 int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length)
829 {
830  int res;
831 
832  res = ioctl(dev->device_handle, HIDIOCSFEATURE(length), data);
833  if (res < 0)
834  perror("ioctl (SFEATURE)");
835 
836  return res;
837 }
838 
839 int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
840 {
841  int res;
842 
843  /* It looks like HIDIOCGFEATURE() on Bluetooth devices doesn't return the report number */
844  if (dev->is_bluetooth) {
845  data[1] = data[0];
846  ++data;
847  --length;
848  }
849  res = ioctl(dev->device_handle, HIDIOCGFEATURE(length), data);
850  if (res < 0)
851  perror("ioctl (GFEATURE)");
852  else if (dev->is_bluetooth)
853  ++res;
854 
855  return res;
856 }
857 
858 
859 void HID_API_EXPORT hid_close(hid_device *dev)
860 {
861  if (!dev)
862  return;
863  close(dev->device_handle);
864  free(dev);
865 }
866 
867 
868 int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *string, size_t maxlen)
869 {
870  return get_device_string(dev, DEVICE_STRING_MANUFACTURER, string, maxlen);
871 }
872 
873 int HID_API_EXPORT_CALL hid_get_product_string(hid_device *dev, wchar_t *string, size_t maxlen)
874 {
875  return get_device_string(dev, DEVICE_STRING_PRODUCT, string, maxlen);
876 }
877 
878 int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *string, size_t maxlen)
879 {
880  return get_device_string(dev, DEVICE_STRING_SERIAL, string, maxlen);
881 }
882 
883 int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
884 {
885  return -1;
886 }
887 
888 
889 HID_API_EXPORT const wchar_t * HID_API_CALL hid_error(hid_device *dev)
890 {
891  return NULL;
892 }
893 
894 #ifdef NAMESPACE
895 }
896 #endif
897 
898 #endif /* SDL_JOYSTICK_HIDAPI */
#define NAMESPACE
Definition: hidusb.cpp:2
HID_API_EXPORT const wchar_t *HID_API_CALL hid_error(hid_device *device)
Get a string describing the last error which occurred.
Definition: hid.cpp:1149
int interface_number
Definition: hidapi.h:79
GLuint64EXT * result
GLdouble s
Definition: SDL_opengl.h:2063
#define HID_API_EXPORT
Definition: hidapi.h:36
void HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hid_device_info *devs)
Free an enumeration Linked List.
Definition: hid.cpp:979
GLuint GLuint end
Definition: SDL_opengl.h:1571
#define memmove
Definition: SDL_qsort.c:59
SDL_EventEntry * free
Definition: SDL_events.c:84
#define memset
Definition: SDL_malloc.c:619
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
wchar_t * manufacturer_string
Definition: hidapi.h:66
char * path
Definition: hidapi.h:55
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.
Definition: hid.cpp:1028
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.
Definition: hid.cpp:1144
unsigned int size_t
GLuint GLint GLboolean GLint GLenum access
GLuint const GLchar * name
GLuint res
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.
Definition: hid.cpp:1040
struct hid_device_info * next
Definition: hidapi.h:82
int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *device, int nonblock)
Set the device handle to be non-blocking.
Definition: hid.cpp:1060
void HID_API_EXPORT HID_API_CALL hid_close(hid_device *device)
Close a HID device.
Definition: hid.cpp:1090
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.
Definition: hid.cpp:1122
GLuint64 key
Definition: gl2ext.h:2192
unsigned short product_id
Definition: hidapi.h:59
EGLImageKHR EGLint EGLint * handle
Definition: eglext.h:937
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 ** d
wchar_t * serial_number
Definition: hidapi.h:61
int hid_exit(void)
Finalize the HIDAPI library.
Definition: hid.cpp:1154
unsigned short vendor_id
Definition: hidapi.h:57
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...
Definition: hid.cpp:989
GLsizei const GLfloat * value
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.
Definition: hid.cpp:1078
wchar_t * product_string
Definition: hidapi.h:68
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.
Definition: hid.cpp:1133
GLsizeiptr size
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define HID_API_EXPORT_CALL
Definition: hidapi.h:40
#define NULL
Definition: begin_code.h:164
int hid_init(void)
Initialize the HIDAPI library.
Definition: hid.cpp:956
GLuint GLfloat x0
HID_API_EXPORT hid_device *HID_API_CALL hid_open_path(const char *path, int bExclusive)
Open a HID device by its path name.
Definition: hid.cpp:995
GLsizei const GLchar *const * path
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.
Definition: hid.cpp:1065
EGLImageKHR int * fds
Definition: eglext.h:947
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.
Definition: hid.cpp:1111
GLuint GLsizei GLsizei * length
EGLDeviceEXT * devices
Definition: eglext.h:621
struct hid_device_info HID_API_EXPORT *HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id)
Enumerate the HID Devices.
Definition: hid.cpp:961
#define HID_API_CALL
Definition: hidapi.h:37
#define malloc
Definition: SDL_qsort.c:47
unsigned short release_number
Definition: hidapi.h:64
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.
Definition: hid.cpp:1053