bring pandora to buildable state. Some cleanups and fixes along the way.
[libpicofe.git] / common / menu.c
index e3c8cef..2f29e87 100644 (file)
@@ -1,4 +1,4 @@
-// (c) Copyright 2006,2007 notaz, All rights reserved.\r
+// (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
 #define array_size(x) (sizeof(x) / sizeof(x[0]))\r
 \r
 static char static_buff[64];\r
-char menuErrorMsg[64] = { 0, };\r
-\r
+static char menu_error_msg[64] = { 0, };\r
+static int  menu_error_time = 0;\r
 \r
 #ifndef UIQ3\r
 \r
-static unsigned char menu_font_data[10240];\r
+static unsigned char *menu_font_data = NULL;\r
 static int menu_text_color = 0xffff; // default to white\r
 static int menu_sel_color = -1; // disabled\r
+#if MENU_X2\r
+static const int me_mfont_w = 16, me_mfont_h = 20;\r
+#else\r
+static const int me_mfont_w = 8, me_mfont_h = 10;\r
+#endif\r
 \r
 // 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 *)SCREEN_BUFFER + x + y*SCREEN_WIDTH;\r
+       unsigned short *dest = (unsigned short *)g_screen_ptr + x + y * g_screen_width;\r
        tr = (color & 0xf800) >> 8;\r
        tg = (color & 0x07e0) >> 3;\r
        tb = (color & 0x001f) << 3;\r
@@ -59,11 +64,11 @@ static void text_out16_(int x, int y, const char *text, int color)
 \r
        for (i = 0; i < len; i++)\r
        {\r
-               unsigned char  *src = menu_font_data + (unsigned int)text[i]*4*10;\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 < 10; l++, dst += SCREEN_WIDTH-8)\r
+               for (l = 0; l < me_mfont_h; l++, dst += g_screen_width - me_mfont_w)\r
                {\r
-                       for (u = 8/2; u > 0; u--, src++)\r
+                       for (u = me_mfont_w / 2; u > 0; u--, src++)\r
                        {\r
                                int c, r, g, b;\r
                                c = *src >> 4;\r
@@ -84,7 +89,7 @@ static void text_out16_(int x, int y, const char *text, int color)
                                *dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);\r
                        }\r
                }\r
-               dest += 8;\r
+               dest += me_mfont_w;\r
        }\r
 }\r
 \r
@@ -92,14 +97,17 @@ void text_out16(int x, int y, const char *texto, ...)
 {\r
        va_list args;\r
        char    buffer[256];\r
-       int     maxw = (SCREEN_WIDTH - x) / 8;\r
+       int     maxw = (g_screen_width - x) / me_mfont_w;\r
+\r
+       if (maxw < 0)\r
+               return;\r
 \r
        va_start(args, texto);\r
        vsnprintf(buffer, sizeof(buffer), texto, args);\r
        va_end(args);\r
 \r
-       if (maxw > 255)\r
-               maxw = 255;\r
+       if (maxw > sizeof(buffer) - 1)\r
+               maxw = sizeof(buffer) - 1;\r
        buffer[maxw] = 0;\r
 \r
        text_out16_(x,y,buffer,menu_text_color);\r
@@ -120,7 +128,7 @@ static void smalltext_out16_(int x, int y, const char *texto, int color)
                if (!c) break;\r
 \r
                src = fontdata6x8[c];\r
-               dst = (unsigned short *)SCREEN_BUFFER + x + y*SCREEN_WIDTH;\r
+               dst = (unsigned short *)g_screen_ptr + x + y * g_screen_width;\r
 \r
                while (h--)\r
                {\r
@@ -133,17 +141,23 @@ static void smalltext_out16_(int x, int y, const char *texto, int color)
                        }\r
                        src++;\r
 \r
-                       dst += SCREEN_WIDTH-6;\r
+                       dst += g_screen_width - 6;\r
                }\r
        }\r
 }\r
 \r
-void smalltext_out16(int x, int y, const char *texto, int color)\r
+static void smalltext_out16(int x, int y, const char *texto, int color)\r
 {\r
-       char buffer[SCREEN_WIDTH/6+1];\r
+       char buffer[128];\r
+       int maxw = (g_screen_width - x) / 6;\r
+\r
+       if (maxw < 0)\r
+               return;\r
 \r
        strncpy(buffer, texto, sizeof(buffer));\r
-       buffer[sizeof(buffer) - 1] = 0;\r
+       if (maxw > sizeof(buffer) - 1)\r
+               maxw = sizeof(buffer) - 1;\r
+       buffer[maxw] = 0;\r
 \r
        smalltext_out16_(x, y, buffer, color);\r
 }\r
