forked from rofl0r/gnuboy
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
397 lines (330 loc) · 8.48 KB
/
main.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
#undef _GNU_SOURCE
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <signal.h>
#include "input.h"
#include "rc.h"
#include "sys.h"
#include "rckeys.h"
#include "emu.h"
#include "exports.h"
#include "loader.h"
#include "Version"
static char *defaultconfig[] =
{
"bind esc quit",
"bind u +up",
"bind d +down",
"bind l +left",
"bind r +right",
"bind a +a",
"bind b +b",
"bind y +a",
"bind x +b",
"bind s +start",
"bind k +select",
"bind q menu",
"bind h aspectratiochange",
"bind c volumeinc",
"bind e volumedec",
"bind g brightnessinc",
"bind w brightnessdec",
"bind p savestate",
"bind tab +select",
"bind joyup +up",
"bind joydown +down",
"bind joyleft +left",
"bind joyright +right",
"bind joy0 +b",
"bind joy1 +a",
"bind joy2 +select",
"bind joy3 +start",
"bind 1 \"set saveslot 1\"",
"bind 2 \"set saveslot 2\"",
"bind 3 \"set saveslot 3\"",
"bind 4 \"set saveslot 4\"",
"bind 5 \"set saveslot 5\"",
"bind 6 \"set saveslot 6\"",
"bind 7 \"set saveslot 7\"",
"bind 8 \"set saveslot 8\"",
"bind 9 \"set saveslot 9\"",
"bind 0 \"set saveslot 0\"",
"bind ins savestate",
"bind del loadstate",
"source gnuboy.rc",
NULL
};
static void banner()
{
printf("\ngnuboy " VERSION "\n");
}
static void copyright()
{
banner();
printf(
"Copyright (C) 2000-2001 Laguna and Gilgamesh\n"
"Portions contributed by other authors; see CREDITS for details.\n"
"\n"
"This program is free software; you can redistribute it and/or modify\n"
"it under the terms of the GNU General Public License as published by\n"
"the Free Software Foundation; either version 2 of the License, or\n"
"(at your option) any later version.\n"
"\n"
"This program is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"GNU General Public License for more details.\n"
"\n"
"You should have received a copy of the GNU General Public License\n"
"along with this program; if not, write to the Free Software\n"
"Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
"\n");
}
static void usage(char *name)
{
copyright();
printf("Type %s --help for detailed help.\n\n", name);
exit(1);
}
static void copying()
{
copyright();
exit(0);
}
static void help(char *name)
{
banner();
printf("Usage: %s [options] romfile\n", name);
printf("\n"
" --source FILE read rc commands from FILE\n"
" --bind KEY COMMAND bind KEY to perform COMMAND\n"
" --VAR=VALUE set rc variable VAR to VALUE\n"
" --VAR set VAR to 1 (turn on boolean options)\n"
" --no-VAR set VAR to 0 (turn off boolean options)\n"
" --showvars list all available rc variables\n"
" --help display this help and exit\n"
" --version output version information and exit\n"
" --copying show copying permissions\n"
"");
exit(0);
}
static void version(char *name)
{
printf("%s-" VERSION "\n", name);
exit(0);
}
void doevents()
{
event_t ev;
int st;
ev_poll();
while (ev_getevent(&ev))
{
if (ev.type != EV_PRESS && ev.type != EV_RELEASE)
continue;
st = (ev.type != EV_RELEASE);
rc_dokey(ev.code, st);
}
}
static void shutdown()
{
vid_close();
pcm_close();
}
void die(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(1);
}
static int bad_signals[] =
{
/* These are all standard, so no need to #ifdef them... */
SIGINT, SIGSEGV, SIGTERM, SIGFPE, SIGABRT, SIGILL,
#ifdef SIGQUIT
SIGQUIT,
#endif
#ifdef SIGPIPE
SIGPIPE,
#endif
0
};
/* Handler for SIGUSR1, caused by closing the console */
void handle_sigusr1(int sig)
{
//printf("Caught signal USR1 %d\n", sig);
/* Exit menu if it was launched */
stop_menu_loop = 1;
/* Signal to quick save and poweoff after next loop */
mQuickSaveAndPoweroff = 1;
}
static void fatalsignal(int s)
{
die("Signal %d\n", s);
}
static void catch_signals()
{
int i;
for (i = 0; bad_signals[i]; i++)
signal(bad_signals[i], fatalsignal);
}
static char *base(char *s)
{
char *p;
p = strrchr(s, '/');
if (p) return p+1;
return s;
}
int main(int argc, char *argv[])
{
int i;
char *opt, *arg, *cmd, *s, *rom = 0;
/* Save program name */
prog_name = argv[0];
/* Avoid initializing video if we don't have to */
for (i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "--help"))
help(base(argv[0]));
else if (!strcmp(argv[i], "--version"))
version(base(argv[0]));
else if (!strcmp(argv[i], "--copying"))
copying();
else if (!strcmp(argv[i], "--bind")) i += 2;
else if (!strcmp(argv[i], "--source")) i++;
else if (!strcmp(argv[i], "--showvars"))
{
show_exports();
exit(0);
}
else if (argv[i][0] == '-' && argv[i][1] == '-');
else if (argv[i][0] == '-' && argv[i][1]);
else rom = argv[i];
}
if (!rom) usage(base(argv[0]));
/* Save Rom path */
mRomName = rom;
mRomPath = (char *) malloc(strlen(mRomName) + 1);
strcpy(mRomPath, mRomName);
char *slash = strrchr((char *) mRomPath, '/');
*slash = 0;
/* Rom name without extension */
char *point = strrchr ((char *) slash + 1, '.');
*point = 0;
/* Set quicksave filename */
quick_save_file = (char *) malloc(strlen(mRomPath) + strlen(slash + 1) +
strlen(quick_save_file_extension) + 2 + 1);
sprintf(quick_save_file, "%s/%s.%s",
mRomPath, slash + 1, quick_save_file_extension);
printf("Quick_save_file: %s\n", quick_save_file);
/* Set rom cfg filepath */
cfg_file_rom = (char *)malloc(strlen(mRomPath) + strlen(slash+1) +
strlen(cfg_file_extension) + 2 + 1);
sprintf(cfg_file_rom, "%s/%s.%s",
mRomPath, slash+1, cfg_file_extension);
printf("cfg_file_rom: %s\n", cfg_file_rom);
/* Set console cfg filepath */
cfg_file_default = (char *)malloc(strlen(mRomPath) + strlen(cfg_file_default_name) +
strlen(cfg_file_extension) + 2 + 1);
sprintf(cfg_file_default, "%s/%s.%s",
mRomPath, cfg_file_default_name, cfg_file_extension);
printf("cfg_file_default: %s\n", cfg_file_default);
/** Load config files */
configfile_load(cfg_file_default);
configfile_load(cfg_file_rom);
/* Init USR1 Signal (for quick save and poweroff) */
signal(SIGUSR1, handle_sigusr1);
/* If we have special perms, drop them ASAP! */
vid_preinit();
init_exports();
s = strdup(argv[0]);
sys_sanitize(s);
sys_initpath(s);
for (i = 0; defaultconfig[i]; i++){
//printf("Apply defaultconfig[%d] = %s\n", i, defaultconfig[i]);
rc_command(defaultconfig[i]);
}
cmd = malloc(strlen(rom) + 11);
sprintf(cmd, "source %s", rom);
s = strchr(cmd, '.');
if (s) *s = 0;
strcat(cmd, ".rc");
rc_command(cmd);
for (i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "--bind"))
{
if (i + 2 >= argc) die("missing arguments to bind\n");
cmd = malloc(strlen(argv[i+1]) + strlen(argv[i+2]) + 9);
sprintf(cmd, "bind %s \"%s\"", argv[i+1], argv[i+2]);
rc_command(cmd);
free(cmd);
i += 2;
}
else if (!strcmp(argv[i], "--source"))
{
if (i + 1 >= argc) die("missing argument to source\n");
cmd = malloc(strlen(argv[i+1]) + 6);
sprintf(cmd, "source %s", argv[++i]);
rc_command(cmd);
free(cmd);
}
else if (!strcmp(argv[i], "--loadStateSlot"))
{
if (i + 1 >= argc) die("missing argument to loadStateSlot\n");
load_state_slot = atoi(argv[i+1]);
}
else if (!strcmp(argv[i], "--loadStateFile"))
{
if (i + 1 >= argc) die("missing argument to loadStateFile\n");
load_state_file = argv[i+1];
}
else if (!strncmp(argv[i], "--no-", 5))
{
opt = strdup(argv[i]+5);
while ((s = strchr(opt, '-'))) *s = '_';
cmd = malloc(strlen(opt) + 7);
sprintf(cmd, "set %s 0", opt);
rc_command(cmd);
free(cmd);
free(opt);
}
else if (argv[i][0] == '-' && argv[i][1] == '-')
{
opt = strdup(argv[i]+2);
if ((s = strchr(opt, '=')))
{
*s = 0;
arg = s+1;
}
else arg = "1";
while ((s = strchr(opt, '-'))) *s = '_';
while ((s = strchr(arg, ','))) *s = ' ';
cmd = malloc(strlen(opt) + strlen(arg) + 6);
sprintf(cmd, "set %s %s", opt, arg);
//printf("cmd = %s\n", cmd);
rc_command(cmd);
free(cmd);
free(opt);
}
/* short options not yet implemented */
else if (argv[i][0] == '-' && argv[i][1]);
}
/* FIXME - make interface modules responsible for atexit() */
atexit(shutdown);
catch_signals();
vid_init();
pcm_init();
rom = strdup(rom);
sys_sanitize(rom);
loader_init(rom);
emu_reset();
emu_run();
/* never reached */
return 0;
}