implement new "toggle" rc command
[rofl0r-gnuboy.git] / rccmds.c
blob04402be78a9ed4631064cbd35ce48ae9b293a4a3
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"
14 * define the command functions for the controller pad.
17 #define CMD_PAD(b, B) \
18 static int (cmd_ ## b)(int c, char **v) \
19 { pad_set((PAD_ ## B), v[0][0] == '+'); return 0; } \
20 static int (cmd_ ## b)(int c, char **v)
22 CMD_PAD(up, UP);
23 CMD_PAD(down, DOWN);
24 CMD_PAD(left, LEFT);
25 CMD_PAD(right, RIGHT);
26 CMD_PAD(a, A);
27 CMD_PAD(b, B);
28 CMD_PAD(start, START);
29 CMD_PAD(select, SELECT);
33 * the set command is used to set rc-exported variables.
36 static int cmd_set(int argc, char **argv)
38 if (argc < 3)
39 return -1;
40 return rc_setvar(argv[1], argc-2, argv+2);
44 * the toggle command is used to switch rc-exported bool variables.
47 static int cmd_toggle(int argc, char **argv)
49 static const char* bools[] = {"0", "1", 0};
50 if (argc < 2)
51 return -1;
52 return rc_setvar(argv[1], 1, rc_getint(argv[1])?bools:bools+1);
57 * the following commands allow keys to be bound to perform rc commands.
60 static int cmd_bind(int argc, char **argv)
62 if (argc < 3)
63 return -1;
64 return rc_bindkey(argv[1], argv[2]);
67 static int cmd_unbind(int argc, char **argv)
69 if (argc < 2)
70 return -1;
71 return rc_unbindkey(argv[1]);
74 static int cmd_unbindall()
76 rc_unbindall();
77 return 0;
80 static int cmd_source(int argc, char **argv)
82 if (argc < 2)
83 return -1;
84 return rc_sourcefile(argv[1]);
87 static int cmd_quit()
89 exit(0);
90 /* NOT REACHED */
93 static int cmd_reset()
95 emu_reset();
96 return 0;
99 static int cmd_savestate(int argc, char **argv)
101 state_save(argc > 1 ? atoi(argv[1]) : -1);
102 return 0;
105 static int cmd_loadstate(int argc, char **argv)
107 state_load(argc > 1 ? atoi(argv[1]) : -1);
108 return 0;
114 * table of command names and the corresponding functions to be called
117 rccmd_t rccmds[] =
119 RCC("set", cmd_set),
120 RCC("toggle", cmd_toggle),
121 RCC("bind", cmd_bind),
122 RCC("unbind", cmd_unbind),
123 RCC("unbindall", cmd_unbindall),
124 RCC("source", cmd_source),
125 RCC("reset", cmd_reset),
126 RCC("quit", cmd_quit),
127 RCC("savestate", cmd_savestate),
128 RCC("loadstate", cmd_loadstate),
130 RCC("+up", cmd_up),
131 RCC("-up", cmd_up),
132 RCC("+down", cmd_down),
133 RCC("-down", cmd_down),
134 RCC("+left", cmd_left),
135 RCC("-left", cmd_left),
136 RCC("+right", cmd_right),
137 RCC("-right", cmd_right),
138 RCC("+a", cmd_a),
139 RCC("-a", cmd_a),
140 RCC("+b", cmd_b),
141 RCC("-b", cmd_b),
142 RCC("+start", cmd_start),
143 RCC("-start", cmd_start),
144 RCC("+select", cmd_select),
145 RCC("-select", cmd_select),
147 RCC_END
154 int rc_command(char *line)
156 int i, argc, ret;
157 char *argv[128], *linecopy;
159 linecopy = malloc(strlen(line)+1);
160 strcpy(linecopy, line);
162 argc = splitline(argv, (sizeof argv)/(sizeof argv[0]), linecopy);
163 if (!argc)
165 free(linecopy);
166 return -1;
169 for (i = 0; rccmds[i].name; i++)
171 if (!strcmp(argv[0], rccmds[i].name))
173 ret = rccmds[i].func(argc, argv);
174 free(linecopy);
175 return ret;
179 /* printf("unknown command: %s\n", argv[0]); */
180 free(linecopy);
182 return -1;