Browse Source

keyrepeat in SDL2 is non-customizable, so use PAL_GetKeyboardState() instead

Wei Mingzhi 2 years ago
parent
commit
42b6b53958
7 changed files with 115 additions and 122 deletions
  1. 107 106
      input.c
  2. 0 5
      input.h
  3. 0 1
      macos/pal_config.h
  4. 6 6
      palette.c
  5. 0 1
      unix/pal_config.h
  6. 2 2
      util.c
  7. 0 1
      win32/pal_config.h

+ 107 - 106
input.c

@@ -40,7 +40,9 @@ static SDL_Joystick     *g_pJoy = NULL;
 # define SDLK_KP_9     SDLK_KP9
 # define SDLK_KP_0     SDLK_KP0
 
-# define SDL_JoystickNameForIndex SDL_JoystickName
+# define SDL_JoystickNameForIndex    SDL_JoystickName
+# define SDL_GetKeyboardState        SDL_GetKeyState
+# define SDL_GetScancodeFromKey(x)   (x)
 #endif
 
 BOOL                     g_fUseJoystick = TRUE;
@@ -86,41 +88,10 @@ static const int g_KeyMap[][2] = {
    { SDLK_f,         kKeyForce }
 };
 
-static INT
-PAL_ConvertKey(
-   INT      keySym
-)
-/*++
-  Purpose:
-
-    Convert SDL key code to our internal key code.
-
-  Parameters:
-
-    [IN]  keySym - SDL key code.
-
-  Return value:
-
-    Internal key code.
-
---*/
-{
-   int i;
-
-   for (i = 0; i < sizeof(g_KeyMap) / sizeof(g_KeyMap[0]); i++)
-   {
-      if (g_KeyMap[i][0] == keySym)
-      {
-         return g_KeyMap[i][1];
-      }
-   }
-
-   return kKeyNone;
-}
-
 static VOID
 PAL_KeyDown(
-   INT         key
+   INT         key,
+   BOOL        fRepeat
 )
 /*++
   Purpose:
@@ -140,7 +111,7 @@ PAL_KeyDown(
    switch (key)
    {
    case kKeyUp:
-      if (gpGlobals->fInBattle || g_InputState.dir != kDirNorth)
+      if (g_InputState.dir != kDirNorth && !fRepeat)
       {
          g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
          g_InputState.dir = kDirNorth;
@@ -149,7 +120,7 @@ PAL_KeyDown(
       break;
 
    case kKeyDown:
-      if (gpGlobals->fInBattle || g_InputState.dir != kDirSouth)
+      if (g_InputState.dir != kDirSouth && !fRepeat)
       {
          g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
          g_InputState.dir = kDirSouth;
@@ -158,7 +129,7 @@ PAL_KeyDown(
       break;
 
    case kKeyLeft:
-      if (gpGlobals->fInBattle || g_InputState.dir != kDirWest)
+      if (g_InputState.dir != kDirWest && !fRepeat)
       {
          g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
          g_InputState.dir = kDirWest;
@@ -167,7 +138,7 @@ PAL_KeyDown(
       break;
 
    case kKeyRight:
-      if (gpGlobals->fInBattle || g_InputState.dir != kDirEast)
+      if (g_InputState.dir != kDirEast && !fRepeat)
       {
          g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
          g_InputState.dir = kDirEast;
@@ -203,36 +174,36 @@ PAL_KeyUp(
    switch (key)
    {
    case kKeyUp:
-     if (g_InputState.dir == kDirNorth)
-     {
-        g_InputState.dir = g_InputState.prevdir;
-     }
-     g_InputState.prevdir = kDirUnknown;
-     break;
+      if (g_InputState.dir == kDirNorth)
+      {
+         g_InputState.dir = g_InputState.prevdir;
+      }
+      g_InputState.prevdir = kDirUnknown;
+      break;
 
    case kKeyDown:
-     if (g_InputState.dir == kDirSouth)
-     {
-        g_InputState.dir = g_InputState.prevdir;
-     }
-     g_InputState.prevdir = kDirUnknown;
-     break;
+      if (g_InputState.dir == kDirSouth)
+      {
+         g_InputState.dir = g_InputState.prevdir;
+      }
+      g_InputState.prevdir = kDirUnknown;
+      break;
 
    case kKeyLeft:
-     if (g_InputState.dir == kDirWest)
-     {
-        g_InputState.dir = g_InputState.prevdir;
-     }
-     g_InputState.prevdir = kDirUnknown;
-     break;
+      if (g_InputState.dir == kDirWest)
+      {
+         g_InputState.dir = g_InputState.prevdir;
+      }
+      g_InputState.prevdir = kDirUnknown;
+      break;
 
    case kKeyRight:
-     if (g_InputState.dir == kDirEast)
-     {
-        g_InputState.dir = g_InputState.prevdir;
-     }
-     g_InputState.prevdir = kDirUnknown;
-     break;
+      if (g_InputState.dir == kDirEast)
+      {
+         g_InputState.dir = g_InputState.prevdir;
+      }
+      g_InputState.prevdir = kDirUnknown;
+      break;
 
    default:
       break;
@@ -240,6 +211,51 @@ PAL_KeyUp(
 }
 
 static VOID
+PAL_UpdateKeyboardState(
+   VOID
+)
+/*++
+  Purpose:
+
+    Poll & update keyboard state.
+
+  Parameters:
+
+    None.
+
+  Return value:
+
+    None.
+
+--*/
+{
+   static DWORD   rgdwKeyLastTime[sizeof(g_KeyMap) / sizeof(g_KeyMap[0])] = {0};
+   LPCBYTE        keyState = (LPCBYTE)SDL_GetKeyboardState(NULL);
+   int            i;
+   DWORD          dwCurrentTime = SDL_GetTicks();
+
+   for (i = 0; i < sizeof(g_KeyMap) / sizeof(g_KeyMap[0]); i++)
+   {
+      if (keyState[SDL_GetScancodeFromKey(g_KeyMap[i][0])])
+      {
+         if (dwCurrentTime > rgdwKeyLastTime[i])
+         {
+            PAL_KeyDown(g_KeyMap[i][1], (rgdwKeyLastTime[i] != 0));
+            rgdwKeyLastTime[i] = dwCurrentTime + (rgdwKeyLastTime[i] == 0 ? 200 : 75);
+         }
+      }
+      else
+      {
+         if (rgdwKeyLastTime[i] != 0)
+         {
+            PAL_KeyUp(g_KeyMap[i][1]);
+            rgdwKeyLastTime[i] = 0;
+         }
+      }
+   }
+}
+
+static VOID
 PAL_KeyboardEventFilter(
    const SDL_Event       *lpEvent
 )
