sdl3-src 3.4.4

Source code of the SDL 3 library
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
/*
  Simple DirectMedia Layer
  Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"

#ifdef SDL_JOYSTICK_GAMEINPUT

#include "../SDL_sysjoystick.h"
#include "../usb_ids.h"
#include "../../core/windows/SDL_windows.h"
#include "../../core/windows/SDL_gameinput.h"

// Default value for SDL_HINT_JOYSTICK_GAMEINPUT
#if defined(SDL_PLATFORM_GDK)
#define SDL_GAMEINPUT_DEFAULT true
#else
#define SDL_GAMEINPUT_DEFAULT false
#endif

// Enable sensor support in GameInput 2.0, once we have a device that can be used for testing
#if GAMEINPUT_API_VERSION >= 2
//#define GAMEINPUT_SENSOR_SUPPORT
#endif

enum
{
    SDL_GAMEPAD_BUTTON_GAMEINPUT_SHARE = 11
};

enum
{
    SDL_GAMEINPUT_RAWTYPE_NONE,
    SDL_GAMEINPUT_RAWTYPE_ROCK_BAND_GUITAR,
    SDL_GAMEINPUT_RAWTYPE_ROCK_BAND_DRUM_KIT,
    SDL_GAMEINPUT_RAWTYPE_GUITAR_HERO_LIVE_GUITAR,
    SDL_GAMEINPUT_RAWTYPE_LEGACY_ADAPTER,
};

typedef struct GAMEINPUT_InternalDevice
{
    IGameInputDevice *device;
    char path[(APP_LOCAL_DEVICE_ID_SIZE * 2) + 1];
    char *name;
    SDL_GUID guid;          // generated by SDL
    SDL_JoystickID device_instance; // generated by SDL
    const GameInputDeviceInfo *info;
    int raw_type;
    int steam_virtual_gamepad_slot;
    bool isAdded;
    bool isDeleteRequested;
} GAMEINPUT_InternalDevice;

typedef struct GAMEINPUT_InternalList
{
    GAMEINPUT_InternalDevice **devices;
    int count;
} GAMEINPUT_InternalList;

typedef struct joystick_hwdata
{
    GAMEINPUT_InternalDevice *devref;
    bool report_sensors;
    GameInputRumbleParams rumbleParams;
    GameInputCallbackToken system_button_callback_token;
} GAMEINPUT_InternalJoystickHwdata;

static GAMEINPUT_InternalList g_GameInputList = { NULL };
static IGameInput *g_pGameInput = NULL;
static GameInputCallbackToken g_GameInputCallbackToken = 0;
static Uint64 g_GameInputTimestampOffset;

extern "C"
{
    extern bool SDL_XINPUT_Enabled(void);
}

static bool GAMEINPUT_InternalIsGamepad(const GameInputDeviceInfo *info)
{
    if (info->supportedInput & GameInputKindGamepad) {
        return true;
    }
    return false;
}

static Uint8 GAMEINPUT_GetDeviceRawType(const GameInputDeviceInfo *info)
{
#if GAMEINPUT_API_VERSION >= 3
    GameInputKind supportedInput = info->supportedInput;
    if (supportedInput & GameInputKindRawDeviceReport) {
        switch (info->vendorId) {
            case USB_VENDOR_MADCATZ:
                switch (info->productId) {
                    case USB_PRODUCT_MADCATZ_XB1_STRATOCASTER_GUITAR:
                        return SDL_GAMEINPUT_RAWTYPE_ROCK_BAND_GUITAR;
                    case USB_PRODUCT_MADCATZ_XB1_DRUM_KIT:
                        return SDL_GAMEINPUT_RAWTYPE_ROCK_BAND_DRUM_KIT;
                    case USB_PRODUCT_MADCATZ_XB1_LEGACY_ADAPTER:
                        return SDL_GAMEINPUT_RAWTYPE_LEGACY_ADAPTER;
                    default:
                        break;
                }
                break;
            case USB_VENDOR_PDP:
                switch (info->productId) {
                    case USB_PRODUCT_PDP_XB1_JAGUAR_GUITAR:
                    case USB_PRODUCT_PDP_XB1_RIFFMASTER_GUITAR:
                        return SDL_GAMEINPUT_RAWTYPE_ROCK_BAND_GUITAR;
                    case USB_PRODUCT_PDP_XB1_DRUM_KIT:
                        return SDL_GAMEINPUT_RAWTYPE_ROCK_BAND_DRUM_KIT;
                    default:
                        break;
                }
                break;
            case USB_VENDOR_CRKD:
                switch (info->productId) {
                    case USB_PRODUCT_PDP_XB1_JAGUAR_GUITAR:
                        return SDL_GAMEINPUT_RAWTYPE_ROCK_BAND_GUITAR;
                    default:
                        break;
                }
                break;
            case USB_VENDOR_RED_OCTANE:
                switch (info->productId) {
                    case USB_PRODUCT_RED_OCTANE_XB1_GUITAR_HERO_LIVE_GUITAR:
                        return SDL_GAMEINPUT_RAWTYPE_GUITAR_HERO_LIVE_GUITAR;
                    default:
                        break;
                }
                break;
        }
    }
#endif // GAMEINPUT_API_VERSION >= 3
    return SDL_GAMEINPUT_RAWTYPE_NONE;
}
static Uint8 GAMEINPUT_GetDeviceSubtype(const GameInputDeviceInfo *info)
{
    GameInputKind supportedInput = info->supportedInput;
    Uint8 rawType = GAMEINPUT_GetDeviceRawType(info);
    if (rawType == SDL_GAMEINPUT_RAWTYPE_ROCK_BAND_GUITAR || rawType == SDL_GAMEINPUT_RAWTYPE_GUITAR_HERO_LIVE_GUITAR) {
        return SDL_JOYSTICK_TYPE_GUITAR;
    }
    if (rawType == SDL_GAMEINPUT_RAWTYPE_ROCK_BAND_DRUM_KIT) {
        return SDL_JOYSTICK_TYPE_DRUM_KIT;
    }
    if (supportedInput & GameInputKindRacingWheel) {
        return SDL_JOYSTICK_TYPE_WHEEL;
    }
    if (supportedInput & GameInputKindArcadeStick) {
        return SDL_JOYSTICK_TYPE_ARCADE_STICK;
    }
    if (supportedInput & GameInputKindFlightStick) {
        return SDL_JOYSTICK_TYPE_FLIGHT_STICK;
    }
    if (supportedInput & (GameInputKindGamepad | GameInputKindController)) {
        return SDL_JOYSTICK_TYPE_GAMEPAD;
    }
    // Other device subtypes don't have their own GameInputKind enum entries.
    return 0;
}

#if GAMEINPUT_API_VERSION >= 1
static int GetSteamVirtualGamepadSlot(const char *device_path)
{
    int slot = -1;

    // The format for the raw input device path is documented here:
    // https://partner.steamgames.com/doc/features/steam_controller/steam_input_gamepad_emulation_bestpractices
    (void)SDL_sscanf(device_path, "\\\\.\\pipe\\HID#VID_045E&PID_028E&IG_00#%*X&%*X&%*X#%d#%*u", &slot);
    return slot;
}
#endif // GAMEINPUT_API_VERSION >= 1

static bool GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
{
    GAMEINPUT_InternalDevice **devicelist = NULL;
    GAMEINPUT_InternalDevice *elem = NULL;
    const GameInputDeviceInfo *info = NULL;
    Uint16 bus = SDL_HARDWARE_BUS_USB;
    Uint16 vendor = 0;
    Uint16 product = 0;
    Uint16 version = 0;
    const char *product_string = NULL;
    Uint8 driver_signature = 'g';
    Uint8 subtype = 0;
    int raw_type = SDL_GAMEINPUT_RAWTYPE_NONE;
    char tmp[4];
    int idx = 0;

    SDL_AssertJoysticksLocked();

#if GAMEINPUT_API_VERSION >= 1
    HRESULT hr = pDevice->GetDeviceInfo(&info);
    if (FAILED(hr)) {
        return WIN_SetErrorFromHRESULT("IGameInputDevice::GetDeviceInfo", hr);
    }
#else
    info = pDevice->GetDeviceInfo();
#endif
    if (false /*info->capabilities & GameInputDeviceCapabilityWireless*/) {
        bus = SDL_HARDWARE_BUS_BLUETOOTH;
    } else {
        bus = SDL_HARDWARE_BUS_USB;
    }
    vendor = info->vendorId;
    product = info->productId;
    //version = (info->firmwareVersion.major << 8) | info->firmwareVersion.minor;
    subtype = GAMEINPUT_GetDeviceSubtype(info);
    raw_type = GAMEINPUT_GetDeviceRawType(info);

