JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Add stacking indicator in the bar. Purdy :-)
[spectrwm.git] / scrotwm.c
index 09f664f..f865183 100644 (file)
--- a/scrotwm.c
+++ b/scrotwm.c
@@ -192,6 +192,7 @@ int                 bar_extra = 1;
 int                    bar_extra_running = 0;
 int                    bar_verbose = 1;
 int                    bar_height = 0;
+int                    stack_enabled = 1;
 int                    clock_enabled = 1;
 int                    title_name_enabled = 0;
 int                    title_class_enabled = 0;
@@ -321,12 +322,17 @@ void      new_region(struct swm_screen *, int, int, int, int);
 struct layout {
        void            (*l_stack)(struct workspace *, struct swm_geometry *);
        void            (*l_config)(struct workspace *, int);
+       u_int32_t       flags;
+#define SWM_L_FOCUSPREV                (1<<0)
+#define SWM_L_MAPONFOCUS       (1<<1)
+       char            *name;
 } layouts[] =  {
        /* stack,               configure */
-       { vertical_stack,       vertical_config},
-       { horizontal_stack,     horizontal_config},
-       { max_stack,            NULL},
-       { NULL,                 NULL},
+       { vertical_stack,       vertical_config,        0,      "[|]" },
+       { horizontal_stack,     horizontal_config,      0,      "[-]" },
+       { max_stack,            NULL,
+         SWM_L_FOCUSPREV | SWM_L_MAPONFOCUS,                   "[ ]"},
+       { NULL,                 NULL,                   0},
 };
 
 #define SWM_H_SLICE            (32)
@@ -755,6 +761,7 @@ bar_update(void)
        char                    s[SWM_BAR_MAX];
        char                    loc[SWM_BAR_MAX];
        char                    *b;
+       char                    *stack = "";
 
        if (bar_enabled == 0)
                return;
@@ -787,12 +794,15 @@ bar_update(void)
                        if (r && r->ws)
                                bar_class_name(s, sizeof s, r->ws->focus);
 
-                       snprintf(loc, sizeof loc, "%d:%d    %s %s    %s",
-                           x++, r->ws->idx + 1, s, bar_ext, bar_vertext);
+                       if (stack_enabled)
+                               stack = r->ws->cur_layout->name;
+
+                       snprintf(loc, sizeof loc, "%d:%d %s   %s %s    %s",
+                           x++, r->ws->idx + 1, stack, s, bar_ext,
+                           bar_vertext);
                        bar_print(r, loc);
                }
        }
-       XSync(display, False);
        alarm(bar_delay);
 }
 
@@ -1224,6 +1234,8 @@ focus_win(struct ws_win *win)
                XSetWindowBorder(display, win->id,
                    win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
                grabbuttons(win, 1);
+               if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
+                       XMapRaised(display, win->id);
                XSetInputFocus(display, win->id,
                    RevertToPointerRoot, CurrentTime);
                XSync(display, False);
@@ -1463,9 +1475,7 @@ focus(struct swm_region *r, union arg *args)
        if (winfocus == winlostfocus || winfocus == NULL)
                return;
 
-       XMapRaised(display, winfocus->id);
        focus_win(winfocus);
-       XSync(display, False);
 }
 
 void
@@ -1485,6 +1495,7 @@ cycle_layout(struct swm_region *r, union arg *args)
        ignore_enter = 1;
        stack();
        focus_win(winfocus);
+       ignore_enter = 0;
 }
 
 void
@@ -1877,6 +1888,9 @@ max_stack(struct workspace *ws, struct swm_geometry *g)
 
        DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
 
+       if (ws == NULL)
+               return;
+
        winno = count_win(ws, 0);
        if (winno == 0 && count_win(ws, 1) == 0)
                return;
@@ -1893,15 +1907,17 @@ max_stack(struct workspace *ws, struct swm_geometry *g)
                        win->g.h = wc.height = gg.h;
                        mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
                        XConfigureWindow(display, win->id, mask, &wc);
