forked from sparklewind/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retroarch.c
2906 lines (2439 loc) · 83.8 KB
/
retroarch.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
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "boolean.h"
#include "libretro.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "driver.h"
#include "file.h"
#include "general.h"
#include "dynamic.h"
#include "performance.h"
#include "audio/utils.h"
#include "record/ffemu.h"
#include "rewind.h"
#include "movie.h"
#include "compat/strl.h"
#include "screenshot.h"
#include "cheats.h"
#include "compat/getopt_rarch.h"
#if defined(_WIN32) && !defined(_XBOX)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#elif defined(_XBOX)
#include <xtl.h>
#endif
#ifdef _WIN32
#include "msvc/msvc_compat.h"
#endif
#ifdef __APPLE__
#include "SDL.h"
// OSX seems to really need -lSDLmain,
// so we include SDL.h here so it can hack our main.
// We want to use -mconsole in Win32, so we need main().
#endif
#if defined(RARCH_CONSOLE) && !defined(RARCH_PERFORMANCE_MODE)
#define RARCH_PERFORMANCE_MODE
#endif
// To avoid continous switching if we hold the button down, we require that the button must go from pressed, unpressed back to pressed to be able to toggle between then.
static void check_fast_forward_button(void)
{
bool new_button_state = input_key_pressed_func(RARCH_FAST_FORWARD_KEY);
bool new_hold_button_state = input_key_pressed_func(RARCH_FAST_FORWARD_HOLD_KEY);
bool update_sync = false;
static bool old_button_state = false;
static bool old_hold_button_state = false;
static bool syncing_state = false;
if (new_button_state && !old_button_state)
{
syncing_state = !syncing_state;
update_sync = true;
}
else if (old_hold_button_state != new_hold_button_state)
{
syncing_state = new_hold_button_state;
update_sync = true;
}
if (update_sync)
{
// Only apply non-block-state for video if we're using vsync.
if (g_extern.video_active && g_settings.video.vsync && !g_extern.system.force_nonblock)
video_set_nonblock_state_func(syncing_state);
if (g_extern.audio_active)
audio_set_nonblock_state_func(g_settings.audio.sync ? syncing_state : true);
g_extern.audio_data.chunk_size =
syncing_state ? g_extern.audio_data.nonblock_chunk_size : g_extern.audio_data.block_chunk_size;
}
old_button_state = new_button_state;
old_hold_button_state = new_hold_button_state;
}
#if defined(HAVE_SCREENSHOTS) && !defined(_XBOX)
static bool take_screenshot_viewport(void)
{
struct rarch_viewport vp = {0};
video_viewport_info_func(&vp);
if (!vp.width || !vp.height)
return false;
uint8_t *buffer = (uint8_t*)malloc(vp.width * vp.height * 3);
if (!buffer)
return false;
if (!video_read_viewport_func(buffer))
{
free(buffer);
return false;
}
// Data read from viewport is in bottom-up order, suitable for BMP.
if (!screenshot_dump(g_settings.screenshot_directory,
buffer,
vp.width, vp.height, vp.width * 3, true))
{
free(buffer);
return false;
}
free(buffer);
return true;
}
static bool take_screenshot_raw(void)
{
const uint16_t *data = (const uint16_t*)g_extern.frame_cache.data;
unsigned width = g_extern.frame_cache.width;
unsigned height = g_extern.frame_cache.height;
int pitch = g_extern.frame_cache.pitch;
// Negative pitch is needed as screenshot takes bottom-up,
// but we use top-down.
return screenshot_dump(g_settings.screenshot_directory,
data + (height - 1) * (pitch >> 1),
width, height, -pitch, false);
}
static void take_screenshot(void)
{
if (!(*g_settings.screenshot_directory))
return;
bool ret = false;
if (g_settings.video.gpu_screenshot && driver.video->read_viewport && driver.video->viewport_info)
ret = take_screenshot_viewport();
else if (g_extern.frame_cache.data)
ret = take_screenshot_raw();
const char *msg = NULL;
if (ret)
{
RARCH_LOG("Taking screenshot.\n");
msg = "Taking screenshot.";
}
else
{
RARCH_WARN("Failed to take screenshot ...\n");
msg = "Failed to take screenshot.";
}
msg_queue_clear(g_extern.msg_queue);
if (g_extern.is_paused)
{
msg_queue_push(g_extern.msg_queue, msg, 1, 1);
rarch_render_cached_frame();
}
else
msg_queue_push(g_extern.msg_queue, msg, 1, 180);
}
#endif
static void readjust_audio_input_rate(void)
{
int avail = audio_write_avail_func();
//RARCH_LOG_OUTPUT("Audio buffer is %u%% full\n",
// (unsigned)(100 - (avail * 100) / g_extern.audio_data.driver_buffer_size));
int half_size = g_extern.audio_data.driver_buffer_size / 2;
int delta_mid = avail - half_size;
double direction = (double)delta_mid / half_size;
double adjust = 1.0 + g_settings.audio.rate_control_delta * direction;
g_extern.audio_data.src_ratio = g_extern.audio_data.orig_src_ratio * adjust;
//RARCH_LOG_OUTPUT("New rate: %lf, Orig rate: %lf\n",
// g_extern.audio_data.src_ratio, g_extern.audio_data.orig_src_ratio);
}
#ifdef HAVE_FFMPEG
static void deinit_recording(void);
static void recording_dump_frame(const void *data, unsigned width, unsigned height, size_t pitch)
{
struct ffemu_video_data ffemu_data = {0};
if (g_extern.record_gpu_buffer)
{
struct rarch_viewport vp = {0};
video_viewport_info_func(&vp);
if (!vp.width || !vp.height)
{
RARCH_WARN("Viewport size calculation failed! Will continue using raw data. This will probably not work right ...\n");
free(g_extern.record_gpu_buffer);
g_extern.record_gpu_buffer = NULL;
recording_dump_frame(data, width, height, pitch);
return;
}
// User has resized. We're kinda fucked now.
if (vp.width != g_extern.record_gpu_width || vp.height != g_extern.record_gpu_height)
{
static const char msg[] = "Recording terminated due to resize.";
RARCH_WARN("%s\n", msg);
msg_queue_clear(g_extern.msg_queue);
msg_queue_push(g_extern.msg_queue, msg, 1, 180);
deinit_recording();
g_extern.recording = false;
return;
}
// Big bottleneck.
// Since we might need to do read-backs asynchronously, it might take 3-4 times
// before this returns true ...
if (!video_read_viewport_func(g_extern.record_gpu_buffer))
return;
ffemu_data.pitch = g_extern.record_gpu_width * 3;
ffemu_data.width = g_extern.record_gpu_width;
ffemu_data.height = g_extern.record_gpu_height;
ffemu_data.data = g_extern.record_gpu_buffer + (ffemu_data.height - 1) * ffemu_data.pitch;
ffemu_data.pitch = -ffemu_data.pitch;
}
else
{
ffemu_data.data = data;
ffemu_data.pitch = pitch;
ffemu_data.width = width;
ffemu_data.height = height;
ffemu_data.is_dupe = !data;
}
ffemu_push_video(g_extern.rec, &ffemu_data);
}
#endif
static void video_frame(const void *data, unsigned width, unsigned height, size_t pitch)
{
if (!g_extern.video_active)
return;
if (g_extern.system.pix_fmt == RETRO_PIXEL_FORMAT_0RGB1555 && data)
{
RARCH_PERFORMANCE_INIT(video_frame_conv);
RARCH_PERFORMANCE_START(video_frame_conv);
driver.scaler.in_width = width;
driver.scaler.in_height = height;
driver.scaler.out_width = width;
driver.scaler.out_height = height;
driver.scaler.in_stride = pitch;
driver.scaler.out_stride = width * sizeof(uint16_t);
scaler_ctx_scale(&driver.scaler, driver.scaler_out, data);
data = driver.scaler_out;
pitch = driver.scaler.out_stride;
RARCH_PERFORMANCE_STOP(video_frame_conv);
}
// Slightly messy code,
// but we really need to do processing before blocking on VSync for best possible scheduling.
#ifdef HAVE_FFMPEG
if (g_extern.recording && (!g_extern.filter.active || !g_settings.video.post_filter_record || !data || g_extern.record_gpu_buffer))
recording_dump_frame(data, width, height, pitch);
#endif
const char *msg = msg_queue_pull(g_extern.msg_queue);
#ifdef HAVE_DYLIB
if (g_extern.filter.active && data)
{
struct scaler_ctx *scaler = &g_extern.filter.scaler;
scaler->in_width = scaler->out_width = width;
scaler->in_height = scaler->out_height = height;
scaler->in_stride = pitch;
scaler->out_stride = width * sizeof(uint16_t);
scaler_ctx_scale(scaler, g_extern.filter.scaler_out, data);
unsigned owidth = width;
unsigned oheight = height;
g_extern.filter.psize(&owidth, &oheight);
g_extern.filter.prender(g_extern.filter.colormap, g_extern.filter.buffer,
g_extern.filter.pitch, g_extern.filter.scaler_out, scaler->out_stride, width, height);
#ifdef HAVE_FFMPEG
if (g_extern.recording && g_settings.video.post_filter_record)
recording_dump_frame(g_extern.filter.buffer, owidth, oheight, g_extern.filter.pitch);
#endif
if (!video_frame_func(g_extern.filter.buffer, owidth, oheight, g_extern.filter.pitch, msg))
g_extern.video_active = false;
}
else if (!video_frame_func(data, width, height, pitch, msg))
g_extern.video_active = false;
#else
if (!video_frame_func(data, width, height, pitch, msg))
g_extern.video_active = false;
#endif
g_extern.frame_cache.data = data;
g_extern.frame_cache.width = width;
g_extern.frame_cache.height = height;
g_extern.frame_cache.pitch = pitch;
}
void rarch_render_cached_frame(void)
{
#ifdef HAVE_FFMPEG
// Cannot allow FFmpeg recording when pushing duped frames.
bool recording = g_extern.recording;
g_extern.recording = false;
#endif
// Not 100% safe, since the library might have
// freed the memory, but no known implementations do this :D
// It would be really stupid at any rate ...
video_frame(g_extern.frame_cache.data,
g_extern.frame_cache.width,
g_extern.frame_cache.height,
g_extern.frame_cache.pitch);
#ifdef HAVE_FFMPEG
g_extern.recording = recording;
#endif
}
static bool audio_flush(const int16_t *data, size_t samples)
{
#ifdef HAVE_FFMPEG
if (g_extern.recording)
{
struct ffemu_audio_data ffemu_data = {0};
ffemu_data.data = data;
ffemu_data.frames = samples / 2;
ffemu_push_audio(g_extern.rec, &ffemu_data);
}
#endif
if (g_extern.is_paused || g_extern.audio_data.mute)
return true;
if (!g_extern.audio_active)
return false;
const sample_t *output_data = NULL;
unsigned output_frames = 0;
struct resampler_data src_data = {0};
RARCH_PERFORMANCE_INIT(audio_convert_s16);
RARCH_PERFORMANCE_START(audio_convert_s16);
audio_convert_s16_to_float(g_extern.audio_data.data, data, samples,
g_extern.audio_data.volume_gain);
RARCH_PERFORMANCE_STOP(audio_convert_s16);
#if defined(HAVE_DYLIB)
rarch_dsp_output_t dsp_output = {0};
rarch_dsp_input_t dsp_input = {0};
dsp_input.samples = g_extern.audio_data.data;
dsp_input.frames = samples >> 1;
if (g_extern.audio_data.dsp_plugin)
g_extern.audio_data.dsp_plugin->process(g_extern.audio_data.dsp_handle, &dsp_output, &dsp_input);
src_data.data_in = dsp_output.samples ? dsp_output.samples : g_extern.audio_data.data;
src_data.input_frames = dsp_output.samples ? dsp_output.frames : (samples >> 1);
#else
src_data.data_in = g_extern.audio_data.data;
src_data.input_frames = samples >> 1;
#endif
src_data.data_out = g_extern.audio_data.outsamples;
if (g_extern.audio_data.rate_control)
readjust_audio_input_rate();
src_data.ratio = g_extern.audio_data.src_ratio;
if (g_extern.is_slowmotion)
src_data.ratio *= g_settings.slowmotion_ratio;
RARCH_PERFORMANCE_INIT(resampler_proc);
RARCH_PERFORMANCE_START(resampler_proc);
resampler_process(g_extern.audio_data.source, &src_data);
RARCH_PERFORMANCE_STOP(resampler_proc);
output_data = g_extern.audio_data.outsamples;
output_frames = src_data.output_frames;
if (g_extern.audio_data.use_float)
{
if (audio_write_func(output_data, output_frames * sizeof(float) * 2) < 0)
{
RARCH_ERR("Audio backend failed to write. Will continue without sound.\n");
return false;
}
}
else
{
RARCH_PERFORMANCE_INIT(audio_convert_float);
RARCH_PERFORMANCE_START(audio_convert_float);
audio_convert_float_to_s16(g_extern.audio_data.conv_outsamples,
output_data, output_frames * 2);
RARCH_PERFORMANCE_STOP(audio_convert_float);
if (audio_write_func(g_extern.audio_data.conv_outsamples, output_frames * sizeof(int16_t) * 2) < 0)
{
RARCH_ERR("Audio backend failed to write. Will continue without sound.\n");
return false;
}
}
return true;
}
static void audio_sample_rewind(int16_t left, int16_t right)
{
g_extern.audio_data.rewind_buf[--g_extern.audio_data.rewind_ptr] = right;
g_extern.audio_data.rewind_buf[--g_extern.audio_data.rewind_ptr] = left;
}
size_t audio_sample_batch_rewind(const int16_t *data, size_t frames)
{
size_t samples = frames << 1;
for (size_t i = 0; i < samples; i++)
g_extern.audio_data.rewind_buf[--g_extern.audio_data.rewind_ptr] = data[i];
return frames;
}
static void audio_sample(int16_t left, int16_t right)
{
g_extern.audio_data.conv_outsamples[g_extern.audio_data.data_ptr++] = left;
g_extern.audio_data.conv_outsamples[g_extern.audio_data.data_ptr++] = right;
if (g_extern.audio_data.data_ptr < g_extern.audio_data.chunk_size)
return;
g_extern.audio_active = audio_flush(g_extern.audio_data.conv_outsamples,
g_extern.audio_data.data_ptr) && g_extern.audio_active;
g_extern.audio_data.data_ptr = 0;
}
size_t audio_sample_batch(const int16_t *data, size_t frames)
{
if (frames > (AUDIO_CHUNK_SIZE_NONBLOCKING >> 1))
frames = AUDIO_CHUNK_SIZE_NONBLOCKING >> 1;
g_extern.audio_active = audio_flush(data, frames << 1) && g_extern.audio_active;
return frames;
}
static void input_poll(void)
{
input_poll_func();
}
// Turbo scheme: If turbo button is held, all buttons pressed except for D-pad will go into
// a turbo mode. Until the button is released again, the input state will be modulated by a periodic pulse defined
// by the configured duty cycle.
static bool input_apply_turbo(unsigned port, unsigned id, bool res)
{
if (res && g_extern.turbo_frame_enable[port])
g_extern.turbo_enable[port] |= (1 << id);
else if (!res)
g_extern.turbo_enable[port] &= ~(1 << id);
if (g_extern.turbo_enable[port] & (1 << id))
return res & ((g_extern.turbo_count % g_settings.input.turbo_period) < g_settings.input.turbo_duty_cycle);
else
return res;
}
static int16_t input_state(unsigned port, unsigned device, unsigned index, unsigned id)
{
device &= RETRO_DEVICE_MASK;
#ifdef HAVE_BSV_MOVIE
if (g_extern.bsv.movie && g_extern.bsv.movie_playback)
{
int16_t ret;
if (bsv_movie_get_input(g_extern.bsv.movie, &ret))
return ret;
else
g_extern.bsv.movie_end = true;
}
#endif
static const struct retro_keybind *binds[MAX_PLAYERS] = {
g_settings.input.binds[0],
g_settings.input.binds[1],
g_settings.input.binds[2],
g_settings.input.binds[3],
g_settings.input.binds[4],
g_settings.input.binds[5],
g_settings.input.binds[6],
g_settings.input.binds[7],
};
int16_t res = 0;
if (id < RARCH_FIRST_META_KEY || device == RETRO_DEVICE_KEYBOARD)
res = input_input_state_func(binds, port, device, index, id);
// Don't allow turbo for D-pad.
if (device == RETRO_DEVICE_JOYPAD && (id < RETRO_DEVICE_ID_JOYPAD_UP || id > RETRO_DEVICE_ID_JOYPAD_RIGHT))
res = input_apply_turbo(port, id, res);
#ifdef HAVE_BSV_MOVIE
if (g_extern.bsv.movie && !g_extern.bsv.movie_playback)
bsv_movie_set_input(g_extern.bsv.movie, res);
#endif
return res;
}
#ifdef _WIN32
#define RARCH_DEFAULT_CONF_PATH_STR "\n\t\tDefaults to retroarch.cfg in same directory as retroarch.exe."
#elif defined(__APPLE__)
#define RARCH_DEFAULT_CONF_PATH_STR " Defaults to $HOME/.retroarch.cfg."
#else
#define RARCH_DEFAULT_CONF_PATH_STR " Defaults to $XDG_CONFIG_HOME/retroarch/retroarch.cfg,\n\t\tor $HOME/.retroarch.cfg, if $XDG_CONFIG_HOME is not defined."
#endif
#include "config.features.h"
#define _PSUPP(var, name, desc) printf("\t%s:\n\t\t%s: %s\n", name, desc, _##var##_supp ? "yes" : "no")
static void print_features(void)
{
puts("");
puts("Features:");
_PSUPP(sdl, "SDL", "SDL drivers");
_PSUPP(thread, "Threads", "Threading support");
_PSUPP(opengl, "OpenGL", "OpenGL driver");
_PSUPP(kms, "KMS", "KMS/EGL context support");
_PSUPP(egl, "EGL", "EGL context support");
_PSUPP(vg, "OpenVG", "OpenVG output support");
_PSUPP(xvideo, "XVideo", "XVideo output");
_PSUPP(alsa, "ALSA", "audio driver");
_PSUPP(oss, "OSS", "audio driver");
_PSUPP(jack, "Jack", "audio driver");
_PSUPP(rsound, "RSound", "audio driver");
_PSUPP(roar, "RoarAudio", "audio driver");
_PSUPP(pulse, "PulseAudio", "audio driver");
_PSUPP(dsound, "DirectSound", "audio driver");
_PSUPP(xaudio, "XAudio2", "audio driver");
_PSUPP(al, "OpenAL", "audio driver");
_PSUPP(dylib, "External", "External filter and driver support");
_PSUPP(cg, "Cg", "Cg pixel shaders");
_PSUPP(xml, "XML", "bSNES XML pixel shaders");
_PSUPP(sdl_image, "SDL_image", "SDL_image image loading");
_PSUPP(libpng, "libpng", "libpng screenshot support");
_PSUPP(fbo, "FBO", "OpenGL render-to-texture (multi-pass shaders)");
_PSUPP(dynamic, "Dynamic", "Dynamic run-time loading of libretro library");
_PSUPP(ffmpeg, "FFmpeg", "On-the-fly recording of gameplay with libavcodec");
_PSUPP(configfile, "Config file", "Configuration file support");
_PSUPP(freetype, "FreeType", "TTF font rendering with FreeType");
_PSUPP(netplay, "Netplay", "Peer-to-peer netplay");
_PSUPP(python, "Python", "Script support in shaders");
}
#undef _PSUPP
static void print_compiler(FILE *file)
{
fprintf(file, "\nCompiler: ");
#if defined(_MSC_VER)
fprintf(file, "MSVC (%d) %u-bit\n", _MSC_VER, (unsigned)(CHAR_BIT * sizeof(size_t)));
#elif defined(__SNC__)
fprintf(file, "SNC (%d) %u-bit\n",
__SN_VER__, (unsigned)(CHAR_BIT * sizeof(size_t)));
#elif defined(_WIN32) && defined(__GNUC__)
fprintf(file, "MinGW (%d.%d.%d) %u-bit\n",
__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__, (unsigned)(CHAR_BIT * sizeof(size_t)));
#elif defined(__clang__)
fprintf(file, "Clang/LLVM (%s) %u-bit\n",
__VERSION__, (unsigned)(CHAR_BIT * sizeof(size_t)));
#elif defined(__GNUC__)
fprintf(file, "GCC (%d.%d.%d) %u-bit\n",
__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__, (unsigned)(CHAR_BIT * sizeof(size_t)));
#else
fprintf(file, "Unknown compiler %u-bit\n",
(unsigned)(CHAR_BIT * sizeof(size_t)));
#endif
fprintf(file, "Built: %s\n", __DATE__);
}
static void print_help(void)
{
puts("===================================================================");
puts("RetroArch: Frontend for libretro -- v" PACKAGE_VERSION " --");
print_compiler(stdout);
puts("===================================================================");
puts("Usage: retroarch [rom file] [options...]");
puts("\t-h/--help: Show this help message.");
puts("\t--features: Prints available features compiled into RetroArch.");
puts("\t-s/--save: Path for save file (*.srm). Required when rom is input from stdin.");
puts("\t-f/--fullscreen: Start RetroArch in fullscreen regardless of config settings.");
puts("\t-S/--savestate: Path to use for save states. If not selected, *.state will be assumed.");
#ifdef HAVE_CONFIGFILE
puts("\t-c/--config: Path for config file." RARCH_DEFAULT_CONF_PATH_STR);
puts("\t--appendconfig: Extra config files are loaded in, and take priority over config selected in -c (or default).");
puts("\t\tMultiple configs are delimited by ','.");
#endif
#ifdef HAVE_DYNAMIC
puts("\t-L/--libretro: Path to libretro implementation. Overrides any config setting.");
#endif
puts("\t-g/--gameboy: Path to Gameboy ROM. Load SuperGameBoy as the regular rom.");
puts("\t-b/--bsx: Path to BSX rom. Load BSX BIOS as the regular rom.");
puts("\t-B/--bsxslot: Path to BSX slotted rom. Load BSX BIOS as the regular rom.");
puts("\t--sufamiA: Path to A slot of Sufami Turbo. Load Sufami base cart as regular rom.");
puts("\t--sufamiB: Path to B slot of Sufami Turbo.");
printf("\t-N/--nodevice: Disconnects controller device connected to port (1 to %d).\n", MAX_PLAYERS);
printf("\t-A/--dualanalog: Connect a DualAnalog controller to port (1 to %d).\n", MAX_PLAYERS);
printf("\t-m/--mouse: Connect a mouse into port of the device (1 to %d).\n", MAX_PLAYERS);
puts("\t-p/--scope: Connect a virtual SuperScope into port 2. (SNES specific).");
puts("\t-j/--justifier: Connect a virtual Konami Justifier into port 2. (SNES specific).");
puts("\t-J/--justifiers: Daisy chain two virtual Konami Justifiers into port 2. (SNES specific).");
puts("\t-4/--multitap: Connect a SNES multitap to port 2. (SNES specific).");
#ifdef HAVE_BSV_MOVIE
puts("\t-P/--bsvplay: Playback a BSV movie file.");
puts("\t-R/--bsvrecord: Start recording a BSV movie file from the beginning.");
puts("\t-M/--sram-mode: Takes an argument telling how SRAM should be handled in the session.");
#endif
puts("\t\t{no,}load-{no,}save describes if SRAM should be loaded, and if SRAM should be saved.");
puts("\t\tDo note that noload-save implies that save files will be deleted and overwritten.");
#ifdef HAVE_NETPLAY
puts("\t-H/--host: Host netplay as player 1.");
puts("\t-C/--connect: Connect to netplay as player 2.");
puts("\t--port: Port used to netplay. Default is 55435.");
puts("\t-F/--frames: Sync frames when using netplay.");
puts("\t--spectate: Netplay will become spectating mode.");
puts("\t\tHost can live stream the game content to players that connect.");
puts("\t\tHowever, the client will not be able to play. Multiple clients can connect to the host.");
puts("\t--nick: Picks a nickname for use with netplay. Not mandatory.");
#endif
#ifdef HAVE_NETWORK_CMD
puts("\t--command: Sends a command over UDP to an already running RetroArch process.");
puts("\t\tAvailable commands are listed if command is invalid.");
#endif
#ifdef HAVE_FFMPEG
puts("\t-r/--record: Path to record video file.\n\t\tUsing .mkv extension is recommended.");
puts("\t--recordconfig: Path to settings used during recording.");
puts("\t--size: Overrides output video size when recording with FFmpeg (format: WIDTHxHEIGHT).");
#endif
puts("\t-v/--verbose: Verbose logging.");
puts("\t-U/--ups: Specifies path for UPS patch that will be applied to ROM.");
puts("\t--bps: Specifies path for BPS patch that will be applied to ROM.");
puts("\t--ips: Specifies path for IPS patch that will be applied to ROM.");
puts("\t--no-patch: Disables all forms of rom patching.");
puts("\t-X/--xml: Specifies path to XML memory map.");
puts("\t-D/--detach: Detach RetroArch from the running console. Not relevant for all platforms.\n");
}
static void set_basename(const char *path)
{
strlcpy(g_extern.fullpath, path, sizeof(g_extern.fullpath));
strlcpy(g_extern.basename, path, sizeof(g_extern.basename));
char *dst = strrchr(g_extern.basename, '.');
if (dst)
*dst = '\0';
}
static void set_paths(const char *path)
{
set_basename(path);
RARCH_LOG("Opening file: \"%s\"\n", path);
g_extern.rom_file = fopen(path, "rb");
if (g_extern.rom_file == NULL)
{
RARCH_ERR("Could not open file: \"%s\"\n", path);
rarch_fail(1, "set_paths()");
}
if (!g_extern.has_set_save_path)
fill_pathname_noext(g_extern.savefile_name_srm, g_extern.basename, ".srm", sizeof(g_extern.savefile_name_srm));
if (!g_extern.has_set_state_path)
fill_pathname_noext(g_extern.savestate_name, g_extern.basename, ".state", sizeof(g_extern.savestate_name));
if (path_is_directory(g_extern.savefile_name_srm))
{
fill_pathname_dir(g_extern.savefile_name_srm, g_extern.basename, ".srm", sizeof(g_extern.savefile_name_srm));
RARCH_LOG("Redirecting save file to \"%s\".\n", g_extern.savefile_name_srm);
}
if (path_is_directory(g_extern.savestate_name))
{
fill_pathname_dir(g_extern.savestate_name, g_extern.basename, ".state", sizeof(g_extern.savestate_name));
RARCH_LOG("Redirecting save state to \"%s\".\n", g_extern.savestate_name);
}
// If this is already set,
// do not overwrite it as this was initialized before in a menu or otherwise.
if (!*g_settings.system_directory)
fill_pathname_basedir(g_settings.system_directory, path, sizeof(g_settings.system_directory));
#ifdef HAVE_CONFIGFILE
if (*g_extern.config_path && path_is_directory(g_extern.config_path))
{
fill_pathname_dir(g_extern.config_path, g_extern.basename, ".cfg", sizeof(g_extern.config_path));
RARCH_LOG("Redirecting config file to \"%s\".\n", g_extern.config_path);
if (!path_file_exists(g_extern.config_path))
{
*g_extern.config_path = '\0';
RARCH_LOG("Did not find config file. Using system default.\n");
}
}
#endif
}
static void verify_stdin_paths(void)
{
if (strlen(g_extern.savefile_name_srm) == 0)
{
RARCH_ERR("Need savefile path argument (--save) when reading rom from stdin.\n");
print_help();
rarch_fail(1, "verify_stdin_paths()");
}
else if (strlen(g_extern.savestate_name) == 0)
{
RARCH_ERR("Need savestate path argument (--savestate) when reading rom from stdin.\n");
print_help();
rarch_fail(1, "verify_stdin_paths()");
}
if (path_is_directory(g_extern.savefile_name_srm))
{
RARCH_ERR("Cannot specify directory for path argument (--save) when reading from stdin.\n");
print_help();
rarch_fail(1, "verify_stdin_paths()");
}
else if (path_is_directory(g_extern.savestate_name))
{
RARCH_ERR("Cannot specify directory for path argument (--savestate) when reading from stdin.\n");
print_help();
rarch_fail(1, "verify_stdin_paths()");
}
#ifdef HAVE_CONFIGFILE
else if (path_is_directory(g_extern.config_path))
{
RARCH_ERR("Cannot specify directory for config file (--config) when reading from stdin.\n");
print_help();
rarch_fail(1, "verify_stdin_paths()");
}
#endif
driver.stdin_claimed = true;
}
static void parse_input(int argc, char *argv[])
{
if (argc < 2)
{
print_help();
rarch_fail(1, "parse_input()");
}
// Make sure we can call parse_input several times ...
optind = 1;
int val = 0;
const struct option opts[] = {
#ifdef HAVE_DYNAMIC
{ "libretro", 1, NULL, 'L' },
#endif
{ "help", 0, NULL, 'h' },
{ "save", 1, NULL, 's' },
{ "fullscreen", 0, NULL, 'f' },
#ifdef HAVE_FFMPEG
{ "record", 1, NULL, 'r' },
{ "recordconfig", 1, &val, 'R' },
{ "size", 1, &val, 's' },
#endif
{ "verbose", 0, NULL, 'v' },
{ "gameboy", 1, NULL, 'g' },
#ifdef HAVE_CONFIGFILE
{ "config", 1, NULL, 'c' },
{ "appendconfig", 1, &val, 'C' },
#endif
{ "mouse", 1, NULL, 'm' },
{ "nodevice", 1, NULL, 'N' },
{ "scope", 0, NULL, 'p' },
{ "justifier", 0, NULL, 'j' },
{ "justifiers", 0, NULL, 'J' },
{ "dualanalog", 1, NULL, 'A' },
{ "savestate", 1, NULL, 'S' },
{ "bsx", 1, NULL, 'b' },
{ "bsxslot", 1, NULL, 'B' },
{ "multitap", 0, NULL, '4' },
{ "sufamiA", 1, NULL, 'Y' },
{ "sufamiB", 1, NULL, 'Z' },
#ifdef HAVE_BSV_MOVIE
{ "bsvplay", 1, NULL, 'P' },
{ "bsvrecord", 1, NULL, 'R' },
{ "sram-mode", 1, NULL, 'M' },
#endif
#ifdef HAVE_NETPLAY
{ "host", 0, NULL, 'H' },
{ "connect", 1, NULL, 'C' },
{ "frames", 1, NULL, 'F' },
{ "port", 1, &val, 'p' },
{ "spectate", 0, &val, 'S' },
{ "nick", 1, &val, 'N' },
#endif
#ifdef HAVE_NETWORK_CMD
{ "command", 1, &val, 'c' },
#endif
{ "ups", 1, NULL, 'U' },
{ "bps", 1, &val, 'B' },
{ "ips", 1, &val, 'I' },
{ "no-patch", 0, &val, 'n' },
{ "xml", 1, NULL, 'X' },
{ "detach", 0, NULL, 'D' },
{ "features", 0, &val, 'f' },
{ NULL, 0, NULL, 0 }
};
#ifdef HAVE_FFMPEG
#define FFMPEG_RECORD_ARG "r:"
#else
#define FFMPEG_RECORD_ARG
#endif
#ifdef HAVE_CONFIGFILE
#define CONFIG_FILE_ARG "c:"
#else
#define CONFIG_FILE_ARG
#endif
#ifdef HAVE_DYNAMIC
#define DYNAMIC_ARG "L:"
#else
#define DYNAMIC_ARG
#endif
#ifdef HAVE_NETPLAY
#define NETPLAY_ARG "HC:F:"
#else
#define NETPLAY_ARG
#endif
#ifdef HAVE_BSV_MOVIE
#define BSV_MOVIE_ARG "P:R:M:"
#else
#define BSV_MOVIE_ARG
#endif
const char *optstring = "hs:fvS:m:p4jJA:g:b:B:Y:Z:U:DN:X:" BSV_MOVIE_ARG NETPLAY_ARG DYNAMIC_ARG FFMPEG_RECORD_ARG CONFIG_FILE_ARG;
for (;;)
{
val = 0;
int c = getopt_long(argc, argv, optstring, opts, NULL);
int port;
if (c == -1)
break;
switch (c)
{
case 'h':
print_help();
exit(0);
case '4':
g_extern.has_multitap = true;
break;
case 'j':
g_extern.has_justifier = true;
break;
case 'J':
g_extern.has_justifiers = true;
break;
case 'A':
port = strtol(optarg, NULL, 0);
if (port < 1 || port > MAX_PLAYERS)
{
RARCH_ERR("Connect dualanalog to a valid port.\n");
print_help();
rarch_fail(1, "parse_input()");
}
g_extern.has_dualanalog[port - 1] = true;
break;
case 's':
strlcpy(g_extern.savefile_name_srm, optarg, sizeof(g_extern.savefile_name_srm));
g_extern.has_set_save_path = true;
break;
case 'f':
g_extern.force_fullscreen = true;
break;
case 'g':
strlcpy(g_extern.gb_rom_path, optarg, sizeof(g_extern.gb_rom_path));
g_extern.game_type = RARCH_CART_SGB;
break;
case 'b':
strlcpy(g_extern.bsx_rom_path, optarg, sizeof(g_extern.bsx_rom_path));
g_extern.game_type = RARCH_CART_BSX;
break;
case 'B':
strlcpy(g_extern.bsx_rom_path, optarg, sizeof(g_extern.bsx_rom_path));
g_extern.game_type = RARCH_CART_BSX_SLOTTED;
break;
case 'Y':
strlcpy(g_extern.sufami_rom_path[0], optarg, sizeof(g_extern.sufami_rom_path[0]));
g_extern.game_type = RARCH_CART_SUFAMI;
break;
case 'Z':
strlcpy(g_extern.sufami_rom_path[1], optarg, sizeof(g_extern.sufami_rom_path[1]));
g_extern.game_type = RARCH_CART_SUFAMI;
break;
case 'S':
strlcpy(g_extern.savestate_name, optarg, sizeof(g_extern.savestate_name));
g_extern.has_set_state_path = true;
break;
case 'v':
g_extern.verbose = true;
break;
case 'm':
port = strtol(optarg, NULL, 0);
if (port < 1 || port > MAX_PLAYERS)
{
RARCH_ERR("Connect mouse to a valid port.\n");
print_help();
rarch_fail(1, "parse_input()");
}
g_extern.has_mouse[port - 1] = true;
break;
case 'N':
port = strtol(optarg, NULL, 0);
if (port < 1 || port > MAX_PLAYERS)
{
RARCH_ERR("Disconnect device from a valid port.\n");
print_help();
rarch_fail(1, "parse_input()");
}
g_extern.disconnect_device[port - 1] = true;
break;
case 'p':
g_extern.has_scope = true;
break;
#ifdef HAVE_CONFIGFILE
case 'c':
strlcpy(g_extern.config_path, optarg, sizeof(g_extern.config_path));
break;
#endif
#ifdef HAVE_FFMPEG
case 'r':
strlcpy(g_extern.record_path, optarg, sizeof(g_extern.record_path));
g_extern.recording = true;
break;
#endif
#ifdef HAVE_DYNAMIC
case 'L':
strlcpy(g_settings.libretro, optarg, sizeof(g_settings.libretro));
break;
#endif
#ifdef HAVE_BSV_MOVIE
case 'P':