#if GAMEINPUT_API_VERSION >= 1
    if (info->displayName) {
        product_string = info->displayName;
    }
#else
    if (info->displayName) {
        product_string = info->displayName->data;
    }
#endif

    if (SDL_ShouldIgnoreJoystick(vendor, product, version, product_string) ||
        SDL_JoystickHandledByAnotherDriver(&SDL_GAMEINPUT_JoystickDriver, vendor, product, version, product_string)) {
        return true;
    }

#if defined(SDL_JOYSTICK_DINPUT) && defined(SDL_HAPTIC_DINPUT)
    // This joystick backend currently doesn't provide a haptic backend, so fallback to DirectInput for haptic-capable devices.
    if (SDL_GetHintBoolean(SDL_HINT_JOYSTICK_DIRECTINPUT, true) && info->forceFeedbackMotorCount > 0 && pDevice->IsForceFeedbackMotorPoweredOn(0)) {
        return true;
    }
#endif

    if (!GAMEINPUT_InternalIsGamepad(info) && raw_type == SDL_GAMEINPUT_RAWTYPE_NONE) {
#if defined(SDL_JOYSTICK_DINPUT) || defined(SDL_JOYSTICK_XINPUT)
        if (SDL_GetHintBoolean(SDL_HINT_JOYSTICK_DIRECTINPUT, true) || SDL_XINPUT_Enabled()) {
            // Let other backends handle non-gamepad controllers to possibly avoid bugs and/or regressions.
            return true;
        }
#endif
        if (info->supportedInput & GameInputKindController) {
            // Maintain GUID compatibility with DirectInput controller mappings.
            driver_signature = 0;
            subtype = 0;
        } else {
            // This joystick backend currently doesn't provide proper reading of other joystick types.
            return true;
        }
    }

    for (idx = 0; idx < g_GameInputList.count; ++idx) {
        elem = g_GameInputList.devices[idx];
        if (elem && elem->device == pDevice) {
            // we're already added
            elem->isDeleteRequested = false;
            return true;
        }
    }

    elem = (GAMEINPUT_InternalDevice *)SDL_calloc(1, sizeof(*elem));
    if (!elem) {
        return false;
    }

    devicelist = (GAMEINPUT_InternalDevice **)SDL_realloc(g_GameInputList.devices, sizeof(elem) * (g_GameInputList.count + 1LL));
    if (!devicelist) {
        SDL_free(elem);
        return false;
    }

    // Generate a device path
    for (idx = 0; idx < APP_LOCAL_DEVICE_ID_SIZE; ++idx) {
        SDL_snprintf(tmp, SDL_arraysize(tmp), "%02hhX", info->deviceId.value[idx]);
        SDL_strlcat(elem->path, tmp, SDL_arraysize(elem->path));
    }

    pDevice->AddRef();
    elem->device = pDevice;
    elem->name = SDL_CreateJoystickName(vendor, product, NULL, product_string);
    elem->guid = SDL_CreateJoystickGUID(bus, vendor, product, version, NULL, product_string, driver_signature, subtype);
    elem->device_instance = SDL_GetNextObjectID();
    elem->info = info;
    elem->raw_type = raw_type;
