configure.in: make XShm.h test compatible with modern autotools
[rofl0r-gnuboy.git] / main.c
blobd3eecfd7e90d66fcc7f0df9e5d7d0e7a2f55f23d
1 #undef _GNU_SOURCE
2 #define _GNU_SOURCE
3 #include <string.h>
4 #include <stdio.h>
5 #include <stdlib.h>
7 #include <stdarg.h>
8 #include <signal.h>
10 #include "input.h"
11 #include "rc.h"
12 #include "sys.h"
13 #include "rckeys.h"
14 #include "emu.h"
15 #include "exports.h"
16 #include "loader.h"
18 #include "Version"
21 static char *defaultconfig[] =
23 "bind esc quit",
24 "bind up +up",
25 "bind down +down",
26 "bind left +left",
27 "bind right +right",
28 "bind d +a",
29 "bind s +b",
30 "bind enter +start",
31 "bind space +select",
32 "bind tab +select",
33 "bind joyup +up",
34 "bind joydown +down",
35 "bind joyleft +left",
36 "bind joyright +right",
37 "bind joy0 +b",
38 "bind joy1 +a",
39 "bind joy2 +select",
40 "bind joy3 +start",
41 "bind 1 \"set saveslot 1\"",
42 "bind 2 \"set saveslot 2\"",
43 "bind 3 \"set saveslot 3\"",
44 "bind 4 \"set saveslot 4\"",
45 "bind 5 \"set saveslot 5\"",
46 "bind 6 \"set saveslot 6\"",
47 "bind 7 \"set saveslot 7\"",
48 "bind 8 \"set saveslot 8\"",
49 "bind 9 \"set saveslot 9\"",
50 "bind 0 \"set saveslot 0\"",
51 "bind ins savestate",
52 "bind del loadstate",
53 "source gnuboy.rc",
54 NULL
58 static void banner()
60 printf("\ngnuboy " VERSION "\n");
63 static void copyright()
65 banner();
66 printf(
67 "Copyright (C) 2000-2001 Laguna and Gilgamesh\n"
68 "Portions contributed by other authors; see CREDITS for details.\n"
69 "\n"
70 "This program is free software; you can redistribute it and/or modify\n"
71 "it under the terms of the GNU General Public License as published by\n"
72 "the Free Software Foundation; either version 2 of the License, or\n"
73 "(at your option) any later version.\n"
74 "\n"
75 "This program is distributed in the hope that it will be useful,\n"
76 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
77 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
78 "GNU General Public License for more details.\n"
79 "\n"
80 "You should have received a copy of the GNU General Public License\n"
81 "along with this program; if not, write to the Free Software\n"
82 "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
83 "\n");
86 static void usage(char *name)
88 copyright();
89 printf("Type %s --help for detailed help.\n\n", name);
90 exit(1);
93 static void copying()
95 copyright();
96 exit(0);
99 static void help(char *name)
101 banner();
102 printf("Usage: %s [options] romfile\n", name);
103 printf("\n"
104 " --source FILE read rc commands from FILE\n"
105 " --bind KEY COMMAND bind KEY to perform COMMAND\n"
106 " --VAR=VALUE set rc variable VAR to VALUE\n"
107 " --VAR set VAR to 1 (turn on boolean options)\n"
108 " --no-VAR set VAR to 0 (turn off boolean options)\n"
109 " --showvars list all available rc variables\n"
110 " --help display this help and exit\n"
111 " --version output version information and exit\n"
112 " --copying show copying permissions\n"
113 "");
114 exit(0);
118 static void version(char *name)
120 printf("%s-" VERSION "\n", name);
121 exit(0);
125 void doevents()
127 event_t ev;
128 int st;
130 ev_poll();
131 while (ev_getevent(&ev))
133 if (ev.type != EV_PRESS && ev.type != EV_RELEASE)
134 continue;
135 st = (ev.type != EV_RELEASE);
136 rc_dokey(ev.code, st);
143 static void shutdown()
145 vid_close();
146 pcm_close();
149 void die(char *fmt, ...)
151 va_list ap;
153 va_start(ap, fmt);
154 vfprintf(stderr, fmt, ap);
155 va_end(ap);
156 exit(1);
159 static int bad_signals[] =
161 /* These are all standard, so no need to #ifdef them... */
162 SIGINT, SIGSEGV, SIGTERM, SIGFPE, SIGABRT, SIGILL,
163 #ifdef SIGQUIT
164 SIGQUIT,
165 #endif
166 #ifdef SIGPIPE
167 SIGPIPE,
168 #endif
172 static void fatalsignal(int s)
174 die("Signal %d\n", s);
177 static void catch_signals()
179 int i;
180 for (i = 0; bad_signals[i]; i++)
181 signal(bad_signals[i], fatalsignal);
186 static char *base(char *s)
188 char *p;
189 p = strrchr(s, '/');
190 if (p) return p+1;
191 return s;
195 int main(int argc, char *argv[])
197 int i;
198 char *opt, *arg, *cmd, *s, *rom = 0;
200 /* Avoid initializing video if we don't have to */
201 for (i = 1; i < argc; i++)
203 if (!strcmp(argv[i], "--help"))
204 help(base(argv[0]));
205 else if (!strcmp(argv[i], "--version"))
206 version(base(argv[0]));
207 else if (!strcmp(argv[i], "--copying"))
208 copying();
209 else if (!strcmp(argv[i], "--bind")) i += 2;
210 else if (!strcmp(argv[i], "--source")) i++;
211 else if (!strcmp(argv[i], "--showvars"))
213 show_exports();
214 exit(0);
216 else if (argv[i][0] == '-' && argv[i][1] == '-');
217 else if (argv[i][0] == '-' && argv[i][1]);
218 else rom = argv[i];
221 if (!rom) usage(base(argv[0]));
223 /* If we have special perms, drop them ASAP! */
224 vid_preinit();
226 init_exports();
228 s = strdup(argv[0]);
229 sys_sanitize(s);
230 sys_initpath(s);
232 for (i = 0; defaultconfig[i]; i++)
233 rc_command(defaultconfig[i]);
235 cmd = malloc(strlen(rom) + 11);
236 sprintf(cmd, "source %s", rom);
237 s = strchr(cmd, '.');
238 if (s) *s = 0;
239 strcat(cmd, ".rc");
240 rc_command(cmd);
242 for (i = 1; i < argc; i++)
244 if (!strcmp(argv[i], "--bind"))
246 if (i + 2 >= argc) die("missing arguments to bind\n");
247 cmd = malloc(strlen(argv[i+1]) + strlen(argv[i+2]) + 9);
248 sprintf(cmd, "bind %s \"%s\"", argv[i+1], argv[i+2]);
249 rc_command(cmd);
250 free(cmd);
251 i += 2;
253 else if (!strcmp(argv[i], "--source"))
255 if (i + 1 >= argc) die("missing argument to source\n");
256 cmd = malloc(strlen(argv[i+1]) + 6);
257 sprintf(cmd, "source %s", argv[++i]);
258 rc_command(cmd);
259 free(cmd);
261 else if (!strncmp(argv[i], "--no-", 5))
263 opt = strdup(argv[i]+5);
264 while ((s = strchr(opt, '-'))) *s = '_';
265 cmd = malloc(strlen(opt) + 7);
266 sprintf(cmd, "set %s 0", opt);
267 rc_command(cmd);
268 free(cmd);
269 free(opt);
271 else if (argv[i][0] == '-' && argv[i][1] == '-')
273 opt = strdup(argv[i]+2);
274 if ((s = strchr(opt, '=')))
276 *s = 0;
277 arg = s+1;
279 else arg = "1";
280 while ((s = strchr(opt, '-'))) *s = '_';
281 while ((s = strchr(arg, ','))) *s = ' ';
283 cmd = malloc(strlen(opt) + strlen(arg) + 6);
284 sprintf(cmd, "set %s %s", opt, arg);
286 rc_command(cmd);
287 free(cmd);
288 free(opt);
290 /* short options not yet implemented */
291 else if (argv[i][0] == '-' && argv[i][1]);
294 /* FIXME - make interface modules responsible for atexit() */
295 atexit(shutdown);
296 catch_signals();
297 vid_init();
298 pcm_init();
300 rom = strdup(rom);
301 sys_sanitize(rom);
303 loader_init(rom);
305 emu_reset();
306 emu_run();
308 /* never reached */
309 return 0;