Commit 345b4d7e14a7ac1249bb39fcde71a592f4c66648

Sam Lantinga 2020-05-29T13:37:21

Fixed bug 5161 - Autodetect controller mappings based on the Linux Gamepad Specification Jan Bujak I wrote a new driver for my gamepad on Linux. I'd like SDL to support it out-of-box, as currently it just treats it as a generic joystick instead of a gamepad. From what I can see the only way to do that is to either 1) pick one of the already supported controllers' PID, VID and button layouts and have my driver send that (effectively lying that it's something else), or 2) submit a preconfigured, hardcoded mapping to SDL. Both of those, in my opinion, are silly when we already have the Linux Gamepad Specification which standarizes this: https://www.kernel.org/doc/html/v4.15/input/gamepad.html Unfortunately SDL doesn't make use of it currently. So I've took it upon myself to add it; patch is in the attachments. Basically what the patch does is that if SDL finds no built-it controller mappings for a given joystick it then asks the joystick backend to autodetect it, and that uses the relevant evdev bits to figure out which button/axis is which. (See the specs for more details.) With this patch applied my own driver for my controller works out-of-box with SDL with no extra configuration and is correctly recognized as a gamepad; this is also going to be the case for any other driver which follows the Linux Gamepad Specification.

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
diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c
index f3f4b57..9bf688a 100644
--- a/src/joystick/SDL_gamecontroller.c
+++ b/src/joystick/SDL_gamecontroller.c
@@ -1089,6 +1089,91 @@ static ControllerMapping_t *SDL_PrivateGetControllerMappingForNameAndGUID(const 
     return mapping;
 }
 
+static void SDL_PrivateAppendToMappingString(char *mapping_string,
+                                             size_t mapping_string_len,
+                                             const char *input_name,
+                                             SDL_InputMapping *mapping)
+{
+    char buffer[16];
+    if (mapping->kind == EMappingKind_None) {
+        return;
+    }
+
+    SDL_strlcat(mapping_string, input_name, mapping_string_len);
+    SDL_strlcat(mapping_string, ":", mapping_string_len);
+    switch (mapping->kind) {
+        case EMappingKind_Button:
+            SDL_snprintf(buffer, sizeof(buffer), "b%i", mapping->target);
+            break;
+        case EMappingKind_Axis:
+            SDL_snprintf(buffer, sizeof(buffer), "a%i", mapping->target);
+            break;
+        case EMappingKind_Hat:
+            SDL_snprintf(buffer, sizeof(buffer), "h%i.%i", mapping->target >> 4, mapping->target & 0x0F);
+            break;
+        default:
+            SDL_assert(SDL_FALSE);
+    }
+
+    SDL_strlcat(mapping_string, buffer, mapping_string_len);
+    SDL_strlcat(mapping_string, ",", mapping_string_len);
+}
+
+static ControllerMapping_t *SDL_PrivateGenerateAutomaticControllerMapping(const char *name,
+                                                                          SDL_JoystickGUID guid,
+                                                                          SDL_GamepadMapping *raw_map)
+{
+    SDL_bool existing;
+    char name_string[128];
+    char mapping[1024];
+
+    /* Remove any commas in the name */
+    SDL_strlcpy(name_string, name, sizeof(name_string));
+    {
+        char *spot;
+        for (spot = name_string; *spot; ++spot) {
+            if (*spot == ',') {
+                *spot = ' ';
+            }
+        }
+    }
+    SDL_snprintf(mapping, sizeof(mapping), "none,%s,", name_string);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "a", &raw_map->a);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "b", &raw_map->b);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "x", &raw_map->x);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "y", &raw_map->y);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "back", &raw_map->back);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "guide", &raw_map->guide);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "start", &raw_map->start);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "leftstick", &raw_map->leftstick);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "rightstick", &raw_map->rightstick);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "leftshoulder", &raw_map->leftshoulder);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "rightshoulder", &raw_map->rightshoulder);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "dpup", &raw_map->dpup);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "dpdown", &raw_map->dpdown);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "dpleft", &raw_map->dpleft);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "dpright", &raw_map->dpright);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "leftx", &raw_map->leftx);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "lefty", &raw_map->lefty);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "rightx", &raw_map->rightx);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "righty", &raw_map->righty);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "lefttrigger", &raw_map->lefttrigger);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "righttrigger", &raw_map->righttrigger);
+
+    /* Remove trailing comma */
+    {
+        int pos = (int)SDL_strlen(mapping) - 1;
+        if (pos >= 0) {
+            if (mapping[pos] == ',') {
+                mapping[pos] = '\0';
+            }
+        }
+    }
+
+    return SDL_PrivateAddMappingForGUID(guid, mapping,
+                      &existing, SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT);
+}
+
 static ControllerMapping_t *SDL_PrivateGetControllerMapping(int device_index)
 {
     const char *name;
@@ -1106,6 +1191,13 @@ static ControllerMapping_t *SDL_PrivateGetControllerMapping(int device_index)
     name = SDL_JoystickNameForIndex(device_index);
     guid = SDL_JoystickGetDeviceGUID(device_index);
     mapping = SDL_PrivateGetControllerMappingForNameAndGUID(name, guid);
+    if (!mapping) {
+        SDL_GamepadMapping raw_map;
+        if (SDL_PrivateJoystickGetAutoGamepadMapping(device_index, &raw_map)) {
+            mapping = SDL_PrivateGenerateAutomaticControllerMapping(name, guid, &raw_map);
+        }
+    }
+
     SDL_UnlockJoysticks();
     return mapping;
 }
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index 4722b60..1ac8d5e 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -584,6 +584,21 @@ SDL_PrivateJoystickValid(SDL_Joystick * joystick)
     return valid;
 }
 
