7 #include <IOKit/ps/IOPowerSources.h>
8 #include <IOKit/ps/IOPSKeys.h>
9 #include <AvailabilityMacros.h>
15 #define LOC QString("PowerOSX: ")
34 CFRunLoopRemoveSource(CFRunLoopGetCurrent(),
36 kCFRunLoopDefaultMode );
44 CFRunLoopRemoveSource(CFRunLoopGetCurrent(),
m_powerRef, kCFRunLoopDefaultMode);
57 CFRunLoopAddSource(CFRunLoopGetCurrent(),
59 kCFRunLoopDefaultMode);
63 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Failed to setup power status callback");
67 CFArrayRef batteryinfo = NULL;
70 CFRelease(batteryinfo);
76 CFRunLoopAddSource(CFRunLoopGetCurrent(),
m_powerRef, kCFRunLoopDefaultMode);
82 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Failed to setup power source callback");
85 if (IOPMSleepEnabled())
103 default:
return false;
114 CFTypeRef
info = IOPSCopyPowerSourcesInfo();
115 CFArrayRef list = IOPSCopyPowerSourcesList(
info);
117 for (
int i = 0; i < CFArrayGetCount(list); i++)
119 CFTypeRef source = CFArrayGetValueAtIndex(list, i);
120 CFDictionaryRef description = IOPSGetPowerSourceDescription(
info, source);
122 if (
static_cast<CFBooleanRef
>(CFDictionaryGetValue(description, CFSTR(kIOPSIsPresentKey))) == kCFBooleanFalse)
125 auto type =
static_cast<CFStringRef
>(CFDictionaryGetValue(description, CFSTR(kIOPSTransportTypeKey)));
126 if (
type && CFStringCompare(
type, CFSTR(kIOPSInternalType), 0) == kCFCompareEqualTo)
128 auto state =
static_cast<CFStringRef
>(CFDictionaryGetValue(description, CFSTR(kIOPSPowerSourceStateKey)));
129 if (state && CFStringCompare(state, CFSTR(kIOPSACPowerValue), 0) == kCFCompareEqualTo)
133 else if (state && CFStringCompare(state, CFSTR(kIOPSBatteryPowerValue), 0) == kCFCompareEqualTo)
137 auto capacity =
static_cast<CFNumberRef
>(CFDictionaryGetValue(description, CFSTR(kIOPSCurrentCapacityKey)));
138 CFNumberGetValue(capacity, kCFNumberSInt32Type, &
current);
139 capacity =
static_cast<CFNumberRef
>(CFDictionaryGetValue(description, CFSTR(kIOPSMaxCapacityKey)));
140 CFNumberGetValue(capacity, kCFNumberSInt32Type, &max);
141 newlevel =
static_cast<int>(((
static_cast<float>(
current) /
142 (
static_cast<float>(max))) * 100.0F));
175 natural_t Type,
void *Data)
184 case kIOMessageCanSystemPowerOff:
185 IOAllowPowerChange(power->m_rootPowerDomain,
reinterpret_cast<intptr_t
>(Data));
188 case kIOMessageCanSystemSleep:
189 IOAllowPowerChange(power->m_rootPowerDomain,
reinterpret_cast<intptr_t
>(Data));
192 case kIOMessageSystemWillPowerOff:
193 IOAllowPowerChange(power->m_rootPowerDomain,
reinterpret_cast<intptr_t
>(Data));
196 case kIOMessageSystemWillRestart:
197 IOAllowPowerChange(power->m_rootPowerDomain,
reinterpret_cast<intptr_t
>(Data));
200 case kIOMessageSystemWillSleep:
201 IOAllowPowerChange(power->m_rootPowerDomain,
reinterpret_cast<intptr_t
>(Data));
204 case kIOMessageSystemHasPoweredOn:
213 AEAddressDesc targetDesc;
214 static const ProcessSerialNumber kPSNOfSystemProcess = { 0, kSystemProcess };
215 AppleEvent eventReply = { typeNull,
nullptr };
216 AppleEvent appleEventToSend = { typeNull,
nullptr };
218 OSStatus
error = noErr;
219 error = AECreateDesc(typeProcessSerialNumber, &kPSNOfSystemProcess,
220 sizeof(kPSNOfSystemProcess), &targetDesc);
224 error = AECreateAppleEvent(kCoreEventClass, EventToSend, &targetDesc,
225 kAutoGenerateReturnID, kAnyTransactionID, &appleEventToSend);
226 AEDisposeDesc(&targetDesc);
230 error = AESendMessage(&appleEventToSend, &eventReply, kAENormalPriority, kAEDefaultTimeout);
231 AEDisposeDesc(&appleEventToSend);
235 AEDisposeDesc(&eventReply);