summaryrefslogtreecommitdiffstats
path: root/driver/test-randr.c
blob: 8acfb9818e2298f68d876178c177ae2a24befd59 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* test-randr.c --- playing with the Resize And Rotate extension.
 * xscreensaver, Copyright © 2004-2022 Jamie Zawinski <jwz@jwz.org>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdlib.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#include <stdio.h>
#include <time.h>
#include <sys/time.h>

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Intrinsic.h>

#include <X11/Xproto.h>
#include <X11/extensions/Xrandr.h>

#include "blurb.h"
char *progclass = "XScreenSaver";

static Bool error_handler_hit_p = False;

static int
ignore_all_errors_ehandler (Display *dpy, XErrorEvent *error)
{
  error_handler_hit_p = True;
  return 0;
}


static void
query_outputs (Display *dpy, int screen)
{
# ifdef HAVE_RANDR_12
  int major = -1, minor = -1;

  if (!XRRQueryVersion(dpy, &major, &minor))
    abort();

  if (major > 1 || (major == 1 && minor >= 2))
    {
      int j;
      XRRScreenResources *res = 
        XRRGetScreenResources (dpy, RootWindow (dpy, screen));
      fprintf (stderr, "\n");
      for (j = 0; j < res->noutput; j++)
        {
          int k;
          XRROutputInfo *rroi = XRRGetOutputInfo (dpy, res, res->outputs[j]);
          fprintf (stderr, "%s:   Output %d: %s: %s (%d)\n", blurb(), j,
                   rroi->name,
                   (rroi->connection == RR_Connected         ? "connected" :
                    rroi->connection == RR_Disconnected      ? "disconnected" :
                    rroi->connection == RR_UnknownConnection ? "unknown" :
                    "ERROR"),
                   (int) rroi->crtc);
          for (k = 0; k < rroi->ncrtc; k++)
            {
              XRRCrtcInfo *crtci = XRRGetCrtcInfo (dpy, res, rroi->crtcs[k]);
              fprintf(stderr, "%s:   %c CRTC %d (%d): %dx%d+%d+%d\n", 
                      blurb(),
                      (rroi->crtc == rroi->crtcs[k] ? '+' : ' '),
                      k, (int) rroi->crtcs[k],
                      crtci->width, crtci->height, crtci->x, crtci->y);
              XRRFreeCrtcInfo (crtci);
            }
          XRRFreeOutputInfo (rroi);
          fprintf (stderr, "\n");
        }
      XRRFreeScreenResources (res);
    }
# endif /* HAVE_RANDR_12 */
}


