revive pandora and win32 builds, rm gp2x dep for linux, lots of refactoring
[libpicofe.git] / common / menu.c
index 688d538..c9fc188 100644 (file)
 #include <pico/pico_int.h>\r
 #include <pico/patch.h>\r
 \r
-#define array_size(x) (sizeof(x) / sizeof(x[0]))\r
-\r
 static char static_buff[64];\r
-static char menu_error_msg[64] = { 0, };\r
 static int  menu_error_time = 0;\r
+char menu_error_msg[64] = { 0, };\r
+void *g_menubg_ptr;\r
 \r
 #ifndef UIQ3\r
 \r
@@ -278,7 +277,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
@@ -301,6 +301,49 @@ void menu_init(void)
 }\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 void menu_enter(int is_rom_loaded)\r
+{\r
+       if (is_rom_loaded)\r
+       {\r
+               // darken the active framebuffer\r
+               menu_darken_bg(g_menubg_ptr, g_screen_ptr, g_screen_width * g_screen_height, 1);\r
+       }\r
+       else\r
+       {\r
+               char buff[256];\r
+\r
+               // should really only happen once, on startup..\r
+               emu_make_path(buff, "skin/background.png", sizeof(buff));\r
+               if (readpng(g_menubg_ptr, buff, READPNG_BG) < 0)\r
+                       memset(g_menubg_ptr, 0, g_screen_width * g_screen_height * 2);\r
+       }\r
+\r
+       plat_video_menu_enter(is_rom_loaded);\r
+}\r
+\r
 static int me_id2offset(const menu_entry *ent, menu_id id)\r
 {\r
        int i;\r
@@ -665,7 +708,7 @@ static void do_delete(const char *fpath, const char *fname)
        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_darken_bg(g_screen_ptr, g_screen_ptr, g_screen_width * g_screen_height, 0);\r
 \r
        len = strlen(fname);\r
        if (len > g_screen_width/6)\r
@@ -713,6 +756,7 @@ static unsigned short file2color(const char *fname)
 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
        start = max_cnt / 2 - sel;\r
@@ -723,7 +767,8 @@ 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 *)g_screen_ptr + g_screen_width * max_cnt/2 * 10, g_screen_width * 8, 0);\r
+       darken_ptr = (short *)g_screen_ptr + g_screen_width * max_cnt/2 * 10;\r
+       menu_darken_bg(darken_ptr, darken_ptr, g_screen_width * 8, 0);\r
 \r
        x = 5 + me_mfont_w + 1;\r
        if (start - 2 >= 0)\r
@@ -746,10 +791,15 @@ static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)
 \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
@@ -791,13 +841,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
@@ -816,6 +869,11 @@ rescan:
                }\r
        }\r
 \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
+               ;\r
+\r
        for (;;)\r
        {\r
                draw_dirlist(curr_path, namelist, n, sel);\r
@@ -990,7 +1048,7 @@ static void draw_savestate_bg(int slot)
 \r
        /* do a frame and fetch menu bg */\r
        pemu_forced_frame(POPT_EN_SOFTSCALE);\r
-       plat_video_menu_enter(1);\r
+       menu_enter(1);\r
 \r
        memcpy(Pico.vram, tmp_vram, sizeof(Pico.vram));\r
        memcpy(Pico.cram, tmp_cram, sizeof(Pico.cram));\r
@@ -1421,6 +1479,21 @@ static int menu_loop_cd_options(menu_id id, int keys)
        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
@@ -1449,7 +1522,7 @@ static int menu_loop_adv_options(menu_id id, int keys)
 \r
 static int mh_opt_render(menu_id id, int keys)\r
 {\r
-       plat_video_toggle_renderer((keys & PBTN_RIGHT) ? 1 : 0, 1);\r
+       plat_video_toggle_renderer((keys & PBTN_RIGHT) ? 1 : 0, 0, 1);\r
        return 0;\r
 }\r
 \r
@@ -1669,6 +1742,7 @@ static menu_entry e_menu_options[] =
        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
@@ -1686,10 +1760,6 @@ static int menu_loop_options(menu_id id, int keys)
 \r
        me_loop(e_menu_options, &sel, NULL);\r
 \r
-       if (PicoRegionOverride)\r
-               // force setting possibly changed..\r
-               Pico.m.pal = (PicoRegionOverride == 2 || PicoRegionOverride == 8) ? 1 : 0;\r
-\r
        return 0;\r
 }\r
 \r
@@ -1749,11 +1819,12 @@ static void draw_text_debug(const char *str, int skip, int from)
 \r
 static void draw_frame_debug(void)\r
 {\r
-       char layer_str[48] = "layers:             ";\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
@@ -1784,7 +1855,7 @@ static void debug_menu_loop(void)
                        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
+                               menu_darken_bg(g_screen_ptr, 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
@@ -1793,19 +1864,23 @@ static void debug_menu_loop(void)
                                        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 = 3; }\r
-               if (inp & PBTN_R) { mode++; if (mode > 3) mode = 0; }\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
-                                       SekStepM68k();\r
+                                       PDebugCPUStep();\r
                                if (inp & PBTN_MA3) {\r
                                        while (inp & PBTN_MA3)\r
                                                inp = in_menu_wait_any(-1);\r
@@ -1823,6 +1898,7 @@ 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_MA2)   PicoDrawMask ^= PDRAW_32X_ON;\r
                                if (inp & PBTN_MOK) {\r
                                        PsndOut = NULL; // just in case\r
                                        PicoSkipFrame = 1;\r
@@ -1939,7 +2015,7 @@ void menu_loop(void)
        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
+       menu_enter(rom_loaded);\r
        in_set_blocking(1);\r
        me_loop(e_menu_main, &sel, menu_main_plat_draw);\r
 \r
@@ -1947,7 +2023,8 @@ void menu_loop(void)
                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
+               while (in_menu_wait_any(50) & (PBTN_MENU|PBTN_MOK|PBTN_MBACK))\r
+                       ;\r
        }\r
 \r
        in_set_blocking(0);\r
@@ -1986,7 +2063,7 @@ int menu_loop_tray(void)
 {\r
        int ret = 1, sel = 0;\r
 \r
-       plat_video_menu_enter(rom_loaded);\r
+       menu_enter(rom_loaded);\r
 \r
        in_set_blocking(1);\r
        me_loop(e_menu_tray, &sel, NULL);\r
@@ -2037,29 +2114,6 @@ void menu_plat_setup(int is_wiz)
        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
@@ -2073,6 +2127,7 @@ static menu_entry *e_menu_table[] =
        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