#if GAMEINPUT_API_VERSION >= 1
    elem->steam_virtual_gamepad_slot = GetSteamVirtualGamepadSlot(info->pnpPath);
#else
    elem->steam_virtual_gamepad_slot = -1;
#endif

    g_GameInputList.devices = devicelist;
    g_GameInputList.devices[g_GameInputList.count++] = elem;

    return true;
}

static bool GAMEINPUT_InternalRemoveByIndex(int idx)
{
    GAMEINPUT_InternalDevice **devicelist = NULL;
    GAMEINPUT_InternalDevice *elem;
    int bytes = 0;

    SDL_AssertJoysticksLocked();

    if (idx < 0 || idx >= g_GameInputList.count) {
        return SDL_SetError("GAMEINPUT_InternalRemoveByIndex argument idx %d is out of range", idx);
    }

    elem = g_GameInputList.devices[idx];
    if (elem) {
        elem->device->Release();
        SDL_free(elem->name);
        SDL_free(elem);
    }
    g_GameInputList.devices[idx] = NULL;

    if (g_GameInputList.count == 1) {
        // last element in the list, free the entire list then
        SDL_free(g_GameInputList.devices);
        g_GameInputList.devices = NULL;
    } else {
        if (idx != g_GameInputList.count - 1) {
            bytes = sizeof(*devicelist) * (g_GameInputList.count - idx);
            SDL_memmove(&g_GameInputList.devices[idx], &g_GameInputList.devices[idx + 1], bytes);
        }
    }

    // decrement the count and return
    --g_GameInputList.count;
    return true;
}

static GAMEINPUT_InternalDevice *GAMEINPUT_InternalFindByIndex(int idx)
{
    // We're guaranteed that the index is in range when this is called
    SDL_AssertJoysticksLocked();
    return g_GameInputList.devices[idx];
}

static void CALLBACK GAMEINPUT_InternalJoystickDeviceCallback(
    _In_ GameInputCallbackToken callbackToken,
    _In_ void *context,
    _In_ IGameInputDevice *device,
    _In_ uint64_t timestamp,
    _In_ GameInputDeviceStatus currentStatus,
    _In_ GameInputDeviceStatus previousStatus)
{
    int idx = 0;
    GAMEINPUT_InternalDevice *elem = NULL;

    if (!device) {
        // This should never happen, but ignore it if it does
        return;
    }

    SDL_LockJoysticks();

    if (currentStatus & GameInputDeviceConnected) {
        GAMEINPUT_InternalAddOrFind(device);
    } else {
        for (idx = 0; idx < g_GameInputList.count; ++idx) {
            elem = g_GameInputList.devices[idx];
            if (elem && elem->device == device) {
                // will be deleted on the next Detect call
                elem->isDeleteRequested = true;
                break;
            }
        }
    }

    SDL_UnlockJoysticks();
}

static void GAMEINPUT_JoystickDetect(void);
static void GAMEINPUT_JoystickQuit(void);

static bool GAMEINPUT_IsRawGameInputEnabled(void)
{
#if GAMEINPUT_API_VERSION >= 3
    return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_GAMEINPUT_RAW, true);
#else
    return false;
#endif
}

static bool GAMEINPUT_JoystickInit(void)
{
    HRESULT hr;

    if (!SDL_GetHintBoolean(SDL_HINT_JOYSTICK_GAMEINPUT, SDL_GAMEINPUT_DEFAULT) && !GAMEINPUT_IsRawGameInputEnabled()) {
        return true;
    }

    if (!SDL_InitGameInput(&g_pGameInput)) {
        return false;
    }

#if GAMEINPUT_API_VERSION >= 2
    // Allow background controller input
    // SDL manages focus policy at a higher level, so we can set this unconditionally.
    g_pGameInput->SetFocusPolicy(GameInputEnableBackgroundInput | GameInputEnableBackgroundGuideButton | GameInputEnableBackgroundShareButton);
#endif

    GameInputKind kind = GameInputKindUnknown;
    if (SDL_GetHintBoolean(SDL_HINT_JOYSTICK_GAMEINPUT, SDL_GAMEINPUT_DEFAULT)) {
        kind |= GameInputKindController;
    }
    if (GAMEINPUT_IsRawGameInputEnabled()) {
        kind |= GameInputKindRawDeviceReport;
    }

    hr = g_pGameInput->RegisterDeviceCallback(NULL,
                                           kind,
                                           GameInputDeviceConnected,
                                           GameInputBlockingEnumeration,
                                           NULL,
                                           GAMEINPUT_InternalJoystickDeviceCallback,
                                           &g_GameInputCallbackToken);
    if (FAILED(hr)) {
        GAMEINPUT_JoystickQuit();
        return WIN_SetErrorFromHRESULT("IGameInput::RegisterDeviceCallback", hr);
    }

    // Calculate the relative offset between SDL timestamps and GameInput timestamps
    Uint64 now = SDL_GetTicksNS();
    uint64_t timestampUS = g_pGameInput->GetCurrentTimestamp();
    g_GameInputTimestampOffset = (SDL_NS_TO_US(now) - timestampUS);

    GAMEINPUT_JoystickDetect();

    return true;
}

