]> code.delx.au - spectrwm/blobdiff - scrotwm.c
add rb stuff to linux
[spectrwm] / scrotwm.c
index b9bed4bb75364612e73587d1d185dd1215dd0f35..bf4a3bdc4d3abecaa2753b3e2af7accbc10eead4 100644 (file)
--- a/scrotwm.c
+++ b/scrotwm.c
 #include <sys/queue.h>
 #include <sys/param.h>
 #include <sys/select.h>
+#if defined(__linux__)
+#include "linux/tree.h"
+#elif defined(__OpenBSD__)
+#include <sys/tree.h>
+# endif
 
 #include <X11/cursorfont.h>
 #include <X11/keysym.h>
@@ -124,6 +129,7 @@ static const char   *buildstr = SCROTWM_VERSION;
 #define SWM_D_SPAWN            0x0800
 #define SWM_D_EVENTQ           0x1000
 #define SWM_D_CONF             0x2000
+#define SWM_D_BAR              0x4000
 
 u_int32_t              swm_debug = 0
                            | SWM_D_MISC
@@ -140,6 +146,7 @@ u_int32_t           swm_debug = 0
                            | SWM_D_SPAWN
                            | SWM_D_EVENTQ
                            | SWM_D_CONF
+                           | SWM_D_BAR
                            ;
 #else
 #define DPRINTF(x...)
@@ -159,9 +166,18 @@ u_int32_t          swm_debug = 0
 #define Y(r)                   (r)->g.y
 #define WIDTH(r)               (r)->g.w
 #define HEIGHT(r)              (r)->g.h
+#define SH_MIN(w)              (w)->sh_mask & PMinSize
+#define SH_MIN_W(w)            (w)->sh.min_width
+#define SH_MIN_H(w)            (w)->sh.min_height
+#define SH_MAX(w)              (w)->sh_mask & PMaxSize
+#define SH_MAX_W(w)            (w)->sh.max_width
+#define SH_MAX_H(w)            (w)->sh.max_height
+#define SH_INC(w)              (w)->sh_mask & PResizeInc
+#define SH_INC_W(w)            (w)->sh.width_inc
+#define SH_INC_H(w)            (w)->sh.height_inc
 #define SWM_MAX_FONT_STEPS     (3)
-#define WINID(w)               (w ? w->id : 0)
-#define YESNO(x)               (x ? "yes" : "no")
+#define WINID(w)               ((w) ? (w)->id : 0)
+#define YESNO(x)               ((x) ? "yes" : "no")
 
 #define SWM_FOCUS_DEFAULT      (0)
 #define SWM_FOCUS_SYNERGY      (1)
@@ -214,6 +230,7 @@ struct search_window {
        TAILQ_ENTRY(search_window)      entry;
        int                             idx;
        struct ws_win                   *win;
+       GC                              gc;
        Window                          indicator;
 };
 TAILQ_HEAD(search_winlist, search_window);
@@ -230,16 +247,27 @@ enum {
 };
 
 /* dialog windows */
-double                 dialog_ratio = .6;
+double                 dialog_ratio = 0.6;
 /* status bar */
 #define SWM_BAR_MAX            (256)
 #define SWM_BAR_JUSTIFY_LEFT   (0)
 #define SWM_BAR_JUSTIFY_CENTER (1)
 #define SWM_BAR_JUSTIFY_RIGHT  (2)
 #define SWM_BAR_OFFSET         (4)
+#define SWM_BAR_FONTS          "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
+                               "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*,"       \
+                               "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*,"    \
+                               "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"
+
+#ifdef X_HAVE_UTF8_STRING
+#define DRAWSTRING(x...)       Xutf8DrawString(x)
+#else
+#define DRAWSTRING(x...)       XmbDrawString(x)
+#endif
+
 char                   *bar_argv[] = { NULL, NULL };
 int                    bar_pipe[2];
-char                   bar_ext[SWM_BAR_MAX];
+unsigned char          bar_ext[SWM_BAR_MAX];
 char                   bar_vertext[SWM_BAR_MAX];
 int                    bar_version = 0;
 sig_atomic_t           bar_alarm = 0;
@@ -264,12 +292,10 @@ int                       disable_border = 0;
 int                    border_width = 1;
 int                    verbose_layout = 0;
 pid_t                  bar_pid;
-GC                     bar_gc;
-XGCValues              bar_gcv;
-int                    bar_fidx = 0;
-XFontStruct            *bar_fs;
-char                   *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
-char                   *spawn_term[] = { NULL, NULL };         /* XXX Make fully dynamic */
+XFontSet               bar_fs;
+XFontSetExtents                *bar_fs_extents;
+char                   *bar_fonts;
+char                   *spawn_term[] = { NULL, NULL }; /* XXX fully dynamic */
 struct passwd          *pwd;
 
 #define SWM_MENU_FN    (2)
@@ -325,6 +351,7 @@ struct ws_win {
        struct swm_screen       *s;     /* always valid, never changes */
        XWindowAttributes       wa;
        XSizeHints              sh;
+       long                    sh_mask;
        XClassHint              ch;
        XWMHints                *hints;
 };
@@ -401,9 +428,11 @@ struct workspace {
                                int horizontal_msize;
                                int horizontal_mwin;
                                int horizontal_stacks;
+                               int horizontal_flip;
                                int vertical_msize;
                                int vertical_mwin;
                                int vertical_stacks;
+                               int vertical_flip;
        } l_state;
 };
 
@@ -424,6 +453,8 @@ struct swm_screen {
                unsigned long   color;
                char            *name;
        } c[SWM_S_COLOR_MAX];
+
+       GC                      bar_gc;
 };
 struct swm_screen      *screens;
 int                    num_screens;
@@ -443,6 +474,7 @@ union arg {
 #define SWM_ARG_ID_MASTERGROW  (21)
 #define SWM_ARG_ID_MASTERADD   (22)
 #define SWM_ARG_ID_MASTERDEL   (23)
+#define SWM_ARG_ID_FLIPLAYOUT  (24)
 #define SWM_ARG_ID_STACKRESET  (30)
 #define SWM_ARG_ID_STACKINIT   (31)
 #define SWM_ARG_ID_CYCLEWS_UP  (40)
@@ -475,6 +507,7 @@ void        focus_magic(struct ws_win *);
 
 /* quirks */
 struct quirk {
+       TAILQ_ENTRY(quirk)      entry;
        char                    *class;
        char                    *name;
        unsigned long           quirk;
@@ -485,8 +518,8 @@ struct quirk {
 #define SWM_Q_FULLSCREEN       (1<<4)  /* remove border */
 #define SWM_Q_FOCUSPREV                (1<<5)  /* focus on caller */
 };
-int                            quirks_size = 0, quirks_length = 0;
-struct quirk                   *quirks = NULL;
+TAILQ_HEAD(quirk_list, quirk);
+struct quirk_list              quirks = TAILQ_HEAD_INITIALIZER(quirks);
 
 /*
  * Supported EWMH hints should be added to
@@ -535,22 +568,25 @@ struct ewmh_hint {
     {"_NET_WM_ACTION_CLOSE", None},
 };
 
-void   store_float_geom(struct ws_win *, struct swm_region *);
-int    floating_toggle_win(struct ws_win *);
-void   spawn_select(struct swm_region *, union arg *, char *, int *);
+void            store_float_geom(struct ws_win *, struct swm_region *);
+int             floating_toggle_win(struct ws_win *);
+void            spawn_select(struct swm_region *, union arg *, char *, int *);
+unsigned char  *get_win_name(Window);
 
 int
-get_property(Window id, Atom atom, long count, Atom type,
-    unsigned long *n, unsigned char **data)
+get_property(Window id, Atom atom, long count, Atom type, unsigned long *nitems,
+    unsigned long *nbytes, unsigned char **data)
 {
        int                     format, status;
-       unsigned long           tmp, extra;
-       unsigned long           *nitems;
+       unsigned long           *nbytes_ret, *nitems_ret;
+       unsigned long           nbytes_tmp, nitems_tmp;
        Atom                    real;
 
-       nitems = n != NULL ? n : &tmp;
+       nbytes_ret = nbytes != NULL ? nbytes : &nbytes_tmp;
+       nitems_ret = nitems != NULL ? nitems : &nitems_tmp;
+
        status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
-           &real, &format, nitems, &extra, data);
+           &real, &format, nitems_ret, nbytes_ret, data);
 
        if (status != Success)
                return False;
@@ -644,7 +680,7 @@ teardown_ewmh(void)
        for (i = 0; i < ScreenCount(display); i++) {
                /* Get the support check window and destroy it */
                success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
-                   &n, &data);
+                   &n, NULL, &data);
 
                if (success) {
                        id = data[0];
@@ -665,7 +701,7 @@ ewmh_autoquirk(struct ws_win *win)
        Atom                    type;
 
        success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
-           XA_ATOM, &n, (void *)&data);
+           XA_ATOM, &n, NULL, (void *)&data);
 
        if (!success) {
                XFree(data);
@@ -851,7 +887,7 @@ ewmh_get_win_state(struct ws_win *win)
                win->ewmh_flags |= SWM_F_MANUAL;
 
        success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
-           (~0L), XA_ATOM, &n, (void *)&states);
+           (~0L), XA_ATOM, &n, NULL, (void *)&states);
 
        if (!success)
                return;