+
+                       /* unmap only if we don't have multi screen */
                        if (win != ws->focus)
-                               unmap_window(win);
+                               if (!(ScreenCount(display) > 1 || outputs > 1))
+                                       unmap_window(win);
                }
        }
 
        /* put the last transient on top */
        if (wintrans) {
                stack_floater(wintrans, ws->r);
-               XMapRaised(display, wintrans->id);
                focus_win(wintrans); /* override */
        }
 }
@@ -1910,11 +1926,15 @@ void
 send_to_ws(struct swm_region *r, union arg *args)
 {
        int                     wsid = args->id;
-       struct ws_win           *win = r->ws->focus, *winfocus = NULL;
+       struct ws_win           *win = win, *winfocus = NULL;
        struct workspace        *ws, *nws;
        Atom                    ws_idx_atom = 0;
        unsigned char           ws_idx_str[SWM_PROPLEN];
 
+       if (r && r->ws)
+               win = r->ws->focus;
+       else
+               return;
        if (win == NULL)
                return;
 
@@ -1928,18 +1948,18 @@ send_to_ws(struct swm_region *r, union arg *args)
        if (TAILQ_FIRST(&ws->winlist) == win)
                winfocus = TAILQ_NEXT(win, entry);
        else {
-               winfocus = TAILQ_PREV(ws->focus, ws_win_list, entry);
+               winfocus = TAILQ_PREV(win, ws_win_list, entry);
                if (winfocus == NULL)
                        winfocus = TAILQ_LAST(&ws->winlist, ws_win_list);
        }
-       /* out of windows in ws so focus on nws instead */
+       /* out of windows in ws so focus on nws instead if we multi screen */
        if (winfocus == NULL)
-               winfocus = win;
+               if (ScreenCount(display) > 1 || outputs > 1)
+                       winfocus = win;
 
-       unmap_window(win);
 
+       unmap_window(win);
        TAILQ_REMOVE(&ws->winlist, win, entry);
-
        TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
        win->ws = nws;
 
@@ -1959,7 +1979,8 @@ send_to_ws(struct swm_region *r, union arg *args)
        nws->restack = 1;
 
        stack();
-       focus_win(winfocus);
+       if (winfocus)
+               focus_win(winfocus);
 }
 
 void
@@ -2958,11 +2979,11 @@ setup_quirks(void)
 /* conf file stuff */
 #define SWM_CONF_FILE  "scrotwm.conf"
 
-enum   { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_CLOCK_ENABLED,
-         SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
-         SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED, SWM_S_TITLE_NAME_ENABLED,
-         SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM, SWM_S_SS_APP,
-         SWM_S_DIALOG_RATIO };
+enum   { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_STACK_ENABLED,
+         SWM_S_CLOCK_ENABLED, SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE,
+         SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
+         SWM_S_TITLE_NAME_ENABLED, SWM_S_BAR_FONT, SWM_S_BAR_ACTION,
+         SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO };
 
 int
 setconfvalue(char *selector, char *value, int flags)
@@ -2974,6 +2995,9 @@ setconfvalue(char *selector, char *value, int flags)
        case SWM_S_BAR_ENABLED:
                bar_enabled = atoi(value);
                break;
+       case SWM_S_STACK_ENABLED:
+               stack_enabled = atoi(value);
+               break;
        case SWM_S_CLOCK_ENABLED:
                clock_enabled = atoi(value);
                break;
@@ -3068,6 +3092,7 @@ struct config_option configopt[] = {
        { "bar_action",                 setconfvalue,   SWM_S_BAR_ACTION },
        { "bar_delay",                  setconfvalue,   SWM_S_BAR_DELAY },
        { "bind",                       setconfbinding, 0 },
+       { "stack_enabled",              setconfvalue,   SWM_S_STACK_ENABLED },
        { "clock_enabled",              setconfvalue,   SWM_S_CLOCK_ENABLED },
        { "color_focus",                setconfcolor,   SWM_S_COLOR_FOCUS },
        { "color_unfocus",              setconfcolor,   SWM_S_COLOR_UNFOCUS },
@@ -3424,8 +3449,6 @@ configurerequest(XEvent *e)
        int                     new = 0;
        XWindowChanges          wc;
 
-       SWM_EV_PROLOGUE(display);
-
        if ((win = find_window(ev->window)) == NULL)
                new = 1;
 
@@ -3475,8 +3498,6 @@ configurerequest(XEvent *e)
                } else
                        config_win(win);
        }
