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