@@ -979,7 +1015,7 @@ geteventname(XEvent *e)
 char *
 xrandr_geteventname(XEvent *e)
 {
-       char                    *name = NULL;
+       char                    *name = NULL;
 
        switch(e->type - xrandr_eventbase) {
        case RRScreenChangeNotify:
@@ -1000,34 +1036,34 @@ dumpwins(struct swm_region *r, union arg *args)
        XWindowAttributes       wa;
 
        if (r->ws == NULL) {
-               fprintf(stderr, "dumpwins: invalid workspace\n");
+               warnx("dumpwins: invalid workspace");
                return;
        }
 
-       fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
+       warnx("=== managed window list ws %02d ===", r->ws->idx);
 
        TAILQ_FOREACH(win, &r->ws->winlist, entry) {
                state = getstate(win->id);
                if (!XGetWindowAttributes(display, win->id, &wa))
-                       fprintf(stderr, "window: 0x%lx, failed "
-                           "XGetWindowAttributes\n", win->id);
-               fprintf(stderr, "window: 0x%lx, map_state: %d, state: %d, "
-                   "transient: 0x%lx\n",
-                   win->id, wa.map_state, state, win->transient);
+                       warnx("window: 0x%lx, failed XGetWindowAttributes",
+                           win->id);
+               warnx("window: 0x%lx, map_state: %d, state: %d, "
+                   "transient: 0x%lx", win->id, wa.map_state, state,
+                   win->transient);
        }
 
-       fprintf(stderr, "===== unmanaged window list =====\n");
+       warnx("===== unmanaged window list =====");
        TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
                state = getstate(win->id);
                if (!XGetWindowAttributes(display, win->id, &wa))
-                       fprintf(stderr, "window: 0x%lx, failed "
-                           "XGetWindowAttributes\n", win->id);
-               fprintf(stderr, "window: 0x%lx, map_state: %d, state: %d, "
-                   "transient: 0x%lx\n",
-                   win->id, wa.map_state, state, win->transient);
+                       warnx("window: 0x%lx, failed XGetWindowAttributes",
+                           win->id);
+               warnx("window: 0x%lx, map_state: %d, state: %d, "
+                   "transient: 0x%lx", win->id, wa.map_state, state,
+                   win->transient);
        }
 
-       fprintf(stderr, "=================================\n");
+       warnx("=================================");
 }
 #else
 void
@@ -1157,7 +1193,7 @@ name_to_color(char *colorname)
        if (status)
                result = screen_def.pixel;
        else
-               fprintf(stderr, "color '%s' not found.\n", colorname);
+               warnx("color '%s' not found", colorname);
 
        return (result);
 }
@@ -1187,10 +1223,12 @@ fancy_stacker(struct workspace *ws)
 {
        strlcpy(ws->stacker, "[   ]", sizeof ws->stacker);
        if (ws->cur_layout->l_stack == vertical_stack)
-               snprintf(ws->stacker, sizeof ws->stacker, "[%d|%d]",
+               snprintf(ws->stacker, sizeof ws->stacker,
+                   ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
                    ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
        if (ws->cur_layout->l_stack == horizontal_stack)
-               snprintf(ws->stacker, sizeof ws->stacker, "[%d-%d]",
+               snprintf(ws->stacker, sizeof ws->stacker,
+                   ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
                    ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
 }
 
@@ -1199,9 +1237,11 @@ plain_stacker(struct workspace *ws)
 {
        strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
        if (ws->cur_layout->l_stack == vertical_stack)
-               strlcpy(ws->stacker, "[|]", sizeof ws->stacker);
+               strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
+                   sizeof ws->stacker);
        if (ws->cur_layout->l_stack == horizontal_stack)
-               strlcpy(ws->stacker, "[-]", sizeof ws->stacker);
+               strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
+                   sizeof ws->stacker);
 }
 
 void
@@ -1224,9 +1264,8 @@ custom_region(char *val)
            y > DisplayHeight(display, sidx) ||
            w + x > DisplayWidth(display, sidx) ||
            h + y > DisplayHeight(display, sidx)) {
-               fprintf(stderr, "ignoring region %ux%u+%u+%u "
-                   "- not within screen boundaries "
-                   "(%ux%u)\n", w, h, x, y,
+               warnx("ignoring region %ux%u+%u+%u - not within screen "
+                   "boundaries (%ux%u)", w, h, x, y,
                    DisplayWidth(display, sidx), DisplayHeight(display, sidx));
                return;
        }
@@ -1249,31 +1288,33 @@ socket_setnonblock(int fd)
 void
 bar_print(struct swm_region *r, char *s)
 {
-       int                     textwidth, x = 0;
-       size_t          len;
+       int                     x = 0;
+       size_t                  len;
+       XRectangle              ibox, lbox;
 
        XClearWindow(display, r->bar_window);
-       XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
 
        len = strlen(s);
-       textwidth = XTextWidth(bar_fs, s, len);
+       XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
 
        switch (bar_justify) {
        case SWM_BAR_JUSTIFY_LEFT:
                x = SWM_BAR_OFFSET;
                break;
        case SWM_BAR_JUSTIFY_CENTER:
-               x = (WIDTH(r) - textwidth) / 2;
+               x = (WIDTH(r) - lbox.width) / 2;
                break;
        case SWM_BAR_JUSTIFY_RIGHT:
-               x = WIDTH(r) - textwidth - SWM_BAR_OFFSET;
+               x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
                break;
        }
 
        if (x < SWM_BAR_OFFSET)
                x = SWM_BAR_OFFSET;
 
-       XDrawString(display, r->bar_window, bar_gc, x, bar_fs->ascent, s, len);
+       DRAWSTRING(display, r->bar_window, bar_fs, r->s->bar_gc,
+           x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 - 
+           lbox.y, s, len);
 }
 
 void
@@ -1287,7 +1328,7 @@ bar_extra_stop(void)
                kill(bar_pid, SIGTERM);
                bar_pid = 0;
        }
-       strlcpy(bar_ext, "", sizeof bar_ext);
+       strlcpy((char *)bar_ext, "", sizeof bar_ext);
        bar_extra = 0;
 }
 
@@ -1316,21 +1357,27 @@ bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
                strlcat(s, "    ", sz);
        }
 out:
