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 *);
108 int ignore_enter = 0;
109 unsigned int numlockmask = 0;
110 unsigned long color_focus = 0xff0000; /* XXX should this be per ws? */
111 unsigned long color_unfocus = 0x888888;
118 unsigned long bar_border = 0x008080;
119 unsigned long bar_color = 0x000000;
120 unsigned long bar_font_color = 0xa0a0a0;
126 char *bar_fonts[] = {
127 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
128 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
132 /* terminal + args */
133 char *spawn_term[] = { "xterm", NULL };
134 char *spawn_menu[] = { "dmenu_run", NULL };
135 char *spawn_scrotwm[] = { "scrotwm", NULL };
137 /* layout manager data */
139 void vertical_init(void);
140 void vertical_stack(void);
141 void horizontal_init(void);
142 void horizontal_stack(void);
144 void max_stack(void);
147 void (*l_init)(void); /* init/reset */
148 void (*l_stack)(void); /* restack windows */
151 { vertical_init, vertical_stack},
152 { horizontal_init, horizontal_stack},
153 /* XXX not working yet
154 * { max_init, max_stack},
160 TAILQ_ENTRY(ws_win) entry;
168 XWindowAttributes wa;
171 TAILQ_HEAD(ws_win_list, ws_win);
173 /* define work spaces */
174 #define SWM_WS_MAX (10)
176 int visible; /* workspace visible */
177 int restack; /* restack on switch */
178 struct layout *cur_layout; /* current layout handlers */
179 struct ws_win *focus; /* which win has focus */
180 struct ws_win_list winlist; /* list of windows in ws */
184 /* args to functions */
187 #define SWM_ARG_ID_FOCUSNEXT (0)
188 #define SWM_ARG_ID_FOCUSPREV (1)
189 #define SWM_ARG_ID_FOCUSMAIN (2)
190 #define SWM_ARG_ID_SWAPNEXT (3)
191 #define SWM_ARG_ID_SWAPPREV (4)
192 #define SWM_ARG_ID_SWAPMAIN (5)
198 #define SWM_CONF_WS "\n= \t"
199 #define SWM_CONF_FILE "scrotwm.conf"
201 conf_load(char *filename)
204 char *line, *cp, *var, *val;
205 size_t len, lineno = 0;
207 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
209 if (filename == NULL)
212 if ((config = fopen(filename, "r")) == NULL)
216 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
220 cp += (long)strspn(cp, SWM_CONF_WS);
226 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
228 cp += (long)strspn(cp, SWM_CONF_WS);
229 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
232 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
235 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
236 bar_enabled = atoi(val);
237 else if (!strncmp(var, "bar_border",
238 strlen("bar_border")))
239 bar_border = strtol(val, NULL, 16);
240 else if (!strncmp(var, "bar_color",
241 strlen("bar_color")))
242 bar_color = strtol(val, NULL, 16);
243 else if (!strncmp(var, "bar_font_color",
244 strlen("bar_font_color")))
245 bar_font_color = strtol(val, NULL, 16);
246 else if (!strncmp(var, "bar_font", strlen("bar_font")))
247 asprintf(&bar_fonts[0], "%s", val);
253 if (!strncmp(var, "color_focus", strlen("color_focus")))
254 color_focus = strtol(val, NULL, 16);
255 else if (!strncmp(var, "color_unfocus",
256 strlen("color_unfocus")))
257 color_unfocus = strtol(val, NULL, 16);
263 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
264 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
275 errx(1, "invalid conf file entry: %s=%s", var, val);
284 if (bar_enabled == 0)
288 XSetForeground(display, bar_gc, bar_color);
289 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
294 localtime_r(&tmt, &tm);
295 strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
296 XSetForeground(display, bar_gc, bar_font_color);
297 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
299 XSync(display, False);
307 /* XXX yeah yeah byte me */
312 bar_toggle(union arg *args)
316 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
320 height += bar_height; /* correct screen height */
321 XUnmapWindow(display, bar_window);
324 height -= bar_height; /* correct screen height */
325 XMapWindow(display, bar_window);
327 XSync(display, False);
328 for (i = 0; i < SWM_WS_MAX; i++)
332 bar_print(); /* must be after stack */
340 for (i = 0; bar_fonts[i] != NULL; i++) {
341 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
345 if (bar_fonts[i] == NULL)
346 errx(1, "couldn't load font");
347 bar_height = bar_fs->ascent + bar_fs->descent + 3;
349 bar_window = XCreateSimpleWindow(display, root, 0, 0, width,
350 bar_height - 2, 1, bar_border, bar_color);
351 bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv);
352 XSetFont(display, bar_gc, bar_fs->fid);
353 XSelectInput(display, bar_window, VisibilityChangeMask);
355 height -= bar_height; /* correct screen height */
356 XMapWindow(display, bar_window);
358 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %d\n", (int)bar_window);
360 if (signal(SIGALRM, bar_signal) == SIG_ERR)
361 err(1, "could not install bar_signal");
366 count_win(int wsid, int count_transient)
371 TAILQ_FOREACH (win, &ws[wsid].winlist, entry) {
372 if (count_transient == 0 && win->transient)
376 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
381 quit(union arg *args)
383 DNPRINTF(SWM_D_MISC, "quit\n");
389 restart(union arg *args)
391 XCloseDisplay(display);
392 execvp(args->argv[0], args->argv);
393 fprintf(stderr, "execvp failed\n");
399 spawn(union arg *args)
401 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
403 * The double-fork construct avoids zombie processes and keeps the code
404 * clean from stupid signal handlers.
409 close(ConnectionNumber(display));
411 execvp(args->argv[0], args->argv);
412 fprintf(stderr, "execvp failed\n");
421 focus_win(struct ws_win *win)
423 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
424 XSetWindowBorder(display, win->id, color_focus);
425 XSetInputFocus(display, win->id, RevertToPointerRoot, CurrentTime);
426 ws[current_ws].focus = win;
430 unfocus_win(struct ws_win *win)
432 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
433 XSetWindowBorder(display, win->id, color_unfocus);
434 if (ws[current_ws].focus == win)
435 ws[current_ws].focus = NULL;
439 switchws(union arg *args)
444 DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
446 if (wsid == current_ws)
449 /* map new window first to prevent ugly blinking */
450 TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
451 XMapRaised(display, win->id);
452 ws[wsid].visible = 1;
454 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
455 XUnmapWindow(display, win->id);
456 ws[current_ws].visible = 0;
461 if (ws[wsid].restack) {
465 if (ws[wsid].focus != NULL)
466 focus_win(ws[wsid].focus);
467 XSync(display, False);
472 swapwin(union arg *args)
474 struct ws_win *target;
476 DNPRINTF(SWM_D_MOVE, "swapwin: id %d\n", args->id);
477 if (ws[current_ws].focus == NULL)
481 case SWM_ARG_ID_SWAPPREV:
482 target = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
483 TAILQ_REMOVE(&ws[current_ws].winlist,
484 ws[current_ws].focus, entry);
486 TAILQ_INSERT_TAIL(&ws[current_ws].winlist,
487 ws[current_ws].focus, entry);
489 TAILQ_INSERT_BEFORE(target, ws[current_ws].focus,
492 case SWM_ARG_ID_SWAPNEXT:
493 target = TAILQ_NEXT(ws[current_ws].focus, entry);
494 TAILQ_REMOVE(&ws[current_ws].winlist,
495 ws[current_ws].focus, entry);
497 TAILQ_INSERT_HEAD(&ws[current_ws].winlist,
498 ws[current_ws].focus, entry);
500 TAILQ_INSERT_AFTER(&ws[current_ws].winlist, target,
501 ws[current_ws].focus, entry);
503 case SWM_ARG_ID_SWAPMAIN:
504 target = TAILQ_FIRST(&ws[current_ws].winlist);
505 if (target == ws[current_ws].focus)
507 TAILQ_REMOVE(&ws[current_ws].winlist, target, entry);
508 TAILQ_INSERT_BEFORE(ws[current_ws].focus, target, entry);
509 TAILQ_REMOVE(&ws[current_ws].winlist,
510 ws[current_ws].focus, entry);
511 TAILQ_INSERT_HEAD(&ws[current_ws].winlist,
512 ws[current_ws].focus, entry);
515 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
524 focus(union arg *args)
526 struct ws_win *winfocus, *winlostfocus;
528 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
529 if (ws[current_ws].focus == NULL)
532 winlostfocus = ws[current_ws].focus;
535 case SWM_ARG_ID_FOCUSPREV:
536 winfocus = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
537 if (winfocus == NULL)
539 TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
542 case SWM_ARG_ID_FOCUSNEXT:
543 winfocus = TAILQ_NEXT(ws[current_ws].focus, entry);
544 if (winfocus == NULL)
545 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
548 case SWM_ARG_ID_FOCUSMAIN:
549 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
556 if (winfocus == winlostfocus)
559 unfocus_win(winlostfocus);
561 XSync(display, False);
565 cycle_layout(union arg *args)
567 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", current_ws);
569 ws[current_ws].cur_layout++;
570 if (ws[current_ws].cur_layout->l_stack == NULL)
571 ws[current_ws].cur_layout = &layouts[0];
577 DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
579 ws[current_ws].restack = 0;
581 ws[current_ws].cur_layout->l_stack();
583 XSync(display, False);
589 DNPRINTF(SWM_D_EVENT, "vertical_init: workspace: %d\n", current_ws);
592 /* I know this sucks but it works well enough */
594 vertical_stack(void) {
596 struct ws_win wf, *win, *winfocus = &wf;
597 int i, h, w, x, y, hrh, winno;
601 DNPRINTF(SWM_D_EVENT, "vertical_stack: workspace: %d\n", current_ws);
605 winno = count_win(current_ws, 0);
615 hrh = height / (winno - 1);
620 y = bar_enabled ? bar_height : 0;
623 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
628 if (i != 0 && hrh != 0) {
629 /* correct the last window for lost pixels */
630 if (win == TAILQ_LAST(&ws[current_ws].winlist,
632 h = height - (i * hrh);
640 /* leave first right hand window at y = 0 */
646 if (win->transient != 0 || win->floating != 0)
651 bzero(&wc, sizeof wc);
656 win->width = wc.width = w;
657 win->height = wc.height = h;
658 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
660 /* make sure we don't clobber the screen */
661 if (win->wa.width > width)
662 win->wa.width = width;
663 if (win->wa.height > height)
664 win->wa.width = height;
665 win->x = wc.x = (width - win->wa.width) / 2;
666 win->y = wc.y = (height - win->wa.height) / 2;
667 mask = CWX | CWY | CWBorderWidth;
669 XConfigureWindow(display, win->id, mask, &wc);
671 if (win == ws[current_ws].focus)
675 XMapRaised(display, win->id);
679 focus_win(winfocus); /* this has to be done outside of the loop */
683 horizontal_init(void)
685 DNPRINTF(SWM_D_EVENT, "horizontal_init: workspace: %d\n", current_ws);
689 horizontal_stack(void) {
691 struct ws_win wf, *win, *winfocus = &wf;
692 int i, h, w, x, y, hrw, winno;
696 DNPRINTF(SWM_D_EVENT, "horizontal_stack: workspace: %d\n", current_ws);
700 winno = count_win(current_ws, 0);
710 hrw = width / (winno - 1);
715 y = bar_enabled ? bar_height : 0;
718 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
723 if (i != 0 && hrw != 0) {
724 /* correct the last window for lost pixels */
725 if (win == TAILQ_LAST(&ws[current_ws].winlist,
727 w = width - (i * hrw);
735 /* leave first bottom window at x = 0 */
741 if (win->transient != 0 || win->floating != 0)
746 bzero(&wc, sizeof wc);
751 win->width = wc.width = w;
752 win->height = wc.height = h;
753 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
755 /* make sure we don't clobber the screen */
756 if (win->wa.width > width)
757 win->wa.width = width;
758 if (win->wa.height > height)
759 win->wa.width = height;
760 win->x = wc.x = (width - win->wa.width) / 2;
761 win->y = wc.y = (height - win->wa.height) / 2;
762 mask = CWX | CWY | CWBorderWidth;
764 XConfigureWindow(display, win->id, mask, &wc);
766 if (win == ws[current_ws].focus)
770 XMapRaised(display, win->id);
774 focus_win(winfocus); /* this has to be done outside of the loop */
777 /* fullscreen view */
781 DNPRINTF(SWM_D_EVENT, "max_init: workspace: %d\n", current_ws);
787 struct ws_win wf, *win, *winfocus = &wf;
788 int i, h, w, x, y, winno;
791 DNPRINTF(SWM_D_EVENT, "max_stack: workspace: %d\n", current_ws);
795 winno = count_win(current_ws, 0);
800 y = bar_enabled ? bar_height : 0;
801 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
802 if (i == 1 && win->transient == 0 && win->floating != 0) {
808 bzero(&wc, sizeof wc);
812 win->width = wc.width = w;
813 win->height = wc.height = h;
814 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
815 XConfigureWindow(display, win->id, mask, &wc);
817 XMapRaised(display, win->id);
819 /* hide all but the master window */
820 XUnmapWindow(display, win->id);
825 focus_win(winfocus); /* this has to be done outside of the loop */
829 send_to_ws(union arg *args)
832 struct ws_win *win = ws[current_ws].focus;
834 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
836 XUnmapWindow(display, win->id);
838 /* find a window to focus */
839 ws[current_ws].focus = TAILQ_PREV(win, ws_win_list, entry);
840 if (ws[current_ws].focus == NULL)
841 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
842 if (ws[current_ws].focus == win)
843 ws[current_ws].focus = NULL;
845 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
847 TAILQ_INSERT_TAIL(&ws[wsid].winlist, win, entry);
848 if (count_win(wsid, 1) == 0)
849 ws[wsid].focus = win;
850 ws[wsid].restack = 1;
855 /* key definitions */
859 void (*func)(union arg *);
862 /* modifier key function argument */
863 /* XXX alt-c is temporary, until I figure out how to grab spacebar */
864 { MODKEY, XK_c, cycle_layout, {0} },
865 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
866 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
867 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
868 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
869 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
870 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
871 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
872 { MODKEY | ShiftMask, XK_q, quit, {0} },
873 { MODKEY, XK_q, restart, {.argv = spawn_scrotwm } },
874 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
875 { MODKEY, XK_1, switchws, {.id = 0} },
876 { MODKEY, XK_2, switchws, {.id = 1} },
877 { MODKEY, XK_3, switchws, {.id = 2} },
878 { MODKEY, XK_4, switchws, {.id = 3} },
879 { MODKEY, XK_5, switchws, {.id = 4} },
880 { MODKEY, XK_6, switchws, {.id = 5} },
881 { MODKEY, XK_7, switchws, {.id = 6} },
882 { MODKEY, XK_8, switchws, {.id = 7} },
883 { MODKEY, XK_9, switchws, {.id = 8} },
884 { MODKEY, XK_0, switchws, {.id = 9} },
885 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
886 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
887 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
888 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
889 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
890 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
891 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
892 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
893 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
894 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
895 { MODKEY, XK_b, bar_toggle, {0} },
896 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
897 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
901 updatenumlockmask(void)
904 XModifierKeymap *modmap;
906 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
908 modmap = XGetModifierMapping(display);
909 for (i = 0; i < 8; i++)
910 for (j = 0; j < modmap->max_keypermod; j++)
911 if (modmap->modifiermap[i * modmap->max_keypermod + j]
912 == XKeysymToKeycode(display, XK_Num_Lock))
913 numlockmask = (1 << i);
915 XFreeModifiermap(modmap);
923 unsigned int modifiers[] =
924 { 0, LockMask, numlockmask, numlockmask | LockMask };
926 DNPRINTF(SWM_D_MISC, "grabkeys\n");
929 XUngrabKey(display, AnyKey, AnyModifier, root);
930 for(i = 0; i < LENGTH(keys); i++) {
931 if((code = XKeysymToKeycode(display, keys[i].keysym)))
932 for(j = 0; j < LENGTH(modifiers); j++)
933 XGrabKey(display, code,
934 keys[i].mod | modifiers[j], root,
935 True, GrabModeAsync, GrabModeAsync);
941 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
949 XKeyEvent *ev = &e->xkey;
951 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
953 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
954 for(i = 0; i < LENGTH(keys); i++)
955 if(keysym == keys[i].keysym
956 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
958 keys[i].func(&(keys[i].args));
962 buttonpress(XEvent *e)
964 XButtonPressedEvent *ev = &e->xbutton;
965 #ifdef SWM_CLICKTOFOCUS
970 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
972 if (ev->window == root)
974 if (ev->window == ws[current_ws].focus->id)
976 #ifdef SWM_CLICKTOFOCUS
977 TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
978 if (win->id == ev->window) {
979 /* focus in the clicked window */
980 XSetWindowBorder(display, ev->window, 0xff0000);
981 XSetWindowBorder(display,
982 ws[current_ws].focus->id, 0x888888);
983 XSetInputFocus(display, ev->window, RevertToPointerRoot,
985 ws[current_ws].focus = win;
986 XSync(display, False);
993 manage_window(Window id)
997 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
999 return (win); /* already being managed */
1002 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1003 errx(1, "calloc: failed to allocate memory for new window");
1006 TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
1008 XSelectInput(display, id, ButtonPressMask | EnterWindowMask |
1009 FocusChangeMask | ExposureMask);
1016 configurerequest(XEvent *e)
1018 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1022 DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window);
1025 win = manage_window(ev->window);
1026 ws[current_ws].focus = win; /* make new win focused */
1028 XGetTransientForHint(display, win->id, &trans);
1030 win->transient = trans;
1031 DNPRINTF(SWM_D_MISC, "configurerequest: win %u transient %u\n",
1032 (unsigned)win->id, win->transient);
1034 XGetWindowAttributes(display, win->id, &win->wa);
1036 XClassHint ch = { 0 };
1037 if(XGetClassHint(display, win->id, &ch)) {
1038 fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name);
1039 if (!strcmp(ch.res_class, "Gvim") && !strcmp(ch.res_name, "gvim")) {
1043 XFree(ch.res_class);
1052 configurenotify(XEvent *e)
1054 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1055 e->xconfigure.window);
1059 destroynotify(XEvent *e)
1062 XDestroyWindowEvent *ev = &e->xdestroywindow;
1064 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1066 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1067 if (ev->window == win->id) {
1068 /* find a window to focus */
1069 ws[current_ws].focus = TAILQ_PREV(win,
1070 ws_win_list, entry);
1071 if (ws[current_ws].focus == NULL)
1072 ws[current_ws].focus =
1073 TAILQ_FIRST(&ws[current_ws].winlist);
1074 if (win == ws[current_ws].focus)
1075 ws[current_ws].focus = NULL;
1077 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
1087 enternotify(XEvent *e)
1089 XCrossingEvent *ev = &e->xcrossing;
1092 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1094 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
1098 /* eat event(s) to prevent autofocus */
1102 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1103 if (win->id == ev->window)
1113 XFocusChangeEvent *ev = &e->xfocus;
1115 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window);
1117 XSync(display, False); /* not sure this helps redrawing graphic apps */
1119 if (ev->window == root)
1122 * kill grab for now so that we can cut and paste , this screws up
1126 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1127 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1128 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1133 mappingnotify(XEvent *e)
1135 XMappingEvent *ev = &e->xmapping;
1137 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1139 XRefreshKeyboardMapping(ev);
1140 if(ev->request == MappingKeyboard)
1145 maprequest(XEvent *e)
1147 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1148 e->xmaprequest.window);
1150 manage_window(e->xmaprequest.window);
1155 propertynotify(XEvent *e)
1157 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1158 e->xproperty.window);
1162 unmapnotify(XEvent *e)
1164 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1168 visibilitynotify(XEvent *e)
1170 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n", e->xvisibility.window);
1172 if (e->xvisibility.window == bar_window &&
1173 e->xvisibility.state == VisibilityUnobscured)
1177 void (*handler[LASTEvent])(XEvent *) = {
1179 [KeyPress] = keypress,
1180 [ButtonPress] = buttonpress,
1181 [ConfigureRequest] = configurerequest,
1182 [ConfigureNotify] = configurenotify,
1183 [DestroyNotify] = destroynotify,
1184 [EnterNotify] = enternotify,
1185 [FocusIn] = focusin,
1186 [MappingNotify] = mappingnotify,
1187 [MapRequest] = maprequest,
1188 [PropertyNotify] = propertynotify,
1189 [UnmapNotify] = unmapnotify,
1190 [VisibilityNotify] = visibilitynotify,
1194 xerror_start(Display *d, XErrorEvent *ee)
1201 xerror(Display *d, XErrorEvent *ee)
1203 fprintf(stderr, "error: %p %p\n", display, ee);
1212 xerrorxlib = XSetErrorHandler(xerror_start);
1214 /* this causes an error if some other window manager is running */
1215 XSelectInput(display, DefaultRootWindow(display),
1216 SubstructureRedirectMask);
1217 XSync(display, False);
1221 XSetErrorHandler(xerror);
1222 XSync(display, False);
1227 main(int argc, char *argv[])
1230 char conf[PATH_MAX], *cfile = NULL;
1233 unsigned int i, num;
1234 Window d1, d2, *wins = NULL;
1235 XWindowAttributes wa;
1237 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1238 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1239 warnx("no locale support");
1241 if(!(display = XOpenDisplay(0)))
1242 errx(1, "can not open display");
1245 errx(1, "other wm running");
1247 screen = DefaultScreen(display);
1248 root = RootWindow(display, screen);
1249 width = DisplayWidth(display, screen) - 2;
1250 height = DisplayHeight(display, screen) - 2;
1252 /* look for local and global conf file */
1253 pwd = getpwuid(getuid());
1255 errx(1, "invalid user %d", getuid());
1257 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
1258 if (stat(conf, &sb) != -1) {
1259 if (S_ISREG(sb.st_mode))
1262 /* try global conf file */
1263 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
1264 if (!stat(conf, &sb))
1265 if (S_ISREG(sb.st_mode))
1271 /* init all workspaces */
1272 for (i = 0; i < SWM_WS_MAX; i++) {
1276 TAILQ_INIT(&ws[i].winlist);
1277 ws[i].cur_layout = &layouts[0];
1279 /* make work space 1 active */
1282 /* grab existing windows */
1283 if (XQueryTree(display, root, &d1, &d2, &wins, &num)) {
1284 for (i = 0; i < num; i++) {
1285 if (!XGetWindowAttributes(display, wins[i], &wa)
1286 || wa.override_redirect ||
1287 XGetTransientForHint(display, wins[i], &d1))
1289 manage_window(wins[i]);
1294 ws[0].focus = TAILQ_FIRST(&ws[0].winlist);
1296 /* setup status bar */
1299 XSelectInput(display, root, SubstructureRedirectMask |
1300 SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
1301 EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
1302 FocusChangeMask | PropertyChangeMask | ExposureMask);
1308 XNextEvent(display, &e);
1309 if (handler[e.type])
1310 handler[e.type](&e);
1313 XCloseDisplay(display);