static int GAMEINPUT_JoystickGetCount(void)
{
    SDL_AssertJoysticksLocked();

    return g_GameInputList.count;
}

static void GAMEINPUT_JoystickDetect(void)
{
    int idx;
    GAMEINPUT_InternalDevice *elem = NULL;

    SDL_AssertJoysticksLocked();

    for (idx = 0; idx < g_GameInputList.count; ++idx) {
        elem = g_GameInputList.devices[idx];
        if (!elem) {
            continue;
        }

        if (!elem->isAdded) {
            SDL_PrivateJoystickAdded(elem->device_instance);
            elem->isAdded = true;
        }

        if (elem->isDeleteRequested || !(elem->device->GetDeviceStatus() & GameInputDeviceConnected)) {
            SDL_PrivateJoystickRemoved(elem->device_instance);
            GAMEINPUT_InternalRemoveByIndex(idx--);
        }
    }
}

static bool GAMEINPUT_JoystickIsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
{
    SDL_AssertJoysticksLocked();

    if (g_pGameInput) {
        if (vendor_id == USB_VENDOR_MICROSOFT &&
            product_id == USB_PRODUCT_XBOX_ONE_XBOXGIP_CONTROLLER) {
            // The Xbox One controller shows up as a hardcoded raw input VID/PID, which we definitely handle
            return true;
        }

        for (int i = 0; i < g_GameInputList.count; ++i) {
            GAMEINPUT_InternalDevice *elem = g_GameInputList.devices[i];
            if (elem && vendor_id == elem->info->vendorId && product_id == elem->info->productId) {
                return true;
            }
        }
    }
    return false;
}

static const char *GAMEINPUT_JoystickGetDeviceName(int device_index)
{
    return GAMEINPUT_InternalFindByIndex(device_index)->name;
}

static const char *GAMEINPUT_JoystickGetDevicePath(int device_index)
{
    // APP_LOCAL_DEVICE_ID as a hex string, since it's required for some association callbacks
    return GAMEINPUT_InternalFindByIndex(device_index)->path;
}

static int GAMEINPUT_JoystickGetDeviceSteamVirtualGamepadSlot(int device_index)
{
    return GAMEINPUT_InternalFindByIndex(device_index)->steam_virtual_gamepad_slot;
}

static int GAMEINPUT_JoystickGetDevicePlayerIndex(int device_index)
{
    return -1;
}

static void GAMEINPUT_JoystickSetDevicePlayerIndex(int device_index, int player_index)
{
}

static SDL_GUID GAMEINPUT_JoystickGetDeviceGUID(int device_index)
{
    return GAMEINPUT_InternalFindByIndex(device_index)->guid;
}

static SDL_JoystickID GAMEINPUT_JoystickGetDeviceInstanceID(int device_index)
{
    return GAMEINPUT_InternalFindByIndex(device_index)->device_instance;
}

static void GAMEINPUT_UpdatePowerInfo(SDL_Joystick *joystick, IGameInputDevice *device)
{
#if 0
    GameInputBatteryState battery_state;
    SDL_PowerState state;
    int percent = 0;

    SDL_zero(battery_state);
    IGameInputDevice_GetBatteryState(device, &battery_state);

    switch (battery_state.status) {
    case GameInputBatteryNotPresent:
        state = SDL_POWERSTATE_NO_BATTERY;
        break;
    case GameInputBatteryDischarging:
        state = SDL_POWERSTATE_ON_BATTERY;
        break;
    case GameInputBatteryIdle:
        state = SDL_POWERSTATE_CHARGED;
        break;
    case GameInputBatteryCharging:
        state = SDL_POWERSTATE_CHARGING;
        break;
    default:
        state = SDL_POWERSTATE_UNKNOWN;
        break;
    }
    if (battery_state.fullChargeCapacity > 0.0f) {
        percent = (int)SDL_roundf((battery_state.remainingCapacity / battery_state.fullChargeCapacity) * 100.0f);
    }
    SDL_SendJoystickPowerInfo(joystick, state, percent);
#endif
}

#if GAMEINPUT_API_VERSION >= 1
static void CALLBACK GAMEINPUT_InternalSystemButtonCallback(
    _In_ GameInputCallbackToken callbackToken,
    _In_ void * context,
    _In_ IGameInputDevice * device,
    _In_ uint64_t timestampUS,
    _In_ GameInputSystemButtons currentButtons,
    _In_ GameInputSystemButtons previousButtons)
{
    SDL_Joystick *joystick = (SDL_Joystick *)context;

    GameInputSystemButtons changedButtons = (previousButtons ^ currentButtons);
    if (changedButtons) {
        Uint64 timestamp = SDL_US_TO_NS(timestampUS + g_GameInputTimestampOffset);

        SDL_LockJoysticks();
        if (changedButtons & GameInputSystemButtonGuide) {
            bool down = ((currentButtons & GameInputSystemButtonGuide) != 0);
            SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, down);
        }
        if (changedButtons & GameInputSystemButtonShare) {
            bool down = ((currentButtons & GameInputSystemButtonShare) != 0);
            SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GAMEINPUT_SHARE, down);
        }
        SDL_UnlockJoysticks();
    }
}
#endif // GAMEINPUT_API_VERSION >= 1