+SDL_bool
+SDL_PrivateJoystickGetAutoGamepadMapping(int device_index, SDL_GamepadMapping * out)
+{
+    SDL_JoystickDriver *driver;
+    SDL_bool is_ok = SDL_FALSE;
+
+    SDL_LockJoysticks();
+    if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
+        is_ok = driver->GetGamepadMapping(device_index, out);
+    }
+    SDL_UnlockJoysticks();
+
+    return is_ok;
+}
+
 /*
  * Get the number of multi-dimensional axis controls on a joystick
  */
diff --git a/src/joystick/SDL_joystick_c.h b/src/joystick/SDL_joystick_c.h
index 392a5f8..02bf2b5 100644
--- a/src/joystick/SDL_joystick_c.h
+++ b/src/joystick/SDL_joystick_c.h
@@ -111,6 +111,49 @@ extern void SDL_PrivateJoystickBatteryLevel(SDL_Joystick * joystick,
 /* Internal sanity checking functions */
 extern SDL_bool SDL_PrivateJoystickValid(SDL_Joystick * joystick);
 
+typedef enum
+{
+    EMappingKind_None = 0,
+    EMappingKind_Button = 1,
+    EMappingKind_Axis = 2,
+    EMappingKind_Hat = 3
+} EMappingKind;
+
+typedef struct _SDL_InputMapping
+{
+    EMappingKind kind;
+    Uint8 target;
+} SDL_InputMapping;
+
+typedef struct _SDL_GamepadMapping
+{
+    SDL_InputMapping a;
+    SDL_InputMapping b;
+    SDL_InputMapping x;
+    SDL_InputMapping y;
+    SDL_InputMapping back;
+    SDL_InputMapping guide;
+    SDL_InputMapping start;
+    SDL_InputMapping leftstick;
+    SDL_InputMapping rightstick;
+    SDL_InputMapping leftshoulder;
+    SDL_InputMapping rightshoulder;
+    SDL_InputMapping dpup;
+    SDL_InputMapping dpdown;
+    SDL_InputMapping dpleft;
+    SDL_InputMapping dpright;
+    SDL_InputMapping leftx;
+    SDL_InputMapping lefty;
+    SDL_InputMapping rightx;
+    SDL_InputMapping righty;
+    SDL_InputMapping lefttrigger;
+    SDL_InputMapping righttrigger;
+} SDL_GamepadMapping;
+
+/* Function to get autodetected gamepad controller mapping from the driver */
+extern SDL_bool SDL_PrivateJoystickGetAutoGamepadMapping(int device_index,
+                                                         SDL_GamepadMapping *out);
+
 #endif /* SDL_joystick_c_h_ */
 
 /* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/joystick/SDL_sysjoystick.h b/src/joystick/SDL_sysjoystick.h
index e0ac967..982e953 100644
--- a/src/joystick/SDL_sysjoystick.h
+++ b/src/joystick/SDL_sysjoystick.h
@@ -136,6 +136,9 @@ typedef struct _SDL_JoystickDriver
     /* Function to perform any system-specific joystick related cleanup */
     void (*Quit)(void);
 
+    /* Function to get the autodetected controller mapping; returns false if there isn't any. */
+    SDL_bool (*GetGamepadMapping)(int device_index, SDL_GamepadMapping * out);
+
 } SDL_JoystickDriver;
 
 /* Windows and Mac OSX has a limit of MAX_DWORD / 1000, Linux kernel has a limit of 0xFFFF */
