SDL: Added infrastructure for querying battery status for GameInput

From 4a59b17de2a1d44a7009def62ee22a0e7e55f0e2 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 17 Feb 2024 19:09:29 -0800
Subject: [PATCH] Added infrastructure for querying battery status for
 GameInput

---
 src/joystick/gdk/SDL_gameinputjoystick.c | 34 +++++++++++++++++-------
 1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/src/joystick/gdk/SDL_gameinputjoystick.c b/src/joystick/gdk/SDL_gameinputjoystick.c
index c2692c442bf2..4b5489b4ad2d 100644
--- a/src/joystick/gdk/SDL_gameinputjoystick.c
+++ b/src/joystick/gdk/SDL_gameinputjoystick.c
@@ -39,6 +39,7 @@ typedef struct GAMEINPUT_InternalDevice
     Uint16 product;
     SDL_JoystickGUID guid;          /* generated by SDL */
     SDL_JoystickID device_instance; /* generated by SDL */
+    SDL_bool wireless;
     GameInputRumbleMotors supportedRumbleMotors;
     SDL_bool isAdded;
     SDL_bool isDeleteRequested;
@@ -125,6 +126,7 @@ static int GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
     elem->product = product;
     elem->guid = SDL_CreateJoystickGUID(bus, vendor, product, version, "GameInput", "Gamepad", 'g', 0);
     elem->device_instance = SDL_GetNextObjectID();
+    elem->wireless = (devinfo->capabilities & GameInputDeviceCapabilityWireless);
     elem->supportedRumbleMotors = devinfo->supportedRumbleMotors;
     g_GameInputList.devices[g_GameInputList.count] = elem;
 
@@ -328,6 +330,19 @@ static SDL_JoystickID GAMEINPUT_JoystickGetDeviceInstanceID(int device_index)
     return GAMEINPUT_InternalFindByIndex(device_index)->device_instance;
 }
 
+static SDL_JoystickPowerLevel GAMEINPUT_InternalGetPowerLevel(IGameInputDevice *device)
+{
+    GameInputBatteryState battery_state;
+
+    SDL_zero(battery_state);
+    IGameInputDevice_GetBatteryState(device, &battery_state);
+
+    if (battery_state.status == GameInputBatteryDischarging) {
+        /* FIXME: What are the units for remainingCapacity? */
+    }
+    return SDL_JOYSTICK_POWER_UNKNOWN;
+}
+
 static int GAMEINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
 {
     GAMEINPUT_InternalDevice *elem = GAMEINPUT_InternalFindByIndex(device_index);
@@ -356,6 +371,11 @@ static int GAMEINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
         SDL_SetBooleanProperty(SDL_GetJoystickProperties(joystick), SDL_PROP_JOYSTICK_CAP_TRIGGER_RUMBLE_BOOLEAN, SDL_TRUE);
     }
 
+    if (elem->wireless) {
+        joystick->epowerlevel = GAMEINPUT_InternalGetPowerLevel(elem->device);
+    } else {
+        joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
+    }
     return 0;
 }
 
@@ -463,16 +483,10 @@ static void GAMEINPUT_JoystickUpdate(SDL_Joystick *joystick)
 
     IGameInputReading_Release(reading);
 
-#if 0
-    /* FIXME: We can poll this at a much lower rate */
-    GameInputBatteryState battery_state;
-    SDL_zero(battery_state);
-    IGameInputDevice_GetBatteryState(device, &battery_state);
-
-
-    /* Xbox doesn't let you obtain the power level, pretend we're always full */
-    SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_FULL);
-#endif
+    if (joystick->epowerlevel != SDL_JOYSTICK_POWER_WIRED) {
+        /* FIXME: We can poll this at a much lower rate */
+        SDL_SendJoystickBatteryLevel(joystick, GAMEINPUT_InternalGetPowerLevel(device));
+    }
 }
 
 static void GAMEINPUT_JoystickClose(SDL_Joystick* joystick)