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 switch (messageType
) {
54 case kIOMessageSystemWillSleep
:
55 debug("powerwatch", "system will sleep");
56 me
->systemWillSleep();
58 case kIOMessageSystemHasPoweredOn
:
59 debug("powerwatch", "system has powered on");
62 case kIOMessageSystemWillPowerOff
:
63 debug("powerwatch", "system will power off");
64 me
->systemWillPowerDown();
68 case kIOMessageSystemWillNotPowerOff
:
69 debug("powerwatch", "system will not power off");
71 case kIOMessageCanSystemSleep
:
72 debug("powerwatch", "can system sleep");
74 case kIOMessageSystemWillNotSleep
:
75 debug("powerwatch", "system will not sleep");
77 case kIOMessageCanSystemPowerOff
:
78 debug("powerwatch", "can system power off");
82 "type 0x%x message received (ignored)", messageType
);
88 IOAllowPowerChange(me
->mKernelPort
, long(argument
));
93 // The default NULL implementations of the callback virtuals.
94 // We define these (rather than leaving them abstract) since
95 // many users want only one of these events.
97 void PowerWatcher::systemWillSleep()
100 void PowerWatcher::systemIsWaking()
103 void PowerWatcher::systemWillPowerDown()
108 // The MachServer hookup
110 PortPowerWatcher::PortPowerWatcher()
112 port(IONotificationPortGetMachPort(mPortRef
));
115 PortPowerWatcher::~PortPowerWatcher()
119 boolean_t
PortPowerWatcher::handle(mach_msg_header_t
*in
)
121 IODispatchCalloutFromMessage(NULL
, in
, mPortRef
);
126 } // end namespace MachPlusPlus
128 } // end namespace Security