-
-       SWM_EV_EPILOGUE(display);
 }
 
 void
@@ -3488,8 +3509,6 @@ configurenotify(XEvent *e)
        DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
            e->xconfigure.window);
 
-       SWM_EV_PROLOGUE(display);
-
        XMapWindow(display, e->xconfigure.window);
        win = find_window(e->xconfigure.window);
        if (win) {
@@ -3499,8 +3518,6 @@ configurenotify(XEvent *e)
                if (font_adjusted)
                        stack();
        }
-
-       SWM_EV_EPILOGUE(display);
 }
 
 void
@@ -3525,12 +3542,22 @@ destroynotify(XEvent *e)
                if (win->transient)
                        winfocus = find_window(win->transient);
                else if (ws->focus == win) {
-                       if (TAILQ_FIRST(wl) == win)
-                               winfocus = TAILQ_NEXT(win, entry);
-                       else {
-                               winfocus = TAILQ_PREV(ws->focus, ws_win_list, entry);
-                               if (winfocus == NULL)
-                                       winfocus = TAILQ_LAST(wl, ws_win_list);
+                       /* if in max_stack try harder */
+                       if (ws->cur_layout->flags & SWM_L_FOCUSPREV)
+                               if (win != ws->focus && win != ws->focus_prev)
+                                       winfocus = ws->focus_prev;
+
+                       /* fallback and normal handling */
+                       if (winfocus == NULL) {
+                               if (TAILQ_FIRST(wl) == win)
+                                       winfocus = TAILQ_NEXT(win, entry);
+                               else {
+                                       winfocus = TAILQ_PREV(ws->focus,
+                                           ws_win_list, entry);
+                                       if (winfocus == NULL)
+                                               winfocus = TAILQ_LAST(wl,
+                                                   ws_win_list);
+                               }
                        }
                }
 
@@ -3672,7 +3699,7 @@ propertynotify(XEvent *e)
 void
 unmapnotify(XEvent *e)
 {
-       struct ws_win           *win, *winfocus;
+       struct ws_win           *win, *winfocus = NULL;
        struct workspace        *ws;
 
        DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
@@ -3692,15 +3719,24 @@ unmapnotify(XEvent *e)
                 * longer visible due to the app unmapping it so unmanage it
                 */
 
-               /* find something to focus */
                ws = win->ws;
-               winfocus = TAILQ_PREV(win, ws_win_list, entry);
-               if (TAILQ_FIRST(&ws->winlist) == win)
-                       winfocus = TAILQ_NEXT(win, entry);
-               else {
-                       winfocus = TAILQ_PREV(ws->focus, ws_win_list, entry);
-                       if (winfocus == NULL)
-                               winfocus = TAILQ_LAST(&ws->winlist, ws_win_list);
+               /* if we are max_stack try harder to focus on something */
+               if (ws->cur_layout->flags & SWM_L_FOCUSPREV)
+                       if (win != ws->focus && win != ws->focus_prev)
+                               winfocus = ws->focus_prev;
+
+               /* normal and fallback if haven't found anything to focus on */
+               if (winfocus == NULL) {
+                       winfocus = TAILQ_PREV(win, ws_win_list, entry);
+                       if (TAILQ_FIRST(&ws->winlist) == win)
+                               winfocus = TAILQ_NEXT(win, entry);
+                       else {
+                               winfocus = TAILQ_PREV(ws->focus, ws_win_list,
+                                   entry);
+                               if (winfocus == NULL)
+                                       winfocus = TAILQ_LAST(&ws->winlist,
+                                           ws_win_list);
+                       }
                }
 
                /* trash window and refocus */