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* systemWatch, 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
MacSystemWatch* macSystemWatch = static_cast<MacSystemWatch*>(systemWatch);
47
switch (messageType) {
48
case kIOMessageSystemWillSleep:
50
macSystemWatch->emitSleep();
51
IOAllowPowerChange(root_port, (long)messageArgument);
54
case kIOMessageCanSystemSleep:
57
// TODO: Check if file transfers are running, and don't go to sleep
59
//IOCancelPowerChange(root_port, (long)messageArgument);
61
macSystemWatch->emitIdleSleep();
62
IOAllowPowerChange(root_port, (long)messageArgument);
65
case kIOMessageSystemHasPoweredOn:
67
macSystemWatch->emitWakeup();
73
// -----------------------------------------------------------------------------
75
// -----------------------------------------------------------------------------
77
MacSystemWatch::MacSystemWatch()
79
// Initialize sleep callback
80
IONotificationPortRef notify;
81
io_object_t anIterator;
82
root_port = IORegisterForSystemPower(this, ¬ify, sleepCallBack, &anIterator);
84
printf("IORegisterForSystemPower failed\n");
87
CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notify), kCFRunLoopCommonModes);