sdl2-compat: Updated for SDL3 change to use SDL_bool for button states

From 0fd0833595e87f714bbb1e0649a2a5682ca0e3b1 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 9 Sep 2024 14:18:19 -0700
Subject: [PATCH] Updated for SDL3 change to use SDL_bool for button states

---
 src/sdl2_compat.c | 30 ++++++++++++++++++++++++++++--
 src/sdl3_syms.h   | 10 +++++-----
 2 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 19d78e0..e360070 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -1209,7 +1209,7 @@ Event3to2(const SDL_Event *event3, SDL2_Event *event2)
         event2->key.keysym.scancode = SDL3ScancodeToSDL2Scancode(event3->key.scancode);
         event2->key.keysym.sym = event3->key.key;
         event2->key.keysym.mod = event3->key.mod;
-        event2->key.state = event3->key.state;
+        event2->key.state = event3->key.down;
         event2->key.repeat = event3->key.repeat;
         break;
     case SDL_EVENT_TEXT_INPUT:
@@ -1361,7 +1361,7 @@ Event2to3(const SDL2_Event *event2, SDL_Event *event3)
         event3->key.key = event2->key.keysym.sym;
         event3->key.mod = event2->key.keysym.mod;
         event3->key.raw = 0;
-        event3->key.state = event2->key.state;
+        event3->key.down = (event2->key.state != 0);
         event3->key.repeat = event2->key.repeat;
         break;
     case SDL_EVENT_MOUSE_MOTION:
@@ -1871,6 +1871,13 @@ SDL_GetKeyFromName(const char *name)
     }
 }
 
+SDL_DECLSPEC const Uint8 *SDLCALL
+SDL_GetKeyboardState(int *numkeys)
+{
+    SDL_COMPILE_TIME_ASSERT(bool_size, sizeof(SDL_bool) == sizeof(Uint8));
+    return (const Uint8 *)SDL3_GetKeyboardState(numkeys);
+}
+
 /* Several SDL3 video backends have had their names lower-cased, map to the SDL2 equivalent name. */
 static const char *ReplaceVideoBackendName(const char *name)
 {
@@ -7947,6 +7954,12 @@ SDL_JoystickGetDeviceInstanceID(int idx)
     return (SDL2_JoystickID)jid;
 }
 
+SDL_DECLSPEC Uint8 SDLCALL
+SDL_JoystickGetButton(SDL_Joystick *joystick, int button)
+{
+    return SDL3_GetJoystickButton(joystick, button);
+}
+
 SDL_DECLSPEC SDL2_JoystickID SDLCALL
 SDL_JoystickInstanceID(SDL_Joystick *joystick)
 {
@@ -8161,6 +8174,13 @@ SDL_DECLSPEC Uint8 SDLCALL SDL_GameControllerGetButton(SDL_GameController *contr
     return SDL3_GetGamepadButton(controller, button);
 }
 
+SDL_DECLSPEC int SDLCALL
+SDL_GameControllerGetTouchpadFinger(SDL_GameController *gamecontroller, int touchpad, int finger, Uint8 *state, float *x, float *y, float *pressure)
+{
+    SDL_COMPILE_TIME_ASSERT(bool_size, sizeof(SDL_bool) == sizeof(Uint8));
+    return SDL3_GetGamepadTouchpadFinger(gamecontroller, touchpad, finger, (SDL_bool *)state, x, y, pressure) ? 0 : -1;
+}
+
 SDL_DECLSPEC SDL_GameControllerButtonBind SDLCALL
 SDL_GameControllerGetBindForAxis(SDL_GameController *controller,
                                  SDL_GameControllerAxis axis)
@@ -8366,6 +8386,12 @@ SDL_JoystickAttachVirtualEx(const SDL2_VirtualJoystickDesc *desc2)
     return GetIndexFromJoystickInstance(jid);
 }
 
