24 #include <IOKit/pwr_mgt/IOPMLib.h> 25 #include <IOKit/ps/IOPowerSources.h> 26 #include <IOKit/ps/IOPSKeys.h> 45 : m_powerRef(nullptr),
47 m_powerNotifier(MACH_PORT_NULL),
48 m_powerNotifyPort(nullptr)
54 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"OS X callback run loop not present - aborting"));
59 m_rootPowerDomain = IORegisterForSystemPower(
this, &m_powerNotifyPort,
PowerCallBack, &m_powerNotifier);
60 if (m_rootPowerDomain)
63 IONotificationPortGetRunLoopSource(m_powerNotifyPort),
64 kCFRunLoopDefaultMode);
68 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to setup power status callback"));
72 CFArrayRef batteryinfo =
nullptr;
74 if (IOPMCopyBatteryInfo(kIOMasterPortDefault, &batteryinfo) == kIOReturnSuccess)
76 CFRelease(batteryinfo);
82 CFRunLoopAddSource(
gAdminRunLoop, m_powerRef, kCFRunLoopDefaultMode);
88 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to setup power source callback"));
107 IONotificationPortGetRunLoopSource(m_powerNotifyPort),
108 kCFRunLoopDefaultMode );
109 IODeregisterForSystemPower(&m_powerNotifier);
110 IOServiceClose(m_rootPowerDomain);
111 IONotificationPortDestroy(m_powerNotifyPort);
117 CFRunLoopRemoveSource(
gAdminRunLoop, m_powerRef, kCFRunLoopDefaultMode);
118 CFRelease(m_powerRef);
131 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Sent shutdown command."));
136 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to send shutdown command."));
149 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Sent sleep command."));
153 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to send sleep command."));
172 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Sent restart command."));
176 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to send restart command."));
192 CFTypeRef info = IOPSCopyPowerSourcesInfo();
193 CFArrayRef list = IOPSCopyPowerSourcesList(info);
195 for (
int i = 0; i < CFArrayGetCount(list); i++)
197 CFTypeRef source = CFArrayGetValueAtIndex(list, i);
198 CFDictionaryRef description = IOPSGetPowerSourceDescription(info, source);
200 if ((CFBooleanRef)CFDictionaryGetValue(description, CFSTR(kIOPSIsPresentKey)) == kCFBooleanFalse)
203 CFStringRef type = (CFStringRef)CFDictionaryGetValue(description, CFSTR(kIOPSTransportTypeKey));
204 if (type && CFStringCompare(type, CFSTR(kIOPSInternalType), 0) == kCFCompareEqualTo)
206 CFStringRef state = (CFStringRef)CFDictionaryGetValue(description, CFSTR(kIOPSPowerSourceStateKey));
207 if (state && CFStringCompare(state, CFSTR(kIOPSACPowerValue), 0) == kCFCompareEqualTo)
211 else if (state && CFStringCompare(state, CFSTR(kIOPSBatteryPowerValue), 0) == kCFCompareEqualTo)
215 CFNumberRef capacity = (CFNumberRef)CFDictionaryGetValue(description, CFSTR(kIOPSCurrentCapacityKey));
216 CFNumberGetValue(capacity, kCFNumberSInt32Type, ¤t);
217 capacity = (CFNumberRef)CFDictionaryGetValue(description, CFSTR(kIOPSMaxCapacityKey));
218 CFNumberGetValue(capacity, kCFNumberSInt32Type, &max);
244 natural_t Type,
void *Data)
255 case kIOMessageCanSystemPowerOff:
256 IOAllowPowerChange(power->m_rootPowerDomain, (
long)Data);
259 case kIOMessageCanSystemSleep:
260 IOAllowPowerChange(power->m_rootPowerDomain, (
long)Data);
263 case kIOMessageSystemWillPowerOff:
264 IOAllowPowerChange(power->m_rootPowerDomain, (
long)Data);
267 case kIOMessageSystemWillRestart:
268 IOAllowPowerChange(power->m_rootPowerDomain, (
long)Data);
271 case kIOMessageSystemWillSleep:
272 IOAllowPowerChange(power->m_rootPowerDomain, (
long)Data);
275 case kIOMessageSystemHasPoweredOn:
300 AEAddressDesc targetDesc;
301 static const ProcessSerialNumber kPSNOfSystemProcess = { 0, kSystemProcess };
302 AppleEvent eventReply = { typeNull,
nullptr };
303 AppleEvent appleEventToSend = { typeNull,
nullptr };
305 OSStatus error = AECreateDesc(typeProcessSerialNumber, &kPSNOfSystemProcess,
306 sizeof(kPSNOfSystemProcess), &targetDesc);
311 error = AECreateAppleEvent(kCoreEventClass, EventToSend, &targetDesc,
312 kAutoGenerateReturnID, kAnyTransactionID, &appleEventToSend);
314 AEDisposeDesc(&targetDesc);
318 error = AESendMessage(&appleEventToSend, &eventReply, kAENormalPriority, kAEDefaultTimeout);
320 AEDisposeDesc(&appleEventToSend);
325 AEDisposeDesc(&eventReply);
333 void Score(
int &Score)
A power monitoring class for OS X.
static OSStatus SendAppleEventToSystemProcess(AEEventID EventToSend)
TorcPowerFactoryOSX TorcPowerFactoryOSX
bool DoRestart(void) override
Restart the system.
bool DoShutdown(void) override
Shutdown the system.
void BatteryUpdated(int Level)
TorcSetting * m_canHibernate
CFRunLoopRef gAdminRunLoop
A reference to the global administration CFRunLoop.
static void NotifyEvent(int Event)
bool DoHibernate(void) override
Hibernate the system.
TorcSetting * m_canSuspend
static void PowerSourceCallBack(void *Reference)
Receive notification of changes to the power supply.
QReadWriteLock m_httpServiceLock
TorcSetting * m_canShutdown
A generic power status class.
bool SetValue(const QVariant &Value)
#define LOG(_MASK_, _LEVEL_, _STRING_)
void Refresh(void)
Update the current power supply status.
bool DoSuspend(void) override
Suspend the system.
static void PowerCallBack(void *Reference, io_service_t Service, natural_t Type, void *Data)
Receive notification of power status changes.
TorcSetting * m_canRestart
Create a TorcPowerOSX singleton to handle power status.