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
18 #include "readpng.h"
\r
19 #include "lprintf.h"
\r
24 static char static_buff[64];
\r
25 static int menu_error_time = 0;
\r
26 char menu_error_msg[64] = { 0, };
\r
27 void *g_menuscreen_ptr;
\r
28 void *g_menubg_src_ptr;
\r
31 #if !MSCREEN_SIZE_FIXED
\r
36 static unsigned char *menu_font_data = NULL;
\r
37 static int menu_text_color = 0xffff; // default to white
\r
38 static int menu_sel_color = -1; // disabled
\r
40 /* note: these might become non-constant in future */
\r
42 static const int me_mfont_w = 16, me_mfont_h = 20;
\r
43 static const int me_sfont_w = 12, me_sfont_h = 20;
\r
45 static const int me_mfont_w = 8, me_mfont_h = 10;
\r
46 static const int me_sfont_w = 6, me_sfont_h = 10;
\r
49 // draws text to current bbp16 screen
\r
50 static void text_out16_(int x, int y, const char *text, int color)
\r
52 int i, lh, tr, tg, tb, len;
\r
53 unsigned short *dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w;
\r
54 tr = (color & 0xf800) >> 8;
\r
55 tg = (color & 0x07e0) >> 3;
\r
56 tb = (color & 0x001f) << 3;
\r
58 if (text == (void *)1)
\r
67 for (p = text; *p != 0 && *p != '\n'; p++)
\r
73 if (y + lh > g_menuscreen_h)
\r
74 lh = g_menuscreen_h - y;
\r
76 for (i = 0; i < len; i++)
\r
78 unsigned char *src = menu_font_data + (unsigned int)text[i] * me_mfont_w * me_mfont_h / 2;
\r
79 unsigned short *dst = dest;
\r
82 for (l = 0; l < lh; l++, dst += g_menuscreen_w - me_mfont_w)
\r
84 for (u = me_mfont_w / 2; u > 0; u--, src++)
\r
88 r = (*dst & 0xf800) >> 8;
\r
89 g = (*dst & 0x07e0) >> 3;
\r
90 b = (*dst & 0x001f) << 3;
\r
91 r = (c^0xf)*r/15 + c*tr/15;
\r
92 g = (c^0xf)*g/15 + c*tg/15;
\r
93 b = (c^0xf)*b/15 + c*tb/15;
\r
94 *dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);
\r
96 r = (*dst & 0xf800) >> 8;
\r
97 g = (*dst & 0x07e0) >> 3;
\r
98 b = (*dst & 0x001f) << 3;
\r
99 r = (c^0xf)*r/15 + c*tr/15;
\r
100 g = (c^0xf)*g/15 + c*tg/15;
\r
101 b = (c^0xf)*b/15 + c*tb/15;
\r
102 *dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);
\r
105 dest += me_mfont_w;
\r
109 void text_out16(int x, int y, const char *texto, ...)
\r
113 int maxw = (g_menuscreen_w - x) / me_mfont_w;
\r
118 va_start(args, texto);
\r
119 vsnprintf(buffer, sizeof(buffer), texto, args);
\r
122 if (maxw > sizeof(buffer) - 1)
\r
123 maxw = sizeof(buffer) - 1;
\r
126 text_out16_(x,y,buffer,menu_text_color);
\r
129 /* draws in 6x8 font, might multiply size by integer */
\r
130 static void smalltext_out16_(int x, int y, const char *texto, int color)
\r
132 unsigned char *src;
\r
133 unsigned short *dst;
\r
134 int multiplier = me_sfont_w / 6;
\r
137 for (i = 0;; i++, x += me_sfont_w)
\r
139 unsigned char c = (unsigned char) texto[i];
\r
142 if (!c || c == '\n')
\r
145 src = fontdata6x8[c];
\r
146 dst = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w;
\r
151 for (h2 = multiplier; h2 > 0; h2--)
\r
153 for (m = 0x20; m; m >>= 1) {
\r
155 for (w2 = multiplier; w2 > 0; w2--)
\r
161 dst += g_menuscreen_w - me_sfont_w;
\r
168 static void smalltext_out16(int x, int y, const char *texto, int color)
\r
171 int maxw = (g_menuscreen_w - x) / me_sfont_w;
\r
176 strncpy(buffer, texto, sizeof(buffer));
\r
177 if (maxw > sizeof(buffer) - 1)
\r
178 maxw = sizeof(buffer) - 1;
\r
181 smalltext_out16_(x, y, buffer, color);
\r
184 static void menu_draw_selection(int x, int y, int w)
\r
187 unsigned short *dst, *dest;
\r
189 text_out16_(x, y, (void *)1, (menu_sel_color < 0) ? menu_text_color : menu_sel_color);
\r
191 if (menu_sel_color < 0) return; // no selection hilight
\r
194 dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w + me_mfont_w * 2 - 2;
\r
195 for (h = me_mfont_h + 1; h > 0; h--)
\r
198 for (i = w - (me_mfont_w * 2 - 2); i > 0; i--)
\r
199 *dst++ = menu_sel_color;
\r
200 dest += g_menuscreen_w;
\r
204 static int parse_hex_color(char *buff)
\r
207 int t = (int) strtoul(buff, &endp, 16);
\r
210 return ((t<<8)&0xf800) | ((t>>5)&0x07e0) | ((t>>19)&0x1f);
\r
212 return ((t>>8)&0xf800) | ((t>>5)&0x07e0) | ((t>>3)&0x1f);
\r
217 void menu_init(void)
\r
220 unsigned char *fd, *fds;
\r
224 if (menu_font_data != NULL)
\r
225 free(menu_font_data);
\r
227 menu_font_data = calloc((MENU_X2 ? 256 * 320 : 128 * 160) / 2, 1);
\r
228 if (menu_font_data == NULL)
\r
231 // generate default 8x10 font from fontdata8x8
\r
232 for (c = 0, fd = menu_font_data; c < 256; c++)
\r
234 for (l = 0; l < 8; l++)
\r
236 unsigned char fd8x8 = fontdata8x8[c*8+l];
\r
237 if (fd8x8&0x80) *fd = 0xf0;
\r
238 if (fd8x8&0x40) *fd |= 0x0f; fd++;
\r
239 if (fd8x8&0x20) *fd = 0xf0;
\r
240 if (fd8x8&0x10) *fd |= 0x0f; fd++;
\r
241 if (fd8x8&0x08) *fd = 0xf0;
\r
242 if (fd8x8&0x04) *fd |= 0x0f; fd++;
\r
243 if (fd8x8&0x02) *fd = 0xf0;
\r
244 if (fd8x8&0x01) *fd |= 0x0f; fd++;
\r
246 fd += 8*2/2; // 2 empty lines
\r
250 // expand default font
\r
251 fds = menu_font_data + 128 * 160 / 2 - 4;
\r
252 fd = menu_font_data + 256 * 320 / 2 - 1;
\r
253 for (c = 255; c >= 0; c--)
\r
255 for (l = 9; l >= 0; l--, fds -= 4)
\r
257 for (i = 3; i >= 0; i--) {
\r
258 int px = fds[i] & 0x0f;
\r
259 *fd-- = px | (px << 4);
\r
260 px = (fds[i] >> 4) & 0x0f;
\r
261 *fd-- = px | (px << 4);
\r
263 for (i = 3; i >= 0; i--) {
\r
264 int px = fds[i] & 0x0f;
\r
265 *fd-- = px | (px << 4);
\r
266 px = (fds[i] >> 4) & 0x0f;
\r
267 *fd-- = px | (px << 4);
\r
273 // load custom font and selector (stored as 1st symbol in font table)
\r
274 emu_make_path(buff, "skin/font.png", sizeof(buff));
\r
275 readpng(menu_font_data, buff, READPNG_FONT,
\r
276 MENU_X2 ? 256 : 128, MENU_X2 ? 320 : 160);
\r
277 // default selector symbol is '>'
\r
278 memcpy(menu_font_data, menu_font_data + ((int)'>') * me_mfont_w * me_mfont_h / 2,
\r
279 me_mfont_w * me_mfont_h / 2);
\r
280 emu_make_path(buff, "skin/selector.png", sizeof(buff));
\r
281 readpng(menu_font_data, buff, READPNG_SELECTOR, me_mfont_w, me_mfont_h);
\r
283 // load custom colors
\r
284 emu_make_path(buff, "skin/skin.txt", sizeof(buff));
\r
285 f = fopen(buff, "r");
\r
288 lprintf("found skin.txt\n");
\r
291 if (fgets(buff, sizeof(buff), f) == NULL)
\r
293 if (buff[0] == '#' || buff[0] == '/') continue; // comment
\r
294 if (buff[0] == '\r' || buff[0] == '\n') continue; // empty line
\r
295 if (strncmp(buff, "text_color=", 11) == 0)
\r
297 int tmp = parse_hex_color(buff+11);
\r
298 if (tmp >= 0) menu_text_color = tmp;
\r
299 else lprintf("skin.txt: parse error for text_color\n");
\r
301 else if (strncmp(buff, "selection_color=", 16) == 0)
\r
303 int tmp = parse_hex_color(buff+16);
\r
304 if (tmp >= 0) menu_sel_color = tmp;
\r
305 else lprintf("skin.txt: parse error for selection_color\n");
\r
308 lprintf("skin.txt: parse error: %s\n", buff);
\r
314 static void menu_draw_begin(int need_bg)
\r
316 plat_video_menu_begin();
\r
318 memcpy(g_menuscreen_ptr, g_menubg_ptr, g_menuscreen_w * g_menuscreen_h * 2);
\r
321 static void menu_draw_end(void)
\r
323 plat_video_menu_end();
\r
326 static void menu_darken_bg(void *dst, void *src, int pixels, int darker)
\r
328 unsigned int *dest = dst;
\r
329 unsigned int *sorc = src;
\r
335 unsigned int p = *sorc++;
\r
336 *dest++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);
\r
343 unsigned int p = *sorc++;
\r
344 *dest++ = (p&0xf79ef79e)>>1;
\r
349 static int me_id2offset(const menu_entry *ent, menu_id id)
\r
352 for (i = 0; ent->name; ent++, i++)
\r
353 if (ent->id == id) return i;
\r
355 lprintf("%s: id %i not found\n", __FUNCTION__, id);
\r
359 static void me_enable(menu_entry *entries, menu_id id, int enable)
\r
361 int i = me_id2offset(entries, id);
\r
362 entries[i].enabled = enable;
\r
365 static int me_count(const menu_entry *ent)
\r
369 for (ret = 0; ent->name; ent++, ret++)
\r
375 static unsigned int me_read_onoff(const menu_entry *ent)
\r
377 // guess var size based on mask to avoid reading too much
\r
378 if (ent->mask & 0xffff0000)
\r
379 return *(unsigned int *)ent->var & ent->mask;
\r
380 else if (ent->mask & 0xff00)
\r
381 return *(unsigned short *)ent->var & ent->mask;
\r
383 return *(unsigned char *)ent->var & ent->mask;
\r
386 static void me_toggle_onoff(menu_entry *ent)
\r
388 // guess var size based on mask to avoid reading too much
\r
389 if (ent->mask & 0xffff0000)
\r
390 *(unsigned int *)ent->var ^= ent->mask;
\r
391 else if (ent->mask & 0xff00)
\r
392 *(unsigned short *)ent->var ^= ent->mask;
\r
394 *(unsigned char *)ent->var ^= ent->mask;
\r
397 static void me_draw(const menu_entry *entries, int sel, void (*draw_more)(void))
\r
399 const menu_entry *ent, *ent_sel = entries;
\r
400 int x, y, w = 0, h = 0;
\r
401 int offs, col2_offs = 27 * me_mfont_w;
\r
406 /* calculate size of menu rect */
\r
407 for (ent = entries, i = n = 0; ent->name; ent++, i++)
\r
420 wt = strlen(ent->name) * me_mfont_w;
\r
421 if (wt == 0 && ent->generate_name)
\r
422 name = ent->generate_name(ent->id, &offs);
\r
424 wt = strlen(name) * me_mfont_w;
\r
426 if (ent->beh != MB_NONE)
\r
428 if (wt > col2_offs)
\r
429 col2_offs = wt + me_mfont_w;
\r
432 switch (ent->beh) {
\r
437 wt += me_mfont_w * 3;
\r
439 case MB_OPT_CUSTOM:
\r
440 case MB_OPT_CUSTONOFF:
\r
441 case MB_OPT_CUSTRANGE:
\r
444 if (ent->generate_name != NULL)
\r
445 name = ent->generate_name(ent->id, &offs);
\r
447 wt += (strlen(name) + offs) * me_mfont_w;
\r
450 wt += 10 * me_mfont_w;
\r
459 h = n * me_mfont_h;
\r
460 w += me_mfont_w * 2; /* selector */
\r
462 if (w > g_menuscreen_w) {
\r
463 lprintf("width %d > %d\n", w, g_menuscreen_w);
\r
464 w = g_menuscreen_w;
\r
466 if (h > g_menuscreen_h) {
\r
467 lprintf("height %d > %d\n", w, g_menuscreen_h);
\r
468 h = g_menuscreen_h;
\r
471 x = g_menuscreen_w / 2 - w / 2;
\r
472 y = g_menuscreen_h / 2 - h / 2;
\r
473 #ifdef MENU_ALIGN_LEFT
\r
474 if (x > 12) x = 12;
\r
478 menu_draw_begin(1);
\r
479 menu_draw_selection(x, y + vi_sel_ln * me_mfont_h, w);
\r
480 x += me_mfont_w * 2;
\r
482 for (ent = entries; ent->name; ent++)
\r
484 const char **names;
\r
485 int len, leftname_end = 0;
\r
491 if (strlen(name) == 0) {
\r
492 if (ent->generate_name)
\r
493 name = ent->generate_name(ent->id, &offs);
\r
495 if (name != NULL) {
\r
496 text_out16(x, y, name);
\r
497 leftname_end = x + (strlen(name) + 1) * me_mfont_w;
\r
500 switch (ent->beh) {
\r
504 text_out16(x + col2_offs, y, me_read_onoff(ent) ? "ON" : "OFF");
\r
507 text_out16(x + col2_offs, y, "%i", *(int *)ent->var);
\r
509 case MB_OPT_CUSTOM:
\r
510 case MB_OPT_CUSTONOFF:
\r
511 case MB_OPT_CUSTRANGE:
\r
514 if (ent->generate_name)
\r
515 name = ent->generate_name(ent->id, &offs);
\r
517 text_out16(x + col2_offs + offs * me_mfont_w, y, "%s", name);
\r
520 names = (const char **)ent->data;
\r
521 for (i = 0; names[i] != NULL; i++) {
\r
522 offs = x + col2_offs;
\r
523 len = strlen(names[i]);
\r
525 offs += (10 - len - 2) * me_mfont_w;
\r
526 if (offs < leftname_end)
\r
527 offs = leftname_end;
\r
528 if (i == *(unsigned char *)ent->var) {
\r
529 text_out16(offs, y, "%s", names[i]);
\r
539 /* display help or message if we have one */
\r
540 h = (g_menuscreen_h - h) / 2; // bottom area height
\r
541 if (menu_error_msg[0] != 0) {
\r
542 if (h >= me_mfont_h + 4)
\r
543 text_out16(5, g_menuscreen_h - me_mfont_h - 4, menu_error_msg);
\r
545 lprintf("menu msg doesn't fit!\n");
\r
547 if (plat_get_ticks_ms() - menu_error_time > 2048)
\r
548 menu_error_msg[0] = 0;
\r
550 else if (ent_sel->help != NULL) {
\r
551 const char *tmp = ent_sel->help;
\r
553 for (l = 0; tmp != NULL && *tmp != 0; l++)
\r
554 tmp = strchr(tmp + 1, '\n');
\r
555 if (h >= l * me_sfont_h + 4)
\r
556 for (tmp = ent_sel->help; l > 0; l--, tmp = strchr(tmp, '\n') + 1)
\r
557 smalltext_out16(5, g_menuscreen_h - (l * me_sfont_h + 4), tmp, 0xffff);
\r
560 if (draw_more != NULL)
\r
566 static int me_process(menu_entry *entry, int is_next, int is_lr)
\r
568 const char **names;
\r
570 switch (entry->beh)
\r
573 case MB_OPT_CUSTONOFF:
\r
574 me_toggle_onoff(entry);
\r
577 case MB_OPT_CUSTRANGE:
\r
578 c = is_lr ? 10 : 1;
\r
579 *(int *)entry->var += is_next ? c : -c;
\r
580 if (*(int *)entry->var < (int)entry->min)
\r
581 *(int *)entry->var = (int)entry->max;
\r
582 if (*(int *)entry->var > (int)entry->max)
\r
583 *(int *)entry->var = (int)entry->min;
\r
586 names = (const char **)entry->data;
\r
587 for (c = 0; names[c] != NULL; c++)
\r
589 *(signed char *)entry->var += is_next ? 1 : -1;
\r
590 if (*(signed char *)entry->var < 0)
\r
591 *(signed char *)entry->var = 0;
\r
592 if (*(signed char *)entry->var >= c)
\r
593 *(signed char *)entry->var = c - 1;
\r
600 static void debug_menu_loop(void);
\r
602 static int me_loop_d(menu_entry *menu, int *menu_sel, void (*draw_prep)(void), void (*draw_more)(void))
\r
604 int ret = 0, inp, sel = *menu_sel, menu_sel_max;
\r
606 menu_sel_max = me_count(menu) - 1;
\r
607 if (menu_sel_max < 0) {
\r
608 lprintf("no enabled menu entries\n");
\r
612 while ((!menu[sel].enabled || !menu[sel].selectable) && sel < menu_sel_max)
\r
615 /* make sure action buttons are not pressed on entering menu */
\r
616 me_draw(menu, sel, NULL);
\r
617 while (in_menu_wait_any(50) & (PBTN_MOK|PBTN_MBACK|PBTN_MENU));
\r
621 if (draw_prep != NULL)
\r
624 me_draw(menu, sel, draw_more);
\r
625 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|
\r
626 PBTN_MOK|PBTN_MBACK|PBTN_MENU|PBTN_L|PBTN_R, 70);
\r
627 if (inp & (PBTN_MENU|PBTN_MBACK))
\r
630 if (inp & PBTN_UP ) {
\r
634 sel = menu_sel_max;
\r
636 while (!menu[sel].enabled || !menu[sel].selectable);
\r
638 if (inp & PBTN_DOWN) {
\r
641 if (sel > menu_sel_max)
\r
644 while (!menu[sel].enabled || !menu[sel].selectable);
\r
647 /* a bit hacky but oh well */
\r
648 if ((inp & (PBTN_L|PBTN_R)) == (PBTN_L|PBTN_R))
\r
651 if (inp & (PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R)) { /* multi choice */
\r
652 if (me_process(&menu[sel], (inp & (PBTN_RIGHT|PBTN_R)) ? 1 : 0,
\r
653 inp & (PBTN_L|PBTN_R)))
\r
657 if (inp & (PBTN_MOK|PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R))
\r
659 /* require PBTN_MOK for MB_NONE */
\r
660 if (menu[sel].handler != NULL && (menu[sel].beh != MB_NONE || (inp & PBTN_MOK))) {
\r
661 ret = menu[sel].handler(menu[sel].id, inp);
\r
663 menu_sel_max = me_count(menu) - 1; /* might change, so update */
\r
672 static int me_loop(menu_entry *menu, int *menu_sel)
\r
674 return me_loop_d(menu, menu_sel, NULL, NULL);
\r
677 /* ***************************************** */
\r
679 static void draw_menu_message(const char *msg, void (*draw_more)(void))
\r
681 int x, y, h, w, wt;
\r
685 for (h = 1, w = 0; *p != 0; h++) {
\r
686 for (wt = 0; *p != 0 && *p != '\n'; p++)
\r
696 x = g_menuscreen_w / 2 - w * me_mfont_w / 2;
\r
697 y = g_menuscreen_h / 2 - h * me_mfont_h / 2;
\r
701 menu_draw_begin(1);
\r
703 for (p = msg; *p != 0 && y <= g_menuscreen_h - me_mfont_h; y += me_mfont_h) {
\r
704 text_out16(x, y, p);
\r
706 for (; *p != 0 && *p != '\n'; p++)
\r
712 if (draw_more != NULL)
\r
718 // -------------- del confirm ---------------
\r
720 static void do_delete(const char *fpath, const char *fname)
\r
726 menu_draw_begin(1);
\r
728 len = strlen(fname);
\r
729 if (len > g_menuscreen_w / me_sfont_w)
\r
730 len = g_menuscreen_w / me_sfont_w;
\r
732 mid = g_menuscreen_w / 2;
\r
733 text_out16(mid - me_mfont_w * 15 / 2, 8 * me_mfont_h, "About to delete");
\r
734 smalltext_out16(mid - len * me_sfont_w / 2, 9 * me_mfont_h + 5, fname, 0xbdff);
\r
735 text_out16(mid - me_mfont_w * 13 / 2, 11 * me_mfont_h, "Are you sure?");
\r
737 nm = in_get_key_name(-1, -PBTN_MA3);
\r
738 snprintf(tmp, sizeof(tmp), "(%s - confirm, ", nm);
\r
740 nm = in_get_key_name(-1, -PBTN_MBACK);
\r
741 snprintf(tmp + len, sizeof(tmp) - len, "%s - cancel)", nm);
\r
744 text_out16(mid - me_mfont_w * len / 2, 12 * me_mfont_h, tmp);
\r
747 while (in_menu_wait_any(50) & (PBTN_MENU|PBTN_MA2));
\r
748 inp = in_menu_wait(PBTN_MA3|PBTN_MBACK, 100);
\r
749 if (inp & PBTN_MA3)
\r
753 // -------------- ROM selector --------------
\r
755 static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)
\r
757 int max_cnt, start, i, x, pos;
\r
760 max_cnt = g_menuscreen_h / me_sfont_h;
\r
761 start = max_cnt / 2 - sel;
\r
762 n--; // exclude current dir (".")
\r
764 menu_draw_begin(1);
\r
766 // if (!rom_loaded)
\r
767 // menu_darken_bg(gp2x_screen, 320*240, 0);
\r
769 darken_ptr = (short *)g_menuscreen_ptr + g_menuscreen_w * max_cnt/2 * me_sfont_h;
\r
770 menu_darken_bg(darken_ptr, darken_ptr, g_menuscreen_w * me_sfont_h * 8 / 10, 0);
\r
772 x = 5 + me_mfont_w + 1;
\r
773 if (start - 2 >= 0)
\r
774 smalltext_out16(14, (start - 2) * me_sfont_h, curdir, 0xffff);
\r
775 for (i = 0; i < n; i++) {
\r
777 if (pos < 0) continue;
\r
778 if (pos >= max_cnt) break;
\r
779 if (namelist[i+1]->d_type == DT_DIR) {
\r
780 smalltext_out16(x, pos * me_sfont_h, "/", 0xfff6);
\r
781 smalltext_out16(x + me_sfont_w, pos * me_sfont_h, namelist[i+1]->d_name, 0xfff6);
\r
783 unsigned short color = fname2color(namelist[i+1]->d_name);
\r
784 smalltext_out16(x, pos * me_sfont_h, namelist[i+1]->d_name, color);
\r
787 smalltext_out16(5, max_cnt/2 * me_sfont_h, ">", 0xffff);
\r
791 static int scandir_cmp(const void *p1, const void *p2)
\r
793 const struct dirent **d1 = (const struct dirent **)p1;
\r
794 const struct dirent **d2 = (const struct dirent **)p2;
\r
795 if ((*d1)->d_type == (*d2)->d_type)
\r
796 return alphasort(d1, d2);
\r
797 if ((*d1)->d_type == DT_DIR)
\r
798 return -1; // put before
\r
799 if ((*d2)->d_type == DT_DIR)
\r
802 return alphasort(d1, d2);
\r
805 static int scandir_filter(const struct dirent *ent)
\r
810 if (ent == NULL || ent->d_name == NULL) return 0;
\r
811 if (strlen(ent->d_name) < 5) return 1;
\r
813 p = ent->d_name + strlen(ent->d_name) - 4;
\r
815 for (i = 0; i < array_size(filter_exts); i++)
\r
816 if (strcmp(p, filter_exts[i]) == 0)
\r
822 static char *menu_loop_romsel(char *curr_path, int len)
\r
824 struct dirent **namelist;
\r
825 int n, inp, sel = 0;
\r
826 char *ret = NULL, *fname = NULL;
\r
829 // is this a dir or a full path?
\r
830 if (!plat_is_dir(curr_path)) {
\r
831 char *p = curr_path + strlen(curr_path) - 1;
\r
832 for (; p > curr_path && *p != '/'; p--)
\r
838 n = scandir(curr_path, &namelist, scandir_filter, (void *)scandir_cmp);
\r
841 lprintf("menu_loop_romsel failed, dir: %s\n", curr_path);
\r
844 t = getcwd(curr_path, len);
\r
846 plat_get_root_dir(curr_path, len);
\r
847 n = scandir(curr_path, &namelist, scandir_filter, (void *)scandir_cmp);
\r
850 lprintf("menu_loop_romsel failed, dir: %s\n", curr_path);
\r
856 if (fname != NULL) {
\r
858 for (i = 1; i < n; i++) {
\r
859 if (strcmp(namelist[i]->d_name, fname) == 0) {
\r
866 /* make sure action buttons are not pressed on entering menu */
\r
867 draw_dirlist(curr_path, namelist, n, sel);
\r
868 while (in_menu_wait_any(50) & (PBTN_MOK|PBTN_MBACK|PBTN_MENU))
\r
873 draw_dirlist(curr_path, namelist, n, sel);
\r
874 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|
\r
875 PBTN_L|PBTN_R|PBTN_MA2|PBTN_MOK|PBTN_MBACK|PBTN_MENU, 33);
\r
876 if (inp & PBTN_UP ) { sel--; if (sel < 0) sel = n-2; }
\r
877 if (inp & PBTN_DOWN) { sel++; if (sel > n-2) sel = 0; }
\r
878 if (inp & PBTN_LEFT) { sel-=10; if (sel < 0) sel = 0; }
\r
879 if (inp & PBTN_L) { sel-=24; if (sel < 0) sel = 0; }
\r
880 if (inp & PBTN_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }
\r
881 if (inp & PBTN_R) { sel+=24; if (sel > n-2) sel = n-2; }
\r
882 if ((inp & PBTN_MOK) || (inp & (PBTN_MENU|PBTN_MA2)) == (PBTN_MENU|PBTN_MA2))
\r
885 if (namelist[sel+1]->d_type == DT_REG)
\r
887 strcpy(rom_fname_reload, curr_path);
\r
888 strcat(rom_fname_reload, "/");
\r
889 strcat(rom_fname_reload, namelist[sel+1]->d_name);
\r
890 if (inp & PBTN_MOK) { // return sel
\r
891 ret = rom_fname_reload;
\r
894 do_delete(rom_fname_reload, namelist[sel+1]->d_name);
\r
896 while (n--) free(namelist[n]);
\r
901 else if (namelist[sel+1]->d_type == DT_DIR)
\r
905 if (!(inp & PBTN_MOK))
\r
907 newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;
\r
908 newdir = malloc(newlen);
\r
909 if (newdir == NULL)
\r
911 if (strcmp(namelist[sel+1]->d_name, "..") == 0) {
\r
912 char *start = curr_path;
\r
913 p = start + strlen(start) - 1;
\r
914 while (*p == '/' && p > start) p--;
\r
915 while (*p != '/' && p > start) p--;
\r
916 if (p <= start) strcpy(newdir, "/");
\r
917 else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }
\r
919 strcpy(newdir, curr_path);
\r
920 p = newdir + strlen(newdir) - 1;
\r
921 while (*p == '/' && p >= newdir) *p-- = 0;
\r
922 strcat(newdir, "/");
\r
923 strcat(newdir, namelist[sel+1]->d_name);
\r
925 ret = menu_loop_romsel(newdir, newlen);
\r
931 // unknown file type, happens on NTFS mounts. Try to guess.
\r
932 FILE *tstf; int tmp;
\r
933 strcpy(rom_fname_reload, curr_path);
\r
934 strcat(rom_fname_reload, "/");
\r
935 strcat(rom_fname_reload, namelist[sel+1]->d_name);
\r
936 tstf = fopen(rom_fname_reload, "rb");
\r
939 if (fread(&tmp, 1, 1, tstf) > 0 || ferror(tstf) == 0)
\r
940 namelist[sel+1]->d_type = DT_REG;
\r
941 else namelist[sel+1]->d_type = DT_DIR;
\r
947 if (inp & PBTN_MBACK)
\r
952 while (n--) free(namelist[n]);
\r
956 // restore curr_path
\r
957 if (fname != NULL) {
\r
958 n = strlen(curr_path);
\r
959 if (curr_path + n + 1 == fname)
\r
960 curr_path[n] = '/';
\r
966 // ------------ savestate loader ------------
\r
968 static int state_slot_flags = 0;
\r
970 static void state_check_slots(void)
\r
974 state_slot_flags = 0;
\r
976 for (slot = 0; slot < 10; slot++) {
\r
977 if (emu_check_save_file(slot))
\r
978 state_slot_flags |= 1 << slot;
\r
982 static void draw_savestate_bg(int slot);
\r
984 static void draw_savestate_menu(int menu_sel, int is_loading)
\r
988 if (state_slot_flags & (1 << menu_sel))
\r
989 draw_savestate_bg(menu_sel);
\r
991 w = (13 + 2) * me_mfont_w;
\r
992 h = (1+2+10+1) * me_mfont_h;
\r
993 x = g_menuscreen_w / 2 - w / 2;
\r
995 y = g_menuscreen_h / 2 - h / 2;
\r
997 #ifdef MENU_ALIGN_LEFT
\r
998 if (x > 12 + me_mfont_w * 2)
\r
999 x = 12 + me_mfont_w * 2;
\r
1002 menu_draw_begin(1);
\r
1004 text_out16(x, y, is_loading ? "Load state" : "Save state");
\r
1005 y += 3 * me_mfont_h;
\r
1007 menu_draw_selection(x - me_mfont_w * 2, y + menu_sel * me_mfont_h, (13 + 2) * me_mfont_w + 4);
\r
1009 /* draw all 10 slots */
\r
1010 for (i = 0; i < 10; i++, y += me_mfont_h)
\r
1012 text_out16(x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");
\r
1014 text_out16(x, y, "back");
\r
1019 static int menu_loop_savestate(int is_loading)
\r
1021 static int menu_sel = 10;
\r
1022 int menu_sel_max = 10;
\r
1023 unsigned long inp = 0;
\r
1026 state_check_slots();
\r
1028 if (!(state_slot_flags & (1 << menu_sel)) && is_loading)
\r
1029 menu_sel = menu_sel_max;
\r
1033 draw_savestate_menu(menu_sel, is_loading);
\r
1034 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_MOK|PBTN_MBACK, 100);
\r
1035 if (inp & PBTN_UP) {
\r
1039 menu_sel = menu_sel_max;
\r
1040 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
\r
1042 if (inp & PBTN_DOWN) {
\r
1045 if (menu_sel > menu_sel_max)
\r
1047 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
\r
1049 if (inp & PBTN_MOK) { // save/load
\r
1050 if (menu_sel < 10) {
\r
1051 state_slot = menu_sel;
\r
1052 if (emu_save_load_game(is_loading, 0)) {
\r
1053 me_update_msg(is_loading ? "Load failed" : "Save failed");
\r
1061 if (inp & PBTN_MBACK)
\r
1068 // -------------- key config --------------
\r
1070 static char *action_binds(int player_idx, int action_mask, int dev_id)
\r
1072 int dev = 0, dev_last = IN_MAX_DEVS - 1;
\r
1073 int can_combo = 1, type;
\r
1075 static_buff[0] = 0;
\r
1077 type = IN_BINDTYPE_EMU;
\r
1078 if (player_idx >= 0) {
\r
1080 type = IN_BINDTYPE_PLAYER12;
\r
1082 if (player_idx == 1)
\r
1083 action_mask <<= 16;
\r
1086 dev = dev_last = dev_id;
\r
1088 for (; dev <= dev_last; dev++) {
\r
1089 int k, count = 0, combo = 0;
\r
1092 binds = in_get_dev_binds(dev);
\r
1093 if (binds == NULL)
\r
1096 in_get_config(dev, IN_CFG_BIND_COUNT, &count);
\r
1097 in_get_config(dev, IN_CFG_DOES_COMBOS, &combo);
\r
1098 combo = combo && can_combo;
\r
1100 for (k = 0; k < count; k++) {
\r
1101 const char *xname;
\r
1104 if (!(binds[IN_BIND_OFFS(k, type)] & action_mask))
\r
1107 xname = in_get_key_name(dev, k);
\r
1108 len = strlen(static_buff);
\r
1110 strncat(static_buff, combo ? " + " : ", ",
\r
1111 sizeof(static_buff) - len - 1);
\r
1112 len += combo ? 3 : 2;
\r
1114 strncat(static_buff, xname, sizeof(static_buff) - len - 1);
\r
1118 return static_buff;
\r
1121 static int count_bound_keys(int dev_id, int action_mask, int bindtype)
\r
1127 binds = in_get_dev_binds(dev_id);
\r
1128 if (binds == NULL)
\r
1131 in_get_config(dev_id, IN_CFG_BIND_COUNT, &count);
\r
1132 for (k = 0; k < count; k++)
\r
1134 if (binds[IN_BIND_OFFS(k, bindtype)] & action_mask)
\r
1141 static void draw_key_config(const me_bind_action *opts, int opt_cnt, int player_idx,
\r
1142 int sel, int dev_id, int dev_count, int is_bind)
\r
1144 char buff[64], buff2[32];
\r
1145 const char *dev_name;
\r
1148 w = ((player_idx >= 0) ? 20 : 30) * me_mfont_w;
\r
1149 x = g_menuscreen_w / 2 - w / 2;
\r
1150 y = (g_menuscreen_h - 4 * me_mfont_h) / 2 - (2 + opt_cnt) * me_mfont_h / 2;
\r
1151 if (x < me_mfont_w * 2)
\r
1152 x = me_mfont_w * 2;
\r
1154 menu_draw_begin(1);
\r
1155 if (player_idx >= 0)
\r
1156 text_out16(x, y, "Player %i controls", player_idx + 1);
\r
1158 text_out16(x, y, "Emulator controls");
\r
1160 y += 2 * me_mfont_h;
\r
1161 menu_draw_selection(x - me_mfont_w * 2, y + sel * me_mfont_h, w + 2 * me_mfont_w);
\r
1163 for (i = 0; i < opt_cnt; i++, y += me_mfont_h)
\r
1164 text_out16(x, y, "%s : %s", opts[i].name,
\r
1165 action_binds(player_idx, opts[i].mask, dev_id));
\r
1168 dev_name = "(all devices)";
\r
1170 dev_name = in_get_dev_name(dev_id, 1, 1);
\r
1171 w = strlen(dev_name) * me_mfont_w;
\r
1172 if (w < 30 * me_mfont_w)
\r
1173 w = 30 * me_mfont_w;
\r
1174 if (w > g_menuscreen_w)
\r
1175 w = g_menuscreen_w;
\r
1177 x = g_menuscreen_w / 2 - w / 2;
\r
1180 snprintf(buff2, sizeof(buff2), "%s", in_get_key_name(-1, -PBTN_MOK));
\r
1181 snprintf(buff, sizeof(buff), "%s - bind, %s - clear", buff2,
\r
1182 in_get_key_name(-1, -PBTN_MA2));
\r
1183 text_out16(x, g_menuscreen_h - 4 * me_mfont_h, buff);
\r
1186 text_out16(x, g_menuscreen_h - 4 * me_mfont_h, "Press a button to bind/unbind");
\r
1188 if (dev_count > 1) {
\r
1189 text_out16(x, g_menuscreen_h - 3 * me_mfont_h, dev_name);
\r
1190 text_out16(x, g_menuscreen_h - 2 * me_mfont_h, "Press left/right for other devs");
\r
1196 static void key_config_loop(const me_bind_action *opts, int opt_cnt, int player_idx)
\r
1198 int i, sel = 0, menu_sel_max = opt_cnt - 1, does_combos = 0;
\r
1199 int dev_id, bind_dev_id, dev_count, kc, is_down, mkey;
\r
1200 int unbind, bindtype, mask_shift;
\r
1202 for (i = 0, dev_id = -1, dev_count = 0; i < IN_MAX_DEVS; i++) {
\r
1203 if (in_get_dev_name(i, 1, 0) != NULL) {
\r
1210 if (dev_id == -1) {
\r
1211 lprintf("no devs, can't do config\n");
\r
1215 dev_id = -1; // show all
\r
1217 if (player_idx == 1)
\r
1219 bindtype = player_idx >= 0 ? IN_BINDTYPE_PLAYER12 : IN_BINDTYPE_EMU;
\r
1223 draw_key_config(opts, opt_cnt, player_idx, sel, dev_id, dev_count, 0);
\r
1224 mkey = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_MBACK|PBTN_MOK|PBTN_MA2, 100);
\r
1226 case PBTN_UP: sel--; if (sel < 0) sel = menu_sel_max; continue;
\r
1227 case PBTN_DOWN: sel++; if (sel > menu_sel_max) sel = 0; continue;
\r
1229 for (i = 0, dev_id--; i < IN_MAX_DEVS + 1; i++, dev_id--) {
\r
1231 dev_id = IN_MAX_DEVS - 1;
\r
1232 if (dev_id == -1 || in_get_dev_name(dev_id, 1, 0) != NULL)
\r
1237 for (i = 0, dev_id++; i < IN_MAX_DEVS; i++, dev_id++) {
\r
1238 if (dev_id >= IN_MAX_DEVS)
\r
1240 if (dev_id == -1 || in_get_dev_name(dev_id, 1, 0) != NULL)
\r
1247 if (sel >= opt_cnt)
\r
1249 while (in_menu_wait_any(30) & PBTN_MOK)
\r
1253 in_unbind_all(dev_id, opts[sel].mask << mask_shift, bindtype);
\r
1258 draw_key_config(opts, opt_cnt, player_idx, sel, dev_id, dev_count, 1);
\r
1260 /* wait for some up event */
\r
1261 for (is_down = 1; is_down; )
\r
1262 kc = in_update_keycode(&bind_dev_id, &is_down, -1);
\r
1264 i = count_bound_keys(bind_dev_id, opts[sel].mask << mask_shift, bindtype);
\r
1267 /* allow combos if device supports them */
\r
1268 in_get_config(bind_dev_id, IN_CFG_DOES_COMBOS, &does_combos);
\r
1269 if (i == 1 && bindtype == IN_BINDTYPE_EMU && does_combos)
\r
1273 in_unbind_all(bind_dev_id, opts[sel].mask << mask_shift, bindtype);
\r
1275 in_bind_key(bind_dev_id, kc, opts[sel].mask << mask_shift, bindtype, 0);
\r