2 * (C) GraÅžvydas "notaz" Ignotas, 2006-2012
\r
4 * This work is licensed under the terms of any of these licenses
\r
6 * - GNU GPL, version 2 or later.
\r
7 * - GNU LGPL, version 2.1 or later.
\r
9 * See the COPYING file in the top-level directory.
\r
17 #include <locale.h> // savestate date
\r
21 #include "readpng.h"
\r
22 #include "lprintf.h"
\r
27 #if defined(__GNUC__) && __GNUC__ >= 7
\r
28 #pragma GCC diagnostic ignored "-Wformat-truncation"
\r
31 static char static_buff[64];
\r
32 static int menu_error_time = 0;
\r
33 char menu_error_msg[64] = { 0, };
\r
34 void *g_menuscreen_ptr;
\r
35 void *g_menubg_src_ptr;
\r
40 int g_menuscreen_pp;
\r
42 int g_autostateld_opt;
\r
44 static unsigned char *menu_font_data = NULL;
\r
45 static int menu_text_color = 0xfffe; // default to white
\r
46 static int menu_sel_color = -1; // disabled
\r
48 /* note: these might become non-constant in future */
\r
50 static const int me_mfont_w = 16, me_mfont_h = 20;
\r
51 static const int me_sfont_w = 12, me_sfont_h = 20;
\r
53 static const int me_mfont_w = 8, me_mfont_h = 10;
\r
54 static const int me_sfont_w = 6, me_sfont_h = 10;
\r
57 static int g_menu_filter_off;
\r
58 static int g_border_style;
\r
59 static int border_left, border_right, border_top, border_bottom;
\r
61 void menuscreen_memset_lines(unsigned short *dst, int c, int l)
\r
63 for (; l > 0; l--, dst += g_menuscreen_pp)
\r
64 memset(dst, c, g_menuscreen_w * 2);
\r
67 // draws text to current bbp16 screen
\r
68 static void text_out16_(int x, int y, const char *text, int color)
\r
70 int i, lh, tr, tg, tb, len;
\r
71 unsigned short *dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_pp;
\r
72 tr = (color & 0xf800) >> 8;
\r
73 tg = (color & 0x07e0) >> 3;
\r
74 tb = (color & 0x001f) << 3;
\r
76 if (text == (void *)1)
\r
85 for (p = text; *p != 0 && *p != '\n'; p++)
\r
91 if (y + lh > g_menuscreen_h)
\r
92 lh = g_menuscreen_h - y;
\r
94 for (i = 0; i < len; i++)
\r
96 unsigned char *src = menu_font_data + (unsigned int)text[i] * me_mfont_w * me_mfont_h / 2;
\r
97 unsigned short *dst = dest;
\r
100 for (l = 0; l < lh; l++, dst += g_menuscreen_pp - me_mfont_w)
\r
102 for (u = me_mfont_w / 2; u > 0; u--, src++)
\r
106 r = (*dst & 0xf800) >> 8;
\r
107 g = (*dst & 0x07e0) >> 3;
\r
108 b = (*dst & 0x001f) << 3;
\r
109 r = (c^0xf)*r/15 + c*tr/15;
\r
110 g = (c^0xf)*g/15 + c*tg/15;
\r
111 b = (c^0xf)*b/15 + c*tb/15;
\r
112 *dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);
\r
114 r = (*dst & 0xf800) >> 8;
\r
115 g = (*dst & 0x07e0) >> 3;
\r
116 b = (*dst & 0x001f) << 3;
\r
117 r = (c^0xf)*r/15 + c*tr/15;
\r
118 g = (c^0xf)*g/15 + c*tg/15;
\r
119 b = (c^0xf)*b/15 + c*tb/15;
\r
120 *dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);
\r
123 dest += me_mfont_w;
\r
126 if (x < border_left)
\r
128 if (x + i * me_mfont_w > border_right)
\r
129 border_right = x + i * me_mfont_w;
\r
130 if (y < border_top)
\r
132 if (y + me_mfont_h > border_bottom)
\r
133 border_bottom = y + me_mfont_h;
\r
136 void text_out16(int x, int y, const char *texto, ...)
\r
140 int maxw = (g_menuscreen_w - x) / me_mfont_w;
\r
145 va_start(args, texto);
\r
146 vsnprintf(buffer, sizeof(buffer), texto, args);
\r
149 if (maxw > sizeof(buffer) - 1)
\r
150 maxw = sizeof(buffer) - 1;
\r
153 text_out16_(x,y,buffer,menu_text_color);
\r
156 /* draws in 6x8 font, might multiply size by integer */
\r
157 static void smalltext_out16_(int x, int y, const char *texto, int color)
\r
159 unsigned char *src;
\r
160 unsigned short *dst;
\r
161 int multiplier = me_sfont_w / 6;
\r
164 for (i = 0;; i++, x += me_sfont_w)
\r
166 unsigned char c = (unsigned char) texto[i];
\r
169 if (!c || c == '\n')
\r
172 src = fontdata6x8[c];
\r
173 dst = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_pp;
\r
178 for (h2 = multiplier; h2 > 0; h2--)
\r
180 for (m = 0x20; m; m >>= 1) {
\r
182 for (w2 = multiplier; w2 > 0; w2--)
\r
188 dst += g_menuscreen_pp - me_sfont_w;
\r
195 static void smalltext_out16(int x, int y, const char *texto, int color)
\r
198 int maxw = (g_menuscreen_w - x) / me_sfont_w;
\r
203 strncpy(buffer, texto, sizeof(buffer));
\r
204 if (maxw > sizeof(buffer) - 1)
\r
205 maxw = sizeof(buffer) - 1;
\r
208 smalltext_out16_(x, y, buffer, color);
\r
211 static void menu_draw_selection(int x, int y, int w)
\r
214 unsigned short *dst, *dest;
\r
216 text_out16_(x, y, (void *)1, (menu_sel_color < 0) ? menu_text_color : menu_sel_color);
\r
218 if (menu_sel_color < 0) return; // no selection hilight
\r
221 dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_pp + me_mfont_w * 2 - 2;
\r
222 for (h = me_mfont_h + 1; h > 0; h--)
\r
225 for (i = w - (me_mfont_w * 2 - 2); i > 0; i--)
\r
226 *dst++ = menu_sel_color;
\r
227 dest += g_menuscreen_pp;
\r
231 static int parse_hex_color(char *buff)
\r
234 int t = (int) strtoul(buff, &endp, 16);
\r
237 return ((t<<8)&0xf800) | ((t>>5)&0x07e0) | ((t>>19)&0x1f);
\r
239 return ((t>>8)&0xf800) | ((t>>5)&0x07e0) | ((t>>3)&0x1f);
\r
244 static char tolower_simple(char c)
\r
246 if ('A' <= c && c <= 'Z')
\r
251 void menu_init_base(void)
\r
254 unsigned char *fd, *fds;
\r
258 if (menu_font_data != NULL)
\r
259 free(menu_font_data);
\r
261 menu_font_data = calloc((MENU_X2 ? 256 * 320 : 128 * 160) / 2, 1);
\r
262 if (menu_font_data == NULL)
\r
265 // generate default 8x10 font from fontdata8x8
\r
266 for (c = 0, fd = menu_font_data; c < 128; c++)
\r
268 for (l = 0; l < 8; l++)
\r
270 unsigned char fd8x8 = fontdata8x8[c*8+l];
\r
271 if (fd8x8&0x80) { *fd = 0xf0; }
\r
272 if (fd8x8&0x40) { *fd |= 0x0f; }; fd++;
\r
273 if (fd8x8&0x20) { *fd = 0xf0; }
\r
274 if (fd8x8&0x10) { *fd |= 0x0f; }; fd++;
\r
275 if (fd8x8&0x08) { *fd = 0xf0; }
\r
276 if (fd8x8&0x04) { *fd |= 0x0f; }; fd++;
\r
277 if (fd8x8&0x02) { *fd = 0xf0; }
\r
278 if (fd8x8&0x01) { *fd |= 0x0f; }; fd++;
\r
280 fd += 8*2/2; // 2 empty lines
\r
284 // expand default font
\r
285 fds = menu_font_data + 128 * 160 / 2 - 4;
\r
286 fd = menu_font_data + 256 * 320 / 2 - 1;
\r
287 for (c = 255; c >= 0; c--)
\r
289 for (l = 9; l >= 0; l--, fds -= 4)
\r
291 for (i = 3; i >= 0; i--) {
\r
292 int px = fds[i] & 0x0f;
\r
293 *fd-- = px | (px << 4);
\r
294 px = (fds[i] >> 4) & 0x0f;
\r
295 *fd-- = px | (px << 4);
\r
297 for (i = 3; i >= 0; i--) {
\r
298 int px = fds[i] & 0x0f;
\r
299 *fd-- = px | (px << 4);
\r
300 px = (fds[i] >> 4) & 0x0f;
\r
301 *fd-- = px | (px << 4);
\r
307 // load custom font and selector (stored as 1st symbol in font table)
\r
308 pos = plat_get_skin_dir(buff, sizeof(buff));
\r
309 strcpy(buff + pos, "font.png");
\r
310 readpng(menu_font_data, buff, READPNG_FONT,
\r
311 MENU_X2 ? 256 : 128, MENU_X2 ? 320 : 160);
\r
312 // default selector symbol is '>'
\r
313 memcpy(menu_font_data, menu_font_data + ((int)'>') * me_mfont_w * me_mfont_h / 2,
\r
314 me_mfont_w * me_mfont_h / 2);
\r
315 strcpy(buff + pos, "selector.png");
\r
316 readpng(menu_font_data, buff, READPNG_SELECTOR, me_mfont_w, me_mfont_h);
\r
318 // load custom colors
\r
319 strcpy(buff + pos, "skin.txt");
\r
320 f = fopen(buff, "r");
\r
323 lprintf("found skin.txt\n");
\r
326 if (fgets(buff, sizeof(buff), f) == NULL)
\r
328 if (buff[0] == '#' || buff[0] == '/') continue; // comment
\r
329 if (buff[0] == '\r' || buff[0] == '\n') continue; // empty line
\r
330 if (strncmp(buff, "text_color=", 11) == 0)
\r
332 int tmp = parse_hex_color(buff+11);
\r
333 if (tmp >= 0) menu_text_color = tmp;
\r
334 else lprintf("skin.txt: parse error for text_color\n");
\r
336 else if (strncmp(buff, "selection_color=", 16) == 0)
\r
338 int tmp = parse_hex_color(buff+16);
\r
339 if (tmp >= 0) menu_sel_color = tmp;
\r
340 else lprintf("skin.txt: parse error for selection_color\n");
\r
343 lprintf("skin.txt: parse error: %s\n", buff);
\r
348 // use user's locale for savestate date display
\r
349 setlocale(LC_TIME, "");
\r
352 static void menu_darken_bg(void *dst, void *src, int pixels, int darker)
\r
354 unsigned int *dest = dst;
\r
355 unsigned int *sorc = src;
\r
361 unsigned int p = *sorc++;
\r
362 *dest++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);
\r
369 unsigned int p = *sorc++;
\r
370 *dest++ = (p&0xf79ef79e)>>1;
\r
375 static void menu_darken_text_bg(void)
\r
377 int x, y, xmin, xmax, ymax, ls;
\r
378 unsigned short *screen = g_menuscreen_ptr;
\r
380 xmin = border_left - 3;
\r
383 xmax = border_right + 2;
\r
384 if (xmax > g_menuscreen_w - 1)
\r
385 xmax = g_menuscreen_w - 1;
\r
387 y = border_top - 3;
\r
390 ymax = border_bottom + 2;
\r
391 if (ymax > g_menuscreen_h - 1)
\r
392 ymax = g_menuscreen_h - 1;
\r
394 for (x = xmin; x <= xmax; x++)
\r
395 screen[y * g_menuscreen_pp + x] = 0xa514;
\r
396 for (y++; y < ymax; y++)
\r
398 ls = y * g_menuscreen_pp;
\r
399 screen[ls + xmin] = 0xffff;
\r
400 for (x = xmin + 1; x < xmax; x++)
\r
402 unsigned int p = screen[ls + x];
\r
403 if (p != menu_text_color)
\r
404 screen[ls + x] = ((p&0xf79e)>>1) - ((p&0xc618)>>3);
\r
406 screen[ls + xmax] = 0xffff;
\r
408 ls = y * g_menuscreen_pp;
\r
409 for (x = xmin; x <= xmax; x++)
\r
410 screen[ls + x] = 0xffff;
\r
413 static int borders_pending;
\r
415 static void menu_reset_borders(void)
\r
417 border_left = g_menuscreen_w;
\r
419 border_top = g_menuscreen_h;
\r
423 static void menu_draw_begin(int need_bg, int no_borders)
\r
427 plat_video_menu_begin();
\r
429 menu_reset_borders();
\r
430 borders_pending = g_border_style && !no_borders;
\r
433 if (g_border_style && no_borders) {
\r
434 for (y = 0; y < g_menuscreen_h; y++)
\r
435 menu_darken_bg((short *)g_menuscreen_ptr + g_menuscreen_pp * y,
\r
436 (short *)g_menubg_ptr + g_menuscreen_w * y, g_menuscreen_w, 1);
\r
439 for (y = 0; y < g_menuscreen_h; y++)
\r
440 memcpy((short *)g_menuscreen_ptr + g_menuscreen_pp * y,
\r
441 (short *)g_menubg_ptr + g_menuscreen_w * y, g_menuscreen_w * 2);
\r
446 static void menu_draw_end(void)
\r
448 if (borders_pending)
\r
449 menu_darken_text_bg();
\r
450 plat_video_menu_end();
\r
453 static void menu_separation(void)
\r
455 if (borders_pending) {
\r
456 menu_darken_text_bg();
\r
457 menu_reset_borders();
\r
461 static int me_id2offset(const menu_entry *ent, menu_id id)
\r
464 for (i = 0; ent->name; ent++, i++)
\r
465 if (ent->id == id) return i;
\r
467 lprintf("%s: id %i not found\n", __FUNCTION__, id);
\r
471 static void me_enable(menu_entry *entries, menu_id id, int enable)
\r
473 int i = me_id2offset(entries, id);
\r
474 entries[i].enabled = enable;
\r
477 static int me_count(const menu_entry *ent)
\r
481 for (ret = 0; ent->name; ent++, ret++)
\r
487 static unsigned int me_read_onoff(const menu_entry *ent)
\r
489 // guess var size based on mask to avoid reading too much
\r
490 if (ent->mask & 0xffff0000)
\r
491 return *(unsigned int *)ent->var & ent->mask;
\r
492 else if (ent->mask & 0xff00)
\r
493 return *(unsigned short *)ent->var & ent->mask;
\r
495 return *(unsigned char *)ent->var & ent->mask;
\r
498 static void me_toggle_onoff(menu_entry *ent)
\r
500 // guess var size based on mask to avoid reading too much
\r
501 if (ent->mask & 0xffff0000)
\r
502 *(unsigned int *)ent->var ^= ent->mask;
\r
503 else if (ent->mask & 0xff00)
\r
504 *(unsigned short *)ent->var ^= ent->mask;
\r
506 *(unsigned char *)ent->var ^= ent->mask;
\r
509 static void me_draw(const menu_entry *entries, int sel, void (*draw_more)(void))
\r
511 const menu_entry *ent, *ent_sel = entries;
\r
512 int x, y, w = 0, h = 0;
\r
513 int offs, col2_offs = 27 * me_mfont_w;
\r
518 /* calculate size of menu rect */
\r
519 for (ent = entries, i = n = 0; ent->name; ent++, i++)
\r
532 wt = strlen(ent->name) * me_mfont_w;
\r
533 if (wt == 0 && ent->generate_name)
\r
534 name = ent->generate_name(ent->id, &offs);
\r
536 wt = strlen(name) * me_mfont_w;
\r
538 if (ent->beh != MB_NONE)
\r
540 if (wt > col2_offs)
\r
541 col2_offs = wt + me_mfont_w;
\r
544 switch (ent->beh) {
\r
549 wt += me_mfont_w * 3;
\r
551 case MB_OPT_CUSTOM:
\r
552 case MB_OPT_CUSTONOFF:
\r
553 case MB_OPT_CUSTRANGE:
\r
556 if (ent->generate_name != NULL)
\r
557 name = ent->generate_name(ent->id, &offs);
\r
559 wt += (strlen(name) + offs) * me_mfont_w;
\r
562 wt += 10 * me_mfont_w;
\r
571 h = n * me_mfont_h;
\r
572 w += me_mfont_w * 2; /* selector */
\r
574 if (w > g_menuscreen_w) {
\r
575 lprintf("width %d > %d\n", w, g_menuscreen_w);
\r
576 w = g_menuscreen_w;
\r
578 if (h > g_menuscreen_h) {
\r
579 lprintf("height %d > %d\n", w, g_menuscreen_h);
\r
580 h = g_menuscreen_h;
\r
583 x = g_menuscreen_w / 2 - w / 2;
\r
584 y = g_menuscreen_h / 2 - h / 2;
\r
585 #ifdef MENU_ALIGN_LEFT
\r
586 if (x > 12) x = 12;
\r
590 menu_draw_begin(1, 0);
\r
591 menu_draw_selection(x, y + vi_sel_ln * me_mfont_h, w);
\r
592 x += me_mfont_w * 2;
\r
594 for (ent = entries; ent->name; ent++)
\r
596 const char **names;
\r
597 int len, leftname_end = 0;
\r
603 if (strlen(name) == 0) {
\r
604 if (ent->generate_name)
\r
605 name = ent->generate_name(ent->id, &offs);
\r
607 if (name != NULL) {
\r
608 text_out16(x, y, name);
\r
609 leftname_end = x + (strlen(name) + 1) * me_mfont_w;
\r
612 switch (ent->beh) {
\r
616 text_out16(x + col2_offs, y, me_read_onoff(ent) ? "ON" : "OFF");
\r
619 text_out16(x + col2_offs, y, "%i", *(int *)ent->var);
\r
621 case MB_OPT_CUSTOM:
\r
622 case MB_OPT_CUSTONOFF:
\r
623 case MB_OPT_CUSTRANGE:
\r
626 if (ent->generate_name)
\r
627 name = ent->generate_name(ent->id, &offs);
\r
629 text_out16(x + col2_offs + offs * me_mfont_w, y, "%s", name);
\r
632 names = (const char **)ent->data;
\r
633 for (i = 0; names[i] != NULL; i++) {
\r
634 offs = x + col2_offs;
\r
635 len = strlen(names[i]);
\r
637 offs += (10 - len - 2) * me_mfont_w;
\r
638 if (offs < leftname_end)
\r
639 offs = leftname_end;
\r
640 if (i == *(unsigned char *)ent->var) {
\r
641 text_out16(offs, y, "%s", names[i]);
\r
653 /* display help or message if we have one */
\r
654 h = (g_menuscreen_h - h) / 2; // bottom area height
\r
655 if (menu_error_msg[0] != 0) {
\r
656 if (h >= me_mfont_h + 4)
\r
657 text_out16(5, g_menuscreen_h - me_mfont_h - 4, menu_error_msg);
\r
659 lprintf("menu msg doesn't fit!\n");
\r
661 if (plat_get_ticks_ms() - menu_error_time > 2048)
\r
662 menu_error_msg[0] = 0;
\r
664 else if (ent_sel->help != NULL) {
\r
665 const char *tmp = ent_sel->help;
\r
667 for (l = 0; tmp != NULL && *tmp != 0; l++)
\r
668 tmp = strchr(tmp + 1, '\n');
\r
669 if (h >= l * me_sfont_h + 4)
\r
670 for (tmp = ent_sel->help; l > 0; l--, tmp = strchr(tmp, '\n') + 1)
\r
671 smalltext_out16(5, g_menuscreen_h - (l * me_sfont_h + 4), tmp, 0xffff);
\r
676 if (draw_more != NULL)
\r
682 static int me_process(menu_entry *entry, int is_next, int is_lr)
\r
684 const char **names;
\r
686 switch (entry->beh)
\r
689 case MB_OPT_CUSTONOFF:
\r
690 me_toggle_onoff(entry);
\r
693 case MB_OPT_CUSTRANGE:
\r
694 c = is_lr ? 10 : 1;
\r
695 *(int *)entry->var += is_next ? c : -c;
\r
696 if (*(int *)entry->var < (int)entry->min)
\r
697 *(int *)entry->var = (int)entry->max;
\r
698 if (*(int *)entry->var > (int)entry->max)
\r
699 *(int *)entry->var = (int)entry->min;
\r
702 names = (const char **)entry->data;
\r
703 for (c = 0; names[c] != NULL; c++)
\r
705 *(signed char *)entry->var += is_next ? 1 : -1;
\r
706 if (*(signed char *)entry->var < 0)
\r
707 *(signed char *)entry->var = 0;
\r
708 if (*(signed char *)entry->var >= c)
\r
709 *(signed char *)entry->var = c - 1;
\r
716 static void debug_menu_loop(void);
\r
718 static int me_loop_d(menu_entry *menu, int *menu_sel, void (*draw_prep)(void), void (*draw_more)(void))
\r
720 int ret = 0, inp, sel = *menu_sel, menu_sel_max;
\r
722 menu_sel_max = me_count(menu) - 1;
\r
723 if (menu_sel_max < 0) {
\r
724 lprintf("no enabled menu entries\n");
\r
728 while ((!menu[sel].enabled || !menu[sel].selectable) && sel < menu_sel_max)
\r
731 /* make sure action buttons are not pressed on entering menu */
\r
732 me_draw(menu, sel, NULL);
\r
733 while (in_menu_wait_any(NULL, 50) & (PBTN_MOK|PBTN_MBACK|PBTN_MENU));
\r
737 if (draw_prep != NULL)
\r
740 me_draw(menu, sel, draw_more);
\r
741 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|
\r
742 PBTN_MOK|PBTN_MBACK|PBTN_MENU|PBTN_L|PBTN_R, NULL, 70);
\r
743 if (inp & (PBTN_MENU|PBTN_MBACK))
\r
746 if (inp & PBTN_UP ) {
\r
750 sel = menu_sel_max;
\r
752 while (!menu[sel].enabled || !menu[sel].selectable);
\r
754 if (inp & PBTN_DOWN) {
\r
757 if (sel > menu_sel_max)
\r
760 while (!menu[sel].enabled || !menu[sel].selectable);
\r
763 /* a bit hacky but oh well */
\r
764 if ((inp & (PBTN_L|PBTN_R)) == (PBTN_L|PBTN_R))
\r
767 if (inp & (PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R)) { /* multi choice */
\r
768 if (me_process(&menu[sel], (inp & (PBTN_RIGHT|PBTN_R)) ? 1 : 0,
\r
769 inp & (PBTN_L|PBTN_R)))
\r
773 if (inp & (PBTN_MOK|PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R))
\r
775 /* require PBTN_MOK for MB_NONE */
\r
776 if (menu[sel].handler != NULL && (menu[sel].beh != MB_NONE || (inp & PBTN_MOK))) {
\r
777 ret = menu[sel].handler(menu[sel].id, inp);
\r
779 menu_sel_max = me_count(menu) - 1; /* might change, so update */
\r
788 static int me_loop(menu_entry *menu, int *menu_sel)
\r
790 return me_loop_d(menu, menu_sel, NULL, NULL);
\r
793 /* ***************************************** */
\r
795 static void draw_menu_message(const char *msg, void (*draw_more)(void))
\r
797 int x, y, h, w, wt;
\r
801 for (h = 1, w = 0; *p != 0; h++) {
\r
802 for (wt = 0; *p != 0 && *p != '\n'; p++)
\r
812 x = g_menuscreen_w / 2 - w * me_mfont_w / 2;
\r
813 y = g_menuscreen_h / 2 - h * me_mfont_h / 2;
\r
817 menu_draw_begin(1, 0);
\r
819 for (p = msg; *p != 0 && y <= g_menuscreen_h - me_mfont_h; y += me_mfont_h) {
\r
820 text_out16(x, y, p);
\r
822 for (; *p != 0 && *p != '\n'; p++)
\r
830 if (draw_more != NULL)
\r
836 // -------------- del confirm ---------------
\r
838 static void do_delete(const char *fpath, const char *fname)
\r
844 menu_draw_begin(1, 0);
\r
846 len = strlen(fname);
\r
847 if (len > g_menuscreen_w / me_sfont_w)
\r
848 len = g_menuscreen_w / me_sfont_w;
\r
850 mid = g_menuscreen_w / 2;
\r
851 text_out16(mid - me_mfont_w * 15 / 2, 8 * me_mfont_h, "About to delete");
\r
852 smalltext_out16(mid - len * me_sfont_w / 2, 9 * me_mfont_h + 5, fname, 0xbdff);
\r
853 text_out16(mid - me_mfont_w * 13 / 2, 11 * me_mfont_h, "Are you sure?");
\r
855 nm = in_get_key_name(-1, -PBTN_MA3);
\r
856 snprintf(tmp, sizeof(tmp), "(%s - confirm, ", nm);
\r
858 nm = in_get_key_name(-1, -PBTN_MBACK);
\r
859 snprintf(tmp + len, sizeof(tmp) - len, "%s - cancel)", nm);
\r
862 text_out16(mid - me_mfont_w * len / 2, 12 * me_mfont_h, tmp);
\r
865 while (in_menu_wait_any(NULL, 50) & (PBTN_MENU|PBTN_MA2));
\r
866 inp = in_menu_wait(PBTN_MA3|PBTN_MBACK, NULL, 100);
\r
867 if (inp & PBTN_MA3)
\r
871 // -------------- ROM selector --------------
\r
873 static void draw_dirlist(char *curdir, struct dirent **namelist,
\r
874 int n, int sel, int show_help)
\r
876 int max_cnt, start, i, x, pos;
\r
880 max_cnt = g_menuscreen_h / me_sfont_h;
\r
881 start = max_cnt / 2 - sel;
\r
882 n--; // exclude current dir (".")
\r
884 menu_draw_begin(1, 1);
\r
886 // if (!rom_loaded)
\r
887 // menu_darken_bg(gp2x_screen, 320*240, 0);
\r
889 darken_ptr = (short *)g_menuscreen_ptr + g_menuscreen_pp * max_cnt/2 * me_sfont_h;
\r
890 menu_darken_bg(darken_ptr, darken_ptr, g_menuscreen_pp * me_sfont_h * 8 / 10, 0);
\r
892 x = 5 + me_mfont_w + 1;
\r
893 if (start - 2 >= 0)
\r
894 smalltext_out16(14, (start - 2) * me_sfont_h, curdir, 0xffff);
\r
895 for (i = 0; i < n; i++) {
\r
897 if (pos < 0) continue;
\r
898 if (pos >= max_cnt) break;
\r
899 if (namelist[i+1]->d_type == DT_DIR) {
\r
900 smalltext_out16(x, pos * me_sfont_h, "/", 0xfff6);
\r
901 smalltext_out16(x + me_sfont_w, pos * me_sfont_h, namelist[i+1]->d_name, 0xfff6);
\r
903 unsigned short color = fname2color(namelist[i+1]->d_name);
\r
904 smalltext_out16(x, pos * me_sfont_h, namelist[i+1]->d_name, color);
\r
907 smalltext_out16(5, max_cnt/2 * me_sfont_h, ">", 0xffff);
\r
910 darken_ptr = (short *)g_menuscreen_ptr
\r
911 + g_menuscreen_pp * (g_menuscreen_h - me_sfont_h * 5 / 2);
\r
912 menu_darken_bg(darken_ptr, darken_ptr,
\r
913 g_menuscreen_pp * (me_sfont_h * 5 / 2), 1);
\r
915 snprintf(buff, sizeof(buff), "%s - select, %s - back",
\r
916 in_get_key_name(-1, -PBTN_MOK), in_get_key_name(-1, -PBTN_MBACK));
\r
917 smalltext_out16(x, g_menuscreen_h - me_sfont_h * 3 - 2, buff, 0xe78c);
\r
919 snprintf(buff, sizeof(buff), g_menu_filter_off ?
\r
920 "%s - hide unknown files" : "%s - show all files",
\r
921 in_get_key_name(-1, -PBTN_MA3));
\r
922 smalltext_out16(x, g_menuscreen_h - me_sfont_h * 2 - 2, buff, 0xe78c);
\r
924 snprintf(buff, sizeof(buff), g_autostateld_opt ?
\r
925 "%s - autoload save is ON" : "%s - autoload save is OFF",
\r
926 in_get_key_name(-1, -PBTN_MA2));
\r
927 smalltext_out16(x, g_menuscreen_h - me_sfont_h * 1 - 2, buff, 0xe78c);
\r
933 static int scandir_cmp(const void *p1, const void *p2)
\r
935 const struct dirent **d1 = (const struct dirent **)p1;
\r
936 const struct dirent **d2 = (const struct dirent **)p2;
\r
937 if ((*d1)->d_type == (*d2)->d_type)
\r
938 return alphasort(d1, d2);
\r
939 if ((*d1)->d_type == DT_DIR)
\r
940 return -1; // put before
\r
941 if ((*d2)->d_type == DT_DIR)
\r
944 return alphasort(d1, d2);
\r
947 static const char **filter_exts_internal;
\r
949 static int scandir_filter(const struct dirent *ent)
\r
951 const char **filter = filter_exts_internal;
\r
958 switch (ent->d_type) {
\r
963 // could be a dir, deal with it later..
\r
967 ext = strrchr(ent->d_name, '.');
\r
972 for (i = 0; filter[i] != NULL; i++)
\r
973 if (strcasecmp(ext, filter[i]) == 0)
\r
979 static int dirent_seek_char(struct dirent **namelist, int len, int sel, char c)
\r
984 for (i = sel + 1; ; i++) {
\r
990 if (tolower_simple(namelist[i]->d_name[0]) == c)
\r
997 static const char *menu_loop_romsel(char *curr_path, int len,
\r
998 const char **filter_exts,
\r
999 int (*extra_filter)(struct dirent **namelist, int count,
\r
1000 const char *basedir))
\r
1002 static char rom_fname_reload[256]; // used for scratch and return
\r
1003 char sel_fname[256];
\r
1004 int (*filter)(const struct dirent *);
\r
1005 struct dirent **namelist = NULL;
\r
1006 int n = 0, inp = 0, sel = 0, show_help = 0;
\r
1007 char *curr_path_restore = NULL;
\r
1008 const char *ret = NULL;
\r
1013 filter_exts_internal = filter_exts;
\r
1016 // is this a dir or a full path?
\r
1017 if (!plat_is_dir(curr_path)) {
\r
1018 char *p = strrchr(curr_path, '/');
\r
1021 curr_path_restore = p;
\r
1022 snprintf(sel_fname, sizeof(sel_fname), "%s", p + 1);
\r
1025 if (rom_fname_reload[0] == 0)
\r
1030 if (namelist != NULL) {
\r
1032 free(namelist[n]);
\r
1038 if (!g_menu_filter_off)
\r
1039 filter = scandir_filter;
\r
1041 n = scandir(curr_path, &namelist, filter, (void *)scandir_cmp);
\r
1044 lprintf("menu_loop_romsel failed, dir: %s\n", curr_path);
\r
1047 t = getcwd(curr_path, len);
\r
1049 plat_get_root_dir(curr_path, len);
\r
1050 n = scandir(curr_path, &namelist, filter, (void *)scandir_cmp);
\r
1052 // oops, we failed
\r
1053 lprintf("menu_loop_romsel failed, dir: %s\n", curr_path);
\r
1058 // try to resolve DT_UNKNOWN and symlinks
\r
1060 for (i = 0; i < n; i++) {
\r
1063 if (namelist[i]->d_type == DT_REG || namelist[i]->d_type == DT_DIR)
\r
1066 snprintf(rom_fname_reload, sizeof(rom_fname_reload),
\r
1067 "%s/%s", curr_path, namelist[i]->d_name);
\r
1068 r = stat(rom_fname_reload, &st);
\r
1071 if (S_ISREG(st.st_mode)) {
\r
1072 namelist[i]->d_type = DT_REG;
\r
1075 else if (S_ISDIR(st.st_mode)) {
\r
1076 namelist[i]->d_type = DT_DIR;
\r
1082 if (!g_menu_filter_off && extra_filter != NULL)
\r
1083 n = extra_filter(namelist, n, curr_path);
\r
1085 if (n > 1 && changed)
\r
1086 qsort(namelist, n, sizeof(namelist[0]), scandir_cmp);
\r
1088 // try to find selected file
\r
1089 // note: we don't show '.' so sel is namelist index - 1
\r
1091 if (sel_fname[0] != 0) {
\r
1092 for (i = 1; i < n; i++) {
\r
1093 char *dname = namelist[i]->d_name;
\r
1094 if (dname[0] == sel_fname[0] && strcmp(dname, sel_fname) == 0) {
\r
1101 /* make sure action buttons are not pressed on entering menu */
\r
1102 draw_dirlist(curr_path, namelist, n, sel, show_help);
\r
1103 while (in_menu_wait_any(NULL, 50) & (PBTN_MOK|PBTN_MBACK|PBTN_MENU))
\r
1108 draw_dirlist(curr_path, namelist, n, sel, show_help);
\r
1109 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT
\r
1110 | PBTN_L|PBTN_R|PBTN_MA2|PBTN_MA3|PBTN_MOK|PBTN_MBACK
\r
1111 | PBTN_MENU|PBTN_CHAR, &cinp, 33);
\r
1112 if (inp & PBTN_MA3) {
\r
1113 g_menu_filter_off = !g_menu_filter_off;
\r
1114 snprintf(sel_fname, sizeof(sel_fname), "%s",
\r
1115 namelist[sel+1]->d_name);
\r
1118 if (inp & PBTN_UP ) { sel--; if (sel < 0) sel = n-2; }
\r
1119 if (inp & PBTN_DOWN) { sel++; if (sel > n-2) sel = 0; }
\r
1120 if (inp & PBTN_LEFT) { sel-=10; if (sel < 0) sel = 0; }
\r
1121 if (inp & PBTN_L) { sel-=24; if (sel < 0) sel = 0; }
\r
1122 if (inp & PBTN_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }
\r
1123 if (inp & PBTN_R) { sel+=24; if (sel > n-2) sel = n-2; }
\r
1125 if ((inp & PBTN_MOK) || (inp & (PBTN_MENU|PBTN_MA2)) == (PBTN_MENU|PBTN_MA2))
\r
1127 if (namelist[sel+1]->d_type == DT_REG)
\r
1129 snprintf(rom_fname_reload, sizeof(rom_fname_reload),
\r
1130 "%s/%s", curr_path, namelist[sel+1]->d_name);
\r
1131 if (inp & PBTN_MOK) { // return sel
\r
1132 ret = rom_fname_reload;
\r
1135 do_delete(rom_fname_reload, namelist[sel+1]->d_name);
\r
1138 else if (namelist[sel+1]->d_type == DT_DIR)
\r
1142 if (!(inp & PBTN_MOK))
\r
1144 newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;
\r
1145 newdir = malloc(newlen);
\r
1146 if (newdir == NULL)
\r
1148 if (strcmp(namelist[sel+1]->d_name, "..") == 0) {
\r
1149 char *start = curr_path;
\r
1150 p = start + strlen(start) - 1;
\r
1151 while (*p == '/' && p > start) p--;
\r
1152 while (*p != '/' && p > start) p--;
\r
1153 if (p <= start) strcpy(newdir, "/");
\r
1154 else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }
\r
1156 strcpy(newdir, curr_path);
\r
1157 p = newdir + strlen(newdir) - 1;
\r
1158 while (*p == '/' && p >= newdir) *p-- = 0;
\r
1159 strcat(newdir, "/");
\r
1160 strcat(newdir, namelist[sel+1]->d_name);
\r
1162 ret = menu_loop_romsel(newdir, newlen, filter_exts, extra_filter);
\r
1167 else if (inp & PBTN_MA2) {
\r
1168 g_autostateld_opt = !g_autostateld_opt;
\r
1171 else if (inp & PBTN_CHAR) {
\r
1173 sel = dirent_seek_char(namelist, n, sel, cinp);
\r
1176 if (inp & PBTN_MBACK)
\r
1179 if (show_help > 0)
\r
1185 free(namelist[n]);
\r
1189 // restore curr_path
\r
1190 if (curr_path_restore != NULL)
\r
1191 *curr_path_restore = '/';
\r
1196 // ------------ savestate loader ------------
\r
1198 #define STATE_SLOT_COUNT 10
\r
1200 static int state_slot_flags = 0;
\r
1201 static int state_slot_times[STATE_SLOT_COUNT];
\r
1203 static void state_check_slots(void)
\r
1207 state_slot_flags = 0;
\r
1209 for (slot = 0; slot < STATE_SLOT_COUNT; slot++) {
\r
1210 state_slot_times[slot] = 0;
\r
1211 if (emu_check_save_file(slot, &state_slot_times[slot]))
\r
1212 state_slot_flags |= 1 << slot;
\r
1216 static void draw_savestate_bg(int slot);
\r
1218 static void draw_savestate_menu(int menu_sel, int is_loading)
\r
1220 int i, x, y, w, h;
\r
1221 char time_buf[32];
\r
1223 if (state_slot_flags & (1 << menu_sel))
\r
1224 draw_savestate_bg(menu_sel);
\r
1226 w = (13 + 2) * me_mfont_w;
\r
1227 h = (1+2+STATE_SLOT_COUNT+1) * me_mfont_h;
\r
1228 x = g_menuscreen_w / 2 - w / 2;
\r
1230 y = g_menuscreen_h / 2 - h / 2;
\r
1232 #ifdef MENU_ALIGN_LEFT
\r
1233 if (x > 12 + me_mfont_w * 2)
\r
1234 x = 12 + me_mfont_w * 2;
\r
1237 menu_draw_begin(1, 1);
\r
1239 text_out16(x, y, is_loading ? "Load state" : "Save state");
\r
1240 y += 3 * me_mfont_h;
\r
1242 menu_draw_selection(x - me_mfont_w * 2, y + menu_sel * me_mfont_h, (23 + 2) * me_mfont_w + 4);
\r
1244 /* draw all slots */
\r
1245 for (i = 0; i < STATE_SLOT_COUNT; i++, y += me_mfont_h)
\r
1247 if (!(state_slot_flags & (1 << i)))
\r
1248 strcpy(time_buf, "free");
\r
1250 strcpy(time_buf, "USED");
\r
1251 if (state_slot_times[i] != 0) {
\r
1252 time_t time = state_slot_times[i];
\r
1253 struct tm *t = localtime(&time);
\r
1254 strftime(time_buf, sizeof(time_buf), "%x %R", t);
\r
1258 text_out16(x, y, "SLOT %i (%s)", i, time_buf);
\r
1260 text_out16(x, y, "back");
\r
1265 static int menu_loop_savestate(int is_loading)
\r
1267 static int menu_sel = STATE_SLOT_COUNT;
\r
1268 int menu_sel_max = STATE_SLOT_COUNT;
\r
1269 unsigned long inp = 0;
\r
1272 state_check_slots();
\r
1274 if (!(state_slot_flags & (1 << menu_sel)) && is_loading)
\r
1275 menu_sel = menu_sel_max;
\r
1279 draw_savestate_menu(menu_sel, is_loading);
\r
1280 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_MOK|PBTN_MBACK, NULL, 100);
\r
1281 if (inp & PBTN_UP) {
\r
1285 menu_sel = menu_sel_max;
\r
1286 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
\r
1288 if (inp & PBTN_DOWN) {
\r
1291 if (menu_sel > menu_sel_max)
\r
1293 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
\r
1295 if (inp & PBTN_MOK) { // save/load
\r
1296 if (menu_sel < STATE_SLOT_COUNT) {
\r
1297 state_slot = menu_sel;
\r
1298 if (emu_save_load_game(is_loading, 0)) {
\r
1299 menu_update_msg(is_loading ? "Load failed" : "Save failed");
\r
1307 if (inp & PBTN_MBACK)
\r
1314 // -------------- key config --------------
\r
1316 static char *action_binds(int player_idx, int action_mask, int dev_id)
\r
1318 int dev = 0, dev_last = IN_MAX_DEVS - 1;
\r
1319 int can_combo = 1, type;
\r
1321 static_buff[0] = 0;
\r
1323 type = IN_BINDTYPE_EMU;
\r
1324 if (player_idx >= 0) {
\r
1326 type = IN_BINDTYPE_PLAYER12;
\r
1328 if (player_idx == 1)
\r
1329 action_mask <<= 16;
\r
1332 dev = dev_last = dev_id;
\r
1334 for (; dev <= dev_last; dev++) {
\r
1335 int k, count = 0, combo = 0;
\r
1338 binds = in_get_dev_binds(dev);
\r
1339 if (binds == NULL)
\r
1342 in_get_config(dev, IN_CFG_BIND_COUNT, &count);
\r
1343 in_get_config(dev, IN_CFG_DOES_COMBOS, &combo);
\r
1344 combo = combo && can_combo;
\r
1346 for (k = 0; k < count; k++) {
\r
1347 const char *xname;
\r
1350 if (!(binds[IN_BIND_OFFS(k, type)] & action_mask))
\r
1353 xname = in_get_key_name(dev, k);
\r
1354 len = strlen(static_buff);
\r
1356 strncat(static_buff, combo ? " + " : ", ",
\r
1357 sizeof(static_buff) - len - 1);
\r
1358 len += combo ? 3 : 2;
\r
1360 strncat(static_buff, xname, sizeof(static_buff) - len - 1);
\r
1364 return static_buff;
\r
1367 static int count_bound_keys(int dev_id, int action_mask, int bindtype)
\r
1373 binds = in_get_dev_binds(dev_id);
\r
1374 if (binds == NULL)
\r
1377 in_get_config(dev_id, IN_CFG_BIND_COUNT, &count);
\r
1378 for (k = 0; k < count; k++)
\r
1380 if (binds[IN_BIND_OFFS(k, bindtype)] & action_mask)
\r
1387 static void draw_key_config(const me_bind_action *opts, int opt_cnt, int player_idx,
\r
1388 int sel, int dev_id, int dev_count, int is_bind)
\r
1390 char buff[64], buff2[32];
\r
1391 const char *dev_name;
\r
1394 w = ((player_idx >= 0) ? 20 : 30) * me_mfont_w;
\r
1395 x = g_menuscreen_w / 2 - w / 2;
\r
1396 y = (g_menuscreen_h - 4 * me_mfont_h) / 2 - (2 + opt_cnt) * me_mfont_h / 2;
\r
1397 if (x < me_mfont_w * 2)
\r
1398 x = me_mfont_w * 2;
\r
1401 menu_draw_begin(1, 0);
\r
1402 if (player_idx >= 0)
\r
1403 text_out16(x, y, "Player %i controls", player_idx + 1);
\r
1405 text_out16(x, y, "Emulator controls");
\r
1407 y += 2 * me_mfont_h;
\r
1408 menu_draw_selection(x - me_mfont_w * 2, y + sel * me_mfont_h, w + 2 * me_mfont_w);
\r
1410 for (i = 0; i < opt_cnt; i++, y += me_mfont_h)
\r
1411 text_out16(x, y, "%s : %s", opts[i].name,
\r
1412 action_binds(player_idx, opts[i].mask, dev_id));
\r
1414 menu_separation();
\r
1417 dev_name = "(all devices)";
\r
1419 dev_name = in_get_dev_name(dev_id, 0, 1);
\r
1420 w = strlen(dev_name) * me_mfont_w;
\r
1421 if (w < 30 * me_mfont_w)
\r
1422 w = 30 * me_mfont_w;
\r
1423 if (w > g_menuscreen_w)
\r
1424 w = g_menuscreen_w;
\r
1426 x = g_menuscreen_w / 2 - w / 2;
\r
1429 snprintf(buff2, sizeof(buff2), "%s", in_get_key_name(-1, -PBTN_MOK));
\r
1430 snprintf(buff, sizeof(buff), "%s - bind, %s - clear", buff2,
\r
1431 in_get_key_name(-1, -PBTN_MA2));
\r
1432 text_out16(x, g_menuscreen_h - 4 * me_mfont_h, buff);
\r
1435 text_out16(x, g_menuscreen_h - 4 * me_mfont_h, "Press a button to bind/unbind");
\r
1437 if (dev_count > 1) {
\r
1438 text_out16(x, g_menuscreen_h - 3 * me_mfont_h, dev_name);
\r
1439 text_out16(x, g_menuscreen_h - 2 * me_mfont_h, "Press left/right for other devs");
\r
1445 static void key_config_loop(const me_bind_action *opts, int opt_cnt, int player_idx)
\r
1447 int i, sel = 0, menu_sel_max = opt_cnt - 1, does_combos = 0;
\r
1448 int dev_id, bind_dev_id, dev_count, kc, is_down, mkey;
\r
1449 int unbind, bindtype, mask_shift;
\r
1451 for (i = 0, dev_id = -1, dev_count = 0; i < IN_MAX_DEVS; i++) {
\r
1452 if (in_get_dev_name(i, 1, 0) != NULL) {
\r
1459 if (dev_id == -1) {
\r
1460 lprintf("no devs, can't do config\n");
\r
1464 dev_id = -1; // show all
\r
1466 if (player_idx == 1)
\r
1468 bindtype = player_idx >= 0 ? IN_BINDTYPE_PLAYER12 : IN_BINDTYPE_EMU;
\r
1472 draw_key_config(opts, opt_cnt, player_idx, sel, dev_id, dev_count, 0);
\r
1473 mkey = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT
\r
1474 |PBTN_MBACK|PBTN_MOK|PBTN_MA2, NULL, 100);
\r
1476 case PBTN_UP: sel--; if (sel < 0) sel = menu_sel_max; continue;
\r
1477 case PBTN_DOWN: sel++; if (sel > menu_sel_max) sel = 0; continue;
\r
1479 for (i = 0, dev_id--; i < IN_MAX_DEVS + 1; i++, dev_id--) {
\r
1481 dev_id = IN_MAX_DEVS - 1;
\r
1482 if (dev_id == -1 || in_get_dev_name(dev_id, 0, 0) != NULL)
\r
1487 for (i = 0, dev_id++; i < IN_MAX_DEVS; i++, dev_id++) {
\r
1488 if (dev_id >= IN_MAX_DEVS)
\r
1490 if (dev_id == -1 || in_get_dev_name(dev_id, 0, 0) != NULL)
\r
1497 if (sel >= opt_cnt)
\r
1499 while (in_menu_wait_any(NULL, 30) & PBTN_MOK)
\r
1503 in_unbind_all(dev_id, opts[sel].mask << mask_shift, bindtype);
\r
1508 draw_key_config(opts, opt_cnt, player_idx, sel, dev_id, dev_count, 1);
\r
1510 /* wait for some up event */
\r
1511 for (is_down = 1; is_down; )
\r
1512 kc = in_update_keycode(&bind_dev_id, &is_down, NULL, -1);
\r
1514 i = count_bound_keys(bind_dev_id, opts[sel].mask << mask_shift, bindtype);
\r
1517 /* allow combos if device supports them */
\r
1518 in_get_config(bind_dev_id, IN_CFG_DOES_COMBOS, &does_combos);
\r
1519 if (i == 1 && bindtype == IN_BINDTYPE_EMU && does_combos)
\r
1523 in_unbind_all(bind_dev_id, opts[sel].mask << mask_shift, bindtype);
\r
1525 in_bind_key(bind_dev_id, kc, opts[sel].mask << mask_shift, bindtype, 0);
\r
1527 // make sure bind change is displayed
\r
1529 dev_id = bind_dev_id;
\r