-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhid.c
489 lines (412 loc) · 15.5 KB
/
hid.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#include "hid.h"
#include "const.h"
#include "server.h"
#include "util.h"
#include "vec.h"
#include <dirent.h>
#include <fcntl.h>
#include <linux/input-event-codes.h>
#include <linux/input.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <time.h>
#include <unistd.h>
// List of ids of the currently known devices
static Vec known_devices;
// Queue of available devices, devices that can only be given to one client
static Vec available_devices;
// List of cloneable devices, devices that can be handed out to multiple clients
static Vec cloneable_devices;
// Mutex for devices
static pthread_mutex_t devices_mutex = PTHREAD_MUTEX_INITIALIZER;
// Condvar notified on devices update
static pthread_cond_t devices_cond = PTHREAD_COND_INITIALIZER;
// Mutex for devices
static pthread_mutex_t known_devices_mutex = PTHREAD_MUTEX_INITIALIZER;
static ServerConfig *config;
// uniqs are just hexadecimal numbers with colons in between each byte
uniq_t parse_uniq(char uniq[17]) {
uniq_t res = 0;
for (int i = 0; i < 17; i++) {
char c = uniq[i];
int digit;
if (c >= '0' && c <= '9')
digit = c - '0';
else if (c >= 'a' && c <= 'f')
digit = c - 'a' + 10;
else if (c >= 'A' && c <= 'F')
digit = c - 'A' + 10;
else
continue;
res <<= 4;
res += digit;
}
return res;
}
// Finish setup of a partially initialized device (set device_info and mapping)
void setup_device(PhysicalDevice *dev) {
dev->device_info.tag = DeviceTagInfo;
dev->device_info.abs.len = 0;
dev->device_info.rel.len = 0;
dev->device_info.key.len = 0;
for (int i = 0; i < ABS_CNT; i++)
dev->mapping.abs_indices[i] = -1;
for (int i = 0; i < REL_CNT; i++)
dev->mapping.rel_indices[i] = -1;
for (int i = 0; i < KEY_CNT; i++)
dev->mapping.key_indices[i] = -1;
uint8_t type_bits[EV_MAX] = {0};
uint8_t feat_bits[(KEY_MAX + 7) / 8] = {0};
ioctl(dev->event, EVIOCGBIT(0, EV_MAX), type_bits);
// Loop over all event types
for (int type = 0; type < EV_MAX; type++) {
// Ignore if the the device doesn't have any of this event type
if (!bit_set(type_bits, type)) {
continue;
}
// Clear feat_bits to only have the features of the current type
memset(feat_bits, 0, sizeof(feat_bits));
ioctl(dev->event, EVIOCGBIT(type, KEY_MAX), feat_bits);
// Loop over "instances" of type (i.e Each axis of a controller for EV_ABS)
for (int i = 0; i < KEY_MAX; i++) {
// "instances" don't have to be consecutive (this is why we do all this instead of just worrying
// about the count)
if (!bit_set(feat_bits, i)) {
continue;
}
if (type == EV_ABS) {
struct input_absinfo abs;
ioctl(dev->event, EVIOCGABS(i), &abs);
uint16_t index = dev->device_info.abs.len++;
Abs *dev_abs = &dev->device_info.abs.data[index];
dev_abs->min = abs.minimum;
dev_abs->max = abs.maximum;
dev_abs->fuzz = abs.fuzz;
dev_abs->flat = abs.flat;
dev_abs->res = abs.resolution;
dev_abs->id = i;
// Bidirectional mapping id <-> index
// We need this to avoid wasting space in packets because ids are sparse
dev->mapping.abs_indices[i] = index;
} else if (type == EV_REL) {
uint16_t index = dev->device_info.rel.len++;
dev->device_info.rel.data[index].id = i;
dev->mapping.rel_indices[i] = index;
} else if (type == EV_KEY) {
uint16_t index = dev->device_info.key.len++;
dev->device_info.key.data[index].id = i;
dev->mapping.key_indices[i] = index;
}
}
}
}
bool filter_event(int fd, char *event, ControllerFilter *filter, uniq_t uniq) {
if (filter->js) {
char device_path[64];
snprintf(device_path, 64, "/sys/class/input/%s/device", event);
DIR *device_dir = opendir(device_path);
struct dirent *device_dirent;
bool found = false;
while ((device_dirent = readdir(device_dir)) != NULL) {
if (device_dirent->d_type == DT_DIR && strncmp(device_dirent->d_name, "js", 2) == 0) {
found = true;
break;
}
}
closedir(device_dir);
if (!found) {
return false;
}
}
if (filter->name != NULL) {
char name[256] = {0};
ioctl(fd, EVIOCGNAME(256), name);
if (strcmp(name, filter->name) != 0) {
return false;
}
}
if (filter->uniq > 0 && uniq != filter->uniq) {
return false;
}
struct input_id ids;
ioctl(fd, EVIOCGID, &ids);
if (filter->vendor > 0 && filter->vendor != ids.vendor)
return false;
if (filter->product > 0 && filter->product != ids.product)
return false;
return true;
}
// Initialize vectors for polling
void poll_devices_init(void) {
known_devices = vec_of(uint64_t);
cloneable_devices = vec_of(Controller);
available_devices = vec_of(Controller);
}
// Find index of tag that matches any of the tags specified in the tags array (of length tag_count), returns -1 otherwise
static int match_tags(char *tag, char **tags, size_t tag_count) {
for (int i = 0; i < tag_count; i++) {
if (strcmp(tag, tags[i]) == 0) {
return i;
}
}
return -1;
}
// Block to get a device, this is thread safe
// stop: additional condition to check before doing anything,
// if the condition is ever found to be true the function will return immediately with a NULL pointer.
bool get_device(char **tags, size_t tag_count, bool *stop, Controller *res, uint8_t *ref_index) {
// Check if we can get one right away
pthread_mutex_lock(&devices_mutex);
while (1) {
if (*stop) {
pthread_mutex_unlock(&devices_mutex);
return false;
}
for (int i = 0; i < available_devices.len; i++) {
Controller *c = vec_get(&available_devices, i);
int index = match_tags(c->ctr.tag, tags, tag_count);
if (index >= 0) {
*ref_index = index;
*res = *c;
vec_remove(&available_devices, i, NULL);
pthread_mutex_unlock(&devices_mutex);
return true;
}
}
for (int i = 0; i < cloneable_devices.len; i++) {
Controller *c = vec_get(&cloneable_devices, i);
int index = match_tags(c->ctr.tag, tags, tag_count);
if (index >= 0) {
*ref_index = index;
*res = *c;
pthread_mutex_unlock(&devices_mutex);
return true;
}
}
// Wait on condvar until there's a device and we can unlock the mutex
pthread_cond_wait(&devices_cond, &devices_mutex);
}
}
// Return a device that isn't used anymore, this really only makes sense for non cloneable devices.
void return_device(Controller *c) {
// If device is cloneable there is nothing to return
if (c->ctr.duplicate) {
return;
}
pthread_mutex_lock(&devices_mutex);
vec_push(&available_devices, c);
// Signal that there are new devices
pthread_cond_broadcast(&devices_cond);
pthread_mutex_unlock(&devices_mutex);
}
// Forget about a broken device. This invalidates the reference to the controller
void forget_device(Controller *c) {
pthread_mutex_lock(&known_devices_mutex);
// If controller is cloneable we need to remove it from the cloneable list
if (c->ctr.duplicate) {
for (int i = 0; i < cloneable_devices.len; i++) {
Controller *d = *(Controller **)vec_get(&cloneable_devices, i);
if (d->dev.id == c->dev.id) {
vec_remove(&cloneable_devices, i, NULL);
break;
}
}
}
// Free the name if it was allocated
if (c->dev.name != NULL && c->dev.name != DEVICE_DEFAULT_NAME) {
printf("HID: Forgetting device '%s' (%lu)\n", c->dev.name, c->dev.id);
free(c->dev.name);
} else {
printf("HID: Forgetting device %lu\n", c->dev.id);
}
// try to close the file descriptor, they may be already closed if the device was unpugged.
close(c->dev.event);
close(c->dev.hidraw);
// Safely remove device from the known device list
for (int i = 0; i < known_devices.len; i++) {
uint64_t *id = vec_get(&known_devices, i);
if (*id == c->dev.id) {
vec_remove(&known_devices, i, NULL);
break;
}
}
pthread_mutex_unlock(&known_devices_mutex);
}
uint64_t parse_event_name(const char *event) { return atol(event + 5); }
// Find all available devices and pick up on new ones
void poll_devices(void) {
// loop over all entries of /sys/class/input
DIR *input_dir = opendir(FSROOT "/sys/class/input");
struct dirent *input;
while ((input = readdir(input_dir)) != NULL) {
// Ignore if the entry isn't a link or doesn't start with event
if (input->d_type != DT_LNK || strncmp(input->d_name, "event", 5) != 0) {
continue;
}
PhysicalDevice dev;
dev.hidraw = -1;
dev.uniq = 0;
dev.id = parse_event_name(input->d_name);
// Open /dev/input/eventXX
{
char event_path[64];
snprintf(event_path, 64, FSROOT "/dev/input/%s", input->d_name);
dev.event = open(event_path, O_RDONLY);
if (dev.event < 0) { // Ignore device if we couldn't open
continue;
}
}
// Try to get the name, default to DEFAULT_NAME if impossible
char *name;
{
static char name_buf[256] = {0};
if (ioctl(dev.event, EVIOCGNAME(256), name_buf) >= 0) {
name = name_buf;
} else {
name = (char *)DEVICE_DEFAULT_NAME;
}
}
// Try to get uniq, drop device if we can't
{
char uniq_str[17] = {0};
ioctl(dev.event, EVIOCGUNIQ(17), uniq_str);
dev.uniq = parse_uniq(uniq_str);
}
// Used for linear searches
bool found;
// Filter devices according server config
ServerConfigController *ctr;
{
found = false;
for (int i = 0; i < config->controller_count; i++) {
ctr = &config->controllers[i];
if (filter_event(dev.event, input->d_name, &ctr->filter, dev.uniq)) {
found = true;
break;
}
}
if (!found) {
goto skip;
}
}
// Check if we already know of this device
{
found = false;
pthread_mutex_lock(&known_devices_mutex);
for (int i = 0; i < known_devices.len; i++) {
uint64_t *id = vec_get(&known_devices, i);
if (*id == dev.id) {
found = true;
break;
}
}
pthread_mutex_unlock(&known_devices_mutex);
if (found) { // Device isn't new
goto skip;
}
}
// Look for hidraw if the device should have one (Dualshock 4 only, with ps4_hidraw property set)
if (ctr->ps4_hidraw) {
// Attempt to find the path
char hidraw_path[64];
{
char hidraw_dir_path[256];
snprintf(hidraw_dir_path, 256, FSROOT "/sys/class/input/%s/device/device/hidraw", input->d_name);
DIR *hidraw_dir = opendir(hidraw_dir_path);
struct dirent *hidraw = NULL;
while ((hidraw = readdir(hidraw_dir)) != NULL) {
if (strncmp(hidraw->d_name, "hidraw", 6) == 0) {
break;
}
}
if (hidraw == NULL) {
printf("HID: Couldn't get hidraw of %s", input->d_name);
goto skip;
}
snprintf(hidraw_path, 64, FSROOT "/dev/%s", hidraw->d_name);
closedir(hidraw_dir);
}
// Try to open
dev.hidraw = open(hidraw_path, O_WRONLY);
if (dev.hidraw < 0) {
goto skip;
}
}
// Allocate for name (only now to avoid unecessary allocations)
if (name != DEVICE_DEFAULT_NAME) {
dev.name = malloc(256);
if (dev.name == NULL) {
dev.name = (char *)DEVICE_DEFAULT_NAME;
} else {
strcpy(dev.name, name);
}
}
// This code is only run if the device has passed all filters and requirements
{
setup_device(&dev);
Controller c = {.dev = dev, .ctr = *ctr};
pthread_mutex_lock(&known_devices_mutex);
vec_push(&known_devices, &c.dev.id);
pthread_mutex_unlock(&known_devices_mutex);
printf("HID: New device, %s [%s] (%s: %lu)\n", name, ctr->tag, input->d_name, dev.id);
if (ctr->duplicate) {
pthread_mutex_lock(&devices_mutex);
vec_push(&cloneable_devices, &c);
// Signal that there are new cloneable devices
pthread_cond_broadcast(&devices_cond);
pthread_mutex_unlock(&devices_mutex);
} else {
pthread_mutex_lock(&devices_mutex);
vec_push(&available_devices, &c);
// Signal that there are new devices
pthread_cond_broadcast(&devices_cond);
pthread_mutex_unlock(&devices_mutex);
}
}
// Continue here avoids running cleanup code
continue;
// close open file descriptor and continue
skip:
close(dev.event);
};
closedir(input_dir);
}
// "Execute" a MessageControllerState: set the led color, rumble and flash using the hidraw interface (Dualshock 4 only)
void apply_controller_state(Controller *c, DeviceControllerState *state) {
if (c->ctr.ps4_hidraw && c->dev.hidraw < 0) {
printf("HID: Trying to apply controller state on incompatible device (%lu)\n", c->dev.id);
return;
}
printf("HID: (%lu) Controller state: #%02x%02x%02x flash: (%d, %d) rumble: (%d, %d)\n", c->dev.id, state->led[0],
state->led[1], state->led[2], state->flash_on, state->flash_off, state->small_rumble, state->big_rumble);
uint8_t buf[32] = {0x05, 0xff, 0x00, 0x00};
buf[4] = state->small_rumble;
buf[5] = state->big_rumble;
buf[6] = state->led[0];
buf[7] = state->led[1];
buf[8] = state->led[2];
buf[9] = state->flash_on;
buf[10] = state->flash_off;
write(c->dev.hidraw, buf, 32);
if (state->flash_on == 0 && state->flash_off == 0) {
// May not be necessary
fsync(c->dev.hidraw);
// Send a second time, to reenable the led
write(c->dev.hidraw, buf, 32);
};
}
// Body of the hid thread
void *hid_thread(void *arg) {
printf("HID: start\n");
config = arg;
poll_devices_init();
while (1) {
poll_devices();
nanosleep(&config->poll_interval, NULL);
}
return NULL;
}