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