diff options
| author | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
| commit | 5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch) | |
| tree | 8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/power/uikit/SDL_syspower.m | |
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/power/uikit/SDL_syspower.m')
| -rw-r--r-- | contrib/SDL-3.2.8/src/power/uikit/SDL_syspower.m | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/power/uikit/SDL_syspower.m b/contrib/SDL-3.2.8/src/power/uikit/SDL_syspower.m new file mode 100644 index 0000000..561f2de --- /dev/null +++ b/contrib/SDL-3.2.8/src/power/uikit/SDL_syspower.m | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 4 | |||
| 5 | This software is provided 'as-is', without any express or implied | ||
| 6 | warranty. In no event will the authors be held liable for any damages | ||
| 7 | arising from the use of this software. | ||
| 8 | |||
| 9 | Permission is granted to anyone to use this software for any purpose, | ||
| 10 | including commercial applications, and to alter it and redistribute it | ||
| 11 | freely, subject to the following restrictions: | ||
| 12 | |||
| 13 | 1. The origin of this software must not be misrepresented; you must not | ||
| 14 | claim that you wrote the original software. If you use this software | ||
| 15 | in a product, an acknowledgment in the product documentation would be | ||
| 16 | appreciated but is not required. | ||
| 17 | 2. Altered source versions must be plainly marked as such, and must not be | ||
| 18 | misrepresented as being the original software. | ||
| 19 | 3. This notice may not be removed or altered from any source distribution. | ||
| 20 | */ | ||
| 21 | #include "SDL_internal.h" | ||
| 22 | |||
| 23 | #ifndef SDL_POWER_DISABLED | ||
| 24 | #ifdef SDL_POWER_UIKIT | ||
| 25 | |||
| 26 | #import <UIKit/UIKit.h> | ||
| 27 | |||
| 28 | #include "SDL_syspower.h" | ||
| 29 | |||
| 30 | #ifndef SDL_PLATFORM_TVOS | ||
| 31 | // turn off the battery monitor if it's been more than X ms since last check. | ||
| 32 | static const int BATTERY_MONITORING_TIMEOUT = 3000; | ||
| 33 | static Uint64 SDL_UIKitLastPowerInfoQuery = 0; | ||
| 34 | |||
| 35 | void SDL_UIKit_UpdateBatteryMonitoring(void) | ||
| 36 | { | ||
| 37 | if (SDL_UIKitLastPowerInfoQuery) { | ||
| 38 | if (SDL_GetTicks() >= (SDL_UIKitLastPowerInfoQuery + BATTERY_MONITORING_TIMEOUT)) { | ||
| 39 | UIDevice *uidev = [UIDevice currentDevice]; | ||
| 40 | SDL_assert([uidev isBatteryMonitoringEnabled] == YES); | ||
| 41 | [uidev setBatteryMonitoringEnabled:NO]; | ||
| 42 | SDL_UIKitLastPowerInfoQuery = 0; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | #else | ||
| 47 | void SDL_UIKit_UpdateBatteryMonitoring(void) | ||
| 48 | { | ||
| 49 | // Do nothing. | ||
| 50 | } | ||
| 51 | #endif // !SDL_PLATFORM_TVOS | ||
| 52 | |||
| 53 | bool SDL_GetPowerInfo_UIKit(SDL_PowerState *state, int *seconds, int *percent) | ||
| 54 | { | ||
| 55 | #ifdef SDL_PLATFORM_TVOS | ||
| 56 | *state = SDL_POWERSTATE_NO_BATTERY; | ||
| 57 | *seconds = -1; | ||
| 58 | *percent = -1; | ||
| 59 | #else // SDL_PLATFORM_TVOS | ||
| 60 | @autoreleasepool { | ||
| 61 | UIDevice *uidev = [UIDevice currentDevice]; | ||
| 62 | |||
| 63 | if (!SDL_UIKitLastPowerInfoQuery) { | ||
| 64 | SDL_assert(uidev.isBatteryMonitoringEnabled == NO); | ||
| 65 | uidev.batteryMonitoringEnabled = YES; | ||
| 66 | } | ||
| 67 | |||
| 68 | /* UIKit_GL_SwapWindow() (etc) will check this and disable the battery | ||
| 69 | * monitoring if the app hasn't queried it in the last X seconds. | ||
| 70 | * Apparently monitoring the battery burns battery life. :) | ||
| 71 | * Apple's docs say not to monitor the battery unless you need it. | ||
| 72 | */ | ||
| 73 | SDL_UIKitLastPowerInfoQuery = SDL_GetTicks(); | ||
| 74 | |||
| 75 | *seconds = -1; // no API to estimate this in UIKit. | ||
| 76 | |||
| 77 | switch (uidev.batteryState) { | ||
| 78 | case UIDeviceBatteryStateCharging: | ||
| 79 | *state = SDL_POWERSTATE_CHARGING; | ||
| 80 | break; | ||
| 81 | |||
| 82 | case UIDeviceBatteryStateFull: | ||
| 83 | *state = SDL_POWERSTATE_CHARGED; | ||
| 84 | break; | ||
| 85 | |||
| 86 | case UIDeviceBatteryStateUnplugged: | ||
| 87 | *state = SDL_POWERSTATE_ON_BATTERY; | ||
| 88 | break; | ||
| 89 | |||
| 90 | case UIDeviceBatteryStateUnknown: | ||
| 91 | default: | ||
| 92 | *state = SDL_POWERSTATE_UNKNOWN; | ||
| 93 | break; | ||
| 94 | } | ||
| 95 | |||
| 96 | const float level = uidev.batteryLevel; | ||
| 97 | *percent = ((level < 0.0f) ? -1 : ((int)((level * 100) + 0.5f))); | ||
| 98 | } | ||
| 99 | #endif // SDL_PLATFORM_TVOS | ||
| 100 | |||
| 101 | return true; // always the definitive answer on iOS. | ||
| 102 | } | ||
| 103 | |||
| 104 | #endif // SDL_POWER_UIKIT | ||
| 105 | #endif // SDL_POWER_DISABLED | ||
