-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
197 lines (179 loc) · 3.82 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <linux/types.h>
#include <linux/input.h>
#include <linux/hidraw.h>
// Ugly hack to work around failing compilation on systems that don't yet populate new version of hidraw.h to userspace
#ifndef HIDIOCSFEATURE
#warning Please have your distro update the userspace kernel headers
#define HIDIOCSFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x06, len)
#define HIDIOCGFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x07, len)
#endif
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <linux/usbdevice_fs.h>
const char* bus_str(int bus)
{
switch (bus) {
case BUS_USB:
return "USB";
break;
case BUS_HIL:
return "HIL";
break;
case BUS_BLUETOOTH:
return "Bluetooth";
break;
case BUS_VIRTUAL:
return "Virtual";
break;
default:
return "Other";
break;
}
}
void WriteHID(char* devicePath)
{
int i, result;
char buf[256];
memset(buf, 0x0, sizeof(buf));
// Open the Device with non-blocking
int fd = open(devicePath, O_RDWR | O_NONBLOCK);
if (fd < 0)
{
//perror("Unable to open device");
return;
}
// Get Info
struct hidraw_devinfo info;
memset(&info, 0x0, sizeof(info));
result = ioctl(fd, HIDIOCGRAWINFO, &info);
if (result < 0)
{
perror("Failed: HIDIOCGRAWINFO");
goto DISPOSE;
}
else
{
// validate vendor & produce IDs
if (info.vendor != 0x0bd0 && info.product != 0x1901) goto DISPOSE;
printf("Found Gamepad HID device");
printf("\tbustype: %d (%s)\n", info.bustype, bus_str(info.bustype));
printf("\tvendor: 0x%04hx\n", info.vendor);
printf("\tproduct: 0x%04hx\n", info.product);
}
// Get Report Descriptor Size
int desc_size = 0;
result = ioctl(fd, HIDIOCGRDESCSIZE, &desc_size);
if (result < 0)
{
desc_size = 0;
perror("Failed: HIDIOCGRDESCSIZE");
//goto DISPOSE;
}
else
{
printf("Report Descriptor Size: %d\n", desc_size);
}
// Get Report Descriptor
if (desc_size > 0)
{
struct hidraw_report_descriptor rpt_desc;
memset(&rpt_desc, 0x0, sizeof(rpt_desc));
rpt_desc.size = desc_size;
result = ioctl(fd, HIDIOCGRDESC, &rpt_desc);
if (result < 0)
{
perror("Failed: HIDIOCGRDESC");
//goto DISPOSE;
}
else
{
printf("Report Descriptor:\n");
for (i = 0; i < rpt_desc.size; i++) printf("%hhx ", rpt_desc.value[i]);
puts("\n");
}
}
// Get Name
result = ioctl(fd, HIDIOCGRAWNAME(256), buf);
if (result < 0)
{
perror("Failed: HIDIOCGRAWNAME");
//goto DISPOSE;
}
else
{
printf("Raw Name: %s\n", buf);
}
// Get Physical Location
result = ioctl(fd, HIDIOCGRAWPHYS(256), buf);
if (result < 0)
{
perror("Failed: HIDIOCGRAWPHYS");
//goto DISPOSE;
}
else
{
printf("Raw Phys: %s\n", buf);
}
// send xpad mode change Report to the Device
memset(buf, 0x0, sizeof(buf));
i = 0;
buf[i++] = 15;// report id
buf[i++] = 0;
buf[i++] = 0;
buf[i++] = 60;
buf[i++] = 36;// we want to switch mode
buf[i++] = 1;// XInput mode
buf[i++] = 0;
buf[i++] = 0;
/* NOTE: gamepad mode options
Offline = 0,
XInput = 1,
DInput = 2,
MSI = 3,
Desktop = 4,
BIOS = 5,
Testing = 6
*/
printf("Sending %d bytes...\n", i);
result = write(fd, buf, i);
if (result < 0)
{
printf("Write mode failed: %d\n", errno);
}
else
{
printf("Xpad mode set: write() wrote %d bytes\n", result);
}
/*// Get a report from the device
sleep(1);
result = read(fd, buf, 256);
if (result < 0)
{
perror("Read mode response failed");
}
else
{
printf("Response read %d bytes:\n\t", result);
for (i = 0; i < result; i++) printf("%hhx ", buf[i]);
puts("\n");
}*/
DISPOSE:;
close(fd);
}
int main(int argc, char **argv)
{
for (int i = 0; i != 20; ++i)
{
char devicePath[64];
memset(devicePath, 0x0, sizeof(devicePath));
snprintf(devicePath, sizeof(devicePath), "/dev/hidraw%d", i);
WriteHID(devicePath);
}
}