http://packetstormsecurity.org/UNIX/admin/xscreensaver-3.28.tar.gz
[xscreensaver] / driver / dpms.c
1 /* dpms.c --- syncing the X Display Power Management values
2  * xscreensaver, Copyright (c) 2001 Jamie Zawinski <jwz@jwz.org>
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation.  No representations are made about the suitability of this
9  * software for any purpose.  It is provided "as is" without express or 
10  * implied warranty.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #include <stdio.h>
18 #include <X11/Xlib.h>
19
20 #ifdef HAVE_DPMS_EXTENSION
21
22 # include <X11/Xproto.h>
23 # include <X11/extensions/dpms.h>
24 # include <X11/extensions/dpmsstr.h>
25
26   /* Why this crap is not in a header file somewhere, I have no idea.  Losers!
27    */
28   extern Bool DPMSQueryExtension (Display *dpy, int *event_ret, int *err_ret);
29   extern Bool DPMSCapable (Display *dpy);
30   extern Status DPMSInfo (Display *dpy, CARD16 *power_level, BOOL *state);
31   extern Status DPMSSetTimeouts (Display *dpy,
32                                  CARD16 standby, CARD16 suspend, CARD16 off);
33   extern Bool DPMSGetTimeouts (Display *dpy,
34                                CARD16 *standby, CARD16 *suspend, CARD16 *off);
35   extern Status DPMSEnable (Display *dpy);
36   extern Status DPMSDisable (Display *dpy);
37
38 #endif /* HAVE_DPMS_EXTENSION */
39
40
41 /* This file doesn't need the Xt headers, so stub these types out... */
42 #undef XtPointer
43 #define XtAppContext void*
44 #define XrmDatabase  void*
45 #define XtIntervalId void*
46 #define XtPointer    void*
47 #define Widget       void*
48
49 #include "xscreensaver.h"
50
51 void
52 sync_server_dpms_settings (Display *dpy, Bool enabled_p,
53                            int standby_secs, int suspend_secs, int off_secs,
54                            Bool verbose_p)
55 {
56 # ifdef HAVE_DPMS_EXTENSION
57
58   int event = 0, error = 0;
59   BOOL o_enabled = False;
60   CARD16 o_power = 0;
61   CARD16 o_standby = 0, o_suspend = 0, o_off = 0;
62
63   Bool bogus_p = (standby_secs == 0 || suspend_secs == 0 || off_secs == 0);
64
65   if (bogus_p) enabled_p = False;
66
67   if (! DPMSQueryExtension (dpy, &event, &error))
68     {
69       if (verbose_p)
70         fprintf (stderr, "%s: XDPMS extension not supported.\n", blurb());
71       return;
72     }
73
74   if (! DPMSCapable (dpy))
75     {
76       if (verbose_p)
77         fprintf (stderr, "%s: DPMS not supported.\n", blurb());
78       return;
79     }
80
81   if (! DPMSInfo (dpy, &o_power, &o_enabled))
82     {
83       if (verbose_p)
84         fprintf (stderr, "%s: unable to get DPMS state.\n", blurb());
85       return;
86     }
87
88   if (o_enabled != enabled_p)
89     {
90       if (! (enabled_p ? DPMSEnable (dpy) : DPMSDisable (dpy)))
91         {
92           if (verbose_p)
93             fprintf (stderr, "%s: unable to set DPMS state.\n", blurb());
94           return;
95         }
96       else if (verbose_p)
97         fprintf (stderr, "%s: turned DPMS %s.\n", blurb(),
98                  enabled_p ? "on" : "off");
99     }
100
101   if (bogus_p)
102     {
103       if (verbose_p)
104         fprintf (stderr, "%s: not setting bogus DPMS timeouts: %d %d %d.\n",
105                  blurb(), standby_secs, suspend_secs, off_secs);
106       return;
107     }
108
109   if (!DPMSGetTimeouts (dpy, &o_standby, &o_suspend, &o_off))
110     {
111       if (verbose_p)
112         fprintf (stderr, "%s: unable to get DPMS timeouts.\n", blurb());
113       return;
114     }
115
116   if (o_standby != standby_secs ||
117       o_suspend != suspend_secs ||
118       o_off != off_secs)
119     {
120       if (!DPMSSetTimeouts (dpy, standby_secs, suspend_secs, off_secs))
121         {
122           if (verbose_p)
123             fprintf (stderr, "%s: unable to set DPMS timeouts.\n", blurb());
124           return;
125         }
126       else if (verbose_p)
127         fprintf (stderr, "%s: set DPMS timeouts: %d %d %d.\n", blurb(),
128                  standby_secs, suspend_secs, off_secs);
129     }
130
131 # else  /* !HAVE_DPMS_EXTENSION */
132
133   if (verbose_p)
134     fprintf (stderr, "%s: DPMS support not compiled in.\n", blurb());
135
136 # endif /* HAVE_DPMS_EXTENSION */
137 }