SIG_IGN->SIG_DFL, fixes usb storage mode
[fceu.git] / drivers / gp2x / menu.c
index b23837a..5cedc85 100644 (file)
@@ -1,7 +1,5 @@
+// menu system for gpfce - FCE Ultra port\r
 // (c) Copyright 2006,2007 notaz, All rights reserved.\r
-// Free for non-commercial use.\r
-\r
-// For commercial use, separate licencing terms must be obtained.\r
 \r
 #include <stdio.h>\r
 #include <string.h>\r
@@ -21,6 +19,7 @@
 #include "../../input.h"\r
 #include "../../state.h"\r
 #include "../../palette.h"\r
+#include "readpng.h"\r
 \r
 #ifndef _DIRENT_HAVE_D_TYPE\r
 #error "need d_type for file browser\r
@@ -44,13 +43,67 @@ static char *gp2xKeyNames[] = {
 \r
 \r
 static char path_buffer[PATH_MAX];\r
+static unsigned short *menu_bg = 0;\r
+static int txt_xmin, txt_xmax, txt_ymin, txt_ymax;\r
 \r
 char menuErrorMsg[40] = {0, };\r
 \r
-// TODO\r
-void gp2x_fceu_copy_bg(void)\r
+static void gp2x_fceu_darken_reset(void)\r
 {\r
-       memset(gp2x_screen, 0, 320*240*2);\r
+       txt_xmin = 320; txt_xmax = 0;\r
+       txt_ymin = 240; txt_ymax = 0;\r
+}\r
+\r
+static void gp2x_fceu_copy_bg(void)\r
+{\r
+       if (menu_bg)\r
+            memcpy(gp2x_screen, menu_bg, 320*240*2);\r
+       else memset(gp2x_screen, 0, 320*240*2);\r
+       gp2x_fceu_darken_reset();\r
+}\r
+\r
+static void gp2x_fceu_darken_text_bg(void)\r
+{\r
+       int x, y, xmin, xmax, ymax;\r
+       unsigned short *screen = gp2x_screen;\r
+\r
+       xmin = txt_xmin - 3;\r
+       if (xmin < 0) xmin = 0;\r
+       xmax = txt_xmax + 2;\r
+       if (xmax > 319) xmax = 319;\r
+\r
+       y = txt_ymin - 3;\r
+       if (y < 0) y = 0;\r
+       ymax = txt_ymax + 2;\r
+       if (ymax > 239) ymax = 239;\r
+\r
+       for (x = xmin; x <= xmax; x++)\r
+               screen[y*320+x] = 0xa514;\r
+       for (y++; y < ymax; y++)\r
+       {\r
+               screen[y*320+xmin] = 0xffff;\r
+               for (x = xmin+1; x < xmax; x++)\r
+               {\r
+                       unsigned int p = screen[y*320+x];\r
+                       if (p != 0xffff)\r
+                               screen[y*320+x] = ((p&0xf79e)>>1) - ((p&0xc618)>>3);\r
+               }\r
+               screen[y*320+xmax] = 0xffff;\r
+       }\r
+       for (x = xmin; x <= xmax; x++)\r
+               screen[y*320+x] = 0xffff;\r
+}\r
+\r
+static void gp2x_fceu_darken_all(void)\r
+{\r
+       unsigned int *screen = gp2x_screen;\r
+       int count = 320*240/2;\r
+\r
+       while (count--)\r
+       {\r
+               unsigned int p = screen[count];\r
+               screen[count] = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);\r
+       }\r
 }\r
 \r
 // draws white text to current bbp15 screen\r
@@ -76,6 +129,10 @@ static void gp2x_text_out15_(int x, int y, const char *text)
                }\r
                screen += 8;\r
        }\r
+       if (x < txt_xmin) txt_xmin = x;\r
+       if (x+i*8 > txt_xmax) txt_xmax = x+i*8;\r
+       if (y < txt_ymin) txt_ymin = y;\r
+       if (y+8   > txt_ymax) txt_ymax = y+8;\r
 }\r
 \r
 void gp2x_text_out15(int x, int y, const char *texto, ...)\r