diff --git a/src/joystick/android/SDL_sysjoystick.c b/src/joystick/android/SDL_sysjoystick.c
index 6ea0446..1ff322e 100644
--- a/src/joystick/android/SDL_sysjoystick.c
+++ b/src/joystick/android/SDL_sysjoystick.c
@@ -693,6 +693,12 @@ ANDROID_JoystickQuit(void)
 #endif /* 0 */
 }
 
+static SDL_bool
+ANDROID_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
+{
+    return SDL_FALSE;
+}
+
 SDL_JoystickDriver SDL_ANDROID_JoystickDriver =
 {
     ANDROID_JoystickInit,
@@ -708,6 +714,7 @@ SDL_JoystickDriver SDL_ANDROID_JoystickDriver =
     ANDROID_JoystickUpdate,
     ANDROID_JoystickClose,
     ANDROID_JoystickQuit,
+    ANDROID_JoystickGetGamepadMapping
 };
 
 #endif /* SDL_JOYSTICK_ANDROID */
diff --git a/src/joystick/bsd/SDL_sysjoystick.c b/src/joystick/bsd/SDL_sysjoystick.c
index 057f93b..e47557d 100644
--- a/src/joystick/bsd/SDL_sysjoystick.c
+++ b/src/joystick/bsd/SDL_sysjoystick.c
@@ -762,6 +762,12 @@ BSD_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 
     return SDL_Unsupported();
 }
 
+static SDL_bool
+BSD_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
+{
+    return SDL_FALSE;
+}
+
 SDL_JoystickDriver SDL_BSD_JoystickDriver =
 {
     BSD_JoystickInit,
@@ -777,6 +783,7 @@ SDL_JoystickDriver SDL_BSD_JoystickDriver =
     BSD_JoystickUpdate,
     BSD_JoystickClose,
     BSD_JoystickQuit,
+    BSD_JoystickGetGamepadMapping
 };
 
 #endif /* SDL_JOYSTICK_USBHID */
diff --git a/src/joystick/darwin/SDL_sysjoystick.c b/src/joystick/darwin/SDL_sysjoystick.c
index fbee787..637975f 100644
--- a/src/joystick/darwin/SDL_sysjoystick.c
+++ b/src/joystick/darwin/SDL_sysjoystick.c
@@ -1051,6 +1051,12 @@ DARWIN_JoystickQuit(void)
     }
 }
 
+static SDL_bool
+DARWIN_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
+{
+    return SDL_FALSE;
+}
+
 SDL_JoystickDriver SDL_DARWIN_JoystickDriver =
 {
     DARWIN_JoystickInit,
@@ -1066,6 +1072,7 @@ SDL_JoystickDriver SDL_DARWIN_JoystickDriver =
     DARWIN_JoystickUpdate,
     DARWIN_JoystickClose,
     DARWIN_JoystickQuit,
+    DARWIN_JoystickGetGamepadMapping
 };
 
 #endif /* SDL_JOYSTICK_IOKIT */
diff --git a/src/joystick/dummy/SDL_sysjoystick.c b/src/joystick/dummy/SDL_sysjoystick.c
index 4fcceaf..4ca2a9f 100644
--- a/src/joystick/dummy/SDL_sysjoystick.c
+++ b/src/joystick/dummy/SDL_sysjoystick.c
@@ -104,6 +104,12 @@ DUMMY_JoystickQuit(void)
 {
 }
 
