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"
64 #include <sys/types.h>
67 #include <sys/queue.h>
68 #include <sys/param.h>
70 #include <X11/cursorfont.h>
71 #include <X11/keysym.h>
72 #include <X11/Xatom.h>
74 #include <X11/Xproto.h>
75 #include <X11/Xutil.h>
76 #include <X11/extensions/Xrandr.h>
79 # error XRandR versions less than 1.0 are not supported
84 #define SWM_XRR_HAS_CRTC
88 /* #define SWM_DEBUG */
90 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
91 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
92 #define SWM_D_MISC 0x0001
93 #define SWM_D_EVENT 0x0002
94 #define SWM_D_WS 0x0004
95 #define SWM_D_FOCUS 0x0008
96 #define SWM_D_MOVE 0x0010
97 #define SWM_D_STACK 0x0020
99 u_int32_t swm_debug = 0
108 #define DPRINTF(x...)
109 #define DNPRINTF(n,x...)
112 #define LENGTH(x) (sizeof x / sizeof x[0])
113 #define MODKEY Mod1Mask
114 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
116 #define X(r) (r)->g.x
117 #define Y(r) (r)->g.y
118 #define WIDTH(r) (r)->g.w
119 #define HEIGHT(r) (r)->g.h
123 int (*xerrorxlib)(Display *, XErrorEvent *);
126 int xrandr_eventbase;
127 int ignore_enter = 0;
128 unsigned int numlockmask = 0;
132 double dialog_ratio = .6;
140 char *bar_fonts[] = {
141 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
142 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
146 /* terminal + args */
147 char *spawn_term[] = { "xterm", NULL };
148 char *spawn_menu[] = { "dmenu_run", NULL };
150 /* layout manager data */
151 struct swm_geometry {
161 /* virtual "screens" */
163 TAILQ_ENTRY(swm_region) entry;
164 struct swm_geometry g;
166 struct workspace *ws; /* current workspace on this region */
167 struct swm_screen *s; /* screen idx */
169 TAILQ_HEAD(swm_region_list, swm_region);
173 TAILQ_ENTRY(ws_win) entry;
175 struct swm_geometry g;
179 struct workspace *ws; /* always valid */
180 struct swm_screen *s; /* always valid, never changes */
181 XWindowAttributes wa;
183 TAILQ_HEAD(ws_win_list, ws_win);
185 /* layout handlers */
187 void vertical_init(struct workspace *);
188 void vertical_resize(struct workspace *, int);
189 void vertical_stack(struct workspace *, struct swm_geometry *);
190 void horizontal_init(struct workspace *);
191 void horizontal_resize(struct workspace *, int);
192 void horizontal_stack(struct workspace *, struct swm_geometry *);
193 void max_init(struct workspace *);
194 void max_stack(struct workspace *, struct swm_geometry *);
197 void (*l_init)(struct workspace *); /* init/reset */
198 void (*l_stack)(struct workspace *, struct swm_geometry *);
199 void (*l_resize)(struct workspace *, int);
201 /* init stack, resize */
202 { vertical_init, vertical_stack, vertical_resize},
203 { horizontal_init, horizontal_stack, horizontal_resize},
204 { NULL, max_stack, NULL},
208 #define SWM_H_SLICE (32)
209 #define SWM_V_SLICE (32)
212 /* define work spaces */
214 int idx; /* workspace index */
215 int restack; /* restack on switch */
216 struct layout *cur_layout; /* current layout handlers */
217 struct ws_win *focus; /* may be NULL */
218 struct swm_region *r; /* may be NULL */
219 struct ws_win_list winlist; /* list of windows in ws */
223 int horizontal_msize;
228 /* physical screen mapping */
229 #define SWM_WS_MAX (10) /* XXX Too small? */
231 int idx; /* screen index */
232 struct swm_region_list rl; /* list of regions on this screen */
235 struct workspace ws[SWM_WS_MAX];
238 unsigned long bar_border;
239 unsigned long bar_color;
240 unsigned long bar_font_color;
241 unsigned long color_focus; /* XXX should this be per ws? */
242 unsigned long color_unfocus;
244 struct swm_screen *screens;
246 Window rootclick = 0;
248 struct ws_win *cur_focus = NULL;
250 /* args to functions */
253 #define SWM_ARG_ID_FOCUSNEXT (0)
254 #define SWM_ARG_ID_FOCUSPREV (1)
255 #define SWM_ARG_ID_FOCUSMAIN (2)
256 #define SWM_ARG_ID_SWAPNEXT (3)
257 #define SWM_ARG_ID_SWAPPREV (4)
258 #define SWM_ARG_ID_SWAPMAIN (5)
259 #define SWM_ARG_ID_MASTERSHRINK (6)
260 #define SWM_ARG_ID_MASTERGROW (7)
265 name_to_color(char *colorname)
269 XColor screen_def, exact_def;
270 unsigned long result = 0;
271 char cname[32] = "#";
273 cmap = DefaultColormap(display, screens[0].idx);
274 status = XAllocNamedColor(display, cmap, colorname,
275 &screen_def, &exact_def);
277 strlcat(cname, colorname + 2, sizeof cname - 1);
278 status = XAllocNamedColor(display, cmap, cname, &screen_def,
282 result = screen_def.pixel;
284 fprintf(stderr, "color '%s' not found.\n", colorname);
289 /* conf file stuff */
290 #define SWM_CONF_WS "\n= \t"
291 #define SWM_CONF_FILE "scrotwm.conf"
293 conf_load(char *filename)
296 char *line, *cp, *var, *val;
297 size_t len, lineno = 0;
300 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
302 if (filename == NULL)
305 if ((config = fopen(filename, "r")) == NULL)
309 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
313 cp += (long)strspn(cp, SWM_CONF_WS);
319 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
321 cp += (long)strspn(cp, SWM_CONF_WS);
322 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
325 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
328 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
329 bar_enabled = atoi(val);
330 else if (!strncmp(var, "bar_border",
331 strlen("bar_border")))
332 for (i = 0; i < ScreenCount(display); i++)
333 screens[i].bar_border = name_to_color(val);
334 else if (!strncmp(var, "bar_color",
335 strlen("bar_color")))
336 for (i = 0; i < ScreenCount(display); i++)
337 screens[i].bar_color = name_to_color(val);
338 else if (!strncmp(var, "bar_font_color",
339 strlen("bar_font_color")))
340 for (i = 0; i < ScreenCount(display); i++)
341 screens[i].bar_font_color = name_to_color(val);
342 else if (!strncmp(var, "bar_font", strlen("bar_font")))
343 asprintf(&bar_fonts[0], "%s", val);
349 if (!strncmp(var, "color_focus", strlen("color_focus")))
350 for (i = 0; i < ScreenCount(display); i++)
351 screens[i].color_focus = name_to_color(val);
352 else if (!strncmp(var, "color_unfocus",
353 strlen("color_unfocus")))
354 for (i = 0; i < ScreenCount(display); i++)
355 screens[i].color_unfocus = name_to_color(val);
361 if (!strncmp(var, "dialog_ratio",
362 strlen("dialog_ratio"))) {
363 dialog_ratio = atof(val);
364 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
371 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
372 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
383 errx(1, "invalid conf file entry: %s=%s", var, val);
387 bar_print(struct swm_region *r)
392 if (bar_enabled == 0)
396 XSetForeground(display, bar_gc, r->s->bar_color);
397 XDrawString(display, r->bar_window,
398 bar_gc, 4, bar_fs->ascent, bar_text, strlen(bar_text));
402 localtime_r(&tmt, &tm);
403 strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
404 XSetForeground(display, bar_gc, r->s->bar_font_color);
405 XDrawString(display, r->bar_window, bar_gc, 4,
406 bar_fs->ascent, bar_text, strlen(bar_text));
407 XSync(display, False);
415 /* XXX yeah yeah byte me */
417 bar_print(cur_focus->ws->r);
421 bar_toggle(struct swm_region *r, union arg *args)
423 struct swm_region *tmpr;
426 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
429 for (i = 0; i < ScreenCount(display); i++)
430 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
431 XUnmapWindow(display, tmpr->bar_window);
433 for (i = 0; i < ScreenCount(display); i++)
434 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
435 XMapRaised(display, tmpr->bar_window);
437 bar_enabled = !bar_enabled;
438 XSync(display, False);
439 for (i = 0; i < ScreenCount(display); i++)
440 for (j = 0; j < SWM_WS_MAX; j++)
441 screens[i].ws[j].restack = 1;
444 /* must be after stack */
445 for (i = 0; i < ScreenCount(display); i++)
446 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
451 bar_setup(struct swm_region *r)
455 for (i = 0; bar_fonts[i] != NULL; i++) {
456 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
460 if (bar_fonts[i] == NULL)
461 errx(1, "couldn't load font");
462 bar_height = bar_fs->ascent + bar_fs->descent + 3;
464 r->bar_window = XCreateSimpleWindow(display,
465 r->s->root, X(r), Y(r), WIDTH(r), bar_height - 2,
466 1, r->s->bar_border, r->s->bar_color);
467 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
468 XSetFont(display, bar_gc, bar_fs->fid);
469 XSelectInput(display, r->bar_window, VisibilityChangeMask);
471 XMapRaised(display, r->bar_window);
472 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
474 if (signal(SIGALRM, bar_signal) == SIG_ERR)
475 err(1, "could not install bar_signal");
480 config_win(struct ws_win *win)
484 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
485 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
486 ce.type = ConfigureNotify;
487 ce.display = display;
493 ce.height = win->g.h;
494 ce.border_width = 1; /* XXX store this! */
496 ce.override_redirect = False;
497 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
501 count_win(struct workspace *ws, int count_transient)
506 TAILQ_FOREACH(win, &ws->winlist, entry) {
507 if (count_transient == 0 && win->floating)
509 if (count_transient == 0 && win->transient)
513 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
519 quit(struct swm_region *r, union arg *args)
521 DNPRINTF(SWM_D_MISC, "quit\n");
526 restart(struct swm_region *r, union arg *args)
528 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
530 XCloseDisplay(display);
531 execvp(start_argv[0], start_argv);
532 fprintf(stderr, "execvp failed\n");
538 root_to_region(Window root)
540 struct swm_region *r;
545 for (i = 0; i < ScreenCount(display); i++)
546 if (screens[i].root == root)
549 if (rootclick != root && /* if root was just clicked in, use cursor */
550 cur_focus && cur_focus->ws->r && cur_focus->s == &screens[i])
551 r = cur_focus->ws->r;
553 if (XQueryPointer(display, screens[i].root,
554 &rr, &cr, &x, &y, &wx, &wy, &mask) == False) {
555 r = TAILQ_FIRST(&screens[i].rl);
557 TAILQ_FOREACH(r, &screens[i].rl, entry) {
558 if (x > X(r) && x < X(r) + WIDTH(r) &&
559 y > Y(r) && y < Y(r) + HEIGHT(r))
564 r = TAILQ_FIRST(&screens[i].rl);
571 find_window(Window id)
576 for (i = 0; i < ScreenCount(display); i++)
577 for (j = 0; j < SWM_WS_MAX; j++)
578 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
585 spawn(struct swm_region *r, union arg *args)
587 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
589 * The double-fork construct avoids zombie processes and keeps the code
590 * clean from stupid signal handlers.
595 close(ConnectionNumber(display));
597 execvp(args->argv[0], args->argv);
598 fprintf(stderr, "execvp failed\n");
607 unfocus_win(struct ws_win *win)
609 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
610 if (win->ws->r && win->focus)
611 XSetWindowBorder(display, win->id,
612 win->ws->r->s->color_unfocus);
614 if (win->ws->focus == win)
615 win->ws->focus = NULL;
616 if (cur_focus == win)
622 focus_win(struct ws_win *win)
624 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
628 win->ws->focus = win;
629 if (win->ws->r != NULL) {
630 if (cur_focus && cur_focus != win)
631 unfocus_win(cur_focus);
634 XSetWindowBorder(display, win->id,
635 win->ws->r->s->color_focus);
637 XSetInputFocus(display, win->id,
638 RevertToPointerRoot, CurrentTime);
643 switchws(struct swm_region *r, union arg *args)
646 struct swm_region *this_r, *other_r;
648 struct workspace *new_ws, *old_ws;
652 new_ws = &this_r->s->ws[wsid];
654 DNPRINTF(SWM_D_WS, "switchws screen %d region %dx%d+%d+%d: "
655 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
658 if (new_ws == old_ws)
663 /* if the other workspace is hidden, switch windows */
665 /* map new window first to prevent ugly blinking */
666 TAILQ_FOREACH(win, &new_ws->winlist, entry)
667 XMapRaised(display, win->id);
669 TAILQ_FOREACH(win, &old_ws->winlist, entry)
670 XUnmapWindow(display, win->id);
675 other_r->ws = old_ws;
684 focus_win(new_ws->focus);
689 swapwin(struct swm_region *r, union arg *args)
691 struct ws_win *target;
692 struct ws_win_list *wl;
695 DNPRINTF(SWM_D_WS, "swapwin id %d "
696 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
697 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
698 if (cur_focus == NULL)
701 wl = &cur_focus->ws->winlist;
704 case SWM_ARG_ID_SWAPPREV:
705 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
706 TAILQ_REMOVE(wl, cur_focus, entry);
708 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
710 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
712 case SWM_ARG_ID_SWAPNEXT:
713 target = TAILQ_NEXT(cur_focus, entry);
714 TAILQ_REMOVE(wl, cur_focus, entry);
716 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
718 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
720 case SWM_ARG_ID_SWAPMAIN:
721 target = TAILQ_FIRST(wl);
722 if (target == cur_focus)
724 TAILQ_REMOVE(wl, target, entry);
725 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
726 TAILQ_REMOVE(wl, cur_focus, entry);
727 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
730 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
739 focus(struct swm_region *r, union arg *args)
741 struct ws_win *winfocus, *winlostfocus;
742 struct ws_win_list *wl;
744 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
745 if (cur_focus == NULL)
748 wl = &cur_focus->ws->winlist;
750 winlostfocus = cur_focus;
753 case SWM_ARG_ID_FOCUSPREV:
754 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
755 if (winfocus == NULL)
756 winfocus = TAILQ_LAST(wl, ws_win_list);
759 case SWM_ARG_ID_FOCUSNEXT:
760 winfocus = TAILQ_NEXT(cur_focus, entry);
761 if (winfocus == NULL)
762 winfocus = TAILQ_FIRST(wl);
765 case SWM_ARG_ID_FOCUSMAIN:
766 winfocus = TAILQ_FIRST(wl);
773 if (winfocus == winlostfocus)
777 XSync(display, False);
781 cycle_layout(struct swm_region *r, union arg *args)
783 struct workspace *ws = r->ws;
785 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
788 if (ws->cur_layout->l_stack == NULL)
789 ws->cur_layout = &layouts[0];
796 resize_master(struct swm_region *r, union arg *args)
798 struct workspace *ws = r->ws;
800 DNPRINTF(SWM_D_STACK, "resize_master for workspace %d (id %d\n",
803 if (ws->cur_layout->l_resize != NULL)
804 ws->cur_layout->l_resize(ws, args->id);
808 stack_reset(struct swm_region *r, union arg *args)
810 struct workspace *ws = r->ws;
812 DNPRINTF(SWM_D_STACK, "stack_reset: ws %d\n", ws->idx);
814 if (ws->cur_layout->l_init != NULL) {
815 ws->cur_layout->l_init(ws);
822 struct swm_geometry g;
823 struct swm_region *r;
826 DNPRINTF(SWM_D_STACK, "stack\n");
828 for (i = 0; i < ScreenCount(display); i++) {
830 TAILQ_FOREACH(r, &screens[i].rl, entry) {
831 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
832 "(screen %d, region %d)\n", r->ws->idx, i, j++);
834 /* start with screen geometry, adjust for bar */
844 r->ws->cur_layout->l_stack(r->ws, &g);
847 XSync(display, False);
851 stack_floater(struct ws_win *win, struct swm_region *r)
856 bzero(&wc, sizeof wc);
857 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
859 if (win->transient) {
860 win->g.w = (double)WIDTH(r) * dialog_ratio;
861 win->g.h = (double)HEIGHT(r) * dialog_ratio;
864 wc.height = win->g.h;
865 wc.x = (WIDTH(r) - win->g.w) / 2;
866 wc.y = (HEIGHT(r) - win->g.h) / 2;
868 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
869 win->id, wc.x, wc.y, wc.width, wc.height);
871 XConfigureWindow(display, win->id, mask, &wc);
874 int vertical_msize[SWM_WS_MAX];
877 vertical_init(struct workspace *ws)
879 DNPRINTF(SWM_D_MISC, "vertical_init: workspace: %d\n", ws->idx);
881 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
885 vertical_resize(struct workspace *ws, int id)
887 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
890 case SWM_ARG_ID_MASTERSHRINK:
891 ws->l_state.vertical_msize--;
892 if (ws->l_state.vertical_msize < 1)
893 ws->l_state.vertical_msize = 1;
895 case SWM_ARG_ID_MASTERGROW:
896 ws->l_state.vertical_msize++;
897 if (ws->l_state.vertical_msize > SWM_V_SLICE - 1)
898 ws->l_state.vertical_msize = SWM_V_SLICE - 1;
907 vertical_stack(struct workspace *ws, struct swm_geometry *g) {
909 struct swm_geometry gg = *g;
910 struct ws_win *win, *winfocus;
911 int i, hrh, winno, main_width;
914 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
916 if ((winno = count_win(ws, 0)) == 0)
919 if (ws->focus == NULL)
920 ws->focus = TAILQ_FIRST(&ws->winlist);
921 winfocus = cur_focus ? cur_focus : ws->focus;
924 main_width = (g->w / SWM_V_SLICE) *
925 ws->l_state.vertical_msize;
930 hrh = g->h / (winno - 1);
935 TAILQ_FOREACH(win, &ws->winlist, entry) {
937 gg.x += main_width + 2;
938 gg.w = g->w - (main_width + 2);
940 if (i != 0 && hrh != 0) {
941 /* correct the last window for lost pixels */
942 if (win == TAILQ_LAST(&ws->winlist, ws_win_list)) {
943 gg.h = hrh + (g->h - (i * hrh));
947 /* leave first right hand window at y = 0 */
953 if (win->transient != 0 || win->floating != 0)
954 stack_floater(win, ws->r);
956 bzero(&wc, sizeof wc);
958 win->g.x = wc.x = gg.x;
959 win->g.y = wc.y = gg.y;
960 win->g.w = wc.width = gg.w;
961 win->g.h = wc.height = gg.h;
962 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
963 XConfigureWindow(display, win->id, mask, &wc);
966 XMapRaised(display, win->id);
971 focus_win(winfocus); /* has to be done outside of the loop */
976 horizontal_init(struct workspace *ws)
978 DNPRINTF(SWM_D_STACK, "horizontal_init: workspace: %d\n", ws->idx);
980 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
984 horizontal_resize(struct workspace *ws, int id)
986 DNPRINTF(SWM_D_STACK, "horizontal_resize: workspace: %d\n", ws->idx);
989 case SWM_ARG_ID_MASTERSHRINK:
990 ws->l_state.horizontal_msize--;
991 if (ws->l_state.horizontal_msize < 1)
992 ws->l_state.horizontal_msize = 1;
994 case SWM_ARG_ID_MASTERGROW:
995 ws->l_state.horizontal_msize++;
996 if (ws->l_state.horizontal_msize > SWM_H_SLICE - 1)
997 ws->l_state.horizontal_msize = SWM_H_SLICE - 1;
1006 horizontal_stack(struct workspace *ws, struct swm_geometry *g) {
1008 struct swm_geometry gg = *g;
1009 struct ws_win *win, *winfocus;
1010 int i, hrw, winno, main_height;
1013 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
1015 if ((winno = count_win(ws, 0)) == 0)
1018 if (ws->focus == NULL)
1019 ws->focus = TAILQ_FIRST(&ws->winlist);
1020 winfocus = cur_focus ? cur_focus : ws->focus;
1023 main_height = (g->h / SWM_H_SLICE) *
1024 ws->l_state.horizontal_msize;
1029 hrw = g->w / (winno - 1);
1034 TAILQ_FOREACH(win, &ws->winlist, entry) {
1036 gg.y += main_height + 2;
1037 gg.h = g->h - (main_height + 2);
1039 if (i != 0 && hrw != 0) {
1040 /* correct the last window for lost pixels */
1041 if (win == TAILQ_LAST(&ws->winlist, ws_win_list)) {
1042 gg.w = hrw + (g->w - (i * hrw));
1046 /* leave first bottom window at x = 0 */
1052 if (win->transient != 0 || win->floating != 0)
1053 stack_floater(win, ws->r);
1055 bzero(&wc, sizeof wc);
1056 wc.border_width = 1;
1057 win->g.x = wc.x = gg.x;
1058 win->g.y = wc.y = gg.y;
1059 win->g.w = wc.width = gg.w;
1060 win->g.h = wc.height = gg.h;
1061 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1062 XConfigureWindow(display, win->id, mask, &wc);
1065 XMapRaised(display, win->id);
1070 focus_win(winfocus); /* this has to be done outside of the loop */
1073 /* fullscreen view */
1075 max_stack(struct workspace *ws, struct swm_geometry *g) {
1077 struct swm_geometry gg = *g;
1078 struct ws_win *win, *winfocus;
1081 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1083 if (count_win(ws, 0) == 0)
1086 if (ws->focus == NULL)
1087 ws->focus = TAILQ_FIRST(&ws->winlist);
1088 winfocus = cur_focus ? cur_focus : ws->focus;
1090 TAILQ_FOREACH(win, &ws->winlist, entry) {
1091 if (win->transient != 0 || win->floating != 0) {
1092 if (win == ws->focus) {
1094 stack_floater(win, ws->r);
1095 XMapRaised(display, win->id);
1097 XUnmapWindow(display, win->id);
1099 bzero(&wc, sizeof wc);
1100 wc.border_width = 1;
1101 win->g.x = wc.x = gg.x;
1102 win->g.y = wc.y = gg.y;
1103 win->g.w = wc.width = gg.w;
1104 win->g.h = wc.height = gg.h;
1105 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1106 XConfigureWindow(display, win->id, mask, &wc);
1108 if (win == ws->focus) {
1109 XMapRaised(display, win->id);
1111 XUnmapWindow(display, win->id);
1116 focus_win(winfocus); /* has to be done outside of the loop */
1120 send_to_ws(struct swm_region *r, union arg *args)
1122 int wsid = args->id;
1123 struct ws_win *win = cur_focus;
1124 struct workspace *ws, *nws;
1126 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1129 nws = &win->s->ws[wsid];
1131 XUnmapWindow(display, win->id);
1133 /* find a window to focus */
1134 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1135 if (ws->focus == NULL)
1136 ws->focus = TAILQ_FIRST(&ws->winlist);
1137 if (ws->focus == win)
1140 TAILQ_REMOVE(&ws->winlist, win, entry);
1142 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1145 if (count_win(nws, 1) == 1)
1153 /* key definitions */
1157 void (*func)(struct swm_region *r, union arg *);
1160 /* modifier key function argument */
1161 { MODKEY, XK_space, cycle_layout, {0} },
1162 { MODKEY | ShiftMask, XK_space, stack_reset, {0} },
1163 { MODKEY, XK_h, resize_master, {.id = SWM_ARG_ID_MASTERSHRINK} },
1164 { MODKEY, XK_l, resize_master, {.id = SWM_ARG_ID_MASTERGROW} },
1165 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1166 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1167 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1168 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1169 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1170 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1171 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
1172 { MODKEY | ShiftMask, XK_q, quit, {0} },
1173 { MODKEY, XK_q, restart, {0} },
1174 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1175 { MODKEY, XK_1, switchws, {.id = 0} },
1176 { MODKEY, XK_2, switchws, {.id = 1} },
1177 { MODKEY, XK_3, switchws, {.id = 2} },
1178 { MODKEY, XK_4, switchws, {.id = 3} },
1179 { MODKEY, XK_5, switchws, {.id = 4} },
1180 { MODKEY, XK_6, switchws, {.id = 5} },
1181 { MODKEY, XK_7, switchws, {.id = 6} },
1182 { MODKEY, XK_8, switchws, {.id = 7} },
1183 { MODKEY, XK_9, switchws, {.id = 8} },
1184 { MODKEY, XK_0, switchws, {.id = 9} },
1185 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1186 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1187 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1188 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1189 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1190 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1191 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1192 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1193 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1194 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1195 { MODKEY, XK_b, bar_toggle, {0} },
1196 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1197 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1201 updatenumlockmask(void)
1204 XModifierKeymap *modmap;
1206 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1208 modmap = XGetModifierMapping(display);
1209 for (i = 0; i < 8; i++)
1210 for (j = 0; j < modmap->max_keypermod; j++)
1211 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1212 == XKeysymToKeycode(display, XK_Num_Lock))
1213 numlockmask = (1 << i);
1215 XFreeModifiermap(modmap);
1221 unsigned int i, j, k;
1223 unsigned int modifiers[] =
1224 { 0, LockMask, numlockmask, numlockmask | LockMask };
1226 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1227 updatenumlockmask();
1229 for (k = 0; k < ScreenCount(display); k++) {
1230 if (TAILQ_EMPTY(&screens[k].rl))
1232 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1233 for(i = 0; i < LENGTH(keys); i++) {
1234 if((code = XKeysymToKeycode(display, keys[i].keysym)))
1235 for(j = 0; j < LENGTH(modifiers); j++)
1236 XGrabKey(display, code,
1237 keys[i].mod | modifiers[j],
1238 screens[k].root, True,
1239 GrabModeAsync, GrabModeAsync);
1246 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1254 XKeyEvent *ev = &e->xkey;
1256 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1258 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1259 for (i = 0; i < LENGTH(keys); i++)
1260 if (keysym == keys[i].keysym
1261 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1263 keys[i].func(root_to_region(ev->root),
1268 buttonpress(XEvent *e)
1270 XButtonPressedEvent *ev = &e->xbutton;
1271 #ifdef SWM_CLICKTOFOCUS
1273 struct workspace *ws;
1274 struct swm_region *r;
1277 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1279 if (ev->window == ev->root) {
1280 rootclick = ev->root;
1283 if (ev->window == cur_focus->id)
1285 #ifdef SWM_CLICKTOFOCUS
1286 r = root_to_region(ev->root);
1288 TAILQ_FOREACH(win, &ws->winlist, entry)
1289 if (win->id == ev->window) {
1290 /* focus in the clicked window */
1291 XSetWindowBorder(display, ev->window, 0xff0000);
1292 XSetWindowBorder(display, ws->focus->id, 0x888888);
1293 XSetInputFocus(display, ev->window, RevertToPointerRoot,
1296 XSync(display, False);
1303 set_win_state(struct ws_win *win, long state)
1305 long data[] = {state, None};
1307 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1309 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1310 (unsigned char *)data, 2);
1314 manage_window(Window id, struct workspace *ws)
1320 TAILQ_FOREACH(win, &ws->winlist, entry) {
1322 return (win); /* already being managed */
1325 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1326 errx(1, "calloc: failed to allocate memory for new window");
1330 win->s = ws->r->s; /* this never changes */
1331 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
1333 /* make new win focused */
1336 XGetTransientForHint(display, win->id, &trans);
1338 win->transient = trans;
1339 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1340 (unsigned)win->id, win->transient);
1342 XGetWindowAttributes(display, id, &win->wa);
1343 win->g.w = win->wa.width;
1344 win->g.h = win->wa.height;
1345 win->g.x = win->wa.x;
1346 win->g.y = win->wa.y;
1348 /* XXX make this a table */
1349 bzero(&ch, sizeof ch);
1350 if (XGetClassHint(display, win->id, &ch)) {
1351 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1352 if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
1356 XFree(ch.res_class);
1361 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
1362 PropertyChangeMask | StructureNotifyMask);
1364 set_win_state(win, NormalState);
1370 configurerequest(XEvent *e)
1372 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1377 if ((win = find_window(ev->window)) == NULL)
1381 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1383 bzero(&wc, sizeof wc);
1386 wc.width = ev->width;
1387 wc.height = ev->height;
1388 wc.border_width = ev->border_width;
1389 wc.sibling = ev->above;
1390 wc.stack_mode = ev->detail;
1391 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1393 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1395 if (win->floating) {
1396 if (ev->value_mask & CWX)
1398 if (ev->value_mask & CWY)
1400 if (ev->value_mask & CWWidth)
1401 win->g.w = ev->width;
1402 if (ev->value_mask & CWHeight)
1403 win->g.h = ev->height;
1404 if (win->ws->r != NULL) {
1405 /* this seems to be full screen */
1406 if (win->g.w > WIDTH(win->ws->r)) {
1411 if (win->g.h > HEIGHT(win->ws->r)) {
1417 if ((ev->value_mask & (CWX|CWY)) &&
1418 !(ev->value_mask & (CWWidth|CWHeight)))
1420 XMoveResizeWindow(display, win->id,
1421 win->g.x, win->g.y, win->g.w, win->g.h);
1428 configurenotify(XEvent *e)
1430 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1431 e->xconfigure.window);
1435 destroynotify(XEvent *e)
1438 XDestroyWindowEvent *ev = &e->xdestroywindow;
1440 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1442 if ((win = find_window(ev->window)) != NULL) {
1443 struct workspace *ws = win->ws;
1444 /* find a window to focus */
1445 if (ws->focus == win)
1446 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1447 if (ws->focus == NULL)
1448 ws->focus = TAILQ_FIRST(&ws->winlist);
1449 if (ws->focus == win)
1451 if (cur_focus == win) {
1452 if (ws->focus == NULL)
1453 unfocus_win(win); /* XXX focus another ws? */
1455 focus_win(ws->focus);
1458 TAILQ_REMOVE(&ws->winlist, win, entry);
1459 set_win_state(win, WithdrawnState);
1466 enternotify(XEvent *e)
1468 XCrossingEvent *ev = &e->xcrossing;
1472 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1474 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
1475 ev->window != ev->root)
1478 /* eat event(r) to prevent autofocus */
1482 /* brute force for now */
1483 for (i = 0; i < ScreenCount(display); i++) {
1484 for (j = 0; j < SWM_WS_MAX; j++) {
1485 TAILQ_FOREACH(win, &screens[i].ws[j].winlist , entry) {
1486 if (win->id == ev->window)
1496 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
1498 /* XXX this probably needs better handling now.
1499 if (ev->window == ev->root)
1503 * kill grab for now so that we can cut and paste , this screws up
1507 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1508 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1509 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1514 mappingnotify(XEvent *e)
1516 XMappingEvent *ev = &e->xmapping;
1518 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1520 XRefreshKeyboardMapping(ev);
1521 if (ev->request == MappingKeyboard)
1526 maprequest(XEvent *e)
1528 XMapRequestEvent *ev = &e->xmaprequest;
1529 XWindowAttributes wa;
1530 struct swm_region *r;
1532 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1533 e->xmaprequest.window);
1535 if (!XGetWindowAttributes(display, ev->window, &wa))
1537 if (wa.override_redirect)
1539 r = root_to_region(wa.root);
1540 manage_window(e->xmaprequest.window, r->ws);
1545 propertynotify(XEvent *e)
1547 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1548 e->xproperty.window);
1552 unmapnotify(XEvent *e)
1554 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1558 visibilitynotify(XEvent *e)
1561 struct swm_region *r;
1563 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
1564 e->xvisibility.window);
1565 if (e->xvisibility.state == VisibilityUnobscured)
1566 for (i = 0; i < ScreenCount(display); i++)
1567 TAILQ_FOREACH(r, &screens[i].rl, entry)
1568 if (e->xvisibility.window == r->bar_window)
1572 void (*handler[LASTEvent])(XEvent *) = {
1574 [KeyPress] = keypress,
1575 [ButtonPress] = buttonpress,
1576 [ConfigureRequest] = configurerequest,
1577 [ConfigureNotify] = configurenotify,
1578 [DestroyNotify] = destroynotify,
1579 [EnterNotify] = enternotify,
1580 [FocusIn] = focusin,
1581 [MappingNotify] = mappingnotify,
1582 [MapRequest] = maprequest,
1583 [PropertyNotify] = propertynotify,
1584 [UnmapNotify] = unmapnotify,
1585 [VisibilityNotify] = visibilitynotify,
1589 xerror_start(Display *d, XErrorEvent *ee)
1596 xerror(Display *d, XErrorEvent *ee)
1598 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1606 xerrorxlib = XSetErrorHandler(xerror_start);
1608 /* this causes an error if some other window manager is running */
1609 XSelectInput(display, DefaultRootWindow(display),
1610 SubstructureRedirectMask);
1611 XSync(display, False);
1615 XSetErrorHandler(xerror);
1616 XSync(display, False);
1625 unsigned char *p = NULL;
1626 unsigned long n, extra;
1629 astate = XInternAtom(display, "WM_STATE", False);
1630 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
1631 &real, &format, &n, &extra, (unsigned char **)&p);
1632 if (status != Success)
1641 new_region(struct swm_screen *s, struct workspace *ws,
1642 int x, int y, int w, int h)
1644 struct swm_region *r;
1646 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
1647 s->idx, x, y, w, h);
1649 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
1650 errx(1, "calloc: failed to allocate memory for screen");
1659 TAILQ_INSERT_TAIL(&s->rl, r, entry);
1666 #ifdef SWM_XRR_HAS_CRTC
1668 XRRScreenResources *sr;
1670 #endif /* SWM_XRR_HAS_CRTC */
1671 Window d1, d2, *wins = NULL;
1672 XWindowAttributes wa;
1673 struct swm_region *r;
1675 int errorbase, major, minor;
1676 int ncrtc = 0, w = 0;
1678 struct workspace *ws;
1680 if ((screens = calloc(ScreenCount(display),
1681 sizeof(struct swm_screen))) == NULL)
1682 errx(1, "calloc: screens");
1684 /* map physical screens */
1685 for (i = 0; i < ScreenCount(display); i++) {
1686 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
1688 TAILQ_INIT(&screens[i].rl);
1689 screens[i].root = RootWindow(display, i);
1690 XGetWindowAttributes(display, screens[i].root, &wa);
1691 XSelectInput(display, screens[i].root,
1692 ButtonPressMask | wa.your_event_mask);
1694 /* set default colors */
1695 screens[i].color_focus = name_to_color("red");
1696 screens[i].color_unfocus = name_to_color("rgb:88/88/88");
1697 screens[i].bar_border = name_to_color("rgb:00/80/80");
1698 screens[i].bar_color = name_to_color("black");
1699 screens[i].bar_font_color = name_to_color("rgb:a0/a0/a0");
1701 /* init all workspaces */
1702 for (j = 0; j < SWM_WS_MAX; j++) {
1703 ws = &screens[i].ws[j];
1708 TAILQ_INIT(&ws->winlist);
1710 for (k = 0; layouts[k].l_stack != NULL; k++)
1711 if (layouts[k].l_init != NULL)
1712 layouts[k].l_init(ws);
1713 ws->cur_layout = &layouts[0];
1716 /* map virtual screens onto physical screens */
1717 screens[i].xrandr_support = XRRQueryExtension(display,
1718 &xrandr_eventbase, &errorbase);
1719 if (screens[i].xrandr_support)
1720 if (XRRQueryVersion(display, &major, &minor) &&
1722 screens[i].xrandr_support = 0;
1724 #if 0 /* not ready for dynamic screen changes */
1725 if (screens[i].xrandr_support)
1726 XRRSelectInput(display,
1728 RRScreenChangeNotifyMask);
1731 /* grab existing windows (before we build the bars)*/
1732 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
1735 #ifdef SWM_XRR_HAS_CRTC
1736 sr = XRRGetScreenResources(display, screens[i].root);
1738 new_region(&screens[i], &screens[i].ws[w],
1739 0, 0, DisplayWidth(display, i),
1740 DisplayHeight(display, i));
1744 for (c = 0; c < ncrtc; c++) {
1745 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
1746 if (ci->noutput == 0)
1749 if (ci != NULL && ci->mode == None)
1750 new_region(&screens[i], &screens[i].ws[w], 0, 0,
1751 DisplayWidth(display, i),
1752 DisplayHeight(display, i));
1754 new_region(&screens[i], &screens[i].ws[w],
1755 ci->x, ci->y, ci->width, ci->height);
1758 XRRFreeCrtcInfo(ci);
1759 XRRFreeScreenResources(sr);
1761 new_region(&screens[i], &screens[i].ws[w], 0, 0,
1762 DisplayWidth(display, i),
1763 DisplayHeight(display, i));
1764 #endif /* SWM_XRR_HAS_CRTC */
1766 /* attach windows to a region */
1767 /* normal windows */
1768 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
1769 errx(1, "no regions on screen %d", i);
1771 for (i = 0; i < no; i++) {
1772 XGetWindowAttributes(display, wins[i], &wa);
1773 if (!XGetWindowAttributes(display, wins[i], &wa) ||
1774 wa.override_redirect ||
1775 XGetTransientForHint(display, wins[i], &d1))
1778 if (wa.map_state == IsViewable ||
1779 getstate(wins[i]) == NormalState)
1780 manage_window(wins[i], r->ws);
1782 /* transient windows */
1783 for (i = 0; i < no; i++) {
1784 if (!XGetWindowAttributes(display, wins[i], &wa))
1787 if (XGetTransientForHint(display, wins[i], &d1) &&
1788 (wa.map_state == IsViewable || getstate(wins[i]) ==
1790 manage_window(wins[i], r->ws);
1800 main(int argc, char *argv[])
1803 char conf[PATH_MAX], *cfile = NULL;
1808 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1809 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1810 warnx("no locale support");
1812 if (!(display = XOpenDisplay(0)))
1813 errx(1, "can not open display");
1816 errx(1, "other wm running");
1818 astate = XInternAtom(display, "WM_STATE", False);
1820 /* look for local and global conf file */
1821 pwd = getpwuid(getuid());
1823 errx(1, "invalid user %d", getuid());
1825 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
1826 if (stat(conf, &sb) != -1) {
1827 if (S_ISREG(sb.st_mode))
1830 /* try global conf file */
1831 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
1832 if (!stat(conf, &sb))
1833 if (S_ISREG(sb.st_mode))
1841 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
1847 XNextEvent(display, &e);
1848 if (handler[e.type])
1849 handler[e.type](&e);
1852 XCloseDisplay(display);