Browse Source

Added infrastructure for querying battery status for GameInput

Sam Lantinga 1 year ago
parent
commit
4a59b17de2
1 changed files with 24 additions and 10 deletions
  1. 24 10
      src/joystick/gdk/SDL_gameinputjoystick.c

+ 24 - 10
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)