2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // powerwatch - hook into system notifications of power events
22 #include "powerwatch.h"
23 #include <IOKit/IOMessage.h>
27 namespace MachPlusPlus
{
31 // The obligatory empty virtual destructor
33 PowerWatcher::PowerWatcher()
35 if (!(mKernelPort
= IORegisterForSystemPower(this, &mPortRef
, ioCallback
, &mHandle
)))
36 UnixError::throwMe(EINVAL
); // no clue
39 PowerWatcher::~PowerWatcher()
42 IODeregisterForSystemPower(&mHandle
);
47 // The callback dispatcher
49 void PowerWatcher::ioCallback(void *refCon
, io_service_t service
,
50 natural_t messageType
, void *argument
)
52 PowerWatcher
*me
= (PowerWatcher
*)refCon
;
53 enum { allow
, refuse
, ignore
} reaction
;
54 switch (messageType
) {
55 case kIOMessageSystemWillSleep
:
56 debug("powerwatch", "system will sleep");
57 me
->systemWillSleep();
60 case kIOMessageSystemHasPoweredOn
:
61 debug("powerwatch", "system has powered on");
65 case kIOMessageSystemWillPowerOff
:
66 debug("powerwatch", "system will power off");
67 me
->systemWillPowerDown();
70 case kIOMessageSystemWillNotPowerOff
:
71 debug("powerwatch", "system will not power off");
74 case kIOMessageCanSystemSleep
:
75 debug("powerwatch", "can system sleep");
78 case kIOMessageSystemWillNotSleep
:
79 debug("powerwatch", "system will not sleep");
82 case kIOMessageCanSystemPowerOff
:
83 debug("powerwatch", "can system power off");
88 "type 0x%x message received (ignored)", messageType
);
93 // handle acknowledgments
96 debug("powerwatch", "calling IOAllowPowerChange");
97 IOAllowPowerChange(me
->mKernelPort
, long(argument
));
100 debug("powerwatch", "calling IOCancelPowerChange");
101 IOCancelPowerChange(me
->mKernelPort
, long(argument
));
104 debug("powerwatch", "sending no response");
111 // The default NULL implementations of the callback virtuals.
112 // We define these (rather than leaving them abstract) since
113 // many users want only one of these events.
115 void PowerWatcher::systemWillSleep()
118 void PowerWatcher::systemIsWaking()
121 void PowerWatcher::systemWillPowerDown()
126 // The MachServer hookup
128 PortPowerWatcher::PortPowerWatcher()
130 port(IONotificationPortGetMachPort(mPortRef
));
133 PortPowerWatcher::~PortPowerWatcher()
137 boolean_t
PortPowerWatcher::handle(mach_msg_header_t
*in
)
139 IODispatchCalloutFromMessage(NULL
, in
, mPortRef
);
144 } // end namespace MachPlusPlus
146 } // end namespace Security