mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-04-29 10:44:16 +00:00
Update for SDL3 coding style (#6717)
I updated .clang-format and ran clang-format 14 over the src and test directories to standardize the code base.
In general I let clang-format have it's way, and added markup to prevent formatting of code that would break or be completely unreadable if formatted.
The script I ran for the src directory is added as build-scripts/clang-format-src.sh
This fixes:
#6592
#6593
#6594
(cherry picked from commit 5750bcb174)
This commit is contained in:
@@ -41,14 +41,13 @@ static const char *proc_acpi_battery_path = "/proc/acpi/battery";
|
||||
static const char *proc_acpi_ac_adapter_path = "/proc/acpi/ac_adapter";
|
||||
static const char *sys_class_power_supply_path = "/sys/class/power_supply";
|
||||
|
||||
static int
|
||||
open_power_file(const char *base, const char *node, const char *key)
|
||||
static int open_power_file(const char *base, const char *node, const char *key)
|
||||
{
|
||||
int fd;
|
||||
const size_t pathlen = SDL_strlen(base) + SDL_strlen(node) + SDL_strlen(key) + 3;
|
||||
char *path = SDL_stack_alloc(char, pathlen);
|
||||
if (path == NULL) {
|
||||
return -1; /* oh well. */
|
||||
return -1; /* oh well. */
|
||||
}
|
||||
|
||||
SDL_snprintf(path, pathlen, "%s/%s/%s", base, node, key);
|
||||
@@ -57,37 +56,33 @@ open_power_file(const char *base, const char *node, const char *key)
|
||||
return fd;
|
||||
}
|
||||
|
||||
|
||||
static SDL_bool
|
||||
read_power_file(const char *base, const char *node, const char *key,
|
||||
char *buf, size_t buflen)
|
||||
static SDL_bool read_power_file(const char *base, const char *node, const char *key,
|
||||
char *buf, size_t buflen)
|
||||
{
|
||||
ssize_t br = 0;
|
||||
const int fd = open_power_file(base, node, key);
|
||||
if (fd == -1) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
br = read(fd, buf, buflen-1);
|
||||
br = read(fd, buf, buflen - 1);
|
||||
close(fd);
|
||||
if (br < 0) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
buf[br] = '\0'; /* null-terminate the string. */
|
||||
buf[br] = '\0'; /* null-terminate the string. */
|
||||
return SDL_TRUE;
|
||||
}
|
||||
|
||||
|
||||
static SDL_bool
|
||||
make_proc_acpi_key_val(char **_ptr, char **_key, char **_val)
|
||||
static SDL_bool make_proc_acpi_key_val(char **_ptr, char **_key, char **_val)
|
||||
{
|
||||
char *ptr = *_ptr;
|
||||
|
||||
while (*ptr == ' ') {
|
||||
ptr++; /* skip whitespace. */
|
||||
ptr++; /* skip whitespace. */
|
||||
}
|
||||
|
||||
if (*ptr == '\0') {
|
||||
return SDL_FALSE; /* EOF. */
|
||||
return SDL_FALSE; /* EOF. */
|
||||
}
|
||||
|
||||
*_key = ptr;
|
||||
@@ -97,17 +92,17 @@ make_proc_acpi_key_val(char **_ptr, char **_key, char **_val)
|
||||
}
|
||||
|
||||
if (*ptr == '\0') {
|
||||
return SDL_FALSE; /* (unexpected) EOF. */
|
||||
return SDL_FALSE; /* (unexpected) EOF. */
|
||||
}
|
||||
|
||||
*(ptr++) = '\0'; /* terminate the key. */
|
||||
*(ptr++) = '\0'; /* terminate the key. */
|
||||
|
||||
while (*ptr == ' ') {
|
||||
ptr++; /* skip whitespace. */
|
||||
ptr++; /* skip whitespace. */
|
||||
}
|
||||
|
||||
if (*ptr == '\0') {
|
||||
return SDL_FALSE; /* (unexpected) EOF. */
|
||||
return SDL_FALSE; /* (unexpected) EOF. */
|
||||
}
|
||||
|
||||
*_val = ptr;
|
||||
@@ -117,16 +112,15 @@ make_proc_acpi_key_val(char **_ptr, char **_key, char **_val)
|
||||
}
|
||||
|
||||
if (*ptr != '\0') {
|
||||
*(ptr++) = '\0'; /* terminate the value. */
|
||||
*(ptr++) = '\0'; /* terminate the value. */
|
||||
}
|
||||
|
||||
*_ptr = ptr; /* store for next time. */
|
||||
*_ptr = ptr; /* store for next time. */
|
||||
return SDL_TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
check_proc_acpi_battery(const char * node, SDL_bool * have_battery,
|
||||
SDL_bool * charging, int *seconds, int *percent)
|
||||
static void check_proc_acpi_battery(const char *node, SDL_bool *have_battery,
|
||||
SDL_bool *charging, int *seconds, int *percent)
|
||||
{
|
||||
const char *base = proc_acpi_battery_path;
|
||||
char info[1024];
|
||||
@@ -141,9 +135,9 @@ check_proc_acpi_battery(const char * node, SDL_bool * have_battery,
|
||||
int secs = -1;
|
||||
int pct = -1;
|
||||
|
||||
if (!read_power_file(base, node, "state", state, sizeof (state))) {
|
||||
if (!read_power_file(base, node, "state", state, sizeof(state))) {
|
||||
return;
|
||||
} else if (!read_power_file(base, node, "info", info, sizeof (info))) {
|
||||
} else if (!read_power_file(base, node, "info", info, sizeof(info))) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -162,7 +156,7 @@ check_proc_acpi_battery(const char * node, SDL_bool * have_battery,
|
||||
}
|
||||
} else if (SDL_strcmp(key, "remaining capacity") == 0) {
|
||||
char *endptr = NULL;
|
||||
const int cvt = (int) SDL_strtol(val, &endptr, 10);
|
||||
const int cvt = (int)SDL_strtol(val, &endptr, 10);
|
||||
if (*endptr == ' ') {
|
||||
remaining = cvt;
|
||||
}
|
||||
@@ -173,7 +167,7 @@ check_proc_acpi_battery(const char * node, SDL_bool * have_battery,
|
||||
while (make_proc_acpi_key_val(&ptr, &key, &val)) {
|
||||
if (SDL_strcmp(key, "design capacity") == 0) {
|
||||
char *endptr = NULL;
|
||||
const int cvt = (int) SDL_strtol(val, &endptr, 10);
|
||||
const int cvt = (int)SDL_strtol(val, &endptr, 10);
|
||||
if (*endptr == ' ') {
|
||||
maximum = cvt;
|
||||
}
|
||||
@@ -181,7 +175,7 @@ check_proc_acpi_battery(const char * node, SDL_bool * have_battery,
|
||||
}
|
||||
|
||||
if ((maximum >= 0) && (remaining >= 0)) {
|
||||
pct = (int) ((((float) remaining) / ((float) maximum)) * 100.0f);
|
||||
pct = (int)((((float)remaining) / ((float)maximum)) * 100.0f);
|
||||
if (pct < 0) {
|
||||
pct = 0;
|
||||
} else if (pct > 100) {
|
||||
@@ -197,7 +191,7 @@ check_proc_acpi_battery(const char * node, SDL_bool * have_battery,
|
||||
*/
|
||||
if ((secs < 0) && (*seconds < 0)) {
|
||||
if ((pct < 0) && (*percent < 0)) {
|
||||
choose = SDL_TRUE; /* at least we know there's a battery. */
|
||||
choose = SDL_TRUE; /* at least we know there's a battery. */
|
||||
}
|
||||
if (pct > *percent) {
|
||||
choose = SDL_TRUE;
|
||||
@@ -213,8 +207,7 @@ check_proc_acpi_battery(const char * node, SDL_bool * have_battery,
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
check_proc_acpi_ac_adapter(const char * node, SDL_bool * have_ac)
|
||||
static void check_proc_acpi_ac_adapter(const char *node, SDL_bool *have_ac)
|
||||
{
|
||||
const char *base = proc_acpi_ac_adapter_path;
|
||||
char state[256];
|
||||
@@ -222,7 +215,7 @@ check_proc_acpi_ac_adapter(const char * node, SDL_bool * have_ac)
|
||||
char *key = NULL;
|
||||
char *val = NULL;
|
||||
|
||||
if (!read_power_file(base, node, "state", state, sizeof (state))) {
|
||||
if (!read_power_file(base, node, "state", state, sizeof(state))) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -236,9 +229,8 @@ check_proc_acpi_ac_adapter(const char * node, SDL_bool * have_ac)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
SDL_bool
|
||||
SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState * state,
|
||||
SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState *state,
|
||||
int *seconds, int *percent)
|
||||
{
|
||||
struct dirent *dent = NULL;
|
||||
@@ -253,7 +245,7 @@ SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState * state,
|
||||
|
||||
dirp = opendir(proc_acpi_battery_path);
|
||||
if (dirp == NULL) {
|
||||
return SDL_FALSE; /* can't use this interface. */
|
||||
return SDL_FALSE; /* can't use this interface. */
|
||||
} else {
|
||||
while ((dent = readdir(dirp)) != NULL) {
|
||||
const char *node = dent->d_name;
|
||||
@@ -265,7 +257,7 @@ SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState * state,
|
||||
|
||||
dirp = opendir(proc_acpi_ac_adapter_path);
|
||||
if (dirp == NULL) {
|
||||
return SDL_FALSE; /* can't use this interface. */
|
||||
return SDL_FALSE; /* can't use this interface. */
|
||||
} else {
|
||||
while ((dent = readdir(dirp)) != NULL) {
|
||||
const char *node = dent->d_name;
|
||||
@@ -284,17 +276,15 @@ SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState * state,
|
||||
*state = SDL_POWERSTATE_ON_BATTERY;
|
||||
}
|
||||
|
||||
return SDL_TRUE; /* definitive answer. */
|
||||
return SDL_TRUE; /* definitive answer. */
|
||||
}
|
||||
|
||||
|
||||
static SDL_bool
|
||||
next_string(char **_ptr, char **_str)
|
||||
static SDL_bool next_string(char **_ptr, char **_str)
|
||||
{
|
||||
char *ptr = *_ptr;
|
||||
char *str;
|
||||
|
||||
while (*ptr == ' ') { /* skip any spaces... */
|
||||
while (*ptr == ' ') { /* skip any spaces... */
|
||||
ptr++;
|
||||
}
|
||||
|
||||
@@ -316,17 +306,16 @@ next_string(char **_ptr, char **_str)
|
||||
return SDL_TRUE;
|
||||
}
|
||||
|
||||
static SDL_bool
|
||||
int_string(char *str, int *val)
|
||||
static SDL_bool int_string(char *str, int *val)
|
||||
{
|
||||
char *endptr = NULL;
|
||||
*val = (int) SDL_strtol(str, &endptr, 0);
|
||||
*val = (int)SDL_strtol(str, &endptr, 0);
|
||||
return (*str != '\0') && (*endptr == '\0');
|
||||
}
|
||||
|
||||
/* http://lxr.linux.no/linux+v2.6.29/drivers/char/apm-emulation.c */
|
||||
SDL_bool
|
||||
SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState * state,
|
||||
SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState *state,
|
||||
int *seconds, int *percent)
|
||||
{
|
||||
SDL_bool need_details = SDL_FALSE;
|
||||
@@ -342,44 +331,44 @@ SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState * state,
|
||||
ssize_t br;
|
||||
|
||||
if (fd == -1) {
|
||||
return SDL_FALSE; /* can't use this interface. */
|
||||
return SDL_FALSE; /* can't use this interface. */
|
||||
}
|
||||
|
||||
br = read(fd, buf, sizeof (buf) - 1);
|
||||
br = read(fd, buf, sizeof(buf) - 1);
|
||||
close(fd);
|
||||
|
||||
if (br < 0) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
buf[br] = '\0'; /* null-terminate the string. */
|
||||
if (!next_string(&ptr, &str)) { /* driver version */
|
||||
buf[br] = '\0'; /* null-terminate the string. */
|
||||
if (!next_string(&ptr, &str)) { /* driver version */
|
||||
return SDL_FALSE;
|
||||
}
|
||||
if (!next_string(&ptr, &str)) { /* BIOS version */
|
||||
if (!next_string(&ptr, &str)) { /* BIOS version */
|
||||
return SDL_FALSE;
|
||||
}
|
||||
if (!next_string(&ptr, &str)) { /* APM flags */
|
||||
if (!next_string(&ptr, &str)) { /* APM flags */
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
if (!next_string(&ptr, &str)) { /* AC line status */
|
||||
if (!next_string(&ptr, &str)) { /* AC line status */
|
||||
return SDL_FALSE;
|
||||
} else if (!int_string(str, &ac_status)) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
if (!next_string(&ptr, &str)) { /* battery status */
|
||||
if (!next_string(&ptr, &str)) { /* battery status */
|
||||
return SDL_FALSE;
|
||||
} else if (!int_string(str, &battery_status)) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
if (!next_string(&ptr, &str)) { /* battery flag */
|
||||
if (!next_string(&ptr, &str)) { /* battery flag */
|
||||
return SDL_FALSE;
|
||||
} else if (!int_string(str, &battery_flag)) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
if (!next_string(&ptr, &str)) { /* remaining battery life percent */
|
||||
if (!next_string(&ptr, &str)) { /* remaining battery life percent */
|
||||
return SDL_FALSE;
|
||||
}
|
||||
if (str[SDL_strlen(str) - 1] == '%') {
|
||||
@@ -389,13 +378,13 @@ SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState * state,
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
if (!next_string(&ptr, &str)) { /* remaining battery life time */
|
||||
if (!next_string(&ptr, &str)) { /* remaining battery life time */
|
||||
return SDL_FALSE;
|
||||
} else if (!int_string(str, &battery_time)) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
if (!next_string(&ptr, &str)) { /* remaining battery life time units */
|
||||
if (!next_string(&ptr, &str)) { /* remaining battery life time units */
|
||||
return SDL_FALSE;
|
||||
} else if (SDL_strcmp(str, "min") == 0) {
|
||||
battery_time *= 60;
|
||||
@@ -403,13 +392,13 @@ SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState * state,
|
||||
|
||||
if (battery_flag == 0xFF) { /* unknown state */
|
||||
*state = SDL_POWERSTATE_UNKNOWN;
|
||||
} else if (battery_flag & (1 << 7)) { /* no battery */
|
||||
} else if (battery_flag & (1 << 7)) { /* no battery */
|
||||
*state = SDL_POWERSTATE_NO_BATTERY;
|
||||
} else if (battery_flag & (1 << 3)) { /* charging */
|
||||
} else if (battery_flag & (1 << 3)) { /* charging */
|
||||
*state = SDL_POWERSTATE_CHARGING;
|
||||
need_details = SDL_TRUE;
|
||||
} else if (ac_status == 1) {
|
||||
*state = SDL_POWERSTATE_CHARGED; /* on AC, not charging. */
|
||||
*state = SDL_POWERSTATE_CHARGED; /* on AC, not charging. */
|
||||
need_details = SDL_TRUE;
|
||||
} else {
|
||||
*state = SDL_POWERSTATE_ON_BATTERY;
|
||||
@@ -422,10 +411,10 @@ SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState * state,
|
||||
const int pct = battery_percent;
|
||||
const int secs = battery_time;
|
||||
|
||||
if (pct >= 0) { /* -1 == unknown */
|
||||
if (pct >= 0) { /* -1 == unknown */
|
||||
*percent = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */
|
||||
}
|
||||
if (secs >= 0) { /* -1 == unknown */
|
||||
if (secs >= 0) { /* -1 == unknown */
|
||||
*seconds = secs;
|
||||
}
|
||||
}
|
||||
@@ -445,7 +434,7 @@ SDL_GetPowerInfo_Linux_sys_class_power_supply(SDL_PowerState *state, int *second
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
*state = SDL_POWERSTATE_NO_BATTERY; /* assume we're just plugged in. */
|
||||
*state = SDL_POWERSTATE_NO_BATTERY; /* assume we're just plugged in. */
|
||||
*seconds = -1;
|
||||
*percent = -1;
|
||||
|
||||
@@ -460,28 +449,28 @@ SDL_GetPowerInfo_Linux_sys_class_power_supply(SDL_PowerState *state, int *second
|
||||
int power;
|
||||
|
||||
if ((SDL_strcmp(name, ".") == 0) || (SDL_strcmp(name, "..") == 0)) {
|
||||
continue; /* skip these, of course. */
|
||||
} else if (!read_power_file(base, name, "type", str, sizeof (str))) {
|
||||
continue; /* Don't know _what_ we're looking at. Give up on it. */
|
||||
continue; /* skip these, of course. */
|
||||
} else if (!read_power_file(base, name, "type", str, sizeof(str))) {
|
||||
continue; /* Don't know _what_ we're looking at. Give up on it. */
|
||||
} else if (SDL_strcmp(str, "Battery\n") != 0) {
|
||||
continue; /* we don't care about UPS and such. */
|
||||
continue; /* we don't care about UPS and such. */
|
||||
}
|
||||
|
||||
/* if the scope is "device," it might be something like a PS4
|
||||
controller reporting its own battery, and not something that powers
|
||||
the system. Most system batteries don't list a scope at all; we
|
||||
assume it's a system battery if not specified. */
|
||||
if (read_power_file(base, name, "scope", str, sizeof (str))) {
|
||||
if (read_power_file(base, name, "scope", str, sizeof(str))) {
|
||||
if (SDL_strcmp(str, "device\n") == 0) {
|
||||
continue; /* skip external devices with their own batteries. */
|
||||
continue; /* skip external devices with their own batteries. */
|
||||
}
|
||||
}
|
||||
|
||||
/* some drivers don't offer this, so if it's not explicitly reported assume it's present. */
|
||||
if (read_power_file(base, name, "present", str, sizeof (str)) && (SDL_strcmp(str, "0\n") == 0)) {
|
||||
if (read_power_file(base, name, "present", str, sizeof(str)) && (SDL_strcmp(str, "0\n") == 0)) {
|
||||
st = SDL_POWERSTATE_NO_BATTERY;
|
||||
} else if (!read_power_file(base, name, "status", str, sizeof (str))) {
|
||||
st = SDL_POWERSTATE_UNKNOWN; /* uh oh */
|
||||
} else if (!read_power_file(base, name, "status", str, sizeof(str))) {
|
||||
st = SDL_POWERSTATE_UNKNOWN; /* uh oh */
|
||||
} else if (SDL_strcmp(str, "Charging\n") == 0) {
|
||||
st = SDL_POWERSTATE_CHARGING;
|
||||
} else if (SDL_strcmp(str, "Discharging\n") == 0) {
|
||||
@@ -489,23 +478,23 @@ SDL_GetPowerInfo_Linux_sys_class_power_supply(SDL_PowerState *state, int *second
|
||||
} else if ((SDL_strcmp(str, "Full\n") == 0) || (SDL_strcmp(str, "Not charging\n") == 0)) {
|
||||
st = SDL_POWERSTATE_CHARGED;
|
||||
} else {
|
||||
st = SDL_POWERSTATE_UNKNOWN; /* uh oh */
|
||||
st = SDL_POWERSTATE_UNKNOWN; /* uh oh */
|
||||
}
|
||||
|
||||
if (!read_power_file(base, name, "capacity", str, sizeof (str))) {
|
||||
if (!read_power_file(base, name, "capacity", str, sizeof(str))) {
|
||||
pct = -1;
|
||||
} else {
|
||||
pct = SDL_atoi(str);
|
||||
pct = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */
|
||||
}
|
||||
|
||||
if (read_power_file(base, name, "time_to_empty_now", str, sizeof (str))) {
|
||||
if (read_power_file(base, name, "time_to_empty_now", str, sizeof(str))) {
|
||||
secs = SDL_atoi(str);
|
||||
secs = (secs <= 0) ? -1 : secs; /* 0 == unknown */
|
||||
secs = (secs <= 0) ? -1 : secs; /* 0 == unknown */
|
||||
} else if (st == SDL_POWERSTATE_ON_BATTERY) {
|
||||
/* energy is Watt*hours and power is Watts */
|
||||
energy = (read_power_file(base, name, "energy_now", str, sizeof (str))) ? SDL_atoi(str) : -1;
|
||||
power = (read_power_file(base, name, "power_now", str, sizeof (str))) ? SDL_atoi(str) : -1;
|
||||
energy = (read_power_file(base, name, "energy_now", str, sizeof(str))) ? SDL_atoi(str) : -1;
|
||||
power = (read_power_file(base, name, "power_now", str, sizeof(str))) ? SDL_atoi(str) : -1;
|
||||
secs = (energy >= 0 && power > 0) ? (3600LL * energy) / power : -1;
|
||||
} else {
|
||||
secs = -1;
|
||||
@@ -517,7 +506,7 @@ SDL_GetPowerInfo_Linux_sys_class_power_supply(SDL_PowerState *state, int *second
|
||||
*/
|
||||
if ((secs < 0) && (*seconds < 0)) {
|
||||
if ((pct < 0) && (*percent < 0)) {
|
||||
choose = SDL_TRUE; /* at least we know there's a battery. */
|
||||
choose = SDL_TRUE; /* at least we know there's a battery. */
|
||||
} else if (pct > *percent) {
|
||||
choose = SDL_TRUE;
|
||||
}
|
||||
@@ -533,19 +522,17 @@ SDL_GetPowerInfo_Linux_sys_class_power_supply(SDL_PowerState *state, int *second
|
||||
}
|
||||
|
||||
closedir(dirp);
|
||||
return SDL_TRUE; /* don't look any further. */
|
||||
return SDL_TRUE; /* don't look any further. */
|
||||
}
|
||||
|
||||
|
||||
/* d-bus queries to org.freedesktop.UPower. */
|
||||
#if SDL_USE_LIBDBUS
|
||||
#define UPOWER_DBUS_NODE "org.freedesktop.UPower"
|
||||
#define UPOWER_DBUS_PATH "/org/freedesktop/UPower"
|
||||
#define UPOWER_DBUS_INTERFACE "org.freedesktop.UPower"
|
||||
#define UPOWER_DBUS_NODE "org.freedesktop.UPower"
|
||||
#define UPOWER_DBUS_PATH "/org/freedesktop/UPower"
|
||||
#define UPOWER_DBUS_INTERFACE "org.freedesktop.UPower"
|
||||
#define UPOWER_DEVICE_DBUS_INTERFACE "org.freedesktop.UPower.Device"
|
||||
|
||||
static void
|
||||
check_upower_device(DBusConnection *conn, const char *path, SDL_PowerState *state, int *seconds, int *percent)
|
||||
static void check_upower_device(DBusConnection *conn, const char *path, SDL_PowerState *state, int *seconds, int *percent)
|
||||
{
|
||||
SDL_bool choose = SDL_FALSE;
|
||||
SDL_PowerState st;
|
||||
@@ -556,13 +543,13 @@ check_upower_device(DBusConnection *conn, const char *path, SDL_PowerState *stat
|
||||
double d = 0.0;
|
||||
|
||||
if (!SDL_DBus_QueryPropertyOnConnection(conn, UPOWER_DBUS_NODE, path, UPOWER_DEVICE_DBUS_INTERFACE, "Type", DBUS_TYPE_UINT32, &ui32)) {
|
||||
return; /* Don't know _what_ we're looking at. Give up on it. */
|
||||
} else if (ui32 != 2) { /* 2==Battery*/
|
||||
return; /* we don't care about UPS and such. */
|
||||
return; /* Don't know _what_ we're looking at. Give up on it. */
|
||||
} else if (ui32 != 2) { /* 2==Battery*/
|
||||
return; /* we don't care about UPS and such. */
|
||||
} else if (!SDL_DBus_QueryPropertyOnConnection(conn, UPOWER_DBUS_NODE, path, UPOWER_DEVICE_DBUS_INTERFACE, "PowerSupply", DBUS_TYPE_BOOLEAN, &ui32)) {
|
||||
return;
|
||||
} else if (!ui32) {
|
||||
return; /* we don't care about random devices with batteries, like wireless controllers, etc */
|
||||
return; /* we don't care about random devices with batteries, like wireless controllers, etc */
|
||||
}
|
||||
|
||||
if (!SDL_DBus_QueryPropertyOnConnection(conn, UPOWER_DBUS_NODE, path, UPOWER_DEVICE_DBUS_INTERFACE, "IsPresent", DBUS_TYPE_BOOLEAN, &ui32)) {
|
||||
@@ -577,30 +564,30 @@ check_upower_device(DBusConnection *conn, const char *path, SDL_PowerState *stat
|
||||
SDL_DBus_CallMethodOnConnection(conn, UPOWER_DBUS_NODE, path, UPOWER_DEVICE_DBUS_INTERFACE, "Refresh", DBUS_TYPE_INVALID, DBUS_TYPE_INVALID);
|
||||
|
||||
if (!SDL_DBus_QueryPropertyOnConnection(conn, UPOWER_DBUS_NODE, path, UPOWER_DEVICE_DBUS_INTERFACE, "State", DBUS_TYPE_UINT32, &ui32)) {
|
||||
st = SDL_POWERSTATE_UNKNOWN; /* uh oh */
|
||||
} else if (ui32 == 1) { /* 1 == charging */
|
||||
st = SDL_POWERSTATE_UNKNOWN; /* uh oh */
|
||||
} else if (ui32 == 1) { /* 1 == charging */
|
||||
st = SDL_POWERSTATE_CHARGING;
|
||||
} else if ((ui32 == 2) || (ui32 == 3)) { /* 2 == discharging, 3 == empty. */
|
||||
} else if ((ui32 == 2) || (ui32 == 3)) { /* 2 == discharging, 3 == empty. */
|
||||
st = SDL_POWERSTATE_ON_BATTERY;
|
||||
} else if (ui32 == 4) { /* 4 == full */
|
||||
} else if (ui32 == 4) { /* 4 == full */
|
||||
st = SDL_POWERSTATE_CHARGED;
|
||||
} else {
|
||||
st = SDL_POWERSTATE_UNKNOWN; /* uh oh */
|
||||
st = SDL_POWERSTATE_UNKNOWN; /* uh oh */
|
||||
}
|
||||
}
|
||||
|
||||
if (!SDL_DBus_QueryPropertyOnConnection(conn, UPOWER_DBUS_NODE, path, UPOWER_DEVICE_DBUS_INTERFACE, "Percentage", DBUS_TYPE_DOUBLE, &d)) {
|
||||
pct = -1; /* some old/cheap batteries don't set this property. */
|
||||
pct = -1; /* some old/cheap batteries don't set this property. */
|
||||
} else {
|
||||
pct = (int) d;
|
||||
pct = (int)d;
|
||||
pct = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */
|
||||
}
|
||||
|
||||
if (!SDL_DBus_QueryPropertyOnConnection(conn, UPOWER_DBUS_NODE, path, UPOWER_DEVICE_DBUS_INTERFACE, "TimeToEmpty", DBUS_TYPE_INT64, &si64)) {
|
||||
secs = -1;
|
||||
} else {
|
||||
secs = (int) si64;
|
||||
secs = (secs <= 0) ? -1 : secs; /* 0 == unknown */
|
||||
secs = (int)si64;
|
||||
secs = (secs <= 0) ? -1 : secs; /* 0 == unknown */
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -609,7 +596,7 @@ check_upower_device(DBusConnection *conn, const char *path, SDL_PowerState *stat
|
||||
*/
|
||||
if ((secs < 0) && (*seconds < 0)) {
|
||||
if ((pct < 0) && (*percent < 0)) {
|
||||
choose = SDL_TRUE; /* at least we know there's a battery. */
|
||||
choose = SDL_TRUE; /* at least we know there's a battery. */
|
||||
} else if (pct > *percent) {
|
||||
choose = SDL_TRUE;
|
||||
}
|
||||
@@ -638,11 +625,11 @@ SDL_GetPowerInfo_Linux_org_freedesktop_upower(SDL_PowerState *state, int *second
|
||||
if (dbus == NULL || !SDL_DBus_CallMethodOnConnection(dbus->system_conn, UPOWER_DBUS_NODE, UPOWER_DBUS_PATH, UPOWER_DBUS_INTERFACE, "EnumerateDevices",
|
||||
DBUS_TYPE_INVALID,
|
||||
DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &paths, &numpaths, DBUS_TYPE_INVALID)) {
|
||||
return SDL_FALSE; /* try a different approach than UPower. */
|
||||
return SDL_FALSE; /* try a different approach than UPower. */
|
||||
}
|
||||
|
||||
retval = SDL_TRUE; /* Clearly we can use this interface. */
|
||||
*state = SDL_POWERSTATE_NO_BATTERY; /* assume we're just plugged in. */
|
||||
retval = SDL_TRUE; /* Clearly we can use this interface. */
|
||||
*state = SDL_POWERSTATE_NO_BATTERY; /* assume we're just plugged in. */
|
||||
*seconds = -1;
|
||||
*percent = -1;
|
||||
|
||||
@@ -651,7 +638,7 @@ SDL_GetPowerInfo_Linux_org_freedesktop_upower(SDL_PowerState *state, int *second
|
||||
}
|
||||
|
||||
dbus->free_string_array(paths);
|
||||
#endif /* SDL_USE_LIBDBUS */
|
||||
#endif /* SDL_USE_LIBDBUS */
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user