2
* systemwatch_mac.cpp - Detect changes in the system state (Mac OS X).
3
* Copyright (C) 2005 Remko Troncon
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
#include <mach/mach_port.h>
26
#include <mach/mach_interface.h>
27
#include <mach/mach_init.h>
29
#include <IOKit/pwr_mgt/IOPMLib.h>
30
#include <IOKit/IOMessage.h>
32
#include "systemwatch_mac.h"
35
// -----------------------------------------------------------------------------
37
// -----------------------------------------------------------------------------
39
io_connect_t root_port;
41
void sleepCallBack(void *, io_service_t, natural_t messageType, void * messageArgument)
43
//printf("messageType %08lx, arg %08lx\n",
44
// (long unsigned int)messageType, (long unsigned int)messageArgument);
46
switch (messageType) {
47
case kIOMessageSystemWillSleep:
49
MacSystemWatch::instance()->emitSleep();
50
IOAllowPowerChange(root_port, (long)messageArgument);
53
case kIOMessageCanSystemSleep:
56
// TODO: Check if file transfers are running, and don't go to sleep
58
//IOCancelPowerChange(root_port, (long)messageArgument);
60
MacSystemWatch::instance()->emitIdleSleep();
61
IOAllowPowerChange(root_port, (long)messageArgument);
64
case kIOMessageSystemHasPoweredOn:
66
MacSystemWatch::instance()->emitWakeup();
72
// -----------------------------------------------------------------------------
74
// -----------------------------------------------------------------------------
76
MacSystemWatch::MacSystemWatch()
78
// Initialize sleep callback
79
IONotificationPortRef notify;
80
io_object_t anIterator;
81
root_port = IORegisterForSystemPower(0, ¬ify, sleepCallBack, &anIterator);
82
if (root_port == NULL)
83
printf("IORegisterForSystemPower failed\n");
85
CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notify), kCFRunLoopCommonModes);
89
MacSystemWatch* MacSystemWatch::instance()
92
instance_ = new MacSystemWatch();
97
MacSystemWatch* MacSystemWatch::instance_ = 0;