-       if (xch)
+       if (xch) {
+               XFree(xch->res_name);
+               XFree(xch->res_class);
                XFree(xch);
+       }
 }
 
 void
 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
 {
-       char                    *title;
+       unsigned char           *title;
 
        if (window_name_enabled && cur_focus != NULL) {
-               XFetchName(display, cur_focus->id, &title);
-               if (title) {
+               title = get_win_name(cur_focus->id);
+               if (title != NULL) {
+                       DNPRINTF(SWM_D_BAR, "bar_window_name: title: %s\n",
+                           title);
+
                        if (cur_focus->floating)
                                strlcat(s, "(f) ", sz);
-                       strlcat(s, title, sz);
+                       strlcat(s, (char *)title, sz);
                        strlcat(s, " ", sz);
                        XFree(title);
                }
@@ -1385,7 +1432,7 @@ bar_update(void)
        size_t                  len;
        char                    ws[SWM_BAR_MAX];
        char                    s[SWM_BAR_MAX];
-       char                    cn[SWM_BAR_MAX];
+       unsigned char           cn[SWM_BAR_MAX];
        char                    loc[SWM_BAR_MAX];
        char                    *b, *stack = "";
 
@@ -1396,34 +1443,36 @@ bar_update(void)
                while ((b = fgetln(stdin, &len)) != NULL)
                        if (b && b[len - 1] == '\n') {
                                b[len - 1] = '\0';
-                               strlcpy(bar_ext, b, sizeof bar_ext);
+                               strlcpy((char *)bar_ext, b, sizeof bar_ext);
                        }
                if (b == NULL && errno != EAGAIN) {
-                       fprintf(stderr, "bar_update: bar_extra failed: "
-                           "errno: %d %s\n", errno, strerror(errno));
+                       warn("bar_update: bar_extra failed");
                        bar_extra_stop();
                }
        } else
-               strlcpy(bar_ext, "", sizeof bar_ext);
+               strlcpy((char *)bar_ext, "", sizeof bar_ext);
 
        if (clock_enabled == 0)
                strlcpy(s, "", sizeof s);
        else {
                time(&tmt);
                localtime_r(&tmt, &tm);
-               strftime(s, sizeof s, clock_format, &tm);
+               len = strftime(s, sizeof s, clock_format, &tm);
+               s[len] = '\0';
                strlcat(s, "    ", sizeof s);
        }
 
        for (i = 0; i < ScreenCount(display); i++) {
                x = 1;
                TAILQ_FOREACH(r, &screens[i].rl, entry) {
-                       strlcpy(cn, "", sizeof cn);
+                       strlcpy((char *)cn, "", sizeof cn);
                        strlcpy(ws, "", sizeof ws);
                        if (r && r->ws) {
-                               bar_urgent(cn, sizeof cn);
-                               bar_class_name(cn, sizeof cn, r->ws->focus);
-                               bar_window_name(cn, sizeof cn, r->ws->focus);
+                               bar_urgent((char *)cn, sizeof cn);
+                               bar_class_name((char *)cn, sizeof cn,
+                                   r->ws->focus);
+                               bar_window_name((char *)cn, sizeof cn,
+                                   r->ws->focus);
                                if (r->ws->name)
                                        snprintf(ws, sizeof ws, "<%s>",
                                            r->ws->name);
@@ -1440,6 +1489,13 @@ bar_update(void)
        alarm(bar_delay);
 }
 
+void
+bar_check_opts(void)
+{
+       if (title_class_enabled || title_name_enabled || window_name_enabled)
+               bar_update();
+}
+
 void
 bar_signal(int sig)
 {
@@ -1452,7 +1508,7 @@ bar_toggle(struct swm_region *r, union arg *args)
        struct swm_region       *tmpr;
        int                     i, sc = ScreenCount(display);
 
-       DNPRINTF(SWM_D_MISC, "bar_toggle\n");
+       DNPRINTF(SWM_D_BAR, "bar_toggle\n");
 
        if (bar_enabled)
                for (i = 0; i < sc; i++)
@@ -1522,27 +1578,48 @@ bar_refresh(void)
 void
 bar_setup(struct swm_region *r)
 {
+       char                    *default_string;
+       char                    **missing_charsets;
+       int                     num_missing_charsets = 0;
        int                     i, x, y;
 
        if (bar_fs) {
-               XFreeFont(display, bar_fs);
+               XFreeFontSet(display, bar_fs);
                bar_fs = NULL;
        }
 
-       for (i = 0; bar_fonts[i] != NULL; i++) {
-               bar_fs = XLoadQueryFont(display, bar_fonts[i]);
-               if (bar_fs) {
-                       bar_fidx = i;
-                       break;
-               }
+
+       DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
+
+       bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
+           &num_missing_charsets, &default_string);
+
+       if (num_missing_charsets > 0) {
+               warnx("Unable to load charset(s):");
+
+               for (i = 0; i < num_missing_charsets; ++i)
+                       warnx("%s", missing_charsets[i]);
+
+               XFreeStringList(missing_charsets);
+
+               if (strcmp(default_string, ""))
+                       warnx("Glyphs from those sets will be replaced "
+                           "by '%s'.", default_string);
+               else
+                       warnx("Glyphs from those sets won't be drawn.");
        }
-       if (bar_fonts[i] == NULL)
-               errx(1, "couldn't load font");
+
        if (bar_fs == NULL)
-               errx(1, "couldn't create font structure");
+               errx(1, "Error creating font set structure.");
 
-       bar_height = bar_fs->ascent + bar_fs->descent + 1 +
+       bar_fs_extents = XExtentsOfFontSet(bar_fs);
+
+       bar_height = bar_fs_extents->max_logical_extent.height +
            2 * bar_border_width;
+
+       if (bar_height < 1)
+               bar_height = 1;
+
        x = X(r);
        y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
 
@@ -1551,12 +1628,10 @@ bar_setup(struct swm_region *r)
            bar_height - 2 * bar_border_width,
            bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
            r->s->c[SWM_S_COLOR_BAR].color);
-       bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
-       XSetFont(display, bar_gc, bar_fs->fid);
        XSelectInput(display, r->bar_window, VisibilityChangeMask);
        if (bar_enabled)
                XMapRaised(display, r->bar_window);
-       DNPRINTF(SWM_D_MISC, "bar_setup: bar_window: 0x%lx\n", r->bar_window);
+       DNPRINTF(SWM_D_BAR, "bar_setup: bar_window: 0x%lx\n", r->bar_window);
 
        if (signal(SIGALRM, bar_signal) == SIG_ERR)
                err(1, "could not install bar_signal");
@@ -1596,7 +1671,7 @@ getstate(Window w)
        unsigned char           *p = NULL;
        unsigned long           n;
 
-       if (!get_property(w, astate, 2L, astate, &n, &p))
+       if (!get_property(w, astate, 2L, astate, &n, NULL, &p))
                return (-1);
        if (n != 0)
                result = *((long *)p);
@@ -1663,6 +1738,43 @@ config_win(struct ws_win *win, XConfigureRequestEvent  *ev)
                ce.display = ev->display;
                ce.event = ev->window;
                ce.window = ev->window;
+
+               /* make response appear more WM_SIZE_HINTS-compliant */
+               if (win->sh_mask)
+                       DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%lx,"
+                           " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
+                           "%d x %d\n", win->id, win->sh_mask, SH_MIN_W(win),
+                           SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
+                           SH_INC_W(win), SH_INC_H(win));
+
+               /* min size */
+               if (SH_MIN(win)) {
+                       /* the hint may be set... to 0! */
+                       if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
+                               ce.width = SH_MIN_W(win);
+                       if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
+                               ce.height = SH_MIN_H(win);
+               }
+
+               /* max size */
+               if (SH_MAX(win)) {
+                       /* may also be advertized as 0 */
+                       if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
+                               ce.width = SH_MAX_W(win);
+                       if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
+                               ce.height = SH_MAX_H(win);
+               }
+
+               /* resize increment. */
+               if (SH_INC(win)) {
+                       if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
+                               ce.width -= (ce.width - SH_MIN_W(win)) %
+                                   SH_INC_W(win);
+                       if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
+                               ce.height -= (ce.height - SH_MIN_H(win)) %
+                                   SH_INC_H(win);
+               }
+
                /* adjust x and y for requested border_width. */
                ce.x += border_width - ev->border_width;
                ce.y += border_width - ev->border_width;
@@ -1779,8 +1891,7 @@ restart(struct swm_region *r, union arg *args)
        unmap_all();
        XCloseDisplay(display);
        execvp(start_argv[0], start_argv);
-       fprintf(stderr, "execvp failed\n");
-       perror(" failed");
+       warn("execvp failed");
        quit(NULL, NULL);
 }
 
@@ -1874,7 +1985,7 @@ spawn(int ws_idx, union arg *args, int close_fd)
        setenv("LD_PRELOAD", SWM_LIB, 1);
 
        if (asprintf(&ret, "%d", ws_idx) == -1) {
-               perror("_SWM_WS");
+               warn("spawn: asprintf SWM_WS");
                _exit(1);
        }
        setenv("_SWM_WS", ret, 1);
@@ -1882,7 +1993,7 @@ spawn(int ws_idx, union arg *args, int close_fd)
        ret = NULL;
 
        if (asprintf(&ret, "%d", getpid()) == -1) {
-               perror("_SWM_PID");
+               warn("spawn: asprintf _SWM_PID");
                _exit(1);
        }
        setenv("_SWM_PID", ret, 1);
@@ -1890,7 +2001,7 @@ spawn(int ws_idx, union arg *args, int close_fd)
        ret = NULL;
 
        if (setsid() == -1) {
-               perror("setsid");
+               warn("spawn: setsid");
                _exit(1);
        }
 
@@ -1901,7 +2012,7 @@ spawn(int ws_idx, union arg *args, int close_fd)
                 * leave stderr open to record errors
                */
                if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
-                       perror("open");
+                       warn("spawn: open");
                        _exit(1);
                }
                dup2(fd, STDIN_FILENO);
@@ -1912,7 +2023,7 @@ spawn(int ws_idx, union arg *args, int close_fd)
 
        execvp(args->argv[0], args->argv);
 
-       perror("execvp");
+       warn("spawn: execvp");
        _exit(1);
 }
 
@@ -2116,8 +2227,7 @@ focus_win(struct ws_win *win)
                    PropModeReplace, (unsigned char *)&win->id,1);
        }
 
-       if (window_name_enabled)
-               bar_update();
+       bar_check_opts();
 }
 
 void
