1 /* screens.c --- dealing with RANDR, Xinerama, and VidMode Viewports.
2 * xscreensaver, Copyright (c) 1991-2008 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
14 /* There are a bunch of different mechanisms for multiple monitors
15 * available in X. XScreenSaver needs to care about this for two
16 * reasons: first, to ensure that all visible areas go black; and
17 * second, so that the windows of screen savers exactly fill the
18 * glass of each monitor (instead of one saver spanning multiple
19 * monitors, or a monitor displaying only a sub-rectangle of the
24 * This is the original way. Each monitor gets its own display
25 * number. :0.0 is the first one, :0.1 is the next, etc. The
26 * value of $DISPLAY determines which screen windows open on by
27 * default. A single app can open windows on multiple screens
28 * with the same display connection, but windows cannot be moved
29 * from one screen to another. The mouse can be moved from one
30 * screen to another, though. Screens may be different depths
31 * (e.g., one can be TrueColor and one can be PseudoColor.)
32 * Screens cannot be resized or moved without restarting X.
34 * Everyone hates this way of doing things because of the
35 * inability to move a window from one screen to another without
36 * restarting the application.
40 * There is a single giant root window that spans all the
41 * monitors. All monitors are the same depth, and windows can be
42 * moved around. Applications can learn which rectangles are
43 * actually visible on monitors by querying the Xinerama server
44 * extension. (If you don't do that, you end up with dialog
45 * boxes that try to appear in the middle of the screen actually
46 * spanning the gap between two monitors.)
48 * Xinerama doesn't work with DRI, which means that if you use
49 * it, you lose hardware acceleration on OpenGL programs. Also,
50 * screens can't be resized or moved without restarting X.
52 * 3) Vidmode Viewports:
54 * With this extension, the root window can be bigger than the
55 * monitor. Moving the mouse near the edges of the screen
56 * scrolls around, like a pan-and-scan movie. There can also be
57 * a hot key for changing the monitor's resolution (zooming
60 * Trying to combine this with Xinerama crashes the server, so
61 * you can only use this if you have only a single screen, or are
62 * in old-multi-screen mode.
64 * Also, half the time it doesn't work at all: it tends to lie
65 * about the size of the rectangle in use.
69 * The first version of the "Resize and Rotate" extension let you
70 * change the resolution of a screen on the fly. The root window
71 * would actually resize. However, it was also incompatible with
72 * Xinerama (did it crash, or just do nothing? I can't remember)
73 * so you needed to be in single-screen or old multi-screen mode.
74 * I believe RANDR could co-exist with Vidmode Viewports, but I'm
79 * Finally, RANDR added the functionality of Xinerama, plus some.
80 * Each X screen (in the sense of #1, "multi-screen") can have a
81 * number of sub-rectangles that are displayed on monitors, and
82 * each of those sub-rectangles can be displayed on more than one
83 * monitor. So it's possible (I think) to have a hybrid of
84 * multi-screen and Xinerama (e.g., to have two monitors running
85 * in one depth, and three monitors running in another?)
86 * Typically though, there will be a single X screen, with
87 * Xinerama-like division of that large root window onto multiple
88 * monitors. Also everything's dynamic: monitors can be added,
89 * removed, and resized at runtime.
91 * I believe that as of RANDR 1.2, the Xinerama extension still
92 * exists but only as a compatiblity layer: it's actually
93 * returning data from the RANDR extension.
95 * Though RANDR 1.2 allows the same image to be cloned onto more
96 * than one monitor, and also allows one monitor to show a
97 * subsection of something on another monitor (e.g., the
98 * rectangles can be enclosed or overlap). Since there's no way
99 * to put seperate savers on those duplicated-or-overlapping
100 * monitors, xscreensaver just ignores them (which allows them to
101 * display duplicates or overlaps).
108 #include <X11/Xlib.h>
111 # include <X11/extensions/Xrandr.h>
112 #endif /* HAVE_RANDR */
115 # include <X11/extensions/Xinerama.h>
116 #endif /* HAVE_XINERAMA */
118 #ifdef HAVE_XF86VMODE
119 # include <X11/extensions/xf86vmode.h>
120 #endif /* HAVE_XF86VMODE */
122 /* This file doesn't need the Xt headers, so stub these types out... */
124 #define XtAppContext void*
125 #define XrmDatabase void*
126 #define XtIntervalId void*
127 #define XtPointer void*
130 #include "xscreensaver.h"
134 typedef enum { S_SANE, S_ENCLOSED, S_DUPLICATE, S_OVERLAP,
135 S_OFFSCREEN, S_DISABLED } monitor_sanity;
137 /* 'typedef monitor' is in types.h */
142 int x, y, width, height;
143 monitor_sanity sanity; /* I'm not crazy you're the one who's crazy */
144 int enemy; /* which monitor it overlaps or duplicates */
148 free_monitors (monitor **monitors)
150 monitor **m2 = monitors;
151 if (! monitors) return;
154 if ((*m2)->desc) free ((*m2)->desc);
165 xinerama_scan_monitors (Display *dpy)
167 Screen *screen = DefaultScreenOfDisplay (dpy);
168 int event, error, nscreens, i;
169 XineramaScreenInfo *xsi;
172 if (! XineramaQueryExtension (dpy, &event, &error))
175 if (! XineramaIsActive (dpy))
178 xsi = XineramaQueryScreens (dpy, &nscreens);
181 monitors = (monitor **) calloc (nscreens + 1, sizeof(*monitors));
182 if (!monitors) return 0;
184 for (i = 0; i < nscreens; i++)
186 monitor *m = (monitor *) calloc (1, sizeof (monitor));
192 m->width = xsi[i].width;
193 m->height = xsi[i].height;
198 #endif /* HAVE_XINERAMA */
201 #ifdef HAVE_XF86VMODE
204 vidmode_scan_monitors (Display *dpy)
206 int event, error, nscreens, i;
209 /* Note that XF86VidModeGetViewPort() tends to be full of lies on laptops
210 that have a docking station or external monitor that runs in a different
211 resolution than the laptop's screen:
213 http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=81593
214 http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=208417
215 http://bugs.xfree86.org/show_bug.cgi?id=421
217 Presumably this is fixed by using RANDR instead of VidMode.
220 # ifdef HAVE_XINERAMA
221 /* Attempts to use the VidMode extension when the Xinerama extension is
222 active can result in a server crash! Yay! */
223 if (XQueryExtension (dpy, "XINERAMA", &error, &event, &error))
225 # endif /* !HAVE_XINERAMA */
227 if (! XF86VidModeQueryExtension (dpy, &event, &error))
230 nscreens = ScreenCount (dpy);
231 monitors = (monitor **) calloc (nscreens + 1, sizeof(*monitors));
232 if (!monitors) return 0;
234 for (i = 0; i < nscreens; i++)
236 monitor *m = (monitor *) calloc (1, sizeof (monitor));
237 XF86VidModeModeLine ml;
239 Screen *screen = ScreenOfDisplay (dpy, i);
245 if (! safe_XF86VidModeGetViewPort (dpy, i, &m->x, &m->y))
248 if (XF86VidModeGetModeLine (dpy, i, &dot, &ml))
250 m->width = ml.hdisplay;
251 m->height = ml.vdisplay;
254 /* Apparently, though the server stores the X position in increments of
255 1 pixel, it will only make changes to the *display* in some other
256 increment. With XF86_SVGA on a Thinkpad, the display only updates
257 in multiples of 8 pixels when in 8-bit mode, and in multiples of 4
258 pixels in 16-bit mode. I don't know what it does in 24- and 32-bit
259 mode, because I don't have enough video memory to find out.
261 I consider it a bug that XF86VidModeGetViewPort() is telling me the
262 server's *target* scroll position rather than the server's *actual*
263 scroll position. David Dawes agrees, and says they may fix this in
264 XFree86 4.0, but it's notrivial.
266 He also confirms that this behavior is server-dependent, so the
267 actual scroll position cannot be reliably determined by the client.
268 So... that means the only solution is to provide a ``sandbox''
269 around the blackout window -- we make the window be up to N pixels
270 larger than the viewport on both the left and right sides. That
271 means some part of the outer edges of each hack might not be
272 visible, but screw it.
274 I'm going to guess that 16 pixels is enough, and that the Y dimension
275 doesn't have this problem.
277 The drawback of doing this, of course, is that some of the screenhacks
278 will still look pretty stupid -- for example, "slidescreen" will cut
279 off the left and right edges of the grid, etc.
282 if (m->x > 0 && m->x < m->width - ml.hdisplay)
284 /* Not at left edge or right edge:
285 Round X position down to next lower multiple of FUDGE.
286 Increase width by 2*FUDGE in case some server rounds up.
288 m->x = ((m->x - 1) / FUDGE) * FUDGE;
289 m->width += (FUDGE * 2);
297 #endif /* HAVE_XF86VMODE */
303 randr_scan_monitors (Display *dpy)
305 int event, error, major, minor, nscreens, i, j;
307 Bool new_randr_p = False;
309 if (! XRRQueryExtension (dpy, &event, &error))
312 if (! XRRQueryVersion (dpy, &major, &minor))
315 if (major <= 0) /* Protocol was still in flux back then -- fuck it. */
318 # ifdef HAVE_RANDR_12
319 new_randr_p = (major > 1 || (major == 1 && minor >= 2));
323 /* RANDR 1.0 -- no Xinerama-like virtual screens. */
324 nscreens = ScreenCount (dpy);
325 else /* RANDR 1.2 or newer -- built-in Xinerama */
327 # ifdef HAVE_RANDR_12
328 int xsc = ScreenCount (dpy);
330 /* Add up the virtual screens on each X screen. */
331 for (i = 0; i < xsc; i++)
333 XRRScreenResources *res =
334 XRRGetScreenResources (dpy, RootWindow (dpy, i));
335 nscreens += res->noutput;
336 XRRFreeScreenResources (res);
338 # endif /* HAVE_RANDR_12 */
341 monitors = (monitor **) calloc (nscreens + 1, sizeof(*monitors));
342 if (!monitors) return 0;
344 for (i = 0, j = 0; i < ScreenCount (dpy); i++)
346 Screen *screen = ScreenOfDisplay (dpy, j);
348 if (! new_randr_p) /* RANDR 1.0 */
350 XRRScreenConfiguration *rrc;
351 monitor *m = (monitor *) calloc (1, sizeof (monitor));
356 rrc = XRRGetScreenInfo (dpy, RootWindowOfScreen (screen));
361 XRRScreenSize *rrsizes;
364 size = XRRConfigCurrentConfiguration (rrc, &rot);
365 rrsizes = XRRConfigSizes (rrc, &nsizes);
367 if (rot & (RR_Rotate_90|RR_Rotate_270))
369 m->width = rrsizes[size].height;
370 m->height = rrsizes[size].width;
374 m->width = rrsizes[size].width;
375 m->height = rrsizes[size].height;
378 /* don't free 'rrsizes' */
379 XRRFreeScreenConfigInfo (rrc);
382 else /* RANDR 1.2 or newer */
384 # ifdef HAVE_RANDR_12
386 XRRScreenResources *res =
387 XRRGetScreenResources (dpy, RootWindowOfScreen (screen));
388 for (k = 0; k < res->noutput; k++)
390 monitor *m = (monitor *) calloc (1, sizeof (monitor));
391 XRROutputInfo *rroi = XRRGetOutputInfo (dpy, res,
393 RRCrtc crtc = (rroi->crtc ? rroi->crtc : rroi->crtcs[0]);
394 XRRCrtcInfo *crtci = XRRGetCrtcInfo (dpy, res, crtc);
398 m->id = (i * 1000) + j;
399 m->desc = (rroi->name ? strdup (rroi->name) : 0);
403 if (crtci->rotation & (RR_Rotate_90|RR_Rotate_270))
405 m->width = crtci->height;
406 m->height = crtci->width;
410 m->width = crtci->width;
411 m->height = crtci->height;
416 if (rroi->connection == RR_Disconnected)
417 m->sanity = S_DISABLED;
418 /* #### do the same for RR_UnknownConnection? */
420 XRRFreeCrtcInfo (crtci);
421 XRRFreeOutputInfo (rroi);
423 XRRFreeScreenResources (res);
424 # endif /* HAVE_RANDR_12 */
431 #endif /* HAVE_RANDR */
435 basic_scan_monitors (Display *dpy)
437 int nscreens = ScreenCount (dpy);
439 monitor **monitors = (monitor **) calloc (nscreens + 1, sizeof(*monitors));
440 if (!monitors) return 0;
442 for (i = 0; i < nscreens; i++)
444 Screen *screen = ScreenOfDisplay (dpy, i);
445 monitor *m = (monitor *) calloc (1, sizeof (monitor));
451 m->width = WidthOfScreen (screen);
452 m->height = HeightOfScreen (screen);
458 #ifdef DEBUG_MULTISCREEN
460 /* If DEBUG_MULTISCREEN is defined, then in "-debug" mode, xscreensaver
461 will pretend that it is changing the number of connected monitors
462 every few seconds, using the geometries in the following list,
463 for stress-testing purposes.
466 debug_scan_monitors (Display *dpy)
468 static const char * const geoms[] = {
472 "800x600+0+22,800x600+800+22",
473 "800x600+0+22,800x600+800+22,800x600+300+622",
474 "800x600+0+22,800x600+800+22,800x600+0+622,800x600+800+622",
475 "640x480+0+22,640x480+640+22,640x480+0+502,640x480+640+502",
476 "640x480+240+22,640x480+0+502,640x480+640+502",
477 "640x480+0+200,640x480+640+200",
479 "320x200+0+22,320x200+320+22,320x200+640+22,320x200+960+22,320x200+0+222,320x200+320+222,320x200+640+222,320x200+960+222,320x200+0+422,320x200+320+422,320x200+640+422,320x200+960+422,320x200+0+622,320x200+320+622,320x200+640+622,320x200+960+622,320x200+0+822,320x200+320+822,320x200+640+822,320x200+960+822"
481 static int index = 0;
482 monitor **monitors = (monitor **) calloc (100, sizeof(*monitors));
484 Screen *screen = DefaultScreenOfDisplay (dpy);
486 char *s = strdup (geoms[index]);
487 char *token = strtok (s, ",");
490 monitor *m = calloc (1, sizeof (monitor));
494 if (4 != sscanf (token, "%dx%d+%d+%d%c",
495 &m->width, &m->height, &m->x, &m->y, &c))
499 monitors[nscreens++] = m;
500 token = strtok (0, ",");
504 index = (index+1) % countof(geoms);
508 #endif /* DEBUG_MULTISCREEN */
513 quadruple (monitor **monitors, Bool debug_p)
517 while (monitors[count])
519 monitors2 = (monitor **) calloc (count * 4 + 1, sizeof(*monitors));
520 if (!monitors2) abort();
522 for (i = 0, j = 0; i < count; i++)
525 for (k = 0; k < 4; k++)
527 monitors2[j+k] = (monitor *) calloc (1, sizeof (monitor));
528 *monitors2[j+k] = *monitors[i];
529 monitors2[j+k]->width /= (debug_p ? 4 : 2);
530 monitors2[j+k]->height /= 2;
531 monitors2[j+k]->id = (monitors[i]->id * 4) + k;
532 monitors2[j+k]->name = (monitors[i]->name
533 ? strdup (monitors[i]->name) : 0);
535 monitors2[j+1]->x += monitors2[j]->width;
536 monitors2[j+2]->y += monitors2[j]->height;
537 monitors2[j+3]->x += monitors2[j]->width;
538 monitors2[j+3]->y += monitors2[j]->height;
542 free_monitors (monitors);
545 #endif /* QUAD_MODE */
549 scan_monitors (saver_info *si)
551 saver_preferences *p = &si->prefs;
552 monitor **monitors = 0;
554 # ifdef DEBUG_MULTISCREEN
555 if (! monitors) monitors = debug_scan_monitors (si->dpy);
559 if (! p->getviewport_full_of_lies_p)
560 if (! monitors) monitors = randr_scan_monitors (si->dpy);
563 # ifdef HAVE_XF86VMODE
564 if (! monitors) monitors = vidmode_scan_monitors (si->dpy);
567 # ifdef HAVE_XF86VMODE
568 if (! monitors) monitors = xinerama_scan_monitors (si->dpy);
571 if (! monitors) monitors = basic_scan_monitors (si->dpy);
575 monitors = quadruple (monitors, p->debug_p);
583 monitors_overlap_p (monitor *a, monitor *b)
585 /* Two rectangles overlap if the max of the tops is less than the
586 min of the bottoms and the max of the lefts is less than the min
591 # define MAX(A,B) ((A)>(B)?(A):(B))
592 # define MIN(A,B) ((A)<(B)?(A):(B))
594 int maxleft = MAX(a->x, b->x);
595 int maxtop = MAX(a->y, b->y);
596 int minright = MIN(a->x + a->width - 1, b->x + b->width);
597 int minbot = MIN(a->y + a->height - 1, b->y + b->height);
598 return (maxtop < minbot && maxleft < minright);
602 /* Mark the ones that overlap, etc.
605 check_monitor_sanity (monitor **monitors)
609 while (monitors[count])
612 # define X1 monitors[i]->x
613 # define X2 monitors[j]->x
614 # define Y1 monitors[i]->y
615 # define Y2 monitors[j]->y
616 # define W1 monitors[i]->width
617 # define W2 monitors[j]->width
618 # define H1 monitors[i]->height
619 # define H2 monitors[j]->height
621 /* If a monitor is enclosed by any other monitor, that's insane.
623 for (i = 0; i < count; i++)
624 for (j = 0; j < count; j++)
626 monitors[i]->sanity == S_SANE &&
627 monitors[j]->sanity == S_SANE &&
630 (X2+W2) <= (X1+W1) &&
637 monitors[j]->sanity = S_DUPLICATE;
639 monitors[j]->sanity = S_ENCLOSED;
640 monitors[j]->enemy = i;
643 /* After checking for enclosure, check for other lossage against earlier
644 monitors. We do enclosure first so that we make sure to pick the
647 for (i = 0; i < count; i++)
648 for (j = 0; j < i; j++)
650 if (monitors[i]->sanity != S_SANE) continue; /* already marked */
651 if (monitors[j]->sanity != S_SANE) continue;
653 if (monitors_overlap_p (monitors[i], monitors[j]))
655 monitors[i]->sanity = S_OVERLAP;
656 monitors[i]->enemy = j;
660 /* Finally, make sure all monitors are enclosed by their X screen.
661 Xinerama sometimes reports 1024x768 VPs at -1936862040, -1953705044.
663 for (i = 0; i < count; i++)
665 int sw = WidthOfScreen (monitors[i]->screen) * 2;
666 int sh = HeightOfScreen (monitors[i]->screen) * 2;
667 if (monitors[i]->sanity != S_SANE) continue; /* already marked */
668 if (X1 < 0 || Y1 < 0 ||
669 W1 <= 0 || H1 <= 0 ||
670 X1+W1 > sw || Y1+H1 > sh)
672 monitors[i]->sanity = S_OFFSCREEN;
673 monitors[i]->enemy = 0;
689 layouts_differ_p (monitor **a, monitor **b)
691 if (!a || !b) return True;
696 if ((*a)->screen != (*b)->screen ||
697 (*a)->x != (*b)->x ||
698 (*a)->y != (*b)->y ||
699 (*a)->width != (*b)->width ||
700 (*a)->height != (*b)->height)
713 describe_monitor_layout (saver_info *si)
715 monitor **monitors = si->monitor_layout;
719 while (monitors[count])
721 if (monitors[count]->sanity == S_SANE)
729 fprintf (stderr, "%s: no screens!\n", blurb());
733 fprintf (stderr, "%s: screens in use: %d\n", blurb(), good_count);
734 for (i = 0; i < count; i++)
736 monitor *m = monitors[i];
737 if (m->sanity != S_SANE) continue;
738 fprintf (stderr, "%s: %3d/%d: %dx%d+%d+%d",
739 blurb(), m->id, screen_number (m->screen),
740 m->width, m->height, m->x, m->y);
741 if (m->desc && *m->desc) fprintf (stderr, " (%s)", m->desc);
742 fprintf (stderr, "\n");
746 fprintf (stderr, "%s: rejected screens: %d\n", blurb(), bad_count);
747 for (i = 0; i < count; i++)
749 monitor *m = monitors[i];
750 monitor *e = monitors[m->enemy];
751 if (m->sanity == S_SANE) continue;
752 fprintf (stderr, "%s: %3d/%d: %dx%d+%d+%d",
753 blurb(), m->id, screen_number (m->screen),
754 m->width, m->height, m->x, m->y);
755 if (m->desc && *m->desc) fprintf (stderr, " (%s)", m->desc);
756 fprintf (stderr, " -- ");
759 case S_SANE: abort(); break;
761 fprintf (stderr, "enclosed by %d (%dx%d+%d+%d)\n",
762 e->id, e->width, e->height, e->x, e->y);
765 fprintf (stderr, "duplicate of %d\n", e->id);
768 fprintf (stderr, "overlaps %d (%dx%d+%d+%d)\n",
769 e->id, e->width, e->height, e->x, e->y);
772 fprintf (stderr, "off screen (%dx%d)\n",
773 WidthOfScreen (e->screen),
774 HeightOfScreen (e->screen));
777 fprintf (stderr, "output disabled\n");
786 /* Synchronize the contents of si->ssi to the current state of the monitors.
787 Doesn't change anything if nothing has changed; otherwise, alters and
788 reuses existing saver_screen_info structs as much as possible.
789 Returns True if anything changed.
792 update_screen_layout (saver_info *si)
794 monitor **monitors = scan_monitors (si);
798 int seen_screens[100] = { 0, };
800 if (! layouts_differ_p (monitors, si->monitor_layout))
802 free_monitors (monitors);
806 free_monitors (si->monitor_layout);
807 si->monitor_layout = monitors;
808 check_monitor_sanity (si->monitor_layout);
810 while (monitors[count])
812 if (monitors[count]->sanity == S_SANE)
817 if (si->ssi_count == 0)
820 si->screens = (saver_screen_info *)
821 calloc (sizeof(*si->screens), si->ssi_count);
824 if (si->ssi_count <= good_count)
826 si->ssi_count = good_count + 10;
827 si->screens = (saver_screen_info *)
828 realloc (si->screens, sizeof(*si->screens) * si->ssi_count);
829 memset (si->screens + si->nscreens, 0,
830 sizeof(*si->screens) * (si->ssi_count - si->nscreens));
833 if (! si->screens) abort();
835 si->nscreens = good_count;
837 /* Regenerate the list of GL visuals as needed. */
838 if (si->best_gl_visuals)
839 free (si->best_gl_visuals);
840 si->best_gl_visuals = 0;
842 for (i = 0, j = 0; i < count; i++)
844 monitor *m = monitors[i];
845 saver_screen_info *ssi = &si->screens[j];
846 Screen *old_screen = ssi->screen;
848 if (monitors[i]->sanity != S_SANE) continue;
853 sn = screen_number (m->screen);
854 ssi->screen = m->screen;
855 ssi->real_screen_number = sn;
856 ssi->real_screen_p = (seen_screens[sn] == 0);
859 ssi->default_visual =
860 get_visual_resource (ssi->screen, "visualID", "VisualID", False);
861 ssi->current_visual = ssi->default_visual;
862 ssi->current_depth = visual_depth (ssi->screen, ssi->current_visual);
864 /* If the screen changed (or if this is the first time) we need
865 a new toplevel shell for this screen's depth.
867 if (ssi->screen != old_screen)
868 initialize_screen_root_widget (ssi);
870 ssi->poll_mouse_last_root_x = -1;
871 ssi->poll_mouse_last_root_y = -1;
875 ssi->width = m->width;
876 ssi->height = m->height;
878 # ifndef DEBUG_MULTISCREEN
880 saver_preferences *p = &si->prefs;
893 si->default_screen = &si->screens[0];