@@ -158,13 +172,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 *)SCREEN_BUFFER + x + y*SCREEN_WIDTH + 14;\r
-       for (h = 11; h > 0; h--)\r
+       dest = (unsigned short *)g_screen_ptr + x + y * g_screen_width + 14;\r
+       for (h = me_mfont_h + 1; h > 0; h--)\r
        {\r
                dst = dest;\r
                for (i = w; i > 0; i--)\r
                        *dst++ = menu_sel_color;\r
-               dest += SCREEN_WIDTH;\r
+               dest += g_screen_width;\r
        }\r
 }\r
 \r
@@ -183,33 +197,65 @@ static int parse_hex_color(char *buff)
 \r
 void menu_init(void)\r
 {\r
-       int c, l;\r
-       unsigned char *fd = menu_font_data;\r
+       int i, c, l;\r
+       unsigned char *fd, *fds;\r
        char buff[256];\r
        FILE *f;\r
 \r
-       // generate default font from fontdata8x8\r
-       memset(menu_font_data, 0, sizeof(menu_font_data));\r
-       for (c = 0; c < 256; c++)\r
+       if (menu_font_data != NULL)\r
+               free(menu_font_data);\r
+\r
+       menu_font_data = calloc((MENU_X2 ? 256 * 320 : 128 * 160) / 2, 1);\r
+       if (menu_font_data == NULL)\r
+               return;\r
+\r
+       // generate default 8x10 font from fontdata8x8\r
+       for (c = 0, fd = menu_font_data; c < 256; c++)\r
        {\r
                for (l = 0; l < 8; l++)\r
                {\r
                        unsigned char fd8x8 = fontdata8x8[c*8+l];\r
-                       if (fd8x8&0x80) *fd |= 0xf0;\r
+                       if (fd8x8&0x80) *fd  = 0xf0;\r
                        if (fd8x8&0x40) *fd |= 0x0f; fd++;\r
-                       if (fd8x8&0x20) *fd |= 0xf0;\r
+                       if (fd8x8&0x20) *fd  = 0xf0;\r
                        if (fd8x8&0x10) *fd |= 0x0f; fd++;\r
-                       if (fd8x8&0x08) *fd |= 0xf0;\r
+                       if (fd8x8&0x08) *fd  = 0xf0;\r
                        if (fd8x8&0x04) *fd |= 0x0f; fd++;\r
-                       if (fd8x8&0x02) *fd |= 0xf0;\r
+                       if (fd8x8&0x02) *fd  = 0xf0;\r
                        if (fd8x8&0x01) *fd |= 0x0f; fd++;\r
                }\r
                fd += 8*2/2; // 2 empty lines\r
        }\r
 \r
+       if (MENU_X2) {\r
+               // expand default font\r
+               fds = menu_font_data + 128 * 160 / 2 - 4;\r
+               fd  = menu_font_data + 256 * 320 / 2 - 1;\r
+               for (c = 255; c >= 0; c--)\r
+               {\r
+                       for (l = 9; l >= 0; l--, fds -= 4)\r
+                       {\r
+                               for (i = 3; i >= 0; i--) {\r
+                                       int px = fds[i] & 0x0f;\r
+                                       *fd-- = px | (px << 4);\r
+                                       px = (fds[i] >> 4) & 0x0f;\r
+                                       *fd-- = px | (px << 4);\r
+                               }\r
+                               for (i = 3; i >= 0; i--) {\r
+                                       int px = fds[i] & 0x0f;\r
+                                       *fd-- = px | (px << 4);\r
+                                       px = (fds[i] >> 4) & 0x0f;\r
+                                       *fd-- = px | (px << 4);\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+\r
        // load custom font and selector (stored as 1st symbol in font table)\r
        readpng(menu_font_data, "skin/font.png", READPNG_FONT);\r
-       memcpy(menu_font_data, menu_font_data + ((int)'>')*4*10, 4*10); // default selector symbol is '>'\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
        readpng(menu_font_data, "skin/selector.png", READPNG_SELECTOR);\r
 \r
        // load custom colors\r
@@ -272,7 +318,7 @@ static void me_draw(const menu_entry *entries, int sel)
 {\r
        const menu_entry *ent;\r
        int x, y, w = 0, h = 0;\r
-       int offs, opt_offs = 27*8;\r
+       int offs, opt_offs = 27 * me_mfont_w;\r
        const char *name;\r
        int asel = 0;\r
        int i, n;\r
@@ -289,22 +335,22 @@ static void me_draw(const menu_entry *entries, int sel)
                        asel = n;\r
 \r
                name = NULL;\r
-               wt = strlen(ent->name) * 8;     /* FIXME: unhardcode font width */\r
+               wt = strlen(ent->name) * me_mfont_w;\r
                if (wt == 0 && ent->generate_name)\r
                        name = ent->generate_name(ent->id, &offs);\r
                if (name != NULL)\r
-                       wt = strlen(name) * 8;\r
+                       wt = strlen(name) * me_mfont_w;\r
 \r
                if (ent->beh != MB_NONE)\r
                {\r
                        if (wt > opt_offs)\r
-                               opt_offs = wt + 8;\r
+                               opt_offs = wt + me_mfont_w;\r
                        wt = opt_offs;\r
 \r
                        switch (ent->beh) {\r
                        case MB_NONE: break;\r
                        case MB_OPT_ONOFF:\r
-                       case MB_OPT_RANGE: wt += 8*3; break;\r
+                       case MB_OPT_RANGE: wt += me_mfont_w * 3; break;\r
                        case MB_OPT_CUSTOM:\r
                        case MB_OPT_CUSTONOFF:\r
                        case MB_OPT_CUSTRANGE:\r
@@ -313,7 +359,7 @@ static void me_draw(const menu_entry *entries, int sel)
                                if (ent->generate_name != NULL)\r
                                        name = ent->generate_name(ent->id, &offs);\r
                                if (name != NULL)\r
-                                       wt += (strlen(name) + offs) * 8;\r
+                                       wt += (strlen(name) + offs) * me_mfont_w;\r
                                break;\r
                        }\r
                }\r
@@ -322,24 +368,25 @@ static void me_draw(const menu_entry *entries, int sel)
                        w = wt;\r
                n++;\r
        }\r
-       h = n * 10;\r
-       w += 16; /* selector */\r
+       h = n * me_mfont_h;\r
+       w += me_mfont_w * 2; /* selector */\r
 \r
-       if (w > SCREEN_WIDTH) {\r
-               lprintf("width %d > %d\n", w, SCREEN_WIDTH);\r
-               w = SCREEN_WIDTH;\r
+       if (w > g_screen_width) {\r
+               lprintf("width %d > %d\n", w, g_screen_width);\r
+               w = g_screen_width;\r
        }\r
-       if (h > SCREEN_HEIGHT) {\r
-               lprintf("height %d > %d\n", w, SCREEN_HEIGHT);\r
-               h = SCREEN_HEIGHT;\r
+       if (h > g_screen_height) {\r
+               lprintf("height %d > %d\n", w, g_screen_height);\r
+               h = g_screen_height;\r
        }\r
 \r
-       x = SCREEN_WIDTH  / 2 - w / 2;\r
-       y = SCREEN_HEIGHT / 2 - h / 2;\r
+       x = g_screen_width  / 2 - w / 2;\r
+       y = g_screen_height / 2 - h / 2;\r
 \r
        /* draw */\r
        plat_video_menu_begin();\r
-       menu_draw_selection(x, y + asel * 10, w);\r
+       menu_draw_selection(x, y + asel * me_mfont_h, w);\r
+       x += me_mfont_w * 2;\r
 \r
        for (ent = entries; ent->name; ent++)\r
        {\r
@@ -352,16 +399,16 @@ static void me_draw(const menu_entry *entries, int sel)
                                name = ent->generate_name(ent->id, &offs);\r
                }\r
                if (name != NULL)\r
-                       text_out16(x + 16, y, name);\r
+                       text_out16(x, y, name);\r
 \r
                switch (ent->beh) {\r
                case MB_NONE:\r
                        break;\r
                case MB_OPT_ONOFF:\r
-                       text_out16(x + 16 + opt_offs, y, (*(int *)ent->var & ent->mask) ? "ON" : "OFF");\r
+                       text_out16(x + opt_offs, y, (*(int *)ent->var & ent->mask) ? "ON" : "OFF");\r
                        break;\r
                case MB_OPT_RANGE:\r
-                       text_out16(x + 16 + opt_offs, y, "%i", *(int *)ent->var);\r
+                       text_out16(x + opt_offs, y, "%i", *(int *)ent->var);\r
                        break;\r
                case MB_OPT_CUSTOM:\r
                case MB_OPT_CUSTONOFF:\r
@@ -371,25 +418,22 @@ static void me_draw(const menu_entry *entries, int sel)
                        if (ent->generate_name)\r
                                name = ent->generate_name(ent->id, &offs);\r
                        if (name != NULL)\r
-                               text_out16(x + 16 + opt_offs + offs * 8, y, "%s", name);\r
+                               text_out16(x + opt_offs + offs * me_mfont_w, y, "%s", name);\r
                        break;\r
                }\r
 \r
-               y += 10;\r
+               y += me_mfont_h;\r
        }\r
 \r
        /* display message if we have one */\r
-       if (menuErrorMsg[0] != 0) {\r
-               static int msg_redraws = 0;\r
-               if (SCREEN_HEIGHT - h >= 2*10)\r
-                       text_out16(5, 226, menuErrorMsg);\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
                else\r
                        lprintf("menu msg doesn't fit!\n");\r
 \r
-               if (++msg_redraws > 4) {\r
-                       menuErrorMsg[0] = 0;\r
-                       msg_redraws = 0;\r
-               }\r
+               if (plat_get_ticks_ms() - menu_error_time > 2048)\r
+                       menu_error_msg[0] = 0;\r
        }\r
 \r
        plat_video_menu_end();\r
@@ -501,14 +545,14 @@ static void draw_menu_credits(void)
                p++;\r
        }\r
 \r
-       x = SCREEN_WIDTH  / 2 - w *  8 / 2;\r
-       y = SCREEN_HEIGHT / 2 - h * 10 / 2;\r
+       x = g_screen_width  / 2 - w * me_mfont_w / 2;\r
+       y = g_screen_height / 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
 \r
-       for (p = creds; *p != 0 && y <= SCREEN_HEIGHT - 10; y += 10) {\r
+       for (p = creds; *p != 0 && y <= g_screen_height - me_mfont_h; y += me_mfont_h) {\r
                text_out16(x, y, p);\r
 \r
                for (; *p != 0 && *p != '\n'; p++)\r
@@ -526,30 +570,30 @@ static int cdload_called = 0;
 \r
 static void load_progress_cb(int percent)\r
 {\r
-       int ln, len = percent * SCREEN_WIDTH / 100;\r
-       unsigned short *dst = (unsigned short *)SCREEN_BUFFER + SCREEN_WIDTH * 10 * 2;\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 > SCREEN_WIDTH)\r
-               len = SCREEN_WIDTH;\r
-       for (ln = 10 - 2; ln > 0; ln--, dst += SCREEN_WIDTH)\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(int percent)\r
 {\r
-       int ln, len = percent * SCREEN_WIDTH / 100;\r
-       unsigned short *dst = (unsigned short *)SCREEN_BUFFER + SCREEN_WIDTH * 10 * 2;\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, SCREEN_WIDTH * (10 - 2) * 2);\r
+       memset(dst, 0xff, g_screen_width * (10 - 2) * 2);\r
 \r
        smalltext_out16(1, 3 * 10, "Processing CD image / MP3s", 0xffff);\r
        smalltext_out16(1, 4 * 10, rom_fname_loaded, 0xffff);\r
-       dst += SCREEN_WIDTH * 30;\r
+       dst += g_screen_width * 30;\r
 \r
-       if (len > SCREEN_WIDTH)\r
-               len = SCREEN_WIDTH;\r
-       for (ln = (10 - 2); ln > 0; ln--, dst += SCREEN_WIDTH)\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
 \r
        plat_video_menu_end();\r
@@ -609,7 +653,7 @@ static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)
 {\r
        int max_cnt, start, i, pos;\r
 \r
-       max_cnt = SCREEN_HEIGHT / 10;\r
+       max_cnt = g_screen_height / 10;\r
        start = max_cnt / 2 - sel;\r
        n--; // exclude current dir (".")\r
 \r
@@ -618,7 +662,7 @@ static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)
 //     if (!rom_loaded)\r
 //             menu_darken_bg(gp2x_screen, 320*240, 0);\r
 \r
-       menu_darken_bg((short *)SCREEN_BUFFER + SCREEN_WIDTH * max_cnt/2 * 10, SCREEN_WIDTH * 8, 0);\r
+       menu_darken_bg((short *)g_screen_ptr + g_screen_width * max_cnt/2 * 10, g_screen_width * 8, 0);\r
 \r
        if (start - 2 >= 0)\r
                smalltext_out16(14, (start - 2)*10, curdir, 0xffff);\r
@@ -634,7 +678,7 @@ static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)
                        smalltext_out16(14,   pos*10, namelist[i+1]->d_name, color);\r
                }\r
        }\r
-       text_out16(5, max_cnt/2 * 10, ">");\r
+       smalltext_out16(5, max_cnt/2 * 10, ">", 0xffff);\r
        plat_video_menu_end();\r
 }\r
 \r
@@ -714,14 +758,14 @@ rescan:
        {\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_WEST|PBTN_MOK|PBTN_MBACK|PBTN_MENU, 33); // TODO L R\r
+                       PBTN_L|PBTN_R|PBTN_MA2|PBTN_MOK|PBTN_MBACK|PBTN_MENU, 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_MOK) || (inp & (PBTN_MENU|PBTN_WEST)) == (PBTN_MENU|PBTN_WEST)) // enter dir/select || delete\r
+               if ((inp & PBTN_MOK) || (inp & (PBTN_MENU|PBTN_MA2)) == (PBTN_MENU|PBTN_MA2))\r
                {\r
                        again:\r
                        if (namelist[sel+1]->d_type == DT_REG)\r
@@ -801,7 +845,7 @@ static void draw_patchlist(int sel)
 {\r
        int max_cnt, start, i, pos, active;\r
 \r
-       max_cnt = SCREEN_HEIGHT / 10;\r
+       max_cnt = g_screen_height / 10;\r
        start = max_cnt / 2 - sel;\r
 \r
        plat_video_menu_begin();\r
@@ -921,23 +965,22 @@ static void draw_savestate_menu(int menu_sel, int is_loading)
        if (state_slot_flags & (1 << menu_sel))\r
                draw_savestate_bg(menu_sel);\r
 \r
-       w = 13 * 8 + 16;\r
-       h = (1+2+10+1) * 10;\r
-       x = SCREEN_WIDTH / 2 - w / 2;\r
+       w = 13 * me_mfont_w + me_mfont_w * 2;\r
+       h = (1+2+10+1) * me_mfont_h;\r
+       x = g_screen_width / 2 - w / 2;\r
        if (x < 0) x = 0;\r
-       y = SCREEN_HEIGHT / 2 - h / 2;\r
+       y = g_screen_height / 2 - h / 2;\r
        if (y < 0) y = 0;\r
 \r
        plat_video_menu_begin();\r
 \r
        text_out16(x, y, is_loading ? "Load state" : "Save state");\r
-       y += 3*10;\r
+       y += 3 * me_mfont_h;\r
 \r
-       menu_draw_selection(x - 16, y + menu_sel * 10, 13 * 8 + 4);\r
+       menu_draw_selection(x - me_mfont_w * 2, y + menu_sel * me_mfont_h, 13 * me_mfont_w + 4);\r
 \r
        /* draw all 10 slots */\r
-       y += 10;\r
-       for (i = 0; i < 10; i++, y += 10)\r
+       for (i = 0; i < 10; i++, y += me_mfont_h)\r
        {\r
                text_out16(x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");\r
        }\r
@@ -976,7 +1019,7 @@ static int menu_loop_savestate(int is_loading)
                        if (menu_sel < 10) {\r
                                state_slot = menu_sel;\r
                                if (emu_SaveLoadGame(is_loading, 0)) {\r
-                                       strcpy(menuErrorMsg, is_loading ? "Load failed" : "Save failed");\r
+                                       me_update_msg(is_loading ? "Load failed" : "Save failed");\r
                                        return 0;\r
                                }\r
                                return 1;\r
@@ -1048,42 +1091,45 @@ static int count_bound_keys(int dev_id, int action_mask, int player_idx)
 static void draw_key_config(const me_bind_action *opts, int opt_cnt, int player_idx,\r
                int sel, int dev_id, int dev_count, int is_bind)\r
 {\r
-       int x, y = 30, w, i;\r
+       int x, y, w, i;\r
        const char *dev_name;\r
 \r
-       x = SCREEN_WIDTH / 2 - 32*8 / 2;\r
-       if (x < 0) x = 0;\r
+       x = g_screen_width / 2 - 20 * me_mfont_w / 2;\r
+       y = (g_screen_height - 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
        if (player_idx >= 0)\r
-               text_out16(x, 10, "Player %i controls", player_idx + 1);\r
+               text_out16(x, y, "Player %i controls", player_idx + 1);\r
        else\r
-               text_out16(x, 10, "Emulator controls");\r
+               text_out16(x, y, "Emulator controls");\r
 \r
-       menu_draw_selection(x - 16, y + sel*10, (player_idx >= 0) ? 66 : 140);\r
+       y += 2 * me_mfont_h;\r
+       menu_draw_selection(x - me_mfont_w * 2, y + sel * me_mfont_h, me_mfont_w); // FIXME last arg\r
 \r
-       for (i = 0; i < opt_cnt; i++, y+=10)\r
+       for (i = 0; i < opt_cnt; i++, y += me_mfont_h)\r
                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
-       w = strlen(dev_name) * 8;\r
-       if (w < 30 * 8)\r
-               w = 30 * 8;\r
-       if (w > SCREEN_WIDTH)\r
-               w = SCREEN_WIDTH;\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
 \r
-       x = SCREEN_WIDTH / 2 - w / 2;\r
+       x = g_screen_width / 2 - w / 2;\r
 \r
        if (dev_count > 1) {\r
-               text_out16(x, SCREEN_HEIGHT - 4*10, "Viewing binds for:");\r
-               text_out16(x, SCREEN_HEIGHT - 3*10, dev_name);\r
+               text_out16(x, g_screen_height - 4 * me_mfont_h, "Viewing binds for:");\r
+               text_out16(x, g_screen_height - 3 * me_mfont_h, dev_name);\r
        }\r
 \r
        if (is_bind)\r
-               text_out16(x, SCREEN_HEIGHT - 2*10, "Press a button to bind/unbind");\r
+               text_out16(x, g_screen_height - 2 * me_mfont_h, "Press a button to bind/unbind");\r
        else if (dev_count > 1)\r
-               text_out16(x, SCREEN_HEIGHT - 2*10, "Press left/right for other devs");\r
+               text_out16(x, g_screen_height - 2 * me_mfont_h, "Press left/right for other devs");\r
 \r
        plat_video_menu_end();\r
 }\r
@@ -1493,15 +1539,15 @@ static int mh_saveloadcfg(menu_id id, int keys)
        case MA_OPT_SAVECFG:\r
        case MA_OPT_SAVECFG_GAME:\r
                if (emu_WriteConfig(id == MA_OPT_SAVECFG_GAME ? 1 : 0))\r
-                       strcpy(menuErrorMsg, "config saved");\r
+                       me_update_msg("config saved");\r
                else\r
-                       strcpy(menuErrorMsg, "failed to write config");\r
+                       me_update_msg("failed to write config");\r
                break;\r
        case MA_OPT_LOADCFG:\r
                ret = emu_ReadConfig(1, 1);\r
                if (!ret) ret = emu_ReadConfig(0, 1);\r
-               if (ret)  strcpy(menuErrorMsg, "config loaded");\r
-               else      strcpy(menuErrorMsg, "failed to load config");\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
@@ -1644,7 +1690,8 @@ static void mplayer_loop(void)
        while (1)\r
        {\r
                PDebugZ80Frame();\r
-               if (in_menu_wait_any(0) & PBTN_NORTH) break;\r
+               if (in_menu_wait_any(0) & PBTN_MA3)\r
+                       break;\r
                emu_waitSound();\r
        }\r
 \r
@@ -1665,7 +1712,7 @@ static void draw_text_debug(const char *str, int skip, int from)
        }\r
 \r
        str = p;\r
-       for (line = from; line < SCREEN_HEIGHT/10; line++)\r
+       for (line = from; line < g_screen_height / 10; line++)\r
        {\r
                while (*p && *p != '\n') p++;\r
                smalltext_out16(1, line*10, str, 0xffff);\r
@@ -1682,9 +1729,9 @@ static void draw_frame_debug(void)
        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
 \r
-       clear_screen();\r
+       memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);\r
        emu_forcedFrame(0);\r
-       smalltext_out16(4, SCREEN_HEIGHT-8, layer_str, 0xffff);\r
+       smalltext_out16(4, g_screen_height - 8, layer_str, 0xffff);\r
 }\r
 \r
 static void debug_menu_loop(void)\r
@@ -1702,26 +1749,27 @@ static void debug_menu_loop(void)
                                emu_platformDebugCat(tmp);\r
                                draw_text_debug(tmp, 0, 0);\r
                                if (dumped) {\r
-                                       smalltext_out16(SCREEN_WIDTH-6*10, SCREEN_HEIGHT-8, "dumped", 0xffff);\r
+                                       smalltext_out16(g_screen_width - 6 * me_mfont_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: clear_screen();\r
+                       case 2: memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);\r
                                emu_forcedFrame(0);\r
-                               darken_screen();\r
-                               PDebugShowSpriteStats((unsigned short *)SCREEN_BUFFER + (SCREEN_HEIGHT/2 - 240/2)*SCREEN_WIDTH +\r
-                                       SCREEN_WIDTH/2 - 320/2, SCREEN_WIDTH); break;\r
-                       case 3: clear_screen();\r
-                               PDebugShowPalette(SCREEN_BUFFER, SCREEN_WIDTH);\r
-                               PDebugShowSprite((unsigned short *)SCREEN_BUFFER + SCREEN_WIDTH*120+SCREEN_WIDTH/2+16,\r
-                                       SCREEN_WIDTH, spr_offs);\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
                }\r
                plat_video_menu_end();\r
 \r
-               inp = in_menu_wait(PBTN_EAST|PBTN_MBACK|PBTN_WEST|PBTN_NORTH|PBTN_L|PBTN_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 = 3; }\r
@@ -1729,15 +1777,17 @@ static void debug_menu_loop(void)
                switch (mode)\r
                {\r
                        case 0:\r
-                               if (inp & PBTN_EAST) SekStepM68k();\r
-                               if (inp & PBTN_NORTH) {\r
-                                       while (inp & PBTN_NORTH) inp = in_menu_wait_any(-1);\r
+                               if (inp & PBTN_MOK)\r
+                                       SekStepM68k();\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_WEST|PBTN_LEFT)) == (PBTN_WEST|PBTN_LEFT)) {\r
+                               if ((inp & (PBTN_MA2|PBTN_LEFT)) == (PBTN_MA2|PBTN_LEFT)) {\r
                                        mkdir("dumps", 0777);\r
                                        PDebugDumpMem();\r
-                                       while (inp & PBTN_WEST) inp = in_menu_wait_any(-1);\r
+                                       while (inp & PBTN_MA2) inp = in_menu_wait_any(-1);\r
                                        dumped = 1;\r
                                }\r
                                break;\r
@@ -1746,12 +1796,12 @@ static void debug_menu_loop(void)
                                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_EAST) {\r
+                               if (inp & PBTN_MOK) {\r
                                        PsndOut = NULL; // just in case\r
                                        PicoSkipFrame = 1;\r
                                        PicoFrame();\r
                                        PicoSkipFrame = 0;\r
-                                       while (inp & PBTN_EAST) inp = in_menu_wait_any(-1);\r
+                                       while (inp & PBTN_MOK) inp = in_menu_wait_any(-1);\r
                                }\r
                                break;\r
                        case 3:\r
@@ -1822,7 +1872,7 @@ static int main_menu_handler(menu_id id, int keys)
                if (rom_loaded && PicoPatches) {\r
                        menu_loop_patches();\r
                        PicoPatchApply();\r
-                       strcpy(menuErrorMsg, "Patches applied");\r
+                       me_update_msg("Patches applied");\r
                }\r
                break;\r
        default:\r
@@ -1886,8 +1936,7 @@ static int mh_tray_load_cd(menu_id id, int keys)
        if (cd_type != CIT_NOT_CD)\r
                ret = Insert_CD(ret_name, cd_type);\r
        if (ret != 0) {\r
-               sprintf(menuErrorMsg, "Load failed, invalid CD image?");\r
-               lprintf("%s\n", menuErrorMsg);\r
+               me_update_msg("Load failed, invalid CD image?");\r
                return 0;\r
        }\r
 \r
@@ -1932,6 +1981,15 @@ int menu_loop_tray(void)
 \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
 /* TODO: rename */\r