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