JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Add new key binding: bar_toggle_ws (M-S-b)
[spectrwm.git] / spectrwm.c
index 5b1eafd..af2fe15 100644 (file)
@@ -348,7 +348,7 @@ double                      dialog_ratio = 0.6;
 
 char           *bar_argv[] = { NULL, NULL };
 int             bar_pipe[2];
-unsigned char   bar_ext[SWM_BAR_MAX];
+char            bar_ext[SWM_BAR_MAX];
 char            bar_vertext[SWM_BAR_MAX];
 int             bar_version = 0;
 sig_atomic_t    bar_alarm = 0;
@@ -494,6 +494,7 @@ struct workspace {
        int                     idx;            /* workspace index */
        char                    *name;          /* workspace name */
        int                     always_raise;   /* raise windows on focus */
+       int                     bar_enabled;    /* bar visibility */
        struct layout           *cur_layout;    /* current layout handlers */
        struct ws_win           *focus;         /* may be NULL */
        struct ws_win           *focus_prev;    /* may be NULL */
@@ -587,6 +588,8 @@ union arg {
 #define SWM_ARG_ID_MOVEDOWN    (101)
 #define SWM_ARG_ID_MOVELEFT    (102)
 #define SWM_ARG_ID_MOVERIGHT   (103)
+#define SWM_ARG_ID_BAR_TOGGLE  (110)
+#define SWM_ARG_ID_BAR_TOGGLE_WS       (111)
        char                    **argv;
 };
 
@@ -712,6 +715,7 @@ struct spawn_list           spawns = TAILQ_HEAD_INITIALIZER(spawns);
 /* user/key callable function IDs */
 enum keyfuncid {
        KF_BAR_TOGGLE,
+       KF_BAR_TOGGLE_WS,
        KF_BUTTON2,
        KF_CYCLE_LAYOUT,
        KF_FLIP_LAYOUT,
@@ -871,7 +875,7 @@ char        *expand_tilde(const char *);
 void    expose(xcb_expose_event_t *);
 void    fake_keypress(struct ws_win *, xcb_keysym_t, uint16_t);
 struct pid_e   *find_pid(pid_t);
-struct ws_win  *find_unmanaged_window(xcb_window_t); 
+struct ws_win  *find_unmanaged_window(xcb_window_t);
 struct ws_win  *find_window(xcb_window_t);
 void    floating_toggle(struct swm_region *, union arg *);
 int     floating_toggle_win(struct ws_win *);
@@ -1852,7 +1856,7 @@ bar_extra_stop(void)
                kill(bar_pid, SIGTERM);
                bar_pid = 0;
        }
-       strlcpy((char *)bar_ext, "", sizeof bar_ext);
+       strlcpy(bar_ext, "", sizeof bar_ext);
        bar_extra = 0;
 }
 
@@ -2175,6 +2179,14 @@ bar_fmt_print(void)
                TAILQ_FOREACH(r, &screens[i].rl, entry) {
                        if (r->bar == NULL)
                                continue;
+
+                       if (r->ws->bar_enabled)
+                               xcb_map_window(conn, r->bar->id);
+                       else {
+                               xcb_unmap_window(conn, r->bar->id);
+                               continue;
+                       }
+
                        bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
                        bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
                        if (bar_font_legacy)
@@ -2198,14 +2210,14 @@ bar_update(void)
                while ((b = fgetln(stdin, &len)) != NULL)
                        if (b && b[len - 1] == '\n') {
                                b[len - 1] = '\0';
-                               strlcpy((char *)bar_ext, b, sizeof bar_ext);
+                               strlcpy(bar_ext, b, sizeof bar_ext);
                        }
                if (b == NULL && errno != EAGAIN) {
                        warn("bar_update: bar_extra failed");
                        bar_extra_stop();
                }
        } else
-               strlcpy((char *)bar_ext, "", sizeof bar_ext);
+               strlcpy(bar_ext, "", sizeof bar_ext);
 
        bar_fmt_print();
        alarm(bar_delay);
@@ -2232,20 +2244,27 @@ bar_toggle(struct swm_region *r, union arg *args)
 
        DNPRINTF(SWM_D_BAR, "bar_toggle\n");
 
-       num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
-       if (bar_enabled) {
-               for (i = 0; i < num_screens; i++)
-                       TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
-                               if (tmpr->bar)
-                                       xcb_unmap_window(conn, tmpr->bar->id);
-       } else {
-               for (i = 0; i < num_screens; i++)
-                       TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
-                               if (tmpr->bar)
-                                       xcb_map_window(conn, tmpr->bar->id);
+       switch (args->id) {
+       case SWM_ARG_ID_BAR_TOGGLE_WS:
+               /* Only change if master switch is enabled. */
+               if (bar_enabled)
+                       r->ws->bar_enabled = !r->ws->bar_enabled;
+               break;
+       case SWM_ARG_ID_BAR_TOGGLE:
+               bar_enabled = !bar_enabled;
+               break;
        }
 
-       bar_enabled = !bar_enabled;
+       /* update bars as necessary */
+       num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
+       for (i = 0; i < num_screens; i++)
+               TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
+                       if (tmpr->bar) {
+                               if (bar_enabled && tmpr->ws->bar_enabled)
+                                       xcb_map_window(conn, tmpr->bar->id);
+                               else
+                                       xcb_unmap_window(conn, tmpr->bar->id);
+                       }
 
        stack();
 
@@ -2405,7 +2424,7 @@ xft_init(struct swm_region *r)
 
        if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
            DefaultColormap(display, r->s->idx), &color, &bar_font_color))
