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
This commit is contained in:
@@ -25,15 +25,13 @@
|
||||
* Returns SDL_TRUE if we have a definitive answer.
|
||||
* SDL_FALSE to try next implementation.
|
||||
*/
|
||||
typedef SDL_bool
|
||||
(*SDL_GetPowerInfo_Impl) (SDL_PowerState * state, int *seconds,
|
||||
int *percent);
|
||||
typedef SDL_bool (*SDL_GetPowerInfo_Impl)(SDL_PowerState *state, int *seconds,
|
||||
int *percent);
|
||||
|
||||
#ifndef SDL_POWER_DISABLED
|
||||
#ifdef SDL_POWER_HARDWIRED
|
||||
/* This is for things that _never_ have a battery */
|
||||
static SDL_bool
|
||||
SDL_GetPowerInfo_Hardwired(SDL_PowerState * state, int *seconds, int *percent)
|
||||
static SDL_bool SDL_GetPowerInfo_Hardwired(SDL_PowerState *state, int *seconds, int *percent)
|
||||
{
|
||||
*seconds = -1;
|
||||
*percent = -1;
|
||||
@@ -43,40 +41,40 @@ SDL_GetPowerInfo_Hardwired(SDL_PowerState * state, int *seconds, int *percent)
|
||||
#endif
|
||||
|
||||
static SDL_GetPowerInfo_Impl implementations[] = {
|
||||
#ifdef SDL_POWER_LINUX /* in order of preference. More than could work. */
|
||||
#ifdef SDL_POWER_LINUX /* in order of preference. More than could work. */
|
||||
SDL_GetPowerInfo_Linux_org_freedesktop_upower,
|
||||
SDL_GetPowerInfo_Linux_sys_class_power_supply,
|
||||
SDL_GetPowerInfo_Linux_proc_acpi,
|
||||
SDL_GetPowerInfo_Linux_proc_apm,
|
||||
#endif
|
||||
#ifdef SDL_POWER_WINDOWS /* handles Win32, Win64, PocketPC. */
|
||||
#ifdef SDL_POWER_WINDOWS /* handles Win32, Win64, PocketPC. */
|
||||
SDL_GetPowerInfo_Windows,
|
||||
#endif
|
||||
#ifdef SDL_POWER_UIKIT /* handles iPhone/iPad/etc */
|
||||
#ifdef SDL_POWER_UIKIT /* handles iPhone/iPad/etc */
|
||||
SDL_GetPowerInfo_UIKit,
|
||||
#endif
|
||||
#ifdef SDL_POWER_MACOSX /* handles macOS, Darwin. */
|
||||
#ifdef SDL_POWER_MACOSX /* handles macOS, Darwin. */
|
||||
SDL_GetPowerInfo_MacOSX,
|
||||
#endif
|
||||
#ifdef SDL_POWER_HAIKU /* with BeOS euc.jp apm driver. Does this work on Haiku? */
|
||||
#ifdef SDL_POWER_HAIKU /* with BeOS euc.jp apm driver. Does this work on Haiku? */
|
||||
SDL_GetPowerInfo_Haiku,
|
||||
#endif
|
||||
#ifdef SDL_POWER_ANDROID /* handles Android. */
|
||||
#ifdef SDL_POWER_ANDROID /* handles Android. */
|
||||
SDL_GetPowerInfo_Android,
|
||||
#endif
|
||||
#ifdef SDL_POWER_PSP /* handles PSP. */
|
||||
#ifdef SDL_POWER_PSP /* handles PSP. */
|
||||
SDL_GetPowerInfo_PSP,
|
||||
#endif
|
||||
#ifdef SDL_POWER_VITA /* handles PSVita. */
|
||||
#ifdef SDL_POWER_VITA /* handles PSVita. */
|
||||
SDL_GetPowerInfo_VITA,
|
||||
#endif
|
||||
#ifdef SDL_POWER_N3DS /* handles N3DS. */
|
||||
#ifdef SDL_POWER_N3DS /* handles N3DS. */
|
||||
SDL_GetPowerInfo_N3DS,
|
||||
#endif
|
||||
#ifdef SDL_POWER_WINRT /* handles WinRT */
|
||||
#ifdef SDL_POWER_WINRT /* handles WinRT */
|
||||
SDL_GetPowerInfo_WinRT,
|
||||
#endif
|
||||
#ifdef SDL_POWER_EMSCRIPTEN /* handles Emscripten */
|
||||
#ifdef SDL_POWER_EMSCRIPTEN /* handles Emscripten */
|
||||
SDL_GetPowerInfo_Emscripten,
|
||||
#endif
|
||||
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
#ifndef SDL_syspower_h_
|
||||
#define SDL_syspower_h_
|
||||
|
||||
|
||||
/* Not all of these are available in a given build. Use #ifdefs, etc. */
|
||||
SDL_bool SDL_GetPowerInfo_Linux_org_freedesktop_upower(SDL_PowerState *, int *, int *);
|
||||
SDL_bool SDL_GetPowerInfo_Linux_sys_class_power_supply(SDL_PowerState *, int *, int *);
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
#include "../../core/android/SDL_android.h"
|
||||
|
||||
SDL_bool
|
||||
SDL_GetPowerInfo_Android(SDL_PowerState * state, int *seconds, int *percent)
|
||||
SDL_GetPowerInfo_Android(SDL_PowerState *state, int *seconds, int *percent)
|
||||
{
|
||||
int battery;
|
||||
int plugged;
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
|
||||
#include <emscripten/html5.h>
|
||||
|
||||
|
||||
SDL_bool
|
||||
SDL_GetPowerInfo_Emscripten(SDL_PowerState *state, int *seconds, int *percent)
|
||||
{
|
||||
|
||||
@@ -34,15 +34,14 @@
|
||||
#include <drivers/Drivers.h>
|
||||
|
||||
/* These values are from apm.h ... */
|
||||
#define APM_DEVICE_PATH "/dev/misc/apm"
|
||||
#define APM_FUNC_OFFSET 0x5300
|
||||
#define APM_DEVICE_PATH "/dev/misc/apm"
|
||||
#define APM_FUNC_OFFSET 0x5300
|
||||
#define APM_FUNC_GET_POWER_STATUS 10
|
||||
#define APM_DEVICE_ALL 1
|
||||
#define APM_BIOS_CALL (B_DEVICE_OP_CODES_END + 3)
|
||||
|
||||
#define APM_DEVICE_ALL 1
|
||||
#define APM_BIOS_CALL (B_DEVICE_OP_CODES_END + 3)
|
||||
|
||||
SDL_bool
|
||||
SDL_GetPowerInfo_Haiku(SDL_PowerState * state, int *seconds, int *percent)
|
||||
SDL_GetPowerInfo_Haiku(SDL_PowerState *state, int *seconds, int *percent)
|
||||
{
|
||||
const int fd = open("/dev/misc/apm", O_RDONLY | O_CLOEXEC);
|
||||
SDL_bool need_details = SDL_FALSE;
|
||||
@@ -55,7 +54,7 @@ SDL_GetPowerInfo_Haiku(SDL_PowerState * state, int *seconds, int *percent)
|
||||
int rc;
|
||||
|
||||
if (fd == -1) {
|
||||
return SDL_FALSE; /* maybe some other method will work? */
|
||||
return SDL_FALSE; /* maybe some other method will work? */
|
||||
}
|
||||
|
||||
SDL_memset(regs, '\0', sizeof(regs));
|
||||
@@ -72,10 +71,10 @@ SDL_GetPowerInfo_Haiku(SDL_PowerState * state, int *seconds, int *percent)
|
||||
battery_status = regs[1] & 0xFF;
|
||||
battery_flags = regs[2] >> 8;
|
||||
battery_life = regs[2] & 0xFF;
|
||||
battery_time = (uint32) regs[3];
|
||||
battery_time = (uint32)regs[3];
|
||||
|
||||
/* in theory, _something_ should be set in battery_flags, right? */
|
||||
if (battery_flags == 0x00) { /* older APM BIOS? Less fields. */
|
||||
if (battery_flags == 0x00) { /* older APM BIOS? Less fields. */
|
||||
battery_time = 0xFFFF;
|
||||
if (battery_status == 0xFF) {
|
||||
battery_flags = 0xFF;
|
||||
@@ -89,36 +88,36 @@ SDL_GetPowerInfo_Haiku(SDL_PowerState * state, int *seconds, int *percent)
|
||||
battery_time = (battery_time & 0x7FFF) * 60;
|
||||
}
|
||||
|
||||
if (battery_flags == 0xFF) { /* unknown state */
|
||||
if (battery_flags == 0xFF) { /* unknown state */
|
||||
*state = SDL_POWERSTATE_UNKNOWN;
|
||||
} else if (battery_flags & (1 << 7)) { /* no battery */
|
||||
} else if (battery_flags & (1 << 7)) { /* no battery */
|
||||
*state = SDL_POWERSTATE_NO_BATTERY;
|
||||
} else if (battery_flags & (1 << 3)) { /* charging */
|
||||
} else if (battery_flags & (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; /* not on AC. */
|
||||
*state = SDL_POWERSTATE_ON_BATTERY; /* not on AC. */
|
||||
need_details = SDL_TRUE;
|
||||
}
|
||||
|
||||
*percent = -1;
|
||||
*seconds = -1;
|
||||
if (need_details) {
|
||||
const int pct = (int) battery_life;
|
||||
const int secs = (int) battery_time;
|
||||
const int pct = (int)battery_life;
|
||||
const int secs = (int)battery_time;
|
||||
|
||||
if (pct != 255) { /* 255 == unknown */
|
||||
if (pct != 255) { /* 255 == unknown */
|
||||
*percent = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */
|
||||
}
|
||||
if (secs != 0xFFFF) { /* 0xFFFF == unknown */
|
||||
if (secs != 0xFFFF) { /* 0xFFFF == unknown */
|
||||
*seconds = secs;
|
||||
}
|
||||
}
|
||||
|
||||
return SDL_TRUE; /* the definitive answer if APM driver replied. */
|
||||
return SDL_TRUE; /* the definitive answer if APM driver replied. */
|
||||
}
|
||||
|
||||
#endif /* SDL_POWER_HAIKU */
|
||||
|
||||
@@ -40,14 +40,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);
|
||||
@@ -56,37 +55,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;
|
||||
@@ -96,17 +91,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;
|
||||
@@ -116,16 +111,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];
|
||||
@@ -140,9 +134,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;
|
||||
}
|
||||
|
||||
@@ -161,7 +155,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;
|
||||
}
|
||||
@@ -172,7 +166,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;
|
||||
}
|
||||
@@ -180,7 +174,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) {
|
||||
@@ -196,7 +190,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;
|
||||
@@ -212,8 +206,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];
|
||||
@@ -221,7 +214,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;
|
||||
}
|
||||
|
||||
@@ -235,9 +228,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;
|
||||
@@ -252,7 +244,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;
|
||||
@@ -264,7 +256,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;
|
||||
@@ -283,17 +275,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++;
|
||||
}
|
||||
|
||||
@@ -315,17 +305,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;
|
||||
@@ -341,44 +330,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] == '%') {
|
||||
@@ -388,13 +377,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;
|
||||
@@ -402,13 +391,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;
|
||||
@@ -421,10 +410,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;
|
||||
}
|
||||
}
|
||||
@@ -444,7 +433,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;
|
||||
|
||||
@@ -459,28 +448,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) {
|
||||
@@ -488,23 +477,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;
|
||||
@@ -516,7 +505,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;
|
||||
}
|
||||
@@ -532,19 +521,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;
|
||||
@@ -555,13 +542,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)) {
|
||||
@@ -576,30 +563,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 */
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -608,7 +595,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;
|
||||
}
|
||||
@@ -637,11 +624,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;
|
||||
|
||||
@@ -650,7 +637,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;
|
||||
}
|
||||
|
||||
@@ -27,18 +27,16 @@
|
||||
#include <IOKit/ps/IOPowerSources.h>
|
||||
#include <IOKit/ps/IOPSKeys.h>
|
||||
|
||||
|
||||
/* CoreFoundation is so verbose... */
|
||||
#define STRMATCH(a,b) (CFStringCompare(a, b, 0) == kCFCompareEqualTo)
|
||||
#define GETVAL(k,v) \
|
||||
CFDictionaryGetValueIfPresent(dict, CFSTR(k), (const void **) v)
|
||||
#define STRMATCH(a, b) (CFStringCompare(a, b, 0) == kCFCompareEqualTo)
|
||||
#define GETVAL(k, v) \
|
||||
CFDictionaryGetValueIfPresent(dict, CFSTR(k), (const void **)v)
|
||||
|
||||
/* Note that AC power sources also include a laptop battery it is charging. */
|
||||
static void
|
||||
checkps(CFDictionaryRef dict, SDL_bool * have_ac, SDL_bool * have_battery,
|
||||
SDL_bool * charging, int *seconds, int *percent)
|
||||
static void checkps(CFDictionaryRef dict, SDL_bool *have_ac, SDL_bool *have_battery,
|
||||
SDL_bool *charging, int *seconds, int *percent)
|
||||
{
|
||||
CFStringRef strval; /* don't CFRelease() this. */
|
||||
CFStringRef strval; /* don't CFRelease() this. */
|
||||
CFBooleanRef bval;
|
||||
CFNumberRef numval;
|
||||
SDL_bool charge = SDL_FALSE;
|
||||
@@ -49,7 +47,7 @@ checkps(CFDictionaryRef dict, SDL_bool * have_ac, SDL_bool * have_battery,
|
||||
int pct = -1;
|
||||
|
||||
if ((GETVAL(kIOPSIsPresentKey, &bval)) && (bval == kCFBooleanFalse)) {
|
||||
return; /* nothing to see here. */
|
||||
return; /* nothing to see here. */
|
||||
}
|
||||
|
||||
if (!GETVAL(kIOPSPowerSourceStateKey, &strval)) {
|
||||
@@ -59,7 +57,7 @@ checkps(CFDictionaryRef dict, SDL_bool * have_ac, SDL_bool * have_battery,
|
||||
if (STRMATCH(strval, CFSTR(kIOPSACPowerValue))) {
|
||||
is_ac = *have_ac = SDL_TRUE;
|
||||
} else if (!STRMATCH(strval, CFSTR(kIOPSBatteryPowerValue))) {
|
||||
return; /* not a battery? */
|
||||
return; /* not a battery? */
|
||||
}
|
||||
|
||||
if ((GETVAL(kIOPSIsChargingKey, &bval)) && (bval == kCFBooleanTrue)) {
|
||||
@@ -71,7 +69,7 @@ checkps(CFDictionaryRef dict, SDL_bool * have_ac, SDL_bool * have_battery,
|
||||
CFNumberGetValue(numval, kCFNumberSInt32Type, &val);
|
||||
if (val > 0) {
|
||||
*have_battery = SDL_TRUE;
|
||||
maxpct = (int) val;
|
||||
maxpct = (int)val;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +78,7 @@ checkps(CFDictionaryRef dict, SDL_bool * have_ac, SDL_bool * have_battery,
|
||||
CFNumberGetValue(numval, kCFNumberSInt32Type, &val);
|
||||
if (val > 0) {
|
||||
*have_battery = SDL_TRUE;
|
||||
maxpct = (int) val;
|
||||
maxpct = (int)val;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,23 +88,23 @@ checkps(CFDictionaryRef dict, SDL_bool * have_ac, SDL_bool * have_battery,
|
||||
|
||||
/* macOS reports 0 minutes until empty if you're plugged in. :( */
|
||||
if ((val == 0) && (is_ac)) {
|
||||
val = -1; /* !!! FIXME: calc from timeToFull and capacity? */
|
||||
val = -1; /* !!! FIXME: calc from timeToFull and capacity? */
|
||||
}
|
||||
|
||||
secs = (int) val;
|
||||
secs = (int)val;
|
||||
if (secs > 0) {
|
||||
secs *= 60; /* value is in minutes, so convert to seconds. */
|
||||
secs *= 60; /* value is in minutes, so convert to seconds. */
|
||||
}
|
||||
}
|
||||
|
||||
if (GETVAL(kIOPSCurrentCapacityKey, &numval)) {
|
||||
SInt32 val = -1;
|
||||
CFNumberGetValue(numval, kCFNumberSInt32Type, &val);
|
||||
pct = (int) val;
|
||||
pct = (int)val;
|
||||
}
|
||||
|
||||
if ((pct > 0) && (maxpct > 0)) {
|
||||
pct = (int) ((((double) pct) / ((double) maxpct)) * 100.0);
|
||||
pct = (int)((((double)pct) / ((double)maxpct)) * 100.0);
|
||||
}
|
||||
|
||||
if (pct > 100) {
|
||||
@@ -119,7 +117,7 @@ checkps(CFDictionaryRef dict, SDL_bool * have_ac, 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;
|
||||
@@ -138,9 +136,8 @@ checkps(CFDictionaryRef dict, SDL_bool * have_ac, SDL_bool * have_battery,
|
||||
#undef GETVAL
|
||||
#undef STRMATCH
|
||||
|
||||
|
||||
SDL_bool
|
||||
SDL_GetPowerInfo_MacOSX(SDL_PowerState * state, int *seconds, int *percent)
|
||||
SDL_GetPowerInfo_MacOSX(SDL_PowerState *state, int *seconds, int *percent)
|
||||
{
|
||||
CFTypeRef blob = IOPSCopyPowerSourcesInfo();
|
||||
|
||||
@@ -158,7 +155,7 @@ SDL_GetPowerInfo_MacOSX(SDL_PowerState * state, int *seconds, int *percent)
|
||||
const CFIndex total = CFArrayGetCount(list);
|
||||
CFIndex i;
|
||||
for (i = 0; i < total; i++) {
|
||||
CFTypeRef ps = (CFTypeRef) CFArrayGetValueAtIndex(list, i);
|
||||
CFTypeRef ps = (CFTypeRef)CFArrayGetValueAtIndex(list, i);
|
||||
CFDictionaryRef dict =
|
||||
IOPSGetPowerSourceDescription(blob, ps);
|
||||
if (dict != NULL) {
|
||||
@@ -182,7 +179,7 @@ SDL_GetPowerInfo_MacOSX(SDL_PowerState * state, int *seconds, int *percent)
|
||||
CFRelease(blob);
|
||||
}
|
||||
|
||||
return SDL_TRUE; /* always the definitive answer on macOS. */
|
||||
return SDL_TRUE; /* always the definitive answer on macOS. */
|
||||
}
|
||||
|
||||
#endif /* SDL_POWER_MACOSX */
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
|
||||
#include <3ds.h>
|
||||
|
||||
|
||||
SDL_FORCE_INLINE SDL_PowerState GetPowerState(void);
|
||||
SDL_FORCE_INLINE int ReadStateFromPTMU(bool *is_plugged, u8 *is_charging);
|
||||
SDL_FORCE_INLINE int GetBatteryPercentage(void);
|
||||
@@ -101,7 +100,7 @@ GetBatteryPercentage(void)
|
||||
|
||||
mcuHwcExit();
|
||||
|
||||
return (int) SDL_round(data[0] + data[1] / 256.0);
|
||||
return (int)SDL_round(data[0] + data[1] / 256.0);
|
||||
}
|
||||
|
||||
#endif /* !SDL_POWER_DISABLED && SDL_POWER_N3DS */
|
||||
|
||||
@@ -26,10 +26,9 @@
|
||||
|
||||
#include <psppower.h>
|
||||
|
||||
|
||||
SDL_bool
|
||||
SDL_GetPowerInfo_PSP(SDL_PowerState * state, int *seconds,
|
||||
int *percent)
|
||||
SDL_GetPowerInfo_PSP(SDL_PowerState *state, int *seconds,
|
||||
int *percent)
|
||||
{
|
||||
int battery = scePowerIsBatteryExist();
|
||||
int plugged = scePowerIsPowerOnline();
|
||||
@@ -46,19 +45,18 @@ SDL_GetPowerInfo_PSP(SDL_PowerState * state, int *seconds,
|
||||
} else if (charging) {
|
||||
*state = SDL_POWERSTATE_CHARGING;
|
||||
*percent = scePowerGetBatteryLifePercent();
|
||||
*seconds = scePowerGetBatteryLifeTime()*60;
|
||||
*seconds = scePowerGetBatteryLifeTime() * 60;
|
||||
} else if (plugged) {
|
||||
*state = SDL_POWERSTATE_CHARGED;
|
||||
*percent = scePowerGetBatteryLifePercent();
|
||||
*seconds = scePowerGetBatteryLifeTime()*60;
|
||||
*seconds = scePowerGetBatteryLifeTime() * 60;
|
||||
} else {
|
||||
*state = SDL_POWERSTATE_ON_BATTERY;
|
||||
*percent = scePowerGetBatteryLifePercent();
|
||||
*seconds = scePowerGetBatteryLifeTime()*60;
|
||||
*seconds = scePowerGetBatteryLifeTime() * 60;
|
||||
}
|
||||
|
||||
|
||||
return SDL_TRUE; /* always the definitive answer on PSP. */
|
||||
return SDL_TRUE; /* always the definitive answer on PSP. */
|
||||
}
|
||||
|
||||
#endif /* SDL_POWER_PSP */
|
||||
|
||||
@@ -22,9 +22,8 @@
|
||||
|
||||
#if SDL_POWER_UIKIT
|
||||
|
||||
|
||||
void SDL_UIKit_UpdateBatteryMonitoring(void);
|
||||
SDL_bool SDL_GetPowerInfo_UIKit(SDL_PowerState * state, int *seconds, int *percent);
|
||||
SDL_bool SDL_GetPowerInfo_UIKit(SDL_PowerState *state, int *seconds, int *percent);
|
||||
|
||||
#endif /* SDL_POWER_UIKIT */
|
||||
|
||||
|
||||
@@ -32,8 +32,7 @@
|
||||
static const int BATTERY_MONITORING_TIMEOUT = 3000;
|
||||
static Uint32 SDL_UIKitLastPowerInfoQuery = 0;
|
||||
|
||||
void
|
||||
SDL_UIKit_UpdateBatteryMonitoring(void)
|
||||
void SDL_UIKit_UpdateBatteryMonitoring(void)
|
||||
{
|
||||
if (SDL_UIKitLastPowerInfoQuery) {
|
||||
if (SDL_TICKS_PASSED(SDL_GetTicks(), SDL_UIKitLastPowerInfoQuery + BATTERY_MONITORING_TIMEOUT)) {
|
||||
@@ -45,21 +44,20 @@ SDL_UIKit_UpdateBatteryMonitoring(void)
|
||||
}
|
||||
}
|
||||
#else
|
||||
void
|
||||
SDL_UIKit_UpdateBatteryMonitoring(void)
|
||||
void SDL_UIKit_UpdateBatteryMonitoring(void)
|
||||
{
|
||||
/* Do nothing. */
|
||||
}
|
||||
#endif /* !TARGET_OS_TV */
|
||||
|
||||
SDL_bool
|
||||
SDL_GetPowerInfo_UIKit(SDL_PowerState * state, int *seconds, int *percent)
|
||||
SDL_GetPowerInfo_UIKit(SDL_PowerState *state, int *seconds, int *percent)
|
||||
{
|
||||
#if TARGET_OS_TV
|
||||
*state = SDL_POWERSTATE_NO_BATTERY;
|
||||
*seconds = -1;
|
||||
*percent = -1;
|
||||
#else /* TARGET_OS_TV */
|
||||
#else /* TARGET_OS_TV */
|
||||
@autoreleasepool {
|
||||
UIDevice *uidev = [UIDevice currentDevice];
|
||||
|
||||
@@ -75,7 +73,7 @@ SDL_GetPowerInfo_UIKit(SDL_PowerState * state, int *seconds, int *percent)
|
||||
*/
|
||||
SDL_UIKitLastPowerInfoQuery = SDL_GetTicks();
|
||||
|
||||
*seconds = -1; /* no API to estimate this in UIKit. */
|
||||
*seconds = -1; /* no API to estimate this in UIKit. */
|
||||
|
||||
switch (uidev.batteryState) {
|
||||
case UIDeviceBatteryStateCharging:
|
||||
@@ -97,7 +95,7 @@ SDL_GetPowerInfo_UIKit(SDL_PowerState * state, int *seconds, int *percent)
|
||||
}
|
||||
|
||||
const float level = uidev.batteryLevel;
|
||||
*percent = ( (level < 0.0f) ? -1 : ((int) ((level * 100) + 0.5f)) );
|
||||
*percent = ((level < 0.0f) ? -1 : ((int)((level * 100) + 0.5f)));
|
||||
}
|
||||
#endif /* TARGET_OS_TV */
|
||||
|
||||
|
||||
@@ -26,10 +26,9 @@
|
||||
|
||||
#include <psp2/power.h>
|
||||
|
||||
|
||||
SDL_bool
|
||||
SDL_GetPowerInfo_VITA(SDL_PowerState * state, int *seconds,
|
||||
int *percent)
|
||||
SDL_GetPowerInfo_VITA(SDL_PowerState *state, int *seconds,
|
||||
int *percent)
|
||||
{
|
||||
int battery = 1;
|
||||
int plugged = scePowerIsPowerOnline();
|
||||
@@ -46,19 +45,18 @@ SDL_GetPowerInfo_VITA(SDL_PowerState * state, int *seconds,
|
||||
} else if (charging) {
|
||||
*state = SDL_POWERSTATE_CHARGING;
|
||||
*percent = scePowerGetBatteryLifePercent();
|
||||
*seconds = scePowerGetBatteryLifeTime()*60;
|
||||
*seconds = scePowerGetBatteryLifeTime() * 60;
|
||||
} else if (plugged) {
|
||||
*state = SDL_POWERSTATE_CHARGED;
|
||||
*percent = scePowerGetBatteryLifePercent();
|
||||
*seconds = scePowerGetBatteryLifeTime()*60;
|
||||
*seconds = scePowerGetBatteryLifeTime() * 60;
|
||||
} else {
|
||||
*state = SDL_POWERSTATE_ON_BATTERY;
|
||||
*percent = scePowerGetBatteryLifePercent();
|
||||
*seconds = scePowerGetBatteryLifeTime()*60;
|
||||
*seconds = scePowerGetBatteryLifeTime() * 60;
|
||||
}
|
||||
|
||||
|
||||
return SDL_TRUE; /* always the definitive answer on VITA. */
|
||||
return SDL_TRUE; /* always the definitive answer on VITA. */
|
||||
}
|
||||
|
||||
#endif /* SDL_POWER_VITA */
|
||||
|
||||
@@ -25,9 +25,8 @@
|
||||
|
||||
#include "../../core/windows/SDL_windows.h"
|
||||
|
||||
|
||||
SDL_bool
|
||||
SDL_GetPowerInfo_Windows(SDL_PowerState * state, int *seconds, int *percent)
|
||||
SDL_GetPowerInfo_Windows(SDL_PowerState *state, int *seconds, int *percent)
|
||||
{
|
||||
SYSTEM_POWER_STATUS status;
|
||||
SDL_bool need_details = SDL_FALSE;
|
||||
@@ -36,7 +35,7 @@ SDL_GetPowerInfo_Windows(SDL_PowerState * state, int *seconds, int *percent)
|
||||
if (!GetSystemPowerStatus(&status)) {
|
||||
/* !!! FIXME: push GetLastError() into SDL_GetError() */
|
||||
*state = SDL_POWERSTATE_UNKNOWN;
|
||||
} else if (status.BatteryFlag == 0xFF) { /* unknown state */
|
||||
} else if (status.BatteryFlag == 0xFF) { /* unknown state */
|
||||
*state = SDL_POWERSTATE_UNKNOWN;
|
||||
} else if (status.BatteryFlag & (1 << 7)) { /* no battery */
|
||||
*state = SDL_POWERSTATE_NO_BATTERY;
|
||||
@@ -44,28 +43,28 @@ SDL_GetPowerInfo_Windows(SDL_PowerState * state, int *seconds, int *percent)
|
||||
*state = SDL_POWERSTATE_CHARGING;
|
||||
need_details = SDL_TRUE;
|
||||
} else if (status.ACLineStatus == 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; /* not on AC. */
|
||||
*state = SDL_POWERSTATE_ON_BATTERY; /* not on AC. */
|
||||
need_details = SDL_TRUE;
|
||||
}
|
||||
|
||||
*percent = -1;
|
||||
*seconds = -1;
|
||||
if (need_details) {
|
||||
const int pct = (int) status.BatteryLifePercent;
|
||||
const int secs = (int) status.BatteryLifeTime;
|
||||
const int pct = (int)status.BatteryLifePercent;
|
||||
const int secs = (int)status.BatteryLifeTime;
|
||||
|
||||
if (pct != 255) { /* 255 == unknown */
|
||||
if (pct != 255) { /* 255 == unknown */
|
||||
*percent = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */
|
||||
}
|
||||
if (secs != 0xFFFFFFFF) { /* ((DWORD)-1) == unknown */
|
||||
if (secs != 0xFFFFFFFF) { /* ((DWORD)-1) == unknown */
|
||||
*seconds = secs;
|
||||
}
|
||||
}
|
||||
|
||||
return SDL_TRUE; /* always the definitive answer on Windows. */
|
||||
return SDL_TRUE; /* always the definitive answer on Windows. */
|
||||
}
|
||||
|
||||
#endif /* SDL_POWER_WINDOWS */
|
||||
|
||||
@@ -23,10 +23,8 @@
|
||||
#ifndef SDL_POWER_DISABLED
|
||||
#if SDL_POWER_WINRT
|
||||
|
||||
|
||||
extern "C"
|
||||
SDL_bool
|
||||
SDL_GetPowerInfo_WinRT(SDL_PowerState * state, int *seconds, int *percent)
|
||||
extern "C" SDL_bool
|
||||
SDL_GetPowerInfo_WinRT(SDL_PowerState *state, int *seconds, int *percent)
|
||||
{
|
||||
/* TODO, WinRT: Battery info is available on at least one WinRT platform (Windows Phone 8). Implement SDL_GetPowerInfo_WinRT as appropriate. */
|
||||
/* Notes:
|
||||
|
||||
Reference in New Issue
Block a user