]> code.delx.au - spectrwm/blobdiff - spectrwm.c
Fix crash in rg_n/mvrg_n when the target region does not exist.
[spectrwm] / spectrwm.c
index 00304b547c8c8256fee343cc45d9bf3924241f1a..4aff709a617294c469dbf0728658cdaf8a5238ab 100644 (file)
@@ -244,6 +244,7 @@ u_int32_t           swm_debug = 0
 #define SH_INC_H(w)            (w)->sh.height_inc
 #define SWM_MAX_FONT_STEPS     (3)
 #define WINID(w)               ((w) ? (w)->id : XCB_WINDOW_NONE)
+#define WS_FOCUSED(ws)         ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
 #define YESNO(x)               ((x) ? "yes" : "no")
 
 /* Constrain Window flags */
@@ -279,10 +280,7 @@ xcb_atom_t         a_state;
 xcb_atom_t             a_prot;
 xcb_atom_t             a_delete;
 xcb_atom_t             a_takefocus;
-xcb_atom_t             a_wmname;
-xcb_atom_t             a_netwmname;
 xcb_atom_t             a_utf8_string;
-xcb_atom_t             a_string;
 xcb_atom_t             a_swm_iconic;
 xcb_atom_t             a_swm_ws;
 volatile sig_atomic_t   running = 1;
@@ -1051,6 +1049,7 @@ void       spawn(int, union arg *, int);
 void    spawn_custom(struct swm_region *, union arg *, const char *);
 int     spawn_expand(struct swm_region *, union arg *, const char *, char ***);
 void    spawn_insert(const char *, const char *, int);
+struct spawn_prog      *spawn_find(const char *);
 void    spawn_remove(struct spawn_prog *);
 void    spawn_replace(struct spawn_prog *, const char *, const char *, int);
 void    spawn_select(struct swm_region *, union arg *, const char *, int *);
@@ -3496,10 +3495,11 @@ focus_region(struct swm_region *r)
 void
 switchws(struct swm_region *r, union arg *args)
 {
-       int                     wsid = args->id, unmap_old = 0;
        struct swm_region       *this_r, *other_r;
        struct ws_win           *win;
        struct workspace        *new_ws, *old_ws;
+       xcb_window_t            none = XCB_WINDOW_NONE;
+       int                     wsid = args->id, unmap_old = 0;
 
        if (!(r && r->s))
                return;
@@ -3519,7 +3519,14 @@ switchws(struct swm_region *r, union arg *args)
        if (new_ws == old_ws)
                return;
 
-       unfocus_win(old_ws->focus);
+       if ((win = old_ws->focus) != NULL) {
+               xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
+                   &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
+
+               xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
+                   ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
+                   &none);
+       }
 
        other_r = new_ws->r;
        if (other_r == NULL) {
@@ -3538,8 +3545,11 @@ switchws(struct swm_region *r, union arg *args)
 
        /* Set focus_pending before stacking, if needed. */
        if (focus_mode != SWM_FOCUS_FOLLOW && (!new_ws->focus_pending ||
-           validate_win(new_ws->focus_pending)))
+           validate_win(new_ws->focus_pending))) {
                new_ws->focus_pending = get_region_focus(new_ws->r);
+               new_ws->focus = new_ws->focus_prev;
+               new_ws->focus_prev = NULL;
+       }
 
        stack();
 
@@ -3649,7 +3659,7 @@ focusrg(struct swm_region *r, union arg *args)
        DNPRINTF(SWM_D_FOCUS, "focusrg: id: %d\n", ridx);
 
        rr = TAILQ_FIRST(&r->s->rl);
-       for (i = 0; i < ridx; ++i)
+       for (i = 0; i < ridx && rr != NULL; ++i)
                rr = TAILQ_NEXT(rr, entry);
 
        if (rr == NULL)
@@ -4661,7 +4671,7 @@ send_to_rg(struct swm_region *r, union arg *args)
        DNPRINTF(SWM_D_FOCUS, "send_to_rg: id: %d\n", ridx);
 
        rr = TAILQ_FIRST(&r->s->rl);
