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