forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua_manager.c
More file actions
3869 lines (3420 loc) · 147 KB
/
lua_manager.c
File metadata and controls
3869 lines (3420 loc) · 147 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* RetroArch - A frontend for libretro.
* Copyright (C) 2025-2026 - eadmaster
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <locale.h>
#include <lists/dir_list.h>
#include <file/file_path.h>
#include <streams/stdin_stream.h>
#include <streams/file_stream.h>
#include <string/stdstring.h>
#include <retro_timers.h>
#include <lrc_hash.h>
#include <compat/strcasestr.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include "lua_manager.h"
#include "runloop.h"
#include "verbosity.h"
#include "paths.h"
#include "core_info.h"
#include "content.h"
#include "version.h"
#include "command.h"
#include "input/input_driver.h"
#include "input/input_keymaps.h"
#include "gfx/video_driver.h"
#include "gfx/video_display_server.h"
#include "gfx/gfx_widgets.h"
#include "tasks/tasks_internal.h"
/* #include "gfx/drivers_font_renderer/bitmap.h" */
#ifdef HAVE_MENU
#include "menu/menu_input.h"
#include "menu/menu_driver.h"
#endif
/* LUA API based on Bizhawk https://tasvideos.org/Bizhawk/LuaFunctions */
static lua_State *co = NULL;
static unsigned int current_memory_domain = RETRO_MEMORY_SYSTEM_RAM;
static const char* memory_domains_list_names[] = { "Battery RAM", "RTC", "RAM", "VRAM", "ROM" };
static bool LUA_SCRIPTS_SANDBOXED=false; /* TODO: turn into user setting */
static void check_sandboxed_path(lua_State *L, char* path)
{
if (!LUA_SCRIPTS_SANDBOXED)
return; /* nothing to check */
if (path_is_absolute(path))
{
/* check if parent dir matches current content */
char content_parent_dir[PATH_MAX_LENGTH] = {0};
snprintf(content_parent_dir, PATH_MAX_LENGTH, "%s", path_get(RARCH_PATH_BASENAME));
path_basedir(content_parent_dir);
if (!string_starts_with(path, content_parent_dir))
luaL_error(L, "Access denied: file path is does not match current content. Disable sandboxing to bypass.");
/* TODO: also allow subdirs of */
/* const char* retroarch_system_dir = path_get(RARCH_PATH_CONFIG); // /system */
}
if (string_starts_with(path, ".."))
luaL_error(L, "Access denied: file path cannot access parent. Disable sandboxing to bypass.");
}
typedef int (*print_fn)(const char *fmt, ...);
static int print_luatable(lua_State *L, print_fn printer)
{
luaL_checktype(L, 1, LUA_TTABLE);
lua_pushnil(L); /* first key */
while (lua_next(L, 1) != 0)
{
const char *key_str = NULL;
char key_buf[64];
/* convert key */
if (lua_type(L, -2) == LUA_TNUMBER)
{
snprintf(key_buf, sizeof(key_buf), "%g", lua_tonumber(L, -2) - 1);
key_str = key_buf;
}
else if (lua_type(L, -2) == LUA_TSTRING)
{
key_str = lua_tostring(L, -2);
}
else
{
/* skip current entry */
lua_pop(L, 1);
continue;
}
/* switch on value type */
int val_type = lua_type(L, -1);
switch (val_type)
{
case LUA_TSTRING:
printer("\"%s\": \"%s\"\n", key_str, lua_tostring(L, -1));
break;
case LUA_TNUMBER:
printer("\"%s\": %g\n", key_str, lua_tonumber(L, -1));
break;
case LUA_TBOOLEAN:
printer("\"%s\": \"%s\"\n", key_str, lua_toboolean(L, -1) ? "True" : "False");
break;
case LUA_TNIL:
printer("\"%s\": nil\n", key_str);
break;
default:
printer("\"%s\": <%s>\n", key_str, lua_typename(L, val_type));
break;
}
lua_pop(L, 1); /* next entry */
}
return 0;
}
/* console.log() */
/* Outputs to the Retroarch debug console */
int console_log(lua_State *L)
{
if (lua_type(L, 1) == LUA_TSTRING)
{
/* simple string passed */
char *msg = lua_tostring(L,1);
RARCH_LOG("[Lua] %s\n", msg);
return 0;
}
else if (lua_type(L, 1) == LUA_TTABLE)
{
return print_luatable(L, (print_fn)RARCH_LOG);
}
else
return luaL_error(L, "console.write expects string or table");
return 0;
}
/* void console.writeline(object[] outputs) */
/* Outputs the given object to the output box on the Lua Console dialog. Note: Can accept a LuaTable */
int console_writeline(lua_State *L)
{
if (lua_type(L, 1) == LUA_TSTRING)
{
/* simple string passed */
char *msg = lua_tostring(L, 1);
printf("%s\n", msg);
}
else if (lua_type(L, 1) == LUA_TTABLE)
{
print_luatable(L, (print_fn)printf);
}
else
return luaL_error(L, "console.write expects string or table");
fflush(stdout);
return 0;
}
/* void console.write(object[] outputs) */
/* Outputs the given object to the output box on the Lua Console dialog. */
int console_write(lua_State *L)
{
if (lua_type(L, 1) == LUA_TSTRING)
{
/* simple string passed */
char *msg = lua_tostring(L, 1);
printf("%s", msg);
fflush(stdout);
return 0;
}
else
return console_writeline(L); /* handle tables and other types */
}
/*
#include "database_info.h"
database_info_t get_database_entry(char* content_name)
{
#ifdef HAVE_LIBRETRODB
unsigned i;
const char *query = string_is_empty(info->path_c) ? NULL : info->path_c;
database_info_list_t *db_list = database_info_list_new(info->path, query);
if (db_list)
{
for (i = 0; i < db_list->count; i++)
if (!string_is_empty(db_list->list[i].name) && ...)
return db_list->list[i]
}
database_info_list_free(db_list);
free(db_list);
#endif
return 0;
}
*/
/* gameinfo.getromhash() */
/* returns the hash of the currently loaded rom, if a rom is loaded */
/* TODO: currently it is the CRC32, Bizhawk uses MD5 for CD-based systems, SHA1 for ROM-based systems */
/* TODO: fceux allows passing "string type" arg like "md5" */
int gameinfo_getromhash(lua_State *L)
{
char reply[40] = {0};
snprintf(reply, sizeof(reply), "%X", content_get_crc());
#ifdef HAVE_LIBRETRODB
/* TODO: try to obtain sha1/md5 hash from database_info_t */
/*
database_info_t e = get_database_entry(content_name)
if (!string_is_empty(e.sha1))
snprintf(reply, sizeof(reply), "%s", e.sha1);
*/
#endif
/* or: rehash content_state_get_ptr()->content_list->entries[0].data (not available for CD-based content) */
lua_pushstring(L, reply);
return 1;
}
/* gameinfo.getromname() */
/* returns the name of the currently loaded rom, if a rom is loaded */
int gameinfo_getromname(lua_State *L)
{
const char *path = path_get(RARCH_PATH_BASENAME);
const char *basename = path ? path_basename(path) : ""; /* fallback to empty string */
lua_pushfstring(L, "%s", basename);
return 1;
}
/* gameinfo_getrompath() */
/* returns the full path of the currently loaded rom (can be a relative path) */
int gameinfo_getrompath(lua_State *L)
{
const char *path = path_get(RARCH_PATH_CONTENT);
const char *r = path ? path : ""; /* fallback to empty string */
lua_pushfstring(L, "%s", r);
return 1;
}
/* bool savestate.loadslot(int slotnum, [bool suppressosd = False]) */
/* Loads the savestate at the given slot number. Returns true if succeeded. */
int savestate_loadslot(lua_State *L)
{
int slotnum = luaL_checkinteger(L, 1);
settings_t *settings = config_get_ptr();
configuration_set_int(settings, settings->ints.state_slot, slotnum);
command_event(CMD_EVENT_LOAD_STATE, NULL);
return 0;
}
/* void savestate.saveslot(int slotnum, [bool suppressosd = False]) */
/* Saves a state at the given save slot. */
int savestate_saveslot(lua_State *L)
{
int slotnum = luaL_checkinteger(L, 1);
settings_t *settings = config_get_ptr();
configuration_set_int(settings, settings->ints.state_slot, slotnum);
command_event(CMD_EVENT_SAVE_STATE, NULL);
return 0;
}
/* void savestate.save(string path, [bool suppressosd = False]) */
/* Saves a state at the given path. */
int savestate_save(lua_State *L)
{
const char *state_path = luaL_checkstring(L, 1);
check_sandboxed_path(L, state_path);
command_event(CMD_EVENT_SAVE_STATE_TO_RAM, NULL);
command_event(CMD_EVENT_RAM_STATE_TO_FILE, state_path);
return 0;
}
/* bool savestate.load(string path, [bool suppressosd = False]) */
/* Loads a savestate with the given path. Returns true if succeeded. */
int savestate_load(lua_State *L)
{
const char *state_path = luaL_checkstring(L, 1);
bool r = content_load_state(state_path, false, true); /* Load a state from disk to memory. */
/* command_event(CMD_EVENT_LOAD_STATE_FROM_RAM, NULL); */
lua_pushboolean(L, r);
return 1;
}
/* bool client.ispaused() */
/* Returns true if emulator is paused, otherwise, false */
int client_ispaused(lua_State *L)
{
runloop_state_t *runloop_st = runloop_state_get_ptr();
int r = (runloop_st->flags & RUNLOOP_FLAG_PAUSED);
lua_pushboolean(L, r);
return 1;
}
/* bool bizstring.contains(string str, string str2) */
/* Returns whether or not str contains str2 */
int bizstring_contains(lua_State *L)
{
const char *str = luaL_checkstring(L, 1);
const char *str2 = luaL_checkstring(L, 2);
if (string_find_index_substring_string(str2, str) == -1)
lua_pushboolean(L, false);
else
lua_pushboolean(L, true);
return 1;
}
/* bool bizstring.endswith(string str, string str2) */
/* Returns whether str ends wth str2 (case-sensitive) */
int bizstring_endswith(lua_State *L)
{
const char *str = luaL_checkstring(L, 1);
const char *str2 = luaL_checkstring(L, 2);
bool r = string_ends_with(str, str2);
lua_pushboolean(L, r);
return 1;
}
/* bool bizstring.startswith(string str, string str2) */
/* Returns whether str starts with str2 */
int bizstring_startswith(lua_State *L)
{
const char *str = luaL_checkstring(L, 1);
const char *str2 = luaL_checkstring(L, 2);
bool r = string_starts_with(str, str2);
lua_pushboolean(L, r);
return 1;
}
/* string bizstring.tolower(string str) */
/* Returns an lowercase version of the given string */
int bizstring_tolower(lua_State *L)
{
char *str = luaL_checkstring(L, 1);
string_to_lower(str);
lua_pushstring(L, str);
return 1;
}
/* string bizstring.toupper(string str) */
/* Returns an uppercase version of the given string */
int bizstring_toupper(lua_State *L)
{
char *str = luaL_checkstring(L, 1);
string_to_upper(str);
lua_pushstring(L, str);
return 1;
}
/* string bizstring.trim(string str) */
/* returns a string that trims whitespace on the left and right ends of the string */
int bizstring_trim(lua_State *L)
{
const char *str = luaL_checkstring(L, 1);
string_trim_whitespace(str);
lua_pushstring(L, str);
return 1;
}
#ifdef HAVE_ICONV
/* #if defined(__has_include) */
/* #if __has_include(<iconv.h>) */
#include <iconv.h>
/* #include <errno.h> */
/* nluatable bizstring.encode(string str, [string encoding = utf-8]) */
/* Encodes a string to a byte array (table). The encoding parameter determines which scheme is used (and it will first be converted from Lua's native encoding if necessary). */
int bizstring_encode(lua_State *L)
{
const char *str = luaL_checkstring(L, 1);
const char *to_enc = luaL_optstring(L, 2, "UTF-8");
const char *from_enc = "UTF-8"; /* Assume Lua input is UTF-8 */
iconv_t cd = iconv_open(to_enc, from_enc);
if (cd == (iconv_t)-1)
return luaL_error(L, "Unsupported encoding: %s", to_enc);
size_t in_bytes = strlen(str);
/* Allocate a buffer; 4x input size is safe for most multi-byte conversions */
size_t out_bytes_left = in_bytes * 4;
size_t out_buf_size = out_bytes_left;
char *out_buf = malloc(out_buf_size);
char *in_ptr = (char *)str;
char *out_ptr = out_buf;
if (iconv(cd, &in_ptr, &in_bytes, &out_ptr, &out_bytes_left) == (size_t)-1)
{
/* int err = errno; */
free(out_buf);
iconv_close(cd);
/* if (err == EILSEQ) return luaL_error(L, "Illegal character sequence"); */
return luaL_error(L, "Conversion failed");
}
/* iconv decrements the 'left' counters, so we calculate actual used size */
size_t final_size = out_buf_size - out_bytes_left;
/* Create Lua Table */
lua_newtable(L);
for (size_t i = 0; i < final_size; i++)
{
lua_pushinteger(L, (unsigned char)out_buf[i]);
lua_rawseti(L, -2, i + 1);
}
free(out_buf);
iconv_close(cd);
return 1;
}
/* string bizstring.decode(nluatable bytes, [string encoding = utf-8]) */
/* Reads a string from an array-like table of bytes. The encoding parameter determines which scheme is used (and it will then be converted to Lua's native encoding if necessary). */
int bizstring_decode(lua_State *L)
{
luaL_checktype(L, 1, LUA_TTABLE);
const char *from_enc = luaL_optstring(L, 2, "UTF-8");
const char *to_enc = "UTF-8";
/* Extract bytes from the Lua Table into a temporary C buffer */
size_t in_bytes = lua_rawlen(L, 1);
if (in_bytes == 0)
{
lua_pushstring(L, "");
return 1;
}
char *in_buf = malloc(in_bytes);
for (size_t i = 0; i < in_bytes; i++)
{
lua_rawgeti(L, 1, i + 1);
in_buf[i] = (char)luaL_checkinteger(L, -1);
lua_pop(L, 1);
}
/* Setup iconv */
iconv_t cd = iconv_open(to_enc, from_enc);
if (cd == (iconv_t)-1)
{
free(in_buf);
return luaL_error(L, "Unsupported encoding: %s", from_enc);
}
/* Prepare output buffer (UTF-8 can be up to 4 bytes per char, */
/* but usually, input size * 4 is a safe maximum) */
size_t out_buf_size = in_bytes * 4 + 1;
char *out_buf = malloc(out_buf_size);
char *in_ptr = in_buf;
char *out_ptr = out_buf;
size_t in_bytes_left = in_bytes;
size_t out_bytes_left = out_buf_size;
/* Reset iconv state for a fresh conversion */
iconv(cd, NULL, NULL, NULL, NULL);
/* Perform Conversion */
if (iconv(cd, &in_ptr, &in_bytes_left, &out_ptr, &out_bytes_left) == (size_t)-1)
{
/* int err = errno; */
free(in_buf);
free(out_buf);
iconv_close(cd);
/* if (err == EILSEQ) return luaL_error(L, "Illegal sequence in source encoding"); */
return luaL_error(L, "Decoding failed");
}
lua_pushlstring(L, out_buf, out_ptr - out_buf);
free(in_buf);
free(out_buf);
iconv_close(cd);
return 1;
}
#endif
/* bool client.emulating() */
/* Returns true if emulator is paused, otherwise, false */
int client_emulating(lua_State *L)
{
runloop_state_t *runloop_st = runloop_state_get_ptr();
int r = (runloop_st->flags & RUNLOOP_FLAG_PAUSED) ? false : true;
lua_pushboolean(L, r);
return 1;
}
/* bool client.isturbo() */
/* Returns true if emulator is in turbo mode, otherwise, false */
int client_isturbo(lua_State *L)
{
runloop_state_t *runloop_st = runloop_state_get_ptr();
int r = (runloop_st->flags & RUNLOOP_FLAG_FASTMOTION);
lua_pushboolean(L, r);
return 1;
}
/* int client.screenheight() */
/* Gets the current height in pixels of the emulator's drawing area */
int client_screenheight(lua_State *L)
{
video_driver_state_t *video_st = video_state_get_ptr();
unsigned r = video_st->height;
lua_pushinteger(L, (lua_Integer)r);
return 1;
}
/* client.screenwidth() */
/* Gets the current height in pixels of the emulator's drawing area */
int client_screenwidth(lua_State *L)
{
video_driver_state_t *video_st = video_state_get_ptr();
unsigned r = video_st->width;
lua_pushinteger(L, (lua_Integer)r);
return 1;
}
/* int client.bufferheight() */
/* Gets the visible height of the emu display surface (the core video output). This excludes the gameExtraPadding you've set. */
int client_bufferheight(lua_State *L)
{
video_driver_state_t *video_st = video_state_get_ptr();
unsigned r = video_st->av_info.geometry.base_height;
/* ALT: unsigned r = video_st->frame_cache_height; */
lua_pushinteger(L, (lua_Integer)r);
return 1;
}
/* int client.bufferwidth() */
/* Gets the visible width of the emu display surface (the core video output). This excludes the gameExtraPadding you've set. */
int client_bufferwidth(lua_State *L)
{
video_driver_state_t *video_st = video_state_get_ptr();
unsigned r = video_st->av_info.geometry.base_width;
/* ALT: unsigned r = video_st->frame_cache_width; */
lua_pushinteger(L, (lua_Integer)r);
return 1;
}
/* string client.getversion() */
/* Returns the current stable Retroarch version */
int client_getversion(lua_State *L)
{
lua_pushstring(L, PACKAGE_VERSION);
return 1;
}
/* void client.pause() */
/* Pauses the emulator */
int client_pause(lua_State *L)
{
command_event(CMD_EVENT_PAUSE, NULL);
return 0;
}
/* void client.unpause() */
/* Unpauses the emulator */
int client_unpause(lua_State *L)
{
command_event(CMD_EVENT_UNPAUSE, NULL);
return 0;
}
/* void client.togglepause() */
/* Toggles the current pause state */
int client_togglepause(lua_State *L)
{
command_event(CMD_EVENT_PAUSE_TOGGLE, NULL);
return 0;
}
/* void client.exit() */
/* Closes the emulator */
int client_exit(lua_State *L)
{
command_event(CMD_EVENT_QUIT, NULL);
return 0;
}
/* void client.reboot_core() */
/* Reboots the currently loaded core */
int client_reboot_core(lua_State *L)
{
command_event(CMD_EVENT_RESET, NULL);
return 0;
}
/* void client.closerom() */
/* Closes the loaded Rom */
int client_closerom(lua_State *L)
{
command_event(CMD_EVENT_CLOSE_CONTENT, NULL);
return 0;
}
/* void client.screenshot([string path = nil]) */
/* TODO: allow passing path */
int client_screenshot(lua_State *L)
{
const char *path = luaL_optstring(L, 1, NULL); /* optional first argument, defaults to NULL */
if (!path)
command_event(CMD_EVENT_TAKE_SCREENSHOT, NULL);
else
{
check_sandboxed_path(L, path);
/*
#ifdef HAVE_SCREENSHOTS
else
take_screenshot(
const char *screenshot_dir,
const char *path, bool silence,
bool has_valid_framebuffer, bool fullpath, true);
#endif
*/
return luaL_error(L, "unsupported path arg");
}
return 0;
}
/* void client.sleep(int millis) */
/* sleeps for n milliseconds */
int client_sleep(lua_State *L)
{
lua_Integer ms = luaL_checkinteger(L, 1);
if (ms < 0)
return luaL_error(L, "emulation_sleep: time must be >= 0");
retro_sleep((uint64_t)ms);
return 0;
}
/* string client.get_lua_engine() */
/* returns the name of the Lua engine currently in use */
int client_get_lua_engine(lua_State *L)
{
lua_getglobal(L, "_VERSION");
/* lua_pushstring(L, LUA_RELEASE); */
return 1;
}
#define LUA_PUSH_STR(L, struct_ptr, field) \
lua_pushstring(L, #field); \
lua_pushstring(L, struct_ptr.field); \
lua_settable(L, -3)
#define LUA_PUSH_BOOL(L, st, field) \
lua_pushboolean(L, st.field); \
lua_setfield(L, -2, #field)
#define LUA_PUSH_NUMBER(L, st, field) \
lua_pushnumber(L, (double)st.field); \
lua_setfield(L, -2, #field)
/* object client.getconfig() */
/* gets the current config settings object */
int client_getconfig(lua_State *L)
{
settings_t *settings = config_get_ptr();
lua_newtable(L);
lua_pushinteger(L, settings->ints.state_slot);
lua_setfield(L, -2, "SaveSlot");
/*
// Set another field, e.g., PauseOnFrame = true
lua_pushboolean(L, 0); // false
lua_setfield(L, -2, "PauseOnFrame");
*/
/* TODO: add more bizhawk-compatible fields, should match the names used here https://github.com/TASEmulators/BizHawk/blob/master/src/BizHawk.Client.Common/config/Config.cs */
/* LibretroCore -> string like "mednafen_saturn_libretro.so" */
/* RecentRoms -> table */
/* RecentCores */
/* PreferredCores */
/* RecentLua */
/* PathEntries -> nested dict: client.getconfig().PathEntries["Paths"][0]["Type"] */
/* Retroarch-specific settings */
/* ints */
LUA_PUSH_NUMBER(L, settings->ints, netplay_check_frames);
LUA_PUSH_NUMBER(L, settings->ints, location_update_interval_ms);
LUA_PUSH_NUMBER(L, settings->ints, location_update_interval_distance);
LUA_PUSH_NUMBER(L, settings->ints, state_slot);
LUA_PUSH_NUMBER(L, settings->ints, replay_slot);
LUA_PUSH_NUMBER(L, settings->ints, crt_switch_center_adjust);
LUA_PUSH_NUMBER(L, settings->ints, crt_switch_porch_adjust);
LUA_PUSH_NUMBER(L, settings->ints, crt_switch_vertical_adjust);
LUA_PUSH_NUMBER(L, settings->ints, video_max_frame_latency);
#ifdef HAVE_VULKAN
LUA_PUSH_NUMBER(L, settings->ints, vulkan_gpu_index);
#endif
#ifdef HAVE_D3D10
LUA_PUSH_NUMBER(L, settings->ints, d3d10_gpu_index);
#endif
#ifdef HAVE_D3D11
LUA_PUSH_NUMBER(L, settings->ints, d3d11_gpu_index);
#endif
#ifdef HAVE_D3D12
LUA_PUSH_NUMBER(L, settings->ints, d3d12_gpu_index);
#endif
#ifdef HAVE_WINDOW_OFFSET
LUA_PUSH_NUMBER(L, settings->ints, video_window_offset_x);
LUA_PUSH_NUMBER(L, settings->ints, video_window_offset_y);
#endif
LUA_PUSH_NUMBER(L, settings->ints, content_favorites_size);
#ifdef _3DS
LUA_PUSH_NUMBER(L, settings->ints, bottom_font_color_red);
LUA_PUSH_NUMBER(L, settings->ints, bottom_font_color_green);
LUA_PUSH_NUMBER(L, settings->ints, bottom_font_color_blue);
LUA_PUSH_NUMBER(L, settings->ints, bottom_font_color_opacity);
#endif
#ifdef HAVE_XMB
LUA_PUSH_NUMBER(L, settings->ints, menu_xmb_title_margin);
LUA_PUSH_NUMBER(L, settings->ints, menu_xmb_title_margin_horizontal_offset);
#endif
#ifdef HAVE_OVERLAY
LUA_PUSH_NUMBER(L, settings->ints, input_overlay_lightgun_port);
#endif
LUA_PUSH_NUMBER(L, settings->ints, input_turbo_bind);
/* uints */
/* LUA_PUSH_NUMBER(L, settings->uints, input_split_joycon[MAX_USERS]); */
/* LUA_PUSH_NUMBER(L, settings->uints, input_joypad_index[MAX_USERS]); */
/* LUA_PUSH_NUMBER(L, settings->uints, input_device[MAX_USERS]); */
/* LUA_PUSH_NUMBER(L, settings->uints, input_mouse_index[MAX_USERS]); */
/* LUA_PUSH_NUMBER(L, settings->uints, input_libretro_device[MAX_USERS]); */
/* LUA_PUSH_NUMBER(L, settings->uints, input_analog_dpad_mode[MAX_USERS]); */
/* LUA_PUSH_NUMBER(L, settings->uints, input_device_reservation_type[MAX_USERS]); */
/* LUA_PUSH_NUMBER(L, settings->uints, input_remap_ports[MAX_USERS]); */
/* LUA_PUSH_NUMBER(L, settings->uints, input_remap_ids[MAX_USERS][RARCH_CUSTOM_BIND_LIST_END]); */
/* LUA_PUSH_NUMBER(L, settings->uints, input_keymapper_ids[MAX_USERS][RARCH_CUSTOM_BIND_LIST_END]); */
/* LUA_PUSH_NUMBER(L, settings->uints, input_remap_port_map[MAX_USERS][MAX_USERS + 1]); */
/* LUA_PUSH_NUMBER(L, settings->uints, led_map[MAX_LEDS]); */
LUA_PUSH_NUMBER(L, settings->uints, audio_output_sample_rate);
LUA_PUSH_NUMBER(L, settings->uints, audio_block_frames);
LUA_PUSH_NUMBER(L, settings->uints, audio_latency);
#ifdef HAVE_WASAPI
LUA_PUSH_NUMBER(L, settings->uints, audio_wasapi_sh_buffer_length);
#endif
#ifdef HAVE_MICROPHONE
LUA_PUSH_NUMBER(L, settings->uints, microphone_sample_rate);
LUA_PUSH_NUMBER(L, settings->uints, microphone_block_frames);
LUA_PUSH_NUMBER(L, settings->uints, microphone_latency);
LUA_PUSH_NUMBER(L, settings->uints, microphone_resampler_quality);
#ifdef HAVE_WASAPI
LUA_PUSH_NUMBER(L, settings->uints, microphone_wasapi_sh_buffer_length);
#endif
#endif
LUA_PUSH_NUMBER(L, settings->uints, fps_update_interval);
LUA_PUSH_NUMBER(L, settings->uints, memory_update_interval);
LUA_PUSH_NUMBER(L, settings->uints, input_block_timeout);
LUA_PUSH_NUMBER(L, settings->uints, audio_resampler_quality);
LUA_PUSH_NUMBER(L, settings->uints, input_turbo_period);
LUA_PUSH_NUMBER(L, settings->uints, input_turbo_duty_cycle);
LUA_PUSH_NUMBER(L, settings->uints, input_turbo_mode);
LUA_PUSH_NUMBER(L, settings->uints, input_turbo_button);
LUA_PUSH_NUMBER(L, settings->uints, input_bind_timeout);
LUA_PUSH_NUMBER(L, settings->uints, input_bind_hold);
#ifdef GEKKO
LUA_PUSH_NUMBER(L, settings->uints, input_mouse_scale);
#endif
LUA_PUSH_NUMBER(L, settings->uints, input_touch_scale);
LUA_PUSH_NUMBER(L, settings->uints, input_hotkey_block_delay);
LUA_PUSH_NUMBER(L, settings->uints, input_quit_gamepad_combo);
LUA_PUSH_NUMBER(L, settings->uints, input_menu_toggle_gamepad_combo);
LUA_PUSH_NUMBER(L, settings->uints, input_keyboard_gamepad_mapping_type);
LUA_PUSH_NUMBER(L, settings->uints, input_poll_type_behavior);
LUA_PUSH_NUMBER(L, settings->uints, input_rumble_gain);
LUA_PUSH_NUMBER(L, settings->uints, input_auto_game_focus);
LUA_PUSH_NUMBER(L, settings->uints, input_max_users);
LUA_PUSH_NUMBER(L, settings->uints, netplay_port);
LUA_PUSH_NUMBER(L, settings->uints, netplay_max_connections);
LUA_PUSH_NUMBER(L, settings->uints, netplay_max_ping);
LUA_PUSH_NUMBER(L, settings->uints, netplay_chat_color_name);
LUA_PUSH_NUMBER(L, settings->uints, netplay_chat_color_msg);
LUA_PUSH_NUMBER(L, settings->uints, netplay_input_latency_frames_min);
LUA_PUSH_NUMBER(L, settings->uints, netplay_input_latency_frames_range);
LUA_PUSH_NUMBER(L, settings->uints, netplay_share_digital);
LUA_PUSH_NUMBER(L, settings->uints, netplay_share_analog);
LUA_PUSH_NUMBER(L, settings->uints, bundle_assets_extract_version_current);
LUA_PUSH_NUMBER(L, settings->uints, bundle_assets_extract_last_version);
LUA_PUSH_NUMBER(L, settings->uints, content_history_size);
LUA_PUSH_NUMBER(L, settings->uints, frontend_log_level);
LUA_PUSH_NUMBER(L, settings->uints, libretro_log_level);
LUA_PUSH_NUMBER(L, settings->uints, rewind_granularity);
LUA_PUSH_NUMBER(L, settings->uints, rewind_buffer_size_step);
LUA_PUSH_NUMBER(L, settings->uints, autosave_interval);
LUA_PUSH_NUMBER(L, settings->uints, replay_checkpoint_interval);
LUA_PUSH_NUMBER(L, settings->uints, replay_max_keep);
LUA_PUSH_NUMBER(L, settings->uints, savestate_max_keep);
LUA_PUSH_NUMBER(L, settings->uints, network_cmd_port);
LUA_PUSH_NUMBER(L, settings->uints, network_remote_base_port);
LUA_PUSH_NUMBER(L, settings->uints, keymapper_port);
LUA_PUSH_NUMBER(L, settings->uints, video_window_opacity);
LUA_PUSH_NUMBER(L, settings->uints, crt_switch_resolution);
LUA_PUSH_NUMBER(L, settings->uints, crt_switch_resolution_super);
LUA_PUSH_NUMBER(L, settings->uints, screen_brightness);
LUA_PUSH_NUMBER(L, settings->uints, video_monitor_index);
LUA_PUSH_NUMBER(L, settings->uints, video_fullscreen_x);
LUA_PUSH_NUMBER(L, settings->uints, video_fullscreen_y);
LUA_PUSH_NUMBER(L, settings->uints, video_scale);
LUA_PUSH_NUMBER(L, settings->uints, video_scale_integer_axis);
LUA_PUSH_NUMBER(L, settings->uints, video_scale_integer_scaling);
LUA_PUSH_NUMBER(L, settings->uints, video_max_swapchain_images);
LUA_PUSH_NUMBER(L, settings->uints, video_swap_interval);
LUA_PUSH_NUMBER(L, settings->uints, video_hard_sync_frames);
LUA_PUSH_NUMBER(L, settings->uints, video_frame_delay);
LUA_PUSH_NUMBER(L, settings->uints, video_viwidth);
LUA_PUSH_NUMBER(L, settings->uints, video_aspect_ratio_idx);
LUA_PUSH_NUMBER(L, settings->uints, video_rotation);
LUA_PUSH_NUMBER(L, settings->uints, screen_orientation);
LUA_PUSH_NUMBER(L, settings->uints, video_msg_bgcolor_red);
LUA_PUSH_NUMBER(L, settings->uints, video_msg_bgcolor_green);
LUA_PUSH_NUMBER(L, settings->uints, video_msg_bgcolor_blue);
LUA_PUSH_NUMBER(L, settings->uints, video_stream_port);
LUA_PUSH_NUMBER(L, settings->uints, video_record_quality);
LUA_PUSH_NUMBER(L, settings->uints, video_stream_quality);
LUA_PUSH_NUMBER(L, settings->uints, video_record_scale_factor);
LUA_PUSH_NUMBER(L, settings->uints, video_stream_scale_factor);
LUA_PUSH_NUMBER(L, settings->uints, video_3ds_display_mode);
LUA_PUSH_NUMBER(L, settings->uints, video_dingux_ipu_filter_type);
LUA_PUSH_NUMBER(L, settings->uints, video_dingux_refresh_rate);
LUA_PUSH_NUMBER(L, settings->uints, video_dingux_rs90_softfilter_type);
#ifdef GEKKO
LUA_PUSH_NUMBER(L, settings->uints, video_overscan_correction_top);
LUA_PUSH_NUMBER(L, settings->uints, video_overscan_correction_bottom);
#endif
LUA_PUSH_NUMBER(L, settings->uints, video_shader_delay);
#ifdef HAVE_SCREENSHOTS
LUA_PUSH_NUMBER(L, settings->uints, notification_show_screenshot_duration);
LUA_PUSH_NUMBER(L, settings->uints, notification_show_screenshot_flash);
#endif
LUA_PUSH_NUMBER(L, settings->uints, accessibility_narrator_speech_speed);
LUA_PUSH_NUMBER(L, settings->uints, menu_timedate_style);
LUA_PUSH_NUMBER(L, settings->uints, menu_timedate_date_separator);
LUA_PUSH_NUMBER(L, settings->uints, gfx_thumbnails);
LUA_PUSH_NUMBER(L, settings->uints, menu_left_thumbnails);
LUA_PUSH_NUMBER(L, settings->uints, menu_icon_thumbnails);
LUA_PUSH_NUMBER(L, settings->uints, gfx_thumbnail_upscale_threshold);
LUA_PUSH_NUMBER(L, settings->uints, menu_rgui_thumbnail_downscaler);
LUA_PUSH_NUMBER(L, settings->uints, menu_rgui_thumbnail_delay);
LUA_PUSH_NUMBER(L, settings->uints, menu_rgui_color_theme);
LUA_PUSH_NUMBER(L, settings->uints, menu_xmb_animation_opening_main_menu);
LUA_PUSH_NUMBER(L, settings->uints, menu_xmb_animation_horizontal_highlight);
LUA_PUSH_NUMBER(L, settings->uints, menu_xmb_animation_move_up_down);
LUA_PUSH_NUMBER(L, settings->uints, menu_xmb_layout);
LUA_PUSH_NUMBER(L, settings->uints, menu_xmb_shader_pipeline);
LUA_PUSH_NUMBER(L, settings->uints, menu_xmb_alpha_factor);
LUA_PUSH_NUMBER(L, settings->uints, menu_xmb_theme);
LUA_PUSH_NUMBER(L, settings->uints, menu_xmb_color_theme);
LUA_PUSH_NUMBER(L, settings->uints, menu_xmb_thumbnail_scale_factor);
LUA_PUSH_NUMBER(L, settings->uints, menu_xmb_vertical_fade_factor);
LUA_PUSH_NUMBER(L, settings->uints, menu_materialui_color_theme);
LUA_PUSH_NUMBER(L, settings->uints, menu_materialui_transition_animation);
LUA_PUSH_NUMBER(L, settings->uints, menu_materialui_thumbnail_view_portrait);
LUA_PUSH_NUMBER(L, settings->uints, menu_materialui_thumbnail_view_landscape);
LUA_PUSH_NUMBER(L, settings->uints, menu_materialui_landscape_layout_optimization);
LUA_PUSH_NUMBER(L, settings->uints, menu_ozone_color_theme);
LUA_PUSH_NUMBER(L, settings->uints, menu_ozone_header_separator);
LUA_PUSH_NUMBER(L, settings->uints, menu_ozone_font_scale);
LUA_PUSH_NUMBER(L, settings->uints, menu_font_color_red);
LUA_PUSH_NUMBER(L, settings->uints, menu_font_color_green);
LUA_PUSH_NUMBER(L, settings->uints, menu_font_color_blue);
LUA_PUSH_NUMBER(L, settings->uints, menu_rgui_internal_upscale_level);
LUA_PUSH_NUMBER(L, settings->uints, menu_rgui_aspect_ratio);
LUA_PUSH_NUMBER(L, settings->uints, menu_rgui_aspect_ratio_lock);
LUA_PUSH_NUMBER(L, settings->uints, menu_rgui_particle_effect);
LUA_PUSH_NUMBER(L, settings->uints, menu_ticker_type);
LUA_PUSH_NUMBER(L, settings->uints, menu_scroll_delay);
LUA_PUSH_NUMBER(L, settings->uints, menu_content_show_add_entry);
LUA_PUSH_NUMBER(L, settings->uints, menu_content_show_contentless_cores);
LUA_PUSH_NUMBER(L, settings->uints, menu_screensaver_timeout);
LUA_PUSH_NUMBER(L, settings->uints, menu_screensaver_animation);
LUA_PUSH_NUMBER(L, settings->uints, menu_remember_selection);
LUA_PUSH_NUMBER(L, settings->uints, menu_startup_page);
LUA_PUSH_NUMBER(L, settings->uints, playlist_entry_remove_enable);
LUA_PUSH_NUMBER(L, settings->uints, playlist_show_inline_core_name);
LUA_PUSH_NUMBER(L, settings->uints, playlist_show_history_icons);
LUA_PUSH_NUMBER(L, settings->uints, playlist_sublabel_runtime_type);
LUA_PUSH_NUMBER(L, settings->uints, playlist_sublabel_last_played_style);
LUA_PUSH_NUMBER(L, settings->uints, camera_width);
LUA_PUSH_NUMBER(L, settings->uints, camera_height);
#ifdef HAVE_OVERLAY
LUA_PUSH_NUMBER(L, settings->uints, input_overlay_show_inputs);
LUA_PUSH_NUMBER(L, settings->uints, input_overlay_show_inputs_port);
LUA_PUSH_NUMBER(L, settings->uints, input_overlay_dpad_diagonal_sensitivity);
LUA_PUSH_NUMBER(L, settings->uints, input_overlay_abxy_diagonal_sensitivity);
LUA_PUSH_NUMBER(L, settings->uints, input_overlay_analog_recenter_zone);
LUA_PUSH_NUMBER(L, settings->uints, input_overlay_lightgun_trigger_delay);
LUA_PUSH_NUMBER(L, settings->uints, input_overlay_lightgun_two_touch_input);
LUA_PUSH_NUMBER(L, settings->uints, input_overlay_lightgun_three_touch_input);
LUA_PUSH_NUMBER(L, settings->uints, input_overlay_lightgun_four_touch_input);
LUA_PUSH_NUMBER(L, settings->uints, input_overlay_mouse_hold_msec);
LUA_PUSH_NUMBER(L, settings->uints, input_overlay_mouse_dtap_msec);
#endif
LUA_PUSH_NUMBER(L, settings->uints, run_ahead_frames);
LUA_PUSH_NUMBER(L, settings->uints, midi_volume);
LUA_PUSH_NUMBER(L, settings->uints, streaming_mode);
LUA_PUSH_NUMBER(L, settings->uints, window_position_x);
LUA_PUSH_NUMBER(L, settings->uints, window_position_y);
LUA_PUSH_NUMBER(L, settings->uints, window_position_width);
LUA_PUSH_NUMBER(L, settings->uints, window_position_height);
LUA_PUSH_NUMBER(L, settings->uints, window_auto_width_max);
LUA_PUSH_NUMBER(L, settings->uints, window_auto_height_max);
LUA_PUSH_NUMBER(L, settings->uints, video_record_threads);
LUA_PUSH_NUMBER(L, settings->uints, libnx_overclock);
LUA_PUSH_NUMBER(L, settings->uints, ai_service_mode);
LUA_PUSH_NUMBER(L, settings->uints, ai_service_target_lang);
LUA_PUSH_NUMBER(L, settings->uints, ai_service_source_lang);
LUA_PUSH_NUMBER(L, settings->uints, core_updater_auto_backup_history_size);
LUA_PUSH_NUMBER(L, settings->uints, video_black_frame_insertion);
LUA_PUSH_NUMBER(L, settings->uints, video_bfi_dark_frames);
LUA_PUSH_NUMBER(L, settings->uints, video_shader_subframes);
LUA_PUSH_NUMBER(L, settings->uints, video_autoswitch_refresh_rate);
LUA_PUSH_NUMBER(L, settings->uints, quit_on_close_content);
#ifdef HAVE_LAKKA
LUA_PUSH_NUMBER(L, settings->uints, cpu_scaling_mode);
LUA_PUSH_NUMBER(L, settings->uints, cpu_min_freq);
LUA_PUSH_NUMBER(L, settings->uints, cpu_max_freq);
#endif
#ifdef HAVE_MIST
LUA_PUSH_NUMBER(L, settings->uints, steam_rich_presence_format);
#endif
LUA_PUSH_NUMBER(L, settings->uints, cheevos_appearance_anchor);
LUA_PUSH_NUMBER(L, settings->uints, cheevos_visibility_summary);
/* floats */
LUA_PUSH_NUMBER(L, settings->floats, video_aspect_ratio);
LUA_PUSH_NUMBER(L, settings->floats, video_vp_bias_x);
LUA_PUSH_NUMBER(L, settings->floats, video_vp_bias_y);
#if defined(RARCH_MOBILE)
LUA_PUSH_NUMBER(L, settings->floats, video_vp_bias_portrait_x);
LUA_PUSH_NUMBER(L, settings->floats, video_vp_bias_portrait_y);
#endif
LUA_PUSH_NUMBER(L, settings->floats, video_refresh_rate);
LUA_PUSH_NUMBER(L, settings->floats, video_autoswitch_pal_threshold);
LUA_PUSH_NUMBER(L, settings->floats, crt_video_refresh_rate);
LUA_PUSH_NUMBER(L, settings->floats, video_font_size);
LUA_PUSH_NUMBER(L, settings->floats, video_msg_pos_x);
LUA_PUSH_NUMBER(L, settings->floats, video_msg_pos_y);
LUA_PUSH_NUMBER(L, settings->floats, video_msg_color_r);
LUA_PUSH_NUMBER(L, settings->floats, video_msg_color_g);
LUA_PUSH_NUMBER(L, settings->floats, video_msg_color_b);
LUA_PUSH_NUMBER(L, settings->floats, video_msg_bgcolor_opacity);
LUA_PUSH_NUMBER(L, settings->floats, video_hdr_max_nits);
LUA_PUSH_NUMBER(L, settings->floats, video_hdr_paper_white_nits);
LUA_PUSH_NUMBER(L, settings->floats, video_hdr_display_contrast);
LUA_PUSH_NUMBER(L, settings->floats, menu_scale_factor);
LUA_PUSH_NUMBER(L, settings->floats, menu_widget_scale_factor);
LUA_PUSH_NUMBER(L, settings->floats, menu_widget_scale_factor_windowed);
LUA_PUSH_NUMBER(L, settings->floats, menu_wallpaper_opacity);
LUA_PUSH_NUMBER(L, settings->floats, menu_framebuffer_opacity);
LUA_PUSH_NUMBER(L, settings->floats, menu_footer_opacity);
LUA_PUSH_NUMBER(L, settings->floats, menu_header_opacity);
LUA_PUSH_NUMBER(L, settings->floats, menu_ticker_speed);
LUA_PUSH_NUMBER(L, settings->floats, menu_rgui_particle_effect_speed);
LUA_PUSH_NUMBER(L, settings->floats, menu_screensaver_animation_speed);