]> Joshua Wise's Git repositories - ambien.git/blob - Ambien.cpp
initial import
[ambien.git] / Ambien.cpp
1 /*
2     File:               Ambien.h
3     Program:            Ambien
4     Original author:    Michael Roßberg
5                         mick@binaervarianz.de
6     Rewritten by:       Joshua Wise
7                         jwise@andrew.cmu.edu
8     Description:        Ambien is a kernel extension to disable sleep-on-clamshell.
9  
10     Ambien is free software; you can redistribute it and/or modify
11     it under the terms of the GNU General Public License as published by
12     the Free Software Foundation; either version 2 of the License, or
13     (at your option) any later version.
14
15     Ambien is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18     GNU General Public License for more details.
19
20     You should have received a copy of the GNU General Public License
21     along with Ambien; if not, write to the Free Software
22     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 #include "Ambien.h"
26 #include <IOKit/IOLib.h>
27 #include <IOKit/pwr_mgt/RootDomain.h>
28 #include <IOKit/pwr_mgt/IOPM.h>
29
30 #define super IOService
31
32 OSDefineMetaClassAndStructors(Ambien, IOService);
33
34 bool Ambien::init(OSDictionary* properties)
35 {
36         IOLog("Ambien.kext for Mac OS X Leopard, by Joshua Wise\n");
37         
38         _workLoop = 0;
39         if (super::init(properties) == false) {
40                 IOLog("Ambien::init: super::init failed\n");
41                 return false;
42         }
43
44         return true;
45 }
46
47 bool Ambien::start(IOService* provider)
48 {
49         static IOPMPowerState states[] =
50         {
51                 {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
52                 {1, kIOPMDeviceUsable, kIOPMPowerOn, kIOPMPowerOn, 0, 0, 0, 0, 0, 0, 0, 0}
53         };
54         
55         if (!super::start(provider)) {
56                 IOLog("Ambien::start: super::start failed\n");
57                 return false;
58         }
59         
60         PMinit();
61         provider->joinPMtree(this);
62         if (registerPowerDriver(this, states, 2) != IOPMNoErr)
63         {
64                 IOLog("Ambien::start: registerPowerDriver returned error -- uh-oh!\n");
65                 return false;
66         }
67
68         return true;
69 }
70
71 void Ambien::stop(IOService* provider)
72 {
73         if (_workLoop)
74         {
75                 _workLoop->release();
76                 _workLoop = 0;
77         }
78         
79         PMstop();
80         super::stop(provider);
81         IOLog("Ambien: your sleep schedule is back to you!\n");
82 }
83
84 IOReturn Ambien::setPowerState(unsigned long which, IOService *dev)
85 {
86         if (which == 0)
87         {
88                 IOLog("Ambien::setPowerState: system going to sleep\n");
89         } else {
90                 IOLog("Ambien::setPowerState: system is awake\n");
91         }
92         
93         return kIOPMAckImplied;
94 }
95
96 void Ambien::free()
97 {
98         super::free();
99         return;
100 }
101
102 IOWorkLoop* Ambien::getWorkLoop()
103 {
104         if (!_workLoop)
105                 _workLoop = IOWorkLoop::workLoop();
106                 
107         return _workLoop;
108 }
109
110 IOReturn Ambien::message(UInt32 type, IOService *provider, void *arg)
111 {
112         unsigned int argi = (unsigned int)arg;
113         
114         if (type == kIOPMMessageClamshellStateChange)
115         {
116                 IOPMrootDomain *root = NULL;
117                 
118                 IOLog("Ambien::message: kIOPMMessageClamshellStateChange: lid is %s, sleep bit is %d (arg %08x)\n",
119                       (argi & kClamshellStateBit) ? "closed" : "open",
120                       !!(argi & kClamshellSleepBit), argi);
121                 
122                 root = getPMRootDomain();
123                 if (!root)
124                 {
125                         IOLog("Ambien::message: failed to get PM root?\n");
126                         return 0;
127                 }
128                 root->receivePowerNotification((argi & kClamshellStateBit) ? kIOPMPreventSleep : kIOPMAllowSleep);
129         } else
130                 IOLog("Ambien::message: unknown type %08x (so I won't worry about it)\n", (unsigned int)type);
131         return super::message(type, provider, arg);
132 }
This page took 0.029157 seconds and 4 git commands to generate.