-               warn("unable to allocate Xft color");
+               warn("Xft error: unable to allocate color.");
 
        bar_height = bar_font->height + 2 * bar_border_width;
 
@@ -2802,6 +2821,7 @@ get_pointer_win(xcb_window_t root)
                } else {
                        DNPRINTF(SWM_D_EVENT, "get_pointer_win: none.\n");
                }
+               free(r);
        }
 
        return win;
@@ -3072,6 +3092,7 @@ unfocus_win(struct ws_win *win)
                kill_refs(win->ws->focus);
                win->ws->focus = NULL;
        }
+
        if (validate_win(win->ws->focus_prev)) {
                kill_refs(win->ws->focus_prev);
                win->ws->focus_prev = NULL;
@@ -3779,7 +3800,7 @@ stack(void) {
                        g = r->g;
                        g.w -= 2 * border_width;
                        g.h -= 2 * border_width;
-                       if (bar_enabled) {
+                       if (bar_enabled && r->ws->bar_enabled) {
                                if (!bar_at_bottom)
                                        g.y += bar_height;
                                g.h -= bar_height;
@@ -4072,7 +4093,8 @@ stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
                else
                        win_g.y += last_h + 2 * border_width;
 
-               if (disable_border && !bar_enabled && winno == 1){
+               if (disable_border && !(bar_enabled && ws->bar_enabled) &&
+                   winno == 1){
                        bordered = 0;
                        win_g.w += 2 * border_width;
                        win_g.h += 2 * border_width;
@@ -4289,7 +4311,7 @@ max_stack(struct workspace *ws, struct swm_geometry *g)
                if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
                    HEIGHT(w) != gg.h) {
                        w->g = gg;
-                       if (bar_enabled){
+                       if (bar_enabled && ws->bar_enabled){
                                w->bordered = 1;
                        } else {
                                w->bordered = 0;
@@ -4488,24 +4510,28 @@ get_win_name(xcb_window_t win)
            XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
        r = xcb_get_property_reply(conn, c, NULL);
 
-       if (!r || r->type == XCB_NONE) {
-               free(r);
-               /* Use WM_NAME instead; no UTF-8. */
-               c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
-                   XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
-               r = xcb_get_property_reply(conn, c, NULL);
-
-               if(!r || r->type == XCB_NONE) {
+       if (r) {
+               if (r->type == XCB_NONE) {
                        free(r);
-                       return NULL;
+                       /* Use WM_NAME instead; no UTF-8. */
+                       c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
+                               XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
+                       r = xcb_get_property_reply(conn, c, NULL);
+
+                       if (!r)
+                               return (NULL);
+                       if (r->type == XCB_NONE) {
+                               free(r);
+                               return (NULL);
+                       }
                }
-       }
+               if (r->length > 0)
+                       name = strndup(xcb_get_property_value(r),
+                                  xcb_get_property_value_length(r));
 
-       if (r->length > 0)
-               name = strndup(xcb_get_property_value(r),
-                   xcb_get_property_value_length(r));
+               free(r);
+       }
 
-       free(r);
        return (name);
 }
 
@@ -5376,7 +5402,8 @@ struct keyfunc {
        union arg               args;
 } keyfuncs[KF_INVALID + 1] = {
        /* name                 function        argument */
-       { "bar_toggle",         bar_toggle,     {0} },
+       { "bar_toggle",         bar_toggle,     {.id = SWM_ARG_ID_BAR_TOGGLE} },
+       { "bar_toggle_ws",      bar_toggle,     {.id = SWM_ARG_ID_BAR_TOGGLE_WS} },
        { "button2",            pressbutton,    {2} },
        { "cycle_layout",       cycle_layout,   {0} },
        { "flip_layout",        stack_config,   {.id = SWM_ARG_ID_FLIPLAYOUT} },
@@ -5940,7 +5967,7 @@ setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
                return;
        }
        if (kfid == KF_INVALID) {
-               warnx("error: setkeybinding: cannot find mod/key combination");
+               warnx("bind: Key combination already unbound.");
                DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
                return;
        }
@@ -6075,6 +6102,7 @@ setup_keys(void)
        setkeybinding(MODKEY_SHIFT,     XK_F11,         KF_MVWS_21,     NULL);
        setkeybinding(MODKEY_SHIFT,     XK_F12,         KF_MVWS_22,     NULL);
        setkeybinding(MODKEY,           XK_b,           KF_BAR_TOGGLE,  NULL);
+       setkeybinding(MODKEY_SHIFT,     XK_b,           KF_BAR_TOGGLE_WS,NULL);
        setkeybinding(MODKEY,           XK_Tab,         KF_FOCUS_NEXT,  NULL);
        setkeybinding(MODKEY_SHIFT,     XK_Tab,         KF_FOCUS_PREV,  NULL);
        setkeybinding(MODKEY_SHIFT,     XK_x,           KF_WIND_KILL,   NULL);
@@ -6177,7 +6205,7 @@ grabkeys(void)
 {
        struct key              *kp;
        int                     num_screens, k, j;
-       unsigned int            modifiers[3];
+       unsigned int            modifiers[4];
        xcb_keycode_t           *code;
 
        DNPRINTF(SWM_D_MISC, "grabkeys\n");
@@ -6185,7 +6213,8 @@ grabkeys(void)
 
        modifiers[0] = 0;
        modifiers[1] = numlockmask;
-       modifiers[2] = numlockmask | XCB_MOD_MASK_LOCK;
+       modifiers[2] = XCB_MOD_MASK_LOCK;
+       modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
 
        num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
        for (k = 0; k < num_screens; k++) {
@@ -6211,16 +6240,25 @@ grabkeys(void)
 void
 grabbuttons(struct ws_win *win)
 {
-       int             i;
+       unsigned int    modifiers[4];
+       int             i, j;
 
        DNPRINTF(SWM_D_MOUSE, "grabbuttons: win 0x%x\n", win->id);
+       updatenumlockmask();
+
+       modifiers[0] = 0;
+       modifiers[1] = numlockmask;
+       modifiers[2] = XCB_MOD_MASK_LOCK;
+       modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
 
        for (i = 0; i < LENGTH(buttons); i++)
                if (buttons[i].action == client_click)
-                       xcb_grab_button(conn, 0, win->id, BUTTONMASK,
-                           XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
-                           XCB_WINDOW_NONE, XCB_CURSOR_NONE,
-                           buttons[i].button, buttons[i].mask);
+                       for (j = 0; j < LENGTH(modifiers); ++j)
+                               xcb_grab_button(conn, 0, win->id, BUTTONMASK,
+                                   XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
+                                   XCB_WINDOW_NONE, XCB_CURSOR_NONE,
+                                   buttons[i].button, buttons[i].mask |
+                                   modifiers[j]);
 }
 
 const char *quirkname[] = {
@@ -7442,7 +7480,13 @@ buttonpress(xcb_button_press_event_t *e)
                        /* Focus on empty region */
                        /* If no windows on region if its empty. */
                        r = root_to_region(e->root, SWM_CK_POINTER);
-                       if (r && TAILQ_EMPTY(&r->ws->winlist)) {
+                       if (r == NULL) {
+                               DNPRINTF(SWM_D_EVENT, "buttonpress: "
+                                   "NULL region; ignoring.\n");
+                               goto out;
+                       }
+
+                       if (TAILQ_EMPTY(&r->ws->winlist)) {
                                old_r = root_to_region(e->root, SWM_CK_FOCUS);
                                if (old_r && old_r != r)
                                        unfocus_win(old_r->ws->focus);
@@ -7462,7 +7506,7 @@ buttonpress(xcb_button_press_event_t *e)
        }
 
        if (win == NULL)
-               return;
+               goto out;
 
        last_event_time = e->time;
 
@@ -7651,9 +7695,10 @@ configurerequest(xcb_configure_request_event_t *e)
                        WIDTH(win) = win->g_float.w;
                        HEIGHT(win) = win->g_float.h;
 
-                       stack_floater(win, win->ws->r);
-
-                       focus_flush();
+                       if (r) {
+                               stack_floater(win, r);
+                               focus_flush();
+                       }
                } else {
                        config_win(win, e);
                        xcb_flush(conn);
@@ -7816,6 +7861,12 @@ enternotify(xcb_enter_notify_event_t *e)
                if (e->event == e->root) {
                        /* If no windows on pointer region, then focus root. */
                        r = root_to_region(e->root, SWM_CK_POINTER);
+                       if (r == NULL) {
+                               DNPRINTF(SWM_D_EVENT, "enterntoify: "
+                                   "NULL region; ignoring.\n");
+                               return;
+                       }
+
                        if (TAILQ_EMPTY(&r->ws->winlist)) {
                                old_r = root_to_region(e->root, SWM_CK_FOCUS);
                                if (old_r && old_r != r)
@@ -7856,9 +7907,6 @@ leavenotify(xcb_leave_notify_event_t *e)
 }
 #endif
 
-/* lets us use one switch statement for arbitrary mode/detail combinations */
-#define MERGE_MEMBERS(a,b)     (((a & 0xffff) << 16) | (b & 0xffff))
-
 void
 mapnotify(xcb_map_notify_event_t *e)
 {
@@ -8579,6 +8627,7 @@ setup_screens(void)
                        ws = &screens[i].ws[j];
                        ws->idx = j;
                        ws->name = NULL;
+                       ws->bar_enabled = 1;
                        ws->focus = NULL;
                        ws->focus_prev = NULL;
                        ws->focus_pending = NULL;