static bool GAMEINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
{
    GAMEINPUT_InternalDevice *elem = GAMEINPUT_InternalFindByIndex(device_index);
    const GameInputDeviceInfo *info = elem->info;
    GAMEINPUT_InternalJoystickHwdata *hwdata = NULL;

    if (!elem) {
        return false;
    }

    hwdata = (GAMEINPUT_InternalJoystickHwdata *)SDL_calloc(1, sizeof(*hwdata));
    if (!hwdata) {
        return false;
    }

    hwdata->devref = elem;

    joystick->hwdata = hwdata;
    if (GAMEINPUT_InternalIsGamepad(info) || GAMEINPUT_GetDeviceRawType(info) != SDL_GAMEINPUT_RAWTYPE_NONE) {
        joystick->naxes = 6;
        joystick->nbuttons = 11;
        joystick->nhats = 1;

#if GAMEINPUT_API_VERSION >= 1
        if (info->supportedSystemButtons != GameInputSystemButtonNone) {
            if (info->supportedSystemButtons & GameInputSystemButtonShare) {
                ++joystick->nbuttons;
            }

            g_pGameInput->RegisterSystemButtonCallback(elem->device, (GameInputSystemButtonGuide | GameInputSystemButtonShare), joystick, GAMEINPUT_InternalSystemButtonCallback, &hwdata->system_button_callback_token);
        }
#endif // GAMEINPUT_API_VERSION >= 1
    } else {
#if GAMEINPUT_API_VERSION >= 3
        joystick->naxes = info->controllerInfo->controllerAxisCount;
        joystick->nbuttons = info->controllerInfo->controllerButtonCount;
        joystick->nhats = info->controllerInfo->controllerSwitchCount;
#else
        joystick->naxes = info->controllerAxisCount;
        joystick->nbuttons = info->controllerButtonCount;
        joystick->nhats = info->controllerSwitchCount;
#endif // GAMEINPUT_API_VERSION >= 3
    }

    if (info->supportedRumbleMotors & (GameInputRumbleLowFrequency | GameInputRumbleHighFrequency)) {
        SDL_SetBooleanProperty(SDL_GetJoystickProperties(joystick), SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, true);
    }
    if (info->supportedRumbleMotors & (GameInputRumbleLeftTrigger | GameInputRumbleRightTrigger)) {
        SDL_SetBooleanProperty(SDL_GetJoystickProperties(joystick), SDL_PROP_JOYSTICK_CAP_TRIGGER_RUMBLE_BOOLEAN, true);
    }

#ifdef GAMEINPUT_SENSOR_SUPPORT
    if (info->supportedInput & GameInputKindSensors) {
        // FIXME: What's the sensor update rate?
        if (info->sensorsInfo->supportedSensors & GameInputSensorsGyrometer) {
            SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO, 250.0f);
        }
        if (info->sensorsInfo->supportedSensors & GameInputSensorsAccelerometer) {
            SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 250.0f);
        }
    }
#endif
    return true;
}

static bool GAMEINPUT_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
{
    // don't check for caps here, since SetRumbleState doesn't return any result - we don't need to check it
    GAMEINPUT_InternalJoystickHwdata *hwdata = joystick->hwdata;
    GameInputRumbleParams *params = &hwdata->rumbleParams;
    params->lowFrequency = (float)low_frequency_rumble / (float)SDL_MAX_UINT16;
    params->highFrequency = (float)high_frequency_rumble / (float)SDL_MAX_UINT16;
    hwdata->devref->device->SetRumbleState(params);
    return true;
}

static bool GAMEINPUT_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
{
    // don't check for caps here, since SetRumbleState doesn't return any result - we don't need to check it
    GAMEINPUT_InternalJoystickHwdata *hwdata = joystick->hwdata;
    GameInputRumbleParams *params = &hwdata->rumbleParams;
    params->leftTrigger = (float)left_rumble / (float)SDL_MAX_UINT16;
    params->rightTrigger = (float)right_rumble / (float)SDL_MAX_UINT16;
    hwdata->devref->device->SetRumbleState(params);
    return true;
}

static bool GAMEINPUT_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
{
    return SDL_Unsupported();
}

static bool GAMEINPUT_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
    return SDL_Unsupported();
}

static bool GAMEINPUT_JoystickSetSensorsEnabled(SDL_Joystick *joystick, bool enabled)
{
    joystick->hwdata->report_sensors = enabled;
    return true;
}

