SDL_bool SDL_GetPowerInfo_MacOSX(SDL_PowerState * state, int *seconds, int *percent) { CFTypeRef blob = IOPSCopyPowerSourcesInfo(); *seconds = -1; *percent = -1; *state = SDL_POWERSTATE_UNKNOWN; if (blob != NULL) { CFArrayRef list = IOPSCopyPowerSourcesList(blob); if (list != NULL) { /* don't CFRelease() the list items, or dictionaries! */ SDL_bool have_ac = SDL_FALSE; SDL_bool have_battery = SDL_FALSE; SDL_bool charging = SDL_FALSE; const CFIndex total = CFArrayGetCount(list); CFIndex i; for (i = 0; i < total; i++) { CFTypeRef ps = (CFTypeRef) CFArrayGetValueAtIndex(list, i); CFDictionaryRef dict = IOPSGetPowerSourceDescription(blob, ps); if (dict != NULL) { checkps(dict, &have_ac, &have_battery, &charging, seconds, percent); } } if (!have_battery) { *state = SDL_POWERSTATE_NO_BATTERY; } else if (charging) { *state = SDL_POWERSTATE_CHARGING; } else if (have_ac) { *state = SDL_POWERSTATE_CHARGED; } else { *state = SDL_POWERSTATE_ON_BATTERY; } CFRelease(list); } CFRelease(blob); } return SDL_TRUE; /* always the definitive answer on Mac OS X. */ }
// CODE CHUNK IMPORTED FROM SDL 2.0 bool power_osx::GetPowerInfo_MacOSX() { CFTypeRef blob = IOPSCopyPowerSourcesInfo(); nsecs_left = -1; percent_left = -1; power_state = OS::POWERSTATE_UNKNOWN; if (blob != NULL) { CFArrayRef list = IOPSCopyPowerSourcesList(blob); if (list != NULL) { /* don't CFRelease() the list items, or dictionaries! */ bool have_ac = false; bool have_battery = false; bool charging = false; const CFIndex total = CFArrayGetCount(list); CFIndex i; for (i = 0; i < total; i++) { CFTypeRef ps = (CFTypeRef)CFArrayGetValueAtIndex(list, i); CFDictionaryRef dict = IOPSGetPowerSourceDescription(blob, ps); if (dict != NULL) { checkps(dict, &have_ac, &have_battery, &charging); } } if (!have_battery) { power_state = OS::POWERSTATE_NO_BATTERY; } else if (charging) { power_state = OS::POWERSTATE_CHARGING; } else if (have_ac) { power_state = OS::POWERSTATE_CHARGED; } else { power_state = OS::POWERSTATE_ON_BATTERY; } CFRelease(list); } CFRelease(blob); } return true; /* always the definitive answer on Mac OS X. */ }
PowerInfo GetPowerState() { PowerInfo info; info.state = PowerInfo::STATE_UNKNOWN; info.seconds = 0; info.percent = 0.0f; #if defined(TARGET_OSX) CFTypeRef blob = IOPSCopyPowerSourcesInfo(); if (blob != NULL) { CFArrayRef list = IOPSCopyPowerSourcesList(blob); if (list != NULL) { /* don't CFRelease() the list items, or dictionaries! */ bool have_ac = false; bool have_battery = false; bool charging = false; const CFIndex total = CFArrayGetCount(list); CFIndex i; for (i = 0; i < total; i++) { CFTypeRef ps = (CFTypeRef) CFArrayGetValueAtIndex(list, i); CFDictionaryRef dict = IOPSGetPowerSourceDescription(blob, ps); if (dict != NULL) { checkps(dict, &have_ac, &have_battery, &charging, &info.seconds, &info.percent); } } if (!have_battery) { info.state = PowerInfo::STATE_NO_BATTERY; } else if (charging) { info.state = PowerInfo::STATE_CHARGING; } else if (have_ac) { info.state = PowerInfo::STATE_CHARGED; } else { info.state = PowerInfo::STATE_ON_BATTERY; } CFRelease(list); } CFRelease(blob); } #elif defined(TARGET_LINUX) #else #endif return info; }