SDL  2.0
SDL_syspower.m
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22 
23 #ifndef SDL_POWER_DISABLED
24 #if SDL_POWER_UIKIT
25 
26 #import <UIKit/UIKit.h>
27 
28 #include "SDL_power.h"
29 #include "SDL_timer.h"
30 #include "SDL_assert.h"
31 #include "SDL_syspower.h"
32 
33 #if !TARGET_OS_TV
34 /* turn off the battery monitor if it's been more than X ms since last check. */
35 static const int BATTERY_MONITORING_TIMEOUT = 3000;
36 static Uint32 SDL_UIKitLastPowerInfoQuery = 0;
37 
38 void
39 SDL_UIKit_UpdateBatteryMonitoring(void)
40 {
41  if (SDL_UIKitLastPowerInfoQuery) {
42  if (SDL_TICKS_PASSED(SDL_GetTicks(), SDL_UIKitLastPowerInfoQuery + BATTERY_MONITORING_TIMEOUT)) {
43  UIDevice *uidev = [UIDevice currentDevice];
44  SDL_assert([uidev isBatteryMonitoringEnabled] == YES);
45  [uidev setBatteryMonitoringEnabled:NO];
46  SDL_UIKitLastPowerInfoQuery = 0;
47  }
48  }
49 }
50 #else
51 void
52 SDL_UIKit_UpdateBatteryMonitoring(void)
53 {
54  /* Do nothing. */
55 }
56 #endif /* !TARGET_OS_TV */
57 
59 SDL_GetPowerInfo_UIKit(SDL_PowerState * state, int *seconds, int *percent)
60 {
61 #if TARGET_OS_TV
63  *seconds = -1;
64  *percent = -1;
65 #else /* TARGET_OS_TV */
66  @autoreleasepool {
67  UIDevice *uidev = [UIDevice currentDevice];
68 
69  if (!SDL_UIKitLastPowerInfoQuery) {
70  SDL_assert(uidev.isBatteryMonitoringEnabled == NO);
71  uidev.batteryMonitoringEnabled = YES;
72  }
73 
74  /* UIKit_GL_SwapWindow() (etc) will check this and disable the battery
75  * monitoring if the app hasn't queried it in the last X seconds.
76  * Apparently monitoring the battery burns battery life. :)
77  * Apple's docs say not to monitor the battery unless you need it.
78  */
79  SDL_UIKitLastPowerInfoQuery = SDL_GetTicks();
80 
81  *seconds = -1; /* no API to estimate this in UIKit. */
82 
83  switch (uidev.batteryState) {
84  case UIDeviceBatteryStateCharging:
85  *state = SDL_POWERSTATE_CHARGING;
86  break;
87 
88  case UIDeviceBatteryStateFull:
89  *state = SDL_POWERSTATE_CHARGED;
90  break;
91 
92  case UIDeviceBatteryStateUnplugged:
94  break;
95 
96  case UIDeviceBatteryStateUnknown:
97  default:
98  *state = SDL_POWERSTATE_UNKNOWN;
99  break;
100  }
101 
102  const float level = uidev.batteryLevel;
103  *percent = ( (level < 0.0f) ? -1 : ((int) ((level * 100) + 0.5f)) );
104  }
105 #endif /* TARGET_OS_TV */
106 
107  return SDL_TRUE; /* always the definitive answer on iOS. */
108 }
109 
110 #endif /* SDL_POWER_UIKIT */
111 #endif /* SDL_POWER_DISABLED */
112 
113 /* vi: set ts=4 sw=4 expandtab: */
GLint level
Definition: SDL_opengl.h:1572
struct xkb_state * state
GLfloat f
uint32_t Uint32
Definition: SDL_stdinc.h:181
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
#define SDL_assert(condition)
Definition: SDL_assert.h:169
SDL_bool
Definition: SDL_stdinc.h:139
SDL_bool SDL_GetPowerInfo_UIKit(SDL_PowerState *, int *, int *)
SDL_PowerState
The basic state for the system&#39;s power supply.
Definition: SDL_power.h:42
#define SDL_TICKS_PASSED(A, B)
Compare SDL ticks values, and return true if A has passed B.
Definition: SDL_timer.h:56