menu: support seeking the filelist with letter keys
[libpicofe.git] / common / menu.c
index 887e0cd..3acab62 100644 (file)
@@ -1,32 +1,39 @@
-// (c) Copyright 2006-2009 notaz, All rights reserved.\r
-// Free for non-commercial use.\r
-\r
-// For commercial use, separate licencing terms must be obtained.\r
+/*\r
+ * (C) GraÅžvydas "notaz" Ignotas, 2006-2011\r
+ *\r
+ * This work is licensed under the terms of any of these licenses\r
+ * (at your option):\r
+ *  - GNU GPL, version 2 or later.\r
+ *  - GNU LGPL, version 2.1 or later.\r
+ * See the COPYING file in the top-level directory.\r
+ */\r
 \r
 #include <stdio.h>\r
 #include <string.h>\r
 #include <stdlib.h>\r
 #include <stdarg.h>\r
+#include <time.h>\r
+#include <locale.h> // savestate date\r
 \r
 #include "menu.h"\r
 #include "fonts.h"\r
 #include "readpng.h"\r
 #include "lprintf.h"\r
 #include "input.h"\r
-#include "emu.h"\r
 #include "plat.h"\r
 #include "posix.h"\r
-#include <version.h>\r
-#include <revision.h>\r
-\r
-#include <pico/pico_int.h>\r
-#include <pico/patch.h>\r
 \r
 static char static_buff[64];\r
-char menu_error_msg[64] = { 0, };\r
 static int  menu_error_time = 0;\r
+char menu_error_msg[64] = { 0, };\r
+void *g_menuscreen_ptr;\r
+void *g_menubg_src_ptr;\r
+void *g_menubg_ptr;\r
 \r
-#ifndef UIQ3\r
+#if !MSCREEN_SIZE_FIXED\r
+int g_menuscreen_w;\r
+int g_menuscreen_h;\r
+#endif\r
 \r
 static unsigned char *menu_font_data = NULL;\r
 static int menu_text_color = 0xffff; // default to white\r
@@ -44,8 +51,8 @@ static const int me_sfont_w = 6, me_sfont_h = 10;
 // draws text to current bbp16 screen\r
 static void text_out16_(int x, int y, const char *text, int color)\r
 {\r
-       int i, l, u, tr, tg, tb, len;\r
-       unsigned short *dest = (unsigned short *)g_screen_ptr + x + y * g_screen_width;\r
+       int i, lh, tr, tg, tb, len;\r
+       unsigned short *dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w;\r
        tr = (color & 0xf800) >> 8;\r
        tg = (color & 0x07e0) >> 3;\r
        tb = (color & 0x001f) << 3;\r
@@ -64,11 +71,17 @@ static void text_out16_(int x, int y, const char *text, int color)
                len = p - text;\r
        }\r
 \r
+       lh = me_mfont_h;\r
+       if (y + lh > g_menuscreen_h)\r
+               lh = g_menuscreen_h - y;\r
+\r
        for (i = 0; i < len; i++)\r
        {\r
                unsigned char  *src = menu_font_data + (unsigned int)text[i] * me_mfont_w * me_mfont_h / 2;\r
                unsigned short *dst = dest;\r
-               for (l = 0; l < me_mfont_h; l++, dst += g_screen_width - me_mfont_w)\r
+               int u, l;\r
+\r
+               for (l = 0; l < lh; l++, dst += g_menuscreen_w - me_mfont_w)\r
                {\r
                        for (u = me_mfont_w / 2; u > 0; u--, src++)\r
                        {\r
@@ -99,7 +112,7 @@ void text_out16(int x, int y, const char *texto, ...)
 {\r
        va_list args;\r
        char    buffer[256];\r
-       int     maxw = (g_screen_width - x) / me_mfont_w;\r
+       int     maxw = (g_menuscreen_w - x) / me_mfont_w;\r
 \r
        if (maxw < 0)\r
                return;\r
@@ -132,7 +145,7 @@ static void smalltext_out16_(int x, int y, const char *texto, int color)
                        break;\r
 \r
                src = fontdata6x8[c];\r
-               dst = (unsigned short *)g_screen_ptr + x + y * g_screen_width;\r
+               dst = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w;\r
 \r
                while (h--)\r
                {\r
@@ -147,7 +160,7 @@ static void smalltext_out16_(int x, int y, const char *texto, int color)
                                                dst += multiplier;\r
                                }\r
 \r
-                               dst += g_screen_width - me_sfont_w;\r
+                               dst += g_menuscreen_w - me_sfont_w;\r
                        }\r
                        src++;\r
                }\r
@@ -157,7 +170,7 @@ static void smalltext_out16_(int x, int y, const char *texto, int color)
 static void smalltext_out16(int x, int y, const char *texto, int color)\r
 {\r
        char buffer[128];\r
-       int maxw = (g_screen_width - x) / 6;\r
+       int maxw = (g_menuscreen_w - x) / me_sfont_w;\r
 \r
        if (maxw < 0)\r
                return;\r
@@ -180,13 +193,13 @@ static void menu_draw_selection(int x, int y, int w)
        if (menu_sel_color < 0) return; // no selection hilight\r
 \r
        if (y > 0) y--;\r
-       dest = (unsigned short *)g_screen_ptr + x + y * g_screen_width + 14;\r
+       dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w + me_mfont_w * 2 - 2;\r
        for (h = me_mfont_h + 1; h > 0; h--)\r
        {\r
                dst = dest;\r
-               for (i = w - 14; i > 0; i--)\r
+               for (i = w - (me_mfont_w * 2 - 2); i > 0; i--)\r
                        *dst++ = menu_sel_color;\r
-               dest += g_screen_width;\r
+               dest += g_menuscreen_w;\r
        }\r
 }\r
 \r
@@ -203,6 +216,13 @@ static int parse_hex_color(char *buff)
        return -1;\r
 }\r
 \r
+static char tolower_simple(char c)\r
+{\r
+       if ('A' <= c && c <= 'Z')\r
+               c = c - 'A' + 'a';\r
+       return c;\r
+}\r
+\r
 void menu_init(void)\r
 {\r
        int i, c, l;\r
@@ -261,12 +281,13 @@ void menu_init(void)
 \r
        // load custom font and selector (stored as 1st symbol in font table)\r
        emu_make_path(buff, "skin/font.png", sizeof(buff));\r
-       readpng(menu_font_data, buff, READPNG_FONT);\r
+       readpng(menu_font_data, buff, READPNG_FONT,\r
+               MENU_X2 ? 256 : 128, MENU_X2 ? 320 : 160);\r
        // default selector symbol is '>'\r
        memcpy(menu_font_data, menu_font_data + ((int)'>') * me_mfont_w * me_mfont_h / 2,\r
                me_mfont_w * me_mfont_h / 2);\r
        emu_make_path(buff, "skin/selector.png", sizeof(buff));\r
-       readpng(menu_font_data, buff, READPNG_SELECTOR);\r
+       readpng(menu_font_data, buff, READPNG_SELECTOR, me_mfont_w, me_mfont_h);\r
 \r
        // load custom colors\r
        emu_make_path(buff, "skin/skin.txt", sizeof(buff));\r
@@ -276,7 +297,8 @@ void menu_init(void)
                lprintf("found skin.txt\n");\r
                while (!feof(f))\r
                {\r
-                       fgets(buff, sizeof(buff), f);\r
+                       if (fgets(buff, sizeof(buff), f) == NULL)\r
+                               break;\r
                        if (buff[0] == '#'  || buff[0] == '/')  continue; // comment\r
                        if (buff[0] == '\r' || buff[0] == '\n') continue; // empty line\r
                        if (strncmp(buff, "text_color=", 11) == 0)\r
@@ -296,8 +318,45 @@ void menu_init(void)
                }\r
                fclose(f);\r
        }\r
+\r
+       // use user's locale for savestate date display\r
+       setlocale(LC_TIME, "");\r
+}\r
+\r
+static void menu_draw_begin(int need_bg)\r
+{\r
+       plat_video_menu_begin();\r
+       if (need_bg)\r
+               memcpy(g_menuscreen_ptr, g_menubg_ptr, g_menuscreen_w * g_menuscreen_h * 2);\r
+}\r
+\r
+static void menu_draw_end(void)\r
+{\r
+       plat_video_menu_end();\r
 }\r
 \r
+static void menu_darken_bg(void *dst, void *src, int pixels, int darker)\r
+{\r
+       unsigned int *dest = dst;\r
+       unsigned int *sorc = src;\r
+       pixels /= 2;\r
+       if (darker)\r
+       {\r
+               while (pixels--)\r
+               {\r
+                       unsigned int p = *sorc++;\r
+                       *dest++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);\r
+               }\r
+       }\r
+       else\r
+       {\r
+               while (pixels--)\r
+               {\r
+                       unsigned int p = *sorc++;\r
+                       *dest++ = (p&0xf79ef79e)>>1;\r
+               }\r
+       }\r
+}\r
 \r
 static int me_id2offset(const menu_entry *ent, menu_id id)\r
 {\r
@@ -325,13 +384,35 @@ static int me_count(const menu_entry *ent)
        return ret;\r
 }\r
 \r
+static unsigned int me_read_onoff(const menu_entry *ent)\r
+{\r
+       // guess var size based on mask to avoid reading too much\r
+       if (ent->mask & 0xffff0000)\r
+               return *(unsigned int *)ent->var & ent->mask;\r
+       else if (ent->mask & 0xff00)\r
+               return *(unsigned short *)ent->var & ent->mask;\r
+       else\r
+               return *(unsigned char *)ent->var & ent->mask;\r
+}\r
+\r
+static void me_toggle_onoff(menu_entry *ent)\r
+{\r
+       // guess var size based on mask to avoid reading too much\r
+       if (ent->mask & 0xffff0000)\r
+               *(unsigned int *)ent->var ^= ent->mask;\r
+       else if (ent->mask & 0xff00)\r
+               *(unsigned short *)ent->var ^= ent->mask;\r
+       else\r
+               *(unsigned char *)ent->var ^= ent->mask;\r
+}\r
+\r
 static void me_draw(const menu_entry *entries, int sel, void (*draw_more)(void))\r
 {\r
-       const menu_entry *ent;\r
+       const menu_entry *ent, *ent_sel = entries;\r
        int x, y, w = 0, h = 0;\r
        int offs, col2_offs = 27 * me_mfont_w;\r
+       int vi_sel_ln = 0;\r
        const char *name;\r
-       int asel = 0;\r
        int i, n;\r
 \r
        /* calculate size of menu rect */\r
@@ -342,8 +423,10 @@ static void me_draw(const menu_entry *entries, int sel, void (*draw_more)(void))
                if (!ent->enabled)\r
                        continue;\r
 \r
-               if (i == sel)\r
-                       asel = n;\r
+               if (i == sel) {\r
+                       ent_sel = ent;\r
+                       vi_sel_ln = n;\r
+               }\r
 \r
                name = NULL;\r
                wt = strlen(ent->name) * me_mfont_w;\r
@@ -359,9 +442,12 @@ static void me_draw(const menu_entry *entries, int sel, void (*draw_more)(void))
                        wt = col2_offs;\r
 \r
                        switch (ent->beh) {\r
-                       case MB_NONE: break;\r
+                       case MB_NONE:\r
+                               break;\r
                        case MB_OPT_ONOFF:\r
-                       case MB_OPT_RANGE: wt += me_mfont_w * 3; break;\r
+                       case MB_OPT_RANGE:\r
+                               wt += me_mfont_w * 3;\r
+                               break;\r
                        case MB_OPT_CUSTOM:\r
                        case MB_OPT_CUSTONOFF:\r
                        case MB_OPT_CUSTRANGE:\r
@@ -372,6 +458,9 @@ static void me_draw(const menu_entry *entries, int sel, void (*draw_more)(void))
                                if (name != NULL)\r
                                        wt += (strlen(name) + offs) * me_mfont_w;\r
                                break;\r
+                       case MB_OPT_ENUM:\r
+                               wt += 10 * me_mfont_w;\r
+                               break;\r
                        }\r
                }\r
 \r
