revive pandora and win32 builds, rm gp2x dep for linux, lots of refactoring
[libpicofe.git] / common / menu.c
index 8673784..c9fc188 100644 (file)
@@ -23,8 +23,9 @@
 #include <pico/patch.h>\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
@@ -276,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
@@ -299,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
@@ -663,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
@@ -711,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
@@ -721,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
@@ -744,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
@@ -789,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
@@ -993,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
@@ -1424,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
@@ -1672,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
@@ -1689,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
@@ -1788,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
@@ -1948,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
@@ -1996,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
@@ -2047,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
@@ -2083,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