forked from MiSTer-devel/Main_MiSTer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfg.cpp
335 lines (289 loc) · 11.2 KB
/
cfg.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
// cfg.c
// 2015, [email protected]
// 2017+, Sorgelig
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <ctype.h>
#include "cfg.h"
#include "debug.h"
#include "file_io.h"
#include "user_io.h"
cfg_t cfg;
typedef enum
{
UINT8 = 0, INT8, UINT16, INT16, UINT32, INT32, FLOAT, STRING
} ini_vartypes_t;
typedef struct
{
const char* name;
void* var;
ini_vartypes_t type;
int min;
int max;
} ini_var_t;
static const ini_var_t ini_vars[] =
{
{ "YPBPR", (void*)(&(cfg.ypbpr)), UINT8, 0, 1 },
{ "COMPOSITE_SYNC", (void*)(&(cfg.csync)), UINT8, 0, 1 },
{ "FORCED_SCANDOUBLER", (void*)(&(cfg.forced_scandoubler)), UINT8, 0, 1 },
{ "VGA_SCALER", (void*)(&(cfg.vga_scaler)), UINT8, 0, 1 },
{ "VGA_SOG", (void*)(&(cfg.vga_sog)), UINT8, 0, 1 },
{ "KEYRAH_MODE", (void*)(&(cfg.keyrah_mode)), UINT32, 0, (int)0xFFFFFFFF },
{ "RESET_COMBO", (void*)(&(cfg.reset_combo)), UINT8, 0, 3 },
{ "KEY_MENU_AS_RGUI", (void*)(&(cfg.key_menu_as_rgui)), UINT8, 0, 1 },
{ "VIDEO_MODE", (void*)(cfg.video_conf), STRING, 0, sizeof(cfg.video_conf) - 1 },
{ "VIDEO_MODE_PAL", (void*)(cfg.video_conf_pal), STRING, 0, sizeof(cfg.video_conf_pal) - 1 },
{ "VIDEO_MODE_NTSC", (void*)(cfg.video_conf_ntsc), STRING, 0, sizeof(cfg.video_conf_ntsc) - 1 },
{ "VIDEO_INFO", (void*)(&(cfg.video_info)), UINT8, 0, 10 },
{ "VSYNC_ADJUST", (void*)(&(cfg.vsync_adjust)), UINT8, 0, 2 },
{ "HDMI_AUDIO_96K", (void*)(&(cfg.hdmi_audio_96k)), UINT8, 0, 1 },
{ "DVI_MODE", (void*)(&(cfg.dvi)), UINT8, 0, 1 },
{ "HDMI_LIMITED", (void*)(&(cfg.hdmi_limited)), UINT8, 0, 2 },
{ "KBD_NOMOUSE", (void*)(&(cfg.kbd_nomouse)), UINT8, 0, 1 },
{ "MOUSE_THROTTLE", (void*)(&(cfg.mouse_throttle)), UINT8, 1, 100 },
{ "BOOTSCREEN", (void*)(&(cfg.bootscreen)), UINT8, 0, 1 },
{ "VSCALE_MODE", (void*)(&(cfg.vscale_mode)), UINT8, 0, 3 },
{ "VSCALE_BORDER", (void*)(&(cfg.vscale_border)), UINT16, 0, 399 },
{ "RBF_HIDE_DATECODE", (void*)(&(cfg.rbf_hide_datecode)), UINT8, 0, 1 },
{ "MENU_PAL", (void*)(&(cfg.menu_pal)), UINT8, 0, 1 },
{ "BOOTCORE", (void*)(&(cfg.bootcore)), STRING, 0, sizeof(cfg.bootcore) - 1 },
{ "BOOTCORE_TIMEOUT", (void*)(&(cfg.bootcore_timeout)), INT16, 2, 30 },
{ "FONT", (void*)(&(cfg.font)), STRING, 0, sizeof(cfg.font) - 1 },
{ "FB_SIZE", (void*)(&(cfg.fb_size)), UINT8, 0, 4 },
{ "FB_TERMINAL", (void*)(&(cfg.fb_terminal)), UINT8, 0, 1 },
{ "OSD_TIMEOUT", (void*)(&(cfg.osd_timeout)), INT16, 5, 3600 },
{ "DIRECT_VIDEO", (void*)(&(cfg.direct_video)), UINT8, 0, 1 },
{ "OSD_ROTATE", (void*)(&(cfg.osd_rotate)), UINT8, 0, 2 },
{ "GAMEPAD_DEFAULTS", (void*)(&(cfg.gamepad_defaults)), UINT8, 0, 1 },
{ "RECENTS", (void*)(&(cfg.recents)), UINT8, 0, 1 },
{ "CONTROLLER_INFO", (void*)(&(cfg.controller_info)), UINT8, 0, 10 },
{ "REFRESH_MIN", (void*)(&(cfg.refresh_min)), UINT8, 0, 150 },
{ "REFRESH_MAX", (void*)(&(cfg.refresh_max)), UINT8, 0, 150 },
{ "JAMMA_VID", (void*)(&(cfg.jamma_vid)), UINT16, 0, 0xFFFF },
{ "JAMMA_PID", (void*)(&(cfg.jamma_pid)), UINT16, 0, 0xFFFF },
{ "SNIPER_MODE", (void*)(&(cfg.sniper_mode)), UINT8, 0, 1 },
{ "BROWSE_EXPAND", (void*)(&(cfg.browse_expand)), UINT8, 0, 1 },
{ "LOGO", (void*)(&(cfg.logo)), UINT8, 0, 1 },
{ "SHARED_FOLDER", (void*)(&(cfg.shared_folder)), STRING, 0, sizeof(cfg.shared_folder) - 1 },
{ "NO_MERGE_VID", (void*)(&(cfg.no_merge_vid)), UINT16, 0, 0xFFFF },
{ "NO_MERGE_PID", (void*)(&(cfg.no_merge_pid)), UINT16, 0, 0xFFFF },
{ "CUSTOM_ASPECT_RATIO_1", (void*)(&(cfg.custom_aspect_ratio[0])), STRING, 0, sizeof(cfg.custom_aspect_ratio[0]) - 1 },
{ "CUSTOM_ASPECT_RATIO_2", (void*)(&(cfg.custom_aspect_ratio[1])), STRING, 0, sizeof(cfg.custom_aspect_ratio[1]) - 1 },
{ "SPINNER_VID", (void*)(&(cfg.spinner_vid)), UINT16, 0, 0xFFFF },
{ "SPINNER_PID", (void*)(&(cfg.spinner_pid)), UINT16, 0, 0xFFFF },
{ "SPINNER_THROTTLE", (void*)(&(cfg.spinner_throttle)), INT32, -10000, 10000 },
{ "AFILTER_DEFAULT", (void*)(&(cfg.afilter_default)), STRING, 0, sizeof(cfg.afilter_default) - 1 },
{ "VFILTER_DEFAULT", (void*)(&(cfg.vfilter_default)), STRING, 0, sizeof(cfg.vfilter_default) - 1 },
};
static const int nvars = (int)(sizeof(ini_vars) / sizeof(ini_var_t));
#define INI_EOT 4 // End-Of-Transmission
#define INI_LINE_SIZE 256
#define INI_SECTION_START '['
#define INI_SECTION_END ']'
#define INI_SECTION_INVALID_ID 0
#define CHAR_IS_NUM(c) (((c) >= '0') && ((c) <= '9'))
#define CHAR_IS_ALPHA_LOWER(c) (((c) >= 'a') && ((c) <= 'z'))
#define CHAR_IS_ALPHA_UPPER(c) (((c) >= 'A') && ((c) <= 'Z'))
#define CHAR_IS_ALPHANUM(c) (CHAR_IS_ALPHA_LOWER(c) || CHAR_IS_ALPHA_UPPER(c) || CHAR_IS_NUM(c))
#define CHAR_IS_SPECIAL(c) (((c) == '[') || ((c) == ']') || ((c) == '(') || ((c) == ')') || \
((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == '=') || \
((c) == '#') || ((c) == '$') || ((c) == '@') || ((c) == '_') || \
((c) == ',') || ((c) == '.') || ((c) == '!') || ((c) == '*') || \
((c) == ':'))
#define CHAR_IS_VALID(c) (CHAR_IS_ALPHANUM(c) || CHAR_IS_SPECIAL(c))
#define CHAR_IS_SPACE(c) (((c) == ' ') || ((c) == '\t'))
#define CHAR_IS_LINEEND(c) (((c) == '\n'))
#define CHAR_IS_COMMENT(c) (((c) == ';'))
#define CHAR_IS_QUOTE(c) (((c) == '"'))
fileTYPE ini_file;
int ini_pt = 0;
static char ini_getch()
{
static uint8_t buf[512];
if (!(ini_pt & 0x1ff)) FileReadSec(&ini_file, buf);
if (ini_pt >= ini_file.size) return 0;
return buf[(ini_pt++) & 0x1ff];
}
static int ini_getline(char* line)
{
char c, ignore = 0, skip = 1;
int i = 0;
while ((c = ini_getch()))
{
if (!CHAR_IS_SPACE(c)) skip = 0;
if (i >= (INI_LINE_SIZE - 1) || CHAR_IS_COMMENT(c)) ignore = 1;
if (CHAR_IS_LINEEND(c)) break;
if ((CHAR_IS_SPACE(c) || CHAR_IS_VALID(c)) && !ignore && !skip) line[i++] = c;
}
line[i] = '\0';
while (i > 0 && CHAR_IS_SPACE(line[i - 1])) line[--i] = 0;
return c == 0 ? INI_EOT : 0;
}
static int ini_get_section(char* buf)
{
int i = 0;
// get section start marker
if (buf[0] != INI_SECTION_START)
{
return INI_SECTION_INVALID_ID;
}
else buf++;
int wc_pos = -1;
// get section stop marker
while (1)
{
if (buf[i] == INI_SECTION_END)
{
buf[i] = '\0';
break;
}
if (buf[i] == '*') wc_pos = i;
i++;
if (i >= INI_LINE_SIZE) return INI_SECTION_INVALID_ID;
}
// convert to uppercase
for (i = 0; i < INI_LINE_SIZE; i++)
{
if (!buf[i]) break;
else buf[i] = toupper(buf[i]);
}
if (!strcasecmp(buf, "MiSTer") || ((wc_pos >= 0) ? !strncasecmp(buf, user_io_get_core_name_ex(), wc_pos) : !strcasecmp(buf, user_io_get_core_name_ex())))
{
ini_parser_debugf("Got SECTION '%s'", buf);
return 1;
}
return INI_SECTION_INVALID_ID;
}
static void ini_parse_var(char* buf)
{
int i = 0, j = 0;
int var_id = -1;
// find var
while (1)
{
if (buf[i] == '=' || CHAR_IS_SPACE(buf[i]))
{
buf[i] = '\0';
break;
}
else if (buf[i] == '\0') return;
i++;
}
// convert to uppercase
for (j = 0; j <= i; j++)
{
if (!buf[j]) break;
else buf[j] = toupper(buf[j]);
}
// parse var
for (j = 0; j < (int)(sizeof(ini_vars) / sizeof(ini_var_t)); j++)
{
if (!strcasecmp(buf, ini_vars[j].name)) var_id = j;
}
// get data
if (var_id != -1)
{
i++;
while (buf[i] == '=' || CHAR_IS_SPACE(buf[i])) i++;
ini_parser_debugf("Got VAR '%s' with VALUE %s", buf, buf+i);
switch (ini_vars[var_id].type)
{
case UINT8:
*(uint8_t*)(ini_vars[var_id].var) = strtoul(&(buf[i]), NULL, 0);
if (*(uint8_t*)(ini_vars[var_id].var) > ini_vars[var_id].max) *(uint8_t*)(ini_vars[var_id].var) = ini_vars[var_id].max;
if (*(uint8_t*)(ini_vars[var_id].var) < ini_vars[var_id].min) *(uint8_t*)(ini_vars[var_id].var) = ini_vars[var_id].min;
break;
case INT8:
*(int8_t*)(ini_vars[var_id].var) = strtol(&(buf[i]), NULL, 0);
if (*(int8_t*)(ini_vars[var_id].var) > ini_vars[var_id].max) *(int8_t*)(ini_vars[var_id].var) = ini_vars[var_id].max;
if (*(int8_t*)(ini_vars[var_id].var) < ini_vars[var_id].min) *(int8_t*)(ini_vars[var_id].var) = ini_vars[var_id].min;
break;
case UINT16:
*(uint16_t*)(ini_vars[var_id].var) = strtoul(&(buf[i]), NULL, 0);
if (*(uint16_t*)(ini_vars[var_id].var) > ini_vars[var_id].max) *(uint16_t*)(ini_vars[var_id].var) = ini_vars[var_id].max;
if (*(uint16_t*)(ini_vars[var_id].var) < ini_vars[var_id].min) *(uint16_t*)(ini_vars[var_id].var) = ini_vars[var_id].min;
break;
case INT16:
*(int16_t*)(ini_vars[var_id].var) = strtol(&(buf[i]), NULL, 0);
if (*(int16_t*)(ini_vars[var_id].var) > ini_vars[var_id].max) *(int16_t*)(ini_vars[var_id].var) = ini_vars[var_id].max;
if (*(int16_t*)(ini_vars[var_id].var) < ini_vars[var_id].min) *(int16_t*)(ini_vars[var_id].var) = ini_vars[var_id].min;
break;
case UINT32:
*(uint32_t*)(ini_vars[var_id].var) = strtoul(&(buf[i]), NULL, 0);
if (*(uint32_t*)(ini_vars[var_id].var) > (uint32_t)ini_vars[var_id].max) *(uint32_t*)(ini_vars[var_id].var) = ini_vars[var_id].max;
if (*(uint32_t*)(ini_vars[var_id].var) < (uint32_t)ini_vars[var_id].min) *(uint32_t*)(ini_vars[var_id].var) = ini_vars[var_id].min;
break;
case INT32:
*(int32_t*)(ini_vars[var_id].var) = strtol(&(buf[i]), NULL, 0);
if (*(int32_t*)(ini_vars[var_id].var) > ini_vars[var_id].max) *(int32_t*)(ini_vars[var_id].var) = ini_vars[var_id].max;
if (*(int32_t*)(ini_vars[var_id].var) < ini_vars[var_id].min) *(int32_t*)(ini_vars[var_id].var) = ini_vars[var_id].min;
break;
case FLOAT:
*(float*)(ini_vars[var_id].var) = strtof(&(buf[i]), NULL);
if (*(float*)(ini_vars[var_id].var) > ini_vars[var_id].max) *(float*)(ini_vars[var_id].var) = ini_vars[var_id].max;
if (*(float*)(ini_vars[var_id].var) < ini_vars[var_id].min) *(float*)(ini_vars[var_id].var) = ini_vars[var_id].min;
break;
case STRING:
memset(ini_vars[var_id].var, 0, ini_vars[var_id].max);
strncpy((char*)(ini_vars[var_id].var), &(buf[i]), ini_vars[var_id].max);
break;
}
}
}
static void ini_parse(int alt)
{
char line[INI_LINE_SIZE] = { 0 };
int section = INI_SECTION_INVALID_ID;
int line_status;
ini_parser_debugf("Start INI parser for core \"%s\".", user_io_get_core_name_ex());
memset(&ini_file, 0, sizeof(ini_file));
const char *name = cfg_get_name(alt);
if (!FileOpen(&ini_file, name)) return;
ini_parser_debugf("Opened file %s with size %llu bytes.", name, ini_file.size);
ini_pt = 0;
// parse ini
while (1)
{
// get line
line_status = ini_getline(line);
ini_parser_debugf("line(%d): \"%s\".", line_status, line);
if (line[0] == INI_SECTION_START)
{
// if first char in line is INI_SECTION_START, get section
section = ini_get_section(line);
}
else if(section)
{
// otherwise this is a variable, get it
ini_parse_var(line);
}
// if end of file, stop
if (line_status == INI_EOT) break;
}
FileClose(&ini_file);
}
const char* cfg_get_name(uint8_t alt)
{
static char name[64];
strcpy(name, "MiSTer.ini");
if (alt == 1)
{
strcpy(name, "MiSTer_alt_1.ini");
if (FileExists(name)) return name;
return "MiSTer_alt.ini";
}
if (alt && alt < 4) sprintf(name, "MiSTer_alt_%d.ini", alt);
return name;
}
void cfg_parse()
{
memset(&cfg, 0, sizeof(cfg));
cfg.bootscreen = 1;
cfg.fb_terminal = 1;
cfg.controller_info = 6;
cfg.browse_expand = 1;
cfg.logo = 1;
ini_parse(altcfg());
}