SDL: Fixed reported cases of "Keyboard layout unknown" messages

From 139192140c8737b5b4abd1a08b9cf918a653e854 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 13 Oct 2022 23:23:55 -0700
Subject: [PATCH] Fixed reported cases of "Keyboard layout unknown" messages

In all cases they were using SDL_SCANCODE_TABLE_XFREE86_2 with some keycodes remapped or fewer than expected keycodes. This adds a sanity check that catches all of them and gives them the right scancode table.
---
 src/video/x11/SDL_x11keyboard.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/video/x11/SDL_x11keyboard.c b/src/video/x11/SDL_x11keyboard.c
index ffa575d550e5..664c816613f7 100644
--- a/src/video/x11/SDL_x11keyboard.c
+++ b/src/video/x11/SDL_x11keyboard.c
@@ -605,10 +605,6 @@ X11_InitKeyboard(_THIS)
         int table_size;
         const SDL_Scancode *table = SDL_GetScancodeTable(scancode_set[i], &table_size);
 
-        /* Make sure the scancode set isn't too big */
-        if (table_size > (max_keycode - min_keycode + 1)) {
-            continue;
-        }
         distance = 0;
         for (j = 0; j < SDL_arraysize(fingerprint); ++j) {
             if (fingerprint[j].value < 0 || fingerprint[j].value >= table_size) {
@@ -622,6 +618,19 @@ X11_InitKeyboard(_THIS)
             best_index = i;
         }
     }
+    if (best_index < 0 || best_distance > 2) {
+        /* This is likely to be SDL_SCANCODE_TABLE_XFREE86_2 with remapped keys, double check a rarely remapped value */
+        int fingerprint_value = X11_XKeysymToKeycode(data->display, 0x1008FF5B /* XF86Documents */) - min_keycode;
+        if (fingerprint_value == 235) {
+            for (i = 0; i < SDL_arraysize(scancode_set); ++i) {
+                if (scancode_set[i] == SDL_SCANCODE_TABLE_XFREE86_2) {
+                    best_index = i;
+                    best_distance = 0;
+                    break;
+                }
+            }
+        }
+    }
     if (best_index >= 0 && best_distance <= 2) {
         SDL_Keycode default_keymap[SDL_NUM_SCANCODES];
         int table_size;