author | Alex Szpakowski |
Tue, 29 Jul 2014 00:36:12 -0300 | |
branch | iOS-improvements |
changeset 9506 | 18e3f94bd860 |
parent 8223 | 18ffe88a2148 |
child 9619 | b94b6d0bff0f |
permissions | -rw-r--r-- |
icculus@4444 | 1 |
/* |
slouken@5535 | 2 |
Simple DirectMedia Layer |
slouken@8149 | 3 |
Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org> |
icculus@4444 | 4 |
|
slouken@5535 | 5 |
This software is provided 'as-is', without any express or implied |
slouken@5535 | 6 |
warranty. In no event will the authors be held liable for any damages |
slouken@5535 | 7 |
arising from the use of this software. |
icculus@4444 | 8 |
|
slouken@5535 | 9 |
Permission is granted to anyone to use this software for any purpose, |
slouken@5535 | 10 |
including commercial applications, and to alter it and redistribute it |
slouken@5535 | 11 |
freely, subject to the following restrictions: |
icculus@4444 | 12 |
|
slouken@5535 | 13 |
1. The origin of this software must not be misrepresented; you must not |
slouken@5535 | 14 |
claim that you wrote the original software. If you use this software |
slouken@5535 | 15 |
in a product, an acknowledgment in the product documentation would be |
slouken@5535 | 16 |
appreciated but is not required. |
slouken@5535 | 17 |
2. Altered source versions must be plainly marked as such, and must not be |
slouken@5535 | 18 |
misrepresented as being the original software. |
slouken@5535 | 19 |
3. This notice may not be removed or altered from any source distribution. |
icculus@4444 | 20 |
*/ |
icculus@8093 | 21 |
#include "../../SDL_internal.h" |
icculus@4444 | 22 |
|
icculus@4444 | 23 |
#ifndef SDL_POWER_DISABLED |
slouken@6044 | 24 |
#if SDL_POWER_UIKIT |
icculus@4444 | 25 |
|
icculus@4444 | 26 |
#import <UIKit/UIKit.h> |
icculus@4444 | 27 |
|
icculus@4444 | 28 |
#include "SDL_power.h" |
icculus@4444 | 29 |
#include "SDL_timer.h" |
icculus@4444 | 30 |
#include "SDL_assert.h" |
slouken@6044 | 31 |
#include "SDL_syspower.h" |
icculus@4444 | 32 |
|
slouken@7191 | 33 |
/* turn off the battery monitor if it's been more than X ms since last check. */ |
icculus@4444 | 34 |
static const int BATTERY_MONITORING_TIMEOUT = 3000; |
icculus@4444 | 35 |
static Uint32 SDL_UIKitLastPowerInfoQuery = 0; |
icculus@4444 | 36 |
|
icculus@4444 | 37 |
void |
icculus@4444 | 38 |
SDL_UIKit_UpdateBatteryMonitoring(void) |
icculus@4444 | 39 |
{ |
icculus@4444 | 40 |
if (SDL_UIKitLastPowerInfoQuery) { |
slouken@7857 | 41 |
if (SDL_TICKS_PASSED(SDL_GetTicks(), SDL_UIKitLastPowerInfoQuery + BATTERY_MONITORING_TIMEOUT)) { |
icculus@4444 | 42 |
UIDevice *uidev = [UIDevice currentDevice]; |
icculus@4444 | 43 |
SDL_assert([uidev isBatteryMonitoringEnabled] == YES); |
icculus@4444 | 44 |
[uidev setBatteryMonitoringEnabled:NO]; |
icculus@4444 | 45 |
SDL_UIKitLastPowerInfoQuery = 0; |
icculus@4444 | 46 |
} |
icculus@4444 | 47 |
} |
icculus@4444 | 48 |
} |
icculus@4444 | 49 |
|
icculus@4444 | 50 |
SDL_bool |
icculus@4444 | 51 |
SDL_GetPowerInfo_UIKit(SDL_PowerState * state, int *seconds, int *percent) |
icculus@4444 | 52 |
{ |
slime73@9506 | 53 |
@autoreleasepool { |
slime73@9506 | 54 |
UIDevice *uidev = [UIDevice currentDevice]; |
icculus@4444 | 55 |
|
slime73@9506 | 56 |
if (!SDL_UIKitLastPowerInfoQuery) { |
slime73@9506 | 57 |
SDL_assert(uidev.isBatteryMonitoringEnabled == NO); |
slime73@9506 | 58 |
uidev.batteryMonitoringEnabled = YES; |
slime73@9506 | 59 |
} |
icculus@4444 | 60 |
|
slime73@9506 | 61 |
/* UIKit_GL_SwapWindow() (etc) will check this and disable the battery |
slime73@9506 | 62 |
* monitoring if the app hasn't queried it in the last X seconds. |
slime73@9506 | 63 |
* Apparently monitoring the battery burns battery life. :) |
slime73@9506 | 64 |
* Apple's docs say not to monitor the battery unless you need it. |
slime73@9506 | 65 |
*/ |
slime73@9506 | 66 |
SDL_UIKitLastPowerInfoQuery = SDL_GetTicks(); |
icculus@4444 | 67 |
|
slime73@9506 | 68 |
*seconds = -1; /* no API to estimate this in UIKit. */ |
icculus@4444 | 69 |
|
slime73@9506 | 70 |
switch (uidev.batteryState) { |
icculus@4444 | 71 |
case UIDeviceBatteryStateCharging: |
icculus@4444 | 72 |
*state = SDL_POWERSTATE_CHARGING; |
icculus@4444 | 73 |
break; |
icculus@4444 | 74 |
|
icculus@4444 | 75 |
case UIDeviceBatteryStateFull: |
icculus@4444 | 76 |
*state = SDL_POWERSTATE_CHARGED; |
icculus@4444 | 77 |
break; |
icculus@4444 | 78 |
|
icculus@4444 | 79 |
case UIDeviceBatteryStateUnplugged: |
icculus@4444 | 80 |
*state = SDL_POWERSTATE_ON_BATTERY; |
icculus@4444 | 81 |
break; |
icculus@4444 | 82 |
|
icculus@4444 | 83 |
case UIDeviceBatteryStateUnknown: |
icculus@4444 | 84 |
default: |
icculus@4444 | 85 |
*state = SDL_POWERSTATE_UNKNOWN; |
icculus@4444 | 86 |
break; |
slime73@9506 | 87 |
} |
icculus@4444 | 88 |
|
slime73@9506 | 89 |
const float level = uidev.batteryLevel; |
slime73@9506 | 90 |
*percent = ( (level < 0.0f) ? -1 : ((int) ((level * 100) + 0.5f)) ); |
slime73@9506 | 91 |
return SDL_TRUE; /* always the definitive answer on iOS. */ |
slime73@9506 | 92 |
} |
icculus@4444 | 93 |
} |
icculus@4444 | 94 |
|
icculus@4444 | 95 |
#endif /* SDL_POWER_UIKIT */ |
icculus@4444 | 96 |
#endif /* SDL_POWER_DISABLED */ |
icculus@4444 | 97 |
|
icculus@4444 | 98 |
/* vi: set ts=4 sw=4 expandtab: */ |