SMB3, combo, fskip fixes
[fceu.git] / drivers / gp2x / input.c
index daadc34..e1046bd 100644 (file)
@@ -18,6 +18,9 @@
 #include "../../state.h"
 #include "../../general.h"
 #include "../../input.h"
+#include "../../svga.h"
+#include "../../video.h"
+#include "usbjoy.h"
 
 /* UsrInputType[] is user-specified.  InputType[] is current
        (game loading can override user settings)
@@ -44,6 +47,13 @@ static void setsoundvol(int soundvolume)
 {
        int soundvolIndex;
        static char soundvolmeter[24];
+       static int prev_snd_on = 0;
+
+       if ((!!soundvolume) ^ prev_snd_on)
+       {
+               FCEUI_Sound(Settings.sound_rate);
+               prev_snd_on = !!soundvolume;
+       }
 
        // draw on screen :D
        gp2x_sound_volume(soundvolume, soundvolume);
@@ -72,14 +82,42 @@ static void do_emu_acts(uint32 acts)
 
        if (acts & (3 << 30))
        {
+               unsigned long keys;
+               int do_it = 1;
                if (acts & (1 << 30))
                {
-                       FCEUI_LoadState();
+                       if (Settings.sstate_confirm & 2)
+                       {
+                               FCEU_DispMessage("LOAD STATE? (Y=yes, X=no)");
+                               FCEU_PutImage();
+                               FCEUD_Update(XBuf+8,NULL,0);
+                               while( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) ) usleep(50*1024);
+                               if (keys & GP2X_X) do_it = 0;
+                               FCEU_CancelDispMessage();
+                       }
+                       if (do_it) FCEUI_LoadState();
                }
                else
                {
-                       FCEUI_SaveState();
+                       if (Settings.sstate_confirm & 1)
+                       {
+                               char *fname = FCEU_MakeFName(FCEUMKF_STATE,CurrentState,0);
+                               FILE *st=fopen(fname,"rb");
+                               free(fname);
+                               if (st)
+                               {
+                                       fclose(st);
+                                       FCEU_DispMessage("OVERWRITE SAVE? (Y=yes, X=no)");
+                                       FCEU_PutImage();
+                                       FCEUD_Update(XBuf+8,NULL,0);
+                                       while( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) ) usleep(50*1024);
+                                       if (keys & GP2X_X) do_it = 0;
+                                       FCEU_CancelDispMessage();
+                               }
+                       }
+                       if (do_it) FCEUI_SaveState();
                }
+               RefreshThrottleFPS();
        }
        else if (acts & (3 << 28)) // state slot next/prev
        {
@@ -153,15 +191,15 @@ static void do_fake_mouse(unsigned long keys)
 }
 
 
-void FCEUD_UpdateInput(void)
+static void FCEUD_UpdateInput(void)
 {
        static int volpushed_frames = 0;
-       static int turbo_rate_cnt_a = 0, turbo_rate_cnt_b = 0;
+       static int turbo_rate_cnt_a[2] = {0,0}, turbo_rate_cnt_b[2] = {0,0};
        unsigned long keys = gp2x_joystick_read(0);
-       uint32 all_acts = 0;
+       uint32 all_acts[2] = {0,0};
        int i;
 
-       if ((down(VOL_DOWN) && down(VOL_UP)) || (keys & (GP2X_L|GP2X_L|GP2X_START)) == (GP2X_L|GP2X_L|GP2X_START))
+       if ((down(VOL_DOWN) && down(VOL_UP)) || (keys & (GP2X_L|GP2X_START)) == (GP2X_L|GP2X_START))
        {
                Exit = 1;
                return;
@@ -213,27 +251,56 @@ void FCEUD_UpdateInput(void)
                                        if ((keys & (1 << u)) && (Settings.KeyBinds[u] & acts))
                                        {
                                                keys &= ~(1 << u);
+                                               acts &= Settings.KeyBinds[u];
                                                break;
                                        }
                                }
                        }
                        if (u != 32) acts &=  combo_acts; // other combo key pressed
                        else         acts &= ~combo_acts;
-                       all_acts |= acts;
+                       all_acts[(acts>>16)&1] |= acts;
                }
        }
 
-       JSreturn |= all_acts & 0xff;
-       if (all_acts & 0x100) {         // A turbo
-               turbo_rate_cnt_a += Settings.turbo_rate_add;
-               JSreturn |= (turbo_rate_cnt_a >> 24) & 1;
+       // add joy inputs
+       if (num_of_joys > 0)
+       {
+               int joy;
+               gp2x_usbjoy_update();
+               for (joy = 0; joy < num_of_joys; joy++) {
+                       int keys = gp2x_usbjoy_check2(joy);
+                       for (i = 0; i < 32; i++) {
+                               if (keys & (1 << i)) {
+                                       int acts = Settings.JoyBinds[joy][i];
+                                       all_acts[(acts>>16)&1] |= acts;
+                               }
+                       }
+               }
        }
-       if (all_acts & 0x200) {         // B turbo
-               turbo_rate_cnt_b += Settings.turbo_rate_add;
-               JSreturn |= (turbo_rate_cnt_b >> 23) & 2;
+
+       // player 1
+       JSreturn |= all_acts[0] & 0xff;
+       if (all_acts[0] & 0x100) {              // A turbo
+               turbo_rate_cnt_a[0] += Settings.turbo_rate_add;
+               JSreturn |= (turbo_rate_cnt_a[0] >> 24) & 1;
+       }
+       if (all_acts[0] & 0x200) {              // B turbo
+               turbo_rate_cnt_b[0] += Settings.turbo_rate_add;
+               JSreturn |= (turbo_rate_cnt_b[0] >> 23) & 2;
+       }
+
+       // player 2
+       JSreturn |= (all_acts[1] & 0xff) << 16;
+       if (all_acts[1] & 0x100) {              // A turbo
+               turbo_rate_cnt_a[1] += Settings.turbo_rate_add;
+               JSreturn |= (turbo_rate_cnt_a[1] >> 8) & 0x10000;
+       }
+       if (all_acts[1] & 0x200) {              // B turbo
+               turbo_rate_cnt_b[1] += Settings.turbo_rate_add;
+               JSreturn |= (turbo_rate_cnt_b[1] >> 7) & 0x20000;
        }
 
-       do_emu_acts(all_acts);
+       do_emu_acts(all_acts[0]|all_acts[1]);
 }
 
 
@@ -284,35 +351,42 @@ static void InitOtherInput(void)
 
 static void PrepareOtherInput(void)
 {
-       uint32 act, key, seen_acts;
+       uint32 act;
 
-       combo_acts = combo_keys = prev_emu_acts = seen_acts = 0;
+       combo_acts = combo_keys = prev_emu_acts = 0;
 
-       // find combo_acts
-       for (act = 1; act; act <<= 1)
+       for (act = 0; act < 32; act++)
        {
-               for (key = 1; key < 32; key++)
+               int u, keyc = 0, keyc2 = 0;
+               if (act == 16 || act == 17) continue; // player2 flag
+               if (act > 17)
                {
-                       if (Settings.KeyBinds[key] & act)
-                       {
-                               if (seen_acts & act) combo_acts |= act;
-                               else seen_acts |= act;
-                       }
+                       for (u = 0; u < 32; u++)
+                               if (Settings.KeyBinds[u] & (1 << act)) keyc++;
                }
-       }
-
-       // find combo_keys
-       for (act = 1; act; act <<= 1)
-       {
-               for (key = 0; key < 32; key++)
+               else
                {
-                       if (Settings.KeyBinds[key] & combo_acts)
+                       for (u = 0; u < 32; u++)
+                               if ((Settings.KeyBinds[u] & 0x30000) == 0 && // pl. 1
+                                       (Settings.KeyBinds[u] & (1 << act))) keyc++;
+                       for (u = 0; u < 32; u++)
+                               if ((Settings.KeyBinds[u] & 0x30000) == 1 && // pl. 2
+                                       (Settings.KeyBinds[u] & (1 << act))) keyc2++;
+                       if (keyc2 > keyc) keyc = keyc2;
+               }
+               if (keyc > 1)
+               {
+                       // loop again and mark those keys and actions as combo
+                       for (u = 0; u < 32; u++)
                        {
-                               combo_keys |= 1 << key;
+                               if (Settings.KeyBinds[u] & (1 << act)) {
+                                       combo_keys |= 1 << u;
+                                       combo_acts |= 1 << act;
+                               }
                        }
                }
        }
 
-       printf("generated combo_acts: %08x, combo_keys: %08x\n", combo_acts, combo_keys);
+       // printf("generated combo_acts: %08x, combo_keys: %08x\n", combo_acts, combo_keys);
 }