@@ -382,25 +471,31 @@ static void me_draw(const menu_entry *entries, int sel, void (*draw_more)(void))
        h = n * me_mfont_h;\r
        w += me_mfont_w * 2; /* selector */\r
 \r
-       if (w > g_screen_width) {\r
-               lprintf("width %d > %d\n", w, g_screen_width);\r
-               w = g_screen_width;\r
+       if (w > g_menuscreen_w) {\r
+               lprintf("width %d > %d\n", w, g_menuscreen_w);\r
+               w = g_menuscreen_w;\r
        }\r
-       if (h > g_screen_height) {\r
-               lprintf("height %d > %d\n", w, g_screen_height);\r
-               h = g_screen_height;\r
+       if (h > g_menuscreen_h) {\r
+               lprintf("height %d > %d\n", w, g_menuscreen_h);\r
+               h = g_menuscreen_h;\r
        }\r
 \r
-       x = g_screen_width  / 2 - w / 2;\r
-       y = g_screen_height / 2 - h / 2;\r
+       x = g_menuscreen_w / 2 - w / 2;\r
+       y = g_menuscreen_h / 2 - h / 2;\r
+#ifdef MENU_ALIGN_LEFT\r
+       if (x > 12) x = 12;\r
+#endif\r
 \r
        /* draw */\r
-       plat_video_menu_begin();\r
-       menu_draw_selection(x, y + asel * me_mfont_h, w);\r
+       menu_draw_begin(1);\r
+       menu_draw_selection(x, y + vi_sel_ln * me_mfont_h, w);\r
        x += me_mfont_w * 2;\r
 \r
        for (ent = entries; ent->name; ent++)\r
        {\r
+               const char **names;\r
+               int len, leftname_end = 0;\r
+\r
                if (!ent->enabled)\r
                        continue;\r
 \r
@@ -409,14 +504,16 @@ static void me_draw(const menu_entry *entries, int sel, void (*draw_more)(void))
                        if (ent->generate_name)\r
                                name = ent->generate_name(ent->id, &offs);\r
                }\r
-               if (name != NULL)\r
+               if (name != NULL) {\r
                        text_out16(x, y, name);\r
+                       leftname_end = x + (strlen(name) + 1) * me_mfont_w;\r
+               }\r
 \r
                switch (ent->beh) {\r
                case MB_NONE:\r
                        break;\r
                case MB_OPT_ONOFF:\r
-                       text_out16(x + col2_offs, y, (*(int *)ent->var & ent->mask) ? "ON" : "OFF");\r
+                       text_out16(x + col2_offs, y, me_read_onoff(ent) ? "ON" : "OFF");\r
                        break;\r
                case MB_OPT_RANGE:\r
                        text_out16(x + col2_offs, y, "%i", *(int *)ent->var);\r
@@ -431,36 +528,62 @@ static void me_draw(const menu_entry *entries, int sel, void (*draw_more)(void))
                        if (name != NULL)\r
                                text_out16(x + col2_offs + offs * me_mfont_w, y, "%s", name);\r
                        break;\r
+               case MB_OPT_ENUM:\r
+                       names = (const char **)ent->data;\r
+                       for (i = 0; names[i] != NULL; i++) {\r
+                               offs = x + col2_offs;\r
+                               len = strlen(names[i]);\r
+                               if (len > 10)\r
+                                       offs += (10 - len - 2) * me_mfont_w;\r
+                               if (offs < leftname_end)\r
+                                       offs = leftname_end;\r
+                               if (i == *(unsigned char *)ent->var) {\r
+                                       text_out16(offs, y, "%s", names[i]);\r
+                                       break;\r
+                               }\r
+                       }\r
+                       break;\r
                }\r
 \r
                y += me_mfont_h;\r
        }\r
 \r
-       /* display message if we have one */\r
+       /* display help or message if we have one */\r
+       h = (g_menuscreen_h - h) / 2; // bottom area height\r
        if (menu_error_msg[0] != 0) {\r
-               if (g_screen_height - h >= 2 * me_mfont_h)\r
-                       text_out16(5, g_screen_height - me_mfont_h - 4, menu_error_msg);\r
+               if (h >= me_mfont_h + 4)\r
+                       text_out16(5, g_menuscreen_h - me_mfont_h - 4, menu_error_msg);\r
                else\r
                        lprintf("menu msg doesn't fit!\n");\r
 \r
                if (plat_get_ticks_ms() - menu_error_time > 2048)\r
                        menu_error_msg[0] = 0;\r
        }\r
+       else if (ent_sel->help != NULL) {\r
+               const char *tmp = ent_sel->help;\r
+               int l;\r
+               for (l = 0; tmp != NULL && *tmp != 0; l++)\r
+                       tmp = strchr(tmp + 1, '\n');\r
+               if (h >= l * me_sfont_h + 4)\r
+                       for (tmp = ent_sel->help; l > 0; l--, tmp = strchr(tmp, '\n') + 1)\r
+                               smalltext_out16(5, g_menuscreen_h - (l * me_sfont_h + 4), tmp, 0xffff);\r
+       }\r
 \r
        if (draw_more != NULL)\r
                draw_more();\r
 \r
-       plat_video_menu_end();\r
+       menu_draw_end();\r
 }\r
 \r
 static int me_process(menu_entry *entry, int is_next, int is_lr)\r
 {\r
+       const char **names;\r
        int c;\r
        switch (entry->beh)\r
        {\r
                case MB_OPT_ONOFF:\r
                case MB_OPT_CUSTONOFF:\r
-                       *(int *)entry->var ^= entry->mask;\r
+                       me_toggle_onoff(entry);\r
                        return 1;\r
                case MB_OPT_RANGE:\r
                case MB_OPT_CUSTRANGE:\r
@@ -471,6 +594,16 @@ static int me_process(menu_entry *entry, int is_next, int is_lr)
                        if (*(int *)entry->var > (int)entry->max)\r
                                *(int *)entry->var = (int)entry->min;\r
                        return 1;\r
+               case MB_OPT_ENUM:\r
+                       names = (const char **)entry->data;\r
+                       for (c = 0; names[c] != NULL; c++)\r
+                               ;\r
+                       *(signed char *)entry->var += is_next ? 1 : -1;\r
+                       if (*(signed char *)entry->var < 0)\r
+                               *(signed char *)entry->var = 0;\r
+                       if (*(signed char *)entry->var >= c)\r
+                               *(signed char *)entry->var = c - 1;\r
+                       return 1;\r
                default:\r
                        return 0;\r
        }\r
@@ -478,14 +611,14 @@ static int me_process(menu_entry *entry, int is_next, int is_lr)
 \r
 static void debug_menu_loop(void);\r
 \r
-static void me_loop(menu_entry *menu, int *menu_sel, void (*draw_more)(void))\r
+static int me_loop_d(menu_entry *menu, int *menu_sel, void (*draw_prep)(void), void (*draw_more)(void))\r
 {\r
-       int ret, inp, sel = *menu_sel, menu_sel_max;\r
+       int ret = 0, inp, sel = *menu_sel, menu_sel_max;\r
 \r
        menu_sel_max = me_count(menu) - 1;\r
        if (menu_sel_max < 0) {\r
                lprintf("no enabled menu entries\n");\r
-               return;\r
+               return 0;\r
        }\r
 \r
        while ((!menu[sel].enabled || !menu[sel].selectable) && sel < menu_sel_max)\r
@@ -493,13 +626,16 @@ static void me_loop(menu_entry *menu, int *menu_sel, void (*draw_more)(void))
 \r
        /* make sure action buttons are not pressed on entering menu */\r
        me_draw(menu, sel, NULL);\r
-       while (in_menu_wait_any(50) & (PBTN_MOK|PBTN_MBACK|PBTN_MENU));\r
+       while (in_menu_wait_any(NULL, 50) & (PBTN_MOK|PBTN_MBACK|PBTN_MENU));\r
 \r
        for (;;)\r
        {\r
+               if (draw_prep != NULL)\r
+                       draw_prep();\r
+\r
                me_draw(menu, sel, draw_more);\r
                inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|\r
-                                       PBTN_MOK|PBTN_MBACK|PBTN_MENU|PBTN_L|PBTN_R, 70);\r
+                       PBTN_MOK|PBTN_MBACK|PBTN_MENU|PBTN_L|PBTN_R, NULL, 70);\r
                if (inp & (PBTN_MENU|PBTN_MBACK))\r
                        break;\r
 \r
@@ -530,30 +666,34 @@ static void me_loop(menu_entry *menu, int *menu_sel, void (*draw_more)(void))
                                continue;\r
                }\r
 \r
-               if (inp & (PBTN_MOK|PBTN_LEFT|PBTN_RIGHT))\r
+               if (inp & (PBTN_MOK|PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R))\r
                {\r
-                       if (menu[sel].handler != NULL) {\r
+                       /* require PBTN_MOK for MB_NONE */\r
+                       if (menu[sel].handler != NULL && (menu[sel].beh != MB_NONE || (inp & PBTN_MOK))) {\r
                                ret = menu[sel].handler(menu[sel].id, inp);\r
                                if (ret) break;\r
-                               menu_sel_max = me_count(menu) - 1; /* might change */\r
+                               menu_sel_max = me_count(menu) - 1; /* might change, so update */\r
                        }\r
                }\r
        }\r
        *menu_sel = sel;\r
+\r
+       return ret;\r
 }\r
 \r
-/* ***************************************** */\r
+static int me_loop(menu_entry *menu, int *menu_sel)\r
+{\r
+       return me_loop_d(menu, menu_sel, NULL, NULL);\r
+}\r
 \r
-/* platform specific options and handlers */\r
-#include "../gp2x/menu.c"\r
+/* ***************************************** */\r
 \r
