#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_syspower.h"
#if !TARGET_OS_TV
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)
{
}
#endif
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
@autoreleasepool {
UIDevice *uidev = [UIDevice currentDevice];
if (!SDL_UIKitLastPowerInfoQuery) {
SDL_assert(uidev.isBatteryMonitoringEnabled == NO);
uidev.batteryMonitoringEnabled = YES;
}
SDL_UIKitLastPowerInfoQuery = SDL_GetTicks();
*seconds = -1;
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
return SDL_TRUE;
}
#endif
#endif