Repository navigation
Expand file tree
/
Copy pathgamecontroller_db.cpp
More file actions
815 lines (721 loc) · 23.6 KB
/
Copy pathgamecontroller_db.cpp
File metadata and controls
815 lines (721 loc) · 23.6 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
#include "gamecontroller_db.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include "input.h"
#include "file_io.h"
#include "user_io.h"
#include "profiling.h"
//Note: sdl gamecontrollerdb maps a,b,x,y differently, so we need to swap each pair (a<->b, x<->y)
static const char *sdlname_to_mister_idx[] = {
"dpright",
"dpleft",
"dpdown",
"dpup",
"b",
"a",
"y",
"x",
"leftshoulder",
"rightshoulder",
"back",
"start",
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL, //20
"guide", //21
"guide2", //Mister extension: guide2 is the 2nd button for OSD chord
"menufunc", //Dummy entry so mister->sdl map string works
"leftx",
"lefty",
"rightx",
"righty",
"asysx",
"asysy",
NULL,
NULL,
"lefttrigger",
"righttrigger",
"leftstick",
"rightstick",
};
typedef struct {
uint16_t id[4]; //bustype, vid, pid, version
char core_name[32];
char orig_core_name[32];
uint32_t map[NUMBUTTONS];
uint32_t right_directions[4];
uint32_t misc1;
} controllerdb_entry;
static controllerdb_entry db_maps[MAX_GCDB_ENTRIES] = {};
static int last_db_idx = 0;
//platform should be at the end of mapping strings. this function will null the start of platform: if found
static bool cdb_entry_matches(char *db_str)
{
char *pl_ptr = NULL;
if (!db_str || !strlen(db_str)) return false;
pl_ptr = strcasestr(db_str, "platform:");
if (!pl_ptr) return false;
*pl_ptr = 0;
pl_ptr += strlen("platform:");
if (!strncasecmp(pl_ptr, "Linux", 5)) return true;
if (!strncasecmp(pl_ptr, "MiSTer", 6))
{
char *core_ptr = NULL;
core_ptr = strcasestr(db_str, "mistercore:");
if (!core_ptr) return true; //platform:MiSTer with no core designators is a generic match
while(core_ptr)
{
char *nxt_c = NULL;
char *match_c = NULL;
core_ptr += 11;
nxt_c = strchr(core_ptr, ',');
if (!nxt_c) break;
match_c = strchr(core_ptr, '*');
if (!match_c || match_c >= nxt_c)
{
match_c = nxt_c;
}
if (!strncasecmp(core_ptr, user_io_get_core_name(), match_c - core_ptr)) return true;
if (!strncasecmp(core_ptr, user_io_get_core_name(1), match_c - core_ptr)) return true;
core_ptr = strcasestr(nxt_c, "mistercore:");
}
}
return false;
}
static int find_mister_button_num(char *sdl_name, bool *idx_high)
{
*idx_high = false;
for(size_t i = 0; i < sizeof(sdlname_to_mister_idx)/sizeof(sdlname_to_mister_idx[0]); i++)
{
const char *map_str = sdlname_to_mister_idx[i];
if (map_str && !strcmp(map_str, sdl_name)) return i;
}
if (!strcasecmp(sdl_name, "menuok"))
{
*idx_high = false;
return SYS_BTN_MENU_FUNC;
}
if (!strcasecmp(sdl_name, "menuesc"))
{
*idx_high = true;
return SYS_BTN_MENU_FUNC;
}
return -1;
}
static bool is_axis_token(const char *btn_name)
{
if (!btn_name || !btn_name[0]) return false;
if (btn_name[0] == 'a') return true;
return (btn_name[0] == '-' || btn_name[0] == '+') && btn_name[1] == 'a';
}
static bool axis_token_is_inverted(const char *btn_name)
{
const size_t len = btn_name ? strlen(btn_name) : 0;
return len && btn_name[len - 1] == '~';
}
static uint16_t axis_from_mapped_code(int mapped_code)
{
return (mapped_code >= KEY_EMU) ? ((mapped_code - KEY_EMU) >> 1) : (uint16_t)mapped_code;
}
static uint32_t trigger_axis_map_from_token(const char *btn_name, int mapped_code)
{
if (!is_axis_token(btn_name)) return 0;
const uint16_t axis = axis_from_mapped_code(mapped_code);
if (axis > ABS_MAX) return 0;
const bool inverted = axis_token_is_inverted(btn_name);
uint32_t map = axis | MAP_FLAG_ANALOG | MAP_FLAG_TRIGGER;
if (btn_name[0] == '-' || btn_name[0] == '+')
{
map |= MAP_FLAG_CENTERED;
if (btn_name[0] == '-') map |= MAP_FLAG_NEGATIVE;
if (inverted) map |= MAP_FLAG_INVERT;
}
else if (inverted)
{
map |= MAP_FLAG_NEGATIVE;
}
return map;
}
static int trigger_button_code_from_axis_map(uint32_t axis_map)
{
if (!axis_map) return -1;
return KEY_EMU + ((axis_map & MAP_AXIS_MASK) << 1) + ((axis_map & MAP_FLAG_NEGATIVE) ? 0 : 1);
}
static bool print_axis_mapping(const char *sdlname, uint32_t axis_map, uint16_t *abs_map)
{
if (!axis_map) return false;
const uint16_t axis_idx = axis_map & MAP_AXIS_MASK;
for (unsigned int j = 0; j < GCDB_AXIS_MAP_SIZE; j++)
{
if (abs_map[j] == axis_idx)
{
if (axis_map & MAP_FLAG_CENTERED)
printf("%s:%ca%d%s,", sdlname, (axis_map & MAP_FLAG_NEGATIVE) ? '-' : '+', j,
(axis_map & MAP_FLAG_INVERT) ? "~" : "");
else
printf("%s:a%d%s,", sdlname, j, (axis_map & MAP_FLAG_NEGATIVE) ? "~" : "");
return true;
}
}
return false;
}
static int parse_mapping_index(const char *text, int max_index, const char **suffix)
{
if (!text) return -1;
char *end = NULL;
long index = strtol(text, &end, 10);
if (end == text || index < 0 || index > max_index) return -1;
if (suffix) *suffix = end;
return index;
}
static int find_linux_code_for_button(const char *btn_name, uint16_t *btn_map, uint16_t *abs_map)
{
if (!btn_name || !strlen(btn_name)) return -1;
char btn_sw = btn_name[0];
int a_edge = 0;
if (btn_name[0] == '-') a_edge = 1;
if (btn_name[0] == '+') a_edge = 2;
if (a_edge != 0)
{
btn_sw = btn_name[1];
}
switch(btn_sw)
{
case 'b':
{
//Normal button
const char *suffix = NULL;
int bidx = parse_mapping_index(btn_name + 1, GCDB_BUTTON_MAP_SIZE - 1, &suffix);
if (bidx < 0 || *suffix || btn_map[bidx] == UINT16_MAX) return -1;
return btn_map[bidx];
}
case 'a':
{
const char *suffix = NULL;
int aidx = parse_mapping_index(btn_name + (a_edge != 0 ? 2 : 1), GCDB_AXIS_MAP_SIZE - 1, &suffix);
if (aidx < 0 || (*suffix && strcmp(suffix, "~")) || abs_map[aidx] == UINT16_MAX) return -1;
int abs_axis = abs_map[aidx];
if (a_edge)
{
if (*suffix) a_edge = 3 - a_edge;
return KEY_EMU + (abs_axis << 1) - 1 + a_edge;
}
return abs_axis;
}
case 'h':
//Mister creates fake digital buttons for hats that depend on the code and axis direction.
{
const char *dot_ptr = NULL;
int hidx = parse_mapping_index(btn_name + 1, (ABS_HAT3Y - ABS_HAT0X) / 2, &dot_ptr);
if (hidx < 0 || *dot_ptr != '.') return -1;
int base_hat = ABS_HAT0X + hidx*2;
//base_hat is X, base_hat+1 is Y
const char *suffix = NULL;
int hat_dir = parse_mapping_index(dot_ptr + 1, 8, &suffix);
if (hat_dir >= 0 && !*suffix)
{
switch(hat_dir)
{
case 1: //UP
return KEY_EMU + ((base_hat+1) << 1); //up is min value of Y axis
case 2: // RIGHT
return KEY_EMU + (base_hat << 1) + 1; // (axis << 1) -1 + 2 right is max value
case 4: //DOWN
return KEY_EMU + ((base_hat+1) << 1) + 1; //down is max value of Y axis
case 8: //LEFT
return KEY_EMU + (base_hat << 1); //(axis << 1) - 1 + 1 left is min value
default:
return -1;
}
}
break;
}
default:
return -1;
}
return -1;
}
#define test_bit(bit, array) (array [bit / 8] & (1 << (bit % 8)))
bool get_ctrl_index_maps(int dev_fd, const char *guid, uint16_t *btn_map, uint16_t *abs_map)
{
if (!btn_map || !abs_map) return false;
bool read = true;
unsigned char keybits[(KEY_MAX + 8) / 8] = {};
unsigned char absbits[(ABS_MAX + 8) / 8] = {};
uint16_t btn_cnt = 0;
uint16_t abs_cnt = 0;
if (guid)
printf("Gamecontrollerdb: mapping buttons for %s ", guid);
if (ioctl(dev_fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits) >= 0)
{
for (int i = BTN_JOYSTICK; i <= KEY_MAX; i++)
{
if (test_bit(i, keybits))
{
//printf("b%d->%d ", btn_cnt, i);
btn_map[btn_cnt] = i;
btn_cnt++;
}
}
for (int i = 0; i < BTN_JOYSTICK; i++)
{
if (test_bit(i, keybits))
{
//printf("b%d -> %d ", btn_cnt, i);
btn_map[btn_cnt] = i;
btn_cnt++;
}
}
if (guid)
printf("\n");
}
else read = false;
if (guid)
printf("Gamecontrollerdb: mapping analog axes for %s ", guid);
if (ioctl(dev_fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits) >= 0)
{
//The "correct" way is to test all the way to ABS_MAX and skip any hats the device has.
//Mister handles hats differently and it is unlikely most things in the db files have axes beyond
//The normal sticks+triggers...
for (int i = 0; i < ABS_HAT0X; i++)
{
if (test_bit(i, absbits))
{
//printf("a%d->%d ", abs_cnt, i);
abs_map[abs_cnt] = i;
abs_cnt++;
}
}
//Just for debugging purposes...
for (int i = ABS_HAT0X; i < ABS_MAX; i++)
{
if (test_bit(i, absbits))
{
//printf("(debug)a%d->%d ", abs_cnt, i);
abs_cnt++;
}
}
}
else read = false;
if(guid)
printf("\n");
return read;
}
void gcdb_show_string_for_ctrl_map(uint16_t bustype, uint16_t vid, uint16_t pid, uint16_t version,int dev_fd, const char *name, uint32_t *cur_map, const uint32_t *right_directions, uint32_t misc1)
{
static char map_guid[GUID_LEN] = {0};
static uint16_t btn_map[GCDB_BUTTON_MAP_SIZE] = {0xFFFF};
static uint16_t abs_map[GCDB_AXIS_MAP_SIZE] = {0xFFFF};
if (!cur_map) return;
uint32_t hold_map[NUMBUTTONS];
if (osd_map_is_hold(cur_map))
{
memcpy(hold_map, cur_map, sizeof(hold_map));
hold_map[SYS_BTN_OSD_1] = hold_map[SYS_BTN_OSD_2];
cur_map = hold_map;
}
char guid_str[GUID_LEN] = {0};
sprintf(guid_str, "%04x0000%04x0000%04x0000%04x0000", (uint16_t)(bustype << 8 | bustype >> 8), (uint16_t)( vid << 8 | vid >> 8), (uint16_t)(pid << 8 | pid >> 8), (uint16_t)(version << 8 | version >> 8));
if (strcmp(map_guid, guid_str))
{
memset(btn_map, 0xFFFF, sizeof(btn_map));
memset(abs_map, 0xFFFF, sizeof(abs_map));
map_guid[0] = 0;
if (get_ctrl_index_maps(dev_fd, guid_str, btn_map, abs_map)) snprintf(map_guid, sizeof(map_guid), "%s", guid_str);
}
//Directions/hats+Buttons
printf("Gamecontrollerdb for mapping: %s,%s,", guid_str, name);
for(int i=0; i < NUMBUTTONS; i++)
{
if (i >= SYS_MS_RIGHT && i <= SYS_MS_BTN_EMU) continue; //Skip mouse buttons
if (i == SYS_BTN_OSD_2 && !osd_map_second_is_separate(cur_map)) continue;
if (i >= (int)(sizeof(sdlname_to_mister_idx)/sizeof(sdlname_to_mister_idx[0]))) continue;
const char *sdlname = sdlname_to_mister_idx[i];
if (!sdlname) continue;
if (i == SYS_BTN_L2 || i == SYS_BTN_R2)
{
const int trigger_axis_idx = SYS_AXIS_L2 + (i - SYS_BTN_L2);
if (print_axis_mapping(sdlname, cur_map[trigger_axis_idx], abs_map))
continue;
}
if (cur_map[i])
{
uint32_t i_code = cur_map[i] & 0xFFFF;
if (i_code >= KEY_EMU) //hat/analog
{
bool is_max = i_code & 0x1;
uint16_t axis_idx = (i_code - KEY_EMU) >> 1;
if (axis_idx >= ABS_HAT0X && axis_idx <= ABS_HAT3Y)
{
uint8_t hat_num = (axis_idx - ABS_HAT0X)/2;
bool axis_is_y = (axis_idx - ABS_HAT0X)%2;
uint8_t hat_sub = 0;
if (axis_is_y)
{
hat_sub = is_max ? 4 : 1;
} else {
hat_sub = is_max ? 2 : 8;
}
if (hat_sub)
{
printf("%s:h%d.%d,", sdlname, hat_num, hat_sub);
}
} else {
//Mister 'fake' analog digital inputs.
for(unsigned int j=0; j < sizeof(abs_map)/sizeof(uint16_t); j++)
{
if (abs_map[j] == axis_idx)
{
if (is_max)
printf("%s:+a%d,", sdlname, j);
else
printf("%s:-a%d,", sdlname, j);
break;
}
}
}
} else if (cur_map[i] & MAP_FLAG_ANALOG) { //Analog
for(unsigned int j=0; j < sizeof(abs_map)/sizeof(uint16_t); j++)
{
if (abs_map[j] == i_code)
{
printf("%s:a%d%s,", sdlname, j, (cur_map[i] & MAP_FLAG_INVERT) ? "~" : "");
break;
}
}
} else {
//Ugh
int menu_func_cnt = 0;
for(unsigned int j=0; j < sizeof(btn_map)/sizeof(uint16_t); j++)
{
if (btn_map[j] == 0xFFFF) break;
if (i == SYS_BTN_MENU_FUNC)
{
if (btn_map[j] == (cur_map[i] & 0xFFFF))
{
printf("menuok:b%d,", j);
menu_func_cnt++;
} else if (btn_map[j] == (cur_map[i] >> 16)) {
printf("menuesc:b%d,",j);
menu_func_cnt++;
}
if (menu_func_cnt == 2) break;
} else if (btn_map[j] == i_code) {
printf("%s:b%d,", sdlname, j);
break;
}
}
}
}
}
const char *direction_names[] = {"rightx-", "rightx+", "righty-", "righty+"};
if (right_directions)
for (int direction = 0; direction < 4; direction++)
if (right_directions[direction])
for (unsigned int j = 0; j < GCDB_BUTTON_MAP_SIZE; j++)
if (btn_map[j] == right_directions[direction])
{
printf("%s:b%u,", direction_names[direction], j);
break;
}
if (misc1)
for (unsigned int j = 0; j < GCDB_BUTTON_MAP_SIZE; j++)
if (btn_map[j] == misc1)
{
printf("misc1:b%u,", j);
break;
}
printf("platform:MiSTer,\n");
}
static bool parse_mapping_string(char *map_str, char *guid, int dev_fd, uint32_t *fill_map, uint32_t *right_directions, uint32_t *misc1)
{
static char map_guid[GUID_LEN] = {0};
static uint16_t btn_map[GCDB_BUTTON_MAP_SIZE] = {};
static uint16_t abs_map[GCDB_AXIS_MAP_SIZE] = {};
if (!map_str || !guid || !fill_map) return false;
//gamecontrollerdb references buttons/axes numerically, and the number depends on the actual buttons supported
//by the controller. Build a map of button number -> keycode (same with axes)
if (strcmp(map_guid, guid)) //New guid, map out button indexes for this new controller
{
memset(btn_map, 0xFF, sizeof(btn_map));
memset(abs_map, 0xFF, sizeof(abs_map));
map_guid[0] = 0;
if (!get_ctrl_index_maps(dev_fd, guid, btn_map, abs_map)) return false;
snprintf(map_guid, sizeof(map_guid), "%s", guid);
}
char l_btn[20] = {};
char m_btn[20] = {};
bool in_m_btn = true;
size_t i = 0;
bool map_parsed = false;
bool whole_axis_right = false, whole_axis_up = false;
char *cur_str = map_str;
while (cur_str && *cur_str)
{
if (*cur_str == ':')
{
i = 0;
in_m_btn = false;
} else if (*cur_str == ',') {
i = 0;
in_m_btn = true;
if (l_btn[0] && m_btn[0])
{
bool m_button_high = false;
int m_button_num = find_mister_button_num(m_btn, &m_button_high);
int l_button_code = find_linux_code_for_button(l_btn, btn_map, abs_map);
const char *direction_names[] = {"rightx-", "rightx+", "righty-", "righty+"};
for (int direction = 0; direction < 4; direction++)
{
if (!strcmp(m_btn, direction_names[direction]) && l_button_code >= 0 && l_btn[0] == 'b')
{
right_directions[direction] = l_button_code;
map_parsed = true;
}
}
if (!strcmp(m_btn, "misc1") && l_button_code >= 0 && l_btn[0] == 'b')
{
*misc1 = l_button_code;
map_parsed = true;
}
if (m_button_num != -1 && l_button_code != -1)
{
map_parsed = true;
if ((m_button_num == SYS_BTN_L2 || m_button_num == SYS_BTN_R2) && is_axis_token(l_btn))
{
const int trigger_axis_idx = SYS_AXIS_L2 + (m_button_num - SYS_BTN_L2);
fill_map[trigger_axis_idx] = trigger_axis_map_from_token(l_btn, l_button_code);
if (fill_map[trigger_axis_idx])
fill_map[m_button_num] = trigger_button_code_from_axis_map(fill_map[trigger_axis_idx]);
}
else
{
if (l_btn[0] == 'a' && !(m_button_num >= SYS_AXIS1_X && m_button_num <= SYS_AXIS_Y))
{
l_button_code = KEY_EMU + (l_button_code << 1) + (axis_token_is_inverted(l_btn) ? 0 : 1);
whole_axis_right |= m_button_num == SYS_BTN_RIGHT;
whole_axis_up |= m_button_num == SYS_BTN_UP;
}
fill_map[m_button_num] = m_button_high ? ((l_button_code << 16) | fill_map[m_button_num]) : ((l_button_code & 0xFFFF) | fill_map[m_button_num]);
}
if (m_button_num >= SYS_AXIS1_X && m_button_num <= SYS_AXIS_Y)
{
fill_map[m_button_num] = l_button_code | MAP_FLAG_ANALOG |
(axis_token_is_inverted(l_btn) ? MAP_FLAG_INVERT : 0);
}
}
}
bzero(l_btn, sizeof(l_btn));
bzero(m_btn, sizeof(m_btn));
} else if (*cur_str == ' ' || *cur_str == '\t') {
} else if (in_m_btn) {
//Just truncate button names if they are too big
if (i + 1 < sizeof(m_btn))
{
m_btn[i] = *cur_str;
i++;
}
} else {
if (i + 1 < sizeof(l_btn))
{
l_btn[i] = *cur_str;
i++;
}
}
cur_str++;
}
if (map_parsed)
{
const uint32_t l2 = fill_map[SYS_AXIS_L2];
const uint32_t r2 = fill_map[SYS_AXIS_R2];
if ((l2 & MAP_FLAG_TRIGGER) && (r2 & MAP_FLAG_TRIGGER) && l2 != r2 &&
(l2 & MAP_AXIS_MASK) == (r2 & MAP_AXIS_MASK) && fill_map[SYS_BTN_L2] == fill_map[SYS_BTN_R2])
fill_map[SYS_BTN_R2] ^= 1;
for (int i = SYS_BTN_L2; i <= SYS_BTN_R3; i++)
{
for (int n = SYS_BTN_RIGHT; fill_map[i] && n <= SYS_BTN_START; n++)
{
if ((fill_map[n] & 0xFFFF) != fill_map[i] && (fill_map[n] >> 16) != fill_map[i]) continue;
fill_map[i] = 0;
if (i <= SYS_BTN_R2) fill_map[SYS_AXIS_L2 + i - SYS_BTN_L2] = 0;
}
}
if ((fill_map[SYS_BTN_MENU_FUNC] & 0xFFFF) == 0)
{
fill_map[SYS_BTN_MENU_FUNC] = fill_map[SYS_BTN_A] & 0xFFFF;
}
if ((fill_map[SYS_BTN_MENU_FUNC] & 0xFFFF0000) == 0)
{
fill_map[SYS_BTN_MENU_FUNC] = (fill_map[SYS_BTN_B] << 16) | fill_map[SYS_BTN_MENU_FUNC];
}
osd_map_fill_second(fill_map);
//Some controllers without analog sticks use the left stick axes for the dpad instead of using the hat axes
//When these are mapped to dpad directions in an entry, it results in the mister
//having no mapping for the corresponding analog axis.
//any analog axis that isn't mapped to either SYS_AXIS1 or SYS_AXIS2 is treated like a trigger and only generates
//'fake' digital inputs on the axis max. This results in only two dpad directions working.
//Populate the entries for SYS_AXIS1 based on what the dpad was mapped to, but only if the entry didn't map the analog
//axes itself.
if (!fill_map[SYS_AXIS1_X] && fill_map[SYS_BTN_RIGHT] > KEY_EMU && !whole_axis_right)
{
uint16_t axis_idx = (fill_map[SYS_BTN_RIGHT] - KEY_EMU) >> 1;
fill_map[SYS_AXIS1_X] = axis_idx | MAP_FLAG_ANALOG;
}
if (!fill_map[SYS_AXIS1_Y] && fill_map[SYS_BTN_UP] > KEY_EMU && !whole_axis_up)
{
uint16_t axis_idx = (fill_map[SYS_BTN_UP] - KEY_EMU) >> 1;
fill_map[SYS_AXIS1_Y] = axis_idx | MAP_FLAG_ANALOG;
}
if (fill_map[SYS_AXIS_X] == 0) fill_map[SYS_AXIS_X] = fill_map[SYS_AXIS1_X];
if (fill_map[SYS_AXIS_Y] == 0) fill_map[SYS_AXIS_Y] = fill_map[SYS_AXIS1_Y];
for (int trigger = 0; trigger < 2; trigger++)
{
const uint32_t axis_map = fill_map[SYS_AXIS_L2 + trigger];
if (!(axis_map & MAP_FLAG_TRIGGER)) continue;
for (int stick = SYS_AXIS1_X; stick <= SYS_AXIS_Y; stick++)
{
if ((fill_map[stick] & MAP_FLAG_ANALOG) && (fill_map[stick] & MAP_AXIS_MASK) == (axis_map & MAP_AXIS_MASK))
{
fill_map[SYS_AXIS_L2 + trigger] = 0;
fill_map[SYS_BTN_L2 + trigger] = 0;
break;
}
}
}
}
return map_parsed;
}
#define GCDB_DIR "/media/fat/linux/gamecontrollerdb/"
bool read_controller_map_from_file(char *fname, char *guid, int dev_fd, uint32_t *fill_map, uint32_t *right_directions, uint32_t *misc1)
{
if (!fname || !guid || strlen(guid) != GUID_LEN - 1 || !fill_map) return false;
fileTextReader reader;
char matched[1024] = {};
char *map_start = NULL;
if (FileOpenTextReader(&reader, fname))
{
const char *line;
printf("Gamecontrollerdb: searching for GUID %s in file %s\n", guid, fname);
while ((line = FileReadLine(&reader)))
{
if (line[0] == '#') continue;
const char *gcom = strchr(line, ',');
if (!gcom || gcom - line != GUID_LEN - 1 || strncasecmp(line, guid, GUID_LEN - 1)) continue;
if (cdb_entry_matches((char *)gcom))
{
map_start = strchr((char *)gcom + 1, ',');
if (map_start)
{
snprintf(matched, sizeof(matched), "%s", map_start + 1);
}
}
}
}
if (matched[0] != 0)
{
printf("Gamecontrollerdb: found match, using config %s\n", matched);
return parse_mapping_string(matched, guid, dev_fd, fill_map, right_directions, misc1);
}
return false;
}
static bool read_controller_name_from_file(const char *fname, const char *guid, char *name, size_t len)
{
fileTextReader reader;
bool found = false;
if (!FileOpenTextReader(&reader, fname)) return false;
const char *line;
while ((line = FileReadLine(&reader)))
{
if (line[0] == '#') continue;
const char *gcom = strchr(line, ',');
if (!gcom || gcom - line != GUID_LEN - 1 || strncasecmp(line, guid, GUID_LEN - 1)) continue;
char entry[1024];
snprintf(entry, sizeof(entry), "%s", gcom);
if (!cdb_entry_matches(entry)) continue;
const char *start = gcom + 1;
const char *end = strchr(start, ',');
if (!end) continue;
snprintf(name, len, "%.*s", (int)(end - start), start);
found = true;
}
return found;
}
bool gcdb_name_for_controller(uint16_t bustype, uint16_t vid, uint16_t pid, uint16_t version, char *name, size_t len)
{
char guid_str[GUID_LEN] = {};
sprintf(guid_str, "%04x0000%04x0000%04x0000%04x0000", (uint16_t)(bustype << 8 | bustype >> 8), (uint16_t)( vid << 8 | vid >> 8), (uint16_t)(pid << 8 | pid >> 8), (uint16_t)(version << 8 | version >> 8));
if (!name || !len) return false;
name[0] = 0;
if (read_controller_name_from_file(GCDB_DIR "gamecontrollerdb_user.txt", guid_str, name, len)) return true;
return read_controller_name_from_file(GCDB_DIR "gamecontrollerdb.txt", guid_str, name, len);
}
static int gcdb_controller_idx(uint16_t bustype, uint16_t vid, uint16_t pid, uint16_t version)
{
for (int i=0; i < MAX_GCDB_ENTRIES; i++)
{
if (db_maps[i].id[0] == bustype && db_maps[i].id[1] == vid &&
db_maps[i].id[2] == pid && db_maps[i].id[3] == version &&
!strcmp(db_maps[i].core_name, user_io_get_core_name()) &&
!strcmp(db_maps[i].orig_core_name, user_io_get_core_name(1)))
{
return i;
}
}
return -1;
}
static void gcdb_cache_controller_map(uint16_t bustype, uint16_t vid, uint16_t pid, uint16_t version, uint32_t *button_map, const uint32_t *right_directions, uint32_t misc1)
{
if (gcdb_controller_idx(bustype, vid, pid, version) > -1)
{
//Already cached
return;
}
db_maps[last_db_idx].id[0] = bustype;
db_maps[last_db_idx].id[1] = vid;
db_maps[last_db_idx].id[2] = pid;
db_maps[last_db_idx].id[3] = version;
snprintf(db_maps[last_db_idx].core_name, sizeof(db_maps[last_db_idx].core_name), "%s", user_io_get_core_name());
snprintf(db_maps[last_db_idx].orig_core_name, sizeof(db_maps[last_db_idx].orig_core_name), "%s", user_io_get_core_name(1));
memcpy(db_maps[last_db_idx].map, button_map, sizeof(uint32_t)*NUMBUTTONS);
memcpy(db_maps[last_db_idx].right_directions, right_directions, sizeof(db_maps[last_db_idx].right_directions));
db_maps[last_db_idx].misc1 = misc1;
last_db_idx = (last_db_idx +1) % MAX_GCDB_ENTRIES;
}
bool gcdb_map_for_controller(uint16_t bustype, uint16_t vid, uint16_t pid, uint16_t version, int dev_fd, uint32_t *fill_map, uint32_t *right_directions, uint32_t *misc1)
{
PROFILE_FUNCTION();
*misc1 = 0;
char guid_str[GUID_LEN] = {};
int cache_idx = gcdb_controller_idx(bustype, vid, pid, version);
if (cache_idx != -1)
{
memcpy(fill_map, db_maps[cache_idx].map, sizeof(uint32_t)*NUMBUTTONS);
memcpy(right_directions, db_maps[cache_idx].right_directions, sizeof(db_maps[cache_idx].right_directions));
*misc1 = db_maps[cache_idx].misc1;
return true;
}
sprintf(guid_str, "%04x0000%04x0000%04x0000%04x0000", (uint16_t)(bustype << 8 | bustype >> 8), (uint16_t)( vid << 8 | vid >> 8), (uint16_t)(pid << 8 | pid >> 8), (uint16_t)(version << 8 | version >> 8));
char path[256] = {GCDB_DIR};
strcat(path, "gamecontrollerdb_user.txt");
bool found_entry = false;
if (!(found_entry = read_controller_map_from_file(path, guid_str, dev_fd, fill_map, right_directions, misc1)))
{
strcpy(path, GCDB_DIR);
strcat(path, "gamecontrollerdb.txt");
found_entry = read_controller_map_from_file(path, guid_str, dev_fd, fill_map, right_directions, misc1);
}
if (found_entry)
{
gcdb_cache_controller_map(bustype, vid, pid, version, fill_map, right_directions, *misc1);
return true;
}
return false;
}