Repository navigation
Expand file tree
/
Copy pathjoymapping.cpp
More file actions
2112 lines (1816 loc) · 58 KB
/
Copy pathjoymapping.cpp
File metadata and controls
2112 lines (1816 loc) · 58 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 contains lookup information on known controllers
*/
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include "joymapping.h"
#include "menu.h"
#include "input.h"
#include "user_io.h"
#include "cfg.h"
#include "hardware.h"
#include "str_util.h"
#include "file_io.h"
#include "osd.h"
#include "gamecontroller_db.h"
#define DPAD_COUNT 4
#define CORE_BUTTON_COUNT (32 - DPAD_COUNT)
/*****************************************************************************/
static void trim(char * s)
{
char *p = s;
int l = strlen(p);
if (!l) return;
while (p[l - 1] == ' ') p[--l] = 0;
while (*p && (*p == ' ')) ++p, --l;
memmove(s, p, l + 1);
}
static char joy_names[NUMBUTTONS][32];
static int joy_count = 0;
static char joy_nnames[NUMBUTTONS][32];
static char joy_pnames[NUMBUTTONS][32];
static int joy_nnames_given = 0, joy_pnames_given = 0;
static int defaults = 0;
static int list_entries(const char *p)
{
int count = 1;
while (*p) count += *p++ == ',';
return count;
}
static void read_buttons()
{
char *p;
memset(joy_names, 0, sizeof(joy_names));
memset(joy_nnames, 0, sizeof(joy_nnames));
memset(joy_pnames, 0, sizeof(joy_pnames));
joy_count = 0;
joy_nnames_given = joy_pnames_given = 0;
defaults = 0;
user_io_read_confstr();
// this option used as default name map (unless jn/jp is supplied)
p = get_buttons(0);
if (p)
{
for (int n = 0; n < CORE_BUTTON_COUNT; n++)
{
substrcpy(joy_names[n], p, n);
if (!joy_names[n][0]) break;
printf("joy_name[%d] = %s\n", n, joy_names[n]);
memcpy(joy_nnames[n], joy_names[n], sizeof(joy_nnames[0]));
char *sstr = strchr(joy_nnames[n], '(');
if (sstr) *sstr = 0;
trim(joy_nnames[n]);
if (!joy_nnames[n][0]) break;
joy_count++;
}
printf("\n");
}
// - supports empty name to skip the button from default map
// - only base button names must be used (ABXYLR Start Select)
// name default map
p = get_buttons(1);
if (p)
{
memset(joy_nnames, 0, sizeof(joy_nnames));
joy_nnames_given = list_entries(p);
for (int n = 0; n < joy_count; n++)
{
substrcpy(joy_nnames[n], p, n);
trim(joy_nnames[n]);
if (joy_nnames[n][0]) printf("joy_nname[%d] = %s\n", n, joy_nnames[n]);
}
printf("\n");
}
// positional default map
p = get_buttons(2);
if (p)
{
defaults = cfg.gamepad_defaults;
joy_pnames_given = list_entries(p);
for (int n = 0; n < joy_count; n++)
{
substrcpy(joy_pnames[n], p, n);
trim(joy_pnames[n]);
if (joy_pnames[n][0]) printf("joy_pname[%d] = %s\n", n, joy_pnames[n]);
}
printf("\n");
}
}
static int is_fire(char* name)
{
if (!strncasecmp(name, "fire", 4) || !strncasecmp(name, "button", 6))
{
if (!strcasecmp(name, "fire") || strchr(name, '1')) return 1;
if (strchr(name, '2')) return 2;
if (strchr(name, '3')) return 3;
if (strchr(name, '4')) return 4;
}
return 0;
}
static int right_stick_direction(const char *name)
{
static const char *const names[][2] = {
{ "RX_LEFT", "RightX-" },
{ "RX_RIGHT", "RightX+" },
{ "RY_UP", "RightY-" },
{ "RY_DOWN", "RightY+" }
};
for (int direction = 0; direction < 4; direction++)
if (!strcasecmp(name, names[direction][0]) || !strcasecmp(name, names[direction][1])) return direction;
return -1;
}
static int is_extended_button(char *name)
{
return !strcasecmp(name, "L2")
|| !strcasecmp(name, "R2")
|| !strcasecmp(name, "L3")
|| !strcasecmp(name, "R3")
|| !strcasecmp(name, "Misc1")
|| right_stick_direction(name) >= 0;
}
static void copy_core_button_name(char *dst, size_t size, int index)
{
snprintf(dst, size, "%.*s", (int)sizeof(joy_names[0]) - 1, joy_names[index]);
char *p = strchr(dst, '(');
if (p) *p = 0;
trim(dst);
}
void map_joystick(uint32_t *map, uint32_t *mmap, const uint32_t *right_directions, uint32_t misc1)
{
/*
attemps to centrally defined core joy mapping to the joystick declaredy by a core config string
we use the names declared by core with some special handling for specific edge cases
Input button order is "virtual SNES" i.e.:
A, B, X, Y, L, R, Select, Start
*/
read_buttons();
map[SYS_BTN_RIGHT] = mmap[SYS_BTN_RIGHT] & 0xFFFF;
map[SYS_BTN_LEFT] = mmap[SYS_BTN_LEFT] & 0xFFFF;
map[SYS_BTN_DOWN] = mmap[SYS_BTN_DOWN] & 0xFFFF;
map[SYS_BTN_UP] = mmap[SYS_BTN_UP] & 0xFFFF;
if (mmap[SYS_AXIS_X] && !is_psx())
{
const uint32_t min_key = KEY_EMU + ((mmap[SYS_AXIS_X] & MAP_AXIS_MASK) << 1);
const uint32_t max_key = min_key + 1;
const uint32_t left_key = (mmap[SYS_AXIS_X] & MAP_FLAG_INVERT) ? max_key : min_key;
const uint32_t right_key = (mmap[SYS_AXIS_X] & MAP_FLAG_INVERT) ? min_key : max_key;
map[SYS_BTN_LEFT] = (left_key << 16) | map[SYS_BTN_LEFT];
map[SYS_BTN_RIGHT] = (right_key << 16) | map[SYS_BTN_RIGHT];
}
if (mmap[SYS_AXIS_Y] && !is_psx())
{
const uint32_t min_key = KEY_EMU + ((mmap[SYS_AXIS_Y] & MAP_AXIS_MASK) << 1);
const uint32_t max_key = min_key + 1;
const uint32_t up_key = (mmap[SYS_AXIS_Y] & MAP_FLAG_INVERT) ? max_key : min_key;
const uint32_t down_key = (mmap[SYS_AXIS_Y] & MAP_FLAG_INVERT) ? min_key : max_key;
map[SYS_BTN_UP] = (up_key << 16) | map[SYS_BTN_UP];
map[SYS_BTN_DOWN] = (down_key << 16) | map[SYS_BTN_DOWN];
}
// loop through core requested buttons and construct result map
for (int i=0, n=0; i<joy_count; i++)
{
if (!strcmp(joy_names[i], "-")) continue;
int idx = i+DPAD_COUNT;
char btn_name[32];
strcpy(btn_name, defaults ? joy_pnames[n] : joy_nnames[n]);
if (!btn_name[0] && n >= (defaults ? joy_pnames_given : joy_nnames_given))
{
copy_core_button_name(btn_name, sizeof(btn_name), i);
if (!is_extended_button(btn_name)) btn_name[0] = 0;
}
char *p = strchr(btn_name, '|');
if (p) *p = 0;
const int direction = right_stick_direction(btn_name);
if(!strcasecmp(btn_name, "A")
|| !strcasecmp(btn_name, "Jump")
|| is_fire(btn_name) == 1)
{
map[idx] = mmap[SYS_BTN_A];
}
else if(!strcasecmp(btn_name, "B")
|| is_fire(btn_name) == 2)
{
map[idx] = mmap[SYS_BTN_B];
}
else if(!strcasecmp(btn_name, "X")
|| !strcasecmp(btn_name, "C")
|| is_fire(btn_name) == 3)
{
map[idx] = mmap[SYS_BTN_X];
}
else if(!strcasecmp(btn_name, "Y")
|| !strcasecmp(btn_name, "D")
|| is_fire(btn_name) == 4)
{
map[idx] = mmap[SYS_BTN_Y];
}
// Genesis C and Z and TG16 V and VI
else if(!strcasecmp(btn_name, "R")
|| !strcasecmp(btn_name, "RT")
|| !strcasecmp(btn_name, "Coin"))
{
map[idx] = mmap[SYS_BTN_R];
}
else if(!strcasecmp(btn_name, "L")
|| !strcasecmp(btn_name, "LT"))
{
map[idx] = mmap[SYS_BTN_L];
}
else if(!strcasecmp(btn_name, "L2"))
{
map[idx] = mmap[SYS_BTN_L2];
}
else if(!strcasecmp(btn_name, "R2"))
{
map[idx] = mmap[SYS_BTN_R2];
}
else if(!strcasecmp(btn_name, "L3"))
{
map[idx] = mmap[SYS_BTN_L3];
}
else if(!strcasecmp(btn_name, "R3"))
{
map[idx] = mmap[SYS_BTN_R3];
}
else if(!strcasecmp(btn_name, "Misc1"))
{
map[idx] = misc1;
}
else if (direction >= 0)
{
map[idx] = 0;
const int component = direction / 2;
const uint32_t primary = mmap[SYS_AXIS_X + component];
const uint32_t stick2 = mmap[SYS_AXIS2_X + component];
const uint32_t axis = (primary & MAP_FLAG_ANALOG) && primary == stick2
? mmap[SYS_AXIS1_X + component] : stick2;
if (right_directions && right_directions[direction]) map[idx] = right_directions[direction];
else if (axis != UINT32_MAX && (axis & MAP_FLAG_ANALOG))
map[idx] = KEY_EMU + ((axis & MAP_AXIS_MASK) << 1) + ((direction & 1) ^ !!(axis & MAP_FLAG_INVERT));
}
else if(!strcasecmp(btn_name, "Select")
|| !strcasecmp(btn_name, "Mode")
|| !strcasecmp(btn_name, "Game Select")
|| !strcasecmp(btn_name, "Start 2P"))
{
map[idx] = mmap[SYS_BTN_SELECT];
}
else if(!strcasecmp(btn_name, "Start")
|| !strcasecmp(btn_name, "Run")
|| !strcasecmp(btn_name, "Pause")
|| !strcasecmp(btn_name, "Start 1P"))
{
map[idx] = mmap[SYS_BTN_START];
}
n++;
}
}
int map_paddle_btn()
{
read_buttons();
for (int i = 0, n = 0; i < joy_count; i++)
{
if (!strcmp(joy_names[i], "-")) continue;
char *p = strchr(defaults ? joy_pnames[n] : joy_nnames[n], '|');
if (p && !strcasecmp(p, "|P")) return i + SYS_BTN_A;
n++;
}
return SYS_BTN_A;
}
static const char* get_std_name(uint16_t code, uint32_t *mmap)
{
if (code)
{
if (code == mmap[SYS_BTN_A]) return "[A]";
if (code == mmap[SYS_BTN_B]) return "[B]";
if (code == mmap[SYS_BTN_X]) return "[X]";
if (code == mmap[SYS_BTN_Y]) return "[Y]";
if (code == mmap[SYS_BTN_L]) return "[L]";
if (code == mmap[SYS_BTN_R]) return "[R]";
if (code == mmap[SYS_BTN_L2]) return "[L2]";
if (code == mmap[SYS_BTN_R2]) return "[R2]";
if (code == mmap[SYS_BTN_L3]) return "[L3]";
if (code == mmap[SYS_BTN_R3]) return "[R3]";
if (code == mmap[SYS_BTN_SELECT]) return "[\x96]";
if (code == mmap[SYS_BTN_START]) return "[\x16]";
return "[ ]";
}
return NULL;
}
void map_joystick_show(uint32_t *map, uint32_t *mmap, int num)
{
static char mapinfo[1024];
read_buttons();
sprintf(mapinfo, "Map (P%d):", num);
if (!num) sprintf(mapinfo, " Map:");
char *list = mapinfo + strlen(mapinfo);
// loop through core requested buttons and construct result map
for (int i = 0; i < joy_count; i++)
{
if (!strcmp(joy_names[i], "-")) continue;
const char *btn = get_std_name((uint16_t)(map[i + DPAD_COUNT]), mmap);
if (btn)
{
strcat(mapinfo, "\n");
strcat(mapinfo, btn);
strcat(mapinfo, ": ");
strcat(mapinfo, joy_names[i]);
}
}
if(strlen(list) && cfg.controller_info) Info(mapinfo, cfg.controller_info * 1000);
}
#define SYS_MAP_GAMEPAD_COUNT (SYS_MAP_POS_OSD + 3)
#define TRIGGER_CAPTURE_AXIS_COUNT (ABS_MAX + 1)
int menu_mouse_map = 0;
typedef struct
{
uint8_t valid;
int dev;
int pos;
int moved_score;
uint8_t has_axis[TRIGGER_CAPTURE_AXIS_COUNT];
input_absinfo absinfo[TRIGGER_CAPTURE_AXIS_COUNT];
} triggerCaptureState;
static void clear_mouse_map_slots(uint32_t *map)
{
memset(&map[SYS_MS_RIGHT], 0, sizeof(map[0]) * (SYS_MS_BTN_EMU - SYS_MS_RIGHT + 1));
memset(&map[SYS_AXIS_MX], 0, sizeof(map[0]) * ((SYS_AXIS_MY - SYS_AXIS_MX) + 1));
}
static void clear_joypad_map_slots(uint32_t *map)
{
uint32_t mouse_buttons[SYS_MS_BTN_EMU - SYS_MS_RIGHT + 1];
uint32_t mouse_axes[(SYS_AXIS_MY - SYS_AXIS_MX) + 1];
memcpy(mouse_buttons, &map[SYS_MS_RIGHT], sizeof(mouse_buttons));
memcpy(mouse_axes, &map[SYS_AXIS_MX], sizeof(mouse_axes));
memset(map, 0, sizeof(map[0]) * NUMBUTTONS);
memcpy(&map[SYS_MS_RIGHT], mouse_buttons, sizeof(mouse_buttons));
memcpy(&map[SYS_AXIS_MX], mouse_axes, sizeof(mouse_axes));
}
static void clear_mapping_slot_for_pos(uint32_t *map, int pos);
static int get_system_map_button_idx(int pos)
{
if (menu_mouse_map)
{
if (pos >= SYS_BTN_A && pos < (SYS_BTN_A + 8)) return SYS_MS_RIGHT + (pos - SYS_BTN_A);
return -1;
}
if (pos >= SYS_BTN_RIGHT && pos <= SYS_BTN_R) return pos;
if (pos >= SYS_MAP_BTN_L2 && pos < SYS_MAP_BTN_SELECT) return SYS_BTN_L2 + (pos - SYS_MAP_BTN_L2);
if (pos >= SYS_MAP_BTN_SELECT && pos < SYS_MAP_POS_OSD) return SYS_BTN_SELECT + (pos - SYS_MAP_BTN_SELECT);
return -1;
}
template<size_t N>
static void get_joymap_path(char (&path)[N], const char *name)
{
sprintfz(path, JOYMAP_DIR "%s", name);
}
int load_map(const char *name, void *pBuffer, int size)
{
char path[256];
get_joymap_path(path, name);
const int ret = FileLoadConfig(path, pBuffer, size);
return ret ? ret : FileLoadConfig(name, pBuffer, size);
}
static void delete_map(const char *name)
{
char path[256];
get_joymap_path(path, name);
FileDeleteConfig(name);
FileDeleteConfig(path);
}
static int save_map(const char *name, void *pBuffer, int size)
{
char path[256];
get_joymap_path(path, name);
FileDeleteConfig(name);
return FileSaveConfig(path, pBuffer, size);
}
int mapping = 0;
static int mapping_paused = 0;
static int mapping_confirm = 0;
static int mapping_confirm_result = 0;
static int mapping_invalidated = 0;
int mapping_osd_owned = 0;
int mapping_node = 0;
int mapping_button;
int mapping_dev = -1;
int mapping_type;
int mapping_count;
static int mapping_clear;
static int mapping_finish;
int mapping_set;
static int mapping_menu_only;
static int mapping_existing_asked;
static uint32_t mapping_mmap[NUMBUTTONS];
enum
{
MAP_HOLD_NONE = 0,
MAP_HOLD_SKIP,
MAP_HOLD_BACK,
MAP_HOLD_CANCEL = 4,
MAP_HOLD_FINISH = 8
};
#define MAP_HOLD_ACTION_MS 1000
#define MAP_HOLD_FEEDBACK_DELAY_MS 333
static int mapping_current_key = 0;
static int mapping_current_dev = -1;
static int mapping_hold_key = 0;
static int mapping_ignore_release_key = 0;
static int mapping_ignore_release_key2 = 0;
static uint32_t mapping_hold_timer = 0;
static uint32_t mapping_hold_feedback_timer = 0;
static int mapping_hold_action = MAP_HOLD_NONE;
static int mapping_pending_hold_action = MAP_HOLD_NONE;
static int mapping_combo_second_key = 0;
static uint8_t mapping_combo_release_mask = 0;
static uint32_t mapping_osd_hold_timer = 0;
static uint8_t mapping_osd_hold_latched = 0;
static advancedButtonMap *mapping_store = NULL;
static uint32_t tmp_axis[4];
static int tmp_axis_n = 0;
static uint8_t tmp_axis_stage_count[6] = {};
static uint8_t tmp_axis_stage_seen = 0;
static uint16_t mapping_last_axis = 0;
int mapping_key_mapped = 0;
static triggerCaptureState mapping_trigger_capture = {};
static void claim_mapping_device(int dev)
{
if (mapping_dev >= 0) return;
mapping_dev = dev;
if (!is_menu())
{
if (!mapping_set) memset(input[dev].map, 0, sizeof(input[dev].map));
return;
}
if (menu_mouse_map)
{
memcpy(input[dev].map, input[dev].mmap, sizeof(input[dev].map));
clear_mouse_map_slots(input[dev].map);
}
else
{
memcpy(mapping_mmap, input[dev].mmap, sizeof(mapping_mmap));
clear_joypad_map_slots(input[dev].map);
}
}
static int is_menu_trigger_map_pos(int pos)
{
return !menu_mouse_map && (pos == SYS_MAP_BTN_L2 || pos == (SYS_MAP_BTN_L2 + 1));
}
void clear_mapping_trigger_capture()
{
memset(&mapping_trigger_capture, 0, sizeof(mapping_trigger_capture));
}
static int mapping_node_fd()
{
return pool[(mapping_node == mapping_dev || input[mapping_node].bind == mapping_dev) ? mapping_node : mapping_dev].fd;
}
static int mapping_has_key(uint32_t code)
{
if (code >= KEY_EMU) return 1;
for (int i = 0; i < NUMDEV; i++)
{
unsigned char keybits[(KEY_MAX + 7) / 8] = {};
if ((i == mapping_dev || input[i].bind == mapping_dev) && pool[i].fd >= 0 &&
ioctl(pool[i].fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits) >= 0 && test_bit(code, keybits)) return 1;
}
return 0;
}
void prepare_mapping_trigger_capture()
{
if (!is_menu() || !mapping || !is_menu_trigger_map_pos(mapping_button) || mapping_dev < 0)
{
if (mapping_trigger_capture.valid) clear_mapping_trigger_capture();
return;
}
if (mapping_trigger_capture.valid &&
mapping_trigger_capture.dev == mapping_dev &&
mapping_trigger_capture.pos == mapping_button)
return;
clear_mapping_trigger_capture();
mapping_trigger_capture.dev = mapping_dev;
mapping_trigger_capture.pos = mapping_button;
unsigned char absbits[(ABS_MAX + 7) / 8] = {};
const int fd = mapping_node_fd();
if (fd < 0 || ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits) < 0)
return;
for (int axis = 0; axis <= ABS_MAX; axis++)
{
if (!test_bit(axis, absbits))
continue;
input_absinfo info = {};
if (ioctl(fd, EVIOCGABS(axis), &info) < 0)
continue;
mapping_trigger_capture.has_axis[axis] = 1;
mapping_trigger_capture.absinfo[axis] = info;
}
mapping_trigger_capture.valid = 1;
}
int map_entry_uses_axis(uint32_t map, uint16_t axis)
{
if (map & MAP_FLAG_ANALOG)
return map_axis_code(map) == axis;
const uint32_t last_axis_key = KEY_EMU + (ABS_MAX << 1) + 1;
return map >= KEY_EMU && map <= last_axis_key && ((map - KEY_EMU) >> 1) == axis;
}
static int trigger_axis_is_reserved(int dev, uint16_t axis)
{
if (axis >= ABS_HAT0X && axis <= ABS_HAT3Y) return 1;
if ((axis == ABS_RUDDER || axis == ABS_WHEEL) && input[dev].quirk != QUIRK_WHEEL) return 1;
for (int i = 0; i < (int)(sizeof(tmp_axis) / sizeof(tmp_axis[0])); i++)
{
if (map_entry_uses_axis(tmp_axis[i], axis)) return 1;
}
for (int i = SYS_BTN_RIGHT; i <= SYS_BTN_UP; i++)
{
if (map_entry_uses_axis(input[dev].map[i], axis)) return 1;
}
for (int i = SYS_AXIS1_X; i <= SYS_AXIS_Y; i++)
{
if (map_entry_uses_axis(input[dev].map[i], axis)) return 1;
}
for (int i = SYS_AXIS_MX; i <= SYS_AXIS_MY; i++)
{
if (map_entry_uses_axis(input[dev].map[i], axis)) return 1;
}
return 0;
}
static int trigger_axis_has_valid_baseline(int dev, uint16_t axis)
{
if (!mapping_trigger_capture.valid ||
mapping_trigger_capture.dev != dev ||
mapping_trigger_capture.pos != mapping_button ||
axis > ABS_MAX ||
!mapping_trigger_capture.has_axis[axis])
return 0;
const input_absinfo *base = &mapping_trigger_capture.absinfo[axis];
const int64_t range = (int64_t)base->maximum - base->minimum;
if (range <= 1) return 0;
const int64_t threshold = (range / 8) > 4 ? (range / 8) : 4;
const int64_t center = base->minimum + (range / 2);
return base->value <= base->minimum + threshold ||
base->value >= base->maximum - threshold ||
llabs(base->value - center) <= threshold;
}
static uint32_t build_trigger_axis_map_for_axis(int dev, uint16_t axis, const input_absinfo *absinfo, int negative, int threshold_divisor)
{
if (!absinfo || axis > ABS_MAX ||
trigger_axis_is_reserved(dev, axis) ||
!trigger_axis_has_valid_baseline(dev, axis))
return 0;
uint32_t map = axis | MAP_FLAG_ANALOG | MAP_FLAG_TRIGGER;
if (mapping_trigger_capture.valid &&
mapping_trigger_capture.dev == dev &&
mapping_trigger_capture.pos == mapping_button &&
mapping_trigger_capture.has_axis[axis])
{
const input_absinfo *base_info = &mapping_trigger_capture.absinfo[axis];
const int64_t range = (int64_t)base_info->maximum - base_info->minimum;
const int64_t delta = llabs((int64_t)absinfo->value - base_info->value);
if (range > 1 && threshold_divisor > 0)
{
const int64_t threshold = (range / threshold_divisor) > 4 ? (range / threshold_divisor) : 4;
if (delta < threshold)
return 0;
}
const int64_t center = base_info->minimum + (range / 2);
const int64_t centered_threshold = range / 4;
if (range > 1 && llabs(base_info->value - center) <= centered_threshold)
map |= MAP_FLAG_CENTERED;
if (delta)
negative = absinfo->value < base_info->value;
}
if (negative)
map |= MAP_FLAG_NEGATIVE;
return map;
}
static uint32_t build_trigger_axis_map(int dev, uint16_t keycode, const input_absinfo *absinfo)
{
if (keycode < KEY_EMU)
return 0;
return build_trigger_axis_map_for_axis(dev, (keycode - KEY_EMU) >> 1, absinfo, !(keycode & 1), 5);
}
static int trigger_axis_moved_score(uint16_t axis, const input_absinfo *absinfo)
{
if (axis > ABS_MAX || !mapping_trigger_capture.has_axis[axis])
return 0;
const input_absinfo *base_info = &mapping_trigger_capture.absinfo[axis];
const int64_t range = (int64_t)base_info->maximum - base_info->minimum;
if (range <= 1)
return 0;
const int64_t delta = llabs((int64_t)absinfo->value - base_info->value);
const int64_t threshold = (range / 32) > 4 ? (range / 32) : 4;
if (delta < threshold)
return 0;
return (int)((delta * 1024) / range);
}
static uint32_t further_moved_trigger_axis_map(int dev, uint16_t axis, const input_absinfo *absinfo)
{
const int score = trigger_axis_moved_score(axis, absinfo);
if (score <= mapping_trigger_capture.moved_score)
return 0;
const uint32_t map = build_trigger_axis_map_for_axis(dev, axis, absinfo, absinfo->value < mapping_trigger_capture.absinfo[axis].value, 32);
if (map) mapping_trigger_capture.moved_score = score;
return map;
}
static uint32_t detect_moved_trigger_axis_map(int dev)
{
if (!mapping_trigger_capture.valid ||
mapping_trigger_capture.dev != dev ||
mapping_trigger_capture.pos != mapping_button ||
mapping_node_fd() < 0)
return 0;
uint32_t best_map = 0;
mapping_trigger_capture.moved_score = 0;
for (uint16_t axis = 0; axis <= ABS_MAX; axis++)
{
if (!mapping_trigger_capture.has_axis[axis])
continue;
input_absinfo absinfo = {};
if (ioctl(mapping_node_fd(), EVIOCGABS(axis), &absinfo) < 0)
continue;
const uint32_t map = further_moved_trigger_axis_map(dev, axis, &absinfo);
if (map) best_map = map;
}
return best_map;
}
static uint32_t detect_standard_trigger_axis_map(int dev, uint16_t keycode)
{
static const uint16_t left_axes[] = { ABS_Z, ABS_BRAKE };
static const uint16_t right_axes[] = { ABS_RZ, ABS_GAS };
const uint16_t *axes;
if (keycode == BTN_TL2) axes = left_axes;
else if (keycode == BTN_TR2) axes = right_axes;
else return 0;
if (!mapping_trigger_capture.valid ||
mapping_trigger_capture.dev != dev ||
mapping_trigger_capture.pos != mapping_button ||
mapping_node_fd() < 0)
return 0;
for (int i = 0; i < 2; i++)
{
const uint16_t axis = axes[i];
if (!mapping_trigger_capture.has_axis[axis])
continue;
input_absinfo absinfo = {};
if (ioctl(mapping_node_fd(), EVIOCGABS(axis), &absinfo) < 0)
continue;
const input_absinfo *base_info = &mapping_trigger_capture.absinfo[axis];
const int64_t range = (int64_t)base_info->maximum - base_info->minimum;
if (range <= 1)
continue;
const int64_t edge_threshold = (range / 8) > 4 ? (range / 8) : 4;
const int released_near_min = base_info->value <= (base_info->minimum + edge_threshold);
const int released_near_max = base_info->value >= (base_info->maximum - edge_threshold);
if (!released_near_min && !released_near_max)
continue;
const uint32_t map = build_trigger_axis_map_for_axis(dev, axis, &absinfo, released_near_max, 0);
if (map) return map;
}
return 0;
}
static void apply_menu_analog_trigger_map(int dev, uint32_t trigger_map)
{
if (!trigger_map)
return;
const int idx = SYS_AXIS_L2 + (mapping_button - SYS_MAP_BTN_L2);
input[dev].map[idx] = trigger_map;
input[dev].mmap[idx] = trigger_map;
}
static void update_menu_analog_trigger_axis(int dev, uint16_t axis, const input_absinfo *absinfo)
{
if (!is_menu_trigger_map_pos(mapping_button) ||
mapping_dev != dev ||
!mapping_key_mapped ||
mapping_key_mapped >= KEY_EMU)
return;
apply_menu_analog_trigger_map(dev, further_moved_trigger_axis_map(dev, axis, absinfo));
}
static void map_menu_analog_trigger(int dev, uint16_t keycode, const input_absinfo *absinfo)
{
if (!is_menu_trigger_map_pos(mapping_button))
return;
uint32_t trigger_map = build_trigger_axis_map(dev, keycode, absinfo);
if (!trigger_map && keycode < KEY_EMU)
trigger_map = detect_moved_trigger_axis_map(dev);
if (!trigger_map && keycode < KEY_EMU)
trigger_map = detect_standard_trigger_axis_map(dev, keycode);
apply_menu_analog_trigger_map(dev, trigger_map);
}
static int get_tmp_axis_stage_index(int pos)
{
if (pos < -6 || pos > -1) return -1;
return pos + 6;
}
static void sync_menu_tmp_axes()
{
if (!is_menu() || menu_mouse_map || mapping_menu_only || mapping_dev < 0) return;
const int primary = (tmp_axis_n >= 4 && (!tmp_axis[0] || tmp_axis_stage_count[get_tmp_axis_stage_index(-4)] >= 2)) ? 2 : 0;
memcpy(&input[mapping_dev].mmap[SYS_AXIS1_X], tmp_axis, sizeof(tmp_axis));
memcpy(&input[mapping_dev].map[SYS_AXIS1_X], tmp_axis, sizeof(tmp_axis));
input[mapping_dev].mmap[SYS_AXIS_X] = (tmp_axis_n >= 2) ? tmp_axis[primary] : 0;
input[mapping_dev].mmap[SYS_AXIS_Y] = (tmp_axis_n >= 2) ? tmp_axis[primary + 1] : 0;
input[mapping_dev].map[SYS_AXIS_X] = input[mapping_dev].mmap[SYS_AXIS_X];
input[mapping_dev].map[SYS_AXIS_Y] = input[mapping_dev].mmap[SYS_AXIS_Y];
}
void record_menu_tmp_axis_stage()
{
const int idx = get_tmp_axis_stage_index(mapping_button);
if (idx < 0) return;
tmp_axis_stage_count[idx] = tmp_axis_n;
tmp_axis_stage_seen |= 1 << idx;
}
static void restore_menu_tmp_axis_stage(int pos)
{
const int idx = get_tmp_axis_stage_index(pos);
if (idx < 0) return;
tmp_axis_n = tmp_axis_stage_count[idx];
if (tmp_axis_n > (int)(sizeof(tmp_axis) / sizeof(tmp_axis[0]))) tmp_axis_n = sizeof(tmp_axis) / sizeof(tmp_axis[0]);
for (int i = tmp_axis_n; i < (int)(sizeof(tmp_axis) / sizeof(tmp_axis[0])); i++) tmp_axis[i] = 0;
tmp_axis_stage_seen &= (2 << idx) - 1;
mapping_last_axis = 0;
sync_menu_tmp_axes();
}
static int tmp_axis_contains_axis(uint16_t axis)
{
for (int i = 0; i < (int)(sizeof(tmp_axis) / sizeof(tmp_axis[0])); i++)
{
if ((tmp_axis[i] & MAP_FLAG_ANALOG) && map_axis_code(tmp_axis[i]) == axis) return 1;
}
return 0;
}
static int is_menu_stick2_axis(uint16_t axis)
{
const int stage = get_tmp_axis_stage_index(-2);
if (!(tmp_axis_stage_seen & (1 << stage))) return 0;
for (int i = tmp_axis_stage_count[stage]; i < tmp_axis_n; i++)
if ((tmp_axis[i] & MAP_FLAG_ANALOG) && map_axis_code(tmp_axis[i]) == axis) return 1;
return 0;
}
static int reserve_tmp_axis_slots_for_pos(int pos)
{
int min_count = 0;
switch (pos)
{
case -3: min_count = 1; break;
case -2: min_count = 2; break;
case -1: min_count = 3; break;
}
while (tmp_axis_n < min_count && tmp_axis_n < (int)(sizeof(tmp_axis) / sizeof(tmp_axis[0])))
tmp_axis[tmp_axis_n++] = 0;
return tmp_axis_n < (int)(sizeof(tmp_axis) / sizeof(tmp_axis[0]));
}
static int append_tmp_axis_for_pos(int pos, uint16_t axis, int invert)
{
if (!reserve_tmp_axis_slots_for_pos(pos)) return 0;
tmp_axis[tmp_axis_n++] = axis | MAP_FLAG_ANALOG | (invert ? MAP_FLAG_INVERT : 0);
return 1;
}
static uint64_t mapping_axis_centred = 0;
uint32_t osd_timer = 0;
static uint32_t map_advance_timer = 0;
static void clear_mapping_hold_state()
{
mapping_hold_key = 0;
mapping_hold_timer = 0;
mapping_hold_feedback_timer = 0;
mapping_hold_action = MAP_HOLD_NONE;
}
void clear_mapping_current_input()
{
mapping_current_key = 0;
mapping_current_dev = -1;
clear_mapping_hold_state();
}
void clear_mapping_osd_hold_capture()
{
mapping_osd_hold_timer = 0;
mapping_osd_hold_latched = 0;
}
static void clear_mapping_combo_state()
{
mapping_combo_second_key = 0;
mapping_combo_release_mask = 0;
clear_mapping_osd_hold_capture();
}
static void set_mapping_ignore_release_keys(int key1, int key2 = 0)
{
mapping_ignore_release_key = key1;
mapping_ignore_release_key2 = key2;
}
static void drop_mapping_menu_press()
{
mapping_osd_owned = 0;
osd_timer = 0;
if (mapping_dev < 0) return;
input[mapping_dev].osd_combo = 0;
input[mapping_dev].osd_hold_timer = 0;
}
void invalidate_map_setting()
{
if (mapping || mapping_paused || mapping_confirm) mapping_invalidated = 1;
else if (!mapping_invalidated) mapping_invalidated = 2;
mapping_osd_owned = 0;
mapping = mapping_paused = mapping_confirm = mapping_confirm_result = 0;
mapping_dev = -1;
mapping_store = NULL;
mapping_key_mapped = mapping_last_axis = 0;
mapping_pending_hold_action = MAP_HOLD_NONE;
osd_timer = map_advance_timer = 0;
clear_mapping_current_input();
clear_mapping_combo_state();
clear_mapping_trigger_capture();
set_mapping_ignore_release_keys(0);
}
static void do_map_clear()
{
if (!mapping || mapping_type > 1 || mapping_dev < 0)
return;
clear_mapping_current_input();
clear_mapping_combo_state();
set_mapping_ignore_release_keys(0);
mapping_key_mapped = 0;
mapping_last_axis = 0;
clear_mapping_trigger_capture();
map_advance_timer = 0;
osd_timer = 0;
mapping_finish = 0;