-
Notifications
You must be signed in to change notification settings - Fork 0
/
rc.cpp
1235 lines (1164 loc) · 35.1 KB
/
rc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This parses the RC file.
#ifdef __MINGW32__
#undef __STRICT_ANSI__
#endif
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <assert.h>
#include <strings.h>
#include <ctype.h>
#include "rc.h"
#include "ckvp.h"
#include "pd-defs.h"
#include "romload.h"
#include "system.h"
#include "md.h"
// CTV names
const char *ctv_names[] = {
"off", "blur", "scanline", "interlace", "swab",
NULL
};
// Scaling algorithms names
const char *scaling_names[] = {
"stretch",
"scale",
"hqx",
"hqx stretch",
"scale2x",
"scale2x stretch",
"none",
NULL
};
// CPU names, keep index in sync with rc-vars.h and enums in md.h
const char *emu_z80_names[] = { "none", "mz80", "cz80", "drz80", NULL };
const char *emu_m68k_names[] = { "none", "star", "musa", "cyclone", NULL };
// The table of strings and the keysyms they map to.
// The order is a bit weird, since this was originally a mapping for the SVGALib
// scancodes, and I just added the SDL stuff on top of it.
struct rc_keysym rc_keysyms[] = {
{ "ESCAPE", PDK_ESCAPE },
{ "BACKSPACE", PDK_BACKSPACE },
{ "TAB", PDK_TAB },
{ "RETURN", PDK_RETURN },
{ "ENTER", PDK_RETURN },
{ "KP_MULTIPLY", PDK_KP_MULTIPLY },
{ "SPACE", PDK_SPACE },
{ "F1", PDK_F1 },
{ "F2", PDK_F2 },
{ "F3", PDK_F3 },
{ "F4", PDK_F4 },
{ "F5", PDK_F5 },
{ "F6", PDK_F6 },
{ "F7", PDK_F7 },
{ "F8", PDK_F8 },
{ "F9", PDK_F9 },
{ "F10", PDK_F10 },
{ "KP_7", PDK_KP7 },
{ "KP_HOME", PDK_KP7 },
{ "KP_8", PDK_KP8 },
{ "KP_UP", PDK_KP8 },
{ "KP_9", PDK_KP9 },
{ "KP_PAGE_UP", PDK_KP9 },
{ "KP_PAGEUP", PDK_KP9 },
{ "KP_MINUS", PDK_KP_MINUS },
{ "KP_4", PDK_KP4 },
{ "KP_LEFT", PDK_KP4 },
{ "KP_5", PDK_KP5 },
{ "KP_6", PDK_KP6 },
{ "KP_RIGHT", PDK_KP6 },
{ "KP_PLUS", PDK_KP_PLUS },
{ "KP_1", PDK_KP1 },
{ "KP_END", PDK_KP1 },
{ "KP_2", PDK_KP2 },
{ "KP_DOWN", PDK_KP2 },
{ "KP_3", PDK_KP3 },
{ "KP_PAGE_DOWN", PDK_KP3 },
{ "KP_PAGEDOWN", PDK_KP3 },
{ "KP_0", PDK_KP0 },
{ "KP_INSERT", PDK_KP0 },
{ "KP_PERIOD", PDK_KP_PERIOD },
{ "KP_DELETE", PDK_KP_PERIOD },
{ "F11", PDK_F11 },
{ "F12", PDK_F12 },
{ "KP_ENTER", PDK_KP_ENTER },
{ "KP_DIVIDE", PDK_KP_DIVIDE },
{ "HOME", PDK_HOME },
{ "UP", PDK_UP },
{ "PAGE_UP", PDK_PAGEUP },
{ "PAGEUP", PDK_PAGEUP },
{ "LEFT", PDK_LEFT },
{ "RIGHT", PDK_RIGHT },
{ "END", PDK_END },
{ "DOWN", PDK_DOWN },
{ "PAGE_DOWN", PDK_PAGEDOWN },
{ "PAGEDOWN", PDK_PAGEDOWN },
{ "INSERT", PDK_INSERT },
{ "DELETE", PDK_DELETE },
{ "NUMLOCK", PDK_NUMLOCK },
{ "NUM_LOCK", PDK_NUMLOCK },
{ "CAPSLOCK", PDK_CAPSLOCK },
{ "CAPS_LOCK", PDK_CAPSLOCK },
{ "SCROLLOCK", PDK_SCROLLOCK },
{ "SCROLL_LOCK", PDK_SCROLLOCK },
{ "LSHIFT", PDK_LSHIFT },
{ "SHIFT_L", PDK_LSHIFT },
{ "RSHIFT", PDK_RSHIFT },
{ "SHIFT_R", PDK_RSHIFT },
{ "LCTRL", PDK_LCTRL },
{ "CTRL_L", PDK_LCTRL },
{ "RCTRL", PDK_RCTRL },
{ "CTRL_R", PDK_RCTRL },
{ "LALT", PDK_LALT },
{ "ALT_L", PDK_LALT },
{ "RALT", PDK_RALT },
{ "ALT_R", PDK_RALT },
{ "LMETA", PDK_LMETA },
{ "META_L", PDK_LMETA },
{ "RMETA", PDK_RMETA },
{ "META_R", PDK_RMETA },
{ "", 0 },
{ NULL, 0 } // Terminator
}; // Phew! ;)
/* Define all the external RC variables */
#include "rc-vars.h"
static const struct {
const char *name;
uint32_t flag;
} keymod[] = {
{ "shift-", KEYSYM_MOD_SHIFT },
{ "ctrl-", KEYSYM_MOD_CTRL },
{ "alt-", KEYSYM_MOD_ALT },
{ "meta-", KEYSYM_MOD_META },
{ "", 0 }
};
/* Parse a keysym.
* If the string matches one of the strings in the keysym table above
* or if it's another valid character, return the keysym, otherwise -1. */
intptr_t rc_keysym(const char *code, intptr_t *)
{
struct rc_keysym *ks = rc_keysyms;
uint32_t m = 0;
uint32_t c;
while (*code != '\0') {
size_t i;
for (i = 0; (keymod[i].name[0] != '\0'); ++i) {
size_t l = strlen(keymod[i].name);
if (strncasecmp(keymod[i].name, code, l))
continue;
m |= keymod[i].flag;
code += l;
break;
}
if (keymod[i].name[0] == '\0')
break;
}
while (ks->name != NULL) {
if (!strcasecmp(ks->name, code))
return (m | ks->keysym);
++ks;
}
/* Not in the list, so expect a single UTF-8 character instead. */
code += utf8u32(&c, (const uint8_t *)code);
if ((c == (uint32_t)-1) || (*code != '\0'))
return -1;
/* Must fit in 16 bits. */
if (c > 0xffff)
return -1;
return (m | (c & 0xffff));
}
/* Convert a keysym to text. */
char *dump_keysym(intptr_t k)
{
char buf[64];
size_t i;
size_t n;
size_t l = 0;
struct rc_keysym *ks = rc_keysyms;
buf[0] = '\0';
for (i = 0; ((keymod[i].name[0] != '\0') && (l < sizeof(buf))); ++i)
if (k & keymod[i].flag) {
n = strlen(keymod[i].name);
if (n > (sizeof(buf) - l))
n = (sizeof(buf) - l);
memcpy(&buf[l], keymod[i].name, n);
l += n;
}
k &= ~KEYSYM_MOD_MASK;
while (ks->name != NULL) {
if (ks->keysym == k) {
n = strlen(ks->name);
if (n > (sizeof(buf) - l))
n = (sizeof(buf) - l);
memcpy(&buf[l], ks->name, n);
l += n;
goto found;
}
++ks;
}
n = utf32u8(NULL, k);
if ((n == 0) || (n > (sizeof(buf) - l)))
return NULL;
utf32u8((uint8_t *)&buf[l], k);
l += n;
found:
return backslashify((uint8_t *)buf, l, 0, NULL);
}
/* Parse a boolean value.
* If the string is "yes" or "true", return 1.
* If the string is "no" or "false", return 0.
* Otherwise, just return atoi(value). */
intptr_t rc_boolean(const char *value, intptr_t *)
{
if(!strcasecmp(value, "yes") || !strcasecmp(value, "true"))
return 1;
if(!strcasecmp(value, "no") || !strcasecmp(value, "false"))
return 0;
return atoi(value);
}
static const char *joypad_axis_type[] = {
"max", "p", "positive",
"min", "n", "negative",
"between", "b", NULL
};
static const unsigned int joypad_axis_value[] = {
JS_AXIS_POSITIVE, JS_AXIS_POSITIVE, JS_AXIS_POSITIVE,
JS_AXIS_NEGATIVE, JS_AXIS_NEGATIVE, JS_AXIS_NEGATIVE,
JS_AXIS_BETWEEN, JS_AXIS_BETWEEN
};
static const char *joypad_hat_type[] = {
"centered", "up", "right", "down", "left"
};
static const unsigned int joypad_hat_value[] = {
JS_HAT_CENTERED, JS_HAT_UP, JS_HAT_RIGHT, JS_HAT_DOWN, JS_HAT_LEFT
};
/* Convert a joypad entry to text. */
char *dump_joypad(intptr_t js)
{
char *str = NULL;
unsigned int id = JS_GET_IDENTIFIER(js);
const char *arg0 = NULL;
unsigned int val0 = 0;
const char *arg1 = NULL;
unsigned int i;
if (JS_IS_BUTTON(js)) {
arg0 = "button";
val0 = JS_GET_BUTTON(js);
}
else if (JS_IS_AXIS(js)) {
arg0 = "axis";
val0 = JS_GET_AXIS(js);
for (i = 0; (i != elemof(joypad_axis_value)); ++i)
if (joypad_axis_value[i] == JS_GET_AXIS_DIR(js)) {
arg1 = joypad_axis_type[i];
break;
}
if (arg1 == NULL)
return NULL;
}
else if (JS_IS_HAT(js)) {
arg0 = "hat";
val0 = JS_GET_HAT(js);
for (i = 0; (i != elemof(joypad_hat_value)); ++i)
if (joypad_hat_value[i] == JS_GET_HAT_DIR(js)) {
arg1 = joypad_hat_type[i];
break;
}
if (arg1 == NULL)
return NULL;
}
else
return NULL;
i = 0;
while (1) {
i = snprintf(str, i, "joystick%u-%s%u%s%s",
id, arg0, val0,
(arg1 ? "-" : ""), (arg1 ? arg1 : ""));
if ((str != NULL) ||
((str = (char *)malloc(++i)) == NULL))
break;
}
return str;
}
/*
* Parse a joystick/joypad command (joy_*). The following syntaxes are
* supported:
*
* Normal buttons:
* (j|js|joystick|joypad)X-(b|button)Y
* Axes:
* (j|js|joystick|joypad)X-(a|axis)Y-(max|p|positive|min|n|negative)
* Hats:
* (j|js|joystick|joypad)X-(h|hat)Y-(up|right|down|left)
*/
intptr_t rc_joypad(const char *value, intptr_t *)
{
static const char *r_js[] = { "j", "js", "joystick", "joypad", NULL };
static const char *r_button[] = { "b", "button", NULL };
static const char *r_axis[] = { "a", "axis", NULL };
static const char **r_axis_type = joypad_axis_type;
static const unsigned int *r_axis_value = joypad_axis_value;
static const char *r_hat[] = { "h", "hat", NULL };
static const char **r_hat_type = joypad_hat_type;
static const unsigned int *r_hat_value = joypad_hat_value;
int i;
unsigned int id;
unsigned int arg;
if (*value == '\0')
return 0;
if ((i = prefix_casematch(value, r_js)) == -1)
return -1;
value += strlen(r_js[i]);
if ((i = prefix_getuint(value, &id)) == 0)
return -1;
value += i;
if (*value != '-')
return -1;
++value;
if ((i = prefix_casematch(value, r_button)) != -1) {
value += strlen(r_button[i]);
if ((i = prefix_getuint(value, &arg)) == 0)
return -1;
value += i;
if (*value != '\0')
return -1;
return JS_BUTTON(id, arg);
}
if ((i = prefix_casematch(value, r_axis)) != -1) {
value += strlen(r_axis[i]);
if ((i = prefix_getuint(value, &arg)) == 0)
return -1;
value += i;
if (*value != '-')
return -1;
++value;
if ((i = prefix_casematch(value, r_axis_type)) == -1)
return -1;
value += strlen(r_axis_type[i]);
if (*value != '\0')
return -1;
return JS_AXIS(id, arg, r_axis_value[i]);
}
if ((i = prefix_casematch(value, r_hat)) != -1) {
value += strlen(r_hat[i]);
if ((i = prefix_getuint(value, &arg)) == 0)
return -1;
value += i;
if (*value != '-')
return -1;
++value;
if ((i = prefix_casematch(value, r_hat_type)) == -1)
return -1;
value += strlen(r_hat_type[i]);
if (*value != '\0')
return -1;
return JS_HAT(id, arg, r_hat_value[i]);
}
return -1;
}
static const char *mouse_motion_type[] = {
"up", "u",
"down", "d",
"left", "l",
"right", "r",
NULL
};
/* Convert a mouse entry to text. */
char *dump_mouse(intptr_t mo)
{
char *str = NULL;
unsigned int id = MO_GET_IDENTIFIER(mo);
const char *arg0 = NULL;
unsigned int val0 = 0;
unsigned int i;
if (MO_IS_BUTTON(mo)) {
arg0 = "button";
val0 = MO_GET_BUTTON(mo);
}
else if (MO_IS_MOTION(mo)) {
val0 = MO_GET_MOTION(mo);
for (i = 0; (i != elemof(mouse_motion_type)); ++i)
if (mouse_motion_type[i][0] == (char)val0) {
arg0 = mouse_motion_type[i];
break;
}
if (arg0 == NULL)
return NULL;
}
else
return NULL;
i = 0;
while (1) {
if (MO_IS_BUTTON(mo))
i = snprintf(str, i, "mouse%u-%s%u", id, arg0, val0);
else
i = snprintf(str, i, "mouse%u-%s", id, arg0);
if ((str != NULL) ||
((str = (char *)malloc(++i)) == NULL))
break;
}
return str;
}
/*
* Parse a mouse command (mou_*). The following syntaxes are
* supported:
*
* Buttons:
* (m|mo|mouse)X-(b|button)Y
* Motions:
* (m|mo|mouse)X-(u|up|d|down|l|left|r|right)
*/
intptr_t rc_mouse(const char *value, intptr_t *)
{
static const char *r_mo[] = { "m", "mo", "mouse", NULL };
static const char *r_button[] = { "b", "button", NULL };
static const char **r_motion_type = mouse_motion_type;
int i;
unsigned int id;
unsigned int arg;
if (*value == '\0')
return 0;
if ((i = prefix_casematch(value, r_mo)) == -1)
return -1;
value += strlen(r_mo[i]);
if ((i = prefix_getuint(value, &id)) == 0)
return -1;
value += i;
if (*value != '-')
return -1;
++value;
if ((i = prefix_casematch(value, r_button)) != -1) {
value += strlen(r_button[i]);
if ((i = prefix_getuint(value, &arg)) == 0)
return -1;
value += i;
if (*value != '\0')
return -1;
return MO_BUTTON(id, arg);
}
if ((i = prefix_casematch(value, r_motion_type)) != -1) {
arg = r_motion_type[i][0];
value += strlen(r_motion_type[i]);
if (*value != '\0')
return -1;
return MO_MOTION(id, arg);
}
return -1;
}
/* Parse the CTV type. As new CTV filters get submitted expect this to grow ;)
* Current values are:
* off - No CTV
* blur - blur bitmap (from DirectX DGen), by Dave <[email protected]>
* scanline - attenuates every other line, looks cool! by Phillip K. Hornung <[email protected]>
*/
intptr_t rc_ctv(const char *value, intptr_t *)
{
for(int i = 0; i < NUM_CTV; ++i)
if(!strcasecmp(value, ctv_names[i])) return i;
return -1;
}
intptr_t rc_scaling(const char *value, intptr_t *)
{
int i;
for (i = 0; (i < NUM_SCALING); ++i)
if (!strcasecmp(value, scaling_names[i]))
return i;
return -1;
}
/* Parse CPU types */
intptr_t rc_emu_z80(const char *value, intptr_t *)
{
unsigned int i;
for (i = 0; (emu_z80_names[i] != NULL); ++i)
if (!strcasecmp(value, emu_z80_names[i]))
return i;
return -1;
}
intptr_t rc_emu_m68k(const char *value, intptr_t *)
{
unsigned int i;
for (i = 0; (emu_m68k_names[i] != NULL); ++i)
if (!strcasecmp(value, emu_m68k_names[i]))
return i;
return -1;
}
intptr_t rc_region(const char *value, intptr_t *)
{
if (strlen(value) != 1)
return -1;
switch (value[0] | 0x20) {
case 'j':
case 'x':
case 'u':
case 'e':
case ' ':
return (value[0] & ~0x20);
}
return -1;
}
intptr_t rc_string(const char *value, intptr_t *)
{
char *val;
if ((val = strdup(value)) == NULL)
return -1;
// -1 is reserved, thus invalid. Should not happen anyway.
if ((intptr_t)val == -1)
abort();
return (intptr_t)val;
}
intptr_t rc_rom_path(const char *value, intptr_t *)
{
intptr_t r = rc_string(value, NULL);
if (r == -1)
return -1;
set_rom_path((char *)r);
return r;
}
intptr_t rc_number(const char *value, intptr_t *)
{
return strtol(value, NULL, 0);
}
intptr_t rc_soundrate(const char *value, intptr_t *)
{
long r = strtol(value, NULL, 0);
if (r < 8000)
r = 8000;
return r;
}
/* This is a table of all the RC options, the variables they affect, and the
* functions to parse their values. */
struct rc_field rc_fields[RC_FIELDS_SIZE] = {
{ "key_pad1_up", rc_keysym, &pad1_up[RCBK] },
{ "joy_pad1_up", rc_joypad, &pad1_up[RCBJ] },
{ "mou_pad1_up", rc_mouse, &pad1_up[RCBM] },
{ "key_pad1_down", rc_keysym, &pad1_down[RCBK] },
{ "joy_pad1_down", rc_joypad, &pad1_down[RCBJ] },
{ "mou_pad1_down", rc_mouse, &pad1_down[RCBM] },
{ "key_pad1_left", rc_keysym, &pad1_left[RCBK] },
{ "joy_pad1_left", rc_joypad, &pad1_left[RCBJ] },
{ "mou_pad1_left", rc_mouse, &pad1_left[RCBM] },
{ "key_pad1_right", rc_keysym, &pad1_right[RCBK] },
{ "joy_pad1_right", rc_joypad, &pad1_right[RCBJ] },
{ "mou_pad1_right", rc_mouse, &pad1_right[RCBM] },
{ "key_pad1_a", rc_keysym, &pad1_a[RCBK] },
{ "joy_pad1_a", rc_joypad, &pad1_a[RCBJ] },
{ "mou_pad1_a", rc_mouse, &pad1_a[RCBM] },
{ "key_pad1_b", rc_keysym, &pad1_b[RCBK] },
{ "joy_pad1_b", rc_joypad, &pad1_b[RCBJ] },
{ "mou_pad1_b", rc_mouse, &pad1_b[RCBM] },
{ "key_pad1_c", rc_keysym, &pad1_c[RCBK] },
{ "joy_pad1_c", rc_joypad, &pad1_c[RCBJ] },
{ "mou_pad1_c", rc_mouse, &pad1_c[RCBM] },
{ "key_pad1_x", rc_keysym, &pad1_x[RCBK] },
{ "joy_pad1_x", rc_joypad, &pad1_x[RCBJ] },
{ "mou_pad1_x", rc_mouse, &pad1_x[RCBM] },
{ "key_pad1_y", rc_keysym, &pad1_y[RCBK] },
{ "joy_pad1_y", rc_joypad, &pad1_y[RCBJ] },
{ "mou_pad1_y", rc_mouse, &pad1_y[RCBM] },
{ "key_pad1_z", rc_keysym, &pad1_z[RCBK] },
{ "joy_pad1_z", rc_joypad, &pad1_z[RCBJ] },
{ "mou_pad1_z", rc_mouse, &pad1_z[RCBM] },
{ "key_pad1_mode", rc_keysym, &pad1_mode[RCBK] },
{ "joy_pad1_mode", rc_joypad, &pad1_mode[RCBJ] },
{ "mou_pad1_mode", rc_mouse, &pad1_mode[RCBM] },
{ "key_pad1_start", rc_keysym, &pad1_start[RCBK] },
{ "joy_pad1_start", rc_joypad, &pad1_start[RCBJ] },
{ "mou_pad1_start", rc_mouse, &pad1_start[RCBM] },
{ "key_pad2_up", rc_keysym, &pad2_up[RCBK] },
{ "joy_pad2_up", rc_joypad, &pad2_up[RCBJ] },
{ "mou_pad2_up", rc_mouse, &pad2_up[RCBM] },
{ "key_pad2_down", rc_keysym, &pad2_down[RCBK] },
{ "joy_pad2_down", rc_joypad, &pad2_down[RCBJ] },
{ "mou_pad2_down", rc_mouse, &pad2_down[RCBM] },
{ "key_pad2_left", rc_keysym, &pad2_left[RCBK] },
{ "joy_pad2_left", rc_joypad, &pad2_left[RCBJ] },
{ "mou_pad2_left", rc_mouse, &pad2_left[RCBM] },
{ "key_pad2_right", rc_keysym, &pad2_right[RCBK] },
{ "joy_pad2_right", rc_joypad, &pad2_right[RCBJ] },
{ "mou_pad2_right", rc_mouse, &pad2_right[RCBM] },
{ "key_pad2_a", rc_keysym, &pad2_a[RCBK] },
{ "joy_pad2_a", rc_joypad, &pad2_a[RCBJ] },
{ "mou_pad2_a", rc_mouse, &pad2_a[RCBM] },
{ "key_pad2_b", rc_keysym, &pad2_b[RCBK] },
{ "joy_pad2_b", rc_joypad, &pad2_b[RCBJ] },
{ "mou_pad2_b", rc_mouse, &pad2_b[RCBM] },
{ "key_pad2_c", rc_keysym, &pad2_c[RCBK] },
{ "joy_pad2_c", rc_joypad, &pad2_c[RCBJ] },
{ "mou_pad2_c", rc_mouse, &pad2_c[RCBM] },
{ "key_pad2_x", rc_keysym, &pad2_x[RCBK] },
{ "joy_pad2_x", rc_joypad, &pad2_x[RCBJ] },
{ "mou_pad2_x", rc_mouse, &pad2_x[RCBM] },
{ "key_pad2_y", rc_keysym, &pad2_y[RCBK] },
{ "joy_pad2_y", rc_joypad, &pad2_y[RCBJ] },
{ "mou_pad2_y", rc_mouse, &pad2_y[RCBM] },
{ "key_pad2_z", rc_keysym, &pad2_z[RCBK] },
{ "joy_pad2_z", rc_joypad, &pad2_z[RCBJ] },
{ "mou_pad2_z", rc_mouse, &pad2_z[RCBM] },
{ "key_pad2_mode", rc_keysym, &pad2_mode[RCBK] },
{ "joy_pad2_mode", rc_joypad, &pad2_mode[RCBJ] },
{ "mou_pad2_mode", rc_mouse, &pad2_mode[RCBM] },
{ "key_pad2_start", rc_keysym, &pad2_start[RCBK] },
{ "joy_pad2_start", rc_joypad, &pad2_start[RCBJ] },
{ "mou_pad2_start", rc_mouse, &pad2_start[RCBM] },
{ "key_pico_pen_up", rc_keysym, &pico_pen_up[RCBK] },
{ "joy_pico_pen_up", rc_joypad, &pico_pen_up[RCBJ] },
{ "mou_pico_pen_up", rc_mouse, &pico_pen_up[RCBM] },
{ "key_pico_pen_down", rc_keysym, &pico_pen_down[RCBK] },
{ "joy_pico_pen_down", rc_joypad, &pico_pen_down[RCBJ] },
{ "mou_pico_pen_down", rc_mouse, &pico_pen_down[RCBM] },
{ "key_pico_pen_left", rc_keysym, &pico_pen_left[RCBK] },
{ "joy_pico_pen_left", rc_joypad, &pico_pen_left[RCBJ] },
{ "mou_pico_pen_left", rc_mouse, &pico_pen_left[RCBM] },
{ "key_pico_pen_right", rc_keysym, &pico_pen_right[RCBK] },
{ "joy_pico_pen_right", rc_joypad, &pico_pen_right[RCBJ] },
{ "mou_pico_pen_right", rc_mouse, &pico_pen_right[RCBM] },
{ "key_pico_pen_button", rc_keysym, &pico_pen_button[RCBK] },
{ "joy_pico_pen_button", rc_joypad, &pico_pen_button[RCBJ] },
{ "mou_pico_pen_button", rc_mouse, &pico_pen_button[RCBM] },
{ "int_pico_pen_stride", rc_number, &pico_pen_stride },
{ "int_pico_pen_delay", rc_number, &pico_pen_delay },
{ "key_fix_checksum", rc_keysym, &dgen_fix_checksum[RCBK] },
{ "joy_fix_checksum", rc_joypad, &dgen_fix_checksum[RCBJ] },
{ "mou_fix_checksum", rc_mouse, &dgen_fix_checksum[RCBM] },
{ "key_quit", rc_keysym, &dgen_quit[RCBK] },
{ "joy_quit", rc_joypad, &dgen_quit[RCBJ] },
{ "mou_quit", rc_mouse, &dgen_quit[RCBM] },
{ "key_craptv_toggle", rc_keysym, &dgen_craptv_toggle[RCBK] },
{ "joy_craptv_toggle", rc_joypad, &dgen_craptv_toggle[RCBJ] },
{ "mou_craptv_toggle", rc_mouse, &dgen_craptv_toggle[RCBM] },
{ "key_scaling_toggle", rc_keysym, &dgen_scaling_toggle[RCBK] },
{ "joy_scaling_toggle", rc_joypad, &dgen_scaling_toggle[RCBJ] },
{ "mou_scaling_toggle", rc_mouse, &dgen_scaling_toggle[RCBM] },
{ "key_screenshot", rc_keysym, &dgen_screenshot[RCBK] },
{ "joy_screenshot", rc_joypad, &dgen_screenshot[RCBJ] },
{ "mou_screenshot", rc_mouse, &dgen_screenshot[RCBM] },
{ "key_reset", rc_keysym, &dgen_reset[RCBK] },
{ "joy_reset", rc_joypad, &dgen_reset[RCBJ] },
{ "mou_reset", rc_mouse, &dgen_reset[RCBM] },
{ "key_slot_0", rc_keysym, &dgen_slot_0[RCBK] },
{ "joy_slot_0", rc_joypad, &dgen_slot_0[RCBJ] },
{ "mou_slot_0", rc_mouse, &dgen_slot_0[RCBM] },
{ "key_slot_1", rc_keysym, &dgen_slot_1[RCBK] },
{ "joy_slot_1", rc_joypad, &dgen_slot_1[RCBJ] },
{ "mou_slot_1", rc_mouse, &dgen_slot_1[RCBM] },
{ "key_slot_2", rc_keysym, &dgen_slot_2[RCBK] },
{ "joy_slot_2", rc_joypad, &dgen_slot_2[RCBJ] },
{ "mou_slot_2", rc_mouse, &dgen_slot_2[RCBM] },
{ "key_slot_3", rc_keysym, &dgen_slot_3[RCBK] },
{ "joy_slot_3", rc_joypad, &dgen_slot_3[RCBJ] },
{ "mou_slot_3", rc_mouse, &dgen_slot_3[RCBM] },
{ "key_slot_4", rc_keysym, &dgen_slot_4[RCBK] },
{ "joy_slot_4", rc_joypad, &dgen_slot_4[RCBJ] },
{ "mou_slot_4", rc_mouse, &dgen_slot_4[RCBM] },
{ "key_slot_5", rc_keysym, &dgen_slot_5[RCBK] },
{ "joy_slot_5", rc_joypad, &dgen_slot_5[RCBJ] },
{ "mou_slot_5", rc_mouse, &dgen_slot_5[RCBM] },
{ "key_slot_6", rc_keysym, &dgen_slot_6[RCBK] },
{ "joy_slot_6", rc_joypad, &dgen_slot_6[RCBJ] },
{ "mou_slot_6", rc_mouse, &dgen_slot_6[RCBM] },
{ "key_slot_7", rc_keysym, &dgen_slot_7[RCBK] },
{ "joy_slot_7", rc_joypad, &dgen_slot_7[RCBJ] },
{ "mou_slot_7", rc_mouse, &dgen_slot_7[RCBM] },
{ "key_slot_8", rc_keysym, &dgen_slot_8[RCBK] },
{ "joy_slot_8", rc_joypad, &dgen_slot_8[RCBJ] },
{ "mou_slot_8", rc_mouse, &dgen_slot_8[RCBM] },
{ "key_slot_9", rc_keysym, &dgen_slot_9[RCBK] },
{ "joy_slot_9", rc_joypad, &dgen_slot_9[RCBJ] },
{ "mou_slot_9", rc_mouse, &dgen_slot_9[RCBM] },
{ "key_slot_next", rc_keysym, &dgen_slot_next[RCBK] },
{ "joy_slot_next", rc_joypad, &dgen_slot_next[RCBJ] },
{ "mou_slot_next", rc_mouse, &dgen_slot_next[RCBM] },
{ "key_slot_prev", rc_keysym, &dgen_slot_prev[RCBK] },
{ "joy_slot_prev", rc_joypad, &dgen_slot_prev[RCBJ] },
{ "mou_slot_prev", rc_mouse, &dgen_slot_prev[RCBM] },
{ "key_save", rc_keysym, &dgen_save[RCBK] },
{ "joy_save", rc_joypad, &dgen_save[RCBJ] },
{ "mou_save", rc_mouse, &dgen_save[RCBM] },
{ "key_load", rc_keysym, &dgen_load[RCBK] },
{ "joy_load", rc_joypad, &dgen_load[RCBJ] },
{ "mou_load", rc_mouse, &dgen_load[RCBM] },
{ "key_z80_toggle", rc_keysym, &dgen_z80_toggle[RCBK] },
{ "joy_z80_toggle", rc_joypad, &dgen_z80_toggle[RCBJ] },
{ "mou_z80_toggle", rc_mouse, &dgen_z80_toggle[RCBM] },
{ "key_cpu_toggle", rc_keysym, &dgen_cpu_toggle[RCBK] },
{ "joy_cpu_toggle", rc_joypad, &dgen_cpu_toggle[RCBJ] },
{ "mou_cpu_toggle", rc_mouse, &dgen_cpu_toggle[RCBM] },
{ "key_stop", rc_keysym, &dgen_stop[RCBK] },
{ "joy_stop", rc_joypad, &dgen_stop[RCBJ] },
{ "mou_stop", rc_mouse, &dgen_stop[RCBM] },
{ "key_game_genie", rc_keysym, &dgen_game_genie[RCBK] },
{ "joy_game_genie", rc_joypad, &dgen_game_genie[RCBJ] },
{ "mou_game_genie", rc_mouse, &dgen_game_genie[RCBM] },
{ "key_fullscreen_toggle", rc_keysym, &dgen_fullscreen_toggle[RCBK] },
{ "joy_fullscreen_toggle", rc_joypad, &dgen_fullscreen_toggle[RCBJ] },
{ "mou_fullscreen_toggle", rc_mouse, &dgen_fullscreen_toggle[RCBM] },
{ "key_debug_enter", rc_keysym, &dgen_debug_enter[RCBK] },
{ "joy_debug_enter", rc_joypad, &dgen_debug_enter[RCBJ] },
{ "mou_debug_enter", rc_mouse, &dgen_debug_enter[RCBM] },
{ "key_prompt", rc_keysym, &dgen_prompt[RCBK] },
{ "joy_prompt", rc_joypad, &dgen_prompt[RCBJ] },
{ "mou_prompt", rc_mouse, &dgen_prompt[RCBM] },
{ "bool_vdp_hide_plane_a", rc_boolean, &dgen_vdp_hide_plane_a },
{ "bool_vdp_hide_plane_b", rc_boolean, &dgen_vdp_hide_plane_b },
{ "bool_vdp_hide_plane_w", rc_boolean, &dgen_vdp_hide_plane_w },
{ "bool_vdp_hide_sprites", rc_boolean, &dgen_vdp_hide_sprites },
{ "bool_vdp_sprites_boxing", rc_boolean, &dgen_vdp_sprites_boxing },
{ "int_vdp_sprites_boxing_fg", rc_number, &dgen_vdp_sprites_boxing_fg },
{ "int_vdp_sprites_boxing_bg", rc_number, &dgen_vdp_sprites_boxing_bg },
{ "bool_autoload", rc_boolean, &dgen_autoload },
{ "bool_autosave", rc_boolean, &dgen_autosave },
{ "bool_autoconf", rc_boolean, &dgen_autoconf },
{ "bool_frameskip", rc_boolean, &dgen_frameskip },
{ "bool_show_carthead", rc_boolean, &dgen_show_carthead },
{ "str_rom_path", rc_rom_path,
(intptr_t *)((void *)&dgen_rom_path) }, // SH
{ "bool_raw_screenshots", rc_boolean, &dgen_raw_screenshots },
{ "ctv_craptv_startup", rc_ctv, &dgen_craptv }, // SH
{ "scaling_startup", rc_scaling, &dgen_scaling }, // SH
{ "emu_z80_startup", rc_emu_z80, &dgen_emu_z80 }, // SH
{ "emu_m68k_startup", rc_emu_m68k, &dgen_emu_m68k }, // SH
{ "bool_sound", rc_boolean, &dgen_sound }, // SH
{ "int_soundrate", rc_soundrate, &dgen_soundrate }, // SH
{ "int_soundsegs", rc_number, &dgen_soundsegs }, // SH
{ "int_soundsamples", rc_number, &dgen_soundsamples }, // SH
{ "int_volume", rc_number, &dgen_volume },
{ "key_volume_inc", rc_keysym, &dgen_volume_inc[RCBK] },
{ "joy_volume_inc", rc_joypad, &dgen_volume_inc[RCBJ] },
{ "mou_volume_inc", rc_mouse, &dgen_volume_inc[RCBM] },
{ "key_volume_dec", rc_keysym, &dgen_volume_dec[RCBK] },
{ "joy_volume_dec", rc_joypad, &dgen_volume_dec[RCBJ] },
{ "mou_volume_dec", rc_mouse, &dgen_volume_dec[RCBM] },
{ "bool_mjazz", rc_boolean, &dgen_mjazz }, // SH
{ "int_nice", rc_number, &dgen_nice },
{ "int_hz", rc_number, &dgen_hz }, // SH
{ "bool_pal", rc_boolean, &dgen_pal }, // SH
{ "region", rc_region, &dgen_region }, // SH
{ "str_region_order", rc_string,
(intptr_t *)((void *)&dgen_region_order) },
{ "bool_fps", rc_boolean, &dgen_fps },
{ "bool_buttons", rc_boolean, &dgen_buttons },
{ "bool_fullscreen", rc_boolean, &dgen_fullscreen }, // SH
{ "int_info_height", rc_number, &dgen_info_height }, // SH
{ "int_width", rc_number, &dgen_width }, // SH
{ "int_height", rc_number, &dgen_height }, // SH
{ "int_scale", rc_number, &dgen_scale }, // SH
{ "int_scale_x", rc_number, &dgen_x_scale }, // SH
{ "int_scale_y", rc_number, &dgen_y_scale }, // SH
{ "int_depth", rc_number, &dgen_depth }, // SH
{ "bool_aspect", rc_boolean, &dgen_aspect }, // SH
{ "bool_swab", rc_boolean, &dgen_swab }, // SH
{ "bool_opengl", rc_boolean, &dgen_opengl }, // SH
{ "bool_opengl_stretch", rc_boolean, &dgen_opengl_stretch }, // SH
// deprecated, use bool_aspect
{ "bool_opengl_aspect", rc_boolean, &dgen_aspect },
// deprecated, use int_width
{ "int_opengl_width", rc_number, &dgen_width },
// deprecated, use int_height
{ "int_opengl_height", rc_number, &dgen_height },
{ "bool_opengl_linear", rc_boolean, &dgen_opengl_linear }, // SH
{ "bool_opengl_32bit", rc_boolean, &dgen_opengl_32bit }, // SH
// deprecated, use bool_swab
{ "bool_opengl_swap", rc_boolean, &dgen_swab }, // SH
{ "bool_opengl_square", rc_boolean, &dgen_opengl_square }, // SH
{ "bool_doublebuffer", rc_boolean, &dgen_doublebuffer }, // SH
{ "bool_screen_thread", rc_boolean, &dgen_screen_thread }, // SH
{ "bool_joystick", rc_boolean, &dgen_joystick }, // SH
{ "int_mouse_delay", rc_number, &dgen_mouse_delay },
{ NULL, NULL, NULL }
};
struct rc_binding rc_binding_head = {
&rc_binding_head,
&rc_binding_head,
{ { false, RCBK, 0, } },
NULL,
NULL
};
static void rc_binding_cleanup(void)
{
struct rc_binding *rcb = rc_binding_head.next;
struct rc_binding *next;
while (rcb != &rc_binding_head) {
next = rcb->next;
assert(rcb->to != NULL);
assert((intptr_t)rcb->to != -1);
free(rcb->to);
#ifndef NDEBUG
memset(rcb, 0x66, sizeof(*rcb));
#endif
free(rcb);
rcb = next;
}
}
struct rc_field *rc_binding_add(const char *rc, const char *to)
{
static bool registered = false;
size_t rc_sz = (strlen(rc) + 1);
struct rc_field *rcf = rc_fields;
struct rc_binding *rcb;
struct rc_binding_item item[elemof(rcb->item)];
unsigned int i;
const char *s;
size_t off;
char *new_to;
if ((registered == false) && (atexit(rc_binding_cleanup) == 0))
registered = true;
// Check if the RC name looks like a valid binding.
for (off = 0; (rc[off] != '\0'); ++off) {
if (RC_BIND_PREFIX[off] == '\0')
break;
if (rc[off] != RC_BIND_PREFIX[off])
return NULL;
}
if (rc[off] == '\0')
return NULL;
// Extract multiple keysyms or joypad codes from RC name.
memset(item, 0, sizeof(item));
s = &rc[off];
i = 0;
while (s += strspn(s, " \t\n"), off = strcspn(s, " \t\n")) {
char tmp[64];
intptr_t code;
enum rc_binding_type type;
if (i == elemof(item))
return NULL;
snprintf(tmp, sizeof(tmp), "%.*s", (int)off, s);
if ((type = RCBJ, ((code = rc_joypad(tmp, NULL)) == -1)) &&
(type = RCBK, ((code = rc_keysym(tmp, NULL)) == -1)) &&
(type = RCBM, ((code = rc_mouse(tmp, NULL)) == -1)))
return NULL;
item[i].assigned = true;
item[i].type = type;
item[i].code = code;
++i;
s += off;
}
// Find a free entry in rc_fields[].
while (rcf->fieldname != NULL)
++rcf;
if ((rcf - rc_fields) == (elemof(rc_fields) - 1))
return NULL;
assert(rcf->parser == NULL);
assert(rcf->variable == NULL);
// Allocate binding.
if ((rcb = (struct rc_binding *)malloc(sizeof(*rcb) + rc_sz)) == NULL)
return NULL;
if ((new_to = strdup(to)) == NULL) {
free(new_to);
free(rcb);
return NULL;
}
// -1 is reserved, thus invalid. Should not happen anyway.
if ((intptr_t)new_to == -1)
abort();
// Configure binding.
rcb->prev = rc_binding_head.prev;
rcb->next = &rc_binding_head;
rcb->prev->next = rcb;
rcb->next->prev = rcb;
memcpy(rcb->item, item, sizeof(rcb->item));
rcb->rc = ((char *)rcb + sizeof(*rcb));
rcb->to = new_to;
memcpy(rcb->rc, rc, rc_sz);
// Configure RC field.
rcf->fieldname = rcb->rc;
rcf->parser = rc_bind;
rcf->variable = (intptr_t *)((void *)&rcb->to);
return rcf;
}
void rc_binding_del(rc_field *rcf)
{
struct rc_binding *rcb =
containerof(rcf->variable, struct rc_binding, to);
assert(rcf >= &rc_fields[0]);
assert(rcf < &rc_fields[elemof(rc_fields)]);
assert(rcf->fieldname != NULL);
assert(rcf->parser != NULL);
assert(rcf->variable != NULL);
if (rcf->parser != rc_bind)
return;
assert(rcb != &rc_binding_head);
// Clean-up.
rcb->prev->next = rcb->next;
rcb->next->prev = rcb->prev;
assert(rcb->to != NULL);
assert((intptr_t)rcb->to != -1);
free(rcb->to);
#ifndef NDEBUG
memset(rcb, 0x88, (sizeof(*rcb) + strlen(rcb->rc) + 1));
#endif
free(rcb);
// Shift the next entries.
do {
memcpy(rcf, (rcf + 1), sizeof(*rcf));
++rcf;
}
while (rcf->fieldname != NULL);
assert(rcf < &rc_fields[elemof(rc_fields)]);
}
intptr_t rc_bind(const char *value, intptr_t *variable)
{
struct rc_binding *rcb = containerof(variable, struct rc_binding, to);
char *to;
assert(*variable != -1);
assert(rcb != NULL);
assert(rcb->prev != NULL);
assert(rcb->next != NULL);
assert(rcb->rc != NULL);
assert(rcb->to != NULL);
assert((intptr_t)rcb->to != -1);
if ((to = strdup(value)) == NULL) {
free(to);
// Get the previous value.
to = rcb->to;
}
// -1 is reserved, thus invalid. Should not happen anyway.
else if ((intptr_t)to == -1)
abort();
else
free(rcb->to);
rcb->to = NULL; // Will be updated by the return value.
// This function must always return a valid pointer.
return (intptr_t)to;
}
/* Replace unprintable characters */
static char *strclean(char *s)
{
size_t i;
for (i = 0; (s[i] != '\0'); ++i)
if (!isprint(s[i]))
s[i] = '?';
return s;
}
/* This is for cleaning up rc_str fields at exit */
struct rc_str *rc_str_list = NULL;
void rc_str_cleanup(void)
{
struct rc_str *rs = rc_str_list;