Atlas - SDL_syspower.c

Home / ext / SDL / src / power / macos Lines: 1 | Size: 5629 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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#ifdef SDL_POWER_MACOSX 25 26#include <CoreFoundation/CoreFoundation.h> 27#include <IOKit/ps/IOPowerSources.h> 28#include <IOKit/ps/IOPSKeys.h> 29 30// CoreFoundation is so verbose... 31#define STRMATCH(a, b) (CFStringCompare(a, b, 0) == kCFCompareEqualTo) 32#define GETVAL(k, v) \ 33 CFDictionaryGetValueIfPresent(dict, CFSTR(k), (const void **)v) 34 35// Note that AC power sources also include a laptop battery it is charging. 36static void checkps(CFDictionaryRef dict, bool *have_ac, bool *have_battery, 37 bool *charging, int *seconds, int *percent) 38{ 39 CFStringRef strval; // don't CFRelease() this. 40 CFBooleanRef bval; 41 CFNumberRef numval; 42 bool charge = false; 43 bool choose = false; 44 bool is_ac = false; 45 int secs = -1; 46 int maxpct = -1; 47 int pct = -1; 48 49 if ((GETVAL(kIOPSIsPresentKey, &bval)) && (bval == kCFBooleanFalse)) { 50 return; // nothing to see here. 51 } 52 53 if (!GETVAL(kIOPSPowerSourceStateKey, &strval)) { 54 return; 55 } 56 57 if (STRMATCH(strval, CFSTR(kIOPSACPowerValue))) { 58 is_ac = *have_ac = true; 59 } else if (!STRMATCH(strval, CFSTR(kIOPSBatteryPowerValue))) { 60 return; // not a battery? 61 } 62 63 if ((GETVAL(kIOPSIsChargingKey, &bval)) && (bval == kCFBooleanTrue)) { 64 charge = true; 65 } 66 67 if (GETVAL(kIOPSMaxCapacityKey, &numval)) { 68 SInt32 val = -1; 69 CFNumberGetValue(numval, kCFNumberSInt32Type, &val); 70 if (val > 0) { 71 *have_battery = true; 72 maxpct = (int)val; 73 } 74 } 75 76 if (GETVAL(kIOPSMaxCapacityKey, &numval)) { 77 SInt32 val = -1; 78 CFNumberGetValue(numval, kCFNumberSInt32Type, &val); 79 if (val > 0) { 80 *have_battery = true; 81 maxpct = (int)val; 82 } 83 } 84 85 if (GETVAL(kIOPSTimeToEmptyKey, &numval)) { 86 SInt32 val = -1; 87 CFNumberGetValue(numval, kCFNumberSInt32Type, &val); 88 89 // macOS reports 0 minutes until empty if you're plugged in. :( 90 if ((val == 0) && (is_ac)) { 91 val = -1; // !!! FIXME: calc from timeToFull and capacity? 92 } 93 94 secs = (int)val; 95 if (secs > 0) { 96 secs *= 60; // value is in minutes, so convert to seconds. 97 } 98 } 99 100 if (GETVAL(kIOPSCurrentCapacityKey, &numval)) { 101 SInt32 val = -1; 102 CFNumberGetValue(numval, kCFNumberSInt32Type, &val); 103 pct = (int)val; 104 } 105 106 if ((pct > 0) && (maxpct > 0)) { 107 pct = (int)((((double)pct) / ((double)maxpct)) * 100.0); 108 } 109 110 if (pct > 100) { 111 pct = 100; 112 } 113 114 /* 115 * We pick the battery that claims to have the most minutes left. 116 * (failing a report of minutes, we'll take the highest percent.) 117 */ 118 if ((secs < 0) && (*seconds < 0)) { 119 if ((pct < 0) && (*percent < 0)) { 120 choose = true; // at least we know there's a battery. 121 } 122 if (pct > *percent) { 123 choose = true; 124 } 125 } else if (secs > *seconds) { 126 choose = true; 127 } 128 129 if (choose) { 130 *seconds = secs; 131 *percent = pct; 132 *charging = charge; 133 } 134} 135 136#undef GETVAL 137#undef STRMATCH 138 139bool SDL_GetPowerInfo_MacOSX(SDL_PowerState *state, int *seconds, int *percent) 140{ 141 CFTypeRef blob = IOPSCopyPowerSourcesInfo(); 142 143 *seconds = -1; 144 *percent = -1; 145 *state = SDL_POWERSTATE_UNKNOWN; 146 147 if (blob != NULL) { 148 CFArrayRef list = IOPSCopyPowerSourcesList(blob); 149 if (list != NULL) { 150 // don't CFRelease() the list items, or dictionaries! 151 bool have_ac = false; 152 bool have_battery = false; 153 bool charging = false; 154 const CFIndex total = CFArrayGetCount(list); 155 CFIndex i; 156 for (i = 0; i < total; i++) { 157 CFTypeRef ps = (CFTypeRef)CFArrayGetValueAtIndex(list, i); 158 CFDictionaryRef dict = 159 IOPSGetPowerSourceDescription(blob, ps); 160 if (dict != NULL) { 161 checkps(dict, &have_ac, &have_battery, &charging, 162 seconds, percent); 163 } 164 } 165 166 if (!have_battery) { 167 *state = SDL_POWERSTATE_NO_BATTERY; 168 } else if (charging) { 169 *state = SDL_POWERSTATE_CHARGING; 170 } else if (have_ac) { 171 *state = SDL_POWERSTATE_CHARGED; 172 } else { 173 *state = SDL_POWERSTATE_ON_BATTERY; 174 } 175 176 CFRelease(list); 177 } 178 CFRelease(blob); 179 } 180 181 return true; // always the definitive answer on macOS. 182} 183 184#endif // SDL_POWER_MACOSX 185#endif // SDL_POWER_DISABLED 186
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.