raspberry pi port
[gpsp.git] / input.c
diff --git a/input.c b/input.c
index 94c561d..f01c53d 100644 (file)
--- a/input.c
+++ b/input.c
@@ -24,7 +24,6 @@
 void trigger_key(u32 key)
 {
   u32 p1_cnt = io_registers[REG_P1CNT];
-  u32 p1;
 
   if((p1_cnt >> 14) & 0x01)
   {
@@ -347,40 +346,14 @@ void init_input()
 #endif
 
 
-#ifdef GP2X_BUILD
+#if defined(GP2X_BUILD) || defined(PND_BUILD)
 
-// GP2X SDL requires a user made input method
-#include <sys/mman.h>
-#include <sys/ioctl.h>
-#include "gp2x/gp2x.h"
-
-u32 gamepad_config_map[16] =
-{
-  BUTTON_ID_UP,                 // Up
-  BUTTON_ID_LEFT,               // Left
-  BUTTON_ID_DOWN,               // Down
-  BUTTON_ID_RIGHT,              // Right
-  BUTTON_ID_START,              // Start
-  BUTTON_ID_SELECT,             // Select
-  BUTTON_ID_L,                  // Ltrigger
-  BUTTON_ID_R,                  // Rtrigger
-  BUTTON_ID_FPS,                // A
-  BUTTON_ID_A,                  // B
-  BUTTON_ID_B,                  // X
-  BUTTON_ID_MENU,               // Y
-  BUTTON_ID_VOLDOWN,            // Vol down
-  BUTTON_ID_VOLUP,              // Vol up
-  BUTTON_ID_FPS,                // Push
-  BUTTON_ID_MENU                // Vol middle
-};
-
-extern u32 gp2x_fps_debug;
-extern u32 gpsp_gp2x_joystick_read(void);
+extern u32 fps_debug;
 
 gui_action_type get_gui_input()
 {
   gui_action_type new_button = CURSOR_NONE;
-  u32 buttons = gpsp_gp2x_joystick_read();
+  u32 buttons = gpsp_plat_joystick_read();
   u32 new_buttons;
 
   static u32 last_buttons = 0;
@@ -391,34 +364,7 @@ gui_action_type get_gui_input()
   new_buttons = (last_buttons ^ buttons) & buttons;
   last_buttons = buttons;
 
-  if(new_buttons & GP2X_A)
-    new_button = CURSOR_BACK;
-
-  if(new_buttons & GP2X_X)
-    new_button = CURSOR_EXIT;
-
-  if(new_buttons & GP2X_B)
-    new_button = CURSOR_SELECT;
-
-  if(new_buttons & GP2X_UP)
-    new_button = CURSOR_UP;
-
-  if(new_buttons & GP2X_DOWN)
-    new_button = CURSOR_DOWN;
-
-  if(new_buttons & GP2X_LEFT)
-    new_button = CURSOR_LEFT;
-
-  if(new_buttons & GP2X_RIGHT)
-    new_button = CURSOR_RIGHT;
-
-  if(new_buttons & GP2X_L)
-    new_button = CURSOR_L;
-
-  if(new_buttons & GP2X_R)
-    new_button = CURSOR_R;
-
-
+  new_button = gpsp_plat_buttons_to_cursor(new_buttons);
   if(new_button != CURSOR_NONE)
   {
     get_ticks_us(&button_repeat_timestamp);
@@ -459,28 +405,6 @@ gui_action_type get_gui_input()
   return new_button;
 }
 
-#define GP2X_VOL_MIDDLE (1 << 24)
-
-u32 button_gp2x_mask_to_config[] =
-{
-  GP2X_UP,
-  GP2X_LEFT,
-  GP2X_DOWN,
-  GP2X_RIGHT,
-  GP2X_START,
-  GP2X_SELECT,
-  GP2X_L,
-  GP2X_R,
-  GP2X_A,
-  GP2X_B,
-  GP2X_X,
-  GP2X_Y,
-  GP2X_VOL_DOWN,
-  GP2X_VOL_UP,
-  GP2X_PUSH,
-  GP2X_VOL_MIDDLE
-};
-
 u32 button_id_to_gba_mask[] =
 {
   BUTTON_UP,
@@ -506,9 +430,10 @@ u32 update_input()
   u32 handled_buttons;
   u32 button_id;
   u32 new_key = 0;
-  u32 buttons = gpsp_gp2x_joystick_read();
+  u32 buttons = gpsp_plat_joystick_read();
   u32 i;
 
+#ifdef GP2X_BUILD
   if((buttons & GP2X_VOL_DOWN) && (buttons & GP2X_VOL_UP))
   {
     buttons &= ~(GP2X_VOL_DOWN | GP2X_VOL_UP);
@@ -522,12 +447,15 @@ u32 update_input()
     buttons |= GP2X_VOL_MIDDLE;
   }
 
-  handled_buttons = ((last_buttons ^ buttons) | GP2X_VOL_DOWN | GP2X_VOL_UP) & buttons;
+  last_buttons &= ~(GP2X_VOL_DOWN | GP2X_VOL_UP);
+#endif
+
+  handled_buttons = (last_buttons ^ buttons) & buttons;
   last_buttons = buttons;
 
-  for(i = 0; i < 16; i++)
+  for(i = 0; i < PLAT_BUTTON_COUNT; i++)
   {
-    if(handled_buttons & button_gp2x_mask_to_config[i])
+    if(handled_buttons & button_plat_mask_to_config[i])
       button_id = gamepad_config_map[i];
     else
       button_id = BUTTON_ID_NONE;
@@ -545,7 +473,7 @@ u32 update_input()
 
       case BUTTON_ID_LOADSTATE:
       {
-        u8 current_savestate_filename[512];
+        char current_savestate_filename[512];
         get_savestate_filename_noshot(savestate_slot,
          current_savestate_filename);
         load_state(current_savestate_filename);
@@ -554,7 +482,7 @@ u32 update_input()
 
       case BUTTON_ID_SAVESTATE:
       {
-        u8 current_savestate_filename[512];
+        char current_savestate_filename[512];
         u16 *current_screen = copy_screen();
         get_savestate_filename_noshot(savestate_slot,
          current_savestate_filename);
@@ -564,10 +492,10 @@ u32 update_input()
       }
 
       case BUTTON_ID_FASTFORWARD:
-        print_string("FASTFORWARD", 0xFFFF, 0x0000, 0, 50);
         synchronize_flag ^= 1;
         return 0;
 
+#ifdef GP2X_BUILD
       case BUTTON_ID_VOLUP:
         gp2x_sound_volume(1);
         break;
@@ -575,13 +503,14 @@ u32 update_input()
       case BUTTON_ID_VOLDOWN:
         gp2x_sound_volume(0);
         break;
+#endif
 
       case BUTTON_ID_FPS:
-        gp2x_fps_debug ^= 1;
+        fps_debug ^= 1;
         break;
     }
 
-    if(buttons & button_gp2x_mask_to_config[i])
+    if(buttons & button_plat_mask_to_config[i])
     {
       button_id = gamepad_config_map[i];
       if(button_id < BUTTON_ID_MENU)
@@ -626,8 +555,49 @@ void init_input()
 #endif
 
 
+#if defined(RPI_BUILD)
 
-#ifdef PC_BUILD
+u32 key_map(SDLKey key_sym)
+{
+  switch(key_sym)
+  {
+    case SDLK_a:
+      return BUTTON_L;
+
+    case SDLK_s:
+      return BUTTON_R;
+
+    case SDLK_DOWN:
+      return BUTTON_DOWN;
+
+    case SDLK_UP:
+      return BUTTON_UP;
+
+    case SDLK_LEFT:
+      return BUTTON_LEFT;
+
+    case SDLK_RIGHT:
+      return BUTTON_RIGHT;
+
+    case SDLK_RETURN:
+      return BUTTON_START;
+
+    case SDLK_BACKSPACE:
+      return BUTTON_SELECT;
+
+    case SDLK_x:
+      return BUTTON_B;
+
+    case SDLK_z:
+      return BUTTON_A;
+
+    default:
+      return BUTTON_NONE;
+  }
+}
+#endif
+
+#if defined(PC_BUILD)
 
 u32 key_map(SDLKey key_sym)
 {
@@ -667,6 +637,8 @@ u32 key_map(SDLKey key_sym)
       return BUTTON_NONE;
   }
 }
+#endif
+#if defined(PC_BUILD) || defined(RPI_BUILD)
 
 u32 joy_map(u32 button)
 {
@@ -678,16 +650,16 @@ u32 joy_map(u32 button)
     case 5:
       return BUTTON_R;
 
-    case 9:
+    case 2:
       return BUTTON_START;
 
-    case 8:
+    case 3:
       return BUTTON_SELECT;
 
-    case 0:
+    case 1:
       return BUTTON_B;
 
-    case 1:
+    case 0:
       return BUTTON_A;
 
     default:
@@ -703,7 +675,7 @@ gui_action_type get_gui_input()
   delay_us(30000);
 
   while(SDL_PollEvent(&event))
-  {
+  { 
     switch(event.type)
     {
       case SDL_QUIT:
@@ -740,12 +712,49 @@ gui_action_type get_gui_input()
           case SDLK_BACKSPACE:
             gui_action = CURSOR_BACK;
             break;
-        }
-        break;
+        default:
+           break;
       }
     }
-  }
+    break;
+#ifdef RPI_BUILD
+    case SDL_JOYBUTTONDOWN:
+    {
+      switch (event.jbutton.button)
+      {
+       case 2:
+            gui_action = CURSOR_BACK;
+            break;
+
+       case 1:
+            gui_action = CURSOR_EXIT;
+            break;
 
+       case 0:
+           gui_action = CURSOR_SELECT;
+           break;
+       }
+     }
+     break;
+
+     case SDL_JOYAXISMOTION:
+     {
+         if (event.jaxis.axis==0) { //Left-Right
+            if (event.jaxis.value < -3200) gui_action = CURSOR_LEFT;
+               else if (event.jaxis.value > 3200) gui_action = CURSOR_RIGHT;
+         }
+         if (event.jaxis.axis==1) {  //Up-Down
+            if (event.jaxis.value < -3200) gui_action = CURSOR_UP;
+               else if (event.jaxis.value > 3200) gui_action = CURSOR_DOWN;
+         }
+    }
+    break;
+
+#endif
+    default:
+        break;
+    }
+  }
   return gui_action;
 }
 
@@ -766,8 +775,11 @@ u32 update_input()
         {
           quit();
         }
-
+#ifdef PC_BUILD
         if(event.key.keysym.sym == SDLK_BACKSPACE)
+#else
+        if(event.key.keysym.sym == SDLK_F10)
+#endif
         {
           u16 *screen_copy = copy_screen();
           u32 ret_val = menu(screen_copy);
@@ -776,7 +788,7 @@ u32 update_input()
           return ret_val;
         }
         else
-
+#ifdef PC_BUILD
         if(event.key.keysym.sym == SDLK_F1)
         {
           debug_on();
@@ -809,10 +821,10 @@ u32 update_input()
           dump_translation_cache();
         }
         else
-
+#endif
         if(event.key.keysym.sym == SDLK_F5)
         {
-          u8 current_savestate_filename[512];
+          char current_savestate_filename[512];
           u16 *current_screen = copy_screen();
           get_savestate_filename_noshot(savestate_slot,
            current_savestate_filename);
@@ -823,7 +835,7 @@ u32 update_input()
 
         if(event.key.keysym.sym == SDLK_F7)
         {
-          u8 current_savestate_filename[512];
+          char current_savestate_filename[512];
           get_savestate_filename_noshot(savestate_slot,
            current_savestate_filename);
           load_state(current_savestate_filename);
@@ -863,6 +875,22 @@ u32 update_input()
         key &= ~(joy_map(event.jbutton.button));
         break;
       }
+#ifdef RPI_BUILD
+      case SDL_JOYAXISMOTION:
+      {
+         if (event.jaxis.axis==0) { //Left-Right
+            key &= ~(BUTTON_LEFT|BUTTON_RIGHT);
+         if (event.jaxis.value < -3200)  key |= BUTTON_LEFT;
+           else if (event.jaxis.value > 3200)  key |= BUTTON_RIGHT;
+       } 
+         if (event.jaxis.axis==1) {  //Up-Down
+            key &= ~(BUTTON_UP|BUTTON_DOWN);
+         if (event.jaxis.value < -3200)  key |= BUTTON_UP;
+           else if (event.jaxis.value > 3200)  key |= BUTTON_DOWN;
+       }
+       break;
+#endif
+      }
     }
   }