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"
64 #include <sys/types.h>
67 #include <sys/queue.h>
68 #include <sys/param.h>
70 #include <X11/cursorfont.h>
71 #include <X11/keysym.h>
72 #include <X11/Xatom.h>
74 #include <X11/Xproto.h>
75 #include <X11/Xutil.h>
77 /* #define SWM_DEBUG */
79 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
80 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
81 #define SWM_D_EVENT 0x0001
82 #define SWM_D_WS 0x0002
83 #define SWM_D_FOCUS 0x0004
84 #define SWM_D_MISC 0x0008
86 u_int32_t swm_debug = 0
94 #define DNPRINTF(n,x...)
97 #define LENGTH(x) (sizeof x / sizeof x[0])
98 #define MODKEY Mod1Mask
99 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
101 int (*xerrorxlib)(Display *, XErrorEvent *);
106 int ignore_enter = 0;
107 unsigned int numlockmask = 0;
108 unsigned long color_focus = 0xff0000; /* XXX should this be per ws? */
109 unsigned long color_unfocus = 0x888888;
116 unsigned long bar_border = 0x008080;
117 unsigned long bar_color = 0x000000;
118 unsigned long bar_font_color = 0xa0a0a0;
124 char *bar_fonts[] = {
125 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
126 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
130 /* terminal + args */
131 char *spawn_term[] = { "xterm", NULL };
132 char *spawn_menu[] = { "dmenu_run", NULL };
133 char *spawn_scrotwm[] = { "scrotwm", NULL };
136 TAILQ_ENTRY(ws_win) entry;
144 XWindowAttributes wa;
147 TAILQ_HEAD(ws_win_list, ws_win);
149 /* define work spaces */
150 #define SWM_WS_MAX (10)
152 int visible; /* workspace visible */
153 int restack; /* restack on switch */
154 struct ws_win *focus; /* which win has focus */
155 struct ws_win_list winlist; /* list of windows in ws */
159 /* args to functions */
162 #define SWM_ARG_ID_FOCUSNEXT (0)
163 #define SWM_ARG_ID_FOCUSPREV (1)
164 #define SWM_ARG_ID_FOCUSMAIN (2)
171 #define SWM_CONF_WS "\n= \t"
172 #define SWM_CONF_FILE "scrotwm.conf"
174 conf_load(char *filename)
177 char *line, *cp, *var, *val;
178 size_t len, lineno = 0;
180 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
182 if (filename == NULL)
185 if ((config = fopen(filename, "r")) == NULL)
189 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
193 cp += (long)strspn(cp, SWM_CONF_WS);
199 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
201 cp += (long)strspn(cp, SWM_CONF_WS);
202 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
205 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
208 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
209 bar_enabled = atoi(val);
210 else if (!strncmp(var, "bar_border",
211 strlen("bar_border")))
212 bar_border = strtol(val, NULL, 16);
213 else if (!strncmp(var, "bar_color",
214 strlen("bar_color")))
215 bar_color = strtol(val, NULL, 16);
216 else if (!strncmp(var, "bar_font_color",
217 strlen("bar_font_color")))
218 bar_font_color = strtol(val, NULL, 16);
219 else if (!strncmp(var, "bar_font", strlen("bar_font")))
220 asprintf(&bar_fonts[0], "%s", val);
226 if (!strncmp(var, "color_focus", strlen("color_focus")))
227 color_focus = strtol(val, NULL, 16);
228 else if (!strncmp(var, "color_unfocus",
229 strlen("color_unfocus")))
230 color_unfocus = strtol(val, NULL, 16);
236 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
237 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
248 errx(1, "invalid conf file entry: %s=%s", var, val);
257 if (bar_enabled == 0)
261 XSetForeground(display, bar_gc, bar_color);
262 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
267 localtime_r(&tmt, &tm);
268 strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
269 XSetForeground(display, bar_gc, bar_font_color);
270 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
272 XSync(display, False);
280 /* XXX yeah yeah byte me */
285 bar_toggle(union arg *args)
289 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
293 height += bar_height; /* correct screen height */
294 XUnmapWindow(display, bar_window);
297 height -= bar_height; /* correct screen height */
298 XMapWindow(display, bar_window);
300 XSync(display, False);
301 for (i = 0; i < SWM_WS_MAX; i++)
305 bar_print(); /* must be after stack */
313 for (i = 0; bar_fonts[i] != NULL; i++) {
314 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
318 if (bar_fonts[i] == NULL)
319 errx(1, "couldn't load font");
320 bar_height = bar_fs->ascent + bar_fs->descent + 3;
322 bar_window = XCreateSimpleWindow(display, root, 0, 0, width,
323 bar_height - 2, 1, bar_border, bar_color);
324 bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv);
325 XSetFont(display, bar_gc, bar_fs->fid);
326 XSelectInput(display, bar_window, VisibilityChangeMask);
328 height -= bar_height; /* correct screen height */
329 XMapWindow(display, bar_window);
331 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %d\n", (int)bar_window);
333 if (signal(SIGALRM, bar_signal) == SIG_ERR)
334 err(1, "could not install bar_signal");
339 count_win(int wsid, int count_transient)
344 TAILQ_FOREACH (win, &ws[wsid].winlist, entry) {
345 if (count_transient == 0 && win->transient)
349 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
354 quit(union arg *args)
356 DNPRINTF(SWM_D_MISC, "quit\n");
362 restart(union arg *args)
364 XCloseDisplay(display);
365 execvp(args->argv[0], args->argv);
366 fprintf(stderr, "execvp failed\n");
372 spawn(union arg *args)
374 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
376 * The double-fork construct avoids zombie processes and keeps the code
377 * clean from stupid signal handlers.
382 close(ConnectionNumber(display));
384 execvp(args->argv[0], args->argv);
385 fprintf(stderr, "execvp failed\n");
394 focus_win(struct ws_win *win)
396 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
397 XSetWindowBorder(display, win->id, color_focus);
398 XSetInputFocus(display, win->id, RevertToPointerRoot, CurrentTime);
399 ws[current_ws].focus = win;
403 unfocus_win(struct ws_win *win)
405 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
406 XSetWindowBorder(display, win->id, color_unfocus);
407 if (ws[current_ws].focus == win)
408 ws[current_ws].focus = NULL;
412 switchws(union arg *args)
417 DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
419 if (wsid == current_ws)
422 /* map new window first to prevent ugly blinking */
423 TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
424 XMapRaised(display, win->id);
425 ws[wsid].visible = 1;
427 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
428 XUnmapWindow(display, win->id);
429 ws[current_ws].visible = 0;
434 if (ws[wsid].restack) {
438 if (ws[wsid].focus != NULL)
439 focus_win(ws[wsid].focus);
440 XSync(display, False);
445 focus(union arg *args)
447 struct ws_win *winfocus, *winlostfocus;
449 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
450 if (ws[current_ws].focus == NULL || count_win(current_ws, 1) == 0)
453 winlostfocus = ws[current_ws].focus;
456 case SWM_ARG_ID_FOCUSPREV:
457 ws[current_ws].focus =
458 TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
459 if (ws[current_ws].focus == NULL)
460 ws[current_ws].focus =
461 TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
464 case SWM_ARG_ID_FOCUSNEXT:
465 ws[current_ws].focus = TAILQ_NEXT(ws[current_ws].focus, entry);
466 if (ws[current_ws].focus == NULL)
467 ws[current_ws].focus =
468 TAILQ_FIRST(&ws[current_ws].winlist);
471 case SWM_ARG_ID_FOCUSMAIN:
472 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
479 winfocus = ws[current_ws].focus;
480 unfocus_win(winlostfocus);
482 XSync(display, False);
485 /* I know this sucks but it works well enough */
490 struct ws_win wf, *win, *winfocus = &wf;
491 int i, h, w, x, y, hrh, winno;
495 DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
499 ws[current_ws].restack = 0;
501 winno = count_win(current_ws, 0);
511 hrh = height / (winno - 1);
516 y = bar_enabled ? bar_height : 0;
519 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
524 if (i != 0 && hrh != 0) {
525 /* correct the last window for lost pixels */
526 if (win == TAILQ_LAST(&ws[current_ws].winlist,
528 h = height - (i * hrh);
536 /* leave first right hand window at y = 0 */
542 if (win->transient != 0 || win->floating != 0)
547 bzero(&wc, sizeof wc);
552 win->width = wc.width = w;
553 win->height = wc.height = h;
554 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
556 /* make sure we don't clobber the screen */
557 if (win->wa.width > width)
558 win->wa.width = width;
559 if (win->wa.height > height)
560 win->wa.width = height;
561 win->x = wc.x = (width - win->wa.width) / 2;
562 win->y = wc.y = (height - win->wa.height) / 2;
563 mask = CWX | CWY | CWBorderWidth;
565 XConfigureWindow(display, win->id, mask, &wc);
567 if (win == ws[current_ws].focus)
571 XMapRaised(display, win->id);
575 focus_win(winfocus); /* this has to be done outside of the loop */
576 XSync(display, False);
580 swap_to_main(union arg *args)
582 struct ws_win *tmpwin = TAILQ_FIRST(&ws[current_ws].winlist);
584 DNPRINTF(SWM_D_MISC, "swap_to_main: win: %lu\n",
585 ws[current_ws].focus ? ws[current_ws].focus->id : 0);
587 if (ws[current_ws].focus == NULL || ws[current_ws].focus == tmpwin)
590 TAILQ_REMOVE(&ws[current_ws].winlist, tmpwin, entry);
591 TAILQ_INSERT_AFTER(&ws[current_ws].winlist, ws[current_ws].focus,
593 TAILQ_REMOVE(&ws[current_ws].winlist, ws[current_ws].focus, entry);
594 TAILQ_INSERT_HEAD(&ws[current_ws].winlist, ws[current_ws].focus, entry);
595 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
601 send_to_ws(union arg *args)
604 struct ws_win *win = ws[current_ws].focus;
606 DNPRINTF(SWM_D_WS, "send_to_ws: win: %lu\n", win->id);
608 XUnmapWindow(display, win->id);
610 /* find a window to focus */
611 ws[current_ws].focus = TAILQ_PREV(win, ws_win_list,entry);
612 if (ws[current_ws].focus == NULL)
613 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
615 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
617 TAILQ_INSERT_TAIL(&ws[wsid].winlist, win, entry);
618 if (count_win(wsid, 1) == 0)
619 ws[wsid].focus = win;
620 ws[wsid].restack = 1;
625 /* key definitions */
629 void (*func)(union arg *);
632 /* modifier key function argument */
633 { MODKEY, XK_Return, swap_to_main, {0} },
634 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
635 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
636 { MODKEY | ShiftMask, XK_q, quit, {0} },
637 { MODKEY, XK_q, restart, {.argv = spawn_scrotwm } },
638 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
639 { MODKEY, XK_1, switchws, {.id = 0} },
640 { MODKEY, XK_2, switchws, {.id = 1} },
641 { MODKEY, XK_3, switchws, {.id = 2} },
642 { MODKEY, XK_4, switchws, {.id = 3} },
643 { MODKEY, XK_5, switchws, {.id = 4} },
644 { MODKEY, XK_6, switchws, {.id = 5} },
645 { MODKEY, XK_7, switchws, {.id = 6} },
646 { MODKEY, XK_8, switchws, {.id = 7} },
647 { MODKEY, XK_9, switchws, {.id = 8} },
648 { MODKEY, XK_0, switchws, {.id = 9} },
649 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
650 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
651 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
652 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
653 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
654 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
655 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
656 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
657 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
658 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
659 { MODKEY, XK_b, bar_toggle, {0} },
660 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
661 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
665 updatenumlockmask(void)
668 XModifierKeymap *modmap;
670 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
672 modmap = XGetModifierMapping(display);
673 for (i = 0; i < 8; i++)
674 for (j = 0; j < modmap->max_keypermod; j++)
675 if (modmap->modifiermap[i * modmap->max_keypermod + j]
676 == XKeysymToKeycode(display, XK_Num_Lock))
677 numlockmask = (1 << i);
679 XFreeModifiermap(modmap);
687 unsigned int modifiers[] =
688 { 0, LockMask, numlockmask, numlockmask | LockMask };
690 DNPRINTF(SWM_D_MISC, "grabkeys\n");
693 XUngrabKey(display, AnyKey, AnyModifier, root);
694 for(i = 0; i < LENGTH(keys); i++) {
695 if((code = XKeysymToKeycode(display, keys[i].keysym)))
696 for(j = 0; j < LENGTH(modifiers); j++)
697 XGrabKey(display, code,
698 keys[i].mod | modifiers[j], root,
699 True, GrabModeAsync, GrabModeAsync);
705 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
713 XKeyEvent *ev = &e->xkey;
715 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
717 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
718 for(i = 0; i < LENGTH(keys); i++)
719 if(keysym == keys[i].keysym
720 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
722 keys[i].func(&(keys[i].args));
726 buttonpress(XEvent *e)
728 XButtonPressedEvent *ev = &e->xbutton;
729 #ifdef SWM_CLICKTOFOCUS
734 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
736 if (ev->window == root)
738 if (ev->window == ws[current_ws].focus->id)
740 #ifdef SWM_CLICKTOFOCUS
741 TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
742 if (win->id == ev->window) {
743 /* focus in the clicked window */
744 XSetWindowBorder(display, ev->window, 0xff0000);
745 XSetWindowBorder(display,
746 ws[current_ws].focus->id, 0x888888);
747 XSetInputFocus(display, ev->window, RevertToPointerRoot,
749 ws[current_ws].focus = win;
750 XSync(display, False);
757 manage_window(Window id)
761 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
763 return (win); /* already being managed */
766 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
767 errx(1, "calloc: failed to allocate memory for new window");
770 TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
772 XSelectInput(display, id, ButtonPressMask | EnterWindowMask |
773 FocusChangeMask | ExposureMask);
780 configurerequest(XEvent *e)
782 XConfigureRequestEvent *ev = &e->xconfigurerequest;
786 DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window);
789 win = manage_window(ev->window);
790 ws[current_ws].focus = win; /* make new win focused */
792 XGetTransientForHint(display, win->id, &trans);
794 win->transient = trans;
795 DNPRINTF(SWM_D_MISC, "configurerequest: win %u transient %u\n",
796 (unsigned)win->id, win->transient);
798 XGetWindowAttributes(display, win->id, &win->wa);
800 XClassHint ch = { 0 };
801 if(XGetClassHint(display, win->id, &ch)) {
802 fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name);
803 if (!strcmp(ch.res_class, "Gvim") && !strcmp(ch.res_name, "gvim")) {
816 configurenotify(XEvent *e)
818 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
819 e->xconfigure.window);
823 destroynotify(XEvent *e)
826 XDestroyWindowEvent *ev = &e->xdestroywindow;
828 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
830 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
831 if (ev->window == win->id) {
832 /* find a window to focus */
833 ws[current_ws].focus =
834 TAILQ_PREV(win, ws_win_list,entry);
835 if (ws[current_ws].focus == NULL)
836 ws[current_ws].focus =
837 TAILQ_FIRST(&ws[current_ws].winlist);
839 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
849 enternotify(XEvent *e)
851 XCrossingEvent *ev = &e->xcrossing;
854 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
856 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
860 /* eat event(s) to prevent autofocus */
864 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
865 if (win->id == ev->window)
875 XFocusChangeEvent *ev = &e->xfocus;
877 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window);
879 XSync(display, False); /* not sure this helps redrawing graphic apps */
881 if (ev->window == root)
884 * kill grab for now so that we can cut and paste , this screws up
888 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
889 XGrabButton(display, Button1, AnyModifier, ev->window, False,
890 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
895 mappingnotify(XEvent *e)
897 XMappingEvent *ev = &e->xmapping;
899 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
901 XRefreshKeyboardMapping(ev);
902 if(ev->request == MappingKeyboard)
907 maprequest(XEvent *e)
909 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
910 e->xmaprequest.window);
912 manage_window(e->xmaprequest.window);
917 propertynotify(XEvent *e)
919 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
920 e->xproperty.window);
924 unmapnotify(XEvent *e)
926 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
930 visibilitynotify(XEvent *e)
932 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n", e->xvisibility.window);
934 if (e->xvisibility.window == bar_window &&
935 e->xvisibility.state == VisibilityUnobscured)
939 void (*handler[LASTEvent])(XEvent *) = {
941 [KeyPress] = keypress,
942 [ButtonPress] = buttonpress,
943 [ConfigureRequest] = configurerequest,
944 [ConfigureNotify] = configurenotify,
945 [DestroyNotify] = destroynotify,
946 [EnterNotify] = enternotify,
948 [MappingNotify] = mappingnotify,
949 [MapRequest] = maprequest,
950 [PropertyNotify] = propertynotify,
951 [UnmapNotify] = unmapnotify,
952 [VisibilityNotify] = visibilitynotify,
956 xerror_start(Display *d, XErrorEvent *ee)
963 xerror(Display *d, XErrorEvent *ee)
965 fprintf(stderr, "error: %p %p\n", display, ee);
974 xerrorxlib = XSetErrorHandler(xerror_start);
976 /* this causes an error if some other window manager is running */
977 XSelectInput(display, DefaultRootWindow(display),
978 SubstructureRedirectMask);
979 XSync(display, False);
983 XSetErrorHandler(xerror);
984 XSync(display, False);
989 main(int argc, char *argv[])
992 char conf[PATH_MAX], *cfile = NULL;
996 Window d1, d2, *wins = NULL;
997 XWindowAttributes wa;
999 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1000 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1001 warnx("no locale support");
1003 if(!(display = XOpenDisplay(0)))
1004 errx(1, "can not open display");
1007 errx(1, "other wm running");
1009 screen = DefaultScreen(display);
1010 root = RootWindow(display, screen);
1011 width = DisplayWidth(display, screen) - 2;
1012 height = DisplayHeight(display, screen) - 2;
1014 /* look for local and global conf file */
1015 pwd = getpwuid(getuid());
1017 errx(1, "invalid user %d", getuid());
1019 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
1020 if (stat(conf, &sb) != -1) {
1021 if (S_ISREG(sb.st_mode))
1024 /* try global conf file */
1025 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
1026 if (!stat(conf, &sb))
1027 if (S_ISREG(sb.st_mode))
1033 /* make work space 1 active */
1036 TAILQ_INIT(&ws[0].winlist);
1037 for (i = 1; i < SWM_WS_MAX; i++) {
1041 TAILQ_INIT(&ws[i].winlist);
1044 /* grab existing windows */
1045 if (XQueryTree(display, root, &d1, &d2, &wins, &num)) {
1046 for (i = 0; i < num; i++) {
1047 if (!XGetWindowAttributes(display, wins[i], &wa)
1048 || wa.override_redirect ||
1049 XGetTransientForHint(display, wins[i], &d1))
1051 manage_window(wins[i]);
1056 ws[0].focus = TAILQ_FIRST(&ws[0].winlist);
1058 /* setup status bar */
1061 XSelectInput(display, root, SubstructureRedirectMask |
1062 SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
1063 EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
1064 FocusChangeMask | PropertyChangeMask | ExposureMask);
1070 XNextEvent(display, &e);
1071 if (handler[e.type])
1072 handler[e.type](&e);
1075 XCloseDisplay(display);