Atlas - SDL_syspower.m

Home / ext / SDL / src / power / uikit Lines: 1 | Size: 3447 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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. 32static const int BATTERY_MONITORING_TIMEOUT = 3000; 33static Uint64 SDL_UIKitLastPowerInfoQuery = 0; 34 35void 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 47void SDL_UIKit_UpdateBatteryMonitoring(void) 48{ 49 // Do nothing. 50} 51#endif // !SDL_PLATFORM_TVOS 52 53bool 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 106
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.