blob: 230042c45ca2904dd3d913ebd76f2d15f970323c [file] [log] [blame]
San Mehata430b2b2014-09-23 08:30:51 -07001/*
2 Copyright (C) 2002-2010 Karl J. Runge <runge@karlrunge.com>
3 All rights reserved.
4
5This file is part of x11vnc.
6
7x11vnc is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or (at
10your option) any later version.
11
12x11vnc is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with x11vnc; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA
20or see <http://www.gnu.org/licenses/>.
21
22In addition, as a special exception, Karl J. Runge
23gives permission to link the code of its release of x11vnc with the
24OpenSSL project's "OpenSSL" library (or with modified versions of it
25that use the same license as the "OpenSSL" library), and distribute
26the linked executables. You must obey the GNU General Public License
27in all respects for all of the code used other than "OpenSSL". If you
28modify this file, you may extend this exception to your version of the
29file, but you are not obligated to do so. If you do not wish to do
30so, delete this exception statement from your version.
31*/
32
33/* -- macosxCGP.c -- */
34
35void macosxCGP_unused(void) {}
36
37#if (defined(__MACH__) && defined(__APPLE__))
38
39#include <ApplicationServices/ApplicationServices.h>
40#include <Cocoa/Cocoa.h>
41#include <Carbon/Carbon.h>
42
43int macosxCGP_save_dim(void);
44int macosxCGP_restore_dim(void);
45int macosxCGP_save_sleep(void);
46int macosxCGP_restore_sleep(void);
47int macosxCGP_init_dimming(void);
48int macosxCGP_undim(void);
49int macosxCGP_dim_shutdown(void);
50void macosxCGP_screensaver_timer_off(void);
51void macosxCGP_screensaver_timer_on(void);
52
53#include <IOKit/pwr_mgt/IOPMLib.h>
54#include <IOKit/pwr_mgt/IOPM.h>
55
56extern CGDirectDisplayID displayID;
57
58static unsigned long dim_time;
59static unsigned long sleep_time;
60static int dim_time_saved = 0;
61static int sleep_time_saved = 0;
62static int initialized = 0;
63static mach_port_t master_dev_port;
64static io_connect_t power_mgt;
65
66extern int client_count;
67extern int macosx_nodimming;
68extern int macosx_nosleep;
69extern int macosx_noscreensaver;
70
71static EventLoopTimerUPP sstimerUPP;
72static EventLoopTimerRef sstimer;
73
74void macosxCG_screensaver_timer(EventLoopTimerRef timer, void *data) {
75 if (0) fprintf(stderr, "macosxCG_screensaver_timer: %d\n", (int) time(0));
76 if (!timer || !data) {}
77 if (macosx_nosleep && client_count) {
78 if (0) fprintf(stderr, "UpdateSystemActivity: %d\n", (int) time(0));
79 UpdateSystemActivity(IdleActivity);
80 }
81}
82
83void macosxCGP_screensaver_timer_off(void) {
84 if (0) fprintf(stderr, "macosxCGP_screensaver_timer_off: %d\n", (int) time(0));
85 RemoveEventLoopTimer(sstimer);
86 DisposeEventLoopTimerUPP(sstimerUPP);
87}
88
89void macosxCGP_screensaver_timer_on(void) {
90 if (0) fprintf(stderr, "macosxCGP_screensaver_timer_on: %d\n", (int) time(0));
91 sstimerUPP = NewEventLoopTimerUPP(macosxCG_screensaver_timer);
92 InstallEventLoopTimer(GetMainEventLoop(), kEventDurationSecond * 30,
93 kEventDurationSecond * 30, sstimerUPP, NULL, &sstimer);
94}
95
96int macosxCGP_save_dim(void) {
97 if (IOPMGetAggressiveness(power_mgt, kPMMinutesToDim,
98 &dim_time) != kIOReturnSuccess) {
99 return 0;
100 }
101 dim_time_saved = 1;
102 return 1;
103}
104
105int macosxCGP_restore_dim(void) {
106 if (! dim_time_saved) {
107 return 0;
108 }
109 if (IOPMSetAggressiveness(power_mgt, kPMMinutesToDim,
110 dim_time) != kIOReturnSuccess) {
111 return 0;
112 }
113 dim_time_saved = 0;
114 dim_time = 0;
115 return 1;
116}
117
118int macosxCGP_save_sleep(void) {
119 if (IOPMGetAggressiveness(power_mgt, kPMMinutesToSleep,
120 &sleep_time) != kIOReturnSuccess) {
121 return 0;
122 }
123 sleep_time_saved = 1;
124 return 1;
125}
126
127int macosxCGP_restore_sleep(void) {
128 if (! sleep_time_saved) {
129 return 0;
130 }
131 if (IOPMSetAggressiveness(power_mgt, kPMMinutesToSleep,
132 dim_time) != kIOReturnSuccess) {
133 return 0;
134 }
135 sleep_time_saved = 0;
136 sleep_time = 0;
137 return 1;
138}
139
140int macosxCGP_init_dimming(void) {
141 if (IOMasterPort(bootstrap_port, &master_dev_port) !=
142 kIOReturnSuccess) {
143 return 0;
144 }
145 if (!(power_mgt = IOPMFindPowerManagement(master_dev_port))) {
146 return 0;
147 }
148 if (macosx_nodimming) {
149 if (! macosxCGP_save_dim()) {
150 return 0;
151 }
152 if (IOPMSetAggressiveness(power_mgt, kPMMinutesToDim, 0)
153 != kIOReturnSuccess) {
154 return 0;
155 }
156 }
157 if (macosx_nosleep) {
158 if (! macosxCGP_save_sleep()) {
159 return 0;
160 }
161 if (IOPMSetAggressiveness(power_mgt, kPMMinutesToSleep, 0)
162 != kIOReturnSuccess) {
163 return 0;
164 }
165 }
166
167 initialized = 1;
168 return 1;
169}
170
171int macosxCGP_undim(void) {
172 if (! initialized) {
173 return 0;
174 }
175 if (! macosx_nodimming) {
176 if (! macosxCGP_save_dim()) {
177 return 0;
178 }
179 if (IOPMSetAggressiveness(power_mgt, kPMMinutesToDim, 0)
180 != kIOReturnSuccess) {
181 return 0;
182 }
183 if (! macosxCGP_restore_dim()) {
184 return 0;
185 }
186 }
187 if (! macosx_nosleep) {
188 if (! macosxCGP_save_sleep()) {
189 return 0;
190 }
191 if (IOPMSetAggressiveness(power_mgt, kPMMinutesToSleep, 0)
192 != kIOReturnSuccess) {
193 return 0;
194 }
195 if (! macosxCGP_restore_sleep()) {
196 return 0;
197 }
198 }
199 return 1;
200}
201
202int macosxCGP_dim_shutdown(void) {
203 if (! initialized) {
204 return 0;
205 }
206 if (dim_time_saved) {
207 if (! macosxCGP_restore_dim()) {
208 return 0;
209 }
210 }
211 if (sleep_time_saved) {
212 if (! macosxCGP_restore_sleep()) {
213 return 0;
214 }
215 }
216 return 1;
217}
218
219#endif /* __APPLE__ */
220
221