forked from libretro-mirrors/beetle-saturn-libretro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
1343 lines (1101 loc) · 44.1 KB
/
input.cpp
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"
#include "mednafen/ss/ss.h"
#include "mednafen/ss/smpc.h"
#include "mednafen/state.h"
#include <math.h>
//------------------------------------------------------------------------------
// Locals
//------------------------------------------------------------------------------
static retro_environment_t environ_cb; /* cached during input_init_env */
#define MAX_CONTROLLERS 12 /* 2x 6 player adaptors */
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;
static int pointer_pressed = 0;
static const int POINTER_PRESSED_CYCLES = 4;
static int pointer_cycles_after_released = 0;
static int pointer_pressed_last_x = 0;
static int pointer_pressed_last_y = 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};
//------------------------------------------------------------------------------
// 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_MOUSE RETRO_DEVICE_SUBCLASS( RETRO_DEVICE_MOUSE, 0 )
#define RETRO_DEVICE_SS_TWINSTICK RETRO_DEVICE_SUBCLASS( RETRO_DEVICE_ANALOG, 4 )
#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 )
enum { INPUT_DEVICE_TYPES_COUNT = 1 /*none*/ + 9 }; /* <-- 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 },
{ "Arcade Racer", RETRO_DEVICE_SS_WHEEL },
{ "Mission Stick", RETRO_DEVICE_SS_MISSION },
{ "Mouse", RETRO_DEVICE_SS_MOUSE },
{ "Stunner", RETRO_DEVICE_SS_GUN_US },
{ "Twin-Stick", RETRO_DEVICE_SS_TWINSTICK },
{ "Virtua Gun", RETRO_DEVICE_SS_GUN_JP },
{ "Dual Mission Sticks", RETRO_DEVICE_SS_MISSION2 }, /*"Panzer Dragoon Zwei" only!*/
{ 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;
//------------------------------------------------------------------------------
// 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;
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, analog_y;
analog_x = input_state_cb( player_index, RETRO_DEVICE_ANALOG, stick, RETRO_DEVICE_ID_ANALOG_X );
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 )
{
const float scale = ((float)TRIGGER_MAX/(float)(TRIGGER_MAX - trigger_deadzone));
if ( input > trigger_deadzone )
{
// Re-scale analog range
float scaled = (input - trigger_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 )
{
uint16_t trigger;
// 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
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.
trigger = input_state_cb( player_index,
RETRO_DEVICE_JOYPAD,
0,
id ) ? 0xFFFF : 0;
}
else
{
// 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
trigger = apply_trigger_deadzone( (unsigned)trigger );
}
return trigger;
}
//------------------------------------------------------------------------------
// Global Functions
//------------------------------------------------------------------------------
void input_init_env( retro_environment_t _environ_cb )
{
// Cache this
environ_cb = _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" }, \
{ _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" }
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
/* 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()
{
// Initialise to default pad type and bind input buffers to SMPC emulation.
for ( unsigned i = 0; i < MAX_CONTROLLERS; ++i )
{
input_type[ i ] = RETRO_DEVICE_JOYPAD;
input_mode[ i ] = INPUT_MODE_DEFAULT;
input_throttle_latch[ i ] = 0;
SMPC_SetInput( i, "gamepad", (uint8*)&input_data[ i ] );
}
}
void input_set_geometry( unsigned width, unsigned height )
{
log_cb( RETRO_LOG_INFO, "input_set_geometry: %dx%d\n", width, 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;
}
}
void input_update(bool supports_bitmasks,
retro_input_state_t input_state_cb )
{
// For each player (logical controller)
for ( unsigned iplayer = 0; iplayer < players; ++iplayer )
{
INPUT_DATA* p_input = &(input_data[ iplayer ]);
int16_t ret;
// reset input
p_input->buttons = 0;
if (supports_bitmasks)
ret = input_state_cb( iplayer, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_MASK );
// What kind of controller is connected?
switch ( input_type[ iplayer ] )
{
case RETRO_DEVICE_JOYPAD:
case RETRO_DEVICE_SS_PAD:
//
// -- standard control pad buttons + d-pad
// input_map_pad is configured to quickly map libretro buttons to the correct bits for the Saturn.
if (supports_bitmasks)
{
for ( int 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 );
}
else
{
for ( int i = 0; i < INPUT_MAP_PAD_SIZE; ++i )
p_input->buttons |= input_state_cb( iplayer, RETRO_DEVICE_JOYPAD, 0, input_map_pad[ i ] ) ? ( 1 << i ) : 0;
// .. the left trigger on the Saturn is a special case since there's a gap in the bits.
p_input->buttons |=
input_state_cb( iplayer, RETRO_DEVICE_JOYPAD, 0, input_map_pad_left_shoulder ) ? ( 1 << 15 ) : 0;
}
if (opposite_directions == false)
{
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;
get_analog_stick( input_state_cb, iplayer, RETRO_DEVICE_INDEX_ANALOG_LEFT, &analog_lx, &analog_ly );
int analog_rx, analog_ry;
get_analog_stick( input_state_cb, iplayer, RETRO_DEVICE_INDEX_ANALOG_RIGHT, &analog_rx, &analog_ry );
const int thresh = 16000;
// 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)
if (supports_bitmasks)
{
// 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 );
}
else
{
// left trigger
p_input->buttons |=
input_state_cb( iplayer, RETRO_DEVICE_JOYPAD, 0, input_map_twinstick_left_trigger ) ? ( 1 << 15 ) : 0;
// left button
p_input->buttons |=
input_state_cb( iplayer, RETRO_DEVICE_JOYPAD, 0, input_map_twinstick_left_button ) ? ( 1 << 3 ) : 0;
// right trigger
p_input->buttons |=
input_state_cb( iplayer, RETRO_DEVICE_JOYPAD, 0, input_map_twinstick_right_trigger ) ? ( 1 << 10 ) : 0;
// right button
p_input->buttons |=
input_state_cb( iplayer, RETRO_DEVICE_JOYPAD, 0, input_map_twinstick_right_button ) ? ( 1 << 9 ) : 0;
// start
p_input->buttons |=
input_state_cb( iplayer, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START ) ? ( 1 << 11 ) : 0;
}
}
break;
case RETRO_DEVICE_SS_3D_PAD:
{
//
// -- 3d control pad buttons
// input_map_3d_pad is configured to quickly map libretro buttons to the correct bits for the Saturn.
if (supports_bitmasks)
{
for ( int i = 0; i < INPUT_MAP_3D_PAD_SIZE; ++i )
if (ret & (1 << input_map_3d_pad[ i ]))
p_input->buttons |= ( 1 << i );
}
else
{
for ( int i = 0; i < INPUT_MAP_3D_PAD_SIZE; ++i )
p_input->buttons |= input_state_cb( iplayer, RETRO_DEVICE_JOYPAD, 0, input_map_3d_pad[ i ] ) ? ( 1 << i ) : 0;
}
//
// -- analog stick
int analog_x, analog_y;
get_analog_stick( input_state_cb, iplayer, RETRO_DEVICE_INDEX_ANALOG_LEFT, &analog_x, &analog_y );
// mednafen wants 0 - 32767 - 65535
uint16_t thumb_x, thumb_y;
thumb_x = static_cast< uint16_t >( analog_x + 32767 );
thumb_y = static_cast< uint16_t >( analog_y + 32767 );
//
// -- triggers
// mednafen wants 0 - 65535
uint16_t l_trigger, r_trigger;
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 (supports_bitmasks)
{
if (ret & (1 << input_map_3d_pad_mode_switch))
held = INPUT_MODE_3D_PAD_PREVIOUS_MASK;
}
else
held = input_state_cb( iplayer, RETRO_DEVICE_JOYPAD, 0, input_map_3d_pad_mode_switch )
? INPUT_MODE_3D_PAD_PREVIOUS_MASK : 0;
// Rising edge trigger
if ( !prev && held )
{
// Toggle 'state' bit: analog/digital mode
input_mode[ iplayer ] ^= INPUT_MODE_3D_PAD_ANALOG;
// Tell user
char text[ 256 ];
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) );
}
struct retro_message msg = { text, 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
// Apply analog/digital mode switch bit.
if ( input_mode[iplayer] & INPUT_MODE_3D_PAD_ANALOG )
p_input->buttons |= 0x1000; // set bit 12
p_input->u8[0x2] = ((thumb_x >> 0) & 0xff);
p_input->u8[0x3] = ((thumb_x >> 8) & 0xff);
p_input->u8[0x4] = ((thumb_y >> 0) & 0xff);
p_input->u8[0x5] = ((thumb_y >> 8) & 0xff);
p_input->u8[0x6] = ((r_trigger >> 0) & 0xff);
p_input->u8[0x7] = ((r_trigger >> 8) & 0xff);
p_input->u8[0x8] = ((l_trigger >> 0) & 0xff);
p_input->u8[0x9] = ((l_trigger >> 8) & 0xff);
}
break;
case RETRO_DEVICE_SS_WHEEL:
{
//
// -- Wheel buttons
// input_map_wheel is configured to quickly map libretro buttons to the correct bits for the Saturn.
if (supports_bitmasks)
{
for ( int i = 0; i < INPUT_MAP_WHEEL_SIZE; ++i )
{
const uint16_t bit = ( 1 << ( i + INPUT_MAP_WHEEL_BITSHIFT ) );
if (ret & (1 << input_map_wheel[ i ]))
p_input->buttons |= bit;
}
// shift-paddles
if (ret & (1 << input_map_wheel_shift_left))
p_input->buttons |= ( 1 << 0 );
if (ret & (1 << input_map_wheel_shift_right))
p_input->buttons |= ( 1 << 1 );
}
else
{
for ( int i = 0; i < INPUT_MAP_WHEEL_SIZE; ++i )
{
const uint16_t bit = ( 1 << ( i + INPUT_MAP_WHEEL_BITSHIFT ) );
p_input->buttons |= input_state_cb( iplayer, RETRO_DEVICE_JOYPAD, 0, input_map_wheel[ i ] ) ? bit : 0;
}
// shift-paddles
p_input->buttons |= input_state_cb( iplayer, RETRO_DEVICE_JOYPAD, 0, input_map_wheel_shift_left ) ? ( 1 << 0 ) : 0;
p_input->buttons |= input_state_cb( iplayer, RETRO_DEVICE_JOYPAD, 0, input_map_wheel_shift_right ) ? ( 1 << 1 ) : 0;
}
//
// -- analog wheel
int analog_x;
get_analog_axis( input_state_cb, iplayer,
RETRO_DEVICE_INDEX_ANALOG_LEFT,
RETRO_DEVICE_ID_ANALOG_X, &analog_x );
//
// -- format input data
// Convert analog values into direction values.
uint16_t right = analog_x > 0 ? analog_x : 0;
uint16_t left = analog_x < 0 ? -analog_x : 0;
p_input->u8[0x2] = ((left >> 0) & 0xff);
p_input->u8[0x3] = ((left >> 8) & 0xff);
p_input->u8[0x4] = ((right >> 0) & 0xff);
p_input->u8[0x5] = ((right >> 8) & 0xff);
}
break;
case RETRO_DEVICE_SS_MOUSE:
{
// mouse buttons
p_input->u8[0x4] = 0;
if ( input_state_cb( iplayer, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_LEFT ) )
p_input->u8[0x4] |= ( 1 << 0 ); // A
if ( input_state_cb( iplayer, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_RIGHT ) )
p_input->u8[0x4] |= ( 1 << 1 ); // B
if ( input_state_cb( iplayer, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_MIDDLE ) )
p_input->u8[0x4] |= ( 1 << 2 ); // C
if ( input_state_cb( iplayer, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START ) ||
input_state_cb( iplayer, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_BUTTON_4 ) ||
input_state_cb( iplayer, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_BUTTON_5 ) ) {
p_input->u8[0x4] |= ( 1 << 3 ); // Start
}
// mouse input
int dx_raw, dy_raw;
dx_raw = input_state_cb( iplayer, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_X );
dy_raw = input_state_cb( iplayer, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_Y );
int16_t *delta;
delta = (int16_t*)p_input;
delta[ 0 ] = (int16_t)roundf( dx_raw * mouse_sensitivity );
delta[ 1 ] = (int16_t)roundf( dy_raw * mouse_sensitivity );
}
break;
case RETRO_DEVICE_SS_MISSION:
{
//
// -- mission stick buttons
// input_map_mission is configured to quickly map libretro buttons to the correct bits for the Saturn.
if (supports_bitmasks)
{
for ( int i = 0; i < INPUT_MAP_MISSION_SIZE; ++i )
{
if (ret & (1 << input_map_mission[ i ]))
p_input->buttons |= ( 1 << i );
}
// .. the left trigger is a special case, there's a gap in the bits.
if (ret & (1 << input_map_mission_left_shoulder))
p_input->buttons |= ( 1 << 11 );
}
else
{
for ( int i = 0; i < INPUT_MAP_MISSION_SIZE; ++i )
p_input->buttons |= input_state_cb( iplayer, RETRO_DEVICE_JOYPAD, 0, input_map_mission[ i ] ) ? ( 1 << i ) : 0;
// .. the left trigger is a special case, there's a gap in the bits.
p_input->buttons |=
input_state_cb( iplayer, RETRO_DEVICE_JOYPAD, 0, input_map_mission_left_shoulder ) ? ( 1 << 11 ) : 0;
}
//
// -- analog stick
int analog_x, analog_y;
get_analog_stick( input_state_cb, iplayer, RETRO_DEVICE_INDEX_ANALOG_LEFT, &analog_x, &analog_y );
//
// -- throttle
int throttle_real;
get_analog_axis( input_state_cb, iplayer,
RETRO_DEVICE_INDEX_ANALOG_RIGHT,
RETRO_DEVICE_ID_ANALOG_Y, &throttle_real );
int16_t throttle;
if ( input_mode[iplayer] & INPUT_MODE_MISSION_THROTTLE_LATCH )
// Use latched value
throttle = input_throttle_latch[iplayer];
else
// Direct read
throttle = throttle_real;
//
// -- throttle latch switch
{
// Handle MODE button as a switch
uint16_t prev = ( input_mode[iplayer] & INPUT_MODE_MISSION_THROTTLE_PREV );
uint16_t held = 0;
if (supports_bitmasks)
{
if (ret & (1 << input_map_mission_throttle_latch))
held = INPUT_MODE_MISSION_THROTTLE_PREV;
}
else
{
held = input_state_cb( iplayer, RETRO_DEVICE_JOYPAD, 0, input_map_mission_throttle_latch )
? INPUT_MODE_MISSION_THROTTLE_PREV : 0;
}
// Rising edge trigger?
if ( !prev && held )
{
// Toggle 'state' bit: throttle latch enable/disable.
input_mode[ iplayer ] ^= INPUT_MODE_MISSION_THROTTLE_LATCH;
// Tell user
if ( input_mode[iplayer] & INPUT_MODE_MISSION_THROTTLE_LATCH ) {
log_cb( RETRO_LOG_INFO, "Controller %u: Latched Throttle at %d\n", (iplayer+1), throttle_real );
input_throttle_latch[iplayer] = (int16_t)throttle_real;
} else {
log_cb( RETRO_LOG_INFO, "Controller %u: Remove Throttle Latch\n", (iplayer+1) );
}
}
// Store held state in 'previous' bit.
input_mode[ iplayer ] = ( input_mode[ iplayer ] & ~INPUT_MODE_MISSION_THROTTLE_PREV ) | held;
}
//
// -- format input data
// Convert analog values into direction values.
uint16_t right = analog_x > 0 ? analog_x : 0;
uint16_t left = analog_x < 0 ? -analog_x : 0;
uint16_t down = analog_y > 0 ? analog_y : 0;
uint16_t up = analog_y < 0 ? -analog_y : 0;
uint16_t th_up = throttle > 0 ? throttle : 0;
uint16_t th_dn = throttle < 0 ? -throttle : 0;
p_input->u8[0x2] = 0; // todo: auto-fire controls.
p_input->u8[0x3] = ((left >> 0) & 0xff);
p_input->u8[0x4] = ((left >> 8) & 0xff);
p_input->u8[0x5] = ((right >> 0) & 0xff);
p_input->u8[0x6] = ((right >> 8) & 0xff);
p_input->u8[0x7] = ((up >> 0) & 0xff);
p_input->u8[0x8] = ((up >> 8) & 0xff);
p_input->u8[0x9] = ((down >> 0) & 0xff);
p_input->u8[0xa] = ((down >> 8) & 0xff);
p_input->u8[0xb] = ((th_up >> 0) & 0xff);
p_input->u8[0xc] = ((th_up >> 8) & 0xff);
p_input->u8[0xd] = ((th_dn >> 0) & 0xff);
p_input->u8[0xe] = ((th_dn >> 8) & 0xff);
}
break;
case RETRO_DEVICE_SS_MISSION2:
{
//
// -- mission stick buttons
// input_map_mission is configured to quickly map libretro buttons to the correct bits for the Saturn.
if (supports_bitmasks)
{
for ( int i = 0; i < INPUT_MAP_MISSION_SIZE; ++i )
{
if (ret & (1 << input_map_mission[ i ]))
p_input->buttons |= ( 1 << i );
}
// .. the left trigger is a special case, there's a gap in the bits.
if (ret & (1 << input_map_mission_left_shoulder))
p_input->buttons |= ( 1 << 11 );
}
else
{
for ( int i = 0; i < INPUT_MAP_MISSION_SIZE; ++i )
p_input->buttons |= input_state_cb( iplayer, RETRO_DEVICE_JOYPAD, 0, input_map_mission[ i ] ) ? ( 1 << i ) : 0;