JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fix longstanding bug where fullscreen was off by one
[spectrwm.git] / scrotwm.c
index 9653403..aab81c0 100644 (file)
--- a/scrotwm.c
+++ b/scrotwm.c
@@ -100,7 +100,7 @@ static const char   *cvstag = "$scrotwm$";
 #endif
 #endif
 
-/* #define SWM_DEBUG */
+/*#define SWM_DEBUG*/
 #ifdef SWM_DEBUG
 #define DPRINTF(x...)          do { if (swm_debug) fprintf(stderr, x); } while (0)
 #define DNPRINTF(n,x...)       do { if (swm_debug & n) fprintf(stderr, x); } while (0)
@@ -403,9 +403,8 @@ union arg {
 };
 
 void   focus(struct swm_region *, union arg *);
-void   focus_magic(struct ws_win *, int);
-#define SWM_F_GENERIC          (0)
-#define SWM_F_TRANSIENT                (1)
+void   focus_magic(struct ws_win *);
+
 /* quirks */
 struct quirk {
        char                    *class;
@@ -657,7 +656,7 @@ ewmh_set_win_fullscreen(struct ws_win *win, int fs)
                win->g.y = rg.y;
                win->g.w = rg.w;
                win->g.h = rg.h;
-       }       else {
+       } else {
                if (win->g_floatvalid) {
                        /* refloat at last floating relative position */
                        win->g.x = win->g_float.x - win->rg_float.x + rg.x;
@@ -1899,6 +1898,7 @@ focus_win(struct ws_win *win)
                if (win->java == 0)
                        XSetInputFocus(display, win->id,
                            RevertToParent, CurrentTime);
+               XMapRaised(display, win->id);
                grabbuttons(win, 1);
                XSetWindowBorder(display, win->id,
                    win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
@@ -2187,7 +2187,7 @@ done:
        if (winfocus == winlostfocus || winfocus == NULL)
                return;
 
-       focus_magic(winfocus, SWM_F_GENERIC);
+       focus_magic(winfocus);
 }
 
 void
@@ -2214,7 +2214,7 @@ focus(struct swm_region *r, union arg *args)
                                if (winfocus->iconic == 0)
                                        break;
 
-               focus_magic(winfocus, SWM_F_GENERIC);
+               focus_magic(winfocus);
                return;
        }
 
@@ -2231,18 +2231,18 @@ focus(struct swm_region *r, union arg *args)
                if (head == NULL)
                        head = TAILQ_LAST(wl, ws_win_list);
                winfocus = head;
-               for (;;) {
-                       if (winfocus == NULL)
-                               break;
-                       if (!winfocus->iconic)
-                               break;
-                       winfocus = TAILQ_PREV(winfocus, ws_win_list, entry);
-                       if (winfocus == NULL)
-                               winfocus = TAILQ_LAST(wl, ws_win_list);
-                       if (winfocus == head) {
-                               winfocus = NULL;
-                               break;
-                       }
+               if (WINID(winfocus) == cur_focus->transient) {
+                       head = TAILQ_PREV(winfocus, ws_win_list, entry);
+                       if (head == NULL)
+                               head = TAILQ_LAST(wl, ws_win_list);
+                       winfocus = head;
+               }
+
+               /* skip iconics */
+               if (winfocus && winfocus->iconic) {
+                       TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
+                               if (winfocus->iconic == 0)
+                                       break;
                }
                break;
 
@@ -2251,18 +2251,12 @@ focus(struct swm_region *r, union arg *args)
                if (head == NULL)
                        head = TAILQ_FIRST(wl);
                winfocus = head;
-               for (;;) {
-                       if (winfocus == NULL)
-                               break;
-                       if (!winfocus->iconic)
-                               break;
-                       winfocus = TAILQ_NEXT(winfocus, entry);
-                       if (winfocus == NULL)
-                               winfocus = TAILQ_FIRST(wl);
-                       if (winfocus == head) {
-                               winfocus = NULL;
-                               break;
-                       }
+
+               /* skip iconics */
+               if (winfocus && winfocus->iconic) {
+                       TAILQ_FOREACH(winfocus, wl, entry)
+                               if (winfocus->iconic == 0)
+                                       break;
                }
                break;
 
@@ -2279,7 +2273,7 @@ focus(struct swm_region *r, union arg *args)
        if (winfocus == winlostfocus || winfocus == NULL)
                return;
 
-       focus_magic(winfocus, SWM_F_GENERIC);
+       focus_magic(winfocus);
 }
 
 void
@@ -2412,22 +2406,22 @@ stack_floater(struct ws_win *win, struct swm_region *r)
                 * floaters and transients are auto-centred unless moved
                 * or resized
                 */
-               win->g.x = r->g.x + (WIDTH(r) - win->g.w) / 2 - border_width;
-               win->g.y = r->g.y + (HEIGHT(r) - win->g.h) / 2 - border_width;
+               win->g.x = r->g.x + (WIDTH(r) - win->g.w) / 2 - wc.border_width;
+               win->g.y = r->g.y + (HEIGHT(r) - win->g.h) / 2 - wc.border_width;
        }
 
        /* win can be outside r if new r smaller than old r */
        /* Ensure top left corner inside r (move probs otherwise) */
-       if (win->g.x < r->g.x - border_width)
-               win->g.x = r->g.x - border_width;
+       if (win->g.x < r->g.x - wc.border_width)
+               win->g.x = r->g.x - wc.border_width;
        if (win->g.x > r->g.x + r->g.w - 1)
                win->g.x = (win->g.w > r->g.w) ? r->g.x :
-                   (r->g.x + r->g.w - win->g.w - 2 * border_width);
-       if (win->g.y < r->g.y - border_width)
-               win->g.y = r->g.y - border_width;
+                   (r->g.x + r->g.w - win->g.w - 2 * wc.border_width);
+       if (win->g.y < r->g.y - wc.border_width)
+               win->g.y = r->g.y - wc.border_width;
        if (win->g.y > r->g.y + r->g.h - 1)
                win->g.y = (win->g.h > r->g.h) ? r->g.y :
-                   (r->g.y + r->g.h - win->g.h - 2 * border_width);
+                   (r->g.y + r->g.h - win->g.h - 2 * wc.border_width);
 
        wc.x = win->g.x;
        wc.y = win->g.y;
@@ -2660,14 +2654,13 @@ stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
                j++;
        }
 