-static void draw_menu_credits(void)\r
+static void draw_menu_message(const char *msg, void (*draw_more)(void))\r
 {\r
-       const char *creds, *p;\r
        int x, y, h, w, wt;\r
+       const char *p;\r
 \r
-       p = creds = plat_get_credits();\r
-\r
+       p = msg;\r
        for (h = 1, w = 0; *p != 0; h++) {\r
                for (wt = 0; *p != 0 && *p != '\n'; p++)\r
                        wt++;\r
@@ -565,14 +705,14 @@ static void draw_menu_credits(void)
                p++;\r
        }\r
 \r
-       x = g_screen_width  / 2 - w * me_mfont_w / 2;\r
-       y = g_screen_height / 2 - h * me_mfont_h / 2;\r
+       x = g_menuscreen_w / 2 - w * me_mfont_w / 2;\r
+       y = g_menuscreen_h / 2 - h * me_mfont_h / 2;\r
        if (x < 0) x = 0;\r
        if (y < 0) y = 0;\r
 \r
-       plat_video_menu_begin();\r
+       menu_draw_begin(1);\r
 \r
-       for (p = creds; *p != 0 && y <= g_screen_height - me_mfont_h; y += me_mfont_h) {\r
+       for (p = msg; *p != 0 && y <= g_menuscreen_h - me_mfont_h; y += me_mfont_h) {\r
                text_out16(x, y, p);\r
 \r
                for (; *p != 0 && *p != '\n'; p++)\r
@@ -581,75 +721,10 @@ static void draw_menu_credits(void)
                        p++;\r
        }\r
 \r
-       plat_video_menu_end();\r
-}\r
-\r
-// --------- loading ROM screen ----------\r
-\r
-static int cdload_called = 0;\r
-\r
-static void load_progress_cb(int percent)\r
-{\r
-       int ln, len = percent * g_screen_width / 100;\r
-       unsigned short *dst = (unsigned short *)g_screen_ptr + g_screen_width * 10 * 2;\r
-\r
-       if (len > g_screen_width)\r
-               len = g_screen_width;\r
-       for (ln = 10 - 2; ln > 0; ln--, dst += g_screen_width)\r
-               memset(dst, 0xff, len * 2);\r
-       plat_video_menu_end();\r
-}\r
-\r
-static void cdload_progress_cb(const char *fname, int percent)\r
-{\r
-       int ln, len = percent * g_screen_width / 100;\r
-       unsigned short *dst = (unsigned short *)g_screen_ptr + g_screen_width * 10 * 2;\r
-\r
-       memset(dst, 0xff, g_screen_width * (me_sfont_h - 2) * 2);\r
-\r
-       smalltext_out16(1, 3 * me_sfont_h, "Processing CD image / MP3s", 0xffff);\r
-       smalltext_out16(1, 4 * me_sfont_h, fname, 0xffff);\r
-       dst += g_screen_width * me_sfont_h * 3;\r
-\r
-       if (len > g_screen_width)\r
-               len = g_screen_width;\r
-       for (ln = (me_sfont_h - 2); ln > 0; ln--, dst += g_screen_width)\r
-               memset(dst, 0xff, len * 2);\r
-\r
-       plat_video_menu_end();\r
-       cdload_called = 1;\r
-}\r
-\r
-void menu_romload_prepare(const char *rom_name)\r
-{\r
-       const char *p = rom_name + strlen(rom_name);\r
-\r
-       while (p > rom_name && *p != '/')\r
-               p--;\r
-\r
-       /* fill both buffers, callbacks won't update in full */\r
-       plat_video_menu_begin();\r
-       smalltext_out16(1, 1, "Loading", 0xffff);\r
-       smalltext_out16(1, me_sfont_h, p, 0xffff);\r
-       plat_video_menu_end();\r
-\r
-       plat_video_menu_begin();\r
-       smalltext_out16(1, 1, "Loading", 0xffff);\r
-       smalltext_out16(1, me_sfont_h, p, 0xffff);\r
-       plat_video_menu_end();\r
+       if (draw_more != NULL)\r
+               draw_more();\r
 \r
-       PicoCartLoadProgressCB = load_progress_cb;\r
-       PicoCDLoadProgressCB = cdload_progress_cb;\r
-       cdload_called = 0;\r
-}\r
-\r
-void menu_romload_end(void)\r
-{\r
-       PicoCartLoadProgressCB = NULL;\r
-       PicoCDLoadProgressCB = NULL;\r
-       smalltext_out16(1, (cdload_called ? 6 : 3) * me_sfont_h,\r
-               "Starting emulation...", 0xffff);\r
-       plat_video_menu_end();\r
+       menu_draw_end();\r
 }\r
 \r
 // -------------- del confirm ---------------\r
@@ -660,16 +735,13 @@ static void do_delete(const char *fpath, const char *fname)
        const char *nm;\r
        char tmp[64];\r
 \r
-       plat_video_menu_begin();\r
-\r
-       if (!rom_loaded)\r
-               menu_darken_bg(g_screen_ptr, g_screen_width * g_screen_height, 0);\r
+       menu_draw_begin(1);\r
 \r
        len = strlen(fname);\r
-       if (len > g_screen_width/6)\r
-               len = g_screen_width/6;\r
+       if (len > g_menuscreen_w / me_sfont_w)\r
+               len = g_menuscreen_w / me_sfont_w;\r
 \r
-       mid = g_screen_width / 2;\r
+       mid = g_menuscreen_w / 2;\r
        text_out16(mid - me_mfont_w * 15 / 2,  8 * me_mfont_h, "About to delete");\r
        smalltext_out16(mid - len * me_sfont_w / 2, 9 * me_mfont_h + 5, fname, 0xbdff);\r
        text_out16(mid - me_mfont_w * 13 / 2, 11 * me_mfont_h, "Are you sure?");\r
@@ -682,46 +754,32 @@ static void do_delete(const char *fpath, const char *fname)
        len = strlen(tmp);\r
 \r
        text_out16(mid - me_mfont_w * len / 2, 12 * me_mfont_h, tmp);\r
-       plat_video_menu_end();\r
+       menu_draw_end();\r
 \r
-       while (in_menu_wait_any(50) & (PBTN_MENU|PBTN_MA2));\r
-       inp = in_menu_wait(PBTN_MA3|PBTN_MBACK, 100);\r
+       while (in_menu_wait_any(NULL, 50) & (PBTN_MENU|PBTN_MA2));\r
+       inp = in_menu_wait(PBTN_MA3|PBTN_MBACK, NULL, 100);\r
        if (inp & PBTN_MA3)\r
                remove(fpath);\r
 }\r
 \r
 // -------------- ROM selector --------------\r
 \r
-// rrrr rggg gggb bbbb\r
-static unsigned short file2color(const char *fname)\r
-{\r
-       const char *ext = fname + strlen(fname) - 3;\r
-       static const char *rom_exts[]   = { "zip", "bin", "smd", "gen", "iso", "cso", "cue" };\r
-       static const char *other_exts[] = { "gmv", "pat" };\r
-       int i;\r
-\r
-       if (ext < fname) ext = fname;\r
-       for (i = 0; i < array_size(rom_exts); i++)\r
-               if (strcasecmp(ext, rom_exts[i]) == 0) return 0xbdff; // FIXME: mk defines\r
-       for (i = 0; i < array_size(other_exts); i++)\r
-               if (strcasecmp(ext, other_exts[i]) == 0) return 0xaff5;\r
-       return 0xffff;\r
-}\r
-\r
 static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)\r
 {\r
        int max_cnt, start, i, x, pos;\r
+       void *darken_ptr;\r
 \r
-       max_cnt = g_screen_height / me_sfont_h;\r
+       max_cnt = g_menuscreen_h / me_sfont_h;\r
        start = max_cnt / 2 - sel;\r
        n--; // exclude current dir (".")\r
 \r
-       plat_video_menu_begin();\r
+       menu_draw_begin(1);\r
 \r
 //     if (!rom_loaded)\r
 //             menu_darken_bg(gp2x_screen, 320*240, 0);\r
 \r
-       menu_darken_bg((short *)g_screen_ptr + g_screen_width * max_cnt/2 * 10, g_screen_width * 8, 0);\r
+       darken_ptr = (short *)g_menuscreen_ptr + g_menuscreen_w * max_cnt/2 * me_sfont_h;\r
+       menu_darken_bg(darken_ptr, darken_ptr, g_menuscreen_w * me_sfont_h * 8 / 10, 0);\r
 \r
        x = 5 + me_mfont_w + 1;\r
        if (start - 2 >= 0)\r
@@ -734,28 +792,28 @@ static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)
                        smalltext_out16(x, pos * me_sfont_h, "/", 0xfff6);\r
                        smalltext_out16(x + me_sfont_w, pos * me_sfont_h, namelist[i+1]->d_name, 0xfff6);\r
                } else {\r
-                       unsigned short color = file2color(namelist[i+1]->d_name);\r
+                       unsigned short color = fname2color(namelist[i+1]->d_name);\r
                        smalltext_out16(x, pos * me_sfont_h, namelist[i+1]->d_name, color);\r
                }\r
        }\r
        smalltext_out16(5, max_cnt/2 * me_sfont_h, ">", 0xffff);\r
-       plat_video_menu_end();\r
+       menu_draw_end();\r
 }\r
 \r
 static int scandir_cmp(const void *p1, const void *p2)\r
 {\r
-       struct dirent **d1 = (struct dirent **)p1, **d2 = (struct dirent **)p2;\r
-       if ((*d1)->d_type == (*d2)->d_type) return alphasort(d1, d2);\r
-       if ((*d1)->d_type == DT_DIR) return -1; // put before\r
-       if ((*d2)->d_type == DT_DIR) return  1;\r
+       const struct dirent **d1 = (const struct dirent **)p1;\r
+       const struct dirent **d2 = (const struct dirent **)p2;\r
+       if ((*d1)->d_type == (*d2)->d_type)\r
+               return alphasort(d1, d2);\r
+       if ((*d1)->d_type == DT_DIR)\r
+               return -1; // put before\r
+       if ((*d2)->d_type == DT_DIR)\r
+               return  1;\r
+\r
        return alphasort(d1, d2);\r
 }\r
 \r
-static const char *filter_exts[] = {\r
-       ".mp3", ".MP3", ".srm", ".brm", "s.gz", ".mds", "bcfg", ".txt", ".htm", "html",\r
-       ".jpg", ".gpe"\r
-};\r
-\r
 static int scandir_filter(const struct dirent *ent)\r
 {\r
        const char *p;\r
@@ -773,11 +831,28 @@ static int scandir_filter(const struct dirent *ent)
        return 1;\r
 }\r
 \r
+static int dirent_seek_char(struct dirent **namelist, int len, int sel, char c)\r
+{\r
+       int i;\r
+\r
+       sel++;\r
+       for (i = sel + 1; i != sel; i++) {\r
+               if (i >= len)\r
+                       i = 1;\r
+\r
+               if (tolower_simple(namelist[i]->d_name[0]) == c)\r
+                       break;\r
+       }\r
+\r
+       return i - 1;\r
+}\r
+\r
 static char *menu_loop_romsel(char *curr_path, int len)\r
 {\r
        struct dirent **namelist;\r
        int n, inp, sel = 0;\r
        char *ret = NULL, *fname = NULL;\r
+       char cinp;\r
 \r
 rescan:\r
        // is this a dir or a full path?\r
@@ -789,13 +864,16 @@ rescan:
                fname = p+1;\r
        }\r
 \r
