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"
62 #include <sys/types.h>
64 #include <sys/queue.h>
66 #include <X11/cursorfont.h>
67 #include <X11/keysym.h>
68 #include <X11/Xatom.h>
70 #include <X11/Xproto.h>
71 #include <X11/Xutil.h>
73 /* #define SWM_DEBUG */
75 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
76 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
77 #define SWM_D_EVENT 0x0001
78 #define SWM_D_WS 0x0002
79 #define SWM_D_FOCUS 0x0004
80 #define SWM_D_MISC 0x0008
82 uint32_t swm_debug = 0
90 #define DNPRINTF(n,x...)
93 #define LENGTH(x) (sizeof x / sizeof x[0])
94 #define MODKEY Mod1Mask
95 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
97 int (*xerrorxlib)(Display *, XErrorEvent *);
102 int ignore_enter = 0;
103 unsigned int numlockmask = 0;
104 unsigned long col_focus = 0xff0000; /* XXX should this be per ws? */
105 unsigned long col_unfocus = 0x888888;
119 TAILQ_ENTRY(ws_win) entry;
127 TAILQ_HEAD(ws_win_list, ws_win);
129 /* define work spaces */
130 #define SWM_WS_MAX (10)
132 int visible; /* workspace visible */
133 int restack; /* restack on switch */
134 struct ws_win *focus; /* which win has focus */
135 int winno; /* total nr of windows */
136 struct ws_win_list winlist; /* list of windows in ws */
140 /* args to functions */
143 #define SWM_ARG_ID_FOCUSNEXT (0)
144 #define SWM_ARG_ID_FOCUSPREV (1)
145 #define SWM_ARG_ID_FOCUSMAIN (2)
158 if (bar_enabled == 0)
162 XSetForeground(display, bar_gc, 0x000000);
163 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
168 localtime_r(&tmt, &tm);
169 strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
170 XSetForeground(display, bar_gc, 0xa0a0a0);
171 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
173 XSync(display, False);
181 /* XXX yeah yeah byte me */
186 bar_toggle(union arg *args)
190 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
194 height += bar_height; /* correct screen height */
195 XUnmapWindow(display, bar_window);
198 height -= bar_height; /* correct screen height */
199 XMapWindow(display, bar_window);
201 XSync(display, False);
202 for (i = 0; i < SWM_WS_MAX; i++)
206 bar_print(); /* must be after stack */
210 quit(union arg *args)
212 DNPRINTF(SWM_D_MISC, "quit\n");
217 spawn(union arg *args)
219 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
221 * The double-fork construct avoids zombie processes and keeps the code
222 * clean from stupid signal handlers.
227 close(ConnectionNumber(display));
229 execvp(args->argv[0], args->argv);
230 fprintf(stderr, "execvp failed\n");
239 focus_win(struct ws_win *win)
241 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
242 XSetWindowBorder(display, win->id, col_focus);
243 XSetInputFocus(display, win->id, RevertToPointerRoot, CurrentTime);
244 ws[current_ws].focus = win;
248 unfocus_win(struct ws_win *win)
250 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
251 XSetWindowBorder(display, win->id, col_unfocus);
252 if (ws[current_ws].focus == win)
253 ws[current_ws].focus = NULL;
257 switchws(union arg *args)
262 DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
264 if (wsid == current_ws)
267 /* map new window first to prevent ugly blinking */
268 TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
269 XMapWindow(display, win->id);
270 ws[wsid].visible = 1;
272 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
273 XUnmapWindow(display, win->id);
274 ws[current_ws].visible = 0;
279 if (ws[wsid].restack) {
282 if (ws[wsid].focus != NULL)
283 focus_win(ws[wsid].focus);
284 XSync(display, False);
289 focus(union arg *args)
291 struct ws_win *winfocus, *winlostfocus;
293 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
294 if (ws[current_ws].focus == NULL || ws[current_ws].winno == 0)
297 winlostfocus = ws[current_ws].focus;
300 case SWM_ARG_ID_FOCUSPREV:
301 ws[current_ws].focus =
302 TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
303 if (ws[current_ws].focus == NULL)
304 ws[current_ws].focus =
305 TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
308 case SWM_ARG_ID_FOCUSNEXT:
309 ws[current_ws].focus = TAILQ_NEXT(ws[current_ws].focus, entry);
310 if (ws[current_ws].focus == NULL)
311 ws[current_ws].focus =
312 TAILQ_FIRST(&ws[current_ws].winlist);
315 case SWM_ARG_ID_FOCUSMAIN:
316 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
323 winfocus = ws[current_ws].focus;
324 unfocus_win(winlostfocus);
326 XSync(display, False);
329 /* I know this sucks but it works well enough */
334 struct ws_win wf, *win, *winfocus = &wf;
335 int i, h, w, x, y, hrh;
337 DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
341 ws[current_ws].restack = 0;
343 if (ws[current_ws].winno == 0)
346 if (ws[current_ws].winno > 1)
351 if (ws[current_ws].winno > 2)
352 hrh = height / (ws[current_ws].winno - 1);
357 y = bar_enabled ? bar_height : 0;
360 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
365 if (i != 0 && hrh != 0) {
366 /* correct the last window for lost pixels */
367 if (win == TAILQ_LAST(&ws[current_ws].winlist,
369 h = height - (i * hrh);
377 /* leave first right hand window at y = 0 */
383 bzero(&wc, sizeof wc);
386 win->width = wc.width = w;
387 win->height = wc.height = h;
389 XConfigureWindow(display, win->id, CWX | CWY | CWWidth |
390 CWHeight | CWBorderWidth, &wc);
391 if (win == ws[current_ws].focus)
395 XMapWindow(display, win->id);
399 focus_win(winfocus); /* this has to be done outside of the loop */
400 XSync(display, False);
404 swap_to_main(union arg *args)
406 struct ws_win *tmpwin = TAILQ_FIRST(&ws[current_ws].winlist);
408 DNPRINTF(SWM_D_MISC, "swap_to_main: win: %lu\n",
409 ws[current_ws].focus ? ws[current_ws].focus->id : 0);
411 if (ws[current_ws].focus == NULL || ws[current_ws].focus == tmpwin)
414 TAILQ_REMOVE(&ws[current_ws].winlist, tmpwin, entry);
415 TAILQ_INSERT_AFTER(&ws[current_ws].winlist, ws[current_ws].focus,
417 TAILQ_REMOVE(&ws[current_ws].winlist, ws[current_ws].focus, entry);
418 TAILQ_INSERT_HEAD(&ws[current_ws].winlist, ws[current_ws].focus, entry);
419 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
425 send_to_ws(union arg *args)
428 struct ws_win *win = ws[current_ws].focus;
430 DNPRINTF(SWM_D_WS, "send_to_ws: win: %lu\n", win->id);
432 XUnmapWindow(display, win->id);
434 /* find a window to focus */
435 ws[current_ws].focus = TAILQ_PREV(win, ws_win_list,entry);
436 if (ws[current_ws].focus == NULL)
437 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
439 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
440 ws[current_ws].winno--;
442 TAILQ_INSERT_TAIL(&ws[wsid].winlist, win, entry);
443 if (ws[wsid].winno == 0)
444 ws[wsid].focus = win;
446 ws[wsid].restack = 1;
451 /* terminal + args */
452 char *term[] = { "xterm", NULL };
454 /* key definitions */
458 void (*func)(union arg *);
461 /* modifier key function argument */
462 { MODKEY, XK_Return, swap_to_main, {0} },
463 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = term } },
464 { MODKEY | ShiftMask, XK_q, quit, {0} },
465 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
466 { MODKEY, XK_1, switchws, {.id = 0} },
467 { MODKEY, XK_2, switchws, {.id = 1} },
468 { MODKEY, XK_3, switchws, {.id = 2} },
469 { MODKEY, XK_4, switchws, {.id = 3} },
470 { MODKEY, XK_5, switchws, {.id = 4} },
471 { MODKEY, XK_6, switchws, {.id = 5} },
472 { MODKEY, XK_7, switchws, {.id = 6} },
473 { MODKEY, XK_8, switchws, {.id = 7} },
474 { MODKEY, XK_9, switchws, {.id = 8} },
475 { MODKEY, XK_0, switchws, {.id = 9} },
476 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
477 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
478 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
479 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
480 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
481 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
482 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
483 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
484 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
485 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
486 { MODKEY, XK_b, bar_toggle, {0} },
487 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
488 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
492 updatenumlockmask(void)
495 XModifierKeymap *modmap;
497 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
499 modmap = XGetModifierMapping(display);
500 for (i = 0; i < 8; i++)
501 for (j = 0; j < modmap->max_keypermod; j++)
502 if (modmap->modifiermap[i * modmap->max_keypermod + j]
503 == XKeysymToKeycode(display, XK_Num_Lock))
504 numlockmask = (1 << i);
506 XFreeModifiermap(modmap);
514 unsigned int modifiers[] =
515 { 0, LockMask, numlockmask, numlockmask | LockMask };
517 DNPRINTF(SWM_D_MISC, "grabkeys\n");
520 XUngrabKey(display, AnyKey, AnyModifier, root);
521 for(i = 0; i < LENGTH(keys); i++) {
522 if((code = XKeysymToKeycode(display, keys[i].keysym)))
523 for(j = 0; j < LENGTH(modifiers); j++)
524 XGrabKey(display, code,
525 keys[i].mod | modifiers[j], root,
526 True, GrabModeAsync, GrabModeAsync);
532 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
540 XKeyEvent *ev = &e->xkey;
542 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
544 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
545 for(i = 0; i < LENGTH(keys); i++)
546 if(keysym == keys[i].keysym
547 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
549 keys[i].func(&(keys[i].args));
553 buttonpress(XEvent *e)
555 XButtonPressedEvent *ev = &e->xbutton;
556 #ifdef SWM_CLICKTOFOCUS
561 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
563 if (ev->window == root)
565 if (ev->window == ws[current_ws].focus->id)
567 #ifdef SWM_CLICKTOFOCUS
568 TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
569 if (win->id == ev->window) {
570 /* focus in the clicked window */
571 XSetWindowBorder(display, ev->window, 0xff0000);
572 XSetWindowBorder(display,
573 ws[current_ws].focus->id, 0x888888);
574 XSetInputFocus(display, ev->window, RevertToPointerRoot,
576 ws[current_ws].focus = win;
577 XSync(display, False);
584 configurerequest(XEvent *e)
586 XConfigureRequestEvent *ev = &e->xconfigurerequest;
589 DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window);
591 XSelectInput(display, ev->window, ButtonPressMask | EnterWindowMask |
594 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
595 errx(1, "calloc: failed to allocate memory for new window");
597 win->id = ev->window;
598 TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
599 ws[current_ws].focus = win; /* make new win focused */
600 ws[current_ws].winno++;
605 configurenotify(XEvent *e)
607 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
608 e->xconfigure.window);
612 destroynotify(XEvent *e)
615 XDestroyWindowEvent *ev = &e->xdestroywindow;
617 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
619 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
620 if (ev->window == win->id) {
621 /* find a window to focus */
622 ws[current_ws].focus =
623 TAILQ_PREV(win, ws_win_list,entry);
624 if (ws[current_ws].focus == NULL)
625 ws[current_ws].focus =
626 TAILQ_FIRST(&ws[current_ws].winlist);
628 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
630 ws[current_ws].winno--;
639 enternotify(XEvent *e)
641 XCrossingEvent *ev = &e->xcrossing;
644 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
646 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
650 /* eat event(s) to prevent autofocus */
654 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
655 if (win->id == ev->window)
665 XFocusChangeEvent *ev = &e->xfocus;
667 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window);
669 if (ev->window == root)
673 * kill grab for now so that we can cut and paste , this screws up
677 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
678 XGrabButton(display, Button1, AnyModifier, ev->window, False,
679 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
684 mappingnotify(XEvent *e)
686 XMappingEvent *ev = &e->xmapping;
688 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
690 XRefreshKeyboardMapping(ev);
691 if(ev->request == MappingKeyboard)
696 maprequest(XEvent *e)
698 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
699 e->xmaprequest.window);
703 propertynotify(XEvent *e)
705 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
706 e->xproperty.window);
710 unmapnotify(XEvent *e)
712 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
715 void (*handler[LASTEvent])(XEvent *) = {
717 [KeyPress] = keypress,
718 [ButtonPress] = buttonpress,
719 [ConfigureRequest] = configurerequest,
720 [ConfigureNotify] = configurenotify,
721 [DestroyNotify] = destroynotify,
722 [EnterNotify] = enternotify,
724 [MappingNotify] = mappingnotify,
725 [MapRequest] = maprequest,
726 [PropertyNotify] = propertynotify,
727 [UnmapNotify] = unmapnotify,
731 xerror_start(Display *d, XErrorEvent *ee)
738 xerror(Display *d, XErrorEvent *ee)
740 fprintf(stderr, "error: %p %p\n", display, ee);
749 xerrorxlib = XSetErrorHandler(xerror_start);
751 /* this causes an error if some other window manager is running */
752 XSelectInput(display, DefaultRootWindow(display),
753 SubstructureRedirectMask);
754 XSync(display, False);
758 XSetErrorHandler(xerror);
759 XSync(display, False);
764 main(int argc, char *argv[])
769 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
770 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
771 warnx("no locale support");
773 if(!(display = XOpenDisplay(0)))
774 errx(1, "can not open display");
777 errx(1, "other wm running");
779 screen = DefaultScreen(display);
780 root = RootWindow(display, screen);
781 width = DisplayWidth(display, screen) - 2;
782 height = DisplayHeight(display, screen) - 2;
784 /* make work space 1 active */
789 TAILQ_INIT(&ws[0].winlist);
790 for (i = 1; i < SWM_WS_MAX; i++) {
795 TAILQ_INIT(&ws[i].winlist);
798 /* setup status bar */
799 bar_fs = XLoadQueryFont(display,
800 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*");
801 if (bar_fs == NULL) {
802 /* load a font that is default */
803 bar_fs = XLoadQueryFont(display,
804 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*");
806 errx(1, "couldn't load font");
808 bar_height = bar_fs->ascent + bar_fs->descent + 3;
810 XSelectInput(display, root, SubstructureRedirectMask |
811 SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
812 EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
813 FocusChangeMask | PropertyChangeMask);
817 bar_window = XCreateSimpleWindow(display, root, 0, 0, width,
818 bar_height - 2, 1, 0x008080, 0x000000);
819 bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv);
820 XSetFont(display, bar_gc, bar_fs->fid);
822 height -= bar_height; /* correct screen height */
823 XMapWindow(display, bar_window);
826 if (signal(SIGALRM, bar_signal) == SIG_ERR)
827 err(1, "could not install bar_signal");
831 XNextEvent(display, &e);
836 XCloseDisplay(display);