sdl2-compat: Updated for latest SDL3 changes as of 560daa07f226c80dcc022a7488558992d12cd45b

From cde306383b741e1d5a2b8b49e74ef0b316a5336f Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 1 Apr 2024 16:49:45 -0700
Subject: [PATCH] Updated for latest SDL3 changes as of
 560daa07f226c80dcc022a7488558992d12cd45b

---
 src/sdl2_compat.c          | 59 ++++++++++++++++++++++++++++++++++++++
 src/sdl2_compat.h          | 11 +++++++
 src/sdl3_include_wrapper.h | 22 ++++++++++----
 src/sdl3_syms.h            |  5 ++--
 4 files changed, 89 insertions(+), 8 deletions(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 0b71ace..15a6765 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -1624,6 +1624,28 @@ Event3to2(const SDL_Event *event3, SDL2_Event *event2)
     case SDL_EVENT_GAMEPAD_ADDED:
         event2->jaxis.which = GetIndexFromJoystickInstance(event3->jaxis.which);
         break;
+    case SDL_EVENT_JOYSTICK_BATTERY_UPDATED:
+        switch (event3->jbattery.state) {
+        case SDL_POWERSTATE_CHARGING:
+        case SDL_POWERSTATE_CHARGED:
+            event2->jbattery.level = SDL_JOYSTICK_POWER_WIRED;
+            break;
+        case SDL_POWERSTATE_ON_BATTERY:
+            if (event3->jbattery.percent > 70) {
+                event2->jbattery.level = SDL_JOYSTICK_POWER_FULL;
+            } else if (event3->jbattery.percent > 20) {
+                event2->jbattery.level = SDL_JOYSTICK_POWER_MEDIUM;
+            } else if (event3->jbattery.percent > 5) {
+                event2->jbattery.level = SDL_JOYSTICK_POWER_LOW;
+            } else {
+                event2->jbattery.level = SDL_JOYSTICK_POWER_EMPTY;
+            }
+            break;
+        default:
+            event2->jbattery.level = SDL_JOYSTICK_POWER_UNKNOWN;
+            break;
+        }
+        break;
     default:
         break;
     }
@@ -1694,6 +1716,9 @@ Event2to3(const SDL2_Event *event2, SDL_Event *event3)
         event3->wheel.mouse_x = (float)event2->wheel.mouseX;
         event3->wheel.mouse_y = (float)event2->wheel.mouseY;
         break;
+    case SDL_EVENT_JOYSTICK_BATTERY_UPDATED:
+        /* This should never happen, but see Event3to2() for details */
+        break;
     default:
         break;
     }
@@ -4390,6 +4415,11 @@ SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface)
     return texture;
 }
 
+DECLSPEC int SDLCALL
+SDL_QueryTexture(SDL_Texture * texture, Uint32 * format, int *access, int *w, int *h)
+{
+    return SDL3_QueryTexture(texture, (SDL_PixelFormatEnum *)format, access, w, h);
+}
 
 DECLSPEC int SDLCALL
 SDL_LockMutex(SDL_Mutex *a)
@@ -6970,6 +7000,35 @@ SDL_JoystickHasRumbleTriggers(SDL_Joystick *joystick)
     return SDL3_GetBooleanProperty(SDL3_GetJoystickProperties(joystick), SDL_PROP_GAMEPAD_CAP_TRIGGER_RUMBLE_BOOLEAN, SDL_FALSE);
 }
 
+DECLSPEC SDL_JoystickPowerLevel SDLCALL
+SDL_JoystickCurrentPowerLevel(SDL_Joystick *joystick)
+{
+    SDL_JoystickConnectionState connection_state;
+
+    connection_state = SDL3_GetJoystickConnectionState(joystick);
+    if (connection_state == SDL_JOYSTICK_CONNECTION_WIRELESS) {
+        int percent = -1;
+        SDL_PowerState state = SDL3_GetJoystickPowerInfo(joystick, &percent);
+        if (state == SDL_POWERSTATE_ON_BATTERY) {
+            if (percent > 70) {
+                return SDL_JOYSTICK_POWER_FULL;
+            } else if (percent > 20) {
+                return SDL_JOYSTICK_POWER_MEDIUM;
+            } else if (percent > 5) {
+                return SDL_JOYSTICK_POWER_LOW;
+            } else {
+                return SDL_JOYSTICK_POWER_EMPTY;
+            }
+        } else {
+            return SDL_JOYSTICK_POWER_UNKNOWN;
+        }
+    } else if (connection_state == SDL_JOYSTICK_CONNECTION_WIRED) {
+        return SDL_JOYSTICK_POWER_WIRED;
+    } else {
+        return SDL_JOYSTICK_POWER_UNKNOWN;
+    }
+}
+
 DECLSPEC char* SDLCALL
 SDL_GameControllerMappingForDeviceIndex(int idx)
 {
diff --git a/src/sdl2_compat.h b/src/sdl2_compat.h
index c35a5db..9129f35 100644
--- a/src/sdl2_compat.h
+++ b/src/sdl2_compat.h
@@ -77,6 +77,17 @@ typedef SDL_Gamepad SDL_GameController;  /* since they're opaque types, for simp
 typedef SDL_GamepadAxis SDL_GameControllerAxis;
 typedef SDL_GamepadButton SDL_GameControllerButton;
 
+typedef enum
+{
+    SDL_JOYSTICK_POWER_UNKNOWN = -1,
+    SDL_JOYSTICK_POWER_EMPTY,   /* <= 5% */
+    SDL_JOYSTICK_POWER_LOW,     /* <= 20% */
+    SDL_JOYSTICK_POWER_MEDIUM,  /* <= 70% */
+    SDL_JOYSTICK_POWER_FULL,    /* <= 100% */
+    SDL_JOYSTICK_POWER_WIRED,
+    SDL_JOYSTICK_POWER_MAX
+} SDL_JoystickPowerLevel;
+
 typedef Sint32 SDL2_JoystickID;  /* this became unsigned in SDL3, but we'll just hope we don't overflow. */
 typedef Sint32 SDL2_SensorID;  /* this became unsigned in SDL3, but we'll just hope we don't overflow. */
 