+static SDL_bool
+DUMMY_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
+{
+    return SDL_FALSE;
+}
+
 SDL_JoystickDriver SDL_DUMMY_JoystickDriver =
 {
     DUMMY_JoystickInit,
@@ -119,6 +125,7 @@ SDL_JoystickDriver SDL_DUMMY_JoystickDriver =
     DUMMY_JoystickUpdate,
     DUMMY_JoystickClose,
     DUMMY_JoystickQuit,
+    DUMMY_JoystickGetGamepadMapping
 };
 
 #endif /* SDL_JOYSTICK_DUMMY || SDL_JOYSTICK_DISABLED */
diff --git a/src/joystick/emscripten/SDL_sysjoystick.c b/src/joystick/emscripten/SDL_sysjoystick.c
index 02d0874..6ddb067 100644
--- a/src/joystick/emscripten/SDL_sysjoystick.c
+++ b/src/joystick/emscripten/SDL_sysjoystick.c
@@ -403,6 +403,12 @@ EMSCRIPTEN_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, 
     return SDL_Unsupported();
 }
 
+static SDL_bool
+EMSCRIPTEN_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
+{
+    return SDL_FALSE;
+}
+
 SDL_JoystickDriver SDL_EMSCRIPTEN_JoystickDriver =
 {
     EMSCRIPTEN_JoystickInit,
@@ -418,6 +424,7 @@ SDL_JoystickDriver SDL_EMSCRIPTEN_JoystickDriver =
     EMSCRIPTEN_JoystickUpdate,
     EMSCRIPTEN_JoystickClose,
     EMSCRIPTEN_JoystickQuit,
+    EMSCRIPTEN_JoystickGetGamepadMapping
 };
 
 #endif /* SDL_JOYSTICK_EMSCRIPTEN */
diff --git a/src/joystick/haiku/SDL_haikujoystick.cc b/src/joystick/haiku/SDL_haikujoystick.cc
index c34db53..370e9e1 100644
--- a/src/joystick/haiku/SDL_haikujoystick.cc
+++ b/src/joystick/haiku/SDL_haikujoystick.cc
@@ -259,6 +259,12 @@ extern "C"
         return SDL_Unsupported();
     }
 
+    static SDL_bool
+    HAIKU_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
+    {
+        return SDL_FALSE;
+    }
+
     SDL_JoystickDriver SDL_HAIKU_JoystickDriver =
     {
         HAIKU_JoystickInit,
@@ -274,6 +280,7 @@ extern "C"
         HAIKU_JoystickUpdate,
         HAIKU_JoystickClose,
         HAIKU_JoystickQuit,
+        HAIKU_JoystickGetGamepadMapping
     };
 
 }                              // extern "C"
diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c
index 9ed82d8..d5aea3e 100644
--- a/src/joystick/hidapi/SDL_hidapijoystick.c
+++ b/src/joystick/hidapi/SDL_hidapijoystick.c
@@ -1101,6 +1101,12 @@ HIDAPI_JoystickQuit(void)
     initialized = SDL_FALSE;
 }
 
+static SDL_bool
+HIDAPI_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
+{
+    return SDL_FALSE;
+}
+
 SDL_JoystickDriver SDL_HIDAPI_JoystickDriver =
 {
     HIDAPI_JoystickInit,
@@ -1116,6 +1122,7 @@ SDL_JoystickDriver SDL_HIDAPI_JoystickDriver =
     HIDAPI_JoystickUpdate,
     HIDAPI_JoystickClose,
     HIDAPI_JoystickQuit,
+    HIDAPI_JoystickGetGamepadMapping
 };
 
 #endif /* SDL_JOYSTICK_HIDAPI */
diff --git a/src/joystick/iphoneos/SDL_sysjoystick.m b/src/joystick/iphoneos/SDL_sysjoystick.m
index 4f413e9..b8b029a 100644
--- a/src/joystick/iphoneos/SDL_sysjoystick.m
+++ b/src/joystick/iphoneos/SDL_sysjoystick.m
@@ -850,6 +850,12 @@ IOS_JoystickQuit(void)
     numjoysticks = 0;
 }
 
