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)
159 XSetForeground(display, bar_gc, 0x000000);
160 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
165 localtime_r(&tmt, &tm);
166 strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
167 XSetForeground(display, bar_gc, 0xa0a0a0);
168 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
170 XSync(display, False);
178 /* XXX yeah yeah byte me */
183 quit(union arg *args)
185 DNPRINTF(SWM_D_MISC, "quit\n");
190 spawn(union arg *args)
192 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
194 * The double-fork construct avoids zombie processes and keeps the code
195 * clean from stupid signal handlers.
200 close(ConnectionNumber(display));
202 execvp(args->argv[0], args->argv);
203 fprintf(stderr, "execvp failed\n");
212 focus_win(struct ws_win *win)
214 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
215 XSetWindowBorder(display, win->id, col_focus);
216 XSetInputFocus(display, win->id, RevertToPointerRoot, CurrentTime);
217 ws[current_ws].focus = win;
221 unfocus_win(struct ws_win *win)
223 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
224 XSetWindowBorder(display, win->id, col_unfocus);
225 if (ws[current_ws].focus == win)
226 ws[current_ws].focus = NULL;
230 switchws(union arg *args)
235 DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
237 if (wsid == current_ws)
240 /* map new window first to prevent ugly blinking */
241 TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
242 XMapWindow(display, win->id);
243 ws[wsid].visible = 1;
245 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
246 XUnmapWindow(display, win->id);
247 ws[current_ws].visible = 0;
252 if (ws[wsid].restack) {
255 if (ws[wsid].focus != NULL)
256 focus_win(ws[wsid].focus);
257 XSync(display, False);
262 focus(union arg *args)
264 struct ws_win *winfocus, *winlostfocus;
266 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
267 if (ws[current_ws].focus == NULL || ws[current_ws].winno == 0)
270 winlostfocus = ws[current_ws].focus;
273 case SWM_ARG_ID_FOCUSPREV:
274 if (ws[current_ws].focus ==
275 TAILQ_FIRST(&ws[current_ws].winlist))
276 ws[current_ws].focus =
277 TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
279 ws[current_ws].focus =TAILQ_PREV(ws[current_ws].focus,
283 case SWM_ARG_ID_FOCUSNEXT:
284 if (ws[current_ws].focus == TAILQ_LAST(&ws[current_ws].winlist,
286 ws[current_ws].focus =
287 TAILQ_FIRST(&ws[current_ws].winlist);
289 ws[current_ws].focus =
290 TAILQ_NEXT(ws[current_ws].focus, entry);
293 case SWM_ARG_ID_FOCUSMAIN:
294 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);;
301 winfocus = ws[current_ws].focus;
302 unfocus_win(winlostfocus);
304 XSync(display, False);
307 /* I know this sucks but it works well enough */
312 struct ws_win wf, *win, *winfocus = &wf;
313 int i, h, w, x, y, hrh;
315 DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
319 if (ws[current_ws].winno == 0)
322 if (ws[current_ws].winno > 1)
327 if (ws[current_ws].winno > 2)
328 hrh = height / (ws[current_ws].winno - 1);
336 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
341 if (i != 0 && hrh != 0) {
342 /* correct the last window for lost pixels */
343 if (win == TAILQ_LAST(&ws[current_ws].winlist,
345 h = height - (i * hrh);
353 /* leave first right hand window at y = 0 */
359 bzero(&wc, sizeof wc);
362 win->width = wc.width = w;
363 win->height = wc.height = h;
365 XConfigureWindow(display, win->id, CWX | CWY | CWWidth |
366 CWHeight | CWBorderWidth, &wc);
367 if (win == ws[current_ws].focus)
371 XMapWindow(display, win->id);
375 focus_win(winfocus); /* this has to be done outside of the loop */
376 XSync(display, False);
380 swap_to_main(union arg *args)
382 struct ws_win *tmpwin = TAILQ_FIRST(&ws[current_ws].winlist);
384 DNPRINTF(SWM_D_MISC, "swap_to_main: win: %lu\n",
385 ws[current_ws].focus ? ws[current_ws].focus->id : 0);
387 if (ws[current_ws].focus == NULL || ws[current_ws].focus == tmpwin)
390 TAILQ_REMOVE(&ws[current_ws].winlist, tmpwin, entry);
391 TAILQ_INSERT_AFTER(&ws[current_ws].winlist, ws[current_ws].focus,
393 TAILQ_REMOVE(&ws[current_ws].winlist, ws[current_ws].focus, entry);
394 TAILQ_INSERT_HEAD(&ws[current_ws].winlist, ws[current_ws].focus, entry);
395 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
401 send_to_ws(union arg *args)
404 struct ws_win *win = ws[current_ws].focus;
406 DNPRINTF(SWM_D_WS, "send_to_ws: win: %lu\n", win->id);
408 XUnmapWindow(display, win->id);
410 /* find a window to focus */
411 ws[current_ws].focus = TAILQ_PREV(win, ws_win_list,entry);
412 if (ws[current_ws].focus == NULL)
413 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
415 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
416 ws[current_ws].winno--;
418 TAILQ_INSERT_TAIL(&ws[wsid].winlist, win, entry);
419 if (ws[wsid].winno == 0)
420 ws[wsid].focus = win;
422 ws[wsid].restack = 1;
428 /* terminal + args */
429 char *term[] = { "xterm", NULL };
431 /* key definitions */
435 void (*func)(union arg *);
438 /* modifier key function argument */
439 { MODKEY, XK_Return, swap_to_main, {0} },
440 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = term } },
441 { MODKEY | ShiftMask, XK_q, quit, {0} },
442 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
443 { MODKEY, XK_1, switchws, {.id = 0} },
444 { MODKEY, XK_2, switchws, {.id = 1} },
445 { MODKEY, XK_3, switchws, {.id = 2} },
446 { MODKEY, XK_4, switchws, {.id = 3} },
447 { MODKEY, XK_5, switchws, {.id = 4} },
448 { MODKEY, XK_6, switchws, {.id = 5} },
449 { MODKEY, XK_7, switchws, {.id = 6} },
450 { MODKEY, XK_8, switchws, {.id = 7} },
451 { MODKEY, XK_9, switchws, {.id = 8} },
452 { MODKEY, XK_0, switchws, {.id = 9} },
453 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
454 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
455 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
456 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
457 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
458 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
459 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
460 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
461 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
462 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
463 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
464 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
465 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
469 updatenumlockmask(void)
472 XModifierKeymap *modmap;
474 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
476 modmap = XGetModifierMapping(display);
477 for (i = 0; i < 8; i++)
478 for (j = 0; j < modmap->max_keypermod; j++)
479 if (modmap->modifiermap[i * modmap->max_keypermod + j]
480 == XKeysymToKeycode(display, XK_Num_Lock))
481 numlockmask = (1 << i);
483 XFreeModifiermap(modmap);
491 unsigned int modifiers[] =
492 { 0, LockMask, numlockmask, numlockmask | LockMask };
494 DNPRINTF(SWM_D_MISC, "grabkeys\n");
497 XUngrabKey(display, AnyKey, AnyModifier, root);
498 for(i = 0; i < LENGTH(keys); i++) {
499 if((code = XKeysymToKeycode(display, keys[i].keysym)))
500 for(j = 0; j < LENGTH(modifiers); j++)
501 XGrabKey(display, code,
502 keys[i].mod | modifiers[j], root,
503 True, GrabModeAsync, GrabModeAsync);
509 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
517 XKeyEvent *ev = &e->xkey;
519 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
521 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
522 for(i = 0; i < LENGTH(keys); i++)
523 if(keysym == keys[i].keysym
524 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
526 keys[i].func(&(keys[i].args));
530 buttonpress(XEvent *e)
532 XButtonPressedEvent *ev = &e->xbutton;
533 #ifdef SWM_CLICKTOFOCUS
538 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
540 if (ev->window == root)
542 if (ev->window == ws[current_ws].focus->id)
544 #ifdef SWM_CLICKTOFOCUS
545 TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
546 if (win->id == ev->window) {
547 /* focus in the clicked window */
548 XSetWindowBorder(display, ev->window, 0xff0000);
549 XSetWindowBorder(display,
550 ws[current_ws].focus->id, 0x888888);
551 XSetInputFocus(display, ev->window, RevertToPointerRoot,
553 ws[current_ws].focus = win;
554 XSync(display, False);
561 configurerequest(XEvent *e)
563 XConfigureRequestEvent *ev = &e->xconfigurerequest;
566 DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window);
568 XSelectInput(display, ev->window, ButtonPressMask | EnterWindowMask |
571 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
572 errx(1, "calloc: failed to allocate memory for new window");
574 win->id = ev->window;
575 TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
576 ws[current_ws].focus = win; /* make new win focused */
577 ws[current_ws].winno++;
582 configurenotify(XEvent *e)
584 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
585 e->xconfigure.window);
589 destroynotify(XEvent *e)
592 XDestroyWindowEvent *ev = &e->xdestroywindow;
594 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
596 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
597 if (ev->window == win->id) {
598 /* find a window to focus */
599 ws[current_ws].focus =
600 TAILQ_PREV(win, ws_win_list,entry);
601 if (ws[current_ws].focus == NULL)
602 ws[current_ws].focus =
603 TAILQ_FIRST(&ws[current_ws].winlist);
605 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
607 ws[current_ws].winno--;
616 enternotify(XEvent *e)
618 XCrossingEvent *ev = &e->xcrossing;
621 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
623 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
627 /* eat event(s) to prevent autofocus */
631 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
632 if (win->id == ev->window)
642 XFocusChangeEvent *ev = &e->xfocus;
644 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window);
646 if (ev->window == root)
650 * kill grab for now so that we can cut and paste , this screws up
654 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
655 XGrabButton(display, Button1, AnyModifier, ev->window, False,
656 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
661 mappingnotify(XEvent *e)
663 XMappingEvent *ev = &e->xmapping;
665 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
667 XRefreshKeyboardMapping(ev);
668 if(ev->request == MappingKeyboard)
673 maprequest(XEvent *e)
675 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
676 e->xmaprequest.window);
680 propertynotify(XEvent *e)
682 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
683 e->xproperty.window);
687 unmapnotify(XEvent *e)
689 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
692 void (*handler[LASTEvent])(XEvent *) = {
694 [KeyPress] = keypress,
695 [ButtonPress] = buttonpress,
696 [ConfigureRequest] = configurerequest,
697 [ConfigureNotify] = configurenotify,
698 [DestroyNotify] = destroynotify,
699 [EnterNotify] = enternotify,
701 [MappingNotify] = mappingnotify,
702 [MapRequest] = maprequest,
703 [PropertyNotify] = propertynotify,
704 [UnmapNotify] = unmapnotify,
708 xerror_start(Display *d, XErrorEvent *ee)
715 xerror(Display *d, XErrorEvent *ee)
717 fprintf(stderr, "error: %p %p\n", display, ee);
726 xerrorxlib = XSetErrorHandler(xerror_start);
728 /* this causes an error if some other window manager is running */
729 XSelectInput(display, DefaultRootWindow(display),
730 SubstructureRedirectMask);
731 XSync(display, False);
735 XSetErrorHandler(xerror);
736 XSync(display, False);
741 main(int argc, char *argv[])
746 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
747 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
748 warnx("no locale support");
750 if(!(display = XOpenDisplay(0)))
751 errx(1, "can not open display");
754 errx(1, "other wm running");
756 screen = DefaultScreen(display);
757 root = RootWindow(display, screen);
758 width = DisplayWidth(display, screen) - 2;
759 height = DisplayHeight(display, screen) - 2;
761 /* make work space 1 active */
766 TAILQ_INIT(&ws[0].winlist);
767 for (i = 1; i < SWM_WS_MAX; i++) {
772 TAILQ_INIT(&ws[i].winlist);
775 /* setup status bar */
776 bar_fs = XLoadQueryFont(display,
777 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*");
778 if (bar_fs == NULL) {
779 /* load a font that is default */
780 bar_fs = XLoadQueryFont(display,
781 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*");
783 errx(1, "couldn't load font");
785 bar_height = bar_fs->ascent + bar_fs->descent + 3;
787 XSelectInput(display, root, SubstructureRedirectMask |
788 SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
789 EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
790 FocusChangeMask | PropertyChangeMask);
794 bar_window = XCreateSimpleWindow(display, root, 0, 0, width,
795 bar_height - 2, 1, 0x008080, 0x000000);
796 bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv);
797 XSetFont(display, bar_gc, bar_fs->fid);
799 height -= bar_height; /* correct screen height */
800 XMapWindow(display, bar_window);
803 if (signal(SIGALRM, bar_signal) == SIG_ERR)
804 err(1, "could not install bar_signal");
808 XNextEvent(display, &e);
813 XCloseDisplay(display);