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;
145 TAILQ_HEAD(ws_win_list, ws_win);
147 /* define work spaces */
148 #define SWM_WS_MAX (10)
150 int visible; /* workspace visible */
151 int restack; /* restack on switch */
152 struct ws_win *focus; /* which win has focus */
153 struct ws_win_list winlist; /* list of windows in ws */
157 /* args to functions */
160 #define SWM_ARG_ID_FOCUSNEXT (0)
161 #define SWM_ARG_ID_FOCUSPREV (1)
162 #define SWM_ARG_ID_FOCUSMAIN (2)
169 #define SWM_CONF_WS "\n= \t"
170 #define SWM_CONF_FILE "scrotwm.conf"
172 conf_load(char *filename)
175 char *line, *cp, *var, *val;
176 size_t len, lineno = 0;
178 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
180 if (filename == NULL)
183 if ((config = fopen(filename, "r")) == NULL)
187 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
191 cp += (long)strspn(cp, SWM_CONF_WS);
197 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
199 cp += (long)strspn(cp, SWM_CONF_WS);
200 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
203 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
206 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
207 bar_enabled = atoi(val);
208 else if (!strncmp(var, "bar_border",
209 strlen("bar_border")))
210 bar_border = strtol(val, NULL, 16);
211 else if (!strncmp(var, "bar_color",
212 strlen("bar_color")))
213 bar_color = strtol(val, NULL, 16);
214 else if (!strncmp(var, "bar_font_color",
215 strlen("bar_font_color")))
216 bar_font_color = strtol(val, NULL, 16);
217 else if (!strncmp(var, "bar_font", strlen("bar_font")))
218 asprintf(&bar_fonts[0], "%s", val);
224 if (!strncmp(var, "color_focus", strlen("color_focus")))
225 color_focus = strtol(val, NULL, 16);
226 else if (!strncmp(var, "color_unfocus",
227 strlen("color_unfocus")))
228 color_unfocus = strtol(val, NULL, 16);
234 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
235 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
246 errx(1, "invalid conf file entry: %s=%s", var, val);
255 if (bar_enabled == 0)
259 XSetForeground(display, bar_gc, bar_color);
260 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
265 localtime_r(&tmt, &tm);
266 strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
267 XSetForeground(display, bar_gc, bar_font_color);
268 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
270 XSync(display, False);
278 /* XXX yeah yeah byte me */
283 bar_toggle(union arg *args)
287 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
291 height += bar_height; /* correct screen height */
292 XUnmapWindow(display, bar_window);
295 height -= bar_height; /* correct screen height */
296 XMapWindow(display, bar_window);
298 XSync(display, False);
299 for (i = 0; i < SWM_WS_MAX; i++)
303 bar_print(); /* must be after stack */
311 for (i = 0; bar_fonts[i] != NULL; i++) {
312 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
316 if (bar_fonts[i] == NULL)
317 errx(1, "couldn't load font");
318 bar_height = bar_fs->ascent + bar_fs->descent + 3;
320 bar_window = XCreateSimpleWindow(display, root, 0, 0, width,
321 bar_height - 2, 1, bar_border, bar_color);
322 bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv);
323 XSetFont(display, bar_gc, bar_fs->fid);
324 XSelectInput(display, bar_window, VisibilityChangeMask);
326 height -= bar_height; /* correct screen height */
327 XMapWindow(display, bar_window);
329 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %d\n", (int)bar_window);
331 if (signal(SIGALRM, bar_signal) == SIG_ERR)
332 err(1, "could not install bar_signal");
337 count_win(int wsid, int count_transient)
342 TAILQ_FOREACH (win, &ws[wsid].winlist, entry) {
343 if (count_transient == 0 && win->transient)
347 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
352 quit(union arg *args)
354 DNPRINTF(SWM_D_MISC, "quit\n");
359 spawn(union arg *args)
361 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
363 * The double-fork construct avoids zombie processes and keeps the code
364 * clean from stupid signal handlers.
369 close(ConnectionNumber(display));
371 execvp(args->argv[0], args->argv);
372 fprintf(stderr, "execvp failed\n");
381 focus_win(struct ws_win *win)
383 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
384 XSetWindowBorder(display, win->id, color_focus);
385 XSetInputFocus(display, win->id, RevertToPointerRoot, CurrentTime);
386 ws[current_ws].focus = win;
390 unfocus_win(struct ws_win *win)
392 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
393 XSetWindowBorder(display, win->id, color_unfocus);
394 if (ws[current_ws].focus == win)
395 ws[current_ws].focus = NULL;
399 switchws(union arg *args)
404 DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
406 if (wsid == current_ws)
409 /* map new window first to prevent ugly blinking */
410 TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
411 XMapRaised(display, win->id);
412 ws[wsid].visible = 1;
414 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
415 XUnmapWindow(display, win->id);
416 ws[current_ws].visible = 0;
421 if (ws[wsid].restack) {
425 if (ws[wsid].focus != NULL)
426 focus_win(ws[wsid].focus);
427 XSync(display, False);
432 focus(union arg *args)
434 struct ws_win *winfocus, *winlostfocus;
436 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
437 if (ws[current_ws].focus == NULL || count_win(current_ws, 1) == 0)
440 winlostfocus = ws[current_ws].focus;
443 case SWM_ARG_ID_FOCUSPREV:
444 ws[current_ws].focus =
445 TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
446 if (ws[current_ws].focus == NULL)
447 ws[current_ws].focus =
448 TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
451 case SWM_ARG_ID_FOCUSNEXT:
452 ws[current_ws].focus = TAILQ_NEXT(ws[current_ws].focus, entry);
453 if (ws[current_ws].focus == NULL)
454 ws[current_ws].focus =
455 TAILQ_FIRST(&ws[current_ws].winlist);
458 case SWM_ARG_ID_FOCUSMAIN:
459 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
466 winfocus = ws[current_ws].focus;
467 unfocus_win(winlostfocus);
469 XSync(display, False);
472 /* I know this sucks but it works well enough */
477 struct ws_win wf, *win, *winfocus = &wf;
478 int i, h, w, x, y, hrh, winno;
482 DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
486 ws[current_ws].restack = 0;
488 winno = count_win(current_ws, 0);
498 hrh = height / (winno - 1);
503 y = bar_enabled ? bar_height : 0;
506 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
511 if (i != 0 && hrh != 0) {
512 /* correct the last window for lost pixels */
513 if (win == TAILQ_LAST(&ws[current_ws].winlist,
515 h = height - (i * hrh);
523 /* leave first right hand window at y = 0 */
529 if (win->transient != 0 || win->floating != 0)
534 bzero(&wc, sizeof wc);
539 win->width = wc.width = w;
540 win->height = wc.height = h;
541 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
543 win->x = wc.x = width / 2;
544 win->y = wc.y = height / 2;
545 mask = CWX | CWY | CWBorderWidth;
547 XConfigureWindow(display, win->id, mask, &wc);
549 if (win == ws[current_ws].focus)
553 XMapRaised(display, win->id);
557 focus_win(winfocus); /* this has to be done outside of the loop */
558 XSync(display, False);
562 swap_to_main(union arg *args)
564 struct ws_win *tmpwin = TAILQ_FIRST(&ws[current_ws].winlist);
566 DNPRINTF(SWM_D_MISC, "swap_to_main: win: %lu\n",
567 ws[current_ws].focus ? ws[current_ws].focus->id : 0);
569 if (ws[current_ws].focus == NULL || ws[current_ws].focus == tmpwin)
572 TAILQ_REMOVE(&ws[current_ws].winlist, tmpwin, entry);
573 TAILQ_INSERT_AFTER(&ws[current_ws].winlist, ws[current_ws].focus,
575 TAILQ_REMOVE(&ws[current_ws].winlist, ws[current_ws].focus, entry);
576 TAILQ_INSERT_HEAD(&ws[current_ws].winlist, ws[current_ws].focus, entry);
577 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
583 send_to_ws(union arg *args)
586 struct ws_win *win = ws[current_ws].focus;
588 DNPRINTF(SWM_D_WS, "send_to_ws: win: %lu\n", win->id);
590 XUnmapWindow(display, win->id);
592 /* find a window to focus */
593 ws[current_ws].focus = TAILQ_PREV(win, ws_win_list,entry);
594 if (ws[current_ws].focus == NULL)
595 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
597 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
599 TAILQ_INSERT_TAIL(&ws[wsid].winlist, win, entry);
600 if (count_win(wsid, 1) == 0)
601 ws[wsid].focus = win;
602 ws[wsid].restack = 1;
607 /* key definitions */
611 void (*func)(union arg *);
614 /* modifier key function argument */
615 { MODKEY, XK_Return, swap_to_main, {0} },
616 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
617 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
618 { MODKEY | ShiftMask, XK_q, quit, {0} },
619 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
620 { MODKEY, XK_1, switchws, {.id = 0} },
621 { MODKEY, XK_2, switchws, {.id = 1} },
622 { MODKEY, XK_3, switchws, {.id = 2} },
623 { MODKEY, XK_4, switchws, {.id = 3} },
624 { MODKEY, XK_5, switchws, {.id = 4} },
625 { MODKEY, XK_6, switchws, {.id = 5} },
626 { MODKEY, XK_7, switchws, {.id = 6} },
627 { MODKEY, XK_8, switchws, {.id = 7} },
628 { MODKEY, XK_9, switchws, {.id = 8} },
629 { MODKEY, XK_0, switchws, {.id = 9} },
630 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
631 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
632 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
633 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
634 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
635 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
636 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
637 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
638 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
639 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
640 { MODKEY, XK_b, bar_toggle, {0} },
641 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
642 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
646 updatenumlockmask(void)
649 XModifierKeymap *modmap;
651 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
653 modmap = XGetModifierMapping(display);
654 for (i = 0; i < 8; i++)
655 for (j = 0; j < modmap->max_keypermod; j++)
656 if (modmap->modifiermap[i * modmap->max_keypermod + j]
657 == XKeysymToKeycode(display, XK_Num_Lock))
658 numlockmask = (1 << i);
660 XFreeModifiermap(modmap);
668 unsigned int modifiers[] =
669 { 0, LockMask, numlockmask, numlockmask | LockMask };
671 DNPRINTF(SWM_D_MISC, "grabkeys\n");
674 XUngrabKey(display, AnyKey, AnyModifier, root);
675 for(i = 0; i < LENGTH(keys); i++) {
676 if((code = XKeysymToKeycode(display, keys[i].keysym)))
677 for(j = 0; j < LENGTH(modifiers); j++)
678 XGrabKey(display, code,
679 keys[i].mod | modifiers[j], root,
680 True, GrabModeAsync, GrabModeAsync);
686 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
694 XKeyEvent *ev = &e->xkey;
696 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
698 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
699 for(i = 0; i < LENGTH(keys); i++)
700 if(keysym == keys[i].keysym
701 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
703 keys[i].func(&(keys[i].args));
707 buttonpress(XEvent *e)
709 XButtonPressedEvent *ev = &e->xbutton;
710 #ifdef SWM_CLICKTOFOCUS
715 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
717 if (ev->window == root)
719 if (ev->window == ws[current_ws].focus->id)
721 #ifdef SWM_CLICKTOFOCUS
722 TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
723 if (win->id == ev->window) {
724 /* focus in the clicked window */
725 XSetWindowBorder(display, ev->window, 0xff0000);
726 XSetWindowBorder(display,
727 ws[current_ws].focus->id, 0x888888);
728 XSetInputFocus(display, ev->window, RevertToPointerRoot,
730 ws[current_ws].focus = win;
731 XSync(display, False);
738 configurerequest(XEvent *e)
740 XConfigureRequestEvent *ev = &e->xconfigurerequest;
744 DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window);
746 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
747 if (ev->window == win->id)
751 XSelectInput(display, ev->window, ButtonPressMask | EnterWindowMask |
752 FocusChangeMask | ExposureMask);
754 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
755 errx(1, "calloc: failed to allocate memory for new window");
757 win->id = ev->window;
758 TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
759 ws[current_ws].focus = win; /* make new win focused */
761 XGetTransientForHint(display, win->id, &trans);
763 win->transient = trans;
764 DNPRINTF(SWM_D_MISC, "configurerequest: win %u transient %u\n",
765 (unsigned)win->id, win->transient);
768 XClassHint ch = { 0 };
769 if(XGetClassHint(display, win->id, &ch)) {
770 fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name);
771 if (!strcmp(ch.res_class, "Gvim") && !strcmp(ch.res_name, "gvim")) {
784 configurenotify(XEvent *e)
786 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
787 e->xconfigure.window);
791 destroynotify(XEvent *e)
794 XDestroyWindowEvent *ev = &e->xdestroywindow;
796 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
798 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
799 if (ev->window == win->id) {
800 /* find a window to focus */
801 ws[current_ws].focus =
802 TAILQ_PREV(win, ws_win_list,entry);
803 if (ws[current_ws].focus == NULL)
804 ws[current_ws].focus =
805 TAILQ_FIRST(&ws[current_ws].winlist);
807 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
817 enternotify(XEvent *e)
819 XCrossingEvent *ev = &e->xcrossing;
822 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
824 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
828 /* eat event(s) to prevent autofocus */
832 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
833 if (win->id == ev->window)
843 XFocusChangeEvent *ev = &e->xfocus;
845 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window);
847 XSync(display, False); /* not sure this helps redrawing graphic apps */
849 if (ev->window == root)
852 * kill grab for now so that we can cut and paste , this screws up
856 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
857 XGrabButton(display, Button1, AnyModifier, ev->window, False,
858 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
863 mappingnotify(XEvent *e)
865 XMappingEvent *ev = &e->xmapping;
867 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
869 XRefreshKeyboardMapping(ev);
870 if(ev->request == MappingKeyboard)
875 maprequest(XEvent *e)
877 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
878 e->xmaprequest.window);
882 propertynotify(XEvent *e)
884 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
885 e->xproperty.window);
889 unmapnotify(XEvent *e)
891 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
895 visibilitynotify(XEvent *e)
897 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n", e->xvisibility.window);
899 if (e->xvisibility.window == bar_window &&
900 e->xvisibility.state == VisibilityUnobscured)
904 void (*handler[LASTEvent])(XEvent *) = {
906 [KeyPress] = keypress,
907 [ButtonPress] = buttonpress,
908 [ConfigureRequest] = configurerequest,
909 [ConfigureNotify] = configurenotify,
910 [DestroyNotify] = destroynotify,
911 [EnterNotify] = enternotify,
913 [MappingNotify] = mappingnotify,
914 [MapRequest] = maprequest,
915 [PropertyNotify] = propertynotify,
916 [UnmapNotify] = unmapnotify,
917 [VisibilityNotify] = visibilitynotify,
921 xerror_start(Display *d, XErrorEvent *ee)
928 xerror(Display *d, XErrorEvent *ee)
930 fprintf(stderr, "error: %p %p\n", display, ee);
939 xerrorxlib = XSetErrorHandler(xerror_start);
941 /* this causes an error if some other window manager is running */
942 XSelectInput(display, DefaultRootWindow(display),
943 SubstructureRedirectMask);
944 XSync(display, False);
948 XSetErrorHandler(xerror);
949 XSync(display, False);
954 main(int argc, char *argv[])
957 char conf[PATH_MAX], *cfile = NULL;
962 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
963 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
964 warnx("no locale support");
966 if(!(display = XOpenDisplay(0)))
967 errx(1, "can not open display");
970 errx(1, "other wm running");
972 screen = DefaultScreen(display);
973 root = RootWindow(display, screen);
974 width = DisplayWidth(display, screen) - 2;
975 height = DisplayHeight(display, screen) - 2;
977 /* look for local and global conf file */
978 pwd = getpwuid(getuid());
980 errx(1, "invalid user %d", getuid());
982 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
983 if (stat(conf, &sb) != -1) {
984 if (S_ISREG(sb.st_mode))
987 /* try global conf file */
988 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
989 if (!stat(conf, &sb))
990 if (S_ISREG(sb.st_mode))
996 /* make work space 1 active */
1000 TAILQ_INIT(&ws[0].winlist);
1001 for (i = 1; i < SWM_WS_MAX; i++) {
1005 TAILQ_INIT(&ws[i].winlist);
1008 /* setup status bar */
1011 XSelectInput(display, root, SubstructureRedirectMask |
1012 SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
1013 EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
1014 FocusChangeMask | PropertyChangeMask | ExposureMask);
1019 XNextEvent(display, &e);
1020 if (handler[e.type])
1021 handler[e.type](&e);
1024 XCloseDisplay(display);