Mercurial > sdl-ios-xcode
comparison src/power/macosx/SDL_syspower.c @ 3186:51750b7a966f
indent
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Wed, 10 Jun 2009 13:34:20 +0000 |
parents | b7a48f533966 |
children | f7b03b6838cb |
comparison
equal
deleted
inserted
replaced
3185:44d5474c2c8a | 3186:51750b7a966f |
---|---|
35 #define GETVAL(k,v) \ | 35 #define GETVAL(k,v) \ |
36 CFDictionaryGetValueIfPresent(dict, CFSTR(k), (const void **) v) | 36 CFDictionaryGetValueIfPresent(dict, CFSTR(k), (const void **) v) |
37 | 37 |
38 /* Note that AC power sources also include a laptop battery it is charging. */ | 38 /* Note that AC power sources also include a laptop battery it is charging. */ |
39 static void | 39 static void |
40 checkps(CFDictionaryRef dict, SDL_bool *have_ac, SDL_bool *have_battery, | 40 checkps(CFDictionaryRef dict, SDL_bool * have_ac, SDL_bool * have_battery, |
41 SDL_bool *charging, int *seconds, int *percent) | 41 SDL_bool * charging, int *seconds, int *percent) |
42 { | 42 { |
43 CFStringRef strval; /* don't CFRelease() this. */ | 43 CFStringRef strval; /* don't CFRelease() this. */ |
44 CFBooleanRef bval; | 44 CFBooleanRef bval; |
45 CFNumberRef numval; | 45 CFNumberRef numval; |
46 SDL_bool charge = SDL_FALSE; | 46 SDL_bool charge = SDL_FALSE; |
47 SDL_bool choose = SDL_FALSE; | 47 SDL_bool choose = SDL_FALSE; |
48 SDL_bool is_ac = SDL_FALSE; | 48 SDL_bool is_ac = SDL_FALSE; |
49 int secs = -1; | 49 int secs = -1; |
50 int maxpct = -1; | 50 int maxpct = -1; |
51 int pct = -1; | 51 int pct = -1; |
52 | 52 |
53 if ((GETVAL(kIOPSIsPresentKey, &bval)) && (bval == kCFBooleanFalse)) { | 53 if ((GETVAL(kIOPSIsPresentKey, &bval)) && (bval == kCFBooleanFalse)) { |
54 return; /* nothing to see here. */ | 54 return; /* nothing to see here. */ |
55 } | 55 } |
56 | 56 |
57 if (!GETVAL(kIOPSPowerSourceStateKey, &strval)) { | 57 if (!GETVAL(kIOPSPowerSourceStateKey, &strval)) { |
58 return; | 58 return; |
59 } | 59 } |
60 | 60 |
61 if (STRMATCH(strval, CFSTR(kIOPSACPowerValue))) { | 61 if (STRMATCH(strval, CFSTR(kIOPSACPowerValue))) { |
62 is_ac = *have_ac = SDL_TRUE; | 62 is_ac = *have_ac = SDL_TRUE; |
63 } else if (!STRMATCH(strval, CFSTR(kIOPSBatteryPowerValue))) { | 63 } else if (!STRMATCH(strval, CFSTR(kIOPSBatteryPowerValue))) { |
64 return; /* not a battery? */ | 64 return; /* not a battery? */ |
65 } | 65 } |
66 | 66 |
67 if ((GETVAL(kIOPSIsChargingKey, &bval)) && (bval == kCFBooleanTrue)) { | 67 if ((GETVAL(kIOPSIsChargingKey, &bval)) && (bval == kCFBooleanTrue)) { |
68 charge = SDL_TRUE; | 68 charge = SDL_TRUE; |
69 } | 69 } |
90 SInt32 val = -1; | 90 SInt32 val = -1; |
91 CFNumberGetValue(numval, kCFNumberSInt32Type, &val); | 91 CFNumberGetValue(numval, kCFNumberSInt32Type, &val); |
92 | 92 |
93 /* Mac OS X reports 0 minutes until empty if you're plugged in. :( */ | 93 /* Mac OS X reports 0 minutes until empty if you're plugged in. :( */ |
94 if ((val == 0) && (is_ac)) { | 94 if ((val == 0) && (is_ac)) { |
95 val = -1; /* !!! FIXME: calc from timeToFull and capacity? */ | 95 val = -1; /* !!! FIXME: calc from timeToFull and capacity? */ |
96 } | 96 } |
97 | 97 |
98 secs = (int) val; | 98 secs = (int) val; |
99 if (secs > 0) { | 99 if (secs > 0) { |
100 secs *= 60; /* value is in minutes, so convert to seconds. */ | 100 secs *= 60; /* value is in minutes, so convert to seconds. */ |
101 } | 101 } |
102 } | 102 } |
103 | 103 |
104 if (GETVAL(kIOPSCurrentCapacityKey, &numval)) { | 104 if (GETVAL(kIOPSCurrentCapacityKey, &numval)) { |
105 SInt32 val = -1; | 105 SInt32 val = -1; |
106 CFNumberGetValue(numval, kCFNumberSInt32Type, &val); | 106 CFNumberGetValue(numval, kCFNumberSInt32Type, &val); |
107 pct = (int) val; | 107 pct = (int) val; |
108 } | 108 } |
109 | 109 |
110 if ((pct > 0) && (maxpct > 0)) { | 110 if ((pct > 0) && (maxpct > 0)) { |
111 pct = (int) ((((double)pct)/((double)maxpct)) * 100.0); | 111 pct = (int) ((((double) pct) / ((double) maxpct)) * 100.0); |
112 } | 112 } |
113 | 113 |
114 if (pct > 100) { | 114 if (pct > 100) { |
115 pct = 100; | 115 pct = 100; |
116 } | 116 } |
119 * We pick the battery that claims to have the most minutes left. | 119 * We pick the battery that claims to have the most minutes left. |
120 * (failing a report of minutes, we'll take the highest percent.) | 120 * (failing a report of minutes, we'll take the highest percent.) |
121 */ | 121 */ |
122 if ((secs < 0) && (*seconds < 0)) { | 122 if ((secs < 0) && (*seconds < 0)) { |
123 if ((pct < 0) && (*percent < 0)) { | 123 if ((pct < 0) && (*percent < 0)) { |
124 choose = SDL_TRUE; /* at least we know there's a battery. */ | 124 choose = SDL_TRUE; /* at least we know there's a battery. */ |
125 } | 125 } |
126 if (pct > *percent) { | 126 if (pct > *percent) { |
127 choose = SDL_TRUE; | 127 choose = SDL_TRUE; |
128 } | 128 } |
129 } else if (secs > *seconds) { | 129 } else if (secs > *seconds) { |
140 #undef GETVAL | 140 #undef GETVAL |
141 #undef STRMATCH | 141 #undef STRMATCH |
142 | 142 |
143 | 143 |
144 SDL_bool | 144 SDL_bool |
145 SDL_GetPowerInfo_MacOSX(SDL_PowerState *state, int *seconds, int *percent) | 145 SDL_GetPowerInfo_MacOSX(SDL_PowerState * state, int *seconds, int *percent) |
146 { | 146 { |
147 CFTypeRef blob = IOPSCopyPowerSourcesInfo(); | 147 CFTypeRef blob = IOPSCopyPowerSourcesInfo(); |
148 | 148 |
149 *seconds = -1; | 149 *seconds = -1; |
150 *percent = -1; | 150 *percent = -1; |
159 SDL_bool charging = SDL_FALSE; | 159 SDL_bool charging = SDL_FALSE; |
160 const CFIndex total = CFArrayGetCount(list); | 160 const CFIndex total = CFArrayGetCount(list); |
161 CFIndex i; | 161 CFIndex i; |
162 for (i = 0; i < total; i++) { | 162 for (i = 0; i < total; i++) { |
163 CFTypeRef ps = (CFTypeRef) CFArrayGetValueAtIndex(list, i); | 163 CFTypeRef ps = (CFTypeRef) CFArrayGetValueAtIndex(list, i); |
164 CFDictionaryRef dict = IOPSGetPowerSourceDescription(blob, ps); | 164 CFDictionaryRef dict = |
165 IOPSGetPowerSourceDescription(blob, ps); | |
165 if (dict != NULL) { | 166 if (dict != NULL) { |
166 checkps(dict, &have_ac, &have_battery, &charging, | 167 checkps(dict, &have_ac, &have_battery, &charging, |
167 seconds, percent); | 168 seconds, percent); |
168 } | 169 } |
169 } | 170 } |
181 CFRelease(list); | 182 CFRelease(list); |
182 } | 183 } |
183 CFRelease(blob); | 184 CFRelease(blob); |
184 } | 185 } |
185 | 186 |
186 return SDL_TRUE; /* always the definitive answer on Mac OS X. */ | 187 return SDL_TRUE; /* always the definitive answer on Mac OS X. */ |
187 } | 188 } |
188 | 189 |
189 #endif /* SDL_POWER_MACOSX */ | 190 #endif /* SDL_POWER_MACOSX */ |
190 #endif /* SDL_POWER_DISABLED */ | 191 #endif /* SDL_POWER_DISABLED */ |
191 | 192 |
192 /* vi: set ts=4 sw=4 expandtab: */ | 193 /* vi: set ts=4 sw=4 expandtab: */ |
193 |