@@ -258,9 +274,8 @@ PAL_KeyboardEventFilter(
 
 --*/
 {
-   switch (lpEvent->type)
+   if (lpEvent->type == SDL_KEYDOWN)
    {
-   case SDL_KEYDOWN:
       //
       // Pressed a key
       //
@@ -286,18 +301,6 @@ PAL_KeyboardEventFilter(
       {
          VIDEO_SaveScreenshot();
       }
-      else
-      {
-         PAL_KeyDown(PAL_ConvertKey(lpEvent->key.keysym.sym));
-      }
-      break;
-
-   case SDL_KEYUP:
-      //
-      // Released a key
-      //
-      PAL_KeyUp(PAL_ConvertKey(lpEvent->key.keysym.sym));
-      break;
    }
 }
 
@@ -1020,10 +1023,6 @@ PAL_InitInput(
    }
 #endif
 
-#ifdef PAL_ALLOW_KEYREPEAT
-   SDL_EnableKeyRepeat(120, 75);
-#endif
-
    input_init_filter();
 }
 
@@ -1056,32 +1055,7 @@ PAL_ShutdownInput(
    input_shutdown_filter();
 }
 
-VOID
-PAL_ProcessEvent(
-   VOID
-)
-/*++
-  Purpose:
-
-    Process all events.
-
-  Parameters:
-
-    None.
-
-  Return value:
-
-    None.
-
---*/
-{
-   while (PAL_PollEvent(NULL));
-#ifdef PAL_HAS_TOUCH
-   PAL_TouchRepeatCheck();
-#endif
-}
-
-int
+static int
 PAL_PollEvent(
    SDL_Event *event
 )
@@ -1117,6 +1091,33 @@ PAL_PollEvent(
 }
 
 VOID
+PAL_ProcessEvent(
+   VOID
+)
+/*++
+  Purpose:
+
+    Process all events.
+
+  Parameters:
+
+    None.
+
+  Return value:
+
+    None.
+
+--*/
+{
+   while (PAL_PollEvent(NULL));
+
+   PAL_UpdateKeyboardState();
+#ifdef PAL_HAS_TOUCH
+   PAL_TouchRepeatCheck();
+#endif
+}
+
+VOID
 PAL_RegisterInputFilter(
    void (*init_filter)(),
    int (*event_filter)(const SDL_Event *, volatile PALINPUTSTATE *),

+ 0 - 5
input.h

@@ -77,11 +77,6 @@ PAL_ShutdownInput(
    VOID
 );
 
-int
-PAL_PollEvent(
-   SDL_Event *event
-);
-
 VOID
 PAL_SetTouchBounds(
    DWORD dwScreenWidth,

+ 0 - 1
macos/pal_config.h

@@ -31,7 +31,6 @@
 # endif
 
 # if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
-#  define PAL_ALLOW_KEYREPEAT   1
 #  define PAL_HAS_SDLCD         1
 # endif
 

+ 6 - 6
palette.c

@@ -323,11 +323,11 @@ PAL_SceneFade(
          }
          VIDEO_SetPalette(newpalette);
 
-         while (PAL_PollEvent(NULL));
+         PAL_ProcessEvent();
 
          while (!SDL_TICKS_PASSED(SDL_GetTicks(), time))
          {
-            while (PAL_PollEvent(NULL));
+            PAL_ProcessEvent();
             SDL_Delay(5);
          }
       }
@@ -359,11 +359,11 @@ PAL_SceneFade(
          }
          VIDEO_SetPalette(newpalette);
 
-         while (PAL_PollEvent(NULL));
+         PAL_ProcessEvent();
 
          while (!SDL_TICKS_PASSED(SDL_GetTicks(), time))
          {
-            while (PAL_PollEvent(NULL));
+            PAL_ProcessEvent();
             SDL_Delay(5);
          }
       }
@@ -439,11 +439,11 @@ PAL_PaletteFade(
          VIDEO_UpdateScreen(NULL);
       }
 
-      while (PAL_PollEvent(NULL));
+      PAL_ProcessEvent();
 
       while (!SDL_TICKS_PASSED(SDL_GetTicks(), time))
       {
-         while (PAL_PollEvent(NULL));
+         PAL_ProcessEvent();
          SDL_Delay(5);
       }
    }