int
main (int argc, char **argv)
{
  int event_number = -1, error_number = -1;
  int major = -1, minor = -1;
  int nscreens = 0;
  int i;

  XtAppContext app;
  Widget toplevel_shell = XtAppInitialize (&app, progclass, 0, 0,
					   &argc, argv, 0, 0, 0);
  Display *dpy = XtDisplay (toplevel_shell);

  progname = argv[0];
  nscreens = ScreenCount(dpy);

  if (!XRRQueryExtension(dpy, &event_number, &error_number))
    {
      fprintf(stderr, "%s: XRRQueryExtension(dpy, ...) ==> False\n",
	      blurb());
      fprintf(stderr, "%s: server does not support the RANDR extension\n",
	      blurb());
      major = -1;
    }
  else
    {
      fprintf(stderr, "%s: XRRQueryExtension(dpy, ...) ==> %d, %d\n",
              blurb(), event_number, error_number);

      if (!XRRQueryVersion(dpy, &major, &minor))
        {
          fprintf(stderr, "%s: XRRQueryVersion(dpy, ...) ==> False\n",
                  blurb());
          fprintf(stderr, "%s: server didn't report RANDR version numbers?\n",
                  blurb());
        }
      else
        fprintf(stderr, "%s: XRRQueryVersion(dpy, ...) ==> %d, %d\n", blurb(),
                major, minor);
    }

  for (i = 0; i < nscreens; i++)
    {
      XRRScreenConfiguration *rrc;
      XErrorHandler old_handler;

      XSync (dpy, False);
      error_handler_hit_p = False;
      old_handler = XSetErrorHandler (ignore_all_errors_ehandler);

      rrc = (major >= 0 ? XRRGetScreenInfo (dpy, RootWindow (dpy, i)) : 0);

      XSync (dpy, False);
      XSetErrorHandler (old_handler);
      XSync (dpy, False);

      if (error_handler_hit_p)
        {
          fprintf(stderr, "%s:   XRRGetScreenInfo(dpy, %d) ==> X error:\n",
                  blurb(), i);
          /* do it again without the error handler to print the error */
          rrc = XRRGetScreenInfo (dpy, RootWindow (dpy, i));
        }
      else if (rrc)
        {
          SizeID current_size = -1;
          Rotation current_rotation = ~0;

          fprintf (stderr, "\n%s: Screen %d\n", blurb(), i);

          current_size =
            XRRConfigCurrentConfiguration (rrc, &current_rotation);

          /* Times */
# if 0    /* #### This is wrong -- I don't understand what these two
                  timestamp numbers represent, or how they correlate
                  to the wall clock or to each other. */
          {
            Time server_time, config_time;
            server_time = XRRConfigTimes (rrc, &config_time);
            if (config_time == 0 || server_time == 0)
              fprintf (stderr, "%s:   config has never been changed\n",
                       blurb());
            else
              fprintf (stderr, "%s:   config changed %lu seconds ago\n",
                       blurb(), (unsigned long) (server_time - config_time));
          }
# endif

          /* Rotations */
          {
            Rotation available, current;
            available = XRRConfigRotations (rrc, &current);

            fprintf (stderr, "%s:   Available Rotations:\t", blurb());
            if (available & RR_Rotate_0)   fprintf (stderr,   " 0");
            if (available & RR_Rotate_90)  fprintf (stderr,  " 90");
            if (available & RR_Rotate_180) fprintf (stderr, " 180");
            if (available & RR_Rotate_270) fprintf (stderr, " 270");
            if (! (available & (RR_Rotate_0   | RR_Rotate_90 |
                                RR_Rotate_180 | RR_Rotate_270)))
              fprintf (stderr, " none");
            fprintf (stderr, "\n");

            if (current_rotation != current)
              fprintf (stderr,
                       "%s:   WARNING: rotation inconsistency: 0x%X vs 0x%X\n",
                       blurb(), current_rotation, current);

            fprintf (stderr, "%s:   Current Rotation:\t", blurb());
            if (current & RR_Rotate_0)   fprintf (stderr,   " 0");
            if (current & RR_Rotate_90)  fprintf (stderr,  " 90");
            if (current & RR_Rotate_180) fprintf (stderr, " 180");
            if (current & RR_Rotate_270) fprintf (stderr, " 270");
            if (! (current & (RR_Rotate_0   | RR_Rotate_90 |
                              RR_Rotate_180 | RR_Rotate_270)))
              fprintf (stderr, " none");
            fprintf (stderr, "\n");

            fprintf (stderr, "%s:   Available Reflections:\t", blurb());
            if (available & RR_Reflect_X) fprintf (stderr,   " X");
            if (available & RR_Reflect_Y) fprintf (stderr,   " Y");
            if (! (available & (RR_Reflect_X | RR_Reflect_Y)))
              fprintf (stderr, " none");
            fprintf (stderr, "\n");

            fprintf (stderr, "%s:   Current Reflections:\t", blurb());
            if (current & RR_Reflect_X) fprintf (stderr,   " X");
            if (current & RR_Reflect_Y) fprintf (stderr,   " Y");
            if (! (current & (RR_Reflect_X | RR_Reflect_Y)))
              fprintf (stderr, " none");
            fprintf (stderr, "\n");
          }

          /* Sizes */
          {
            int nsizes, j;
            XRRScreenSize *rrsizes;

            rrsizes = XRRConfigSizes (rrc, &nsizes);
            if (nsizes <= 0)
                fprintf (stderr, "%s:   sizes:\t none\n", blurb());
            else
              for (j = 0; j < nsizes; j++)
                {
                  short *rates;
                  int nrates, k;
                  fprintf (stderr,
                           "%s:   %c size %d: %d x %d\t rates:",
                           blurb(), 
                           (j == current_size ? '+' : ' '),
                           j,
                           rrsizes[j].width, rrsizes[j].height);

                  rates = XRRConfigRates (rrc, j, &nrates);
                  if (nrates == 0)
                    fprintf (stderr, " none?");
                  else
                    for (k = 0; k < nrates; k++)
                      fprintf (stderr, " %d", rates[k]);
                  fprintf (stderr, "\n");
                  /* don't free 'rates' */
                }
            /* don't free 'rrsizes' */
          }

          XRRFreeScreenConfigInfo (rrc);
        }
      else if (major >= 0)
        {
          fprintf(stderr, "%s:   XRRGetScreenInfo(dpy, %d) ==> NULL\n",
                  blurb(), i);
        }

      query_outputs (dpy, i);
    }

  if (major > 0)
    {
      Window w[20];
      XWindowAttributes xgwa[20];

      for (i = 0; i < nscreens; i++)
        {
          XRRSelectInput (dpy, RootWindow (dpy, i), RRScreenChangeNotifyMask);
          w[i] = RootWindow (dpy, i);
          XGetWindowAttributes (dpy, w[i], &xgwa[i]);
        }

      XSync (dpy, False);

      fprintf (stderr, "\n%s: awaiting events...\n\n"
          "\t(If you resize the screen or add/remove monitors, this should\n"
          "\tnotice that and print stuff.  Otherwise, hit ^C.)\n\n",
               blurb());
      while (1)
        {
	  XEvent event;
	  XNextEvent (dpy, &event);

          if (event.type == event_number + RRScreenChangeNotify)
            {
              XRRScreenChangeNotifyEvent *xrr_event =
                (XRRScreenChangeNotifyEvent *) &event;
              int screen = XRRRootToScreen (dpy, xrr_event->window);

              fprintf (stderr, "%s: screen %d: RRScreenChangeNotify event\n",
                       blurb(), screen);

              fprintf (stderr, "%s: screen %d: old size: \t%d x %d\n",
                       blurb(), screen,
                       DisplayWidth (dpy, screen),
                       DisplayHeight (dpy, screen));
              fprintf (stderr, "%s: screen %d: old root: \t%d x %d\t0x%lx\n",
                       blurb(), screen,
                       xgwa[screen].width, xgwa[screen].height,
                       (unsigned long) w[screen]);

              XRRUpdateConfiguration (&event);
              XSync (dpy, False);

              fprintf (stderr, "%s: screen %d: new size: \t%d x %d\n",
                       blurb(), screen,
                       DisplayWidth (dpy, screen),
                       DisplayHeight (dpy, screen));

              w[screen] = RootWindow (dpy, screen);
              XGetWindowAttributes (dpy, w[screen], &xgwa[screen]);
              fprintf (stderr, "%s: screen %d: new root:\t%d x %d\t0x%lx\n",
                       blurb(), screen,
                       xgwa[screen].width, xgwa[screen].height,
                       (unsigned long) w[screen]);

              for (i = 0; i < nscreens; i++)
                query_outputs (dpy, i);
            }
          else
            {
              fprintf (stderr, "%s: event %d\n", blurb(), event.type);
            }
        }
    }

  XSync (dpy, False);
  exit (0);
}