SDL - added new SDL_JoystickCurrentPowerLevel() API that returns the battery level of the selected joystick. Currently only implemented for XInput devices, other platforms are a TODO.

CR: Sam
This commit is contained in:
Sam Lantinga 2015-09-30 15:39:30 -07:00
parent 8038b8bb7f
commit bf785c766b
7 changed files with 127 additions and 8 deletions

View file

@ -29,6 +29,7 @@
XInputGetState_t SDL_XInputGetState = NULL;
XInputSetState_t SDL_XInputSetState = NULL;
XInputGetCapabilities_t SDL_XInputGetCapabilities = NULL;
XInputGetBatteryInformation_t SDL_XInputGetBatteryInformation = NULL;
DWORD SDL_XInputVersion = 0;
static HANDLE s_pXInputDLL = 0;
@ -55,6 +56,7 @@ WIN_LoadXInputDLL(void)
SDL_XInputGetState = (XInputGetState_t)XInputGetState;
SDL_XInputSetState = (XInputSetState_t)XInputSetState;
SDL_XInputGetCapabilities = (XInputGetCapabilities_t)XInputGetCapabilities;
SDL_XInputGetBatteryInformation = (XInputGetBatteryInformation_t)XInputGetBatteryInformation;
/* XInput 1.4 ships with Windows 8 and 8.1: */
SDL_XInputVersion = (1 << 16) | 4;
@ -108,6 +110,7 @@ WIN_LoadXInputDLL(void)
}
SDL_XInputSetState = (XInputSetState_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputSetState");
SDL_XInputGetCapabilities = (XInputGetCapabilities_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputGetCapabilities");
SDL_XInputGetBatteryInformation = (XInputGetBatteryInformation_t)GetProcAddress( (HMODULE)s_pXInputDLL, "XInputGetBatteryInformation" );
if (!SDL_XInputGetState || !SDL_XInputSetState || !SDL_XInputGetCapabilities) {
WIN_UnloadXInputDLL();
return -1;

View file

@ -76,6 +76,29 @@
#define XINPUT_GAMEPAD_GUIDE 0x0400
#endif
#ifndef BATTERY_DEVTYPE_GAMEPAD
#define BATTERY_DEVTYPE_GAMEPAD 0x00
#endif
#ifndef BATTERY_TYPE_WIRED
#define BATTERY_TYPE_WIRED 0x01
#endif
#ifndef BATTERY_TYPE_UNKNOWN
#define BATTERY_TYPE_UNKNOWN 0xFF
#endif
#ifndef BATTERY_LEVEL_EMPTY
#define BATTERY_LEVEL_EMPTY 0x00
#endif
#ifndef BATTERY_LEVEL_LOW
#define BATTERY_LEVEL_LOW 0x01
#endif
#ifndef BATTERY_LEVEL_MEDIUM
#define BATTERY_LEVEL_MEDIUM 0x02
#endif
#ifndef BATTERY_LEVEL_FULL
#define BATTERY_LEVEL_FULL 0x03
#endif
/* typedef's for XInput structs we use */
typedef struct
{
@ -95,6 +118,12 @@ typedef struct
XINPUT_GAMEPAD_EX Gamepad;
} XINPUT_STATE_EX;
typedef struct _XINPUT_BATTERY_INFORMATION
{
BYTE BatteryType;
BYTE BatteryLevel;
} XINPUT_BATTERY_INFORMATION, *PXINPUT_BATTERY_INFORMATION;
/* Forward decl's for XInput API's we load dynamically and use if available */
typedef DWORD (WINAPI *XInputGetState_t)
(
@ -115,17 +144,26 @@ typedef DWORD (WINAPI *XInputGetCapabilities_t)
XINPUT_CAPABILITIES* pCapabilities /* [out] Receives the capabilities */
);
typedef DWORD (WINAPI *XInputGetBatteryInformation_t)
(
_In_ DWORD dwUserIndex,
_In_ BYTE devType,
_Out_ XINPUT_BATTERY_INFORMATION *pBatteryInformation
);
extern int WIN_LoadXInputDLL(void);
extern void WIN_UnloadXInputDLL(void);
extern XInputGetState_t SDL_XInputGetState;
extern XInputSetState_t SDL_XInputSetState;
extern XInputGetCapabilities_t SDL_XInputGetCapabilities;
extern XInputGetBatteryInformation_t SDL_XInputGetBatteryInformation;
extern DWORD SDL_XInputVersion; /* ((major << 16) & 0xFF00) | (minor & 0xFF) */
#define XINPUTGETSTATE SDL_XInputGetState
#define XINPUTSETSTATE SDL_XInputSetState
#define XINPUTGETCAPABILITIES SDL_XInputGetCapabilities
#define XINPUTGETBATTERYINFORMATION SDL_XInputGetBatteryInformation
#endif /* HAVE_XINPUT_H */