+ 0 - 1
unix/pal_config.h

@@ -31,7 +31,6 @@
 # endif
 
 # if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
-#  define PAL_ALLOW_KEYREPEAT   1
 #  define PAL_HAS_SDLCD         1
 # endif
 

+ 2 - 2
util.c

@@ -288,12 +288,12 @@ UTIL_Delay(
 {
    unsigned int t = SDL_GetTicks() + ms;
 
-   while (PAL_PollEvent(NULL));
+   PAL_ProcessEvent();
 
    while (!SDL_TICKS_PASSED(SDL_GetTicks(), t))
    {
       SDL_Delay(1);
-      while (PAL_PollEvent(NULL));
+      PAL_ProcessEvent();
    }
 }
 

+ 0 - 1
win32/pal_config.h

@@ -41,7 +41,6 @@
 #else
 # define PAL_VIDEO_INIT_FLAGS  (SDL_HWSURFACE | SDL_RESIZABLE | (gConfig.fFullScreen ? SDL_FULLSCREEN : 0))
 # define PAL_FATAL_OUTPUT(s)   MessageBoxA(0, (s), "FATAL ERROR", MB_ICONERROR)
-# define PAL_ALLOW_KEYREPEAT   1
 # define PAL_HAS_SDLCD         1
 #endif