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"
61 #include <sys/types.h>
63 #include <sys/queue.h>
65 #include <X11/cursorfont.h>
66 #include <X11/keysym.h>
67 #include <X11/Xatom.h>
69 #include <X11/Xproto.h>
70 #include <X11/Xutil.h>
72 /* #define SWM_DEBUG */
74 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
75 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
76 #define SWM_D_EVENT 0x0001
77 #define SWM_D_WS 0x0002
78 #define SWM_D_FOCUS 0x0004
79 #define SWM_D_MISC 0x0008
81 uint32_t swm_debug = 0
89 #define DNPRINTF(n,x...)
92 #define LENGTH(x) (sizeof x / sizeof x[0])
93 #define MODKEY Mod1Mask
94 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
96 int (*xerrorxlib)(Display *, XErrorEvent *);
101 int ignore_enter = 0;
102 unsigned int numlockmask = 0;
103 unsigned long col_focus = 0xff0000; /* XXX should this be per ws? */
104 unsigned long col_unfocus = 0x888888;
118 TAILQ_ENTRY(ws_win) entry;
126 TAILQ_HEAD(ws_win_list, ws_win);
128 /* define work spaces */
129 #define SWM_WS_MAX (10)
131 int visible; /* workspace visible */
132 int restack; /* restack on switch */
133 struct ws_win *focus; /* which win has focus */
134 int winno; /* total nr of windows */
135 struct ws_win_list winlist; /* list of windows in ws */
139 /* args to functions */
142 #define SWM_ARG_ID_FOCUSNEXT (0)
143 #define SWM_ARG_ID_FOCUSPREV (1)
144 #define SWM_ARG_ID_FOCUSMAIN (2)
158 XSetForeground(display, bar_gc, 0x000000);
159 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
164 localtime_r(&tmt, &tm);
165 strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
166 XSetForeground(display, bar_gc, 0xa0a0a0);
167 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
169 XSync(display, False);
172 quit(union arg *args)
174 DNPRINTF(SWM_D_MISC, "quit\n");
179 spawn(union arg *args)
181 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
183 * The double-fork construct avoids zombie processes and keeps the code
184 * clean from stupid signal handlers.
189 close(ConnectionNumber(display));
191 execvp(args->argv[0], args->argv);
192 fprintf(stderr, "execvp failed\n");
201 focus_win(struct ws_win *win)
203 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
204 XSetWindowBorder(display, win->id, col_focus);
205 XSetInputFocus(display, win->id, RevertToPointerRoot, CurrentTime);
206 ws[current_ws].focus = win;
210 unfocus_win(struct ws_win *win)
212 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
213 XSetWindowBorder(display, win->id, col_unfocus);
214 if (ws[current_ws].focus == win)
215 ws[current_ws].focus = NULL;
219 switchws(union arg *args)
224 DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
226 if (wsid == current_ws)
229 /* map new window first to prevent ugly blinking */
230 TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
231 XMapWindow(display, win->id);
232 ws[wsid].visible = 1;
234 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
235 XUnmapWindow(display, win->id);
236 ws[current_ws].visible = 0;
241 if (ws[wsid].restack) {
244 if (ws[wsid].focus != NULL)
245 focus_win(ws[wsid].focus);
246 XSync(display, False);
251 focus(union arg *args)
253 struct ws_win *winfocus, *winlostfocus;
255 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
256 if (ws[current_ws].focus == NULL || ws[current_ws].winno == 0)
259 winlostfocus = ws[current_ws].focus;
262 case SWM_ARG_ID_FOCUSPREV:
263 if (ws[current_ws].focus ==
264 TAILQ_FIRST(&ws[current_ws].winlist))
265 ws[current_ws].focus =
266 TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
268 ws[current_ws].focus =TAILQ_PREV(ws[current_ws].focus,
272 case SWM_ARG_ID_FOCUSNEXT:
273 if (ws[current_ws].focus == TAILQ_LAST(&ws[current_ws].winlist,
275 ws[current_ws].focus =
276 TAILQ_FIRST(&ws[current_ws].winlist);
278 ws[current_ws].focus =
279 TAILQ_NEXT(ws[current_ws].focus, entry);
282 case SWM_ARG_ID_FOCUSMAIN:
283 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);;
290 winfocus = ws[current_ws].focus;
291 unfocus_win(winlostfocus);
293 XSync(display, False);
296 /* I know this sucks but it works well enough */
301 struct ws_win wf, *win, *winfocus = &wf;
302 int i, h, w, x, y, hrh;
304 DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
308 if (ws[current_ws].winno == 0)
311 if (ws[current_ws].winno > 1)
316 if (ws[current_ws].winno > 2)
317 hrh = height / (ws[current_ws].winno - 1);
325 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
330 if (i != 0 && hrh != 0) {
331 /* correct the last window for lost pixels */
332 if (win == TAILQ_LAST(&ws[current_ws].winlist,
334 h = height - (i * hrh);
342 /* leave first right hand window at y = 0 */
348 bzero(&wc, sizeof wc);
351 win->width = wc.width = w;
352 win->height = wc.height = h;
354 XConfigureWindow(display, win->id, CWX | CWY | CWWidth |
355 CWHeight | CWBorderWidth, &wc);
356 if (win == ws[current_ws].focus)
360 XMapWindow(display, win->id);
364 focus_win(winfocus); /* this has to be done outside of the loop */
365 XSync(display, False);
369 swap_to_main(union arg *args)
371 struct ws_win *tmpwin = TAILQ_FIRST(&ws[current_ws].winlist);
373 DNPRINTF(SWM_D_MISC, "swap_to_main: win: %lu\n",
374 ws[current_ws].focus ? ws[current_ws].focus->id : 0);
376 if (ws[current_ws].focus == NULL || ws[current_ws].focus == tmpwin)
379 TAILQ_REMOVE(&ws[current_ws].winlist, tmpwin, entry);
380 TAILQ_INSERT_AFTER(&ws[current_ws].winlist, ws[current_ws].focus,
382 TAILQ_REMOVE(&ws[current_ws].winlist, ws[current_ws].focus, entry);
383 TAILQ_INSERT_HEAD(&ws[current_ws].winlist, ws[current_ws].focus, entry);
384 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
390 send_to_ws(union arg *args)
393 struct ws_win *win = ws[current_ws].focus;
395 DNPRINTF(SWM_D_MISC, "send_to_ws: win: %lu\n", win->id);
397 XUnmapWindow(display, win->id);
399 /* find a window to focus */
400 ws[current_ws].focus = TAILQ_PREV(win, ws_win_list,entry);
401 if (ws[current_ws].focus == NULL)
402 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
404 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
405 ws[current_ws].winno--;
407 TAILQ_INSERT_TAIL(&ws[wsid].winlist, win, entry);
408 if (ws[wsid].winno == 0)
409 ws[wsid].focus = win;
411 ws[wsid].restack = 1;
417 /* terminal + args */
418 char *term[] = { "xterm", NULL };
420 /* key definitions */
424 void (*func)(union arg *);
427 /* modifier key function argument */
428 { MODKEY, XK_Return, swap_to_main, {0} },
429 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = term } },
430 { MODKEY | ShiftMask, XK_q, quit, {0} },
431 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
432 { MODKEY, XK_1, switchws, {.id = 0} },
433 { MODKEY, XK_2, switchws, {.id = 1} },
434 { MODKEY, XK_3, switchws, {.id = 2} },
435 { MODKEY, XK_4, switchws, {.id = 3} },
436 { MODKEY, XK_5, switchws, {.id = 4} },
437 { MODKEY, XK_6, switchws, {.id = 5} },
438 { MODKEY, XK_7, switchws, {.id = 6} },
439 { MODKEY, XK_8, switchws, {.id = 7} },
440 { MODKEY, XK_9, switchws, {.id = 8} },
441 { MODKEY, XK_0, switchws, {.id = 9} },
442 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
443 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
444 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
445 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
446 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
447 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
448 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
449 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
450 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
451 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
452 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
453 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
454 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
458 updatenumlockmask(void)
461 XModifierKeymap *modmap;
463 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
465 modmap = XGetModifierMapping(display);
466 for (i = 0; i < 8; i++)
467 for (j = 0; j < modmap->max_keypermod; j++)
468 if (modmap->modifiermap[i * modmap->max_keypermod + j]
469 == XKeysymToKeycode(display, XK_Num_Lock))
470 numlockmask = (1 << i);
472 XFreeModifiermap(modmap);
480 unsigned int modifiers[] =
481 { 0, LockMask, numlockmask, numlockmask | LockMask };
483 DNPRINTF(SWM_D_MISC, "grabkeys\n");
486 XUngrabKey(display, AnyKey, AnyModifier, root);
487 for(i = 0; i < LENGTH(keys); i++) {
488 if((code = XKeysymToKeycode(display, keys[i].keysym)))
489 for(j = 0; j < LENGTH(modifiers); j++)
490 XGrabKey(display, code,
491 keys[i].mod | modifiers[j], root,
492 True, GrabModeAsync, GrabModeAsync);
498 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
506 XKeyEvent *ev = &e->xkey;
508 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
510 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
511 for(i = 0; i < LENGTH(keys); i++)
512 if(keysym == keys[i].keysym
513 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
515 keys[i].func(&(keys[i].args));
519 buttonpress(XEvent *e)
521 XButtonPressedEvent *ev = &e->xbutton;
522 #ifdef SWM_CLICKTOFOCUS
527 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
529 if (ev->window == root)
531 if (ev->window == ws[current_ws].focus->id)
533 #ifdef SWM_CLICKTOFOCUS
534 TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
535 if (win->id == ev->window) {
536 /* focus in the clicked window */
537 XSetWindowBorder(display, ev->window, 0xff0000);
538 XSetWindowBorder(display,
539 ws[current_ws].focus->id, 0x888888);
540 XSetInputFocus(display, ev->window, RevertToPointerRoot,
542 ws[current_ws].focus = win;
543 XSync(display, False);
550 configurerequest(XEvent *e)
552 XConfigureRequestEvent *ev = &e->xconfigurerequest;
555 DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window);
557 XSelectInput(display, ev->window, ButtonPressMask | EnterWindowMask |
560 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
561 errx(1, "calloc: failed to allocate memory for new window");
563 win->id = ev->window;
564 TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
565 ws[current_ws].focus = win; /* make new win focused */
566 ws[current_ws].winno++;
571 configurenotify(XEvent *e)
573 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
574 e->xconfigure.window);
578 destroynotify(XEvent *e)
581 XDestroyWindowEvent *ev = &e->xdestroywindow;
583 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
585 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
586 if (ev->window == win->id) {
587 /* find a window to focus */
588 ws[current_ws].focus =
589 TAILQ_PREV(win, ws_win_list,entry);
590 if (ws[current_ws].focus == NULL)
591 ws[current_ws].focus =
592 TAILQ_FIRST(&ws[current_ws].winlist);
594 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
596 ws[current_ws].winno--;
605 enternotify(XEvent *e)
607 XCrossingEvent *ev = &e->xcrossing;
610 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
612 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
616 /* eat event(s) to prevent autofocus */
620 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
621 if (win->id == ev->window)
631 XFocusChangeEvent *ev = &e->xfocus;
633 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window);
635 if (ev->window == root)
639 * kill grab for now so that we can cut and paste , this screws up
643 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
644 XGrabButton(display, Button1, AnyModifier, ev->window, False,
645 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
650 mappingnotify(XEvent *e)
652 XMappingEvent *ev = &e->xmapping;
654 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
656 XRefreshKeyboardMapping(ev);
657 if(ev->request == MappingKeyboard)
662 maprequest(XEvent *e)
664 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
665 e->xmaprequest.window);
669 propertynotify(XEvent *e)
671 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
672 e->xproperty.window);
676 unmapnotify(XEvent *e)
678 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
681 void (*handler[LASTEvent])(XEvent *) = {
683 [KeyPress] = keypress,
684 [ButtonPress] = buttonpress,
685 [ConfigureRequest] = configurerequest,
686 [ConfigureNotify] = configurenotify,
687 [DestroyNotify] = destroynotify,
688 [EnterNotify] = enternotify,
690 [MappingNotify] = mappingnotify,
691 [MapRequest] = maprequest,
692 [PropertyNotify] = propertynotify,
693 [UnmapNotify] = unmapnotify,
697 xerror_start(Display *d, XErrorEvent *ee)
704 xerror(Display *d, XErrorEvent *ee)
706 fprintf(stderr, "error: %p %p\n", display, ee);
715 xerrorxlib = XSetErrorHandler(xerror_start);
717 /* this causes an error if some other window manager is running */
718 XSelectInput(display, DefaultRootWindow(display),
719 SubstructureRedirectMask);
720 XSync(display, False);
724 XSetErrorHandler(xerror);
725 XSync(display, False);
730 main(int argc, char *argv[])
735 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
736 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
737 warnx("no locale support");
739 if(!(display = XOpenDisplay(0)))
740 errx(1, "can not open display");
743 errx(1, "other wm running");
745 screen = DefaultScreen(display);
746 root = RootWindow(display, screen);
747 width = DisplayWidth(display, screen) - 2;
748 height = DisplayHeight(display, screen) - 2;
750 /* make work space 1 active */
755 TAILQ_INIT(&ws[0].winlist);
756 for (i = 1; i < SWM_WS_MAX; i++) {
761 TAILQ_INIT(&ws[i].winlist);
764 /* setup status bar */
765 bar_fs = XLoadQueryFont(display,
766 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*");
767 if (bar_fs == NULL) {
768 /* load a font that is default */
769 bar_fs = XLoadQueryFont(display,
770 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*");
772 errx(1, "couldn't load font");
774 bar_height = bar_fs->ascent + bar_fs->descent + 3;
776 XSelectInput(display, root, SubstructureRedirectMask |
777 SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
778 EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
779 FocusChangeMask | PropertyChangeMask);
783 bar_window = XCreateSimpleWindow(display, root, 0, 0, width,
784 bar_height - 2, 1, 0x008080, 0x000000);
785 bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv);
786 XSetFont(display, bar_gc, bar_fs->fid);
788 height -= bar_height; /* correct screen height */
789 XMapWindow(display, bar_window);
794 XNextEvent(display, &e);
799 XCloseDisplay(display);