@@ -2295,7 +2405,7 @@ sort_windows(struct ws_win_list *wl)
                if (win->transient) {
                        parent = find_window(win->transient);
                        if (parent == NULL) {
-                               fprintf(stderr, "not possible bug\n");
+                               warnx("not possible bug");
                                continue;
                        }
                        TAILQ_REMOVE(wl, win, entry);
@@ -2382,7 +2492,7 @@ swapwin(struct swm_region *r, union arg *args)
 void
 focus_prev(struct ws_win *win)
 {
-       struct ws_win           *winfocus = NULL, *winlostfocus = NULL;
+       struct ws_win           *winfocus = NULL;
        struct ws_win           *cur_focus = NULL;
        struct ws_win_list      *wl = NULL;
        struct workspace        *ws = NULL;
@@ -2395,7 +2505,6 @@ focus_prev(struct ws_win *win)
        ws = win->ws;
        wl = &ws->winlist;
        cur_focus = ws->focus;
-       winlostfocus = cur_focus;
 
        /* pickle, just focus on whatever */
        if (cur_focus == NULL) {
@@ -2433,23 +2542,15 @@ focus_prev(struct ws_win *win)
                winfocus = TAILQ_LAST(wl, ws_win_list);
        if (winfocus == NULL || winfocus == win)
                winfocus = TAILQ_NEXT(cur_focus, entry);
-done:
-       if (winfocus == winlostfocus || winfocus == NULL) {
-               /* update the bar so that title/class/name will be cleared. */
-               if (window_name_enabled || title_name_enabled ||
-                   title_class_enabled)
-                       bar_update();
-               
-               return;
-       }
 
+done:
        focus_magic(winfocus);
 }
 
 void
 focus(struct swm_region *r, union arg *args)
 {
-       struct ws_win           *winfocus = NULL, *winlostfocus = NULL, *head;
+       struct ws_win           *winfocus = NULL, *head;
        struct ws_win           *cur_focus = NULL;
        struct ws_win_list      *wl = NULL;
        struct workspace        *ws = NULL;
@@ -2491,8 +2592,6 @@ focus(struct swm_region *r, union arg *args)
        if (all_iconics)
                return;
 
-       winlostfocus = cur_focus;
-
        switch (args->id) {
        case SWM_ARG_ID_FOCUSPREV:
                head = TAILQ_PREV(cur_focus, ws_win_list, entry);
@@ -2546,14 +2645,6 @@ focus(struct swm_region *r, union arg *args)
        default:
                return;
        }
-       if (winfocus == winlostfocus || winfocus == NULL) {
-               /* update the bar so that title/class/name will be cleared. */
-               if (window_name_enabled || title_name_enabled ||
-                   title_class_enabled)
-                       bar_update();
-
-               return;
-       }
 
        focus_magic(winfocus);
 }
@@ -3000,6 +3091,9 @@ vertical_config(struct workspace *ws, int id)
                if (ws->l_state.vertical_stacks > 1)
                        ws->l_state.vertical_stacks--;
                break;
+       case SWM_ARG_ID_FLIPLAYOUT:
+               ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
+               break;
        default:
                return;
        }
@@ -3010,7 +3104,7 @@ vertical_stack(struct workspace *ws, struct swm_geometry *g)
 {
        DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
 
-       stack_master(ws, g, 0, 0);
+       stack_master(ws, g, 0, ws->l_state.vertical_flip);
 }
 
 void
@@ -3047,6 +3141,9 @@ horizontal_config(struct workspace *ws, int id)
                if (ws->l_state.horizontal_stacks > 1)
                        ws->l_state.horizontal_stacks--;
                break;
+       case SWM_ARG_ID_FLIPLAYOUT:
+               ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
+               break;
        default:
                return;
        }
@@ -3057,7 +3154,7 @@ horizontal_stack(struct workspace *ws, struct swm_geometry *g)
 {
        DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
 
-       stack_master(ws, g, 1, 0);
+       stack_master(ws, g, 1, ws->l_state.horizontal_flip);
 }
 
 /* fullscreen view */
@@ -3223,32 +3320,28 @@ iconify(struct swm_region *r, union arg *args)
 }
 
 unsigned char *
-get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
-    unsigned long *slen)
+get_win_name(Window win)
 {
-       int                     status, retfmt;
-       unsigned long           nitems, nbytes, nextra;
        unsigned char           *prop = NULL;
-       Atom                    rettype;
-
-       status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
-           &rettype, &retfmt,  &nitems, &nbytes, &prop);
-       if (status != Success)
-               return (NULL);
-       XFree(prop);
+       unsigned long           nbytes, nitems;
 
-       status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
-           stype, &rettype, &retfmt, &nitems, &nextra, &prop);
-       if (status != Success) {
+       /* try _NET_WM_NAME first */
+       if (get_property(win, a_netwmname, 0L, a_utf8_string, NULL, &nbytes,
+           &prop)) {
                XFree(prop);
-               return (NULL);
+               if (get_property(win, a_netwmname, nbytes, a_utf8_string,
+                   &nitems, NULL, &prop))
+                       return (prop);
        }
-       if (rettype != stype) {
-               XFree(prop);
+
+       /* fallback to WM_NAME */
+       if (!get_property(win, a_wmname, 0L, a_string, NULL, &nbytes, &prop))
                return (NULL);
-       }
-       *slen = nitems;
-       return (prop);
+       XFree(prop);
+       if (get_property(win, a_wmname, nbytes, a_string, &nitems, NULL, &prop))
+               return (prop);
+
+       return (NULL);
 }
 
 void
@@ -3258,7 +3351,6 @@ uniconify(struct swm_region *r, union arg *args)
        FILE                    *lfile;
        unsigned char           *name;
        int                     count = 0;
-       unsigned long           len;
 
        DNPRINTF(SWM_D_MISC, "uniconify\n");
 
@@ -3290,8 +3382,7 @@ uniconify(struct swm_region *r, union arg *args)
                if (win->iconic == 0)
                        continue;
 
-               name = get_win_name(display, win->id, a_netwmname,
-                   a_utf8_string, &len);
+               name = get_win_name(win->id);
                if (name == NULL)
                        continue;
                fprintf(lfile, "%s.%lu\n", name, win->id);
@@ -3361,6 +3452,7 @@ search_win_cleanup(void)
 
        while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
                XDestroyWindow(display, sw->indicator);
+               XFreeGC(display, sw->gc);
                TAILQ_REMOVE(&search_wl, sw, entry);
                free(sw);
        }
@@ -3372,13 +3464,12 @@ search_win(struct swm_region *r, union arg *args)
        struct ws_win           *win = NULL;
        struct search_window    *sw = NULL;
        Window                  w;
-       GC                      gc;
        XGCValues               gcv;
        int                     i;
        char                    s[8];
        FILE                    *lfile;
        size_t                  len;
-       int                     textwidth;
+       XRectangle              ibox, lbox;
 
        DNPRINTF(SWM_D_MISC, "search_win\n");
 
@@ -3399,8 +3490,7 @@ search_win(struct swm_region *r, union arg *args)
 
                sw = calloc(1, sizeof(struct search_window));
                if (sw == NULL) {
-                       fprintf(stderr, "search_win: calloc: %s",
-                           strerror(errno));
+                       warn("search_win: calloc");
                        fclose(lfile);
                        search_win_cleanup();
                        return;
@@ -3410,23 +3500,25 @@ search_win(struct swm_region *r, union arg *args)
 
                snprintf(s, sizeof s, "%d", i);
                len = strlen(s);
-               textwidth = XTextWidth(bar_fs, s, len);
+
+               XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
 
                w = XCreateSimpleWindow(display,
-                   win->id, 0, 0, textwidth + 12,
-                   bar_fs->ascent + bar_fs->descent + 4, 1,
+                   win->id, 0, 0,lbox.width + 4,
+                   bar_fs_extents->max_logical_extent.height, 1,
                    r->s->c[SWM_S_COLOR_UNFOCUS].color,
                    r->s->c[SWM_S_COLOR_FOCUS].color);
 
                sw->indicator = w;
                TAILQ_INSERT_TAIL(&search_wl, sw, entry);
 
-               gc = XCreateGC(display, w, 0, &gcv);
-               XSetFont(display, gc, bar_fs->fid);
+               sw->gc = XCreateGC(display, w, 0, &gcv);
                XMapRaised(display, w);
-               XSetForeground(display, gc, r->s->c[SWM_S_COLOR_BAR].color);
+               XSetForeground(display, sw->gc, r->s->c[SWM_S_COLOR_BAR].color);
 
-               XDrawString(display, w, gc, 6, bar_fs->ascent + 2, s, len);
+               DRAWSTRING(display, w, bar_fs, sw->gc, 2,
+                   (bar_fs_extents->max_logical_extent.height -
+                   lbox.height) / 2 - lbox.y, s, len);
 
                fprintf(lfile, "%d\n", i);
                i++;
@@ -3447,8 +3539,7 @@ search_resp_uniconify(char *resp, unsigned long len)
        TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
                if (win->iconic == 0)
                        continue;
-               name = get_win_name(display, win->id, a_netwmname,
-                   a_utf8_string, &len);
+               name = get_win_name(win->id);
                if (name == NULL)
                        continue;
                if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
@@ -3571,13 +3662,13 @@ search_do_resp(void)
        searchpid = 0;
 
        if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
-               fprintf(stderr, "search: calloc\n");
+               warn("search: calloc");
                goto done;
        }
 
        rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
        if (rbytes <= 0) {
-               fprintf(stderr, "search: read error: %s\n", strerror(errno));
+               warn("search: read error");
                goto done;
        }
        resp[rbytes] = '\0';
