1 // (c) Copyright 2006-2010 notaz, All rights reserved.
\r
2 // Free for non-commercial use.
\r
4 // For commercial use, separate licencing terms must be obtained.
\r
13 #include "readpng.h"
\r
14 #include "lprintf.h"
\r
19 static char static_buff[64];
\r
20 static int menu_error_time = 0;
\r
21 char menu_error_msg[64] = { 0, };
\r
22 void *g_menuscreen_ptr;
\r
23 void *g_menubg_src_ptr;
\r
26 #if !MSCREEN_SIZE_FIXED
\r
31 static unsigned char *menu_font_data = NULL;
\r
32 static int menu_text_color = 0xffff; // default to white
\r
33 static int menu_sel_color = -1; // disabled
\r
35 /* note: these might become non-constant in future */
\r
37 static const int me_mfont_w = 16, me_mfont_h = 20;
\r
38 static const int me_sfont_w = 12, me_sfont_h = 20;
\r
40 static const int me_mfont_w = 8, me_mfont_h = 10;
\r
41 static const int me_sfont_w = 6, me_sfont_h = 10;
\r
44 // draws text to current bbp16 screen
\r
45 static void text_out16_(int x, int y, const char *text, int color)
\r
47 int i, lh, tr, tg, tb, len;
\r
48 unsigned short *dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w;
\r
49 tr = (color & 0xf800) >> 8;
\r
50 tg = (color & 0x07e0) >> 3;
\r
51 tb = (color & 0x001f) << 3;
\r
53 if (text == (void *)1)
\r
62 for (p = text; *p != 0 && *p != '\n'; p++)
\r
68 if (y + lh > g_menuscreen_h)
\r
69 lh = g_menuscreen_h - y;
\r
71 for (i = 0; i < len; i++)
\r
73 unsigned char *src = menu_font_data + (unsigned int)text[i] * me_mfont_w * me_mfont_h / 2;
\r
74 unsigned short *dst = dest;
\r
77 for (l = 0; l < lh; l++, dst += g_menuscreen_w - me_mfont_w)
\r
79 for (u = me_mfont_w / 2; u > 0; u--, src++)
\r
83 r = (*dst & 0xf800) >> 8;
\r
84 g = (*dst & 0x07e0) >> 3;
\r
85 b = (*dst & 0x001f) << 3;
\r
86 r = (c^0xf)*r/15 + c*tr/15;
\r
87 g = (c^0xf)*g/15 + c*tg/15;
\r
88 b = (c^0xf)*b/15 + c*tb/15;
\r
89 *dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);
\r
91 r = (*dst & 0xf800) >> 8;
\r
92 g = (*dst & 0x07e0) >> 3;
\r
93 b = (*dst & 0x001f) << 3;
\r
94 r = (c^0xf)*r/15 + c*tr/15;
\r
95 g = (c^0xf)*g/15 + c*tg/15;
\r
96 b = (c^0xf)*b/15 + c*tb/15;
\r
97 *dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);
\r
100 dest += me_mfont_w;
\r
104 void text_out16(int x, int y, const char *texto, ...)
\r
108 int maxw = (g_menuscreen_w - x) / me_mfont_w;
\r
113 va_start(args, texto);
\r
114 vsnprintf(buffer, sizeof(buffer), texto, args);
\r
117 if (maxw > sizeof(buffer) - 1)
\r
118 maxw = sizeof(buffer) - 1;
\r
121 text_out16_(x,y,buffer,menu_text_color);
\r
124 /* draws in 6x8 font, might multiply size by integer */
\r
125 static void smalltext_out16_(int x, int y, const char *texto, int color)
\r
127 unsigned char *src;
\r
128 unsigned short *dst;
\r
129 int multiplier = me_sfont_w / 6;
\r
132 for (i = 0;; i++, x += me_sfont_w)
\r
134 unsigned char c = (unsigned char) texto[i];
\r
137 if (!c || c == '\n')
\r
140 src = fontdata6x8[c];
\r
141 dst = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w;
\r
146 for (h2 = multiplier; h2 > 0; h2--)
\r
148 for (m = 0x20; m; m >>= 1) {
\r
150 for (w2 = multiplier; w2 > 0; w2--)
\r
156 dst += g_menuscreen_w - me_sfont_w;
\r
163 static void smalltext_out16(int x, int y, const char *texto, int color)
\r
166 int maxw = (g_menuscreen_w - x) / me_sfont_w;
\r
171 strncpy(buffer, texto, sizeof(buffer));
\r
172 if (maxw > sizeof(buffer) - 1)
\r
173 maxw = sizeof(buffer) - 1;
\r
176 smalltext_out16_(x, y, buffer, color);
\r
179 static void menu_draw_selection(int x, int y, int w)
\r
182 unsigned short *dst, *dest;
\r
184 text_out16_(x, y, (void *)1, (menu_sel_color < 0) ? menu_text_color : menu_sel_color);
\r
186 if (menu_sel_color < 0) return; // no selection hilight
\r
189 dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w + me_mfont_w * 2 - 2;
\r
190 for (h = me_mfont_h + 1; h > 0; h--)
\r
193 for (i = w - (me_mfont_w * 2 - 2); i > 0; i--)
\r
194 *dst++ = menu_sel_color;
\r
195 dest += g_menuscreen_w;
\r
199 static int parse_hex_color(char *buff)
\r
202 int t = (int) strtoul(buff, &endp, 16);
\r
205 return ((t<<8)&0xf800) | ((t>>5)&0x07e0) | ((t>>19)&0x1f);
\r
207 return ((t>>8)&0xf800) | ((t>>5)&0x07e0) | ((t>>3)&0x1f);
\r
212 void menu_init(void)
\r
215 unsigned char *fd, *fds;
\r
219 if (menu_font_data != NULL)
\r
220 free(menu_font_data);
\r
222 menu_font_data = calloc((MENU_X2 ? 256 * 320 : 128 * 160) / 2, 1);
\r
223 if (menu_font_data == NULL)
\r
226 // generate default 8x10 font from fontdata8x8
\r
227 for (c = 0, fd = menu_font_data; c < 256; c++)
\r
229 for (l = 0; l < 8; l++)
\r
231 unsigned char fd8x8 = fontdata8x8[c*8+l];
\r
232 if (fd8x8&0x80) *fd = 0xf0;
\r
233 if (fd8x8&0x40) *fd |= 0x0f; fd++;
\r
234 if (fd8x8&0x20) *fd = 0xf0;
\r
235 if (fd8x8&0x10) *fd |= 0x0f; fd++;
\r
236 if (fd8x8&0x08) *fd = 0xf0;
\r
237 if (fd8x8&0x04) *fd |= 0x0f; fd++;
\r
238 if (fd8x8&0x02) *fd = 0xf0;
\r
239 if (fd8x8&0x01) *fd |= 0x0f; fd++;
\r
241 fd += 8*2/2; // 2 empty lines
\r
245 // expand default font
\r
246 fds = menu_font_data + 128 * 160 / 2 - 4;
\r
247 fd = menu_font_data + 256 * 320 / 2 - 1;
\r
248 for (c = 255; c >= 0; c--)
\r
250 for (l = 9; l >= 0; l--, fds -= 4)
\r
252 for (i = 3; i >= 0; i--) {
\r
253 int px = fds[i] & 0x0f;
\r
254 *fd-- = px | (px << 4);
\r
255 px = (fds[i] >> 4) & 0x0f;
\r
256 *fd-- = px | (px << 4);
\r
258 for (i = 3; i >= 0; i--) {
\r
259 int px = fds[i] & 0x0f;
\r
260 *fd-- = px | (px << 4);
\r
261 px = (fds[i] >> 4) & 0x0f;
\r
262 *fd-- = px | (px << 4);
\r
268 // load custom font and selector (stored as 1st symbol in font table)
\r
269 emu_make_path(buff, "skin/font.png", sizeof(buff));
\r
270 readpng(menu_font_data, buff, READPNG_FONT,
\r
271 MENU_X2 ? 256 : 128, MENU_X2 ? 320 : 160);
\r
272 // default selector symbol is '>'
\r
273 memcpy(menu_font_data, menu_font_data + ((int)'>') * me_mfont_w * me_mfont_h / 2,
\r
274 me_mfont_w * me_mfont_h / 2);
\r
275 emu_make_path(buff, "skin/selector.png", sizeof(buff));
\r
276 readpng(menu_font_data, buff, READPNG_SELECTOR, me_mfont_w, me_mfont_h);
\r
278 // load custom colors
\r
279 emu_make_path(buff, "skin/skin.txt", sizeof(buff));
\r
280 f = fopen(buff, "r");
\r
283 lprintf("found skin.txt\n");
\r
286 if (fgets(buff, sizeof(buff), f) == NULL)
\r
288 if (buff[0] == '#' || buff[0] == '/') continue; // comment
\r
289 if (buff[0] == '\r' || buff[0] == '\n') continue; // empty line
\r
290 if (strncmp(buff, "text_color=", 11) == 0)
\r
292 int tmp = parse_hex_color(buff+11);
\r
293 if (tmp >= 0) menu_text_color = tmp;
\r
294 else lprintf("skin.txt: parse error for text_color\n");
\r
296 else if (strncmp(buff, "selection_color=", 16) == 0)
\r
298 int tmp = parse_hex_color(buff+16);
\r
299 if (tmp >= 0) menu_sel_color = tmp;
\r
300 else lprintf("skin.txt: parse error for selection_color\n");
\r
303 lprintf("skin.txt: parse error: %s\n", buff);
\r
309 static void menu_draw_begin(int need_bg)
\r
311 plat_video_menu_begin();
\r
313 memcpy(g_menuscreen_ptr, g_menubg_ptr, g_menuscreen_w * g_menuscreen_h * 2);
\r
316 static void menu_draw_end(void)
\r
318 plat_video_menu_end();
\r
321 static void menu_darken_bg(void *dst, void *src, int pixels, int darker)
\r
323 unsigned int *dest = dst;
\r
324 unsigned int *sorc = src;
\r
330 unsigned int p = *sorc++;
\r
331 *dest++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);
\r
338 unsigned int p = *sorc++;
\r
339 *dest++ = (p&0xf79ef79e)>>1;
\r
344 static void menu_enter(int is_rom_loaded)
\r
348 // darken the active framebuffer
\r
349 menu_darken_bg(g_menubg_ptr, g_menubg_src_ptr, g_menuscreen_w * g_menuscreen_h, 1);
\r
355 // should really only happen once, on startup..
\r
356 emu_make_path(buff, "skin/background.png", sizeof(buff));
\r
357 if (readpng(g_menubg_ptr, buff, READPNG_BG, g_menuscreen_w, g_menuscreen_h) < 0)
\r
358 memset(g_menubg_ptr, 0, g_menuscreen_w * g_menuscreen_h * 2);
\r
361 plat_video_menu_enter(is_rom_loaded);
\r
364 static int me_id2offset(const menu_entry *ent, menu_id id)
\r
367 for (i = 0; ent->name; ent++, i++)
\r
368 if (ent->id == id) return i;
\r
370 lprintf("%s: id %i not found\n", __FUNCTION__, id);
\r
374 static void me_enable(menu_entry *entries, menu_id id, int enable)
\r
376 int i = me_id2offset(entries, id);
\r
377 entries[i].enabled = enable;
\r
380 static int me_count(const menu_entry *ent)
\r
384 for (ret = 0; ent->name; ent++, ret++)
\r
390 static void me_draw(const menu_entry *entries, int sel, void (*draw_more)(void))
\r
392 const menu_entry *ent, *ent_sel = entries;
\r
393 int x, y, w = 0, h = 0;
\r
394 int offs, col2_offs = 27 * me_mfont_w;
\r
399 /* calculate size of menu rect */
\r
400 for (ent = entries, i = n = 0; ent->name; ent++, i++)
\r
413 wt = strlen(ent->name) * me_mfont_w;
\r
414 if (wt == 0 && ent->generate_name)
\r
415 name = ent->generate_name(ent->id, &offs);
\r
417 wt = strlen(name) * me_mfont_w;
\r
419 if (ent->beh != MB_NONE)
\r
421 if (wt > col2_offs)
\r
422 col2_offs = wt + me_mfont_w;
\r
425 switch (ent->beh) {
\r
430 wt += me_mfont_w * 3;
\r
432 case MB_OPT_CUSTOM:
\r
433 case MB_OPT_CUSTONOFF:
\r
434 case MB_OPT_CUSTRANGE:
\r
437 if (ent->generate_name != NULL)
\r
438 name = ent->generate_name(ent->id, &offs);
\r
440 wt += (strlen(name) + offs) * me_mfont_w;
\r
443 wt += 10 * me_mfont_w;
\r
452 h = n * me_mfont_h;
\r
453 w += me_mfont_w * 2; /* selector */
\r
455 if (w > g_menuscreen_w) {
\r
456 lprintf("width %d > %d\n", w, g_menuscreen_w);
\r
457 w = g_menuscreen_w;
\r
459 if (h > g_menuscreen_h) {
\r
460 lprintf("height %d > %d\n", w, g_menuscreen_h);
\r
461 h = g_menuscreen_h;
\r
464 x = g_menuscreen_w / 2 - w / 2;
\r
465 y = g_menuscreen_h / 2 - h / 2;
\r
468 menu_draw_begin(1);
\r
469 menu_draw_selection(x, y + vi_sel_ln * me_mfont_h, w);
\r
470 x += me_mfont_w * 2;
\r
472 for (ent = entries; ent->name; ent++)
\r
474 const char **names;
\r
481 if (strlen(name) == 0) {
\r
482 if (ent->generate_name)
\r
483 name = ent->generate_name(ent->id, &offs);
\r
486 text_out16(x, y, name);
\r
488 switch (ent->beh) {
\r
492 text_out16(x + col2_offs, y, (*(int *)ent->var & ent->mask) ? "ON" : "OFF");
\r
495 text_out16(x + col2_offs, y, "%i", *(int *)ent->var);
\r
497 case MB_OPT_CUSTOM:
\r
498 case MB_OPT_CUSTONOFF:
\r
499 case MB_OPT_CUSTRANGE:
\r
502 if (ent->generate_name)
\r
503 name = ent->generate_name(ent->id, &offs);
\r
505 text_out16(x + col2_offs + offs * me_mfont_w, y, "%s", name);
\r
508 names = (const char **)ent->data;
\r
510 for (i = 0; names[i] != NULL; i++) {
\r
511 len = strlen(names[i]);
\r
513 offs = 10 - len - 2;
\r
514 if (i == *(int *)ent->var) {
\r
515 text_out16(x + col2_offs + offs * me_mfont_w, y, "%s", names[i]);
\r
525 /* display help or message if we have one */
\r
526 h = (g_menuscreen_h - h) / 2; // bottom area height
\r
527 if (menu_error_msg[0] != 0) {
\r
528 if (h >= me_mfont_h + 4)
\r
529 text_out16(5, g_menuscreen_h - me_mfont_h - 4, menu_error_msg);
\r
531 lprintf("menu msg doesn't fit!\n");
\r
533 if (plat_get_ticks_ms() - menu_error_time > 2048)
\r
534 menu_error_msg[0] = 0;
\r
536 else if (ent_sel->help != NULL) {
\r
537 const char *tmp = ent_sel->help;
\r
539 for (l = 0; tmp != NULL && *tmp != 0; l++)
\r
540 tmp = strchr(tmp + 1, '\n');
\r
541 if (h >= l * me_sfont_h + 4)
\r
542 for (tmp = ent_sel->help; l > 0; l--, tmp = strchr(tmp, '\n') + 1)
\r
543 smalltext_out16(5, g_menuscreen_h - (l * me_sfont_h + 4), tmp, 0xffff);
\r
546 if (draw_more != NULL)
\r
552 static int me_process(menu_entry *entry, int is_next, int is_lr)
\r
554 const char **names;
\r
556 switch (entry->beh)
\r
559 case MB_OPT_CUSTONOFF:
\r
560 *(int *)entry->var ^= entry->mask;
\r
563 case MB_OPT_CUSTRANGE:
\r
564 c = is_lr ? 10 : 1;
\r
565 *(int *)entry->var += is_next ? c : -c;
\r
566 if (*(int *)entry->var < (int)entry->min)
\r
567 *(int *)entry->var = (int)entry->max;
\r
568 if (*(int *)entry->var > (int)entry->max)
\r
569 *(int *)entry->var = (int)entry->min;
\r
572 names = (const char **)entry->data;
\r
573 for (c = 0; names[c] != NULL; c++)
\r
575 *(int *)entry->var += is_next ? 1 : -1;
\r
576 if (*(int *)entry->var < 0)
\r
577 *(int *)entry->var = 0;
\r
578 if (*(int *)entry->var >= c)
\r
579 *(int *)entry->var = c - 1;
\r
586 static void debug_menu_loop(void);
\r
588 static void me_loop(menu_entry *menu, int *menu_sel, void (*draw_more)(void))
\r
590 int ret, inp, sel = *menu_sel, menu_sel_max;
\r
592 menu_sel_max = me_count(menu) - 1;
\r
593 if (menu_sel_max < 0) {
\r
594 lprintf("no enabled menu entries\n");
\r
598 while ((!menu[sel].enabled || !menu[sel].selectable) && sel < menu_sel_max)
\r
601 /* make sure action buttons are not pressed on entering menu */
\r
602 me_draw(menu, sel, NULL);
\r
603 while (in_menu_wait_any(50) & (PBTN_MOK|PBTN_MBACK|PBTN_MENU));
\r
607 me_draw(menu, sel, draw_more);
\r
608 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|
\r
609 PBTN_MOK|PBTN_MBACK|PBTN_MENU|PBTN_L|PBTN_R, 70);
\r
610 if (inp & (PBTN_MENU|PBTN_MBACK))
\r
613 if (inp & PBTN_UP ) {
\r
617 sel = menu_sel_max;
\r
619 while (!menu[sel].enabled || !menu[sel].selectable);
\r
621 if (inp & PBTN_DOWN) {
\r
624 if (sel > menu_sel_max)
\r
627 while (!menu[sel].enabled || !menu[sel].selectable);
\r
630 /* a bit hacky but oh well */
\r
631 if ((inp & (PBTN_L|PBTN_R)) == (PBTN_L|PBTN_R))
\r
634 if (inp & (PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R)) { /* multi choice */
\r
635 if (me_process(&menu[sel], (inp & (PBTN_RIGHT|PBTN_R)) ? 1 : 0,
\r
636 inp & (PBTN_L|PBTN_R)))
\r
640 if (inp & (PBTN_MOK|PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R))
\r
642 /* require PBTN_MOK for MB_NONE */
\r
643 if (menu[sel].handler != NULL && (menu[sel].beh != MB_NONE || (inp & PBTN_MOK))) {
\r
644 ret = menu[sel].handler(menu[sel].id, inp);
\r
646 menu_sel_max = me_count(menu) - 1; /* might change, so update */
\r
653 /* ***************************************** */
\r
655 static void draw_menu_credits(void)
\r
657 const char *creds, *p;
\r
658 int x, y, h, w, wt;
\r
660 p = creds = plat_get_credits();
\r
662 for (h = 1, w = 0; *p != 0; h++) {
\r
663 for (wt = 0; *p != 0 && *p != '\n'; p++)
\r
673 x = g_menuscreen_w / 2 - w * me_mfont_w / 2;
\r
674 y = g_menuscreen_h / 2 - h * me_mfont_h / 2;
\r
678 menu_draw_begin(1);
\r
680 for (p = creds; *p != 0 && y <= g_menuscreen_h - me_mfont_h; y += me_mfont_h) {
\r
681 text_out16(x, y, p);
\r
683 for (; *p != 0 && *p != '\n'; p++)
\r
692 // -------------- del confirm ---------------
\r
694 static void do_delete(const char *fpath, const char *fname)
\r
700 menu_draw_begin(1);
\r
702 len = strlen(fname);
\r
703 if (len > g_menuscreen_w / me_sfont_w)
\r
704 len = g_menuscreen_w / me_sfont_w;
\r
706 mid = g_menuscreen_w / 2;
\r
707 text_out16(mid - me_mfont_w * 15 / 2, 8 * me_mfont_h, "About to delete");
\r
708 smalltext_out16(mid - len * me_sfont_w / 2, 9 * me_mfont_h + 5, fname, 0xbdff);
\r
709 text_out16(mid - me_mfont_w * 13 / 2, 11 * me_mfont_h, "Are you sure?");
\r
711 nm = in_get_key_name(-1, -PBTN_MA3);
\r
712 snprintf(tmp, sizeof(tmp), "(%s - confirm, ", nm);
\r
714 nm = in_get_key_name(-1, -PBTN_MBACK);
\r
715 snprintf(tmp + len, sizeof(tmp) - len, "%s - cancel)", nm);
\r
718 text_out16(mid - me_mfont_w * len / 2, 12 * me_mfont_h, tmp);
\r
721 while (in_menu_wait_any(50) & (PBTN_MENU|PBTN_MA2));
\r
722 inp = in_menu_wait(PBTN_MA3|PBTN_MBACK, 100);
\r
723 if (inp & PBTN_MA3)
\r
727 // -------------- ROM selector --------------
\r
729 // rrrr rggg gggb bbbb
\r
730 static unsigned short file2color(const char *fname)
\r
732 const char *ext = fname + strlen(fname) - 3;
\r
733 static const char *rom_exts[] = { "zip", "bin", "smd", "gen", "iso", "cso", "cue" };
\r
734 static const char *other_exts[] = { "gmv", "pat" };
\r
737 if (ext < fname) ext = fname;
\r
738 for (i = 0; i < array_size(rom_exts); i++)
\r
739 if (strcasecmp(ext, rom_exts[i]) == 0) return 0xbdff; // FIXME: mk defines
\r
740 for (i = 0; i < array_size(other_exts); i++)
\r
741 if (strcasecmp(ext, other_exts[i]) == 0) return 0xaff5;
\r
745 static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)
\r
747 int max_cnt, start, i, x, pos;
\r
750 max_cnt = g_menuscreen_h / me_sfont_h;
\r
751 start = max_cnt / 2 - sel;
\r
752 n--; // exclude current dir (".")
\r
754 menu_draw_begin(1);
\r
756 // if (!rom_loaded)
\r
757 // menu_darken_bg(gp2x_screen, 320*240, 0);
\r
759 darken_ptr = (short *)g_menuscreen_ptr + g_menuscreen_w * max_cnt/2 * me_sfont_h;
\r
760 menu_darken_bg(darken_ptr, darken_ptr, g_menuscreen_w * me_sfont_h * 8 / 10, 0);
\r
762 x = 5 + me_mfont_w + 1;
\r
763 if (start - 2 >= 0)
\r
764 smalltext_out16(14, (start - 2) * me_sfont_h, curdir, 0xffff);
\r
765 for (i = 0; i < n; i++) {
\r
767 if (pos < 0) continue;
\r
768 if (pos >= max_cnt) break;
\r
769 if (namelist[i+1]->d_type == DT_DIR) {
\r
770 smalltext_out16(x, pos * me_sfont_h, "/", 0xfff6);
\r
771 smalltext_out16(x + me_sfont_w, pos * me_sfont_h, namelist[i+1]->d_name, 0xfff6);
\r
773 unsigned short color = file2color(namelist[i+1]->d_name);
\r
774 smalltext_out16(x, pos * me_sfont_h, namelist[i+1]->d_name, color);
\r
777 smalltext_out16(5, max_cnt/2 * me_sfont_h, ">", 0xffff);
\r
781 static int scandir_cmp(const void *p1, const void *p2)
\r
783 const struct dirent **d1 = (const struct dirent **)p1;
\r
784 const struct dirent **d2 = (const struct dirent **)p2;
\r
785 if ((*d1)->d_type == (*d2)->d_type)
\r
786 return alphasort(d1, d2);
\r
787 if ((*d1)->d_type == DT_DIR)
\r
788 return -1; // put before
\r
789 if ((*d2)->d_type == DT_DIR)
\r
792 return alphasort(d1, d2);
\r
795 static const char *filter_exts[] = {
\r
796 ".mp3", ".MP3", ".srm", ".brm", "s.gz", ".mds", "bcfg", ".txt", ".htm", "html",
\r
800 static int scandir_filter(const struct dirent *ent)
\r
805 if (ent == NULL || ent->d_name == NULL) return 0;
\r
806 if (strlen(ent->d_name) < 5) return 1;
\r
808 p = ent->d_name + strlen(ent->d_name) - 4;
\r
810 for (i = 0; i < array_size(filter_exts); i++)
\r
811 if (strcmp(p, filter_exts[i]) == 0)
\r
817 static char *menu_loop_romsel(char *curr_path, int len)
\r
819 struct dirent **namelist;
\r
820 int n, inp, sel = 0;
\r
821 char *ret = NULL, *fname = NULL;
\r
824 // is this a dir or a full path?
\r
825 if (!plat_is_dir(curr_path)) {
\r
826 char *p = curr_path + strlen(curr_path) - 1;
\r
827 for (; p > curr_path && *p != '/'; p--)
\r
833 n = scandir(curr_path, &namelist, scandir_filter, (void *)scandir_cmp);
\r
836 lprintf("menu_loop_romsel failed, dir: %s\n", curr_path);
\r
839 t = getcwd(curr_path, len);
\r
841 plat_get_root_dir(curr_path, len);
\r
842 n = scandir(curr_path, &namelist, scandir_filter, (void *)scandir_cmp);
\r
845 lprintf("menu_loop_romsel failed, dir: %s\n", curr_path);
\r
851 if (fname != NULL) {
\r
853 for (i = 1; i < n; i++) {
\r
854 if (strcmp(namelist[i]->d_name, fname) == 0) {
\r
861 /* make sure action buttons are not pressed on entering menu */
\r
862 draw_dirlist(curr_path, namelist, n, sel);
\r
863 while (in_menu_wait_any(50) & (PBTN_MOK|PBTN_MBACK|PBTN_MENU))
\r
868 draw_dirlist(curr_path, namelist, n, sel);
\r
869 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|
\r
870 PBTN_L|PBTN_R|PBTN_MA2|PBTN_MOK|PBTN_MBACK|PBTN_MENU, 33);
\r
871 if (inp & PBTN_UP ) { sel--; if (sel < 0) sel = n-2; }
\r
872 if (inp & PBTN_DOWN) { sel++; if (sel > n-2) sel = 0; }
\r
873 if (inp & PBTN_LEFT) { sel-=10; if (sel < 0) sel = 0; }
\r
874 if (inp & PBTN_L) { sel-=24; if (sel < 0) sel = 0; }
\r
875 if (inp & PBTN_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }
\r
876 if (inp & PBTN_R) { sel+=24; if (sel > n-2) sel = n-2; }
\r
877 if ((inp & PBTN_MOK) || (inp & (PBTN_MENU|PBTN_MA2)) == (PBTN_MENU|PBTN_MA2))
\r
880 if (namelist[sel+1]->d_type == DT_REG)
\r
882 strcpy(rom_fname_reload, curr_path);
\r
883 strcat(rom_fname_reload, "/");
\r
884 strcat(rom_fname_reload, namelist[sel+1]->d_name);
\r
885 if (inp & PBTN_MOK) { // return sel
\r
886 ret = rom_fname_reload;
\r
889 do_delete(rom_fname_reload, namelist[sel+1]->d_name);
\r
891 while (n--) free(namelist[n]);
\r
896 else if (namelist[sel+1]->d_type == DT_DIR)
\r
900 if (!(inp & PBTN_MOK)) continue;
\r
901 newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;
\r
902 newdir = malloc(newlen);
\r
903 if (strcmp(namelist[sel+1]->d_name, "..") == 0) {
\r
904 char *start = curr_path;
\r
905 p = start + strlen(start) - 1;
\r
906 while (*p == '/' && p > start) p--;
\r
907 while (*p != '/' && p > start) p--;
\r
908 if (p <= start) strcpy(newdir, "/");
\r
909 else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }
\r
911 strcpy(newdir, curr_path);
\r
912 p = newdir + strlen(newdir) - 1;
\r
913 while (*p == '/' && p >= newdir) *p-- = 0;
\r
914 strcat(newdir, "/");
\r
915 strcat(newdir, namelist[sel+1]->d_name);
\r
917 ret = menu_loop_romsel(newdir, newlen);
\r
923 // unknown file type, happens on NTFS mounts. Try to guess.
\r
924 FILE *tstf; int tmp;
\r
925 strcpy(rom_fname_reload, curr_path);
\r
926 strcat(rom_fname_reload, "/");
\r
927 strcat(rom_fname_reload, namelist[sel+1]->d_name);
\r
928 tstf = fopen(rom_fname_reload, "rb");
\r
931 if (fread(&tmp, 1, 1, tstf) > 0 || ferror(tstf) == 0)
\r
932 namelist[sel+1]->d_type = DT_REG;
\r
933 else namelist[sel+1]->d_type = DT_DIR;
\r
939 if (inp & PBTN_MBACK)
\r
944 while (n--) free(namelist[n]);
\r
951 // ------------ savestate loader ------------
\r
953 static int state_slot_flags = 0;
\r
955 static void state_check_slots(void)
\r
959 state_slot_flags = 0;
\r
961 for (slot = 0; slot < 10; slot++) {
\r
962 if (emu_check_save_file(slot))
\r
963 state_slot_flags |= 1 << slot;
\r
967 static void draw_savestate_bg(int slot);
\r
969 static void draw_savestate_menu(int menu_sel, int is_loading)
\r
973 if (state_slot_flags & (1 << menu_sel))
\r
974 draw_savestate_bg(menu_sel);
\r
976 w = (13 + 2) * me_mfont_w;
\r
977 h = (1+2+10+1) * me_mfont_h;
\r
978 x = g_menuscreen_w / 2 - w / 2;
\r
980 y = g_menuscreen_h / 2 - h / 2;
\r
983 menu_draw_begin(1);
\r
985 text_out16(x, y, is_loading ? "Load state" : "Save state");
\r
986 y += 3 * me_mfont_h;
\r
988 menu_draw_selection(x - me_mfont_w * 2, y + menu_sel * me_mfont_h, (13 + 2) * me_mfont_w + 4);
\r
990 /* draw all 10 slots */
\r
991 for (i = 0; i < 10; i++, y += me_mfont_h)
\r
993 text_out16(x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");
\r
995 text_out16(x, y, "back");
\r
1000 static int menu_loop_savestate(int is_loading)
\r
1002 static int menu_sel = 10;
\r
1003 int menu_sel_max = 10;
\r
1004 unsigned long inp = 0;
\r
1007 state_check_slots();
\r
1009 if (!(state_slot_flags & (1 << menu_sel)) && is_loading)
\r
1010 menu_sel = menu_sel_max;
\r
1014 draw_savestate_menu(menu_sel, is_loading);
\r
1015 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_MOK|PBTN_MBACK, 100);
\r
1016 if (inp & PBTN_UP) {
\r
1020 menu_sel = menu_sel_max;
\r
1021 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
\r
1023 if (inp & PBTN_DOWN) {
\r
1026 if (menu_sel > menu_sel_max)
\r
1028 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
\r
1030 if (inp & PBTN_MOK) { // save/load
\r
1031 if (menu_sel < 10) {
\r
1032 state_slot = menu_sel;
\r
1033 if (emu_save_load_game(is_loading, 0)) {
\r
1034 me_update_msg(is_loading ? "Load failed" : "Save failed");
\r
1042 if (inp & PBTN_MBACK)
\r
1049 // -------------- key config --------------
\r
1051 static char *action_binds(int player_idx, int action_mask, int dev_id)
\r
1053 int k, count = 0, can_combo = 0, type;
\r
1056 static_buff[0] = 0;
\r
1058 binds = in_get_dev_binds(dev_id);
\r
1059 if (binds == NULL)
\r
1060 return static_buff;
\r
1062 in_get_config(dev_id, IN_CFG_BIND_COUNT, &count);
\r
1063 in_get_config(dev_id, IN_CFG_DOES_COMBOS, &can_combo);
\r
1065 type = IN_BINDTYPE_EMU;
\r
1066 if (player_idx >= 0) {
\r
1068 type = IN_BINDTYPE_PLAYER12;
\r
1070 if (player_idx == 1)
\r
1071 action_mask <<= 16;
\r
1073 for (k = 0; k < count; k++)
\r
1075 const char *xname;
\r
1078 if (!(binds[IN_BIND_OFFS(k, type)] & action_mask))
\r
1081 xname = in_get_key_name(dev_id, k);
\r
1082 len = strlen(static_buff);
\r
1084 strncat(static_buff, can_combo ? " + " : ", ",
\r
1085 sizeof(static_buff) - len - 1);
\r
1086 len += can_combo ? 3 : 2;
\r
1088 strncat(static_buff, xname, sizeof(static_buff) - len - 1);
\r
1091 return static_buff;
\r
1094 static int count_bound_keys(int dev_id, int action_mask, int bindtype)
\r
1100 binds = in_get_dev_binds(dev_id);
\r
1101 if (binds == NULL)
\r
1104 in_get_config(dev_id, IN_CFG_BIND_COUNT, &count);
\r
1105 for (k = 0; k < count; k++)
\r
1107 if (binds[IN_BIND_OFFS(k, bindtype)] & action_mask)
\r
1114 static void draw_key_config(const me_bind_action *opts, int opt_cnt, int player_idx,
\r
1115 int sel, int dev_id, int dev_count, int is_bind)
\r
1117 char buff[64], buff2[32];
\r
1118 const char *dev_name;
\r
1121 w = ((player_idx >= 0) ? 20 : 30) * me_mfont_w;
\r
1122 x = g_menuscreen_w / 2 - w / 2;
\r
1123 y = (g_menuscreen_h - 4 * me_mfont_h) / 2 - (2 + opt_cnt) * me_mfont_h / 2;
\r
1124 if (x < me_mfont_w * 2)
\r
1125 x = me_mfont_w * 2;
\r
1127 menu_draw_begin(1);
\r
1128 if (player_idx >= 0)
\r
1129 text_out16(x, y, "Player %i controls", player_idx + 1);
\r
1131 text_out16(x, y, "Emulator controls");
\r
1133 y += 2 * me_mfont_h;
\r
1134 menu_draw_selection(x - me_mfont_w * 2, y + sel * me_mfont_h, w + 2 * me_mfont_w);
\r
1136 for (i = 0; i < opt_cnt; i++, y += me_mfont_h)
\r
1137 text_out16(x, y, "%s : %s", opts[i].name,
\r
1138 action_binds(player_idx, opts[i].mask, dev_id));
\r
1140 dev_name = in_get_dev_name(dev_id, 1, 1);
\r
1141 w = strlen(dev_name) * me_mfont_w;
\r
1142 if (w < 30 * me_mfont_w)
\r
1143 w = 30 * me_mfont_w;
\r
1144 if (w > g_menuscreen_w)
\r
1145 w = g_menuscreen_w;
\r
1147 x = g_menuscreen_w / 2 - w / 2;
\r
1150 snprintf(buff2, sizeof(buff2), "%s", in_get_key_name(-1, -PBTN_MOK));
\r
1151 snprintf(buff, sizeof(buff), "%s - bind, %s - clear", buff2,
\r
1152 in_get_key_name(-1, -PBTN_MA2));
\r
1153 text_out16(x, g_menuscreen_h - 4 * me_mfont_h, buff);
\r
1156 text_out16(x, g_menuscreen_h - 4 * me_mfont_h, "Press a button to bind/unbind");
\r
1158 if (dev_count > 1) {
\r
1159 text_out16(x, g_menuscreen_h - 3 * me_mfont_h, dev_name);
\r
1160 text_out16(x, g_menuscreen_h - 2 * me_mfont_h, "Press left/right for other devs");
\r
1166 static void key_config_loop(const me_bind_action *opts, int opt_cnt, int player_idx)
\r
1168 int i, sel = 0, menu_sel_max = opt_cnt - 1, does_combos = 0;
\r
1169 int dev_id, dev_count, kc, is_down, mkey;
\r
1170 int unbind, bindtype, mask_shift;
\r
1172 for (i = 0, dev_id = -1, dev_count = 0; i < IN_MAX_DEVS; i++) {
\r
1173 if (in_get_dev_name(i, 1, 0) != NULL) {
\r
1180 if (dev_id == -1) {
\r
1181 lprintf("no devs, can't do config\n");
\r
1186 if (player_idx == 1)
\r
1188 bindtype = player_idx >= 0 ? IN_BINDTYPE_PLAYER12 : IN_BINDTYPE_EMU;
\r
1192 draw_key_config(opts, opt_cnt, player_idx, sel, dev_id, dev_count, 0);
\r
1193 mkey = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_MBACK|PBTN_MOK|PBTN_MA2, 100);
\r
1195 case PBTN_UP: sel--; if (sel < 0) sel = menu_sel_max; continue;
\r
1196 case PBTN_DOWN: sel++; if (sel > menu_sel_max) sel = 0; continue;
\r
1198 for (i = 0, dev_id--; i < IN_MAX_DEVS; i++, dev_id--) {
\r
1200 dev_id = IN_MAX_DEVS - 1;
\r
1201 if (in_get_dev_name(dev_id, 1, 0) != NULL)
\r
1206 for (i = 0, dev_id++; i < IN_MAX_DEVS; i++, dev_id++) {
\r
1207 if (dev_id >= IN_MAX_DEVS)
\r
1209 if (in_get_dev_name(dev_id, 1, 0) != NULL)
\r
1216 if (sel >= opt_cnt)
\r
1218 while (in_menu_wait_any(30) & PBTN_MOK)
\r
1222 in_unbind_all(dev_id, opts[sel].mask << mask_shift, bindtype);
\r
1227 draw_key_config(opts, opt_cnt, player_idx, sel, dev_id, dev_count, 1);
\r
1229 /* wait for some up event */
\r
1230 for (is_down = 1; is_down; )
\r
1231 kc = in_update_keycode(&dev_id, &is_down, -1);
\r
1233 i = count_bound_keys(dev_id, opts[sel].mask << mask_shift, bindtype);
\r
1236 /* allow combos if device supports them */
\r
1237 in_get_config(dev_id, IN_CFG_DOES_COMBOS, &does_combos);
\r
1238 if (i == 1 && bindtype == IN_BINDTYPE_EMU && does_combos)
\r
1242 in_unbind_all(dev_id, opts[sel].mask << mask_shift, bindtype);
\r
1244 in_bind_key(dev_id, kc, opts[sel].mask << mask_shift, bindtype, 0);
\r