added support for special media buttons on Amazon Fire TV remote control
[rocksndiamonds.git] / src / events.c
index a870bf7433f4156337d627d82632ecf2ebf53a14..2472fdf5f8978b048af638730299206544445255 100644 (file)
@@ -354,6 +354,7 @@ void ClearEventQueue()
 
 #if defined(TARGET_SDL2)
       case SDL_CONTROLLERBUTTONUP:
 
 #if defined(TARGET_SDL2)
       case SDL_CONTROLLERBUTTONUP:
+       HandleJoystickEvent(&event);
        ClearPlayerAction();
        break;
 #endif
        ClearPlayerAction();
        break;
 #endif
@@ -399,6 +400,7 @@ void SleepWhileUnmapped()
 
 #if defined(TARGET_SDL2)
       case SDL_CONTROLLERBUTTONUP:
 
 #if defined(TARGET_SDL2)
       case SDL_CONTROLLERBUTTONUP:
+       HandleJoystickEvent(&event);
        key_joystick_mapping = 0;
        break;
 #endif
        key_joystick_mapping = 0;
        break;
 #endif
@@ -1523,6 +1525,14 @@ void HandleKey(Key key, int key_status)
   int joy = 0;
   int i;
 
   int joy = 0;
   int i;
 
+#if defined(TARGET_SDL2)
+  /* map special "play/pause" media key to default key for play/pause actions */
+  if (key == KSYM_PlayPause)
+    key = KSYM_space;
+#endif
+
+  HandleSpecialGameControllerKeys(key, key_status);
+
   if (game_status == GAME_MODE_PLAYING)
   {
     /* only needed for single-step tape recording mode */
   if (game_status == GAME_MODE_PLAYING)
   {
     /* only needed for single-step tape recording mode */
@@ -2010,10 +2020,23 @@ void HandleJoystick()
     case GAME_MODE_INFO:
     {
       static unsigned int joystickmove_delay = 0;
     case GAME_MODE_INFO:
     {
       static unsigned int joystickmove_delay = 0;
+      static unsigned int joystickmove_delay_value = GADGET_FRAME_DELAY;
+      static int joystick_last = 0;
 
       if (joystick && !button &&
 
       if (joystick && !button &&
-         !DelayReached(&joystickmove_delay, GADGET_FRAME_DELAY))
+         !DelayReached(&joystickmove_delay, joystickmove_delay_value))
+      {
+       /* delay joystick actions if buttons/axes continually pressed */
        newbutton = dx = dy = 0;
        newbutton = dx = dy = 0;
+      }
+      else
+      {
+       /* start with longer delay, then continue with shorter delay */
+       if (joystick != joystick_last)
+         joystickmove_delay_value = GADGET_FRAME_DELAY_FIRST;
+       else
+         joystickmove_delay_value = GADGET_FRAME_DELAY;
+      }
 
       if (game_status == GAME_MODE_TITLE)
        HandleTitleScreen(0,0,dx,dy, newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
 
       if (game_status == GAME_MODE_TITLE)
        HandleTitleScreen(0,0,dx,dy, newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
@@ -2027,6 +2050,9 @@ void HandleJoystick()
        HandleSetupScreen(0,0,dx,dy, newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
       else if (game_status == GAME_MODE_INFO)
        HandleInfoScreen(0,0,dx,dy, newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
        HandleSetupScreen(0,0,dx,dy, newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
       else if (game_status == GAME_MODE_INFO)
        HandleInfoScreen(0,0,dx,dy, newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
+
+      joystick_last = joystick;
+
       break;
     }
 
       break;
     }
 
@@ -2081,3 +2107,33 @@ void HandleSpecialGameControllerButtons(Event *event)
   }
 #endif
 }
   }
 #endif
 }
+
+void HandleSpecialGameControllerKeys(Key key, int key_status)
+{
+#if defined(TARGET_SDL2)
+#if defined(KSYM_Rewind) && defined(KSYM_FastForward)
+  int button = SDL_CONTROLLER_BUTTON_INVALID;
+
+  /* map keys to joystick buttons (special hack for Amazon Fire TV remote) */
+  if (key == KSYM_Rewind)
+    button = SDL_CONTROLLER_BUTTON_A;
+  else if (key == KSYM_FastForward || key == KSYM_Menu)
+    button = SDL_CONTROLLER_BUTTON_B;
+
+  if (button != SDL_CONTROLLER_BUTTON_INVALID)
+  {
+    Event event;
+
+    event.type = (key_status == KEY_PRESSED ? SDL_CONTROLLERBUTTONDOWN :
+                 SDL_CONTROLLERBUTTONUP);
+
+    event.cbutton.which = 0;   /* first joystick (Amazon Fire TV remote) */
+    event.cbutton.button = button;
+    event.cbutton.state = (key_status == KEY_PRESSED ? SDL_PRESSED :
+                          SDL_RELEASED);
+
+    HandleJoystickEvent(&event);
+  }
+#endif
+#endif
+}