@@ -4062,6 +4153,7 @@ move_step(struct swm_region *r, union arg *args)
 /* user/key callable function IDs */
 enum keyfuncid {
        kf_cycle_layout,
+       kf_flip_layout,
        kf_stack_reset,
        kf_master_shrink,
        kf_master_grow,
@@ -4153,6 +4245,7 @@ struct keyfunc {
 } keyfuncs[kf_invalid + 1] = {
        /* name                 function        argument */
        { "cycle_layout",       cycle_layout,   {0} },
+       { "flip_layout",        stack_config,   {.id = SWM_ARG_ID_FLIPLAYOUT} },
        { "stack_reset",        stack_config,   {.id = SWM_ARG_ID_STACKRESET} },
        { "master_shrink",      stack_config,   {.id = SWM_ARG_ID_MASTERSHRINK} },
        { "master_grow",        stack_config,   {.id = SWM_ARG_ID_MASTERGROW} },
@@ -4226,13 +4319,32 @@ struct keyfunc {
        { "invalid key func",   NULL,           {0} },
 };
 struct key {
+       RB_ENTRY(key)           entry;
        unsigned int            mod;
        KeySym                  keysym;
        enum keyfuncid          funcid;
        char                    *spawn_name;
 };
-int                            keys_size = 0, keys_length = 0;
-struct key                     *keys = NULL;
+RB_HEAD(key_list, key);
+
+int
+key_cmp(struct key *kp1, struct key *kp2)
+{
+       if (kp1->keysym < kp2->keysym)
+               return (-1);
+       if (kp1->keysym > kp2->keysym)
+               return (1);
+
+       if (kp1->mod < kp2->mod)
+               return (-1);
+       if (kp1->mod > kp2->mod)
+               return (1);
+
+       return (0);
+}
+
+RB_GENERATE_STATIC(key_list, key, entry, key_cmp);
+struct key_list                        keys;
 
 /* mouse */
 enum { client_click, root_click };
@@ -4253,13 +4365,14 @@ void
 update_modkey(unsigned int mod)
 {
        int                     i;
+       struct key              *kp;
 
        mod_key = mod;
-       for (i = 0; i < keys_length; i++)
-               if (keys[i].mod & ShiftMask)
-                       keys[i].mod = mod | ShiftMask;
+       RB_FOREACH(kp, key_list, &keys)
+               if (kp->mod & ShiftMask)
+                       kp->mod = mod | ShiftMask;
                else
-                       keys[i].mod = mod;
+                       kp->mod = mod;
 
        for (i = 0; i < LENGTH(buttons); i++)
                if (buttons[i].mask & ShiftMask)
@@ -4270,13 +4383,13 @@ update_modkey(unsigned int mod)
 
 /* spawn */
 struct spawn_prog {
+       TAILQ_ENTRY(spawn_prog) entry;
        char                    *name;
        int                     argc;
        char                    **argv;
 };
-
-int                            spawns_size = 0, spawns_length = 0;
-struct spawn_prog              *spawns = NULL;
+TAILQ_HEAD(spawn_list, spawn_prog);
+struct spawn_list              spawns = TAILQ_HEAD_INITIALIZER(spawns);
 
 int
 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
@@ -4289,13 +4402,12 @@ spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
        DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
 
        /* find program */
-       for (i = 0; i < spawns_length; i++) {
-               if (!strcasecmp(spawn_name, spawns[i].name))
-                       prog = &spawns[i];
+       TAILQ_FOREACH(prog, &spawns, entry) {
+               if (!strcasecmp(spawn_name, prog->name))
+                       break;
        }
        if (prog == NULL) {
-               fprintf(stderr, "spawn_custom: program %s not found\n",
-                   spawn_name);
+               warnx("spawn_custom: program %s not found", spawn_name);
                return (-1);
        }
 
@@ -4318,7 +4430,7 @@ spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
                            == NULL)
                                err(1, "spawn_custom bar color");
                } else if (!strcasecmp(ap, "$bar_font")) {
-                       if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
+                       if ((real_args[i] = strdup(bar_fonts))
                            == NULL)
                                err(1, "spawn_custom bar fonts");
                } else if (!strcasecmp(ap, "$bar_font_color")) {
@@ -4416,123 +4528,102 @@ spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
 }
 
 void
-setspawn(struct spawn_prog *prog)
+spawn_insert(char *name, char *args)
 {
-       int                     i, j;
+       char                    *arg, *cp, *ptr;
+       struct spawn_prog       *sp;
 
-       if (prog == NULL || prog->name == NULL)
-               return;
+       DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
 
-       /* find existing */
-       for (i = 0; i < spawns_length; i++) {
-               if (!strcmp(spawns[i].name, prog->name)) {
-                       /* found */
-                       if (prog->argv == NULL) {
-                               /* delete */
-                               DNPRINTF(SWM_D_SPAWN,
-                                   "setspawn: delete #%d: %s\n",
-                                   i, spawns[i].name);
-                               free(spawns[i].name);
-                               for (j = 0; j < spawns[i].argc; j++)
-                                       free(spawns[i].argv[j]);
-                               free(spawns[i].argv);
-                               j = spawns_length - 1;
-                               if (i < j)
-                                       spawns[i] = spawns[j];
-                               spawns_length--;
-                               free(prog->name);
-                       } else {
-                               /* replace */
-                               DNPRINTF(SWM_D_SPAWN,
-                                   "setspawn: replace #%d: %s\n",
-                                   i, spawns[i].name);
-                               free(spawns[i].name);
-                               for (j = 0; j < spawns[i].argc; j++)
-                                       free(spawns[i].argv[j]);
-                               free(spawns[i].argv);
-                               spawns[i] = *prog;
-                       }
-                       /* found case handled */
-                       free(prog);
-                       return;
-               }
+       if ((sp = calloc(1, sizeof *sp)) == NULL)
+               err(1, "spawn_insert: malloc");
+       if ((sp->name = strdup(name)) == NULL)
+               err(1, "spawn_insert: strdup");
+
+       /* convert the arguments to an argument list */
+       if ((ptr = cp = strdup(args)) == NULL)
+               err(1, "spawn_insert: strdup");
+       while ((arg = strsep(&ptr, " \t")) != NULL) {
+               /* empty field; skip it */
+               if (*arg == '\0')
+                       continue;
+
+               sp->argc++;
+               if ((sp->argv = realloc(sp->argv, sp->argc *
+                   sizeof *sp->argv)) == NULL)
+                       err(1, "spawn_insert: realloc");
+               if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
+                       err(1, "spawn_insert: strdup");
        }
+       free(cp);
 
-       if (prog->argv == NULL) {
-               fprintf(stderr,
-                   "error: setspawn: cannot find program: %s", prog->name);
-               free(prog);
+       TAILQ_INSERT_TAIL(&spawns, sp, entry);
+       DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
+}
+
+void
+spawn_remove(struct spawn_prog *sp)
+{
+       int                     i;
+
+       DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
+
+       TAILQ_REMOVE(&spawns, sp, entry);
+       for (i = 0; i < sp->argc; i++)
+               free(sp->argv[i]);
+       free(sp->argv);
+       free(sp->name);
+       free(sp);
+
+       DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
+}
+
+void
+spawn_replace(struct spawn_prog *sp, char *name, char *args)
+{
+       DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
+
+       spawn_remove(sp);
+       spawn_insert(name, args);
+
+       DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
+}
+
+void
+setspawn(char *name, char *args)
+{
+       struct spawn_prog       *sp;
+
+       DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
+
+       if (name == NULL)
                return;
-       }
 
-       /* not found: add */
-       if (spawns_size == 0 || spawns == NULL) {
-               spawns_size = 4;
-               DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
-               spawns = malloc((size_t)spawns_size *
-                   sizeof(struct spawn_prog));
-               if (spawns == NULL) {
-                       fprintf(stderr, "setspawn: malloc failed\n");
-                       perror(" failed");
-                       quit(NULL, NULL);
-               }
-       } else if (spawns_length == spawns_size) {
-               spawns_size *= 2;
-               DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
-               spawns = realloc(spawns, (size_t)spawns_size *
-                   sizeof(struct spawn_prog));
-               if (spawns == NULL) {
-                       fprintf(stderr, "setspawn: realloc failed\n");
-                       perror(" failed");
-                       quit(NULL, NULL);
+       TAILQ_FOREACH(sp, &spawns, entry) {
+               if (!strcmp(sp->name, name)) {
+                       if (*args == '\0')
+                               spawn_remove(sp);
+                       else
+                               spawn_replace(sp, name, args);
+                       DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
+                       return;
                }
        }
-
-       if (spawns_length < spawns_size) {
-               DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
-                   spawns_length, prog->name);
-               i = spawns_length++;
-               spawns[i] = *prog;
-       } else {
-               fprintf(stderr, "spawns array problem?\n");
-               if (spawns == NULL) {
-                       fprintf(stderr, "spawns array is NULL!\n");
-                       quit(NULL, NULL);
-               }
+       if (*args == '\0') {
+               warnx("error: setspawn: cannot find program: %s", name);
+               return;
        }
-       free(prog);
+
+       spawn_insert(name, args);
+       DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
 }
 
 int
 setconfspawn(char *selector, char *value, int flags)
 {
-       struct spawn_prog       *prog;
-       char                    *vp, *cp, *word;
-
        DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
-       if ((prog = calloc(1, sizeof *prog)) == NULL)
-               err(1, "setconfspawn: calloc prog");
-       prog->name = strdup(selector);
-       if (prog->name == NULL)
-               err(1, "setconfspawn prog->name");
-       if ((cp = vp = strdup(value)) == NULL)
-               err(1, "setconfspawn: strdup(value) ");
-       while ((word = strsep(&cp, " \t")) != NULL) {
-               DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
-               if (cp)
-                       cp += (long)strspn(cp, " \t");
-               if (strlen(word) > 0) {
-                       prog->argc++;
-                       if ((prog->argv = realloc(prog->argv,
-                           prog->argc * sizeof(char *))) == NULL)
-                               err(1, "setconfspawn: realloc");
-                       if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
-                               err(1, "setconfspawn: strdup");
-               }
-       }
-       free(vp);
 
-       setspawn(prog);
+       setspawn(selector, value);
 
        DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
        return (0);
@@ -4630,85 +4721,86 @@ strdupsafe(char *str)
                return (strdup(str));
 }
 
+void
+key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
+{
+       struct key              *kp;
+
+       DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
+           keyfuncs[kfid].name, spawn_name);
+
+       if ((kp = malloc(sizeof *kp)) == NULL)
+               err(1, "key_insert: malloc");
+
+       kp->mod = mod;
+       kp->keysym = ks;
+       kp->funcid = kfid;
+       kp->spawn_name = strdupsafe(spawn_name);
+       RB_INSERT(key_list, &keys, kp);
+
+       DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
+}
+
+struct key *
+key_lookup(unsigned int mod, KeySym ks)
+{
+       struct key              kp;
+
+       kp.keysym = ks;
+       kp.mod = mod;
+
+       return (RB_FIND(key_list, &keys, &kp));
+}
+
+void
+key_remove(struct key *kp)
+{
+       DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
+
+       RB_REMOVE(key_list, &keys, kp);
+       free(kp->spawn_name);
+       free(kp);
+
+       DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
+}
+
+void
+key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
+    char *spawn_name)
+{
+       DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
+           spawn_name);
+
+       key_remove(kp);
+       key_insert(mod, ks, kfid, spawn_name);
+
+       DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
+}
+
 void
 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
     char *spawn_name)
 {
-       int                     i, j;
+       struct key              *kp;
+
        DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
            keyfuncs[kfid].name, spawn_name);
-       /* find existing */
-       for (i = 0; i < keys_length; i++) {
-               if (keys[i].mod == mod && keys[i].keysym == ks) {
-                       if (kfid == kf_invalid) {
-                               /* found: delete */
-                               DNPRINTF(SWM_D_KEY,
-                                   "setkeybinding: delete #%d %s\n",
-                                   i, keyfuncs[keys[i].funcid].name);
-                               free(keys[i].spawn_name);
-                               j = keys_length - 1;
-                               if (i < j)
-                                       keys[i] = keys[j];
-                               keys_length--;
-                               DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
-                               return;
-                       } else {
-                               /* found: replace */
-                               DNPRINTF(SWM_D_KEY,
-                                   "setkeybinding: replace #%d %s [%s]\n",
-                                   i, keyfuncs[keys[i].funcid].name,
-                                   spawn_name);
-                               free(keys[i].spawn_name);
-                               keys[i].mod = mod;
-                               keys[i].keysym = ks;
-                               keys[i].funcid = kfid;
-                               keys[i].spawn_name = strdupsafe(spawn_name);
-                               DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
-                               return;
-                       }
-               }
+
+       if ((kp = key_lookup(mod, ks)) != NULL) {
+               if (kfid == kf_invalid)
+                       key_remove(kp);
+               else
+                       key_replace(kp, mod, ks, kfid, spawn_name);
+               DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
+               return;
        }
        if (kfid == kf_invalid) {
-               fprintf(stderr,
-                   "error: setkeybinding: cannot find mod/key combination");
+               warnx("error: setkeybinding: cannot find mod/key combination");
                DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
                return;
        }
-       /* not found: add */
-       if (keys_size == 0 || keys == NULL) {
-               keys_size = 4;
-               DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
-               keys = malloc((size_t)keys_size * sizeof(struct key));
-               if (keys == NULL) {
-                       fprintf(stderr, "malloc failed\n");
-                       perror(" failed");
-                       quit(NULL, NULL);
-               }
-       } else if (keys_length == keys_size) {
-               keys_size *= 2;
-               DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
-               keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
-               if (keys == NULL) {
-                       fprintf(stderr, "realloc failed\n");
-                       perror(" failed");
-                       quit(NULL, NULL);
-               }
-       }
-       if (keys_length < keys_size) {
-               j = keys_length++;
-               DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s [%s]\n",
-                   j, keyfuncs[kfid].name, spawn_name);
-               keys[j].mod = mod;
-               keys[j].keysym = ks;
-               keys[j].funcid = kfid;
-               keys[j].spawn_name = strdupsafe(spawn_name);
-       } else {
-               fprintf(stderr, "keys array problem?\n");
-               if (keys == NULL) {
-                       fprintf(stderr, "keys array problem\n");
-                       quit(NULL, NULL);
-               }
-       }
+
+       key_insert(mod, ks, kfid, spawn_name);
        DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
 }
 
