+void
+new_region(struct swm_screen *s, struct workspace *ws,
+ int x, int y, int w, int h)
+{
+ struct swm_region *r;
+
+ DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
+ s->idx, x, y, w, h);
+
+ if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
+ errx(1, "calloc: failed to allocate memory for screen");
+
+ X(r) = x;
+ Y(r) = y;
+ WIDTH(r) = w;
+ HEIGHT(r) = h;
+ r->s = s;
+ r->ws = ws;
+ ws->r = r;
+ TAILQ_INSERT_TAIL(&s->rl, r, entry);
+ bar_setup(r);
+}
+
+void
+setup_screens(void)
+{
+#ifdef SWM_XRR_HAS_CRTC
+ XRRCrtcInfo *ci;
+ XRRScreenResources *sr;
+ int c;
+#endif /* SWM_XRR_HAS_CRTC */
+ Window d1, d2, *wins = NULL;
+ XWindowAttributes wa;
+ struct swm_region *r;
+ unsigned int no;
+ int errorbase, major, minor;
+ int ncrtc = 0, w = 0;
+ int i, j, k;
+ struct workspace *ws;
+
+ if ((screens = calloc(ScreenCount(display),
+ sizeof(struct swm_screen))) == NULL)
+ errx(1, "calloc: screens");
+
+ /* map physical screens */
+ for (i = 0; i < ScreenCount(display); i++) {
+ DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
+ screens[i].idx = i;
+ TAILQ_INIT(&screens[i].rl);
+ screens[i].root = RootWindow(display, i);
+
+ /* set default colors */
+ screens[i].color_focus = name_to_color("red");
+ screens[i].color_unfocus = name_to_color("rgb:88/88/88");
+ screens[i].bar_border = name_to_color("rgb:00/80/80");
+ screens[i].bar_color = name_to_color("black");
+ screens[i].bar_font_color = name_to_color("rgb:a0/a0/a0");
+
+ /* init all workspaces */
+ for (j = 0; j < SWM_WS_MAX; j++) {
+ ws = &screens[i].ws[j];
+ ws->idx = j;
+ ws->restack = 1;
+ ws->focus = NULL;
+ ws->r = NULL;
+ TAILQ_INIT(&ws->winlist);
+
+ for (k = 0; layouts[k].l_stack != NULL; k++)
+ if (layouts[k].l_config != NULL)
+ layouts[k].l_config(ws,
+ SWM_ARG_ID_STACKINIT);
+ ws->cur_layout = &layouts[0];
+ }
+
+ /* map virtual screens onto physical screens */
+ screens[i].xrandr_support = XRRQueryExtension(display,
+ &xrandr_eventbase, &errorbase);
+ if (screens[i].xrandr_support)
+ if (XRRQueryVersion(display, &major, &minor) &&
+ major < 1)
+ screens[i].xrandr_support = 0;
+
+#if 0 /* not ready for dynamic screen changes */
+ if (screens[i].xrandr_support)
+ XRRSelectInput(display,
+ screens[r->s].root,
+ RRScreenChangeNotifyMask);
+#endif
+
+ /* grab existing windows (before we build the bars)*/
+ if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
+ continue;
+
+#ifdef SWM_XRR_HAS_CRTC
+ sr = XRRGetScreenResources(display, screens[i].root);
+ if (sr == NULL)
+ new_region(&screens[i], &screens[i].ws[w],
+ 0, 0, DisplayWidth(display, i),
+ DisplayHeight(display, i));
+ else
+ ncrtc = sr->ncrtc;
+
+ for (c = 0; c < ncrtc; c++) {
+ ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
+ if (ci->noutput == 0)
+ continue;
+
+ if (ci != NULL && ci->mode == None)
+ new_region(&screens[i], &screens[i].ws[w], 0, 0,
+ DisplayWidth(display, i),
+ DisplayHeight(display, i));
+ else
+ new_region(&screens[i], &screens[i].ws[w],
+ ci->x, ci->y, ci->width, ci->height);
+ w++;
+ }
+ XRRFreeCrtcInfo(ci);
+ XRRFreeScreenResources(sr);
+#else
+ new_region(&screens[i], &screens[i].ws[w], 0, 0,
+ DisplayWidth(display, i),
+ DisplayHeight(display, i));
+#endif /* SWM_XRR_HAS_CRTC */
+
+ /* attach windows to a region */
+ /* normal windows */
+ if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
+ errx(1, "no regions on screen %d", i);
+
+ for (i = 0; i < no; i++) {
+ XGetWindowAttributes(display, wins[i], &wa);
+ if (!XGetWindowAttributes(display, wins[i], &wa) ||
+ wa.override_redirect ||
+ XGetTransientForHint(display, wins[i], &d1))
+ continue;
+
+ if (wa.map_state == IsViewable ||
+ getstate(wins[i]) == NormalState)
+ manage_window(wins[i], r->ws);
+ }
+ /* transient windows */
+ for (i = 0; i < no; i++) {
+ if (!XGetWindowAttributes(display, wins[i], &wa))
+ continue;
+
+ if (XGetTransientForHint(display, wins[i], &d1) &&
+ (wa.map_state == IsViewable || getstate(wins[i]) ==
+ NormalState))
+ manage_window(wins[i], r->ws);
+ }
+ if (wins) {
+ XFree(wins);
+ wins = NULL;
+ }
+ }
+}
+