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>
77 /* #define SWM_DEBUG */
79 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
80 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
81 #define SWM_D_MISC 0x0001
82 #define SWM_D_EVENT 0x0002
83 #define SWM_D_WS 0x0004
84 #define SWM_D_FOCUS 0x0008
85 #define SWM_D_MOVE 0x0010
87 u_int32_t swm_debug = 0
96 #define DNPRINTF(n,x...)
99 #define LENGTH(x) (sizeof x / sizeof x[0])
100 #define MODKEY Mod1Mask
101 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
103 int (*xerrorxlib)(Display *, XErrorEvent *);
107 int ignore_enter = 0;
108 unsigned int numlockmask = 0;
109 unsigned long color_focus = 0xff0000; /* XXX should this be per ws? */
110 unsigned long color_unfocus = 0x888888;
117 unsigned long bar_border = 0x008080;
118 unsigned long bar_color = 0x000000;
119 unsigned long bar_font_color = 0xa0a0a0;
125 char *bar_fonts[] = {
126 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
127 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
131 /* terminal + args */
132 char *spawn_term[] = { "xterm", NULL };
133 char *spawn_menu[] = { "dmenu_run", NULL };
134 char *spawn_scrotwm[] = { "scrotwm", NULL };
136 /* layout manager data */
137 struct swm_geometry {
145 void vertical_init(void);
146 void vertical_stack(struct swm_geometry *);
147 void horizontal_init(void);
148 void horizontal_stack(struct swm_geometry *);
150 void max_stack(struct swm_geometry *);
153 void (*l_init)(void); /* init/reset */
154 void (*l_stack)(struct swm_geometry *);
157 { vertical_init, vertical_stack},
158 { horizontal_init, horizontal_stack},
159 /* XXX not working yet
160 * { max_init, max_stack},
166 TAILQ_ENTRY(ws_win) entry;
168 struct swm_geometry g;
171 XWindowAttributes wa;
175 TAILQ_HEAD(ws_win_list, ws_win);
177 /* define work spaces */
178 #define SWM_WS_MAX (10)
180 int visible; /* workspace visible */
181 int restack; /* restack on switch */
182 struct swm_geometry g;
183 struct layout *cur_layout; /* current layout handlers */
184 struct ws_win *focus; /* which win has focus */
185 struct ws_win_list winlist; /* list of windows in ws */
189 /* args to functions */
192 #define SWM_ARG_ID_FOCUSNEXT (0)
193 #define SWM_ARG_ID_FOCUSPREV (1)
194 #define SWM_ARG_ID_FOCUSMAIN (2)
195 #define SWM_ARG_ID_SWAPNEXT (3)
196 #define SWM_ARG_ID_SWAPPREV (4)
197 #define SWM_ARG_ID_SWAPMAIN (5)
201 /* conf file stuff */
202 #define SWM_CONF_WS "\n= \t"
203 #define SWM_CONF_FILE "scrotwm.conf"
205 conf_load(char *filename)
208 char *line, *cp, *var, *val;
209 size_t len, lineno = 0;
211 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
213 if (filename == NULL)
216 if ((config = fopen(filename, "r")) == NULL)
220 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
224 cp += (long)strspn(cp, SWM_CONF_WS);
230 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
232 cp += (long)strspn(cp, SWM_CONF_WS);
233 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
236 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
239 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
240 bar_enabled = atoi(val);
241 else if (!strncmp(var, "bar_border",
242 strlen("bar_border")))
243 bar_border = strtol(val, NULL, 16);
244 else if (!strncmp(var, "bar_color",
245 strlen("bar_color")))
246 bar_color = strtol(val, NULL, 16);
247 else if (!strncmp(var, "bar_font_color",
248 strlen("bar_font_color")))
249 bar_font_color = strtol(val, NULL, 16);
250 else if (!strncmp(var, "bar_font", strlen("bar_font")))
251 asprintf(&bar_fonts[0], "%s", val);
257 if (!strncmp(var, "color_focus", strlen("color_focus")))
258 color_focus = strtol(val, NULL, 16);
259 else if (!strncmp(var, "color_unfocus",
260 strlen("color_unfocus")))
261 color_unfocus = strtol(val, NULL, 16);
267 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
268 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
279 errx(1, "invalid conf file entry: %s=%s", var, val);
288 if (bar_enabled == 0)
292 XSetForeground(display, bar_gc, bar_color);
293 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
298 localtime_r(&tmt, &tm);
299 strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
300 XSetForeground(display, bar_gc, bar_font_color);
301 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
303 XSync(display, False);
311 /* XXX yeah yeah byte me */
316 bar_toggle(union arg *args)
320 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
324 XUnmapWindow(display, bar_window);
327 XMapWindow(display, bar_window);
329 XSync(display, False);
330 for (i = 0; i < SWM_WS_MAX; i++)
334 bar_print(); /* must be after stack */
342 for (i = 0; bar_fonts[i] != NULL; i++) {
343 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
347 if (bar_fonts[i] == NULL)
348 errx(1, "couldn't load font");
349 bar_height = bar_fs->ascent + bar_fs->descent + 3;
351 bar_window = XCreateSimpleWindow(display, root, 0, 0,
352 ws[current_ws].g.w, bar_height - 2, 1, bar_border, bar_color);
353 bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv);
354 XSetFont(display, bar_gc, bar_fs->fid);
355 XSelectInput(display, bar_window, VisibilityChangeMask);
357 XMapWindow(display, bar_window);
359 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %d\n", (int)bar_window);
361 if (signal(SIGALRM, bar_signal) == SIG_ERR)
362 err(1, "could not install bar_signal");
367 count_win(int wsid, int count_transient)
372 TAILQ_FOREACH (win, &ws[wsid].winlist, entry) {
373 if (count_transient == 0 && win->transient)
377 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
382 quit(union arg *args)
384 DNPRINTF(SWM_D_MISC, "quit\n");
390 restart(union arg *args)
392 XCloseDisplay(display);
393 execvp(args->argv[0], args->argv);
394 fprintf(stderr, "execvp failed\n");
400 spawn(union arg *args)
402 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
404 * The double-fork construct avoids zombie processes and keeps the code
405 * clean from stupid signal handlers.
410 close(ConnectionNumber(display));
412 execvp(args->argv[0], args->argv);
413 fprintf(stderr, "execvp failed\n");
422 focus_win(struct ws_win *win)
424 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
425 XSetWindowBorder(display, win->id, color_focus);
426 XSetInputFocus(display, win->id, RevertToPointerRoot, CurrentTime);
427 ws[current_ws].focus = win;
431 unfocus_win(struct ws_win *win)
433 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
434 XSetWindowBorder(display, win->id, color_unfocus);
435 if (ws[current_ws].focus == win)
436 ws[current_ws].focus = NULL;
440 switchws(union arg *args)
445 DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
447 if (wsid == current_ws)
450 /* map new window first to prevent ugly blinking */
451 TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
452 XMapRaised(display, win->id);
453 ws[wsid].visible = 1;
455 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
456 XUnmapWindow(display, win->id);
457 ws[current_ws].visible = 0;
462 if (ws[wsid].restack) {
466 if (ws[wsid].focus != NULL)
467 focus_win(ws[wsid].focus);
468 XSync(display, False);
473 swapwin(union arg *args)
475 struct ws_win *target;
477 DNPRINTF(SWM_D_MOVE, "swapwin: id %d\n", args->id);
478 if (ws[current_ws].focus == NULL)
482 case SWM_ARG_ID_SWAPPREV:
483 target = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
484 TAILQ_REMOVE(&ws[current_ws].winlist,
485 ws[current_ws].focus, entry);
487 TAILQ_INSERT_TAIL(&ws[current_ws].winlist,
488 ws[current_ws].focus, entry);
490 TAILQ_INSERT_BEFORE(target, ws[current_ws].focus,
493 case SWM_ARG_ID_SWAPNEXT:
494 target = TAILQ_NEXT(ws[current_ws].focus, entry);
495 TAILQ_REMOVE(&ws[current_ws].winlist,
496 ws[current_ws].focus, entry);
498 TAILQ_INSERT_HEAD(&ws[current_ws].winlist,
499 ws[current_ws].focus, entry);
501 TAILQ_INSERT_AFTER(&ws[current_ws].winlist, target,
502 ws[current_ws].focus, entry);
504 case SWM_ARG_ID_SWAPMAIN:
505 target = TAILQ_FIRST(&ws[current_ws].winlist);
506 if (target == ws[current_ws].focus)
508 TAILQ_REMOVE(&ws[current_ws].winlist, target, entry);
509 TAILQ_INSERT_BEFORE(ws[current_ws].focus, target, entry);
510 TAILQ_REMOVE(&ws[current_ws].winlist,
511 ws[current_ws].focus, entry);
512 TAILQ_INSERT_HEAD(&ws[current_ws].winlist,
513 ws[current_ws].focus, entry);
516 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
525 focus(union arg *args)
527 struct ws_win *winfocus, *winlostfocus;
529 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
530 if (ws[current_ws].focus == NULL)
533 winlostfocus = ws[current_ws].focus;
536 case SWM_ARG_ID_FOCUSPREV:
537 winfocus = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
538 if (winfocus == NULL)
540 TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
543 case SWM_ARG_ID_FOCUSNEXT:
544 winfocus = TAILQ_NEXT(ws[current_ws].focus, entry);
545 if (winfocus == NULL)
546 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
549 case SWM_ARG_ID_FOCUSMAIN:
550 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
557 if (winfocus == winlostfocus)
560 unfocus_win(winlostfocus);
562 XSync(display, False);
566 cycle_layout(union arg *args)
568 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", current_ws);
570 ws[current_ws].cur_layout++;
571 if (ws[current_ws].cur_layout->l_stack == NULL)
572 ws[current_ws].cur_layout = &layouts[0];
578 struct swm_geometry g;
579 DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
581 /* start with workspace geometry, adjust for bar */
582 g = ws[current_ws].g;
588 ws[current_ws].restack = 0;
589 ws[current_ws].cur_layout->l_stack(&g);
590 XSync(display, False);
594 stack_floater(struct ws_win *win)
599 bzero(&wc, sizeof wc);
601 mask = CWX | CWY | CWBorderWidth;
603 /* use obsolete width height */
604 if (win->sh.flags & USPosition) {
605 win->g.w = wc.width = win->sh.width;
606 win->g.h = wc.height = win->sh.height;
607 mask |= CWWidth | CWHeight;
611 if (win->sh.flags & PMinSize) {
612 /* some hints are retarded */
613 if (win->sh.min_width < ws[current_ws].g.w / 10)
614 win->sh.min_width = ws[current_ws].g.w / 3;
615 if (win->sh.min_height < ws[current_ws].g.h / 10)
616 win->wa.height = ws[current_ws].g.h / 3;
618 win->g.w = wc.width = win->sh.min_width * 2;
619 win->g.h = wc.height = win->sh.min_height * 2;
620 mask |= CWWidth | CWHeight;
622 if (win->sh.flags & PMaxSize) {
623 /* potentially override min values */
624 if (win->sh.max_width < ws[current_ws].g.w) {
625 win->g.w = wc.width = win->sh.max_width;
628 if (win->sh.max_height < ws[current_ws].g.h) {
629 win->g.h = wc.height = win->sh.max_height;
634 /* make sure we don't clobber the screen */
635 if ((mask & CWWidth) && win->wa.width > ws[current_ws].g.w)
636 win->wa.width = ws[current_ws].g.w - 4;
637 if ((mask & CWHeight) && win->wa.height > ws[current_ws].g.h)
638 win->wa.height = ws[current_ws].g.h - 4;
640 /* supposed to be obsolete */
641 if (win->sh.flags & USPosition) {
642 win->g.x = wc.x = win->sh.x;
643 win->g.y = wc.y = win->sh.y;
645 win->g.x = wc.x = (ws[current_ws].g.w - win->wa.width) / 2;
646 win->g.y = wc.y = (ws[current_ws].g.h - win->wa.height) / 2;
648 DNPRINTF(SWM_D_EVENT, "stack_floater: win %d x %d y %d w %d h %d\n",
649 win, wc.x, wc.y, wc.width, wc.height);
651 XConfigureWindow(display, win->id, mask, &wc);
657 DNPRINTF(SWM_D_EVENT, "vertical_init: workspace: %d\n", current_ws);
660 /* I know this sucks but it works well enough */
662 vertical_stack(struct swm_geometry *g) {
664 struct swm_geometry gg = *g;
665 struct ws_win wf, *win, *winfocus = &wf;
669 DNPRINTF(SWM_D_EVENT, "vertical_stack: workspace: %d\n", current_ws);
673 winno = count_win(current_ws, 0);
681 hrh = g->h / (winno - 1);
686 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
691 if (i != 0 && hrh != 0) {
692 /* correct the last window for lost pixels */
693 if (win == TAILQ_LAST(&ws[current_ws].winlist,
695 gg.h = hrh + (g->h - (i * hrh));
699 /* leave first right hand window at y = 0 */
705 if (win->transient != 0 || win->floating != 0)
708 bzero(&wc, sizeof wc);
710 win->g.x = wc.x = gg.x;
711 win->g.y = wc.y = gg.y;
712 win->g.w = wc.width = gg.w;
713 win->g.h = wc.height = gg.h;
714 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
715 XConfigureWindow(display, win->id, mask, &wc);
718 if (win == ws[current_ws].focus)
722 XMapRaised(display, win->id);
726 focus_win(winfocus); /* this has to be done outside of the loop */
730 horizontal_init(void)
732 DNPRINTF(SWM_D_EVENT, "horizontal_init: workspace: %d\n", current_ws);
736 horizontal_stack(struct swm_geometry *g) {
738 struct swm_geometry gg = *g;
739 struct ws_win wf, *win, *winfocus = &wf;
743 DNPRINTF(SWM_D_EVENT, "horizontal_stack: workspace: %d\n", current_ws);
747 winno = count_win(current_ws, 0);
755 hrw = g->w / (winno - 1);
760 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
763 gg.h -= (2 - g->h % 2);
765 if (i != 0 && hrw != 0) {
766 /* correct the last window for lost pixels */
767 if (win == TAILQ_LAST(&ws[current_ws].winlist,
769 gg.w = hrw + (g->w - (i * hrw));
773 /* leave first bottom window at x = 0 */
779 if (win->transient != 0 || win->floating != 0)
782 bzero(&wc, sizeof wc);
784 win->g.x = wc.x = gg.x;
785 win->g.y = wc.y = gg.y;
786 win->g.w = wc.width = gg.w;
787 win->g.h = wc.height = gg.h;
788 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
789 XConfigureWindow(display, win->id, mask, &wc);
792 if (win == ws[current_ws].focus)
796 XMapRaised(display, win->id);
800 focus_win(winfocus); /* this has to be done outside of the loop */
803 /* fullscreen view */
807 DNPRINTF(SWM_D_EVENT, "max_init: workspace: %d\n", current_ws);
811 max_stack(struct swm_geometry *g) {
813 struct swm_geometry gg = *g;
814 struct ws_win wf, *win, *winfocus = &wf;
818 DNPRINTF(SWM_D_EVENT, "max_stack: workspace: %d\n", current_ws);
822 winno = count_win(current_ws, 0);
826 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
827 if (i == 1 && win->transient == 0 && win->floating != 0) {
833 bzero(&wc, sizeof wc);
835 win->g.x = wc.x = gg.x;
836 win->g.y = wc.y = gg.y;
837 win->g.w = wc.width = gg.w;
838 win->g.h = wc.height = gg.h;
839 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
840 XConfigureWindow(display, win->id, mask, &wc);
842 XMapRaised(display, win->id);
844 /* hide all but the master window */
845 XUnmapWindow(display, win->id);
850 focus_win(winfocus); /* this has to be done outside of the loop */
854 send_to_ws(union arg *args)
857 struct ws_win *win = ws[current_ws].focus;
859 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
861 XUnmapWindow(display, win->id);
863 /* find a window to focus */
864 ws[current_ws].focus = TAILQ_PREV(win, ws_win_list, entry);
865 if (ws[current_ws].focus == NULL)
866 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
867 if (ws[current_ws].focus == win)
868 ws[current_ws].focus = NULL;
870 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
872 TAILQ_INSERT_TAIL(&ws[wsid].winlist, win, entry);
873 if (count_win(wsid, 1) == 1)
874 ws[wsid].focus = win;
875 ws[wsid].restack = 1;
880 /* key definitions */
884 void (*func)(union arg *);
887 /* modifier key function argument */
888 /* XXX alt-c is temporary, until I figure out how to grab spacebar */
889 { MODKEY, XK_c, cycle_layout, {0} },
890 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
891 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
892 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
893 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
894 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
895 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
896 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
897 { MODKEY | ShiftMask, XK_q, quit, {0} },
898 { MODKEY, XK_q, restart, {.argv = spawn_scrotwm } },
899 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
900 { MODKEY, XK_1, switchws, {.id = 0} },
901 { MODKEY, XK_2, switchws, {.id = 1} },
902 { MODKEY, XK_3, switchws, {.id = 2} },
903 { MODKEY, XK_4, switchws, {.id = 3} },
904 { MODKEY, XK_5, switchws, {.id = 4} },
905 { MODKEY, XK_6, switchws, {.id = 5} },
906 { MODKEY, XK_7, switchws, {.id = 6} },
907 { MODKEY, XK_8, switchws, {.id = 7} },
908 { MODKEY, XK_9, switchws, {.id = 8} },
909 { MODKEY, XK_0, switchws, {.id = 9} },
910 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
911 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
912 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
913 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
914 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
915 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
916 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
917 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
918 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
919 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
920 { MODKEY, XK_b, bar_toggle, {0} },
921 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
922 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
926 updatenumlockmask(void)
929 XModifierKeymap *modmap;
931 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
933 modmap = XGetModifierMapping(display);
934 for (i = 0; i < 8; i++)
935 for (j = 0; j < modmap->max_keypermod; j++)
936 if (modmap->modifiermap[i * modmap->max_keypermod + j]
937 == XKeysymToKeycode(display, XK_Num_Lock))
938 numlockmask = (1 << i);
940 XFreeModifiermap(modmap);
948 unsigned int modifiers[] =
949 { 0, LockMask, numlockmask, numlockmask | LockMask };
951 DNPRINTF(SWM_D_MISC, "grabkeys\n");
954 XUngrabKey(display, AnyKey, AnyModifier, root);
955 for(i = 0; i < LENGTH(keys); i++) {
956 if((code = XKeysymToKeycode(display, keys[i].keysym)))
957 for(j = 0; j < LENGTH(modifiers); j++)
958 XGrabKey(display, code,
959 keys[i].mod | modifiers[j], root,
960 True, GrabModeAsync, GrabModeAsync);
966 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
974 XKeyEvent *ev = &e->xkey;
976 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
978 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
979 for(i = 0; i < LENGTH(keys); i++)
980 if(keysym == keys[i].keysym
981 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
983 keys[i].func(&(keys[i].args));
987 buttonpress(XEvent *e)
989 XButtonPressedEvent *ev = &e->xbutton;
990 #ifdef SWM_CLICKTOFOCUS
995 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
997 if (ev->window == root)
999 if (ev->window == ws[current_ws].focus->id)
1001 #ifdef SWM_CLICKTOFOCUS
1002 TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
1003 if (win->id == ev->window) {
1004 /* focus in the clicked window */
1005 XSetWindowBorder(display, ev->window, 0xff0000);
1006 XSetWindowBorder(display,
1007 ws[current_ws].focus->id, 0x888888);
1008 XSetInputFocus(display, ev->window, RevertToPointerRoot,
1010 ws[current_ws].focus = win;
1011 XSync(display, False);
1018 manage_window(Window id)
1022 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1024 return (win); /* already being managed */
1027 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1028 errx(1, "calloc: failed to allocate memory for new window");
1031 TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
1033 XSelectInput(display, id, ButtonPressMask | EnterWindowMask |
1034 FocusChangeMask | ExposureMask);
1040 configurerequest(XEvent *e)
1042 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1046 DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window);
1049 win = manage_window(ev->window);
1050 ws[current_ws].focus = win; /* make new win focused */
1052 XGetTransientForHint(display, win->id, &trans);
1054 win->transient = trans;
1055 DNPRINTF(SWM_D_MISC, "configurerequest: win %u transient %u\n",
1056 (unsigned)win->id, win->transient);
1058 XGetWindowAttributes(display, win->id, &win->wa);
1059 XGetNormalHints(display, win->id, &win->sh);
1061 XClassHint ch = { 0 };
1062 if(XGetClassHint(display, win->id, &ch)) {
1063 fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name);
1064 if (!strcmp(ch.res_class, "Gvim") && !strcmp(ch.res_name, "gvim")) {
1068 XFree(ch.res_class);
1077 configurenotify(XEvent *e)
1080 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1081 e->xconfigure.window);
1085 destroynotify(XEvent *e)
1088 XDestroyWindowEvent *ev = &e->xdestroywindow;
1090 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1092 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1093 if (ev->window == win->id) {
1094 /* find a window to focus */
1095 ws[current_ws].focus = TAILQ_PREV(win,
1096 ws_win_list, entry);
1097 if (ws[current_ws].focus == NULL)
1098 ws[current_ws].focus =
1099 TAILQ_FIRST(&ws[current_ws].winlist);
1100 if (win == ws[current_ws].focus)
1101 ws[current_ws].focus = NULL;
1103 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
1113 enternotify(XEvent *e)
1115 XCrossingEvent *ev = &e->xcrossing;
1118 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1120 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
1124 /* eat event(s) to prevent autofocus */
1128 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1129 if (win->id == ev->window)
1139 XFocusChangeEvent *ev = &e->xfocus;
1141 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window);
1143 XSync(display, False); /* not sure this helps redrawing graphic apps */
1145 if (ev->window == root)
1148 * kill grab for now so that we can cut and paste , this screws up
1152 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1153 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1154 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1159 mappingnotify(XEvent *e)
1161 XMappingEvent *ev = &e->xmapping;
1163 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1165 XRefreshKeyboardMapping(ev);
1166 if(ev->request == MappingKeyboard)
1171 maprequest(XEvent *e)
1173 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1174 e->xmaprequest.window);
1176 manage_window(e->xmaprequest.window);
1181 propertynotify(XEvent *e)
1183 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1184 e->xproperty.window);
1188 unmapnotify(XEvent *e)
1190 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1194 visibilitynotify(XEvent *e)
1196 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n", e->xvisibility.window);
1198 if (e->xvisibility.window == bar_window &&
1199 e->xvisibility.state == VisibilityUnobscured)
1203 void (*handler[LASTEvent])(XEvent *) = {
1205 [KeyPress] = keypress,
1206 [ButtonPress] = buttonpress,
1207 [ConfigureRequest] = configurerequest,
1208 [ConfigureNotify] = configurenotify,
1209 [DestroyNotify] = destroynotify,
1210 [EnterNotify] = enternotify,
1211 [FocusIn] = focusin,
1212 [MappingNotify] = mappingnotify,
1213 [MapRequest] = maprequest,
1214 [PropertyNotify] = propertynotify,
1215 [UnmapNotify] = unmapnotify,
1216 [VisibilityNotify] = visibilitynotify,
1220 xerror_start(Display *d, XErrorEvent *ee)
1227 xerror(Display *d, XErrorEvent *ee)
1229 fprintf(stderr, "error: %p %p\n", display, ee);
1238 xerrorxlib = XSetErrorHandler(xerror_start);
1240 /* this causes an error if some other window manager is running */
1241 XSelectInput(display, DefaultRootWindow(display),
1242 SubstructureRedirectMask);
1243 XSync(display, False);
1247 XSetErrorHandler(xerror);
1248 XSync(display, False);
1253 main(int argc, char *argv[])
1256 char conf[PATH_MAX], *cfile = NULL;
1259 unsigned int i, num;
1260 Window d1, d2, *wins = NULL;
1261 XWindowAttributes wa;
1263 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1264 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1265 warnx("no locale support");
1267 if(!(display = XOpenDisplay(0)))
1268 errx(1, "can not open display");
1271 errx(1, "other wm running");
1273 screen = DefaultScreen(display);
1274 root = RootWindow(display, screen);
1276 /* look for local and global conf file */
1277 pwd = getpwuid(getuid());
1279 errx(1, "invalid user %d", getuid());
1281 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
1282 if (stat(conf, &sb) != -1) {
1283 if (S_ISREG(sb.st_mode))
1286 /* try global conf file */
1287 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
1288 if (!stat(conf, &sb))
1289 if (S_ISREG(sb.st_mode))
1295 /* init all workspaces */
1296 for (i = 0; i < SWM_WS_MAX; i++) {
1302 ws[i].g.w = DisplayWidth(display, screen) - 2;
1303 ws[i].g.h = DisplayHeight(display, screen) - 2;
1304 TAILQ_INIT(&ws[i].winlist);
1305 ws[i].cur_layout = &layouts[0];
1307 /* make work space 1 active */
1310 /* grab existing windows */
1311 if (XQueryTree(display, root, &d1, &d2, &wins, &num)) {
1312 for (i = 0; i < num; i++) {
1313 if (!XGetWindowAttributes(display, wins[i], &wa)
1314 || wa.override_redirect ||
1315 XGetTransientForHint(display, wins[i], &d1))
1317 manage_window(wins[i]);
1322 ws[0].focus = TAILQ_FIRST(&ws[0].winlist);
1324 /* setup status bar */
1327 XSelectInput(display, root, SubstructureRedirectMask |
1328 SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
1329 EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
1330 FocusChangeMask | PropertyChangeMask | ExposureMask);
1336 XNextEvent(display, &e);
1337 if (handler[e.type])
1338 handler[e.type](&e);
1341 XCloseDisplay(display);