@@ -4718,7 +4810,7 @@ setconfbinding(char *selector, char *value, int flags)
        enum keyfuncid          kfid;
        unsigned int            mod;
        KeySym                  ks;
-       int                     i;
+       struct spawn_prog       *sp;
        DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
        if (selector == NULL) {
                DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
@@ -4743,13 +4835,13 @@ setconfbinding(char *selector, char *value, int flags)
                }
        }
        /* search by custom spawn name */
-       for (i = 0; i < spawns_length; i++) {
-               if (strcasecmp(selector, spawns[i].name) == 0) {
+       TAILQ_FOREACH(sp, &spawns, entry) {
+               if (strcasecmp(selector, sp->name) == 0) {
                        DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
                            selector);
                        if (parsekeys(value, mod_key, &mod, &ks) == 0) {
                                setkeybinding(mod, ks, kf_spawn_custom,
-                                   spawns[i].name);
+                                   sp->name);
                                return (0);
                        } else
                                return (1);
@@ -4763,6 +4855,7 @@ void
 setup_keys(void)
 {
        setkeybinding(MODKEY,           XK_space,       kf_cycle_layout,NULL);
+       setkeybinding(MODKEY|ShiftMask, XK_backslash,   kf_flip_layout, NULL);
        setkeybinding(MODKEY|ShiftMask, XK_space,       kf_stack_reset, NULL);
        setkeybinding(MODKEY,           XK_h,           kf_master_shrink,NULL);
        setkeybinding(MODKEY,           XK_l,           kf_master_grow, NULL);
@@ -4841,12 +4934,12 @@ setup_keys(void)
 void
 clear_keys(void)
 {
-       int                     i;
+       struct key              *kp;
 
-       /* clear all key bindings, if any */
-       for (i = 0; i < keys_length; i++)
-               free(keys[i].spawn_name);
-       keys_length = 0;
+       while (RB_EMPTY(&keys) == 0) {
+               kp = RB_ROOT(&keys);
+               key_remove(kp);
+       }
 }
 
 int
@@ -4890,10 +4983,11 @@ updatenumlockmask(void)
 void
 grabkeys(void)
 {
-       unsigned int            i, j, k;
+       unsigned int            j, k;
        KeyCode                 code;
        unsigned int            modifiers[] =
            { 0, LockMask, numlockmask, numlockmask | LockMask };
+       struct key              *kp;
 
        DNPRINTF(SWM_D_MISC, "grabkeys\n");
        updatenumlockmask();
@@ -4902,11 +4996,11 @@ grabkeys(void)
                if (TAILQ_EMPTY(&screens[k].rl))
                        continue;
                XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
-               for (i = 0; i < keys_length; i++) {
-                       if ((code = XKeysymToKeycode(display, keys[i].keysym)))
+               RB_FOREACH(kp, key_list, &keys) {
+                       if ((code = XKeysymToKeycode(display, kp->keysym)))
                                for (j = 0; j < LENGTH(modifiers); j++)
                                        XGrabKey(display, code,
-                                           keys[i].mod | modifiers[j],
+                                           kp->mod | modifiers[j],
                                            screens[k].root, True,
                                            GrabModeAsync, GrabModeAsync);
                }
@@ -4984,79 +5078,78 @@ parsequirks(char *qstr, unsigned long *quirk)
 }
 
 void
-setquirk(const char *class, const char *name, const int quirk)
+quirk_insert(const char *class, const char *name, unsigned long quirk)
 {
-       int                     i, j;
+       struct quirk            *qp;
 
-       /* find existing */
-       for (i = 0; i < quirks_length; i++) {
-               if (!strcmp(quirks[i].class, class) &&
-                   !strcmp(quirks[i].name, name)) {
-                       if (!quirk) {
-                               /* found: delete */
-                               DNPRINTF(SWM_D_QUIRK,
-                                   "setquirk: delete #%d %s:%s\n",
-                                   i, quirks[i].class, quirks[i].name);
-                               free(quirks[i].class);
-                               free(quirks[i].name);
-                               j = quirks_length - 1;
-                               if (i < j)
-                                       quirks[i] = quirks[j];
-                               quirks_length--;
-                               return;
-                       } else {
-                               /* found: replace */
-                               DNPRINTF(SWM_D_QUIRK,
-                                   "setquirk: replace #%d %s:%s\n",
-                                   i, quirks[i].class, quirks[i].name);
-                               free(quirks[i].class);
-                               free(quirks[i].name);
-                               quirks[i].class = strdup(class);
-                               quirks[i].name = strdup(name);
-                               quirks[i].quirk = quirk;
-                               return;
-                       }
+       DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
+           quirk);
+
+       if ((qp = malloc(sizeof *qp)) == NULL)
+               err(1, "quirk_insert: malloc");
+       if ((qp->class = strdup(class)) == NULL)
+               err(1, "quirk_insert: strdup");
+       if ((qp->name = strdup(name)) == NULL)
+               err(1, "quirk_insert: strdup");
+
+       qp->quirk = quirk;
+       TAILQ_INSERT_TAIL(&quirks, qp, entry);
+
+       DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
+}
+
+void
+quirk_remove(struct quirk *qp)
+{
+       DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
+           qp->name, qp->quirk);
+
+       TAILQ_REMOVE(&quirks, qp, entry);
+       free(qp->class);
+       free(qp->name);
+       free(qp);
+
+       DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
+}
+
+void
+quirk_replace(struct quirk *qp, const char *class, const char *name,
+    unsigned long quirk)
+{
+       DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
+           qp->name, qp->quirk);
+
+       quirk_remove(qp);
+       quirk_insert(class, name, quirk);
+
+       DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
+}
+
+void
+setquirk(const char *class, const char *name, unsigned long quirk)
+{
+       struct quirk            *qp;
+
+       DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
+          quirk);
+
+       TAILQ_FOREACH(qp, &quirks, entry) {
+               if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
+                       if (!quirk)
+                               quirk_remove(qp);
+                       else
+                               quirk_replace(qp, class, name, quirk);
+                       DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
+                       return;
                }
        }
        if (!quirk) {
-               fprintf(stderr,
-                   "error: setquirk: cannot find class/name combination");
+               warnx("error: setquirk: cannot find class/name combination");
                return;
        }
-       /* not found: add */
-       if (quirks_size == 0 || quirks == NULL) {
-               quirks_size = 4;
-               DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
-               quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
-               if (quirks == NULL) {
-                       fprintf(stderr, "setquirk: malloc failed\n");
-                       perror(" failed");
-                       quit(NULL, NULL);
-               }
-       } else if (quirks_length == quirks_size) {
-               quirks_size *= 2;
-               DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
-               quirks = realloc(quirks,
-                   (size_t)quirks_size * sizeof(struct quirk));
-               if (quirks == NULL) {
-                       fprintf(stderr, "setquirk: realloc failed\n");
-                       perror(" failed");
-                       quit(NULL, NULL);
-               }
-       }
-       if (quirks_length < quirks_size) {
-               DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
-               j = quirks_length++;
-               quirks[j].class = strdup(class);
-               quirks[j].name = strdup(name);
-               quirks[j].quirk = quirk;
-       } else {
-               fprintf(stderr, "quirks array problem?\n");
-               if (quirks == NULL) {
-                       fprintf(stderr, "quirks array problem!\n");
-                       quit(NULL, NULL);
-               }
-       }
+
+       quirk_insert(class, name, quirk);
+       DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
 }
 
 int
@@ -5113,6 +5206,7 @@ int
 setconfvalue(char *selector, char *value, int flags)
 {
        int     i;
+       char    *b;
 
        switch (flags) {
        case SWM_S_BAR_DELAY:
@@ -5191,9 +5285,12 @@ setconfvalue(char *selector, char *value, int flags)
                border_width = atoi(value);
                break;
        case SWM_S_BAR_FONT:
-               free(bar_fonts[0]);
-               if ((bar_fonts[0] = strdup(value)) == NULL)
-                       err(1, "setconfvalue: bar_font");
+               b = bar_fonts;
+               if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
+                       err(1, "setconfvalue: asprintf: failed to allocate "
+                               "memory for bar_fonts.");
+
+               free(b);
                break;
        case SWM_S_BAR_ACTION:
                free(bar_argv[0]);
@@ -5448,7 +5545,7 @@ conf_load(char *filename, int keymapping)
        DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
 
        if (filename == NULL) {
-               fprintf(stderr, "conf_load: no filename\n");
+               warnx("conf_load: no filename");
                return (1);
        }
        if ((config = fopen(filename, "r")) == NULL) {
@@ -5527,12 +5624,9 @@ conf_load(char *filename, int keymapping)
                optval = strdup(cp);
                /* call function to deal with it all */
                if (configopt[optind].func(optsub, optval,
-                   configopt[optind].funcflags) != 0) {
-                       fprintf(stderr, "%s line %zd: %s\n",
-                           filename, lineno, line);
+                   configopt[optind].funcflags) != 0)
                        errx(1, "%s: line %zd: invalid data for %s",
                            filename, lineno, configopt[optind].optname);
-               }
                free(optval);
                free(optsub);
                free(line);
@@ -5546,6 +5640,7 @@ conf_load(char *filename, int keymapping)
 out:
        free(line);
        fclose(config);
+       DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
 
        return (1);
 }
@@ -5566,7 +5661,7 @@ set_child_transient(struct ws_win *win, Window *trans)
                    " for 0x%lx trans 0x%lx\n", win->id, win->transient);
 
                if (win->hints == NULL) {
-                       fprintf(stderr, "no hints for 0x%lx\n", win->id);
+                       warnx("no hints for 0x%lx", win->id);
                        return;
                }
 
