forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dynamic.c
1689 lines (1388 loc) · 51.5 KB
/
dynamic.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-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2017 - Daniel De Matteis
*
* 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 <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <boolean.h>
#include <file/file_path.h>
#include <compat/strl.h>
#include <compat/posix_string.h>
#include <dynamic/dylib.h>
#include <string/stdstring.h>
#include <retro_assert.h>
#include <libretro.h>
#define VFS_FRONTEND
#include <vfs/vfs_implementation.h>
#include <features/features_cpu.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_CHEEVOS
#include "cheevos/cheevos.h"
#endif
#ifdef HAVE_NETWORKING
#include "network/netplay/netplay.h"
#endif
#include "dynamic.h"
#include "command.h"
#include "audio/audio_driver.h"
#include "camera/camera_driver.h"
#include "location/location_driver.h"
#include "record/record_driver.h"
#include "core.h"
#include "driver.h"
#include "performance_counters.h"
#include "gfx/video_driver.h"
#include "cores/internal_cores.h"
#include "frontend/frontend_driver.h"
#include "content.h"
#include "dirs.h"
#include "paths.h"
#include "retroarch.h"
#include "configuration.h"
#include "msg_hash.h"
#include "verbosity.h"
#ifdef HAVE_DYNAMIC
#define SYMBOL(x) do { \
function_t func = dylib_proc(lib_handle, #x); \
memcpy(¤t_core->x, &func, sizeof(func)); \
if (current_core->x == NULL) { RARCH_ERR("Failed to load symbol: \"%s\"\n", #x); retroarch_fail(1, "init_libretro_sym()"); } \
} while (0)
static dylib_t lib_handle;
#else
#define SYMBOL(x) current_core->x = x
#endif
#define SYMBOL_DUMMY(x) current_core->x = libretro_dummy_##x
#ifdef HAVE_FFMPEG
#define SYMBOL_FFMPEG(x) current_core->x = libretro_ffmpeg_##x
#endif
#ifdef HAVE_IMAGEVIEWER
#define SYMBOL_IMAGEVIEWER(x) current_core->x = libretro_imageviewer_##x
#endif
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
#define SYMBOL_NETRETROPAD(x) current_core->x = libretro_netretropad_##x
#endif
#if defined(HAVE_VIDEO_PROCESSOR)
#define SYMBOL_VIDEOPROCESSOR(x) current_core->x = libretro_videoprocessor_##x
#endif
static bool ignore_environment_cb = false;
static bool core_set_shared_context = false;
static bool *load_no_content_hook = NULL;
const struct retro_subsystem_info *libretro_find_subsystem_info(
const struct retro_subsystem_info *info, unsigned num_info,
const char *ident)
{
unsigned i;
for (i = 0; i < num_info; i++)
{
if (string_is_equal(info[i].ident, ident))
return &info[i];
else if (string_is_equal(info[i].desc, ident))
return &info[i];
}
return NULL;
}
/**
* libretro_find_controller_description:
* @info : Pointer to controller info handle.
* @id : Identifier of controller to search
* for.
*
* Search for a controller of type @id in @info.
*
* Returns: controller description of found controller on success,
* otherwise NULL.
**/
const struct retro_controller_description *
libretro_find_controller_description(
const struct retro_controller_info *info, unsigned id)
{
unsigned i;
for (i = 0; i < info->num_types; i++)
{
if (info->types[i].id != id)
continue;
return &info->types[i];
}
return NULL;
}
/**
* libretro_free_system_info:
* @info : Pointer to system info information.
*
* Frees system information.
**/
void libretro_free_system_info(struct retro_system_info *info)
{
if (!info)
return;
free((void*)info->library_name);
free((void*)info->library_version);
free((void*)info->valid_extensions);
memset(info, 0, sizeof(*info));
}
static bool environ_cb_get_system_info(unsigned cmd, void *data)
{
switch (cmd)
{
case RETRO_ENVIRONMENT_SET_SUPPORT_NO_GAME:
*load_no_content_hook = *(const bool*)data;
break;
default:
return false;
}
return true;
}
#ifdef HAVE_DYNAMIC
/**
* libretro_get_environment_info:
* @func : Function pointer for get_environment_info.
* @load_no_content : If true, core should be able to auto-start
* without any content loaded.
*
* Sets environment callback in order to get statically known
* information from it.
*
* Fetched via environment callbacks instead of
* retro_get_system_info(), as this info is part of extensions.
*
* Should only be called once right after core load to
* avoid overwriting the "real" environ callback.
*
* For statically linked cores, pass retro_set_environment as argument.
*/
static void libretro_get_environment_info(void (*func)(retro_environment_t),
bool *load_no_content)
{
load_no_content_hook = load_no_content;
/* load_no_content gets set in this callback. */
func(environ_cb_get_system_info);
/* It's possible that we just set get_system_info callback
* to the currently running core.
*
* Make sure we reset it to the actual environment callback.
* Ignore any environment callbacks here in case we're running
* on the non-current core. */
ignore_environment_cb = true;
func(rarch_environment_cb);
ignore_environment_cb = false;
}
static bool load_dynamic_core(void)
{
function_t sym = dylib_proc(NULL, "retro_init");
if (sym)
{
/* Try to verify that -lretro was not linked in from other modules
* since loading it dynamically and with -l will fail hard. */
RARCH_ERR("Serious problem. RetroArch wants to load libretro cores"
" dynamically, but it is already linked.\n");
RARCH_ERR("This could happen if other modules RetroArch depends on "
"link against libretro directly.\n");
RARCH_ERR("Proceeding could cause a crash. Aborting ...\n");
retroarch_fail(1, "init_libretro_sym()");
}
if (string_is_empty(path_get(RARCH_PATH_CORE)))
{
RARCH_ERR("RetroArch is built for dynamic libretro cores, but "
"libretro_path is not set. Cannot continue.\n");
retroarch_fail(1, "init_libretro_sym()");
}
/* Need to use absolute path for this setting. It can be
* saved to content history, and a relative path would
* break in that scenario. */
path_resolve_realpath(
path_get_ptr(RARCH_PATH_CORE),
path_get_realsize(RARCH_PATH_CORE));
RARCH_LOG("Loading dynamic libretro core from: \"%s\"\n",
path_get(RARCH_PATH_CORE));
lib_handle = dylib_load(path_get(RARCH_PATH_CORE));
if (lib_handle)
return true;
RARCH_ERR("Failed to open libretro core: \"%s\"\n",
path_get(RARCH_PATH_CORE));
RARCH_ERR("Error(s): %s\n", dylib_error());
runloop_msg_queue_push(msg_hash_to_str(MSG_FAILED_TO_OPEN_LIBRETRO_CORE), 1, 180, true);
return false;
}
static dylib_t libretro_get_system_info_lib(const char *path,
struct retro_system_info *info, bool *load_no_content)
{
dylib_t lib = dylib_load(path);
void (*proc)(struct retro_system_info*);
if (!lib)
{
RARCH_ERR("%s: \"%s\"\n",
msg_hash_to_str(MSG_FAILED_TO_OPEN_LIBRETRO_CORE),
path);
RARCH_ERR("Error(s): %s\n", dylib_error());
return NULL;
}
proc = (void (*)(struct retro_system_info*))
dylib_proc(lib, "retro_get_system_info");
if (!proc)
{
dylib_close(lib);
return NULL;
}
proc(info);
if (load_no_content)
{
void (*set_environ)(retro_environment_t);
*load_no_content = false;
set_environ = (void (*)(retro_environment_t))
dylib_proc(lib, "retro_set_environment");
if (!set_environ)
return lib;
libretro_get_environment_info(set_environ, load_no_content);
}
return lib;
}
#endif
/**
* libretro_get_system_info:
* @path : Path to libretro library.
* @info : Pointer to system info information.
* @load_no_content : If true, core should be able to auto-start
* without any content loaded.
*
* Gets system info from an arbitrary lib.
* The struct returned must be freed as strings are allocated dynamically.
*
* Returns: true (1) if successful, otherwise false (0).
**/
bool libretro_get_system_info(const char *path,
struct retro_system_info *info, bool *load_no_content)
{
struct retro_system_info dummy_info;
#ifdef HAVE_DYNAMIC
dylib_t lib;
#endif
dummy_info.library_name = NULL;
dummy_info.library_version = NULL;
dummy_info.valid_extensions = NULL;
dummy_info.need_fullpath = false;
dummy_info.block_extract = false;
#ifdef HAVE_DYNAMIC
lib = libretro_get_system_info_lib(
path, &dummy_info, load_no_content);
if (!lib)
return false;
#else
if (load_no_content)
{
load_no_content_hook = load_no_content;
/* load_no_content gets set in this callback. */
retro_set_environment(environ_cb_get_system_info);
/* It's possible that we just set get_system_info callback
* to the currently running core.
*
* Make sure we reset it to the actual environment callback.
* Ignore any environment callbacks here in case we're running
* on the non-current core. */
ignore_environment_cb = true;
retro_set_environment(rarch_environment_cb);
ignore_environment_cb = false;
}
retro_get_system_info(&dummy_info);
#endif
memcpy(info, &dummy_info, sizeof(*info));
if (!string_is_empty(dummy_info.library_name))
info->library_name = strdup(dummy_info.library_name);
if (!string_is_empty(dummy_info.library_version))
info->library_version = strdup(dummy_info.library_version);
if (dummy_info.valid_extensions)
info->valid_extensions = strdup(dummy_info.valid_extensions);
#ifdef HAVE_DYNAMIC
dylib_close(lib);
#endif
return true;
}
/**
* load_symbols:
* @type : Type of core to be loaded.
* If CORE_TYPE_DUMMY, will
* load dummy symbols.
*
* Setup libretro callback symbols. Returns true on success,
* or false if symbols could not be loaded.
**/
static bool load_symbols(enum rarch_core_type type, struct retro_core_t *current_core)
{
switch (type)
{
case CORE_TYPE_PLAIN:
#ifdef HAVE_DYNAMIC
if (!load_dynamic_core())
return false;
#endif
SYMBOL(retro_init);
SYMBOL(retro_deinit);
SYMBOL(retro_api_version);
SYMBOL(retro_get_system_info);
SYMBOL(retro_get_system_av_info);
SYMBOL(retro_set_environment);
SYMBOL(retro_set_video_refresh);
SYMBOL(retro_set_audio_sample);
SYMBOL(retro_set_audio_sample_batch);
SYMBOL(retro_set_input_poll);
SYMBOL(retro_set_input_state);
SYMBOL(retro_set_controller_port_device);
SYMBOL(retro_reset);
SYMBOL(retro_run);
SYMBOL(retro_serialize_size);
SYMBOL(retro_serialize);
SYMBOL(retro_unserialize);
SYMBOL(retro_cheat_reset);
SYMBOL(retro_cheat_set);
SYMBOL(retro_load_game);
SYMBOL(retro_load_game_special);
SYMBOL(retro_unload_game);
SYMBOL(retro_get_region);
SYMBOL(retro_get_memory_data);
SYMBOL(retro_get_memory_size);
break;
case CORE_TYPE_DUMMY:
SYMBOL_DUMMY(retro_init);
SYMBOL_DUMMY(retro_deinit);
SYMBOL_DUMMY(retro_api_version);
SYMBOL_DUMMY(retro_get_system_info);
SYMBOL_DUMMY(retro_get_system_av_info);
SYMBOL_DUMMY(retro_set_environment);
SYMBOL_DUMMY(retro_set_video_refresh);
SYMBOL_DUMMY(retro_set_audio_sample);
SYMBOL_DUMMY(retro_set_audio_sample_batch);
SYMBOL_DUMMY(retro_set_input_poll);
SYMBOL_DUMMY(retro_set_input_state);
SYMBOL_DUMMY(retro_set_controller_port_device);
SYMBOL_DUMMY(retro_reset);
SYMBOL_DUMMY(retro_run);
SYMBOL_DUMMY(retro_serialize_size);
SYMBOL_DUMMY(retro_serialize);
SYMBOL_DUMMY(retro_unserialize);
SYMBOL_DUMMY(retro_cheat_reset);
SYMBOL_DUMMY(retro_cheat_set);
SYMBOL_DUMMY(retro_load_game);
SYMBOL_DUMMY(retro_load_game_special);
SYMBOL_DUMMY(retro_unload_game);
SYMBOL_DUMMY(retro_get_region);
SYMBOL_DUMMY(retro_get_memory_data);
SYMBOL_DUMMY(retro_get_memory_size);
break;
case CORE_TYPE_FFMPEG:
#ifdef HAVE_FFMPEG
SYMBOL_FFMPEG(retro_init);
SYMBOL_FFMPEG(retro_deinit);
SYMBOL_FFMPEG(retro_api_version);
SYMBOL_FFMPEG(retro_get_system_info);
SYMBOL_FFMPEG(retro_get_system_av_info);
SYMBOL_FFMPEG(retro_set_environment);
SYMBOL_FFMPEG(retro_set_video_refresh);
SYMBOL_FFMPEG(retro_set_audio_sample);
SYMBOL_FFMPEG(retro_set_audio_sample_batch);
SYMBOL_FFMPEG(retro_set_input_poll);
SYMBOL_FFMPEG(retro_set_input_state);
SYMBOL_FFMPEG(retro_set_controller_port_device);
SYMBOL_FFMPEG(retro_reset);
SYMBOL_FFMPEG(retro_run);
SYMBOL_FFMPEG(retro_serialize_size);
SYMBOL_FFMPEG(retro_serialize);
SYMBOL_FFMPEG(retro_unserialize);
SYMBOL_FFMPEG(retro_cheat_reset);
SYMBOL_FFMPEG(retro_cheat_set);
SYMBOL_FFMPEG(retro_load_game);
SYMBOL_FFMPEG(retro_load_game_special);
SYMBOL_FFMPEG(retro_unload_game);
SYMBOL_FFMPEG(retro_get_region);
SYMBOL_FFMPEG(retro_get_memory_data);
SYMBOL_FFMPEG(retro_get_memory_size);
#endif
break;
case CORE_TYPE_IMAGEVIEWER:
#ifdef HAVE_IMAGEVIEWER
SYMBOL_IMAGEVIEWER(retro_init);
SYMBOL_IMAGEVIEWER(retro_deinit);
SYMBOL_IMAGEVIEWER(retro_api_version);
SYMBOL_IMAGEVIEWER(retro_get_system_info);
SYMBOL_IMAGEVIEWER(retro_get_system_av_info);
SYMBOL_IMAGEVIEWER(retro_set_environment);
SYMBOL_IMAGEVIEWER(retro_set_video_refresh);
SYMBOL_IMAGEVIEWER(retro_set_audio_sample);
SYMBOL_IMAGEVIEWER(retro_set_audio_sample_batch);
SYMBOL_IMAGEVIEWER(retro_set_input_poll);
SYMBOL_IMAGEVIEWER(retro_set_input_state);
SYMBOL_IMAGEVIEWER(retro_set_controller_port_device);
SYMBOL_IMAGEVIEWER(retro_reset);
SYMBOL_IMAGEVIEWER(retro_run);
SYMBOL_IMAGEVIEWER(retro_serialize_size);
SYMBOL_IMAGEVIEWER(retro_serialize);
SYMBOL_IMAGEVIEWER(retro_unserialize);
SYMBOL_IMAGEVIEWER(retro_cheat_reset);
SYMBOL_IMAGEVIEWER(retro_cheat_set);
SYMBOL_IMAGEVIEWER(retro_load_game);
SYMBOL_IMAGEVIEWER(retro_load_game_special);
SYMBOL_IMAGEVIEWER(retro_unload_game);
SYMBOL_IMAGEVIEWER(retro_get_region);
SYMBOL_IMAGEVIEWER(retro_get_memory_data);
SYMBOL_IMAGEVIEWER(retro_get_memory_size);
#endif
break;
case CORE_TYPE_NETRETROPAD:
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
SYMBOL_NETRETROPAD(retro_init);
SYMBOL_NETRETROPAD(retro_deinit);
SYMBOL_NETRETROPAD(retro_api_version);
SYMBOL_NETRETROPAD(retro_get_system_info);
SYMBOL_NETRETROPAD(retro_get_system_av_info);
SYMBOL_NETRETROPAD(retro_set_environment);
SYMBOL_NETRETROPAD(retro_set_video_refresh);
SYMBOL_NETRETROPAD(retro_set_audio_sample);
SYMBOL_NETRETROPAD(retro_set_audio_sample_batch);
SYMBOL_NETRETROPAD(retro_set_input_poll);
SYMBOL_NETRETROPAD(retro_set_input_state);
SYMBOL_NETRETROPAD(retro_set_controller_port_device);
SYMBOL_NETRETROPAD(retro_reset);
SYMBOL_NETRETROPAD(retro_run);
SYMBOL_NETRETROPAD(retro_serialize_size);
SYMBOL_NETRETROPAD(retro_serialize);
SYMBOL_NETRETROPAD(retro_unserialize);
SYMBOL_NETRETROPAD(retro_cheat_reset);
SYMBOL_NETRETROPAD(retro_cheat_set);
SYMBOL_NETRETROPAD(retro_load_game);
SYMBOL_NETRETROPAD(retro_load_game_special);
SYMBOL_NETRETROPAD(retro_unload_game);
SYMBOL_NETRETROPAD(retro_get_region);
SYMBOL_NETRETROPAD(retro_get_memory_data);
SYMBOL_NETRETROPAD(retro_get_memory_size);
#endif
break;
case CORE_TYPE_VIDEO_PROCESSOR:
#if defined(HAVE_VIDEO_PROCESSOR)
SYMBOL_VIDEOPROCESSOR(retro_init);
SYMBOL_VIDEOPROCESSOR(retro_deinit);
SYMBOL_VIDEOPROCESSOR(retro_api_version);
SYMBOL_VIDEOPROCESSOR(retro_get_system_info);
SYMBOL_VIDEOPROCESSOR(retro_get_system_av_info);
SYMBOL_VIDEOPROCESSOR(retro_set_environment);
SYMBOL_VIDEOPROCESSOR(retro_set_video_refresh);
SYMBOL_VIDEOPROCESSOR(retro_set_audio_sample);
SYMBOL_VIDEOPROCESSOR(retro_set_audio_sample_batch);
SYMBOL_VIDEOPROCESSOR(retro_set_input_poll);
SYMBOL_VIDEOPROCESSOR(retro_set_input_state);
SYMBOL_VIDEOPROCESSOR(retro_set_controller_port_device);
SYMBOL_VIDEOPROCESSOR(retro_reset);
SYMBOL_VIDEOPROCESSOR(retro_run);
SYMBOL_VIDEOPROCESSOR(retro_serialize_size);
SYMBOL_VIDEOPROCESSOR(retro_serialize);
SYMBOL_VIDEOPROCESSOR(retro_unserialize);
SYMBOL_VIDEOPROCESSOR(retro_cheat_reset);
SYMBOL_VIDEOPROCESSOR(retro_cheat_set);
SYMBOL_VIDEOPROCESSOR(retro_load_game);
SYMBOL_VIDEOPROCESSOR(retro_load_game_special);
SYMBOL_VIDEOPROCESSOR(retro_unload_game);
SYMBOL_VIDEOPROCESSOR(retro_get_region);
SYMBOL_VIDEOPROCESSOR(retro_get_memory_data);
SYMBOL_VIDEOPROCESSOR(retro_get_memory_size);
#endif
break;
}
return true;
}
/**
* init_libretro_sym:
* @type : Type of core to be loaded.
* If CORE_TYPE_DUMMY, will
* load dummy symbols.
*
* Initializes libretro symbols and
* setups environment callback functions. Returns true on success,
* or false if symbols could not be loaded.
**/
bool init_libretro_sym(enum rarch_core_type type, struct retro_core_t *current_core)
{
/* Guarantee that we can do "dirty" casting.
* Every OS that this program supports should pass this. */
retro_assert(sizeof(void*) == sizeof(void (*)(void)));
if (!load_symbols(type, current_core))
return false;
return true;
}
bool libretro_get_shared_context(void)
{
return core_set_shared_context;
}
/**
* uninit_libretro_sym:
*
* Frees libretro core.
*
* Frees all core options,
* associated state, and
* unbind all libretro callback symbols.
**/
void uninit_libretro_sym(struct retro_core_t *current_core)
{
#ifdef HAVE_DYNAMIC
if (lib_handle)
dylib_close(lib_handle);
lib_handle = NULL;
#endif
memset(current_core, 0, sizeof(struct retro_core_t));
core_set_shared_context = false;
rarch_ctl(RARCH_CTL_CORE_OPTIONS_DEINIT, NULL);
rarch_ctl(RARCH_CTL_SYSTEM_INFO_FREE, NULL);
rarch_ctl(RARCH_CTL_FRAME_TIME_FREE, NULL);
camera_driver_ctl(RARCH_CAMERA_CTL_UNSET_ACTIVE, NULL);
location_driver_ctl(RARCH_LOCATION_CTL_UNSET_ACTIVE, NULL);
/* Performance counters no longer valid. */
performance_counters_clear();
}
static void rarch_log_libretro(enum retro_log_level level,
const char *fmt, ...)
{
va_list vp;
settings_t *settings = config_get_ptr();
if ((unsigned)level < settings->uints.libretro_log_level)
return;
va_start(vp, fmt);
switch (level)
{
case RETRO_LOG_DEBUG:
RARCH_LOG_V("[libretro DEBUG]", fmt, vp);
break;
case RETRO_LOG_INFO:
RARCH_LOG_OUTPUT_V("[libretro INFO]", fmt, vp);
break;
case RETRO_LOG_WARN:
RARCH_WARN_V("[libretro WARN]", fmt, vp);
break;
case RETRO_LOG_ERROR:
RARCH_ERR_V("[libretro ERROR]", fmt, vp);
break;
default:
break;
}
va_end(vp);
}
static size_t mmap_add_bits_down(size_t n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
/* double shift to avoid warnings on 32bit (it's dead code, but compilers suck) */
if (sizeof(size_t) > 4)
n |= n >> 16 >> 16;
return n;
}
static size_t mmap_inflate(size_t addr, size_t mask)
{
while (mask)
{
size_t tmp = (mask - 1) & ~mask;
/* to put in an 1 bit instead, OR in tmp+1 */
addr = ((addr & ~tmp) << 1) | (addr & tmp);
mask = mask & (mask - 1);
}
return addr;
}
static size_t mmap_reduce(size_t addr, size_t mask)
{
while (mask)
{
size_t tmp = (mask - 1) & ~mask;
addr = (addr & tmp) | ((addr >> 1) & ~tmp);
mask = (mask & (mask - 1)) >> 1;
}
return addr;
}
static size_t mmap_highest_bit(size_t n)
{
n = mmap_add_bits_down(n);
return n ^ (n >> 1);
}
static bool mmap_preprocess_descriptors(rarch_memory_descriptor_t *first, unsigned count)
{
size_t top_addr = 1;
rarch_memory_descriptor_t *desc = NULL;
const rarch_memory_descriptor_t *end = first + count;
for (desc = first; desc < end; desc++)
{
if (desc->core.select != 0)
top_addr |= desc->core.select;
else
top_addr |= desc->core.start + desc->core.len - 1;
}
top_addr = mmap_add_bits_down(top_addr);
for (desc = first; desc < end; desc++)
{
if (desc->core.select == 0)
{
if (desc->core.len == 0)
return false;
if ((desc->core.len & (desc->core.len - 1)) != 0)
return false;
desc->core.select = top_addr & ~mmap_inflate(mmap_add_bits_down(desc->core.len - 1),
desc->core.disconnect);
}
if (desc->core.len == 0)
desc->core.len = mmap_add_bits_down(mmap_reduce(top_addr & ~desc->core.select,
desc->core.disconnect)) + 1;
if (desc->core.start & ~desc->core.select)
return false;
while (mmap_reduce(top_addr & ~desc->core.select, desc->core.disconnect) >> 1 > desc->core.len - 1)
desc->core.disconnect |= mmap_highest_bit(top_addr & ~desc->core.select & ~desc->core.disconnect);
desc->disconnect_mask = mmap_add_bits_down(desc->core.len - 1);
desc->core.disconnect &= desc->disconnect_mask;
while ((~desc->disconnect_mask) >> 1 & desc->core.disconnect)
{
desc->disconnect_mask >>= 1;
desc->core.disconnect &= desc->disconnect_mask;
}
}
return true;
}
static bool dynamic_request_hw_context(enum retro_hw_context_type type,
unsigned minor, unsigned major)
{
switch (type)
{
case RETRO_HW_CONTEXT_NONE:
RARCH_LOG("Requesting no HW context.\n");
break;
case RETRO_HW_CONTEXT_VULKAN:
#ifdef HAVE_VULKAN
RARCH_LOG("Requesting Vulkan context.\n");
break;
#else
RARCH_ERR("Requesting Vulkan context, but RetroArch is not compiled against Vulkan. Cannot use HW context.\n");
return false;
#endif
#if defined(HAVE_OPENGLES)
#if (defined(HAVE_OPENGLES2) || defined(HAVE_OPENGLES3))
case RETRO_HW_CONTEXT_OPENGLES2:
case RETRO_HW_CONTEXT_OPENGLES3:
RARCH_LOG("Requesting OpenGLES%u context.\n",
type == RETRO_HW_CONTEXT_OPENGLES2 ? 2 : 3);
break;
#if defined(HAVE_OPENGLES3)
case RETRO_HW_CONTEXT_OPENGLES_VERSION:
RARCH_LOG("Requesting OpenGLES%u.%u context.\n",
major, minor);
break;
#endif
#endif
case RETRO_HW_CONTEXT_OPENGL:
case RETRO_HW_CONTEXT_OPENGL_CORE:
RARCH_ERR("Requesting OpenGL context, but RetroArch "
"is compiled against OpenGLES. Cannot use HW context.\n");
return false;
#elif defined(HAVE_OPENGL)
case RETRO_HW_CONTEXT_OPENGLES2:
case RETRO_HW_CONTEXT_OPENGLES3:
RARCH_ERR("Requesting OpenGLES%u context, but RetroArch "
"is compiled against OpenGL. Cannot use HW context.\n",
type == RETRO_HW_CONTEXT_OPENGLES2 ? 2 : 3);
return false;
case RETRO_HW_CONTEXT_OPENGLES_VERSION:
RARCH_ERR("Requesting OpenGLES%u.%u context, but RetroArch "
"is compiled against OpenGL. Cannot use HW context.\n",
major, minor);
return false;
case RETRO_HW_CONTEXT_OPENGL:
RARCH_LOG("Requesting OpenGL context.\n");
break;
case RETRO_HW_CONTEXT_OPENGL_CORE:
{
gfx_ctx_flags_t flags;
flags.flags = 0;
BIT32_SET(flags.flags, GFX_CTX_FLAGS_GL_CORE_CONTEXT);
video_context_driver_set_flags(&flags);
RARCH_LOG("Requesting core OpenGL context (%u.%u).\n",
major, minor);
}
break;
#endif
default:
RARCH_LOG("Requesting unknown context.\n");
return false;
}
return true;
}
static bool dynamic_verify_hw_context(enum retro_hw_context_type type,
unsigned minor, unsigned major)
{
const char *video_ident = video_driver_get_ident();
switch (type)
{
case RETRO_HW_CONTEXT_VULKAN:
if (!string_is_equal(video_ident, "vulkan"))
return false;
break;
case RETRO_HW_CONTEXT_OPENGLES2:
case RETRO_HW_CONTEXT_OPENGLES3:
case RETRO_HW_CONTEXT_OPENGLES_VERSION:
case RETRO_HW_CONTEXT_OPENGL:
case RETRO_HW_CONTEXT_OPENGL_CORE:
if (!string_is_equal(video_ident, "gl"))
return false;
break;
default:
break;
}
return true;
}
static void core_performance_counter_start(struct retro_perf_counter *perf)
{
if (rarch_ctl(RARCH_CTL_IS_PERFCNT_ENABLE, NULL))
{
perf->call_cnt++;
perf->start = cpu_features_get_perf_counter();
}
}
static void core_performance_counter_stop(struct retro_perf_counter *perf)
{
if (rarch_ctl(RARCH_CTL_IS_PERFCNT_ENABLE, NULL))
perf->total += cpu_features_get_perf_counter() - perf->start;
}
/**
* rarch_environment_cb:
* @cmd : Identifier of command.
* @data : Pointer to data.
*
* Environment callback function implementation.
*
* Returns: true (1) if environment callback command could
* be performed, otherwise false (0).
**/
bool rarch_environment_cb(unsigned cmd, void *data)
{
unsigned p;
settings_t *settings = config_get_ptr();
rarch_system_info_t *system = runloop_get_system_info();
if (ignore_environment_cb)
return false;
switch (cmd)
{
case RETRO_ENVIRONMENT_GET_OVERSCAN:
*(bool*)data = !settings->bools.video_crop_overscan;
RARCH_LOG("Environ GET_OVERSCAN: %u\n",
(unsigned)!settings->bools.video_crop_overscan);
break;
case RETRO_ENVIRONMENT_GET_CAN_DUPE:
*(bool*)data = true;
RARCH_LOG("Environ GET_CAN_DUPE: true\n");
break;
case RETRO_ENVIRONMENT_GET_VARIABLE:
if (!rarch_ctl(RARCH_CTL_CORE_OPTIONS_GET, data))
{
struct retro_variable *var = (struct retro_variable*)data;
if (var)
{
RARCH_LOG("Environ GET_VARIABLE %s: not implemented.\n", var->key);
var->value = NULL;
}
}
break;
case RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE:
*(bool*)data = rarch_ctl(RARCH_CTL_IS_CORE_OPTION_UPDATED, NULL);
break;
case RETRO_ENVIRONMENT_SET_VARIABLES:
RARCH_LOG("Environ SET_VARIABLES.\n");
rarch_ctl(RARCH_CTL_CORE_OPTIONS_DEINIT, NULL);
rarch_ctl(RARCH_CTL_CORE_OPTIONS_INIT, data);
break;
case RETRO_ENVIRONMENT_SET_MESSAGE:
{
const struct retro_message *msg = (const struct retro_message*)data;
RARCH_LOG("Environ SET_MESSAGE: %s\n", msg->msg);
runloop_msg_queue_push(msg->msg, 3, msg->frames, true);
break;
}
case RETRO_ENVIRONMENT_SET_ROTATION:
{