SDL: Added KMOD_SCROLL to track the scroll lock state

From cb1e20b058f1de55b98f32b7d16cff1f896f88dd Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 10 Aug 2021 17:50:17 -0700
Subject: [PATCH] Added KMOD_SCROLL to track the scroll lock state

Fixes https://github.com/libsdl-org/SDL/issues/4566
---
 include/SDL_keycode.h                   |  2 +-
 src/events/SDL_keyboard.c               |  3 +++
 src/video/windows/SDL_windowsevents.c   |  1 +
 src/video/windows/SDL_windowskeyboard.c |  1 +
 src/video/x11/SDL_x11events.c           | 27 +++++++++++++++++++++++++
 test/checkkeys.c                        |  2 ++
 test/checkkeysthreads.c                 |  2 ++
 test/testautomation_keyboard.c          |  2 +-
 8 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/include/SDL_keycode.h b/include/SDL_keycode.h
index 4fb0d39c3..f94fcec9c 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 18089af55..e8f0db4ec 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 555f9b226..3fbd5a862 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 157ec2a59..52d2bbe3f 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 d3b02f568..f91131305 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 0bfb2cb3d..cdf982cf2 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 4019a80a3..fd812e241 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 dc05ff1c2..f415da478 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();