ftp://ftp.linux.ncsu.edu/mirror/ftp.redhat.com/pub/redhat/linux/enterprise/4/en/os...
[xscreensaver] / driver / test-randr.c
1 /* test-randr.c --- playing with the Resize And Rotate extension.
2  * xscreensaver, Copyright (c) 2004 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/Xrandr.h>
32
33 char *progname = 0;
34 char *progclass = "XScreenSaver";
35
36 static const char *
37 blurb (void)
38 {
39   static char buf[255];
40   time_t now = time ((time_t *) 0);
41   char *ct = (char *) ctime (&now);
42   int n = strlen(progname);
43   if (n > 100) n = 99;
44   strncpy(buf, progname, n);
45   buf[n++] = ':';
46   buf[n++] = ' ';
47   strncpy(buf+n, ct+11, 8);
48   strcpy(buf+n+9, ": ");
49   return buf;
50 }
51
52
53 static Bool error_handler_hit_p = False;
54
55 static int
56 ignore_all_errors_ehandler (Display *dpy, XErrorEvent *error)
57 {
58   error_handler_hit_p = True;
59   return 0;
60 }
61
62
63 int
64 main (int argc, char **argv)
65 {
66   int event_number = -1, error_number = -1;
67   int major = -1, minor = -1;
68   int nscreens = 0;
69   int i;
70
71   XtAppContext app;
72   Widget toplevel_shell = XtAppInitialize (&app, progclass, 0, 0,
73                                            &argc, argv, 0, 0, 0);
74   Display *dpy = XtDisplay (toplevel_shell);
75   XtGetApplicationNameAndClass (dpy, &progname, &progclass);
76
77   nscreens = ScreenCount(dpy);
78
79   if (!XRRQueryExtension(dpy, &event_number, &error_number))
80     {
81       fprintf(stderr, "%s: XRRQueryExtension(dpy, ...) ==> False\n",
82               blurb());
83       fprintf(stderr, "%s: server does not support the RANDR extension.\n",
84               blurb());
85       major = -1;
86     }
87   else
88     {
89       fprintf(stderr, "%s: XRRQueryExtension(dpy, ...) ==> %d, %d\n",
90               blurb(), event_number, error_number);
91
92       if (!XRRQueryVersion(dpy, &major, &minor))
93         {
94           fprintf(stderr, "%s: XRRQueryVersion(dpy, ...) ==> False\n",
95                   blurb());
96           fprintf(stderr, "%s: server didn't report RANDR version numbers?\n",
97                   blurb());
98         }
99       else
100         fprintf(stderr, "%s: XRRQueryVersion(dpy, ...) ==> %d, %d\n", blurb(),
101                 major, minor);
102     }
103
104   for (i = 0; i < nscreens; i++)
105     {
106       XRRScreenConfiguration *rrc;
107       XErrorHandler old_handler;
108
109       XSync (dpy, False);
110       error_handler_hit_p = False;
111       old_handler = XSetErrorHandler (ignore_all_errors_ehandler);
112
113       rrc = (major >= 0 ? XRRGetScreenInfo (dpy, RootWindow (dpy, i)) : 0);
114
115       XSync (dpy, False);
116       XSetErrorHandler (old_handler);
117       XSync (dpy, False);
118
119       if (error_handler_hit_p)
120         {
121           fprintf(stderr, "%s:   XRRGetScreenInfo(dpy, %d) ==> X error:\n",
122                   blurb(), i);
123           /* do it again without the error handler to print the error */
124           rrc = XRRGetScreenInfo (dpy, RootWindow (dpy, i));
125         }
126       else if (rrc)
127         {
128           SizeID current_size = -1;
129           Rotation current_rotation = ~0;
130
131           fprintf (stderr, "\n%s: Screen %d\n", blurb(), i);
132
133           current_size =
134             XRRConfigCurrentConfiguration (rrc, &current_rotation);
135
136           /* Times */
137           {
138             Time server_time, config_time;
139             server_time = XRRConfigTimes (rrc, &config_time);
140             if (config_time == 0 || server_time == 0)
141               fprintf (stderr, "%s:   config has never been changed\n",
142                        blurb());
143             else
144               fprintf (stderr, "%s:   config changed %lu seconds ago\n",
145                        blurb(), (unsigned long) (server_time - config_time));
146           }
147
148           /* Rotations */
149           {
150             Rotation available, current;
151             available = XRRConfigRotations (rrc, &current);
152
153             fprintf (stderr, "%s:   Available Rotations:\t", blurb());
154             if (available & RR_Rotate_0)   fprintf (stderr,   " 0");
155             if (available & RR_Rotate_90)  fprintf (stderr,  " 90");
156             if (available & RR_Rotate_180) fprintf (stderr, " 180");
157             if (available & RR_Rotate_270) fprintf (stderr, " 270");
158             if (! (available & (RR_Rotate_0   | RR_Rotate_90 |
159                                 RR_Rotate_180 | RR_Rotate_270)))
160               fprintf (stderr, " none");
161             fprintf (stderr, "\n");
162
163             if (current_rotation != current)
164               fprintf (stderr,
165                        "%s:   WARNING: rotation inconsistency: 0x%X vs 0x%X\n",
166                        blurb(), current_rotation, current);
167
168             fprintf (stderr, "%s:   Current Rotation:\t", blurb());
169             if (current & RR_Rotate_0)   fprintf (stderr,   " 0");
170             if (current & RR_Rotate_90)  fprintf (stderr,  " 90");
171             if (current & RR_Rotate_180) fprintf (stderr, " 180");
172             if (current & RR_Rotate_270) fprintf (stderr, " 270");
173             if (! (current & (RR_Rotate_0   | RR_Rotate_90 |
174                               RR_Rotate_180 | RR_Rotate_270)))
175               fprintf (stderr, " none");
176             fprintf (stderr, "\n");
177
178             fprintf (stderr, "%s:   Available Reflections:\t", blurb());
179             if (available & RR_Reflect_X) fprintf (stderr,   " X");
180             if (available & RR_Reflect_Y) fprintf (stderr,   " Y");
181             if (! (available & (RR_Reflect_X | RR_Reflect_Y)))
182               fprintf (stderr, " none");
183             fprintf (stderr, "\n");
184
185             fprintf (stderr, "%s:   Current Reflections:\t", blurb());
186             if (current & RR_Reflect_X) fprintf (stderr,   " X");
187             if (current & RR_Reflect_Y) fprintf (stderr,   " Y");
188             if (! (current & (RR_Reflect_X | RR_Reflect_Y)))
189               fprintf (stderr, " none");
190             fprintf (stderr, "\n");
191           }
192
193           /* Sizes */
194           {
195             int nsizes, j;
196             XRRScreenSize *rrsizes;
197
198             rrsizes = XRRConfigSizes (rrc, &nsizes);
199             if (nsizes <= 0)
200                 fprintf (stderr, "%s:   sizes:\t none\n", blurb());
201             else
202               for (j = 0; j < nsizes; j++)
203                 {
204                   short *rates;
205                   int nrates, k;
206                   fprintf (stderr,
207                            "%s:   %c size %d: %d x %d\t rates:",
208                            blurb(), 
209                            (j == current_size ? '+' : ' '),
210                            j,
211                            rrsizes[j].width, rrsizes[j].height);
212
213                   rates = XRRConfigRates (rrc, j, &nrates);
214                   if (nrates == 0)
215                     fprintf (stderr, " none?");
216                   else
217                     for (k = 0; k < nrates; k++)
218                       fprintf (stderr, " %d", rates[k]);
219                   fprintf (stderr, "\n");
220                   /* don't free 'rates' */
221                 }
222             /* don't free 'rrsizes' */
223           }
224
225           XRRFreeScreenConfigInfo (rrc);
226         }
227       else if (major >= 0)
228         {
229           fprintf(stderr, "%s:   XRRGetScreenInfo(dpy, %d) ==> NULL\n",
230                   blurb(), i);
231         }
232     }
233
234   if (major > 0)
235     {
236       Window w[20];
237       XWindowAttributes xgwa[20];
238
239       for (i = 0; i < nscreens; i++)
240         {
241           XRRSelectInput (dpy, RootWindow (dpy, i), RRScreenChangeNotifyMask);
242           w[i] = RootWindow (dpy, i);
243           XGetWindowAttributes (dpy, w[i], &xgwa[i]);
244         }
245
246       XSync (dpy, False);
247       fprintf (stderr, "\n%s: awaiting events...\n", progname);
248       while (1)
249         {
250           XEvent event;
251           XNextEvent (dpy, &event);
252
253           if (event.type == event_number + RRScreenChangeNotify)
254             {
255               XRRScreenChangeNotifyEvent *xrr_event =
256                 (XRRScreenChangeNotifyEvent *) &event;
257               int screen = XRRRootToScreen (dpy, xrr_event->window);
258
259               fprintf (stderr, "%s: screen %d: RRScreenChangeNotify event\n",
260                        progname, screen);
261
262               fprintf (stderr, "%s: screen %d: old size: \t%d x %d\n",
263                        progname, screen,
264                        DisplayWidth (dpy, screen),
265                        DisplayHeight (dpy, screen));
266               fprintf (stderr, "%s: screen %d: old root 0x%lx:\t%d x %d\n",
267                        progname, screen, (unsigned long) w[screen],
268                        xgwa[screen].width, xgwa[screen].height);
269
270               XRRUpdateConfiguration (&event);
271               XSync (dpy, False);
272
273               fprintf (stderr, "%s: screen %d: new size: \t%d x %d\n",
274                        progname, screen,
275                        DisplayWidth (dpy, screen),
276                        DisplayHeight (dpy, screen));
277
278               w[screen] = RootWindow (dpy, screen);
279               XGetWindowAttributes (dpy, w[screen], &xgwa[screen]);
280               fprintf (stderr, "%s: screen %d: new root 0x%lx:\t%d x %d\n",
281                        progname, screen, (unsigned long) w[screen],
282                        xgwa[screen].width, xgwa[screen].height);
283               fprintf (stderr, "\n");
284             }
285           else
286             {
287               fprintf (stderr, "%s: event %d\n", progname, event.type);
288             }
289         }
290     }
291
292   XSync (dpy, False);
293   exit (0);
294 }