re-generate configure script
[rofl0r-gnuboy.git] / main.c
blobceefd4c11813d14113fad6e33f56d3ba98a8830b
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"
17 #include "mem.h"
18 #include "menu.h"
20 #include "Version"
22 #ifdef __psp__
23 #include <pspmoduleinfo.h>
24 #include <pspthreadman.h>
25 PSP_MODULE_INFO("GnuBoy", 0, 1, 0);
26 PSP_MAIN_THREAD_ATTR(THREAD_ATTR_VFPU | THREAD_ATTR_USER);
27 #endif
30 static char *defaultconfig[] =
32 "bootrom_dmg \"\"",
33 "bootrom_gbc \"\"",
34 "bind esc quit",
35 "bind up +up",
36 "bind down +down",
37 "bind left +left",
38 "bind right +right",
39 "bind d +a",
40 "bind s +b",
41 "bind enter +start",
42 "bind space +select",
43 "bind tab +select",
44 "bind joyup +up",
45 "bind joydown +down",
46 "bind joyleft +left",
47 "bind joyright +right",
48 "bind joy0 +b",
49 "bind joy1 +a",
50 "bind joy2 +select",
51 "bind joy3 +start",
52 "bind 1 \"set saveslot 1\"",
53 "bind 2 \"set saveslot 2\"",
54 "bind 3 \"set saveslot 3\"",
55 "bind 4 \"set saveslot 4\"",
56 "bind 5 \"set saveslot 5\"",
57 "bind 6 \"set saveslot 6\"",
58 "bind 7 \"set saveslot 7\"",
59 "bind 8 \"set saveslot 8\"",
60 "bind 9 \"set saveslot 9\"",
61 "bind 0 \"set saveslot 0\"",
62 "bind ins savestate",
63 "bind del loadstate",
64 "set romdir .",
65 "source gnuboy.rc",
66 NULL
70 static void banner()
72 printf("\ngnuboy " VERSION "\n");
75 static void copyright()
77 banner();
78 printf(
79 "Copyright (C) 2000-2001 Laguna and Gilgamesh\n"
80 "Portions contributed by other authors; see CREDITS for details.\n"
81 "\n"
82 "This program is free software; you can redistribute it and/or modify\n"
83 "it under the terms of the GNU General Public License as published by\n"
84 "the Free Software Foundation; either version 2 of the License, or\n"
85 "(at your option) any later version.\n"
86 "\n"
87 "This program is distributed in the hope that it will be useful,\n"
88 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
89 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
90 "GNU General Public License for more details.\n"
91 "\n"
92 "You should have received a copy of the GNU General Public License\n"
93 "along with this program; if not, write to the Free Software\n"
94 "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
95 "\n");
98 static void usage(char *name)
100 copyright();
101 printf("Type %s --help for detailed help.\n\n", name);
102 exit(1);
105 static void copying()
107 copyright();
108 exit(0);
111 static void joytest()
113 event_t e;
114 char *ename, *kname;
115 printf("press joystick buttons to see their name mappings\n");
116 joy_init();
117 while(1) {
118 ev_poll(0);
119 if(ev_getevent(&e)) {
120 switch(e.type) {
121 case EV_NONE: ename = "none"; break;
122 case EV_PRESS: ename = "press"; break;
123 case EV_RELEASE: ename = "release"; break;
124 case EV_REPEAT: ename = "repeat"; break;
125 case EV_MOUSE: ename = "mouse"; break;
126 default: ename = "unknown";
128 kname = k_keyname(e.code);
129 printf("%s: %s\n", ename, kname ? kname : "<null>");
130 } else {
131 sys_sleep(300);
134 joy_close();
135 exit(0);
138 static void help(char *name)
140 banner();
141 printf("Usage: %s [options] romfile\n", name);
142 printf("\n"
143 " --source FILE read rc commands from FILE\n"
144 " --bind KEY COMMAND bind KEY to perform COMMAND\n"
145 " --VAR=VALUE set rc variable VAR to VALUE\n"
146 " --VAR set VAR to 1 (turn on boolean options)\n"
147 " --no-VAR set VAR to 0 (turn off boolean options)\n"
148 " --showvars list all available rc variables\n"
149 " --help display this help and exit\n"
150 " --version output version information and exit\n"
151 " --copying show copying permissions\n"
152 " --joytest init joystick and show button names pressed\n"
153 " --rominfo show some info about the rom\n"
154 "");
155 exit(0);
158 static void rominfo(char* fn) {
159 extern int rom_load_simple(char *fn);
160 if(rom_load_simple(fn)) {
161 fprintf(stderr, "rom load failed: %s\n", loader_get_error()?loader_get_error():"");
162 exit(1);
164 printf( "rom name:\t%s\n"
165 "mbc type:\t%s\n"
166 "rom size:\t%u KB\n"
167 "ram size:\t%u KB\n"
168 , rom.name, mbc_to_string(mbc.type), mbc.romsize*16, mbc.ramsize*8);
169 exit(0);
172 static void version(char *name)
174 printf("%s-" VERSION "\n", name);
175 exit(0);
179 void doevents()
181 event_t ev;
182 int st;
184 ev_poll(0);
185 while (ev_getevent(&ev))
187 if (ev.type != EV_PRESS && ev.type != EV_RELEASE)
188 continue;
189 st = (ev.type != EV_RELEASE);
190 rc_dokey(ev.code, st);
197 static void shutdown()
199 joy_close();
200 vid_close();
201 pcm_close();
204 void die(char *fmt, ...)
206 va_list ap;
208 va_start(ap, fmt);
209 vfprintf(stderr, fmt, ap);
210 va_end(ap);
211 exit(1);
214 static int bad_signals[] =
216 /* These are all standard, so no need to #ifdef them... */
217 SIGINT, SIGSEGV, SIGTERM, SIGFPE, SIGABRT, SIGILL,
218 #ifdef SIGQUIT
219 SIGQUIT,
220 #endif
221 #ifdef SIGPIPE
222 SIGPIPE,
223 #endif
227 static void fatalsignal(int s)
229 die("Signal %d\n", s);
232 static void catch_signals()
234 int i;
235 for (i = 0; bad_signals[i]; i++)
236 signal(bad_signals[i], fatalsignal);
241 static char *base(char *s)
243 char *p;
244 p = strrchr(s, '/');
245 if (p) return p+1;
246 return s;
249 int load_rom_and_rc(char *rom) {
250 char *s, *cmd = malloc(strlen(rom) + 11);
251 sprintf(cmd, "source %s", rom);
252 s = strchr(cmd, '.');
253 if (s) *s = 0;
254 strcat(cmd, ".rc");
255 rc_command(cmd);
256 free(cmd);
257 rom = strdup(rom);
258 sys_sanitize(rom);
259 if(loader_init(rom)) {
260 /*loader_get_error();*/
261 return -1;
263 emu_reset();
264 return 0;
268 int main(int argc, char *argv[])
270 int i, ri = 0, sv = 0;
271 char *opt, *arg, *cmd, *s, *rom = 0;
273 /* Avoid initializing video if we don't have to */
274 for (i = 1; i < argc; i++)
276 if (!strcmp(argv[i], "--help"))
277 help(base(argv[0]));
278 else if (!strcmp(argv[i], "--version"))
279 version(base(argv[0]));
280 else if (!strcmp(argv[i], "--copying"))
281 copying();
282 else if (!strcmp(argv[i], "--bind")) i += 2;
283 else if (!strcmp(argv[i], "--source")) i++;
284 else if (!strcmp(argv[i], "--showvars")) sv = 1;
285 else if (!strcmp(argv[i], "--joytest"))
286 joytest();
287 else if (!strcmp(argv[i], "--rominfo")) ri = 1;
288 else if (argv[i][0] == '-' && argv[i][1] == '-');
289 else if (argv[i][0] == '-' && argv[i][1]);
290 else rom = argv[i];
293 if (ri && !rom) usage(base(argv[0]));
294 if (ri) rominfo(rom);
296 /* If we have special perms, drop them ASAP! */
297 vid_preinit();
299 init_exports();
301 s = strdup(argv[0]);
302 sys_sanitize(s);
303 sys_initpath(s);
305 for (i = 0; defaultconfig[i]; i++)
306 rc_command(defaultconfig[i]);
308 if (sv) {
309 show_exports();
310 exit(0);
313 for (i = 1; i < argc; i++)
315 if (!strcmp(argv[i], "--bind"))
317 if (i + 2 >= argc) die("missing arguments to bind\n");
318 cmd = malloc(strlen(argv[i+1]) + strlen(argv[i+2]) + 9);
319 sprintf(cmd, "bind %s \"%s\"", argv[i+1], argv[i+2]);
320 rc_command(cmd);
321 free(cmd);
322 i += 2;
324 else if (!strcmp(argv[i], "--source"))
326 if (i + 1 >= argc) die("missing argument to source\n");
327 cmd = malloc(strlen(argv[i+1]) + 6);
328 sprintf(cmd, "source %s", argv[++i]);
329 rc_command(cmd);
330 free(cmd);
332 else if (!strncmp(argv[i], "--no-", 5))
334 opt = strdup(argv[i]+5);
335 while ((s = strchr(opt, '-'))) *s = '_';
336 cmd = malloc(strlen(opt) + 7);
337 sprintf(cmd, "set %s 0", opt);
338 rc_command(cmd);
339 free(cmd);
340 free(opt);
342 else if (argv[i][0] == '-' && argv[i][1] == '-')
344 opt = strdup(argv[i]+2);
345 if ((s = strchr(opt, '=')))
347 *s = 0;
348 arg = s+1;
350 else arg = "1";
351 while ((s = strchr(opt, '-'))) *s = '_';
352 while ((s = strchr(arg, ','))) *s = ' ';
354 cmd = malloc(strlen(opt) + strlen(arg) + 6);
355 sprintf(cmd, "set %s %s", opt, arg);
357 rc_command(cmd);
358 free(cmd);
359 free(opt);
361 /* short options not yet implemented */
362 else if (argv[i][0] == '-' && argv[i][1]);
365 /* FIXME - make interface modules responsible for atexit() */
366 atexit(shutdown);
367 catch_signals();
368 vid_init();
369 joy_init();
370 pcm_init();
371 menu_init();
373 if(rom) load_rom_and_rc(rom);
374 else {
375 rc_command("bind esc menu");
376 menu_initpage(mp_romsel);
377 menu_enter();
379 while(1) {
380 emu_run();
381 /* if we get here it means emu was paused, so enter menu. */
382 pcm_pause(1);
383 menu_initpage(mp_main);
384 menu_enter();
385 pcm_pause(0);
386 emu_pause(0);
389 /* never reached */
390 return 0;