@@ -5578,8 +5673,7 @@ set_child_transient(struct ws_win *win, Window *trans)
                                XFree(wmh);
 
                        if ((wmh = XGetWMHints(display, w->id)) == NULL) {
-                               fprintf(stderr, "can't get hints for 0x%lx\n",
-                                   w->id);
+                               warnx("can't get hints for 0x%lx", w->id);
                                continue;
                        }
 
@@ -5661,6 +5755,7 @@ manage_window(Window id)
        const char              *errstr;
        XWindowChanges          wc;
        struct pid_e            *p;
+       struct quirk            *qp;
 
        if ((win = find_window(id)) != NULL)
                return (win);   /* already being managed */
@@ -5696,7 +5791,7 @@ manage_window(Window id)
                    False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
        }
        XGetWindowAttributes(display, id, &win->wa);
-       XGetWMNormalHints(display, id, &win->sh, &mask);
+       XGetWMNormalHints(display, id, &win->sh, &win->sh_mask);
        win->hints = XGetWMHints(display, id);
        XGetTransientForHint(display, id, &trans);
        if (trans) {
@@ -5749,9 +5844,8 @@ manage_window(Window id)
                                        if (ww->ws->r)
                                                r = ww->ws->r;
                                        else
-                                               fprintf(stderr,
-                                                   "manage_window: fix this "
-                                                   "bug mcbride\n");
+                                               warnx("manage_window: fix this "
+                                                   "bug mcbride");
                                        border_me = 1;
                                }
        }
@@ -5797,17 +5891,17 @@ manage_window(Window id)
                        border_me = 1;
                }
 
