~ubuntu-branches/debian/lenny/italc/lenny

« back to all changes in this revision

Viewing changes to ica/x11/x11vnc/pm.c

  • Committer: Bazaar Package Importer
  • Author(s): Patrick Winnertz
  • Date: 2008-06-17 13:46:54 UTC
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20080617134654-2y5m7ki93r5c1ysf
Tags: upstream-1.0.9~rc3
ImportĀ upstreamĀ versionĀ 1.0.9~rc3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -- pm.c -- */
 
2
#include "x11vnc.h"
 
3
#include "cleanup.h"
 
4
 
 
5
void check_pm(void);
 
6
void set_dpms_mode(char *mode);
 
7
static void check_fbpm(void);
 
8
static void check_dpms(void);
 
9
 
 
10
#if LIBVNCSERVER_HAVE_FBPM
 
11
#include <X11/Xmd.h>
 
12
#include <X11/extensions/fbpm.h>
 
13
#endif
 
14
 
 
15
#if LIBVNCSERVER_HAVE_DPMS
 
16
#include <X11/extensions/dpms.h>
 
17
#endif
 
18
 
 
19
void check_pm(void) {
 
20
        check_fbpm();
 
21
        check_dpms();
 
22
        /* someday dpms activities? */
 
23
}
 
24
 
 
25
static void check_fbpm(void) {
 
26
        static int init_fbpm = 0;
 
27
#if LIBVNCSERVER_HAVE_FBPM
 
28
        static int fbpm_capable = 0;
 
29
        static time_t last_fbpm = 0;
 
30
        int db = 0;
 
31
 
 
32
        CARD16 level;
 
33
        BOOL enabled;
 
34
 
 
35
        RAWFB_RET_VOID
 
36
 
 
37
        if (! init_fbpm) {
 
38
                if (getenv("FBPM_DEBUG")) {
 
39
                        db = atoi(getenv("FBPM_DEBUG"));
 
40
                }
 
41
                if (FBPMCapable(dpy)) {
 
42
                        fbpm_capable = 1;
 
43
                        rfbLog("X display is capable of FBPM.\n");
 
44
                        if (watch_fbpm) {
 
45
                                rfbLog("Preventing low-power FBPM modes when"
 
46
                                    " clients are connected.\n");
 
47
                        }
 
48
                } else {
 
49
                        if (! raw_fb_str) {
 
50
                                rfbLog("X display is not capable of FBPM.\n");
 
51
                        }
 
52
                        fbpm_capable = 0;
 
53
                }
 
54
                init_fbpm = 1;
 
55
        }
 
56
 
 
57
        if (! watch_fbpm) {
 
58
                return;
 
59
        }
 
60
        if (! fbpm_capable) {
 
61
                return;
 
62
        }
 
63
        if (! client_count) {
 
64
                return;
 
65
        }
 
66
        if (time(NULL) < last_fbpm + 5) {
 
67
                return;
 
68
        }
 
69
        last_fbpm = time(NULL);
 
70
 
 
71
        if (FBPMInfo(dpy, &level, &enabled)) {
 
72
                if (db) fprintf(stderr, "FBPMInfo level: %d enabled: %d\n", level, enabled);
 
73
 
 
74
                if (enabled && level != FBPMModeOn) {
 
75
                        char *from = "unknown-fbpm-state";
 
76
                        XErrorHandler old_handler = XSetErrorHandler(trap_xerror);
 
77
                        trapped_xerror = 0;
 
78
 
 
79
                        if (level == FBPMModeStandby) {
 
80
                                from = "FBPMModeStandby";
 
81
                        } else if (level == FBPMModeSuspend) {
 
82
                                from = "FBPMModeSuspend";
 
83
                        } else if (level == FBPMModeOff) {
 
84
                                from = "FBPMModeOff";
 
85
                        }
 
86
 
 
87
                        rfbLog("switching FBPM state from %s to FBPMModeOn\n", from);
 
88
                        
 
89
                        FBPMForceLevel(dpy, FBPMModeOn);
 
90
                
 
91
                        XSetErrorHandler(old_handler);
 
92
                        trapped_xerror = 0;
 
93
                }
 
94
        } else {
 
95
                if (db) fprintf(stderr, "FBPMInfo failed.\n");
 
96
        }
 
97
#else
 
98
        RAWFB_RET_VOID
 
99
        if (! init_fbpm) {
 
100
                if (! raw_fb_str) {
 
101
                        rfbLog("X FBPM extension not supported.\n");
 
102
                }
 
103
                init_fbpm = 1;
 
104
        }
 
105
#endif
 
106
}
 
