forked from libretro-mirrors/beetle-saturn-libretro
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathinput.c
More file actions
2190 lines (1809 loc) · 73.9 KB
/
input.c
File metadata and controls
2190 lines (1809 loc) · 73.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "libretro.h"
#include "libretro_settings.h"
#include "mednafen/mednafen-types.h"
/* log_cb is the only symbol this TU uses from mednafen/git.h.
* Forward-declare directly so this TU's include surface stays
* minimal: retro_log_printf_t comes from libretro.h (above) and
* the defining TU for log_cb is libretro.c. Pulling in git.h
* here would also drag in emuspec.h / mdfn_gameinfo.h / state.h
* which input.c doesn't otherwise need. */
extern retro_log_printf_t log_cb;
#include "mednafen/ss/ss.h"
#include "mednafen/ss/smpc.h"
#include "mednafen/ss/cart.h" /* CART_STV (ActiveCartType compare for ST-V coin wireup) */
#include "mednafen/ss/stvio.h" /* STVIO_InsertCoin */
#include "mednafen/state.h"
#include <math.h>
#include <stdio.h>
//------------------------------------------------------------------------------
// Locals
//------------------------------------------------------------------------------
#define MAX_CONTROLLERS 12 /* 2x 6 player adaptors */
static retro_environment_t environ_cb; /* cached during input_init_env */
static unsigned players = 2;
static int astick_deadzone = 0;
static int trigger_deadzone = 0;
static const int TRIGGER_MAX = 0xFFFF;
static float mouse_sensitivity = 1.0f;
static unsigned geometry_width = 0;
static unsigned geometry_height = 0;
// Pointer/touchscreen state, per-player. Previously these were single
// globals shared across all players, which broke multi-player gun setups
// and caused player 0's hold state to clobber player 1's input.
// The release-frame debounce -- holding the gun position for one frame
// after touch release so that a quick light tap reads as a real shot --
// is implemented purely by `pointer_pressed[iplayer]` going 0->1 on
// press and 1->0 on release, with the release frame writing the latched
// position into INPUT_DATA before the input loop advances. Upstream
// once supported a multi-frame latch via a `POINTER_PRESSED_CYCLES`
// constant + `pointer_cycles_after_released[]` counter, but the cycle
// count had been pinned at 1 for some time and the >1-frame branch was
// unreachable; both have been removed.
static int pointer_pressed[ MAX_CONTROLLERS ] = {0};
static int pointer_pressed_last_x[ MAX_CONTROLLERS ] = {0};
static int pointer_pressed_last_y[ MAX_CONTROLLERS ] = {0};
typedef union
{
uint8_t u8[ 32 ];
uint16_t gun_pos[ 2 ];
uint16_t buttons;
} INPUT_DATA;
// Controller state buffer (per player)
static INPUT_DATA input_data[ MAX_CONTROLLERS ] = {0};
// Controller type (per player)
static uint32_t input_type[ MAX_CONTROLLERS ] = {0};
#define INPUT_MODE_3D_PAD_ANALOG ( 1 << 0 ) // Set means analog mode.
#define INPUT_MODE_3D_PAD_PREVIOUS_MASK ( 1 << 1 ) // Edge trigger helper.
#define INPUT_MODE_MISSION_THROTTLE_LATCH ( 1 << 2 ) // Latch throttle enabled?
#define INPUT_MODE_MISSION_THROTTLE_PREV ( 1 << 3 ) // Edge trigger helper.
#define INPUT_MODE_DEFAULT 0
#define INPUT_MODE_DEFAULT_3D_PAD INPUT_MODE_3D_PAD_ANALOG
// Mode switch for 3D Control Pad (per player)
static uint16_t input_mode[ MAX_CONTROLLERS ] = {0};
static int16_t input_throttle_latch[ MAX_CONTROLLERS ] = {0};
/* ST-V coin-insert edge-detect state. RETRO_DEVICE_ID_JOYPAD_SELECT
* doubles as the 3D Pad mode switch on consumer Saturn games (cf.
* input_map_3d_pad_mode_switch below) and as the coin button on ST-V
* games -- the two uses can't conflict since ST-V games don't use
* the 3D Pad. prev_coin[port] tracks last frame's SELECT bit per
* port; check_stv_coin_inserts edge-triggers on rising 0 -> 1 to
* dispatch one STVIO_InsertCoin() per press, gated on ActiveCartType
* == CART_STV. Both player 1 and player 2 SELECTs feed the same
* single CoinPending counter in stvio.c -- the ST-V coin slot is
* one-per-cabinet, so cumulating presses is the right model. */
static bool prev_coin[ MAX_CONTROLLERS ] = {0};
/* ST-V "misc input" port (port index 12 in SMPC's port-number space).
*
* One byte, bit layout (matches mednafen/ss/stvio.c consumers):
* bit 0 - SS reset button (stvio's TransformInput clears this every
* frame after read, so we only ever set it transiently)
* bit 2 - Test (operator-panel test button)
* bit 3 - Service (operator-panel service button -- free credit)
* bit 4 - Pause (operator-panel pause button -- not all games)
*
* Polarity is "1 = pressed" on our side; stvio XORs into DataIn[]
* which starts at 0xFF (active-low to the game) so the game sees
* 1=unpressed / 0=pressed as expected.
*
* Mednafen's standalone wires this up via STVIO_SetInput(12, ...).
* Our fork's input.c only iterated 0..MAX_CONTROLLERS-1 (= 0..11),
* never port 12, so DPtr[12] stayed NULL throughout ST-V game
* lifetime -- Test/Service/Pause were unreachable, and stvio.c
* needed defensive NULL guards to avoid crashing. Now we bind
* this byte at input_init time when ActiveCartType == CART_STV.
* update_stv_misc_inputs() populates the bits from per-frame poll
* state; the NULL guards in stvio.c become belt-and-suspenders. */
static uint8_t input_stv_misc_byte = 0;
/* ActiveCartType lives in mednafen/ss/ss.c. No header exposes it
* (it's a TU-local global with extern decls inline at the usage
* sites in ss.c itself), so input.c picks it up the same way. */
extern int ActiveCartType;
//------------------------------------------------------------------------------
// Supported Devices
//------------------------------------------------------------------------------
#define RETRO_DEVICE_SS_PAD RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 0)
#define RETRO_DEVICE_SS_3D_PAD RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 0)
#define RETRO_DEVICE_SS_WHEEL RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 1)
#define RETRO_DEVICE_SS_MISSION RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 2)
#define RETRO_DEVICE_SS_MISSION2 RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 3)
#define RETRO_DEVICE_SS_TWINSTICK RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 4)
#define RETRO_DEVICE_SS_MOUSE RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_MOUSE, 0)
#define RETRO_DEVICE_SS_GUN_JP RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_LIGHTGUN, 0)
#define RETRO_DEVICE_SS_GUN_US RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_LIGHTGUN, 1)
/* Sega Saturn Keyboard (US 101-key PS/2 adapter). The SMPC-side
* IODevice already exists in mednafen/ss/smpc_iodevice.c
* (IODevice_Keyboard_Create). We expose it to RetroArch as a
* KEYBOARD subclass so users can select it from the controller
* type list, and we pump scan-code state into the 18-byte phys[]
* bitmap that UpdateInput consumes. Subclass 1 is reserved for
* the JP keyboard (separate IDII layout) if/when it's exposed. */
#define RETRO_DEVICE_SS_KEYBOARD RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_KEYBOARD, 0)
enum { INPUT_DEVICE_TYPES_COUNT = 1 /*none*/ + 10 }; /* <-- update me! */
static const struct retro_controller_description input_device_types[ INPUT_DEVICE_TYPES_COUNT ] =
{
{ "Control Pad", RETRO_DEVICE_JOYPAD },
{ "3D Control Pad", RETRO_DEVICE_SS_3D_PAD },
{ "Virtua Gun", RETRO_DEVICE_SS_GUN_JP },
{ "Stunner", RETRO_DEVICE_SS_GUN_US },
{ "Mouse", RETRO_DEVICE_SS_MOUSE },
{ "Arcade Racer", RETRO_DEVICE_SS_WHEEL },
{ "Mission Stick", RETRO_DEVICE_SS_MISSION },
{ "Dual Mission Sticks", RETRO_DEVICE_SS_MISSION2 }, /*"Panzer Dragoon Zwei" only!*/
{ "Twin-Stick", RETRO_DEVICE_SS_TWINSTICK },
{ "Keyboard", RETRO_DEVICE_SS_KEYBOARD },
{ NULL, 0 },
};
static const struct retro_controller_info ports_no_6player[ 1 + 1 + 1 ] =
{
/* port one */
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
/* port two */
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ 0 },
};
static const struct retro_controller_info ports_left_6player[ 6 + 1 + 1 ] =
{
/* port one */
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
/* port two: 6player adaptor */
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ 0 },
};
static const struct retro_controller_info ports_right_6player[ 1 + 6 + 1 ] =
{
/* port one */
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
/* port two: 6player adaptor */
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ 0 },
};
static const struct retro_controller_info ports_two_6player[ 6 + 6 + 1 ] =
{
/* port one: 6player adaptor */
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
/* port two: 6player adaptor */
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ input_device_types, INPUT_DEVICE_TYPES_COUNT },
{ 0 },
};
/* Lookup table to select ports info for all combinations
of 6player adaptor connection. */
static const struct retro_controller_info* ports_lut[ 4 ] =
{
ports_no_6player,
ports_left_6player,
ports_right_6player,
ports_two_6player
};
//------------------------------------------------------------------------------
// Mapping Helpers
//------------------------------------------------------------------------------
/* Control Pad (default) */
enum { INPUT_MAP_PAD_SIZE = 12 };
static const unsigned input_map_pad[ INPUT_MAP_PAD_SIZE ] =
{
// libretro input at position || maps to Saturn on bit
//-----------------------------------------------------------------------------
RETRO_DEVICE_ID_JOYPAD_L, // L1 -> Z 0
RETRO_DEVICE_ID_JOYPAD_X, // X(top) -> Y 1
RETRO_DEVICE_ID_JOYPAD_Y, // Y(left) -> X 2
RETRO_DEVICE_ID_JOYPAD_R2, // R2 -> R 3
RETRO_DEVICE_ID_JOYPAD_UP, // Pad-Up -> Pad-Up 4
RETRO_DEVICE_ID_JOYPAD_DOWN, // Pad-Down -> Pad-Down 5
RETRO_DEVICE_ID_JOYPAD_LEFT, // Pad-Left -> Pad-Left 6
RETRO_DEVICE_ID_JOYPAD_RIGHT, // Pad-Right -> Pad-Right 7
RETRO_DEVICE_ID_JOYPAD_A, // A(right) -> B 8
RETRO_DEVICE_ID_JOYPAD_R, // R1 -> C 9
RETRO_DEVICE_ID_JOYPAD_B, // B(down) -> A 10
RETRO_DEVICE_ID_JOYPAD_START, // Start -> Start 11
};
static const unsigned input_map_pad_left_shoulder =
RETRO_DEVICE_ID_JOYPAD_L2; // L2 -> L 15
/* 3D Control Pad */
enum { INPUT_MAP_3D_PAD_SIZE = 11 };
static const unsigned input_map_3d_pad[ INPUT_MAP_3D_PAD_SIZE ] =
{
// libretro input at position || maps to Saturn on bit
//-----------------------------------------------------------------------------
RETRO_DEVICE_ID_JOYPAD_UP, // Pad-Up -> Pad-Up 0
RETRO_DEVICE_ID_JOYPAD_DOWN, // Pad-Down -> Pad-Down 1
RETRO_DEVICE_ID_JOYPAD_LEFT, // Pad-Left -> Pad-Left 2
RETRO_DEVICE_ID_JOYPAD_RIGHT, // Pad-Right -> Pad-Right 3
RETRO_DEVICE_ID_JOYPAD_A, // A(right) -> B 4
RETRO_DEVICE_ID_JOYPAD_R, // R1 -> C 5
RETRO_DEVICE_ID_JOYPAD_B, // B(down) -> A 6
RETRO_DEVICE_ID_JOYPAD_START, // Start -> Start 7
RETRO_DEVICE_ID_JOYPAD_L, // L1 -> Z 8
RETRO_DEVICE_ID_JOYPAD_X, // X(top) -> Y 9
RETRO_DEVICE_ID_JOYPAD_Y, // Y(left) -> X 10
};
static const unsigned input_map_3d_pad_mode_switch = RETRO_DEVICE_ID_JOYPAD_SELECT;
/* Arcade Racer (wheel) */
enum { INPUT_MAP_WHEEL_BITSHIFT = 4 };
enum { INPUT_MAP_WHEEL_SIZE = 7 };
static const unsigned input_map_wheel[ INPUT_MAP_WHEEL_SIZE ] =
{
// libretro input at position || maps to Saturn on bit
//-----------------------------------------------------------------------------
RETRO_DEVICE_ID_JOYPAD_A, // A(right) -> B 4
RETRO_DEVICE_ID_JOYPAD_R, // R1 -> C 5
RETRO_DEVICE_ID_JOYPAD_B, // B(down) -> A 6
RETRO_DEVICE_ID_JOYPAD_START, // Start -> Start 7
RETRO_DEVICE_ID_JOYPAD_L, // L1 -> Z 8
RETRO_DEVICE_ID_JOYPAD_X, // X(top) -> Y 9
RETRO_DEVICE_ID_JOYPAD_Y, // Y(left) -> X 10
};
static const unsigned input_map_wheel_shift_left = RETRO_DEVICE_ID_JOYPAD_L2;
static const unsigned input_map_wheel_shift_right = RETRO_DEVICE_ID_JOYPAD_R2;
/* Mission Stick */
enum { INPUT_MAP_MISSION_SIZE = 8 };
static const unsigned input_map_mission[ INPUT_MAP_MISSION_SIZE ] =
{
// libretro input at position || maps to Saturn on bit
//-----------------------------------------------------------------------------
RETRO_DEVICE_ID_JOYPAD_A, // A(right) -> B 0
RETRO_DEVICE_ID_JOYPAD_R, // R1 -> C 1
RETRO_DEVICE_ID_JOYPAD_B, // B(down) -> A 2
RETRO_DEVICE_ID_JOYPAD_START, // Start -> Start 3
RETRO_DEVICE_ID_JOYPAD_L, // L1 -> Z 4
RETRO_DEVICE_ID_JOYPAD_X, // X(top) -> Y 5
RETRO_DEVICE_ID_JOYPAD_Y, // Y(left) -> X 6
RETRO_DEVICE_ID_JOYPAD_R2, // R2 -> R 7
};
static const unsigned input_map_mission_left_shoulder =
RETRO_DEVICE_ID_JOYPAD_L2; // L2 -> L 15
static const unsigned input_map_mission_throttle_latch = RETRO_DEVICE_ID_JOYPAD_R3;
/* Twin-Stick */
static const unsigned input_map_twinstick_left_trigger = RETRO_DEVICE_ID_JOYPAD_L2;
static const unsigned input_map_twinstick_left_button = RETRO_DEVICE_ID_JOYPAD_L;
static const unsigned input_map_twinstick_right_trigger = RETRO_DEVICE_ID_JOYPAD_R2;
static const unsigned input_map_twinstick_right_button = RETRO_DEVICE_ID_JOYPAD_R;
/* Sega Saturn US 101-key keyboard scan-code map.
*
* Each entry maps a libretro RETROK_ keysym to the Saturn keyboard's
* 8-bit scan-code value as documented in the IDII table in upstream
* mednafen/ss/input/keyboard.cpp. Scan codes 0x00-0x8F are valid
* (144 slots, of which ~89 are real keys; the rest are IDII Padding
* slots that don't correspond to any physical key).
*
* poll_ss_keyboard() walks this table, queries each RETROK_ via
* input_state_cb(port, RETRO_DEVICE_KEYBOARD, 0, key), and ORs the
* corresponding scan-code bit into the 18-byte phys-bitmap buffer
* that IODevice_Keyboard_UpdateInput consumes (see smpc_iodevice.c).
*
* Keys deliberately omitted: Windows / Meta / Super keys, since the
* PS/2-adapter-emulated keyboard didn't expose them reliably (cf.
* upstream comment in keyboard.cpp). Print Screen has no RETROK_
* equivalent that's commonly mapped, so it's also dropped. */
struct ss_keyboard_map_entry { uint16_t retro_key; uint8_t saturn_sc; };
static const struct ss_keyboard_map_entry ss_keyboard_map[] = {
{ RETROK_F9, 0x01 },
{ RETROK_F5, 0x03 },
{ RETROK_F3, 0x04 },
{ RETROK_F1, 0x05 },
{ RETROK_F2, 0x06 },
{ RETROK_F12, 0x07 },
{ RETROK_F10, 0x09 },
{ RETROK_F8, 0x0A },
{ RETROK_F6, 0x0B },
{ RETROK_F4, 0x0C },
{ RETROK_TAB, 0x0D },
{ RETROK_BACKQUOTE, 0x0E },
{ RETROK_LALT, 0x11 },
{ RETROK_LSHIFT, 0x12 },
{ RETROK_LCTRL, 0x14 },
{ RETROK_q, 0x15 },
{ RETROK_1, 0x16 },
{ RETROK_RALT, 0x17 },
{ RETROK_RCTRL, 0x18 },
{ RETROK_KP_ENTER, 0x19 },
{ RETROK_z, 0x1A },
{ RETROK_s, 0x1B },
{ RETROK_a, 0x1C },
{ RETROK_w, 0x1D },
{ RETROK_2, 0x1E },
{ RETROK_c, 0x21 },
{ RETROK_x, 0x22 },
{ RETROK_d, 0x23 },
{ RETROK_e, 0x24 },
{ RETROK_4, 0x25 },
{ RETROK_3, 0x26 },
{ RETROK_SPACE, 0x29 },
{ RETROK_v, 0x2A },
{ RETROK_f, 0x2B },
{ RETROK_t, 0x2C },
{ RETROK_r, 0x2D },
{ RETROK_5, 0x2E },
{ RETROK_n, 0x31 },
{ RETROK_b, 0x32 },
{ RETROK_h, 0x33 },
{ RETROK_g, 0x34 },
{ RETROK_y, 0x35 },
{ RETROK_6, 0x36 },
{ RETROK_m, 0x3A },
{ RETROK_j, 0x3B },
{ RETROK_u, 0x3C },
{ RETROK_7, 0x3D },
{ RETROK_8, 0x3E },
{ RETROK_COMMA, 0x41 },
{ RETROK_k, 0x42 },
{ RETROK_i, 0x43 },
{ RETROK_o, 0x44 },
{ RETROK_0, 0x45 },
{ RETROK_9, 0x46 },
{ RETROK_PERIOD, 0x49 },
{ RETROK_SLASH, 0x4A },
{ RETROK_l, 0x4B },
{ RETROK_SEMICOLON, 0x4C },
{ RETROK_p, 0x4D },
{ RETROK_MINUS, 0x4E },
{ RETROK_QUOTE, 0x52 },
{ RETROK_LEFTBRACKET, 0x54 },
{ RETROK_EQUALS, 0x55 },
{ RETROK_CAPSLOCK, 0x58 },
{ RETROK_RSHIFT, 0x59 },
{ RETROK_RETURN, 0x5A },
{ RETROK_RIGHTBRACKET, 0x5B },
{ RETROK_BACKSLASH, 0x5D },
{ RETROK_BACKSPACE, 0x66 },
{ RETROK_KP1, 0x69 },
{ RETROK_KP4, 0x6B },
{ RETROK_KP7, 0x6C },
{ RETROK_KP0, 0x70 },
{ RETROK_KP_PERIOD, 0x71 },
{ RETROK_KP2, 0x72 },
{ RETROK_KP5, 0x73 },
{ RETROK_KP6, 0x74 },
{ RETROK_KP8, 0x75 },
{ RETROK_ESCAPE, 0x76 },
{ RETROK_NUMLOCK, 0x77 },
{ RETROK_F11, 0x78 },
{ RETROK_KP_PLUS, 0x79 },
{ RETROK_KP3, 0x7A },
{ RETROK_KP_MINUS, 0x7B },
{ RETROK_KP_MULTIPLY, 0x7C },
{ RETROK_KP9, 0x7D },
{ RETROK_SCROLLOCK, 0x7E },
{ RETROK_KP_DIVIDE, 0x80 },
{ RETROK_INSERT, 0x81 },
{ RETROK_PAUSE, 0x82 },
{ RETROK_F7, 0x83 },
{ RETROK_DELETE, 0x85 },
{ RETROK_LEFT, 0x86 },
{ RETROK_HOME, 0x87 },
{ RETROK_END, 0x88 },
{ RETROK_UP, 0x89 },
{ RETROK_DOWN, 0x8A },
{ RETROK_PAGEUP, 0x8B },
{ RETROK_PAGEDOWN, 0x8C },
{ RETROK_RIGHT, 0x8D },
};
#define SS_KEYBOARD_MAP_SIZE ( sizeof(ss_keyboard_map) / sizeof(ss_keyboard_map[0]) )
//------------------------------------------------------------------------------
// Local Functions
//------------------------------------------------------------------------------
static void get_analog_axis( retro_input_state_t input_state_cb,
int player_index,
int stick,
int axis,
int* p_analog )
{
int analog = input_state_cb( player_index, RETRO_DEVICE_ANALOG, stick, axis );
// Analog stick deadzone
if ( astick_deadzone > 0 )
{
static const int ASTICK_MAX = 0x8000;
const float scale = ((float)ASTICK_MAX/(float)(ASTICK_MAX - astick_deadzone));
if ( analog < -astick_deadzone )
{
// Re-scale analog stick range
float scaled = (-analog - astick_deadzone)*scale;
analog = (int)round(-scaled);
if (analog < -32767)
analog = -32767;
}
else if ( analog > astick_deadzone )
{
// Re-scale analog stick range
float scaled = (analog - astick_deadzone)*scale;
analog = (int)round(scaled);
if (analog > +32767)
analog = +32767;
}
else
analog = 0;
}
// output
*p_analog = analog;
}
static void get_analog_stick( retro_input_state_t input_state_cb,
int player_index,
int stick,
int* p_analog_x,
int* p_analog_y )
{
int analog_x = input_state_cb( player_index, RETRO_DEVICE_ANALOG, stick, RETRO_DEVICE_ID_ANALOG_X );
int analog_y = input_state_cb( player_index, RETRO_DEVICE_ANALOG, stick, RETRO_DEVICE_ID_ANALOG_Y );
// Analog stick deadzone (borrowed code from parallel-n64 core)
if ( astick_deadzone > 0 )
{
static const int ASTICK_MAX = 0x8000;
// Convert cartesian coordinate analog stick to polar coordinates
double radius = sqrt(analog_x * analog_x + analog_y * analog_y);
double angle = atan2(analog_y, analog_x);
if (radius > astick_deadzone)
{
// Re-scale analog stick range to negate deadzone (makes slow movements possible)
radius = (radius - astick_deadzone)*((float)ASTICK_MAX/(ASTICK_MAX - astick_deadzone));
// Convert back to cartesian coordinates
analog_x = (int)round(radius * cos(angle));
analog_y = (int)round(radius * sin(angle));
// Clamp to correct range
if (analog_x > +32767) analog_x = +32767;
if (analog_x < -32767) analog_x = -32767;
if (analog_y > +32767) analog_y = +32767;
if (analog_y < -32767) analog_y = -32767;
}
else
{
analog_x = 0;
analog_y = 0;
}
}
// output
*p_analog_x = analog_x;
*p_analog_y = analog_y;
}
static uint32_t apply_trigger_deadzone( uint32_t input )
{
// Scale by two and apply outer deadzone (about 1%)
input = ( input * 66191 ) / 32768;
// Inner deadzone
if ( trigger_deadzone > 0 )
{
// trigger_deadzone is a signed int (user-configurable from libretro
// core options) but the > 0 guard above means it's safe to treat
// as unsigned for the comparison/subtraction against the uint32_t
// input. The alias also silences a sign-compare warning.
const uint32_t deadzone = (uint32_t)trigger_deadzone;
const float scale = ((float)TRIGGER_MAX/(float)(TRIGGER_MAX - trigger_deadzone));
if ( input > deadzone )
{
// Re-scale analog range
float scaled = (input - deadzone)*scale;
input = (int)round(scaled);
}
else
input = 0;
}
// Clamp
if (input > TRIGGER_MAX)
input = TRIGGER_MAX;
return input;
}
static uint16_t get_analog_trigger( retro_input_state_t input_state_cb,
int player_index,
int id )
{
// NOTE: Analog triggers were added Nov 2017. Not all front-ends support this
// feature (or pre-date it) so we need to handle this in a graceful way.
// First, try and get an analog value using the new libretro API constant
uint16_t trigger = input_state_cb( player_index,
RETRO_DEVICE_ANALOG,
RETRO_DEVICE_INDEX_ANALOG_BUTTON,
id );
if ( trigger == 0 )
{
// If we got exactly zero, we're either not pressing the button, or the front-end
// is not reporting analog values. We need to do a second check using the classic
// digital API method, to at least get some response - better than nothing.
// NOTE: If we're really just not holding the trigger, we're still going to get zero.
if (input_state_cb( player_index,
RETRO_DEVICE_JOYPAD,
0,
id ))
return 0xFFFF;
return 0;
}
// We got something, which means the front-end can handle analog buttons.
// So we apply a deadzone to the input and use it.
// Mednafen wants 0 - 65535 so we scale up from 0 - 32767
return apply_trigger_deadzone( (unsigned)trigger );
}
//------------------------------------------------------------------------------
// Global Functions
//------------------------------------------------------------------------------
void input_init_env( retro_environment_t _environ_cb )
{
#define RETRO_DESCRIPTOR_BLOCK( _user ) \
{ _user, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" }, \
{ _user, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" }, \
{ _user, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" }, \
{ _user, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" }, \
{ _user, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "A Button" }, \
{ _user, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "B Button" }, \
{ _user, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "C Button" }, \
{ _user, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "X Button" }, \
{ _user, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Y Button" }, \
{ _user, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "Z Button" }, \
{ _user, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L Button" }, \
{ _user, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R Button" }, \
{ _user, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start Button" }, \
{ _user, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Mode Switch / Coin (ST-V)" }, \
{ _user, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "Test (ST-V)" }, \
{ _user, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "Service (ST-V) -- chord with Test for Pause" }, \
{ _user, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Analog X" }, \
{ _user, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Analog Y" }, \
{ _user, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Analog X (Right)" }, \
{ _user, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Analog Y (Right)" }, \
{ _user, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_TRIGGER, "Gun Trigger" }, \
{ _user, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_START, "Gun Start" }, \
{ _user, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_RELOAD, "Gun Reload" }, \
{ _user, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_LEFT, "Mouse A" }, \
{ _user, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_RIGHT, "Mouse B" }, \
{ _user, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_MIDDLE, "Mouse C" }, \
{ _user, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_BUTTON_4, "Mouse Start" }, \
{ _user, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_BUTTON_5, "Mouse Start (Alt)" }
struct retro_input_descriptor desc[] =
{
RETRO_DESCRIPTOR_BLOCK( 0 ),
RETRO_DESCRIPTOR_BLOCK( 1 ),
RETRO_DESCRIPTOR_BLOCK( 2 ),
RETRO_DESCRIPTOR_BLOCK( 3 ),
RETRO_DESCRIPTOR_BLOCK( 4 ),
RETRO_DESCRIPTOR_BLOCK( 5 ),
RETRO_DESCRIPTOR_BLOCK( 6 ),
RETRO_DESCRIPTOR_BLOCK( 7 ),
RETRO_DESCRIPTOR_BLOCK( 8 ),
RETRO_DESCRIPTOR_BLOCK( 9 ),
RETRO_DESCRIPTOR_BLOCK( 10 ),
RETRO_DESCRIPTOR_BLOCK( 11 ),
{ 0 },
};
#undef RETRO_DESCRIPTOR_BLOCK
// Cache this
environ_cb = _environ_cb;
/* Send to front-end */
environ_cb( RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc );
}
void input_set_env( retro_environment_t environ_cb )
{
/* Pick ports selection */
const struct retro_controller_info* ports = ports_lut[ setting_multitap_port1 + setting_multitap_port2 * 2 ];
/* Send to front-end */
environ_cb( RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports );
}
void input_init(void)
{
unsigned i;
// Initialise to default pad type and bind input buffers to SMPC emulation.
for ( i = 0; i < MAX_CONTROLLERS; ++i )
{
input_type[ i ] = RETRO_DEVICE_JOYPAD;
input_mode[ i ] = INPUT_MODE_DEFAULT;
input_throttle_latch[ i ] = 0;
SS_SetInput( i, "gamepad", (uint8_t*)&input_data[ i ] );
}
/* Bind the ST-V misc-input port (test/service/pause/reset bits).
* SS_SetInput routes port 12 to BOTH STVIO_SetInput (which sets
* DPtr[12] = ptr) and SMPC_SetInput (which sets MiscInputPtr).
* Gated on ActiveCartType so consumer Saturn games don't get a
* stray misc-input pointer registered with SMPC -- it'd be
* harmless there (the byte is always 0), but skipping the call
* keeps the SMPC state matching Saturn-only expectations. */
if ( ActiveCartType == CART_STV )
{
input_stv_misc_byte = 0;
SS_SetInput( 12, "misc", &input_stv_misc_byte );
}
}
void input_set_geometry(unsigned width, unsigned height)
{
geometry_width = width;
geometry_height = height;
}
void input_set_deadzone_stick( int percent )
{
if ( percent >= 0 && percent <= 100 )
astick_deadzone = (int)( percent * 0.01f * 0x8000);
}
void input_set_deadzone_trigger( int percent )
{
if ( percent >= 0 && percent <= 100 )
trigger_deadzone = (int)( percent * 0.01f * TRIGGER_MAX);
}
void input_set_mouse_sensitivity( int percent )
{
if ( percent > 0 && percent <= 200 )
mouse_sensitivity = (float)percent / 100.0f;
}
/* Rising-edge detect SELECT on each port; on press, queue one ST-V
* coin via STVIO_InsertCoin. Called from the tail of both the
* bitmask and per-key polling paths. No-op outside ST-V mode so
* the prev_coin[] state stays valid across cart transitions without
* resetting -- if a user swaps ST-V game while pressing SELECT, the
* next press on the new game still edge-triggers (the held-through
* state stays true, the next 0 -> 1 transition is the next press).
*
* The MASK fast path reads all buttons in one input_state_cb call;
* the non-bitmask path is a per-button query. Both share this
* helper, which queries SELECT once per port directly -- one extra
* per-port input_state_cb call vs. piggy-backing on the existing
* per-player input_type dispatch, but cheaper than threading the
* coin-bit through every input-type branch and easier to reason
* about (input_type-specific transforms don't bleed into the ST-V
* coin path). */
static void check_stv_coin_inserts( retro_input_state_t input_state_cb )
{
unsigned port;
if ( ActiveCartType != CART_STV )
return;
for ( port = 0; port < MAX_CONTROLLERS && port < 2; ++port )
{
const bool now = !!input_state_cb( port, RETRO_DEVICE_JOYPAD, 0,
RETRO_DEVICE_ID_JOYPAD_SELECT );
if ( now && !prev_coin[ port ] )
STVIO_InsertCoin();
prev_coin[ port ] = now;
}
}
/* Per-frame populate of the ST-V misc-input byte (test/service/pause).
* Bound by input_init via SS_SetInput(12, "misc", &input_stv_misc_byte)
* when ActiveCartType == CART_STV; stvio.c reads the byte through
* DPtr[12].
*
* Inputs are level-triggered (not edge-triggered like the coin button)
* because the underlying ST-V misc-input register samples continuously
* -- holding "test" should keep the test menu navigation responsive,
* not pulse it once.
*
* Source bindings (P1 controller, retropad):
* L3 -> Test
* R3 -> Service
* L3 + R3 chord -> Pause
*
* The chord for Pause keeps the binding count down (no fourth button
* needed) and avoids accidentally bumping Pause when the user just
* means to hold Test or Service. Only port 0 (P1's pad) drives these
* -- the physical ST-V cabinet has a single operator panel, so taking
* exactly one controller's chord as the source is the closest analogue.
*
* Bit 0 (reset button) is left zeroed here -- stvio's TransformInput
* clears it after each read anyway, and there's no obvious mapping
* for a "soft-reset the Saturn from the front panel" button at this
* layer (RetroArch's reset hotkey calls our retro_reset() instead). */
static void update_stv_misc_inputs( retro_input_state_t input_state_cb )
{
bool l3;
bool r3;
uint8_t misc = 0;
if ( ActiveCartType != CART_STV )
return;
l3 = !!input_state_cb( 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3 );
r3 = !!input_state_cb( 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3 );
if ( l3 && !r3 ) misc |= 0x04; /* Test (bit 2) */
if ( r3 && !l3 ) misc |= 0x08; /* Service (bit 3) */
if ( l3 && r3 ) misc |= 0x10; /* Pause (bit 4) */
input_stv_misc_byte = misc;
}
/* Per-frame Saturn keyboard poll. Translates RETROK_ keysyms (the
* libretro keyboard event stream surfaced through input_state_cb's
* RETRO_DEVICE_KEYBOARD path) into the 18-byte Saturn scan-code
* bitmap that IODevice_Keyboard_UpdateInput consumes via DPtr[port].
*
* Bit layout: scan code sc lives at byte (sc >> 3) bit (sc & 7), LSB
* first within the byte. Buffer is fully zeroed at the head of each
* frame because the consumer XORs against its own previous-frame
* `processed[]` to detect make/break edges -- it relies on the input
* bitmap being a complete present-state snapshot, not a delta.
*
* Called only from the RETRO_DEVICE_SS_KEYBOARD case in the poll-path
* switch (input_update / input_update_with_bitmasks), so the function
* itself doesn't have to gate on input_type. */
static void poll_ss_keyboard( retro_input_state_t input_state_cb, unsigned iplayer, uint8_t *buf )
{
unsigned i;
memset( buf, 0, 18 );
for ( i = 0; i < SS_KEYBOARD_MAP_SIZE; ++i )
{
const uint8_t sc = ss_keyboard_map[ i ].saturn_sc;
if ( input_state_cb( iplayer, RETRO_DEVICE_KEYBOARD, 0,
ss_keyboard_map[ i ].retro_key ) )
buf[ sc >> 3 ] |= (uint8_t)(1u << (sc & 7));
}
}
void input_update_with_bitmasks( retro_input_state_t input_state_cb )
{
unsigned iplayer;
// For each player (logical controller)
for ( iplayer = 0; iplayer < players; ++iplayer )
{
INPUT_DATA* p_input = &(input_data[ iplayer ]);
int16_t ret = input_state_cb( iplayer, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_MASK );
// reset input
p_input->buttons = 0;
// What kind of controller is connected?
switch ( input_type[ iplayer ] )
{
case RETRO_DEVICE_JOYPAD:
case RETRO_DEVICE_SS_PAD:
{
int i;
//
// -- standard control pad buttons + d-pad
// input_map_pad is configured to quickly map libretro buttons to the correct bits for the Saturn.
for ( i = 0; i < INPUT_MAP_PAD_SIZE; ++i )
{
if (ret & (1 << input_map_pad[ i ]))
p_input->buttons |= ( 1 << i );
}
// .. the left trigger on the Saturn is a special case since there's a gap in the bits.
if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_L2))
p_input->buttons |= ( 1 << 15 );
if (!opposite_directions)
{
if ((p_input->buttons & 0x30) == 0x30)
p_input->buttons &= ~0x30;
if ((p_input->buttons & 0xC0) == 0xC0)
p_input->buttons &= ~0xC0;
}
break;
}
case RETRO_DEVICE_SS_TWINSTICK:
{
int analog_lx, analog_ly;
int analog_rx, analog_ry;
const int thresh = 16000;
get_analog_stick( input_state_cb, iplayer, RETRO_DEVICE_INDEX_ANALOG_LEFT, &analog_lx, &analog_ly );
get_analog_stick( input_state_cb, iplayer, RETRO_DEVICE_INDEX_ANALOG_RIGHT, &analog_rx, &analog_ry );
// left-stick
if ( analog_ly <= -thresh )
p_input->buttons |= ( 1 << 4 ); // Up
if ( analog_lx >= thresh )
p_input->buttons |= ( 1 << 7 ); // Right
if ( analog_ly >= thresh )
p_input->buttons |= ( 1 << 5 ); // Down
if ( analog_lx <= -thresh )
p_input->buttons |= ( 1 << 6 ); // Left
// right-stick
if ( analog_ry <= -thresh )
p_input->buttons |= ( 1 << 1 ); // Up <-(Y)
if ( analog_rx >= thresh )
p_input->buttons |= ( 1 << 0 ); // Right <-(Z)
if ( analog_ry >= thresh )
p_input->buttons |= ( 1 << 8 ); // Down <-(B)
if ( analog_rx <= -thresh )
p_input->buttons |= ( 1 << 2 ); // Left <-(X)
// left trigger
if (ret & (1 << input_map_twinstick_left_trigger))
p_input->buttons |= (1 << 15);
// left button
if (ret & (1 << input_map_twinstick_left_button))
p_input->buttons |= ( 1 << 3 );
// right trigger
if (ret & (1 << input_map_twinstick_right_trigger))
p_input->buttons |= ( 1 << 10 );
// right button
if (ret & (1 << input_map_twinstick_right_button))
p_input->buttons |= ( 1 << 9 );
// start
if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_START))
p_input->buttons |= ( 1 << 11 );
}
break;
case RETRO_DEVICE_SS_3D_PAD:
{
int i;
int analog_x, analog_y;
uint16_t thumb_x, thumb_y;
uint16_t l_trigger, r_trigger;
//
// -- 3d control pad buttons
// input_map_3d_pad is configured to quickly map libretro buttons to the correct bits for the Saturn.
for ( i = 0; i < INPUT_MAP_3D_PAD_SIZE; ++i )
if (ret & (1 << input_map_3d_pad[ i ]))
p_input->buttons |= ( 1 << i );
//
// -- analog stick
get_analog_stick( input_state_cb, iplayer, RETRO_DEVICE_INDEX_ANALOG_LEFT, &analog_x, &analog_y );
// mednafen wants 0 - 32767 - 65535
thumb_x = (uint16_t)(analog_x + 32767);
thumb_y = (uint16_t)(analog_y + 32767);
//
// -- triggers
// mednafen wants 0 - 65535
l_trigger = get_analog_trigger( input_state_cb, iplayer, RETRO_DEVICE_ID_JOYPAD_L2 );
r_trigger = get_analog_trigger( input_state_cb, iplayer, RETRO_DEVICE_ID_JOYPAD_R2 );
//
// -- mode switch
{
// Handle MODE button as a switch
uint16_t prev = ( input_mode[iplayer] & INPUT_MODE_3D_PAD_PREVIOUS_MASK );
uint16_t held = 0;
if (ret & (1 << input_map_3d_pad_mode_switch))
held = INPUT_MODE_3D_PAD_PREVIOUS_MASK;
// Rising edge trigger
if ( !prev && held )
{
char text[ 256 ];
struct retro_message msg;
// Toggle 'state' bit: analog/digital mode
input_mode[ iplayer ] ^= INPUT_MODE_3D_PAD_ANALOG;
// Tell user
if ( input_mode[iplayer] & INPUT_MODE_3D_PAD_ANALOG )
sprintf( text, "Controller %u: Analog Mode", (iplayer+1) );
else
sprintf( text, "Controller %u: Digital Mode", (iplayer+1) );
msg.msg = text;
msg.frames = 180;
environ_cb( RETRO_ENVIRONMENT_SET_MESSAGE, &msg );
}
// Store held state in 'previous' bit.
input_mode[ iplayer ] = ( input_mode[ iplayer ] & ~INPUT_MODE_3D_PAD_PREVIOUS_MASK ) | held;
}
//
// -- format input data