2 * Copyright (c) 2000-2004,2011-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 // powerwatch - hook into system notifications of power events
28 #include "powerwatch.h"
29 #include <IOKit/IOMessage.h>
34 namespace MachPlusPlus
{
38 // The obligatory empty virtual destructor
40 PowerWatcher::~PowerWatcher()
45 // The default NULL implementations of the callback virtuals.
46 // We define these (rather than leaving them abstract) since
47 // many users want only one of these events.
49 void PowerWatcher::systemWillSleep()
52 void PowerWatcher::systemIsWaking()
55 void PowerWatcher::systemWillPowerDown()
58 void PowerWatcher::systemWillPowerOn()
65 IOPowerWatcher::IOPowerWatcher() :
68 if (!(mKernelPort
= ::IORegisterForSystemPower(this, &mPortRef
, ioCallback
, &mHandle
)))
69 UnixError::throwMe(EINVAL
); // no clue
72 IOPowerWatcher::~IOPowerWatcher()
75 ::IODeregisterForSystemPower(&mHandle
);
79 // The callback dispatcher
81 void IOPowerWatcher::ioCallback(void *refCon
, io_service_t service
,
82 natural_t messageType
, void *argument
)
84 IOPowerWatcher
*me
= (IOPowerWatcher
*)refCon
;
85 enum { allow
, refuse
, ignore
} reaction
;
86 switch (messageType
) {
87 case kIOMessageSystemWillSleep
:
88 secnotice("powerwatch", "system will sleep");
89 me
->systemWillSleep();
92 case kIOMessageSystemHasPoweredOn
:
93 secnotice("powerwatch", "system has powered on");
97 case kIOMessageSystemWillPowerOff
:
98 secnotice("powerwatch", "system will power off");
99 me
->systemWillPowerDown();
102 case kIOMessageSystemWillNotPowerOff
:
103 secnotice("powerwatch", "system will not power off");
106 case kIOMessageCanSystemSleep
:
107 secnotice("powerwatch", "can system sleep");
110 case kIOMessageSystemWillNotSleep
:
111 secnotice("powerwatch", "system will not sleep");
114 case kIOMessageCanSystemPowerOff
:
115 secnotice("powerwatch", "can system power off");
118 case kIOMessageSystemWillPowerOn
:
119 secnotice("powerwatch", "system will power on");
120 me
->systemWillPowerOn();
124 secnotice("powerwatch",
125 "type 0x%x message received (ignored)", messageType
);
130 // handle acknowledgments
133 secnotice("powerwatch", "calling IOAllowPowerChange");
134 IOAllowPowerChange(me
->mKernelPort
, long(argument
));
137 secnotice("powerwatch", "calling IOCancelPowerChange");
138 IOCancelPowerChange(me
->mKernelPort
, long(argument
));
141 secnotice("powerwatch", "sending no response");
148 // The MachServer hookup
150 PortPowerWatcher::PortPowerWatcher()
152 port(IONotificationPortGetMachPort(mPortRef
));
155 boolean_t
PortPowerWatcher::handle(mach_msg_header_t
*in
)
157 IODispatchCalloutFromMessage(NULL
, in
, mPortRef
);
162 } // end namespace MachPlusPlus
164 } // end namespace Security
166 #endif //TARGET_OS_OSX