1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/*
Copyright (C) 2002-2010 Karl J. Runge <runge@karlrunge.com>
All rights reserved.
This file is part of x11vnc.
x11vnc is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
x11vnc is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with x11vnc; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA
or see <http://www.gnu.org/licenses/>.
In addition, as a special exception, Karl J. Runge
gives permission to link the code of its release of x11vnc with the
OpenSSL project's "OpenSSL" library (or with modified versions of it
that use the same license as the "OpenSSL" library), and distribute
the linked executables. You must obey the GNU General Public License
in all respects for all of the code used other than "OpenSSL". If you
modify this file, you may extend this exception to your version of the
file, but you are not obligated to do so. If you do not wish to do
so, delete this exception statement from your version.
*/
/* -- pm.c -- */
#include "x11vnc.h"
#include "cleanup.h"
void check_pm(void);
void set_dpms_mode(char *mode);
static void check_fbpm(void);
static void check_dpms(void);
#if LIBVNCSERVER_HAVE_FBPM
#include <X11/Xmd.h>
#include <X11/extensions/fbpm.h>
#endif
#if LIBVNCSERVER_HAVE_DPMS
#include <X11/extensions/dpms.h>
#endif
void check_pm(void) {
static int skip = -1;
if (skip < 0) {
skip = 0;
if (getenv("X11VNC_NO_CHECK_PM")) {
skip = 1;
}
}
if (skip) {
return;
}
check_fbpm();
check_dpms();
/* someday dpms activities? */
}
static void check_fbpm(void) {
static int init_fbpm = 0;
#if LIBVNCSERVER_HAVE_FBPM
static int fbpm_capable = 0;
static time_t last_fbpm = 0;
int db = 0;
CARD16 level;
BOOL enabled;
RAWFB_RET_VOID
if (! init_fbpm) {
if (getenv("FBPM_DEBUG")) {
db = atoi(getenv("FBPM_DEBUG"));
}
if (FBPMCapable(dpy)) {
fbpm_capable = 1;
rfbLog("X display is capable of FBPM.\n");
if (watch_fbpm) {
rfbLog("Preventing low-power FBPM modes when"
" clients are connected.\n");
}
} else {
if (! raw_fb_str) {
rfbLog("X display is not capable of FBPM.\n");
}
fbpm_capable = 0;
}
init_fbpm = 1;
}
if (! watch_fbpm) {
return;
}
if (! fbpm_capable) {
return;
}
if (! client_count) {
return;
}
if (time(NULL) < last_fbpm + 5) {
return;
}
last_fbpm = time(NULL);
if (FBPMInfo(dpy, &level, &enabled)) {
if (db) fprintf(stderr, "FBPMInfo level: %d enabled: %d\n", level, enabled);
if (enabled && level != FBPMModeOn) {
char *from = "unknown-fbpm-state";
XErrorHandler old_handler = XSetErrorHandler(trap_xerror);
trapped_xerror = 0;
if (level == FBPMModeStandby) {
from = "FBPMModeStandby";
} else if (level == FBPMModeSuspend) {
from = "FBPMModeSuspend";
} else if (level == FBPMModeOff) {
from = "FBPMModeOff";
}
rfbLog("switching FBPM state from %s to FBPMModeOn\n", from);
FBPMForceLevel(dpy, FBPMModeOn);
XSetErrorHandler(old_handler);
trapped_xerror = 0;
}
} else {
if (db) fprintf(stderr, "FBPMInfo failed.\n");
}
#else
RAWFB_RET_VOID
if (! init_fbpm) {
if (! raw_fb_str) {
rfbLog("X FBPM extension not supported.\n");
}
init_fbpm = 1;
}
#endif
}
void set_dpms_mode(char *mode) {
#if NO_X11
return;
#else
RAWFB_RET_VOID
#if LIBVNCSERVER_HAVE_DPMS
if (dpy && DPMSCapable(dpy)) {
CARD16 level;
CARD16 want;
BOOL enabled;
if (!strcmp(mode, "off")) {
want = DPMSModeOff;
} else if (!strcmp(mode, "on")) {
want = DPMSModeOn;
} else if (!strcmp(mode, "standby")) {
want = DPMSModeStandby;
} else if (!strcmp(mode, "suspend")) {
want = DPMSModeSuspend;
} else if (!strcmp(mode, "enable")) {
DPMSEnable(dpy);
return;
} else if (!strcmp(mode, "disable")) {
DPMSDisable(dpy);
return;
} else {
return;
}
if (DPMSInfo(dpy, &level, &enabled)) {
char *from = "unk";
if (enabled && level != want) {
XErrorHandler old_handler = XSetErrorHandler(trap_xerror);
trapped_xerror = 0;
rfbLog("DPMSInfo level: %d enabled: %d\n", level, enabled);
if (level == DPMSModeStandby) {
from = "DPMSModeStandby";
} else if (level == DPMSModeSuspend) {
from = "DPMSModeSuspend";
} else if (level == DPMSModeOff) {
from = "DPMSModeOff";
} else if (level == DPMSModeOn) {
from = "DPMSModeOn";
}
rfbLog("switching DPMS state from %s to %s\n", from, mode);
DPMSForceLevel(dpy, want);
XSetErrorHandler(old_handler);
trapped_xerror = 0;
}
}
}
#endif
#endif
}
static void check_dpms(void) {
static int init_dpms = 0;
#if LIBVNCSERVER_HAVE_DPMS
static int dpms_capable = 0;
static time_t last_dpms = 0;
int db = 0;
CARD16 level;
BOOL enabled;
RAWFB_RET_VOID
if (! init_dpms) {
if (getenv("DPMS_DEBUG")) {
db = atoi(getenv("DPMS_DEBUG"));
}
if (DPMSCapable(dpy)) {
dpms_capable = 1;
rfbLog("X display is capable of DPMS.\n");
if (watch_dpms) {
rfbLog("Preventing low-power DPMS modes when"
" clients are connected.\n");
}
} else {
if (! raw_fb_str) {
rfbLog("X display is not capable of DPMS.\n");
}
dpms_capable = 0;
}
init_dpms = 1;
}
if (force_dpms || (client_dpms && client_count)) {
static int last_enable = 0;
if (time(NULL) > last_enable) {
set_dpms_mode("enable");
last_enable = time(NULL);
}
set_dpms_mode("off");
}
if (! watch_dpms) {
return;
}
if (! dpms_capable) {
return;
}
if (! client_count) {
return;
}
if (time(NULL) < last_dpms + 5) {
return;
}
last_dpms = time(NULL);
if (DPMSInfo(dpy, &level, &enabled)) {
if (db) fprintf(stderr, "DPMSInfo level: %d enabled: %d\n", level, enabled);
if (enabled && level != DPMSModeOn) {
char *from = "unknown-dpms-state";
XErrorHandler old_handler = XSetErrorHandler(trap_xerror);
trapped_xerror = 0;
if (level == DPMSModeStandby) {
from = "DPMSModeStandby";
} else if (level == DPMSModeSuspend) {
from = "DPMSModeSuspend";
} else if (level == DPMSModeOff) {
from = "DPMSModeOff";
}
rfbLog("switching DPMS state from %s to DPMSModeOn\n", from);
DPMSForceLevel(dpy, DPMSModeOn);
XSetErrorHandler(old_handler);
trapped_xerror = 0;
}
} else {
if (db) fprintf(stderr, "DPMSInfo failed.\n");
}
#else
RAWFB_RET_VOID
if (! init_dpms) {
if (! raw_fb_str) {
rfbLog("X DPMS extension not supported.\n");
}
init_dpms = 1;
}
#endif
}
|