-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathtestcontroller.c
2243 lines (1980 loc) · 79.9 KB
/
testcontroller.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 1997-2025 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.
*/
/* Simple program to test the SDL controller routines */
#define SDL_MAIN_USE_CALLBACKS
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <SDL3/SDL_test.h>
#include <SDL3/SDL_test_font.h>
#ifdef SDL_PLATFORM_EMSCRIPTEN
#include <emscripten/emscripten.h>
#endif
#include "gamepadutils.h"
#include "testutils.h"
#if 0
#define DEBUG_AXIS_MAPPING
#endif
#define TITLE_HEIGHT 48.0f
#define PANEL_SPACING 25.0f
#define PANEL_WIDTH 250.0f
#define MINIMUM_BUTTON_WIDTH 96.0f
#define BUTTON_MARGIN 16.0f
#define BUTTON_PADDING 12.0f
#define GAMEPAD_WIDTH 512.0f
#define GAMEPAD_HEIGHT 560.0f
#define SCREEN_WIDTH (PANEL_WIDTH + PANEL_SPACING + GAMEPAD_WIDTH + PANEL_SPACING + PANEL_WIDTH)
#define SCREEN_HEIGHT (TITLE_HEIGHT + GAMEPAD_HEIGHT)
typedef struct
{
bool m_bMoving;
int m_nLastValue;
int m_nStartingValue;
int m_nFarthestValue;
} AxisState;
typedef struct
{
SDL_JoystickID id;
SDL_Joystick *joystick;
int num_axes;
AxisState *axis_state;
SDL_Gamepad *gamepad;
char *mapping;
bool has_bindings;
int audio_route;
int trigger_effect;
} Controller;
static SDLTest_CommonState *state;
static SDL_Window *window = NULL;
static SDL_Renderer *screen = NULL;
static ControllerDisplayMode display_mode = CONTROLLER_MODE_TESTING;
static GamepadImage *image = NULL;
static GamepadDisplay *gamepad_elements = NULL;
static GamepadTypeDisplay *gamepad_type = NULL;
static JoystickDisplay *joystick_elements = NULL;
static GamepadButton *setup_mapping_button = NULL;
static GamepadButton *done_mapping_button = NULL;
static GamepadButton *cancel_button = NULL;
static GamepadButton *clear_button = NULL;
static GamepadButton *copy_button = NULL;
static GamepadButton *paste_button = NULL;
static char *backup_mapping = NULL;
static bool done = false;
static bool set_LED = false;
static int num_controllers = 0;
static Controller *controllers;
static Controller *controller;
static SDL_JoystickID mapping_controller = 0;
static int binding_element = SDL_GAMEPAD_ELEMENT_INVALID;
static int last_binding_element = SDL_GAMEPAD_ELEMENT_INVALID;
static bool binding_flow = false;
static int binding_flow_direction = 0;
static Uint64 binding_advance_time = 0;
static SDL_FRect title_area;
static bool title_highlighted;
static bool title_pressed;
static SDL_FRect type_area;
static bool type_highlighted;
static bool type_pressed;
static char *controller_name;
static SDL_Joystick *virtual_joystick = NULL;
static SDL_GamepadAxis virtual_axis_active = SDL_GAMEPAD_AXIS_INVALID;
static float virtual_axis_start_x;
static float virtual_axis_start_y;
static SDL_GamepadButton virtual_button_active = SDL_GAMEPAD_BUTTON_INVALID;
static bool virtual_touchpad_active = false;
static float virtual_touchpad_x;
static float virtual_touchpad_y;
static int s_arrBindingOrder[] = {
/* Standard sequence */
SDL_GAMEPAD_BUTTON_SOUTH,
SDL_GAMEPAD_BUTTON_EAST,
SDL_GAMEPAD_BUTTON_WEST,
SDL_GAMEPAD_BUTTON_NORTH,
SDL_GAMEPAD_BUTTON_DPAD_LEFT,
SDL_GAMEPAD_BUTTON_DPAD_RIGHT,
SDL_GAMEPAD_BUTTON_DPAD_UP,
SDL_GAMEPAD_BUTTON_DPAD_DOWN,
SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_NEGATIVE,
SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_POSITIVE,
SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_NEGATIVE,
SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_POSITIVE,
SDL_GAMEPAD_BUTTON_LEFT_STICK,
SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_NEGATIVE,
SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_POSITIVE,
SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_NEGATIVE,
SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_POSITIVE,
SDL_GAMEPAD_BUTTON_RIGHT_STICK,
SDL_GAMEPAD_BUTTON_LEFT_SHOULDER,
SDL_GAMEPAD_ELEMENT_AXIS_LEFT_TRIGGER,
SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER,
SDL_GAMEPAD_ELEMENT_AXIS_RIGHT_TRIGGER,
SDL_GAMEPAD_BUTTON_BACK,
SDL_GAMEPAD_BUTTON_START,
SDL_GAMEPAD_BUTTON_GUIDE,
SDL_GAMEPAD_BUTTON_MISC1,
SDL_GAMEPAD_ELEMENT_INVALID,
/* Paddle sequence */
SDL_GAMEPAD_BUTTON_RIGHT_PADDLE1,
SDL_GAMEPAD_BUTTON_LEFT_PADDLE1,
SDL_GAMEPAD_BUTTON_RIGHT_PADDLE2,
SDL_GAMEPAD_BUTTON_LEFT_PADDLE2,
SDL_GAMEPAD_ELEMENT_INVALID,
};
static const char *GetSensorName(SDL_SensorType sensor)
{
switch (sensor) {
case SDL_SENSOR_ACCEL:
return "accelerometer";
case SDL_SENSOR_GYRO:
return "gyro";
case SDL_SENSOR_ACCEL_L:
return "accelerometer (L)";
case SDL_SENSOR_GYRO_L:
return "gyro (L)";
case SDL_SENSOR_ACCEL_R:
return "accelerometer (R)";
case SDL_SENSOR_GYRO_R:
return "gyro (R)";
default:
return "UNKNOWN";
}
}
/* PS5 trigger effect documentation:
https://controllers.fandom.com/wiki/Sony_DualSense#FFB_Trigger_Modes
*/
typedef struct
{
Uint8 ucEnableBits1; /* 0 */
Uint8 ucEnableBits2; /* 1 */
Uint8 ucRumbleRight; /* 2 */
Uint8 ucRumbleLeft; /* 3 */
Uint8 ucHeadphoneVolume; /* 4 */
Uint8 ucSpeakerVolume; /* 5 */
Uint8 ucMicrophoneVolume; /* 6 */
Uint8 ucAudioEnableBits; /* 7 */
Uint8 ucMicLightMode; /* 8 */
Uint8 ucAudioMuteBits; /* 9 */
Uint8 rgucRightTriggerEffect[11]; /* 10 */
Uint8 rgucLeftTriggerEffect[11]; /* 21 */
Uint8 rgucUnknown1[6]; /* 32 */
Uint8 ucLedFlags; /* 38 */
Uint8 rgucUnknown2[2]; /* 39 */
Uint8 ucLedAnim; /* 41 */
Uint8 ucLedBrightness; /* 42 */
Uint8 ucPadLights; /* 43 */
Uint8 ucLedRed; /* 44 */
Uint8 ucLedGreen; /* 45 */
Uint8 ucLedBlue; /* 46 */
} DS5EffectsState_t;
static void CyclePS5AudioRoute(Controller *device)
{
DS5EffectsState_t effects;
device->audio_route = (device->audio_route + 1) % 4;
SDL_zero(effects);
switch (device->audio_route) {
case 0:
/* Audio disabled */
effects.ucEnableBits1 |= (0x80 | 0x20 | 0x10); /* Modify audio route and speaker / headphone volume */
effects.ucSpeakerVolume = 0; /* Minimum volume */
effects.ucHeadphoneVolume = 0; /* Minimum volume */
effects.ucAudioEnableBits = 0x00; /* Output to headphones */
break;
case 1:
/* Headphones */
effects.ucEnableBits1 |= (0x80 | 0x10); /* Modify audio route and headphone volume */
effects.ucHeadphoneVolume = 50; /* 50% volume - don't blast into the ears */
effects.ucAudioEnableBits = 0x00; /* Output to headphones */
break;
case 2:
/* Speaker */
effects.ucEnableBits1 |= (0x80 | 0x20); /* Modify audio route and speaker volume */
effects.ucSpeakerVolume = 100; /* Maximum volume */
effects.ucAudioEnableBits = 0x30; /* Output to speaker */
break;
case 3:
/* Both */
effects.ucEnableBits1 |= (0x80 | 0x20 | 0x10); /* Modify audio route and speaker / headphone volume */
effects.ucSpeakerVolume = 100; /* Maximum volume */
effects.ucHeadphoneVolume = 50; /* 50% volume - don't blast into the ears */
effects.ucAudioEnableBits = 0x20; /* Output to both speaker and headphones */
break;
}
SDL_SendGamepadEffect(device->gamepad, &effects, sizeof(effects));
}
static void CyclePS5TriggerEffect(Controller *device)
{
DS5EffectsState_t effects;
Uint8 trigger_effects[3][11] = {
/* Clear trigger effect */
{ 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
/* Constant resistance across entire trigger pull */
{ 0x01, 0, 110, 0, 0, 0, 0, 0, 0, 0, 0 },
/* Resistance and vibration when trigger is pulled */
{ 0x06, 15, 63, 128, 0, 0, 0, 0, 0, 0, 0 },
};
device->trigger_effect = (device->trigger_effect + 1) % SDL_arraysize(trigger_effects);
SDL_zero(effects);
effects.ucEnableBits1 |= (0x04 | 0x08); /* Modify right and left trigger effect respectively */
SDL_memcpy(effects.rgucRightTriggerEffect, trigger_effects[device->trigger_effect], sizeof(trigger_effects[0]));
SDL_memcpy(effects.rgucLeftTriggerEffect, trigger_effects[device->trigger_effect], sizeof(trigger_effects[0]));
SDL_SendGamepadEffect(device->gamepad, &effects, sizeof(effects));
}
static void ClearButtonHighlights(void)
{
title_highlighted = false;
title_pressed = false;
type_highlighted = false;
type_pressed = false;
ClearGamepadImage(image);
SetGamepadDisplayHighlight(gamepad_elements, SDL_GAMEPAD_ELEMENT_INVALID, false);
SetGamepadTypeDisplayHighlight(gamepad_type, SDL_GAMEPAD_TYPE_UNSELECTED, false);
SetGamepadButtonHighlight(setup_mapping_button, false, false);
SetGamepadButtonHighlight(done_mapping_button, false, false);
SetGamepadButtonHighlight(cancel_button, false, false);
SetGamepadButtonHighlight(clear_button, false, false);
SetGamepadButtonHighlight(copy_button, false, false);
SetGamepadButtonHighlight(paste_button, false, false);
}
static void UpdateButtonHighlights(float x, float y, bool button_down)
{
ClearButtonHighlights();
if (display_mode == CONTROLLER_MODE_TESTING) {
SetGamepadButtonHighlight(setup_mapping_button, GamepadButtonContains(setup_mapping_button, x, y), button_down);
} else if (display_mode == CONTROLLER_MODE_BINDING) {
SDL_FPoint point;
int gamepad_highlight_element = SDL_GAMEPAD_ELEMENT_INVALID;
char *joystick_highlight_element;
point.x = x;
point.y = y;
if (SDL_PointInRectFloat(&point, &title_area)) {
title_highlighted = true;
title_pressed = button_down;
} else {
title_highlighted = false;
title_pressed = false;
}
if (SDL_PointInRectFloat(&point, &type_area)) {
type_highlighted = true;
type_pressed = button_down;
} else {
type_highlighted = false;
type_pressed = false;
}
if (controller->joystick != virtual_joystick) {
gamepad_highlight_element = GetGamepadImageElementAt(image, x, y);
}
if (gamepad_highlight_element == SDL_GAMEPAD_ELEMENT_INVALID) {
gamepad_highlight_element = GetGamepadDisplayElementAt(gamepad_elements, controller->gamepad, x, y);
}
SetGamepadDisplayHighlight(gamepad_elements, gamepad_highlight_element, button_down);
if (binding_element == SDL_GAMEPAD_ELEMENT_TYPE) {
int gamepad_highlight_type = GetGamepadTypeDisplayAt(gamepad_type, x, y);
SetGamepadTypeDisplayHighlight(gamepad_type, gamepad_highlight_type, button_down);
}
joystick_highlight_element = GetJoystickDisplayElementAt(joystick_elements, controller->joystick, x, y);
SetJoystickDisplayHighlight(joystick_elements, joystick_highlight_element, button_down);
SDL_free(joystick_highlight_element);
SetGamepadButtonHighlight(done_mapping_button, GamepadButtonContains(done_mapping_button, x, y), button_down);
SetGamepadButtonHighlight(cancel_button, GamepadButtonContains(cancel_button, x, y), button_down);
SetGamepadButtonHighlight(clear_button, GamepadButtonContains(clear_button, x, y), button_down);
SetGamepadButtonHighlight(copy_button, GamepadButtonContains(copy_button, x, y), button_down);
SetGamepadButtonHighlight(paste_button, GamepadButtonContains(paste_button, x, y), button_down);
}
}
static int StandardizeAxisValue(int nValue)
{
if (nValue > SDL_JOYSTICK_AXIS_MAX / 2) {
return SDL_JOYSTICK_AXIS_MAX;
} else if (nValue < SDL_JOYSTICK_AXIS_MIN / 2) {
return SDL_JOYSTICK_AXIS_MIN;
} else {
return 0;
}
}
static void RefreshControllerName(void)
{
const char *name = NULL;
SDL_free(controller_name);
controller_name = NULL;
if (controller) {
if (controller->gamepad) {
name = SDL_GetGamepadName(controller->gamepad);
} else {
name = SDL_GetJoystickName(controller->joystick);
}
}
if (name) {
controller_name = SDL_strdup(name);
} else {
controller_name = SDL_strdup("");
}
}
static void SetAndFreeGamepadMapping(char *mapping)
{
SDL_SetGamepadMapping(controller->id, mapping);
SDL_free(mapping);
}
static void SetCurrentBindingElement(int element, bool flow)
{
int i;
if (binding_element == SDL_GAMEPAD_ELEMENT_NAME) {
RefreshControllerName();
}
if (element == SDL_GAMEPAD_ELEMENT_INVALID) {
binding_flow_direction = 0;
last_binding_element = SDL_GAMEPAD_ELEMENT_INVALID;
} else {
last_binding_element = binding_element;
}
binding_element = element;
binding_flow = flow || (element == SDL_GAMEPAD_BUTTON_SOUTH);
binding_advance_time = 0;
for (i = 0; i < controller->num_axes; ++i) {
controller->axis_state[i].m_nFarthestValue = controller->axis_state[i].m_nStartingValue;
}
SetGamepadDisplaySelected(gamepad_elements, element);
}
static void SetNextBindingElement(void)
{
int i;
if (binding_element == SDL_GAMEPAD_ELEMENT_INVALID) {
return;
}
for (i = 0; i < SDL_arraysize(s_arrBindingOrder); ++i) {
if (binding_element == s_arrBindingOrder[i]) {
binding_flow_direction = 1;
SetCurrentBindingElement(s_arrBindingOrder[i + 1], true);
return;
}
}
SetCurrentBindingElement(SDL_GAMEPAD_ELEMENT_INVALID, false);
}
static void SetPrevBindingElement(void)
{
int i;
if (binding_element == SDL_GAMEPAD_ELEMENT_INVALID) {
return;
}
for (i = 1; i < SDL_arraysize(s_arrBindingOrder); ++i) {
if (binding_element == s_arrBindingOrder[i]) {
binding_flow_direction = -1;
SetCurrentBindingElement(s_arrBindingOrder[i - 1], true);
return;
}
}
SetCurrentBindingElement(SDL_GAMEPAD_ELEMENT_INVALID, false);
}
static void StopBinding(void)
{
SetCurrentBindingElement(SDL_GAMEPAD_ELEMENT_INVALID, false);
}
typedef struct
{
int axis;
int direction;
} AxisInfo;
static bool ParseAxisInfo(const char *description, AxisInfo *info)
{
if (!description) {
return false;
}
if (*description == '-') {
info->direction = -1;
++description;
} else if (*description == '+') {
info->direction = 1;
++description;
} else {
info->direction = 0;
}
if (description[0] == 'a' && SDL_isdigit(description[1])) {
++description;
info->axis = SDL_atoi(description);
return true;
}
return false;
}
static void CommitBindingElement(const char *binding, bool force)
{
char *mapping;
int direction = 1;
bool ignore_binding = false;
if (binding_element == SDL_GAMEPAD_ELEMENT_INVALID) {
return;
}
if (controller->mapping) {
mapping = SDL_strdup(controller->mapping);
} else {
mapping = NULL;
}
/* If the controller generates multiple events for a single element, pick the best one */
if (!force && binding_advance_time) {
char *current = GetElementBinding(mapping, binding_element);
bool native_button = (binding_element < SDL_GAMEPAD_BUTTON_COUNT);
bool native_axis = (binding_element >= SDL_GAMEPAD_BUTTON_COUNT &&
binding_element <= SDL_GAMEPAD_ELEMENT_AXIS_MAX);
bool native_trigger = (binding_element == SDL_GAMEPAD_ELEMENT_AXIS_LEFT_TRIGGER ||
binding_element == SDL_GAMEPAD_ELEMENT_AXIS_RIGHT_TRIGGER);
bool native_dpad = (binding_element == SDL_GAMEPAD_BUTTON_DPAD_UP ||
binding_element == SDL_GAMEPAD_BUTTON_DPAD_DOWN ||
binding_element == SDL_GAMEPAD_BUTTON_DPAD_LEFT ||
binding_element == SDL_GAMEPAD_BUTTON_DPAD_RIGHT);
if (native_button) {
bool current_button = (current && *current == 'b');
bool proposed_button = (binding && *binding == 'b');
if (current_button && !proposed_button) {
ignore_binding = true;
}
/* Use the lower index button (we map from lower to higher button index) */
if (current_button && proposed_button && current[1] < binding[1]) {
ignore_binding = true;
}
}
if (native_axis) {
AxisInfo current_axis_info = { 0, 0 };
AxisInfo proposed_axis_info = { 0, 0 };
bool current_axis = ParseAxisInfo(current, ¤t_axis_info);
bool proposed_axis = ParseAxisInfo(binding, &proposed_axis_info);
if (current_axis) {
/* Ignore this unless the proposed binding extends the existing axis */
ignore_binding = true;
if (native_trigger &&
((*current == '-' && *binding == '+' &&
SDL_strcmp(current + 1, binding + 1) == 0) ||
(*current == '+' && *binding == '-' &&
SDL_strcmp(current + 1, binding + 1) == 0))) {
/* Merge two half axes into a whole axis for a trigger */
++binding;
ignore_binding = false;
}
/* Use the lower index axis (we map from lower to higher axis index) */
if (proposed_axis && proposed_axis_info.axis < current_axis_info.axis) {
ignore_binding = false;
}
}
}
if (native_dpad) {
bool current_hat = (current && *current == 'h');
bool proposed_hat = (binding && *binding == 'h');
if (current_hat && !proposed_hat) {
ignore_binding = true;
}
/* Use the lower index hat (we map from lower to higher hat index) */
if (current_hat && proposed_hat && current[1] < binding[1]) {
ignore_binding = true;
}
}
SDL_free(current);
}
if (!ignore_binding && binding_flow && !force) {
int existing = GetElementForBinding(mapping, binding);
if (existing != SDL_GAMEPAD_ELEMENT_INVALID) {
SDL_GamepadButton action_forward = SDL_GAMEPAD_BUTTON_SOUTH;
SDL_GamepadButton action_backward = SDL_GAMEPAD_BUTTON_EAST;
SDL_GamepadButton action_delete = SDL_GAMEPAD_BUTTON_WEST;
if (binding_element == action_forward) {
/* Bind it! */
} else if (binding_element == action_backward) {
if (existing == action_forward) {
bool bound_backward = MappingHasElement(controller->mapping, action_backward);
if (bound_backward) {
/* Just move on to the next one */
ignore_binding = true;
SetNextBindingElement();
} else {
/* You can't skip the backward action, go back and start over */
ignore_binding = true;
SetPrevBindingElement();
}
} else if (existing == action_backward && binding_flow_direction == -1) {
/* Keep going backwards */
ignore_binding = true;
SetPrevBindingElement();
} else {
/* Bind it! */
}
} else if (existing == action_forward) {
/* Just move on to the next one */
ignore_binding = true;
SetNextBindingElement();
} else if (existing == action_backward) {
ignore_binding = true;
SetPrevBindingElement();
} else if (existing == binding_element) {
/* We're rebinding the same thing, just move to the next one */
ignore_binding = true;
SetNextBindingElement();
} else if (existing == action_delete) {
/* Clear the current binding and move to the next one */
binding = NULL;
direction = 1;
force = true;
} else if (binding_element != action_forward &&
binding_element != action_backward) {
/* Actually, we'll just clear the existing binding */
/*ignore_binding = true;*/
}
}
}
if (ignore_binding) {
SDL_free(mapping);
return;
}
mapping = ClearMappingBinding(mapping, binding);
mapping = SetElementBinding(mapping, binding_element, binding);
SetAndFreeGamepadMapping(mapping);
if (force) {
if (binding_flow) {
if (direction > 0) {
SetNextBindingElement();
} else if (direction < 0) {
SetPrevBindingElement();
}
} else {
StopBinding();
}
} else {
/* Wait to see if any more bindings come in */
binding_advance_time = SDL_GetTicks() + 30;
}
}
static void ClearBinding(void)
{
CommitBindingElement(NULL, true);
}
static void SetDisplayMode(ControllerDisplayMode mode)
{
float x, y;
SDL_MouseButtonFlags button_state;
if (mode == CONTROLLER_MODE_BINDING) {
/* Make a backup of the current mapping */
if (controller->mapping) {
backup_mapping = SDL_strdup(controller->mapping);
}
mapping_controller = controller->id;
if (MappingHasBindings(backup_mapping)) {
SetCurrentBindingElement(SDL_GAMEPAD_ELEMENT_INVALID, false);
} else {
SetCurrentBindingElement(SDL_GAMEPAD_BUTTON_SOUTH, true);
}
} else {
if (backup_mapping) {
SDL_free(backup_mapping);
backup_mapping = NULL;
}
mapping_controller = 0;
StopBinding();
}
display_mode = mode;
SetGamepadImageDisplayMode(image, mode);
SetGamepadDisplayDisplayMode(gamepad_elements, mode);
button_state = SDL_GetMouseState(&x, &y);
SDL_RenderCoordinatesFromWindow(screen, x, y, &x, &y);
UpdateButtonHighlights(x, y, button_state ? true : false);
}
static void CancelMapping(void)
{
SetAndFreeGamepadMapping(backup_mapping);
backup_mapping = NULL;
SetDisplayMode(CONTROLLER_MODE_TESTING);
}
static void ClearMapping(void)
{
SetAndFreeGamepadMapping(NULL);
SetCurrentBindingElement(SDL_GAMEPAD_ELEMENT_INVALID, false);
}
static void CopyMapping(void)
{
if (controller && controller->mapping) {
SDL_SetClipboardText(controller->mapping);
}
}
static void PasteMapping(void)
{
if (controller) {
char *mapping = SDL_GetClipboardText();
if (MappingHasBindings(mapping)) {
StopBinding();
SDL_SetGamepadMapping(controller->id, mapping);
RefreshControllerName();
} else {
/* Not a valid mapping, ignore it */
}
SDL_free(mapping);
}
}
static void CommitControllerName(void)
{
char *mapping = NULL;
if (controller->mapping) {
mapping = SDL_strdup(controller->mapping);
} else {
mapping = NULL;
}
mapping = SetMappingName(mapping, controller_name);
SetAndFreeGamepadMapping(mapping);
}
static void AddControllerNameText(const char *text)
{
size_t current_length = (controller_name ? SDL_strlen(controller_name) : 0);
size_t text_length = SDL_strlen(text);
size_t size = current_length + text_length + 1;
char *name = (char *)SDL_realloc(controller_name, size);
if (name) {
SDL_memcpy(&name[current_length], text, text_length + 1);
controller_name = name;
}
CommitControllerName();
}
static void BackspaceControllerName(void)
{
size_t length = (controller_name ? SDL_strlen(controller_name) : 0);
if (length > 0) {
controller_name[length - 1] = '\0';
}
CommitControllerName();
}
static void ClearControllerName(void)
{
if (controller_name) {
*controller_name = '\0';
}
CommitControllerName();
}
static void CopyControllerName(void)
{
SDL_SetClipboardText(controller_name);
}
static void PasteControllerName(void)
{
SDL_free(controller_name);
controller_name = SDL_GetClipboardText();
CommitControllerName();
}
static void CommitGamepadType(SDL_GamepadType type)
{
char *mapping = NULL;
if (controller->mapping) {
mapping = SDL_strdup(controller->mapping);
} else {
mapping = NULL;
}
mapping = SetMappingType(mapping, type);
SetAndFreeGamepadMapping(mapping);
}
static const char *GetBindingInstruction(void)
{
switch (binding_element) {
case SDL_GAMEPAD_ELEMENT_INVALID:
return "Select an element to bind from the list on the left";
case SDL_GAMEPAD_BUTTON_SOUTH:
case SDL_GAMEPAD_BUTTON_EAST:
case SDL_GAMEPAD_BUTTON_WEST:
case SDL_GAMEPAD_BUTTON_NORTH:
switch (SDL_GetGamepadButtonLabelForType(GetGamepadImageType(image), (SDL_GamepadButton)binding_element)) {
case SDL_GAMEPAD_BUTTON_LABEL_A:
return "Press the A button";
case SDL_GAMEPAD_BUTTON_LABEL_B:
return "Press the B button";
case SDL_GAMEPAD_BUTTON_LABEL_X:
return "Press the X button";
case SDL_GAMEPAD_BUTTON_LABEL_Y:
return "Press the Y button";
case SDL_GAMEPAD_BUTTON_LABEL_CROSS:
return "Press the Cross (X) button";
case SDL_GAMEPAD_BUTTON_LABEL_CIRCLE:
return "Press the Circle button";
case SDL_GAMEPAD_BUTTON_LABEL_SQUARE:
return "Press the Square button";
case SDL_GAMEPAD_BUTTON_LABEL_TRIANGLE:
return "Press the Triangle button";
default:
return "";
}
break;
case SDL_GAMEPAD_BUTTON_BACK:
return "Press the left center button (Back/View/Share)";
case SDL_GAMEPAD_BUTTON_GUIDE:
return "Press the center button (Home/Guide)";
case SDL_GAMEPAD_BUTTON_START:
return "Press the right center button (Start/Menu/Options)";
case SDL_GAMEPAD_BUTTON_LEFT_STICK:
return "Press the left thumbstick button (LSB/L3)";
case SDL_GAMEPAD_BUTTON_RIGHT_STICK:
return "Press the right thumbstick button (RSB/R3)";
case SDL_GAMEPAD_BUTTON_LEFT_SHOULDER:
return "Press the left shoulder button (LB/L1)";
case SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER:
return "Press the right shoulder button (RB/R1)";
case SDL_GAMEPAD_BUTTON_DPAD_UP:
return "Press the D-Pad up";
case SDL_GAMEPAD_BUTTON_DPAD_DOWN:
return "Press the D-Pad down";
case SDL_GAMEPAD_BUTTON_DPAD_LEFT:
return "Press the D-Pad left";
case SDL_GAMEPAD_BUTTON_DPAD_RIGHT:
return "Press the D-Pad right";
case SDL_GAMEPAD_BUTTON_MISC1:
return "Press the bottom center button (Share/Capture)";
case SDL_GAMEPAD_BUTTON_RIGHT_PADDLE1:
return "Press the upper paddle under your right hand";
case SDL_GAMEPAD_BUTTON_LEFT_PADDLE1:
return "Press the upper paddle under your left hand";
case SDL_GAMEPAD_BUTTON_RIGHT_PADDLE2:
return "Press the lower paddle under your right hand";
case SDL_GAMEPAD_BUTTON_LEFT_PADDLE2:
return "Press the lower paddle under your left hand";
case SDL_GAMEPAD_BUTTON_TOUCHPAD:
return "Press down on the touchpad";
case SDL_GAMEPAD_BUTTON_MISC2:
case SDL_GAMEPAD_BUTTON_MISC3:
case SDL_GAMEPAD_BUTTON_MISC4:
case SDL_GAMEPAD_BUTTON_MISC5:
case SDL_GAMEPAD_BUTTON_MISC6:
return "Press any additional button not already bound";
case SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_NEGATIVE:
return "Move the left thumbstick to the left";
case SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_POSITIVE:
return "Move the left thumbstick to the right";
case SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_NEGATIVE:
return "Move the left thumbstick up";
case SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_POSITIVE:
return "Move the left thumbstick down";
case SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_NEGATIVE:
return "Move the right thumbstick to the left";
case SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_POSITIVE:
return "Move the right thumbstick to the right";
case SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_NEGATIVE:
return "Move the right thumbstick up";
case SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_POSITIVE:
return "Move the right thumbstick down";
case SDL_GAMEPAD_ELEMENT_AXIS_LEFT_TRIGGER:
return "Pull the left trigger (LT/L2)";
case SDL_GAMEPAD_ELEMENT_AXIS_RIGHT_TRIGGER:
return "Pull the right trigger (RT/R2)";
case SDL_GAMEPAD_ELEMENT_NAME:
return "Type the name of your controller";
case SDL_GAMEPAD_ELEMENT_TYPE:
return "Select the type of your controller";
default:
return "";
}
}
static int FindController(SDL_JoystickID id)
{
int i;
for (i = 0; i < num_controllers; ++i) {
if (id == controllers[i].id) {
return i;
}
}
return -1;
}
static void SetController(SDL_JoystickID id)
{
int i = FindController(id);
if (i < 0 && num_controllers > 0) {
i = 0;
}
if (i >= 0) {
controller = &controllers[i];
} else {
controller = NULL;
}
RefreshControllerName();
}
static void AddController(SDL_JoystickID id, bool verbose)
{
Controller *new_controllers;
Controller *new_controller;
SDL_Joystick *joystick;
if (FindController(id) >= 0) {
/* We already have this controller */
return;
}
new_controllers = (Controller *)SDL_realloc(controllers, (num_controllers + 1) * sizeof(*controllers));
if (!new_controllers) {
return;
}
controller = NULL;
controllers = new_controllers;
new_controller = &new_controllers[num_controllers++];
SDL_zerop(new_controller);
new_controller->id = id;
new_controller->joystick = SDL_OpenJoystick(id);
if (new_controller->joystick) {
new_controller->num_axes = SDL_GetNumJoystickAxes(new_controller->joystick);
new_controller->axis_state = (AxisState *)SDL_calloc(new_controller->num_axes, sizeof(*new_controller->axis_state));
}
joystick = new_controller->joystick;
if (joystick) {
if (verbose && !SDL_IsGamepad(id)) {
const char *name = SDL_GetJoystickName(joystick);
const char *path = SDL_GetJoystickPath(joystick);
char guid[33];
SDL_Log("Opened joystick %s%s%s", name, path ? ", " : "", path ? path : "");
SDL_GUIDToString(SDL_GetJoystickGUID(joystick), guid, sizeof(guid));
SDL_Log("No gamepad mapping for %s", guid);
}
} else {
SDL_Log("Couldn't open joystick: %s", SDL_GetError());
}
if (mapping_controller) {
SetController(mapping_controller);
} else {
SetController(id);
}
}
static void DelController(SDL_JoystickID id)
{
int i = FindController(id);
if (i < 0) {
return;
}
if (display_mode == CONTROLLER_MODE_BINDING && id == controller->id) {
SetDisplayMode(CONTROLLER_MODE_TESTING);
}
/* Reset trigger state */
if (controllers[i].trigger_effect != 0) {
controllers[i].trigger_effect = -1;
CyclePS5TriggerEffect(&controllers[i]);
}
SDL_assert(controllers[i].gamepad == NULL);
if (controllers[i].axis_state) {
SDL_free(controllers[i].axis_state);
}
if (controllers[i].joystick) {
SDL_CloseJoystick(controllers[i].joystick);
}
--num_controllers;
if (i < num_controllers) {
SDL_memcpy(&controllers[i], &controllers[i + 1], (num_controllers - i) * sizeof(*controllers));
}
if (mapping_controller) {
SetController(mapping_controller);
} else {
SetController(id);
}
}
static void HandleGamepadRemapped(SDL_JoystickID id)
{
char *mapping;
int i = FindController(id);
SDL_assert(i >= 0);
if (i < 0) {
return;
}
if (!controllers[i].gamepad) {
/* Failed to open this controller */
return;
}
/* Get the current mapping */
mapping = SDL_GetGamepadMapping(controllers[i].gamepad);
/* Make sure the mapping has a valid name */
if (mapping && !MappingHasName(mapping)) {
mapping = SetMappingName(mapping, SDL_GetJoystickName(controllers[i].joystick));
}