diff --git a/src/sdl3_include_wrapper.h b/src/sdl3_include_wrapper.h
index 3d9b7de..34ac534 100644
--- a/src/sdl3_include_wrapper.h
+++ b/src/sdl3_include_wrapper.h
@@ -264,6 +264,7 @@
 #define SDL_GetGamepadButtonFromString IGNORE_THIS_VERSION_OF_SDL_GetGamepadButtonFromString
 #define SDL_GetGamepadButtonLabel IGNORE_THIS_VERSION_OF_SDL_GetGamepadButtonLabel
 #define SDL_GetGamepadButtonLabelForType IGNORE_THIS_VERSION_OF_SDL_GetGamepadButtonLabelForType
+#define SDL_GetGamepadConnectionState IGNORE_THIS_VERSION_OF_SDL_GetGamepadConnectionState
 #define SDL_GetGamepadFirmwareVersion IGNORE_THIS_VERSION_OF_SDL_GetGamepadFirmwareVersion
 #define SDL_GetGamepadFromInstanceID IGNORE_THIS_VERSION_OF_SDL_GetGamepadFromInstanceID
 #define SDL_GetGamepadFromPlayerIndex IGNORE_THIS_VERSION_OF_SDL_GetGamepadFromPlayerIndex
@@ -284,7 +285,7 @@
 #define SDL_GetGamepadName IGNORE_THIS_VERSION_OF_SDL_GetGamepadName
 #define SDL_GetGamepadPath IGNORE_THIS_VERSION_OF_SDL_GetGamepadPath
 #define SDL_GetGamepadPlayerIndex IGNORE_THIS_VERSION_OF_SDL_GetGamepadPlayerIndex
-#define SDL_GetGamepadPowerLevel IGNORE_THIS_VERSION_OF_SDL_GetGamepadPowerLevel
+#define SDL_GetGamepadPowerInfo IGNORE_THIS_VERSION_OF_SDL_GetGamepadPowerInfo
 #define SDL_GetGamepadProduct IGNORE_THIS_VERSION_OF_SDL_GetGamepadProduct
 #define SDL_GetGamepadProductVersion IGNORE_THIS_VERSION_OF_SDL_GetGamepadProductVersion
 #define SDL_GetGamepadProperties IGNORE_THIS_VERSION_OF_SDL_GetGamepadProperties
@@ -319,6 +320,7 @@
 #define SDL_GetJoystickAxisInitialState IGNORE_THIS_VERSION_OF_SDL_GetJoystickAxisInitialState
 #define SDL_GetJoystickBall IGNORE_THIS_VERSION_OF_SDL_GetJoystickBall
 #define SDL_GetJoystickButton IGNORE_THIS_VERSION_OF_SDL_GetJoystickButton
+#define SDL_GetJoystickConnectionState IGNORE_THIS_VERSION_OF_SDL_GetJoystickConnectionState
 #define SDL_GetJoystickFirmwareVersion IGNORE_THIS_VERSION_OF_SDL_GetJoystickFirmwareVersion
 #define SDL_GetJoystickFromInstanceID IGNORE_THIS_VERSION_OF_SDL_GetJoystickFromInstanceID
 #define SDL_GetJoystickFromPlayerIndex IGNORE_THIS_VERSION_OF_SDL_GetJoystickFromPlayerIndex
@@ -339,7 +341,7 @@
 #define SDL_GetJoystickName IGNORE_THIS_VERSION_OF_SDL_GetJoystickName
 #define SDL_GetJoystickPath IGNORE_THIS_VERSION_OF_SDL_GetJoystickPath
 #define SDL_GetJoystickPlayerIndex IGNORE_THIS_VERSION_OF_SDL_GetJoystickPlayerIndex
