-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.c
678 lines (610 loc) · 21.2 KB
/
controller.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <libevdev/libevdev.h>
#include "controller.h"
#include "log.h"
#include "utils.h"
#define CONTROLLER_AXIS_MAX 32767
#define CONTROLLER_AXIS_MIN -32768
#define CONTROLLER_AXIS_ROUND 0.01f
#define CONTROLLER_RUMBLE_DURATION 500 // ms
#define DEVICE_PATH_SIZE 27
// Mapping buttons to their code
#define CONTROLLER_BUTTONS \
BUTTON(CONTROLLER_BUTTON_A, BTN_EAST) \
BUTTON(CONTROLLER_BUTTON_B, BTN_SOUTH) \
BUTTON(CONTROLLER_BUTTON_Y, BTN_NORTH) \
BUTTON(CONTROLLER_BUTTON_X, BTN_WEST) \
BUTTON(CONTROLLER_BUTTON_L, BTN_TL) \
BUTTON(CONTROLLER_BUTTON_R, BTN_TR) \
BUTTON(CONTROLLER_BUTTON_MINUS, BTN_SELECT) \
BUTTON(CONTROLLER_BUTTON_PLUS, BTN_START) \
BUTTON(CONTROLLER_BUTTON_HOME, BTN_MODE) \
BUTTON(CONTROLLER_BUTTON_LPAD, BTN_THUMBL) \
BUTTON(CONTROLLER_BUTTON_RPAD, BTN_THUMBR)
struct _Controller {
int fd;
struct libevdev *dev;
float hat_state[4];
bool grabbed;
bool is_rumbling;
int16_t rumble_effect_id;
uint64_t rumble_start;
};
/**
* Logging function use to log error from libevdev.
* See libevdev_device_log_func_t.
*/
static void controller_log_func(
const struct libevdev *dev,
enum libevdev_log_priority priority,
void *data,
const char *file,
int line,
const char *func,
const char *format,
va_list args
) {
(void)dev;
(void)data;
#ifdef PROD
(void)file;
(void)line;
#endif
if (priority == LIBEVDEV_LOG_ERROR) {
#ifndef PROD
fprintf(stderr, "%s:%d: ", file, line);
#endif
fprintf(stderr, "%s: error: ", func),
vfprintf(stderr, format, args);
return;
}
#ifndef PROD
if (priority == LIBEVDEV_LOG_DEBUG) {
printf("%s:%d: %s: debug: ", file, line, func);
vprintf(format, args);
return;
}
#endif
#ifndef PROD
printf("%s:%d: ", file, line);
#endif
printf("%s: info: ", func);
vprintf(format, args);
return;
}
/**
* Initialize a new libevdev device from the given fd.
*
* \param fd The file descriptor of the controller device.
* \param dev A pointer to a libevdev object pointer that will be set to the new
* device.
*
* \returns true on success, or false on failure.
*/
static bool controller_init_libevdev(const int fd, struct libevdev **dev) {
*dev = libevdev_new();
libevdev_set_device_log_function(
*dev,
controller_log_func,
#ifndef PROD
LIBEVDEV_LOG_DEBUG,
#else
LIBEVDEV_LOG_INFO,
#endif
NULL
);
const int err = libevdev_set_fd(*dev, fd);
if (err < 0) {
log_errorf("failed to initialize libevdev device: %s", strerror(-err));
return false;
}
return true;
}
/**
* Check if a device has the required events to be a controller.
*
* \param dev The device to check.
*
* \returns true if the device matchs the requirements to be a controller.
*/
static bool is_controller(struct libevdev *dev) {
return (
libevdev_has_event_type(dev, EV_ABS) &&
libevdev_has_event_code(dev, EV_ABS, ABS_X) &&
libevdev_has_event_code(dev, EV_ABS, ABS_Y) &&
libevdev_has_event_code(dev, EV_ABS, ABS_Z) &&
libevdev_has_event_code(dev, EV_ABS, ABS_RX) &&
libevdev_has_event_code(dev, EV_ABS, ABS_RY) &&
libevdev_has_event_code(dev, EV_ABS, ABS_RZ) &&
libevdev_has_event_code(dev, EV_ABS, ABS_HAT0X) &&
libevdev_has_event_code(dev, EV_ABS, ABS_HAT0Y) &&
libevdev_has_event_type(dev, EV_FF) &&
libevdev_has_event_code(dev, EV_FF, FF_RUMBLE) &&
libevdev_has_event_type(dev, EV_KEY)
#define BUTTON(controller_button, button_code) \
&& libevdev_has_event_code(dev, EV_KEY, button_code)
CONTROLLER_BUTTONS
#undef BUTTON
);
}
#ifndef PROD
#define controller_dump_info(controller) _controller_dump_info(controller)
/**
* Print the informations about a controller device.
*
* \param controller A pointer to the controller object.
*/
static void _controller_dump_info(const Controller *controller) {
log_debugf("controller name: '%s'", libevdev_get_name(controller->dev));
const char *phys = libevdev_get_phys(controller->dev);
if (phys) log_debugf("controller physical location: '%s'", phys);
log_debugf("controller vendor: %d",
libevdev_get_id_vendor(controller->dev));
log_debugf("controller product: %d",
libevdev_get_id_product(controller->dev));
}
#else
#define controller_dump_info(controller)
#endif
/**
* Initialize a controller from it's fd and libevdev object. The controller need
* to closed with controller_destroy().
*
* Before calling this function, ensure that the provided libevdev object
* represents a valid controller by using the is_controller() function.
*
* \param fd The file descriptor of the controller device.
* \param dev A pointer to the libevdev object associated with the controller.
*
* \returns a pointer to the controller or NULL on failure.
*/
static Controller *controller_from_fd_and_dev(const int fd,
struct libevdev *dev) {
Controller *controller = malloc(sizeof(*controller));
if (!controller) {
log_errorf("failed to allocate memory: %s", strerror(errno));
return NULL;
}
controller->fd = fd;
controller->dev = dev;
struct ff_effect effect;
memset(&effect, 0, sizeof(effect));
effect.type = FF_RUMBLE;
effect.id = -1;
effect.replay.length = CONTROLLER_RUMBLE_DURATION;
effect.u.rumble.strong_magnitude = 0xffff;
effect.u.rumble.weak_magnitude = 0xffff;
if (ioctl(controller->fd, EVIOCSFF, &effect) < 0) {
log_errorf("failed to upload rumble effect: %s", strerror(errno));
controller_destroy(controller);
return NULL;
}
controller->is_rumbling = false;
controller->rumble_effect_id = effect.id;
controller->rumble_start = 0;
log_debugf("uploaded rumble effect");
const int err = libevdev_grab(controller->dev, LIBEVDEV_GRAB);
if (err < 0) {
log_errorf("failed to grab controller: %s", strerror(-err));
controller_destroy(controller);
return NULL;
}
controller->grabbed = true;
log_debugf("grab controller");
controller_dump_info(controller);
if (!controller_rumble(controller)) {
controller_destroy(controller);
return NULL;
}
memset(controller->hat_state, false, sizeof(controller->hat_state));
return controller;
}
Controller *controller_from_device_path(const char *device_path) {
const int fd = open(device_path, O_RDWR | O_NONBLOCK);
if (fd < 0) {
log_errorf("failed to open %s: %s", device_path, strerror(errno));
return NULL;
}
struct libevdev *dev;
if (!controller_init_libevdev(fd, &dev)) {
close(fd);
return NULL;
}
if (!is_controller(dev)) {
log_errorf("failed to connect to device %s: not a controller",
device_path);
libevdev_free(dev);
close(fd);
return NULL;
}
log_debugf("connect to controller %s", device_path);
return controller_from_fd_and_dev(fd, dev);
}
Controller *controller_from_first(void) {
char *device_path = malloc(DEVICE_PATH_SIZE);
if (!device_path) {
log_errorf("failed to allocate memory: %s", strerror(errno));
return NULL;
}
Controller *controller = NULL;
for (uint32_t i = 0;; ++i) {
snprintf(device_path, DEVICE_PATH_SIZE, "/dev/input/event%d", i);
const int fd = open(device_path, O_RDWR | O_NONBLOCK);
if (fd < 0) {
if (errno == ENOENT) { // no more device
log_errorf("no controller found");
break;
}
if (errno == EACCES) continue; // we can't access this device
log_errorf("failed to open %s: %s", device_path,
strerror(errno));
break;
}
struct libevdev *dev;
if (!controller_init_libevdev(fd, &dev)) {
close(fd);
break;
}
if (is_controller(dev)) {
log_debugf("connect to controller %s", device_path);
controller = controller_from_fd_and_dev(fd, dev);
break;
}
libevdev_free(dev);
close(fd);
}
free(device_path);
return controller;
}
void controller_destroy(Controller *controller) {
libevdev_free(controller->dev);
close(controller->fd);
free(controller);
}
bool controller_list(void) {
for (uint32_t i = 0;; ++i) {
char device_path[DEVICE_PATH_SIZE];
snprintf(device_path, DEVICE_PATH_SIZE, "/dev/input/event%d", i);
const int fd = open(device_path, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
if (errno == ENOENT) return true; // no more device
if (errno == EACCES) continue; // we can't access this device
log_errorf("failed to open %s: %s", device_path,
strerror(errno));
return false;
}
struct libevdev *dev;
if (!controller_init_libevdev(fd, &dev)) {
close(fd);
return false;
}
if (is_controller(dev)) {
printf("%s: '%s'\n", device_path, libevdev_get_name(dev));
}
libevdev_free(dev);
close(fd);
}
}
#ifndef PROD
#define log_event(event) \
const char *type_name = libevdev_event_type_get_name(event.type); \
if (!type_name) { \
log_errorf("failed to get event type name: type=%d", event.type); \
return false; \
} \
const char *code_name = libevdev_event_code_get_name(event.type, \
event.code); \
if (!code_name) { \
log_errorf("failed to get event code name: type=%s code=%d", \
type_name, event.code); \
return false; \
} \
log_debugf("event: type=%s code=%s value=%d", type_name, code_name, \
event.value);
#else
#define log_event(event)
#endif
#define HAT_INDEX(hat_button) ((hat_button) - CONTROLLER_BUTTON_UP)
/**
* This function processes input events from a hat switch (D-pad), updates
* the state of the associated buttons and triggers the appropriate button
* callbacks.
*
* \param controller A pointer to the Controller object that is receiving the
* event.
* \param event A pointer to the input event structure containing the hat switch
* event.
* \param button_positive The button associated with the positive direction of
* the hat switch.
* \param button_negative The button associated with the negative direction of
* the hat switch.
* \param on_button_down A callback function that is invoked when a button
* is pressed down.
* \param on_button_up A callback function that is invoked when a button
* is released.
*/
static void controller_handle_hat_event(
Controller *controller,
const struct input_event *event,
const ControllerButton button_positive,
const ControllerButton button_negative,
const ControllerButtonEventCallBack on_button_down,
const ControllerButtonEventCallBack on_button_up
) {
if (event->value == 1) {
if (controller->hat_state[HAT_INDEX(button_negative)]) {
controller->hat_state[HAT_INDEX(button_negative)] = false;
on_button_up(button_negative);
}
if (!controller->hat_state[HAT_INDEX(button_positive)]) {
controller->hat_state[HAT_INDEX(button_positive)] = true;
on_button_down(button_positive);
}
} else if (event->value == 0) {
if (controller->hat_state[HAT_INDEX(button_negative)]) {
controller->hat_state[HAT_INDEX(button_negative)] = false;
on_button_up(button_negative);
}
if (controller->hat_state[HAT_INDEX(button_positive)]) {
controller->hat_state[HAT_INDEX(button_positive)] = false;
on_button_up(button_positive);
}
} else if (event->value == -1) {
if (!controller->hat_state[HAT_INDEX(button_negative)]) {
controller->hat_state[HAT_INDEX(button_negative)] = true;
on_button_down(button_negative);
}
if (controller->hat_state[HAT_INDEX(button_positive)]) {
controller->hat_state[HAT_INDEX(button_positive)] = false;
on_button_up(button_positive);
}
}
}
/**
* Handles input events for the controller and triggers the appropriate button
* callbacks.
*
* \param controller A pointer to the Controller object that is receiving the
* event.
* \param event A pointer to the input event structure containing the event
* data.
* \param on_button_down A callback function that is invoked when a button
* is pressed down.
* \param on_button_up A callback function that is invoked when a button
* is released.
*/
static void controller_handle_event(
Controller *controller,
const struct input_event *event,
const ControllerButtonEventCallBack on_button_down,
const ControllerButtonEventCallBack on_button_up
) {
if (event->type == EV_KEY) {
#define BUTTON(controller_button, button_code) \
if (event->code == button_code) { \
if (event->value) { \
on_button_down(controller_button); \
} else { \
on_button_up(controller_button); \
} \
return; \
}
CONTROLLER_BUTTONS
#undef BUTTON
} else if (event->type == EV_ABS) {
if (event->code == ABS_Z) {
if (!event->value) {
on_button_up(CONTROLLER_BUTTON_ZL);
} else {
on_button_down(CONTROLLER_BUTTON_ZL);
}
return;
}
if (event->code == ABS_RZ) {
if (!event->value) {
on_button_up(CONTROLLER_BUTTON_ZR);
} else {
on_button_down(CONTROLLER_BUTTON_ZR);
}
return;
}
if (event->code == ABS_HAT0Y) {
controller_handle_hat_event(
controller,
event,
CONTROLLER_BUTTON_DOWN,
CONTROLLER_BUTTON_UP,
on_button_down,
on_button_up
);
return;
}
if (event->code == ABS_HAT0X) {
controller_handle_hat_event(
controller,
event,
CONTROLLER_BUTTON_RIGHT,
CONTROLLER_BUTTON_LEFT,
on_button_down,
on_button_up
);
return;
}
}
}
/**
* Handles synchronization of dropped events after an input event overflow.
*
* \param controller A pointer to the Controller object that is receiving the
* event.
* \param on_button_down A callback function that is invoked when a button
* is pressed down.
* \param on_button_up A callback function that is invoked when a button
* is released.
*
* \returns true on success, or false on failure.
*/
static bool controller_handle_syn_dropped(
Controller *controller,
const ControllerButtonEventCallBack on_button_down,
const ControllerButtonEventCallBack on_button_up
) {
struct input_event event;
int return_code = LIBEVDEV_READ_STATUS_SYNC;
while (return_code == LIBEVDEV_READ_STATUS_SYNC) {
return_code = libevdev_next_event(controller->dev,
LIBEVDEV_READ_FLAG_SYNC,
&event);
if (return_code < 0) {
if (return_code != -EAGAIN) {
log_errorf("failed to get next event: %s",
strerror(-return_code));
return false;
}
}
log_event(event)
controller_handle_event(controller, &event, on_button_down,
on_button_up);
}
return true;
}
/**
* Stop the rumble effect.
*
* \param controller A pointer to the controller object to stop the rumble
* effect.
*
* \returns true on success, or false on failure.
*/
static bool controller_stop_rumble(Controller *controller) {
struct input_event stop;
stop.type = EV_FF;
stop.code = controller->rumble_effect_id;
stop.value = 0;
if (write(controller->fd, &stop, sizeof(stop)) < 0) {
log_errorf("failed to send rumble stop event: %s", strerror(errno));
return false;
}
controller->is_rumbling = false;
return true;
}
bool controller_update(Controller *controller,
const ControllerButtonEventCallBack on_button_down,
const ControllerButtonEventCallBack on_button_up) {
if (controller->is_rumbling &&
get_time_ms() - controller->rumble_start > CONTROLLER_RUMBLE_DURATION) {
if (!controller_stop_rumble(controller)) return false;
}
struct input_event event;
int return_code = LIBEVDEV_READ_STATUS_SUCCESS;
while (return_code != -EAGAIN) {
return_code = libevdev_next_event(controller->dev,
LIBEVDEV_READ_FLAG_NORMAL,
&event);
if (return_code < 0) {
if (return_code != -EAGAIN) {
log_errorf("failed to get next event: %s",
strerror(-return_code));
return false;
}
} else if (return_code == LIBEVDEV_READ_STATUS_SYNC) {
log_event(event)
if (!controller_handle_syn_dropped(
controller,
on_button_down,
on_button_up
)) return false;
} else if (return_code == LIBEVDEV_READ_STATUS_SUCCESS) {
log_event(event)
controller_handle_event(controller, &event, on_button_down,
on_button_up);
}
}
return true;
}
void controller_get_stick(const Controller *controller,
const ControllerStick stick, float *x, float *y) {
int x_axis, y_axis;
if (stick == CONTROLLER_STICK_LEFT) {
x_axis = ABS_X;
y_axis = ABS_Y;
} else if (stick == CONTROLLER_STICK_RIGHT) {
x_axis = ABS_RX;
y_axis = ABS_RY;
} else {
assert(false && "unreachable");
return;
}
const int x_value = libevdev_get_event_value(controller->dev, EV_ABS,
x_axis);
const int y_value = libevdev_get_event_value(controller->dev, EV_ABS,
y_axis);
*x = 2.0f * (float)(x_value + CONTROLLER_AXIS_MAX) /
(CONTROLLER_AXIS_MAX - CONTROLLER_AXIS_MIN) - 1.0f;
*y = 2.0f * (float)(y_value + CONTROLLER_AXIS_MAX) /
(CONTROLLER_AXIS_MAX - CONTROLLER_AXIS_MIN) - 1.0f;
if (fabsf(*x) < CONTROLLER_AXIS_ROUND) {
*x = 0.0f;
} else if (*x > 1.0f - CONTROLLER_AXIS_ROUND) {
*x = 1.0f;
} else if (*x < -1.0f + CONTROLLER_AXIS_ROUND) {
*x = -1.0f;
}
if (fabsf(*y) < CONTROLLER_AXIS_ROUND) {
*y = 0.0f;
} else if (*y > 1.0f - CONTROLLER_AXIS_ROUND) {
*y = 1.0f;
} else if (*y < -1.0f + CONTROLLER_AXIS_ROUND) {
*y = -1.0f;
}
}
bool controller_rumble(Controller *controller) {
log_debugf("controller rumble");
if (controller->is_rumbling) {
if (!controller_stop_rumble(controller)) return false;
}
struct input_event play;
play.type = EV_FF;
play.code = controller->rumble_effect_id;
play.value = 1;
if (write(controller->fd, &play, sizeof(play)) < 0) {
log_errorf("failed to send rumble play event: %s", strerror(errno));
return false;
}
controller->is_rumbling = true;
controller->rumble_start = get_time_ms();
return true;
}
bool controller_get_grabbed(const Controller *controller) {
return controller->grabbed;
}
bool controller_toggle_grabbed(Controller *controller) {
const int err = libevdev_grab(
controller->dev,
controller->grabbed ? LIBEVDEV_UNGRAB : LIBEVDEV_GRAB
);
if (err < 0) {
log_errorf("failed to grab/ungrab controller: %s", strerror(-err));
return false;
}
controller->grabbed = !controller->grabbed;
#ifndef PROD
if (controller->grabbed) {
log_debugf("grabbed controller");
} else {
log_debugf("ungrabbed controller");
}
#endif
return true;
}