forked from NeroReflex/ROGueENEMY
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoutput_dev.c
1117 lines (984 loc) · 34.7 KB
/
output_dev.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
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
#include "output_dev.h"
#include "logic.h"
#include "platform.h"
#include "queue.h"
#include "message.h"
#include "settings.h"
#include "virt_ds4.h"
int create_output_dev(const char* uinput_path, output_dev_type_t type) {
int fd = open(uinput_path, O_WRONLY | O_NONBLOCK);
if(fd < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
struct uinput_setup dev = {0};
strncpy(dev.name, OUTPUT_DEV_NAME, UINPUT_MAX_NAME_SIZE-1);
#if defined(OUTPUT_DEV_BUS_TYPE)
dev.id.bustype = OUTPUT_DEV_BUS_TYPE;
#else
dev.id.bustype = BUS_VIRTUAL;
#endif
dev.id.vendor = OUTPUT_DEV_VENDOR_ID;
dev.id.product = OUTPUT_DEV_PRODUCT_ID;
#if defined(OUTPUT_DEV_VERSION)
dev.id.version = OUTPUT_DEV_VERSION;
#endif
//if (ioctl(fd, /*UI_SET_PHYS_STR*/ 18, PHYS_STR) != 0) {
// fprintf(stderr, "Controller and gyroscope will NOT be recognized as a single device!\n");
//}
if (ioctl(fd, UI_SET_PHYS, PHYS_STR) != 0) {
fprintf(stderr, "Error setting the phys of the virtual controller.\n");
}
#if !defined(UI_SET_PHYS_STR)
#warning Will not change the PHYS
#endif
#if !defined(UI_SET_UNIQ_STR)
#warning Will not change the UNIQ
#endif
switch (type) {
case output_dev_imu: {
#if defined(UI_SET_PHYS_STR)
ioctl(fd, UI_SET_PHYS_STR(18), PHYS_STR);
#else
fprintf(stderr, "UI_SET_PHYS_STR unavailable.\n");
#endif
#if defined(UI_SET_UNIQ_STR)
ioctl(fd, UI_SET_UNIQ_STR(18), PHYS_STR);
#else
fprintf(stderr, "UI_SET_UNIQ_STR unavailable.\n");
#endif
ioctl(fd, UI_SET_PROPBIT, INPUT_PROP_ACCELEROMETER);
ioctl(fd, UI_SET_EVBIT, EV_ABS);
#if defined(INCLUDE_TIMESTAMP)
ioctl(fd, UI_SET_EVBIT, EV_MSC);
ioctl(fd, UI_SET_MSCBIT, MSC_TIMESTAMP);
#endif
ioctl(fd, UI_SET_ABSBIT, ABS_X);
ioctl(fd, UI_SET_ABSBIT, ABS_Y);
ioctl(fd, UI_SET_ABSBIT, ABS_Z);
ioctl(fd, UI_SET_ABSBIT, ABS_RX);
ioctl(fd, UI_SET_ABSBIT, ABS_RY);
ioctl(fd, UI_SET_ABSBIT, ABS_RZ);
//ioctl(fd, UI_SET_KEYBIT, BTN_TRIGGER);
//ioctl(fd, UI_SET_KEYBIT, BTN_THUMB);
struct uinput_abs_setup devAbsX = {0};
devAbsX.code = ABS_X;
devAbsX.absinfo.minimum = -ACCEL_RANGE;
devAbsX.absinfo.maximum = ACCEL_RANGE;
devAbsX.absinfo.resolution = 255; // 255 units = 1g
devAbsX.absinfo.fuzz = 5;
devAbsX.absinfo.flat = 0;
if(ioctl(fd, UI_ABS_SETUP, &devAbsX) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
struct uinput_abs_setup devAbsY = {0};
devAbsY.code = ABS_Y;
devAbsY.absinfo.minimum = -ACCEL_RANGE;
devAbsY.absinfo.maximum = ACCEL_RANGE;
devAbsY.absinfo.resolution = 255; // 255 units = 1g
devAbsY.absinfo.fuzz = 5;
devAbsY.absinfo.flat = 0;
if(ioctl(fd, UI_ABS_SETUP, &devAbsY) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
struct uinput_abs_setup devAbsZ = {0};
devAbsZ.code = ABS_Z;
devAbsZ.absinfo.minimum = -ACCEL_RANGE;
devAbsZ.absinfo.maximum = ACCEL_RANGE;
devAbsZ.absinfo.resolution = 255; // 255 units = 1g
devAbsZ.absinfo.fuzz = 5;
devAbsZ.absinfo.flat = 0;
if(ioctl(fd, UI_ABS_SETUP, &devAbsZ) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
struct uinput_abs_setup devAbsRX = {0};
devAbsRX.code = ABS_RX;
devAbsRX.absinfo.minimum = -GYRO_RANGE;
devAbsRX.absinfo.maximum = GYRO_RANGE;
devAbsRX.absinfo.resolution = 1; // 1 unit = 1 degree/s
devAbsRX.absinfo.fuzz = 0;
devAbsRX.absinfo.flat = GYRO_DEADZONE;
if(ioctl(fd, UI_ABS_SETUP, &devAbsRX) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
struct uinput_abs_setup devAbsRY = {0};
devAbsRY.code = ABS_RY;
devAbsRY.absinfo.minimum = -GYRO_RANGE;
devAbsRY.absinfo.maximum = GYRO_RANGE;
devAbsRY.absinfo.resolution = 1; // 1 unit = 1 degree/s
devAbsRY.absinfo.fuzz = 0;
devAbsRY.absinfo.flat = GYRO_DEADZONE;
if(ioctl(fd, UI_ABS_SETUP, &devAbsRY) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
struct uinput_abs_setup devAbsRZ = {0};
devAbsRZ.code = ABS_RZ;
devAbsRZ.absinfo.minimum = -GYRO_RANGE;
devAbsRZ.absinfo.maximum = GYRO_RANGE;
devAbsRZ.absinfo.resolution = 1; // 1 unit = 1 degree/s
devAbsRZ.absinfo.fuzz = 0;
devAbsRZ.absinfo.flat = GYRO_DEADZONE;
if(ioctl(fd, UI_ABS_SETUP, &devAbsRZ) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
if(ioctl(fd, UI_DEV_SETUP, &dev) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
if(ioctl(fd, UI_DEV_CREATE) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
break;
}
case output_dev_gamepad: {
#if defined(UI_SET_PHYS_STR)
ioctl(fd, UI_SET_PHYS_STR(18), PHYS_STR);
#else
fprintf(stderr, "UI_SET_PHYS_STR unavailable.\n");
#endif
#if defined(UI_SET_UNIQ_STR)
ioctl(fd, UI_SET_UNIQ_STR(18), PHYS_STR);
#else
fprintf(stderr, "UI_SET_UNIQ_STR unavailable.\n");
#endif
//ioctl(fd, UI_SET_PROPBIT, INPUT_PROP_BUTTONPAD);
ioctl(fd, UI_SET_EVBIT, EV_ABS);
ioctl(fd, UI_SET_EVBIT, EV_KEY);
ioctl(fd, UI_SET_EVBIT, EV_SYN);
#if defined(INCLUDE_TIMESTAMP)
ioctl(fd, UI_SET_EVBIT, EV_MSC);
ioctl(fd, UI_SET_MSCBIT, MSC_TIMESTAMP);
#endif
ioctl(fd, UI_SET_ABSBIT, ABS_X);
ioctl(fd, UI_SET_ABSBIT, ABS_Y);
ioctl(fd, UI_SET_ABSBIT, ABS_Z);
ioctl(fd, UI_SET_ABSBIT, ABS_RX);
ioctl(fd, UI_SET_ABSBIT, ABS_RY);
ioctl(fd, UI_SET_ABSBIT, ABS_RZ);
ioctl(fd, UI_SET_ABSBIT, ABS_HAT0X);
ioctl(fd, UI_SET_ABSBIT, ABS_HAT0Y);
ioctl(fd, UI_SET_ABSBIT, ABS_HAT2X);
ioctl(fd, UI_SET_ABSBIT, ABS_HAT2Y);
ioctl(fd, UI_SET_KEYBIT, BTN_SOUTH);
ioctl(fd, UI_SET_KEYBIT, BTN_EAST);
ioctl(fd, UI_SET_KEYBIT, BTN_NORTH);
ioctl(fd, UI_SET_KEYBIT, BTN_WEST);
ioctl(fd, UI_SET_KEYBIT, BTN_TL);
ioctl(fd, UI_SET_KEYBIT, BTN_TR);
ioctl(fd, UI_SET_KEYBIT, BTN_TL2);
ioctl(fd, UI_SET_KEYBIT, BTN_TR2);
ioctl(fd, UI_SET_KEYBIT, BTN_SELECT);
ioctl(fd, UI_SET_KEYBIT, BTN_START);
ioctl(fd, UI_SET_KEYBIT, BTN_MODE);
ioctl(fd, UI_SET_KEYBIT, BTN_THUMBL);
ioctl(fd, UI_SET_KEYBIT, BTN_THUMBR);
ioctl(fd, UI_SET_KEYBIT, BTN_GEAR_DOWN);
ioctl(fd, UI_SET_KEYBIT, BTN_GEAR_UP);
ioctl(fd, UI_SET_KEYBIT, BTN_DPAD_UP);
ioctl(fd, UI_SET_KEYBIT, BTN_DPAD_DOWN);
ioctl(fd, UI_SET_KEYBIT, BTN_DPAD_LEFT);
ioctl(fd, UI_SET_KEYBIT, BTN_DPAD_RIGHT);
const struct uinput_abs_setup devAbsX = {
.code = ABS_X,
.absinfo = {
.value = 0,
.minimum = -32768,
.maximum = +32768,
.resolution = 1,
.fuzz = 16,
.flat = 128,
}
};
if(ioctl(fd, UI_ABS_SETUP, &devAbsX) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
struct uinput_abs_setup devAbsY = {
.code = ABS_Y,
.absinfo = {
.value = 0,
.minimum = -32768,
.maximum = +32768,
.resolution = 1,
.fuzz = 16,
.flat = 128,
}
};
if(ioctl(fd, UI_ABS_SETUP, &devAbsY) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
struct uinput_abs_setup devAbsZ = {
.code = ABS_Z,
.absinfo = {
.value = 0,
.minimum = 0,
.maximum = 255,
.resolution = 1,
//.fuzz = 16,
//.flat = 128,
}
};
if(ioctl(fd, UI_ABS_SETUP, &devAbsZ) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
struct uinput_abs_setup devAbsRX = {
.code = ABS_RX,
.absinfo = {
.value = 0,
.minimum = -32768,
.maximum = +32768,
.resolution = 1,
.fuzz = 16,
.flat = 128,
}
};
if(ioctl(fd, UI_ABS_SETUP, &devAbsRX) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
struct uinput_abs_setup devAbsRY = {
.code = ABS_RY,
.absinfo = {
.value = -1,
.minimum = -32768,
.maximum = +32768,
.resolution = 1,
.fuzz = 16,
.flat = 128,
}
};
if(ioctl(fd, UI_ABS_SETUP, &devAbsRY) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
struct uinput_abs_setup devAbsRZ = {
.code = ABS_RZ,
.absinfo = {
.value = 0,
.minimum = 0,
.maximum = 255,
.resolution = 1,
//.fuzz = 16,
//.flat = 128,
}
};
if(ioctl(fd, UI_ABS_SETUP, &devAbsRZ) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
struct uinput_abs_setup devAbsHat0X = {
.code = ABS_HAT0X,
.absinfo = {
.value = 0,
.minimum = -1,
.maximum = 1,
.resolution = 1,
//.fuzz = 16,
//.flat = 128,
}
};
if(ioctl(fd, UI_ABS_SETUP, &devAbsHat0X) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
struct uinput_abs_setup devAbsHat0Y = {
.code = ABS_HAT0Y,
.absinfo = {
.value = 0,
.minimum = -1,
.maximum = 1,
.resolution = 1,
//.fuzz = 16,
//.flat = 128,
}
};
if(ioctl(fd, UI_ABS_SETUP, &devAbsHat0Y) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
struct uinput_abs_setup devAbsHat2X = {
.code = ABS_HAT2X,
.absinfo = {
.value = 0,
.minimum = 0,
.maximum = 255,
.resolution = 51,
//.fuzz = 16,
//.flat = 128,
}
};
if(ioctl(fd, UI_ABS_SETUP, &devAbsHat2X) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
struct uinput_abs_setup devAbsHat2Y = {
.code = ABS_HAT2Y,
.absinfo = {
.value = 0,
.minimum = 0,
.maximum = 255,
.resolution = 51,
//.fuzz = 16,
//.flat = 128,
}
};
if(ioctl(fd, UI_ABS_SETUP, &devAbsHat2Y) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
if(ioctl(fd, UI_DEV_SETUP, &dev) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
if(ioctl(fd, UI_DEV_CREATE) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
break;
}
case output_dev_mouse: {
ioctl(fd, UI_SET_EVBIT, EV_REL);
ioctl(fd, UI_SET_EVBIT, EV_KEY);
ioctl(fd, UI_SET_EVBIT, EV_MSC);
ioctl(fd, UI_SET_EVBIT, EV_SYN);
#if defined(INCLUDE_TIMESTAMP)
ioctl(fd, UI_SET_MSCBIT, MSC_TIMESTAMP);
#endif
ioctl(fd, UI_SET_KEYBIT, BTN_LEFT);
ioctl(fd, UI_SET_KEYBIT, BTN_MIDDLE);
ioctl(fd, UI_SET_KEYBIT, BTN_RIGHT);
//ioctl(fd, UI_SET_KEYBIT, BTN_SIDE);
//ioctl(fd, UI_SET_KEYBIT, BTN_EXTRA);
ioctl(fd, UI_SET_RELBIT, REL_X);
ioctl(fd, UI_SET_RELBIT, REL_Y);
ioctl(fd, UI_SET_RELBIT, REL_WHEEL);
ioctl(fd, UI_SET_RELBIT, REL_WHEEL_HI_RES);
if(ioctl(fd, UI_DEV_SETUP, &dev) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
if(ioctl(fd, UI_DEV_CREATE) < 0) {
fd = -1;
close(fd);
goto create_output_dev_err;
}
break;
}
default:
// error
close(fd);
fd = -1;
goto create_output_dev_err;
}
create_output_dev_err:
return fd;
}
static void emit_ev(output_dev_t *const out_dev, const message_t *const msg) {
// if events are flagged as do not emit... Do NOT emit!
if (msg->flags & INPUT_FILTER_FLAGS_DO_NOT_EMIT) {
return;
}
int fd = out_dev->gamepad_fd;
const uint32_t msg_flags = msg->data.event.ev_flags;
if ((msg_flags & EV_MESSAGE_FLAGS_IMU) != 0) {
fd = out_dev->imu_fd;
} else if ((msg_flags & EV_MESSAGE_FLAGS_MOUSE) != 0) {
fd = out_dev->mouse_fd;
} else {
fd = out_dev->gamepad_fd;
}
for (uint32_t i = 0; i < msg->data.event.ev_count; ++i) {
struct input_event ev = {
.code = msg->data.event.ev[i].code,
.type = msg->data.event.ev[i].type,
.value = msg->data.event.ev[i].value,
.time = msg->data.event.ev[i].time,
};
if ((msg_flags & EV_MESSAGE_FLAGS_PRESERVE_TIME) == 0) {
gettimeofday(&ev.time, NULL);
}
#if defined(INCLUDE_OUTPUT_DEBUG)
printf(
"Output: Received event %s (%s): %d\n",
libevdev_event_type_get_name(ev.type),
libevdev_event_code_get_name(ev.type, ev.code),
ev.value
);
#endif
const ssize_t written = write(fd, (void*)&ev, sizeof(ev));
if (written != sizeof(ev)) {
fprintf(
stderr,
"Error writing event %s %s %d: written %ld bytes out of %ld\n",
libevdev_event_type_get_name(ev.type),
libevdev_event_code_get_name(ev.type, ev.code),
ev.value,
written,
sizeof(ev)
);
}
}
#if defined(INCLUDE_TIMESTAMP)
gettimeofday(&now, NULL);
const struct input_event timestamp_ev = {
.code = MSC_TIMESTAMP,
.type = EV_MSC,
.value = (now.tv_sec - secAtInit)*1000000 + (now.tv_usec - usecAtInit),
.time = now,
};
const ssize_t timestamp_written = write(fd, (void*)×tamp_ev, sizeof(timestamp_ev));
if (timestamp_written != sizeof(timestamp_ev)) {
fprintf(stderr, "Error in sync: written %ld bytes out of %ld\n", timestamp_written, sizeof(timestamp_ev));
}
#endif
struct timeval now = {0};
gettimeofday(&now, NULL);
const struct input_event syn_ev = {
.code = SYN_REPORT,
.type = EV_SYN,
.value = 0,
.time = now,
};
const ssize_t sync_written = write(fd, (void*)&syn_ev, sizeof(syn_ev));
if (sync_written != sizeof(syn_ev)) {
fprintf(stderr, "Error in sync: written %ld bytes out of %ld\n", sync_written, sizeof(syn_ev));
}
}
static void decode_ev(output_dev_t *const out_dev, message_t *const msg) {
static int F15_status = 0;
static int gyroscope_mouse_translation = 0;
// scan for mouse mode and emit events in the virtual mouse if required
if ((is_rc71l_ready(out_dev->logic)) && (is_mouse_mode(&out_dev->logic->platform))) {
// search for mouse-related events
for (uint32_t a = 0; a < msg->data.event.ev_count; ++a) {
if (msg->data.event.ev[a].type == EV_KEY) {
if ((msg->data.event.ev[a].code == BTN_MIDDLE) || (msg->data.event.ev[a].code == BTN_LEFT) || (msg->data.event.ev[a].code == BTN_RIGHT)) {
msg->data.event.ev_flags |= EV_MESSAGE_FLAGS_PRESERVE_TIME | EV_MESSAGE_FLAGS_MOUSE;
return;
}
} else if (msg->data.event.ev[a].type == EV_REL) {
msg->data.event.ev_flags |= EV_MESSAGE_FLAGS_PRESERVE_TIME | EV_MESSAGE_FLAGS_MOUSE;
return;
}
}
}
if (msg->data.event.ev[0].type == EV_REL) {
msg->data.event.ev_flags |= EV_MESSAGE_FLAGS_MOUSE;
} else if ((msg->data.event.ev_count >= 2) && (msg->data.event.ev[0].type == EV_MSC) && (msg->data.event.ev[0].code == MSC_SCAN)) {
if ((msg->data.event.ev[0].value == -13565784) && (msg->data.event.ev[1].type == EV_KEY) && (msg->data.event.ev[1].code == KEY_F18)) {
if (msg->data.event.ev[1].value == 1) {
printf("Detected mode switch command, switching mode...\n");
const int new_mode = cycle_mode(&out_dev->logic->platform);
if (new_mode < 0) {
fprintf(stderr, "Error in mode switching: %d\n", new_mode);
} else {
printf("Mode correctly switched to %d\n", new_mode);
out_dev->logic->gamepad_output - GAMEPAD_OUTPUT_DS5;
// if (new_mode == 0) {
// printf("Mode switched to virtual DualSense for game mode.\n");
// out_dev->logic->gamepad_output = (out_dev->logic->flags & LOGIC_FLAGS_VIRT_DS5_ENABLE) ? GAMEPAD_OUTPUT_DS5 : ((out_dev->logic->flags & LOGIC_FLAGS_VIRT_DS4_ENABLE) ? GAMEPAD_OUTPUT_DS4: GAMEPAD_OUTPUT_EVDEV);
// } else if (new_mode == 1) {
// printf("Mode switched to virtual evdev for lizard mode.\n");
// out_dev->logic->gamepad_output = GAMEPAD_OUTPUT_EVDEV;
// } else if (new_mode == 2) {
// printf("Mode switched to virtual DualShock for macro mode.\n");
// out_dev->logic->gamepad_output = (out_dev->logic->flags & LOGIC_FLAGS_VIRT_DS4_ENABLE) ? GAMEPAD_OUTPUT_DS4 : GAMEPAD_OUTPUT_EVDEV;
// }
}
} else {
// Do nothing effectively discarding the input
}
msg->flags |= INPUT_FILTER_FLAGS_DO_NOT_EMIT;
} else if (msg->data.event.ev[0].value == -13565784) {
msg->flags |= INPUT_FILTER_FLAGS_DO_NOT_EMIT;
} else if ((msg->data.event.ev_count == 2) && (msg->data.event.ev[0].value == 458860) && (msg->data.event.ev[1].type == EV_KEY) && (msg->data.event.ev[1].code == KEY_F17)) {
if (is_rc71l_ready(out_dev->logic)) {
if (is_mouse_mode(&out_dev->logic->platform)) {
if (msg->data.event.ev[1].value < 2) {
msg->data.event.ev_count = 1;
msg->data.event.ev[0].type = EV_KEY;
msg->data.event.ev[0].code = BTN_GEAR_DOWN;
msg->data.event.ev[0].value = msg->data.event.ev[1].value;
return;
}
} else if (is_gamepad_mode(&out_dev->logic->platform)) {
if (msg->data.event.ev[1].value == 0) {
--gyroscope_mouse_translation;
} else if (msg->data.event.ev[1].value == 1) {
++gyroscope_mouse_translation;
}
}
msg->flags |= INPUT_FILTER_FLAGS_DO_NOT_EMIT;
}
} else if ((msg->data.event.ev_count == 2) && (msg->data.event.ev[0].value == 458861) && (msg->data.event.ev[1].type == EV_KEY) && (msg->data.event.ev[1].code == KEY_F18)) {
if (is_rc71l_ready(out_dev->logic)) {
if (is_mouse_mode(&out_dev->logic->platform)) {
if (msg->data.event.ev[1].value < 2) {
msg->data.event.ev_count = 1;
msg->data.event.ev[0].type = EV_KEY;
msg->data.event.ev[0].code = BTN_GEAR_UP;
msg->data.event.ev[0].value = msg->data.event.ev[1].value;
}
} else if (is_gamepad_mode(&out_dev->logic->platform)) {
if (msg->data.event.ev[1].value == 0) {
--gyroscope_mouse_translation;
} else if (msg->data.event.ev[1].value == 1) {
++gyroscope_mouse_translation;
}
msg->flags |= INPUT_FILTER_FLAGS_DO_NOT_EMIT;
}
}
} else if ((msg->data.event.ev_count == 2) && (msg->data.event.ev[0].value == -13565786) && (msg->data.event.ev[1].type == EV_KEY) && (msg->data.event.ev[1].code == KEY_F16)) {
printf("Converted AC short-press button to BTN_MODE\n");
msg->data.event.ev_count = 1;
msg->data.event.ev[0].type = EV_KEY;
msg->data.event.ev[0].code = BTN_MODE;
msg->data.event.ev[0].value = msg->data.event.ev[1].value;
} else if ((msg->data.event.ev_count == 2) && (msg->data.event.ev[0].value == -13565787) && (msg->data.event.ev[1].type == EV_KEY) && (msg->data.event.ev[1].code == KEY_F15)) {
if (msg->data.event.ev[1].value == 0) {
if (F15_status > 0) {
--F15_status;
}
if (F15_status == 0) {
printf("Exiting gyro mode.\n");
}
} else if (msg->data.event.ev[1].value == 1) {
if (F15_status <= 2) {
++F15_status;
}
if (F15_status == 1) {
printf("Entering gyro mode.\n");
}
}
msg->flags |= INPUT_FILTER_FLAGS_DO_NOT_EMIT;
} else if (
(msg->data.event.ev_count == 2) &&
(msg->data.event.ev_size >= 3) &&
(msg->data.event.ev[0].value == -13565896) &&
(msg->data.event.ev[1].type == EV_KEY) &&
(msg->data.event.ev[1].code == KEY_PROG1)
) {
// do nothing to match the DS4 joystick
/*
msg->data.event.ev_count = 3;
const int32_t val = msg->data.event.ev[1].value;
//struct timeval time = msg->data.event.ev[1].time;
msg->data.event.ev[0].type = EV_KEY;
msg->data.event.ev[0].code = BTN_MODE;
msg->data.event.ev[0].value = val;
msg->data.event.ev[1].type = SYN_REPORT;
msg->data.event.ev[1].code = EV_SYN;
msg->data.event.ev[1].value = 0;
msg->data.event.ev[2].type = EV_KEY;
msg->data.event.ev[2].code = BTN_SOUTH;
msg->data.event.ev[2].value = val;
events[3].type = SYN_REPORT;
events[3].code = EV_SYN;
events[3].value = 0;
events[4].type = EV_KEY;
events[4].code = BTN_SOUTH;
events[4].value = 0;
*/
}
}
}
int swapLegionButtons = 0;
int crossButtonDelayCounter = 0;
int crossButtonHoldCounter = 0;
int centerButtonHoldCounter = 0;
int buttonState = 0;
const int INITIAL_DELAY = 15; // Delay before pressing the cross button
const int PRESS_DURATION = 5; // Duration to hold both buttons down
const int CROSS_RELEASE_DELAY = 7; // Delay before releasing cross button
const int CENTER_RELEASE_DELAY = 10; // Delay before releasing center button after cross is released
void decode_hidraw_to_gamepad(gamepad_status_t *gamepad, const message_t *msg) {
// Assuming your HIDRAW data is in msg->data.hidraw.data
// and the size of the data is in msg->data.hidraw.data_size
// Check for sufficient data size
// if (msg->data.hidraw.data_size < 64) {
// fprintf(stderr, "Insufficient HIDRAW data size\n");
// return;
// }
// Decode each control from the HIDRAW data
// This is highly dependent on how your specific HID device maps data
// Here are some hypothetical examples:
// Example: Decode a button press
// Let's assume the first byte corresponds to a button's state
unsigned char backButtonbyte = msg->data.hidraw.data[20];
unsigned char legionButtonbyte = msg->data.hidraw.data[18];
gamepad->l4 = (backButtonbyte & 0x80) ? 1 : 0; // L4 button
gamepad->l5 = (backButtonbyte & 0x40) ? 1 : 0; // L5 button
gamepad->r4 = (backButtonbyte & 0x20) ? 1 : 0; // R4 button
gamepad->r5 = (backButtonbyte & 0x04) ? 1 : 0; // R5 button
gamepad->touchpad_press = (backButtonbyte & 0x08) ? 1 : 0; //M2 button
if (swapLegionButtons == 0) {
// Swap the share and option buttons with Legion buttons
gamepad->share = (legionButtonbyte & 0x40) ? 1 : 0;
gamepad->option = (legionButtonbyte & 0x80) ? 1 : 0;
// gamepad->center = (backButtonbyte & 0x01) ? 1 : 0;
//QAM contraption using hidraw BUG: SENDS AND EXTRA X
static int wasSpecialComboPressed = 0;
// gamepad->center = (backButtonbyte & 0x01) ? 1 : 0; // Center button
if(gamepad->flags == 0){
gamepad->center = (backButtonbyte & 0x01) ? 1 : 0; // Center button
}
if((backButtonbyte & 0x02) != 0){
wasSpecialComboPressed = 1;
} else {
if(wasSpecialComboPressed){
gamepad->flags |= GAMEPAD_STATUS_FLAGS_OPEN_STEAM_QAM;
wasSpecialComboPressed = 0;
}
}
} else {
// Original position
gamepad->share = (backButtonbyte & 0x01) ? 1 : 0;
gamepad->option = (backButtonbyte & 0x02) ? 1 : 0;
//QAM contraption using hidraw BUG: SENDS AND EXTRA X
static int wasSpecialComboPressed = 0;
if(gamepad->flags == 0){
gamepad->center = (legionButtonbyte & 0x80) ? 1 : 0; // Center button
}
if((legionButtonbyte & 0x40) != 0){
wasSpecialComboPressed = 1;
} else {
if(wasSpecialComboPressed){
gamepad->flags |= GAMEPAD_STATUS_FLAGS_OPEN_STEAM_QAM;
wasSpecialComboPressed = 0;
}
}
// Special handling for the combination of Center + Cross
}
// Toucpad mapping
// Disabled; not finished
// X/Y is 12bits
// Combine the counter and data bytes to get the full position
// Assuming the counter represents the upper 2 bits
unsigned int trackpadDataX = msg->data.hidraw.data[27]; // X axis data
unsigned int counterX = msg->data.hidraw.data[26]; // X axis counter
unsigned int trackpadDataY = msg->data.hidraw.data[29]; // Y axis data
unsigned int counterY = msg->data.hidraw.data[28]; // Y axis counter
// Calculate the cumulative value
unsigned int cumulativeX = (counterX * 0xFF) + trackpadDataX;
unsigned int cumulativeY = (counterY * 0xFF) + trackpadDataY;
gamepad->touchpadX = cumulativeX; //Sending a value from 0 - 1000
gamepad->touchpadY = cumulativeY; //Sending a value from 0 - 1000
// Uncomment for debugging
// printf("CounterX/Y %d/%d\tCumulative Touchpad Position: X:%d Y:%d\n",counterX,counterY, cumulativeX, cumulativeY);
}
void update_gs_from_hidraw(gamepad_status_t *gs, const message_t *msg) {
// Decode the HIDRAW data to gamepad inputs
decode_hidraw_to_gamepad(gs, msg);
// After calling decode_hidraw_to_gamepad, the gamepad status gs should now be updated
// Here, you can add any additional logic required for your gamepad status
// For example, handling special buttons, macros, or other complex inputs
// ...
// Example: Let's assume your gamepad has a special combo button feature
// if (gs->button_x && gs->button_y) {
// // Perform some action when both X and Y buttons are pressed
// }
// Handle other special cases or additional logic as needed
// ...
}
static void update_gs_from_ev(gamepad_status_t *const gs, message_t *const msg, controller_settings_t *const settings) {
if ( // this is what happens at release of the left-screen button of the ROG Ally
(msg->data.event.ev_count == 2) &&
(msg->data.event.ev[0].type == EV_MSC) &&
(msg->data.event.ev[0].code == MSC_SCAN) &&
(msg->data.event.ev[0].value == -13565786) &&
(msg->data.event.ev[1].type == EV_KEY) &&
(msg->data.event.ev[1].code == KEY_F16) &&
(msg->data.event.ev[1].value == 1)
) {
#if defined(INCLUDE_OUTPUT_DEBUG)
printf("RC71L CC button short-press detected\n");
#endif
gs->flags |= GAMEPAD_STATUS_FLAGS_PRESS_AND_REALEASE_CENTER;
} else if ( // this is what happens at release of the right-screen button of the ROG Ally
(msg->data.event.ev_count == 2) &&
(msg->data.event.ev[0].type == EV_MSC) &&
(msg->data.event.ev[0].code == MSC_SCAN) &&
(msg->data.event.ev[0].value == -13565896) &&
(msg->data.event.ev[1].type == EV_KEY) &&
(msg->data.event.ev[1].code == KEY_PROG1) &&
(msg->data.event.ev[1].value == 1)
) {
#if defined(INCLUDE_OUTPUT_DEBUG)
printf("RC71L AC button short-press detected\n");
#endif
if (settings->enable_qam) {
gs->flags |= GAMEPAD_STATUS_FLAGS_OPEN_STEAM_QAM;
}
} else if (
(msg->data.event.ev_count == 2) &&
(msg->data.event.ev[0].type == EV_MSC) &&
(msg->data.event.ev[0].code == MSC_SCAN) &&
(msg->data.event.ev[0].value == 458860) &&
(msg->data.event.ev[1].type == EV_KEY) &&
(msg->data.event.ev[1].code == KEY_F17)
) {
gs->l4 = msg->data.event.ev[1].value;
} else if (
(msg->data.event.ev_count == 2) &&
(msg->data.event.ev[0].type == EV_MSC) &&
(msg->data.event.ev[0].code == MSC_SCAN) &&
(msg->data.event.ev[0].value == 458861) &&
(msg->data.event.ev[1].type == EV_KEY) &&
(msg->data.event.ev[1].code == KEY_F18)
) {
gs->r4 = msg->data.event.ev[1].value;
}
for (uint32_t i = 0; i < msg->data.event.ev_count; ++i) {
if (msg->data.event.ev[i].type == EV_KEY) {
if (msg->data.event.ev[i].code == BTN_EAST) {
if (settings->nintendo_layout) {
gs->cross = msg->data.event.ev[i].value;
} else {
gs->circle = msg->data.event.ev[i].value;
}
} else if (msg->data.event.ev[i].code == BTN_NORTH) {
if (settings->nintendo_layout) {
gs->triangle = msg->data.event.ev[i].value;
} else {
gs->square = msg->data.event.ev[i].value;
}
} else if (msg->data.event.ev[i].code == BTN_SOUTH) {
if (settings->nintendo_layout) {
gs->circle = msg->data.event.ev[i].value;
} else {
gs->cross = msg->data.event.ev[i].value;
}
} else if (msg->data.event.ev[i].code == BTN_WEST) {
if (settings->nintendo_layout) {
gs->square = msg->data.event.ev[i].value;
} else {
gs->triangle = msg->data.event.ev[i].value;
}
// } else if (msg->data.event.ev[i].code == BTN_SELECT) {
// gs->option = msg->data.event.ev[i].value;
// } else if (msg->data.event.ev[i].code == BTN_START) {
// gs->share = msg->data.event.ev[i].value;
} else if (msg->data.event.ev[i].code == BTN_TR) {
gs->r1 = msg->data.event.ev[i].value;
} else if (msg->data.event.ev[i].code == BTN_TL) {
gs->l1 = msg->data.event.ev[i].value;
} else if (msg->data.event.ev[i].code == BTN_THUMBR) {
gs->r3 = msg->data.event.ev[i].value;
} else if (msg->data.event.ev[i].code == BTN_THUMBL) {
gs->l3 = msg->data.event.ev[i].value;
} else if (msg->data.event.ev[i].code == BTN_MODE) {
gs->flags |= GAMEPAD_STATUS_FLAGS_PRESS_AND_REALEASE_CENTER;
}
} else if (msg->data.event.ev[i].type == EV_ABS) {
if (msg->data.event.ev[i].code == ABS_X) {
gs->joystick_positions[0][0] = (int32_t)msg->data.event.ev[i].value;
} else if (msg->data.event.ev[i].code == ABS_Y) {
gs->joystick_positions[0][1] = (int32_t)msg->data.event.ev[i].value;
} else if (msg->data.event.ev[i].code == ABS_RX) {
gs->joystick_positions[1][0] = (int32_t)msg->data.event.ev[i].value;
} else if (msg->data.event.ev[i].code == ABS_RY) {
gs->joystick_positions[1][1] = (int32_t)msg->data.event.ev[i].value;
} else if (msg->data.event.ev[i].code == ABS_Z) {
gs->l2_trigger = (int32_t)msg->data.event.ev[i].value;
} else if (msg->data.event.ev[i].code == ABS_RZ) {
gs->r2_trigger = (int32_t)msg->data.event.ev[i].value;
} else if (msg->data.event.ev[i].code == ABS_HAT0X) {
const int v = msg->data.event.ev[i].value;
gs->dpad &= 0xF0;
if (v == 0) {
gs->dpad |= 0x00;
} else if (v == 1) {
gs->dpad |= 0x01;
} else if (v == -1) {
gs->dpad |= 0x02;
}
} else if (msg->data.event.ev[i].code == ABS_HAT0Y) {
const int v = msg->data.event.ev[i].value;
gs->dpad &= 0x0F;
if (v == 0) {
gs->dpad |= 0x00;
} else if (v == 1) {
gs->dpad |= 0x20;
} else if (v == -1) {
gs->dpad |= 0x10;
}
}
}
}
}
static void handle_msg(output_dev_t *const out_dev, message_t *const msg) {
if (msg->type == MSG_TYPE_EV) {
decode_ev(out_dev, msg);
// TODO: the mode could have been switched by decode_ev so change the output device too
const int upd_beg_res = logic_begin_status_update(out_dev->logic);
if (upd_beg_res == 0) {
update_gs_from_ev(&out_dev->logic->gamepad, msg, &out_dev->logic->controller_settings);
logic_end_status_update(out_dev->logic);
} else {
fprintf(stderr, "[ev] Unable to begin the gamepad status update: %d\n", upd_beg_res);
}
if (out_dev->logic->gamepad_output == GAMEPAD_OUTPUT_EVDEV) {
emit_ev(out_dev, msg);
}
} else if (msg->type == MSG_TYPE_IMU) {
const int upd_beg_res = logic_begin_status_update(out_dev->logic);
const scaleFactor = 20;
if (upd_beg_res == 0) {
if (msg->data.imu.flags & IMU_MESSAGE_FLAGS_ANGLVEL) {
out_dev->logic->gamepad.last_gyro_motion_time = msg->data.imu.gyro_read_time;
memcpy(out_dev->logic->gamepad.gyro, msg->data.imu.gyro_rad_s, sizeof(double[3]));
out_dev->logic->gamepad.raw_gyro[0] = scaleFactor * msg->data.imu.gyro_x_raw;
out_dev->logic->gamepad.raw_gyro[1] = scaleFactor * msg->data.imu.gyro_y_raw;
out_dev->logic->gamepad.raw_gyro[2] = scaleFactor * msg->data.imu.gyro_z_raw;
}
if (msg->data.imu.flags & IMU_MESSAGE_FLAGS_ACCEL) {
out_dev->logic->gamepad.last_accel_motion_time = msg->data.imu.accel_read_time;
memcpy(out_dev->logic->gamepad.accel, msg->data.imu.accel_m2s, sizeof(double[3]));
out_dev->logic->gamepad.raw_accel[0] = scaleFactor * msg->data.imu.accel_x_raw;
out_dev->logic->gamepad.raw_accel[1] = scaleFactor * msg->data.imu.accel_y_raw;
out_dev->logic->gamepad.raw_accel[2] = scaleFactor * msg->data.imu.accel_z_raw;
logic_end_status_update(out_dev->logic);
}
#if defined(INCLUDE_OUTPUT_DEBUG)
// printf("gyro_x: %d\t\t| gyro_y: %d\t\t| gyro_z: %d\t\t\n", (int)out_dev->logic->gamepad.raw_gyro[0], (int)out_dev->logic->gamepad.raw_gyro[1], (int)out_dev->logic->gamepad.raw_gyro[2]);
#endif
} else {
fprintf(stderr, "[imu] Unable to begin the gamepad status update: %d\n", upd_beg_res);
}
} else if (msg->type == MSG_TYPE_HIDRAW) {
// printf("HIDRAW Data (%zd bytes): ", msg->data.hidraw.data_size);
// for (ssize_t i = 0; i < msg->data.hidraw.data_size; ++i) {
// printf("%02X ", msg->data.hidraw.data[i]);
// }
// printf("\n");
decode_hidraw_to_gamepad(&out_dev->logic->gamepad, msg);
//Begin updating gamepad status
const int upd_hidraw_res = logic_begin_status_update(out_dev->logic);
if(upd_hidraw_res == 0){
update_gs_from_hidraw(&out_dev->logic->gamepad,msg);
logic_end_status_update(out_dev->logic);