-       for (i = 0; i < ridx; ++i)
+       for (i = 0; i < ridx && rr != NULL; ++i)
                rr = TAILQ_NEXT(rr, entry);
 
        if (rr == NULL)
@@ -4859,7 +4869,7 @@ get_win_name(xcb_window_t win)
        xcb_get_property_reply_t        *r;
 
        /* First try _NET_WM_NAME for UTF-8. */
-       c = xcb_get_property(conn, 0, win, a_netwmname,
+       c = xcb_get_property(conn, 0, win, ewmh[_NET_WM_NAME].atom,
            XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
        r = xcb_get_property_reply(conn, c, NULL);
 
@@ -5308,16 +5318,12 @@ wkill(struct swm_region *r, union arg *args)
 int
 floating_toggle_win(struct ws_win *win)
 {
-       struct swm_region       *r;
-
        if (win == NULL)
                return (0);
 
        if (!win->ws->r)
                return (0);
 
-       r = win->ws->r;
-
        /* reject floating toggles in max stack mode */
        if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
                return (0);
@@ -5707,14 +5713,12 @@ move(struct ws_win *win, union arg *args)
 {
        xcb_timestamp_t         timestamp = 0;
        int                     move_stp = 0, moving;
-       struct swm_region       *r = NULL;
        xcb_query_pointer_reply_t       *qpr = NULL;
        xcb_generic_event_t             *evt;
        xcb_motion_notify_event_t       *mne;
 
        if (win == NULL)
                return;
-       r = win->ws->r;
 
        if (win->ewmh_flags & EWMH_F_FULLSCREEN)
                return;
@@ -6040,7 +6044,7 @@ spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
     char ***ret_args)
 {
        struct spawn_prog       *prog = NULL;
-       int                     i;
+       int                     i, c;
        char                    *ap, **real_args;
 
        /* suppress unused warning since var is needed */
@@ -6063,62 +6067,68 @@ spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
                err(1, "spawn_custom: calloc real_args");
 
        /* expand spawn_args into real_args */
-       for (i = 0; i < prog->argc; i++) {
+       for (i = c = 0; i < prog->argc; i++) {
                ap = prog->argv[i];
                DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
                if (!strcasecmp(ap, "$bar_border")) {
-                       if ((real_args[i] =
+                       if ((real_args[c] =
                            strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
                            == NULL)
                                err(1,  "spawn_custom border color");
                } else if (!strcasecmp(ap, "$bar_color")) {
-                       if ((real_args[i] =
+                       if ((real_args[c] =
                            strdup(r->s->c[SWM_S_COLOR_BAR].name))
                            == NULL)
                                err(1, "spawn_custom bar color");
                } else if (!strcasecmp(ap, "$bar_font")) {
-                       if ((real_args[i] = strdup(bar_fonts))
+                       if ((real_args[c] = strdup(bar_fonts))
                            == NULL)
                                err(1, "spawn_custom bar fonts");
                } else if (!strcasecmp(ap, "$bar_font_color")) {
-                       if ((real_args[i] =
+                       if ((real_args[c] =
                            strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
                            == NULL)
                                err(1, "spawn_custom color font");
                } else if (!strcasecmp(ap, "$color_focus")) {
-                       if ((real_args[i] =
+                       if ((real_args[c] =
                            strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
                            == NULL)
                                err(1, "spawn_custom color focus");
                } else if (!strcasecmp(ap, "$color_unfocus")) {
-                       if ((real_args[i] =
+                       if ((real_args[c] =
                            strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
                            == NULL)
                                err(1, "spawn_custom color unfocus");
                } else if (!strcasecmp(ap, "$region_index")) {
-                       if (asprintf(&real_args[i], "%d",
+                       if (asprintf(&real_args[c], "%d",
                            get_region_index(r) + 1) < 1)
                                err(1, "spawn_custom region index");
                } else if (!strcasecmp(ap, "$workspace_index")) {
-                       if (asprintf(&real_args[i], "%d", r->ws->idx + 1) < 1)
+                       if (asprintf(&real_args[c], "%d", r->ws->idx + 1) < 1)
+                               err(1, "spawn_custom workspace index");
+               } else if (!strcasecmp(ap, "$dmenu_bottom")) {
+                       if (!bar_at_bottom)
+                               continue;
+                       if ((real_args[c] = strdup("-b")) == NULL)
                                err(1, "spawn_custom workspace index");
                } else {
                        /* no match --> copy as is */
-                       if ((real_args[i] = strdup(ap)) == NULL)
+                       if ((real_args[c] = strdup(ap)) == NULL)
                                err(1, "spawn_custom strdup(ap)");
                }
                DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
-                   real_args[i]);
+                   real_args[c]);
+               ++c;
        }
 
 #ifdef SWM_DEBUG
        DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
-       for (i = 0; i < prog->argc; i++)
-               DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
-       DNPRINTF(SWM_D_SPAWN, "\n");
+       for (i = 0; i < c; ++i)
+               DPRINTF("\"%s\" ", real_args[i]);
+       DPRINTF("\n");
 #endif
        *ret_args = real_args;
-       return (prog->argc);
+       return (c);
 }
 
 void
@@ -6235,6 +6245,18 @@ spawn_remove(struct spawn_prog *sp)
        DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
 }
 
+struct spawn_prog*
+spawn_find(const char *name)
+{
+       struct spawn_prog       *sp;
+
+       TAILQ_FOREACH(sp, &spawns, entry)
+               if (!strcasecmp(sp->name, name))
+                       return sp;
+
+       return NULL;
+}
+
 void
 setspawn(const char *name, const char *args, int flags)
 {
@@ -6246,11 +6268,8 @@ setspawn(const char *name, const char *args, int flags)
                return;
 
        /* Remove any old spawn under the same name. */
-       TAILQ_FOREACH(sp, &spawns, entry)
-               if (!strcmp(sp->name, name)) {
-                       spawn_remove(sp);
-                       break;
-               }
+       if ((sp = spawn_find(name)) != NULL)
+               spawn_remove(sp);
 
        if (*args != '\0')
                spawn_insert(name, args, flags);
@@ -6290,11 +6309,7 @@ validate_spawns(void)
                        continue;
 
                /* find program */
-               TAILQ_FOREACH(sp, &spawns, entry) {
-                       if (!strcasecmp(kp->spawn_name, sp->name))
-                               break;
-               }
-
+               sp = spawn_find(kp->spawn_name);
                if (sp == NULL || sp->flags & SWM_SPAWN_OPTIONAL)
                        continue;
 
@@ -6322,6 +6337,7 @@ setup_spawn(void)
        setconfspawn("spawn_term",      "xterm",                0);
 
        setconfspawn("menu",            "dmenu_run"
+                                       " $dmenu_bottom"
                                        " -fn $bar_font"
                                        " -nb $bar_color"
                                        " -nf $bar_font_color"
@@ -6329,6 +6345,7 @@ setup_spawn(void)
                                        " -sf $bar_color",      0);
 
        setconfspawn("search",          "dmenu"
+                                       " $dmenu_bottom"
                                        " -i"
                                        " -fn $bar_font"
                                        " -nb $bar_color"
@@ -6337,6 +6354,7 @@ setup_spawn(void)
                                        " -sf $bar_color",      0);
 
        setconfspawn("name_workspace",  "dmenu"
+                                       " $dmenu_bottom"
                                        " -p Workspace"
                                        " -fn $bar_font"
                                        " -nb $bar_color"
@@ -6531,17 +6549,15 @@ setconfbinding(char *selector, char *value, int flags)
                }
        }
        /* search by custom spawn name */
-       TAILQ_FOREACH(sp, &spawns, entry) {
-               if (strcasecmp(selector, sp->name) == 0) {
-                       DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
-                           "spawn\n", selector);
-                       if (parsekeys(value, mod_key, &mod, &ks) == 0) {
-                               setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
-                                   sp->name);
-                               return (0);
-                       } else
-                               return (1);
-               }
+       if ((sp = spawn_find(selector)) != NULL) {
+               DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
+                   "spawn\n", selector);
+               if (parsekeys(value, mod_key, &mod, &ks) == 0) {
+                       setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
+                           sp->name);
+                       return (0);
+               } else
+                       return (1);
        }
        DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
        return (1);
@@ -8402,11 +8418,11 @@ destroynotify(xcb_destroy_notify_event_t *e)
        unmanage_window(win);
        stack();
 
-       if (focus_mode != SWM_FOCUS_FOLLOW) {
+       if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
                if (win->ws->focus_pending) {
                        focus_win(win->ws->focus_pending);
                        win->ws->focus_pending = NULL;
-               } else if (win == win->ws->focus && win->ws->r) {
+               } else if (win == win->ws->focus) {
                        xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
                            win->ws->r->id, XCB_CURRENT_TIME);
                }
@@ -8555,7 +8571,7 @@ mapnotify(xcb_map_notify_event_t *e)
        win->mapped = 1;
        set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
 
-       if (focus_mode != SWM_FOCUS_FOLLOW) {
+       if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
                if (win->ws->focus_pending == win) {
                        focus_win(win);
                        win->ws->focus_pending = NULL;
@@ -8737,7 +8753,8 @@ propertynotify(xcb_property_notify_event_t *e)
                        if (ws->r) {
                                stack();
 
-                               if (focus_mode != SWM_FOCUS_FOLLOW) {
+                               if (focus_mode != SWM_FOCUS_FOLLOW &&
+                                   WS_FOCUSED(ws)) {
                                        if (ws->focus_pending) {
                                                focus_win(ws->focus_pending);
                                                ws->focus_pending = NULL;
@@ -8764,7 +8781,7 @@ propertynotify(xcb_property_notify_event_t *e)
        } else if (e->atom == a_state) {
                /* State just changed, make sure it gets focused if mapped. */
                if (e->state == XCB_PROPERTY_NEW_VALUE) {
-                       if (focus_mode != SWM_FOCUS_FOLLOW) {
+                       if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(ws)) {
                                if (win->mapped &&
                                    ws->focus_pending == win) {
                                        focus_win(ws->focus_pending);
@@ -8820,15 +8837,16 @@ unmapnotify(xcb_unmap_notify_event_t *e)
                if (ws->r)
                        stack();
 
-               if (focus_mode == SWM_FOCUS_FOLLOW) {
-                       if (ws->r)
+               if (WS_FOCUSED(ws)) {
+                       if (focus_mode == SWM_FOCUS_FOLLOW) {
                                focus_win(get_pointer_win(ws->r->s->root));
-               } else if (ws->focus_pending) {
-                       focus_win(ws->focus_pending);
-                       ws->focus_pending = NULL;
-               } else if (ws->focus == NULL && ws->r) {
-                       xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
-                           ws->r->id, XCB_CURRENT_TIME);
+                       } else if (ws->focus_pending) {
+                               focus_win(ws->focus_pending);
+                               ws->focus_pending = NULL;
+                       } else if (ws->focus == NULL) {
+                               xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
+                                   ws->r->id, XCB_CURRENT_TIME);
+                       }
                }
        }
 
@@ -8875,7 +8893,10 @@ clientmessage(xcb_client_message_event_t *e)
 
        if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
                DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
-               focus_win(win);
+               if (WS_FOCUSED(win->ws))
+                       focus_win(win);
+               else
+                       win->ws->focus_pending = win;
        }
        if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
                DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
@@ -9425,10 +9446,7 @@ setup_globals(void)
        a_prot = get_atom_from_string("WM_PROTOCOLS");
        a_delete = get_atom_from_string("WM_DELETE_WINDOW");
        a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
-       a_wmname = get_atom_from_string("WM_NAME");
-       a_netwmname = get_atom_from_string("_NET_WM_NAME");
        a_utf8_string = get_atom_from_string("UTF8_STRING");
-       a_string = get_atom_from_string("STRING");
        a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
        a_swm_ws = get_atom_from_string("_SWM_WS");
 }
@@ -9457,7 +9475,8 @@ workaround(void)
                xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
                    netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
                xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
-                   a_netwmname, a_utf8_string, 8, strlen("LG3D"), "LG3D");
+                   ewmh[_NET_WM_NAME].atom, a_utf8_string, 8, strlen("LG3D"),
+                   "LG3D");
        }
 }