newstyle key config, code940.bin -> pico940.bin
[libpicofe.git] / gp2x / menu.c
index 8de891b..9b51d7c 100644 (file)
@@ -1,4 +1,4 @@
-// (c) Copyright 2006 notaz, All rights reserved.\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
@@ -26,7 +26,6 @@
 #error "need d_type for file browser\r
 #endif\r
 \r
-extern char *actionNames[];\r
 extern char romFileName[PATH_MAX];\r
 extern char *rom_data;\r
 extern int  mmuhack_status;\r
@@ -817,17 +816,17 @@ static char *usb_joy_key_name(int joy, int num)
        return name;\r
 }\r
 \r
-static void draw_key_config(int curr_act, int is_p2)\r
+static char *action_binds(int player_idx, int action_mask)\r
 {\r
-       char strkeys[32*5];\r
+       static char strkeys[32*5];\r
        int joy, i;\r
 \r
        strkeys[0] = 0;\r
-       for (i = 0; i < 32; i++)\r
+       for (i = 0; i < 32; i++) // i is key index\r
        {\r
-               if (currentConfig.KeyBinds[i] & (1 << curr_act))\r
+               if (currentConfig.KeyBinds[i] & action_mask)\r
                {\r
-                       if (curr_act < 16 && (currentConfig.KeyBinds[i] & (1 << 16)) != (is_p2 << 16)) continue;\r
+                       if (player_idx >= 0 && ((currentConfig.KeyBinds[i] >> 16) & 3) != player_idx) continue;\r
                        if (strkeys[0]) { strcat(strkeys, " + "); strcat(strkeys, gp2xKeyNames[i]); break; }\r
                        else strcpy(strkeys, gp2xKeyNames[i]);\r
                }\r
@@ -836,9 +835,9 @@ static void draw_key_config(int curr_act, int is_p2)
        {\r
                for (i = 0; i < 32; i++)\r
                {\r
-                       if (currentConfig.JoyBinds[joy][i] & (1 << curr_act))\r
+                       if (currentConfig.JoyBinds[joy][i] & action_mask)\r
                        {\r
-                               if (curr_act < 16 && (currentConfig.JoyBinds[joy][i] & (1 << 16)) != (is_p2 << 16)) continue;\r
+                               if (player_idx >= 0 && ((currentConfig.JoyBinds[joy][i] >> 16) & 3) != player_idx) continue;\r
                                if (strkeys[0]) {\r
                                        strcat(strkeys, ", "); strcat(strkeys, usb_joy_key_name(joy + 1, i));\r
                                        break;\r
@@ -848,51 +847,122 @@ static void draw_key_config(int curr_act, int is_p2)
                }\r
        }\r
 \r
-       //memset(gp2x_screen, 0, 320*240);\r
+       return strkeys;\r
+}\r
+\r
+static void unbind_action(int action)\r
+{\r
+       int i, u;\r
+\r
+       for (i = 0; i < 32; i++)\r
+               currentConfig.KeyBinds[i] &= ~action;\r
+       for (u = 0; u < 4; u++)\r
+               for (i = 0; i < 32; i++)\r
+                       currentConfig.JoyBinds[u][i] &= ~action;\r
+}\r
+\r
+static int count_bound_keys(int action, int joy)\r
+{\r
+       int i, keys = 0;\r
+\r
+       if (joy)\r
+       {\r
+               for (i = 0; i < 32; i++)\r
+                       if (currentConfig.JoyBinds[joy-1][i] & action) keys++;\r
+       }\r
+       else\r
+       {\r
+               for (i = 0; i < 32; i++)\r
+                       if (currentConfig.KeyBinds[i] & action) keys++;\r
+       }\r
+       return keys;\r
+}\r
+\r
+typedef struct { char *name; int mask; } bind_action_t;\r
+\r
+static void draw_key_config(const bind_action_t *opts, int opt_cnt, int player_idx, int sel)\r
+{\r
+       int x, y, tl_y = 40, i;\r
+\r
        gp2x_pd_clone_buffer2();\r
-       gp2x_text_out8(60, 40, "Action: %s", actionNames[curr_act]);\r
-       gp2x_text_out8(60, 60, "Keys: %s", strkeys);\r
+       if (player_idx >= 0) {\r
+               gp2x_text_out8(80, 20, "Player %i controls", player_idx + 1);\r
+               x = 100;\r
+       } else {\r
+               gp2x_text_out8(80, 20, "Emulator controls");\r
+               x = 40;\r
+       }\r
+\r
+       y = tl_y;\r
+       for (i = 0; i < opt_cnt; i++, y+=10)\r
+               gp2x_text_out8(x, y, "%s : %s", opts[i].name, action_binds(player_idx, opts[i].mask));\r
+\r
+       gp2x_text_out8(x, y, "Done");\r
 \r
-       gp2x_text_out8(30, 180, "Use SELECT to change action");\r
-       gp2x_text_out8(30, 190, "Press a key to bind/unbind");\r
-       gp2x_text_out8(30, 200, "Select \"Done\" action and");\r
-       gp2x_text_out8(30, 210, "  press any key to finish");\r
+       // draw cursor\r
+       gp2x_text_out8(x - 16, tl_y + sel*10, ">");\r
+\r
+       if (sel < opt_cnt) {\r
+               gp2x_text_out8(30, 190, "Press a button to bind/unbind");\r
+               gp2x_text_out8(30, 200, "Use SELECT to clear");\r
+               gp2x_text_out8(30, 210, "To bind UP/DOWN, hold SELECT");\r
+               gp2x_text_out8(30, 220, "Select \"Done\" to exit");\r
+       } else {\r
+               gp2x_text_out8(30, 200, "Use Options -> Save cfg");\r
+               gp2x_text_out8(30, 210, "to save controls");\r
+               gp2x_text_out8(30, 220, "Press B or X to exit");\r
+       }\r
        gp2x_video_flip2();\r
 }\r
 \r
-static void key_config_loop(int is_p2)\r
+static void key_config_loop(const bind_action_t *opts, int opt_cnt, int player_idx)\r
 {\r
-       int curr_act = 0, joy = 0, i;\r
+       int joy = 0, sel = 0, menu_sel_max = opt_cnt, prev_select = 0, i;\r
        unsigned long inp = 0;\r
 \r
        for (;;)\r
        {\r
-               draw_key_config(curr_act, is_p2);\r
+               draw_key_config(opts, opt_cnt, player_idx, sel);\r
                inp = wait_for_input_usbjoy(CONFIGURABLE_KEYS, &joy);\r
                // printf("got %08lX from joy %i\n", inp, joy);\r
                if (joy == 0) {\r
-                       if (inp & GP2X_SELECT) {\r
-                               curr_act++;\r
-                               while (!actionNames[curr_act] && curr_act < 32) curr_act++;\r
-                               if (curr_act > 31) curr_act = 0;\r
+                       if (!(inp & GP2X_SELECT)) {\r
+                               prev_select = 0;\r
+                               if(inp & GP2X_UP  ) { sel--; if (sel < 0) sel = menu_sel_max; continue; }\r
+                               if(inp & GP2X_DOWN) { sel++; if (sel > menu_sel_max) sel = 0; continue; }\r
                        }\r
+                       if (sel >= opt_cnt) {\r
+                               if (inp & (GP2X_B|GP2X_X)) break;\r
+                               else continue;\r
+                       }\r
+                       // if we are here, we want to bind/unbind something\r
+                       if ((inp & GP2X_SELECT) && !prev_select)\r
+                               unbind_action(opts[sel].mask);\r
+                       prev_select = inp & GP2X_SELECT;\r
                        inp &= CONFIGURABLE_KEYS;\r
                        inp &= ~GP2X_SELECT;\r
-               }\r
-               if (curr_act == 31 && inp) break;\r
-               if (joy == 0) {\r
                        for (i = 0; i < 32; i++)\r
                                if (inp & (1 << i)) {\r
-                                       currentConfig.KeyBinds[i] ^= (1 << curr_act);\r
-                                       if (is_p2) currentConfig.KeyBinds[i] |=  (1 << 16); // player 2 flag\r
-                                       else       currentConfig.KeyBinds[i] &= ~(1 << 16);\r
+                                       if (count_bound_keys(opts[sel].mask, 0) >= 2)\r
+                                            currentConfig.KeyBinds[i] &= ~opts[sel].mask; // allow to unbind only\r
+                                       else currentConfig.KeyBinds[i] ^=  opts[sel].mask;\r
+                                       if (player_idx >= 0) {\r
+                                               currentConfig.KeyBinds[i] &= ~(3 << 16);\r
+                                               currentConfig.KeyBinds[i] |= player_idx << 16;\r
+                                       }\r
                                }\r
-               } else {\r
+               }\r
+               else if (sel < opt_cnt)\r
+               {\r
                        for (i = 0; i < 32; i++)\r
                                if (inp & (1 << i)) {\r
-                                       currentConfig.JoyBinds[joy-1][i] ^= (1 << curr_act);\r
-                                       if (is_p2) currentConfig.JoyBinds[joy-1][i] |=  (1 << 16);\r
-                                       else       currentConfig.JoyBinds[joy-1][i] &= ~(1 << 16);\r
+                                       if (count_bound_keys(opts[sel].mask, joy) >= 1) // disallow combos for usbjoy\r
+                                            currentConfig.JoyBinds[joy-1][i] &= ~opts[sel].mask;\r
+                                       else currentConfig.JoyBinds[joy-1][i] ^=  opts[sel].mask;\r
+                                       if (player_idx >= 0) {\r
+                                               currentConfig.JoyBinds[joy-1][i] &= ~(3 << 16);\r
+                                               currentConfig.JoyBinds[joy-1][i] |= player_idx << 16;\r
+                                       }\r
                                }\r
                }\r
        }\r
@@ -904,10 +974,10 @@ static void draw_kc_sel(int menu_sel)
        char joyname[36];\r
 \r
        y = tl_y;\r
-       //memset(gp2x_screen, 0, 320*240);\r
        gp2x_pd_clone_buffer2();\r
        gp2x_text_out8(tl_x, y,       "Player 1");\r
        gp2x_text_out8(tl_x, (y+=10), "Player 2");\r
+       gp2x_text_out8(tl_x, (y+=10), "Emulator controls");\r
        gp2x_text_out8(tl_x, (y+=10), "Done");\r
 \r
        // draw cursor\r
@@ -924,34 +994,69 @@ static void draw_kc_sel(int menu_sel)
                gp2x_text_out8(tl_x, (y+=10), "none");\r
        }\r
 \r
-\r
        gp2x_video_flip2();\r
 }\r
 \r
+\r
+// PicoPad[] format: MXYZ SACB RLDU\r
+static bind_action_t ctrl_actions[] =\r
+{\r
+       { "UP     ", 0x001 },\r
+       { "DOWN   ", 0x002 },\r
+       { "LEFT   ", 0x004 },\r
+       { "RIGHT  ", 0x008 },\r
+       { "A      ", 0x040 },\r
+       { "B      ", 0x010 },\r
+       { "C      ", 0x020 },\r
+       { "START  ", 0x080 },\r
+       { "MODE   ", 0x800 },\r
+       { "X      ", 0x400 },\r
+       { "Y      ", 0x200 },\r
+       { "Z      ", 0x100 },\r
+};\r
+\r
+// player2_flag, ?, ?, ?, ?, ?, ?, menu\r
+// "NEXT SAVE SLOT", "PREV SAVE SLOT", "SWITCH RENDERER", "SAVE STATE",\r
+// "LOAD STATE", "VOLUME UP", "VOLUME DOWN", "DONE"\r
+static bind_action_t emuctrl_actions[] =\r
+{\r
+       { "Volume Down    ", 1<<30 },\r
+       { "Volume Up      ", 1<<29 },\r
+       { "Load State     ", 1<<28 },\r
+       { "Save State     ", 1<<27 },\r
+       { "Switch Renderer", 1<<26 },\r
+       { "Prev Save Slot ", 1<<25 },\r
+       { "Next Save Slot ", 1<<24 },\r
+       { "Enter Menu     ", 1<<23 },\r
+};\r
+\r
 static void kc_sel_loop(void)\r
 {\r
-       int menu_sel = 2, menu_sel_max = 2;\r
+       int menu_sel = 3, menu_sel_max = 3;\r
        unsigned long inp = 0;\r
+       int is_6button = currentConfig.PicoOpt & 0x020;\r
 \r
-       for(;;)\r
+       while (1)\r
        {\r
                draw_kc_sel(menu_sel);\r
                inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X);\r
-               if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
-               if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
-               if(inp & GP2X_B) {\r
+               if (inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
+               if (inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
+               if (inp & GP2X_B) {\r
                        switch (menu_sel) {\r
-                               case 0: key_config_loop(0); return;\r
-                               case 1: key_config_loop(1); return;\r
+                               case 0: key_config_loop(ctrl_actions, is_6button ? 12 : 8, 0); return;\r
+                               case 1: key_config_loop(ctrl_actions, is_6button ? 12 : 8, 1); return;\r
+                               case 2: key_config_loop(emuctrl_actions,\r
+                                               sizeof(emuctrl_actions)/sizeof(emuctrl_actions[0]), -1); return;\r
+                               case 3: if (rom_data == NULL) emu_WriteConfig(0); return;\r
                                default: return;\r
                        }\r
                }\r
-               if(inp & GP2X_X) return;\r
+               if (inp & GP2X_X) return;\r
        }\r
 }\r
 \r
 \r
-\r
 // --------- sega/mega cd options ----------\r
 \r
 menu_entry cdopt_entries[] =\r
@@ -1359,11 +1464,9 @@ static void menu_options_save(void)
        PicoOpt = currentConfig.PicoOpt;\r
        PsndRate = currentConfig.PsndRate;\r
        PicoRegionOverride = currentConfig.PicoRegion;\r
-       if (PicoOpt & 0x20) {\r
-               actionNames[ 8] = "Z"; actionNames[ 9] = "Y";\r
-               actionNames[10] = "X"; actionNames[11] = "MODE";\r
-       } else {\r
-               actionNames[8] = actionNames[9] = actionNames[10] = actionNames[11] = 0;\r
+       if (!(PicoOpt & 0x20)) {\r
+               // unbind XYZ MODE, just in case\r
+               unbind_action(0xf00);\r
        }\r
        scaling_update();\r
 }\r
@@ -1382,6 +1485,7 @@ static int menu_loop_options(void)
        me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_SAVECFG_GAME, rom_data != NULL);\r
        me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_LOADCFG, config_slot != config_slot_current);\r
        menu_sel_max = me_count_enabled(opt_entries, OPT_ENTRY_COUNT) - 1;\r
+       if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;\r
 \r
        while (1)\r
        {\r
@@ -1477,9 +1581,9 @@ static int menu_loop_options(void)
                                                else strcpy(menuErrorMsg, "failed to write config");\r
                                                return 1;\r
                                        case MA_OPT_LOADCFG:\r
-                                               ret = emu_ReadConfig(1);\r
-                                               if (!ret) ret = emu_ReadConfig(0);\r
-                                               if (!ret) strcpy(menuErrorMsg, "config loaded");\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
                                                return 1;\r
                                        default:\r
@@ -1585,6 +1689,7 @@ static void menu_loop_root(void)
        me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_PATCHES,     PicoPatches != NULL);\r
 \r
        menu_sel_max = me_count_enabled(main_entries, MAIN_ENTRY_COUNT) - 1;\r
+       if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;\r
 \r
        /* make sure action buttons are not pressed on entering menu */\r
        draw_menu_root(menu_sel);\r