-#define SDL_GetJoystickPowerLevel IGNORE_THIS_VERSION_OF_SDL_GetJoystickPowerLevel
+#define SDL_GetJoystickPowerInfo IGNORE_THIS_VERSION_OF_SDL_GetJoystickPowerInfo
 #define SDL_GetJoystickProduct IGNORE_THIS_VERSION_OF_SDL_GetJoystickProduct
 #define SDL_GetJoystickProductVersion IGNORE_THIS_VERSION_OF_SDL_GetJoystickProductVersion
 #define SDL_GetJoystickProperties IGNORE_THIS_VERSION_OF_SDL_GetJoystickProperties
@@ -1991,6 +1993,10 @@
 #undef SDL_GetGamepadButtonLabelForType
 #endif
 
+#ifdef SDL_GetGamepadConnectionState
+#undef SDL_GetGamepadConnectionState
+#endif
+
 #ifdef SDL_GetGamepadFirmwareVersion
 #undef SDL_GetGamepadFirmwareVersion
 #endif
@@ -2071,8 +2077,8 @@
 #undef SDL_GetGamepadPlayerIndex
 #endif
 
-#ifdef SDL_GetGamepadPowerLevel
-#undef SDL_GetGamepadPowerLevel
+#ifdef SDL_GetGamepadPowerInfo
+#undef SDL_GetGamepadPowerInfo
 #endif
 
 #ifdef SDL_GetGamepadProduct
@@ -2211,6 +2217,10 @@
 #undef SDL_GetJoystickButton
 #endif
 
+#ifdef SDL_GetJoystickConnectionState
+#undef SDL_GetJoystickConnectionState
+#endif
+
 #ifdef SDL_GetJoystickFirmwareVersion
 #undef SDL_GetJoystickFirmwareVersion
 #endif
@@ -2291,8 +2301,8 @@
 #undef SDL_GetJoystickPlayerIndex
 #endif
 
-#ifdef SDL_GetJoystickPowerLevel
-#undef SDL_GetJoystickPowerLevel
+#ifdef SDL_GetJoystickPowerInfo
+#undef SDL_GetJoystickPowerInfo
 #endif
 
 #ifdef SDL_GetJoystickProduct
diff --git a/src/sdl3_syms.h b/src/sdl3_syms.h
index b3e2da9..1b73489 100644
--- a/src/sdl3_syms.h
+++ b/src/sdl3_syms.h
@@ -310,7 +310,7 @@ SDL3_SYM_PASSTHROUGH(int,GetRendererInfo,(SDL_Renderer *a, SDL_RendererInfo *b),
 SDL3_SYM_RENAMED(int,GetRendererOutputSize,GetRenderOutputSize,(SDL_Renderer *a, int *b, int *c),(a,b,c),return)
 SDL3_SYM(SDL_Texture*,CreateTexture,(SDL_Renderer *a, SDL_PixelFormatEnum b, int c, int d, int e),(a,b,c,d,e),return)
 SDL3_SYM(SDL_Texture*,CreateTextureFromSurface,(SDL_Renderer *a, SDL_Surface *b),(a,b),return)
-SDL3_SYM_PASSTHROUGH(int,QueryTexture,(SDL_Texture *a, Uint32 *b, int *c, int *d, int *e),(a,b,c,d,e),return)
+SDL3_SYM(int,QueryTexture,(SDL_Texture *a, SDL_PixelFormatEnum *b, int *c, int *d, int *e),(a,b,c,d,e),return)
 SDL3_SYM_PASSTHROUGH(int,SetTextureColorMod,(SDL_Texture *a, Uint8 b, Uint8 c, Uint8 d),(a,b,c,d),return)
 SDL3_SYM_PASSTHROUGH(int,GetTextureColorMod,(SDL_Texture *a, Uint8 *b, Uint8 *c, Uint8 *d),(a,b,c,d),return)
 SDL3_SYM_PASSTHROUGH(int,SetTextureAlphaMod,(SDL_Texture *a, Uint8 b),(a,b),return)
@@ -530,7 +530,8 @@ SDL3_SYM_PASSTHROUGH(int,CaptureMouse,(SDL_bool a),(a),return)
 SDL3_SYM_PASSTHROUGH(int,SetWindowHitTest,(SDL_Window *a, SDL_HitTest b, void *c),(a,b,c),return)
 SDL3_SYM(Uint32,GetGlobalMouseState,(float *a, float *b),(a,b),return)
 SDL3_SYM_PASSTHROUGH(SDL_Window*,GetGrabbedWindow,(void),(),return)
-SDL3_SYM_RENAMED(SDL_JoystickPowerLevel,JoystickCurrentPowerLevel,GetJoystickPowerLevel,(SDL_Joystick *a),(a),return)
+SDL3_SYM(SDL_JoystickConnectionState,GetJoystickConnectionState,(SDL_Joystick *a),(a),return)
+SDL3_SYM(SDL_PowerState,GetJoystickPowerInfo,(SDL_Joystick *a, int *b),(a,b),return)
 SDL3_SYM(SDL_GameController*,GetGamepadFromInstanceID,(SDL_JoystickID a),(a),return)
 SDL3_SYM(SDL_Joystick*,GetJoystickFromInstanceID,(SDL_JoystickID a),(a),return)
 SDL3_SYM(int,GetDisplayUsableBounds,(SDL_DisplayID a, SDL_Rect *b),(a,b),return)