2 * (C) GraÅžvydas "notaz" Ignotas, 2006-2011
\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
8 * See the COPYING file in the top-level directory.
\r
16 #include <locale.h> // savestate date
\r
20 #include "readpng.h"
\r
21 #include "lprintf.h"
\r
26 static char static_buff[64];
\r
27 static int menu_error_time = 0;
\r
28 char menu_error_msg[64] = { 0, };
\r
29 void *g_menuscreen_ptr;
\r
30 void *g_menubg_src_ptr;
\r
33 #if !MSCREEN_SIZE_FIXED
\r
38 static unsigned char *menu_font_data = NULL;
\r
39 static int menu_text_color = 0xffff; // default to white
\r
40 static int menu_sel_color = -1; // disabled
\r
42 /* note: these might become non-constant in future */
\r
44 static const int me_mfont_w = 16, me_mfont_h = 20;
\r
45 static const int me_sfont_w = 12, me_sfont_h = 20;
\r
47 static const int me_mfont_w = 8, me_mfont_h = 10;
\r
48 static const int me_sfont_w = 6, me_sfont_h = 10;
\r
51 // draws text to current bbp16 screen
\r
52 static void text_out16_(int x, int y, const char *text, int color)
\r
54 int i, lh, tr, tg, tb, len;
\r
55 unsigned short *dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w;
\r
56 tr = (color & 0xf800) >> 8;
\r
57 tg = (color & 0x07e0) >> 3;
\r
58 tb = (color & 0x001f) << 3;
\r
60 if (text == (void *)1)
\r
69 for (p = text; *p != 0 && *p != '\n'; p++)
\r
75 if (y + lh > g_menuscreen_h)
\r
76 lh = g_menuscreen_h - y;
\r
78 for (i = 0; i < len; i++)
\r
80 unsigned char *src = menu_font_data + (unsigned int)text[i] * me_mfont_w * me_mfont_h / 2;
\r
81 unsigned short *dst = dest;
\r
84 for (l = 0; l < lh; l++, dst += g_menuscreen_w - me_mfont_w)
\r
86 for (u = me_mfont_w / 2; u > 0; u--, src++)
\r
90 r = (*dst & 0xf800) >> 8;
\r
91 g = (*dst & 0x07e0) >> 3;
\r
92 b = (*dst & 0x001f) << 3;
\r
93 r = (c^0xf)*r/15 + c*tr/15;
\r
94 g = (c^0xf)*g/15 + c*tg/15;
\r
95 b = (c^0xf)*b/15 + c*tb/15;
\r
96 *dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);
\r
98 r = (*dst & 0xf800) >> 8;
\r
99 g = (*dst & 0x07e0) >> 3;
\r
100 b = (*dst & 0x001f) << 3;
\r
101 r = (c^0xf)*r/15 + c*tr/15;
\r
102 g = (c^0xf)*g/15 + c*tg/15;
\r
103 b = (c^0xf)*b/15 + c*tb/15;
\r
104 *dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);
\r
107 dest += me_mfont_w;
\r
111 void text_out16(int x, int y, const char *texto, ...)
\r
115 int maxw = (g_menuscreen_w - x) / me_mfont_w;
\r
120 va_start(args, texto);
\r
121 vsnprintf(buffer, sizeof(buffer), texto, args);
\r
124 if (maxw > sizeof(buffer) - 1)
\r
125 maxw = sizeof(buffer) - 1;
\r
128 text_out16_(x,y,buffer,menu_text_color);
\r
131 /* draws in 6x8 font, might multiply size by integer */
\r
132 static void smalltext_out16_(int x, int y, const char *texto, int color)
\r
134 unsigned char *src;
\r
135 unsigned short *dst;
\r
136 int multiplier = me_sfont_w / 6;
\r
139 for (i = 0;; i++, x += me_sfont_w)
\r
141 unsigned char c = (unsigned char) texto[i];
\r
144 if (!c || c == '\n')
\r
147 src = fontdata6x8[c];
\r
148 dst = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w;
\r
153 for (h2 = multiplier; h2 > 0; h2--)
\r
155 for (m = 0x20; m; m >>= 1) {
\r
157 for (w2 = multiplier; w2 > 0; w2--)
\r
163 dst += g_menuscreen_w - me_sfont_w;
\r
170 static void smalltext_out16(int x, int y, const char *texto, int color)
\r
173 int maxw = (g_menuscreen_w - x) / me_sfont_w;
\r
178 strncpy(buffer, texto, sizeof(buffer));
\r
179 if (maxw > sizeof(buffer) - 1)
\r
180 maxw = sizeof(buffer) - 1;
\r
183 smalltext_out16_(x, y, buffer, color);
\r
186 static void menu_draw_selection(int x, int y, int w)
\r
189 unsigned short *dst, *dest;
\r
191 text_out16_(x, y, (void *)1, (menu_sel_color < 0) ? menu_text_color : menu_sel_color);
\r
193 if (menu_sel_color < 0) return; // no selection hilight
\r
196 dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w + me_mfont_w * 2 - 2;
\r
197 for (h = me_mfont_h + 1; h > 0; h--)
\r
200 for (i = w - (me_mfont_w * 2 - 2); i > 0; i--)
\r
201 *dst++ = menu_sel_color;
\r
202 dest += g_menuscreen_w;
\r
206 static int parse_hex_color(char *buff)
\r
209 int t = (int) strtoul(buff, &endp, 16);
\r
212 return ((t<<8)&0xf800) | ((t>>5)&0x07e0) | ((t>>19)&0x1f);
\r
214 return ((t>>8)&0xf800) | ((t>>5)&0x07e0) | ((t>>3)&0x1f);
\r
219 static char tolower_simple(char c)
\r
221 if ('A' <= c && c <= 'Z')
\r
226 void menu_init(void)
\r
229 unsigned char *fd, *fds;
\r
233 if (menu_font_data != NULL)
\r
234 free(menu_font_data);
\r
236 menu_font_data = calloc((MENU_X2 ? 256 * 320 : 128 * 160) / 2, 1);
\r
237 if (menu_font_data == NULL)
\r
240 // generate default 8x10 font from fontdata8x8
\r
241 for (c = 0, fd = menu_font_data; c < 256; c++)
\r
243 for (l = 0; l < 8; l++)
\r
245 unsigned char fd8x8 = fontdata8x8[c*8+l];
\r
246 if (fd8x8&0x80) *fd = 0xf0;
\r
247 if (fd8x8&0x40) *fd |= 0x0f; fd++;
\r
248 if (fd8x8&0x20) *fd = 0xf0;
\r
249 if (fd8x8&0x10) *fd |= 0x0f; fd++;
\r
250 if (fd8x8&0x08) *fd = 0xf0;
\r
251 if (fd8x8&0x04) *fd |= 0x0f; fd++;
\r
252 if (fd8x8&0x02) *fd = 0xf0;
\r
253 if (fd8x8&0x01) *fd |= 0x0f; fd++;
\r
255 fd += 8*2/2; // 2 empty lines
\r
259 // expand default font
\r
260 fds = menu_font_data + 128 * 160 / 2 - 4;
\r
261 fd = menu_font_data + 256 * 320 / 2 - 1;
\r
262 for (c = 255; c >= 0; c--)
\r
264 for (l = 9; l >= 0; l--, fds -= 4)
\r
266 for (i = 3; i >= 0; i--) {
\r
267 int px = fds[i] & 0x0f;
\r
268 *fd-- = px | (px << 4);
\r
269 px = (fds[i] >> 4) & 0x0f;
\r
270 *fd-- = px | (px << 4);
\r
272 for (i = 3; i >= 0; i--) {
\r
273 int px = fds[i] & 0x0f;
\r
274 *fd-- = px | (px << 4);
\r
275 px = (fds[i] >> 4) & 0x0f;
\r
276 *fd-- = px | (px << 4);
\r
282 // load custom font and selector (stored as 1st symbol in font table)
\r
283 emu_make_path(buff, "skin/font.png", sizeof(buff));
\r
284 readpng(menu_font_data, buff, READPNG_FONT,
\r
285 MENU_X2 ? 256 : 128, MENU_X2 ? 320 : 160);
\r
286 // default selector symbol is '>'
\r
287 memcpy(menu_font_data, menu_font_data + ((int)'>') * me_mfont_w * me_mfont_h / 2,
\r
288 me_mfont_w * me_mfont_h / 2);
\r
289 emu_make_path(buff, "skin/selector.png", sizeof(buff));
\r
290 readpng(menu_font_data, buff, READPNG_SELECTOR, me_mfont_w, me_mfont_h);
\r
292 // load custom colors
\r
293 emu_make_path(buff, "skin/skin.txt", sizeof(buff));
\r
294 f = fopen(buff, "r");
\r
297 lprintf("found skin.txt\n");
\r
300 if (fgets(buff, sizeof(buff), f) == NULL)
\r
302 if (buff[0] == '#' || buff[0] == '/') continue; // comment
\r
303 if (buff[0] == '\r' || buff[0] == '\n') continue; // empty line
\r
304 if (strncmp(buff, "text_color=", 11) == 0)
\r
306 int tmp = parse_hex_color(buff+11);
\r
307 if (tmp >= 0) menu_text_color = tmp;
\r
308 else lprintf("skin.txt: parse error for text_color\n");
\r
310 else if (strncmp(buff, "selection_color=", 16) == 0)
\r
312 int tmp = parse_hex_color(buff+16);
\r
313 if (tmp >= 0) menu_sel_color = tmp;
\r
314 else lprintf("skin.txt: parse error for selection_color\n");
\r
317 lprintf("skin.txt: parse error: %s\n", buff);
\r
322 // use user's locale for savestate date display
\r
323 setlocale(LC_TIME, "");
\r
326 static void menu_draw_begin(int need_bg)
\r
328 plat_video_menu_begin();
\r
330 memcpy(g_menuscreen_ptr, g_menubg_ptr, g_menuscreen_w * g_menuscreen_h * 2);
\r
333 static void menu_draw_end(void)
\r
335 plat_video_menu_end();
\r
338 static void menu_darken_bg(void *dst, void *src, int pixels, int darker)
\r
340 unsigned int *dest = dst;
\r
341 unsigned int *sorc = src;
\r
347 unsigned int p = *sorc++;
\r
348 *dest++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);
\r
355 unsigned int p = *sorc++;
\r
356 *dest++ = (p&0xf79ef79e)>>1;
\r
361 static int me_id2offset(const menu_entry *ent, menu_id id)
\r
364 for (i = 0; ent->name; ent++, i++)
\r
365 if (ent->id == id) return i;
\r
367 lprintf("%s: id %i not found\n", __FUNCTION__, id);
\r
371 static void me_enable(menu_entry *entries, menu_id id, int enable)
\r
373 int i = me_id2offset(entries, id);
\r
374 entries[i].enabled = enable;
\r
377 static int me_count(const menu_entry *ent)
\r
381 for (ret = 0; ent->name; ent++, ret++)
\r
387 static unsigned int me_read_onoff(const menu_entry *ent)
\r
389 // guess var size based on mask to avoid reading too much
\r
390 if (ent->mask & 0xffff0000)
\r
391 return *(unsigned int *)ent->var & ent->mask;
\r
392 else if (ent->mask & 0xff00)
\r
393 return *(unsigned short *)ent->var & ent->mask;
\r
395 return *(unsigned char *)ent->var & ent->mask;
\r
398 static void me_toggle_onoff(menu_entry *ent)
\r
400 // guess var size based on mask to avoid reading too much
\r
401 if (ent->mask & 0xffff0000)
\r
402 *(unsigned int *)ent->var ^= ent->mask;
\r
403 else if (ent->mask & 0xff00)
\r
404 *(unsigned short *)ent->var ^= ent->mask;
\r
406 *(unsigned char *)ent->var ^= ent->mask;
\r
409 static void me_draw(const menu_entry *entries, int sel, void (*draw_more)(void))
\r
411 const menu_entry *ent, *ent_sel = entries;
\r
412 int x, y, w = 0, h = 0;
\r
413 int offs, col2_offs = 27 * me_mfont_w;
\r
418 /* calculate size of menu rect */
\r
419 for (ent = entries, i = n = 0; ent->name; ent++, i++)
\r
432 wt = strlen(ent->name) * me_mfont_w;
\r
433 if (wt == 0 && ent->generate_name)
\r
434 name = ent->generate_name(ent->id, &offs);
\r
436 wt = strlen(name) * me_mfont_w;
\r
438 if (ent->beh != MB_NONE)
\r
440 if (wt > col2_offs)
\r
441 col2_offs = wt + me_mfont_w;
\r
444 switch (ent->beh) {
\r
449 wt += me_mfont_w * 3;
\r
451 case MB_OPT_CUSTOM:
\r
452 case MB_OPT_CUSTONOFF:
\r
453 case MB_OPT_CUSTRANGE:
\r
456 if (ent->generate_name != NULL)
\r
457 name = ent->generate_name(ent->id, &offs);
\r
459 wt += (strlen(name) + offs) * me_mfont_w;
\r
462 wt += 10 * me_mfont_w;
\r
471 h = n * me_mfont_h;
\r
472 w += me_mfont_w * 2; /* selector */
\r
474 if (w > g_menuscreen_w) {
\r
475 lprintf("width %d > %d\n", w, g_menuscreen_w);
\r
476 w = g_menuscreen_w;
\r
478 if (h > g_menuscreen_h) {
\r
479 lprintf("height %d > %d\n", w, g_menuscreen_h);
\r
480 h = g_menuscreen_h;
\r
483 x = g_menuscreen_w / 2 - w / 2;
\r
484 y = g_menuscreen_h / 2 - h / 2;
\r
485 #ifdef MENU_ALIGN_LEFT
\r
486 if (x > 12) x = 12;
\r
490 menu_draw_begin(1);
\r
491 menu_draw_selection(x, y + vi_sel_ln * me_mfont_h, w);
\r
492 x += me_mfont_w * 2;
\r
494 for (ent = entries; ent->name; ent++)
\r
496 const char **names;
\r
497 int len, leftname_end = 0;
\r
503 if (strlen(name) == 0) {
\r
504 if (ent->generate_name)
\r
505 name = ent->generate_name(ent->id, &offs);
\r
507 if (name != NULL) {
\r
508 text_out16(x, y, name);
\r
509 leftname_end = x + (strlen(name) + 1) * me_mfont_w;
\r
512 switch (ent->beh) {
\r
516 text_out16(x + col2_offs, y, me_read_onoff(ent) ? "ON" : "OFF");
\r
519 text_out16(x + col2_offs, y, "%i", *(int *)ent->var);
\r
521 case MB_OPT_CUSTOM:
\r
522 case MB_OPT_CUSTONOFF:
\r
523 case MB_OPT_CUSTRANGE:
\r
526 if (ent->generate_name)
\r
527 name = ent->generate_name(ent->id, &offs);
\r
529 text_out16(x + col2_offs + offs * me_mfont_w, y, "%s", name);
\r
532 names = (const char **)ent->data;
\r
533 for (i = 0; names[i] != NULL; i++) {
\r
534 offs = x + col2_offs;
\r
535 len = strlen(names[i]);
\r
537 offs += (10 - len - 2) * me_mfont_w;
\r
538 if (offs < leftname_end)
\r
539 offs = leftname_end;
\r
540 if (i == *(unsigned char *)ent->var) {
\r
541 text_out16(offs, y, "%s", names[i]);
\r
551 /* display help or message if we have one */
\r
552 h = (g_menuscreen_h - h) / 2; // bottom area height
\r
553 if (menu_error_msg[0] != 0) {
\r
554 if (h >= me_mfont_h + 4)
\r
555 text_out16(5, g_menuscreen_h - me_mfont_h - 4, menu_error_msg);
\r
557 lprintf("menu msg doesn't fit!\n");
\r
559 if (plat_get_ticks_ms() - menu_error_time > 2048)
\r
560 menu_error_msg[0] = 0;
\r
562 else if (ent_sel->help != NULL) {
\r
563 const char *tmp = ent_sel->help;
\r
565 for (l = 0; tmp != NULL && *tmp != 0; l++)
\r
566 tmp = strchr(tmp + 1, '\n');
\r
567 if (h >= l * me_sfont_h + 4)
\r
568 for (tmp = ent_sel->help; l > 0; l--, tmp = strchr(tmp, '\n') + 1)
\r
569 smalltext_out16(5, g_menuscreen_h - (l * me_sfont_h + 4), tmp, 0xffff);
\r
572 if (draw_more != NULL)
\r
578 static int me_process(menu_entry *entry, int is_next, int is_lr)
\r
580 const char **names;
\r
582 switch (entry->beh)
\r
585 case MB_OPT_CUSTONOFF:
\r
586 me_toggle_onoff(entry);
\r
589 case MB_OPT_CUSTRANGE:
\r
590 c = is_lr ? 10 : 1;
\r
591 *(int *)entry->var += is_next ? c : -c;
\r
592 if (*(int *)entry->var < (int)entry->min)
\r
593 *(int *)entry->var = (int)entry->max;
\r
594 if (*(int *)entry->var > (int)entry->max)
\r
595 *(int *)entry->var = (int)entry->min;
\r
598 names = (const char **)entry->data;
\r
599 for (c = 0; names[c] != NULL; c++)
\r
601 *(signed char *)entry->var += is_next ? 1 : -1;
\r
602 if (*(signed char *)entry->var < 0)
\r
603 *(signed char *)entry->var = 0;
\r
604 if (*(signed char *)entry->var >= c)
\r
605 *(signed char *)entry->var = c - 1;
\r
612 static void debug_menu_loop(void);
\r
614 static int me_loop_d(menu_entry *menu, int *menu_sel, void (*draw_prep)(void), void (*draw_more)(void))
\r
616 int ret = 0, inp, sel = *menu_sel, menu_sel_max;
\r
618 menu_sel_max = me_count(menu) - 1;
\r
619 if (menu_sel_max < 0) {
\r
620 lprintf("no enabled menu entries\n");
\r
624 while ((!menu[sel].enabled || !menu[sel].selectable) && sel < menu_sel_max)
\r
627 /* make sure action buttons are not pressed on entering menu */
\r
628 me_draw(menu, sel, NULL);
\r
629 while (in_menu_wait_any(NULL, 50) & (PBTN_MOK|PBTN_MBACK|PBTN_MENU));
\r
633 if (draw_prep != NULL)
\r
636 me_draw(menu, sel, draw_more);
\r
637 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|
\r
638 PBTN_MOK|PBTN_MBACK|PBTN_MENU|PBTN_L|PBTN_R, NULL, 70);
\r
639 if (inp & (PBTN_MENU|PBTN_MBACK))
\r
642 if (inp & PBTN_UP ) {
\r
646 sel = menu_sel_max;
\r
648 while (!menu[sel].enabled || !menu[sel].selectable);
\r
650 if (inp & PBTN_DOWN) {
\r
653 if (sel > menu_sel_max)
\r
656 while (!menu[sel].enabled || !menu[sel].selectable);
\r
659 /* a bit hacky but oh well */
\r
660 if ((inp & (PBTN_L|PBTN_R)) == (PBTN_L|PBTN_R))
\r
663 if (inp & (PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R)) { /* multi choice */
\r
664 if (me_process(&menu[sel], (inp & (PBTN_RIGHT|PBTN_R)) ? 1 : 0,
\r
665 inp & (PBTN_L|PBTN_R)))
\r
669 if (inp & (PBTN_MOK|PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R))
\r
671 /* require PBTN_MOK for MB_NONE */
\r
672 if (menu[sel].handler != NULL && (menu[sel].beh != MB_NONE || (inp & PBTN_MOK))) {
\r
673 ret = menu[sel].handler(menu[sel].id, inp);
\r
675 menu_sel_max = me_count(menu) - 1; /* might change, so update */
\r
684 static int me_loop(menu_entry *menu, int *menu_sel)
\r
686 return me_loop_d(menu, menu_sel, NULL, NULL);
\r
689 /* ***************************************** */
\r
691 static void draw_menu_message(const char *msg, void (*draw_more)(void))
\r
693 int x, y, h, w, wt;
\r
697 for (h = 1, w = 0; *p != 0; h++) {
\r
698 for (wt = 0; *p != 0 && *p != '\n'; p++)
\r
708 x = g_menuscreen_w / 2 - w * me_mfont_w / 2;
\r
709 y = g_menuscreen_h / 2 - h * me_mfont_h / 2;
\r
713 menu_draw_begin(1);
\r
715 for (p = msg; *p != 0 && y <= g_menuscreen_h - me_mfont_h; y += me_mfont_h) {
\r
716 text_out16(x, y, p);
\r
718 for (; *p != 0 && *p != '\n'; p++)
\r
724 if (draw_more != NULL)
\r
730 // -------------- del confirm ---------------
\r
732 static void do_delete(const char *fpath, const char *fname)
\r
738 menu_draw_begin(1);
\r
740 len = strlen(fname);
\r
741 if (len > g_menuscreen_w / me_sfont_w)
\r
742 len = g_menuscreen_w / me_sfont_w;
\r
744 mid = g_menuscreen_w / 2;
\r
745 text_out16(mid - me_mfont_w * 15 / 2, 8 * me_mfont_h, "About to delete");
\r
746 smalltext_out16(mid - len * me_sfont_w / 2, 9 * me_mfont_h + 5, fname, 0xbdff);
\r
747 text_out16(mid - me_mfont_w * 13 / 2, 11 * me_mfont_h, "Are you sure?");
\r
749 nm = in_get_key_name(-1, -PBTN_MA3);
\r
750 snprintf(tmp, sizeof(tmp), "(%s - confirm, ", nm);
\r
752 nm = in_get_key_name(-1, -PBTN_MBACK);
\r
753 snprintf(tmp + len, sizeof(tmp) - len, "%s - cancel)", nm);
\r
756 text_out16(mid - me_mfont_w * len / 2, 12 * me_mfont_h, tmp);
\r
759 while (in_menu_wait_any(NULL, 50) & (PBTN_MENU|PBTN_MA2));
\r
760 inp = in_menu_wait(PBTN_MA3|PBTN_MBACK, NULL, 100);
\r
761 if (inp & PBTN_MA3)
\r
765 // -------------- ROM selector --------------
\r
767 static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)
\r
769 int max_cnt, start, i, x, pos;
\r
772 max_cnt = g_menuscreen_h / me_sfont_h;
\r
773 start = max_cnt / 2 - sel;
\r
774 n--; // exclude current dir (".")
\r
776 menu_draw_begin(1);
\r
778 // if (!rom_loaded)
\r
779 // menu_darken_bg(gp2x_screen, 320*240, 0);
\r
781 darken_ptr = (short *)g_menuscreen_ptr + g_menuscreen_w * max_cnt/2 * me_sfont_h;
\r
782 menu_darken_bg(darken_ptr, darken_ptr, g_menuscreen_w * me_sfont_h * 8 / 10, 0);
\r
784 x = 5 + me_mfont_w + 1;
\r
785 if (start - 2 >= 0)
\r
786 smalltext_out16(14, (start - 2) * me_sfont_h, curdir, 0xffff);
\r
787 for (i = 0; i < n; i++) {
\r
789 if (pos < 0) continue;
\r
790 if (pos >= max_cnt) break;
\r
791 if (namelist[i+1]->d_type == DT_DIR) {
\r
792 smalltext_out16(x, pos * me_sfont_h, "/", 0xfff6);
\r
793 smalltext_out16(x + me_sfont_w, pos * me_sfont_h, namelist[i+1]->d_name, 0xfff6);
\r
795 unsigned short color = fname2color(namelist[i+1]->d_name);
\r
796 smalltext_out16(x, pos * me_sfont_h, namelist[i+1]->d_name, color);
\r
799 smalltext_out16(5, max_cnt/2 * me_sfont_h, ">", 0xffff);
\r
803 static int scandir_cmp(const void *p1, const void *p2)
\r
805 const struct dirent **d1 = (const struct dirent **)p1;
\r
806 const struct dirent **d2 = (const struct dirent **)p2;
\r
807 if ((*d1)->d_type == (*d2)->d_type)
\r
808 return alphasort(d1, d2);
\r
809 if ((*d1)->d_type == DT_DIR)
\r
810 return -1; // put before
\r
811 if ((*d2)->d_type == DT_DIR)
\r
814 return alphasort(d1, d2);
\r
817 static int scandir_filter(const struct dirent *ent)
\r
822 if (ent == NULL || ent->d_name == NULL) return 0;
\r
823 if (strlen(ent->d_name) < 5) return 1;
\r
825 p = ent->d_name + strlen(ent->d_name) - 4;
\r
827 for (i = 0; i < array_size(filter_exts); i++)
\r
828 if (strcmp(p, filter_exts[i]) == 0)
\r
834 static int dirent_seek_char(struct dirent **namelist, int len, int sel, char c)
\r
839 for (i = sel + 1; i != sel; i++) {
\r
843 if (tolower_simple(namelist[i]->d_name[0]) == c)
\r
850 static char *menu_loop_romsel(char *curr_path, int len)
\r
852 struct dirent **namelist;
\r
853 int n, inp, sel = 0;
\r
854 char *ret = NULL, *fname = NULL;
\r
858 // is this a dir or a full path?
\r
859 if (!plat_is_dir(curr_path)) {
\r
860 char *p = curr_path + strlen(curr_path) - 1;
\r
861 for (; p > curr_path && *p != '/'; p--)
\r
867 n = scandir(curr_path, &namelist, scandir_filter, (void *)scandir_cmp);
\r
870 lprintf("menu_loop_romsel failed, dir: %s\n", curr_path);
\r
873 t = getcwd(curr_path, len);
\r
875 plat_get_root_dir(curr_path, len);
\r
876 n = scandir(curr_path, &namelist, scandir_filter, (void *)scandir_cmp);
\r
879 lprintf("menu_loop_romsel failed, dir: %s\n", curr_path);
\r
885 // note: we don't show '.' so sel is namelist index - 1
\r
886 if (fname != NULL) {
\r
888 for (i = 1; i < n; i++) {
\r
889 char *dname = namelist[i]->d_name;
\r
890 if (dname[0] == fname[0] && strcmp(dname, fname) == 0) {
\r
897 /* make sure action buttons are not pressed on entering menu */
\r
898 draw_dirlist(curr_path, namelist, n, sel);
\r
899 while (in_menu_wait_any(NULL, 50) & (PBTN_MOK|PBTN_MBACK|PBTN_MENU))
\r
904 draw_dirlist(curr_path, namelist, n, sel);
\r
905 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|
\r
906 PBTN_L|PBTN_R|PBTN_MA2|PBTN_MOK|PBTN_MBACK|PBTN_MENU|PBTN_CHAR, &cinp, 33);
\r
907 if (inp & PBTN_UP ) { sel--; if (sel < 0) sel = n-2; }
\r
908 if (inp & PBTN_DOWN) { sel++; if (sel > n-2) sel = 0; }
\r
909 if (inp & PBTN_LEFT) { sel-=10; if (sel < 0) sel = 0; }
\r
910 if (inp & PBTN_L) { sel-=24; if (sel < 0) sel = 0; }
\r
911 if (inp & PBTN_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }
\r
912 if (inp & PBTN_R) { sel+=24; if (sel > n-2) sel = n-2; }
\r
913 if (inp & PBTN_CHAR) sel = dirent_seek_char(namelist, n, sel, cinp);
\r
914 if ((inp & PBTN_MOK) || (inp & (PBTN_MENU|PBTN_MA2)) == (PBTN_MENU|PBTN_MA2))
\r
917 if (namelist[sel+1]->d_type == DT_REG)
\r
919 strcpy(rom_fname_reload, curr_path);
\r
920 strcat(rom_fname_reload, "/");
\r
921 strcat(rom_fname_reload, namelist[sel+1]->d_name);
\r
922 if (inp & PBTN_MOK) { // return sel
\r
923 ret = rom_fname_reload;
\r
926 do_delete(rom_fname_reload, namelist[sel+1]->d_name);
\r
928 while (n--) free(namelist[n]);
\r
933 else if (namelist[sel+1]->d_type == DT_DIR)
\r
937 if (!(inp & PBTN_MOK))
\r
939 newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;
\r
940 newdir = malloc(newlen);
\r
941 if (newdir == NULL)
\r
943 if (strcmp(namelist[sel+1]->d_name, "..") == 0) {
\r
944 char *start = curr_path;
\r
945 p = start + strlen(start) - 1;
\r
946 while (*p == '/' && p > start) p--;
\r
947 while (*p != '/' && p > start) p--;
\r
948 if (p <= start) strcpy(newdir, "/");
\r
949 else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }
\r
951 strcpy(newdir, curr_path);
\r
952 p = newdir + strlen(newdir) - 1;
\r
953 while (*p == '/' && p >= newdir) *p-- = 0;
\r
954 strcat(newdir, "/");
\r
955 strcat(newdir, namelist[sel+1]->d_name);
\r
957 ret = menu_loop_romsel(newdir, newlen);
\r
963 // unknown file type, happens on NTFS mounts. Try to guess.
\r
964 FILE *tstf; int tmp;
\r
965 strcpy(rom_fname_reload, curr_path);
\r
966 strcat(rom_fname_reload, "/");
\r
967 strcat(rom_fname_reload, namelist[sel+1]->d_name);
\r
968 tstf = fopen(rom_fname_reload, "rb");
\r
971 if (fread(&tmp, 1, 1, tstf) > 0 || ferror(tstf) == 0)
\r
972 namelist[sel+1]->d_type = DT_REG;
\r
973 else namelist[sel+1]->d_type = DT_DIR;
\r
979 if (inp & PBTN_MBACK)
\r
984 while (n--) free(namelist[n]);
\r
988 // restore curr_path
\r
989 if (fname != NULL) {
\r
990 n = strlen(curr_path);
\r
991 if (curr_path + n + 1 == fname)
\r
992 curr_path[n] = '/';
\r
998 // ------------ savestate loader ------------
\r
1000 #define STATE_SLOT_COUNT 10
\r
1002 static int state_slot_flags = 0;
\r
1003 static int state_slot_times[STATE_SLOT_COUNT];
\r
1005 static void state_check_slots(void)
\r
1009 state_slot_flags = 0;
\r
1011 for (slot = 0; slot < STATE_SLOT_COUNT; slot++) {
\r
1012 state_slot_times[slot] = 0;
\r
1013 if (emu_check_save_file(slot, &state_slot_times[slot]))
\r
1014 state_slot_flags |= 1 << slot;
\r
1018 static void draw_savestate_bg(int slot);
\r
1020 static void draw_savestate_menu(int menu_sel, int is_loading)
\r
1022 int i, x, y, w, h;
\r
1023 char time_buf[32];
\r
1025 if (state_slot_flags & (1 << menu_sel))
\r
1026 draw_savestate_bg(menu_sel);
\r
1028 w = (13 + 2) * me_mfont_w;
\r
1029 h = (1+2+STATE_SLOT_COUNT+1) * me_mfont_h;
\r
1030 x = g_menuscreen_w / 2 - w / 2;
\r
1032 y = g_menuscreen_h / 2 - h / 2;
\r
1034 #ifdef MENU_ALIGN_LEFT
\r
1035 if (x > 12 + me_mfont_w * 2)
\r
1036 x = 12 + me_mfont_w * 2;
\r
1039 menu_draw_begin(1);
\r
1041 text_out16(x, y, is_loading ? "Load state" : "Save state");
\r
1042 y += 3 * me_mfont_h;
\r
1044 menu_draw_selection(x - me_mfont_w * 2, y + menu_sel * me_mfont_h, (23 + 2) * me_mfont_w + 4);
\r
1046 /* draw all slots */
\r
1047 for (i = 0; i < STATE_SLOT_COUNT; i++, y += me_mfont_h)
\r
1049 if (!(state_slot_flags & (1 << i)))
\r
1050 strcpy(time_buf, "free");
\r
1052 strcpy(time_buf, "USED");
\r
1053 if (state_slot_times[i] != 0) {
\r
1054 time_t time = state_slot_times[i];
\r
1055 struct tm *t = localtime(&time);
\r
1056 strftime(time_buf, sizeof(time_buf), "%x %R", t);
\r
1060 text_out16(x, y, "SLOT %i (%s)", i, time_buf);
\r
1062 text_out16(x, y, "back");
\r
1067 static int menu_loop_savestate(int is_loading)
\r
1069 static int menu_sel = STATE_SLOT_COUNT;
\r
1070 int menu_sel_max = STATE_SLOT_COUNT;
\r
1071 unsigned long inp = 0;
\r
1074 state_check_slots();
\r
1076 if (!(state_slot_flags & (1 << menu_sel)) && is_loading)
\r
1077 menu_sel = menu_sel_max;
\r
1081 draw_savestate_menu(menu_sel, is_loading);
\r
1082 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_MOK|PBTN_MBACK, NULL, 100);
\r
1083 if (inp & PBTN_UP) {
\r
1087 menu_sel = menu_sel_max;
\r
1088 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
\r
1090 if (inp & PBTN_DOWN) {
\r
1093 if (menu_sel > menu_sel_max)
\r
1095 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
\r
1097 if (inp & PBTN_MOK) { // save/load
\r
1098 if (menu_sel < STATE_SLOT_COUNT) {
\r
1099 state_slot = menu_sel;
\r
1100 if (emu_save_load_game(is_loading, 0)) {
\r
1101 me_update_msg(is_loading ? "Load failed" : "Save failed");
\r
1109 if (inp & PBTN_MBACK)
\r
1116 // -------------- key config --------------
\r
1118 static char *action_binds(int player_idx, int action_mask, int dev_id)
\r
1120 int dev = 0, dev_last = IN_MAX_DEVS - 1;
\r
1121 int can_combo = 1, type;
\r
1123 static_buff[0] = 0;
\r
1125 type = IN_BINDTYPE_EMU;
\r
1126 if (player_idx >= 0) {
\r
1128 type = IN_BINDTYPE_PLAYER12;
\r
1130 if (player_idx == 1)
\r
1131 action_mask <<= 16;
\r
1134 dev = dev_last = dev_id;
\r
1136 for (; dev <= dev_last; dev++) {
\r
1137 int k, count = 0, combo = 0;
\r
1140 binds = in_get_dev_binds(dev);
\r
1141 if (binds == NULL)
\r
1144 in_get_config(dev, IN_CFG_BIND_COUNT, &count);
\r
1145 in_get_config(dev, IN_CFG_DOES_COMBOS, &combo);
\r
1146 combo = combo && can_combo;
\r
1148 for (k = 0; k < count; k++) {
\r
1149 const char *xname;
\r
1152 if (!(binds[IN_BIND_OFFS(k, type)] & action_mask))
\r
1155 xname = in_get_key_name(dev, k);
\r
1156 len = strlen(static_buff);
\r
1158 strncat(static_buff, combo ? " + " : ", ",
\r
1159 sizeof(static_buff) - len - 1);
\r
1160 len += combo ? 3 : 2;
\r
1162 strncat(static_buff, xname, sizeof(static_buff) - len - 1);
\r
1166 return static_buff;
\r
1169 static int count_bound_keys(int dev_id, int action_mask, int bindtype)
\r
1175 binds = in_get_dev_binds(dev_id);
\r
1176 if (binds == NULL)
\r
1179 in_get_config(dev_id, IN_CFG_BIND_COUNT, &count);
\r
1180 for (k = 0; k < count; k++)
\r
1182 if (binds[IN_BIND_OFFS(k, bindtype)] & action_mask)
\r
1189 static void draw_key_config(const me_bind_action *opts, int opt_cnt, int player_idx,
\r
1190 int sel, int dev_id, int dev_count, int is_bind)
\r
1192 char buff[64], buff2[32];
\r
1193 const char *dev_name;
\r
1196 w = ((player_idx >= 0) ? 20 : 30) * me_mfont_w;
\r
1197 x = g_menuscreen_w / 2 - w / 2;
\r
1198 y = (g_menuscreen_h - 4 * me_mfont_h) / 2 - (2 + opt_cnt) * me_mfont_h / 2;
\r
1199 if (x < me_mfont_w * 2)
\r
1200 x = me_mfont_w * 2;
\r
1202 menu_draw_begin(1);
\r
1203 if (player_idx >= 0)
\r
1204 text_out16(x, y, "Player %i controls", player_idx + 1);
\r
1206 text_out16(x, y, "Emulator controls");
\r
1208 y += 2 * me_mfont_h;
\r
1209 menu_draw_selection(x - me_mfont_w * 2, y + sel * me_mfont_h, w + 2 * me_mfont_w);
\r
1211 for (i = 0; i < opt_cnt; i++, y += me_mfont_h)
\r
1212 text_out16(x, y, "%s : %s", opts[i].name,
\r
1213 action_binds(player_idx, opts[i].mask, dev_id));
\r
1216 dev_name = "(all devices)";
\r
1218 dev_name = in_get_dev_name(dev_id, 1, 1);
\r
1219 w = strlen(dev_name) * me_mfont_w;
\r
1220 if (w < 30 * me_mfont_w)
\r
1221 w = 30 * me_mfont_w;
\r
1222 if (w > g_menuscreen_w)
\r
1223 w = g_menuscreen_w;
\r
1225 x = g_menuscreen_w / 2 - w / 2;
\r
1228 snprintf(buff2, sizeof(buff2), "%s", in_get_key_name(-1, -PBTN_MOK));
\r
1229 snprintf(buff, sizeof(buff), "%s - bind, %s - clear", buff2,
\r
1230 in_get_key_name(-1, -PBTN_MA2));
\r
1231 text_out16(x, g_menuscreen_h - 4 * me_mfont_h, buff);
\r
1234 text_out16(x, g_menuscreen_h - 4 * me_mfont_h, "Press a button to bind/unbind");
\r
1236 if (dev_count > 1) {
\r
1237 text_out16(x, g_menuscreen_h - 3 * me_mfont_h, dev_name);
\r
1238 text_out16(x, g_menuscreen_h - 2 * me_mfont_h, "Press left/right for other devs");
\r
1244 static void key_config_loop(const me_bind_action *opts, int opt_cnt, int player_idx)
\r
1246 int i, sel = 0, menu_sel_max = opt_cnt - 1, does_combos = 0;
\r
1247 int dev_id, bind_dev_id, dev_count, kc, is_down, mkey;
\r
1248 int unbind, bindtype, mask_shift;
\r
1250 for (i = 0, dev_id = -1, dev_count = 0; i < IN_MAX_DEVS; i++) {
\r
1251 if (in_get_dev_name(i, 1, 0) != NULL) {
\r
1258 if (dev_id == -1) {
\r
1259 lprintf("no devs, can't do config\n");
\r
1263 dev_id = -1; // show all
\r
1265 if (player_idx == 1)
\r
1267 bindtype = player_idx >= 0 ? IN_BINDTYPE_PLAYER12 : IN_BINDTYPE_EMU;
\r
1271 draw_key_config(opts, opt_cnt, player_idx, sel, dev_id, dev_count, 0);
\r
1272 mkey = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT
\r
1273 |PBTN_MBACK|PBTN_MOK|PBTN_MA2, NULL, 100);
\r
1275 case PBTN_UP: sel--; if (sel < 0) sel = menu_sel_max; continue;
\r
1276 case PBTN_DOWN: sel++; if (sel > menu_sel_max) sel = 0; continue;
\r
1278 for (i = 0, dev_id--; i < IN_MAX_DEVS + 1; i++, dev_id--) {
\r
1280 dev_id = IN_MAX_DEVS - 1;
\r
1281 if (dev_id == -1 || in_get_dev_name(dev_id, 1, 0) != NULL)
\r
1286 for (i = 0, dev_id++; i < IN_MAX_DEVS; i++, dev_id++) {
\r
1287 if (dev_id >= IN_MAX_DEVS)
\r
1289 if (dev_id == -1 || in_get_dev_name(dev_id, 1, 0) != NULL)
\r
1296 if (sel >= opt_cnt)
\r
1298 while (in_menu_wait_any(NULL, 30) & PBTN_MOK)
\r
1302 in_unbind_all(dev_id, opts[sel].mask << mask_shift, bindtype);
\r
1307 draw_key_config(opts, opt_cnt, player_idx, sel, dev_id, dev_count, 1);
\r
1309 /* wait for some up event */
\r
1310 for (is_down = 1; is_down; )
\r
1311 kc = in_update_keycode(&bind_dev_id, &is_down, NULL, -1);
\r
1313 i = count_bound_keys(bind_dev_id, opts[sel].mask << mask_shift, bindtype);
\r
1316 /* allow combos if device supports them */
\r
1317 in_get_config(bind_dev_id, IN_CFG_DOES_COMBOS, &does_combos);
\r
1318 if (i == 1 && bindtype == IN_BINDTYPE_EMU && does_combos)
\r
1322 in_unbind_all(bind_dev_id, opts[sel].mask << mask_shift, bindtype);
\r
1324 in_bind_key(bind_dev_id, kc, opts[sel].mask << mask_shift, bindtype, 0);
\r