+#ifdef HAVE_XINERAMA
+
+static Bool
+screens_overlap_p (XineramaScreenInfo *a, XineramaScreenInfo *b)
+{
+ /* Two rectangles overlap if the max of the tops is less than the
+ min of the bottoms and the max of the lefts is less than the min
+ of the rights.
+ */
+# undef MAX
+# undef MIN
+# define MAX(A,B) ((A)>(B)?(A):(B))
+# define MIN(A,B) ((A)<(B)?(A):(B))
+
+ int maxleft = MAX(a->x_org, b->x_org);
+ int maxtop = MAX(a->y_org, b->y_org);
+ int minright = MIN(a->x_org + a->width - 1, b->x_org + b->width);
+ int minbot = MIN(a->y_org + a->height - 1, b->y_org + b->height);
+ return (maxtop < minbot && maxleft < minright);
+}
+
+
+/* Go through the list of Xinerama screen descriptions, and mark the
+ ones that appear to be insane, so that we don't use them.
+ */
+static void
+check_xinerama_sanity (int count, Bool verbose_p, XineramaScreenInfo *xsi)
+{
+ static Bool printed_p = False;
+ int i, j;
+ char err[1024];
+ *err = 0;
+
+# define X1 xsi[i].x_org
+# define X2 xsi[j].x_org
+# define Y1 xsi[i].y_org
+# define Y2 xsi[j].y_org
+# define W1 xsi[i].width
+# define W2 xsi[j].width
+# define H1 xsi[i].height
+# define H2 xsi[j].height
+
+# define WHINE() do { \
+ if (verbose_p) { \
+ if (! printed_p) { \
+ fprintf (stderr, "%s: compensating for Xinerama braindamage:\n", \
+ blurb()); \
+ printed_p = True; \
+ } \
+ fprintf (stderr, "%s: %d: %s\n", blurb(), xsi[i].screen_number,err); \
+ } \
+ xsi[i].screen_number = -1; \
+ } while(0)
+
+ /* If a screen is enclosed by any other screen, that's insane.
+ */
+ for (i = 0; i < count; i++)
+ for (j = 0; j < count; j++)
+ if (i != j &&
+ xsi[i].screen_number >= 0 &&
+ xsi[j].screen_number >= 0 &&
+ X1 >= X2 && Y1 >= Y2 && (X1+W1) <= (X2+W2) && (X1+H1) <= (X2+H2))
+ {
+ sprintf (err, "%dx%d+%d+%d enclosed by %dx%d+%d+%d",
+ W1, H1, X1, Y1,
+ W2, H2, X2, Y2);
+ WHINE();
+ continue;
+ }
+
+ /* After checking for enclosure, check for other lossage against earlier
+ screens. We do enclosure first so that we make sure to pick the
+ larger one.
+ */
+ for (i = 0; i < count; i++)
+ for (j = 0; j < i; j++)
+ {
+ if (xsi[i].screen_number < 0) continue; /* already marked */
+
+ *err = 0;
+ if (X1 == X2 && Y1 == Y2 && W1 == W2 && H1 == H2)
+ sprintf (err, "%dx%d+%d+%d duplicated", W1, H1, X1, Y1);
+
+ else if (screens_overlap_p (&xsi[i], &xsi[j]))
+ sprintf (err, "%dx%d+%d+%d overlaps %dx%d+%d+%d",
+ W1, H1, X1, Y1,
+ W2, H2, X2, Y2);
+
+ if (*err) WHINE();
+ }
+
+# undef X1
+# undef X2
+# undef Y1
+# undef Y2
+# undef W1
+# undef W2
+# undef H1
+# undef H2
+}
+
+#endif /* HAVE_XINERAMA */
+
+
+