sdl12-compat: Modifier keys on Mac OS X do not have scancodes

From e1d7b809d73715491a18db4fe6e2ec7557dfb32a Mon Sep 17 00:00:00 2001
From: David Gow <[EMAIL REDACTED]>
Date: Thu, 10 Jun 2021 21:04:59 +0800
Subject: [PATCH] Modifier keys on Mac OS X do not have scancodes

This actually fixes three related issues:
- Modifier keys on OS X should have "unknown" scancodes
- Unknown scancodes on OS X should have the value 0, even though that
  conflicts with the scancode for "a"
- RGUI and RSHIFT should have scancodes on non-OSX platforms.

SDL 1.2's Quartz backend, used on Mac OS X, always gives modifier keys a
scancode of 0, even though OS X actually does have a scancode number for
them. This can be seen (both for < OS X 10.3, where left and right
modifiers could not be distinguished, and for newer versions where they
can) in the implementation here:
https://github.com/libsdl-org/SDL-1.2/blob/02c0253780c8a36569b271500a37591ab80e923d/src/video/quartz/SDL_QuartzEvents.m#L475

Applications such as DOSBox rely heavily on this behaviour, and the keys
will not function in the default "usescancodes=true" mode if a scancode
other than 0 is returned for the right-hand-side modifier keys.

This can be seen in sdl12-compat GitHub issue #84, where the issue was
analysed:
https://github.com/libsdl-org/sdl12-compat/issues/84#issuecomment-858597299
---
 src/SDL12_compat.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index 30cee4f..12f3a0c 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -2418,7 +2418,7 @@ Scancode20to12(SDL_Scancode sc)
     CASESCANCODE20TO12(BACKSLASH, 0x33, 0x2A);
     CASESCANCODE20TO12(BACKSPACE, 0x16, 0x33);
     CASESCANCODE20TO12(C, 0x36, 0x08);
-    CASESCANCODE20TO12(CAPSLOCK, 0x42, 0x39);
+    CASESCANCODE20TO12(CAPSLOCK, 0x42, 0x00);
     CASESCANCODE20TO12(COMMA, 0x3B, 0x2B);
     CASESCANCODE20TO12(D, 0x28, 0x02);
     CASESCANCODE20TO12(DELETE, 0x00, 0x75);
@@ -2466,12 +2466,12 @@ Scancode20to12(SDL_Scancode sc)
     CASESCANCODE20TO12(KP_PERIOD, 0x5B, 0x41);
     CASESCANCODE20TO12(KP_PLUS, 0x56, 0x44);
     CASESCANCODE20TO12(L, 0x2E, 0x25);
-    CASESCANCODE20TO12(LALT, 0x40, 0x3A);
-    CASESCANCODE20TO12(LCTRL, 0x25, 0x3B);
+    CASESCANCODE20TO12(LALT, 0x40, 0x00);
+    CASESCANCODE20TO12(LCTRL, 0x25, 0x00);
     CASESCANCODE20TO12(LEFT, 0x00, 0x7B);
     CASESCANCODE20TO12(LEFTBRACKET, 0x22, 0x21);
-    CASESCANCODE20TO12(LGUI, 0x85, 0x37);
-    CASESCANCODE20TO12(LSHIFT, 0x32, 0x38);
+    CASESCANCODE20TO12(LGUI, 0x85, 0x00);
+    CASESCANCODE20TO12(LSHIFT, 0x32, 0x00);
     CASESCANCODE20TO12(M, 0x3A, 0x29);
     CASESCANCODE20TO12(MINUS, 0x14, 0x1B);
     CASESCANCODE20TO12(N, 0x39, 0x28);
@@ -2485,8 +2485,10 @@ Scancode20to12(SDL_Scancode sc)
     CASESCANCODE20TO12(Q, 0x18, 0x0C);
     CASESCANCODE20TO12(R, 0x1B, 0x0F);
     CASESCANCODE20TO12(RETURN, 0x24, 0x24);
+    CASESCANCODE20TO12(RGUI, 0x86, 0x00);
     CASESCANCODE20TO12(RIGHT, 0x00, 0x7C);
     CASESCANCODE20TO12(RIGHTBRACKET, 0x23, 0x1E);
+    CASESCANCODE20TO12(RSHIFT, 0x3E, 0x00);
     CASESCANCODE20TO12(S, 0x27, 0x01);
     CASESCANCODE20TO12(SCROLLLOCK, 0x4E, 0x71);
     CASESCANCODE20TO12(SEMICOLON, 0x2F, 0x29);
@@ -2503,13 +2505,9 @@ Scancode20to12(SDL_Scancode sc)
     CASESCANCODE20TO12(Z, 0x34, 0x06);
 #undef CASESCANCODE20TO12    
     default:
-        /* If we don't know it, return 0, which is "unknown" on everything
-           except Mac OS X, where it's "A" for whatever reason. */
-#ifdef __MACOSX__
-        return 0x34;    /* DOSBox should treat this as unknown. */
-#else
+        /* If we don't know it, return 0, which is "unknown".
+           It's also "a" on Mac OS X, but SDL 1.2 uses it as "unknown", too. */
         return 0;
-#endif
     }
     return 0;
 }