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"
59 #include <sys/types.h>
61 #include <sys/queue.h>
63 #include <X11/cursorfont.h>
64 #include <X11/keysym.h>
65 #include <X11/Xatom.h>
67 #include <X11/Xproto.h>
68 #include <X11/Xutil.h>
70 /* #define SWM_DEBUG */
72 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
73 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
74 #define SWM_D_EVENT 0x0001
75 #define SWM_D_WS 0x0002
77 uint32_t swm_debug = 0
83 #define DNPRINTF(n,x...)
86 #define LENGTH(x) (sizeof x / sizeof x[0])
87 #define MODKEY Mod1Mask
88 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
90 int (*xerrorxlib)(Display *, XErrorEvent *);
96 unsigned int numlockmask = 0;
101 TAILQ_ENTRY(ws_win) entry;
105 TAILQ_HEAD(ws_win_list, ws_win);
107 /* define work spaces */
108 #define SWM_WS_MAX (10)
110 int visible; /* workspace visible */
111 struct ws_win *focus; /* which win has focus */
112 int winno; /* total nr of windows */
113 struct ws_win_list winlist;
117 /* args to functions */
120 #define SWM_ARG_ID_FOCUSNEXT (0)
121 #define SWM_ARG_ID_FOCUSPREV (1)
122 #define SWM_ARG_ID_FOCUSMAIN (2)
126 quit(union arg *args)
132 spawn(union arg *args)
134 char *argv[] = { "xterm", NULL }; /* XXX make this in args */
137 * The double-fork construct avoids zombie processes and keeps the code
138 * clean from stupid signal handlers.
143 close(ConnectionNumber(display));
145 execvp(argv[0], argv);
146 fprintf(stderr, "execvp failed\n");
155 focus_win(struct ws_win *win)
157 XSetWindowBorder(display, win->id, 0xff0000);
158 XSetInputFocus(display, win->id, RevertToPointerRoot, CurrentTime);
159 ws[current_ws].focus = win;
163 unfocus_win(struct ws_win *win)
165 XSetWindowBorder(display, win->id, 0x888888);
166 if (ws[current_ws].focus == win)
167 ws[current_ws].focus = NULL;
171 switchws(union arg *args)
177 DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
179 if (wsid == current_ws)
182 /* map new window first to prevent ugly blinking */
183 TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
184 XMapWindow(display, win->id);
185 ws[wsid].visible = 1;
187 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
188 XUnmapWindow(display, win->id);
189 ws[current_ws].visible = 0;
194 if (ws[wsid].focus != NULL)
195 focus_win(ws[wsid].focus);
196 XSync(display, False);
200 focus(union arg *args)
202 struct ws_win *winfocus, *winlostfocus;
204 if (ws[current_ws].focus == NULL || ws[current_ws].winno == 0)
207 winlostfocus = ws[current_ws].focus;
210 case SWM_ARG_ID_FOCUSPREV:
211 if (ws[current_ws].focus ==
212 TAILQ_FIRST(&ws[current_ws].winlist))
213 ws[current_ws].focus =
214 TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
216 ws[current_ws].focus =TAILQ_PREV(ws[current_ws].focus,
220 case SWM_ARG_ID_FOCUSNEXT:
221 if (ws[current_ws].focus == TAILQ_LAST(&ws[current_ws].winlist,
223 ws[current_ws].focus =
224 TAILQ_FIRST(&ws[current_ws].winlist);
226 ws[current_ws].focus =
227 TAILQ_NEXT(ws[current_ws].focus, entry);
230 case SWM_ARG_ID_FOCUSMAIN:
231 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);;
238 winfocus = ws[current_ws].focus;
239 unfocus_win(winlostfocus);
241 XSync(display, False);
244 /* I know this sucks but it works well enough */
249 struct ws_win wf, *win, *winfocus = &wf;
250 int i, h, w, x, y, winno, hrh;
252 DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
256 if (ws[current_ws].winno == 0)
259 if (ws[current_ws].winno > 1)
264 if (ws[current_ws].winno > 2)
265 hrh = height / (ws[current_ws].winno - 1);
273 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
278 if (i != 0 && hrh != 0) {
279 /* correct the last window for lost pixels */
280 if (win == TAILQ_LAST(&ws[current_ws].winlist,
282 h = height - (i * hrh);
290 /* leave first right hand window at y = 0 */
296 bzero(&wc, sizeof wc);
302 XConfigureWindow(display, win->id, CWX | CWY | CWWidth |
303 CWHeight | CWBorderWidth, &wc);
304 if (win == ws[current_ws].focus)
308 XMapWindow(display, win->id);
313 XSync(display, False);
317 swap_to_main(union arg *args)
319 struct ws_win *tmpwin = TAILQ_FIRST(&ws[current_ws].winlist);
321 TAILQ_REMOVE(&ws[current_ws].winlist, tmpwin, entry);
322 TAILQ_INSERT_AFTER(&ws[current_ws].winlist, ws[current_ws].focus,
324 TAILQ_REMOVE(&ws[current_ws].winlist, ws[current_ws].focus, entry);
325 TAILQ_INSERT_HEAD(&ws[current_ws].winlist, ws[current_ws].focus, entry);
326 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
331 /* key definitions */
335 void (*func)(union arg *);
338 /* modifier key function argument */
339 { MODKEY, XK_Return, swap_to_main, {0} },
340 { MODKEY | ShiftMask, XK_Return, spawn, {0} },
341 { MODKEY | ShiftMask, XK_q, quit, {0} },
342 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
343 { MODKEY, XK_1, switchws, {.id = 0} },
344 { MODKEY, XK_2, switchws, {.id = 1} },
345 { MODKEY, XK_3, switchws, {.id = 2} },
346 { MODKEY, XK_4, switchws, {.id = 3} },
347 { MODKEY, XK_5, switchws, {.id = 4} },
348 { MODKEY, XK_6, switchws, {.id = 5} },
349 { MODKEY, XK_7, switchws, {.id = 6} },
350 { MODKEY, XK_8, switchws, {.id = 7} },
351 { MODKEY, XK_9, switchws, {.id = 8} },
352 { MODKEY, XK_0, switchws, {.id = 9} },
353 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
354 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
358 updatenumlockmask(void)
361 XModifierKeymap *modmap;
364 modmap = XGetModifierMapping(display);
365 for (i = 0; i < 8; i++)
366 for (j = 0; j < modmap->max_keypermod; j++)
367 if (modmap->modifiermap[i * modmap->max_keypermod + j]
368 == XKeysymToKeycode(display, XK_Num_Lock))
369 numlockmask = (1 << i);
371 XFreeModifiermap(modmap);
379 unsigned int modifiers[] =
380 { 0, LockMask, numlockmask, numlockmask | LockMask };
384 XUngrabKey(display, AnyKey, AnyModifier, root);
385 for(i = 0; i < LENGTH(keys); i++) {
386 if((code = XKeysymToKeycode(display, keys[i].keysym)))
387 for(j = 0; j < LENGTH(modifiers); j++)
388 XGrabKey(display, code,
389 keys[i].mod | modifiers[j], root,
390 True, GrabModeAsync, GrabModeAsync);
396 XExposeEvent *ev = &e->xexpose;
398 DNPRINTF(SWM_D_EVENT, "expose: window: %d\n", ev->window);
406 XKeyEvent *ev = &e->xkey;
408 DNPRINTF(SWM_D_EVENT, "keypress: window: %d\n", ev->window);
410 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
411 for(i = 0; i < LENGTH(keys); i++)
412 if(keysym == keys[i].keysym
413 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
415 keys[i].func(&(keys[i].args));
419 buttonpress(XEvent *e)
421 XButtonPressedEvent *ev = &e->xbutton;
424 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %d\n", ev->window);
426 if (ev->window == root)
428 if (ev->window == ws[current_ws].focus->id)
431 TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
432 if (win->id == ev->window) {
433 /* focus in the clicked window */
434 XSetWindowBorder(display, ev->window, 0xff0000);
435 XSetWindowBorder(display,
436 ws[current_ws].focus->id, 0x888888);
437 XSetInputFocus(display, ev->window, RevertToPointerRoot,
439 ws[current_ws].focus = win;
440 XSync(display, False);
447 configurerequest(XEvent *e)
449 XConfigureRequestEvent *ev = &e->xconfigurerequest;
452 DNPRINTF(SWM_D_EVENT, "configurerequest: window: %d\n", ev->window);
454 XSelectInput(display, ev->window, ButtonPressMask | EnterWindowMask |
457 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
458 errx(1, "calloc: failed to allocate memory for new window");
460 win->id = ev->window;
461 TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
462 ws[current_ws].focus = win; /* make new win focused */
463 ws[current_ws].winno++;
468 configurenotify(XEvent *e)
470 XConfigureEvent *ev = &e->xconfigure;
472 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %d\n", ev->window);
476 destroynotify(XEvent *e)
480 XDestroyWindowEvent *ev = &e->xdestroywindow;
482 DNPRINTF(SWM_D_EVENT, "destroynotify: window %d\n", ev->window);
484 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
485 DNPRINTF(SWM_D_EVENT, "trying: %x\n", win->id);
486 if (ev->window == win->id) {
487 /* find a window to focus */
488 ws[current_ws].focus =
489 TAILQ_PREV(win, ws_win_list,entry);
490 if (ws[current_ws].focus == NULL)
491 ws[current_ws].focus =
492 TAILQ_FIRST(&ws[current_ws].winlist);
494 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
496 ws[current_ws].winno--;
505 enternotify(XEvent *e)
507 XCrossingEvent *ev = &e->xcrossing;
510 DNPRINTF(SWM_D_EVENT, "enternotify: window: %d\n", ev->window);
512 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
516 /* eat event(s) to prevent autofocus */
520 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
521 if (win->id == ev->window)
531 XFocusChangeEvent *ev = &e->xfocus;
533 DNPRINTF(SWM_D_EVENT, "focusin: window: %d\n", ev->window);
535 if (ev->window == root)
539 * kill grab for now so that we can cut and paste , this screws up
543 DNPRINTF(SWM_D_EVENT, "focusin: window: %d grabbing\n", ev->window);
544 XGrabButton(display, Button1, AnyModifier, ev->window, False,
545 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
550 mappingnotify(XEvent *e)
552 XMappingEvent *ev = &e->xmapping;
554 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %d\n", ev->window);
556 XRefreshKeyboardMapping(ev);
557 if(ev->request == MappingKeyboard)
562 maprequest(XEvent *e)
564 XMapRequestEvent *ev = &e->xmaprequest;
566 DNPRINTF(SWM_D_EVENT, "maprequest: window: %d\n", ev->window);
570 propertynotify(XEvent *e)
572 XPropertyEvent *ev = &e->xproperty;
574 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %d\n", ev->window);
578 unmapnotify(XEvent *e)
580 XUnmapEvent *ev = &e->xunmap;
582 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %d\n", ev->window);
585 void (*handler[LASTEvent])(XEvent *) = {
587 [KeyPress] = keypress,
588 [ButtonPress] = buttonpress,
589 [ConfigureRequest] = configurerequest,
590 [ConfigureNotify] = configurenotify,
591 [DestroyNotify] = destroynotify,
592 [EnterNotify] = enternotify,
594 [MappingNotify] = mappingnotify,
595 [MapRequest] = maprequest,
596 [PropertyNotify] = propertynotify,
597 [UnmapNotify] = unmapnotify,
601 xerror_start(Display *d, XErrorEvent *ee)
608 xerror(Display *d, XErrorEvent *ee)
610 fprintf(stderr, "error: %p %p\n", display, ee);
619 xerrorxlib = XSetErrorHandler(xerror_start);
621 /* this causes an error if some other window manager is running */
622 XSelectInput(display, DefaultRootWindow(display),
623 SubstructureRedirectMask);
624 XSync(display, False);
628 XSetErrorHandler(xerror);
629 XSync(display, False);
634 main(int argc, char *argv[])
639 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
640 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
641 warnx("no locale support");
643 if(!(display = XOpenDisplay(0)))
644 errx(1, "can not open display");
647 errx(1, "other wm running");
649 screen = DefaultScreen(display);
650 root = RootWindow(display, screen);
651 width = DisplayWidth(display, screen) - 2;
652 height = DisplayHeight(display, screen) - 2;
654 /* make work space 1 active */
658 TAILQ_INIT(&ws[0].winlist);
659 for (i = 1; i < SWM_WS_MAX; i++) {
663 TAILQ_INIT(&ws[i].winlist);
666 XSelectInput(display, root, SubstructureRedirectMask |
667 SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
668 EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
669 FocusChangeMask | PropertyChangeMask);
674 XNextEvent(display, &e);
679 XCloseDisplay(display);