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 switchws(union arg *args)
161 DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
163 if (wsid == current_ws)
166 /* map new window first to prevent ugly blinking */
167 TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
168 XMapWindow(display, win->id);
169 ws[wsid].visible = 1;
171 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
172 XUnmapWindow(display, win->id);
173 ws[current_ws].visible = 0;
178 if (ws[wsid].focus != NULL) {
179 DNPRINTF(SWM_D_WS, "switchws: focus %d\n", ws[wsid].focus);
180 XSetInputFocus(display, ws[wsid].focus->id, RevertToPointerRoot,
183 XSync(display, False);
187 focus(union arg *args)
189 Window winfocus, winlostfocus;
191 if (ws[current_ws].focus == NULL || ws[current_ws].winno == 0)
194 winlostfocus = ws[current_ws].focus->id;
197 case SWM_ARG_ID_FOCUSPREV:
198 if (ws[current_ws].focus ==
199 TAILQ_FIRST(&ws[current_ws].winlist))
200 ws[current_ws].focus =
201 TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
203 ws[current_ws].focus =TAILQ_PREV(ws[current_ws].focus,
207 case SWM_ARG_ID_FOCUSNEXT:
208 if (ws[current_ws].focus == TAILQ_LAST(&ws[current_ws].winlist,
210 ws[current_ws].focus =
211 TAILQ_FIRST(&ws[current_ws].winlist);
213 ws[current_ws].focus =
214 TAILQ_NEXT(ws[current_ws].focus, entry);
217 case SWM_ARG_ID_FOCUSMAIN:
218 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);;
225 winfocus = ws[current_ws].focus->id;
226 XSetWindowBorder(display, winlostfocus, 0x888888);
227 XSetWindowBorder(display, winfocus, 0xff0000);
228 XSetInputFocus(display, winfocus, RevertToPointerRoot, CurrentTime);
229 XSync(display, False);
232 /* I know this sucks but it works well enough */
238 Window winfocus = root;
239 int i, h, w, x, y, winno, hrh;
241 DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
243 if (ws[current_ws].winno == 0)
246 if (ws[current_ws].winno > 1)
251 if (ws[current_ws].winno > 2)
252 hrh = height / (ws[current_ws].winno - 1);
260 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
265 if (i != 0 && hrh != 0) {
266 /* correct the last window for lost pixels */
267 if (win == TAILQ_LAST(&ws[current_ws].winlist,
269 h = height - (i * hrh);
277 /* leave first right hand window at y = 0 */
283 bzero(&wc, sizeof wc);
289 XConfigureWindow(display, win->id, CWX | CWY | CWWidth |
290 CWHeight | CWBorderWidth, &wc);
291 if (win == ws[current_ws].focus) {
293 XSetWindowBorder(display, win->id, 0xff0000);
295 XSetWindowBorder(display, win->id, 0x888888);
296 XMapWindow(display, win->id);
300 XSetInputFocus(display, winfocus, RevertToPointerRoot, CurrentTime);
301 XSync(display, False);
305 swap_to_main(union arg *args)
307 struct ws_win *tmpwin = TAILQ_FIRST(&ws[current_ws].winlist);
309 TAILQ_REMOVE(&ws[current_ws].winlist, tmpwin, entry);
310 TAILQ_INSERT_AFTER(&ws[current_ws].winlist, ws[current_ws].focus,
312 TAILQ_REMOVE(&ws[current_ws].winlist, ws[current_ws].focus, entry);
313 TAILQ_INSERT_HEAD(&ws[current_ws].winlist, ws[current_ws].focus, entry);
314 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
319 /* key definitions */
323 void (*func)(union arg *);
326 /* modifier key function argument */
327 { MODKEY, XK_Return, swap_to_main, {0} },
328 { MODKEY | ShiftMask, XK_Return, spawn, {0} },
329 { MODKEY | ShiftMask, XK_q, quit, {0} },
330 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
331 { MODKEY, XK_1, switchws, {.id = 0} },
332 { MODKEY, XK_2, switchws, {.id = 1} },
333 { MODKEY, XK_3, switchws, {.id = 2} },
334 { MODKEY, XK_4, switchws, {.id = 3} },
335 { MODKEY, XK_5, switchws, {.id = 4} },
336 { MODKEY, XK_6, switchws, {.id = 5} },
337 { MODKEY, XK_7, switchws, {.id = 6} },
338 { MODKEY, XK_8, switchws, {.id = 7} },
339 { MODKEY, XK_9, switchws, {.id = 8} },
340 { MODKEY, XK_0, switchws, {.id = 9} },
341 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
342 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
346 updatenumlockmask(void)
349 XModifierKeymap *modmap;
352 modmap = XGetModifierMapping(display);
353 for (i = 0; i < 8; i++)
354 for (j = 0; j < modmap->max_keypermod; j++)
355 if (modmap->modifiermap[i * modmap->max_keypermod + j]
356 == XKeysymToKeycode(display, XK_Num_Lock))
357 numlockmask = (1 << i);
359 XFreeModifiermap(modmap);
367 unsigned int modifiers[] =
368 { 0, LockMask, numlockmask, numlockmask | LockMask };
372 XUngrabKey(display, AnyKey, AnyModifier, root);
373 for(i = 0; i < LENGTH(keys); i++) {
374 if((code = XKeysymToKeycode(display, keys[i].keysym)))
375 for(j = 0; j < LENGTH(modifiers); j++)
376 XGrabKey(display, code,
377 keys[i].mod | modifiers[j], root,
378 True, GrabModeAsync, GrabModeAsync);
384 XExposeEvent *ev = &e->xexpose;
386 DNPRINTF(SWM_D_EVENT, "expose: window: %d\n", ev->window);
394 XKeyEvent *ev = &e->xkey;
396 DNPRINTF(SWM_D_EVENT, "keypress: window: %d\n", ev->window);
398 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
399 for(i = 0; i < LENGTH(keys); i++)
400 if(keysym == keys[i].keysym
401 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
403 keys[i].func(&(keys[i].args));
407 buttonpress(XEvent *e)
409 XButtonPressedEvent *ev = &e->xbutton;
412 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %d\n", ev->window);
414 if (ev->window == root)
416 if (ev->window == ws[current_ws].focus->id)
419 TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
420 if (win->id == ev->window) {
421 /* focus in the clicked window */
422 XSetWindowBorder(display, ev->window, 0xff0000);
423 XSetWindowBorder(display,
424 ws[current_ws].focus->id, 0x888888);
425 XSetInputFocus(display, ev->window, RevertToPointerRoot,
427 ws[current_ws].focus = win;
428 XSync(display, False);
434 configurerequest(XEvent *e)
436 XConfigureRequestEvent *ev = &e->xconfigurerequest;
439 DNPRINTF(SWM_D_EVENT, "configurerequest: window: %d\n", ev->window);
441 XSelectInput(display, ev->window, ButtonPressMask | EnterWindowMask |
444 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
445 errx(1, "calloc: failed to allocate memory for new window");
447 win->id = ev->window;
448 TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
449 ws[current_ws].focus = win; /* make new win focused */
450 ws[current_ws].winno++;
455 configurenotify(XEvent *e)
457 XConfigureEvent *ev = &e->xconfigure;
459 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %d\n", ev->window);
463 destroynotify(XEvent *e)
467 XDestroyWindowEvent *ev = &e->xdestroywindow;
469 DNPRINTF(SWM_D_EVENT, "destroynotify: window %d\n", ev->window);
471 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
472 DNPRINTF(SWM_D_EVENT, "trying: %x\n", win->id);
473 if (ev->window == win->id) {
474 /* find a window to focus */
475 ws[current_ws].focus =
476 TAILQ_PREV(win, ws_win_list,entry);
477 if (ws[current_ws].focus == NULL)
478 ws[current_ws].focus =
479 TAILQ_FIRST(&ws[current_ws].winlist);
481 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
483 ws[current_ws].winno--;
492 enternotify(XEvent *e)
494 XCrossingEvent *ev = &e->xcrossing;
497 DNPRINTF(SWM_D_EVENT, "enternotify: window: %d\n", ev->window);
499 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
503 /* eat event(s) to prevent autofocus */
507 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
508 if (win->id == ev->window) {
509 XSetInputFocus(display, ev->window, RevertToPointerRoot,
511 XSetWindowBorder(display, ev->window, 0xff0000);
512 ws[current_ws].focus = win;
514 XSetWindowBorder(display, win->id, 0x888888);
521 XFocusChangeEvent *ev = &e->xfocus;
523 DNPRINTF(SWM_D_EVENT, "focusin: window: %d\n", ev->window);
525 if (ev->window == root)
529 * kill grab for now so that we can cut and paste , this screws up
533 DNPRINTF(SWM_D_EVENT, "focusin: window: %d grabbing\n", ev->window);
534 XGrabButton(display, Button1, AnyModifier, ev->window, False,
535 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
540 mappingnotify(XEvent *e)
542 XMappingEvent *ev = &e->xmapping;
544 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %d\n", ev->window);
546 XRefreshKeyboardMapping(ev);
547 if(ev->request == MappingKeyboard)
552 maprequest(XEvent *e)
554 XMapRequestEvent *ev = &e->xmaprequest;
556 DNPRINTF(SWM_D_EVENT, "maprequest: window: %d\n", ev->window);
560 propertynotify(XEvent *e)
562 XPropertyEvent *ev = &e->xproperty;
564 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %d\n", ev->window);
568 unmapnotify(XEvent *e)
570 XUnmapEvent *ev = &e->xunmap;
572 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %d\n", ev->window);
575 void (*handler[LASTEvent])(XEvent *) = {
577 [KeyPress] = keypress,
578 [ButtonPress] = buttonpress,
579 [ConfigureRequest] = configurerequest,
580 [ConfigureNotify] = configurenotify,
581 [DestroyNotify] = destroynotify,
582 [EnterNotify] = enternotify,
584 [MappingNotify] = mappingnotify,
585 [MapRequest] = maprequest,
586 [PropertyNotify] = propertynotify,
587 [UnmapNotify] = unmapnotify,
591 xerror_start(Display *d, XErrorEvent *ee)
598 xerror(Display *d, XErrorEvent *ee)
600 fprintf(stderr, "error: %p %p\n", display, ee);
609 xerrorxlib = XSetErrorHandler(xerror_start);
611 /* this causes an error if some other window manager is running */
612 XSelectInput(display, DefaultRootWindow(display),
613 SubstructureRedirectMask);
614 XSync(display, False);
618 XSetErrorHandler(xerror);
619 XSync(display, False);
624 main(int argc, char *argv[])
629 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
630 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
631 warnx("no locale support");
633 if(!(display = XOpenDisplay(0)))
634 errx(1, "can not open display");
637 errx(1, "other wm running");
639 screen = DefaultScreen(display);
640 root = RootWindow(display, screen);
641 width = DisplayWidth(display, screen) - 2;
642 height = DisplayHeight(display, screen) - 2;
644 /* make work space 1 active */
648 TAILQ_INIT(&ws[0].winlist);
649 for (i = 1; i < SWM_WS_MAX; i++) {
653 TAILQ_INIT(&ws[i].winlist);
656 XSelectInput(display, root, SubstructureRedirectMask |
657 SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
658 EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
659 FocusChangeMask | PropertyChangeMask);
664 XNextEvent(display, &e);
669 XCloseDisplay(display);