+static SDL_bool
+IOS_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
+{
+    return SDL_FALSE;
+}
+
 SDL_JoystickDriver SDL_IOS_JoystickDriver =
 {
     IOS_JoystickInit,
@@ -865,6 +871,7 @@ SDL_JoystickDriver SDL_IOS_JoystickDriver =
     IOS_JoystickUpdate,
     IOS_JoystickClose,
     IOS_JoystickQuit,
+    IOS_JoystickGetGamepadMapping
 };
 
 /* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c
index 28bccd8..2ffb4eb 100644
--- a/src/joystick/linux/SDL_sysjoystick.c
+++ b/src/joystick/linux/SDL_sysjoystick.c
@@ -656,6 +656,7 @@ ConfigJoystick(SDL_Joystick * joystick, int fd)
                 printf("Joystick has button: 0x%x\n", i);
 #endif
                 joystick->hwdata->key_map[i] = joystick->nbuttons;
+                joystick->hwdata->has_key[i] = SDL_TRUE;
                 ++joystick->nbuttons;
             }
         }
@@ -665,6 +666,7 @@ ConfigJoystick(SDL_Joystick * joystick, int fd)
                 printf("Joystick has button: 0x%x\n", i);
 #endif
                 joystick->hwdata->key_map[i] = joystick->nbuttons;
+                joystick->hwdata->has_key[i] = SDL_TRUE;
                 ++joystick->nbuttons;
             }
         }
@@ -687,6 +689,7 @@ ConfigJoystick(SDL_Joystick * joystick, int fd)
                        absinfo.fuzz, absinfo.flat);
 #endif /* DEBUG_INPUT_EVENTS */
                 joystick->hwdata->abs_map[i] = joystick->naxes;
+                joystick->hwdata->has_abs[i] = SDL_TRUE;
                 if (absinfo.minimum == absinfo.maximum) {
                     joystick->hwdata->abs_correct[i].used = 0;
                 } else {
@@ -721,6 +724,7 @@ ConfigJoystick(SDL_Joystick * joystick, int fd)
                        absinfo.fuzz, absinfo.flat);
 #endif /* DEBUG_INPUT_EVENTS */
                 joystick->hwdata->hats_indices[hat_index] = joystick->nhats++;
+                joystick->hwdata->has_hat[hat_index] = SDL_TRUE;
             }
         }
         if (test_bit(REL_X, relbit) || test_bit(REL_Y, relbit)) {
@@ -1100,6 +1104,181 @@ LINUX_JoystickQuit(void)
     SDL_QuitSteamControllers();
 }
 