-               for (i = 0; i < quirks_length; i++){
-                       if (!strcmp(win->ch.res_class, quirks[i].class) &&
-                           !strcmp(win->ch.res_name, quirks[i].name)) {
+               TAILQ_FOREACH(qp, &quirks, entry) {
+                       if (!strcmp(win->ch.res_class, qp->class) &&
+                           !strcmp(win->ch.res_name, qp->name)) {
                                DNPRINTF(SWM_D_CLASS, "manage_window: found: "
                                    "class: %s, name: %s\n", win->ch.res_class,
                                    win->ch.res_name);
-                               if (quirks[i].quirk & SWM_Q_FLOAT) {
+                               if (qp->quirk & SWM_Q_FLOAT) {
                                        win->floating = 1;
                                        border_me = 1;
                                }
-                               win->quirks = quirks[i].quirk;
+                               win->quirks = qp->quirk;
                        }
                }
        }
@@ -5905,6 +5999,11 @@ unmanage_window(struct ws_win *win)
 
        focus_prev(win);
 
+       if (win->hints) {
+               XFree(win->hints);
+               win->hints = NULL;
+       }
+
        TAILQ_REMOVE(&win->ws->winlist, win, entry);
        TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
 
@@ -5916,8 +6015,11 @@ focus_magic(struct ws_win *win)
 {
        DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%lx\n", WINID(win));
 
-       if (win == NULL)
+       if (win == NULL) {
+               /* if there are no windows clear the status-bar */
+               bar_check_opts();
                return;
+       }
 
        if (win->child_trans) {
                /* win = parent & has a transient so focus on that */
@@ -5926,7 +6028,7 @@ focus_magic(struct ws_win *win)
                        if (win->child_trans->take_focus)
                                client_msg(win, takefocus);
                } else {
-                       /* make sure transient hasn't dissapeared */
+                       /* make sure transient hasn't disappeared */
                        if (validate_win(win->child_trans) == 0) {
                                focus_win(win->child_trans);
                                if (win->child_trans->take_focus)
@@ -5955,27 +6057,22 @@ expose(XEvent *e)
 void
 keypress(XEvent *e)
 {
-       unsigned int            i;
        KeySym                  keysym;
        XKeyEvent               *ev = &e->xkey;
+       struct key              *kp;
+       struct swm_region       *r;
 
        keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
-       for (i = 0; i < keys_length; i++)
-               if (keysym == keys[i].keysym
-                   && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
-                   && keyfuncs[keys[i].funcid].func) {
-                       if (keys[i].funcid == kf_spawn_custom)
-                               spawn_custom(
-                                   root_to_region(ev->root),
-                                   &(keyfuncs[keys[i].funcid].args),
-                                   keys[i].spawn_name
-                                   );
-                       else
-                               keyfuncs[keys[i].funcid].func(
-                                   root_to_region(ev->root),
-                                   &(keyfuncs[keys[i].funcid].args)
-                                   );
-               }
+       if ((kp = key_lookup(CLEANMASK(ev->state), keysym)) == NULL)
+               return;
+       if (keyfuncs[kp->funcid].func == NULL)
+               return;
+
+       r = root_to_region(ev->root);
+       if (kp->funcid == kf_spawn_custom)
+               spawn_custom(r, &(keyfuncs[kp->funcid].args), kp->spawn_name);
+       else
+               keyfuncs[kp->funcid].func(r, &(keyfuncs[kp->funcid].args));
 }
 
 void
@@ -6032,14 +6129,13 @@ void
 configurenotify(XEvent *e)
 {
        struct ws_win           *win;
-       long                    mask;
 
        DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%lx\n",
            e->xconfigure.window);
 
        win = find_window(e->xconfigure.window);
        if (win) {
-               XGetWMNormalHints(display, win->id, &win->sh, &mask);
+               XGetWMNormalHints(display, win->id, &win->sh, &win->sh_mask);
                adjust_font(win);
                if (font_adjusted)
                        stack();
@@ -6230,7 +6326,7 @@ focusevent(XEvent *e)
                            SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
                break;
        default:
-               fprintf(stderr, "ignoring focusevent\n");
+               warnx("ignoring focusevent");
                DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
                break;
        }
@@ -6293,9 +6389,13 @@ propertynotify(XEvent *e)
 {
        struct ws_win           *win;
        XPropertyEvent          *ev = &e->xproperty;
-
+#ifdef SWM_DEBUG
+       char                    *name;
+       name = XGetAtomName(display, ev->atom);
        DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%lx, atom: %s\n",
-           ev->window, XGetAtomName(display, ev->atom));
+           ev->window, name);
+       XFree(name);
+#endif
 
        win = find_window(ev->window);
        if (win == NULL)
@@ -6314,11 +6414,11 @@ propertynotify(XEvent *e)
        case XA_WM_NORMAL_HINTS:
                long            mask;
                XGetWMNormalHints(display, win->id, &win->sh, &mask);
-               fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
+               warnx("normal hints: flag 0x%x", win->sh.flags);
                if (win->sh.flags & PMinSize) {
                        WIDTH(win) = win->sh.min_width;
                        HEIGHT(win) = win->sh.min_height;
-                       fprintf(stderr, "min %d %d\n", WIDTH(win), HEIGHT(win));
+                       warnx("min %d %d", WIDTH(win), HEIGHT(win));
                }
                XMoveResizeWindow(display, win->id,
                    X(win), Y(win), WIDTH(win), HEIGHT(win));
@@ -6455,7 +6555,7 @@ xerror_start(Display *d, XErrorEvent *ee)
 int
 xerror(Display *d, XErrorEvent *ee)
 {
-       /* fprintf(stderr, "error: %p %p\n", display, ee); */
+       /* warnx("error: %p %p", display, ee); */
        return (-1);
 }
 
@@ -6692,6 +6792,7 @@ setup_screens(void)
        int                     i, j, k;
        int                     errorbase, major, minor;
        struct workspace        *ws;
+       XGCValues               gcv;
 
        if ((screens = calloc(ScreenCount(display),
             sizeof(struct swm_screen))) == NULL)
@@ -6720,6 +6821,13 @@ setup_screens(void)
                setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
                setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
 
+               /* create graphics context on screen with default font color */
+               screens[i].bar_gc = XCreateGC(display, screens[i].root, 0,
+                   &gcv);
+
+               XSetForeground(display, screens[i].bar_gc,
+                   screens[i].c[SWM_S_COLOR_BAR_FONT].color);
+
                /* set default cursor */
                XDefineCursor(display, screens[i].root,
                    XCreateFontCursor(display, XC_left_ptr));
@@ -6755,19 +6863,14 @@ setup_screens(void)
 void
 setup_globals(void)
 {
-       if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
-           == NULL)
-               err(1, "setup_globals: strdup");
-       if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
-           == NULL)
-               err(1, "setup_globals: strdup");
-       if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
-           == NULL)
-               err(1, "setup_globals: strdup");
+       if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
+               err(1, "setup_globals: strdup: failed to allocate memory.");
+
        if ((spawn_term[0] = strdup("xterm")) == NULL)
-               err(1, "setup_globals: strdup");
+               err(1, "setup_globals: strdup: failed to allocate memory.");
+
        if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
-               err(1, "strdup");
+               err(1, "setup_globals: strdup: failed to allocate memory.");
 }
 
 void
@@ -6811,12 +6914,14 @@ main(int argc, char *argv[])
        struct sigaction        sact;
 
        start_argv = argv;
-       fprintf(stderr, "Welcome to scrotwm V%s Build: %s\n",
-           SCROTWM_VERSION, buildstr);
+       warnx("Welcome to scrotwm V%s Build: %s", SCROTWM_VERSION, buildstr);
        if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
            !XSupportsLocale())
                warnx("no locale support");
 
+       if (!X_HAVE_UTF8_STRING)
+               warnx("no UTF-8 support");
+
        if (!(display = XOpenDisplay(0)))
                errx(1, "can not open display");
 
@@ -6852,8 +6957,8 @@ main(int argc, char *argv[])
        if (pwd == NULL)
                errx(1, "invalid user: %d", getuid());
 
-       setup_screens();
        setup_globals();
+       setup_screens();
        setup_keys();
        setup_quirks();
        setup_spawn();
@@ -6870,8 +6975,12 @@ main(int argc, char *argv[])
                        if (S_ISREG(sb.st_mode))
                                cfile = conf;
        }
-       if (cfile)
-               conf_load(cfile, SWM_CONF_DEFAULT);
+
+       /* load conf (if any) and refresh font color in bar graphics contexts */
+       if (cfile && conf_load(cfile, SWM_CONF_DEFAULT) == 0)
+               for (i = 0; i < ScreenCount(display); ++i)
+                       XSetForeground(display, screens[i].bar_gc,
+                           screens[i].c[SWM_S_COLOR_BAR_FONT].color);
 
        setup_ewmh();
        /* set some values to work around bad programs */
@@ -6968,7 +7077,12 @@ main(int argc, char *argv[])
 done:
        teardown_ewmh();
        bar_extra_stop();
-       XFreeGC(display, bar_gc);
+
+       for (i = 0; i < ScreenCount(display); ++i)
+               if (screens[i].bar_gc != NULL)
+                       XFreeGC(display, screens[i].bar_gc);
+
+       XFreeFontSet(display, bar_fs);
        XCloseDisplay(display);
 
        return (0);