107
 
 
108
void set_dpms_mode(char *mode) {
 
109
#if NO_X11
 
110
        return;
 
111
#else
 
112
        RAWFB_RET_VOID
 
113
#if LIBVNCSERVER_HAVE_DPMS
 
114
        if (dpy && DPMSCapable(dpy)) {
 
115
                CARD16 level;
 
116
                CARD16 want;
 
117
                BOOL enabled;
 
118
                if (!strcmp(mode, "off")) {
 
119
                        want = DPMSModeOff;
 
120
                } else if (!strcmp(mode, "on")) {
 
121
                        want = DPMSModeOn;
 
122
                } else if (!strcmp(mode, "standby")) {
 
123
                        want = DPMSModeStandby;
 
124
                } else if (!strcmp(mode, "suspend")) {
 
125
                        want = DPMSModeSuspend;
 
126
                } else if (!strcmp(mode, "enable")) {
 
127
                        DPMSEnable(dpy);
 
128
                        return;
 
129
                } else if (!strcmp(mode, "disable")) {
 
130
                        DPMSDisable(dpy);
 
131
                        return;
 
132
                } else {
 
133
                        return;
 
134
                }
 
135
                if (DPMSInfo(dpy, &level, &enabled)) {
 
136
                        char *from = "unk";
 
137
                        if (enabled && level != want) {
 
138
                                XErrorHandler old_handler = XSetErrorHandler(trap_xerror);
 
139
                                trapped_xerror = 0;
 
140
 
 
141
                                rfbLog("DPMSInfo level: %d enabled: %d\n", level, enabled);
 
142
                                if (level == DPMSModeStandby) {
 
143
                                        from = "DPMSModeStandby";
 
144
                                } else if (level == DPMSModeSuspend) {
 
145
                                        from = "DPMSModeSuspend";
 
146
                                } else if (level == DPMSModeOff) {
 
147
                                        from = "DPMSModeOff";
 
148
                                } else if (level == DPMSModeOn) {
 
149
                                        from = "DPMSModeOn";
 
150
                                }
 
151
 
 
152
                                rfbLog("switching DPMS state from %s to %s\n", from, mode);
 
153
                                
 
154
                                DPMSForceLevel(dpy, want);
 
155
                        
 
156
                                XSetErrorHandler(old_handler);
 
157
                                trapped_xerror = 0;
 
158
                        }
 
159
                }
 
160
        }
 
161
#endif
 
162
#endif
 
163
}
 
164
 
 
165
static void check_dpms(void) {
 
166
        static int init_dpms = 0;
 
167
#if LIBVNCSERVER_HAVE_DPMS
 
168
        static int dpms_capable = 0;
 
169
        static time_t last_dpms = 0;
 
170
        int db = 0;
 
171
 
 
172
        CARD16 level;
 
173
        BOOL enabled;
 
174
 
 
175
        RAWFB_RET_VOID
 
176
 
 
177
        if (! init_dpms) {
 
178
                if (getenv("DPMS_DEBUG")) {
 
179
                        db = atoi(getenv("DPMS_DEBUG"));
 
180
                }
 
181
                if (DPMSCapable(dpy)) {
 
182
                        dpms_capable = 1;
 
183
                        rfbLog("X display is capable of DPMS.\n");
 
184
                        if (watch_dpms) {
 
185
                                rfbLog("Preventing low-power DPMS modes when"
 
186
                                    " clients are connected.\n");
 
187
                        }
 
188
                } else {
 
189
                        if (! raw_fb_str) {
 
190
                                rfbLog("X display is not capable of DPMS.\n");
 
191
                        }
 
192
                        dpms_capable = 0;
 
193
                }
 
194
                init_dpms = 1;
 
195
        }
 
196
 
 
197
        if (force_dpms || (client_dpms && client_count)) {
 
198
                static int last_enable = 0;
 
199
                if (time(NULL) > last_enable) {
 
200
                        set_dpms_mode("enable");
 
201
                        last_enable = time(NULL);
 
202
                }
 
203
                set_dpms_mode("off");
 
204
        }
 
205
        if (! watch_dpms) {
 
206
                return;
 
207
        }
 
208
        if (! dpms_capable) {
 
209
                return;
 
210
        }
 
211
        if (! client_count) {
 
212
                return;
 
213
        }
 
214
        if (time(NULL) < last_dpms + 5) {
 
215
                return;
 
216
        }
 
217
        last_dpms = time(NULL);
 
218
 
 
219
        if (DPMSInfo(dpy, &level, &enabled)) {
 
220
                if (db) fprintf(stderr, "DPMSInfo level: %d enabled: %d\n", level, enabled);
 
221
 
 
222
                if (enabled && level != DPMSModeOn) {
 
223
                        char *from = "unknown-dpms-state";
 
224
                        XErrorHandler old_handler = XSetErrorHandler(trap_xerror);
 
225
                        trapped_xerror = 0;
 
226
 
 
227
                        if (level == DPMSModeStandby) {
 
228
                                from = "DPMSModeStandby";
 
229
                        } else if (level == DPMSModeSuspend) {
 
230
                                from = "DPMSModeSuspend";
 
231
                        } else if (level == DPMSModeOff) {
 
232
                                from = "DPMSModeOff";
 
233
                        }
 
234
 
 
235
                        rfbLog("switching DPMS state from %s to DPMSModeOn\n", from);
 
236
                        
 
237
                        DPMSForceLevel(dpy, DPMSModeOn);
 
238
                
 
239
                        XSetErrorHandler(old_handler);
 
240
                        trapped_xerror = 0;
 
241
                }
 
242
        } else {
 
243
                if (db) fprintf(stderr, "DPMSInfo failed.\n");
 
244
        }
 
245
#else
 
246
        RAWFB_RET_VOID
 
247
        if (! init_dpms) {
 
248
                if (! raw_fb_str) {
 
249
                        rfbLog("X DPMS extension not supported.\n");
 
250
                }
 
251
                init_dpms = 1;
 
252
        }
 
253
#endif
 
254
}
 
255