1 /* test-randr.c --- playing with the Resize And Rotate extension.
2 * xscreensaver, Copyright (c) 2004 Jamie Zawinski <jwz@jwz.org>
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
27 #include <X11/Xatom.h>
28 #include <X11/Intrinsic.h>
30 #include <X11/Xproto.h>
31 #include <X11/extensions/Xrandr.h>
34 char *progclass = "XScreenSaver";
40 time_t now = time ((time_t *) 0);
41 char *ct = (char *) ctime (&now);
42 int n = strlen(progname);
44 strncpy(buf, progname, n);
47 strncpy(buf+n, ct+11, 8);
48 strcpy(buf+n+9, ": ");
54 main (int argc, char **argv)
56 int event_number, error_number;
62 Widget toplevel_shell = XtAppInitialize (&app, progclass, 0, 0,
63 &argc, argv, 0, 0, 0);
64 Display *dpy = XtDisplay (toplevel_shell);
65 XtGetApplicationNameAndClass (dpy, &progname, &progclass);
67 nscreens = ScreenCount(dpy);
69 if (!XRRQueryExtension(dpy, &event_number, &error_number))
71 fprintf(stderr, "%s: XRRQueryExtension(dpy, ...) ==> False\n",
73 fprintf(stderr, "%s: server does not support the RANDR extension.\n",
79 fprintf(stderr, "%s: XRRQueryExtension(dpy, ...) ==> %d, %d\n",
80 blurb(), event_number, error_number);
82 if (!XRRQueryVersion(dpy, &major, &minor))
84 fprintf(stderr, "%s: XRRQueryVersion(dpy, ...) ==> False\n",
86 fprintf(stderr, "%s: server didn't report RANDR version numbers?\n",
90 fprintf(stderr, "%s: XRRQueryVersion(dpy, ...) ==> %d, %d\n", blurb(),
94 for (i = 0; i < nscreens; i++)
96 XRRScreenConfiguration *rrc =
97 (major >= 0 ? XRRGetScreenInfo (dpy, RootWindow (dpy, i)) : 0);
101 SizeID current_size = -1;
102 Rotation current_rotation = ~0;
104 fprintf (stderr, "\n%s: Screen %d\n", blurb(), i);
107 XRRConfigCurrentConfiguration (rrc, ¤t_rotation);
111 Time server_time, config_time;
112 server_time = XRRConfigTimes (rrc, &config_time);
113 if (config_time == 0 || server_time == 0)
114 fprintf (stderr, "%s: config has never been changed\n",
117 fprintf (stderr, "%s: config changed %lu seconds ago\n",
118 blurb(), (unsigned long) (server_time - config_time));
123 Rotation available, current;
124 available = XRRConfigRotations (rrc, ¤t);
126 fprintf (stderr, "%s: Available Rotations:\t", blurb());
127 if (available & RR_Rotate_0) fprintf (stderr, " 0");
128 if (available & RR_Rotate_90) fprintf (stderr, " 90");
129 if (available & RR_Rotate_180) fprintf (stderr, " 180");
130 if (available & RR_Rotate_270) fprintf (stderr, " 270");
131 if (! (available & (RR_Rotate_0 | RR_Rotate_90 |
132 RR_Rotate_180 | RR_Rotate_270)))
133 fprintf (stderr, " none");
134 fprintf (stderr, "\n");
136 if (current_rotation != current)
138 "%s: WARNING: rotation inconsistency: 0x%X vs 0x%X\n",
139 blurb(), current_rotation, current);
141 fprintf (stderr, "%s: Current Rotation:\t", blurb());
142 if (current & RR_Rotate_0) fprintf (stderr, " 0");
143 if (current & RR_Rotate_90) fprintf (stderr, " 90");
144 if (current & RR_Rotate_180) fprintf (stderr, " 180");
145 if (current & RR_Rotate_270) fprintf (stderr, " 270");
146 if (! (current & (RR_Rotate_0 | RR_Rotate_90 |
147 RR_Rotate_180 | RR_Rotate_270)))
148 fprintf (stderr, " none");
149 fprintf (stderr, "\n");
151 fprintf (stderr, "%s: Available Reflections:\t", blurb());
152 if (available & RR_Reflect_X) fprintf (stderr, " X");
153 if (available & RR_Reflect_Y) fprintf (stderr, " Y");
154 if (! (available & (RR_Reflect_X | RR_Reflect_Y)))
155 fprintf (stderr, " none");
156 fprintf (stderr, "\n");
158 fprintf (stderr, "%s: Current Reflections:\t", blurb());
159 if (current & RR_Reflect_X) fprintf (stderr, " X");
160 if (current & RR_Reflect_Y) fprintf (stderr, " Y");
161 if (! (current & (RR_Reflect_X | RR_Reflect_Y)))
162 fprintf (stderr, " none");
163 fprintf (stderr, "\n");
169 XRRScreenSize *rrsizes;
171 rrsizes = XRRConfigSizes (rrc, &nsizes);
173 fprintf (stderr, "%s: sizes:\t none\n", blurb());
175 for (j = 0; j < nsizes; j++)
180 "%s: %c size %d: %d x %d\t rates:",
182 (j == current_size ? '+' : ' '),
184 rrsizes[j].width, rrsizes[j].height);
186 rates = XRRConfigRates (rrc, j, &nrates);
188 fprintf (stderr, " none?");
190 for (k = 0; k < nrates; k++)
191 fprintf (stderr, " %d", rates[k]);
192 fprintf (stderr, "\n");
193 /* don't free 'rates' */
195 /* don't free 'rrsizes' */
198 XRRFreeScreenConfigInfo (rrc);
202 fprintf(stderr, "%s: XRRGetScreenInfo(dpy, %d) ==> NULL\n",
210 XWindowAttributes xgwa[20];
212 for (i = 0; i < nscreens; i++)
214 XRRSelectInput (dpy, RootWindow (dpy, i), RRScreenChangeNotifyMask);
215 w[i] = RootWindow (dpy, i);
216 XGetWindowAttributes (dpy, w[i], &xgwa[i]);
220 fprintf (stderr, "\n%s: awaiting events...\n", progname);
224 XNextEvent (dpy, &event);
226 if (event.type == event_number + RRScreenChangeNotify)
228 XRRScreenChangeNotifyEvent *xrr_event =
229 (XRRScreenChangeNotifyEvent *) &event;
230 int screen = XRRRootToScreen (dpy, xrr_event->window);
232 fprintf (stderr, "%s: screen %d: RRScreenChangeNotify event\n",
235 fprintf (stderr, "%s: screen %d: old size: \t%d x %d\n",
237 DisplayWidth (dpy, screen),
238 DisplayHeight (dpy, screen));
239 fprintf (stderr, "%s: screen %d: old root 0x%lx:\t%d x %d\n",
240 progname, screen, (unsigned long) w[screen],
241 xgwa[screen].width, xgwa[screen].height);
243 XRRUpdateConfiguration (&event);
246 fprintf (stderr, "%s: screen %d: new size: \t%d x %d\n",
248 DisplayWidth (dpy, screen),
249 DisplayHeight (dpy, screen));
251 w[screen] = RootWindow (dpy, screen);
252 XGetWindowAttributes (dpy, w[screen], &xgwa[screen]);
253 fprintf (stderr, "%s: screen %d: new root 0x%lx:\t%d x %d\n",
254 progname, screen, (unsigned long) w[screen],
255 xgwa[screen].width, xgwa[screen].height);
256 fprintf (stderr, "\n");
260 fprintf (stderr, "%s: event %d\n", progname, event.type);