San Mehat | a430b2b | 2014-09-23 08:30:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | Copyright (C) 2002-2010 Karl J. Runge <runge@karlrunge.com> |
| 3 | All rights reserved. |
| 4 | |
| 5 | This file is part of x11vnc. |
| 6 | |
| 7 | x11vnc is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or (at |
| 10 | your option) any later version. |
| 11 | |
| 12 | x11vnc is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with x11vnc; if not, write to the Free Software |
| 19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA |
| 20 | or see <http://www.gnu.org/licenses/>. |
| 21 | |
| 22 | In addition, as a special exception, Karl J. Runge |
| 23 | gives permission to link the code of its release of x11vnc with the |
| 24 | OpenSSL project's "OpenSSL" library (or with modified versions of it |
| 25 | that use the same license as the "OpenSSL" library), and distribute |
| 26 | the linked executables. You must obey the GNU General Public License |
| 27 | in all respects for all of the code used other than "OpenSSL". If you |
| 28 | modify this file, you may extend this exception to your version of the |
| 29 | file, but you are not obligated to do so. If you do not wish to do |
| 30 | so, delete this exception statement from your version. |
| 31 | */ |
| 32 | |
| 33 | /* -- macosxCGP.c -- */ |
| 34 | |
| 35 | void 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 | |
| 43 | int macosxCGP_save_dim(void); |
| 44 | int macosxCGP_restore_dim(void); |
| 45 | int macosxCGP_save_sleep(void); |
| 46 | int macosxCGP_restore_sleep(void); |
| 47 | int macosxCGP_init_dimming(void); |
| 48 | int macosxCGP_undim(void); |
| 49 | int macosxCGP_dim_shutdown(void); |
| 50 | void macosxCGP_screensaver_timer_off(void); |
| 51 | void macosxCGP_screensaver_timer_on(void); |
| 52 | |
| 53 | #include <IOKit/pwr_mgt/IOPMLib.h> |
| 54 | #include <IOKit/pwr_mgt/IOPM.h> |
| 55 | |
| 56 | extern CGDirectDisplayID displayID; |
| 57 | |
| 58 | static unsigned long dim_time; |
| 59 | static unsigned long sleep_time; |
| 60 | static int dim_time_saved = 0; |
| 61 | static int sleep_time_saved = 0; |
| 62 | static int initialized = 0; |
| 63 | static mach_port_t master_dev_port; |
| 64 | static io_connect_t power_mgt; |
| 65 | |
| 66 | extern int client_count; |
| 67 | extern int macosx_nodimming; |
| 68 | extern int macosx_nosleep; |
| 69 | extern int macosx_noscreensaver; |
| 70 | |
| 71 | static EventLoopTimerUPP sstimerUPP; |
| 72 | static EventLoopTimerRef sstimer; |
| 73 | |
| 74 | void 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 | |
| 83 | void 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 | |
| 89 | void 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 | |
| 96 | int 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 | |
| 105 | int 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 | |
| 118 | int 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 | |
| 127 | int 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 | |
| 140 | int 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 | |
| 171 | int 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 | |
| 202 | int 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 | |