+SDL_DECLSPEC int SDLCALL
+SDL_JoystickSetVirtualButton(SDL_Joystick *joystick, int button, Uint8 value)
+{
+    return SDL3_SetJoystickVirtualButton(joystick, button, (value != 0)) ? 0 : -1;
+}
+
 SDL_DECLSPEC int SDLCALL
 SDL_JoystickDetachVirtual(int device_index)
 {
diff --git a/src/sdl3_syms.h b/src/sdl3_syms.h
index 12b8972..d052539 100644
--- a/src/sdl3_syms.h
+++ b/src/sdl3_syms.h
@@ -224,7 +224,7 @@ SDL3_SYM_RENAMED(const char*,GameControllerGetSerial,GetGamepadSerial,(SDL_GameC
 SDL3_SYM_RENAMED(Uint64,GameControllerGetSteamHandle,GetGamepadSteamHandle,(SDL_GameController *a),(a),return)
 SDL3_SYM_RENAMED(const char*,GameControllerGetStringForAxis,GetGamepadStringForAxis,(SDL_GameControllerAxis a),(a),return)
 SDL3_SYM_RENAMED(const char*,GameControllerGetStringForButton,GetGamepadStringForButton,(SDL_GameControllerButton a),(a),return)
-SDL3_SYM_RENAMED_RETCODE(SDL_bool,GameControllerGetTouchpadFinger,GetGamepadTouchpadFinger,(SDL_GameController *a, int b, int c, Uint8 *d, float *e, float *f, float *g),(a,b,c,d,e,f,g),return)
+SDL3_SYM(SDL_bool,GetGamepadTouchpadFinger,(SDL_GameController *a, int b, int c, SDL_bool *d, float *e, float *f, float *g),(a,b,c,d,e,f,g),return)
 SDL3_SYM_RENAMED(Uint16,GameControllerGetVendor,GetGamepadVendor,(SDL_GameController *a),(a),return)
 SDL3_SYM_RENAMED_BOOL(SDL_bool,GameControllerHasAxis,GamepadHasAxis,(SDL_GameController *a, SDL_GameControllerAxis b),(a,b),return)
 SDL3_SYM_RENAMED_BOOL(SDL_bool,GameControllerHasButton,GamepadHasButton,(SDL_GameController *a, SDL_GameControllerButton b),(a,b),return)
@@ -279,7 +279,7 @@ SDL3_SYM(SDL_bool,GetEventFilter,(SDL_EventFilter *a, void **b),(a,b),)
 SDL3_SYM(float,GetFloatProperty,(SDL_PropertiesID a, const char *b, float c),(a,b,c),return)
 SDL3_SYM(SDL_DisplayMode **,GetFullscreenDisplayModes,(SDL_DisplayID a, int *b),(a,b),return)
 SDL3_SYM(SDL_GamepadBinding **,GetGamepadBindings,(SDL_Gamepad *a, int *b),(a,b),return)
-SDL3_SYM(Uint8,GetGamepadButton,(SDL_Gamepad *a, SDL_GamepadButton b),(a,b),return)
+SDL3_SYM(SDL_bool,GetGamepadButton,(SDL_Gamepad *a, SDL_GamepadButton b),(a,b),return)
 SDL3_SYM(SDL_GamepadButtonLabel,GetGamepadButtonLabel,(SDL_Gamepad *a, SDL_GamepadButton b),(a,b),return)
 SDL3_SYM(SDL_GameController*,GetGamepadFromID,(SDL_JoystickID a),(a),return)
 SDL3_SYM(SDL_JoystickID,GetGamepadID,(SDL_GameController *a),(a),return)
@@ -320,7 +320,7 @@ SDL3_SYM(SDL_Keycode,GetKeyFromName,(const char *a),(a),return)
 SDL3_SYM(SDL_Keycode,GetKeyFromScancode,(SDL_Scancode a, SDL_Keymod b, SDL_bool c),(a,b,c),return)
 SDL3_SYM(const char*,GetKeyName,(SDL_Keycode a),(a),return)
 SDL3_SYM_PASSTHROUGH(SDL_Window*,GetKeyboardFocus,(void),(),return)
-SDL3_SYM_PASSTHROUGH(const Uint8*,GetKeyboardState,(int *a),(a),return)
+SDL3_SYM(const SDL_bool*,GetKeyboardState,(int *a),(a),return)
 SDL3_SYM(SDL_bool,GetMasksForPixelFormat,(SDL_PixelFormat a, int *b, Uint32 *c, Uint32 *d, Uint32 *e, Uint32 *f),(a,b,c,d,e,f),return)
 SDL3_SYM_PASSTHROUGH(void,GetMemoryFunctions,(SDL_malloc_func *a, SDL_calloc_func *b, SDL_realloc_func *c, SDL_free_func *d),(a,b,c,d),)
 SDL3_SYM(SDL_Keymod,GetModState,(void),(),return)
@@ -481,7 +481,7 @@ SDL3_SYM_RENAMED_BOOL(SDL_bool,JoystickGetAttached,JoystickConnected,(SDL_Joysti
 SDL3_SYM_RENAMED(Sint16,JoystickGetAxis,GetJoystickAxis,(SDL_Joystick *a, int b),(a,b),return)
 SDL3_SYM_RENAMED_BOOL(SDL_bool,JoystickGetAxisInitialState,GetJoystickAxisInitialState,(SDL_Joystick *a, int b, Sint16 *c),(a,b,c),return)
 SDL3_SYM_RENAMED_RETCODE(SDL_bool,JoystickGetBall,GetJoystickBall,(SDL_Joystick *a, int b, int *c, int *d),(a,b,c,d),return);
-SDL3_SYM_RENAMED(Uint8,JoystickGetButton,GetJoystickButton,(SDL_Joystick *a, int b),(a,b),return)
+SDL3_SYM(SDL_bool,GetJoystickButton,(SDL_Joystick *a, int b),(a,b),return)
 SDL3_SYM_RENAMED(Uint16,JoystickGetFirmwareVersion,GetJoystickFirmwareVersion,(SDL_Joystick *a),(a),return)
 SDL3_SYM_RENAMED(SDL_GUID,JoystickGetGUID,GetJoystickGUID,(SDL_Joystick *a),(a),return)
 SDL3_SYM_RENAMED(Uint8,JoystickGetHat,GetJoystickHat,(SDL_Joystick *a, int b),(a,b),return)
@@ -503,7 +503,7 @@ SDL3_SYM_RENAMED_RETCODE(SDL_bool,JoystickRumbleTriggers,RumbleJoystickTriggers,
 SDL3_SYM_RENAMED_RETCODE(SDL_bool,JoystickSendEffect,SendJoystickEffect,(SDL_Joystick *a, const void *b, int c),(a,b,c),return)
 SDL3_SYM_RENAMED_RETCODE(SDL_bool,JoystickSetLED,SetJoystickLED,(SDL_Joystick *a, Uint8 b, Uint8 c, Uint8 d),(a,b,c,d),return)
 SDL3_SYM_RENAMED_RETCODE(SDL_bool,JoystickSetVirtualAxis,SetJoystickVirtualAxis,(SDL_Joystick *a, int b, Sint16 c),(a,b,c),return)
-SDL3_SYM_RENAMED_RETCODE(SDL_bool,JoystickSetVirtualButton,SetJoystickVirtualButton,(SDL_Joystick *a, int b, Uint8 c),(a,b,c),return)
+SDL3_SYM(SDL_bool,SetJoystickVirtualButton,(SDL_Joystick *a, int b, SDL_bool c),(a,b,c),return)
 SDL3_SYM_RENAMED_RETCODE(SDL_bool,JoystickSetVirtualHat,SetJoystickVirtualHat,(SDL_Joystick *a, int b, Uint8 c),(a,b,c),return)
 SDL3_SYM_RENAMED(void,JoystickUpdate,UpdateJoysticks,(void),(),)
 SDL3_SYM(SDL_Surface*,LoadBMP_IO,(SDL_IOStream *a, SDL_bool b),(a,b),return)