3 * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Much code and ideas taken from dwm under the following license:
20 * MIT/X Consortium License
22 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
23 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
24 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
25 * 2007 Premysl Hruby <dfenze at gmail dot com>
26 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
27 * 2007 Christof Musik <christof at sendfax dot de>
28 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
29 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
30 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
32 * Permission is hereby granted, free of charge, to any person obtaining a
33 * copy of this software and associated documentation files (the "Software"),
34 * to deal in the Software without restriction, including without limitation
35 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
36 * and/or sell copies of the Software, and to permit persons to whom the
37 * Software is furnished to do so, subject to the following conditions:
39 * The above copyright notice and this permission notice shall be included in
40 * all copies or substantial portions of the Software.
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48 * DEALINGS IN THE SOFTWARE.
51 #define SWM_VERSION "0.5"
66 #include <sys/types.h>
70 #include <sys/queue.h>
71 #include <sys/param.h>
72 #include <sys/select.h>
74 #include <X11/cursorfont.h>
75 #include <X11/keysym.h>
76 #include <X11/Xatom.h>
78 #include <X11/Xproto.h>
79 #include <X11/Xutil.h>
80 #include <X11/extensions/Xrandr.h>
83 # error XRandR versions less than 1.0 are not supported
88 #define SWM_XRR_HAS_CRTC
92 /* #define SWM_DEBUG */
94 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
95 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
96 #define SWM_D_MISC 0x0001
97 #define SWM_D_EVENT 0x0002
98 #define SWM_D_WS 0x0004
99 #define SWM_D_FOCUS 0x0008
100 #define SWM_D_MOVE 0x0010
101 #define SWM_D_STACK 0x0020
103 u_int32_t swm_debug = 0
112 #define DPRINTF(x...)
113 #define DNPRINTF(n,x...)
116 #define LENGTH(x) (sizeof x / sizeof x[0])
117 #define MODKEY Mod1Mask
118 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
120 #define X(r) (r)->g.x
121 #define Y(r) (r)->g.y
122 #define WIDTH(r) (r)->g.w
123 #define HEIGHT(r) (r)->g.h
127 int (*xerrorxlib)(Display *, XErrorEvent *);
130 int xrandr_eventbase;
131 int ignore_enter = 0;
132 unsigned int numlockmask = 0;
136 int cycle_visible = 0;
139 double dialog_ratio = .6;
141 #define SWM_BAR_MAX (128)
142 sig_atomic_t bar_alarm = 0;
149 char *bar_fonts[] = {
150 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
151 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
155 /* terminal + args */
156 char *spawn_term[] = { "xterm", NULL };
157 char *spawn_menu[] = { "dmenu_run", NULL };
159 /* layout manager data */
160 struct swm_geometry {
170 /* virtual "screens" */
172 TAILQ_ENTRY(swm_region) entry;
173 struct swm_geometry g;
175 struct workspace *ws; /* current workspace on this region */
176 struct swm_screen *s; /* screen idx */
178 TAILQ_HEAD(swm_region_list, swm_region);
182 TAILQ_ENTRY(ws_win) entry;
184 struct swm_geometry g;
188 struct workspace *ws; /* always valid */
189 struct swm_screen *s; /* always valid, never changes */
190 XWindowAttributes wa;
192 TAILQ_HEAD(ws_win_list, ws_win);
194 /* layout handlers */
196 void vertical_config(struct workspace *, int);
197 void vertical_stack(struct workspace *, struct swm_geometry *);
198 void horizontal_config(struct workspace *, int);
199 void horizontal_stack(struct workspace *, struct swm_geometry *);
200 void max_stack(struct workspace *, struct swm_geometry *);
203 void (*l_stack)(struct workspace *, struct swm_geometry *);
204 void (*l_config)(struct workspace *, int);
206 /* stack, configure */
207 { vertical_stack, vertical_config},
208 { horizontal_stack, horizontal_config},
213 #define SWM_H_SLICE (32)
214 #define SWM_V_SLICE (32)
216 /* define work spaces */
218 int idx; /* workspace index */
219 int restack; /* restack on switch */
220 struct layout *cur_layout; /* current layout handlers */
221 struct ws_win *focus; /* may be NULL */
222 struct swm_region *r; /* may be NULL */
223 struct ws_win_list winlist; /* list of windows in ws */
227 int horizontal_msize;
234 /* physical screen mapping */
235 #define SWM_WS_MAX (10) /* XXX Too small? */
237 int idx; /* screen index */
238 struct swm_region_list rl; /* list of regions on this screen */
241 struct workspace ws[SWM_WS_MAX];
244 unsigned long bar_border;
245 unsigned long bar_color;
246 unsigned long bar_font_color;
247 unsigned long color_focus; /* XXX should this be per ws? */
248 unsigned long color_unfocus;
250 struct swm_screen *screens;
253 struct ws_win *cur_focus = NULL;
255 /* args to functions */
258 #define SWM_ARG_ID_FOCUSNEXT (0)
259 #define SWM_ARG_ID_FOCUSPREV (1)
260 #define SWM_ARG_ID_FOCUSMAIN (2)
261 #define SWM_ARG_ID_SWAPNEXT (3)
262 #define SWM_ARG_ID_SWAPPREV (4)
263 #define SWM_ARG_ID_SWAPMAIN (5)
264 #define SWM_ARG_ID_MASTERSHRINK (6)
265 #define SWM_ARG_ID_MASTERGROW (7)
266 #define SWM_ARG_ID_MASTERADD (8)
267 #define SWM_ARG_ID_MASTERDEL (9)
268 #define SWM_ARG_ID_STACKRESET (10)
269 #define SWM_ARG_ID_STACKINIT (11)
270 #define SWM_ARG_ID_CYCLEWS_UP (12)
271 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
276 name_to_color(char *colorname)
280 XColor screen_def, exact_def;
281 unsigned long result = 0;
282 char cname[32] = "#";
284 cmap = DefaultColormap(display, screens[0].idx);
285 status = XAllocNamedColor(display, cmap, colorname,
286 &screen_def, &exact_def);
288 strlcat(cname, colorname + 2, sizeof cname - 1);
289 status = XAllocNamedColor(display, cmap, cname, &screen_def,
293 result = screen_def.pixel;
295 fprintf(stderr, "color '%s' not found.\n", colorname);
301 varmatch(char *var, char *name, int *index)
306 i = strncmp(var, name, 255);
313 p = var + strlen(name);
316 bzero(buf, sizeof buf);
318 while (isdigit(*p) && i < sizeof buf)
320 if (i == 0 || i >= sizeof buf || *p != ']')
322 *index = strtonum(buf, 0, 99, NULL);
326 /* conf file stuff */
327 #define SWM_CONF_WS "\n= \t"
328 #define SWM_CONF_FILE "scrotwm.conf"
330 conf_load(char *filename)
333 char *line, *cp, *var, *val;
334 size_t len, lineno = 0;
337 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
339 if (filename == NULL)
342 if ((config = fopen(filename, "r")) == NULL)
345 for (sc = ScreenCount(display);;) {
346 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
350 cp += (long)strspn(cp, SWM_CONF_WS);
356 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
358 cp += (long)strspn(cp, SWM_CONF_WS);
359 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
362 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
365 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
366 bar_enabled = atoi(val);
367 else if (!varmatch(var, "bar_border", &i))
368 if (i > 0 && i <= sc)
369 screens[i - 1].bar_border =
372 for (i = 0; i < sc; i++)
373 screens[i].bar_border =
377 else if (!varmatch(var, "bar_color", &i))
378 if (i > 0 && i <= sc)
379 screens[i - 1].bar_color =
382 for (i = 0; i < sc; i++)
383 screens[i].bar_color =
387 else if (!varmatch(var, "bar_font_color", &i))
388 if (i > 0 && i <= sc)
389 screens[i - 1].bar_font_color =
392 for (i = 0; i < sc; i++)
393 screens[i].bar_font_color =
398 else if (!strncmp(var, "bar_font", strlen("bar_font")))
399 asprintf(&bar_fonts[0], "%s", val);
405 if (!varmatch(var, "color_focus", &i))
406 if (i > 0 && i <= sc)
407 screens[i - 1].color_focus =
410 for (i = 0; i < sc; i++)
411 screens[i].color_focus =
415 else if (!varmatch(var, "color_unfocus", &i))
416 if (i > 0 && i <= sc)
417 screens[i - 1].color_unfocus =
420 for (i = 0; i < sc; i++)
421 screens[i].color_unfocus =
425 else if (!strncmp(var, "cycle_empty", strlen("cycle_empty")))
426 cycle_visible = atoi(val);
427 else if (!strncmp(var, "cycle_visible", strlen("cycle_visible")))
428 cycle_visible = atoi(val);
434 if (!strncmp(var, "dialog_ratio",
435 strlen("dialog_ratio"))) {
436 dialog_ratio = atof(val);
437 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
444 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
445 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
456 errx(1, "invalid conf file entry: %s=%s", var, val);
458 errx(1, "invalid screen index: %s out of bounds", var);
462 bar_print(struct swm_region *r, char *s)
464 XClearWindow(display, r->bar_window);
465 XSetForeground(display, bar_gc, r->s->bar_font_color);
466 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
475 struct swm_region *r;
480 if (bar_enabled == 0)
484 localtime_r(&tmt, &tm);
485 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
486 for (i = 0; i < ScreenCount(display); i++) {
488 TAILQ_FOREACH(r, &screens[i].rl, entry) {
489 snprintf(e, sizeof e, "%s %d:%d",
490 s, x++, r->ws->idx + 1);
494 XSync(display, False);
505 bar_toggle(struct swm_region *r, union arg *args)
507 struct swm_region *tmpr;
508 int i, j, sc = ScreenCount(display);
510 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
513 for (i = 0; i < sc; i++)
514 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
515 XUnmapWindow(display, tmpr->bar_window);
517 for (i = 0; i < sc; i++)
518 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
519 XMapRaised(display, tmpr->bar_window);
521 bar_enabled = !bar_enabled;
522 XSync(display, False);
523 for (i = 0; i < sc; i++)
524 for (j = 0; j < SWM_WS_MAX; j++)
525 screens[i].ws[j].restack = 1;
528 /* must be after stack */
529 for (i = 0; i < sc; i++)
530 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
535 bar_setup(struct swm_region *r)
539 for (i = 0; bar_fonts[i] != NULL; i++) {
540 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
544 if (bar_fonts[i] == NULL)
545 errx(1, "couldn't load font");
546 bar_height = bar_fs->ascent + bar_fs->descent + 3;
548 r->bar_window = XCreateSimpleWindow(display,
549 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
550 1, r->s->bar_border, r->s->bar_color);
551 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
552 XSetFont(display, bar_gc, bar_fs->fid);
553 XSelectInput(display, r->bar_window, VisibilityChangeMask);
555 XMapRaised(display, r->bar_window);
556 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
558 if (signal(SIGALRM, bar_signal) == SIG_ERR)
559 err(1, "could not install bar_signal");
566 XSetWindowAttributes wa;
567 struct swm_region *r;
570 bzero(&wa, sizeof wa);
571 for (i = 0; i < ScreenCount(display); i++)
572 TAILQ_FOREACH(r, &screens[i].rl, entry) {
573 wa.border_pixel = screens[i].bar_border;
574 wa.background_pixel = screens[i].bar_color;
575 XChangeWindowAttributes(display, r->bar_window,
576 CWBackPixel | CWBorderPixel, &wa);
582 config_win(struct ws_win *win)
586 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
587 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
588 ce.type = ConfigureNotify;
589 ce.display = display;
595 ce.height = win->g.h;
596 ce.border_width = 1; /* XXX store this! */
598 ce.override_redirect = False;
599 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
603 count_win(struct workspace *ws, int count_transient)
608 TAILQ_FOREACH(win, &ws->winlist, entry) {
609 if (count_transient == 0 && win->floating)
611 if (count_transient == 0 && win->transient)
615 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
621 quit(struct swm_region *r, union arg *args)
623 DNPRINTF(SWM_D_MISC, "quit\n");
628 restart(struct swm_region *r, union arg *args)
630 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
632 /* disable alarm because the following code may not be interrupted */
634 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
635 errx(1, "can't disable alarm");
637 XCloseDisplay(display);
638 execvp(start_argv[0], start_argv);
639 fprintf(stderr, "execvp failed\n");
645 root_to_region(Window root)
647 struct swm_region *r;
652 for (i = 0; i < ScreenCount(display); i++)
653 if (screens[i].root == root)
656 if (XQueryPointer(display, screens[i].root,
657 &rr, &cr, &x, &y, &wx, &wy, &mask) == False) {
658 /* if we can't query the pointer, grab the first region */
659 r = TAILQ_FIRST(&screens[i].rl);
661 /* otherwise, choose a region based on pointer location */
662 TAILQ_FOREACH(r, &screens[i].rl, entry) {
663 if (x > X(r) && x < X(r) + WIDTH(r) &&
664 y > Y(r) && y < Y(r) + HEIGHT(r))
669 r = TAILQ_FIRST(&screens[i].rl);
675 find_window(Window id)
680 for (i = 0; i < ScreenCount(display); i++)
681 for (j = 0; j < SWM_WS_MAX; j++)
682 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
689 spawn(struct swm_region *r, union arg *args)
691 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
693 * The double-fork construct avoids zombie processes and keeps the code
694 * clean from stupid signal handlers.
699 close(ConnectionNumber(display));
701 execvp(args->argv[0], args->argv);
702 fprintf(stderr, "execvp failed\n");
716 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
718 for (i = 0; i < ScreenCount(display); i++)
719 for (j = 0; j < SWM_WS_MAX; j++)
720 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
721 if (win->ws->r == NULL)
723 XSetWindowBorder(display, win->id,
724 win->ws->r->s->color_unfocus);
726 win->ws->focus = NULL;
732 focus_win(struct ws_win *win)
734 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
740 win->ws->focus = win;
741 if (win->ws->r != NULL) {
744 XSetWindowBorder(display, win->id,
745 win->ws->r->s->color_focus);
747 XSetInputFocus(display, win->id,
748 RevertToPointerRoot, CurrentTime);
753 switchws(struct swm_region *r, union arg *args)
756 struct swm_region *this_r, *other_r;
758 struct workspace *new_ws, *old_ws;
762 new_ws = &this_r->s->ws[wsid];
764 DNPRINTF(SWM_D_WS, "switchws screen %d region %dx%d+%d+%d: "
765 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
768 if (new_ws == old_ws)
773 /* if the other workspace is hidden, switch windows */
774 /* map new window first to prevent ugly blinking */
775 TAILQ_FOREACH(win, &new_ws->winlist, entry)
776 XMapRaised(display, win->id);
778 TAILQ_FOREACH(win, &old_ws->winlist, entry)
779 XUnmapWindow(display, win->id);
784 other_r->ws = old_ws;
793 focus_win(new_ws->focus);
799 cyclews(struct swm_region *r, union arg *args)
802 struct swm_screen *s = r->s;
804 DNPRINTF(SWM_D_WS, "cyclews id %d "
805 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
806 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
812 case SWM_ARG_ID_CYCLEWS_UP:
813 if (a.id < SWM_WS_MAX - 1)
818 case SWM_ARG_ID_CYCLEWS_DOWN:
822 a.id = SWM_WS_MAX - 1;
828 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
830 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
834 } while (a.id != r->ws->idx);
838 swapwin(struct swm_region *r, union arg *args)
840 struct ws_win *target;
841 struct ws_win_list *wl;
844 DNPRINTF(SWM_D_WS, "swapwin id %d "
845 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
846 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
847 if (cur_focus == NULL)
850 wl = &cur_focus->ws->winlist;
853 case SWM_ARG_ID_SWAPPREV:
854 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
855 TAILQ_REMOVE(wl, cur_focus, entry);
857 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
859 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
861 case SWM_ARG_ID_SWAPNEXT:
862 target = TAILQ_NEXT(cur_focus, entry);
863 TAILQ_REMOVE(wl, cur_focus, entry);
865 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
867 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
869 case SWM_ARG_ID_SWAPMAIN:
870 target = TAILQ_FIRST(wl);
871 if (target == cur_focus)
873 TAILQ_REMOVE(wl, target, entry);
874 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
875 TAILQ_REMOVE(wl, cur_focus, entry);
876 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
879 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
888 focus(struct swm_region *r, union arg *args)
890 struct ws_win *winfocus, *winlostfocus;
891 struct ws_win_list *wl;
893 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
894 if (cur_focus == NULL)
897 wl = &cur_focus->ws->winlist;
899 winlostfocus = cur_focus;
902 case SWM_ARG_ID_FOCUSPREV:
903 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
904 if (winfocus == NULL)
905 winfocus = TAILQ_LAST(wl, ws_win_list);
908 case SWM_ARG_ID_FOCUSNEXT:
909 winfocus = TAILQ_NEXT(cur_focus, entry);
910 if (winfocus == NULL)
911 winfocus = TAILQ_FIRST(wl);
914 case SWM_ARG_ID_FOCUSMAIN:
915 winfocus = TAILQ_FIRST(wl);
922 if (winfocus == winlostfocus)
926 XSync(display, False);
930 cycle_layout(struct swm_region *r, union arg *args)
932 struct workspace *ws = r->ws;
934 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
937 if (ws->cur_layout->l_stack == NULL)
938 ws->cur_layout = &layouts[0];
945 stack_config(struct swm_region *r, union arg *args)
947 struct workspace *ws = r->ws;
949 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
952 if (ws->cur_layout->l_config != NULL)
953 ws->cur_layout->l_config(ws, args->id);
955 if (args->id != SWM_ARG_ID_STACKINIT);
961 struct swm_geometry g;
962 struct swm_region *r;
965 DNPRINTF(SWM_D_STACK, "stack\n");
967 for (i = 0; i < ScreenCount(display); i++) {
969 TAILQ_FOREACH(r, &screens[i].rl, entry) {
970 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
971 "(screen %d, region %d)\n", r->ws->idx, i, j++);
973 /* start with screen geometry, adjust for bar */
983 r->ws->cur_layout->l_stack(r->ws, &g);
986 XSync(display, False);
990 stack_floater(struct ws_win *win, struct swm_region *r)
995 bzero(&wc, sizeof wc);
996 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
998 if (win->transient) {
999 win->g.w = (double)WIDTH(r) * dialog_ratio;
1000 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1002 wc.width = win->g.w;
1003 wc.height = win->g.h;
1004 wc.x = (WIDTH(r) - win->g.w) / 2;
1005 wc.y = (HEIGHT(r) - win->g.h) / 2;
1007 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1008 win->id, wc.x, wc.y, wc.width, wc.height);
1010 XConfigureWindow(display, win->id, mask, &wc);
1015 vertical_config(struct workspace *ws, int id)
1017 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1020 case SWM_ARG_ID_STACKRESET:
1021 case SWM_ARG_ID_STACKINIT:
1022 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1023 ws->l_state.vertical_mwin = 1;
1025 case SWM_ARG_ID_MASTERSHRINK:
1026 if (ws->l_state.vertical_msize > 1)
1027 ws->l_state.vertical_msize--;
1029 case SWM_ARG_ID_MASTERGROW:
1030 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1031 ws->l_state.vertical_msize++;
1033 case SWM_ARG_ID_MASTERADD:
1034 ws->l_state.vertical_mwin++;
1036 case SWM_ARG_ID_MASTERDEL:
1037 if (ws->l_state.vertical_mwin > 0)
1038 ws->l_state.vertical_mwin--;
1046 vertical_stack(struct workspace *ws, struct swm_geometry *g) {
1048 struct swm_geometry gg = *g;
1049 struct ws_win *win, *winfocus;
1050 int i, j, split, colno, hrh, winno, main_width;
1053 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1055 if ((winno = count_win(ws, 0)) == 0)
1058 if (ws->focus == NULL)
1059 ws->focus = TAILQ_FIRST(&ws->winlist);
1060 winfocus = cur_focus ? cur_focus : ws->focus;
1062 if (ws->l_state.vertical_mwin &&
1063 winno > ws->l_state.vertical_mwin) {
1064 split = ws->l_state.vertical_mwin;
1066 main_width = (g->w / SWM_V_SLICE) *
1067 ws->l_state.vertical_msize;
1077 TAILQ_FOREACH(win, &ws->winlist, entry) {
1078 if (split && i == split) {
1079 colno = winno - split;
1080 hrh = (g->h / colno);
1081 gg.x += main_width + 2;
1082 gg.w = g->w - (main_width + 2);
1087 gg.h = (hrh + (g->h - (colno * hrh)));
1095 if (win->transient != 0 || win->floating != 0)
1096 stack_floater(win, ws->r);
1098 bzero(&wc, sizeof wc);
1099 wc.border_width = 1;
1100 win->g.x = wc.x = gg.x;
1101 win->g.y = wc.y = gg.y;
1102 win->g.w = wc.width = gg.w;
1103 win->g.h = wc.height = gg.h;
1104 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1105 XConfigureWindow(display, win->id, mask, &wc);
1108 XMapRaised(display, win->id);
1114 focus_win(winfocus); /* has to be done outside of the loop */
1119 horizontal_config(struct workspace *ws, int id)
1121 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1124 case SWM_ARG_ID_STACKRESET:
1125 case SWM_ARG_ID_STACKINIT:
1126 ws->l_state.horizontal_mwin = 1;
1127 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1129 case SWM_ARG_ID_MASTERSHRINK:
1130 if (ws->l_state.horizontal_msize > 1)
1131 ws->l_state.horizontal_msize--;
1133 case SWM_ARG_ID_MASTERGROW:
1134 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1135 ws->l_state.horizontal_msize++;
1137 case SWM_ARG_ID_MASTERADD:
1138 ws->l_state.horizontal_mwin++;
1140 case SWM_ARG_ID_MASTERDEL:
1141 if (ws->l_state.horizontal_mwin > 0)
1142 ws->l_state.horizontal_mwin--;
1150 horizontal_stack(struct workspace *ws, struct swm_geometry *g) {
1152 struct swm_geometry gg = *g;
1153 struct ws_win *win, *winfocus;
1154 int i, j, split, rowno, hrw, winno, main_height;
1157 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
1159 if ((winno = count_win(ws, 0)) == 0)
1162 if (ws->focus == NULL)
1163 ws->focus = TAILQ_FIRST(&ws->winlist);
1164 winfocus = cur_focus ? cur_focus : ws->focus;
1166 if (ws->l_state.horizontal_mwin &&
1167 winno > ws->l_state.horizontal_mwin) {
1168 split = ws->l_state.horizontal_mwin;
1170 main_height = (g->h / SWM_V_SLICE) *
1171 ws->l_state.horizontal_msize;
1181 TAILQ_FOREACH(win, &ws->winlist, entry) {
1182 if (split && i == split) {
1183 rowno = winno - split;
1184 hrw = (g->w / rowno);
1185 gg.y += main_height + 2;
1186 gg.h = g->h - (main_height + 2);
1191 gg.w = (hrw + (g->w - (rowno * hrw)));
1198 if (win->transient != 0 || win->floating != 0)
1199 stack_floater(win, ws->r);
1201 bzero(&wc, sizeof wc);
1202 wc.border_width = 1;
1203 win->g.x = wc.x = gg.x;
1204 win->g.y = wc.y = gg.y;
1205 win->g.w = wc.width = gg.w;
1206 win->g.h = wc.height = gg.h;
1207 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1208 XConfigureWindow(display, win->id, mask, &wc);
1211 XMapRaised(display, win->id);
1217 focus_win(winfocus); /* this has to be done outside of the loop */
1220 /* fullscreen view */
1222 max_stack(struct workspace *ws, struct swm_geometry *g) {
1224 struct swm_geometry gg = *g;
1225 struct ws_win *win, *winfocus;
1228 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1230 if (count_win(ws, 0) == 0)
1233 if (ws->focus == NULL)
1234 ws->focus = TAILQ_FIRST(&ws->winlist);
1235 winfocus = cur_focus ? cur_focus : ws->focus;
1237 TAILQ_FOREACH(win, &ws->winlist, entry) {
1238 if (win->transient != 0 || win->floating != 0) {
1239 if (win == ws->focus) {
1241 stack_floater(win, ws->r);
1242 XMapRaised(display, win->id);
1244 XUnmapWindow(display, win->id);
1246 bzero(&wc, sizeof wc);
1247 wc.border_width = 1;
1248 win->g.x = wc.x = gg.x;
1249 win->g.y = wc.y = gg.y;
1250 win->g.w = wc.width = gg.w;
1251 win->g.h = wc.height = gg.h;
1252 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1253 XConfigureWindow(display, win->id, mask, &wc);
1255 if (win == ws->focus) {
1256 XMapRaised(display, win->id);
1258 XUnmapWindow(display, win->id);
1263 focus_win(winfocus); /* has to be done outside of the loop */
1267 send_to_ws(struct swm_region *r, union arg *args)
1269 int wsid = args->id;
1270 struct ws_win *win = cur_focus;
1271 struct workspace *ws, *nws;
1273 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1276 nws = &win->s->ws[wsid];
1278 XUnmapWindow(display, win->id);
1280 /* find a window to focus */
1281 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1282 if (ws->focus == NULL)
1283 ws->focus = TAILQ_FIRST(&ws->winlist);
1284 if (ws->focus == win)
1287 TAILQ_REMOVE(&ws->winlist, win, entry);
1289 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1292 if (count_win(nws, 1) == 1)
1300 /* key definitions */
1304 void (*func)(struct swm_region *r, union arg *);
1307 /* modifier key function argument */
1308 { MODKEY, XK_space, cycle_layout, {0} },
1309 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1310 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1311 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1312 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1313 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1314 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1315 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1316 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1317 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1318 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1319 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1320 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
1321 { MODKEY | ShiftMask, XK_q, quit, {0} },
1322 { MODKEY, XK_q, restart, {0} },
1323 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1324 { MODKEY, XK_1, switchws, {.id = 0} },
1325 { MODKEY, XK_2, switchws, {.id = 1} },
1326 { MODKEY, XK_3, switchws, {.id = 2} },
1327 { MODKEY, XK_4, switchws, {.id = 3} },
1328 { MODKEY, XK_5, switchws, {.id = 4} },
1329 { MODKEY, XK_6, switchws, {.id = 5} },
1330 { MODKEY, XK_7, switchws, {.id = 6} },
1331 { MODKEY, XK_8, switchws, {.id = 7} },
1332 { MODKEY, XK_9, switchws, {.id = 8} },
1333 { MODKEY, XK_0, switchws, {.id = 9} },
1334 { MODKEY, XK_Right, cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1335 { MODKEY, XK_Left, cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1336 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1337 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1338 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1339 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1340 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1341 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1342 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1343 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1344 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1345 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1346 { MODKEY, XK_b, bar_toggle, {0} },
1347 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1348 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1353 enum { client_click, root_click };
1357 unsigned int button;
1358 void (*func)(struct swm_region *r, union arg *);
1361 /* action key mouse button func args */
1362 { client_click, MODKEY, Button2, NULL, {0} },
1367 updatenumlockmask(void)
1370 XModifierKeymap *modmap;
1372 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1374 modmap = XGetModifierMapping(display);
1375 for (i = 0; i < 8; i++)
1376 for (j = 0; j < modmap->max_keypermod; j++)
1377 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1378 == XKeysymToKeycode(display, XK_Num_Lock))
1379 numlockmask = (1 << i);
1381 XFreeModifiermap(modmap);
1387 unsigned int i, j, k;
1389 unsigned int modifiers[] =
1390 { 0, LockMask, numlockmask, numlockmask | LockMask };
1392 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1393 updatenumlockmask();
1395 for (k = 0; k < ScreenCount(display); k++) {
1396 if (TAILQ_EMPTY(&screens[k].rl))
1398 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1399 for (i = 0; i < LENGTH(keys); i++) {
1400 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1401 for (j = 0; j < LENGTH(modifiers); j++)
1402 XGrabKey(display, code,
1403 keys[i].mod | modifiers[j],
1404 screens[k].root, True,
1405 GrabModeAsync, GrabModeAsync);
1412 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1420 XKeyEvent *ev = &e->xkey;
1422 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1424 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1425 for (i = 0; i < LENGTH(keys); i++)
1426 if (keysym == keys[i].keysym
1427 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1429 keys[i].func(root_to_region(ev->root),
1434 buttonpress(XEvent *e)
1436 XButtonPressedEvent *ev = &e->xbutton;
1437 #ifdef SWM_CLICKTOFOCUS
1439 struct workspace *ws;
1440 struct swm_region *r;
1443 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1445 if (ev->window == ev->root)
1447 if (ev->window == cur_focus->id)
1449 #ifdef SWM_CLICKTOFOCUS
1450 r = root_to_region(ev->root);
1452 TAILQ_FOREACH(win, &ws->winlist, entry)
1453 if (win->id == ev->window) {
1454 /* focus in the clicked window */
1455 XSetWindowBorder(display, ev->window, 0xff0000);
1456 XSetWindowBorder(display, ws->focus->id, 0x888888);
1457 XSetInputFocus(display, ev->window, RevertToPointerRoot,
1460 XSync(display, False);
1467 set_win_state(struct ws_win *win, long state)
1469 long data[] = {state, None};
1471 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1473 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1474 (unsigned char *)data, 2);
1478 manage_window(Window id, struct workspace *ws)
1484 TAILQ_FOREACH(win, &ws->winlist, entry) {
1486 return (win); /* already being managed */
1489 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1490 errx(1, "calloc: failed to allocate memory for new window");
1494 win->s = ws->r->s; /* this never changes */
1495 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
1497 /* make new win focused */
1500 XGetTransientForHint(display, win->id, &trans);
1502 win->transient = trans;
1503 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1504 (unsigned)win->id, win->transient);
1506 XGetWindowAttributes(display, id, &win->wa);
1507 win->g.w = win->wa.width;
1508 win->g.h = win->wa.height;
1509 win->g.x = win->wa.x;
1510 win->g.y = win->wa.y;
1512 /* XXX make this a table */
1513 bzero(&ch, sizeof ch);
1514 if (XGetClassHint(display, win->id, &ch)) {
1515 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1516 if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
1520 XFree(ch.res_class);
1525 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
1526 PropertyChangeMask | StructureNotifyMask);
1528 set_win_state(win, NormalState);
1534 configurerequest(XEvent *e)
1536 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1541 if ((win = find_window(ev->window)) == NULL)
1545 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1547 bzero(&wc, sizeof wc);
1550 wc.width = ev->width;
1551 wc.height = ev->height;
1552 wc.border_width = ev->border_width;
1553 wc.sibling = ev->above;
1554 wc.stack_mode = ev->detail;
1555 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1557 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1559 if (win->floating) {
1560 if (ev->value_mask & CWX)
1562 if (ev->value_mask & CWY)
1564 if (ev->value_mask & CWWidth)
1565 win->g.w = ev->width;
1566 if (ev->value_mask & CWHeight)
1567 win->g.h = ev->height;
1568 if (win->ws->r != NULL) {
1569 /* this seems to be full screen */
1570 if (win->g.w > WIDTH(win->ws->r)) {
1575 if (win->g.h > HEIGHT(win->ws->r)) {
1581 if ((ev->value_mask & (CWX|CWY)) &&
1582 !(ev->value_mask & (CWWidth|CWHeight)))
1584 XMoveResizeWindow(display, win->id,
1585 win->g.x, win->g.y, win->g.w, win->g.h);
1592 configurenotify(XEvent *e)
1594 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1595 e->xconfigure.window);
1599 destroynotify(XEvent *e)
1602 XDestroyWindowEvent *ev = &e->xdestroywindow;
1603 struct workspace *ws;
1605 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1607 if ((win = find_window(ev->window)) != NULL) {
1609 /* find a window to focus */
1610 if (ws->focus == win)
1611 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1612 if (ws->focus == NULL)
1613 ws->focus = TAILQ_FIRST(&ws->winlist);
1614 if (ws->focus == win)
1616 if (cur_focus == win)
1617 focus_win(ws->focus);
1619 TAILQ_REMOVE(&ws->winlist, win, entry);
1620 set_win_state(win, WithdrawnState);
1627 enternotify(XEvent *e)
1629 XCrossingEvent *ev = &e->xcrossing;
1632 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1635 /* eat event(r) to prevent autofocus */
1640 if ((win = find_window(ev->window)) != NULL)
1647 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
1649 /* XXX this probably needs better handling now.
1650 if (ev->window == ev->root)
1654 * kill grab for now so that we can cut and paste , this screws up
1658 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1659 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1660 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1665 mappingnotify(XEvent *e)
1667 XMappingEvent *ev = &e->xmapping;
1669 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1671 XRefreshKeyboardMapping(ev);
1672 if (ev->request == MappingKeyboard)
1677 maprequest(XEvent *e)
1679 XMapRequestEvent *ev = &e->xmaprequest;
1680 XWindowAttributes wa;
1681 struct swm_region *r;
1683 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1684 e->xmaprequest.window);
1686 if (!XGetWindowAttributes(display, ev->window, &wa))
1688 if (wa.override_redirect)
1690 r = root_to_region(wa.root);
1691 manage_window(e->xmaprequest.window, r->ws);
1696 propertynotify(XEvent *e)
1698 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1699 e->xproperty.window);
1703 unmapnotify(XEvent *e)
1705 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1709 visibilitynotify(XEvent *e)
1712 struct swm_region *r;
1714 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
1715 e->xvisibility.window);
1716 if (e->xvisibility.state == VisibilityUnobscured)
1717 for (i = 0; i < ScreenCount(display); i++)
1718 TAILQ_FOREACH(r, &screens[i].rl, entry)
1719 if (e->xvisibility.window == r->bar_window)
1723 void (*handler[LASTEvent])(XEvent *) = {
1725 [KeyPress] = keypress,
1726 [ButtonPress] = buttonpress,
1727 [ConfigureRequest] = configurerequest,
1728 [ConfigureNotify] = configurenotify,
1729 [DestroyNotify] = destroynotify,
1730 [EnterNotify] = enternotify,
1731 [FocusIn] = focusin,
1732 [MappingNotify] = mappingnotify,
1733 [MapRequest] = maprequest,
1734 [PropertyNotify] = propertynotify,
1735 [UnmapNotify] = unmapnotify,
1736 [VisibilityNotify] = visibilitynotify,
1740 xerror_start(Display *d, XErrorEvent *ee)
1747 xerror(Display *d, XErrorEvent *ee)
1749 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1757 xerrorxlib = XSetErrorHandler(xerror_start);
1759 /* this causes an error if some other window manager is running */
1760 XSelectInput(display, DefaultRootWindow(display),
1761 SubstructureRedirectMask);
1762 XSync(display, False);
1766 XSetErrorHandler(xerror);
1767 XSync(display, False);
1776 unsigned char *p = NULL;
1777 unsigned long n, extra;
1780 astate = XInternAtom(display, "WM_STATE", False);
1781 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
1782 &real, &format, &n, &extra, (unsigned char **)&p);
1783 if (status != Success)
1792 new_region(struct swm_screen *s, struct workspace *ws,
1793 int x, int y, int w, int h)
1795 struct swm_region *r;
1797 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
1798 s->idx, x, y, w, h);
1800 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
1801 errx(1, "calloc: failed to allocate memory for screen");
1810 TAILQ_INSERT_TAIL(&s->rl, r, entry);
1817 #ifdef SWM_XRR_HAS_CRTC
1819 XRRScreenResources *sr;
1821 #endif /* SWM_XRR_HAS_CRTC */
1822 Window d1, d2, *wins = NULL;
1823 XWindowAttributes wa;
1824 struct swm_region *r;
1826 int errorbase, major, minor;
1827 int ncrtc = 0, w = 0;
1829 struct workspace *ws;
1831 if ((screens = calloc(ScreenCount(display),
1832 sizeof(struct swm_screen))) == NULL)
1833 errx(1, "calloc: screens");
1835 /* map physical screens */
1836 for (i = 0; i < ScreenCount(display); i++) {
1837 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
1839 TAILQ_INIT(&screens[i].rl);
1840 screens[i].root = RootWindow(display, i);
1842 /* set default colors */
1843 screens[i].color_focus = name_to_color("red");
1844 screens[i].color_unfocus = name_to_color("rgb:88/88/88");
1845 screens[i].bar_border = name_to_color("rgb:00/80/80");
1846 screens[i].bar_color = name_to_color("black");
1847 screens[i].bar_font_color = name_to_color("rgb:a0/a0/a0");
1849 /* init all workspaces */
1850 for (j = 0; j < SWM_WS_MAX; j++) {
1851 ws = &screens[i].ws[j];
1856 TAILQ_INIT(&ws->winlist);
1858 for (k = 0; layouts[k].l_stack != NULL; k++)
1859 if (layouts[k].l_config != NULL)
1860 layouts[k].l_config(ws,
1861 SWM_ARG_ID_STACKINIT);
1862 ws->cur_layout = &layouts[0];
1865 /* map virtual screens onto physical screens */
1866 screens[i].xrandr_support = XRRQueryExtension(display,
1867 &xrandr_eventbase, &errorbase);
1868 if (screens[i].xrandr_support)
1869 if (XRRQueryVersion(display, &major, &minor) &&
1871 screens[i].xrandr_support = 0;
1873 #if 0 /* not ready for dynamic screen changes */
1874 if (screens[i].xrandr_support)
1875 XRRSelectInput(display,
1877 RRScreenChangeNotifyMask);
1880 /* grab existing windows (before we build the bars)*/
1881 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
1884 #ifdef SWM_XRR_HAS_CRTC
1885 sr = XRRGetScreenResources(display, screens[i].root);
1887 new_region(&screens[i], &screens[i].ws[w],
1888 0, 0, DisplayWidth(display, i),
1889 DisplayHeight(display, i));
1893 for (c = 0; c < ncrtc; c++) {
1894 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
1895 if (ci->noutput == 0)
1898 if (ci != NULL && ci->mode == None)
1899 new_region(&screens[i], &screens[i].ws[w], 0, 0,
1900 DisplayWidth(display, i),
1901 DisplayHeight(display, i));
1903 new_region(&screens[i], &screens[i].ws[w],
1904 ci->x, ci->y, ci->width, ci->height);
1907 XRRFreeCrtcInfo(ci);
1908 XRRFreeScreenResources(sr);
1910 new_region(&screens[i], &screens[i].ws[w], 0, 0,
1911 DisplayWidth(display, i),
1912 DisplayHeight(display, i));
1913 #endif /* SWM_XRR_HAS_CRTC */
1915 /* attach windows to a region */
1916 /* normal windows */
1917 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
1918 errx(1, "no regions on screen %d", i);
1920 for (i = 0; i < no; i++) {
1921 XGetWindowAttributes(display, wins[i], &wa);
1922 if (!XGetWindowAttributes(display, wins[i], &wa) ||
1923 wa.override_redirect ||
1924 XGetTransientForHint(display, wins[i], &d1))
1927 if (wa.map_state == IsViewable ||
1928 getstate(wins[i]) == NormalState)
1929 manage_window(wins[i], r->ws);
1931 /* transient windows */
1932 for (i = 0; i < no; i++) {
1933 if (!XGetWindowAttributes(display, wins[i], &wa))
1936 if (XGetTransientForHint(display, wins[i], &d1) &&
1937 (wa.map_state == IsViewable || getstate(wins[i]) ==
1939 manage_window(wins[i], r->ws);
1949 main(int argc, char *argv[])
1952 char conf[PATH_MAX], *cfile = NULL;
1959 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1960 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1961 warnx("no locale support");
1963 if (!(display = XOpenDisplay(0)))
1964 errx(1, "can not open display");
1967 errx(1, "other wm running");
1969 astate = XInternAtom(display, "WM_STATE", False);
1971 /* look for local and global conf file */
1972 pwd = getpwuid(getuid());
1974 errx(1, "invalid user %d", getuid());
1978 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
1979 if (stat(conf, &sb) != -1) {
1980 if (S_ISREG(sb.st_mode))
1983 /* try global conf file */
1984 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
1985 if (!stat(conf, &sb))
1986 if (S_ISREG(sb.st_mode))
1993 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
1998 xfd = ConnectionNumber(display);
2001 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
2003 errx(1, "select failed");
2008 while(XPending(display)) {
2009 XNextEvent(display, &e);
2010 if (handler[e.type])
2011 handler[e.type](&e);
2015 XCloseDisplay(display);