+/*
+   This is based on the Linux Gamepad Specification
+   available at: https://www.kernel.org/doc/html/v4.15/input/gamepad.html
+ */
+static SDL_bool
+LINUX_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
+{
+    SDL_Joystick * joystick;
+
+    joystick = (SDL_Joystick *) SDL_calloc(sizeof(*joystick), 1);
+    if (joystick == NULL) {
+        SDL_OutOfMemory();
+        return SDL_FALSE;
+    }
+
+    /* We temporarily open the device to check how it's configured. */
+    if (LINUX_JoystickOpen(joystick, device_index) < 0) {
+        SDL_free(joystick);
+        return SDL_FALSE;
+    }
+
+    if (!joystick->hwdata->has_key[BTN_GAMEPAD]) {
+        /* Not a gamepad according to the specs. */
+        LINUX_JoystickClose(joystick);
+        SDL_free(joystick);
+        return SDL_FALSE;
+    }
+
+    /* We have a gamepad; start filling out the mappings. */
+    memset(out, 0, sizeof(SDL_GamepadMapping));
+
+    if (joystick->hwdata->has_key[BTN_SOUTH]) {
+        out->a.kind = EMappingKind_Button;
+        out->a.target = joystick->hwdata->key_map[BTN_SOUTH];
+    }
+
+    if (joystick->hwdata->has_key[BTN_EAST]) {
+        out->b.kind = EMappingKind_Button;
+        out->b.target = joystick->hwdata->key_map[BTN_EAST];
+    }
+
+    if (joystick->hwdata->has_key[BTN_NORTH]) {
+        out->y.kind = EMappingKind_Button;
+        out->y.target = joystick->hwdata->key_map[BTN_NORTH];
+    }
+
+    if (joystick->hwdata->has_key[BTN_WEST]) {
+        out->x.kind = EMappingKind_Button;
+        out->x.target = joystick->hwdata->key_map[BTN_WEST];
+    }
+
+    if (joystick->hwdata->has_key[BTN_SELECT]) {
+        out->back.kind = EMappingKind_Button;
+        out->back.target = joystick->hwdata->key_map[BTN_SELECT];
+    }
+
+    if (joystick->hwdata->has_key[BTN_START]) {
+        out->start.kind = EMappingKind_Button;
+        out->start.target = joystick->hwdata->key_map[BTN_START];
+    }
+
+    if (joystick->hwdata->has_key[BTN_THUMBL]) {
+        out->leftstick.kind = EMappingKind_Button;
+        out->leftstick.target = joystick->hwdata->key_map[BTN_THUMBL];
+    }
+
+    if (joystick->hwdata->has_key[BTN_THUMBR]) {
+        out->rightstick.kind = EMappingKind_Button;
+        out->rightstick.target = joystick->hwdata->key_map[BTN_THUMBR];
+    }
+
+    if (joystick->hwdata->has_key[BTN_MODE]) {
+        out->guide.kind = EMappingKind_Button;
+        out->guide.target = joystick->hwdata->key_map[BTN_MODE];
+    }
+
+    /*
+       According to the specs the D-Pad, the shoulder buttons and the triggers
+       can be digital, or analog, or both at the same time.
+     */
+
+    /* Prefer digital shoulder buttons, but settle for analog if missing. */
+    if (joystick->hwdata->has_key[BTN_TL]) {
+        out->leftshoulder.kind = EMappingKind_Button;
+        out->leftshoulder.target = joystick->hwdata->key_map[BTN_TL];
+    }
+
+    if (joystick->hwdata->has_key[BTN_TR]) {
+        out->rightshoulder.kind = EMappingKind_Button;
+        out->rightshoulder.target = joystick->hwdata->key_map[BTN_TR];
+    }
+
+    if (joystick->hwdata->has_hat[1] && /* Check if ABS_HAT1{X, Y} is available. */
+       (!joystick->hwdata->has_key[BTN_TL] || !joystick->hwdata->has_key[BTN_TR])) {
+        int hat = joystick->hwdata->hats_indices[1] << 4;
+        out->leftshoulder.kind = EMappingKind_Hat;
+        out->rightshoulder.kind = EMappingKind_Hat;
+        out->leftshoulder.target = hat | 0x4;
+        out->rightshoulder.target = hat | 0x2;
+    }
+
+    /* Prefer analog triggers, but settle for digital if missing. */
+    if (joystick->hwdata->has_hat[2]) { /* Check if ABS_HAT2{X,Y} is available. */
+        int hat = joystick->hwdata->hats_indices[2] << 4;
+        out->lefttrigger.kind = EMappingKind_Hat;
+        out->righttrigger.kind = EMappingKind_Hat;
+        out->lefttrigger.target = hat | 0x4;
+        out->righttrigger.target = hat | 0x2;
+    } else {
+        if (joystick->hwdata->has_key[BTN_TL2]) {
+            out->lefttrigger.kind = EMappingKind_Button;
+            out->lefttrigger.target = joystick->hwdata->key_map[BTN_TL2];
+        }
+
+        if (joystick->hwdata->has_key[BTN_TR2]) {
+            out->righttrigger.kind = EMappingKind_Button;
+            out->righttrigger.target = joystick->hwdata->key_map[BTN_TR2];
+        }
+    }
+
+    /* Prefer digital D-Pad, but settle for analog if missing. */
+    if (joystick->hwdata->has_key[BTN_DPAD_UP]) {
+        out->dpup.kind = EMappingKind_Button;
+        out->dpup.target = joystick->hwdata->key_map[BTN_DPAD_UP];
+    }
+
+    if (joystick->hwdata->has_key[BTN_DPAD_DOWN]) {
+        out->dpdown.kind = EMappingKind_Button;
+        out->dpdown.target = joystick->hwdata->key_map[BTN_DPAD_DOWN];
+    }
+
+    if (joystick->hwdata->has_key[BTN_DPAD_LEFT]) {
+        out->dpleft.kind = EMappingKind_Button;
+        out->dpleft.target = joystick->hwdata->key_map[BTN_DPAD_LEFT];
+    }
+
+    if (joystick->hwdata->has_key[BTN_DPAD_RIGHT]) {
+        out->dpright.kind = EMappingKind_Button;
+        out->dpright.target = joystick->hwdata->key_map[BTN_DPAD_RIGHT];
+    }
+
+    if (joystick->hwdata->has_hat[0] && /* Check if ABS_HAT0{X,Y} is available. */
+       (!joystick->hwdata->has_key[BTN_DPAD_LEFT] || !joystick->hwdata->has_key[BTN_DPAD_RIGHT] ||
+        !joystick->hwdata->has_key[BTN_DPAD_UP] || !joystick->hwdata->has_key[BTN_DPAD_DOWN])) {
+       int hat = joystick->hwdata->hats_indices[0] << 4;
+       out->dpleft.kind = EMappingKind_Hat;
+       out->dpright.kind = EMappingKind_Hat;
+       out->dpup.kind = EMappingKind_Hat;
+       out->dpdown.kind = EMappingKind_Hat;
+       out->dpleft.target = hat | 0x8;
+       out->dpright.target = hat | 0x2;
+       out->dpup.target = hat | 0x1;
+       out->dpdown.target = hat | 0x4;
+    }
+
+    if (joystick->hwdata->has_abs[ABS_X] && joystick->hwdata->has_abs[ABS_Y]) {
+        out->leftx.kind = EMappingKind_Axis;
+        out->lefty.kind = EMappingKind_Axis;
+        out->leftx.target = joystick->hwdata->abs_map[ABS_X];
+        out->lefty.target = joystick->hwdata->abs_map[ABS_Y];
+    }
+
+    if (joystick->hwdata->has_abs[ABS_RX] && joystick->hwdata->has_abs[ABS_RY]) {
+        out->rightx.kind = EMappingKind_Axis;
+        out->righty.kind = EMappingKind_Axis;
+        out->rightx.target = joystick->hwdata->abs_map[ABS_RX];
+        out->righty.target = joystick->hwdata->abs_map[ABS_RY];
+    }
+
+    LINUX_JoystickClose(joystick);
+    SDL_free(joystick);
+
+    return SDL_TRUE;
+}
+
 SDL_JoystickDriver SDL_LINUX_JoystickDriver =
 {
     LINUX_JoystickInit,
@@ -1115,6 +1294,7 @@ SDL_JoystickDriver SDL_LINUX_JoystickDriver =
     LINUX_JoystickUpdate,
     LINUX_JoystickClose,
     LINUX_JoystickQuit,
+    LINUX_JoystickGetGamepadMapping
 };
 
 #endif /* SDL_JOYSTICK_LINUX */
