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 uint32_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 };
135 TAILQ_ENTRY(ws_win) entry;
143 XWindowAttributes wa;
146 TAILQ_HEAD(ws_win_list, ws_win);
148 /* define work spaces */
149 #define SWM_WS_MAX (10)
151 int visible; /* workspace visible */
152 int restack; /* restack on switch */
153 struct ws_win *focus; /* which win has focus */
154 struct ws_win_list winlist; /* list of windows in ws */
158 /* args to functions */
161 #define SWM_ARG_ID_FOCUSNEXT (0)
162 #define SWM_ARG_ID_FOCUSPREV (1)
163 #define SWM_ARG_ID_FOCUSMAIN (2)
170 #define SWM_CONF_WS "\n= \t"
171 #define SWM_CONF_FILE "scrotwm.conf"
173 conf_load(char *filename)
176 char *line, *cp, *var, *val;
177 size_t len, lineno = 0;
179 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
181 if (filename == NULL)
184 if ((config = fopen(filename, "r")) == NULL)
188 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
192 cp += (long)strspn(cp, SWM_CONF_WS);
198 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
200 cp += (long)strspn(cp, SWM_CONF_WS);
201 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
204 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
207 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
208 bar_enabled = atoi(val);
209 else if (!strncmp(var, "bar_border",
210 strlen("bar_border")))
211 bar_border = strtol(val, NULL, 16);
212 else if (!strncmp(var, "bar_color",
213 strlen("bar_color")))
214 bar_color = strtol(val, NULL, 16);
215 else if (!strncmp(var, "bar_font_color",
216 strlen("bar_font_color")))
217 bar_font_color = strtol(val, NULL, 16);
218 else if (!strncmp(var, "bar_font", strlen("bar_font")))
219 asprintf(&bar_fonts[0], "%s", val);
225 if (!strncmp(var, "color_focus", strlen("color_focus")))
226 color_focus = strtol(val, NULL, 16);
227 else if (!strncmp(var, "color_unfocus",
228 strlen("color_unfocus")))
229 color_unfocus = strtol(val, NULL, 16);
235 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
236 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
247 errx(1, "invalid conf file entry: %s=%s", var, val);
256 if (bar_enabled == 0)
260 XSetForeground(display, bar_gc, bar_color);
261 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
266 localtime_r(&tmt, &tm);
267 strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
268 XSetForeground(display, bar_gc, bar_font_color);
269 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
271 XSync(display, False);
279 /* XXX yeah yeah byte me */
284 bar_toggle(union arg *args)
288 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
292 height += bar_height; /* correct screen height */
293 XUnmapWindow(display, bar_window);
296 height -= bar_height; /* correct screen height */
297 XMapWindow(display, bar_window);
299 XSync(display, False);
300 for (i = 0; i < SWM_WS_MAX; i++)
304 bar_print(); /* must be after stack */
312 for (i = 0; bar_fonts[i] != NULL; i++) {
313 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
317 if (bar_fonts[i] == NULL)
318 errx(1, "couldn't load font");
319 bar_height = bar_fs->ascent + bar_fs->descent + 3;
321 bar_window = XCreateSimpleWindow(display, root, 0, 0, width,
322 bar_height - 2, 1, bar_border, bar_color);
323 bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv);
324 XSetFont(display, bar_gc, bar_fs->fid);
325 XSelectInput(display, bar_window, VisibilityChangeMask);
327 height -= bar_height; /* correct screen height */
328 XMapWindow(display, bar_window);
330 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %d\n", (int)bar_window);
332 if (signal(SIGALRM, bar_signal) == SIG_ERR)
333 err(1, "could not install bar_signal");
338 count_win(int wsid, int count_transient)
343 TAILQ_FOREACH (win, &ws[wsid].winlist, entry) {
344 if (count_transient == 0 && win->transient)
348 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
353 quit(union arg *args)
355 DNPRINTF(SWM_D_MISC, "quit\n");
360 spawn(union arg *args)
362 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
364 * The double-fork construct avoids zombie processes and keeps the code
365 * clean from stupid signal handlers.
370 close(ConnectionNumber(display));
372 execvp(args->argv[0], args->argv);
373 fprintf(stderr, "execvp failed\n");
382 focus_win(struct ws_win *win)
384 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
385 XSetWindowBorder(display, win->id, color_focus);
386 XSetInputFocus(display, win->id, RevertToPointerRoot, CurrentTime);
387 ws[current_ws].focus = win;
391 unfocus_win(struct ws_win *win)
393 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
394 XSetWindowBorder(display, win->id, color_unfocus);
395 if (ws[current_ws].focus == win)
396 ws[current_ws].focus = NULL;
400 switchws(union arg *args)
405 DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
407 if (wsid == current_ws)
410 /* map new window first to prevent ugly blinking */
411 TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
412 XMapRaised(display, win->id);
413 ws[wsid].visible = 1;
415 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
416 XUnmapWindow(display, win->id);
417 ws[current_ws].visible = 0;
422 if (ws[wsid].restack) {
426 if (ws[wsid].focus != NULL)
427 focus_win(ws[wsid].focus);
428 XSync(display, False);
433 focus(union arg *args)
435 struct ws_win *winfocus, *winlostfocus;
437 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
438 if (ws[current_ws].focus == NULL || count_win(current_ws, 1) == 0)
441 winlostfocus = ws[current_ws].focus;
444 case SWM_ARG_ID_FOCUSPREV:
445 ws[current_ws].focus =
446 TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
447 if (ws[current_ws].focus == NULL)
448 ws[current_ws].focus =
449 TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
452 case SWM_ARG_ID_FOCUSNEXT:
453 ws[current_ws].focus = TAILQ_NEXT(ws[current_ws].focus, entry);
454 if (ws[current_ws].focus == NULL)
455 ws[current_ws].focus =
456 TAILQ_FIRST(&ws[current_ws].winlist);
459 case SWM_ARG_ID_FOCUSMAIN:
460 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
467 winfocus = ws[current_ws].focus;
468 unfocus_win(winlostfocus);
470 XSync(display, False);
473 /* I know this sucks but it works well enough */
478 struct ws_win wf, *win, *winfocus = &wf;
479 int i, h, w, x, y, hrh, winno;
483 DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
487 ws[current_ws].restack = 0;
489 winno = count_win(current_ws, 0);
499 hrh = height / (winno - 1);
504 y = bar_enabled ? bar_height : 0;
507 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
512 if (i != 0 && hrh != 0) {
513 /* correct the last window for lost pixels */
514 if (win == TAILQ_LAST(&ws[current_ws].winlist,
516 h = height - (i * hrh);
524 /* leave first right hand window at y = 0 */
530 if (win->transient != 0 || win->floating != 0)
535 bzero(&wc, sizeof wc);
540 win->width = wc.width = w;
541 win->height = wc.height = h;
542 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
544 /* make sure we don't clobber the screen */
545 if (win->wa.width > width)
546 win->wa.width = width;
547 if (win->wa.height > height)
548 win->wa.width = height;
549 win->x = wc.x = (width - win->wa.width) / 2;
550 win->y = wc.y = (height - win->wa.height) / 2;
551 mask = CWX | CWY | CWBorderWidth;
553 XConfigureWindow(display, win->id, mask, &wc);
555 if (win == ws[current_ws].focus)
559 XMapRaised(display, win->id);
563 focus_win(winfocus); /* this has to be done outside of the loop */
564 XSync(display, False);
568 swap_to_main(union arg *args)
570 struct ws_win *tmpwin = TAILQ_FIRST(&ws[current_ws].winlist);
572 DNPRINTF(SWM_D_MISC, "swap_to_main: win: %lu\n",
573 ws[current_ws].focus ? ws[current_ws].focus->id : 0);
575 if (ws[current_ws].focus == NULL || ws[current_ws].focus == tmpwin)
578 TAILQ_REMOVE(&ws[current_ws].winlist, tmpwin, entry);
579 TAILQ_INSERT_AFTER(&ws[current_ws].winlist, ws[current_ws].focus,
581 TAILQ_REMOVE(&ws[current_ws].winlist, ws[current_ws].focus, entry);
582 TAILQ_INSERT_HEAD(&ws[current_ws].winlist, ws[current_ws].focus, entry);
583 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
589 send_to_ws(union arg *args)
592 struct ws_win *win = ws[current_ws].focus;
594 DNPRINTF(SWM_D_WS, "send_to_ws: win: %lu\n", win->id);
596 XUnmapWindow(display, win->id);
598 /* find a window to focus */
599 ws[current_ws].focus = TAILQ_PREV(win, ws_win_list,entry);
600 if (ws[current_ws].focus == NULL)
601 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
603 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
605 TAILQ_INSERT_TAIL(&ws[wsid].winlist, win, entry);
606 if (count_win(wsid, 1) == 0)
607 ws[wsid].focus = win;
608 ws[wsid].restack = 1;
613 /* key definitions */
617 void (*func)(union arg *);
620 /* modifier key function argument */
621 { MODKEY, XK_Return, swap_to_main, {0} },
622 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
623 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
624 { MODKEY | ShiftMask, XK_q, quit, {0} },
625 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
626 { MODKEY, XK_1, switchws, {.id = 0} },
627 { MODKEY, XK_2, switchws, {.id = 1} },
628 { MODKEY, XK_3, switchws, {.id = 2} },
629 { MODKEY, XK_4, switchws, {.id = 3} },
630 { MODKEY, XK_5, switchws, {.id = 4} },
631 { MODKEY, XK_6, switchws, {.id = 5} },
632 { MODKEY, XK_7, switchws, {.id = 6} },
633 { MODKEY, XK_8, switchws, {.id = 7} },
634 { MODKEY, XK_9, switchws, {.id = 8} },
635 { MODKEY, XK_0, switchws, {.id = 9} },
636 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
637 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
638 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
639 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
640 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
641 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
642 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
643 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
644 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
645 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
646 { MODKEY, XK_b, bar_toggle, {0} },
647 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
648 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
652 updatenumlockmask(void)
655 XModifierKeymap *modmap;
657 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
659 modmap = XGetModifierMapping(display);
660 for (i = 0; i < 8; i++)
661 for (j = 0; j < modmap->max_keypermod; j++)
662 if (modmap->modifiermap[i * modmap->max_keypermod + j]
663 == XKeysymToKeycode(display, XK_Num_Lock))
664 numlockmask = (1 << i);
666 XFreeModifiermap(modmap);
674 unsigned int modifiers[] =
675 { 0, LockMask, numlockmask, numlockmask | LockMask };
677 DNPRINTF(SWM_D_MISC, "grabkeys\n");
680 XUngrabKey(display, AnyKey, AnyModifier, root);
681 for(i = 0; i < LENGTH(keys); i++) {
682 if((code = XKeysymToKeycode(display, keys[i].keysym)))
683 for(j = 0; j < LENGTH(modifiers); j++)
684 XGrabKey(display, code,
685 keys[i].mod | modifiers[j], root,
686 True, GrabModeAsync, GrabModeAsync);
692 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
700 XKeyEvent *ev = &e->xkey;
702 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
704 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
705 for(i = 0; i < LENGTH(keys); i++)
706 if(keysym == keys[i].keysym
707 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
709 keys[i].func(&(keys[i].args));
713 buttonpress(XEvent *e)
715 XButtonPressedEvent *ev = &e->xbutton;
716 #ifdef SWM_CLICKTOFOCUS
721 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
723 if (ev->window == root)
725 if (ev->window == ws[current_ws].focus->id)
727 #ifdef SWM_CLICKTOFOCUS
728 TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
729 if (win->id == ev->window) {
730 /* focus in the clicked window */
731 XSetWindowBorder(display, ev->window, 0xff0000);
732 XSetWindowBorder(display,
733 ws[current_ws].focus->id, 0x888888);
734 XSetInputFocus(display, ev->window, RevertToPointerRoot,
736 ws[current_ws].focus = win;
737 XSync(display, False);
744 configurerequest(XEvent *e)
746 XConfigureRequestEvent *ev = &e->xconfigurerequest;
750 DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window);
752 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
753 if (ev->window == win->id)
757 XSelectInput(display, ev->window, ButtonPressMask | EnterWindowMask |
758 FocusChangeMask | ExposureMask);
760 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
761 errx(1, "calloc: failed to allocate memory for new window");
763 win->id = ev->window;
764 TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
765 ws[current_ws].focus = win; /* make new win focused */
767 XGetTransientForHint(display, win->id, &trans);
769 win->transient = trans;
770 DNPRINTF(SWM_D_MISC, "configurerequest: win %u transient %u\n",
771 (unsigned)win->id, win->transient);
773 XGetWindowAttributes(display, win->id, &win->wa);
775 XClassHint ch = { 0 };
776 if(XGetClassHint(display, win->id, &ch)) {
777 fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name);
778 if (!strcmp(ch.res_class, "Gvim") && !strcmp(ch.res_name, "gvim")) {
791 configurenotify(XEvent *e)
793 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
794 e->xconfigure.window);
798 destroynotify(XEvent *e)
801 XDestroyWindowEvent *ev = &e->xdestroywindow;
803 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
805 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
806 if (ev->window == win->id) {
807 /* find a window to focus */
808 ws[current_ws].focus =
809 TAILQ_PREV(win, ws_win_list,entry);
810 if (ws[current_ws].focus == NULL)
811 ws[current_ws].focus =
812 TAILQ_FIRST(&ws[current_ws].winlist);
814 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
824 enternotify(XEvent *e)
826 XCrossingEvent *ev = &e->xcrossing;
829 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
831 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
835 /* eat event(s) to prevent autofocus */
839 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
840 if (win->id == ev->window)
850 XFocusChangeEvent *ev = &e->xfocus;
852 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window);
854 XSync(display, False); /* not sure this helps redrawing graphic apps */
856 if (ev->window == root)
859 * kill grab for now so that we can cut and paste , this screws up
863 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
864 XGrabButton(display, Button1, AnyModifier, ev->window, False,
865 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
870 mappingnotify(XEvent *e)
872 XMappingEvent *ev = &e->xmapping;
874 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
876 XRefreshKeyboardMapping(ev);
877 if(ev->request == MappingKeyboard)
882 maprequest(XEvent *e)
884 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
885 e->xmaprequest.window);
889 propertynotify(XEvent *e)
891 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
892 e->xproperty.window);
896 unmapnotify(XEvent *e)
898 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
902 visibilitynotify(XEvent *e)
904 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n", e->xvisibility.window);
906 if (e->xvisibility.window == bar_window &&
907 e->xvisibility.state == VisibilityUnobscured)
911 void (*handler[LASTEvent])(XEvent *) = {
913 [KeyPress] = keypress,
914 [ButtonPress] = buttonpress,
915 [ConfigureRequest] = configurerequest,
916 [ConfigureNotify] = configurenotify,
917 [DestroyNotify] = destroynotify,
918 [EnterNotify] = enternotify,
920 [MappingNotify] = mappingnotify,
921 [MapRequest] = maprequest,
922 [PropertyNotify] = propertynotify,
923 [UnmapNotify] = unmapnotify,
924 [VisibilityNotify] = visibilitynotify,
928 xerror_start(Display *d, XErrorEvent *ee)
935 xerror(Display *d, XErrorEvent *ee)
937 fprintf(stderr, "error: %p %p\n", display, ee);
946 xerrorxlib = XSetErrorHandler(xerror_start);
948 /* this causes an error if some other window manager is running */
949 XSelectInput(display, DefaultRootWindow(display),
950 SubstructureRedirectMask);
951 XSync(display, False);
955 XSetErrorHandler(xerror);
956 XSync(display, False);
961 main(int argc, char *argv[])
964 char conf[PATH_MAX], *cfile = NULL;
969 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
970 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
971 warnx("no locale support");
973 if(!(display = XOpenDisplay(0)))
974 errx(1, "can not open display");
977 errx(1, "other wm running");
979 screen = DefaultScreen(display);
980 root = RootWindow(display, screen);
981 width = DisplayWidth(display, screen) - 2;
982 height = DisplayHeight(display, screen) - 2;
984 /* look for local and global conf file */
985 pwd = getpwuid(getuid());
987 errx(1, "invalid user %d", getuid());
989 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
990 if (stat(conf, &sb) != -1) {
991 if (S_ISREG(sb.st_mode))
994 /* try global conf file */
995 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
996 if (!stat(conf, &sb))
997 if (S_ISREG(sb.st_mode))
1003 /* make work space 1 active */
1007 TAILQ_INIT(&ws[0].winlist);
1008 for (i = 1; i < SWM_WS_MAX; i++) {
1012 TAILQ_INIT(&ws[i].winlist);
1015 /* setup status bar */
1018 XSelectInput(display, root, SubstructureRedirectMask |
1019 SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
1020 EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
1021 FocusChangeMask | PropertyChangeMask | ExposureMask);
1026 XNextEvent(display, &e);
1027 if (handler[e.type])
1028 handler[e.type](&e);
1031 XCloseDisplay(display);