@@ -135,7 +192,7 @@ static void gp2x_smalltext16(int x, int y, const char *texto)
        }\r
 }\r
 \r
-static void gp2x_smalltext8_lim(int x, int y, const char *texto, int max)\r
+static void gp2x_smalltext16_lim(int x, int y, const char *texto, int max)\r
 {\r
        char    buffer[320/6+1];\r
 \r
@@ -243,20 +300,20 @@ static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)
        start = 12 - sel;\r
        n--; // exclude current dir (".")\r
 \r
-       //memset(gp2x_screen, 0, 320*240);\r
        gp2x_fceu_copy_bg();\r
+       gp2x_fceu_darken_all();\r
 \r
        if(start - 2 >= 0)\r
-               gp2x_smalltext8_lim(14, (start - 2)*10, curdir, 53-2);\r
+               gp2x_smalltext16_lim(14, (start - 2)*10, curdir, 53-2);\r
        for (i = 0; i < n; i++) {\r
                pos = start + i;\r
                if (pos < 0)  continue;\r
                if (pos > 23) break;\r
                if (namelist[i+1]->d_type == DT_DIR) {\r
-                       gp2x_smalltext8_lim(14,   pos*10, "/", 1);\r
-                       gp2x_smalltext8_lim(14+6, pos*10, namelist[i+1]->d_name, 53-3);\r
+                       gp2x_smalltext16_lim(14,   pos*10, "/", 1);\r
+                       gp2x_smalltext16_lim(14+6, pos*10, namelist[i+1]->d_name, 53-3);\r
                } else {\r
-                       gp2x_smalltext8_lim(14,   pos*10, namelist[i+1]->d_name, 53-2);\r
+                       gp2x_smalltext16_lim(14,   pos*10, namelist[i+1]->d_name, 53-2);\r
                }\r
        }\r
        gp2x_text_out15(5, 120, ">");\r
@@ -369,7 +426,7 @@ static char *filesel_loop(char *curr_path, char *final_dest)
                                } else {\r
                                        strcpy(newdir, curr_path);\r
                                        p = newdir + strlen(newdir) - 1;\r
-                                       while (*p == '/' && p >= newdir) *p-- = 0;\r
+                                       while (p >= newdir && *p == '/') *p-- = 0;\r
                                        strcat(newdir, "/");\r
                                        strcat(newdir, namelist[sel+1]->d_name);\r
                                }\r
@@ -409,47 +466,69 @@ static char *filesel_loop(char *curr_path, char *final_dest)
 \r
 // ------------ patch/gg menu ------------\r
 \r
-#if 0 // TODO?\r
-static void draw_patchlist(int sel)\r
+extern void *cheats;\r
+static int cheat_count = 0, cheat_start, cheat_pos;\r
+\r
+static int countcallb(char *name, uint32 a, uint8 v, int compare, int s, int type, void *data)\r
 {\r
-       int start, i, pos;\r
+       cheat_count++;\r
+       return 1;\r
+}\r
 \r
-       start = 12 - sel;\r
+static int clistcallb(char *name, uint32 a, uint8 v, int compare, int s, int type, void *data)\r
+{\r
+       int pos;\r
+\r
+       pos = cheat_start + cheat_pos;\r
+       cheat_pos++;\r
+       if (pos < 0)  return 1;\r
+       if (pos > 23) return 0;\r
+\r
+       gp2x_smalltext16_lim(14,     pos*10, s ? "ON " : "OFF", 3);\r
+       gp2x_smalltext16_lim(14+6*4, pos*10, type ? "S" : "R", 1);\r
+       gp2x_smalltext16_lim(14+6*6, pos*10, name, 53-8);\r
+\r
+       return 1;\r
+}\r
+\r
+static void draw_patchlist(int sel)\r
+{\r
+       int pos;\r
 \r
        gp2x_fceu_copy_bg();\r
+       gp2x_fceu_darken_all();\r
 \r
-       for (i = 0; i < PicoPatchCount; i++) {\r
-               pos = start + i;\r
-               if (pos < 0)  continue;\r
-               if (pos > 23) break;\r
-               gp2x_smalltext8_lim(14,     pos*10, PicoPatches[i].active ? "ON " : "OFF", 3);\r
-               gp2x_smalltext8_lim(14+6*4, pos*10, PicoPatches[i].name, 53-6);\r
-       }\r
-       pos = start + i;\r
-       if (pos < 24) gp2x_smalltext8_lim(14, pos*10, "done", 4);\r
+       cheat_start = 12 - sel;\r
+       cheat_pos = 0;\r
+       FCEUI_ListCheats(clistcallb,0);\r
+\r
+       pos = cheat_start + cheat_pos;\r
+       if (pos < 24) gp2x_smalltext16_lim(14, pos*10, "done", 4);\r
 \r
        gp2x_text_out15(5, 120, ">");\r
        gp2x_video_flip();\r
 }\r
 \r