static void GAMEINPUT_GuitarUpdate(SDL_Joystick *joystick, IGameInputReading *reading, Uint64 timestamp)
{
    IGameInputRawDeviceReport* rawState;
    if (reading->GetRawReport(&rawState)) {
        static WORD s_GuitarButtons[] = {
            0x0010,  // SDL_GAMEPAD_BUTTON_SOUTH
            0x0020,  // SDL_GAMEPAD_BUTTON_EAST
            0x0040,  // SDL_GAMEPAD_BUTTON_WEST
            0x0080,  // SDL_GAMEPAD_BUTTON_NORTH
            0x0008,  // SDL_GAMEPAD_BUTTON_BACK
            0,       // The guide button is not available
            0x0004,  // SDL_GAMEPAD_BUTTON_START
            0,       // right joystick click unavailable
            0x4000,  // SDL_GAMEPAD_BUTTON_RIGHT_STICK
            0x1000,  // SDL_GAMEPAD_BUTTON_LEFT_SHOULDER
            0,       // right shoulder unavailable
        };
        Uint8 btnidx = 0, hat = 0;
        uint8_t rawData[40];
        SDL_memset(rawData, 0, sizeof(rawData));
        size_t len = rawState->GetRawData(sizeof(rawData), rawData);
        uint16_t buttons = rawData[0] | rawData[1] << 8;
        if (len >= 10) {
            for (btnidx = 0; btnidx < SDL_arraysize(s_GuitarButtons); ++btnidx) {
                WORD button_mask = s_GuitarButtons[btnidx];
                if (!button_mask) {
                    continue;
                }
                bool down = ((buttons & button_mask) != 0);
                SDL_SendJoystickButton(timestamp, joystick, btnidx, down);
            }
            if (buttons & 0x0100) {
                hat |= SDL_HAT_UP;
            }
            if (buttons & 0x0200) {
                hat |= SDL_HAT_DOWN;
            }
            if (buttons & 0x0400) {
                hat |= SDL_HAT_LEFT;
            }
            if (buttons & 0x0800) {
                hat |= SDL_HAT_RIGHT;
            }
            SDL_SendJoystickHat(timestamp, joystick, 0, hat);
            SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, (rawData[3] * 257) - 32768);
            // PS3 RB guitars had tilt on right shoulder
            SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, rawData[2] >= 0xD0);
            // PS3 RB guitars send L2 when using solo buttons
            SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, (rawData[6]) ? 32767 : -32768);
            // Align pickup selector mappings with PS3 instruments
            static const Sint16 effects_mappings[] = {-26880, -13568, -1792, 11008, 24576};
            SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, effects_mappings[rawData[4] >> 4]);
        }
    }
}

static void GAMEINPUT_GamepadUpdate(SDL_Joystick *joystick, IGameInputReading *reading, Uint64 timestamp) {
    GameInputGamepadState state;
    static WORD s_XInputButtons[] = {
        GameInputGamepadA,               // SDL_GAMEPAD_BUTTON_SOUTH
        GameInputGamepadB,               // SDL_GAMEPAD_BUTTON_EAST
        GameInputGamepadX,               // SDL_GAMEPAD_BUTTON_WEST
        GameInputGamepadY,               // SDL_GAMEPAD_BUTTON_NORTH
        GameInputGamepadView,            // SDL_GAMEPAD_BUTTON_BACK
        0,                               // The guide button is not available
        GameInputGamepadMenu,            // SDL_GAMEPAD_BUTTON_START
        GameInputGamepadLeftThumbstick,  // SDL_GAMEPAD_BUTTON_LEFT_STICK
        GameInputGamepadRightThumbstick, // SDL_GAMEPAD_BUTTON_RIGHT_STICK
        GameInputGamepadLeftShoulder,    // SDL_GAMEPAD_BUTTON_LEFT_SHOULDER
        GameInputGamepadRightShoulder,   // SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER
    };
    Uint8 btnidx = 0, hat = 0;

    if (reading->GetGamepadState(&state)) {
        for (btnidx = 0; btnidx < SDL_arraysize(s_XInputButtons); ++btnidx) {
            WORD button_mask = s_XInputButtons[btnidx];
            if (!button_mask) {
                continue;
            }
            bool down = ((state.buttons & button_mask) != 0);
            SDL_SendJoystickButton(timestamp, joystick, btnidx, down);
        }

        if (state.buttons & GameInputGamepadDPadUp) {
            hat |= SDL_HAT_UP;
        }
        if (state.buttons & GameInputGamepadDPadDown) {
            hat |= SDL_HAT_DOWN;
        }
        if (state.buttons & GameInputGamepadDPadLeft) {
            hat |= SDL_HAT_LEFT;
        }
        if (state.buttons & GameInputGamepadDPadRight) {
            hat |= SDL_HAT_RIGHT;
        }
        SDL_SendJoystickHat(timestamp, joystick, 0, hat);

#define CONVERT_AXIS(v) (Sint16)(((v) < 0.0f) ? ((v)*32768.0f) : ((v)*32767.0f))
        SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, CONVERT_AXIS(state.leftThumbstickX));
        SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, CONVERT_AXIS(-state.leftThumbstickY));
        SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, CONVERT_AXIS(state.rightThumbstickX));
        SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, CONVERT_AXIS(-state.rightThumbstickY));
#undef CONVERT_AXIS
#define CONVERT_TRIGGER(v) (Sint16)((v)*65535.0f - 32768.0f)
        SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, CONVERT_TRIGGER(state.leftTrigger));
        SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, CONVERT_TRIGGER(state.rightTrigger));
#undef CONVERT_TRIGGER
    }
}

