]> mj.ucw.cz Git - misc.git/blob - hidtest.c
Comments...
[misc.git] / hidtest.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <fcntl.h>
5 #include <sys/ioctl.h>
6 #include <asm/types.h>
7 #include "/home/mj/linux/linux-2.6.27.4/include/linux/hiddev.h"
8
9 static void die(char *x)
10 {
11         fprintf(stderr, "hidtest: %s: %m\n", x);
12         exit(1);
13 }
14
15 int main(void)
16 {
17         int i, j;
18         int fd = open("/dev/.static/dev/usb/hiddev5", O_RDWR);
19         if (fd < 0) die("open");
20
21         int ver;
22         if (ioctl(fd, HIDIOCGVERSION, &ver) < 0)
23                 die("getversion");
24         printf("hiddev version %x\n", ver);
25
26         struct hiddev_devinfo di;
27         if (ioctl(fd, HIDIOCGDEVINFO, &di) < 0)
28                 die("gdevinfo");
29         printf("%d:%d:%d vendor %04x, product %04x, version %04x, num_appl %d\n",
30                 di.busnum, di.devnum, di.ifnum, di.vendor, di.product, di.version, di.num_applications);
31
32 #if 0
33         for (i=0; i<100; i++) {
34                 struct hiddev_string_descriptor ss;
35                 ss.index = 0;
36                 if (ioctl(fd, HIDIOCGSTRING, &ss) < 0)
37                         printf("S%d: ???\n", i);
38                 else
39                         printf("S%d: %s\n", i, ss.value);
40         }
41 #endif
42
43         for (i=0; i<di.num_applications; i++) {
44                 int appl = ioctl(fd, HIDIOCAPPLICATION, i);
45                 if (appl < 0) die("application");
46                 printf("Appl %d: %x\n", i, appl);
47         }
48
49         //ioctl(fd, HIDIOCINITREPORT, 0);
50
51         struct hiddev_report_info rinfo;
52         struct hiddev_field_info finfo;
53         struct hiddev_usage_ref uref;
54         rinfo.report_type = HID_REPORT_TYPE_INPUT;
55         rinfo.report_id = HID_REPORT_ID_FIRST;
56         int ret = ioctl(fd, HIDIOCGREPORTINFO, &rinfo);
57         while (ret >= 0) {
58                 printf("rinfo: type=%d, id=%d, fields=%d\n", rinfo.report_type, rinfo.report_id, rinfo.num_fields);
59                 for (i = 0; i < rinfo.num_fields; i++) { 
60                         finfo.report_type = rinfo.report_type;
61                         finfo.report_id = rinfo.report_id;
62                         finfo.field_index = i;
63                         ioctl(fd, HIDIOCGFIELDINFO, &finfo);
64                         printf("\tField %d:\n", i);
65                         printf("\t\tflags: %04x, phys: %d, log: %d, appl: %x, log=<%d,%d>, phys=<%d,%d>, unit=%d E%d\n",
66                                 finfo.flags, finfo.physical, finfo.logical, finfo.application,
67                                 finfo.logical_minimum, finfo.logical_maximum,
68                                 finfo.physical_minimum, finfo.physical_maximum,
69                                 finfo.unit, finfo.unit_exponent);
70                         for (j = 0; j < finfo.maxusage; j++) {
71                                 uref.report_type = finfo.report_type;
72                                 uref.report_id = finfo.report_id;
73                                 uref.field_index = i;
74                                 uref.usage_index = j;
75                                 ioctl(fd, HIDIOCGUCODE, &uref);
76                                 ioctl(fd, HIDIOCGUSAGE, &uref);
77                                 printf("\t\tUsage %d: code=%x, value=%d\n", j, uref.usage_code, uref.value);
78                         }
79                 }
80                 rinfo.report_id |= HID_REPORT_ID_NEXT;          /* FIXME: bug in hiddev.h, beware */
81                 ret = ioctl(fd, HIDIOCGREPORTINFO, &rinfo);
82         }
83
84         int f = HIDDEV_FLAG_UREF;
85         if (ioctl(fd, HIDIOCSFLAG, &f) < 0) die("sflag");
86         struct hiddev_usage_ref ev;
87         while (read(fd, &ev, sizeof(ev))) {
88                 printf("EV: %x = %d\n", ev.usage_code, ev.value);
89         }
90
91         return 0;
92 }