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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2005 William Jon McCann <mccann@jhu.edu>
* Copyright (C) 2006-2009 Richard Hughes <richard@hughsie.com>
*
* Licensed under the GNU General Public License Version 2
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <X11/Xproto.h>
#include <X11/extensions/dpms.h>
#include "egg-debug.h"
#include "gpm-dpms.h"
static void gpm_dpms_finalize (GObject *object);
#define GPM_DPMS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GPM_TYPE_DPMS, GpmDpmsPrivate))
/* until we get a nice event-emitting DPMS extension, we have to poll... */
#define GPM_DPMS_POLL_TIME 10
struct GpmDpmsPrivate
{
gboolean dpms_capable;
GpmDpmsMode mode;
guint timer_id;
Display *display;
};
enum {
MODE_CHANGED,
LAST_SIGNAL
};
static guint signals [LAST_SIGNAL] = { 0 };
static gpointer gpm_dpms_object = NULL;
G_DEFINE_TYPE (GpmDpms, gpm_dpms, G_TYPE_OBJECT)
/**
* gpm_dpms_error_quark:
**/
GQuark
gpm_dpms_error_quark (void)
{
static GQuark quark = 0;
if (!quark)
quark = g_quark_from_static_string ("gpm_dpms_error");
return quark;
}
/**
* gpm_dpms_x11_get_mode:
**/
static gboolean
gpm_dpms_x11_get_mode (GpmDpms *dpms, GpmDpmsMode *mode, GError **error)
{
GpmDpmsMode result;
BOOL enabled = FALSE;
CARD16 state;
if (dpms->priv->dpms_capable == FALSE) {
/* Server or monitor can't DPMS -- assume the monitor is on. */
result = GPM_DPMS_MODE_ON;
goto out;
}
DPMSInfo (dpms->priv->display, &state, &enabled);
if (!enabled) {
/* Server says DPMS is disabled -- so the monitor is on. */
result = GPM_DPMS_MODE_ON;
goto out;
}
switch (state) {
case DPMSModeOn:
result = GPM_DPMS_MODE_ON;
break;
case DPMSModeStandby:
result = GPM_DPMS_MODE_STANDBY;
break;
case DPMSModeSuspend:
result = GPM_DPMS_MODE_SUSPEND;
break;
case DPMSModeOff:
result = GPM_DPMS_MODE_OFF;
break;
default:
result = GPM_DPMS_MODE_ON;
break;
}
out:
if (mode)
*mode = result;
return TRUE;
}
/**
* gpm_dpms_x11_set_mode:
**/
static gboolean
gpm_dpms_x11_set_mode (GpmDpms *dpms, GpmDpmsMode mode, GError **error)
{
GpmDpmsMode current_mode;
CARD16 state;
CARD16 current_state;
BOOL current_enabled;
if (!dpms->priv->dpms_capable) {
egg_debug ("not DPMS capable");
g_set_error (error, GPM_DPMS_ERROR, GPM_DPMS_ERROR_GENERAL,
"Display is not DPMS capable");
return FALSE;
}
if (!DPMSInfo (dpms->priv->display, ¤t_state, ¤t_enabled)) {
egg_debug ("couldn't get DPMS info");
g_set_error (error, GPM_DPMS_ERROR, GPM_DPMS_ERROR_GENERAL,
"Unable to get DPMS state");
return FALSE;
}
if (!current_enabled) {
egg_debug ("DPMS not enabled");
g_set_error (error, GPM_DPMS_ERROR, GPM_DPMS_ERROR_GENERAL,
"DPMS is not enabled");
return FALSE;
}
switch (mode) {
case GPM_DPMS_MODE_ON:
state = DPMSModeOn;
break;
case GPM_DPMS_MODE_STANDBY:
state = DPMSModeStandby;
break;
case GPM_DPMS_MODE_SUSPEND:
state = DPMSModeSuspend;
break;
case GPM_DPMS_MODE_OFF:
state = DPMSModeOff;
break;
default:
state = DPMSModeOn;
break;
}
gpm_dpms_x11_get_mode (dpms, ¤t_mode, NULL);
if (current_mode != mode) {
if (! DPMSForceLevel (dpms->priv->display, state)) {
g_set_error (error, GPM_DPMS_ERROR, GPM_DPMS_ERROR_GENERAL,
"Could not change DPMS mode");
return FALSE;
}
XSync (dpms->priv->display, FALSE);
}
return TRUE;
}
/**
* gpm_dpms_mode_from_string:
**/
GpmDpmsMode
gpm_dpms_mode_from_string (const gchar *str)
{
if (str == NULL)
return GPM_DPMS_MODE_UNKNOWN;
if (strcmp (str, "on") == 0)
return GPM_DPMS_MODE_ON;
if (strcmp (str, "standby") == 0)
return GPM_DPMS_MODE_STANDBY;
if (strcmp (str, "suspend") == 0)
return GPM_DPMS_MODE_SUSPEND;
if (strcmp (str, "off") == 0)
return GPM_DPMS_MODE_OFF;
return GPM_DPMS_MODE_UNKNOWN;
}
/**
* gpm_dpms_mode_to_string:
**/
const gchar *
gpm_dpms_mode_to_string (GpmDpmsMode mode)
{
const gchar *str = NULL;
switch (mode) {
case GPM_DPMS_MODE_ON:
str = "on";
break;
case GPM_DPMS_MODE_STANDBY:
str = "standby";
break;
case GPM_DPMS_MODE_SUSPEND:
str = "suspend";
break;
case GPM_DPMS_MODE_OFF:
str = "off";
break;
default:
str = NULL;
break;
}
return str;
}
/**
* gpm_dpms_set_mode:
**/
gboolean
gpm_dpms_set_mode (GpmDpms *dpms, GpmDpmsMode mode, GError **error)
{
gboolean ret;
g_return_val_if_fail (GPM_IS_DPMS (dpms), FALSE);
if (mode == GPM_DPMS_MODE_UNKNOWN) {
egg_debug ("mode unknown");
g_set_error (error, GPM_DPMS_ERROR, GPM_DPMS_ERROR_GENERAL,
"Unknown DPMS mode");
return FALSE;
}
ret = gpm_dpms_x11_set_mode (dpms, mode, error);
return ret;
}
/**
* gpm_dpms_get_mode:
**/
gboolean
gpm_dpms_get_mode (GpmDpms *dpms, GpmDpmsMode *mode, GError **error)
{
gboolean ret;
if (mode)
*mode = GPM_DPMS_MODE_UNKNOWN;
ret = gpm_dpms_x11_get_mode (dpms, mode, error);
return ret;
}
/**
* gpm_dpms_poll_mode_cb:
**/
static gboolean
gpm_dpms_poll_mode_cb (GpmDpms *dpms)
{
gboolean ret;
GpmDpmsMode mode;
GError *error = NULL;
/* Try again */
ret = gpm_dpms_x11_get_mode (dpms, &mode, &error);
if (!ret) {
g_clear_error (&error);
return TRUE;
}
if (mode != dpms->priv->mode) {
dpms->priv->mode = mode;
g_signal_emit (dpms, signals [MODE_CHANGED], 0, mode);
}
return TRUE;
}
/**
* gpm_dpms_clear_timeouts:
**/
static gboolean
gpm_dpms_clear_timeouts (GpmDpms *dpms)
{
gboolean ret = FALSE;
/* never going to work */
if (!dpms->priv->dpms_capable) {
egg_debug ("not DPMS capable");
goto out;
}
egg_debug ("set timeouts to zero");
ret = DPMSSetTimeouts (dpms->priv->display, 0, 0, 0);
out:
return ret;
}
/**
* gpm_dpms_class_init:
**/
static void
gpm_dpms_class_init (GpmDpmsClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gpm_dpms_finalize;
signals [MODE_CHANGED] =
g_signal_new ("mode-changed",
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GpmDpmsClass, mode_changed),
NULL, NULL, g_cclosure_marshal_VOID__UINT,
G_TYPE_NONE, 1, G_TYPE_UINT);
g_type_class_add_private (klass, sizeof (GpmDpmsPrivate));
}
/**
* gpm_dpms_init:
**/
static void
gpm_dpms_init (GpmDpms *dpms)
{
dpms->priv = GPM_DPMS_GET_PRIVATE (dpms);
/* DPMSCapable() can never change for a given display */
dpms->priv->display = GDK_DISPLAY_XDISPLAY (gdk_display_get_default());
dpms->priv->dpms_capable = DPMSCapable (dpms->priv->display);
dpms->priv->timer_id = g_timeout_add_seconds (GPM_DPMS_POLL_TIME, (GSourceFunc)gpm_dpms_poll_mode_cb, dpms);
/* ensure we clear the default timeouts (Standby: 1200s, Suspend: 1800s, Off: 2400s) */
gpm_dpms_clear_timeouts (dpms);
}
/**
* gpm_dpms_finalize:
**/
static void
gpm_dpms_finalize (GObject *object)
{
GpmDpms *dpms;
g_return_if_fail (object != NULL);
g_return_if_fail (GPM_IS_DPMS (object));
dpms = GPM_DPMS (object);
g_return_if_fail (dpms->priv != NULL);
if (dpms->priv->timer_id != 0)
g_source_remove (dpms->priv->timer_id);
G_OBJECT_CLASS (gpm_dpms_parent_class)->finalize (object);
}
/**
* gpm_dpms_new:
**/
GpmDpms *
gpm_dpms_new (void)
{
if (gpm_dpms_object != NULL) {
g_object_ref (gpm_dpms_object);
} else {
gpm_dpms_object = g_object_new (GPM_TYPE_DPMS, NULL);
g_object_add_weak_pointer (gpm_dpms_object, &gpm_dpms_object);
}
return GPM_DPMS (gpm_dpms_object);
}
/***************************************************************************
*** MAKE CHECK TESTS ***
***************************************************************************/
#ifdef EGG_TEST
#include "egg-test.h"
void
gpm_dpms_test (gpointer data)
{
GpmDpms *dpms;
gboolean ret;
GError *error = NULL;
EggTest *test = (EggTest *) data;
if (!egg_test_start (test, "GpmDpms"))
return;
/************************************************************/
egg_test_title (test, "get object");
dpms = gpm_dpms_new ();
if (dpms != NULL)
egg_test_success (test, NULL);
else
egg_test_failed (test, "got no object");
/************************************************************/
egg_test_title (test, "set on");
ret = gpm_dpms_set_mode (dpms, GPM_DPMS_MODE_ON, &error);
if (ret)
egg_test_success (test, NULL);
else
egg_test_failed (test, "failed: %s", error->message);
g_usleep (2*1000*1000);
/************************************************************/
egg_test_title (test, "set STANDBY");
ret = gpm_dpms_set_mode (dpms, GPM_DPMS_MODE_STANDBY, &error);
if (ret)
egg_test_success (test, NULL);
else
egg_test_failed (test, "failed: %s", error->message);
g_usleep (2*1000*1000);
/************************************************************/
egg_test_title (test, "set SUSPEND");
ret = gpm_dpms_set_mode (dpms, GPM_DPMS_MODE_SUSPEND, &error);
if (ret)
egg_test_success (test, NULL);
else
egg_test_failed (test, "failed: %s", error->message);
g_usleep (2*1000*1000);
/************************************************************/
egg_test_title (test, "set OFF");
ret = gpm_dpms_set_mode (dpms, GPM_DPMS_MODE_OFF, &error);
if (ret)
egg_test_success (test, NULL);
else
egg_test_failed (test, "failed: %s", error->message);
g_usleep (2*1000*1000);
/************************************************************/
egg_test_title (test, "set on");
ret = gpm_dpms_set_mode (dpms, GPM_DPMS_MODE_ON, &error);
if (ret)
egg_test_success (test, NULL);
else
egg_test_failed (test, "failed: %s", error->message);
g_usleep (2*1000*1000);
g_object_unref (dpms);
egg_test_end (test);
}
#endif
|