static void GAMEINPUT_ControllerUpdate(SDL_Joystick *joystick, IGameInputReading *reading, Uint64 timestamp)
{
    bool *button_state = SDL_stack_alloc(bool, joystick->nbuttons);
    float *axis_state = SDL_stack_alloc(float, joystick->naxes);
    GameInputSwitchPosition *switch_state = SDL_stack_alloc(GameInputSwitchPosition, joystick->nhats);

    if (button_state) {
        uint32_t i;
        uint32_t button_count = reading->GetControllerButtonState(joystick->nbuttons, button_state);
        for (i = 0; i < button_count; ++i) {
            SDL_SendJoystickButton(timestamp, joystick, (Uint8)i, button_state[i]);
        }
        SDL_stack_free(button_state);
    }

#define CONVERT_AXIS(v) (Sint16)((v)*65535.0f - 32768.0f)
    if (axis_state) {
        uint32_t i;
        uint32_t axis_count = reading->GetControllerAxisState(joystick->naxes, axis_state);
        for (i = 0; i < axis_count; ++i) {
            SDL_SendJoystickAxis(timestamp, joystick, (Uint8)i, CONVERT_AXIS(axis_state[i]));
        }
        SDL_stack_free(axis_state);
    }
#undef CONVERT_AXIS

    if (switch_state) {
        uint32_t i;
        uint32_t switch_count = reading->GetControllerSwitchState(joystick->nhats, switch_state);
        for (i = 0; i < switch_count; ++i) {
            Uint8 hat;
            switch (switch_state[i]) {
            case GameInputSwitchUp:
                hat = SDL_HAT_UP;
                break;
            case GameInputSwitchUpRight:
                hat = SDL_HAT_UP | SDL_HAT_RIGHT;
                break;
            case GameInputSwitchRight:
                hat = SDL_HAT_RIGHT;
                break;
            case GameInputSwitchDownRight:
                hat = SDL_HAT_DOWN | SDL_HAT_RIGHT;
                break;
            case GameInputSwitchDown:
                hat = SDL_HAT_DOWN;
                break;
            case GameInputSwitchDownLeft:
                hat = SDL_HAT_DOWN | SDL_HAT_LEFT;
                break;
            case GameInputSwitchLeft:
                hat = SDL_HAT_LEFT;
                break;
            case GameInputSwitchUpLeft:
                hat = SDL_HAT_UP | SDL_HAT_LEFT;
                break;
            case GameInputSwitchCenter:
            default:
                hat = SDL_HAT_CENTERED;
                break;
            }
            SDL_SendJoystickHat(timestamp, joystick, (Uint8)i, hat);
        }
        SDL_stack_free(switch_state);
    }
}

static void GAMEINPUT_JoystickUpdate(SDL_Joystick *joystick)
{
    GAMEINPUT_InternalJoystickHwdata *hwdata = joystick->hwdata;
    GAMEINPUT_InternalDevice *internal_device = hwdata->devref;
    IGameInputDevice *device = hwdata->devref->device;
    const GameInputDeviceInfo *info = hwdata->devref->info;
    IGameInputReading *reading = NULL;
    Uint64 timestamp;
    HRESULT hr;

    hr = g_pGameInput->GetCurrentReading(info->supportedInput, device, &reading);
    if (FAILED(hr)) {
        // don't SetError here since there can be a legitimate case when there's no reading avail
        return;
    }

    timestamp = SDL_US_TO_NS(reading->GetTimestamp() + g_GameInputTimestampOffset);
    if (internal_device->raw_type == SDL_GAMEINPUT_RAWTYPE_ROCK_BAND_GUITAR) {
        GAMEINPUT_GuitarUpdate(joystick, reading, timestamp);
    } else if (GAMEINPUT_InternalIsGamepad(info)) {
        GAMEINPUT_GamepadUpdate(joystick, reading, timestamp);
    } else {
        GAMEINPUT_ControllerUpdate(joystick, reading, timestamp);
    }

#ifdef GAMEINPUT_SENSOR_SUPPORT
    if (hwdata->report_sensors) {
        GameInputSensorsState sensor_state;

        if (reading->GetSensorsState(&sensor_state)) {
            if ((info->sensorsInfo->supportedSensors & GameInputSensorsGyrometer) != 0) {
                float data[3] = {
                    sensor_state.angularVelocityInRadPerSecX,
                    sensor_state.angularVelocityInRadPerSecY,
                    sensor_state.angularVelocityInRadPerSecZ
                };
                SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO, timestamp, data, SDL_arraysize(data));
            }
            if ((info->sensorsInfo->supportedSensors & GameInputSensorsAccelerometer) != 0) {
                float data[3] = {
                    sensor_state.accelerationInGX * SDL_STANDARD_GRAVITY,
                    sensor_state.accelerationInGY * SDL_STANDARD_GRAVITY,
                    sensor_state.accelerationInGZ * SDL_STANDARD_GRAVITY
                };
                SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, timestamp, data, SDL_arraysize(data));
            }
        }
    }
#endif // GAMEINPUT_SENSOR_SUPPORT

    reading->Release();

    // FIXME: We can poll this at a much lower rate
    GAMEINPUT_UpdatePowerInfo(joystick, device);
}

static void GAMEINPUT_JoystickClose(SDL_Joystick *joystick)
{
    GAMEINPUT_InternalJoystickHwdata *hwdata = joystick->hwdata;

    if (hwdata->system_button_callback_token) {
#if GAMEINPUT_API_VERSION >= 1
        g_pGameInput->UnregisterCallback(hwdata->system_button_callback_token);
#else
        g_pGameInput->UnregisterCallback(hwdata->system_button_callback_token, 10000);
#endif
    }
    SDL_free(hwdata);

    joystick->hwdata = NULL;
}