- notiles:
+notiles:
        /* now, stack all the floaters and transients */
        TAILQ_FOREACH(win, &ws->winlist, entry) {
                if (win->transient == 0 && win->floating == 0)
                        continue;
-               if (win->iconic == 0)
+               if (win->iconic == 1)
                        continue;
-
                if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
                        fs_win = win;
                        continue;
@@ -2841,7 +2834,7 @@ max_stack(struct workspace *ws, struct swm_geometry *g)
                if (parent)
                        XMapRaised(display, parent->id);
                stack_floater(wintrans, ws->r);
-               focus_magic(wintrans, SWM_F_TRANSIENT);
+               focus_magic(wintrans);
        }
 }
 
@@ -4576,9 +4569,12 @@ manage_window(Window id)
                DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
                    "%lu\n", win->id);
                TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
-               TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
                if (win->transient)
                        set_child_transient(win);
+               if (trans && (ww = find_window(trans)))
+                       TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
+               else
+                       TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
                ewmh_update_actions(win);
                return (win);
        }
@@ -4651,7 +4647,10 @@ manage_window(Window id)
        win->id = id;
        win->ws = ws;
        win->s = r->s;  /* this never changes */
-       TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
+       if (trans && (ww = find_window(trans)))
+               TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
+       else
+               TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
 
        win->g.w = win->wa.width;
        win->g.h = win->wa.height;
@@ -4793,8 +4792,7 @@ unmanage_window(struct ws_win *win)
        /* focus on root just in case */
        XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
 
-       if (!win->floating)
-               focus_prev(win);
+       focus_prev(win);
 
        TAILQ_REMOVE(&win->ws->winlist, win, entry);
        TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
@@ -4803,14 +4801,14 @@ unmanage_window(struct ws_win *win)
 }
 
 void
-focus_magic(struct ws_win *win, int do_trans)
+focus_magic(struct ws_win *win)
 {
-       DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu %d\n", WINID(win), do_trans);
+       DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu\n", WINID(win));
 
        if (win == NULL)
                return;
 
-       if (do_trans == SWM_F_TRANSIENT && win->child_trans) {
+       if (win->child_trans) {
                /* win = parent & has a transient so focus on that */
                if (win->java) {
                        focus_win(win->child_trans);
@@ -4884,7 +4882,7 @@ buttonpress(XEvent *e)
        if ((win = find_window(ev->window)) == NULL)
                return;
 
-       focus_magic(win, SWM_F_TRANSIENT);
+       focus_magic(win);
        action = client_click;
 
        for (i = 0; i < LENGTH(buttons); i++)
@@ -5079,7 +5077,7 @@ enternotify(XEvent *e)
                return;
        }
 
-       focus_magic(win, SWM_F_TRANSIENT);
+       focus_magic(win);
 }
 
 /* lets us use one switch statement for arbitrary mode/detail combinations */
@@ -5177,7 +5175,7 @@ maprequest(XEvent *e)
        /* make new win focused */
        r = root_to_region(win->wa.root);
        if (win->ws == r->ws)
-               focus_magic(win, SWM_F_GENERIC);
+               focus_magic(win);
 }
 
 void