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)
662 fprintf(stderr, "switchws: other_r %p\n", other_r);
664 /* if the other workspace is hidden, switch windows */
666 /* map new window first to prevent ugly blinking */
667 TAILQ_FOREACH(win, &new_ws->winlist, entry)
668 XMapRaised(display, win->id);
670 TAILQ_FOREACH(win, &old_ws->winlist, entry)
671 XUnmapWindow(display, win->id);
676 other_r->ws = old_ws;
685 focus_win(new_ws->focus);
690 swapwin(struct swm_region *r, union arg *args)
692 struct ws_win *target;
693 struct ws_win_list *wl;
696 DNPRINTF(SWM_D_WS, "swapwin id %d "
697 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
698 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
699 if (cur_focus == NULL)
702 wl = &cur_focus->ws->winlist;
705 case SWM_ARG_ID_SWAPPREV:
706 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
707 TAILQ_REMOVE(wl, cur_focus, entry);
709 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
711 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
713 case SWM_ARG_ID_SWAPNEXT:
714 target = TAILQ_NEXT(cur_focus, entry);
715 TAILQ_REMOVE(wl, cur_focus, entry);
717 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
719 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
721 case SWM_ARG_ID_SWAPMAIN:
722 target = TAILQ_FIRST(wl);
723 if (target == cur_focus)
725 TAILQ_REMOVE(wl, target, entry);
726 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
727 TAILQ_REMOVE(wl, cur_focus, entry);
728 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
731 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
740 focus(struct swm_region *r, union arg *args)
742 struct ws_win *winfocus, *winlostfocus;
743 struct ws_win_list *wl;
745 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
746 if (cur_focus == NULL)
749 wl = &cur_focus->ws->winlist;
751 winlostfocus = cur_focus;
754 case SWM_ARG_ID_FOCUSPREV:
755 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
756 if (winfocus == NULL)
757 winfocus = TAILQ_LAST(wl, ws_win_list);
760 case SWM_ARG_ID_FOCUSNEXT:
761 winfocus = TAILQ_NEXT(cur_focus, entry);
762 if (winfocus == NULL)
763 winfocus = TAILQ_FIRST(wl);
766 case SWM_ARG_ID_FOCUSMAIN:
767 winfocus = TAILQ_FIRST(wl);
774 if (winfocus == winlostfocus)
778 XSync(display, False);
782 cycle_layout(struct swm_region *r, union arg *args)
784 struct workspace *ws = r->ws;
786 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
789 if (ws->cur_layout->l_stack == NULL)
790 ws->cur_layout = &layouts[0];
797 resize_master(struct swm_region *r, union arg *args)
799 struct workspace *ws = r->ws;
801 DNPRINTF(SWM_D_STACK, "resize_master for workspace %d (id %d\n",
804 if (ws->cur_layout->l_resize != NULL)
805 ws->cur_layout->l_resize(ws, args->id);
809 stack_reset(struct swm_region *r, union arg *args)
811 struct workspace *ws = r->ws;
813 DNPRINTF(SWM_D_STACK, "stack_reset: ws %d\n", ws->idx);
815 if (ws->cur_layout->l_init != NULL) {
816 ws->cur_layout->l_init(ws);
823 struct swm_geometry g;
824 struct swm_region *r;
827 DNPRINTF(SWM_D_STACK, "stack\n");
829 for (i = 0; i < ScreenCount(display); i++) {
831 TAILQ_FOREACH(r, &screens[i].rl, entry) {
832 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
833 "(screen %d, region %d)\n", r->ws->idx, i, j++);
835 /* start with screen geometry, adjust for bar */
845 r->ws->cur_layout->l_stack(r->ws, &g);
848 XSync(display, False);
852 stack_floater(struct ws_win *win, struct swm_region *r)
857 bzero(&wc, sizeof wc);
858 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
860 if (win->transient) {
861 win->g.w = (double)WIDTH(r) * dialog_ratio;
862 win->g.h = (double)HEIGHT(r) * dialog_ratio;
865 wc.height = win->g.h;
866 wc.x = (WIDTH(r) - win->g.w) / 2;
867 wc.y = (HEIGHT(r) - win->g.h) / 2;
869 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
870 win->id, wc.x, wc.y, wc.width, wc.height);
872 XConfigureWindow(display, win->id, mask, &wc);
875 int vertical_msize[SWM_WS_MAX];
878 vertical_init(struct workspace *ws)
880 DNPRINTF(SWM_D_MISC, "vertical_init: workspace: %d\n", ws->idx);
882 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
886 vertical_resize(struct workspace *ws, int id)
888 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
891 case SWM_ARG_ID_MASTERSHRINK:
892 ws->l_state.vertical_msize--;
893 if (ws->l_state.vertical_msize < 1)
894 ws->l_state.vertical_msize = 1;
896 case SWM_ARG_ID_MASTERGROW:
897 ws->l_state.vertical_msize++;
898 if (ws->l_state.vertical_msize > SWM_V_SLICE - 1)
899 ws->l_state.vertical_msize = SWM_V_SLICE - 1;
908 vertical_stack(struct workspace *ws, struct swm_geometry *g) {
910 struct swm_geometry gg = *g;
911 struct ws_win *win, *winfocus;
912 int i, hrh, winno, main_width;
915 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
917 if ((winno = count_win(ws, 0)) == 0)
920 if (ws->focus == NULL)
921 ws->focus = TAILQ_FIRST(&ws->winlist);
922 winfocus = cur_focus ? cur_focus : ws->focus;
925 main_width = (g->w / SWM_V_SLICE) *
926 ws->l_state.vertical_msize;
931 hrh = g->h / (winno - 1);
936 TAILQ_FOREACH(win, &ws->winlist, entry) {
938 gg.x += main_width + 2;
939 gg.w = g->w - (main_width + 2);
941 if (i != 0 && hrh != 0) {
942 /* correct the last window for lost pixels */
943 if (win == TAILQ_LAST(&ws->winlist, ws_win_list)) {
944 gg.h = hrh + (g->h - (i * hrh));
948 /* leave first right hand window at y = 0 */
954 if (win->transient != 0 || win->floating != 0)
955 stack_floater(win, ws->r);
957 bzero(&wc, sizeof wc);
959 win->g.x = wc.x = gg.x;
960 win->g.y = wc.y = gg.y;
961 win->g.w = wc.width = gg.w;
962 win->g.h = wc.height = gg.h;
963 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
964 XConfigureWindow(display, win->id, mask, &wc);
967 XMapRaised(display, win->id);
972 focus_win(winfocus); /* has to be done outside of the loop */
977 horizontal_init(struct workspace *ws)
979 DNPRINTF(SWM_D_STACK, "horizontal_init: workspace: %d\n", ws->idx);
981 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
985 horizontal_resize(struct workspace *ws, int id)
987 DNPRINTF(SWM_D_STACK, "horizontal_resize: workspace: %d\n", ws->idx);
990 case SWM_ARG_ID_MASTERSHRINK:
991 ws->l_state.horizontal_msize--;
992 if (ws->l_state.horizontal_msize < 1)
993 ws->l_state.horizontal_msize = 1;
995 case SWM_ARG_ID_MASTERGROW:
996 ws->l_state.horizontal_msize++;
997 if (ws->l_state.horizontal_msize > SWM_H_SLICE - 1)
998 ws->l_state.horizontal_msize = SWM_H_SLICE - 1;
1007 horizontal_stack(struct workspace *ws, struct swm_geometry *g) {
1009 struct swm_geometry gg = *g;
1010 struct ws_win *win, *winfocus;
1011 int i, hrw, winno, main_height;
1014 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
1016 if ((winno = count_win(ws, 0)) == 0)
1019 if (ws->focus == NULL)
1020 ws->focus = TAILQ_FIRST(&ws->winlist);
1021 winfocus = cur_focus ? cur_focus : ws->focus;
1024 main_height = (g->h / SWM_H_SLICE) *
1025 ws->l_state.horizontal_msize;
1030 hrw = g->w / (winno - 1);
1035 TAILQ_FOREACH(win, &ws->winlist, entry) {
1037 gg.y += main_height + 2;
1038 gg.h = g->h - (main_height + 2);
1040 if (i != 0 && hrw != 0) {
1041 /* correct the last window for lost pixels */
1042 if (win == TAILQ_LAST(&ws->winlist, ws_win_list)) {
1043 gg.w = hrw + (g->w - (i * hrw));
1047 /* leave first bottom window at x = 0 */
1053 if (win->transient != 0 || win->floating != 0)
1054 stack_floater(win, ws->r);
1056 bzero(&wc, sizeof wc);
1057 wc.border_width = 1;
1058 win->g.x = wc.x = gg.x;
1059 win->g.y = wc.y = gg.y;
1060 win->g.w = wc.width = gg.w;
1061 win->g.h = wc.height = gg.h;
1062 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1063 XConfigureWindow(display, win->id, mask, &wc);
1066 XMapRaised(display, win->id);
1071 focus_win(winfocus); /* this has to be done outside of the loop */
1074 /* fullscreen view */
1076 max_stack(struct workspace *ws, struct swm_geometry *g) {
1078 struct swm_geometry gg = *g;
1079 struct ws_win *win, *winfocus;
1082 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1084 if (count_win(ws, 0) == 0)
1087 if (ws->focus == NULL)
1088 ws->focus = TAILQ_FIRST(&ws->winlist);
1089 winfocus = cur_focus ? cur_focus : ws->focus;
1091 TAILQ_FOREACH(win, &ws->winlist, entry) {
1092 if (win->transient != 0 || win->floating != 0) {
1093 if (win == ws->focus) {
1095 stack_floater(win, ws->r);
1096 XMapRaised(display, win->id);
1098 XUnmapWindow(display, win->id);
1100 bzero(&wc, sizeof wc);
1101 wc.border_width = 1;
1102 win->g.x = wc.x = gg.x;
1103 win->g.y = wc.y = gg.y;
1104 win->g.w = wc.width = gg.w;
1105 win->g.h = wc.height = gg.h;
1106 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1107 XConfigureWindow(display, win->id, mask, &wc);
1109 if (win == ws->focus) {
1110 XMapRaised(display, win->id);
1112 XUnmapWindow(display, win->id);
1117 focus_win(winfocus); /* has to be done outside of the loop */
1121 send_to_ws(struct swm_region *r, union arg *args)
1123 int wsid = args->id;
1124 struct ws_win *win = cur_focus;
1125 struct workspace *ws, *nws;
1127 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1130 nws = &win->s->ws[wsid];
1132 XUnmapWindow(display, win->id);
1134 /* find a window to focus */
1135 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1136 if (ws->focus == NULL)
1137 ws->focus = TAILQ_FIRST(&ws->winlist);
1138 if (ws->focus == win)
1141 TAILQ_REMOVE(&ws->winlist, win, entry);
1143 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1146 if (count_win(nws, 1) == 1)
1154 /* key definitions */
1158 void (*func)(struct swm_region *r, union arg *);
1161 /* modifier key function argument */
1162 { MODKEY, XK_space, cycle_layout, {0} },
1163 { MODKEY | ShiftMask, XK_space, stack_reset, {0} },
1164 { MODKEY, XK_h, resize_master, {.id = SWM_ARG_ID_MASTERSHRINK} },
1165 { MODKEY, XK_l, resize_master, {.id = SWM_ARG_ID_MASTERGROW} },
1166 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1167 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1168 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1169 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1170 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1171 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1172 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
1173 { MODKEY | ShiftMask, XK_q, quit, {0} },
1174 { MODKEY, XK_q, restart, {0} },
1175 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1176 { MODKEY, XK_1, switchws, {.id = 0} },
1177 { MODKEY, XK_2, switchws, {.id = 1} },
1178 { MODKEY, XK_3, switchws, {.id = 2} },
1179 { MODKEY, XK_4, switchws, {.id = 3} },
1180 { MODKEY, XK_5, switchws, {.id = 4} },
1181 { MODKEY, XK_6, switchws, {.id = 5} },
1182 { MODKEY, XK_7, switchws, {.id = 6} },
1183 { MODKEY, XK_8, switchws, {.id = 7} },
1184 { MODKEY, XK_9, switchws, {.id = 8} },
1185 { MODKEY, XK_0, switchws, {.id = 9} },
1186 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1187 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1188 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1189 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1190 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1191 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1192 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1193 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1194 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1195 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1196 { MODKEY, XK_b, bar_toggle, {0} },
1197 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1198 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1202 updatenumlockmask(void)
1205 XModifierKeymap *modmap;
1207 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1209 modmap = XGetModifierMapping(display);
1210 for (i = 0; i < 8; i++)
1211 for (j = 0; j < modmap->max_keypermod; j++)
1212 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1213 == XKeysymToKeycode(display, XK_Num_Lock))
1214 numlockmask = (1 << i);
1216 XFreeModifiermap(modmap);
1222 unsigned int i, j, k;
1224 unsigned int modifiers[] =
1225 { 0, LockMask, numlockmask, numlockmask | LockMask };
1227 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1228 updatenumlockmask();
1230 for (k = 0; k < ScreenCount(display); k++) {
1231 if (TAILQ_EMPTY(&screens[k].rl))
1233 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1234 for(i = 0; i < LENGTH(keys); i++) {
1235 if((code = XKeysymToKeycode(display, keys[i].keysym)))
1236 for(j = 0; j < LENGTH(modifiers); j++)
1237 XGrabKey(display, code,
1238 keys[i].mod | modifiers[j],
1239 screens[k].root, True,
1240 GrabModeAsync, GrabModeAsync);
1247 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1255 XKeyEvent *ev = &e->xkey;
1257 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1259 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1260 for (i = 0; i < LENGTH(keys); i++)
1261 if (keysym == keys[i].keysym
1262 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1264 keys[i].func(root_to_region(ev->root),
1269 buttonpress(XEvent *e)
1271 XButtonPressedEvent *ev = &e->xbutton;
1272 #ifdef SWM_CLICKTOFOCUS
1274 struct workspace *ws;
1275 struct swm_region *r;
1278 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1280 if (ev->window == ev->root) {
1281 rootclick = ev->root;
1284 if (ev->window == cur_focus->id)
1286 #ifdef SWM_CLICKTOFOCUS
1287 r = root_to_region(ev->root);
1289 TAILQ_FOREACH(win, &ws->winlist, entry)
1290 if (win->id == ev->window) {
1291 /* focus in the clicked window */
1292 XSetWindowBorder(display, ev->window, 0xff0000);
1293 XSetWindowBorder(display, ws->focus->id, 0x888888);
1294 XSetInputFocus(display, ev->window, RevertToPointerRoot,
1297 XSync(display, False);
1304 set_win_state(struct ws_win *win, long state)
1306 long data[] = {state, None};
1308 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1310 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1311 (unsigned char *)data, 2);
1315 manage_window(Window id, struct workspace *ws)
1321 TAILQ_FOREACH(win, &ws->winlist, entry) {
1323 return (win); /* already being managed */
1326 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1327 errx(1, "calloc: failed to allocate memory for new window");
1331 win->s = ws->r->s; /* this never changes */
1332 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
1334 /* make new win focused */
1337 XGetTransientForHint(display, win->id, &trans);
1339 win->transient = trans;
1340 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1341 (unsigned)win->id, win->transient);
1343 XGetWindowAttributes(display, id, &win->wa);
1344 win->g.w = win->wa.width;
1345 win->g.h = win->wa.height;
1346 win->g.x = win->wa.x;
1347 win->g.y = win->wa.y;
1349 /* XXX make this a table */
1350 bzero(&ch, sizeof ch);
1351 if (XGetClassHint(display, win->id, &ch)) {
1352 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1353 if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
1357 XFree(ch.res_class);
1362 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
1363 PropertyChangeMask | StructureNotifyMask);
1365 set_win_state(win, NormalState);
1371 configurerequest(XEvent *e)
1373 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1378 if ((win = find_window(ev->window)) == NULL)
1382 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1384 bzero(&wc, sizeof wc);
1387 wc.width = ev->width;
1388 wc.height = ev->height;
1389 wc.border_width = ev->border_width;
1390 wc.sibling = ev->above;
1391 wc.stack_mode = ev->detail;
1392 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1394 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1396 if (win->floating) {
1397 if (ev->value_mask & CWX)
1399 if (ev->value_mask & CWY)
1401 if (ev->value_mask & CWWidth)
1402 win->g.w = ev->width;
1403 if (ev->value_mask & CWHeight)
1404 win->g.h = ev->height;
1405 if (win->ws->r != NULL) {
1406 /* this seems to be full screen */
1407 if (win->g.w > WIDTH(win->ws->r)) {
1412 if (win->g.h > HEIGHT(win->ws->r)) {
1418 if ((ev->value_mask & (CWX|CWY)) &&
1419 !(ev->value_mask & (CWWidth|CWHeight)))
1421 XMoveResizeWindow(display, win->id,
1422 win->g.x, win->g.y, win->g.w, win->g.h);
1429 configurenotify(XEvent *e)
1431 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1432 e->xconfigure.window);
1436 destroynotify(XEvent *e)
1439 XDestroyWindowEvent *ev = &e->xdestroywindow;
1441 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1443 if ((win = find_window(ev->window)) != NULL) {
1444 struct workspace *ws = win->ws;
1445 /* find a window to focus */
1446 if (ws->focus == win)
1447 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1448 if (ws->focus == NULL)
1449 ws->focus = TAILQ_FIRST(&ws->winlist);
1450 if (ws->focus == win)
1452 if (cur_focus == win) {
1453 if (ws->focus == NULL)
1454 unfocus_win(win); /* XXX focus another ws? */
1456 focus_win(ws->focus);
1459 TAILQ_REMOVE(&ws->winlist, win, entry);
1460 set_win_state(win, WithdrawnState);
1468 enternotify(XEvent *e)
1470 XCrossingEvent *ev = &e->xcrossing;
1474 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1476 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
1477 ev->window != ev->root)
1480 /* eat event(r) to prevent autofocus */
1484 /* brute force for now */
1485 for (i = 0; i < ScreenCount(display); i++) {
1486 for (j = 0; j < SWM_WS_MAX; j++) {
1487 TAILQ_FOREACH(win, &screens[i].ws[j].winlist , entry) {
1488 if (win->id == ev->window)
1498 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
1500 /* XXX this probably needs better handling now.
1501 if (ev->window == ev->root)
1505 * kill grab for now so that we can cut and paste , this screws up
1509 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1510 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1511 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1516 mappingnotify(XEvent *e)
1518 XMappingEvent *ev = &e->xmapping;
1520 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1522 XRefreshKeyboardMapping(ev);
1523 if (ev->request == MappingKeyboard)
1528 maprequest(XEvent *e)
1530 XMapRequestEvent *ev = &e->xmaprequest;
1531 XWindowAttributes wa;
1532 struct swm_region *r;
1534 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1535 e->xmaprequest.window);
1537 if (!XGetWindowAttributes(display, ev->window, &wa))
1539 if (wa.override_redirect)
1541 r = root_to_region(wa.root);
1542 manage_window(e->xmaprequest.window, r->ws);
1547 propertynotify(XEvent *e)
1549 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1550 e->xproperty.window);
1554 unmapnotify(XEvent *e)
1556 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1560 visibilitynotify(XEvent *e)
1563 struct swm_region *r;
1565 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
1566 e->xvisibility.window);
1567 if (e->xvisibility.state == VisibilityUnobscured)
1568 for (i = 0; i < ScreenCount(display); i++)
1569 TAILQ_FOREACH(r, &screens[i].rl, entry)
1570 if (e->xvisibility.window == r->bar_window)
1574 void (*handler[LASTEvent])(XEvent *) = {
1576 [KeyPress] = keypress,
1577 [ButtonPress] = buttonpress,
1578 [ConfigureRequest] = configurerequest,
1579 [ConfigureNotify] = configurenotify,
1580 [DestroyNotify] = destroynotify,
1581 [EnterNotify] = enternotify,
1582 [FocusIn] = focusin,
1583 [MappingNotify] = mappingnotify,
1584 [MapRequest] = maprequest,
1585 [PropertyNotify] = propertynotify,
1586 [UnmapNotify] = unmapnotify,
1587 [VisibilityNotify] = visibilitynotify,
1591 xerror_start(Display *d, XErrorEvent *ee)
1598 xerror(Display *d, XErrorEvent *ee)
1600 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1608 xerrorxlib = XSetErrorHandler(xerror_start);
1610 /* this causes an error if some other window manager is running */
1611 XSelectInput(display, DefaultRootWindow(display),
1612 SubstructureRedirectMask);
1613 XSync(display, False);
1617 XSetErrorHandler(xerror);
1618 XSync(display, False);
1627 unsigned char *p = NULL;
1628 unsigned long n, extra;
1631 astate = XInternAtom(display, "WM_STATE", False);
1632 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
1633 &real, &format, &n, &extra, (unsigned char **)&p);
1634 if (status != Success)
1643 new_region(struct swm_screen *s, struct workspace *ws,
1644 int x, int y, int w, int h)
1646 struct swm_region *r;
1648 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
1649 s->idx, x, y, w, h);
1651 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
1652 errx(1, "calloc: failed to allocate memory for screen");
1661 TAILQ_INSERT_TAIL(&s->rl, r, entry);
1668 #ifdef SWM_XRR_HAS_CRTC
1670 XRRScreenResources *sr;
1672 #endif /* SWM_XRR_HAS_CRTC */
1673 Window d1, d2, *wins = NULL;
1674 XWindowAttributes wa;
1675 struct swm_region *r;
1677 int errorbase, major, minor;
1680 struct workspace *ws;
1682 if ((screens = calloc(ScreenCount(display),
1683 sizeof(struct swm_screen))) == NULL)
1684 errx(1, "calloc: screens");
1686 /* map physical screens */
1687 for (i = 0; i < ScreenCount(display); i++) {
1688 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
1690 TAILQ_INIT(&screens[i].rl);
1691 screens[i].root = RootWindow(display, i);
1692 XGetWindowAttributes(display, screens[i].root, &wa);
1693 XSelectInput(display, screens[i].root,
1694 ButtonPressMask | wa.your_event_mask);
1696 /* set default colors */
1697 screens[i].color_focus = name_to_color("red");
1698 screens[i].color_unfocus = name_to_color("rgb:88/88/88");
1699 screens[i].bar_border = name_to_color("rgb:00/80/80");
1700 screens[i].bar_color = name_to_color("black");
1701 screens[i].bar_font_color = name_to_color("rgb:a0/a0/a0");
1703 /* init all workspaces */
1704 for (j = 0; j < SWM_WS_MAX; j++) {
1705 ws = &screens[i].ws[j];
1710 TAILQ_INIT(&ws->winlist);
1712 for (k = 0; layouts[k].l_stack != NULL; k++) {
1713 if (layouts[k].l_init != NULL)
1714 layouts[k].l_init(ws);
1716 ws->cur_layout = &layouts[0];
1719 /* map virtual screens onto physical screens */
1720 screens[i].xrandr_support = XRRQueryExtension(display,
1721 &xrandr_eventbase, &errorbase);
1722 if (screens[i].xrandr_support)
1723 if (XRRQueryVersion(display, &major, &minor) &&
1725 screens[i].xrandr_support = 0;
1727 #if 0 /* not ready for dynamic screen changes */
1728 if (screens[i].xrandr_support)
1729 XRRSelectInput(display,
1731 RRScreenChangeNotifyMask);
1734 /* grab existing windows (before we build the bars)*/
1735 if (!XQueryTree(display, screens[i].root,
1736 &d1, &d2, &wins, &num)) {
1737 if (wins) /* not sure if this is necessary */
1742 #ifdef SWM_XRR_HAS_CRTC
1743 sr = XRRGetScreenResources(display, screens[i].root);
1746 new_region(&screens[i], &screens[i].ws[w],
1747 0, 0, DisplayWidth(display, i),
1748 DisplayHeight(display, i));
1752 for (c = 0; c < ncrtc; c++) {
1753 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
1754 if (ci->noutput == 0)
1757 if (ci != NULL && ci->mode == None)
1758 new_region(&screens[i], &screens[i].ws[w],
1759 0, 0, DisplayWidth(display, i),
1760 DisplayHeight(display, i));
1762 new_region(&screens[i], &screens[i].ws[w],
1763 ci->x, ci->y, ci->width, ci->height);
1766 XRRFreeCrtcInfo(ci);
1767 XRRFreeScreenResources(sr);
1769 new_region(&screens[i], &screens[i].ws[w],
1770 0, 0, DisplayWidth(display, i),
1771 DisplayHeight(display, i));
1772 #endif /* SWM_XRR_HAS_CRTC */
1774 /* attach windows to a region */
1775 /* normal windows */
1776 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
1777 errx(1, "no regions on screen %d", i);
1779 for (i = 0; i < num; i++) {
1780 XGetWindowAttributes(display, wins[i], &wa);
1781 if (!XGetWindowAttributes(display, wins[i], &wa) ||
1782 wa.override_redirect ||
1783 XGetTransientForHint(display, wins[i], &d1))
1785 if (wa.map_state == IsViewable ||
1786 getstate(wins[i]) == NormalState)
1787 manage_window(wins[i], r->ws);
1789 /* transient windows */
1790 for (i = 0; i < num; i++) {
1791 if (!XGetWindowAttributes(display, wins[i], &wa))
1793 if (XGetTransientForHint(display, wins[i], &d1) &&
1794 (wa.map_state == IsViewable || getstate(wins[i]) ==
1796 manage_window(wins[i], r->ws);
1804 main(int argc, char *argv[])
1807 char conf[PATH_MAX], *cfile = NULL;
1812 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1813 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1814 warnx("no locale support");
1816 if (!(display = XOpenDisplay(0)))
1817 errx(1, "can not open display");
1820 errx(1, "other wm running");
1822 astate = XInternAtom(display, "WM_STATE", False);
1824 /* look for local and global conf file */
1825 pwd = getpwuid(getuid());
1827 errx(1, "invalid user %d", getuid());
1829 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
1830 if (stat(conf, &sb) != -1) {
1831 if (S_ISREG(sb.st_mode))
1834 /* try global conf file */
1835 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
1836 if (!stat(conf, &sb))
1837 if (S_ISREG(sb.st_mode))
1845 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
1851 XNextEvent(display, &e);
1852 if (handler[e.type])
1853 handler[e.type](&e);
1856 XCloseDisplay(display);