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 double dialog_ratio = .6;
138 #define SWM_BAR_MAX (128)
139 sig_atomic_t bar_alarm = 0;
146 char *bar_fonts[] = {
147 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
148 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
152 /* terminal + args */
153 char *spawn_term[] = { "xterm", NULL };
154 char *spawn_menu[] = { "dmenu_run", NULL };
156 /* layout manager data */
157 struct swm_geometry {
167 /* virtual "screens" */
169 TAILQ_ENTRY(swm_region) entry;
170 struct swm_geometry g;
172 struct workspace *ws; /* current workspace on this region */
173 struct swm_screen *s; /* screen idx */
175 TAILQ_HEAD(swm_region_list, swm_region);
179 TAILQ_ENTRY(ws_win) entry;
181 struct swm_geometry g;
185 struct workspace *ws; /* always valid */
186 struct swm_screen *s; /* always valid, never changes */
187 XWindowAttributes wa;
189 TAILQ_HEAD(ws_win_list, ws_win);
191 /* layout handlers */
193 void vertical_config(struct workspace *, int);
194 void vertical_stack(struct workspace *, struct swm_geometry *);
195 void horizontal_config(struct workspace *, int);
196 void horizontal_stack(struct workspace *, struct swm_geometry *);
197 void max_stack(struct workspace *, struct swm_geometry *);
200 void (*l_stack)(struct workspace *, struct swm_geometry *);
201 void (*l_config)(struct workspace *, int);
203 /* stack, configure */
204 { vertical_stack, vertical_config},
205 { horizontal_stack, horizontal_config},
210 #define SWM_H_SLICE (32)
211 #define SWM_V_SLICE (32)
213 /* define work spaces */
215 int idx; /* workspace index */
216 int restack; /* restack on switch */
217 struct layout *cur_layout; /* current layout handlers */
218 struct ws_win *focus; /* may be NULL */
219 struct swm_region *r; /* may be NULL */
220 struct ws_win_list winlist; /* list of windows in ws */
224 int horizontal_msize;
231 /* physical screen mapping */
232 #define SWM_WS_MAX (10) /* XXX Too small? */
234 int idx; /* screen index */
235 struct swm_region_list rl; /* list of regions on this screen */
238 struct workspace ws[SWM_WS_MAX];
241 unsigned long bar_border;
242 unsigned long bar_color;
243 unsigned long bar_font_color;
244 unsigned long color_focus; /* XXX should this be per ws? */
245 unsigned long color_unfocus;
247 struct swm_screen *screens;
249 Window rootclick = 0;
251 struct ws_win *cur_focus = NULL;
253 /* args to functions */
256 #define SWM_ARG_ID_FOCUSNEXT (0)
257 #define SWM_ARG_ID_FOCUSPREV (1)
258 #define SWM_ARG_ID_FOCUSMAIN (2)
259 #define SWM_ARG_ID_SWAPNEXT (3)
260 #define SWM_ARG_ID_SWAPPREV (4)
261 #define SWM_ARG_ID_SWAPMAIN (5)
262 #define SWM_ARG_ID_MASTERSHRINK (6)
263 #define SWM_ARG_ID_MASTERGROW (7)
264 #define SWM_ARG_ID_MASTERADD (8)
265 #define SWM_ARG_ID_MASTERDEL (9)
266 #define SWM_ARG_ID_STACKRESET (10)
267 #define SWM_ARG_ID_STACKINIT (11)
272 name_to_color(char *colorname)
276 XColor screen_def, exact_def;
277 unsigned long result = 0;
278 char cname[32] = "#";
280 cmap = DefaultColormap(display, screens[0].idx);
281 status = XAllocNamedColor(display, cmap, colorname,
282 &screen_def, &exact_def);
284 strlcat(cname, colorname + 2, sizeof cname - 1);
285 status = XAllocNamedColor(display, cmap, cname, &screen_def,
289 result = screen_def.pixel;
291 fprintf(stderr, "color '%s' not found.\n", colorname);
297 varmatch(char *var, char *name, int *index)
302 i = strncmp(var, name, 255);
309 p = var + strlen(name);
312 bzero(buf, sizeof buf);
314 while (isdigit(*p) && i < sizeof buf)
316 if (i == 0 || i >= sizeof buf || *p != ']')
318 *index = strtonum(buf, 0, 99, NULL);
322 /* conf file stuff */
323 #define SWM_CONF_WS "\n= \t"
324 #define SWM_CONF_FILE "scrotwm.conf"
326 conf_load(char *filename)
329 char *line, *cp, *var, *val;
330 size_t len, lineno = 0;
333 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
335 if (filename == NULL)
338 if ((config = fopen(filename, "r")) == NULL)
341 for (sc = ScreenCount(display);;) {
342 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
346 cp += (long)strspn(cp, SWM_CONF_WS);
352 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
354 cp += (long)strspn(cp, SWM_CONF_WS);
355 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
358 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
361 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
362 bar_enabled = atoi(val);
363 else if (!varmatch(var, "bar_border", &i))
364 if (i > 0 && i <= sc)
365 screens[i - 1].bar_border =
368 for (i = 0; i < sc; i++)
369 screens[i].bar_border =
373 else if (!varmatch(var, "bar_color", &i))
374 if (i > 0 && i <= sc)
375 screens[i - 1].bar_color =
378 for (i = 0; i < sc; i++)
379 screens[i].bar_color =
383 else if (!varmatch(var, "bar_font_color", &i))
384 if (i > 0 && i <= sc)
385 screens[i - 1].bar_font_color =
388 for (i = 0; i < sc; i++)
389 screens[i].bar_font_color =
394 else if (!strncmp(var, "bar_font", strlen("bar_font")))
395 asprintf(&bar_fonts[0], "%s", val);
401 if (!varmatch(var, "color_focus", &i))
402 if (i > 0 && i <= sc)
403 screens[i - 1].color_focus =
406 for (i = 0; i < sc; i++)
407 screens[i].color_focus =
411 else if (!varmatch(var, "color_unfocus", &i))
412 if (i > 0 && i <= sc)
413 screens[i - 1].color_unfocus =
416 for (i = 0; i < sc; i++)
417 screens[i].color_unfocus =
426 if (!strncmp(var, "dialog_ratio",
427 strlen("dialog_ratio"))) {
428 dialog_ratio = atof(val);
429 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
436 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
437 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
448 errx(1, "invalid conf file entry: %s=%s", var, val);
450 errx(1, "invalid screen index: %s out of bounds", var);
454 bar_print(struct swm_region *r, char *s)
456 XClearWindow(display, r->bar_window);
457 XSetForeground(display, bar_gc, r->s->bar_font_color);
458 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
467 struct swm_region *r;
472 if (bar_enabled == 0)
476 localtime_r(&tmt, &tm);
477 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
478 for (i = 0; i < ScreenCount(display); i++) {
480 TAILQ_FOREACH(r, &screens[i].rl, entry) {
481 snprintf(e, sizeof e, "%s %d:%d",
482 s, x++, r->ws->idx + 1);
486 XSync(display, False);
497 bar_toggle(struct swm_region *r, union arg *args)
499 struct swm_region *tmpr;
500 int i, j, sc = ScreenCount(display);
502 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
505 for (i = 0; i < sc; i++)
506 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
507 XUnmapWindow(display, tmpr->bar_window);
509 for (i = 0; i < sc; i++)
510 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
511 XMapRaised(display, tmpr->bar_window);
513 bar_enabled = !bar_enabled;
514 XSync(display, False);
515 for (i = 0; i < sc; i++)
516 for (j = 0; j < SWM_WS_MAX; j++)
517 screens[i].ws[j].restack = 1;
520 /* must be after stack */
521 for (i = 0; i < sc; i++)
522 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
527 bar_setup(struct swm_region *r)
531 for (i = 0; bar_fonts[i] != NULL; i++) {
532 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
536 if (bar_fonts[i] == NULL)
537 errx(1, "couldn't load font");
538 bar_height = bar_fs->ascent + bar_fs->descent + 3;
540 r->bar_window = XCreateSimpleWindow(display,
541 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
542 1, r->s->bar_border, r->s->bar_color);
543 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
544 XSetFont(display, bar_gc, bar_fs->fid);
545 XSelectInput(display, r->bar_window, VisibilityChangeMask);
547 XMapRaised(display, r->bar_window);
548 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
550 if (signal(SIGALRM, bar_signal) == SIG_ERR)
551 err(1, "could not install bar_signal");
558 XSetWindowAttributes wa;
559 struct swm_region *r;
562 bzero(&wa, sizeof wa);
563 for (i = 0; i < ScreenCount(display); i++)
564 TAILQ_FOREACH(r, &screens[i].rl, entry) {
565 wa.border_pixel = screens[i].bar_border;
566 wa.background_pixel = screens[i].bar_color;
567 XChangeWindowAttributes(display, r->bar_window,
568 CWBackPixel | CWBorderPixel, &wa);
574 config_win(struct ws_win *win)
578 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
579 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
580 ce.type = ConfigureNotify;
581 ce.display = display;
587 ce.height = win->g.h;
588 ce.border_width = 1; /* XXX store this! */
590 ce.override_redirect = False;
591 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
595 count_win(struct workspace *ws, int count_transient)
600 TAILQ_FOREACH(win, &ws->winlist, entry) {
601 if (count_transient == 0 && win->floating)
603 if (count_transient == 0 && win->transient)
607 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
613 quit(struct swm_region *r, union arg *args)
615 DNPRINTF(SWM_D_MISC, "quit\n");
620 restart(struct swm_region *r, union arg *args)
622 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
624 /* disable alarm because the following code may not be interrupted */
626 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
627 errx(1, "can't disable alarm");
629 XCloseDisplay(display);
630 execvp(start_argv[0], start_argv);
631 fprintf(stderr, "execvp failed\n");
637 root_to_region(Window root)
639 struct swm_region *r;
644 for (i = 0; i < ScreenCount(display); i++)
645 if (screens[i].root == root)
648 if (rootclick != root && /* if root was just clicked in, use cursor */
649 cur_focus && cur_focus->ws->r && cur_focus->s == &screens[i])
650 r = cur_focus->ws->r;
652 if (XQueryPointer(display, screens[i].root,
653 &rr, &cr, &x, &y, &wx, &wy, &mask) == False) {
654 r = TAILQ_FIRST(&screens[i].rl);
656 TAILQ_FOREACH(r, &screens[i].rl, entry) {
657 if (x > X(r) && x < X(r) + WIDTH(r) &&
658 y > Y(r) && y < Y(r) + HEIGHT(r))
663 r = TAILQ_FIRST(&screens[i].rl);
670 find_window(Window id)
675 for (i = 0; i < ScreenCount(display); i++)
676 for (j = 0; j < SWM_WS_MAX; j++)
677 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
684 spawn(struct swm_region *r, union arg *args)
686 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
688 * The double-fork construct avoids zombie processes and keeps the code
689 * clean from stupid signal handlers.
694 close(ConnectionNumber(display));
696 execvp(args->argv[0], args->argv);
697 fprintf(stderr, "execvp failed\n");
706 unfocus_win(struct ws_win *win)
708 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
709 if (win->ws->r && win->focus)
710 XSetWindowBorder(display, win->id,
711 win->ws->r->s->color_unfocus);
713 if (win->ws->focus == win)
714 win->ws->focus = NULL;
715 if (cur_focus == win)
721 focus_win(struct ws_win *win)
723 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
727 win->ws->focus = win;
728 if (win->ws->r != NULL) {
729 if (cur_focus && cur_focus != win)
730 unfocus_win(cur_focus);
733 XSetWindowBorder(display, win->id,
734 win->ws->r->s->color_focus);
736 XSetInputFocus(display, win->id,
737 RevertToPointerRoot, CurrentTime);
742 switchws(struct swm_region *r, union arg *args)
745 struct swm_region *this_r, *other_r;
747 struct workspace *new_ws, *old_ws;
751 new_ws = &this_r->s->ws[wsid];
753 DNPRINTF(SWM_D_WS, "switchws screen %d region %dx%d+%d+%d: "
754 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
757 if (new_ws == old_ws)
762 /* if the other workspace is hidden, switch windows */
763 /* map new window first to prevent ugly blinking */
764 TAILQ_FOREACH(win, &new_ws->winlist, entry)
765 XMapRaised(display, win->id);
767 TAILQ_FOREACH(win, &old_ws->winlist, entry)
768 XUnmapWindow(display, win->id);
773 other_r->ws = old_ws;
782 focus_win(new_ws->focus);
788 swapwin(struct swm_region *r, union arg *args)
790 struct ws_win *target;
791 struct ws_win_list *wl;
794 DNPRINTF(SWM_D_WS, "swapwin id %d "
795 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
796 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
797 if (cur_focus == NULL)
800 wl = &cur_focus->ws->winlist;
803 case SWM_ARG_ID_SWAPPREV:
804 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
805 TAILQ_REMOVE(wl, cur_focus, entry);
807 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
809 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
811 case SWM_ARG_ID_SWAPNEXT:
812 target = TAILQ_NEXT(cur_focus, entry);
813 TAILQ_REMOVE(wl, cur_focus, entry);
815 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
817 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
819 case SWM_ARG_ID_SWAPMAIN:
820 target = TAILQ_FIRST(wl);
821 if (target == cur_focus)
823 TAILQ_REMOVE(wl, target, entry);
824 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
825 TAILQ_REMOVE(wl, cur_focus, entry);
826 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
829 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
838 focus(struct swm_region *r, union arg *args)
840 struct ws_win *winfocus, *winlostfocus;
841 struct ws_win_list *wl;
843 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
844 if (cur_focus == NULL)
847 wl = &cur_focus->ws->winlist;
849 winlostfocus = cur_focus;
852 case SWM_ARG_ID_FOCUSPREV:
853 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
854 if (winfocus == NULL)
855 winfocus = TAILQ_LAST(wl, ws_win_list);
858 case SWM_ARG_ID_FOCUSNEXT:
859 winfocus = TAILQ_NEXT(cur_focus, entry);
860 if (winfocus == NULL)
861 winfocus = TAILQ_FIRST(wl);
864 case SWM_ARG_ID_FOCUSMAIN:
865 winfocus = TAILQ_FIRST(wl);
872 if (winfocus == winlostfocus)
876 XSync(display, False);
880 cycle_layout(struct swm_region *r, union arg *args)
882 struct workspace *ws = r->ws;
884 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
887 if (ws->cur_layout->l_stack == NULL)
888 ws->cur_layout = &layouts[0];
895 stack_config(struct swm_region *r, union arg *args)
897 struct workspace *ws = r->ws;
899 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
902 if (ws->cur_layout->l_config != NULL)
903 ws->cur_layout->l_config(ws, args->id);
905 if (args->id != SWM_ARG_ID_STACKINIT);
911 struct swm_geometry g;
912 struct swm_region *r;
915 DNPRINTF(SWM_D_STACK, "stack\n");
917 for (i = 0; i < ScreenCount(display); i++) {
919 TAILQ_FOREACH(r, &screens[i].rl, entry) {
920 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
921 "(screen %d, region %d)\n", r->ws->idx, i, j++);
923 /* start with screen geometry, adjust for bar */
933 r->ws->cur_layout->l_stack(r->ws, &g);
936 XSync(display, False);
940 stack_floater(struct ws_win *win, struct swm_region *r)
945 bzero(&wc, sizeof wc);
946 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
948 if (win->transient) {
949 win->g.w = (double)WIDTH(r) * dialog_ratio;
950 win->g.h = (double)HEIGHT(r) * dialog_ratio;
953 wc.height = win->g.h;
954 wc.x = (WIDTH(r) - win->g.w) / 2;
955 wc.y = (HEIGHT(r) - win->g.h) / 2;
957 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
958 win->id, wc.x, wc.y, wc.width, wc.height);
960 XConfigureWindow(display, win->id, mask, &wc);
965 vertical_config(struct workspace *ws, int id)
967 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
970 case SWM_ARG_ID_STACKRESET:
971 case SWM_ARG_ID_STACKINIT:
972 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
973 ws->l_state.vertical_mwin = 1;
975 case SWM_ARG_ID_MASTERSHRINK:
976 if (ws->l_state.vertical_msize > 1)
977 ws->l_state.vertical_msize--;
979 case SWM_ARG_ID_MASTERGROW:
980 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
981 ws->l_state.vertical_msize++;
983 case SWM_ARG_ID_MASTERADD:
984 ws->l_state.vertical_mwin++;
986 case SWM_ARG_ID_MASTERDEL:
987 if (ws->l_state.vertical_mwin > 0)
988 ws->l_state.vertical_mwin--;
996 vertical_stack(struct workspace *ws, struct swm_geometry *g) {
998 struct swm_geometry gg = *g;
999 struct ws_win *win, *winfocus;
1000 int i, j, split, colno, hrh, winno, main_width;
1003 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1005 if ((winno = count_win(ws, 0)) == 0)
1008 if (ws->focus == NULL)
1009 ws->focus = TAILQ_FIRST(&ws->winlist);
1010 winfocus = cur_focus ? cur_focus : ws->focus;
1012 if (ws->l_state.vertical_mwin &&
1013 winno > ws->l_state.vertical_mwin) {
1014 split = ws->l_state.vertical_mwin;
1016 main_width = (g->w / SWM_V_SLICE) *
1017 ws->l_state.vertical_msize;
1027 TAILQ_FOREACH(win, &ws->winlist, entry) {
1028 if (split && i == split) {
1029 colno = winno - split;
1030 hrh = (g->h / colno);
1031 gg.x += main_width + 2;
1032 gg.w = g->w - (main_width + 2);
1037 gg.h = (hrh + (g->h - (colno * hrh)));
1045 if (win->transient != 0 || win->floating != 0)
1046 stack_floater(win, ws->r);
1048 bzero(&wc, sizeof wc);
1049 wc.border_width = 1;
1050 win->g.x = wc.x = gg.x;
1051 win->g.y = wc.y = gg.y;
1052 win->g.w = wc.width = gg.w;
1053 win->g.h = wc.height = gg.h;
1054 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1055 XConfigureWindow(display, win->id, mask, &wc);
1058 XMapRaised(display, win->id);
1064 focus_win(winfocus); /* has to be done outside of the loop */
1069 horizontal_config(struct workspace *ws, int id)
1071 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1074 case SWM_ARG_ID_STACKRESET:
1075 case SWM_ARG_ID_STACKINIT:
1076 ws->l_state.horizontal_mwin = 1;
1077 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1079 case SWM_ARG_ID_MASTERSHRINK:
1080 if (ws->l_state.horizontal_msize > 1)
1081 ws->l_state.horizontal_msize--;
1083 case SWM_ARG_ID_MASTERGROW:
1084 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1085 ws->l_state.horizontal_msize++;
1087 case SWM_ARG_ID_MASTERADD:
1088 ws->l_state.horizontal_mwin++;
1090 case SWM_ARG_ID_MASTERDEL:
1091 if (ws->l_state.horizontal_mwin > 0)
1092 ws->l_state.horizontal_mwin--;
1100 horizontal_stack(struct workspace *ws, struct swm_geometry *g) {
1102 struct swm_geometry gg = *g;
1103 struct ws_win *win, *winfocus;
1104 int i, j, split, rowno, hrw, winno, main_height;
1107 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
1109 if ((winno = count_win(ws, 0)) == 0)
1112 if (ws->focus == NULL)
1113 ws->focus = TAILQ_FIRST(&ws->winlist);
1114 winfocus = cur_focus ? cur_focus : ws->focus;
1116 if (ws->l_state.horizontal_mwin &&
1117 winno > ws->l_state.horizontal_mwin) {
1118 split = ws->l_state.horizontal_mwin;
1120 main_height = (g->h / SWM_V_SLICE) *
1121 ws->l_state.horizontal_msize;
1131 TAILQ_FOREACH(win, &ws->winlist, entry) {
1132 if (split && i == split) {
1133 rowno = winno - split;
1134 hrw = (g->w / rowno);
1135 gg.y += main_height + 2;
1136 gg.h = g->h - (main_height + 2);
1141 gg.w = (hrw + (g->w - (rowno * hrw)));
1148 if (win->transient != 0 || win->floating != 0)
1149 stack_floater(win, ws->r);
1151 bzero(&wc, sizeof wc);
1152 wc.border_width = 1;
1153 win->g.x = wc.x = gg.x;
1154 win->g.y = wc.y = gg.y;
1155 win->g.w = wc.width = gg.w;
1156 win->g.h = wc.height = gg.h;
1157 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1158 XConfigureWindow(display, win->id, mask, &wc);
1161 XMapRaised(display, win->id);
1167 focus_win(winfocus); /* this has to be done outside of the loop */
1170 /* fullscreen view */
1172 max_stack(struct workspace *ws, struct swm_geometry *g) {
1174 struct swm_geometry gg = *g;
1175 struct ws_win *win, *winfocus;
1178 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1180 if (count_win(ws, 0) == 0)
1183 if (ws->focus == NULL)
1184 ws->focus = TAILQ_FIRST(&ws->winlist);
1185 winfocus = cur_focus ? cur_focus : ws->focus;
1187 TAILQ_FOREACH(win, &ws->winlist, entry) {
1188 if (win->transient != 0 || win->floating != 0) {
1189 if (win == ws->focus) {
1191 stack_floater(win, ws->r);
1192 XMapRaised(display, win->id);
1194 XUnmapWindow(display, win->id);
1196 bzero(&wc, sizeof wc);
1197 wc.border_width = 1;
1198 win->g.x = wc.x = gg.x;
1199 win->g.y = wc.y = gg.y;
1200 win->g.w = wc.width = gg.w;
1201 win->g.h = wc.height = gg.h;
1202 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1203 XConfigureWindow(display, win->id, mask, &wc);
1205 if (win == ws->focus) {
1206 XMapRaised(display, win->id);
1208 XUnmapWindow(display, win->id);
1213 focus_win(winfocus); /* has to be done outside of the loop */
1217 send_to_ws(struct swm_region *r, union arg *args)
1219 int wsid = args->id;
1220 struct ws_win *win = cur_focus;
1221 struct workspace *ws, *nws;
1223 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1226 nws = &win->s->ws[wsid];
1228 XUnmapWindow(display, win->id);
1230 /* find a window to focus */
1231 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1232 if (ws->focus == NULL)
1233 ws->focus = TAILQ_FIRST(&ws->winlist);
1234 if (ws->focus == win)
1237 TAILQ_REMOVE(&ws->winlist, win, entry);
1239 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1242 if (count_win(nws, 1) == 1)
1250 /* key definitions */
1254 void (*func)(struct swm_region *r, union arg *);
1257 /* modifier key function argument */
1258 { MODKEY, XK_space, cycle_layout, {0} },
1259 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1260 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1261 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1262 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1263 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1264 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1265 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1266 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1267 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1268 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1269 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1270 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
1271 { MODKEY | ShiftMask, XK_q, quit, {0} },
1272 { MODKEY, XK_q, restart, {0} },
1273 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1274 { MODKEY, XK_1, switchws, {.id = 0} },
1275 { MODKEY, XK_2, switchws, {.id = 1} },
1276 { MODKEY, XK_3, switchws, {.id = 2} },
1277 { MODKEY, XK_4, switchws, {.id = 3} },
1278 { MODKEY, XK_5, switchws, {.id = 4} },
1279 { MODKEY, XK_6, switchws, {.id = 5} },
1280 { MODKEY, XK_7, switchws, {.id = 6} },
1281 { MODKEY, XK_8, switchws, {.id = 7} },
1282 { MODKEY, XK_9, switchws, {.id = 8} },
1283 { MODKEY, XK_0, switchws, {.id = 9} },
1284 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1285 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1286 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1287 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1288 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1289 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1290 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1291 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1292 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1293 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1294 { MODKEY, XK_b, bar_toggle, {0} },
1295 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1296 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1300 updatenumlockmask(void)
1303 XModifierKeymap *modmap;
1305 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1307 modmap = XGetModifierMapping(display);
1308 for (i = 0; i < 8; i++)
1309 for (j = 0; j < modmap->max_keypermod; j++)
1310 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1311 == XKeysymToKeycode(display, XK_Num_Lock))
1312 numlockmask = (1 << i);
1314 XFreeModifiermap(modmap);
1320 unsigned int i, j, k;
1322 unsigned int modifiers[] =
1323 { 0, LockMask, numlockmask, numlockmask | LockMask };
1325 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1326 updatenumlockmask();
1328 for (k = 0; k < ScreenCount(display); k++) {
1329 if (TAILQ_EMPTY(&screens[k].rl))
1331 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1332 for (i = 0; i < LENGTH(keys); i++) {
1333 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1334 for (j = 0; j < LENGTH(modifiers); j++)
1335 XGrabKey(display, code,
1336 keys[i].mod | modifiers[j],
1337 screens[k].root, True,
1338 GrabModeAsync, GrabModeAsync);
1345 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1353 XKeyEvent *ev = &e->xkey;
1355 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1357 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1358 for (i = 0; i < LENGTH(keys); i++)
1359 if (keysym == keys[i].keysym
1360 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1362 keys[i].func(root_to_region(ev->root),
1367 buttonpress(XEvent *e)
1369 XButtonPressedEvent *ev = &e->xbutton;
1370 #ifdef SWM_CLICKTOFOCUS
1372 struct workspace *ws;
1373 struct swm_region *r;
1376 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1378 if (ev->window == ev->root) {
1379 rootclick = ev->root;
1382 if (ev->window == cur_focus->id)
1384 #ifdef SWM_CLICKTOFOCUS
1385 r = root_to_region(ev->root);
1387 TAILQ_FOREACH(win, &ws->winlist, entry)
1388 if (win->id == ev->window) {
1389 /* focus in the clicked window */
1390 XSetWindowBorder(display, ev->window, 0xff0000);
1391 XSetWindowBorder(display, ws->focus->id, 0x888888);
1392 XSetInputFocus(display, ev->window, RevertToPointerRoot,
1395 XSync(display, False);
1402 set_win_state(struct ws_win *win, long state)
1404 long data[] = {state, None};
1406 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1408 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1409 (unsigned char *)data, 2);
1413 manage_window(Window id, struct workspace *ws)
1419 TAILQ_FOREACH(win, &ws->winlist, entry) {
1421 return (win); /* already being managed */
1424 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1425 errx(1, "calloc: failed to allocate memory for new window");
1429 win->s = ws->r->s; /* this never changes */
1430 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
1432 /* make new win focused */
1435 XGetTransientForHint(display, win->id, &trans);
1437 win->transient = trans;
1438 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1439 (unsigned)win->id, win->transient);
1441 XGetWindowAttributes(display, id, &win->wa);
1442 win->g.w = win->wa.width;
1443 win->g.h = win->wa.height;
1444 win->g.x = win->wa.x;
1445 win->g.y = win->wa.y;
1447 /* XXX make this a table */
1448 bzero(&ch, sizeof ch);
1449 if (XGetClassHint(display, win->id, &ch)) {
1450 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1451 if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
1455 XFree(ch.res_class);
1460 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
1461 PropertyChangeMask | StructureNotifyMask);
1463 set_win_state(win, NormalState);
1469 configurerequest(XEvent *e)
1471 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1476 if ((win = find_window(ev->window)) == NULL)
1480 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1482 bzero(&wc, sizeof wc);
1485 wc.width = ev->width;
1486 wc.height = ev->height;
1487 wc.border_width = ev->border_width;
1488 wc.sibling = ev->above;
1489 wc.stack_mode = ev->detail;
1490 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1492 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1494 if (win->floating) {
1495 if (ev->value_mask & CWX)
1497 if (ev->value_mask & CWY)
1499 if (ev->value_mask & CWWidth)
1500 win->g.w = ev->width;
1501 if (ev->value_mask & CWHeight)
1502 win->g.h = ev->height;
1503 if (win->ws->r != NULL) {
1504 /* this seems to be full screen */
1505 if (win->g.w > WIDTH(win->ws->r)) {
1510 if (win->g.h > HEIGHT(win->ws->r)) {
1516 if ((ev->value_mask & (CWX|CWY)) &&
1517 !(ev->value_mask & (CWWidth|CWHeight)))
1519 XMoveResizeWindow(display, win->id,
1520 win->g.x, win->g.y, win->g.w, win->g.h);
1527 configurenotify(XEvent *e)
1529 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1530 e->xconfigure.window);
1534 destroynotify(XEvent *e)
1537 XDestroyWindowEvent *ev = &e->xdestroywindow;
1539 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1541 if ((win = find_window(ev->window)) != NULL) {
1542 struct workspace *ws = win->ws;
1543 /* find a window to focus */
1544 if (ws->focus == win)
1545 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1546 if (ws->focus == NULL)
1547 ws->focus = TAILQ_FIRST(&ws->winlist);
1548 if (ws->focus == win)
1550 if (cur_focus == win) {
1551 if (ws->focus == NULL)
1552 unfocus_win(win); /* XXX focus another ws? */
1554 focus_win(ws->focus);
1557 TAILQ_REMOVE(&ws->winlist, win, entry);
1558 set_win_state(win, WithdrawnState);
1565 enternotify(XEvent *e)
1567 XCrossingEvent *ev = &e->xcrossing;
1571 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1573 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
1574 ev->window != ev->root)
1577 /* eat event(r) to prevent autofocus */
1581 /* brute force for now */
1582 for (i = 0; i < ScreenCount(display); i++) {
1583 for (j = 0; j < SWM_WS_MAX; j++) {
1584 TAILQ_FOREACH(win, &screens[i].ws[j].winlist , entry) {
1585 if (win->id == ev->window)
1595 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
1597 /* XXX this probably needs better handling now.
1598 if (ev->window == ev->root)
1602 * kill grab for now so that we can cut and paste , this screws up
1606 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1607 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1608 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1613 mappingnotify(XEvent *e)
1615 XMappingEvent *ev = &e->xmapping;
1617 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1619 XRefreshKeyboardMapping(ev);
1620 if (ev->request == MappingKeyboard)
1625 maprequest(XEvent *e)
1627 XMapRequestEvent *ev = &e->xmaprequest;
1628 XWindowAttributes wa;
1629 struct swm_region *r;
1631 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1632 e->xmaprequest.window);
1634 if (!XGetWindowAttributes(display, ev->window, &wa))
1636 if (wa.override_redirect)
1638 r = root_to_region(wa.root);
1639 manage_window(e->xmaprequest.window, r->ws);
1644 propertynotify(XEvent *e)
1646 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1647 e->xproperty.window);
1651 unmapnotify(XEvent *e)
1653 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1657 visibilitynotify(XEvent *e)
1660 struct swm_region *r;
1662 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
1663 e->xvisibility.window);
1664 if (e->xvisibility.state == VisibilityUnobscured)
1665 for (i = 0; i < ScreenCount(display); i++)
1666 TAILQ_FOREACH(r, &screens[i].rl, entry)
1667 if (e->xvisibility.window == r->bar_window)
1671 void (*handler[LASTEvent])(XEvent *) = {
1673 [KeyPress] = keypress,
1674 [ButtonPress] = buttonpress,
1675 [ConfigureRequest] = configurerequest,
1676 [ConfigureNotify] = configurenotify,
1677 [DestroyNotify] = destroynotify,
1678 [EnterNotify] = enternotify,
1679 [FocusIn] = focusin,
1680 [MappingNotify] = mappingnotify,
1681 [MapRequest] = maprequest,
1682 [PropertyNotify] = propertynotify,
1683 [UnmapNotify] = unmapnotify,
1684 [VisibilityNotify] = visibilitynotify,
1688 xerror_start(Display *d, XErrorEvent *ee)
1695 xerror(Display *d, XErrorEvent *ee)
1697 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1705 xerrorxlib = XSetErrorHandler(xerror_start);
1707 /* this causes an error if some other window manager is running */
1708 XSelectInput(display, DefaultRootWindow(display),
1709 SubstructureRedirectMask);
1710 XSync(display, False);
1714 XSetErrorHandler(xerror);
1715 XSync(display, False);
1724 unsigned char *p = NULL;
1725 unsigned long n, extra;
1728 astate = XInternAtom(display, "WM_STATE", False);
1729 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
1730 &real, &format, &n, &extra, (unsigned char **)&p);
1731 if (status != Success)
1740 new_region(struct swm_screen *s, struct workspace *ws,
1741 int x, int y, int w, int h)
1743 struct swm_region *r;
1745 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
1746 s->idx, x, y, w, h);
1748 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
1749 errx(1, "calloc: failed to allocate memory for screen");
1758 TAILQ_INSERT_TAIL(&s->rl, r, entry);
1765 #ifdef SWM_XRR_HAS_CRTC
1767 XRRScreenResources *sr;
1769 #endif /* SWM_XRR_HAS_CRTC */
1770 Window d1, d2, *wins = NULL;
1771 XWindowAttributes wa;
1772 struct swm_region *r;
1774 int errorbase, major, minor;
1775 int ncrtc = 0, w = 0;
1777 struct workspace *ws;
1779 if ((screens = calloc(ScreenCount(display),
1780 sizeof(struct swm_screen))) == NULL)
1781 errx(1, "calloc: screens");
1783 /* map physical screens */
1784 for (i = 0; i < ScreenCount(display); i++) {
1785 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
1787 TAILQ_INIT(&screens[i].rl);
1788 screens[i].root = RootWindow(display, i);
1789 XGetWindowAttributes(display, screens[i].root, &wa);
1790 XSelectInput(display, screens[i].root,
1791 ButtonPressMask | wa.your_event_mask);
1793 /* set default colors */
1794 screens[i].color_focus = name_to_color("red");
1795 screens[i].color_unfocus = name_to_color("rgb:88/88/88");
1796 screens[i].bar_border = name_to_color("rgb:00/80/80");
1797 screens[i].bar_color = name_to_color("black");
1798 screens[i].bar_font_color = name_to_color("rgb:a0/a0/a0");
1800 /* init all workspaces */
1801 for (j = 0; j < SWM_WS_MAX; j++) {
1802 ws = &screens[i].ws[j];
1807 TAILQ_INIT(&ws->winlist);
1809 for (k = 0; layouts[k].l_stack != NULL; k++)
1810 if (layouts[k].l_config != NULL)
1811 layouts[k].l_config(ws,
1812 SWM_ARG_ID_STACKINIT);
1813 ws->cur_layout = &layouts[0];
1816 /* map virtual screens onto physical screens */
1817 screens[i].xrandr_support = XRRQueryExtension(display,
1818 &xrandr_eventbase, &errorbase);
1819 if (screens[i].xrandr_support)
1820 if (XRRQueryVersion(display, &major, &minor) &&
1822 screens[i].xrandr_support = 0;
1824 #if 0 /* not ready for dynamic screen changes */
1825 if (screens[i].xrandr_support)
1826 XRRSelectInput(display,
1828 RRScreenChangeNotifyMask);
1831 /* grab existing windows (before we build the bars)*/
1832 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
1835 #ifdef SWM_XRR_HAS_CRTC
1836 sr = XRRGetScreenResources(display, screens[i].root);
1838 new_region(&screens[i], &screens[i].ws[w],
1839 0, 0, DisplayWidth(display, i),
1840 DisplayHeight(display, i));
1844 for (c = 0; c < ncrtc; c++) {
1845 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
1846 if (ci->noutput == 0)
1849 if (ci != NULL && ci->mode == None)
1850 new_region(&screens[i], &screens[i].ws[w], 0, 0,
1851 DisplayWidth(display, i),
1852 DisplayHeight(display, i));
1854 new_region(&screens[i], &screens[i].ws[w],
1855 ci->x, ci->y, ci->width, ci->height);
1858 XRRFreeCrtcInfo(ci);
1859 XRRFreeScreenResources(sr);
1861 new_region(&screens[i], &screens[i].ws[w], 0, 0,
1862 DisplayWidth(display, i),
1863 DisplayHeight(display, i));
1864 #endif /* SWM_XRR_HAS_CRTC */
1866 /* attach windows to a region */
1867 /* normal windows */
1868 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
1869 errx(1, "no regions on screen %d", i);
1871 for (i = 0; i < no; i++) {
1872 XGetWindowAttributes(display, wins[i], &wa);
1873 if (!XGetWindowAttributes(display, wins[i], &wa) ||
1874 wa.override_redirect ||
1875 XGetTransientForHint(display, wins[i], &d1))
1878 if (wa.map_state == IsViewable ||
1879 getstate(wins[i]) == NormalState)
1880 manage_window(wins[i], r->ws);
1882 /* transient windows */
1883 for (i = 0; i < no; i++) {
1884 if (!XGetWindowAttributes(display, wins[i], &wa))
1887 if (XGetTransientForHint(display, wins[i], &d1) &&
1888 (wa.map_state == IsViewable || getstate(wins[i]) ==
1890 manage_window(wins[i], r->ws);
1900 main(int argc, char *argv[])
1903 char conf[PATH_MAX], *cfile = NULL;
1910 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1911 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1912 warnx("no locale support");
1914 if (!(display = XOpenDisplay(0)))
1915 errx(1, "can not open display");
1918 errx(1, "other wm running");
1920 astate = XInternAtom(display, "WM_STATE", False);
1922 /* look for local and global conf file */
1923 pwd = getpwuid(getuid());
1925 errx(1, "invalid user %d", getuid());
1929 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
1930 if (stat(conf, &sb) != -1) {
1931 if (S_ISREG(sb.st_mode))
1934 /* try global conf file */
1935 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
1936 if (!stat(conf, &sb))
1937 if (S_ISREG(sb.st_mode))
1944 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
1949 xfd = ConnectionNumber(display);
1952 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
1954 errx(1, "select failed");
1959 while(XPending(display)) {
1960 XNextEvent(display, &e);
1961 if (handler[e.type])
1962 handler[e.type](&e);
1966 XCloseDisplay(display);