Commit cb1e20b058f1de55b98f32b7d16cff1f896f88dd

Sam Lantinga 2021-08-10T17:50:17

Added KMOD_SCROLL to track the scroll lock state Fixes https://github.com/libsdl-org/SDL/issues/4566

diff --git a/include/SDL_keycode.h b/include/SDL_keycode.h
index 4fb0d39..f94fcec 100644
--- a/include/SDL_keycode.h
+++ b/include/SDL_keycode.h
@@ -338,7 +338,7 @@ typedef enum
     KMOD_NUM = 0x1000,
     KMOD_CAPS = 0x2000,
     KMOD_MODE = 0x4000,
-    KMOD_RESERVED = 0x8000,
+    KMOD_SCROLL = 0x8000,
 
     KMOD_CTRL = KMOD_LCTRL | KMOD_RCTRL,
     KMOD_SHIFT = KMOD_LSHIFT | KMOD_RSHIFT,
diff --git a/src/events/SDL_keyboard.c b/src/events/SDL_keyboard.c
index 18089af..e8f0db4 100644
--- a/src/events/SDL_keyboard.c
+++ b/src/events/SDL_keyboard.c
@@ -767,6 +767,9 @@ SDL_SendKeyboardKeyInternal(Uint8 source, Uint8 state, SDL_Scancode scancode)
         case SDLK_CAPSLOCK:
             keyboard->modstate ^= KMOD_CAPS;
             break;
+        case SDLK_SCROLLLOCK:
+            keyboard->modstate ^= KMOD_SCROLL;
+            break;
         default:
             keyboard->modstate |= modifier;
             break;
diff --git a/src/video/windows/SDL_windowsevents.c b/src/video/windows/SDL_windowsevents.c
index 555f9b2..3fbd5a8 100644
--- a/src/video/windows/SDL_windowsevents.c
+++ b/src/video/windows/SDL_windowsevents.c
@@ -627,6 +627,7 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
 
                 SDL_ToggleModState(KMOD_CAPS, (GetKeyState(VK_CAPITAL) & 0x0001) != 0);
                 SDL_ToggleModState(KMOD_NUM, (GetKeyState(VK_NUMLOCK) & 0x0001) != 0);
+                SDL_ToggleModState(KMOD_SCROLL, (GetKeyState(VK_SCROLL) & 0x0001) != 0);
             } else {
                 RECT rect;
 
diff --git a/src/video/windows/SDL_windowskeyboard.c b/src/video/windows/SDL_windowskeyboard.c
index 157ec2a..52d2bbe 100644
--- a/src/video/windows/SDL_windowskeyboard.c
+++ b/src/video/windows/SDL_windowskeyboard.c
@@ -106,6 +106,7 @@ WIN_InitKeyboard(_THIS)
     /* Are system caps/num/scroll lock active? Set our state to match. */
     SDL_ToggleModState(KMOD_CAPS, (GetKeyState(VK_CAPITAL) & 0x0001) != 0);
     SDL_ToggleModState(KMOD_NUM, (GetKeyState(VK_NUMLOCK) & 0x0001) != 0);
+    SDL_ToggleModState(KMOD_SCROLL, (GetKeyState(VK_SCROLL) & 0x0001) != 0);
 }
 
 void
diff --git a/src/video/x11/SDL_x11events.c b/src/video/x11/SDL_x11events.c
index d3b02f5..f911313 100644
--- a/src/video/x11/SDL_x11events.c
+++ b/src/video/x11/SDL_x11events.c
@@ -353,6 +353,32 @@ X11_GetNumLockModifierMask(_THIS)
     return num_mask;
 }
 
+static unsigned
+X11_GetScrollLockModifierMask(_THIS)
+{
+    SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
+    Display *display = viddata->display;
+    unsigned num_mask = 0;
+    int i, j;
+    XModifierKeymap *xmods;
+    unsigned n;
+
+    xmods = X11_XGetModifierMapping(display);
+    n = xmods->max_keypermod;
+    for(i = 3; i < 8; i++) {
+        for(j = 0; j < n; j++) {
+            KeyCode kc = xmods->modifiermap[i * n + j];
+            if (viddata->key_layout[kc] == SDL_SCANCODE_SCROLLLOCK) {
+                num_mask = 1 << i;
+                break;
+            }
+        }
+    }
+    X11_XFreeModifiermap(xmods);
+
+    return num_mask;
+}
+
 static void
 X11_ReconcileKeyboardState(_THIS)
 {
@@ -371,6 +397,7 @@ X11_ReconcileKeyboardState(_THIS)
     if (X11_XQueryPointer(display, DefaultRootWindow(display), &junk_window, &junk_window, &x, &y, &x, &y, &mask)) {
         SDL_ToggleModState(KMOD_CAPS, (mask & LockMask) != 0);
         SDL_ToggleModState(KMOD_NUM, (mask & X11_GetNumLockModifierMask(_this)) != 0);
+        SDL_ToggleModState(KMOD_SCROLL, (mask & X11_GetScrollLockModifierMask(_this)) != 0);
     }
 
     keyboardState = SDL_GetKeyboardState(0);
diff --git a/test/checkkeys.c b/test/checkkeys.c
index 0bfb2cb..cdf982c 100644
--- a/test/checkkeys.c
+++ b/test/checkkeys.c
@@ -86,6 +86,8 @@ print_modifiers(char **text, size_t *maxlen)
         print_string(text, maxlen, " CAPS");
     if (mod & KMOD_MODE)
         print_string(text, maxlen, " MODE");
+    if (mod & KMOD_SCROLL)
+        print_string(text, maxlen, " SCROLL");
 }
 
 static void
diff --git a/test/checkkeysthreads.c b/test/checkkeysthreads.c
index 4019a80..fd812e2 100644
--- a/test/checkkeysthreads.c
+++ b/test/checkkeysthreads.c
@@ -86,6 +86,8 @@ print_modifiers(char **text, size_t *maxlen)
         print_string(text, maxlen, " CAPS");
     if (mod & KMOD_MODE)
         print_string(text, maxlen, " MODE");
+    if (mod & KMOD_SCROLL)
+        print_string(text, maxlen, " SCROLL");
 }
 
 static void
diff --git a/test/testautomation_keyboard.c b/test/testautomation_keyboard.c
index dc05ff1..f415da4 100644
--- a/test/testautomation_keyboard.c
+++ b/test/testautomation_keyboard.c
@@ -305,7 +305,7 @@ keyboard_getSetModState(void *arg)
     KMOD_NUM |
     KMOD_CAPS |
     KMOD_MODE |
-    KMOD_RESERVED;
+    KMOD_SCROLL;
 
    /* Get state, cache for later reset */
    result = SDL_GetModState();