forked from rofl0r/gnuboy
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrccmds.c
More file actions
301 lines (249 loc) · 5.76 KB
/
rccmds.c
File metadata and controls
301 lines (249 loc) · 5.76 KB
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
#include <string.h>
#include <stdlib.h>
#include "defs.h"
#include "rc.h"
#include "rckeys.h"
#include "hw.h"
#include "emu.h"
#include "loader.h"
#include "split.h"
#include "sys.h"
#include "configfile_fk.h"
/*
* define the command functions for the controller pad.
*/
#define CMD_PAD(b, B) \
static int (cmd_ ## b)(int c, char **v) \
{ pad_set((PAD_ ## B), v[0][0] == '+'); return 0; } \
static int (cmd_ ## b)(int c, char **v)
CMD_PAD(up, UP);
CMD_PAD(down, DOWN);
CMD_PAD(left, LEFT);
CMD_PAD(right, RIGHT);
CMD_PAD(a, A);
CMD_PAD(b, B);
/*CMD_PAD(x, A);
CMD_PAD(y, B);*/
CMD_PAD(start, START);
CMD_PAD(select, SELECT);
/*
* the set command is used to set rc-exported variables.
*/
static int cmd_set(int argc, char **argv)
{
if (argc < 3)
return -1;
return rc_setvar(argv[1], argc-2, argv+2);
}
/*
* the following commands allow keys to be bound to perform rc commands.
*/
static int cmd_bind(int argc, char **argv)
{
if (argc < 3)
return -1;
return rc_bindkey(argv[1], argv[2]);
}
static int cmd_unbind(int argc, char **argv)
{
if (argc < 2)
return -1;
return rc_unbindkey(argv[1]);
}
static int cmd_unbindall()
{
rc_unbindall();
return 0;
}
static int cmd_source(int argc, char **argv)
{
if (argc < 2)
return -1;
return rc_sourcefile(argv[1]);
}
static int cmd_quit()
{
exit(0);
/* NOT REACHED */
}
static int cmd_reset()
{
emu_reset();
return 0;
}
static int cmd_savestate(int argc, char **argv)
{
state_save(argc > 1 ? atoi(argv[1]) : -1);
return 0;
}
static int cmd_loadstate(int argc, char **argv)
{
printf("cmd_loadstate %d\n", atoi(argv[1]));
state_load(argc > 1 ? atoi(argv[1]) : -1);
return 0;
}
static int cmd_inc_volume(int argc, char **argv)
{
printf("Increase volume\n");
char shell_cmd[100];
FILE *fp;
/// ----- Compute new value -----
volume_percentage = (volume_percentage > 100 - STEP_CHANGE_VOLUME) ?
100 : (volume_percentage + STEP_CHANGE_VOLUME);
/// ----- Shell cmd ----
sprintf(shell_cmd, "%s %d", SHELL_CMD_VOLUME_SET, volume_percentage);
fp = popen(shell_cmd, "r");
if (fp == NULL)
{
printf("Failed to run command %s\n", shell_cmd);
} else {
pclose(fp);
}
return 0;
}
static int cmd_dec_volume(int argc, char **argv)
{
printf("Decrease volume\n");
char shell_cmd[100];
FILE *fp;
/// ----- Compute new value -----
volume_percentage = (volume_percentage < STEP_CHANGE_VOLUME) ?
0 : (volume_percentage - STEP_CHANGE_VOLUME);
/// ----- Shell cmd ----
sprintf(shell_cmd, "%s %d", SHELL_CMD_VOLUME_SET, volume_percentage);
fp = popen(shell_cmd, "r");
if (fp == NULL)
{
printf("Failed to run command %s\n", shell_cmd);
} else {
pclose(fp);
}
return 0;
}
static int cmd_inc_brightness(int argc, char **argv)
{
printf("Increase brightness\n");
char shell_cmd[100];
FILE *fp;
/// ----- Compute new value -----
brightness_percentage = (brightness_percentage > 100 - STEP_CHANGE_BRIGHTNESS) ?
100 : (brightness_percentage + STEP_CHANGE_BRIGHTNESS);
/// ----- Shell cmd ----
sprintf(shell_cmd, "%s %d", SHELL_CMD_BRIGHTNESS_SET, brightness_percentage);
fp = popen(shell_cmd, "r");
if (fp == NULL)
{
printf("Failed to run command %s\n", shell_cmd);
} else {
pclose(fp);
}
return 0;
}
static int cmd_dec_brightness(int argc, char **argv)
{
printf("Decrease brightness\n");
char shell_cmd[100];
FILE *fp;
/// ----- Compute new value -----
brightness_percentage = (brightness_percentage < STEP_CHANGE_BRIGHTNESS) ?
0 : (brightness_percentage-STEP_CHANGE_BRIGHTNESS);
/// ----- Shell cmd ----
sprintf(shell_cmd, "%s %d", SHELL_CMD_BRIGHTNESS_SET, brightness_percentage);
fp = popen(shell_cmd, "r");
if (fp == NULL)
{
printf("Failed to run command %s\n", shell_cmd);
} else {
pclose(fp);
}
return 0;
}
static int cmd_menu(int argc, char **argv)
{
printf("Launch menu\n");
run_menu_loop();
return 0;
}
static int cmd_aspectratiochange(int argc, char **argv)
{
printf("Change aspect ratio\n");
FILE *fp;
char shell_cmd[100];
aspect_ratio = (aspect_ratio + 1) % NB_ASPECT_RATIOS_TYPES;
/// ----- Hud Msg -----
sprintf(shell_cmd, "%s %d \" DISPLAY MODE: %s\"",
SHELL_CMD_NOTIF_SET, NOTIF_SECONDS_DISP, aspect_ratio_name[aspect_ratio]);
fp = popen(shell_cmd, "r");
if (fp == NULL)
{
printf("Failed to run command %s\n", shell_cmd);
} else {
pclose(fp);
}
// Save config file
configfile_save(cfg_file_rom);
return 0;
}
/*
* table of command names and the corresponding functions to be called
*/
rccmd_t rccmds[] =
{
RCC("set", cmd_set),
RCC("bind", cmd_bind),
RCC("unbind", cmd_unbind),
RCC("unbindall", cmd_unbindall),
RCC("source", cmd_source),
RCC("reset", cmd_reset),
RCC("quit", cmd_quit),
RCC("savestate", cmd_savestate),
RCC("loadstate", cmd_loadstate),
RCC("aspectratiochange", cmd_aspectratiochange),
RCC("menu", cmd_menu),
RCC("volumeinc", cmd_inc_volume),
RCC("volumedec", cmd_dec_volume),
RCC("brightnessinc", cmd_inc_brightness),
RCC("brightnessdec", cmd_dec_brightness),
RCC("+up", cmd_up),
RCC("-up", cmd_up),
RCC("+down", cmd_down),
RCC("-down", cmd_down),
RCC("+left", cmd_left),
RCC("-left", cmd_left),
RCC("+right", cmd_right),
RCC("-right", cmd_right),
RCC("+a", cmd_a),
RCC("-a", cmd_a),
RCC("+b", cmd_b),
RCC("-b", cmd_b),
RCC("+start", cmd_start),
RCC("-start", cmd_start),
RCC("+select", cmd_select),
RCC("-select", cmd_select),
RCC_END
};
int rc_command(char *line)
{
int i, argc, ret;
char *argv[128], *linecopy;
linecopy = malloc(strlen(line)+1);
strcpy(linecopy, line);
argc = splitline(argv, (sizeof argv)/(sizeof argv[0]), linecopy);
if (!argc)
{
free(linecopy);
return -1;
}
for (i = 0; rccmds[i].name; i++)
{
if (!strcmp(argv[0], rccmds[i].name))
{
ret = rccmds[i].func(argc, argv);
free(linecopy);
return ret;
}
}
/* printf("unknown command: %s\n", argv[0]); */
free(linecopy);
return -1;
}