diff options
Diffstat (limited to 'source/3rd-party/SDL2/src/power')
-rw-r--r-- | source/3rd-party/SDL2/src/power/SDL_power.c | 116 | ||||
-rw-r--r-- | source/3rd-party/SDL2/src/power/SDL_syspower.h | 49 | ||||
-rw-r--r-- | source/3rd-party/SDL2/src/power/android/SDL_syspower.c | 64 | ||||
-rw-r--r-- | source/3rd-party/SDL2/src/power/emscripten/SDL_syspower.c | 62 | ||||
-rw-r--r-- | source/3rd-party/SDL2/src/power/haiku/SDL_syspower.c | 128 | ||||
-rw-r--r-- | source/3rd-party/SDL2/src/power/linux/SDL_syspower.c | 642 | ||||
-rw-r--r-- | source/3rd-party/SDL2/src/power/macosx/SDL_syspower.c | 192 | ||||
-rw-r--r-- | source/3rd-party/SDL2/src/power/psp/SDL_syspower.c | 68 | ||||
-rw-r--r-- | source/3rd-party/SDL2/src/power/uikit/SDL_syspower.h | 32 | ||||
-rw-r--r-- | source/3rd-party/SDL2/src/power/uikit/SDL_syspower.m | 113 | ||||
-rw-r--r-- | source/3rd-party/SDL2/src/power/windows/SDL_syspower.c | 76 | ||||
-rw-r--r-- | source/3rd-party/SDL2/src/power/winrt/SDL_syspower.cpp | 44 |
12 files changed, 1586 insertions, 0 deletions
diff --git a/source/3rd-party/SDL2/src/power/SDL_power.c b/source/3rd-party/SDL2/src/power/SDL_power.c new file mode 100644 index 0000000..de77c09 --- /dev/null +++ b/source/3rd-party/SDL2/src/power/SDL_power.c @@ -0,0 +1,116 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org> + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../SDL_internal.h" +#include "SDL_power.h" +#include "SDL_syspower.h" + +/* + * 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); + +#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) +{ + *seconds = -1; + *percent = -1; + *state = SDL_POWERSTATE_NO_BATTERY; + return SDL_TRUE; +} +#endif + +static SDL_GetPowerInfo_Impl implementations[] = { +#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. */ + SDL_GetPowerInfo_Windows, +#endif +#ifdef SDL_POWER_UIKIT /* handles iPhone/iPad/etc */ + SDL_GetPowerInfo_UIKit, +#endif +#ifdef SDL_POWER_MACOSX /* handles Mac OS X, Darwin. */ + SDL_GetPowerInfo_MacOSX, +#endif +#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. */ + SDL_GetPowerInfo_Android, +#endif +#ifdef SDL_POWER_PSP /* handles PSP. */ + SDL_GetPowerInfo_PSP, +#endif +#ifdef SDL_POWER_WINRT /* handles WinRT */ + SDL_GetPowerInfo_WinRT, +#endif +#ifdef SDL_POWER_EMSCRIPTEN /* handles Emscripten */ + SDL_GetPowerInfo_Emscripten, +#endif + +#ifdef SDL_POWER_HARDWIRED + SDL_GetPowerInfo_Hardwired, +#endif +}; +#endif + +SDL_PowerState +SDL_GetPowerInfo(int *seconds, int *percent) +{ +#ifndef SDL_POWER_DISABLED + const int total = sizeof(implementations) / sizeof(implementations[0]); + SDL_PowerState retval = SDL_POWERSTATE_UNKNOWN; + int i; +#endif + + int _seconds, _percent; + /* Make these never NULL for platform-specific implementations. */ + if (seconds == NULL) { + seconds = &_seconds; + } + if (percent == NULL) { + percent = &_percent; + } + +#ifndef SDL_POWER_DISABLED + for (i = 0; i < total; i++) { + if (implementations[i](&retval, seconds, percent)) { + return retval; + } + } +#endif + + /* nothing was definitive. */ + *seconds = -1; + *percent = -1; + return SDL_POWERSTATE_UNKNOWN; +} + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/source/3rd-party/SDL2/src/power/SDL_syspower.h b/source/3rd-party/SDL2/src/power/SDL_syspower.h new file mode 100644 index 0000000..afd6268 --- /dev/null +++ b/source/3rd-party/SDL2/src/power/SDL_syspower.h @@ -0,0 +1,49 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org> + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../SDL_internal.h" + +/* These are functions that need to be implemented by a port of SDL */ + +#ifndef SDL_syspower_h_ +#define SDL_syspower_h_ + +#include "SDL_power.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 *); +SDL_bool SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState *, int *, int *); +SDL_bool SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState *, int *, int *); +SDL_bool SDL_GetPowerInfo_Windows(SDL_PowerState *, int *, int *); +SDL_bool SDL_GetPowerInfo_UIKit(SDL_PowerState *, int *, int *); +SDL_bool SDL_GetPowerInfo_MacOSX(SDL_PowerState *, int *, int *); +SDL_bool SDL_GetPowerInfo_Haiku(SDL_PowerState *, int *, int *); +SDL_bool SDL_GetPowerInfo_Android(SDL_PowerState *, int *, int *); +SDL_bool SDL_GetPowerInfo_PSP(SDL_PowerState *, int *, int *); +SDL_bool SDL_GetPowerInfo_WinRT(SDL_PowerState *, int *, int *); +SDL_bool SDL_GetPowerInfo_Emscripten(SDL_PowerState *, int *, int *); + +/* this one is static in SDL_power.c */ +/* SDL_bool SDL_GetPowerInfo_Hardwired(SDL_PowerState *, int *, int *);*/ + +#endif /* SDL_syspower_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/source/3rd-party/SDL2/src/power/android/SDL_syspower.c b/source/3rd-party/SDL2/src/power/android/SDL_syspower.c new file mode 100644 index 0000000..f0f492d --- /dev/null +++ b/source/3rd-party/SDL2/src/power/android/SDL_syspower.c @@ -0,0 +1,64 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org> + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_POWER_DISABLED +#if SDL_POWER_ANDROID + +#include "SDL_power.h" +#include "../SDL_syspower.h" + +#include "../../core/android/SDL_android.h" + +SDL_bool +SDL_GetPowerInfo_Android(SDL_PowerState * state, int *seconds, int *percent) +{ + int battery; + int plugged; + int charged; + + if (Android_JNI_GetPowerInfo(&plugged, &charged, &battery, seconds, percent) != -1) { + if (plugged) { + if (charged) { + *state = SDL_POWERSTATE_CHARGED; + } else if (battery) { + *state = SDL_POWERSTATE_CHARGING; + } else { + *state = SDL_POWERSTATE_NO_BATTERY; + *seconds = -1; + *percent = -1; + } + } else { + *state = SDL_POWERSTATE_ON_BATTERY; + } + } else { + *state = SDL_POWERSTATE_UNKNOWN; + *seconds = -1; + *percent = -1; + } + + return SDL_TRUE; +} + +#endif /* SDL_POWER_ANDROID */ +#endif /* SDL_POWER_DISABLED */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/source/3rd-party/SDL2/src/power/emscripten/SDL_syspower.c b/source/3rd-party/SDL2/src/power/emscripten/SDL_syspower.c new file mode 100644 index 0000000..9b92168 --- /dev/null +++ b/source/3rd-party/SDL2/src/power/emscripten/SDL_syspower.c @@ -0,0 +1,62 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org> + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_POWER_DISABLED +#if SDL_POWER_EMSCRIPTEN + +#include <emscripten/html5.h> + +#include "SDL_power.h" + +SDL_bool +SDL_GetPowerInfo_Emscripten(SDL_PowerState *state, int *seconds, int *percent) +{ + EmscriptenBatteryEvent batteryState; + int haveBattery = 0; + + if (emscripten_get_battery_status(&batteryState) == EMSCRIPTEN_RESULT_NOT_SUPPORTED) + return SDL_FALSE; + + haveBattery = batteryState.level != 1.0 || !batteryState.charging || batteryState.chargingTime != 0.0; + + if (!haveBattery) { + *state = SDL_POWERSTATE_NO_BATTERY; + *seconds = -1; + *percent = -1; + return SDL_TRUE; + } + + if (batteryState.charging) + *state = batteryState.chargingTime == 0.0 ? SDL_POWERSTATE_CHARGED : SDL_POWERSTATE_CHARGING; + else + *state = SDL_POWERSTATE_ON_BATTERY; + + *seconds = batteryState.dischargingTime; + *percent = batteryState.level * 100; + + return SDL_TRUE; +} + +#endif /* SDL_POWER_EMSCRIPTEN */ +#endif /* SDL_POWER_DISABLED */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/source/3rd-party/SDL2/src/power/haiku/SDL_syspower.c b/source/3rd-party/SDL2/src/power/haiku/SDL_syspower.c new file mode 100644 index 0000000..47961bb --- /dev/null +++ b/source/3rd-party/SDL2/src/power/haiku/SDL_syspower.c @@ -0,0 +1,128 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org> + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +/* uses BeOS euc.jp apm driver. */ +/* !!! FIXME: does this thing even work on Haiku? */ + +#ifndef SDL_POWER_DISABLED +#if SDL_POWER_HAIKU + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <fcntl.h> +#include <ctype.h> +#include <drivers/Drivers.h> + +/* These values are from apm.h ... */ +#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) + +#include "SDL_power.h" + +SDL_bool +SDL_GetPowerInfo_Haiku(SDL_PowerState * state, int *seconds, int *percent) +{ + const int fd = open("/dev/misc/apm", O_RDONLY); + SDL_bool need_details = SDL_FALSE; + uint16 regs[6]; + uint8 ac_status; + uint8 battery_status; + uint8 battery_flags; + uint8 battery_life; + uint32 battery_time; + int rc; + + if (fd == -1) { + return SDL_FALSE; /* maybe some other method will work? */ + } + + memset(regs, '\0', sizeof(regs)); + regs[0] = APM_FUNC_OFFSET + APM_FUNC_GET_POWER_STATUS; + regs[1] = APM_DEVICE_ALL; + rc = ioctl(fd, APM_BIOS_CALL, regs); + close(fd); + + if (rc < 0) { + return SDL_FALSE; + } + + ac_status = regs[1] >> 8; + battery_status = regs[1] & 0xFF; + battery_flags = regs[2] >> 8; + battery_life = regs[2] & 0xFF; + battery_time = (uint32) regs[3]; + + /* in theory, _something_ should be set in battery_flags, right? */ + if (battery_flags == 0x00) { /* older APM BIOS? Less fields. */ + battery_time = 0xFFFF; + if (battery_status == 0xFF) { + battery_flags = 0xFF; + } else { + battery_flags = (1 << battery_status); + } + } + + if ((battery_time != 0xFFFF) && (battery_time & (1 << 15))) { + /* time is in minutes, not seconds */ + battery_time = (battery_time & 0x7FFF) * 60; + } + + if (battery_flags == 0xFF) { /* unknown state */ + *state = SDL_POWERSTATE_UNKNOWN; + } else if (battery_flags & (1 << 7)) { /* no battery */ + *state = SDL_POWERSTATE_NO_BATTERY; + } 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. */ + need_details = SDL_TRUE; + } else { + *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; + + if (pct != 255) { /* 255 == unknown */ + *percent = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */ + } + if (secs != 0xFFFF) { /* 0xFFFF == unknown */ + *seconds = secs; + } + } + + return SDL_TRUE; /* the definitive answer if APM driver replied. */ +} + +#endif /* SDL_POWER_HAIKU */ +#endif /* SDL_POWER_DISABLED */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/source/3rd-party/SDL2/src/power/linux/SDL_syspower.c b/source/3rd-party/SDL2/src/power/linux/SDL_syspower.c new file mode 100644 index 0000000..e6c0c1c --- /dev/null +++ b/source/3rd-party/SDL2/src/power/linux/SDL_syspower.c @@ -0,0 +1,642 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org> + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_POWER_DISABLED +#if SDL_POWER_LINUX + +#include <stdio.h> +#include <unistd.h> + +#include <sys/types.h> +#include <sys/stat.h> +#include <dirent.h> +#include <fcntl.h> + +#include "SDL_power.h" +#include "../SDL_syspower.h" + +#include "../../core/linux/SDL_dbus.h" + +static const char *proc_apm_path = "/proc/apm"; +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) +{ + const size_t pathlen = strlen(base) + strlen(node) + strlen(key) + 3; + char *path = (char *) alloca(pathlen); + if (path == NULL) { + return -1; /* oh well. */ + } + + snprintf(path, pathlen, "%s/%s/%s", base, node, key); + return open(path, O_RDONLY); +} + + +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); + close(fd); + if (br < 0) { + return SDL_FALSE; + } + buf[br] = '\0'; /* null-terminate the string. */ + return SDL_TRUE; +} + + +static SDL_bool +make_proc_acpi_key_val(char **_ptr, char **_key, char **_val) +{ + char *ptr = *_ptr; + + while (*ptr == ' ') { + ptr++; /* skip whitespace. */ + } + + if (*ptr == '\0') { + return SDL_FALSE; /* EOF. */ + } + + *_key = ptr; + + while ((*ptr != ':') && (*ptr != '\0')) { + ptr++; + } + + if (*ptr == '\0') { + return SDL_FALSE; /* (unexpected) EOF. */ + } + + *(ptr++) = '\0'; /* terminate the key. */ + + while ((*ptr == ' ') && (*ptr != '\0')) { + ptr++; /* skip whitespace. */ + } + + if (*ptr == '\0') { + return SDL_FALSE; /* (unexpected) EOF. */ + } + + *_val = ptr; + + while ((*ptr != '\n') && (*ptr != '\0')) { + ptr++; + } + + if (*ptr != '\0') { + *(ptr++) = '\0'; /* terminate the value. */ + } + + *_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) +{ + const char *base = proc_acpi_battery_path; + char info[1024]; + char state[1024]; + char *ptr = NULL; + char *key = NULL; + char *val = NULL; + SDL_bool charge = SDL_FALSE; + SDL_bool choose = SDL_FALSE; + int maximum = -1; + int remaining = -1; + int secs = -1; + int pct = -1; + + if (!read_power_file(base, node, "state", state, sizeof (state))) { + return; + } else if (!read_power_file(base, node, "info", info, sizeof (info))) { + return; + } + + ptr = &state[0]; + while (make_proc_acpi_key_val(&ptr, &key, &val)) { + if (strcmp(key, "present") == 0) { + if (strcmp(val, "yes") == 0) { + *have_battery = SDL_TRUE; + } + } else if (strcmp(key, "charging state") == 0) { + /* !!! FIXME: what exactly _does_ charging/discharging mean? */ + if (strcmp(val, "charging/discharging") == 0) { + charge = SDL_TRUE; + } else if (strcmp(val, "charging") == 0) { + charge = SDL_TRUE; + } + } else if (strcmp(key, "remaining capacity") == 0) { + char *endptr = NULL; + const int cvt = (int) strtol(val, &endptr, 10); + if (*endptr == ' ') { + remaining = cvt; + } + } + } + + ptr = &info[0]; + while (make_proc_acpi_key_val(&ptr, &key, &val)) { + if (strcmp(key, "design capacity") == 0) { + char *endptr = NULL; + const int cvt = (int) strtol(val, &endptr, 10); + if (*endptr == ' ') { + maximum = cvt; + } + } + } + + if ((maximum >= 0) && (remaining >= 0)) { + pct = (int) ((((float) remaining) / ((float) maximum)) * 100.0f); + if (pct < 0) { + pct = 0; + } else if (pct > 100) { + pct = 100; + } + } + + /* !!! FIXME: calculate (secs). */ + + /* + * We pick the battery that claims to have the most minutes left. + * (failing a report of minutes, we'll take the highest percent.) + */ + if ((secs < 0) && (*seconds < 0)) { + if ((pct < 0) && (*percent < 0)) { + choose = SDL_TRUE; /* at least we know there's a battery. */ + } + if (pct > *percent) { + choose = SDL_TRUE; + } + } else if (secs > *seconds) { + choose = SDL_TRUE; + } + + if (choose) { + *seconds = secs; + *percent = pct; + *charging = charge; + } +} + +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]; + char *ptr = NULL; + char *key = NULL; + char *val = NULL; + + if (!read_power_file(base, node, "state", state, sizeof (state))) { + return; + } + + ptr = &state[0]; + while (make_proc_acpi_key_val(&ptr, &key, &val)) { + if (strcmp(key, "state") == 0) { + if (strcmp(val, "on-line") == 0) { + *have_ac = SDL_TRUE; + } + } + } +} + + +SDL_bool +SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState * state, + int *seconds, int *percent) +{ + struct dirent *dent = NULL; + DIR *dirp = NULL; + SDL_bool have_battery = SDL_FALSE; + SDL_bool have_ac = SDL_FALSE; + SDL_bool charging = SDL_FALSE; + + *seconds = -1; + *percent = -1; + *state = SDL_POWERSTATE_UNKNOWN; + + dirp = opendir(proc_acpi_battery_path); + if (dirp == NULL) { + return SDL_FALSE; /* can't use this interface. */ + } else { + while ((dent = readdir(dirp)) != NULL) { + const char *node = dent->d_name; + check_proc_acpi_battery(node, &have_battery, &charging, + seconds, percent); + } + closedir(dirp); + } + + dirp = opendir(proc_acpi_ac_adapter_path); + if (dirp == NULL) { + return SDL_FALSE; /* can't use this interface. */ + } else { + while ((dent = readdir(dirp)) != NULL) { + const char *node = dent->d_name; + check_proc_acpi_ac_adapter(node, &have_ac); + } + closedir(dirp); + } + + if (!have_battery) { + *state = SDL_POWERSTATE_NO_BATTERY; + } else if (charging) { + *state = SDL_POWERSTATE_CHARGING; + } else if (have_ac) { + *state = SDL_POWERSTATE_CHARGED; + } else { + *state = SDL_POWERSTATE_ON_BATTERY; + } + + return SDL_TRUE; /* definitive answer. */ +} + + +static SDL_bool +next_string(char **_ptr, char **_str) +{ + char *ptr = *_ptr; + char *str = *_str; + + while (*ptr == ' ') { /* skip any spaces... */ + ptr++; + } + + if (*ptr == '\0') { + return SDL_FALSE; + } + + str = ptr; + while ((*ptr != ' ') && (*ptr != '\n') && (*ptr != '\0')) + ptr++; + + if (*ptr != '\0') + *(ptr++) = '\0'; + + *_str = str; + *_ptr = ptr; + return SDL_TRUE; +} + +static SDL_bool +int_string(char *str, int *val) +{ + char *endptr = NULL; + *val = (int) 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, + int *seconds, int *percent) +{ + SDL_bool need_details = SDL_FALSE; + int ac_status = 0; + int battery_status = 0; + int battery_flag = 0; + int battery_percent = 0; + int battery_time = 0; + const int fd = open(proc_apm_path, O_RDONLY); + char buf[128]; + char *ptr = &buf[0]; + char *str = NULL; + ssize_t br; + + if (fd == -1) { + return SDL_FALSE; /* can't use this interface. */ + } + + 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 */ + return SDL_FALSE; + } + if (!next_string(&ptr, &str)) { /* BIOS version */ + return SDL_FALSE; + } + if (!next_string(&ptr, &str)) { /* APM flags */ + return SDL_FALSE; + } + + 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 */ + return SDL_FALSE; + } else if (!int_string(str, &battery_status)) { + return SDL_FALSE; + } + 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 */ + return SDL_FALSE; + } + if (str[strlen(str) - 1] == '%') { + str[strlen(str) - 1] = '\0'; + } + if (!int_string(str, &battery_percent)) { + return SDL_FALSE; + } + + 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 */ + return SDL_FALSE; + } else if (strcmp(str, "min") == 0) { + battery_time *= 60; + } + + if (battery_flag == 0xFF) { /* unknown state */ + *state = SDL_POWERSTATE_UNKNOWN; + } else if (battery_flag & (1 << 7)) { /* no battery */ + *state = SDL_POWERSTATE_NO_BATTERY; + } 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. */ + need_details = SDL_TRUE; + } else { + *state = SDL_POWERSTATE_ON_BATTERY; + need_details = SDL_TRUE; + } + + *percent = -1; + *seconds = -1; + if (need_details) { + const int pct = battery_percent; + const int secs = battery_time; + + if (pct >= 0) { /* -1 == unknown */ + *percent = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */ + } + if (secs >= 0) { /* -1 == unknown */ + *seconds = secs; + } + } + + return SDL_TRUE; +} + +SDL_bool +SDL_GetPowerInfo_Linux_sys_class_power_supply(SDL_PowerState *state, int *seconds, int *percent) +{ + const char *base = sys_class_power_supply_path; + struct dirent *dent; + DIR *dirp; + + dirp = opendir(base); + if (!dirp) { + return SDL_FALSE; + } + + *state = SDL_POWERSTATE_NO_BATTERY; /* assume we're just plugged in. */ + *seconds = -1; + *percent = -1; + + while ((dent = readdir(dirp)) != NULL) { + const char *name = dent->d_name; + SDL_bool choose = SDL_FALSE; + char str[64]; + SDL_PowerState st; + int secs; + int pct; + + 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. */ + } else if (SDL_strcmp(str, "Battery\n") != 0) { + 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 (SDL_strcmp(str, "device\n") == 0) { + 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)) { + st = SDL_POWERSTATE_NO_BATTERY; + } 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) { + st = SDL_POWERSTATE_ON_BATTERY; + } 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 */ + } + + 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))) { + secs = -1; + } else { + secs = SDL_atoi(str); + secs = (secs <= 0) ? -1 : secs; /* 0 == unknown */ + } + + /* + * We pick the battery that claims to have the most minutes left. + * (failing a report of minutes, we'll take the highest percent.) + */ + if ((secs < 0) && (*seconds < 0)) { + if ((pct < 0) && (*percent < 0)) { + choose = SDL_TRUE; /* at least we know there's a battery. */ + } else if (pct > *percent) { + choose = SDL_TRUE; + } + } else if (secs > *seconds) { + choose = SDL_TRUE; + } + + if (choose) { + *seconds = secs; + *percent = pct; + *state = st; + } + } + + closedir(dirp); + 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_DEVICE_DBUS_INTERFACE "org.freedesktop.UPower.Device" + +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; + int secs; + int pct; + Uint32 ui32 = 0; + Sint64 si64 = 0; + 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. */ + } 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 */ + } else if (!SDL_DBus_QueryPropertyOnConnection(conn, UPOWER_DBUS_NODE, path, UPOWER_DEVICE_DBUS_INTERFACE, "IsPresent", DBUS_TYPE_BOOLEAN, &ui32)) { + return; + } else if (!ui32) { + st = SDL_POWERSTATE_NO_BATTERY; + } else 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_CHARGING; + } else if ((ui32 == 2) || (ui32 == 3)) { /* 2 == discharging, 3 == empty. */ + st = SDL_POWERSTATE_ON_BATTERY; + } else if (ui32 == 4) { /* 4 == full */ + st = SDL_POWERSTATE_CHARGED; + } else { + 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. */ + } else { + 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 */ + } + + /* + * We pick the battery that claims to have the most minutes left. + * (failing a report of minutes, we'll take the highest percent.) + */ + if ((secs < 0) && (*seconds < 0)) { + if ((pct < 0) && (*percent < 0)) { + choose = SDL_TRUE; /* at least we know there's a battery. */ + } else if (pct > *percent) { + choose = SDL_TRUE; + } + } else if (secs > *seconds) { + choose = SDL_TRUE; + } + + if (choose) { + *seconds = secs; + *percent = pct; + *state = st; + } +} +#endif + +SDL_bool +SDL_GetPowerInfo_Linux_org_freedesktop_upower(SDL_PowerState *state, int *seconds, int *percent) +{ + SDL_bool retval = SDL_FALSE; + +#if SDL_USE_LIBDBUS + SDL_DBusContext *dbus = SDL_DBus_GetContext(); + char **paths = NULL; + int i, numpaths = 0; + + if (!dbus || !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. */ + } + + retval = SDL_TRUE; /* Clearly we can use this interface. */ + *state = SDL_POWERSTATE_NO_BATTERY; /* assume we're just plugged in. */ + *seconds = -1; + *percent = -1; + + for (i = 0; i < numpaths; i++) { + check_upower_device(dbus->system_conn, paths[i], state, seconds, percent); + } + + if (dbus) { + dbus->free_string_array(paths); + } +#endif /* SDL_USE_LIBDBUS */ + + return retval; +} + +#endif /* SDL_POWER_LINUX */ +#endif /* SDL_POWER_DISABLED */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/source/3rd-party/SDL2/src/power/macosx/SDL_syspower.c b/source/3rd-party/SDL2/src/power/macosx/SDL_syspower.c new file mode 100644 index 0000000..f28b6c8 --- /dev/null +++ b/source/3rd-party/SDL2/src/power/macosx/SDL_syspower.c @@ -0,0 +1,192 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org> + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_POWER_DISABLED +#if SDL_POWER_MACOSX + +#include <CoreFoundation/CoreFoundation.h> +#include <IOKit/ps/IOPowerSources.h> +#include <IOKit/ps/IOPSKeys.h> + +#include "SDL_power.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) + +/* 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) +{ + CFStringRef strval; /* don't CFRelease() this. */ + CFBooleanRef bval; + CFNumberRef numval; + SDL_bool charge = SDL_FALSE; + SDL_bool choose = SDL_FALSE; + SDL_bool is_ac = SDL_FALSE; + int secs = -1; + int maxpct = -1; + int pct = -1; + + if ((GETVAL(kIOPSIsPresentKey, &bval)) && (bval == kCFBooleanFalse)) { + return; /* nothing to see here. */ + } + + if (!GETVAL(kIOPSPowerSourceStateKey, &strval)) { + return; + } + + if (STRMATCH(strval, CFSTR(kIOPSACPowerValue))) { + is_ac = *have_ac = SDL_TRUE; + } else if (!STRMATCH(strval, CFSTR(kIOPSBatteryPowerValue))) { + return; /* not a battery? */ + } + + if ((GETVAL(kIOPSIsChargingKey, &bval)) && (bval == kCFBooleanTrue)) { + charge = SDL_TRUE; + } + + if (GETVAL(kIOPSMaxCapacityKey, &numval)) { + SInt32 val = -1; + CFNumberGetValue(numval, kCFNumberSInt32Type, &val); + if (val > 0) { + *have_battery = SDL_TRUE; + maxpct = (int) val; + } + } + + if (GETVAL(kIOPSMaxCapacityKey, &numval)) { + SInt32 val = -1; + CFNumberGetValue(numval, kCFNumberSInt32Type, &val); + if (val > 0) { + *have_battery = SDL_TRUE; + maxpct = (int) val; + } + } + + if (GETVAL(kIOPSTimeToEmptyKey, &numval)) { + SInt32 val = -1; + CFNumberGetValue(numval, kCFNumberSInt32Type, &val); + + /* Mac OS X reports 0 minutes until empty if you're plugged in. :( */ + if ((val == 0) && (is_ac)) { + val = -1; /* !!! FIXME: calc from timeToFull and capacity? */ + } + + secs = (int) val; + if (secs > 0) { + secs *= 60; /* value is in minutes, so convert to seconds. */ + } + } + + if (GETVAL(kIOPSCurrentCapacityKey, &numval)) { + SInt32 val = -1; + CFNumberGetValue(numval, kCFNumberSInt32Type, &val); + pct = (int) val; + } + + if ((pct > 0) && (maxpct > 0)) { + pct = (int) ((((double) pct) / ((double) maxpct)) * 100.0); + } + + if (pct > 100) { + pct = 100; + } + + /* + * We pick the battery that claims to have the most minutes left. + * (failing a report of minutes, we'll take the highest percent.) + */ + if ((secs < 0) && (*seconds < 0)) { + if ((pct < 0) && (*percent < 0)) { + choose = SDL_TRUE; /* at least we know there's a battery. */ + } + if (pct > *percent) { + choose = SDL_TRUE; + } + } else if (secs > *seconds) { + choose = SDL_TRUE; + } + + if (choose) { + *seconds = secs; + *percent = pct; + *charging = charge; + } +} + +#undef GETVAL +#undef STRMATCH + + +SDL_bool +SDL_GetPowerInfo_MacOSX(SDL_PowerState * state, int *seconds, int *percent) +{ + CFTypeRef blob = IOPSCopyPowerSourcesInfo(); + + *seconds = -1; + *percent = -1; + *state = SDL_POWERSTATE_UNKNOWN; + + if (blob != NULL) { + CFArrayRef list = IOPSCopyPowerSourcesList(blob); + if (list != NULL) { + /* don't CFRelease() the list items, or dictionaries! */ + SDL_bool have_ac = SDL_FALSE; + SDL_bool have_battery = SDL_FALSE; + SDL_bool charging = SDL_FALSE; + const CFIndex total = CFArrayGetCount(list); + CFIndex i; + for (i = 0; i < total; i++) { + CFTypeRef ps = (CFTypeRef) CFArrayGetValueAtIndex(list, i); + CFDictionaryRef dict = + IOPSGetPowerSourceDescription(blob, ps); + if (dict != NULL) { + checkps(dict, &have_ac, &have_battery, &charging, + seconds, percent); + } + } + + if (!have_battery) { + *state = SDL_POWERSTATE_NO_BATTERY; + } else if (charging) { + *state = SDL_POWERSTATE_CHARGING; + } else if (have_ac) { + *state = SDL_POWERSTATE_CHARGED; + } else { + *state = SDL_POWERSTATE_ON_BATTERY; + } + + CFRelease(list); + } + CFRelease(blob); + } + + return SDL_TRUE; /* always the definitive answer on Mac OS X. */ +} + +#endif /* SDL_POWER_MACOSX */ +#endif /* SDL_POWER_DISABLED */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/source/3rd-party/SDL2/src/power/psp/SDL_syspower.c b/source/3rd-party/SDL2/src/power/psp/SDL_syspower.c new file mode 100644 index 0000000..74585b2 --- /dev/null +++ b/source/3rd-party/SDL2/src/power/psp/SDL_syspower.c @@ -0,0 +1,68 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org> + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "../../SDL_internal.h" + +#ifndef SDL_POWER_DISABLED +#if SDL_POWER_PSP + +#include "SDL_power.h" +#include <psppower.h> + + +SDL_bool +SDL_GetPowerInfo_PSP(SDL_PowerState * state, int *seconds, + int *percent) +{ + int battery = scePowerIsBatteryExist(); + int plugged = scePowerIsPowerOnline(); + int charging = scePowerIsBatteryCharging(); + + *state = SDL_POWERSTATE_UNKNOWN; + *seconds = -1; + *percent = -1; + + if (!battery) { + *state = SDL_POWERSTATE_NO_BATTERY; + *seconds = -1; + *percent = -1; + } else if (charging) { + *state = SDL_POWERSTATE_CHARGING; + *percent = scePowerGetBatteryLifePercent(); + *seconds = scePowerGetBatteryLifeTime()*60; + } else if (plugged) { + *state = SDL_POWERSTATE_CHARGED; + *percent = scePowerGetBatteryLifePercent(); + *seconds = scePowerGetBatteryLifeTime()*60; + } else { + *state = SDL_POWERSTATE_ON_BATTERY; + *percent = scePowerGetBatteryLifePercent(); + *seconds = scePowerGetBatteryLifeTime()*60; + } + + + return SDL_TRUE; /* always the definitive answer on PSP. */ +} + +#endif /* SDL_POWER_PSP */ +#endif /* SDL_POWER_DISABLED */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/source/3rd-party/SDL2/src/power/uikit/SDL_syspower.h b/source/3rd-party/SDL2/src/power/uikit/SDL_syspower.h new file mode 100644 index 0000000..4a42fd2 --- /dev/null +++ b/source/3rd-party/SDL2/src/power/uikit/SDL_syspower.h @@ -0,0 +1,32 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org> + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#if SDL_POWER_UIKIT + +#include "SDL_power.h" + +void SDL_UIKit_UpdateBatteryMonitoring(void); +SDL_bool SDL_GetPowerInfo_UIKit(SDL_PowerState * state, int *seconds, int *percent); + +#endif /* SDL_POWER_UIKIT */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/source/3rd-party/SDL2/src/power/uikit/SDL_syspower.m b/source/3rd-party/SDL2/src/power/uikit/SDL_syspower.m new file mode 100644 index 0000000..cb8a252 --- /dev/null +++ b/source/3rd-party/SDL2/src/power/uikit/SDL_syspower.m @@ -0,0 +1,113 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org> + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_POWER_DISABLED +#if SDL_POWER_UIKIT + +#import <UIKit/UIKit.h> + +#include "SDL_power.h" +#include "SDL_timer.h" +#include "SDL_assert.h" +#include "SDL_syspower.h" + +#if !TARGET_OS_TV +/* turn off the battery monitor if it's been more than X ms since last check. */ +static const int BATTERY_MONITORING_TIMEOUT = 3000; +static Uint32 SDL_UIKitLastPowerInfoQuery = 0; + +void +SDL_UIKit_UpdateBatteryMonitoring(void) +{ + if (SDL_UIKitLastPowerInfoQuery) { + if (SDL_TICKS_PASSED(SDL_GetTicks(), SDL_UIKitLastPowerInfoQuery + BATTERY_MONITORING_TIMEOUT)) { + UIDevice *uidev = [UIDevice currentDevice]; + SDL_assert([uidev isBatteryMonitoringEnabled] == YES); + [uidev setBatteryMonitoringEnabled:NO]; + SDL_UIKitLastPowerInfoQuery = 0; + } + } +} +#else +void +SDL_UIKit_UpdateBatteryMonitoring(void) +{ + /* Do nothing. */ +} +#endif /* !TARGET_OS_TV */ + +SDL_bool +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 */ + @autoreleasepool { + UIDevice *uidev = [UIDevice currentDevice]; + + if (!SDL_UIKitLastPowerInfoQuery) { + SDL_assert(uidev.isBatteryMonitoringEnabled == NO); + uidev.batteryMonitoringEnabled = YES; + } + + /* UIKit_GL_SwapWindow() (etc) will check this and disable the battery + * monitoring if the app hasn't queried it in the last X seconds. + * Apparently monitoring the battery burns battery life. :) + * Apple's docs say not to monitor the battery unless you need it. + */ + SDL_UIKitLastPowerInfoQuery = SDL_GetTicks(); + + *seconds = -1; /* no API to estimate this in UIKit. */ + + switch (uidev.batteryState) { + case UIDeviceBatteryStateCharging: + *state = SDL_POWERSTATE_CHARGING; + break; + + case UIDeviceBatteryStateFull: + *state = SDL_POWERSTATE_CHARGED; + break; + + case UIDeviceBatteryStateUnplugged: + *state = SDL_POWERSTATE_ON_BATTERY; + break; + + case UIDeviceBatteryStateUnknown: + default: + *state = SDL_POWERSTATE_UNKNOWN; + break; + } + + const float level = uidev.batteryLevel; + *percent = ( (level < 0.0f) ? -1 : ((int) ((level * 100) + 0.5f)) ); + } +#endif /* TARGET_OS_TV */ + + return SDL_TRUE; /* always the definitive answer on iOS. */ +} + +#endif /* SDL_POWER_UIKIT */ +#endif /* SDL_POWER_DISABLED */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/source/3rd-party/SDL2/src/power/windows/SDL_syspower.c b/source/3rd-party/SDL2/src/power/windows/SDL_syspower.c new file mode 100644 index 0000000..be6c9d3 --- /dev/null +++ b/source/3rd-party/SDL2/src/power/windows/SDL_syspower.c @@ -0,0 +1,76 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org> + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_POWER_DISABLED +#if SDL_POWER_WINDOWS + +#include "../../core/windows/SDL_windows.h" + +#include "SDL_power.h" + +SDL_bool +SDL_GetPowerInfo_Windows(SDL_PowerState * state, int *seconds, int *percent) +{ + SYSTEM_POWER_STATUS status; + SDL_bool need_details = SDL_FALSE; + + /* This API should exist back to Win95. */ + if (!GetSystemPowerStatus(&status)) + { + /* !!! FIXME: push GetLastError() into SDL_GetError() */ + *state = SDL_POWERSTATE_UNKNOWN; + } else if (status.BatteryFlag == 0xFF) { /* unknown state */ + *state = SDL_POWERSTATE_UNKNOWN; + } else if (status.BatteryFlag & (1 << 7)) { /* no battery */ + *state = SDL_POWERSTATE_NO_BATTERY; + } else if (status.BatteryFlag & (1 << 3)) { /* charging */ + *state = SDL_POWERSTATE_CHARGING; + need_details = SDL_TRUE; + } else if (status.ACLineStatus == 1) { + *state = SDL_POWERSTATE_CHARGED; /* on AC, not charging. */ + need_details = SDL_TRUE; + } else { + *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; + + if (pct != 255) { /* 255 == unknown */ + *percent = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */ + } + if (secs != 0xFFFFFFFF) { /* ((DWORD)-1) == unknown */ + *seconds = secs; + } + } + + return SDL_TRUE; /* always the definitive answer on Windows. */ +} + +#endif /* SDL_POWER_WINDOWS */ +#endif /* SDL_POWER_DISABLED */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/source/3rd-party/SDL2/src/power/winrt/SDL_syspower.cpp b/source/3rd-party/SDL2/src/power/winrt/SDL_syspower.cpp new file mode 100644 index 0000000..9f2c2ad --- /dev/null +++ b/source/3rd-party/SDL2/src/power/winrt/SDL_syspower.cpp @@ -0,0 +1,44 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org> + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_POWER_DISABLED +#if SDL_POWER_WINRT + +#include "SDL_power.h" + +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: + - the Win32 function, GetSystemPowerStatus, is not available for use on WinRT + - Windows Phone 8 has a 'Battery' class, which is documented as available for C++ + - More info on WP8's Battery class can be found at http://msdn.microsoft.com/library/windowsphone/develop/jj207231 + */ + return SDL_FALSE; +} + +#endif /* SDL_POWER_WINRT */ +#endif /* SDL_POWER_DISABLED */ + +/* vi: set ts=4 sw=4 expandtab: */ |