-\r
 void patches_menu_loop(void)\r
 {\r
        int menu_sel = 0;\r
        unsigned long inp = 0;\r
 \r
+       cheat_count = 0;\r
+       FCEUI_ListCheats(countcallb,0);\r
+\r
        for(;;)\r
        {\r
                draw_patchlist(menu_sel);\r
                inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_L|GP2X_R|GP2X_B|GP2X_X);\r
-               if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = PicoPatchCount; }\r
-               if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > PicoPatchCount) menu_sel = 0; }\r
+               if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = cheat_count; }\r
+               if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > cheat_count) menu_sel = 0; }\r
                if(inp &(GP2X_LEFT|GP2X_L))  { menu_sel-=10; if (menu_sel < 0) menu_sel = 0; }\r
-               if(inp &(GP2X_RIGHT|GP2X_R)) { menu_sel+=10; if (menu_sel > PicoPatchCount) menu_sel = PicoPatchCount; }\r
+               if(inp &(GP2X_RIGHT|GP2X_R)) { menu_sel+=10; if (menu_sel > cheat_count) menu_sel = cheat_count; }\r
                if(inp & GP2X_B) { // action\r
-                       if (menu_sel < PicoPatchCount)\r
-                               PicoPatches[menu_sel].active = !PicoPatches[menu_sel].active;\r
-                       else    return;\r
+                       if (menu_sel < cheat_count)\r
+                            FCEUI_ToggleCheat(menu_sel);\r
+                       else return;\r
                }\r
                if(inp & GP2X_X) return;\r
        }\r
@@ -458,6 +537,7 @@ void patches_menu_loop(void)
 \r
 // ------------ savestate loader ------------\r
 \r
+#if 0\r
 static void menu_prepare_bg(void);\r
 \r
 static int state_slot_flags = 0;\r
@@ -689,6 +769,9 @@ static void draw_key_config(const bind_action_t *opts, int opt_cnt, int player_i
        // draw cursor\r
        gp2x_text_out15(x - 16, tl_y + sel*10, ">");\r
 \r
+       gp2x_fceu_darken_text_bg();\r
+       gp2x_fceu_darken_reset();\r
+\r
        if (sel < opt_cnt) {\r
                gp2x_text_out15(30, 190, "Press a button to bind/unbind");\r
                gp2x_text_out15(30, 200, "Use VOL+ to clear");\r
@@ -699,6 +782,7 @@ static void draw_key_config(const bind_action_t *opts, int opt_cnt, int player_i
                gp2x_text_out15(30, 210, "to save controls");\r
                gp2x_text_out15(30, 220, "Press B or X to exit");\r
        }\r
+       gp2x_fceu_darken_text_bg();\r
        gp2x_video_flip();\r
 }\r
 \r
@@ -778,6 +862,7 @@ static void draw_kc_sel(int menu_sel)
                gp2x_text_out15(tl_x, (y+=10), "none");\r
        }\r
 \r
+       gp2x_fceu_darken_text_bg();\r
        gp2x_video_flip();\r
 }\r
 \r
