http://packetstormsecurity.org/UNIX/admin/xscreensaver-3.31.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   Bool bogus_p = False;
63
64   if (standby_secs == 0 && suspend_secs == 0 && off_secs == 0)
65     /* all zero implies "DPMS disabled" */
66     enabled_p = False;
67
68   else if ((standby_secs != 0 && standby_secs < 10) ||
69            (suspend_secs != 0 && suspend_secs < 10) ||
70            (off_secs     != 0 && off_secs     < 10))
71     /* any negative, or any positive-and-less-than-10-seconds, is crazy. */
72     bogus_p = True;
73
74   if (bogus_p) enabled_p = False;
75
76   if (! DPMSQueryExtension (dpy, &event, &error))
77     {
78       if (verbose_p)
79         fprintf (stderr, "%s: XDPMS extension not supported.\n", blurb());
80       return;
81     }
82
83   if (! DPMSCapable (dpy))
84     {
85       if (verbose_p)
86         fprintf (stderr, "%s: DPMS not supported.\n", blurb());
87       return;
88     }
89
90   if (! DPMSInfo (dpy, &o_power, &o_enabled))
91     {
92       if (verbose_p)
93         fprintf (stderr, "%s: unable to get DPMS state.\n", blurb());
94       return;
95     }
96
97   if (o_enabled != enabled_p)
98     {
99       if (! (enabled_p ? DPMSEnable (dpy) : DPMSDisable (dpy)))
100         {
101           if (verbose_p)
102             fprintf (stderr, "%s: unable to set DPMS state.\n", blurb());
103           return;
104         }
105       else if (verbose_p)
106         fprintf (stderr, "%s: turned DPMS %s.\n", blurb(),
107                  enabled_p ? "on" : "off");
108     }
109
110   if (bogus_p)
111     {
112       if (verbose_p)
113         fprintf (stderr, "%s: not setting bogus DPMS timeouts: %d %d %d.\n",
114                  blurb(), standby_secs, suspend_secs, off_secs);
115       return;
116     }
117
118   if (!DPMSGetTimeouts (dpy, &o_standby, &o_suspend, &o_off))
119     {
120       if (verbose_p)
121         fprintf (stderr, "%s: unable to get DPMS timeouts.\n", blurb());
122       return;
123     }
124
125   if (o_standby != standby_secs ||
126       o_suspend != suspend_secs ||
127       o_off != off_secs)
128     {
129       if (!DPMSSetTimeouts (dpy, standby_secs, suspend_secs, off_secs))
130         {
131           if (verbose_p)
132             fprintf (stderr, "%s: unable to set DPMS timeouts.\n", blurb());
133           return;
134         }
135       else if (verbose_p)
136         fprintf (stderr, "%s: set DPMS timeouts: %d %d %d.\n", blurb(),
137                  standby_secs, suspend_secs, off_secs);
138     }
139
140 # else  /* !HAVE_DPMS_EXTENSION */
141
142   if (verbose_p)
143     fprintf (stderr, "%s: DPMS support not compiled in.\n", blurb());
144
145 # endif /* HAVE_DPMS_EXTENSION */
146 }