1 | /*
|
---|
2 | * systemwatch_mac.cpp - Detect changes in the system state (Mac OS X).
|
---|
3 | * Copyright (C) 2005 Remko Troncon
|
---|
4 | *
|
---|
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.
|
---|
9 | *
|
---|
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.
|
---|
14 | *
|
---|
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
|
---|
18 | *
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include <ctype.h>
|
---|
22 | #include <stdlib.h>
|
---|
23 | #include <stdio.h>
|
---|
24 |
|
---|
25 | #include <mach/mach_port.h>
|
---|
26 | #include <mach/mach_interface.h>
|
---|
27 | #include <mach/mach_init.h>
|
---|
28 |
|
---|
29 | #include <IOKit/pwr_mgt/IOPMLib.h>
|
---|
30 | #include <IOKit/IOMessage.h>
|
---|
31 |
|
---|
32 | #include "systemwatch_mac.h"
|
---|
33 |
|
---|
34 |
|
---|
35 | // -----------------------------------------------------------------------------
|
---|
36 | // Callbacks
|
---|
37 | // -----------------------------------------------------------------------------
|
---|
38 |
|
---|
39 | io_connect_t root_port;
|
---|
40 |
|
---|
41 | void sleepCallBack(void *, io_service_t, natural_t messageType, void * messageArgument)
|
---|
42 | {
|
---|
43 | //printf("messageType %08lx, arg %08lx\n",
|
---|
44 | // (long unsigned int)messageType, (long unsigned int)messageArgument);
|
---|
45 |
|
---|
46 | switch (messageType) {
|
---|
47 | case kIOMessageSystemWillSleep:
|
---|
48 | // Sleep
|
---|
49 | MacSystemWatch::instance()->emitSleep();
|
---|
50 | IOAllowPowerChange(root_port, (long)messageArgument);
|
---|
51 | break;
|
---|
52 |
|
---|
53 | case kIOMessageCanSystemSleep:
|
---|
54 | // Idle time sleep
|
---|
55 |
|
---|
56 | // TODO: Check if file transfers are running, and don't go to sleep
|
---|
57 | // if there are.
|
---|
58 | //IOCancelPowerChange(root_port, (long)messageArgument);
|
---|
59 |
|
---|
60 | MacSystemWatch::instance()->emitIdleSleep();
|
---|
61 | IOAllowPowerChange(root_port, (long)messageArgument);
|
---|
62 | break;
|
---|
63 |
|
---|
64 | case kIOMessageSystemHasPoweredOn:
|
---|
65 | // Wakeup
|
---|
66 | MacSystemWatch::instance()->emitWakeup();
|
---|
67 | break;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | // -----------------------------------------------------------------------------
|
---|
73 | // MacSystemWatch
|
---|
74 | // -----------------------------------------------------------------------------
|
---|
75 |
|
---|
76 | MacSystemWatch::MacSystemWatch()
|
---|
77 | {
|
---|
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");
|
---|
84 | else
|
---|
85 | CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notify), kCFRunLoopCommonModes);
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | MacSystemWatch* MacSystemWatch::instance()
|
---|
90 | {
|
---|
91 | if (!instance_)
|
---|
92 | instance_ = new MacSystemWatch();
|
---|
93 |
|
---|
94 | return instance_;
|
---|
95 | }
|
---|
96 |
|
---|
97 | MacSystemWatch* MacSystemWatch::instance_ = 0;
|
---|