@@ -878,22 +963,27 @@ static void draw_fcemenu_options(int menu_sel)
        gp2x_text_out15(tl_x, (y+=10), "Last visible line (PAL)    %i", erendlinev[1]);\r
        gp2x_text_out15(tl_x, (y+=10), "Clip 8 left/right columns  %s", (eoptions&EO_CLIPSIDES)?"ON":"OFF");\r
        gp2x_text_out15(tl_x, (y+=10), "Disable 8 sprite limit     %s", (eoptions&EO_NO8LIM)?"ON":"OFF");\r
-       gp2x_text_out15(tl_x, (y+=10), "Done");                                                 // 10\r
+       gp2x_text_out15(tl_x, (y+=10), "Enable authentic GameGenie %s", (eoptions&EO_GG)?"ON":"OFF");\r
+       gp2x_text_out15(tl_x, (y+=10), "Done");                                                 // 11\r
+\r
+       // draw cursor\r
+       gp2x_text_out15(tl_x - 16, tl_y + menu_sel*10, ">");\r
 \r
        if (menu_sel == 0) {\r
+               gp2x_fceu_darken_text_bg();\r
+               gp2x_fceu_darken_reset();\r
+\r
                gp2x_text_out15(30, 210, "Press B to browse,");\r
                gp2x_text_out15(30, 220, "START to use default");\r
        }\r
 \r
-       // draw cursor\r
-       gp2x_text_out15(tl_x - 16, tl_y + menu_sel*10, ">");\r
-\r
+       gp2x_fceu_darken_text_bg();\r
        gp2x_video_flip();\r
 }\r
 \r
 static void fcemenu_loop_options(void)\r
 {\r
-       int menu_sel = 0, menu_sel_max = 10, i;\r
+       int menu_sel = 0, menu_sel_max = 11, i;\r
        unsigned long inp = 0;\r
 \r
        FCEUI_GetNTSCTH(&ntsctint, &ntschue);\r
@@ -909,7 +999,8 @@ static void fcemenu_loop_options(void)
                                case  1: ntsccol = !ntsccol; break;\r
                                case  8: eoptions^=EO_CLIPSIDES; break;\r
                                case  9: eoptions^=EO_NO8LIM; break;\r
-                               case 10: return;\r
+                               case 10: eoptions^=EO_GG; break;\r
+                               case 11: return;\r
                        }\r
                }\r
                if(inp & (GP2X_X|GP2X_A)) {\r
@@ -921,6 +1012,7 @@ static void fcemenu_loop_options(void)
                        FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);\r
                        FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);\r
                        FCEUI_DisableSpriteLimitation(eoptions&EO_NO8LIM);\r
+                       FCEUI_SetGameGenie(eoptions&EO_GG);\r
                        if (cpalette) LoadCPalette();\r
                        else FCEUI_SetPaletteArray(0); // set to default\r
                        FCEU_ResetPalette();\r
@@ -1006,6 +1098,7 @@ static void draw_menu_options(int menu_sel)
        // draw cursor\r
        gp2x_text_out15(tl_x - 16, tl_y + menu_sel*10, ">");\r
 \r
+       gp2x_fceu_darken_text_bg();\r
        gp2x_video_flip();\r
 }\r
 \r
@@ -1032,7 +1125,7 @@ static void config_commit(void)
 static int menu_loop_options(void)\r
 {\r
        static int menu_sel = 0;\r
-       int menu_sel_max = 15;\r
+       int menu_sel_max = 14;\r
        unsigned long inp = 0;\r
 \r
        if (fceugi) menu_sel_max++;\r
@@ -1121,6 +1214,7 @@ static void draw_menu_credits(void)
        gp2x_text_out15(20, 180, "  cpuctrl, gamma libs");\r
        gp2x_text_out15(20, 190, "Squidge: squidgehack");\r
 \r
+       gp2x_fceu_darken_text_bg();\r
        gp2x_video_flip();\r
 }\r
 \r
