-
Notifications
You must be signed in to change notification settings - Fork 361
Expand file tree
/
Copy pathevdev.c
More file actions
1129 lines (998 loc) · 37.1 KB
/
evdev.c
File metadata and controls
1129 lines (998 loc) · 37.1 KB
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of Moonlight Embedded.
*
* Copyright (C) 2015-2017 Iwan Timmer
*
* Moonlight is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Moonlight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moonlight; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __HAIKU__
#include "evdev.h"
#include "keyboard.h"
#include "../loop.h"
#include "libevdev/libevdev.h"
#include <Limelight.h>
#include <libudev.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <poll.h>
#include <limits.h>
#include <unistd.h>
#include <pthread.h>
#ifdef __linux__
#include <endian.h>
#else
#include <sys/endian.h>
#endif
#include <math.h>
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define int16_to_le(val) val
#else
#define int16_to_le(val) ((((val) >> 8) & 0x00FF) | (((val) << 8) & 0xFF00))
#endif
struct input_abs_parms {
int min, max;
int flat;
int avg;
int range, diff;
};
struct input_device {
struct libevdev *dev;
bool is_keyboard;
bool is_mouse;
bool is_touchscreen;
int rotate;
struct mapping* map;
int key_map[KEY_CNT];
int abs_map[ABS_CNT];
int hats_state[3][2];
int fd;
char modifiers;
#ifdef __linux__
__s32 mouseDeltaX, mouseDeltaY, mouseVScroll, mouseHScroll;
__s32 touchDownX, touchDownY, touchX, touchY;
#else
int32_t mouseDeltaX, mouseDeltaY, mouseVScroll, mouseHScroll;
int32_t touchDownX, touchDownY, touchX, touchY;
#endif
struct timeval touchDownTime;
struct timeval btnDownTime;
short controllerId;
int haptic_effect_id;
int buttonFlags;
unsigned char leftTrigger, rightTrigger;
short leftStickX, leftStickY;
short rightStickX, rightStickY;
bool gamepadModified;
bool mouseEmulation;
pthread_t meThread;
struct input_abs_parms xParms, yParms, rxParms, ryParms, zParms, rzParms;
struct input_abs_parms leftParms, rightParms, upParms, downParms;
};
#define HAT_UP 1
#define HAT_RIGHT 2
#define HAT_DOWN 4
#define HAT_LEFT 8
static const int hat_constants[3][3] = {{HAT_UP | HAT_LEFT, HAT_UP, HAT_UP | HAT_RIGHT}, {HAT_LEFT, 0, HAT_RIGHT}, {HAT_LEFT | HAT_DOWN, HAT_DOWN, HAT_DOWN | HAT_RIGHT}};
#define set_hat(flags, flag, hat, hat_flag) flags = (hat & hat_flag) == hat_flag ? flags | flag : flags & ~flag
#define TOUCH_UP -1
#define TOUCH_CLICK_RADIUS 10
#define TOUCH_CLICK_DELAY 100000 // microseconds
#define TOUCH_RCLICK_TIME 750 // milliseconds
// How long the Start button must be pressed to toggle mouse emulation
#define MOUSE_EMULATION_LONG_PRESS_TIME 750
// How long between polling the gamepad to send virtual mouse input
#define MOUSE_EMULATION_POLLING_INTERVAL 50000
// Determines how fast the mouse will move each interval
#define MOUSE_EMULATION_MOTION_MULTIPLIER 3
// Determines the maximum motion amount before allowing movement
#define MOUSE_EMULATION_DEADZONE 2
// Limited by number of bits in activeGamepadMask
#define MAX_GAMEPADS 16
static struct input_device* devices = NULL;
static int numDevices = 0;
static int assignedControllerIds = 0;
static short* currentKey;
static short* currentHat;
static short* currentHatDir;
static short* currentAbs;
static bool* currentReverse;
static bool grabbingDevices;
static bool mouseEmulationEnabled;
static bool waitingToExitOnModifiersUp = false;
int evdev_gamepads = 0;
#define ACTION_MODIFIERS (MODIFIER_SHIFT|MODIFIER_ALT|MODIFIER_CTRL)
#define QUIT_KEY KEY_Q
#define QUIT_BUTTONS (PLAY_FLAG|BACK_FLAG|LB_FLAG|RB_FLAG)
static bool (*handler) (struct input_event*, struct input_device*);
static int evdev_get_map(int* map, int length, int value) {
for (int i = 0; i < length; i++) {
if (value == map[i])
return i;
}
return -1;
}
static bool evdev_init_parms(struct input_device *dev, struct input_abs_parms *parms, int code) {
int abs = evdev_get_map(dev->abs_map, ABS_MAX, code);
if (abs >= 0) {
parms->flat = libevdev_get_abs_flat(dev->dev, abs);
parms->min = libevdev_get_abs_minimum(dev->dev, abs);
parms->max = libevdev_get_abs_maximum(dev->dev, abs);
if (parms->flat == 0 && parms->min == 0 && parms->max == 0)
return false;
parms->avg = (parms->min+parms->max)/2;
parms->range = parms->max - parms->avg;
parms->diff = parms->max - parms->min;
}
return true;
}
static void evdev_remove(int devindex) {
numDevices--;
printf("Input device removed: %s (player %d)\n", libevdev_get_name(devices[devindex].dev), devices[devindex].controllerId + 1);
if (devices[devindex].controllerId >= 0) {
assignedControllerIds &= ~(1 << devices[devindex].controllerId);
LiSendMultiControllerEvent(devices[devindex].controllerId, assignedControllerIds, 0, 0, 0, 0, 0, 0, 0);
}
if (devices[devindex].mouseEmulation) {
devices[devindex].mouseEmulation = false;
pthread_join(devices[devindex].meThread, NULL);
}
libevdev_free(devices[devindex].dev);
loop_remove_fd(devices[devindex].fd);
close(devices[devindex].fd);
if (devindex != numDevices && numDevices > 0)
memcpy(&devices[devindex], &devices[numDevices], sizeof(struct input_device));
}
static short evdev_convert_value(struct input_event *ev, struct input_device *dev, struct input_abs_parms *parms, bool reverse) {
if (parms->max == 0 && parms->min == 0) {
fprintf(stderr, "Axis not found: %d\n", ev->code);
return 0;
}
if (abs(ev->value - parms->avg) < parms->flat)
return 0;
else if (ev->value > parms->max)
return reverse?SHRT_MIN:SHRT_MAX;
else if (ev->value < parms->min)
return reverse?SHRT_MAX:SHRT_MIN;
else if (reverse)
return (long long)(parms->max - (ev->value<parms->avg?parms->flat*2:0) - ev->value) * (SHRT_MAX-SHRT_MIN) / (parms->max-parms->min-parms->flat*2) + SHRT_MIN;
else
return (long long)(ev->value - (ev->value>parms->avg?parms->flat*2:0) - parms->min) * (SHRT_MAX-SHRT_MIN) / (parms->max-parms->min-parms->flat*2) + SHRT_MIN;
}
static unsigned char evdev_convert_value_byte(struct input_event *ev, struct input_device *dev, struct input_abs_parms *parms, char halfaxis) {
if (parms->max == 0 && parms->min == 0) {
fprintf(stderr, "Axis not found: %d\n", ev->code);
return 0;
}
if (halfaxis == 0) {
if (abs(ev->value-parms->min)<parms->flat)
return 0;
else if (ev->value>parms->max)
return UCHAR_MAX;
else
return (ev->value - parms->flat - parms->min) * UCHAR_MAX / (parms->diff - parms->flat);
} else {
short val = evdev_convert_value(ev, dev, parms, false);
if (halfaxis == '-' && val < 0)
return -(int)val * UCHAR_MAX / (SHRT_MAX-SHRT_MIN);
else if (halfaxis == '+' && val > 0)
return (int)val * UCHAR_MAX / (SHRT_MAX-SHRT_MIN);
else
return 0;
}
}
void *HandleMouseEmulation(void* param)
{
struct input_device* dev = (struct input_device*) param;
while (dev->mouseEmulation) {
usleep(MOUSE_EMULATION_POLLING_INTERVAL);
short rawX;
short rawY;
// Determine which analog stick is currently receiving the strongest input
if ((uint32_t)abs(dev->leftStickX) + abs(dev->leftStickY) > (uint32_t)abs(dev->rightStickX) + abs(dev->rightStickY)) {
rawX = dev->leftStickX;
rawY = dev->leftStickY;
} else {
rawX = dev->rightStickX;
rawY = dev->rightStickY;
}
float deltaX;
float deltaY;
// Produce a base vector for mouse movement with increased speed as we deviate further from center
deltaX = pow((float)rawX / 32767.0f * MOUSE_EMULATION_MOTION_MULTIPLIER, 3);
deltaY = pow((float)rawY / 32767.0f * MOUSE_EMULATION_MOTION_MULTIPLIER, 3);
// Enforce deadzones
deltaX = fabs(deltaX) > MOUSE_EMULATION_DEADZONE ? deltaX - MOUSE_EMULATION_DEADZONE : 0;
deltaY = fabs(deltaY) > MOUSE_EMULATION_DEADZONE ? deltaY - MOUSE_EMULATION_DEADZONE : 0;
if (deltaX != 0 || deltaY != 0)
LiSendMouseMoveEvent(deltaX, -deltaY);
}
return NULL;
}
#define SET_BTN_FLAG(x, y) supportedButtonFlags |= (x >= 0) ? y : 0
static void send_controller_arrival(struct input_device *dev) {
unsigned char type = LI_CTYPE_UNKNOWN;
unsigned int supportedButtonFlags = 0;
unsigned short capabilities = 0;
switch (libevdev_get_id_vendor(dev->dev)) {
case 0x045e: // Microsoft
type = LI_CTYPE_XBOX;
break;
case 0x054c: // Sony
type = LI_CTYPE_PS;
break;
case 0x057e: // Nintendo
type = LI_CTYPE_NINTENDO;
break;
}
const char* name = libevdev_get_name(dev->dev);
if (name && type == LI_CTYPE_UNKNOWN) {
// Try to guess based on the name
if (strstr(name, "Xbox") || strstr(name, "X-Box") || strstr(name, "XBox") || strstr(name, "XBOX")) {
type = LI_CTYPE_XBOX;
}
}
SET_BTN_FLAG(dev->map->btn_a, A_FLAG);
SET_BTN_FLAG(dev->map->btn_b, B_FLAG);
SET_BTN_FLAG(dev->map->btn_x, X_FLAG);
SET_BTN_FLAG(dev->map->btn_y, Y_FLAG);
SET_BTN_FLAG(dev->map->btn_back, BACK_FLAG);
SET_BTN_FLAG(dev->map->btn_start, PLAY_FLAG);
SET_BTN_FLAG(dev->map->btn_guide, SPECIAL_FLAG);
SET_BTN_FLAG(dev->map->btn_leftstick, LS_CLK_FLAG);
SET_BTN_FLAG(dev->map->btn_rightstick, RS_CLK_FLAG);
SET_BTN_FLAG(dev->map->btn_leftshoulder, LB_FLAG);
SET_BTN_FLAG(dev->map->btn_rightshoulder, RB_FLAG);
SET_BTN_FLAG(dev->map->btn_misc1, MISC_FLAG);
SET_BTN_FLAG(dev->map->btn_paddle1, PADDLE1_FLAG);
SET_BTN_FLAG(dev->map->btn_paddle2, PADDLE2_FLAG);
SET_BTN_FLAG(dev->map->btn_paddle3, PADDLE3_FLAG);
SET_BTN_FLAG(dev->map->btn_paddle4, PADDLE4_FLAG);
SET_BTN_FLAG(dev->map->btn_touchpad, TOUCHPAD_FLAG);
if (dev->map->abs_lefttrigger >= 0 && dev->map->abs_righttrigger >= 0)
capabilities |= LI_CCAP_ANALOG_TRIGGERS;
// TODO: Probe for this properly
capabilities |= LI_CCAP_RUMBLE;
LiSendControllerArrivalEvent(dev->controllerId, assignedControllerIds, type,
supportedButtonFlags, capabilities);
}
static bool evdev_handle_event(struct input_event *ev, struct input_device *dev) {
bool gamepadModified = false;
switch (ev->type) {
case EV_SYN:
if (dev->mouseDeltaX != 0 || dev->mouseDeltaY != 0) {
switch (dev->rotate) {
case 90:
LiSendMouseMoveEvent(dev->mouseDeltaY, -dev->mouseDeltaX);
break;
case 180:
LiSendMouseMoveEvent(-dev->mouseDeltaX, -dev->mouseDeltaY);
break;
case 270:
LiSendMouseMoveEvent(-dev->mouseDeltaY, dev->mouseDeltaX);
break;
default:
LiSendMouseMoveEvent(dev->mouseDeltaX, dev->mouseDeltaY);
break;
}
dev->mouseDeltaX = 0;
dev->mouseDeltaY = 0;
}
if (dev->mouseVScroll != 0) {
LiSendScrollEvent(dev->mouseVScroll);
dev->mouseVScroll = 0;
}
if (dev->mouseHScroll != 0) {
LiSendHScrollEvent(dev->mouseHScroll);
dev->mouseHScroll = 0;
}
if (dev->gamepadModified) {
if (dev->controllerId < 0) {
for (int i = 0; i < MAX_GAMEPADS; i++) {
if ((assignedControllerIds & (1 << i)) == 0) {
assignedControllerIds |= (1 << i);
dev->controllerId = i;
printf("Assigned %s as player %d\n", libevdev_get_name(dev->dev), i+1);
break;
}
}
//Use id 0 when too many gamepads are connected
if (dev->controllerId < 0)
dev->controllerId = 0;
// Send controller arrival event to the host
send_controller_arrival(dev);
}
// Send event only if mouse emulation is disabled.
if (dev->mouseEmulation == false)
LiSendMultiControllerEvent(dev->controllerId, assignedControllerIds, dev->buttonFlags, dev->leftTrigger, dev->rightTrigger, dev->leftStickX, dev->leftStickY, dev->rightStickX, dev->rightStickY);
dev->gamepadModified = false;
}
break;
case EV_KEY:
if (ev->code > KEY_MAX)
return true;
if (ev->code < sizeof(keyCodes)/sizeof(keyCodes[0])) {
char modifier = 0;
switch (ev->code) {
case KEY_LEFTSHIFT:
case KEY_RIGHTSHIFT:
modifier = MODIFIER_SHIFT;
break;
case KEY_LEFTALT:
case KEY_RIGHTALT:
modifier = MODIFIER_ALT;
break;
case KEY_LEFTCTRL:
case KEY_RIGHTCTRL:
modifier = MODIFIER_CTRL;
break;
case KEY_LEFTMETA:
case KEY_RIGHTMETA:
modifier = MODIFIER_META;
break;
}
if (modifier != 0) {
if (ev->value)
dev->modifiers |= modifier;
else
dev->modifiers &= ~modifier;
}
// After the quit key combo is pressed, quit once all keys are raised
if ((dev->modifiers & ACTION_MODIFIERS) == ACTION_MODIFIERS &&
ev->code == QUIT_KEY && ev->value != 0) {
waitingToExitOnModifiersUp = true;
return true;
} else if (waitingToExitOnModifiersUp && dev->modifiers == 0)
return false;
short code = 0x80 << 8 | keyCodes[ev->code];
LiSendKeyboardEvent(code, ev->value?KEY_ACTION_DOWN:KEY_ACTION_UP, dev->modifiers);
} else {
int mouseCode = 0;
int gamepadCode = 0;
int index = dev->key_map[ev->code];
switch (ev->code) {
case BTN_LEFT:
mouseCode = BUTTON_LEFT;
break;
case BTN_MIDDLE:
mouseCode = BUTTON_MIDDLE;
break;
case BTN_RIGHT:
mouseCode = BUTTON_RIGHT;
break;
case BTN_SIDE:
mouseCode = BUTTON_X1;
break;
case BTN_EXTRA:
mouseCode = BUTTON_X2;
break;
case BTN_TOUCH:
if (ev->value == 1) {
dev->touchDownTime = ev->time;
} else {
if (dev->touchDownX != TOUCH_UP && dev->touchDownY != TOUCH_UP) {
int deltaX = dev->touchX - dev->touchDownX;
int deltaY = dev->touchY - dev->touchDownY;
if (deltaX * deltaX + deltaY * deltaY < TOUCH_CLICK_RADIUS * TOUCH_CLICK_RADIUS) {
struct timeval elapsedTime;
timersub(&ev->time, &dev->touchDownTime, &elapsedTime);
int holdTimeMs = elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000;
int button = holdTimeMs >= TOUCH_RCLICK_TIME ? BUTTON_RIGHT : BUTTON_LEFT;
LiSendMouseButtonEvent(BUTTON_ACTION_PRESS, button);
usleep(TOUCH_CLICK_DELAY);
LiSendMouseButtonEvent(BUTTON_ACTION_RELEASE, button);
}
}
dev->touchDownX = TOUCH_UP;
dev->touchDownY = TOUCH_UP;
}
break;
default:
gamepadModified = true;
if (dev->map == NULL)
break;
else if (index == dev->map->btn_a)
gamepadCode = A_FLAG;
else if (index == dev->map->btn_x)
gamepadCode = X_FLAG;
else if (index == dev->map->btn_y)
gamepadCode = Y_FLAG;
else if (index == dev->map->btn_b)
gamepadCode = B_FLAG;
else if (index == dev->map->btn_dpup)
gamepadCode = UP_FLAG;
else if (index == dev->map->btn_dpdown)
gamepadCode = DOWN_FLAG;
else if (index == dev->map->btn_dpright)
gamepadCode = RIGHT_FLAG;
else if (index == dev->map->btn_dpleft)
gamepadCode = LEFT_FLAG;
else if (index == dev->map->btn_leftstick)
gamepadCode = LS_CLK_FLAG;
else if (index == dev->map->btn_rightstick)
gamepadCode = RS_CLK_FLAG;
else if (index == dev->map->btn_leftshoulder)
gamepadCode = LB_FLAG;
else if (index == dev->map->btn_rightshoulder)
gamepadCode = RB_FLAG;
else if (index == dev->map->btn_start)
gamepadCode = PLAY_FLAG;
else if (index == dev->map->btn_back)
gamepadCode = BACK_FLAG;
else if (index == dev->map->btn_guide)
gamepadCode = SPECIAL_FLAG;
else if (index == dev->map->btn_misc1)
gamepadCode = MISC_FLAG;
else if (index == dev->map->btn_paddle1)
gamepadCode = PADDLE1_FLAG;
else if (index == dev->map->btn_paddle2)
gamepadCode = PADDLE2_FLAG;
else if (index == dev->map->btn_paddle3)
gamepadCode = PADDLE3_FLAG;
else if (index == dev->map->btn_paddle4)
gamepadCode = PADDLE4_FLAG;
else if (index == dev->map->btn_touchpad)
gamepadCode = TOUCHPAD_FLAG;
}
if (mouseCode != 0) {
LiSendMouseButtonEvent(ev->value?BUTTON_ACTION_PRESS:BUTTON_ACTION_RELEASE, mouseCode);
gamepadModified = false;
} else if (gamepadCode != 0) {
if (ev->value) {
dev->buttonFlags |= gamepadCode;
dev->btnDownTime = ev->time;
} else
dev->buttonFlags &= ~gamepadCode;
if (mouseEmulationEnabled && gamepadCode == PLAY_FLAG && ev->value == 0) {
struct timeval elapsedTime;
timersub(&ev->time, &dev->btnDownTime, &elapsedTime);
int holdTimeMs = elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000;
if (holdTimeMs >= MOUSE_EMULATION_LONG_PRESS_TIME) {
if (dev->mouseEmulation) {
dev->mouseEmulation = false;
pthread_join(dev->meThread, NULL);
dev->meThread = 0;
printf("Mouse emulation disabled for controller %d.\n", dev->controllerId);
} else {
dev->mouseEmulation = true;
pthread_create(&dev->meThread, NULL, HandleMouseEmulation, dev);
printf("Mouse emulation enabled for controller %d.\n", dev->controllerId);
}
// clear gamepad state.
LiSendMultiControllerEvent(dev->controllerId, assignedControllerIds, 0, 0, 0, 0, 0, 0, 0);
}
} else if (dev->mouseEmulation) {
char action = ev->value ? BUTTON_ACTION_PRESS : BUTTON_ACTION_RELEASE;
switch (gamepadCode) {
case A_FLAG:
LiSendMouseButtonEvent(action, BUTTON_LEFT);
break;
case B_FLAG:
LiSendMouseButtonEvent(action, BUTTON_RIGHT);
break;
case X_FLAG:
LiSendMouseButtonEvent(action, BUTTON_MIDDLE);
break;
case LB_FLAG:
LiSendMouseButtonEvent(action, BUTTON_X1);
break;
case RB_FLAG:
LiSendMouseButtonEvent(action, BUTTON_X2);
break;
}
}
} else if (dev->map != NULL && index == dev->map->btn_lefttrigger)
dev->leftTrigger = ev->value ? UCHAR_MAX : 0;
else if (dev->map != NULL && index == dev->map->btn_righttrigger)
dev->rightTrigger = ev->value ? UCHAR_MAX : 0;
else {
if (dev->map != NULL)
fprintf(stderr, "Unmapped button: %d\n", ev->code);
gamepadModified = false;
}
}
break;
case EV_REL:
switch (ev->code) {
case REL_X:
dev->mouseDeltaX = ev->value;
break;
case REL_Y:
dev->mouseDeltaY = ev->value;
break;
case REL_HWHEEL:
dev->mouseHScroll = ev->value;
break;
case REL_WHEEL:
dev->mouseVScroll = ev->value;
break;
}
break;
case EV_ABS:
if (ev->code > ABS_MAX)
return true;
if (dev->is_touchscreen) {
switch (ev->code) {
case ABS_X:
if (dev->touchDownX == TOUCH_UP) {
dev->touchDownX = ev->value;
dev->touchX = ev->value;
} else {
dev->mouseDeltaX += (ev->value - dev->touchX);
dev->touchX = ev->value;
}
break;
case ABS_Y:
if (dev->touchDownY == TOUCH_UP) {
dev->touchDownY = ev->value;
dev->touchY = ev->value;
} else {
dev->mouseDeltaY += (ev->value - dev->touchY);
dev->touchY = ev->value;
}
break;
}
break;
}
if (dev->map == NULL)
break;
gamepadModified = true;
int index = dev->abs_map[ev->code];
int hat_index = (ev->code - ABS_HAT0X) / 2;
int hat_dir_index = (ev->code - ABS_HAT0X) % 2;
switch (ev->code) {
case ABS_HAT0X:
case ABS_HAT0Y:
case ABS_HAT1X:
case ABS_HAT1Y:
case ABS_HAT2X:
case ABS_HAT2Y:
case ABS_HAT3X:
case ABS_HAT3Y:
dev->hats_state[hat_index][hat_dir_index] = ev->value < 0 ? -1 : (ev->value == 0 ? 0 : 1);
int hat_state = hat_constants[dev->hats_state[hat_index][1] + 1][dev->hats_state[hat_index][0] + 1];
if (hat_index == dev->map->hat_dpup)
set_hat(dev->buttonFlags, UP_FLAG, hat_state, dev->map->hat_dir_dpup);
if (hat_index == dev->map->hat_dpdown)
set_hat(dev->buttonFlags, DOWN_FLAG, hat_state, dev->map->hat_dir_dpdown);
if (hat_index == dev->map->hat_dpright)
set_hat(dev->buttonFlags, RIGHT_FLAG, hat_state, dev->map->hat_dir_dpright);
if (hat_index == dev->map->hat_dpleft)
set_hat(dev->buttonFlags, LEFT_FLAG, hat_state, dev->map->hat_dir_dpleft);
break;
default:
if (index == dev->map->abs_leftx)
dev->leftStickX = evdev_convert_value(ev, dev, &dev->xParms, dev->map->reverse_leftx);
else if (index == dev->map->abs_lefty)
dev->leftStickY = evdev_convert_value(ev, dev, &dev->yParms, !dev->map->reverse_lefty);
else if (index == dev->map->abs_rightx)
dev->rightStickX = evdev_convert_value(ev, dev, &dev->rxParms, dev->map->reverse_rightx);
else if (index == dev->map->abs_righty)
dev->rightStickY = evdev_convert_value(ev, dev, &dev->ryParms, !dev->map->reverse_righty);
else
gamepadModified = false;
if (index == dev->map->abs_lefttrigger) {
dev->leftTrigger = evdev_convert_value_byte(ev, dev, &dev->zParms, dev->map->halfaxis_lefttrigger);
gamepadModified = true;
}
if (index == dev->map->abs_righttrigger) {
dev->rightTrigger = evdev_convert_value_byte(ev, dev, &dev->rzParms, dev->map->halfaxis_righttrigger);
gamepadModified = true;
}
if (index == dev->map->abs_dpright) {
if (evdev_convert_value_byte(ev, dev, &dev->rightParms, dev->map->halfaxis_dpright) > 127)
dev->buttonFlags |= RIGHT_FLAG;
else
dev->buttonFlags &= ~RIGHT_FLAG;
gamepadModified = true;
}
if (index == dev->map->abs_dpleft) {
if (evdev_convert_value_byte(ev, dev, &dev->leftParms, dev->map->halfaxis_dpleft) > 127)
dev->buttonFlags |= LEFT_FLAG;
else
dev->buttonFlags &= ~LEFT_FLAG;
gamepadModified = true;
}
if (index == dev->map->abs_dpup) {
if (evdev_convert_value_byte(ev, dev, &dev->upParms, dev->map->halfaxis_dpup) > 127)
dev->buttonFlags |= UP_FLAG;
else
dev->buttonFlags &= ~UP_FLAG;
gamepadModified = true;
}
if (index == dev->map->abs_dpdown) {
if (evdev_convert_value_byte(ev, dev, &dev->downParms, dev->map->halfaxis_dpdown) > 127)
dev->buttonFlags |= DOWN_FLAG;
else
dev->buttonFlags &= ~DOWN_FLAG;
gamepadModified = true;
}
}
}
if (gamepadModified && (dev->buttonFlags & QUIT_BUTTONS) == QUIT_BUTTONS) {
LiSendMultiControllerEvent(dev->controllerId, assignedControllerIds, 0, 0, 0, 0, 0, 0, 0);
return false;
}
dev->gamepadModified |= gamepadModified;
return true;
}
static bool evdev_handle_mapping_event(struct input_event *ev, struct input_device *dev) {
int index, hat_index;
switch (ev->type) {
case EV_KEY:
index = dev->key_map[ev->code];
if (currentKey != NULL) {
if (ev->value)
*currentKey = index;
else if (*currentKey != -1 && index == *currentKey)
return false;
}
break;
case EV_ABS:
hat_index = (ev->code - ABS_HAT0X) / 2;
if (hat_index >= 0 && hat_index < 4) {
int hat_dir_index = (ev->code - ABS_HAT0X) % 2;
dev->hats_state[hat_index][hat_dir_index] = ev->value < 0 ? -1 : (ev->value == 0 ? 0 : 1);
}
if (currentAbs != NULL) {
struct input_abs_parms parms;
evdev_init_parms(dev, &parms, ev->code);
if (ev->value > parms.avg + parms.range/2) {
*currentAbs = dev->abs_map[ev->code];
*currentReverse = false;
} else if (ev->value < parms.avg - parms.range/2) {
*currentAbs = dev->abs_map[ev->code];
*currentReverse = true;
} else if (ev->code == *currentAbs)
return false;
} else if (currentHat != NULL) {
if (hat_index >= 0 && hat_index < 4) {
*currentHat = hat_index;
*currentHatDir = hat_constants[dev->hats_state[hat_index][1] + 1][dev->hats_state[hat_index][0] + 1];
return false;
}
}
break;
}
return true;
}
static void evdev_drain(void) {
for (int i = 0; i < numDevices; i++) {
struct input_event ev;
while (libevdev_next_event(devices[i].dev, LIBEVDEV_READ_FLAG_NORMAL, &ev) >= 0);
}
}
static int evdev_handle(int fd) {
for (int i=0;i<numDevices;i++) {
if (devices[i].fd == fd) {
int rc;
struct input_event ev;
while ((rc = libevdev_next_event(devices[i].dev, LIBEVDEV_READ_FLAG_NORMAL, &ev)) >= 0) {
if (rc == LIBEVDEV_READ_STATUS_SYNC)
fprintf(stderr, "Error: cannot keep up\n");
else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
if (!handler(&ev, &devices[i]))
return LOOP_RETURN;
}
}
if (rc == -ENODEV) {
evdev_remove(i);
} else if (rc != -EAGAIN && rc < 0) {
fprintf(stderr, "Error: %s\n", strerror(-rc));
exit(EXIT_FAILURE);
}
}
}
return LOOP_OK;
}
void evdev_create(const char* device, struct mapping* mappings, bool verbose, int rotate) {
int fd = open(device, O_RDWR|O_NONBLOCK);
if (fd <= 0) {
fprintf(stderr, "Failed to open device %s\n", device);
fflush(stderr);
return;
}
struct libevdev *evdev = libevdev_new();
libevdev_set_fd(evdev, fd);
const char* name = libevdev_get_name(evdev);
int16_t guid[8] = {0};
guid[0] = int16_to_le(libevdev_get_id_bustype(evdev));
int16_t vendor = libevdev_get_id_vendor(evdev);
int16_t product = libevdev_get_id_product(evdev);
if (vendor && product) {
guid[2] = int16_to_le(vendor);
guid[4] = int16_to_le(product);
guid[6] = int16_to_le(libevdev_get_id_version(evdev));
} else
strncpy((char*) &guid[2], name, 11);
char str_guid[33];
char* buf = str_guid;
for (int i = 0; i < 16; i++)
buf += sprintf(buf, "%02x", ((unsigned char*) guid)[i]);
struct mapping* default_mapping = NULL;
struct mapping* xwc_mapping = NULL;
while (mappings != NULL) {
if (strncmp(str_guid, mappings->guid, 32) == 0) {
if (verbose)
printf("Detected %s (%s) on %s as %s\n", name, str_guid, device, mappings->name);
break;
} else if (strncmp("default", mappings->guid, 32) == 0)
default_mapping = mappings;
else if (strncmp("xwc", mappings->guid, 32) == 0)
xwc_mapping = mappings;
mappings = mappings->next;
}
if (mappings == NULL && strstr(name, "Xbox 360 Wireless Receiver") != NULL)
mappings = xwc_mapping;
bool is_keyboard = libevdev_has_event_code(evdev, EV_KEY, KEY_Q);
bool is_mouse = libevdev_has_event_type(evdev, EV_REL) || libevdev_has_event_code(evdev, EV_KEY, BTN_LEFT);
bool is_touchscreen = libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH);
// This classification logic comes from SDL
bool is_accelerometer =
((libevdev_has_event_code(evdev, EV_ABS, ABS_X) &&
libevdev_has_event_code(evdev, EV_ABS, ABS_Y) &&
libevdev_has_event_code(evdev, EV_ABS, ABS_Z)) ||
(libevdev_has_event_code(evdev, EV_ABS, ABS_RX) &&
libevdev_has_event_code(evdev, EV_ABS, ABS_RY) &&
libevdev_has_event_code(evdev, EV_ABS, ABS_RZ))) &&
!libevdev_has_event_type(evdev, EV_KEY);
bool is_gamepad =
((libevdev_has_event_code(evdev, EV_ABS, ABS_X) &&
libevdev_has_event_code(evdev, EV_ABS, ABS_Y)) ||
(libevdev_has_event_code(evdev, EV_ABS, ABS_HAT0X) &&
libevdev_has_event_code(evdev, EV_ABS, ABS_HAT0Y))) &&
(libevdev_has_event_code(evdev, EV_KEY, BTN_TRIGGER) ||
libevdev_has_event_code(evdev, EV_KEY, BTN_A) ||
libevdev_has_event_code(evdev, EV_KEY, BTN_1) ||
libevdev_has_event_code(evdev, EV_ABS, ABS_RX) ||
libevdev_has_event_code(evdev, EV_ABS, ABS_RY) ||
libevdev_has_event_code(evdev, EV_ABS, ABS_RZ) ||
libevdev_has_event_code(evdev, EV_ABS, ABS_THROTTLE) ||
libevdev_has_event_code(evdev, EV_ABS, ABS_RUDDER) ||
libevdev_has_event_code(evdev, EV_ABS, ABS_WHEEL) ||
libevdev_has_event_code(evdev, EV_ABS, ABS_GAS) ||
libevdev_has_event_code(evdev, EV_ABS, ABS_BRAKE));
if (is_accelerometer) {
if (verbose)
printf("Ignoring accelerometer: %s\n", name);
libevdev_free(evdev);
close(fd);
return;
}
if (is_gamepad) {
evdev_gamepads++;
if (mappings == NULL) {
fprintf(stderr, "No mapping available for %s (%s) on %s\n", name, str_guid, device);
mappings = default_mapping;
}
} else {
if (verbose)
printf("Not mapping %s as a gamepad\n", name);
mappings = NULL;
}
int dev = numDevices;
numDevices++;
if (devices == NULL) {
devices = malloc(sizeof(struct input_device));
} else {
devices = realloc(devices, sizeof(struct input_device)*numDevices);
}
if (devices == NULL) {
fprintf(stderr, "Not enough memory\n");
exit(EXIT_FAILURE);
}
memset(&devices[dev], 0, sizeof(devices[0]));
devices[dev].fd = fd;
devices[dev].dev = evdev;
devices[dev].map = mappings;
/* Set unused evdev indices to -2 to avoid aliasing with the default -1 in our mappings */
memset(&devices[dev].key_map, -2, sizeof(devices[dev].key_map));
memset(&devices[dev].abs_map, -2, sizeof(devices[dev].abs_map));
devices[dev].is_keyboard = is_keyboard;
devices[dev].is_mouse = is_mouse;
devices[dev].is_touchscreen = is_touchscreen;
devices[dev].rotate = rotate;
devices[dev].touchDownX = TOUCH_UP;
devices[dev].touchDownY = TOUCH_UP;
int nbuttons = 0;
/* Count joystick buttons first like SDL does */
for (int i = BTN_JOYSTICK; i < KEY_MAX; ++i) {
if (libevdev_has_event_code(devices[dev].dev, EV_KEY, i))
devices[dev].key_map[i] = nbuttons++;
}
for (int i = 0; i < BTN_JOYSTICK; ++i) {
if (libevdev_has_event_code(devices[dev].dev, EV_KEY, i))
devices[dev].key_map[i] = nbuttons++;
}
int naxes = 0;
for (int i = 0; i < ABS_MAX; ++i) {
/* Skip hats */
if (i == ABS_HAT0X)
i = ABS_HAT3Y;
else if (libevdev_has_event_code(devices[dev].dev, EV_ABS, i))
devices[dev].abs_map[i] = naxes++;
}
devices[dev].controllerId = -1;
devices[dev].haptic_effect_id = -1;
if (devices[dev].map != NULL) {
bool valid = evdev_init_parms(&devices[dev], &(devices[dev].xParms), devices[dev].map->abs_leftx);
valid &= evdev_init_parms(&devices[dev], &(devices[dev].yParms), devices[dev].map->abs_lefty);
valid &= evdev_init_parms(&devices[dev], &(devices[dev].zParms), devices[dev].map->abs_lefttrigger);
valid &= evdev_init_parms(&devices[dev], &(devices[dev].rxParms), devices[dev].map->abs_rightx);
valid &= evdev_init_parms(&devices[dev], &(devices[dev].ryParms), devices[dev].map->abs_righty);
valid &= evdev_init_parms(&devices[dev], &(devices[dev].rzParms), devices[dev].map->abs_righttrigger);
valid &= evdev_init_parms(&devices[dev], &(devices[dev].leftParms), devices[dev].map->abs_dpleft);
valid &= evdev_init_parms(&devices[dev], &(devices[dev].rightParms), devices[dev].map->abs_dpright);
valid &= evdev_init_parms(&devices[dev], &(devices[dev].upParms), devices[dev].map->abs_dpup);
valid &= evdev_init_parms(&devices[dev], &(devices[dev].downParms), devices[dev].map->abs_dpdown);
if (!valid)
fprintf(stderr, "Mapping for %s (%s) on %s is incorrect\n", name, str_guid, device);
}
if (grabbingDevices && (is_keyboard || is_mouse || is_touchscreen)) {
if (ioctl(fd, EVIOCGRAB, 1) < 0) {
fprintf(stderr, "EVIOCGRAB failed with error %d\n", errno);
}
}
loop_add_fd(devices[dev].fd, &evdev_handle, POLLIN);
}
static void evdev_map_key(char* keyName, short* key) {
printf("Press %s\n", keyName);
currentKey = key;
currentHat = NULL;
currentAbs = NULL;
*key = -1;
loop_main();
usleep(250000);
evdev_drain();
}
static void evdev_map_abs(char* keyName, short* abs, bool* reverse) {
printf("Move %s\n", keyName);
currentKey = NULL;
currentHat = NULL;
currentAbs = abs;
currentReverse = reverse;
*abs = -1;
loop_main();
usleep(250000);
evdev_drain();
}
static void evdev_map_hatkey(char* keyName, short* hat, short* hat_dir, short* key) {
printf("Press %s\n", keyName);
currentKey = key;
currentHat = hat;
currentHatDir = hat_dir;
currentAbs = NULL;
*key = -1;
*hat = -1;
*hat_dir = -1;
*currentReverse = false;
loop_main();
usleep(250000);
evdev_drain();
}
static void evdev_map_abskey(char* keyName, short* abs, short* key, bool* reverse) {
printf("Press %s\n", keyName);
currentKey = key;
currentHat = NULL;
currentAbs = abs;
currentReverse = reverse;
*key = -1;