icculus@4444
|
1 |
/*
|
icculus@4444
|
2 |
SDL - Simple DirectMedia Layer
|
icculus@4444
|
3 |
Copyright (C) 1997-2010 Sam Lantinga
|
icculus@4444
|
4 |
|
icculus@4444
|
5 |
This library is free software; you can redistribute it and/or
|
icculus@4444
|
6 |
modify it under the terms of the GNU Lesser General Public
|
icculus@4444
|
7 |
License as published by the Free Software Foundation; either
|
icculus@4444
|
8 |
version 2.1 of the License, or (at your option) any later version.
|
icculus@4444
|
9 |
|
icculus@4444
|
10 |
This library is distributed in the hope that it will be useful,
|
icculus@4444
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
icculus@4444
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
icculus@4444
|
13 |
Lesser General Public License for more details.
|
icculus@4444
|
14 |
|
icculus@4444
|
15 |
You should have received a copy of the GNU Lesser General Public
|
icculus@4444
|
16 |
License along with this library; if not, write to the Free Software
|
icculus@4444
|
17 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
icculus@4444
|
18 |
|
icculus@4444
|
19 |
Sam Lantinga
|
icculus@4444
|
20 |
slouken@libsdl.org
|
icculus@4444
|
21 |
*/
|
icculus@4444
|
22 |
#include "SDL_config.h"
|
icculus@4444
|
23 |
|
icculus@4444
|
24 |
#ifndef SDL_POWER_DISABLED
|
icculus@4444
|
25 |
#ifdef SDL_POWER_UIKIT
|
icculus@4444
|
26 |
|
icculus@4444
|
27 |
#import <UIKit/UIKit.h>
|
icculus@4444
|
28 |
|
icculus@4444
|
29 |
#include "SDL_power.h"
|
icculus@4444
|
30 |
#include "SDL_timer.h"
|
icculus@4444
|
31 |
#include "SDL_assert.h"
|
icculus@4444
|
32 |
|
icculus@4444
|
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) {
|
icculus@4444
|
41 |
const Uint32 prev = SDL_UIKitLastPowerInfoQuery;
|
icculus@4444
|
42 |
const UInt32 now = SDL_GetTicks();
|
icculus@4444
|
43 |
const UInt32 ticks = now - prev;
|
icculus@4444
|
44 |
// if timer wrapped (now < prev), shut down, too.
|
icculus@4444
|
45 |
if ((now < prev) || (ticks >= BATTERY_MONITORING_TIMEOUT)) {
|
icculus@4444
|
46 |
UIDevice *uidev = [UIDevice currentDevice];
|
icculus@4444
|
47 |
SDL_assert([uidev isBatteryMonitoringEnabled] == YES);
|
icculus@4444
|
48 |
[uidev setBatteryMonitoringEnabled:NO];
|
icculus@4444
|
49 |
SDL_UIKitLastPowerInfoQuery = 0;
|
icculus@4444
|
50 |
}
|
icculus@4444
|
51 |
}
|
icculus@4444
|
52 |
}
|
icculus@4444
|
53 |
|
icculus@4444
|
54 |
SDL_bool
|
icculus@4444
|
55 |
SDL_GetPowerInfo_UIKit(SDL_PowerState * state, int *seconds, int *percent)
|
icculus@4444
|
56 |
{
|
icculus@4444
|
57 |
UIDevice *uidev = [UIDevice currentDevice];
|
icculus@4444
|
58 |
|
icculus@4444
|
59 |
if (!SDL_UIKitLastPowerInfoQuery) {
|
icculus@4444
|
60 |
SDL_assert([uidev isBatteryMonitoringEnabled] == NO);
|
icculus@4444
|
61 |
[uidev setBatteryMonitoringEnabled:YES];
|
icculus@4444
|
62 |
}
|
icculus@4444
|
63 |
|
icculus@4444
|
64 |
// UIKit_GL_SwapWindow() (etc) will check this and disable the battery
|
icculus@4444
|
65 |
// monitoring if the app hasn't queried it in the last X seconds.
|
icculus@4444
|
66 |
// Apparently monitoring the battery burns battery life. :)
|
icculus@4444
|
67 |
// Apple's docs say not to monitor the battery unless you need it.
|
icculus@4444
|
68 |
SDL_UIKitLastPowerInfoQuery = SDL_GetTicks();
|
icculus@4444
|
69 |
|
icculus@4444
|
70 |
*seconds = -1; // no API to estimate this in UIKit.
|
icculus@4444
|
71 |
|
icculus@4444
|
72 |
switch ([uidev batteryState])
|
icculus@4444
|
73 |
{
|
icculus@4444
|
74 |
case UIDeviceBatteryStateCharging:
|
icculus@4444
|
75 |
*state = SDL_POWERSTATE_CHARGING;
|
icculus@4444
|
76 |
break;
|
icculus@4444
|
77 |
|
icculus@4444
|
78 |
case UIDeviceBatteryStateFull:
|
icculus@4444
|
79 |
*state = SDL_POWERSTATE_CHARGED;
|
icculus@4444
|
80 |
break;
|
icculus@4444
|
81 |
|
icculus@4444
|
82 |
case UIDeviceBatteryStateUnplugged:
|
icculus@4444
|
83 |
*state = SDL_POWERSTATE_ON_BATTERY;
|
icculus@4444
|
84 |
break;
|
icculus@4444
|
85 |
|
icculus@4444
|
86 |
case UIDeviceBatteryStateUnknown:
|
icculus@4444
|
87 |
default:
|
icculus@4444
|
88 |
*state = SDL_POWERSTATE_UNKNOWN;
|
icculus@4444
|
89 |
break;
|
icculus@4444
|
90 |
}
|
icculus@4444
|
91 |
|
icculus@4444
|
92 |
const float level = [uidev batteryLevel];
|
icculus@4444
|
93 |
*percent = ( (level < 0.0f) ? -1 : (((int) (level + 0.5f)) * 100) );
|
icculus@4444
|
94 |
return SDL_TRUE; /* always the definitive answer on iPhoneOS. */
|
icculus@4444
|
95 |
}
|
icculus@4444
|
96 |
|
icculus@4444
|
97 |
#endif /* SDL_POWER_UIKIT */
|
icculus@4444
|
98 |
#endif /* SDL_POWER_DISABLED */
|
icculus@4444
|
99 |
|
icculus@4444
|
100 |
/* vi: set ts=4 sw=4 expandtab: */
|