Skip to content

Commit 7dd5e76

Browse files
endriftslouken
authored andcommitted
joystick: Report battery on GIP controllers
1 parent c54a017 commit 7dd5e76

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

src/joystick/hidapi/SDL_hidapi_gip.c

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,17 +1412,71 @@ static bool GIP_HandleCommandStatusDevice(
14121412
const Uint8 *bytes,
14131413
int num_bytes)
14141414
{
1415-
GIP_ExtendedStatus status = {{0}};
1415+
GIP_ExtendedStatus status;
1416+
SDL_Joystick *joystick = NULL;
1417+
SDL_PowerState power_state;
1418+
int power_percent = 0;
14161419
int i;
14171420

14181421
if (num_bytes < 1) {
14191422
return false;
14201423
}
1424+
SDL_zero(status);
14211425
status.base.battery_level = bytes[0] & 3;
14221426
status.base.battery_type = (bytes[0] >> 2) & 3;
14231427
status.base.charge = (bytes[0] >> 4) & 3;
14241428
status.base.power_level = (bytes[0] >> 6) & 3;
14251429

1430+
if (attachment->joystick) {
1431+
joystick = SDL_GetJoystickFromID(attachment->joystick);
1432+
}
1433+
if (joystick) {
1434+
switch (status.base.battery_level) {
1435+
case GIP_BATTERY_CRITICAL:
1436+
power_percent = 1;
1437+
break;
1438+
case GIP_BATTERY_LOW:
1439+
power_percent = 25;
1440+
break;
1441+
case GIP_BATTERY_MEDIUM:
1442+
power_percent = 50;
1443+
break;
1444+
case GIP_BATTERY_FULL:
1445+
power_percent = 100;
1446+
break;
1447+
}
1448+
switch (status.base.charge) {
1449+
case GIP_CHARGING:
1450+
if (status.base.battery_level == GIP_BATTERY_FULL) {
1451+
power_state = SDL_POWERSTATE_CHARGED;
1452+
} else {
1453+
power_state = SDL_POWERSTATE_CHARGING;
1454+
}
1455+
break;
1456+
case GIP_NOT_CHARGING:
1457+
power_state = SDL_POWERSTATE_ON_BATTERY;
1458+
break;
1459+
case GIP_CHARGE_ERROR:
1460+
default:
1461+
power_state = SDL_POWERSTATE_UNKNOWN;
1462+
break;
1463+
}
1464+
1465+
switch (status.base.battery_type) {
1466+
case GIP_BATTERY_ABSENT:
1467+
power_state = SDL_POWERSTATE_NO_BATTERY;
1468+
break;
1469+
case GIP_BATTERY_STANDARD:
1470+
case GIP_BATTERY_RECHARGEABLE:
1471+
break;
1472+
default:
1473+
power_state = SDL_POWERSTATE_UNKNOWN;
1474+
break;
1475+
}
1476+
1477+
SDL_SendJoystickPowerInfo(joystick, power_state, power_percent);
1478+
}
1479+
14261480
if (num_bytes >= 4) {
14271481
status.device_active = bytes[1] & 1;
14281482
if (bytes[1] & 2) {

0 commit comments

Comments
 (0)