35 #if defined(__APPLE__)
36 #include <mach/mach_port.h>
37 #include <mach/mach_interface.h>
38 #include <mach/mach_init.h>
39 #include <IOKit/pwr_mgt/IOPMLib.h>
40 #include <IOKit/IOMessage.h>
47 std::function<
void()> wakeup_callback)
49 m_callback_sleep(sleep_callback), m_callback_wakeup(wakeup_callback) {
63 #if defined(__APPLE__)
65 void system_sleep_callback(
void * refCon, io_service_t service,
66 natural_t messageType,
void * messageArgument) {
69 switch ( messageType ) {
71 case kIOMessageCanSystemSleep:
86 IOAllowPowerChange( sleep_wake_notifier->get_root_port(), (long)messageArgument );
89 case kIOMessageSystemWillSleep:
98 IOAllowPowerChange( sleep_wake_notifier->get_root_port(), (long)messageArgument );
101 case kIOMessageSystemWillPowerOn:
106 case kIOMessageSystemHasPoweredOn:
120 #if defined(__APPLE__)
123 m_root_port = IORegisterForSystemPower(
this, &m_notify_port_ref,
124 system_sleep_callback,
126 if ( m_root_port == 0 ) {
127 HT_INFO(
"IORegisterForSystemPower failed");
132 CFRunLoopAddSource( CFRunLoopGetCurrent(),
133 IONotificationPortGetRunLoopSource(m_notify_port_ref), kCFRunLoopCommonModes );
136 m_run_loop = CFRunLoopGetCurrent();
150 #if defined(__APPLE__)
152 CFRunLoopRemoveSource( CFRunLoopGetCurrent(),
153 IONotificationPortGetRunLoopSource(m_notify_port_ref),
154 kCFRunLoopCommonModes );
157 IODeregisterForSystemPower( &m_notifier_object );
161 IOServiceClose( m_root_port );
164 IONotificationPortDestroy( m_notify_port_ref );
167 CFRunLoopStop(m_run_loop);
Delivers sleep and wakeup notifications.
SleepWakeNotifier(std::function< void()> sleep_callback, std::function< void()> wakeup_callback)
Constructor.
std::thread m_thread
Event notification polling thread.
~SleepWakeNotifier()
Destructor Does OS-specific deregistering of sleep/wakeup notification interest and then joins m_thre...
std::function< void()> m_callback_sleep
Registered sleep callback.
void thread_func()
Thread function for event notification polling thread (m_thread)
Logging routines and macros.
void handle_sleep()
Called by polling thread to deliver sleep notification Calls registered sleep callback m_callback_sle...
Compatibility Macros for C/C++.
std::function< void()> m_callback_wakeup
Registered wakeup callback.
Declaration for SleepWakeNotifier.
void handle_wakeup()
Called by polling thread to deliver wakeup notification Calls registered wakeup callback m_callback_w...