-
Notifications
You must be signed in to change notification settings - Fork 109
/
mame2003.c
1725 lines (1429 loc) · 65.3 KB
/
mame2003.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
/*********************************************************************
mame2003.c
an updated port of Xmame 0.78 to the libretro API
*********************************************************************/
#include <stdint.h>
#include <string/stdstring.h>
#include <libretro.h>
#include <file/file_path.h>
#include <streams/file_stream.h>
#include <math.h>
#if (HAS_DRZ80 || HAS_CYCLONE)
#include "frontend_list.h"
#endif
#include "mame.h"
#include "driver.h"
#include "state.h"
#include "log.h"
#include "input.h"
#include "inptport.h"
#include "fileio.h"
#include "controls.h"
#include "usrintrf.h"
const struct GameDriver *game_driver;
int retro_running = 0;
int gotFrame;
static float delta_samples;
int samples_per_frame = 0;
int orig_samples_per_frame = 0;
short* samples_buffer;
short* conversion_buffer;
int usestereo = 1;
/* MAME data structures to store and translate keyboard state */
const struct KeyboardInfo retroKeys[];
retro_log_printf_t log_cb;
static struct retro_message frontend_message;
struct retro_perf_callback perf_cb;
retro_environment_t environ_cb = NULL;
retro_video_refresh_t video_cb = NULL;
static retro_input_poll_t poll_cb = NULL;
static retro_input_state_t input_cb = NULL;
static retro_audio_sample_batch_t audio_batch_cb = NULL;
retro_set_led_state_t led_state_cb = NULL;
struct retro_audio_buffer_status_callback buf_status_cb;
#ifdef _MSC_VER
#if _MSC_VER < 1800
double round(double number)
{
return (number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5);
}
#endif
#endif
/******************************************************************************
private function prototypes
******************************************************************************/
static void check_system_specs(void);
void retro_describe_controls(void);
unsigned get_device_parent(unsigned device_id);
int get_retro_code(const char* type, unsigned osd_code);
unsigned get_ctrl_ipt_code(unsigned player_number, unsigned standard_code);
unsigned encode_osd_joycode(unsigned player_number, unsigned osd_code);
unsigned decode_osd_joycode(unsigned joycode);
unsigned calc_player_number(unsigned joycode);
int rescale_analog(int libretro_coordinate);
static void remove_slash (char* temp);
static void configure_cyclone_mode (int driverIndex);
/******************************************************************************
Data structures for libretro controllers
*******************************************************************************/
/* the first of our controllers can use the base retropad type and rename it,
* while any layout variations must subclass the type.
*/
#define PAD_CLASSIC RETRO_DEVICE_JOYPAD
#define PAD_FIGHTSTICK RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 0)
#define PAD_8BUTTON RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 1)
#define PAD_6BUTTON RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 2)
const struct retro_controller_description controllers[] = {
{ "RetroPad", PAD_CLASSIC },
{ "Fightstick", PAD_FIGHTSTICK },
{ "8-Button", PAD_8BUTTON },
{ "6-Button", PAD_6BUTTON },
};
/******************************************************************************
frontend message interface
******************************************************************************/
void frontend_message_cb(const char *message_string, unsigned frames_to_display)
{
frontend_message.msg = message_string;
frontend_message.frames = frames_to_display;
environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, &frontend_message);
}
/******************************************************************************
implementation of key libretro functions
******************************************************************************/
bool retro_audio_buff_active = false;
unsigned retro_audio_buff_occupancy = 0;
bool retro_audio_buff_underrun = false;
static void retro_audio_buff_status_cb(bool active, unsigned occupancy, bool underrun_likely)
{
retro_audio_buff_active = active;
retro_audio_buff_occupancy = occupancy;
retro_audio_buff_underrun = underrun_likely;
}
void retro_set_audio_buff_status_cb(void)
{
if (options.frameskip > 0 && options.frameskip >= 12)
{
buf_status_cb.callback = &retro_audio_buff_status_cb;
if (!environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK,
&buf_status_cb))
{
if (log_cb)
log_cb(RETRO_LOG_WARN, "Frameskip disabled - frontend does not support audio buffer status monitoring.\n");
retro_audio_buff_active = false;
retro_audio_buff_occupancy = 0;
retro_audio_buff_underrun = false;
}
else
log_cb(RETRO_LOG_INFO, "Frameskip Enabled\n");
}
else
environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK,NULL);
}
unsigned retro_api_version(void)
{
return RETRO_API_VERSION;
}
void retro_get_system_info(struct retro_system_info *info)
{
/* this must match the 'corename' field in mame2003_plus_libretro.info
* in order for netplay to work. */
info->library_name = "MAME 2003-Plus";
#ifndef GIT_VERSION
#define GIT_VERSION ""
#endif
info->library_version = GIT_VERSION;
info->valid_extensions = "zip";
info->need_fullpath = true;
info->block_extract = true;
}
void retro_init (void)
{
struct retro_log_callback log;
if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log))
log_cb = log.log;
else
log_cb = NULL;
#ifdef LOG_PERFORMANCE
environ_cb(RETRO_ENVIRONMENT_GET_PERF_INTERFACE, &perf_cb);
#endif
check_system_specs();
}
static void check_system_specs(void)
{
#ifndef RETRO_PROFILE
#define RETRO_PROFILE 10
#endif
/* This value can be set in the Makefile, and then modified here before being passed,
* even on an individual game basis.
* However, as of June 2021, the libretro performance profile callback is not known
* to be implemented by any frontends including RetroArch. The RA developers do not
* have a suggested range of values. We use 10 by convention (copying other cores).
*/
unsigned level = (unsigned)RETRO_PROFILE;
environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
}
void retro_set_environment(retro_environment_t cb)
{
struct retro_vfs_interface_info vfs_iface_info;
environ_cb = cb;
/* Initialise VFS */
vfs_iface_info.required_interface_version = 1;
vfs_iface_info.iface = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VFS_INTERFACE, &vfs_iface_info))
filestream_vfs_init(&vfs_iface_info);
}
void retro_get_system_av_info(struct retro_system_av_info *info)
{
mame2003_video_get_geometry(&info->geometry);
info->timing.fps = Machine->drv->frames_per_second;
info->timing.sample_rate = options.samplerate ;
}
bool retro_load_game(const struct retro_game_info *game)
{
struct retro_controller_info input_subdevice_ports[MAX_PLAYER_COUNT + 1];
int driverIndex = 0;
int port_index;
char *driver_lookup = NULL;
if(string_is_empty(game->path))
{
log_cb(RETRO_LOG_ERROR, LOGPRE "Content path is not set. Exiting!\n");
return false;
}
log_cb(RETRO_LOG_INFO, LOGPRE "Full content path %s\n", game->path);
if(!path_is_valid(game->path))
{
log_cb(RETRO_LOG_ERROR, LOGPRE "Content path is not valid. Exiting!");
return false;
}
log_cb(RETRO_LOG_INFO, LOGPRE "Git Version %s\n",GIT_VERSION);
driver_lookup = strdup(path_basename(game->path));
path_remove_extension(driver_lookup);
log_cb(RETRO_LOG_INFO, LOGPRE "Content lookup name: %s\n", driver_lookup);
for (driverIndex = 0; driverIndex < total_drivers; driverIndex++)
{
const struct GameDriver *needle = drivers[driverIndex];
if ( strcasecmp(driver_lookup, needle->name) == 0 )
{
log_cb(RETRO_LOG_INFO, LOGPRE "Driver index counter: %d. Matched game driver: %s\n", driverIndex, needle->name);
game_driver = needle;
options.romset_filename_noext = driver_lookup;
break;
}
if(driverIndex == total_drivers -2) /* we could fix the total drives in drivers c but the it pointless its taken into account here */
{
log_cb(RETRO_LOG_ERROR, LOGPRE "Driver index counter: %d. Game driver not found for %s!\n", driverIndex, driver_lookup);
return false;
}
}
if(!init_game(driverIndex))
return false;
set_content_flags();
options.libretro_content_path = strdup(game->path);
path_basedir(options.libretro_content_path);
/* Get system directory from frontend */
options.libretro_system_path = NULL;
environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY,&options.libretro_system_path);
if (options.libretro_system_path == NULL || options.libretro_system_path[0] == '\0')
{
log_cb(RETRO_LOG_INFO, LOGPRE "libretro system path not set by frontend, using content path\n");
options.libretro_system_path = options.libretro_content_path;
}
/* Get save directory from frontend */
options.libretro_save_path = NULL;
environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY,&options.libretro_save_path);
if (options.libretro_save_path == NULL || options.libretro_save_path[0] == '\0')
{
log_cb(RETRO_LOG_INFO, LOGPRE "libretro save path not set by frontend, using content path\n");
options.libretro_save_path = options.libretro_content_path;
}
/* Remove trailing slashes for specified systems */
remove_slash(options.libretro_content_path);
remove_slash(options.libretro_system_path);
remove_slash(options.libretro_save_path);
log_cb(RETRO_LOG_INFO, LOGPRE "content path: %s\n", options.libretro_content_path);
log_cb(RETRO_LOG_INFO, LOGPRE " system path: %s\n", options.libretro_system_path);
log_cb(RETRO_LOG_INFO, LOGPRE " save path: %s\n", options.libretro_save_path);
init_core_options();
update_variables(true);
configure_cyclone_mode(driverIndex);
/* Not all drivers support the maximum number of players. Only send controller info
* to the frontend for the number of players supported then zero the final record.
*/
for(port_index = 0; port_index < options.content_flags[CONTENT_CTRL_COUNT]; port_index++)
{
input_subdevice_ports[port_index].types = controllers;
input_subdevice_ports[port_index].num_types = IDX_NUMBER_OF_INPUT_TYPES;
}
input_subdevice_ports[options.content_flags[CONTENT_CTRL_COUNT]].types = 0;
input_subdevice_ports[options.content_flags[CONTENT_CTRL_COUNT]].num_types = 0;
environ_cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)input_subdevice_ports);
if(!run_game(driverIndex))
return true;
return false;
}
void retro_reset (void)
{
machine_reset(); /* use internal core function */
}
/* get pointer axis vector from coord */
int16_t get_pointer_delta(int16_t coord, int16_t *prev_coord)
{
int16_t delta = 0;
if (*prev_coord == 0 || coord == 0)
{
*prev_coord = coord;
}
else
{
if (coord != *prev_coord)
{
delta = coord - *prev_coord;
*prev_coord = coord;
}
}
return delta;
}
void retro_run (void)
{
bool updated = false;
poll_cb(); /* execute input callback */
if (retro_running == 0) /* first time through the loop */
{
retro_running = 1;
log_cb(RETRO_LOG_DEBUG, LOGPRE "Entering retro_run() for the first time.\n");
}
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
update_variables(false);
if (options.cpu_clock_scale)
{
if (cpunum_get_clockscale(0) != options.cpu_clock_scale)
{
log_cb(RETRO_LOG_DEBUG, LOGPRE "changing cpu clock scale from %lf to %lf\n",cpunum_get_clockscale(0),options.cpu_clock_scale);
cpunum_set_clockscale(0, options.cpu_clock_scale);
}
}
mame_frame();
}
void retro_unload_game(void)
{
mame_done();
/* do we need to be freeing things here? */
free(options.romset_filename_noext);
}
void retro_deinit(void)
{
#ifdef LOG_PERFORMANCE
perf_cb.perf_log();
#endif
}
extern size_t state_get_dump_size(void);
size_t retro_serialize_size(void)
{
return state_get_dump_size();
}
bool retro_serialize(void *data, size_t size)
{
int cpunum;
if( retro_serialize_size() == size && size )
{
/* write the save state */
state_save_save_begin(data);
/* write tag 0 */
state_save_set_current_tag(0);
if(state_save_save_continue())
{
return false;
}
/* loop over CPUs */
for (cpunum = 0; cpunum < cpu_gettotalcpu(); cpunum++)
{
cpuintrf_push_context(cpunum);
/* make sure banking is set */
activecpu_reset_banking();
/* save the CPU data */
state_save_set_current_tag(cpunum + 1);
if(state_save_save_continue())
return false;
cpuintrf_pop_context();
}
/* finish and close */
state_save_save_finish();
return true;
}
return false;
}
bool retro_unserialize(const void * data, size_t size)
{
int cpunum;
/* if successful, load it */
if ( (retro_serialize_size() ) && ( data ) && ( size ) && ( !state_save_load_begin((void*)data, size) ) )
{
/* read tag 0 */
state_save_set_current_tag(0);
if(state_save_load_continue())
return false;
/* loop over CPUs */
for (cpunum = 0; cpunum < cpu_gettotalcpu(); cpunum++)
{
cpuintrf_push_context(cpunum);
/* make sure banking is set */
activecpu_reset_banking();
/* load the CPU data */
state_save_set_current_tag(cpunum + 1);
if(state_save_load_continue())
return false;
cpuintrf_pop_context();
}
/* finish and close */
state_save_load_finish();
return true;
}
return false;
}
/******************************************************************************
Sound
osd_start_audio_stream() is called at the start of the emulation to initialize
the output stream, then osd_update_audio_stream() is called every frame to
feed new data. osd_stop_audio_stream() is called when the emulation is stopped.
The sample rate is fixed at Machine->sample_rate. Samples are 16-bit, signed.
When the stream is stereo, left and right samples are alternated in the
stream.
osd_start_audio_stream() and osd_update_audio_stream() must return the number
of samples (or couples of samples, when using stereo) required for next frame.
This will be around Machine->sample_rate / Machine->drv->frames_per_second,
the code may adjust it by SMALL AMOUNTS to keep timing accurate and to
maintain audio and video in sync when using vsync. Note that sound emulation,
especially when DACs are involved, greatly depends on the number of samples
per frame to be roughly constant, so the returned value must always stay close
to the reference value of Machine->sample_rate / Machine->drv->frames_per_second.
Of course that value is not necessarily an integer so at least a +/- 1
adjustment is necessary to avoid drifting over time.
******************************************************************************/
int osd_start_audio_stream(int stereo)
{
Machine->sample_rate = options.samplerate;
delta_samples = 0.0f;
usestereo = stereo ? 1 : 0;
/* determine the number of samples per frame */
samples_per_frame = Machine->sample_rate / Machine->drv->frames_per_second;
orig_samples_per_frame = samples_per_frame;
if (Machine->sample_rate == 0) return 0;
samples_buffer = (short *) calloc(samples_per_frame+16, 2 + usestereo * 2);
if (!usestereo) conversion_buffer = (short *) calloc(samples_per_frame+16, 4);
return samples_per_frame;
}
int osd_update_audio_stream(INT16 *buffer)
{
int i,j;
if ( Machine->sample_rate !=0 && buffer )
{
memcpy(samples_buffer, buffer, samples_per_frame * (usestereo ? 4 : 2));
if (usestereo)
audio_batch_cb(samples_buffer, samples_per_frame);
else
{
for (i = 0, j = 0; i < samples_per_frame; i++)
{
conversion_buffer[j++] = samples_buffer[i];
conversion_buffer[j++] = samples_buffer[i];
}
audio_batch_cb(conversion_buffer,samples_per_frame);
}
/*process next frame */
if ( samples_per_frame != orig_samples_per_frame ) samples_per_frame = orig_samples_per_frame;
/* dont drop any sample frames some games like mk will drift with time */
delta_samples += (Machine->sample_rate / Machine->drv->frames_per_second) - orig_samples_per_frame;
if ( delta_samples >= 1.0f )
{
int integer_delta = (int)delta_samples;
if (integer_delta <= 16 )
{
log_cb(RETRO_LOG_DEBUG,"sound: Delta added value %d added to frame\n",integer_delta);
samples_per_frame += integer_delta;
}
else if(integer_delta >= 16) log_cb(RETRO_LOG_INFO, "sound: Delta not added to samples_per_frame too large integer_delta: %d\n", integer_delta);
else log_cb(RETRO_LOG_DEBUG,"sound(delta) no conditions met\n");
delta_samples -= integer_delta;
}
}
return samples_per_frame;
}
void osd_stop_audio_stream(void)
{
}
/******************************************************************************
Miscellaneous
******************************************************************************/
unsigned retro_get_region (void) {return RETRO_REGION_NTSC;}
void *retro_get_memory_data(unsigned type) {return 0;}
size_t retro_get_memory_size(unsigned type) {return 0;}
bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info){return false;}
void retro_cheat_reset(void){}
void retro_cheat_set(unsigned unused, bool unused1, const char* unused2){}
void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
void retro_set_audio_sample(retro_audio_sample_t cb) { }
void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
void retro_set_input_poll(retro_input_poll_t cb) { poll_cb = cb; }
void retro_set_input_state(retro_input_state_t cb) { input_cb = cb; }
/******************************************************************************
RetroPad mapping
******************************************************************************/
/* Assuming the standard RetroPad layout:
*
* [L2] [R2]
* [L] [R]
*
* [^] [X]
*
* [<] [>] [start] [selct] [Y] [A]
*
* [v] [B]
*
*
* or standard RetroPad fight stick layout:
*
* [start] [selct]
* [X] [R] [L]
* [^] [Y]
*
* [<] [>] [A] [R2] [L2]
* [B]
* [v]
*
*
*
* key: [Button code/Street Fighter II move]
*
*
*
* PAD_CLASSIC
* ========================
* Same as FB Alpha's "Classic" mapping.
* Not sensible for 6 button fighters, but may suit other games.
*
* [7/-] [8/-] |
* [5/MK] [6/HK] |
* | [4/WK] [6/HK] [5/MK]
* [^] [4/WK] | [3/HP]
* |
* [<] [>] [start] [selct] [3/HP] [2/MP] | [2/MP] [8/-] [7/-]
* | [1/LP]
* [v] [1/LP] |
* |
*
*
* PAD_FIGHTSTICK
* ========================
* Uses the fight stick & pad layout popularised by Street Figher IV.
* Needs an 8+ button controller by default.
*
* [8/-] [6/HK] |
* [7/-] [3/HP] |
* | [2/MP] [3/HP] [7/-]
* [^] [2/MP] | [1/LP]
* |
* [<] [>] [start] [selct] [1/LP] [5/MK] | [5/MK] [6/HK] [8/-]
* | [4/LK]
* [v] [4/LK] |
* |
*
*
* PAD_6BUTTON
* ========================
* Only needs a 6+ button controller by default, doesn't suit 8+ button fight sticks.
*
* [7/-] [8/-] |
* [3/HP] [6/HK] |
* | [2/MP] [6/HK] [3/HP]
* [^] [2/MP] | [1/LP]
* |
* [<] [>] [start] [selct] [1/LP] [5/MK] | [5/MK] [8/-] [7/-]
* | [4/LK]
* [v] [4/LK] |
* |
*
*/
/* libretro presents "Player 1", "Player 2", "Player 3", etc while internally using indexed data starting at 0, 1, 2 */
/* The core presents "Player 1", "Player 2," "Player 3", and indexes them via enum values like JOYCODE_1_BUTTON1, */
/* JOYCODE_2_BUTTON1, JOYCODE_3_BUTTON1 or with #define-d masks IPF_PLAYER1, IPF_PLAYER2, IPF_PLAYER3. */
/* */
/* We are by convention passing "display" value used for mapping to core enums and player # masks to these macros. */
/* (Display Index - 1) can be used for indexed data structures. */
void retro_set_controller_port_device(unsigned in_port, unsigned device)
{
static bool run_update = false;
if (in_port == (options.content_flags[CONTENT_CTRL_COUNT] - 1) ) run_update = true;
options.active_control_type[in_port] = device;
log_cb(RETRO_LOG_DEBUG, LOGPRE "Preparing to connect input in_port: %i device: %i\n", in_port, device);
/* During core init we update and describe the controls when we encounter the last controller port connected
* to prevent spamming the frontend. On subsequent calls, we update and describe the structures each time.
*/
if (run_update)
{
internal_code_update(); /* update MAME data structures for controls */
retro_describe_controls(); /* update libretro data structures for controls */
}
}
void retro_describe_controls(void)
{
int port_number = 0;
struct retro_input_descriptor desc[(MAX_PLAYER_COUNT * OSD_INPUT_CODES_PER_PLAYER) + 1]; /* + 1 for the final zeroed record. */
struct retro_input_descriptor *needle = &desc[0];
log_cb(RETRO_LOG_DEBUG, LOGPRE "Describing %i controllers supported by the content loaded\n", options.content_flags[CONTENT_CTRL_COUNT]);
for(port_number = 0; port_number < options.content_flags[CONTENT_CTRL_COUNT]; port_number++)
{
unsigned osd_code = 0;
unsigned device_code = options.active_control_type[port_number];
log_cb(RETRO_LOG_DEBUG, LOGPRE "Controller port: %i device type: %i parent type: %i\n", port_number, device_code, get_device_parent(device_code));
for(osd_code = OSD_JOYPAD_B; osd_code < OSD_INPUT_CODES_PER_PLAYER; osd_code++)
{
unsigned joycode = 0; /* the unique code (including across players) created by the libretro OSD */
unsigned standard_code = 0; /* standard code is the MAME term for the internal input code, associated with a controller */
unsigned ctrl_ipt_code = 0; /* input code connects an input port with standard input code */
unsigned retro_code = 0; /* #define code from the libretro.h input API scheme */
const char *control_name = 0;
joycode = encode_osd_joycode(port_number + 1, osd_code);
if(joycode == INT_MAX) continue;
standard_code = oscode_find(joycode, CODE_TYPE_JOYSTICK);
if(standard_code == CODE_NONE) continue;
ctrl_ipt_code = get_ctrl_ipt_code(port_number + 1, standard_code) & ~IPF_PLAYERMASK; /* discard the player mask, although later we may want to distinguish control names by player number */
if(ctrl_ipt_code == CODE_NONE) continue;
if(ctrl_ipt_code >= IPT_BUTTON1 && ctrl_ipt_code <= IPT_BUTTON10)
{
if( ctrl_ipt_code==IPT_BUTTON6 && options.content_flags[CONTENT_HAS_PEDAL] ) goto skip;
if( ctrl_ipt_code==IPT_BUTTON5 && options.content_flags[CONTENT_HAS_PEDAL2]) goto skip;
if((ctrl_ipt_code - IPT_BUTTON1 + 1) > options.content_flags[CONTENT_BUTTON_COUNT])
continue; /* button has a higher index than supported by the driver */
skip:; //bypass button count check
}
/* try to get the corresponding ID for this control in libretro.h */
/* from the retropad section, or INT_MAX if not valid */
retro_code = get_retro_code("retropad", osd_code);
if(retro_code != INT_MAX)
{
/* First try to get specific name */
control_name = game_driver->ctrl_dat->get_name(ctrl_ipt_code);
/* override control name for pedals */
if( ctrl_ipt_code==IPT_BUTTON6 && options.content_flags[CONTENT_HAS_PEDAL] ) control_name = "Pedal";
if( ctrl_ipt_code==IPT_BUTTON5 && options.content_flags[CONTENT_HAS_PEDAL2]) control_name = "Pedal2";
if(string_is_empty(control_name))
{
switch(retro_code) /* universal default mappings */
{
case RETRO_DEVICE_ID_JOYPAD_LEFT: control_name = "Left"; break;
case RETRO_DEVICE_ID_JOYPAD_RIGHT: control_name = "Right"; break;
case RETRO_DEVICE_ID_JOYPAD_UP: control_name = "Up"; break;
case RETRO_DEVICE_ID_JOYPAD_DOWN: control_name = "Down"; break;
case RETRO_DEVICE_ID_JOYPAD_SELECT: control_name = "Coin"; break;
case RETRO_DEVICE_ID_JOYPAD_START: control_name = "Start"; break;
}
}
}
if(string_is_empty(control_name)) continue;
/* With regard to the device number, we refer to the input polling comments in
* libretro.h which says we "should only poll input based on the base input device
* types". That seems to be true in here too, because using the result of
* RETRO_DEVICE_SUBCLASS does not work in RetroArch in April 2021 when passed as part
* of the descriptions. Therefore, get_device_parent() is used.
*/
needle->port = port_number;
needle->device = get_device_parent(device_code);
needle->index = 0;
needle->id = retro_code;
needle->description = control_name;
log_cb(RETRO_LOG_DEBUG, LOGPRE "joycode: %i | standard code: %i | id: %2i | desc: %s\n", joycode, standard_code, needle->id, needle->description);
needle++;
}
}
/* the extra final record remains zeroed to indicate the end of the description to the frontend */
/* which specifically looks at the description field to be NULL per libretro.h */
needle->port = 0;
needle->device = 0;
needle->index = 0;
needle->id = 0;
needle->description = NULL;
#if 0
needle = &desc[0];
log_cb(RETRO_LOG_DEBUG, LOGPRE "Beginning of description list.\n");
while(needle->description != NULL)
{
log_cb(RETRO_LOG_DEBUG, LOGPRE "Description || port: %i | device: %i | index: %i | id: %i \t| name: %s\n", needle->port, needle->device, needle->index, needle->id, needle->description);
needle++;
}
log_cb(RETRO_LOG_DEBUG, LOGPRE "End of description list.\n");
#endif
environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
}
/* get_device_parent
* returns the "base" libretro device type, which we may sometimes need
*/
unsigned get_device_parent(unsigned device_id)
{
if(device_id == RETRO_DEVICE_NONE) return RETRO_DEVICE_NONE;
switch(device_id)
{
case PAD_CLASSIC:
case PAD_FIGHTSTICK:
case PAD_8BUTTON:
case PAD_6BUTTON:
return RETRO_DEVICE_JOYPAD;
}
return INT_MAX;
}
/* get_retro_code
* converts from OSD_ in mame2003.h to the codes from libretro.h
* returns INT_MAX if the code is not valid
*/
int get_retro_code(const char* type, unsigned osd_code)
{
if(strcmp(type, "retropad") == 0)
{
switch(osd_code)
{
case OSD_JOYPAD_B: return RETRO_DEVICE_ID_JOYPAD_B;
case OSD_JOYPAD_Y: return RETRO_DEVICE_ID_JOYPAD_Y;
case OSD_JOYPAD_SELECT: return RETRO_DEVICE_ID_JOYPAD_SELECT;
case OSD_JOYPAD_START: return RETRO_DEVICE_ID_JOYPAD_START;
case OSD_JOYPAD_UP: return RETRO_DEVICE_ID_JOYPAD_UP;
case OSD_JOYPAD_DOWN: return RETRO_DEVICE_ID_JOYPAD_DOWN;
case OSD_JOYPAD_LEFT: return RETRO_DEVICE_ID_JOYPAD_LEFT;
case OSD_JOYPAD_RIGHT: return RETRO_DEVICE_ID_JOYPAD_RIGHT;
case OSD_JOYPAD_A: return RETRO_DEVICE_ID_JOYPAD_A;
case OSD_JOYPAD_X: return RETRO_DEVICE_ID_JOYPAD_X;
case OSD_JOYPAD_L: return RETRO_DEVICE_ID_JOYPAD_L;
case OSD_JOYPAD_R: return RETRO_DEVICE_ID_JOYPAD_R;
case OSD_JOYPAD_L2: return RETRO_DEVICE_ID_JOYPAD_L2;
case OSD_JOYPAD_R2: return RETRO_DEVICE_ID_JOYPAD_R2;
case OSD_JOYPAD_L3: return RETRO_DEVICE_ID_JOYPAD_L3;
case OSD_JOYPAD_R3: return RETRO_DEVICE_ID_JOYPAD_R3;
}
}
else if(strcmp(type, "mouse") == 0)
{
switch(osd_code)
{
case OSD_MOUSE_BUTTON_1: return RETRO_DEVICE_ID_MOUSE_LEFT;
case OSD_MOUSE_BUTTON_2: return RETRO_DEVICE_ID_MOUSE_RIGHT;
case OSD_MOUSE_BUTTON_3: return RETRO_DEVICE_ID_MOUSE_MIDDLE;
case OSD_MOUSE_BUTTON_4: return RETRO_DEVICE_ID_MOUSE_BUTTON_4;
case OSD_MOUSE_BUTTON_5: return RETRO_DEVICE_ID_MOUSE_BUTTON_5;
}
}
else if(strcmp(type, "lightgun") == 0)
{
switch(osd_code)
{
case OSD_LIGHTGUN_IS_OFFSCREEN: return RETRO_DEVICE_ID_LIGHTGUN_IS_OFFSCREEN;
case OSD_LIGHTGUN_IS_TRIGGER: return RETRO_DEVICE_ID_LIGHTGUN_TRIGGER; /*Status Check*/
case OSD_LIGHTGUN_RELOAD: return RETRO_DEVICE_ID_LIGHTGUN_RELOAD; /*Forced off-screen shot*/
case OSD_LIGHTGUN_AUX_A: return RETRO_DEVICE_ID_LIGHTGUN_AUX_A;
case OSD_LIGHTGUN_AUX_B: return RETRO_DEVICE_ID_LIGHTGUN_AUX_B;
case OSD_LIGHTGUN_START: return RETRO_DEVICE_ID_LIGHTGUN_START;
case OSD_LIGHTGUN_SELECT: return RETRO_DEVICE_ID_LIGHTGUN_SELECT;
case OSD_LIGHTGUN_AUX_C: return RETRO_DEVICE_ID_LIGHTGUN_AUX_C;
case OSD_LIGHTGUN_DPAD_UP: return RETRO_DEVICE_ID_LIGHTGUN_DPAD_UP;
case OSD_LIGHTGUN_DPAD_DOWN: return RETRO_DEVICE_ID_LIGHTGUN_DPAD_DOWN;
case OSD_LIGHTGUN_DPAD_LEFT: return RETRO_DEVICE_ID_LIGHTGUN_DPAD_LEFT;
case OSD_LIGHTGUN_DPAD_RIGHT: return RETRO_DEVICE_ID_LIGHTGUN_DPAD_RIGHT;
}
}
else
log_cb(RETRO_LOG_WARN, LOGPRE "get_retro_code() called with invalid type! %s\n", type);
return INT_MAX; /* no match found */
}
/* surely there is a MAME function equivalent already for PLAYER_CODE_COMPARE
* and get_ctrl_ipt_code(). but I haven't found it.
*/
#define PLAYER_CODE_COMPARE(PLAYER_NUMBER) \
switch(standard_code) \
{ \
case JOYCODE_##PLAYER_NUMBER##_SELECT: return IPT_COIN##PLAYER_NUMBER; \
case JOYCODE_##PLAYER_NUMBER##_START: return IPT_START##PLAYER_NUMBER; \
case JOYCODE_##PLAYER_NUMBER##_UP: return player_flag | IPT_JOYSTICK_UP; \
case JOYCODE_##PLAYER_NUMBER##_DOWN: return player_flag | IPT_JOYSTICK_DOWN; \
case JOYCODE_##PLAYER_NUMBER##_LEFT: return player_flag | IPT_JOYSTICK_LEFT; \
case JOYCODE_##PLAYER_NUMBER##_RIGHT: return player_flag | IPT_JOYSTICK_RIGHT; \
case JOYCODE_##PLAYER_NUMBER##_BUTTON1: return player_flag | IPT_BUTTON1; \
case JOYCODE_##PLAYER_NUMBER##_BUTTON2: return player_flag | IPT_BUTTON2; \
case JOYCODE_##PLAYER_NUMBER##_BUTTON3: return player_flag | IPT_BUTTON3; \
case JOYCODE_##PLAYER_NUMBER##_BUTTON4: return player_flag | IPT_BUTTON4; \
case JOYCODE_##PLAYER_NUMBER##_BUTTON5: return player_flag | IPT_BUTTON5; \
case JOYCODE_##PLAYER_NUMBER##_BUTTON6: return player_flag | IPT_BUTTON6; \
case JOYCODE_##PLAYER_NUMBER##_BUTTON7: return player_flag | IPT_BUTTON7; \
case JOYCODE_##PLAYER_NUMBER##_BUTTON8: return player_flag | IPT_BUTTON8; \
case JOYCODE_##PLAYER_NUMBER##_BUTTON9: return player_flag | IPT_BUTTON9; \
case JOYCODE_##PLAYER_NUMBER##_BUTTON10: return player_flag | IPT_BUTTON10; \
case JOYCODE_##PLAYER_NUMBER##_LEFT_UP: return player_flag | IPT_JOYSTICKLEFT_UP; \
case JOYCODE_##PLAYER_NUMBER##_LEFT_DOWN: return player_flag | IPT_JOYSTICKLEFT_DOWN; \
case JOYCODE_##PLAYER_NUMBER##_LEFT_LEFT: return player_flag | IPT_JOYSTICKLEFT_LEFT; \
case JOYCODE_##PLAYER_NUMBER##_LEFT_RIGHT: return player_flag | IPT_JOYSTICKLEFT_RIGHT; \
case JOYCODE_##PLAYER_NUMBER##_RIGHT_UP: return player_flag | IPT_JOYSTICKRIGHT_UP; \
case JOYCODE_##PLAYER_NUMBER##_RIGHT_DOWN: return player_flag | IPT_JOYSTICKRIGHT_DOWN; \
case JOYCODE_##PLAYER_NUMBER##_RIGHT_LEFT: return player_flag | IPT_JOYSTICKRIGHT_LEFT; \
case JOYCODE_##PLAYER_NUMBER##_RIGHT_RIGHT: return player_flag | IPT_JOYSTICKRIGHT_RIGHT; \
case JOYCODE_MOUSE_##PLAYER_NUMBER##_BUTTON1: return player_flag | IPT_BUTTON1; \
case JOYCODE_MOUSE_##PLAYER_NUMBER##_BUTTON2: return player_flag | IPT_BUTTON2; \
case JOYCODE_MOUSE_##PLAYER_NUMBER##_BUTTON3: return player_flag | IPT_BUTTON3; \
case JOYCODE_MOUSE_##PLAYER_NUMBER##_BUTTON4: return player_flag | IPT_BUTTON4; \
case JOYCODE_MOUSE_##PLAYER_NUMBER##_BUTTON5: return player_flag | IPT_BUTTON5; \
case JOYCODE_MOUSE_##PLAYER_NUMBER##_BUTTON6: return player_flag | IPT_BUTTON6; \
case JOYCODE_GUN_##PLAYER_NUMBER##_BUTTON1: return player_flag | IPT_BUTTON1; \
case JOYCODE_GUN_##PLAYER_NUMBER##_BUTTON2: return player_flag | IPT_BUTTON2; \
case JOYCODE_GUN_##PLAYER_NUMBER##_BUTTON3: return player_flag | IPT_BUTTON3; \
case JOYCODE_GUN_##PLAYER_NUMBER##_BUTTON4: return player_flag | IPT_BUTTON4; \
case JOYCODE_GUN_##PLAYER_NUMBER##_START: return IPT_START##PLAYER_NUMBER; \
case JOYCODE_GUN_##PLAYER_NUMBER##_SELECT: return IPT_COIN##PLAYER_NUMBER; \
case JOYCODE_GUN_##PLAYER_NUMBER##_DPAD_UP: return player_flag | IPT_JOYSTICK_UP; \
case JOYCODE_GUN_##PLAYER_NUMBER##_DPAD_DOWN: return player_flag | IPT_JOYSTICK_DOWN; \
case JOYCODE_GUN_##PLAYER_NUMBER##_DPAD_LEFT: return player_flag | IPT_JOYSTICK_LEFT; \
case JOYCODE_GUN_##PLAYER_NUMBER##_DPAD_RIGHT: return player_flag | IPT_JOYSTICK_RIGHT; \
} \
unsigned get_ctrl_ipt_code(unsigned player_number, unsigned standard_code)
{
int player_flag = 0;
switch(player_number)
{
case 1: player_flag = IPF_PLAYER1; PLAYER_CODE_COMPARE(1) break;
case 2: player_flag = IPF_PLAYER2; PLAYER_CODE_COMPARE(2) break;
case 3: player_flag = IPF_PLAYER3; PLAYER_CODE_COMPARE(3) break;
case 4: player_flag = IPF_PLAYER4; PLAYER_CODE_COMPARE(4) break;
case 5: player_flag = IPF_PLAYER5; PLAYER_CODE_COMPARE(5) break;
case 6: player_flag = IPF_PLAYER6; PLAYER_CODE_COMPARE(6) break;
case 7: player_flag = IPF_PLAYER7; PLAYER_CODE_COMPARE(7) break;
case 8: player_flag = IPF_PLAYER8; PLAYER_CODE_COMPARE(8) break;
default: player_flag = IPF_PLAYER1; PLAYER_CODE_COMPARE(1) break;
}
return CODE_NONE; /* no match was found */
}
/*
* Each line created by these emitters composes a JoystickInfo struct.
*
* struct JoystickInfo
* {
* const char *name; // OS dependant name; 0 terminates the list
* unsigned code; // OS dependant code
* InputCode standardcode; // CODE_xxx equivalent from list in input.h, or CODE_OTHER if n/a
* };
*
* In the context of MAME 2003+, the 'OS' is the libretro, so we determine the unique codes for
* our input. When the control mappings are emitted, the input codes are incremented by 1000 * the
* player number so that simple arithmetic can determine which is the associated player.
*/
#define EMIT_RETROPAD_CLASSIC(DISPLAY_IDX) \
{"RP" #DISPLAY_IDX " B", (DISPLAY_IDX * 1000) + OSD_JOYPAD_B, JOYCODE_##DISPLAY_IDX##_BUTTON1}, \
{"RP" #DISPLAY_IDX " A", (DISPLAY_IDX * 1000) + OSD_JOYPAD_A, JOYCODE_##DISPLAY_IDX##_BUTTON2}, \
{"RP" #DISPLAY_IDX " Y", (DISPLAY_IDX * 1000) + OSD_JOYPAD_Y, JOYCODE_##DISPLAY_IDX##_BUTTON3}, \
{"RP" #DISPLAY_IDX " X", (DISPLAY_IDX * 1000) + OSD_JOYPAD_X, JOYCODE_##DISPLAY_IDX##_BUTTON4}, \
{"RP" #DISPLAY_IDX " L", (DISPLAY_IDX * 1000) + OSD_JOYPAD_L, JOYCODE_##DISPLAY_IDX##_BUTTON5}, \
{"RP" #DISPLAY_IDX " R", (DISPLAY_IDX * 1000) + OSD_JOYPAD_R, JOYCODE_##DISPLAY_IDX##_BUTTON6}, \
{"RP" #DISPLAY_IDX " L2", (DISPLAY_IDX * 1000) + OSD_JOYPAD_L2, JOYCODE_##DISPLAY_IDX##_BUTTON7}, \