http://ftp.x.org/contrib/applications/xscreensaver-2.34.tar.gz
[xscreensaver] / driver / test-xdpms.c
1 /* test-xdpms.c --- playing with the XDPMS extension.
2  * xscreensaver, Copyright (c) 1998 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 <stdlib.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <sys/time.h>
21
22 #include <X11/Xlib.h>
23 #include <X11/Xatom.h>
24 #include <X11/Intrinsic.h>
25
26 #include <X11/Xproto.h>
27 #include <X11/extensions/dpms.h>
28 #include <X11/extensions/dpmsstr.h>
29
30 extern Bool DPMSQueryExtension (Display *dpy, int *event_ret, int *error_ret);
31 extern Bool DPMSCapable (Display *dpy);
32 extern Status DPMSForceLevel (Display *dpy, CARD16 level);
33 extern Status DPMSInfo (Display *dpy, CARD16 *power_level, BOOL *state);
34
35 extern Status DPMSGetVersion (Display *dpy, int *major_ret, int *minor_ret);
36 extern Status DPMSSetTimeouts (Display *dpy,
37                                CARD16 standby, CARD16 suspend, CARD16 off);
38 extern Bool DPMSGetTimeouts (Display *dpy,
39                              CARD16 *standby, CARD16 *suspend, CARD16 *off);
40 extern Status DPMSEnable (Display *dpy);
41 extern Status DPMSDisable (Display *dpy);
42
43
44 char *progname = 0;
45 char *progclass = "XScreenSaver";
46
47 static const char *
48 blurb (void)
49 {
50   static char buf[255];
51   time_t now = time ((time_t *) 0);
52   char *ct = (char *) ctime (&now);
53   int n = strlen(progname);
54   if (n > 100) n = 99;
55   strncpy(buf, progname, n);
56   buf[n++] = ':';
57   buf[n++] = ' ';
58   strncpy(buf+n, ct+11, 8);
59   strcpy(buf+n+9, ": ");
60   return buf;
61 }
62
63
64 int
65 main (int argc, char **argv)
66 {
67   int delay = 10;
68
69   int event_number, error_number;
70   int major, minor;
71   CARD16 standby, suspend, off;
72   CARD16 state;
73   BOOL onoff;
74
75   XtAppContext app;
76   Widget toplevel_shell = XtAppInitialize (&app, progclass, 0, 0,
77                                            &argc, argv, 0, 0, 0);
78   Display *dpy = XtDisplay (toplevel_shell);
79   XtGetApplicationNameAndClass (dpy, &progname, &progclass);
80
81   if (!DPMSQueryExtension(dpy, &event_number, &error_number))
82     {
83       fprintf(stderr, "%s: DPMSQueryExtension(dpy, ...) ==> False\n",
84               blurb());
85       fprintf(stderr, "%s: server does not support the XDPMS extension.\n",
86               blurb());
87       exit(1);
88     }
89   else
90     fprintf(stderr, "%s: DPMSQueryExtension(dpy, ...) ==> %d, %d\n", blurb(),
91             event_number, error_number);
92
93   if (!DPMSCapable(dpy))
94     {
95       fprintf(stderr, "%s: DPMSCapable(dpy) ==> False\n", blurb());
96       fprintf(stderr, "%s: server says hardware doesn't support DPMS.\n",
97               blurb());
98       exit(1);
99     }
100   else
101     fprintf(stderr, "%s: DPMSCapable(dpy) ==> True\n", blurb());
102
103   if (!DPMSGetVersion(dpy, &major, &minor))
104     {
105       fprintf(stderr, "%s: DPMSGetVersion(dpy, ...) ==> False\n", blurb());
106       fprintf(stderr, "%s: server didn't report XDPMS version numbers?\n",
107               blurb());
108     }
109   else
110     fprintf(stderr, "%s: DPMSGetVersion(dpy, ...) ==> %d, %d\n", blurb(),
111             major, minor);
112
113   if (!DPMSGetTimeouts(dpy, &standby, &suspend, &off))
114     {
115       fprintf(stderr, "%s: DPMSGetTimeouts(dpy, ...) ==> False\n", blurb());
116       fprintf(stderr, "%s: server didn't report DPMS timeouts?\n", blurb());
117     }
118   else
119     fprintf(stderr,
120             "%s: DPMSGetTimeouts(dpy, ...)\n"
121             "\t ==> standby = %d, suspend = %d, off = %d\n",
122             blurb(), standby, suspend, off);
123
124   while (1)
125     {
126       if (!DPMSInfo(dpy, &state, &onoff))
127         {
128           fprintf(stderr, "%s: DPMSInfo(dpy, ...) ==> False\n", blurb());
129           fprintf(stderr, "%s: couldn't read DPMS state?\n", blurb());
130           onoff = 0;
131           state = -1;
132         }
133       else
134         {
135           fprintf(stderr, "%s: DPMSInfo(dpy, ...) ==> %s, %s\n", blurb(),
136                   (state == DPMSModeOn ? "DPMSModeOn" :
137                    state == DPMSModeStandby ? "DPMSModeStandby" :
138                    state == DPMSModeSuspend ? "DPMSModeSuspend" :
139                    state == DPMSModeOff ? "DPMSModeOff" : "???"),
140                   (onoff == 1 ? "On" : onoff == 0 ? "Off" : "???"));
141         }
142
143       if (state == DPMSModeStandby ||
144           state == DPMSModeSuspend ||
145           state == DPMSModeOff)
146         {
147           Status st;
148           fprintf(stderr, "%s: monitor is off; turning it on.\n", blurb());
149           st = DPMSForceLevel (dpy, DPMSModeOn);
150           fprintf (stderr, "%s: DPMSForceLevel (dpy, DPMSModeOn) ==> %s\n",
151                    blurb(), (st ? "Ok" : "Error"));
152         }
153
154       sleep (delay);
155     }
156 }