95a2ec38 |
1 | /*\r |
2 | * (C) GraÅžvydas "notaz" Ignotas, 2006-2010\r |
3 | *\r |
4 | * This work is licensed under the terms of any of these licenses\r |
5 | * (at your option):\r |
6 | * - GNU GPL, version 2 or later.\r |
7 | * - GNU LGPL, version 2.1 or later.\r |
8 | * See the COPYING file in the top-level directory.\r |
9 | */\r |
c7a4ff64 |
10 | \r |
11 | #include <stdio.h>\r |
12 | #include <string.h>\r |
13 | #include <stdlib.h>\r |
14 | #include <stdarg.h>\r |
15 | \r |
16 | #include "menu.h"\r |
17 | #include "fonts.h"\r |
18 | #include "readpng.h"\r |
19 | #include "lprintf.h"\r |
fce20e73 |
20 | #include "input.h"\r |
24b24674 |
21 | #include "plat.h"\r |
049a6b3e |
22 | #include "posix.h"\r |
c7a4ff64 |
23 | \r |
049a6b3e |
24 | static char static_buff[64];\r |
3ffee69c |
25 | static int menu_error_time = 0;\r |
d2f29611 |
26 | char menu_error_msg[64] = { 0, };\r |
c66f49e6 |
27 | void *g_menuscreen_ptr;\r |
28 | void *g_menubg_src_ptr;\r |
d2f29611 |
29 | void *g_menubg_ptr;\r |
367b6f1f |
30 | \r |
b6eccda3 |
31 | #if !MSCREEN_SIZE_FIXED\r |
94a1d22e |
32 | int g_menuscreen_w;\r |
33 | int g_menuscreen_h;\r |
c66f49e6 |
34 | #endif\r |
35 | \r |
3ffee69c |
36 | static unsigned char *menu_font_data = NULL;\r |
c7a4ff64 |
37 | static int menu_text_color = 0xffff; // default to white\r |
38 | static int menu_sel_color = -1; // disabled\r |
f15ca4db |
39 | \r |
40 | /* note: these might become non-constant in future */\r |
3ffee69c |
41 | #if MENU_X2\r |
42 | static const int me_mfont_w = 16, me_mfont_h = 20;\r |
f15ca4db |
43 | static const int me_sfont_w = 12, me_sfont_h = 20;\r |
3ffee69c |
44 | #else\r |
45 | static const int me_mfont_w = 8, me_mfont_h = 10;\r |
f15ca4db |
46 | static const int me_sfont_w = 6, me_sfont_h = 10;\r |
3ffee69c |
47 | #endif\r |
c7a4ff64 |
48 | \r |
49 | // draws text to current bbp16 screen\r |
50 | static void text_out16_(int x, int y, const char *text, int color)\r |
51 | {\r |
d5d59eb4 |
52 | int i, lh, tr, tg, tb, len;\r |
c66f49e6 |
53 | unsigned short *dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w;\r |
c7a4ff64 |
54 | tr = (color & 0xf800) >> 8;\r |
55 | tg = (color & 0x07e0) >> 3;\r |
56 | tb = (color & 0x001f) << 3;\r |
57 | \r |
58 | if (text == (void *)1)\r |
59 | {\r |
60 | // selector symbol\r |
61 | text = "";\r |
62 | len = 1;\r |
63 | }\r |
64 | else\r |
049a6b3e |
65 | {\r |
66 | const char *p;\r |
67 | for (p = text; *p != 0 && *p != '\n'; p++)\r |
68 | ;\r |
69 | len = p - text;\r |
70 | }\r |
c7a4ff64 |
71 | \r |
d5d59eb4 |
72 | lh = me_mfont_h;\r |
c66f49e6 |
73 | if (y + lh > g_menuscreen_h)\r |
74 | lh = g_menuscreen_h - y;\r |
d5d59eb4 |
75 | \r |
c7a4ff64 |
76 | for (i = 0; i < len; i++)\r |
77 | {\r |
3ffee69c |
78 | unsigned char *src = menu_font_data + (unsigned int)text[i] * me_mfont_w * me_mfont_h / 2;\r |
c7a4ff64 |
79 | unsigned short *dst = dest;\r |
d5d59eb4 |
80 | int u, l;\r |
81 | \r |
c66f49e6 |
82 | for (l = 0; l < lh; l++, dst += g_menuscreen_w - me_mfont_w)\r |
c7a4ff64 |
83 | {\r |
3ffee69c |
84 | for (u = me_mfont_w / 2; u > 0; u--, src++)\r |
c7a4ff64 |
85 | {\r |
86 | int c, r, g, b;\r |
87 | c = *src >> 4;\r |
88 | r = (*dst & 0xf800) >> 8;\r |
89 | g = (*dst & 0x07e0) >> 3;\r |
90 | b = (*dst & 0x001f) << 3;\r |
91 | r = (c^0xf)*r/15 + c*tr/15;\r |
92 | g = (c^0xf)*g/15 + c*tg/15;\r |
93 | b = (c^0xf)*b/15 + c*tb/15;\r |
94 | *dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);\r |
95 | c = *src & 0xf;\r |
96 | r = (*dst & 0xf800) >> 8;\r |
97 | g = (*dst & 0x07e0) >> 3;\r |
98 | b = (*dst & 0x001f) << 3;\r |
99 | r = (c^0xf)*r/15 + c*tr/15;\r |
100 | g = (c^0xf)*g/15 + c*tg/15;\r |
101 | b = (c^0xf)*b/15 + c*tb/15;\r |
102 | *dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);\r |
103 | }\r |
104 | }\r |
3ffee69c |
105 | dest += me_mfont_w;\r |
c7a4ff64 |
106 | }\r |
107 | }\r |
108 | \r |
109 | void text_out16(int x, int y, const char *texto, ...)\r |
110 | {\r |
111 | va_list args;\r |
24b24674 |
112 | char buffer[256];\r |
c66f49e6 |
113 | int maxw = (g_menuscreen_w - x) / me_mfont_w;\r |
c7a4ff64 |
114 | \r |
73a85369 |
115 | if (maxw < 0)\r |
116 | return;\r |
117 | \r |
24b24674 |
118 | va_start(args, texto);\r |
119 | vsnprintf(buffer, sizeof(buffer), texto, args);\r |
c7a4ff64 |
120 | va_end(args);\r |
121 | \r |
73a85369 |
122 | if (maxw > sizeof(buffer) - 1)\r |
123 | maxw = sizeof(buffer) - 1;\r |
24b24674 |
124 | buffer[maxw] = 0;\r |
125 | \r |
c7a4ff64 |
126 | text_out16_(x,y,buffer,menu_text_color);\r |
127 | }\r |
128 | \r |
f15ca4db |
129 | /* draws in 6x8 font, might multiply size by integer */\r |
049a6b3e |
130 | static void smalltext_out16_(int x, int y, const char *texto, int color)\r |
c7a4ff64 |
131 | {\r |
c7a4ff64 |
132 | unsigned char *src;\r |
133 | unsigned short *dst;\r |
f15ca4db |
134 | int multiplier = me_sfont_w / 6;\r |
135 | int i;\r |
c7a4ff64 |
136 | \r |
f15ca4db |
137 | for (i = 0;; i++, x += me_sfont_w)\r |
c7a4ff64 |
138 | {\r |
139 | unsigned char c = (unsigned char) texto[i];\r |
140 | int h = 8;\r |
141 | \r |
f15ca4db |
142 | if (!c || c == '\n')\r |
143 | break;\r |
c7a4ff64 |
144 | \r |
145 | src = fontdata6x8[c];\r |
c66f49e6 |
146 | dst = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w;\r |
c7a4ff64 |
147 | \r |
148 | while (h--)\r |
149 | {\r |
f15ca4db |
150 | int m, w2, h2;\r |
151 | for (h2 = multiplier; h2 > 0; h2--)\r |
c7a4ff64 |
152 | {\r |
f15ca4db |
153 | for (m = 0x20; m; m >>= 1) {\r |
154 | if (*src & m)\r |
155 | for (w2 = multiplier; w2 > 0; w2--)\r |
156 | *dst++ = color;\r |
157 | else\r |
158 | dst += multiplier;\r |
159 | }\r |
160 | \r |
c66f49e6 |
161 | dst += g_menuscreen_w - me_sfont_w;\r |
c7a4ff64 |
162 | }\r |
163 | src++;\r |
c7a4ff64 |
164 | }\r |
165 | }\r |
166 | }\r |
167 | \r |
3ffee69c |
168 | static void smalltext_out16(int x, int y, const char *texto, int color)\r |
c7a4ff64 |
169 | {\r |
e31266dd |
170 | char buffer[128];\r |
c66f49e6 |
171 | int maxw = (g_menuscreen_w - x) / me_sfont_w;\r |
73a85369 |
172 | \r |
173 | if (maxw < 0)\r |
174 | return;\r |
c7a4ff64 |
175 | \r |
049a6b3e |
176 | strncpy(buffer, texto, sizeof(buffer));\r |
73a85369 |
177 | if (maxw > sizeof(buffer) - 1)\r |
178 | maxw = sizeof(buffer) - 1;\r |
179 | buffer[maxw] = 0;\r |
c7a4ff64 |
180 | \r |
049a6b3e |
181 | smalltext_out16_(x, y, buffer, color);\r |
c7a4ff64 |
182 | }\r |
183 | \r |
049a6b3e |
184 | static void menu_draw_selection(int x, int y, int w)\r |
c7a4ff64 |
185 | {\r |
186 | int i, h;\r |
187 | unsigned short *dst, *dest;\r |
188 | \r |
189 | text_out16_(x, y, (void *)1, (menu_sel_color < 0) ? menu_text_color : menu_sel_color);\r |
190 | \r |
191 | if (menu_sel_color < 0) return; // no selection hilight\r |
192 | \r |
193 | if (y > 0) y--;\r |
c66f49e6 |
194 | dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w + me_mfont_w * 2 - 2;\r |
3ffee69c |
195 | for (h = me_mfont_h + 1; h > 0; h--)\r |
c7a4ff64 |
196 | {\r |
197 | dst = dest;\r |
f6eaae4f |
198 | for (i = w - (me_mfont_w * 2 - 2); i > 0; i--)\r |
c7a4ff64 |
199 | *dst++ = menu_sel_color;\r |
c66f49e6 |
200 | dest += g_menuscreen_w;\r |
c7a4ff64 |
201 | }\r |
202 | }\r |
203 | \r |
204 | static int parse_hex_color(char *buff)\r |
205 | {\r |
206 | char *endp = buff;\r |
207 | int t = (int) strtoul(buff, &endp, 16);\r |
703e4c7b |
208 | if (endp != buff)\r |
209 | #ifdef PSP\r |
210 | return ((t<<8)&0xf800) | ((t>>5)&0x07e0) | ((t>>19)&0x1f);\r |
211 | #else\r |
212 | return ((t>>8)&0xf800) | ((t>>5)&0x07e0) | ((t>>3)&0x1f);\r |
213 | #endif\r |
c7a4ff64 |
214 | return -1;\r |
215 | }\r |
216 | \r |
217 | void menu_init(void)\r |
218 | {\r |
3ffee69c |
219 | int i, c, l;\r |
220 | unsigned char *fd, *fds;\r |
c7a4ff64 |
221 | char buff[256];\r |
222 | FILE *f;\r |
223 | \r |
3ffee69c |
224 | if (menu_font_data != NULL)\r |
225 | free(menu_font_data);\r |
226 | \r |
227 | menu_font_data = calloc((MENU_X2 ? 256 * 320 : 128 * 160) / 2, 1);\r |
228 | if (menu_font_data == NULL)\r |
229 | return;\r |
230 | \r |
231 | // generate default 8x10 font from fontdata8x8\r |
232 | for (c = 0, fd = menu_font_data; c < 256; c++)\r |
c7a4ff64 |
233 | {\r |
234 | for (l = 0; l < 8; l++)\r |
235 | {\r |
236 | unsigned char fd8x8 = fontdata8x8[c*8+l];\r |
3ffee69c |
237 | if (fd8x8&0x80) *fd = 0xf0;\r |
c7a4ff64 |
238 | if (fd8x8&0x40) *fd |= 0x0f; fd++;\r |
3ffee69c |
239 | if (fd8x8&0x20) *fd = 0xf0;\r |
c7a4ff64 |
240 | if (fd8x8&0x10) *fd |= 0x0f; fd++;\r |
3ffee69c |
241 | if (fd8x8&0x08) *fd = 0xf0;\r |
c7a4ff64 |
242 | if (fd8x8&0x04) *fd |= 0x0f; fd++;\r |
3ffee69c |
243 | if (fd8x8&0x02) *fd = 0xf0;\r |
c7a4ff64 |
244 | if (fd8x8&0x01) *fd |= 0x0f; fd++;\r |
245 | }\r |
246 | fd += 8*2/2; // 2 empty lines\r |
247 | }\r |
248 | \r |
3ffee69c |
249 | if (MENU_X2) {\r |
250 | // expand default font\r |
251 | fds = menu_font_data + 128 * 160 / 2 - 4;\r |
252 | fd = menu_font_data + 256 * 320 / 2 - 1;\r |
253 | for (c = 255; c >= 0; c--)\r |
254 | {\r |
255 | for (l = 9; l >= 0; l--, fds -= 4)\r |
256 | {\r |
257 | for (i = 3; i >= 0; i--) {\r |
258 | int px = fds[i] & 0x0f;\r |
259 | *fd-- = px | (px << 4);\r |
260 | px = (fds[i] >> 4) & 0x0f;\r |
261 | *fd-- = px | (px << 4);\r |
262 | }\r |
263 | for (i = 3; i >= 0; i--) {\r |
264 | int px = fds[i] & 0x0f;\r |
265 | *fd-- = px | (px << 4);\r |
266 | px = (fds[i] >> 4) & 0x0f;\r |
267 | *fd-- = px | (px << 4);\r |
268 | }\r |
269 | }\r |
270 | }\r |
271 | }\r |
272 | \r |
c7a4ff64 |
273 | // load custom font and selector (stored as 1st symbol in font table)\r |
45deb409 |
274 | emu_make_path(buff, "skin/font.png", sizeof(buff));\r |
f6eaae4f |
275 | readpng(menu_font_data, buff, READPNG_FONT,\r |
276 | MENU_X2 ? 256 : 128, MENU_X2 ? 320 : 160);\r |
3ffee69c |
277 | // default selector symbol is '>'\r |
278 | memcpy(menu_font_data, menu_font_data + ((int)'>') * me_mfont_w * me_mfont_h / 2,\r |
279 | me_mfont_w * me_mfont_h / 2);\r |
45deb409 |
280 | emu_make_path(buff, "skin/selector.png", sizeof(buff));\r |
209a7eff |
281 | readpng(menu_font_data, buff, READPNG_SELECTOR, me_mfont_w, me_mfont_h);\r |
c7a4ff64 |
282 | \r |
283 | // load custom colors\r |
45deb409 |
284 | emu_make_path(buff, "skin/skin.txt", sizeof(buff));\r |
285 | f = fopen(buff, "r");\r |
c7a4ff64 |
286 | if (f != NULL)\r |
287 | {\r |
288 | lprintf("found skin.txt\n");\r |
289 | while (!feof(f))\r |
290 | {\r |
5f7b5155 |
291 | if (fgets(buff, sizeof(buff), f) == NULL)\r |
292 | break;\r |
c7a4ff64 |
293 | if (buff[0] == '#' || buff[0] == '/') continue; // comment\r |
294 | if (buff[0] == '\r' || buff[0] == '\n') continue; // empty line\r |
295 | if (strncmp(buff, "text_color=", 11) == 0)\r |
296 | {\r |
297 | int tmp = parse_hex_color(buff+11);\r |
298 | if (tmp >= 0) menu_text_color = tmp;\r |
299 | else lprintf("skin.txt: parse error for text_color\n");\r |
300 | }\r |
301 | else if (strncmp(buff, "selection_color=", 16) == 0)\r |
302 | {\r |
303 | int tmp = parse_hex_color(buff+16);\r |
304 | if (tmp >= 0) menu_sel_color = tmp;\r |
305 | else lprintf("skin.txt: parse error for selection_color\n");\r |
306 | }\r |
307 | else\r |
308 | lprintf("skin.txt: parse error: %s\n", buff);\r |
309 | }\r |
310 | fclose(f);\r |
311 | }\r |
312 | }\r |
313 | \r |
902972d1 |
314 | static void menu_draw_begin(int need_bg)\r |
315 | {\r |
316 | plat_video_menu_begin();\r |
317 | if (need_bg)\r |
318 | memcpy(g_menuscreen_ptr, g_menubg_ptr, g_menuscreen_w * g_menuscreen_h * 2);\r |
319 | }\r |
320 | \r |
321 | static void menu_draw_end(void)\r |
322 | {\r |
323 | plat_video_menu_end();\r |
324 | }\r |
c7a4ff64 |
325 | \r |
d2f29611 |
326 | static void menu_darken_bg(void *dst, void *src, int pixels, int darker)\r |
327 | {\r |
328 | unsigned int *dest = dst;\r |
329 | unsigned int *sorc = src;\r |
330 | pixels /= 2;\r |
331 | if (darker)\r |
332 | {\r |
333 | while (pixels--)\r |
334 | {\r |
335 | unsigned int p = *sorc++;\r |
336 | *dest++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);\r |
337 | }\r |
338 | }\r |
339 | else\r |
340 | {\r |
341 | while (pixels--)\r |
342 | {\r |
343 | unsigned int p = *sorc++;\r |
344 | *dest++ = (p&0xf79ef79e)>>1;\r |
345 | }\r |
346 | }\r |
347 | }\r |
348 | \r |
049a6b3e |
349 | static int me_id2offset(const menu_entry *ent, menu_id id)\r |
c7a4ff64 |
350 | {\r |
351 | int i;\r |
24b24674 |
352 | for (i = 0; ent->name; ent++, i++)\r |
353 | if (ent->id == id) return i;\r |
c7a4ff64 |
354 | \r |
355 | lprintf("%s: id %i not found\n", __FUNCTION__, id);\r |
356 | return 0;\r |
357 | }\r |
358 | \r |
049a6b3e |
359 | static void me_enable(menu_entry *entries, menu_id id, int enable)\r |
c7a4ff64 |
360 | {\r |
24b24674 |
361 | int i = me_id2offset(entries, id);\r |
c7a4ff64 |
362 | entries[i].enabled = enable;\r |
363 | }\r |
364 | \r |
049a6b3e |
365 | static int me_count(const menu_entry *ent)\r |
c7a4ff64 |
366 | {\r |
24b24674 |
367 | int ret;\r |
c7a4ff64 |
368 | \r |
24b24674 |
369 | for (ret = 0; ent->name; ent++, ret++)\r |
370 | ;\r |
c7a4ff64 |
371 | \r |
372 | return ret;\r |
373 | }\r |
374 | \r |
053bef76 |
375 | static void me_draw(const menu_entry *entries, int sel, void (*draw_more)(void))\r |
24b24674 |
376 | {\r |
f6eaae4f |
377 | const menu_entry *ent, *ent_sel = entries;\r |
24b24674 |
378 | int x, y, w = 0, h = 0;\r |
8e77275e |
379 | int offs, col2_offs = 27 * me_mfont_w;\r |
f6eaae4f |
380 | int vi_sel_ln = 0;\r |
24b24674 |
381 | const char *name;\r |
24b24674 |
382 | int i, n;\r |
383 | \r |
384 | /* calculate size of menu rect */\r |
385 | for (ent = entries, i = n = 0; ent->name; ent++, i++)\r |
386 | {\r |
387 | int wt;\r |
388 | \r |
389 | if (!ent->enabled)\r |
390 | continue;\r |
391 | \r |
f6eaae4f |
392 | if (i == sel) {\r |
393 | ent_sel = ent;\r |
394 | vi_sel_ln = n;\r |
395 | }\r |
24b24674 |
396 | \r |
397 | name = NULL;\r |
3ffee69c |
398 | wt = strlen(ent->name) * me_mfont_w;\r |
24b24674 |
399 | if (wt == 0 && ent->generate_name)\r |
049a6b3e |
400 | name = ent->generate_name(ent->id, &offs);\r |
24b24674 |
401 | if (name != NULL)\r |
3ffee69c |
402 | wt = strlen(name) * me_mfont_w;\r |
24b24674 |
403 | \r |
404 | if (ent->beh != MB_NONE)\r |
405 | {\r |
8e77275e |
406 | if (wt > col2_offs)\r |
407 | col2_offs = wt + me_mfont_w;\r |
408 | wt = col2_offs;\r |
24b24674 |
409 | \r |
410 | switch (ent->beh) {\r |
209a7eff |
411 | case MB_NONE:\r |
412 | break;\r |
24b24674 |
413 | case MB_OPT_ONOFF:\r |
209a7eff |
414 | case MB_OPT_RANGE:\r |
415 | wt += me_mfont_w * 3;\r |
416 | break;\r |
24b24674 |
417 | case MB_OPT_CUSTOM:\r |
049a6b3e |
418 | case MB_OPT_CUSTONOFF:\r |
419 | case MB_OPT_CUSTRANGE:\r |
24b24674 |
420 | name = NULL;\r |
049a6b3e |
421 | offs = 0;\r |
24b24674 |
422 | if (ent->generate_name != NULL)\r |
049a6b3e |
423 | name = ent->generate_name(ent->id, &offs);\r |
24b24674 |
424 | if (name != NULL)\r |
3ffee69c |
425 | wt += (strlen(name) + offs) * me_mfont_w;\r |
24b24674 |
426 | break;\r |
0c9ae592 |
427 | case MB_OPT_ENUM:\r |
428 | wt += 10 * me_mfont_w;\r |
429 | break;\r |
24b24674 |
430 | }\r |
431 | }\r |
432 | \r |
433 | if (wt > w)\r |
434 | w = wt;\r |
435 | n++;\r |
436 | }\r |
3ffee69c |
437 | h = n * me_mfont_h;\r |
438 | w += me_mfont_w * 2; /* selector */\r |
24b24674 |
439 | \r |
c66f49e6 |
440 | if (w > g_menuscreen_w) {\r |
441 | lprintf("width %d > %d\n", w, g_menuscreen_w);\r |
442 | w = g_menuscreen_w;\r |
24b24674 |
443 | }\r |
c66f49e6 |
444 | if (h > g_menuscreen_h) {\r |
445 | lprintf("height %d > %d\n", w, g_menuscreen_h);\r |
446 | h = g_menuscreen_h;\r |
24b24674 |
447 | }\r |
448 | \r |
95a2ec38 |
449 | x = g_menuscreen_w / 2 - w / 2;\r |
c66f49e6 |
450 | y = g_menuscreen_h / 2 - h / 2;\r |
95a2ec38 |
451 | #ifdef MENU_ALIGN_LEFT\r |
452 | if (x > 12) x = 12;\r |
453 | #endif\r |
24b24674 |
454 | \r |
455 | /* draw */\r |
902972d1 |
456 | menu_draw_begin(1);\r |
f6eaae4f |
457 | menu_draw_selection(x, y + vi_sel_ln * me_mfont_h, w);\r |
3ffee69c |
458 | x += me_mfont_w * 2;\r |
24b24674 |
459 | \r |
460 | for (ent = entries; ent->name; ent++)\r |
461 | {\r |
0c9ae592 |
462 | const char **names;\r |
463 | int len;\r |
464 | \r |
24b24674 |
465 | if (!ent->enabled)\r |
466 | continue;\r |
467 | \r |
468 | name = ent->name;\r |
469 | if (strlen(name) == 0) {\r |
470 | if (ent->generate_name)\r |
049a6b3e |
471 | name = ent->generate_name(ent->id, &offs);\r |
24b24674 |
472 | }\r |
473 | if (name != NULL)\r |
3ffee69c |
474 | text_out16(x, y, name);\r |
24b24674 |
475 | \r |
476 | switch (ent->beh) {\r |
477 | case MB_NONE:\r |
478 | break;\r |
479 | case MB_OPT_ONOFF:\r |
8e77275e |
480 | text_out16(x + col2_offs, y, (*(int *)ent->var & ent->mask) ? "ON" : "OFF");\r |
24b24674 |
481 | break;\r |
482 | case MB_OPT_RANGE:\r |
8e77275e |
483 | text_out16(x + col2_offs, y, "%i", *(int *)ent->var);\r |
24b24674 |
484 | break;\r |
485 | case MB_OPT_CUSTOM:\r |
049a6b3e |
486 | case MB_OPT_CUSTONOFF:\r |
487 | case MB_OPT_CUSTRANGE:\r |
24b24674 |
488 | name = NULL;\r |
049a6b3e |
489 | offs = 0;\r |
24b24674 |
490 | if (ent->generate_name)\r |
049a6b3e |
491 | name = ent->generate_name(ent->id, &offs);\r |
24b24674 |
492 | if (name != NULL)\r |
8e77275e |
493 | text_out16(x + col2_offs + offs * me_mfont_w, y, "%s", name);\r |
24b24674 |
494 | break;\r |
0c9ae592 |
495 | case MB_OPT_ENUM:\r |
496 | names = (const char **)ent->data;\r |
497 | offs = 0;\r |
498 | for (i = 0; names[i] != NULL; i++) {\r |
499 | len = strlen(names[i]);\r |
500 | if (len > 10)\r |
501 | offs = 10 - len - 2;\r |
95a2ec38 |
502 | if (i == *(unsigned char *)ent->var) {\r |
0c9ae592 |
503 | text_out16(x + col2_offs + offs * me_mfont_w, y, "%s", names[i]);\r |
504 | break;\r |
505 | }\r |
506 | }\r |
507 | break;\r |
24b24674 |
508 | }\r |
509 | \r |
3ffee69c |
510 | y += me_mfont_h;\r |
24b24674 |
511 | }\r |
512 | \r |
0c9ae592 |
513 | /* display help or message if we have one */\r |
c66f49e6 |
514 | h = (g_menuscreen_h - h) / 2; // bottom area height\r |
3ffee69c |
515 | if (menu_error_msg[0] != 0) {\r |
0c9ae592 |
516 | if (h >= me_mfont_h + 4)\r |
c66f49e6 |
517 | text_out16(5, g_menuscreen_h - me_mfont_h - 4, menu_error_msg);\r |
049a6b3e |
518 | else\r |
519 | lprintf("menu msg doesn't fit!\n");\r |
520 | \r |
3ffee69c |
521 | if (plat_get_ticks_ms() - menu_error_time > 2048)\r |
522 | menu_error_msg[0] = 0;\r |
049a6b3e |
523 | }\r |
f6eaae4f |
524 | else if (ent_sel->help != NULL) {\r |
525 | const char *tmp = ent_sel->help;\r |
0c9ae592 |
526 | int l;\r |
527 | for (l = 0; tmp != NULL && *tmp != 0; l++)\r |
528 | tmp = strchr(tmp + 1, '\n');\r |
529 | if (h >= l * me_sfont_h + 4)\r |
f6eaae4f |
530 | for (tmp = ent_sel->help; l > 0; l--, tmp = strchr(tmp, '\n') + 1)\r |
c66f49e6 |
531 | smalltext_out16(5, g_menuscreen_h - (l * me_sfont_h + 4), tmp, 0xffff);\r |
0c9ae592 |
532 | }\r |
049a6b3e |
533 | \r |
053bef76 |
534 | if (draw_more != NULL)\r |
535 | draw_more();\r |
536 | \r |
902972d1 |
537 | menu_draw_end();\r |
c7a4ff64 |
538 | }\r |
539 | \r |
5d34757d |
540 | static int me_process(menu_entry *entry, int is_next, int is_lr)\r |
c7a4ff64 |
541 | {\r |
0c9ae592 |
542 | const char **names;\r |
5d34757d |
543 | int c;\r |
c7a4ff64 |
544 | switch (entry->beh)\r |
545 | {\r |
24b24674 |
546 | case MB_OPT_ONOFF:\r |
049a6b3e |
547 | case MB_OPT_CUSTONOFF:\r |
c7a4ff64 |
548 | *(int *)entry->var ^= entry->mask;\r |
549 | return 1;\r |
24b24674 |
550 | case MB_OPT_RANGE:\r |
049a6b3e |
551 | case MB_OPT_CUSTRANGE:\r |
5d34757d |
552 | c = is_lr ? 10 : 1;\r |
553 | *(int *)entry->var += is_next ? c : -c;\r |
049a6b3e |
554 | if (*(int *)entry->var < (int)entry->min)\r |
555 | *(int *)entry->var = (int)entry->max;\r |
556 | if (*(int *)entry->var > (int)entry->max)\r |
557 | *(int *)entry->var = (int)entry->min;\r |
c7a4ff64 |
558 | return 1;\r |
0c9ae592 |
559 | case MB_OPT_ENUM:\r |
560 | names = (const char **)entry->data;\r |
561 | for (c = 0; names[c] != NULL; c++)\r |
562 | ;\r |
563 | *(int *)entry->var += is_next ? 1 : -1;\r |
564 | if (*(int *)entry->var < 0)\r |
565 | *(int *)entry->var = 0;\r |
566 | if (*(int *)entry->var >= c)\r |
567 | *(int *)entry->var = c - 1;\r |
568 | return 1;\r |
c7a4ff64 |
569 | default:\r |
570 | return 0;\r |
571 | }\r |
572 | }\r |
573 | \r |
049a6b3e |
574 | static void debug_menu_loop(void);\r |
575 | \r |
053bef76 |
576 | static void me_loop(menu_entry *menu, int *menu_sel, void (*draw_more)(void))\r |
24b24674 |
577 | {\r |
578 | int ret, inp, sel = *menu_sel, menu_sel_max;\r |
579 | \r |
580 | menu_sel_max = me_count(menu) - 1;\r |
053bef76 |
581 | if (menu_sel_max < 0) {\r |
24b24674 |
582 | lprintf("no enabled menu entries\n");\r |
583 | return;\r |
584 | }\r |
585 | \r |
5a31ef07 |
586 | while ((!menu[sel].enabled || !menu[sel].selectable) && sel < menu_sel_max)\r |
24b24674 |
587 | sel++;\r |
588 | \r |
589 | /* make sure action buttons are not pressed on entering menu */\r |
053bef76 |
590 | me_draw(menu, sel, NULL);\r |
24b24674 |
591 | while (in_menu_wait_any(50) & (PBTN_MOK|PBTN_MBACK|PBTN_MENU));\r |
592 | \r |
593 | for (;;)\r |
594 | {\r |
053bef76 |
595 | me_draw(menu, sel, draw_more);\r |
049a6b3e |
596 | inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|\r |
597 | PBTN_MOK|PBTN_MBACK|PBTN_MENU|PBTN_L|PBTN_R, 70);\r |
598 | if (inp & (PBTN_MENU|PBTN_MBACK))\r |
599 | break;\r |
600 | \r |
24b24674 |
601 | if (inp & PBTN_UP ) {\r |
602 | do {\r |
603 | sel--;\r |
604 | if (sel < 0)\r |
605 | sel = menu_sel_max;\r |
606 | }\r |
049a6b3e |
607 | while (!menu[sel].enabled || !menu[sel].selectable);\r |
24b24674 |
608 | }\r |
609 | if (inp & PBTN_DOWN) {\r |
610 | do {\r |
611 | sel++;\r |
612 | if (sel > menu_sel_max)\r |
613 | sel = 0;\r |
614 | }\r |
049a6b3e |
615 | while (!menu[sel].enabled || !menu[sel].selectable);\r |
616 | }\r |
617 | \r |
618 | /* a bit hacky but oh well */\r |
619 | if ((inp & (PBTN_L|PBTN_R)) == (PBTN_L|PBTN_R))\r |
620 | debug_menu_loop();\r |
621 | \r |
5d34757d |
622 | if (inp & (PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R)) { /* multi choice */\r |
623 | if (me_process(&menu[sel], (inp & (PBTN_RIGHT|PBTN_R)) ? 1 : 0,\r |
624 | inp & (PBTN_L|PBTN_R)))\r |
049a6b3e |
625 | continue;\r |
24b24674 |
626 | }\r |
24b24674 |
627 | \r |
0c9ae592 |
628 | if (inp & (PBTN_MOK|PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R))\r |
24b24674 |
629 | {\r |
0c9ae592 |
630 | /* require PBTN_MOK for MB_NONE */\r |
631 | if (menu[sel].handler != NULL && (menu[sel].beh != MB_NONE || (inp & PBTN_MOK))) {\r |
049a6b3e |
632 | ret = menu[sel].handler(menu[sel].id, inp);\r |
24b24674 |
633 | if (ret) break;\r |
0c9ae592 |
634 | menu_sel_max = me_count(menu) - 1; /* might change, so update */\r |
24b24674 |
635 | }\r |
636 | }\r |
24b24674 |
637 | }\r |
638 | *menu_sel = sel;\r |
639 | }\r |
640 | \r |
641 | /* ***************************************** */\r |
642 | \r |
ad6fbeba |
643 | static void draw_menu_message(const char *msg, void (*draw_more)(void))\r |
049a6b3e |
644 | {\r |
049a6b3e |
645 | int x, y, h, w, wt;\r |
ad6fbeba |
646 | const char *p;\r |
24b24674 |
647 | \r |
ad6fbeba |
648 | p = msg;\r |
049a6b3e |
649 | for (h = 1, w = 0; *p != 0; h++) {\r |
650 | for (wt = 0; *p != 0 && *p != '\n'; p++)\r |
651 | wt++;\r |
24b24674 |
652 | \r |
049a6b3e |
653 | if (wt > w)\r |
654 | w = wt;\r |
655 | if (*p == 0)\r |
24b24674 |
656 | break;\r |
049a6b3e |
657 | p++;\r |
24b24674 |
658 | }\r |
659 | \r |
95a2ec38 |
660 | x = g_menuscreen_w / 2 - w * me_mfont_w / 2;\r |
c66f49e6 |
661 | y = g_menuscreen_h / 2 - h * me_mfont_h / 2;\r |
049a6b3e |
662 | if (x < 0) x = 0;\r |
663 | if (y < 0) y = 0;\r |
664 | \r |
902972d1 |
665 | menu_draw_begin(1);\r |
049a6b3e |
666 | \r |
ad6fbeba |
667 | for (p = msg; *p != 0 && y <= g_menuscreen_h - me_mfont_h; y += me_mfont_h) {\r |
049a6b3e |
668 | text_out16(x, y, p);\r |
669 | \r |
670 | for (; *p != 0 && *p != '\n'; p++)\r |
671 | ;\r |
672 | if (*p != 0)\r |
673 | p++;\r |
674 | }\r |
675 | \r |
95a2ec38 |
676 | if (draw_more != NULL)\r |
677 | draw_more();\r |
678 | \r |
902972d1 |
679 | menu_draw_end();\r |
24b24674 |
680 | }\r |
681 | \r |
82abf46f |
682 | // -------------- del confirm ---------------\r |
683 | \r |
684 | static void do_delete(const char *fpath, const char *fname)\r |
685 | {\r |
686 | int len, mid, inp;\r |
687 | const char *nm;\r |
688 | char tmp[64];\r |
689 | \r |
902972d1 |
690 | menu_draw_begin(1);\r |
82abf46f |
691 | \r |
82abf46f |
692 | len = strlen(fname);\r |
c66f49e6 |
693 | if (len > g_menuscreen_w / me_sfont_w)\r |
694 | len = g_menuscreen_w / me_sfont_w;\r |
82abf46f |
695 | \r |
c66f49e6 |
696 | mid = g_menuscreen_w / 2;\r |
82abf46f |
697 | text_out16(mid - me_mfont_w * 15 / 2, 8 * me_mfont_h, "About to delete");\r |
698 | smalltext_out16(mid - len * me_sfont_w / 2, 9 * me_mfont_h + 5, fname, 0xbdff);\r |
699 | text_out16(mid - me_mfont_w * 13 / 2, 11 * me_mfont_h, "Are you sure?");\r |
700 | \r |
701 | nm = in_get_key_name(-1, -PBTN_MA3);\r |
702 | snprintf(tmp, sizeof(tmp), "(%s - confirm, ", nm);\r |
703 | len = strlen(tmp);\r |
704 | nm = in_get_key_name(-1, -PBTN_MBACK);\r |
705 | snprintf(tmp + len, sizeof(tmp) - len, "%s - cancel)", nm);\r |
706 | len = strlen(tmp);\r |
707 | \r |
708 | text_out16(mid - me_mfont_w * len / 2, 12 * me_mfont_h, tmp);\r |
902972d1 |
709 | menu_draw_end();\r |
82abf46f |
710 | \r |
711 | while (in_menu_wait_any(50) & (PBTN_MENU|PBTN_MA2));\r |
712 | inp = in_menu_wait(PBTN_MA3|PBTN_MBACK, 100);\r |
713 | if (inp & PBTN_MA3)\r |
714 | remove(fpath);\r |
715 | }\r |
716 | \r |
049a6b3e |
717 | // -------------- ROM selector --------------\r |
725d7f6c |
718 | \r |
049a6b3e |
719 | static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)\r |
b8464531 |
720 | {\r |
f15ca4db |
721 | int max_cnt, start, i, x, pos;\r |
d2f29611 |
722 | void *darken_ptr;\r |
b8464531 |
723 | \r |
c66f49e6 |
724 | max_cnt = g_menuscreen_h / me_sfont_h;\r |
049a6b3e |
725 | start = max_cnt / 2 - sel;\r |
726 | n--; // exclude current dir (".")\r |
b8464531 |
727 | \r |
902972d1 |
728 | menu_draw_begin(1);\r |
049a6b3e |
729 | \r |
730 | // if (!rom_loaded)\r |
731 | // menu_darken_bg(gp2x_screen, 320*240, 0);\r |
732 | \r |
c66f49e6 |
733 | darken_ptr = (short *)g_menuscreen_ptr + g_menuscreen_w * max_cnt/2 * me_sfont_h;\r |
734 | menu_darken_bg(darken_ptr, darken_ptr, g_menuscreen_w * me_sfont_h * 8 / 10, 0);\r |
049a6b3e |
735 | \r |
f15ca4db |
736 | x = 5 + me_mfont_w + 1;\r |
049a6b3e |
737 | if (start - 2 >= 0)\r |
f15ca4db |
738 | smalltext_out16(14, (start - 2) * me_sfont_h, curdir, 0xffff);\r |
049a6b3e |
739 | for (i = 0; i < n; i++) {\r |
740 | pos = start + i;\r |
741 | if (pos < 0) continue;\r |
742 | if (pos >= max_cnt) break;\r |
743 | if (namelist[i+1]->d_type == DT_DIR) {\r |
f15ca4db |
744 | smalltext_out16(x, pos * me_sfont_h, "/", 0xfff6);\r |
745 | smalltext_out16(x + me_sfont_w, pos * me_sfont_h, namelist[i+1]->d_name, 0xfff6);\r |
049a6b3e |
746 | } else {\r |
95a2ec38 |
747 | unsigned short color = fname2color(namelist[i+1]->d_name);\r |
f15ca4db |
748 | smalltext_out16(x, pos * me_sfont_h, namelist[i+1]->d_name, color);\r |
049a6b3e |
749 | }\r |
b8464531 |
750 | }\r |
f15ca4db |
751 | smalltext_out16(5, max_cnt/2 * me_sfont_h, ">", 0xffff);\r |
902972d1 |
752 | menu_draw_end();\r |
b8464531 |
753 | }\r |
754 | \r |
049a6b3e |
755 | static int scandir_cmp(const void *p1, const void *p2)\r |
b8464531 |
756 | {\r |
5f7b5155 |
757 | const struct dirent **d1 = (const struct dirent **)p1;\r |
758 | const struct dirent **d2 = (const struct dirent **)p2;\r |
759 | if ((*d1)->d_type == (*d2)->d_type)\r |
760 | return alphasort(d1, d2);\r |
761 | if ((*d1)->d_type == DT_DIR)\r |
762 | return -1; // put before\r |
763 | if ((*d2)->d_type == DT_DIR)\r |
764 | return 1;\r |
765 | \r |
049a6b3e |
766 | return alphasort(d1, d2);\r |
767 | }\r |
b8464531 |
768 | \r |
049a6b3e |
769 | static const char *filter_exts[] = {\r |
770 | ".mp3", ".MP3", ".srm", ".brm", "s.gz", ".mds", "bcfg", ".txt", ".htm", "html",\r |
771 | ".jpg", ".gpe"\r |
772 | };\r |
773 | \r |
774 | static int scandir_filter(const struct dirent *ent)\r |
775 | {\r |
776 | const char *p;\r |
777 | int i;\r |
778 | \r |
779 | if (ent == NULL || ent->d_name == NULL) return 0;\r |
780 | if (strlen(ent->d_name) < 5) return 1;\r |
781 | \r |
782 | p = ent->d_name + strlen(ent->d_name) - 4;\r |
783 | \r |
784 | for (i = 0; i < array_size(filter_exts); i++)\r |
785 | if (strcmp(p, filter_exts[i]) == 0)\r |
786 | return 0;\r |
787 | \r |
788 | return 1;\r |
b8464531 |
789 | }\r |
790 | \r |
049a6b3e |
791 | static char *menu_loop_romsel(char *curr_path, int len)\r |
b8464531 |
792 | {\r |
049a6b3e |
793 | struct dirent **namelist;\r |
794 | int n, inp, sel = 0;\r |
795 | char *ret = NULL, *fname = NULL;\r |
796 | \r |
797 | rescan:\r |
798 | // is this a dir or a full path?\r |
799 | if (!plat_is_dir(curr_path)) {\r |
800 | char *p = curr_path + strlen(curr_path) - 1;\r |
801 | for (; p > curr_path && *p != '/'; p--)\r |
802 | ;\r |
803 | *p = 0;\r |
804 | fname = p+1;\r |
805 | }\r |
b8464531 |
806 | \r |
5f7b5155 |
807 | n = scandir(curr_path, &namelist, scandir_filter, (void *)scandir_cmp);\r |
049a6b3e |
808 | if (n < 0) {\r |
a96477bb |
809 | char *t;\r |
049a6b3e |
810 | lprintf("menu_loop_romsel failed, dir: %s\n", curr_path);\r |
811 | \r |
812 | // try root\r |
a96477bb |
813 | t = getcwd(curr_path, len);\r |
814 | if (t == NULL)\r |
815 | plat_get_root_dir(curr_path, len);\r |
5f7b5155 |
816 | n = scandir(curr_path, &namelist, scandir_filter, (void *)scandir_cmp);\r |
049a6b3e |
817 | if (n < 0) {\r |
818 | // oops, we failed\r |
819 | lprintf("menu_loop_romsel failed, dir: %s\n", curr_path);\r |
820 | return NULL;\r |
821 | }\r |
822 | }\r |
823 | \r |
824 | // try to find sel\r |
825 | if (fname != NULL) {\r |
826 | int i;\r |
827 | for (i = 1; i < n; i++) {\r |
828 | if (strcmp(namelist[i]->d_name, fname) == 0) {\r |
829 | sel = i - 1;\r |
b8464531 |
830 | break;\r |
049a6b3e |
831 | }\r |
b8464531 |
832 | }\r |
049a6b3e |
833 | }\r |
b8464531 |
834 | \r |
e0e9248c |
835 | /* make sure action buttons are not pressed on entering menu */\r |
836 | draw_dirlist(curr_path, namelist, n, sel);\r |
837 | while (in_menu_wait_any(50) & (PBTN_MOK|PBTN_MBACK|PBTN_MENU))\r |
838 | ;\r |
839 | \r |
049a6b3e |
840 | for (;;)\r |
841 | {\r |
842 | draw_dirlist(curr_path, namelist, n, sel);\r |
843 | inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|\r |
e31266dd |
844 | PBTN_L|PBTN_R|PBTN_MA2|PBTN_MOK|PBTN_MBACK|PBTN_MENU, 33);\r |
049a6b3e |
845 | if (inp & PBTN_UP ) { sel--; if (sel < 0) sel = n-2; }\r |
846 | if (inp & PBTN_DOWN) { sel++; if (sel > n-2) sel = 0; }\r |
847 | if (inp & PBTN_LEFT) { sel-=10; if (sel < 0) sel = 0; }\r |
848 | if (inp & PBTN_L) { sel-=24; if (sel < 0) sel = 0; }\r |
849 | if (inp & PBTN_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }\r |
850 | if (inp & PBTN_R) { sel+=24; if (sel > n-2) sel = n-2; }\r |
e31266dd |
851 | if ((inp & PBTN_MOK) || (inp & (PBTN_MENU|PBTN_MA2)) == (PBTN_MENU|PBTN_MA2))\r |
b8464531 |
852 | {\r |
049a6b3e |
853 | again:\r |
854 | if (namelist[sel+1]->d_type == DT_REG)\r |
855 | {\r |
856 | strcpy(rom_fname_reload, curr_path);\r |
857 | strcat(rom_fname_reload, "/");\r |
858 | strcat(rom_fname_reload, namelist[sel+1]->d_name);\r |
859 | if (inp & PBTN_MOK) { // return sel\r |
860 | ret = rom_fname_reload;\r |
861 | break;\r |
725d7f6c |
862 | }\r |
82abf46f |
863 | do_delete(rom_fname_reload, namelist[sel+1]->d_name);\r |
049a6b3e |
864 | if (n > 0) {\r |
865 | while (n--) free(namelist[n]);\r |
866 | free(namelist);\r |
867 | }\r |
868 | goto rescan;\r |
869 | }\r |
870 | else if (namelist[sel+1]->d_type == DT_DIR)\r |
871 | {\r |
872 | int newlen;\r |
873 | char *p, *newdir;\r |
95a2ec38 |
874 | if (!(inp & PBTN_MOK))\r |
875 | continue;\r |
049a6b3e |
876 | newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;\r |
877 | newdir = malloc(newlen);\r |
95a2ec38 |
878 | if (newdir == NULL)\r |
879 | break;\r |
049a6b3e |
880 | if (strcmp(namelist[sel+1]->d_name, "..") == 0) {\r |
881 | char *start = curr_path;\r |
882 | p = start + strlen(start) - 1;\r |
883 | while (*p == '/' && p > start) p--;\r |
884 | while (*p != '/' && p > start) p--;\r |
885 | if (p <= start) strcpy(newdir, "/");\r |
886 | else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }\r |
887 | } else {\r |
888 | strcpy(newdir, curr_path);\r |
889 | p = newdir + strlen(newdir) - 1;\r |
890 | while (*p == '/' && p >= newdir) *p-- = 0;\r |
891 | strcat(newdir, "/");\r |
892 | strcat(newdir, namelist[sel+1]->d_name);\r |
b8464531 |
893 | }\r |
049a6b3e |
894 | ret = menu_loop_romsel(newdir, newlen);\r |
895 | free(newdir);\r |
b8464531 |
896 | break;\r |
049a6b3e |
897 | }\r |
898 | else\r |
899 | {\r |
900 | // unknown file type, happens on NTFS mounts. Try to guess.\r |
901 | FILE *tstf; int tmp;\r |
902 | strcpy(rom_fname_reload, curr_path);\r |
903 | strcat(rom_fname_reload, "/");\r |
904 | strcat(rom_fname_reload, namelist[sel+1]->d_name);\r |
905 | tstf = fopen(rom_fname_reload, "rb");\r |
906 | if (tstf != NULL)\r |
907 | {\r |
908 | if (fread(&tmp, 1, 1, tstf) > 0 || ferror(tstf) == 0)\r |
909 | namelist[sel+1]->d_type = DT_REG;\r |
910 | else namelist[sel+1]->d_type = DT_DIR;\r |
911 | fclose(tstf);\r |
912 | goto again;\r |
913 | }\r |
914 | }\r |
915 | }\r |
916 | if (inp & PBTN_MBACK)\r |
917 | break;\r |
918 | }\r |
919 | \r |
920 | if (n > 0) {\r |
921 | while (n--) free(namelist[n]);\r |
922 | free(namelist);\r |
923 | }\r |
924 | \r |
95a2ec38 |
925 | // restore curr_path\r |
926 | if (fname != NULL) {\r |
927 | n = strlen(curr_path);\r |
928 | if (curr_path + n + 1 == fname)\r |
929 | curr_path[n] = '/';\r |
930 | }\r |
931 | \r |
049a6b3e |
932 | return ret;\r |
933 | }\r |
934 | \r |
049a6b3e |
935 | // ------------ savestate loader ------------\r |
936 | \r |
937 | static int state_slot_flags = 0;\r |
938 | \r |
939 | static void state_check_slots(void)\r |
940 | {\r |
941 | int slot;\r |
942 | \r |
943 | state_slot_flags = 0;\r |
944 | \r |
945 | for (slot = 0; slot < 10; slot++) {\r |
ee0f881e |
946 | if (emu_check_save_file(slot))\r |
049a6b3e |
947 | state_slot_flags |= 1 << slot;\r |
948 | }\r |
949 | }\r |
950 | \r |
94a1d22e |
951 | static void draw_savestate_bg(int slot);\r |
049a6b3e |
952 | \r |
953 | static void draw_savestate_menu(int menu_sel, int is_loading)\r |
954 | {\r |
955 | int i, x, y, w, h;\r |
956 | \r |
957 | if (state_slot_flags & (1 << menu_sel))\r |
958 | draw_savestate_bg(menu_sel);\r |
959 | \r |
f15ca4db |
960 | w = (13 + 2) * me_mfont_w;\r |
3ffee69c |
961 | h = (1+2+10+1) * me_mfont_h;\r |
c66f49e6 |
962 | x = g_menuscreen_w / 2 - w / 2;\r |
049a6b3e |
963 | if (x < 0) x = 0;\r |
c66f49e6 |
964 | y = g_menuscreen_h / 2 - h / 2;\r |
049a6b3e |
965 | if (y < 0) y = 0;\r |
95a2ec38 |
966 | #ifdef MENU_ALIGN_LEFT\r |
967 | if (x > 12 + me_mfont_w * 2)\r |
968 | x = 12 + me_mfont_w * 2;\r |
969 | #endif\r |
049a6b3e |
970 | \r |
902972d1 |
971 | menu_draw_begin(1);\r |
049a6b3e |
972 | \r |
973 | text_out16(x, y, is_loading ? "Load state" : "Save state");\r |
3ffee69c |
974 | y += 3 * me_mfont_h;\r |
049a6b3e |
975 | \r |
5a31ef07 |
976 | menu_draw_selection(x - me_mfont_w * 2, y + menu_sel * me_mfont_h, (13 + 2) * me_mfont_w + 4);\r |
049a6b3e |
977 | \r |
978 | /* draw all 10 slots */\r |
3ffee69c |
979 | for (i = 0; i < 10; i++, y += me_mfont_h)\r |
049a6b3e |
980 | {\r |
981 | text_out16(x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");\r |
982 | }\r |
983 | text_out16(x, y, "back");\r |
984 | \r |
902972d1 |
985 | menu_draw_end();\r |
049a6b3e |
986 | }\r |
987 | \r |
988 | static int menu_loop_savestate(int is_loading)\r |
989 | {\r |
990 | static int menu_sel = 10;\r |
991 | int menu_sel_max = 10;\r |
992 | unsigned long inp = 0;\r |
d2275713 |
993 | int ret = 0;\r |
049a6b3e |
994 | \r |
995 | state_check_slots();\r |
996 | \r |
5354c909 |
997 | if (!(state_slot_flags & (1 << menu_sel)) && is_loading)\r |
998 | menu_sel = menu_sel_max;\r |
999 | \r |
049a6b3e |
1000 | for (;;)\r |
1001 | {\r |
1002 | draw_savestate_menu(menu_sel, is_loading);\r |
1003 | inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_MOK|PBTN_MBACK, 100);\r |
1004 | if (inp & PBTN_UP) {\r |
1005 | do {\r |
1006 | menu_sel--;\r |
1007 | if (menu_sel < 0)\r |
1008 | menu_sel = menu_sel_max;\r |
1009 | } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);\r |
1010 | }\r |
1011 | if (inp & PBTN_DOWN) {\r |
1012 | do {\r |
1013 | menu_sel++;\r |
1014 | if (menu_sel > menu_sel_max)\r |
1015 | menu_sel = 0;\r |
1016 | } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);\r |
1017 | }\r |
1018 | if (inp & PBTN_MOK) { // save/load\r |
1019 | if (menu_sel < 10) {\r |
1020 | state_slot = menu_sel;\r |
ee0f881e |
1021 | if (emu_save_load_game(is_loading, 0)) {\r |
3ffee69c |
1022 | me_update_msg(is_loading ? "Load failed" : "Save failed");\r |
d2275713 |
1023 | break;\r |
049a6b3e |
1024 | }\r |
d2275713 |
1025 | ret = 1;\r |
1026 | break;\r |
049a6b3e |
1027 | }\r |
d2275713 |
1028 | break;\r |
049a6b3e |
1029 | }\r |
1030 | if (inp & PBTN_MBACK)\r |
d2275713 |
1031 | break;\r |
049a6b3e |
1032 | }\r |
d2275713 |
1033 | \r |
1034 | return ret;\r |
049a6b3e |
1035 | }\r |
1036 | \r |
1037 | // -------------- key config --------------\r |
1038 | \r |
1039 | static char *action_binds(int player_idx, int action_mask, int dev_id)\r |
1040 | {\r |
4ec1acc1 |
1041 | int dev = 0, dev_last = IN_MAX_DEVS - 1;\r |
1042 | int can_combo = 1, type;\r |
049a6b3e |
1043 | \r |
1044 | static_buff[0] = 0;\r |
1045 | \r |
8e77275e |
1046 | type = IN_BINDTYPE_EMU;\r |
1047 | if (player_idx >= 0) {\r |
1048 | can_combo = 0;\r |
1049 | type = IN_BINDTYPE_PLAYER12;\r |
1050 | }\r |
1051 | if (player_idx == 1)\r |
1052 | action_mask <<= 16;\r |
1053 | \r |
4ec1acc1 |
1054 | if (dev_id >= 0)\r |
1055 | dev = dev_last = dev_id;\r |
1056 | \r |
1057 | for (; dev <= dev_last; dev++) {\r |
1058 | int k, count = 0, combo = 0;\r |
1059 | const int *binds;\r |
049a6b3e |
1060 | \r |
4ec1acc1 |
1061 | binds = in_get_dev_binds(dev);\r |
1062 | if (binds == NULL)\r |
049a6b3e |
1063 | continue;\r |
1064 | \r |
4ec1acc1 |
1065 | in_get_config(dev, IN_CFG_BIND_COUNT, &count);\r |
1066 | in_get_config(dev, IN_CFG_DOES_COMBOS, &combo);\r |
1067 | combo = combo && can_combo;\r |
1068 | \r |
1069 | for (k = 0; k < count; k++) {\r |
1070 | const char *xname;\r |
1071 | int len;\r |
1072 | \r |
1073 | if (!(binds[IN_BIND_OFFS(k, type)] & action_mask))\r |
1074 | continue;\r |
1075 | \r |
1076 | xname = in_get_key_name(dev, k);\r |
1077 | len = strlen(static_buff);\r |
1078 | if (len) {\r |
1079 | strncat(static_buff, combo ? " + " : ", ",\r |
1080 | sizeof(static_buff) - len - 1);\r |
1081 | len += combo ? 3 : 2;\r |
1082 | }\r |
1083 | strncat(static_buff, xname, sizeof(static_buff) - len - 1);\r |
f15ca4db |
1084 | }\r |
049a6b3e |
1085 | }\r |
1086 | \r |
1087 | return static_buff;\r |
1088 | }\r |
1089 | \r |
8e77275e |
1090 | static int count_bound_keys(int dev_id, int action_mask, int bindtype)\r |
049a6b3e |
1091 | {\r |
1092 | const int *binds;\r |
1093 | int k, keys = 0;\r |
902972d1 |
1094 | int count = 0;\r |
049a6b3e |
1095 | \r |
1096 | binds = in_get_dev_binds(dev_id);\r |
1097 | if (binds == NULL)\r |
1098 | return 0;\r |
1099 | \r |
902972d1 |
1100 | in_get_config(dev_id, IN_CFG_BIND_COUNT, &count);\r |
049a6b3e |
1101 | for (k = 0; k < count; k++)\r |
1102 | {\r |
8e77275e |
1103 | if (binds[IN_BIND_OFFS(k, bindtype)] & action_mask)\r |
1104 | keys++;\r |
049a6b3e |
1105 | }\r |
1106 | \r |
1107 | return keys;\r |
1108 | }\r |
1109 | \r |
1110 | static void draw_key_config(const me_bind_action *opts, int opt_cnt, int player_idx,\r |
1111 | int sel, int dev_id, int dev_count, int is_bind)\r |
1112 | {\r |
92068389 |
1113 | char buff[64], buff2[32];\r |
049a6b3e |
1114 | const char *dev_name;\r |
5a31ef07 |
1115 | int x, y, w, i;\r |
049a6b3e |
1116 | \r |
5a31ef07 |
1117 | w = ((player_idx >= 0) ? 20 : 30) * me_mfont_w;\r |
c66f49e6 |
1118 | x = g_menuscreen_w / 2 - w / 2;\r |
1119 | y = (g_menuscreen_h - 4 * me_mfont_h) / 2 - (2 + opt_cnt) * me_mfont_h / 2;\r |
3ffee69c |
1120 | if (x < me_mfont_w * 2)\r |
1121 | x = me_mfont_w * 2;\r |
049a6b3e |
1122 | \r |
902972d1 |
1123 | menu_draw_begin(1);\r |
049a6b3e |
1124 | if (player_idx >= 0)\r |
3ffee69c |
1125 | text_out16(x, y, "Player %i controls", player_idx + 1);\r |
049a6b3e |
1126 | else\r |
3ffee69c |
1127 | text_out16(x, y, "Emulator controls");\r |
049a6b3e |
1128 | \r |
3ffee69c |
1129 | y += 2 * me_mfont_h;\r |
5a31ef07 |
1130 | menu_draw_selection(x - me_mfont_w * 2, y + sel * me_mfont_h, w + 2 * me_mfont_w);\r |
049a6b3e |
1131 | \r |
3ffee69c |
1132 | for (i = 0; i < opt_cnt; i++, y += me_mfont_h)\r |
049a6b3e |
1133 | text_out16(x, y, "%s : %s", opts[i].name,\r |
1134 | action_binds(player_idx, opts[i].mask, dev_id));\r |
1135 | \r |
4ec1acc1 |
1136 | if (dev_id < 0)\r |
1137 | dev_name = "(all devices)";\r |
1138 | else\r |
1139 | dev_name = in_get_dev_name(dev_id, 1, 1);\r |
3ffee69c |
1140 | w = strlen(dev_name) * me_mfont_w;\r |
1141 | if (w < 30 * me_mfont_w)\r |
1142 | w = 30 * me_mfont_w;\r |
c66f49e6 |
1143 | if (w > g_menuscreen_w)\r |
1144 | w = g_menuscreen_w;\r |
049a6b3e |
1145 | \r |
c66f49e6 |
1146 | x = g_menuscreen_w / 2 - w / 2;\r |
049a6b3e |
1147 | \r |
92068389 |
1148 | if (!is_bind) {\r |
1149 | snprintf(buff2, sizeof(buff2), "%s", in_get_key_name(-1, -PBTN_MOK));\r |
1150 | snprintf(buff, sizeof(buff), "%s - bind, %s - clear", buff2,\r |
1151 | in_get_key_name(-1, -PBTN_MA2));\r |
c66f49e6 |
1152 | text_out16(x, g_menuscreen_h - 4 * me_mfont_h, buff);\r |
049a6b3e |
1153 | }\r |
92068389 |
1154 | else\r |
c66f49e6 |
1155 | text_out16(x, g_menuscreen_h - 4 * me_mfont_h, "Press a button to bind/unbind");\r |
049a6b3e |
1156 | \r |
92068389 |
1157 | if (dev_count > 1) {\r |
c66f49e6 |
1158 | text_out16(x, g_menuscreen_h - 3 * me_mfont_h, dev_name);\r |
1159 | text_out16(x, g_menuscreen_h - 2 * me_mfont_h, "Press left/right for other devs");\r |
92068389 |
1160 | }\r |
049a6b3e |
1161 | \r |
902972d1 |
1162 | menu_draw_end();\r |
049a6b3e |
1163 | }\r |
1164 | \r |
1165 | static void key_config_loop(const me_bind_action *opts, int opt_cnt, int player_idx)\r |
1166 | {\r |
902972d1 |
1167 | int i, sel = 0, menu_sel_max = opt_cnt - 1, does_combos = 0;\r |
4ec1acc1 |
1168 | int dev_id, bind_dev_id, dev_count, kc, is_down, mkey;\r |
92068389 |
1169 | int unbind, bindtype, mask_shift;\r |
049a6b3e |
1170 | \r |
1171 | for (i = 0, dev_id = -1, dev_count = 0; i < IN_MAX_DEVS; i++) {\r |
1172 | if (in_get_dev_name(i, 1, 0) != NULL) {\r |
1173 | dev_count++;\r |
1174 | if (dev_id < 0)\r |
1175 | dev_id = i;\r |
1176 | }\r |
1177 | }\r |
1178 | \r |
1179 | if (dev_id == -1) {\r |
1180 | lprintf("no devs, can't do config\n");\r |
1181 | return;\r |
1182 | }\r |
1183 | \r |
4ec1acc1 |
1184 | dev_id = -1; // show all\r |
92068389 |
1185 | mask_shift = 0;\r |
1186 | if (player_idx == 1)\r |
1187 | mask_shift = 16;\r |
1188 | bindtype = player_idx >= 0 ? IN_BINDTYPE_PLAYER12 : IN_BINDTYPE_EMU;\r |
1189 | \r |
049a6b3e |
1190 | for (;;)\r |
1191 | {\r |
1192 | draw_key_config(opts, opt_cnt, player_idx, sel, dev_id, dev_count, 0);\r |
92068389 |
1193 | mkey = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_MBACK|PBTN_MOK|PBTN_MA2, 100);\r |
049a6b3e |
1194 | switch (mkey) {\r |
1195 | case PBTN_UP: sel--; if (sel < 0) sel = menu_sel_max; continue;\r |
1196 | case PBTN_DOWN: sel++; if (sel > menu_sel_max) sel = 0; continue;\r |
1197 | case PBTN_LEFT:\r |
4ec1acc1 |
1198 | for (i = 0, dev_id--; i < IN_MAX_DEVS + 1; i++, dev_id--) {\r |
1199 | if (dev_id < -1)\r |
049a6b3e |
1200 | dev_id = IN_MAX_DEVS - 1;\r |
4ec1acc1 |
1201 | if (dev_id == -1 || in_get_dev_name(dev_id, 1, 0) != NULL)\r |
049a6b3e |
1202 | break;\r |
1203 | }\r |
1204 | continue;\r |
1205 | case PBTN_RIGHT:\r |
1206 | for (i = 0, dev_id++; i < IN_MAX_DEVS; i++, dev_id++) {\r |
1207 | if (dev_id >= IN_MAX_DEVS)\r |
4ec1acc1 |
1208 | dev_id = -1;\r |
1209 | if (dev_id == -1 || in_get_dev_name(dev_id, 1, 0) != NULL)\r |
049a6b3e |
1210 | break;\r |
1211 | }\r |
1212 | continue;\r |
ec54eeef |
1213 | case PBTN_MBACK:\r |
1214 | return;\r |
049a6b3e |
1215 | case PBTN_MOK:\r |
1216 | if (sel >= opt_cnt)\r |
1217 | return;\r |
ec54eeef |
1218 | while (in_menu_wait_any(30) & PBTN_MOK)\r |
1219 | ;\r |
049a6b3e |
1220 | break;\r |
92068389 |
1221 | case PBTN_MA2:\r |
1222 | in_unbind_all(dev_id, opts[sel].mask << mask_shift, bindtype);\r |
1223 | continue;\r |
049a6b3e |
1224 | default:continue;\r |
1225 | }\r |
1226 | \r |
1227 | draw_key_config(opts, opt_cnt, player_idx, sel, dev_id, dev_count, 1);\r |
1228 | \r |
1229 | /* wait for some up event */\r |
1230 | for (is_down = 1; is_down; )\r |
4ec1acc1 |
1231 | kc = in_update_keycode(&bind_dev_id, &is_down, -1);\r |
049a6b3e |
1232 | \r |
4ec1acc1 |
1233 | i = count_bound_keys(bind_dev_id, opts[sel].mask << mask_shift, bindtype);\r |
2c600560 |
1234 | unbind = (i > 0);\r |
1235 | \r |
1236 | /* allow combos if device supports them */\r |
4ec1acc1 |
1237 | in_get_config(bind_dev_id, IN_CFG_DOES_COMBOS, &does_combos);\r |
902972d1 |
1238 | if (i == 1 && bindtype == IN_BINDTYPE_EMU && does_combos)\r |
2c600560 |
1239 | unbind = 0;\r |
049a6b3e |
1240 | \r |
ec54eeef |
1241 | if (unbind)\r |
4ec1acc1 |
1242 | in_unbind_all(bind_dev_id, opts[sel].mask << mask_shift, bindtype);\r |
ec54eeef |
1243 | \r |
4ec1acc1 |
1244 | in_bind_key(bind_dev_id, kc, opts[sel].mask << mask_shift, bindtype, 0);\r |
049a6b3e |
1245 | }\r |
1246 | }\r |
1247 | \r |