4 * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
5 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * Much code and ideas taken from dwm under the following license:
21 * MIT/X Consortium License
23 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
24 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
25 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
26 * 2007 Premysl Hruby <dfenze at gmail dot com>
27 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
28 * 2007 Christof Musik <christof at sendfax dot de>
29 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
30 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
31 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
33 * Permission is hereby granted, free of charge, to any person obtaining a
34 * copy of this software and associated documentation files (the "Software"),
35 * to deal in the Software without restriction, including without limitation
36 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
37 * and/or sell copies of the Software, and to permit persons to whom the
38 * Software is furnished to do so, subject to the following conditions:
40 * The above copyright notice and this permission notice shall be included in
41 * all copies or substantial portions of the Software.
43 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
44 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
45 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
46 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
47 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
48 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
49 * DEALINGS IN THE SOFTWARE.
52 #define SWM_VERSION "0.5"
60 #include <sys/types.h>
62 #include <sys/queue.h>
64 #include <X11/cursorfont.h>
65 #include <X11/keysym.h>
66 #include <X11/Xatom.h>
68 #include <X11/Xproto.h>
69 #include <X11/Xutil.h>
71 /* #define SWM_DEBUG */
73 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
74 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
75 #define SWM_D_EVENT 0x0001
76 #define SWM_D_WS 0x0002
78 uint32_t swm_debug = 0
84 #define DNPRINTF(n,x...)
87 #define LENGTH(x) (sizeof x / sizeof x[0])
88 #define MODKEY Mod1Mask
89 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
91 int (*xerrorxlib)(Display *, XErrorEvent *);
97 unsigned int numlockmask = 0;
102 TAILQ_ENTRY(ws_win) entry;
106 TAILQ_HEAD(ws_win_list, ws_win);
108 /* define work spaces */
109 #define SWM_WS_MAX (10)
111 int visible; /* workspace visible */
112 struct ws_win *focus; /* which win has focus */
113 int winno; /* total nr of windows */
114 struct ws_win_list winlist;
118 /* args to functions */
121 #define SWM_ARG_ID_FOCUSNEXT (0)
122 #define SWM_ARG_ID_FOCUSPREV (1)
123 #define SWM_ARG_ID_FOCUSMAIN (2)
127 quit(union arg *args)
133 spawn(union arg *args)
135 char *argv[] = { "xterm", NULL }; /* XXX make this in args */
138 * The double-fork construct avoids zombie processes and keeps the code
139 * clean from stupid signal handlers.
144 close(ConnectionNumber(display));
146 execvp(argv[0], argv);
147 fprintf(stderr, "execvp failed\n");
156 switchws(union arg *args)
162 DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
164 if (wsid == current_ws)
167 /* map new window first to prevent ugly blinking */
168 TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
169 XMapWindow(display, win->id);
170 ws[wsid].visible = 1;
172 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
173 XUnmapWindow(display, win->id);
174 ws[current_ws].visible = 0;
179 if (ws[wsid].focus != NULL) {
180 DNPRINTF(SWM_D_WS, "switchws: focus %d\n", ws[wsid].focus);
181 XSetInputFocus(display, ws[wsid].focus->id, RevertToPointerRoot,
184 XSync(display, False);
188 focus(union arg *args)
190 Window winfocus, winlostfocus;
192 if (ws[current_ws].focus == NULL || ws[current_ws].winno == 0)
195 winlostfocus = ws[current_ws].focus->id;
198 case SWM_ARG_ID_FOCUSPREV:
199 if (ws[current_ws].focus ==
200 TAILQ_FIRST(&ws[current_ws].winlist))
201 ws[current_ws].focus =
202 TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
204 ws[current_ws].focus =TAILQ_PREV(ws[current_ws].focus,
208 case SWM_ARG_ID_FOCUSNEXT:
209 if (ws[current_ws].focus == TAILQ_LAST(&ws[current_ws].winlist,
211 ws[current_ws].focus =
212 TAILQ_FIRST(&ws[current_ws].winlist);
214 ws[current_ws].focus =
215 TAILQ_NEXT(ws[current_ws].focus, entry);
218 case SWM_ARG_ID_FOCUSMAIN:
219 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);;
226 winfocus = ws[current_ws].focus->id;
227 XSetWindowBorder(display, winlostfocus, 0x888888);
228 XSetWindowBorder(display, winfocus, 0xff0000);
229 XSetInputFocus(display, winfocus, RevertToPointerRoot, CurrentTime);
230 XSync(display, False);
233 /* I know this sucks but it works well enough */
239 Window winfocus = root;
240 int i, h, w, x, y, winno, hrh;
242 DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
244 if (ws[current_ws].winno == 0)
247 if (ws[current_ws].winno > 1)
252 if (ws[current_ws].winno > 2)
253 hrh = height / (ws[current_ws].winno - 1);
261 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
266 if (i != 0 && hrh != 0) {
267 /* correct the last window for lost pixels */
268 if (win == TAILQ_LAST(&ws[current_ws].winlist,
270 h = height - (i * hrh);
278 /* leave first right hand window at y = 0 */
284 bzero(&wc, sizeof wc);
290 XConfigureWindow(display, win->id, CWX | CWY | CWWidth |
291 CWHeight | CWBorderWidth, &wc);
292 if (win == ws[current_ws].focus) {
294 XSetWindowBorder(display, win->id, 0xff0000);
296 XSetWindowBorder(display, win->id, 0x888888);
297 XMapWindow(display, win->id);
301 XSetInputFocus(display, winfocus, RevertToPointerRoot, CurrentTime);
302 XSync(display, False);
306 swap_to_main(union arg *args)
308 struct ws_win *tmpwin = TAILQ_FIRST(&ws[current_ws].winlist);
310 TAILQ_REMOVE(&ws[current_ws].winlist, tmpwin, entry);
311 TAILQ_INSERT_AFTER(&ws[current_ws].winlist, ws[current_ws].focus,
313 TAILQ_REMOVE(&ws[current_ws].winlist, ws[current_ws].focus, entry);
314 TAILQ_INSERT_HEAD(&ws[current_ws].winlist, ws[current_ws].focus, entry);
315 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
320 /* key definitions */
324 void (*func)(union arg *);
327 /* modifier key function argument */
328 { MODKEY, XK_Return, swap_to_main, {0} },
329 { MODKEY | ShiftMask, XK_Return, spawn, {0} },
330 { MODKEY | ShiftMask, XK_q, quit, {0} },
331 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
332 { MODKEY, XK_1, switchws, {.id = 0} },
333 { MODKEY, XK_2, switchws, {.id = 1} },
334 { MODKEY, XK_3, switchws, {.id = 2} },
335 { MODKEY, XK_4, switchws, {.id = 3} },
336 { MODKEY, XK_5, switchws, {.id = 4} },
337 { MODKEY, XK_6, switchws, {.id = 5} },
338 { MODKEY, XK_7, switchws, {.id = 6} },
339 { MODKEY, XK_8, switchws, {.id = 7} },
340 { MODKEY, XK_9, switchws, {.id = 8} },
341 { MODKEY, XK_0, switchws, {.id = 9} },
342 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
343 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
347 updatenumlockmask(void)
350 XModifierKeymap *modmap;
353 modmap = XGetModifierMapping(display);
354 for (i = 0; i < 8; i++)
355 for (j = 0; j < modmap->max_keypermod; j++)
356 if (modmap->modifiermap[i * modmap->max_keypermod + j]
357 == XKeysymToKeycode(display, XK_Num_Lock))
358 numlockmask = (1 << i);
360 XFreeModifiermap(modmap);
368 unsigned int modifiers[] =
369 { 0, LockMask, numlockmask, numlockmask | LockMask };
373 XUngrabKey(display, AnyKey, AnyModifier, root);
374 for(i = 0; i < LENGTH(keys); i++) {
375 if((code = XKeysymToKeycode(display, keys[i].keysym)))
376 for(j = 0; j < LENGTH(modifiers); j++)
377 XGrabKey(display, code,
378 keys[i].mod | modifiers[j], root,
379 True, GrabModeAsync, GrabModeAsync);
385 XExposeEvent *ev = &e->xexpose;
387 DNPRINTF(SWM_D_EVENT, "expose: window: %d\n", ev->window);
395 XKeyEvent *ev = &e->xkey;
397 DNPRINTF(SWM_D_EVENT, "keypress: window: %d\n", ev->window);
399 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
400 for(i = 0; i < LENGTH(keys); i++)
401 if(keysym == keys[i].keysym
402 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
404 keys[i].func(&(keys[i].args));
408 buttonpress(XEvent *e)
410 XButtonPressedEvent *ev = &e->xbutton;
413 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %d\n", ev->window);
415 if (ev->window == root)
417 if (ev->window == ws[current_ws].focus->id)
420 TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
421 if (win->id == ev->window) {
422 /* focus in the clicked window */
423 XSetWindowBorder(display, ev->window, 0xff0000);
424 XSetWindowBorder(display,
425 ws[current_ws].focus->id, 0x888888);
426 XSetInputFocus(display, ev->window, RevertToPointerRoot,
428 ws[current_ws].focus = win;
429 XSync(display, False);
435 configurerequest(XEvent *e)
437 XConfigureRequestEvent *ev = &e->xconfigurerequest;
440 DNPRINTF(SWM_D_EVENT, "configurerequest: window: %d\n", ev->window);
442 XSelectInput(display, ev->window, ButtonPressMask | EnterWindowMask |
445 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
446 errx(1, "calloc: failed to allocate memory for new window");
448 win->id = ev->window;
449 TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
450 ws[current_ws].focus = win; /* make new win focused */
451 ws[current_ws].winno++;
456 configurenotify(XEvent *e)
458 XConfigureEvent *ev = &e->xconfigure;
460 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %d\n", ev->window);
464 destroynotify(XEvent *e)
468 XDestroyWindowEvent *ev = &e->xdestroywindow;
470 DNPRINTF(SWM_D_EVENT, "destroynotify: window %d\n", ev->window);
472 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
473 DNPRINTF(SWM_D_EVENT, "trying: %x\n", win->id);
474 if (ev->window == win->id) {
475 /* find a window to focus */
476 ws[current_ws].focus =
477 TAILQ_PREV(win, ws_win_list,entry);
478 if (ws[current_ws].focus == NULL)
479 ws[current_ws].focus =
480 TAILQ_FIRST(&ws[current_ws].winlist);
482 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
484 ws[current_ws].winno--;
493 enternotify(XEvent *e)
495 XCrossingEvent *ev = &e->xcrossing;
498 DNPRINTF(SWM_D_EVENT, "enternotify: window: %d\n", ev->window);
500 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
504 /* eat event(s) to prevent autofocus */
508 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
509 if (win->id == ev->window) {
510 XSetInputFocus(display, ev->window, RevertToPointerRoot,
512 XSetWindowBorder(display, ev->window, 0xff0000);
513 ws[current_ws].focus = win;
515 XSetWindowBorder(display, win->id, 0x888888);
522 XFocusChangeEvent *ev = &e->xfocus;
524 DNPRINTF(SWM_D_EVENT, "focusin: window: %d\n", ev->window);
526 if (ev->window == root)
530 * kill grab for now so that we can cut and paste , this screws up
534 DNPRINTF(SWM_D_EVENT, "focusin: window: %d grabbing\n", ev->window);
535 XGrabButton(display, Button1, AnyModifier, ev->window, False,
536 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
541 mappingnotify(XEvent *e)
543 XMappingEvent *ev = &e->xmapping;
545 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %d\n", ev->window);
547 XRefreshKeyboardMapping(ev);
548 if(ev->request == MappingKeyboard)
553 maprequest(XEvent *e)
555 XMapRequestEvent *ev = &e->xmaprequest;
557 DNPRINTF(SWM_D_EVENT, "maprequest: window: %d\n", ev->window);
561 propertynotify(XEvent *e)
563 XPropertyEvent *ev = &e->xproperty;
565 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %d\n", ev->window);
569 unmapnotify(XEvent *e)
571 XUnmapEvent *ev = &e->xunmap;
573 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %d\n", ev->window);
576 void (*handler[LASTEvent])(XEvent *) = {
578 [KeyPress] = keypress,
579 [ButtonPress] = buttonpress,
580 [ConfigureRequest] = configurerequest,
581 [ConfigureNotify] = configurenotify,
582 [DestroyNotify] = destroynotify,
583 [EnterNotify] = enternotify,
585 [MappingNotify] = mappingnotify,
586 [MapRequest] = maprequest,
587 [PropertyNotify] = propertynotify,
588 [UnmapNotify] = unmapnotify,
592 xerror_start(Display *d, XErrorEvent *ee)
599 xerror(Display *d, XErrorEvent *ee)
601 fprintf(stderr, "error: %p %p\n", display, ee);
610 xerrorxlib = XSetErrorHandler(xerror_start);
612 /* this causes an error if some other window manager is running */
613 XSelectInput(display, DefaultRootWindow(display),
614 SubstructureRedirectMask);
615 XSync(display, False);
619 XSetErrorHandler(xerror);
620 XSync(display, False);
625 main(int argc, char *argv[])
630 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
631 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
632 warnx("no locale support");
634 if(!(display = XOpenDisplay(0)))
635 errx(1, "can not open display");
638 errx(1, "other wm running");
640 screen = DefaultScreen(display);
641 root = RootWindow(display, screen);
642 width = DisplayWidth(display, screen) - 2;
643 height = DisplayHeight(display, screen) - 2;
645 /* make work space 1 active */
649 TAILQ_INIT(&ws[0].winlist);
650 for (i = 1; i < SWM_WS_MAX; i++) {
654 TAILQ_INIT(&ws[i].winlist);
657 XSelectInput(display, root, SubstructureRedirectMask |
658 SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
659 EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
660 FocusChangeMask | PropertyChangeMask);
665 XNextEvent(display, &e);
670 XCloseDisplay(display);