@@ -1129,7 +1223,7 @@ static void draw_menu_credits(void)
 \r
 static void draw_menu_root(int menu_sel)\r
 {\r
-       int tl_x = 30, tl_y = 128, y;\r
+       int tl_x = 30, tl_y = 126, y;\r
        gp2x_fceu_copy_bg();\r
 \r
        y = tl_y;\r
@@ -1146,19 +1240,24 @@ static void draw_menu_root(int menu_sel)
        gp2x_text_out15(tl_x, (y+=10), "Controls");\r
        gp2x_text_out15(tl_x, (y+=10), "Credits");\r
        gp2x_text_out15(tl_x, (y+=10), "Exit");\r
-// TODO\r
-//     if (PicoPatches)\r
-//             gp2x_text_out15(tl_x, (y+=10), "Patches / GameGenie");\r
+\r
+       if (cheats)\r
+               gp2x_text_out15(tl_x, (y+=10), "Cheats");\r
 \r
        // draw cursor\r
        gp2x_text_out15(tl_x - 16, tl_y + menu_sel*10, ">");\r
-       // error\r
-       if (menuErrorMsg[0]) gp2x_text_out15(1, 230, menuErrorMsg);\r
+\r
+       gp2x_fceu_darken_text_bg();\r
+       gp2x_fceu_darken_reset();\r
+\r
+       // error / version\r
+       if (menuErrorMsg[0]) gp2x_text_out15(1, 229, menuErrorMsg);\r
        else {\r
                char vstr[16];\r
                sprintf(vstr, "v" GP2X_PORT_VERSION " r%i", GP2X_PORT_REV);\r
-               gp2x_text_out15(320-strlen(vstr)*8-1, 230, vstr);\r
+               gp2x_text_out15(320-strlen(vstr)*8-1, 228, vstr);\r
        }\r
+       gp2x_fceu_darken_text_bg();\r
        gp2x_video_flip();\r
 }\r
 \r
@@ -1170,7 +1269,7 @@ static int menu_loop_root(void)
        unsigned long inp = 0;\r
 \r
        if (fceugi) menu_sel_min = 0;\r
-// TODO        if (PicoPatches) menu_sel_max = 9;\r
+       if (cheats) menu_sel_max = 9;\r
        if (menu_sel < menu_sel_min || menu_sel > menu_sel_max)\r
                menu_sel = menu_sel_min;\r
 \r
@@ -1266,6 +1365,7 @@ static int menu_loop_root(void)
                                case 8: // exit\r
                                        return 1;\r
                                case 9: // patches/gg\r
+                                       patches_menu_loop();\r
                                        break;\r
                        }\r
                }\r
@@ -1274,9 +1374,33 @@ static int menu_loop_root(void)
 }\r
 \r
 \r
+extern unsigned short gp2x_palette16[256];\r
+\r
 static void menu_prepare_bg(void)\r
 {\r
-       // TODO...\r
+       menu_bg = malloc(320*240*2);\r
+       if (menu_bg == NULL) return;\r
+\r
+       if (fceugi)\r
+       {\r
+               /* raw emu frame should now be at gp2x_screen */\r
+               if (Settings.scaling != 0)\r
+               {\r
+                       soft_scale((char *)gp2x_screen + 32, gp2x_palette16, srendline, erendline-srendline);\r
+                       if (srendline)\r
+                               memset32((int *)((char *)gp2x_screen + 32), 0, srendline*320*2/4);\r
+                       memcpy(menu_bg, gp2x_screen + 32, 320*240*2);\r
+               }\r
+               else\r
+               {\r
+                       convert2RGB555(menu_bg, gp2x_screen, gp2x_palette16, 320*240);\r
+               }\r
+       }\r
+       else\r
+       {\r
+               memset32((int *)menu_bg, 0, 320*240*2/4);\r
+               readpng(menu_bg, "background.png");\r
+       }\r
 }\r
 \r
 static void menu_gfx_prepare(void)\r
@@ -1299,6 +1423,8 @@ int gp2x_menu_do(void)
 \r
        ret = menu_loop_root();\r
 \r
+       if (menu_bg) free(menu_bg);\r
+       menu_bg = NULL;\r
        menuErrorMsg[0] = 0;\r
 \r
        return ret;\r