configure.ac: make it possible to turn X11 off
[rofl0r-gnuboy.git] / rccmds.c
blob4747c6fd5c5523245cf32b245f2fe59b580acfaf
1 #include <string.h>
2 #include <stdlib.h>
4 #include "defs.h"
5 #include "rc.h"
6 #include "rckeys.h"
7 #include "hw.h"
8 #include "emu.h"
9 #include "loader.h"
10 #include "split.h"
11 #include "menu.h"
12 #include "sys.h"
16 * define the command functions for the controller pad.
19 #define CMD_PAD(b, B) \
20 static int (cmd_ ## b)(int c, char **v) \
21 { pad_set((PAD_ ## B), v[0][0] == '+'); return 0; } \
22 static int (cmd_ ## b)(int c, char **v)
24 CMD_PAD(up, UP);
25 CMD_PAD(down, DOWN);
26 CMD_PAD(left, LEFT);
27 CMD_PAD(right, RIGHT);
28 CMD_PAD(a, A);
29 CMD_PAD(b, B);
30 CMD_PAD(start, START);
31 CMD_PAD(select, SELECT);
35 * the set command is used to set rc-exported variables.
38 static int cmd_set(int argc, char **argv)
40 if (argc < 3)
41 return -1;
42 return rc_setvar(argv[1], argc-2, argv+2);
46 * the toggle command is used to switch rc-exported bool variables.
49 static int cmd_toggle(int argc, char **argv)
51 static const char* bools[] = {"0", "1", 0};
52 if (argc < 2)
53 return -1;
54 return rc_setvar(argv[1], 1, rc_getint(argv[1])?(void*)bools:(void*)(bools+1));
59 * the following commands allow keys to be bound to perform rc commands.
62 static int cmd_bind(int argc, char **argv)
64 if (argc < 3)
65 return -1;
66 return rc_bindkey(argv[1], argv[2]);
69 static int cmd_unbind(int argc, char **argv)
71 if (argc < 2)
72 return -1;
73 return rc_unbindkey(argv[1]);
76 static int cmd_unbindall()
78 rc_unbindall();
79 return 0;
82 static int cmd_source(int argc, char **argv)
84 if (argc < 2)
85 return -1;
86 return rc_sourcefile(argv[1]);
89 static int cmd_quit()
91 exit(0);
92 /* NOT REACHED */
95 static int cmd_reset()
97 emu_reset();
98 return 0;
101 static int cmd_savestate(int argc, char **argv)
103 state_save(argc > 1 ? atoi(argv[1]) : -1);
104 return 0;
107 static int cmd_loadstate(int argc, char **argv)
109 state_load(argc > 1 ? atoi(argv[1]) : -1);
110 return 0;
113 static int cmd_menu(int argc, char **argv)
115 /* some of the actions we perform from the menu require us
116 to break out of the emu_run main loop, so we just signal
117 it to return and then let main() enter the menu instead. */
118 emu_pause(1);
119 return 0;
124 * table of command names and the corresponding functions to be called
127 rccmd_t rccmds[] =
129 RCC("set", cmd_set),
130 RCC("toggle", cmd_toggle),
131 RCC("bind", cmd_bind),
132 RCC("unbind", cmd_unbind),
133 RCC("unbindall", cmd_unbindall),
134 RCC("source", cmd_source),
135 RCC("reset", cmd_reset),
136 RCC("quit", cmd_quit),
137 RCC("menu", cmd_menu),
138 RCC("savestate", cmd_savestate),
139 RCC("loadstate", cmd_loadstate),
141 RCC("+up", cmd_up),
142 RCC("-up", cmd_up),
143 RCC("+down", cmd_down),
144 RCC("-down", cmd_down),
145 RCC("+left", cmd_left),
146 RCC("-left", cmd_left),
147 RCC("+right", cmd_right),
148 RCC("-right", cmd_right),
149 RCC("+a", cmd_a),
150 RCC("-a", cmd_a),
151 RCC("+b", cmd_b),
152 RCC("-b", cmd_b),
153 RCC("+start", cmd_start),
154 RCC("-start", cmd_start),
155 RCC("+select", cmd_select),
156 RCC("-select", cmd_select),
158 RCC_END
165 int rc_command(char *line)
167 int i, argc, ret;
168 char *argv[128], *linecopy;
170 linecopy = malloc(strlen(line)+1);
171 strcpy(linecopy, line);
173 argc = splitline(argv, (sizeof argv)/(sizeof argv[0]), linecopy);
174 if (!argc)
176 free(linecopy);
177 return -1;
180 for (i = 0; rccmds[i].name; i++)
182 if (!strcmp(argv[0], rccmds[i].name))
184 ret = rccmds[i].func(argc, argv);
185 free(linecopy);
186 return ret;
190 /* printf("unknown command: %s\n", argv[0]); */
191 free(linecopy);
193 return -1;