diff --git a/src/joystick/linux/SDL_sysjoystick_c.h b/src/joystick/linux/SDL_sysjoystick_c.h
index a92429b..6f162ae 100644
--- a/src/joystick/linux/SDL_sysjoystick_c.h
+++ b/src/joystick/linux/SDL_sysjoystick_c.h
@@ -53,6 +53,9 @@ struct joystick_hwdata
     /* Support for the Linux 2.4 unified input interface */
     Uint8 key_map[KEY_MAX];
     Uint8 abs_map[ABS_MAX];
+    SDL_bool has_key[KEY_MAX];
+    SDL_bool has_abs[ABS_MAX];
+
     struct axis_correct
     {
         int used;
@@ -65,6 +68,7 @@ struct joystick_hwdata
     SDL_bool m_bSteamController;
     /* 4 = (ABS_HAT3X-ABS_HAT0X)/2 (see input-event-codes.h in kernel) */
     int hats_indices[4];
+    SDL_bool has_hat[4];
 
     /* Set when gamepad is pending removal due to ENODEV read error */
     SDL_bool gone;
diff --git a/src/joystick/virtual/SDL_virtualjoystick.c b/src/joystick/virtual/SDL_virtualjoystick.c
index 623ed1c..ce075b0 100644
--- a/src/joystick/virtual/SDL_virtualjoystick.c
+++ b/src/joystick/virtual/SDL_virtualjoystick.c
@@ -391,6 +391,11 @@ VIRTUAL_JoystickQuit(void)
     }
 }
 
