comparison src/power/uikit/SDL_syspower.m @ 4444:42e1092225f9

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