static void GAMEINPUT_JoystickQuit(void)
{
    if (g_pGameInput) {
        // free the callback
        if (g_GameInputCallbackToken) {
#if GAMEINPUT_API_VERSION >= 1
            g_pGameInput->UnregisterCallback(g_GameInputCallbackToken);
#else
            g_pGameInput->UnregisterCallback(g_GameInputCallbackToken, 10000);
#endif
            g_GameInputCallbackToken = 0;
        }

        // free the list
        while (g_GameInputList.count > 0) {
            GAMEINPUT_InternalRemoveByIndex(0);
        }

        SDL_QuitGameInput();
        g_pGameInput = NULL;
    }
}

static bool GAMEINPUT_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
{
    GAMEINPUT_InternalDevice *elem = GAMEINPUT_InternalFindByIndex(device_index);

    if (!GAMEINPUT_InternalIsGamepad(elem->info) && elem->raw_type == SDL_GAMEINPUT_RAWTYPE_NONE) {
        return false;
    }

    out->a.kind = EMappingKind_Button;
    out->a.target = SDL_GAMEPAD_BUTTON_SOUTH;

    out->b.kind = EMappingKind_Button;
    out->b.target = SDL_GAMEPAD_BUTTON_EAST;

    out->x.kind = EMappingKind_Button;
    out->x.target = SDL_GAMEPAD_BUTTON_WEST;

    out->y.kind = EMappingKind_Button;
    out->y.target = SDL_GAMEPAD_BUTTON_NORTH;

    out->back.kind = EMappingKind_Button;
    out->back.target = SDL_GAMEPAD_BUTTON_BACK;

#if GAMEINPUT_API_VERSION >= 1
    if (elem->info->supportedSystemButtons & GameInputSystemButtonGuide) {
        out->guide.kind = EMappingKind_Button;
        out->guide.target = SDL_GAMEPAD_BUTTON_GUIDE;
    }

    if (elem->info->supportedSystemButtons & GameInputSystemButtonShare) {
        out->misc1.kind = EMappingKind_Button;
        out->misc1.target = SDL_GAMEPAD_BUTTON_GAMEINPUT_SHARE;
    }
#endif // GAMEINPUT_API_VERSION >= 1

    out->start.kind = EMappingKind_Button;
    out->start.target = SDL_GAMEPAD_BUTTON_START;

    out->leftstick.kind = EMappingKind_Button;
    out->leftstick.target = SDL_GAMEPAD_BUTTON_LEFT_STICK;

    out->rightstick.kind = EMappingKind_Button;
    out->rightstick.target = SDL_GAMEPAD_BUTTON_RIGHT_STICK;

    out->leftshoulder.kind = EMappingKind_Button;
    out->leftshoulder.target = SDL_GAMEPAD_BUTTON_LEFT_SHOULDER;

    out->rightshoulder.kind = EMappingKind_Button;
    out->rightshoulder.target = SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER;

    out->dpup.kind = EMappingKind_Hat;
    out->dpup.target = SDL_HAT_UP;

    out->dpdown.kind = EMappingKind_Hat;
    out->dpdown.target = SDL_HAT_DOWN;

    out->dpleft.kind = EMappingKind_Hat;
    out->dpleft.target = SDL_HAT_LEFT;

    out->dpright.kind = EMappingKind_Hat;
    out->dpright.target = SDL_HAT_RIGHT;

    out->leftx.kind = EMappingKind_Axis;
    out->leftx.target = SDL_GAMEPAD_AXIS_LEFTX;

    out->lefty.kind = EMappingKind_Axis;
    out->lefty.target = SDL_GAMEPAD_AXIS_LEFTY;

    out->rightx.kind = EMappingKind_Axis;
    out->rightx.target = SDL_GAMEPAD_AXIS_RIGHTX;

    out->righty.kind = EMappingKind_Axis;
    out->righty.target = SDL_GAMEPAD_AXIS_RIGHTY;

    out->lefttrigger.kind = EMappingKind_Axis;
    out->lefttrigger.target = SDL_GAMEPAD_AXIS_LEFT_TRIGGER;

    out->righttrigger.kind = EMappingKind_Axis;
    out->righttrigger.target = SDL_GAMEPAD_AXIS_RIGHT_TRIGGER;

    return true;
}


SDL_JoystickDriver SDL_GAMEINPUT_JoystickDriver =
{
    GAMEINPUT_JoystickInit,
    GAMEINPUT_JoystickGetCount,
    GAMEINPUT_JoystickDetect,
    GAMEINPUT_JoystickIsDevicePresent,
    GAMEINPUT_JoystickGetDeviceName,
    GAMEINPUT_JoystickGetDevicePath,
    GAMEINPUT_JoystickGetDeviceSteamVirtualGamepadSlot,
    GAMEINPUT_JoystickGetDevicePlayerIndex,
    GAMEINPUT_JoystickSetDevicePlayerIndex,
    GAMEINPUT_JoystickGetDeviceGUID,
    GAMEINPUT_JoystickGetDeviceInstanceID,
    GAMEINPUT_JoystickOpen,
    GAMEINPUT_JoystickRumble,
    GAMEINPUT_JoystickRumbleTriggers,
    GAMEINPUT_JoystickSetLED,
    GAMEINPUT_JoystickSendEffect,
    GAMEINPUT_JoystickSetSensorsEnabled,
    GAMEINPUT_JoystickUpdate,
    GAMEINPUT_JoystickClose,
    GAMEINPUT_JoystickQuit,
    GAMEINPUT_JoystickGetGamepadMapping
};


#endif // SDL_JOYSTICK_GAMEINPUT