+static SDL_bool
+VIRTUAL_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
+{
+    return SDL_FALSE;
+}
 
 SDL_JoystickDriver SDL_VIRTUAL_JoystickDriver =
 {
@@ -407,6 +412,7 @@ SDL_JoystickDriver SDL_VIRTUAL_JoystickDriver =
     VIRTUAL_JoystickUpdate,
     VIRTUAL_JoystickClose,
     VIRTUAL_JoystickQuit,
+    VIRTUAL_JoystickGetGamepadMapping
 };
 
 #endif /* SDL_JOYSTICK_VIRTUAL || SDL_JOYSTICK_DISABLED */
diff --git a/src/joystick/windows/SDL_rawinputjoystick.c b/src/joystick/windows/SDL_rawinputjoystick.c
index f17e852..4134e4c 100644
--- a/src/joystick/windows/SDL_rawinputjoystick.c
+++ b/src/joystick/windows/SDL_rawinputjoystick.c
@@ -725,6 +725,11 @@ RAWINPUT_JoystickQuit(void)
     SDL_RAWINPUT_inited = SDL_FALSE;
 }
 
+static SDL_bool
+RAWINPUT_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
+{
+    return SDL_FALSE;
+}
 
 SDL_JoystickDriver SDL_RAWINPUT_JoystickDriver =
 {
@@ -741,6 +746,7 @@ SDL_JoystickDriver SDL_RAWINPUT_JoystickDriver =
     RAWINPUT_JoystickUpdate,
     RAWINPUT_JoystickClose,
     RAWINPUT_JoystickQuit,
+    RAWINPUT_JoystickGetGamepadMapping
 };
 
 #endif /* SDL_JOYSTICK_RAWINPUT */
diff --git a/src/joystick/windows/SDL_windows_gaming_input.c b/src/joystick/windows/SDL_windows_gaming_input.c
index 90ab8cd..e491d3e 100644
--- a/src/joystick/windows/SDL_windows_gaming_input.c
+++ b/src/joystick/windows/SDL_windows_gaming_input.c
@@ -699,6 +699,12 @@ WGI_JoystickQuit(void)
     WIN_CoUninitialize();
 }
 
+static SDL_bool
+WGI_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
+{
+    return SDL_FALSE;
+}
+
 SDL_JoystickDriver SDL_WGI_JoystickDriver =
 {
     WGI_JoystickInit,
@@ -714,6 +720,7 @@ SDL_JoystickDriver SDL_WGI_JoystickDriver =
     WGI_JoystickUpdate,
     WGI_JoystickClose,
     WGI_JoystickQuit,
+    WGI_JoystickGetGamepadMapping
 };
 
 #endif /* SDL_JOYSTICK_WGI */
diff --git a/src/joystick/windows/SDL_windowsjoystick.c b/src/joystick/windows/SDL_windowsjoystick.c
index df8fdf1..0426760 100644
--- a/src/joystick/windows/SDL_windowsjoystick.c
+++ b/src/joystick/windows/SDL_windowsjoystick.c
@@ -555,6 +555,12 @@ WINDOWS_JoystickQuit(void)
     s_bDeviceRemoved = SDL_FALSE;
 }
 
+static SDL_bool
+WINDOWS_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
+{
+    return SDL_FALSE;
+}
+
 SDL_JoystickDriver SDL_WINDOWS_JoystickDriver =
 {
     WINDOWS_JoystickInit,
@@ -570,6 +576,7 @@ SDL_JoystickDriver SDL_WINDOWS_JoystickDriver =
     WINDOWS_JoystickUpdate,
     WINDOWS_JoystickClose,
     WINDOWS_JoystickQuit,
+    WINDOWS_JoystickGetGamepadMapping
 };
 
 #endif /* SDL_JOYSTICK_DINPUT || SDL_JOYSTICK_XINPUT */