Mac: Use CoreFoundation headers instead of Carbon headers, in GetPowerInfo code.
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
21 #include "../../SDL_internal.h"
23 #ifndef SDL_POWER_DISABLED
26 #include <CoreFoundation/CoreFoundation.h>
27 #include <IOKit/ps/IOPowerSources.h>
28 #include <IOKit/ps/IOPSKeys.h>
30 #include "SDL_power.h"
32 /* CoreFoundation is so verbose... */
33 #define STRMATCH(a,b) (CFStringCompare(a, b, 0) == kCFCompareEqualTo)
35 CFDictionaryGetValueIfPresent(dict, CFSTR(k), (const void **) v)
37 /* Note that AC power sources also include a laptop battery it is charging. */
39 checkps(CFDictionaryRef dict, SDL_bool * have_ac, SDL_bool * have_battery,
40 SDL_bool * charging, int *seconds, int *percent)
42 CFStringRef strval; /* don't CFRelease() this. */
45 SDL_bool charge = SDL_FALSE;
46 SDL_bool choose = SDL_FALSE;
47 SDL_bool is_ac = SDL_FALSE;
52 if ((GETVAL(kIOPSIsPresentKey, &bval)) && (bval == kCFBooleanFalse)) {
53 return; /* nothing to see here. */
56 if (!GETVAL(kIOPSPowerSourceStateKey, &strval)) {
60 if (STRMATCH(strval, CFSTR(kIOPSACPowerValue))) {
61 is_ac = *have_ac = SDL_TRUE;
62 } else if (!STRMATCH(strval, CFSTR(kIOPSBatteryPowerValue))) {
63 return; /* not a battery? */
66 if ((GETVAL(kIOPSIsChargingKey, &bval)) && (bval == kCFBooleanTrue)) {
70 if (GETVAL(kIOPSMaxCapacityKey, &numval)) {
72 CFNumberGetValue(numval, kCFNumberSInt32Type, &val);
74 *have_battery = SDL_TRUE;
79 if (GETVAL(kIOPSMaxCapacityKey, &numval)) {
81 CFNumberGetValue(numval, kCFNumberSInt32Type, &val);
83 *have_battery = SDL_TRUE;
88 if (GETVAL(kIOPSTimeToEmptyKey, &numval)) {
90 CFNumberGetValue(numval, kCFNumberSInt32Type, &val);
92 /* Mac OS X reports 0 minutes until empty if you're plugged in. :( */
93 if ((val == 0) && (is_ac)) {
94 val = -1; /* !!! FIXME: calc from timeToFull and capacity? */
99 secs *= 60; /* value is in minutes, so convert to seconds. */
103 if (GETVAL(kIOPSCurrentCapacityKey, &numval)) {
105 CFNumberGetValue(numval, kCFNumberSInt32Type, &val);
109 if ((pct > 0) && (maxpct > 0)) {
110 pct = (int) ((((double) pct) / ((double) maxpct)) * 100.0);
118 * We pick the battery that claims to have the most minutes left.
119 * (failing a report of minutes, we'll take the highest percent.)
121 if ((secs < 0) && (*seconds < 0)) {
122 if ((pct < 0) && (*percent < 0)) {
123 choose = SDL_TRUE; /* at least we know there's a battery. */
125 if (pct > *percent) {
128 } else if (secs > *seconds) {
144 SDL_GetPowerInfo_MacOSX(SDL_PowerState * state, int *seconds, int *percent)
146 CFTypeRef blob = IOPSCopyPowerSourcesInfo();
150 *state = SDL_POWERSTATE_UNKNOWN;
153 CFArrayRef list = IOPSCopyPowerSourcesList(blob);
155 /* don't CFRelease() the list items, or dictionaries! */
156 SDL_bool have_ac = SDL_FALSE;
157 SDL_bool have_battery = SDL_FALSE;
158 SDL_bool charging = SDL_FALSE;
159 const CFIndex total = CFArrayGetCount(list);
161 for (i = 0; i < total; i++) {
162 CFTypeRef ps = (CFTypeRef) CFArrayGetValueAtIndex(list, i);
163 CFDictionaryRef dict =
164 IOPSGetPowerSourceDescription(blob, ps);
166 checkps(dict, &have_ac, &have_battery, &charging,
172 *state = SDL_POWERSTATE_NO_BATTERY;
173 } else if (charging) {
174 *state = SDL_POWERSTATE_CHARGING;
175 } else if (have_ac) {
176 *state = SDL_POWERSTATE_CHARGED;
178 *state = SDL_POWERSTATE_ON_BATTERY;
186 return SDL_TRUE; /* always the definitive answer on Mac OS X. */
189 #endif /* SDL_POWER_MACOSX */
190 #endif /* SDL_POWER_DISABLED */
192 /* vi: set ts=4 sw=4 expandtab: */