-       n = scandir(curr_path, &namelist, scandir_filter, scandir_cmp);\r
+       n = scandir(curr_path, &namelist, scandir_filter, (void *)scandir_cmp);\r
        if (n < 0) {\r
+               char *t;\r
                lprintf("menu_loop_romsel failed, dir: %s\n", curr_path);\r
 \r
                // try root\r
-               getcwd(curr_path, len);\r
-               n = scandir(curr_path, &namelist, scandir_filter, scandir_cmp);\r
+               t = getcwd(curr_path, len);\r
+               if (t == NULL)\r
+                       plat_get_root_dir(curr_path, len);\r
+               n = scandir(curr_path, &namelist, scandir_filter, (void *)scandir_cmp);\r
                if (n < 0) {\r
                        // oops, we failed\r
                        lprintf("menu_loop_romsel failed, dir: %s\n", curr_path);\r
@@ -804,10 +882,12 @@ rescan:
        }\r
 \r
        // try to find sel\r
+       // note: we don't show '.' so sel is namelist index - 1\r
        if (fname != NULL) {\r
                int i;\r
                for (i = 1; i < n; i++) {\r
-                       if (strcmp(namelist[i]->d_name, fname) == 0) {\r
+                       char *dname = namelist[i]->d_name;\r
+                       if (dname[0] == fname[0] && strcmp(dname, fname) == 0) {\r
                                sel = i - 1;\r
                                break;\r
                        }\r
@@ -816,20 +896,21 @@ rescan:
 \r
        /* make sure action buttons are not pressed on entering menu */\r
        draw_dirlist(curr_path, namelist, n, sel);\r
-       while (in_menu_wait_any(50) & (PBTN_MOK|PBTN_MBACK|PBTN_MENU))\r
+       while (in_menu_wait_any(NULL, 50) & (PBTN_MOK|PBTN_MBACK|PBTN_MENU))\r
                ;\r
 \r
        for (;;)\r
        {\r
                draw_dirlist(curr_path, namelist, n, sel);\r
                inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|\r
-                       PBTN_L|PBTN_R|PBTN_MA2|PBTN_MOK|PBTN_MBACK|PBTN_MENU, 33);\r
+                       PBTN_L|PBTN_R|PBTN_MA2|PBTN_MOK|PBTN_MBACK|PBTN_MENU|PBTN_CHAR, &cinp, 33);\r
                if (inp & PBTN_UP  )  { sel--;   if (sel < 0)   sel = n-2; }\r
                if (inp & PBTN_DOWN)  { sel++;   if (sel > n-2) sel = 0; }\r
                if (inp & PBTN_LEFT)  { sel-=10; if (sel < 0)   sel = 0; }\r
                if (inp & PBTN_L)     { sel-=24; if (sel < 0)   sel = 0; }\r
                if (inp & PBTN_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }\r
                if (inp & PBTN_R)     { sel+=24; if (sel > n-2) sel = n-2; }\r
+               if (inp & PBTN_CHAR)  sel = dirent_seek_char(namelist, n, sel, cinp);\r
                if ((inp & PBTN_MOK) || (inp & (PBTN_MENU|PBTN_MA2)) == (PBTN_MENU|PBTN_MA2))\r
                {\r
                        again:\r
@@ -853,9 +934,12 @@ rescan:
                        {\r
                                int newlen;\r
                                char *p, *newdir;\r
-                               if (!(inp & PBTN_MOK)) continue;\r
+                               if (!(inp & PBTN_MOK))\r
+                                       continue;\r
                                newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;\r
                                newdir = malloc(newlen);\r
+                               if (newdir == NULL)\r
+                                       break;\r
                                if (strcmp(namelist[sel+1]->d_name, "..") == 0) {\r
                                        char *start = curr_path;\r
                                        p = start + strlen(start) - 1;\r
@@ -901,62 +985,22 @@ rescan:
                free(namelist);\r
        }\r
 \r
-       return ret;\r
-}\r
-\r
-// ------------ patch/gg menu ------------\r
-\r
-static void draw_patchlist(int sel)\r
-{\r
-       int max_cnt, start, i, pos, active;\r
-\r
-       max_cnt = g_screen_height / 10;\r
-       start = max_cnt / 2 - sel;\r
-\r
-       plat_video_menu_begin();\r
-\r
-       for (i = 0; i < PicoPatchCount; i++) {\r
-               pos = start + i;\r
-               if (pos < 0) continue;\r
-               if (pos >= max_cnt) break;\r
-               active = PicoPatches[i].active;\r
-               smalltext_out16(14,     pos * me_sfont_h, active ? "ON " : "OFF", active ? 0xfff6 : 0xffff);\r
-               smalltext_out16(14+6*4, pos * me_sfont_h, PicoPatches[i].name,    active ? 0xfff6 : 0xffff);\r
+       // restore curr_path\r
+       if (fname != NULL) {\r
+               n = strlen(curr_path);\r
+               if (curr_path + n + 1 == fname)\r
+                       curr_path[n] = '/';\r
        }\r
-       pos = start + i;\r
-       if (pos < max_cnt)\r
-               smalltext_out16(14, pos * me_sfont_h, "done", 0xffff);\r
 \r
-       text_out16(5, max_cnt / 2 * me_sfont_h, ">");\r
-       plat_video_menu_end();\r
-}\r
-\r
-static void menu_loop_patches(void)\r
-{\r
-       static int menu_sel = 0;\r
-       int inp;\r
-\r
-       for (;;)\r
-       {\r
-               draw_patchlist(menu_sel);\r
-               inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R|PBTN_MOK|PBTN_MBACK, 33);\r
-               if (inp & PBTN_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = PicoPatchCount; }\r
-               if (inp & PBTN_DOWN) { menu_sel++; if (menu_sel > PicoPatchCount) menu_sel = 0; }\r
-               if (inp &(PBTN_LEFT|PBTN_L))  { menu_sel-=10; if (menu_sel < 0) menu_sel = 0; }\r
-               if (inp &(PBTN_RIGHT|PBTN_R)) { menu_sel+=10; if (menu_sel > PicoPatchCount) menu_sel = PicoPatchCount; }\r
-               if (inp & PBTN_MOK) { // action\r
-                       if (menu_sel < PicoPatchCount)\r
-                               PicoPatches[menu_sel].active = !PicoPatches[menu_sel].active;\r
-                       else    break;\r
-               }\r
-               if (inp & PBTN_MBACK)\r
-                       break;\r
-       }\r
+       return ret;\r
 }\r
 \r
 // ------------ savestate loader ------------\r
 \r
+#define STATE_SLOT_COUNT 10\r
+\r
 static int state_slot_flags = 0;\r
+static int state_slot_times[STATE_SLOT_COUNT];\r
 \r
 static void state_check_slots(void)\r
 {\r
@@ -964,80 +1008,68 @@ static void state_check_slots(void)
 \r
        state_slot_flags = 0;\r
 \r
-       for (slot = 0; slot < 10; slot++) {\r
-               if (emu_check_save_file(slot))\r
+       for (slot = 0; slot < STATE_SLOT_COUNT; slot++) {\r
+               state_slot_times[slot] = 0;\r
+               if (emu_check_save_file(slot, &state_slot_times[slot]))\r
                        state_slot_flags |= 1 << slot;\r
        }\r
 }\r
 \r
-static void draw_savestate_bg(int slot)\r
-{\r
-       struct PicoVideo tmp_pv;\r
-       unsigned short tmp_cram[0x40];\r
-       unsigned short tmp_vsram[0x40];\r
-       void *tmp_vram;\r
-       const char *fname;\r
-\r
-       fname = emu_get_save_fname(1, 0, slot);\r
-       if (!fname) return;\r
-\r
-       tmp_vram = malloc(sizeof(Pico.vram));\r
-       if (tmp_vram == NULL) return;\r
-\r
-       memcpy(tmp_vram, Pico.vram, sizeof(Pico.vram));\r
-       memcpy(tmp_cram, Pico.cram, sizeof(Pico.cram));\r
-       memcpy(tmp_vsram, Pico.vsram, sizeof(Pico.vsram));\r
-       memcpy(&tmp_pv, &Pico.video, sizeof(Pico.video));\r
-\r
-       PicoStateLoadVDP(fname);\r
-\r
-       /* do a frame and fetch menu bg */\r
-       pemu_forced_frame(POPT_EN_SOFTSCALE);\r
-       plat_video_menu_enter(1);\r
-\r
-       memcpy(Pico.vram, tmp_vram, sizeof(Pico.vram));\r
-       memcpy(Pico.cram, tmp_cram, sizeof(Pico.cram));\r
-       memcpy(Pico.vsram, tmp_vsram, sizeof(Pico.vsram));\r
-       memcpy(&Pico.video, &tmp_pv,  sizeof(Pico.video));\r
-       free(tmp_vram);\r
-}\r
+static void draw_savestate_bg(int slot);\r
 \r
 static void draw_savestate_menu(int menu_sel, int is_loading)\r
 {\r
        int i, x, y, w, h;\r
+       char time_buf[32];\r
 \r
        if (state_slot_flags & (1 << menu_sel))\r
                draw_savestate_bg(menu_sel);\r
 \r
        w = (13 + 2) * me_mfont_w;\r
-       h = (1+2+10+1) * me_mfont_h;\r
-       x = g_screen_width / 2 - w / 2;\r
+       h = (1+2+STATE_SLOT_COUNT+1) * me_mfont_h;\r
+       x = g_menuscreen_w / 2 - w / 2;\r
        if (x < 0) x = 0;\r
-       y = g_screen_height / 2 - h / 2;\r
+       y = g_menuscreen_h / 2 - h / 2;\r
        if (y < 0) y = 0;\r
+#ifdef MENU_ALIGN_LEFT\r
+       if (x > 12 + me_mfont_w * 2)\r
+               x = 12 + me_mfont_w * 2;\r
+#endif\r
 \r
-       plat_video_menu_begin();\r
+       menu_draw_begin(1);\r
 \r
        text_out16(x, y, is_loading ? "Load state" : "Save state");\r
        y += 3 * me_mfont_h;\r
 \r
-       menu_draw_selection(x - me_mfont_w * 2, y + menu_sel * me_mfont_h, (13 + 2) * me_mfont_w + 4);\r
+       menu_draw_selection(x - me_mfont_w * 2, y + menu_sel * me_mfont_h, (23 + 2) * me_mfont_w + 4);\r
 \r
-       /* draw all 10 slots */\r
-       for (i = 0; i < 10; i++, y += me_mfont_h)\r
+       /* draw all slots */\r
+       for (i = 0; i < STATE_SLOT_COUNT; i++, y += me_mfont_h)\r
        {\r
-               text_out16(x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");\r
+               if (!(state_slot_flags & (1 << i)))\r
+                       strcpy(time_buf, "free");\r
+               else {\r
+                       strcpy(time_buf, "USED");\r
+                       if (state_slot_times[i] != 0) {\r
+                               time_t time = state_slot_times[i];\r
+                               struct tm *t = localtime(&time);\r
+                               strftime(time_buf, sizeof(time_buf), "%x %R", t);\r
+                       }\r
+               }\r
+\r
+               text_out16(x, y, "SLOT %i (%s)", i, time_buf);\r
        }\r
        text_out16(x, y, "back");\r
 \r
-       plat_video_menu_end();\r
+       menu_draw_end();\r
 }\r
 \r
 static int menu_loop_savestate(int is_loading)\r
 {\r
-       static int menu_sel = 10;\r
-       int menu_sel_max = 10;\r
+       static int menu_sel = STATE_SLOT_COUNT;\r
+       int menu_sel_max = STATE_SLOT_COUNT;\r
        unsigned long inp = 0;\r
+       int ret = 0;\r
 \r
        state_check_slots();\r
 \r
@@ -1047,7 +1079,7 @@ static int menu_loop_savestate(int is_loading)
        for (;;)\r
        {\r
                draw_savestate_menu(menu_sel, is_loading);\r
-               inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_MOK|PBTN_MBACK, 100);\r
+               inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_MOK|PBTN_MBACK, NULL, 100);\r
                if (inp & PBTN_UP) {\r
                        do {\r
                                menu_sel--;\r
@@ -1063,37 +1095,33 @@ static int menu_loop_savestate(int is_loading)
                        } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);\r
                }\r
                if (inp & PBTN_MOK) { // save/load\r
-                       if (menu_sel < 10) {\r
+                       if (menu_sel < STATE_SLOT_COUNT) {\r
                                state_slot = menu_sel;\r
                                if (emu_save_load_game(is_loading, 0)) {\r
                                        me_update_msg(is_loading ? "Load failed" : "Save failed");\r
-                                       return 0;\r
+                                       break;\r
                                }\r
-                               return 1;\r
+                               ret = 1;\r
+                               break;\r
                        }\r
-                       return 0;\r
+                       break;\r
                }\r
                if (inp & PBTN_MBACK)\r
-                       return 0;\r
+                       break;\r
        }\r
+\r
+       return ret;\r
 }\r
 \r
 // -------------- key config --------------\r
 \r
 static char *action_binds(int player_idx, int action_mask, int dev_id)\r
 {\r
-       int k, count, can_combo, type;\r
-       const int *binds;\r
+       int dev = 0, dev_last = IN_MAX_DEVS - 1;\r
+       int can_combo = 1, type;\r
 \r
        static_buff[0] = 0;\r
 \r
-       binds = in_get_dev_binds(dev_id);\r
-       if (binds == NULL)\r
-               return static_buff;\r
-\r
-       count = in_get_dev_info(dev_id, IN_INFO_BIND_COUNT);\r
-       can_combo = in_get_dev_info(dev_id, IN_INFO_DOES_COMBOS);\r
-\r
        type = IN_BINDTYPE_EMU;\r
        if (player_idx >= 0) {\r
                can_combo = 0;\r
@@ -1102,22 +1130,37 @@ static char *action_binds(int player_idx, int action_mask, int dev_id)
        if (player_idx == 1)\r
                action_mask <<= 16;\r
 \r
-       for (k = 0; k < count; k++)\r
-       {\r
-               const char *xname;\r
-               int len;\r
+       if (dev_id >= 0)\r
+               dev = dev_last = dev_id;\r
 \r
-               if (!(binds[IN_BIND_OFFS(k, type)] & action_mask))\r
+       for (; dev <= dev_last; dev++) {\r
+               int k, count = 0, combo = 0;\r
+               const int *binds;\r
+\r
+               binds = in_get_dev_binds(dev);\r
+               if (binds == NULL)\r
                        continue;\r
 \r
-               xname = in_get_key_name(dev_id, k);\r
-               len = strlen(static_buff);\r
-               if (len) {\r
-                       strncat(static_buff, can_combo ? " + " : ", ",\r
-                               sizeof(static_buff) - len - 1);\r
-                       len += can_combo ? 3 : 2;\r
+               in_get_config(dev, IN_CFG_BIND_COUNT, &count);\r
+               in_get_config(dev, IN_CFG_DOES_COMBOS, &combo);\r
+               combo = combo && can_combo;\r
+\r
+               for (k = 0; k < count; k++) {\r
+                       const char *xname;\r
+                       int len;\r
+\r
+                       if (!(binds[IN_BIND_OFFS(k, type)] & action_mask))\r
+                               continue;\r
+\r
+                       xname = in_get_key_name(dev, k);\r
+                       len = strlen(static_buff);\r
+                       if (len) {\r
+                               strncat(static_buff, combo ? " + " : ", ",\r
+                                       sizeof(static_buff) - len - 1);\r
+                               len += combo ? 3 : 2;\r
+                       }\r
+                       strncat(static_buff, xname, sizeof(static_buff) - len - 1);\r
                }\r
-               strncat(static_buff, xname, sizeof(static_buff) - len - 1);\r
        }\r
 \r
        return static_buff;\r
@@ -1127,13 +1170,13 @@ static int count_bound_keys(int dev_id, int action_mask, int bindtype)
 {\r
        const int *binds;\r
        int k, keys = 0;\r
-       int count;\r
+       int count = 0;\r
 \r
        binds = in_get_dev_binds(dev_id);\r
        if (binds == NULL)\r
                return 0;\r
 \r
-       count = in_get_dev_info(dev_id, IN_INFO_BIND_COUNT);\r
+       in_get_config(dev_id, IN_CFG_BIND_COUNT, &count);\r
        for (k = 0; k < count; k++)\r
        {\r
                if (binds[IN_BIND_OFFS(k, bindtype)] & action_mask)\r
@@ -1151,12 +1194,12 @@ static void draw_key_config(const me_bind_action *opts, int opt_cnt, int player_
        int x, y, w, i;\r
 \r
        w = ((player_idx >= 0) ? 20 : 30) * me_mfont_w;\r
-       x = g_screen_width / 2 - w / 2;\r
-       y = (g_screen_height - 4 * me_mfont_h) / 2 - (2 + opt_cnt) * me_mfont_h / 2;\r
+       x = g_menuscreen_w / 2 - w / 2;\r
+       y = (g_menuscreen_h - 4 * me_mfont_h) / 2 - (2 + opt_cnt) * me_mfont_h / 2;\r
        if (x < me_mfont_w * 2)\r
                x = me_mfont_w * 2;\r
 \r
-       plat_video_menu_begin();\r
+       menu_draw_begin(1);\r
        if (player_idx >= 0)\r
                text_out16(x, y, "Player %i controls", player_idx + 1);\r
        else\r
@@ -1169,36 +1212,39 @@ static void draw_key_config(const me_bind_action *opts, int opt_cnt, int player_
                text_out16(x, y, "%s : %s", opts[i].name,\r
                        action_binds(player_idx, opts[i].mask, dev_id));\r
 \r
-       dev_name = in_get_dev_name(dev_id, 1, 1);\r
+       if (dev_id < 0)\r
+               dev_name = "(all devices)";\r
+       else\r
+               dev_name = in_get_dev_name(dev_id, 1, 1);\r
        w = strlen(dev_name) * me_mfont_w;\r
        if (w < 30 * me_mfont_w)\r
                w = 30 * me_mfont_w;\r
-       if (w > g_screen_width)\r
-               w = g_screen_width;\r
+       if (w > g_menuscreen_w)\r
+               w = g_menuscreen_w;\r
 \r
-       x = g_screen_width / 2 - w / 2;\r
+       x = g_menuscreen_w / 2 - w / 2;\r
 \r
        if (!is_bind) {\r
                snprintf(buff2, sizeof(buff2), "%s", in_get_key_name(-1, -PBTN_MOK));\r
                snprintf(buff, sizeof(buff), "%s - bind, %s - clear", buff2,\r
                                in_get_key_name(-1, -PBTN_MA2));\r
-               text_out16(x, g_screen_height - 4 * me_mfont_h, buff);\r
+               text_out16(x, g_menuscreen_h - 4 * me_mfont_h, buff);\r
        }\r
        else\r
-               text_out16(x, g_screen_height - 4 * me_mfont_h, "Press a button to bind/unbind");\r
+               text_out16(x, g_menuscreen_h - 4 * me_mfont_h, "Press a button to bind/unbind");\r
 \r
        if (dev_count > 1) {\r
-               text_out16(x, g_screen_height - 3 * me_mfont_h, dev_name);\r
-               text_out16(x, g_screen_height - 2 * me_mfont_h, "Press left/right for other devs");\r
+               text_out16(x, g_menuscreen_h - 3 * me_mfont_h, dev_name);\r
+               text_out16(x, g_menuscreen_h - 2 * me_mfont_h, "Press left/right for other devs");\r
        }\r
 \r
-       plat_video_menu_end();\r
+       menu_draw_end();\r
 }\r
 \r
 static void key_config_loop(const me_bind_action *opts, int opt_cnt, int player_idx)\r
 {\r
-       int i, sel = 0, menu_sel_max = opt_cnt - 1;\r
-       int dev_id, dev_count, kc, is_down, mkey;\r
+       int i, sel = 0, menu_sel_max = opt_cnt - 1, does_combos = 0;\r
+       int dev_id, bind_dev_id, dev_count, kc, is_down, mkey;\r
        int unbind, bindtype, mask_shift;\r
 \r
        for (i = 0, dev_id = -1, dev_count = 0; i < IN_MAX_DEVS; i++) {\r
@@ -1214,6 +1260,7 @@ static void key_config_loop(const me_bind_action *opts, int opt_cnt, int player_
                return;\r
        }\r
 \r
+       dev_id = -1; // show all\r
        mask_shift = 0;\r
        if (player_idx == 1)\r
                mask_shift = 16;\r
@@ -1222,31 +1269,34 @@ static void key_config_loop(const me_bind_action *opts, int opt_cnt, int player_
        for (;;)\r
        {\r
                draw_key_config(opts, opt_cnt, player_idx, sel, dev_id, dev_count, 0);\r
-               mkey = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_MBACK|PBTN_MOK|PBTN_MA2, 100);\r
+               mkey = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT\r
+                               |PBTN_MBACK|PBTN_MOK|PBTN_MA2, NULL, 100);\r
                switch (mkey) {\r
                        case PBTN_UP:   sel--; if (sel < 0) sel = menu_sel_max; continue;\r
                        case PBTN_DOWN: sel++; if (sel > menu_sel_max) sel = 0; continue;\r
                        case PBTN_LEFT:\r
-                               for (i = 0, dev_id--; i < IN_MAX_DEVS; i++, dev_id--) {\r
-                                       if (dev_id < 0)\r
+                               for (i = 0, dev_id--; i < IN_MAX_DEVS + 1; i++, dev_id--) {\r
+                                       if (dev_id < -1)\r
                                                dev_id = IN_MAX_DEVS - 1;\r
-                                       if (in_get_dev_name(dev_id, 1, 0) != NULL)\r
+                                       if (dev_id == -1 || in_get_dev_name(dev_id, 1, 0) != NULL)\r
                                                break;\r
                                }\r
                                continue;\r
                        case PBTN_RIGHT:\r
                                for (i = 0, dev_id++; i < IN_MAX_DEVS; i++, dev_id++) {\r
                                        if (dev_id >= IN_MAX_DEVS)\r
-                                               dev_id = 0;\r
-                                       if (in_get_dev_name(dev_id, 1, 0) != NULL)\r
+                                               dev_id = -1;\r
+                                       if (dev_id == -1 || in_get_dev_name(dev_id, 1, 0) != NULL)\r
                                                break;\r
                                }\r
                                continue;\r
-                       case PBTN_MBACK: return;\r
+                       case PBTN_MBACK:\r
+                               return;\r
                        case PBTN_MOK:\r
                                if (sel >= opt_cnt)\r
                                        return;\r
-                               while (in_menu_wait_any(30) & PBTN_MOK);\r
+                               while (in_menu_wait_any(NULL, 30) & PBTN_MOK)\r
+                                       ;\r
                                break;\r
                        case PBTN_MA2:\r
                                in_unbind_all(dev_id, opts[sel].mask << mask_shift, bindtype);\r
@@ -1258,874 +1308,20 @@ static void key_config_loop(const me_bind_action *opts, int opt_cnt, int player_
 \r
                /* wait for some up event */\r
                for (is_down = 1; is_down; )\r
-                       kc = in_update_keycode(&dev_id, &is_down, -1);\r
+                       kc = in_update_keycode(&bind_dev_id, &is_down, NULL, -1);\r
 \r
-               i = count_bound_keys(dev_id, opts[sel].mask << mask_shift, bindtype);\r
+               i = count_bound_keys(bind_dev_id, opts[sel].mask << mask_shift, bindtype);\r
                unbind = (i > 0);\r
 \r
                /* allow combos if device supports them */\r
-               if (i == 1 && bindtype == IN_BINDTYPE_EMU &&\r
-                               in_get_dev_info(dev_id, IN_INFO_DOES_COMBOS))\r
+               in_get_config(bind_dev_id, IN_CFG_DOES_COMBOS, &does_combos);\r
+               if (i == 1 && bindtype == IN_BINDTYPE_EMU && does_combos)\r
                        unbind = 0;\r
 \r
-               in_bind_key(dev_id, kc, opts[sel].mask << mask_shift, bindtype, unbind);\r
-       }\r
-}\r
-\r
-// PicoPad[] format: MXYZ SACB RLDU\r
-me_bind_action me_ctrl_actions[15] =\r
-{\r
-       { "UP     ", 0x0001 },\r
-       { "DOWN   ", 0x0002 },\r
-       { "LEFT   ", 0x0004 },\r
-       { "RIGHT  ", 0x0008 },\r
-       { "A      ", 0x0040 },\r
-       { "B      ", 0x0010 },\r
-       { "C      ", 0x0020 },\r
-       { "A turbo", 0x4000 },\r
-       { "B turbo", 0x1000 },\r
-       { "C turbo", 0x2000 },\r
-       { "START  ", 0x0080 },\r
-       { "MODE   ", 0x0800 },\r
-       { "X      ", 0x0400 },\r
-       { "Y      ", 0x0200 },\r
-       { "Z      ", 0x0100 }\r
-};\r
-\r
-me_bind_action emuctrl_actions[] =\r
-{\r
-       { "Load State       ", PEV_STATE_LOAD },\r
-       { "Save State       ", PEV_STATE_SAVE },\r
-       { "Prev Save Slot   ", PEV_SSLOT_PREV },\r
-       { "Next Save Slot   ", PEV_SSLOT_NEXT },\r
-       { "Switch Renderer  ", PEV_SWITCH_RND },\r
-       { "Volume Down      ", PEV_VOL_DOWN },\r
-       { "Volume Up        ", PEV_VOL_UP },\r
-       { "Fast forward     ", PEV_FF },\r
-       { "Enter Menu       ", PEV_MENU },\r
-       { "Pico Next page   ", PEV_PICO_PNEXT },\r
-       { "Pico Prev page   ", PEV_PICO_PPREV },\r
-       { "Pico Switch input", PEV_PICO_SWINP },\r
-       { NULL,                0 }\r
-};\r
-\r
-static int key_config_loop_wrap(menu_id id, int keys)\r
-{\r
-       switch (id) {\r
-               case MA_CTRL_PLAYER1:\r
-                       key_config_loop(me_ctrl_actions, array_size(me_ctrl_actions), 0);\r
-                       break;\r
-               case MA_CTRL_PLAYER2:\r
-                       key_config_loop(me_ctrl_actions, array_size(me_ctrl_actions), 1);\r
-                       break;\r
-               case MA_CTRL_EMU:\r
-                       key_config_loop(emuctrl_actions, array_size(emuctrl_actions) - 1, -1);\r
-                       break;\r
-               default:\r
-                       break;\r
-       }\r
-       return 0;\r
-}\r
-\r
-static const char *mgn_dev_name(menu_id id, int *offs)\r
-{\r
-       const char *name = NULL;\r
-       static int it = 0;\r
-\r
-       if (id == MA_CTRL_DEV_FIRST)\r
-               it = 0;\r
-\r
-       for (; it < IN_MAX_DEVS; it++) {\r
-               name = in_get_dev_name(it, 1, 1);\r
-               if (name != NULL)\r
-                       break;\r
-       }\r
-\r
-       it++;\r
-       return name;\r
-}\r
-\r
-static int mh_saveloadcfg(menu_id id, int keys);\r
-static const char *mgn_savecfg(menu_id id, int *offs);\r
-\r
-static menu_entry e_menu_keyconfig[] =\r
-{\r
-       mee_handler_id("Player 1",          MA_CTRL_PLAYER1,    key_config_loop_wrap),\r
-       mee_handler_id("Player 2",          MA_CTRL_PLAYER2,    key_config_loop_wrap),\r
-       mee_handler_id("Emulator controls", MA_CTRL_EMU,        key_config_loop_wrap),\r
-       mee_onoff     ("6 button pad",      MA_OPT_6BUTTON_PAD, PicoOpt, POPT_6BTN_PAD),\r
-       mee_range     ("Turbo rate",        MA_CTRL_TURBO_RATE, currentConfig.turbo_rate, 1, 30),\r
-       mee_handler_mkname_id(MA_OPT_SAVECFG, mh_saveloadcfg, mgn_savecfg),\r
-       mee_handler_id("Save cfg for loaded game", MA_OPT_SAVECFG_GAME, mh_saveloadcfg),\r
-       mee_label     (""),\r
-       mee_label     ("Input devices:"),\r
-       mee_label_mk  (MA_CTRL_DEV_FIRST, mgn_dev_name),\r
-       mee_label_mk  (MA_CTRL_DEV_NEXT,  mgn_dev_name),\r
-       mee_label_mk  (MA_CTRL_DEV_NEXT,  mgn_dev_name),\r
-       mee_label_mk  (MA_CTRL_DEV_NEXT,  mgn_dev_name),\r
-       mee_label_mk  (MA_CTRL_DEV_NEXT,  mgn_dev_name),\r
-       mee_label_mk  (MA_CTRL_DEV_NEXT,  mgn_dev_name),\r
-       mee_label_mk  (MA_CTRL_DEV_NEXT,  mgn_dev_name),\r
-       mee_end,\r
-};\r
-\r
-static int menu_loop_keyconfig(menu_id id, int keys)\r
-{\r
-       static int sel = 0;\r
-\r
-       me_enable(e_menu_keyconfig, MA_OPT_SAVECFG_GAME, rom_loaded);\r
-       me_loop(e_menu_keyconfig, &sel, NULL);\r
-       return 0;\r
-}\r
-\r
-// ------------ SCD options menu ------------\r
+               if (unbind)\r
+                       in_unbind_all(bind_dev_id, opts[sel].mask << mask_shift, bindtype);\r
 \r
-static const char *mgn_cdopt_ra(menu_id id, int *offs)\r
-{\r
-       *offs = -5;\r
-       if (PicoCDBuffers <= 0)\r
-               return "     OFF";\r
-       sprintf(static_buff, "%5iK", PicoCDBuffers * 2);\r
-       return static_buff;\r
-}\r
-\r
-static int mh_cdopt_ra(menu_id id, int keys)\r
-{\r
-       if (keys & PBTN_LEFT) {\r
-               PicoCDBuffers >>= 1;\r
-               if (PicoCDBuffers < 2)\r
-                       PicoCDBuffers = 0;\r
-       } else {\r
-               if (PicoCDBuffers <= 0)\r
-                       PicoCDBuffers = 1;\r
-               PicoCDBuffers <<= 1;\r
-               if (PicoCDBuffers > 8*1024)\r
-                       PicoCDBuffers = 8*1024; // 16M\r
+               in_bind_key(bind_dev_id, kc, opts[sel].mask << mask_shift, bindtype, 0);\r
        }\r
-       return 0;\r
-}\r
-\r
-static menu_entry e_menu_cd_options[] =\r
-{\r
-       mee_onoff("CD LEDs",              MA_CDOPT_LEDS,          currentConfig.EmuOpt, EOPT_EN_CD_LEDS),\r
-       mee_onoff("CDDA audio",           MA_CDOPT_CDDA,          PicoOpt, POPT_EN_MCD_CDDA),\r
-       mee_onoff("PCM audio",            MA_CDOPT_PCM,           PicoOpt, POPT_EN_MCD_PCM),\r
-       mee_cust ("ReadAhead buffer",     MA_CDOPT_READAHEAD,     mh_cdopt_ra, mgn_cdopt_ra),\r
-       mee_onoff("SaveRAM cart",         MA_CDOPT_SAVERAM,       PicoOpt, POPT_EN_MCD_RAMCART),\r
-       mee_onoff("Scale/Rot. fx (slow)", MA_CDOPT_SCALEROT_CHIP, PicoOpt, POPT_EN_MCD_GFX),\r
-       mee_onoff("Better sync (slow)",   MA_CDOPT_BETTER_SYNC,   PicoOpt, POPT_EN_MCD_PSYNC),\r
-       mee_end,\r
-};\r
-\r
-static int menu_loop_cd_options(menu_id id, int keys)\r
-{\r
-       static int sel = 0;\r
-       me_loop(e_menu_cd_options, &sel, NULL);\r
-       return 0;\r
-}\r
-\r
-// ------------ 32X options menu ------------\r
-\r
-static menu_entry e_menu_32x_options[] =\r
-{\r
-       mee_onoff("32X enabled",          MA_32XOPT_ENABLE_32X,   PicoOpt, POPT_EN_32X),\r
-       mee_onoff("PWM sound",            MA_32XOPT_PWM,          PicoOpt, POPT_EN_PWM),\r
-};\r
-\r
-static int menu_loop_32x_options(menu_id id, int keys)\r
-{\r
-       static int sel = 0;\r
-       me_loop(e_menu_32x_options, &sel, NULL);\r
-       return 0;\r
-}\r
-\r
-// ------------ adv options menu ------------\r
-\r
-static menu_entry e_menu_adv_options[] =\r
-{\r
-       mee_onoff     ("SRAM/BRAM saves",          MA_OPT_SRAM_STATES,    currentConfig.EmuOpt, EOPT_EN_SRAM),\r
-       mee_onoff     ("Disable sprite limit",     MA_OPT2_NO_SPRITE_LIM, PicoOpt, POPT_DIS_SPRITE_LIM),\r
-       mee_onoff     ("Emulate Z80",              MA_OPT2_ENABLE_Z80,    PicoOpt, POPT_EN_Z80),\r
-       mee_onoff     ("Emulate YM2612 (FM)",      MA_OPT2_ENABLE_YM2612, PicoOpt, POPT_EN_FM),\r
-       mee_onoff     ("Emulate SN76496 (PSG)",    MA_OPT2_ENABLE_SN76496,PicoOpt, POPT_EN_PSG),\r
-       mee_onoff     ("gzip savestates",          MA_OPT2_GZIP_STATES,   currentConfig.EmuOpt, EOPT_GZIP_SAVES),\r
-       mee_onoff     ("Don't save last used ROM", MA_OPT2_NO_LAST_ROM,   currentConfig.EmuOpt, EOPT_NO_AUTOSVCFG),\r
-       mee_onoff     ("Disable idle loop patching",MA_OPT2_NO_IDLE_LOOPS,PicoOpt, POPT_DIS_IDLE_DET),\r
-       mee_onoff     ("Disable frame limiter",    MA_OPT2_NO_FRAME_LIMIT,currentConfig.EmuOpt, EOPT_NO_FRMLIMIT),\r
-       MENU_GP2X_OPTIONS_ADV\r
-       mee_end,\r
-};\r
-\r
-static int menu_loop_adv_options(menu_id id, int keys)\r
-{\r
-       static int sel = 0;\r
-       me_loop(e_menu_adv_options, &sel, NULL);\r
-       return 0;\r
-}\r
-\r
-// ------------ gfx options menu ------------\r
-\r
-static int mh_opt_render(menu_id id, int keys)\r
-{\r
-       plat_video_toggle_renderer((keys & PBTN_RIGHT) ? 1 : 0, 0, 1);\r
-       return 0;\r
-}\r
-\r
-static menu_entry e_menu_gfx_options[] =\r
-{\r
-       mee_cust      ("Renderer",                 MA_OPT_RENDERER,       mh_opt_render, mgn_opt_renderer),\r
-       MENU_GP2X_OPTIONS_GFX\r
-       mee_end,\r
-};\r
-\r
-static int menu_loop_gfx_options(menu_id id, int keys)\r
-{\r
-       static int sel = 0;\r
-       me_loop(e_menu_gfx_options, &sel, NULL);\r
-       return 0;\r
-}\r
-\r
-// ------------ options menu ------------\r
-\r
-static menu_entry e_menu_options[];\r
-\r
-static int sndrate_prevnext(int rate, int dir)\r
-{\r
-       static const int rates[] = { 8000, 11025, 16000, 22050, 44100 };\r
-       int i;\r
-\r
-       for (i = 0; i < 5; i++)\r
-               if (rates[i] == rate) break;\r
-\r
-       i += dir ? 1 : -1;\r
-       if (i > 4) {\r
-               if (!(PicoOpt & POPT_EN_STEREO)) {\r
-                       PicoOpt |= POPT_EN_STEREO;\r
-                       return rates[0];\r
-               }\r
-               return rates[4];\r
-       }\r
-       if (i < 0) {\r
-               if (PicoOpt & POPT_EN_STEREO) {\r
-                       PicoOpt &= ~POPT_EN_STEREO;\r
-                       return rates[4];\r
-               }\r
-               return rates[0];\r
-       }\r
-       return rates[i];\r
-}\r
-\r
-static void region_prevnext(int right)\r
-{\r
-       // jp_ntsc=1, jp_pal=2, usa=4, eu=8\r
-       static const int rgn_orders[] = { 0x148, 0x184, 0x814, 0x418, 0x841, 0x481 };\r
-       int i;\r
-\r
-       if (right) {\r
-               if (!PicoRegionOverride) {\r
-                       for (i = 0; i < 6; i++)\r
-                               if (rgn_orders[i] == PicoAutoRgnOrder) break;\r
-                       if (i < 5) PicoAutoRgnOrder = rgn_orders[i+1];\r
-                       else PicoRegionOverride=1;\r
-               }\r
-               else\r
-                       PicoRegionOverride <<= 1;\r
-               if (PicoRegionOverride > 8)\r
-                       PicoRegionOverride = 8;\r
-       } else {\r
-               if (!PicoRegionOverride) {\r
-                       for (i = 0; i < 6; i++)\r
-                               if (rgn_orders[i] == PicoAutoRgnOrder) break;\r
-                       if (i > 0) PicoAutoRgnOrder = rgn_orders[i-1];\r
-               }\r
-               else\r
-                       PicoRegionOverride >>= 1;\r
-       }\r
-}\r
-\r
-static int mh_opt_misc(menu_id id, int keys)\r
-{\r
-       int i;\r
-\r
-       switch (id) {\r
-       case MA_OPT_SOUND_QUALITY:\r
-               PsndRate = sndrate_prevnext(PsndRate, keys & PBTN_RIGHT);\r
-               break;\r
-       case MA_OPT_REGION:\r
-               region_prevnext(keys & PBTN_RIGHT);\r
-               break;\r
-       case MA_OPT_CONFIRM_STATES:\r
-               i = ((currentConfig.EmuOpt>>9)&1) | ((currentConfig.EmuOpt>>10)&2);\r
-               i += (keys & PBTN_LEFT) ? -1 : 1;\r
-               if (i < 0) i = 0; else if (i > 3) i = 3;\r
-               i |= i << 1; i &= ~2;\r
-               currentConfig.EmuOpt &= ~0xa00;\r
-               currentConfig.EmuOpt |= i << 9;\r
-               break;\r
-       default:\r
-               break;\r
-       }\r
-       return 0;\r
-}\r
-\r
-static int mh_saveloadcfg(menu_id id, int keys)\r
-{\r
-       int ret;\r
-\r
-       if (keys & (PBTN_LEFT|PBTN_RIGHT)) { // multi choice\r
-               config_slot += (keys & PBTN_LEFT) ? -1 : 1;\r
-               if (config_slot < 0) config_slot = 9;\r
-               else if (config_slot > 9) config_slot = 0;\r
-               me_enable(e_menu_options, MA_OPT_LOADCFG, config_slot != config_slot_current);\r
-               return 0;\r
-       }\r
-\r
-       switch (id) {\r
-       case MA_OPT_SAVECFG:\r
-       case MA_OPT_SAVECFG_GAME:\r
-               if (emu_write_config(id == MA_OPT_SAVECFG_GAME ? 1 : 0))\r
-                       me_update_msg("config saved");\r
-               else\r
-                       me_update_msg("failed to write config");\r
-               break;\r
-       case MA_OPT_LOADCFG:\r
-               ret = emu_read_config(1, 1);\r
-               if (!ret) ret = emu_read_config(0, 1);\r
-               if (ret)  me_update_msg("config loaded");\r
-               else      me_update_msg("failed to load config");\r
-               break;\r
-       default:\r
-               return 0;\r
-       }\r
-\r
-       return 1;\r
-}\r
-\r
-static int mh_restore_defaults(menu_id id, int keys)\r
-{\r
-       emu_set_defconfig();\r
-       me_update_msg("defaults restored");\r
-       return 1;\r
-}\r
-\r
-static const char *mgn_opt_fskip(menu_id id, int *offs)\r
-{\r
-       if (currentConfig.Frameskip < 0)\r
-               return "Auto";\r
-       sprintf(static_buff, "%d", currentConfig.Frameskip);\r
-       return static_buff;\r
-}\r
-\r
-static const char *mgn_opt_sound(menu_id id, int *offs)\r
-{\r
-       const char *str2;\r
-       *offs = -8;\r
-       str2 = (PicoOpt & POPT_EN_STEREO) ? "stereo" : "mono";\r
-       sprintf(static_buff, "%5iHz %s", PsndRate, str2);\r
-       return static_buff;\r
-}\r
-\r
-static const char *mgn_opt_region(menu_id id, int *offs)\r
-{\r
-       static const char *names[] = { "Auto", "      Japan NTSC", "      Japan PAL", "      USA", "      Europe" };\r
-       static const char *names_short[] = { "", " JP", " JP", " US", " EU" };\r
-       int code = PicoRegionOverride;\r
-       int u, i = 0;\r
-\r
-       *offs = -6;\r
-       if (code) {\r
-               code <<= 1;\r
-               while ((code >>= 1)) i++;\r
-               if (i > 4)\r
-                       return "unknown";\r
-               return names[i];\r
-       } else {\r
-               strcpy(static_buff, "Auto:");\r
-               for (u = 0; u < 3; u++) {\r
-                       code = (PicoAutoRgnOrder >> u*4) & 0xf;\r
-                       for (i = 0; code; code >>= 1, i++)\r
-                               ;\r
-                       strcat(static_buff, names_short[i]);\r
-               }\r
-               return static_buff;\r
-       }\r
-}\r
-\r
-static const char *mgn_opt_c_saves(menu_id id, int *offs)\r
-{\r
-       switch ((currentConfig.EmuOpt >> 9) & 5) {\r
-               default: return "OFF";\r
-               case 1:  return "writes";\r
-               case 4:  return "loads";\r
-               case 5:  return "both";\r
-       }\r
-}\r
-\r
-static const char *mgn_savecfg(menu_id id, int *offs)\r
-{\r
-       strcpy(static_buff, "Save global config");\r
-       if (config_slot != 0)\r
-               sprintf(static_buff + strlen(static_buff), " (profile: %i)", config_slot);\r
-       return static_buff;\r
-}\r
-\r
-static const char *mgn_loadcfg(menu_id id, int *offs)\r
-{\r
-       sprintf(static_buff, "Load cfg from profile %i", config_slot);\r
-       return static_buff;\r
-}\r
-\r
-static menu_entry e_menu_options[] =\r
-{\r
-       mee_range     ("Save slot",                MA_OPT_SAVE_SLOT,     state_slot, 0, 9),\r
-       mee_range_cust("Frameskip",                MA_OPT_FRAMESKIP,     currentConfig.Frameskip, -1, 16, mgn_opt_fskip),\r
-       mee_cust      ("Region",                   MA_OPT_REGION,        mh_opt_misc, mgn_opt_region),\r
-       mee_onoff     ("Show FPS",                 MA_OPT_SHOW_FPS,      currentConfig.EmuOpt, EOPT_SHOW_FPS),\r
-       mee_onoff     ("Enable sound",             MA_OPT_ENABLE_SOUND,  currentConfig.EmuOpt, EOPT_EN_SOUND),\r
-       mee_cust      ("Sound Quality",            MA_OPT_SOUND_QUALITY, mh_opt_misc, mgn_opt_sound),\r
-       mee_cust      ("Confirm savestate",        MA_OPT_CONFIRM_STATES,mh_opt_misc, mgn_opt_c_saves),\r
-       mee_range     (cpu_clk_name,               MA_OPT_CPU_CLOCKS,    currentConfig.CPUclock, 20, 900),\r
-       mee_handler   ("[Display options]",        menu_loop_gfx_options),\r
-       mee_handler   ("[Sega/Mega CD options]",   menu_loop_cd_options),\r
-       mee_handler   ("[32X options]",            menu_loop_32x_options),\r
-       mee_handler   ("[Advanced options]",       menu_loop_adv_options),\r
-       mee_handler_mkname_id(MA_OPT_SAVECFG, mh_saveloadcfg, mgn_savecfg),\r
-       mee_handler_id("Save cfg for current game only", MA_OPT_SAVECFG_GAME, mh_saveloadcfg),\r
-       mee_handler_mkname_id(MA_OPT_LOADCFG, mh_saveloadcfg, mgn_loadcfg),\r
-       mee_handler   ("Restore defaults",         mh_restore_defaults),\r
-       mee_end,\r
-};\r
-\r
-static int menu_loop_options(menu_id id, int keys)\r
-{\r
-       static int sel = 0;\r
-\r
-       me_enable(e_menu_options, MA_OPT_SAVECFG_GAME, rom_loaded);\r
-       me_enable(e_menu_options, MA_OPT_LOADCFG, config_slot != config_slot_current);\r
-\r
-       me_loop(e_menu_options, &sel, NULL);\r
-\r
-       return 0;\r
-}\r
-\r
-// ------------ debug menu ------------\r
-\r
-#include <pico/debug.h>\r
-\r
-extern void SekStepM68k(void);\r
-\r
-static void mplayer_loop(void)\r
-{\r
-       pemu_sound_start();\r
-\r
-       while (1)\r
-       {\r
-               PDebugZ80Frame();\r
-               if (in_menu_wait_any(0) & PBTN_MA3)\r
-                       break;\r
-               pemu_sound_wait();\r
-       }\r
-\r
-       pemu_sound_stop();\r
-}\r
-\r
-static void draw_text_debug(const char *str, int skip, int from)\r
-{\r
-       const char *p;\r
-       int line;\r
-\r
-       p = str;\r
-       while (skip-- > 0)\r
-       {\r
-               while (*p && *p != '\n')\r
-                       p++;\r
-               if (*p == 0 || p[1] == 0)\r
-                       return;\r
-               p++;\r
-       }\r
-\r
-       str = p;\r
-       for (line = from; line < g_screen_height / me_sfont_h; line++)\r
-       {\r
-               smalltext_out16(1, line * me_sfont_h, str, 0xffff);\r
-               while (*p && *p != '\n')\r
-                       p++;\r
-               if (*p == 0)\r
-                       break;\r
-               p++; str = p;\r
-       }\r
-}\r
-\r
-#ifdef __GNUC__\r
-#define COMPILER "gcc " __VERSION__\r
-#else\r
-#define COMPILER\r
-#endif\r
-\r
-static void draw_frame_debug(void)\r
-{\r
-       char layer_str[48] = "layers:                   ";\r
-       if (PicoDrawMask & PDRAW_LAYERB_ON)      memcpy(layer_str +  8, "B", 1);\r
-       if (PicoDrawMask & PDRAW_LAYERA_ON)      memcpy(layer_str + 10, "A", 1);\r
-       if (PicoDrawMask & PDRAW_SPRITES_LOW_ON) memcpy(layer_str + 12, "spr_lo", 6);\r
-       if (PicoDrawMask & PDRAW_SPRITES_HI_ON)  memcpy(layer_str + 19, "spr_hi", 6);\r
-       if (PicoDrawMask & PDRAW_32X_ON)         memcpy(layer_str + 26, "32x", 4);\r
-\r
-       memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);\r
-       pemu_forced_frame(0);\r
-       smalltext_out16(4, 1, "build: r" REVISION "  "__DATE__ " " __TIME__ " " COMPILER, 0xffff);\r
-       smalltext_out16(4, g_screen_height - me_sfont_h, layer_str, 0xffff);\r
-}\r
-\r
-static void debug_menu_loop(void)\r
-{\r
-       int inp, mode = 0;\r
-       int spr_offs = 0, dumped = 0;\r
-       char *tmp;\r
-\r
-       while (1)\r
-       {\r
-               switch (mode)\r
-               {\r
-                       case 0: plat_video_menu_begin();\r
-                               tmp = PDebugMain();\r
-                               plat_debug_cat(tmp);\r
-                               draw_text_debug(tmp, 0, 0);\r
-                               if (dumped) {\r
-                                       smalltext_out16(g_screen_width - 6 * me_sfont_h,\r
-                                               g_screen_height - me_mfont_h, "dumped", 0xffff);\r
-                                       dumped = 0;\r
-                               }\r
-                               break;\r
-                       case 1: draw_frame_debug(); break;\r
-                       case 2: memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);\r
-                               pemu_forced_frame(0);\r
-                               menu_darken_bg(g_screen_ptr, g_screen_width * g_screen_height, 0);\r
-                               PDebugShowSpriteStats((unsigned short *)g_screen_ptr + (g_screen_height/2 - 240/2)*g_screen_width +\r
-                                       g_screen_width/2 - 320/2, g_screen_width); break;\r
-                       case 3: memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);\r
-                               PDebugShowPalette(g_screen_ptr, g_screen_width);\r
-                               PDebugShowSprite((unsigned short *)g_screen_ptr + g_screen_width*120 + g_screen_width/2 + 16,\r
-                                       g_screen_width, spr_offs);\r
-                               draw_text_debug(PDebugSpriteList(), spr_offs, 6);\r
-                               break;\r
-                       case 4: plat_video_menu_begin();\r
-                               tmp = PDebug32x();\r
-                               draw_text_debug(tmp, 0, 0);\r
-                               break;\r
-               }\r
-               plat_video_menu_end();\r
-\r
-               inp = in_menu_wait(PBTN_MOK|PBTN_MBACK|PBTN_MA2|PBTN_MA3|PBTN_L|PBTN_R |\r
-                                       PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT, 70);\r
-               if (inp & PBTN_MBACK) return;\r
-               if (inp & PBTN_L) { mode--; if (mode < 0) mode = 4; }\r
-               if (inp & PBTN_R) { mode++; if (mode > 4) mode = 0; }\r
-               switch (mode)\r
-               {\r
-                       case 0:\r
-                               if (inp & PBTN_MOK)\r
-                                       PDebugCPUStep();\r
-                               if (inp & PBTN_MA3) {\r
-                                       while (inp & PBTN_MA3)\r
-                                               inp = in_menu_wait_any(-1);\r
-                                       mplayer_loop();\r
-                               }\r
-                               if ((inp & (PBTN_MA2|PBTN_LEFT)) == (PBTN_MA2|PBTN_LEFT)) {\r
-                                       mkdir("dumps", 0777);\r
-                                       PDebugDumpMem();\r
-                                       while (inp & PBTN_MA2) inp = in_menu_wait_any(-1);\r
-                                       dumped = 1;\r
-                               }\r
-                               break;\r
-                       case 1:\r
-                               if (inp & PBTN_LEFT)  PicoDrawMask ^= PDRAW_LAYERB_ON;\r
-                               if (inp & PBTN_RIGHT) PicoDrawMask ^= PDRAW_LAYERA_ON;\r
-                               if (inp & PBTN_DOWN)  PicoDrawMask ^= PDRAW_SPRITES_LOW_ON;\r
-                               if (inp & PBTN_UP)    PicoDrawMask ^= PDRAW_SPRITES_HI_ON;\r
-                               if (inp & PBTN_MA2)   PicoDrawMask ^= PDRAW_32X_ON;\r
-                               if (inp & PBTN_MOK) {\r
-                                       PsndOut = NULL; // just in case\r
-                                       PicoSkipFrame = 1;\r
-                                       PicoFrame();\r
-                                       PicoSkipFrame = 0;\r
-                                       while (inp & PBTN_MOK) inp = in_menu_wait_any(-1);\r
-                               }\r
-                               break;\r
-                       case 3:\r
-                               if (inp & PBTN_DOWN)  spr_offs++;\r
-                               if (inp & PBTN_UP)    spr_offs--;\r
-                               if (spr_offs < 0) spr_offs = 0;\r
-                               break;\r
-               }\r
-       }\r
-}\r
-\r
-// ------------ main menu ------------\r
-\r
-static char *romsel_run(void)\r
-{\r
-       char *ret, *sel_name;\r
-\r
-       sel_name = malloc(sizeof(rom_fname_loaded));\r
-       if (sel_name == NULL)\r
-               return NULL;\r
-       strcpy(sel_name, rom_fname_loaded);\r
-\r
-       ret = menu_loop_romsel(sel_name, sizeof(rom_fname_loaded));\r
-       free(sel_name);\r
-       return ret;\r
-}\r
-\r
-static int main_menu_handler(menu_id id, int keys)\r
-{\r
-       char *ret_name;\r
-\r
-       switch (id)\r
-       {\r
-       case MA_MAIN_RESUME_GAME:\r
-               if (rom_loaded)\r
-                       return 1;\r
-               break;\r
-       case MA_MAIN_SAVE_STATE:\r
-               if (rom_loaded)\r
-                       return menu_loop_savestate(0);\r
-               break;\r
-       case MA_MAIN_LOAD_STATE:\r
-               if (rom_loaded)\r
-                       return menu_loop_savestate(1);\r
-               break;\r
-       case MA_MAIN_RESET_GAME:\r
-               if (rom_loaded) {\r
-                       emu_reset_game();\r
-                       return 1;\r
-               }\r
-               break;\r
-       case MA_MAIN_LOAD_ROM:\r
-               ret_name = romsel_run();\r
-               if (ret_name != NULL) {\r
-                       lprintf("selected file: %s\n", ret_name);\r
-                       engineState = PGS_ReloadRom;\r
-                       return 1;\r
-               }\r
-               break;\r
-       case MA_MAIN_CREDITS:\r
-               draw_menu_credits();\r
-               in_menu_wait(PBTN_MOK|PBTN_MBACK, 70);\r
-               break;\r
-       case MA_MAIN_EXIT:\r
-               engineState = PGS_Quit;\r
-               return 1;\r
-       case MA_MAIN_PATCHES:\r
-               if (rom_loaded && PicoPatches) {\r
-                       menu_loop_patches();\r
-                       PicoPatchApply();\r
-                       me_update_msg("Patches applied");\r
-               }\r
-               break;\r
-       default:\r
-               lprintf("%s: something unknown selected\n", __FUNCTION__);\r
-               break;\r
-       }\r
-\r
-       return 0;\r
-}\r
-\r
-static menu_entry e_menu_main[] =\r
-{\r
-       mee_label     ("PicoDrive " VERSION),\r
-       mee_label     (""),\r
-       mee_label     (""),\r
-       mee_label     (""),\r
-       mee_handler_id("Resume game",        MA_MAIN_RESUME_GAME, main_menu_handler),\r
-       mee_handler_id("Save State",         MA_MAIN_SAVE_STATE,  main_menu_handler),\r
-       mee_handler_id("Load State",         MA_MAIN_LOAD_STATE,  main_menu_handler),\r
-       mee_handler_id("Reset game",         MA_MAIN_RESET_GAME,  main_menu_handler),\r
-       mee_handler_id("Load new ROM/ISO",   MA_MAIN_LOAD_ROM,    main_menu_handler),\r
-       mee_handler_id("Change options",     MA_MAIN_OPTIONS,     menu_loop_options),\r
-       mee_handler_id("Configure controls", MA_MAIN_OPTIONS,     menu_loop_keyconfig),\r
-       mee_handler_id("Credits",            MA_MAIN_CREDITS,     main_menu_handler),\r
-       mee_handler_id("Patches / GameGenie",MA_MAIN_PATCHES,     main_menu_handler),\r
-       mee_handler_id("Exit",               MA_MAIN_EXIT,        main_menu_handler),\r
-       mee_end,\r
-};\r
-\r
-void menu_loop(void)\r
-{\r
-       static int sel = 0;\r
-\r
-       me_enable(e_menu_main, MA_MAIN_RESUME_GAME, rom_loaded);\r
-       me_enable(e_menu_main, MA_MAIN_SAVE_STATE,  rom_loaded);\r
-       me_enable(e_menu_main, MA_MAIN_LOAD_STATE,  rom_loaded);\r
-       me_enable(e_menu_main, MA_MAIN_RESET_GAME,  rom_loaded);\r
-       me_enable(e_menu_main, MA_MAIN_PATCHES, PicoPatches != NULL);\r
-\r
-       plat_video_menu_enter(rom_loaded);\r
-       in_set_blocking(1);\r
-       me_loop(e_menu_main, &sel, menu_main_plat_draw);\r
-\r
-       if (rom_loaded) {\r
-               if (engineState == PGS_Menu)\r
-                       engineState = PGS_Running;\r
-               /* wait until menu, ok, back is released */\r
-               while (in_menu_wait_any(50) & (PBTN_MENU|PBTN_MOK|PBTN_MBACK))\r
-                       ;\r
-       }\r
-\r
-       in_set_blocking(0);\r
-}\r
-\r
-// --------- CD tray close menu ----------\r
-\r
-static int mh_tray_load_cd(menu_id id, int keys)\r
-{\r
-       char *ret_name;\r
-\r
-       ret_name = romsel_run();\r
-       if (ret_name == NULL)\r
-               return 0;\r
-\r
-       engineState = PGS_RestartRun;\r
-       return emu_swap_cd(ret_name);\r
-}\r
-\r
-static int mh_tray_nothing(menu_id id, int keys)\r
-{\r
-       return 1;\r
-}\r
-\r
-static menu_entry e_menu_tray[] =\r
-{\r
-       mee_label  ("The CD tray has opened."),\r
-       mee_label  (""),\r
-       mee_label  (""),\r
-       mee_handler("Load CD image",  mh_tray_load_cd),\r
-       mee_handler("Insert nothing", mh_tray_nothing),\r
-       mee_end,\r
-};\r
-\r
-int menu_loop_tray(void)\r
-{\r
-       int ret = 1, sel = 0;\r
-\r
-       plat_video_menu_enter(rom_loaded);\r
-\r
-       in_set_blocking(1);\r
-       me_loop(e_menu_tray, &sel, NULL);\r
-\r
-       if (engineState != PGS_RestartRun) {\r
-               engineState = PGS_RestartRun;\r
-               ret = 0; /* no CD inserted */\r
-       }\r
-\r
-       while (in_menu_wait_any(50) & (PBTN_MENU|PBTN_MOK|PBTN_MBACK));\r
-       in_set_blocking(0);\r
-\r
-       return ret;\r
-}\r
-\r
-#endif // !UIQ3\r
-\r
-void me_update_msg(const char *msg)\r
-{\r
-       strncpy(menu_error_msg, msg, sizeof(menu_error_msg));\r
-       menu_error_msg[sizeof(menu_error_msg) - 1] = 0;\r
-\r
-       menu_error_time = plat_get_ticks_ms();\r
-       lprintf("msg: %s\n", menu_error_msg);\r
-}\r
-\r
-// ------------ util ------------\r
-\r
-/* GP2X/wiz for now, probably extend later */\r
-void menu_plat_setup(int is_wiz)\r
-{\r
-       int i;\r
-\r
-       if (!is_wiz) {\r
-               me_enable(e_menu_gfx_options, MA_OPT_TEARING_FIX, 0);\r
-               i = me_id2offset(e_menu_gfx_options, MA_OPT_TEARING_FIX);\r
-               e_menu_gfx_options[i].need_to_save = 0;\r
-               return;\r
-       }\r
-\r
-       me_enable(e_menu_adv_options, MA_OPT_ARM940_SOUND, 0);\r
-       me_enable(e_menu_gfx_options, MA_OPT2_GAMMA, 0);\r
-       me_enable(e_menu_gfx_options, MA_OPT2_A_SN_GAMMA, 0);\r
-\r
-       i = me_id2offset(e_menu_gfx_options, MA_OPT_SCALING);\r
-       e_menu_gfx_options[i].max = 1;  /* only off and sw */\r
-       i = me_id2offset(e_menu_gfx_options, MA_OPT_ARM940_SOUND);\r
-       e_menu_gfx_options[i].need_to_save = 0;\r
-}\r
-\r
-/* TODO: rename */\r
-void menu_darken_bg(void *dst, int pixels, int darker)\r
-{\r
-       unsigned int *screen = dst;\r
-       pixels /= 2;\r
-       if (darker)\r
-       {\r
-               while (pixels--)\r
-               {\r
-                       unsigned int p = *screen;\r
-                       *screen++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);\r
-               }\r
-       }\r
-       else\r
-       {\r
-               while (pixels--)\r
-               {\r
-                       unsigned int p = *screen;\r
-                       *screen++ = (p&0xf79ef79e)>>1;\r
-               }\r
-       }\r
-}\r
-\r
-/* hidden options for config engine only */\r
-static menu_entry e_menu_hidden[] =\r
-{\r
-       mee_onoff("Accurate sprites", MA_OPT_ACC_SPRITES, PicoOpt, 0x080),\r
-       mee_end,\r
-};\r
-\r
-static menu_entry *e_menu_table[] =\r
-{\r
-       e_menu_options,\r
-       e_menu_gfx_options,\r
-       e_menu_adv_options,\r
-       e_menu_cd_options,\r
-       e_menu_32x_options,\r
-       e_menu_keyconfig,\r
-       e_menu_hidden,\r
-};\r
-\r
-static menu_entry *me_list_table = NULL;\r
-static menu_entry *me_list_i = NULL;\r
-\r
-menu_entry *me_list_get_first(void)\r
-{\r
-       me_list_table = me_list_i = e_menu_table[0];\r
-       return me_list_i;\r
-}\r
-\r
-menu_entry *me_list_get_next(void)\r
-{\r
-       int i;\r
-\r
-       me_list_i++;\r
-       if (me_list_i->name != NULL)\r
-               return me_list_i;\r
-\r
-       for (i = 0; i < array_size(e_menu_table); i++)\r
-               if (me_list_table == e_menu_table[i])\r
-                       break;\r
-\r
-       if (i + 1 < array_size(e_menu_table))\r
-               me_list_table = me_list_i = e_menu_table[i + 1];\r
-       else\r
-               me_list_table = me_list_i = NULL;\r
-\r
-       return me_list_i;\r
 }\r
 \r