JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
somehow if( and for( remained in the code borrowed from dwm. Kill that dead.
[spectrwm.git] / scrotwm.c
index b8dca30..7a13f55 100644 (file)
--- a/scrotwm.c
+++ b/scrotwm.c
 #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)
-#define        SWM_D_EVENT             0x0001
-#define        SWM_D_WS                0x0002
-#define        SWM_D_FOCUS             0x0004
-#define        SWM_D_MISC              0x0008
+#define        SWM_D_MISC              0x0001
+#define        SWM_D_EVENT             0x0002
+#define        SWM_D_WS                0x0004
+#define        SWM_D_FOCUS             0x0008
+#define        SWM_D_MOVE              0x0010
 
-uint32_t               swm_debug = 0
+u_int32_t              swm_debug = 0
+                           | SWM_D_MISC
                            | SWM_D_EVENT
                            | SWM_D_WS
                            | SWM_D_FOCUS
-                           | SWM_D_MISC
+                           | SWM_D_MOVE
                            ;
 #else
 #define DPRINTF(x...)
@@ -98,10 +100,11 @@ uint32_t           swm_debug = 0
 #define MODKEY                 Mod1Mask
 #define CLEANMASK(mask)         (mask & ~(numlockmask | LockMask))
 
+char                   **start_argv;
+Atom                   astate;
 int                    (*xerrorxlib)(Display *, XErrorEvent *);
 int                    other_wm;
 int                    screen;
-int                    width, height;
 int                    running = 1;
 int                    ignore_enter = 0;
 unsigned int           numlockmask = 0;
@@ -131,24 +134,59 @@ char                      *bar_fonts[] = {
 char                           *spawn_term[] = { "xterm", NULL };
 char                           *spawn_menu[] = { "dmenu_run", NULL };
 
+/* layout manager data */
+struct swm_geometry {
+       int                     x;
+       int                     y;
+       int                     w;
+       int                     h;
+};
+
+
 struct ws_win {
        TAILQ_ENTRY(ws_win)     entry;
        Window                  id;
-       int                     x;
-       int                     y;
-       int                     width;
-       int                     height;
+       struct swm_geometry     g;
        int                     floating;
        int                     transient;
+       XWindowAttributes       wa;
+       XSizeHints              sh;
 };
-
 TAILQ_HEAD(ws_win_list, ws_win);
 
+/* layout handlers */
+void   stack(void);
+void   vertical_init(int);
+void   vertical_resize(int);
+void   vertical_stack(struct swm_geometry *);
+void   horizontal_init(int);
+void   horizontal_resize(int);
+void   horizontal_stack(struct swm_geometry *);
+void   max_init(int);
+void   max_focus(struct ws_win *);
+void   max_stack(struct swm_geometry *);
+
+struct layout {
+       void                    (*l_init)(int); /* init/reset */
+       void                    (*l_stack)(struct swm_geometry *);
+       void                    (*l_resize)(int);
+       void                    (*l_focus)(struct ws_win *);
+} layouts[] =  {
+       /* init                 stack,                  resize */
+       { vertical_init,        vertical_stack,         vertical_resize,        NULL},
+       { horizontal_init,      horizontal_stack,       horizontal_resize,      NULL},
+       { NULL,                 max_stack,              NULL,                   max_focus},
+       { NULL,                 NULL,                   NULL,                   NULL},
+};
+
+
 /* define work spaces */
 #define SWM_WS_MAX             (10)
 struct workspace {
        int                     visible;        /* workspace visible */
        int                     restack;        /* restack on switch */
+       struct swm_geometry     g;
+       struct layout           *cur_layout;    /* current layout handlers */
        struct ws_win           *focus;         /* which win has focus */
        struct ws_win_list      winlist;        /* list of windows in ws */
 } ws[SWM_WS_MAX];
@@ -160,12 +198,15 @@ union arg {
 #define SWM_ARG_ID_FOCUSNEXT   (0)
 #define SWM_ARG_ID_FOCUSPREV   (1)
 #define SWM_ARG_ID_FOCUSMAIN   (2)
+#define SWM_ARG_ID_SWAPNEXT    (3)
+#define SWM_ARG_ID_SWAPPREV    (4)
+#define SWM_ARG_ID_SWAPMAIN    (5)
+#define SWM_ARG_ID_MASTERSHRINK (6)
+#define SWM_ARG_ID_MASTERGROW  (7)
        char                    **argv;
 };
 
-
-void   stack(void);
-
+/* conf file stuff */
 #define        SWM_CONF_WS     "\n= \t"
 #define SWM_CONF_FILE  "scrotwm.conf"
 int
@@ -288,12 +329,10 @@ bar_toggle(union arg *args)
 
        if (bar_enabled) {
                bar_enabled = 0;
-               height += bar_height; /* correct screen height */
                XUnmapWindow(display, bar_window);
        } else {
                bar_enabled = 1;
-               height -= bar_height; /* correct screen height */
-               XMapWindow(display, bar_window);
+               XMapRaised(display, bar_window);
        }
        XSync(display, False);
        for (i = 0; i < SWM_WS_MAX; i++)
@@ -317,14 +356,13 @@ bar_setup(void)
                        errx(1, "couldn't load font");
        bar_height = bar_fs->ascent + bar_fs->descent + 3;
 
-       bar_window = XCreateSimpleWindow(display, root, 0, 0, width,
-           bar_height - 2, 1, bar_border, bar_color);
+       bar_window = XCreateSimpleWindow(display, root, 0, 0,
+           ws[current_ws].g.w, bar_height - 2, 1, bar_border, bar_color);
        bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv);
        XSetFont(display, bar_gc, bar_fs->fid);
        XSelectInput(display, bar_window, VisibilityChangeMask);
        if (bar_enabled) {
-               height -= bar_height; /* correct screen height */
-               XMapWindow(display, bar_window);
+               XMapRaised(display, bar_window);
        }
        DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %d\n", (int)bar_window);
 
@@ -333,6 +371,27 @@ bar_setup(void)
        bar_print();
 }
 
+void
+config_win(struct ws_win *win)
+{
+       XConfigureEvent         ce;
+
+       DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
+           win->id, win->g.x, win->g.y, win->g.w, win->g.h);
+       ce.type = ConfigureNotify;
+       ce.display = display;
+       ce.event = win->id;
+       ce.window = win->id;
+       ce.x = win->g.x;
+       ce.y = win->g.y;
+       ce.width = win->g.w;
+       ce.height = win->g.h;
+       ce.border_width = 1; /* XXX store this! */
+       ce.above = None;
+       ce.override_redirect = False;
+       XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
+}
+
 int
 count_win(int wsid, int count_transient)
 {
@@ -340,6 +399,8 @@ count_win(int wsid, int count_transient)
        int                     count = 0;
 
        TAILQ_FOREACH (win, &ws[wsid].winlist, entry) {
+               if (count_transient == 0 && win->floating)
+                       continue;
                if (count_transient == 0 && win->transient)
                        continue;
                count++;
@@ -356,6 +417,18 @@ quit(union arg *args)
 }
 
 void
+restart(union arg *args)
+{
+       DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
+
+       XCloseDisplay(display);
+       execvp(start_argv[0], start_argv);
+       fprintf(stderr, "execvp failed\n");
+       perror(" failed");
+       quit(NULL);
+}
+
+void
 spawn(union arg *args)
 {
        DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
@@ -363,9 +436,9 @@ spawn(union arg *args)
         * The double-fork construct avoids zombie processes and keeps the code
         * clean from stupid signal handlers.
         */
-       if(fork() == 0) {
-               if(fork() == 0) {
-                       if(display)
+       if (fork() == 0) {
+               if (fork() == 0) {
+                       if (display)
                                close(ConnectionNumber(display));
                        setsid();
                        execvp(args->argv[0], args->argv);
@@ -429,122 +502,419 @@ switchws(union arg *args)
 }
 
 void
+swapwin(union arg *args)
+{
+       struct ws_win           *target;
+
+       DNPRINTF(SWM_D_MOVE, "swapwin: id %d\n", args->id);
+       if (ws[current_ws].focus == NULL)
+               return;
+
+       switch (args->id) {
+       case SWM_ARG_ID_SWAPPREV:
+               target = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
+               TAILQ_REMOVE(&ws[current_ws].winlist,
+                   ws[current_ws].focus, entry);
+               if (target == NULL)
+                       TAILQ_INSERT_TAIL(&ws[current_ws].winlist,
+                           ws[current_ws].focus, entry);
+               else
+                       TAILQ_INSERT_BEFORE(target, ws[current_ws].focus,
+                           entry);
+               break;
+       case SWM_ARG_ID_SWAPNEXT: 
+               target = TAILQ_NEXT(ws[current_ws].focus, entry);
+               TAILQ_REMOVE(&ws[current_ws].winlist,
+                   ws[current_ws].focus, entry);
+               if (target == NULL)
+                       TAILQ_INSERT_HEAD(&ws[current_ws].winlist,
+                           ws[current_ws].focus, entry);
+               else
+                       TAILQ_INSERT_AFTER(&ws[current_ws].winlist, target,
+                           ws[current_ws].focus, entry);
+               break;
+       case SWM_ARG_ID_SWAPMAIN:
+               target = TAILQ_FIRST(&ws[current_ws].winlist);
+               if (target == ws[current_ws].focus)
+                       return;
+               TAILQ_REMOVE(&ws[current_ws].winlist, target, entry);
+               TAILQ_INSERT_BEFORE(ws[current_ws].focus, target, entry);
+               TAILQ_REMOVE(&ws[current_ws].winlist,
+                   ws[current_ws].focus, entry);
+               TAILQ_INSERT_HEAD(&ws[current_ws].winlist,
+                   ws[current_ws].focus, entry);
+               break;
+       default:
+               DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
+               return;
+       }
+
+       ignore_enter = 2;
+       stack();
+}
+
+void
 focus(union arg *args)
 {
        struct ws_win           *winfocus, *winlostfocus;
 
        DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
-       if (ws[current_ws].focus == NULL || count_win(current_ws, 1) == 0)
+       if (ws[current_ws].focus == NULL)
                return;
 
        winlostfocus = ws[current_ws].focus;
 
        switch (args->id) {
        case SWM_ARG_ID_FOCUSPREV:
-               ws[current_ws].focus =
-                   TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
-               if (ws[current_ws].focus == NULL)
-                       ws[current_ws].focus =
+               winfocus = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
+               if (winfocus == NULL)
+                       winfocus =
                            TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
                break;
 
        case SWM_ARG_ID_FOCUSNEXT:
-               ws[current_ws].focus = TAILQ_NEXT(ws[current_ws].focus, entry);
-               if (ws[current_ws].focus == NULL)
-                       ws[current_ws].focus =
-                           TAILQ_FIRST(&ws[current_ws].winlist);
+               winfocus = TAILQ_NEXT(ws[current_ws].focus, entry);
+               if (winfocus == NULL)
+                       winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
                break;
 
        case SWM_ARG_ID_FOCUSMAIN:
-               ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
+               winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
                break;
 
        default:
                return;
        }
 
-       winfocus = ws[current_ws].focus;
+       if (winfocus == winlostfocus)
+               return;
+
        unfocus_win(winlostfocus);
        focus_win(winfocus);
+       /* XXX if we hook in focus_win(), we get a nasty cycle */
+       if (ws[current_ws].cur_layout->l_focus != NULL)
+               ws[current_ws].cur_layout->l_focus(winfocus);
        XSync(display, False);
 }
 
-/* I know this sucks but it works well enough */
 void
-stack(void)
+cycle_layout(union arg *args)
 {
-       XWindowChanges          wc;
-       struct ws_win           wf, *win, *winfocus = &wf;
-       int                     i, h, w, x, y, hrh, winno;
-       int floater = 0;
-       unsigned int mask;
+       DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", current_ws);
+
+       ws[current_ws].cur_layout++;
+       if (ws[current_ws].cur_layout->l_stack == NULL)
+               ws[current_ws].cur_layout = &layouts[0];
+       ignore_enter = 1;
+
+       stack();
+}
+
+void
+resize_master(union arg *args)
+{
+       DNPRINTF(SWM_D_EVENT, "resize_master: id %d\n", args->id);
+
+       if (ws[current_ws].cur_layout->l_resize != NULL)
+               ws[current_ws].cur_layout->l_resize(args->id);
+}
 
+void
+stack_reset(union arg *args)
+{
+       DNPRINTF(SWM_D_EVENT, "stack_reset: ws %d\n", current_ws);
+
+       if (ws[current_ws].cur_layout->l_init != NULL) {
+               ws[current_ws].cur_layout->l_init(current_ws);
+               stack();
+       }
+}
+
+void
+stack(void) {
+       struct swm_geometry g;
        DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
 
-       winfocus->id = root;
+       /* start with workspace geometry, adjust for bar */
+       g = ws[current_ws].g;
+       if (bar_enabled) {
+               g.y += bar_height;
+               g.h -= bar_height;
+       } 
 
        ws[current_ws].restack = 0;
+       ws[current_ws].cur_layout->l_stack(&g);
+       XSync(display, False);
+}
+
+void
+stack_floater(struct ws_win *win)
+{
+       unsigned int            mask;
+       XWindowChanges          wc;
+
+#if 0
+       bzero(&wc, sizeof wc);
+       wc.border_width = 1;
+       mask = CWX | CWY | CWBorderWidth;
+
+       win->g.w = wc.width = win->wa.width;
+       win->g.h = wc.height = win->wa.height;
+       win->g.x = wc.x = (ws[current_ws].g.w - win->wa.width) / 2;
+       win->g.y = wc.y = (ws[current_ws].g.h - win->wa.height) / 2;
+
+       DNPRINTF(SWM_D_EVENT, "stack_floater: win %d x %d y %d w %d h %d\n",
+           win, wc.x, wc.y, wc.width, wc.height);
+
+       XConfigureWindow(display, win->id, mask, &wc);
+return;
+#endif
+       bzero(&wc, sizeof wc);
+       wc.border_width = 1;
+       mask = CWX | CWY | CWBorderWidth;
+
+       /* use obsolete width height */
+       if (win->sh.flags & USPosition) {
+               win->g.w = wc.width = win->sh.width;
+               win->g.h = wc.height = win->sh.height;
+               mask |= CWWidth | CWHeight;
+       }
+
+       /* try min max */
+       if (win->sh.flags & PMinSize) {
+               /* some hints are retarded */
+               if (win->sh.min_width < ws[current_ws].g.w / 10)
+                       win->sh.min_width = ws[current_ws].g.w / 3;
+               if (win->sh.min_height < ws[current_ws].g.h / 10)
+                       win->sh.height = ws[current_ws].g.h / 3;
+
+               win->g.w = wc.width = win->sh.min_width * 2;
+               win->g.h = wc.height = win->sh.min_height * 2;
+               mask |= CWWidth | CWHeight;
+       }
+       if (win->sh.flags & PMaxSize) {
+               /* potentially override min values */
+               if (win->sh.max_width < ws[current_ws].g.w) {
+                       win->g.w = wc.width = win->sh.max_width;
+                       mask |= CWWidth;
+               }
+               if (win->sh.max_height < ws[current_ws].g.h) {
+                       win->g.h = wc.height = win->sh.max_height;
+                       mask |= CWHeight;
+               }
+       }
+
+       /* make sure we don't clobber the screen */
+       if ((mask & CWWidth) && win->wa.width > ws[current_ws].g.w)
+               win->wa.width = ws[current_ws].g.w - 4;
+       if ((mask & CWHeight) && win->wa.height > ws[current_ws].g.h)
+               win->wa.height = ws[current_ws].g.h - 4;
+
+       /* supposed to be obsolete */
+       if (win->sh.flags & USPosition) {
+               win->g.x = wc.x = win->sh.x;
+               win->g.y = wc.y = win->sh.y;
+       } else {
+               win->g.x = wc.x = (ws[current_ws].g.w - win->wa.width) / 2;
+               win->g.y = wc.y = (ws[current_ws].g.h - win->wa.height) / 2;
+       }
+       DNPRINTF(SWM_D_EVENT, "stack_floater: win %d x %d y %d w %d h %d\n",
+           win, wc.x, wc.y, wc.width, wc.height);
+
+       XConfigureWindow(display, win->id, mask, &wc);
+}
+
+
+int vertical_msize[SWM_WS_MAX];
+
+void
+vertical_init(int ws_idx)
+{
+       DNPRINTF(SWM_D_MISC, "vertical_init: workspace: %d\n", current_ws);
+
+       vertical_msize[ws_idx] = ws[ws_idx].g.w / 2;
+}
+
+void
+vertical_resize(int id)
+{
+       DNPRINTF(SWM_D_MISC, "vertical_resize: workspace: %d\n", current_ws);
+
+       switch (id) {
+       case SWM_ARG_ID_MASTERSHRINK:
+               vertical_msize[current_ws] -= ws[current_ws].g.w / 32;
+               if ( vertical_msize[current_ws] < ws[current_ws].g.w / 16)
+                       vertical_msize[current_ws] = ws[current_ws].g.w / 16;
+               break;
+       case SWM_ARG_ID_MASTERGROW:
+               vertical_msize[current_ws] += ws[current_ws].g.w / 32;
+               if ( vertical_msize[current_ws] >
+                  (ws[current_ws].g.w - (ws[current_ws].g.w / 16)))
+                       vertical_msize[current_ws] =
+                           ws[current_ws].g.w - ws[current_ws].g.w / 16;
+               break;
+       default:
+               return;
+       }
+       stack();
+}
+
+void
+vertical_stack(struct swm_geometry *g) {
+       XWindowChanges          wc;
+       struct swm_geometry     gg = *g;
+       struct ws_win           wf, *win, *winfocus = &wf;
+       int                     i, hrh, winno;
+       unsigned int            mask;
+
+       DNPRINTF(SWM_D_EVENT, "vertical_stack: workspace: %d\n", current_ws);
 
        winno = count_win(current_ws, 0);
        if (winno == 0)
                return;
+       winfocus->id = root;
 
        if (winno > 1)
-               w = width / 2;
-       else
-               w = width;
+               gg.w = vertical_msize[current_ws];
 
        if (winno > 2)
-               hrh = height / (winno - 1);
+               hrh = g->h / (winno - 1);
        else
                hrh = 0;
 
-       x = 0;
-       y = bar_enabled ? bar_height : 0;
-       h = height;
        i = 0;
        TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
                if (i == 1) {
-                       x += w + 2;
-                       w -= 2;
+                       gg.x += vertical_msize[current_ws] + 2;
+                       gg.w = g->w - (vertical_msize[current_ws] + 2);
                }
                if (i != 0 && hrh != 0) {
                        /* correct the last window for lost pixels */
                        if (win == TAILQ_LAST(&ws[current_ws].winlist,
                            ws_win_list)) {
-                               h = height - (i * hrh);
-                               if (h == 0)
-                                       h = hrh;
-                               else
-                                       h += hrh;
-                               y += hrh;
+                               gg.h = hrh + (g->h - (i * hrh));
+                               gg.y += hrh;
                        } else {
-                               h = hrh - 2;
+                               gg.h = hrh - 2;
                                /* leave first right hand window at y = 0 */
                                if (i > 1)
-                                       y += h + 2;
+                                       gg.y += gg.h + 2;
                        }
                }
 
                if (win->transient != 0 || win->floating != 0)
-                       floater = 1;
+                       stack_floater(win);
+               else {
+                       bzero(&wc, sizeof wc);
+                       wc.border_width = 1;
+                       win->g.x = wc.x = gg.x;
+                       win->g.y = wc.y = gg.y;
+                       win->g.w = wc.width = gg.w;
+                       win->g.h = wc.height = gg.h;
+                       mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
+                       XConfigureWindow(display, win->id, mask, &wc);
+               }
+
+               if (win == ws[current_ws].focus)
+                       winfocus = win;
                else
-                       floater = 0;
+                       unfocus_win(win);
+               XMapRaised(display, win->id);
+               i++;
+       }
 
-               bzero(&wc, sizeof wc);
-               wc.border_width = 1;
-               if (floater == 0) {
-                       win->x = wc.x = x;
-                       win->y = wc.y = y;
-                       win->width = wc.width = w;
-                       win->height = wc.height = h;
+       focus_win(winfocus); /* this has to be done outside of the loop */
+}
+
+int horizontal_msize[SWM_WS_MAX];
+
+void
+horizontal_init(int ws_idx)
+{
+       DNPRINTF(SWM_D_MISC, "horizontal_init: workspace: %d\n", current_ws);
+
+       horizontal_msize[ws_idx] = ws[ws_idx].g.h / 2;
+}
+
+void
+horizontal_resize(int id)
+{
+       DNPRINTF(SWM_D_MISC, "horizontal_resize: workspace: %d\n", current_ws);
+
+       switch (id) {
+       case SWM_ARG_ID_MASTERSHRINK:
+               horizontal_msize[current_ws] -= ws[current_ws].g.h / 32;
+               if ( horizontal_msize[current_ws] < ws[current_ws].g.h / 16)
+                       horizontal_msize[current_ws] = ws[current_ws].g.h / 16;
+               break;
+       case SWM_ARG_ID_MASTERGROW:
+               horizontal_msize[current_ws] += ws[current_ws].g.h / 32;
+               if ( horizontal_msize[current_ws] >
+                  (ws[current_ws].g.h - (ws[current_ws].g.h / 16)))
+                       horizontal_msize[current_ws] =
+                           ws[current_ws].g.h - ws[current_ws].g.h / 16;
+               break;
+       default:
+               return;
+       }
+       stack();
+}
+
+void
+horizontal_stack(struct swm_geometry *g) {
+       XWindowChanges          wc;
+       struct swm_geometry     gg = *g;
+       struct ws_win           wf, *win, *winfocus = &wf;
+       int                     i, hrw, winno;
+       unsigned int            mask;
+
+       DNPRINTF(SWM_D_EVENT, "horizontal_stack: workspace: %d\n", current_ws);
+
+       winno = count_win(current_ws, 0);
+       if (winno == 0)
+               return;
+       winfocus->id = root;
+
+       if (winno > 1)
+               gg.h = horizontal_msize[current_ws];
+
+       if (winno > 2)
+               hrw = g->w / (winno - 1);
+       else
+               hrw = 0;
+
+       i = 0;
+       TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
+               if (i == 1) {
+                       gg.y += horizontal_msize[current_ws] + 2;
+                       gg.h = g->h - (horizontal_msize[current_ws] + 2);
+               }
+               if (i != 0 && hrw != 0) {
+                       /* correct the last window for lost pixels */
+                       if (win == TAILQ_LAST(&ws[current_ws].winlist,
+                           ws_win_list)) {
+                               gg.w = hrw + (g->w - (i * hrw));
+                               gg.x += hrw;
+                       } else {
+                               gg.w = hrw - 2;
+                               /* leave first bottom window at x = 0 */
+                               if (i > 1)
+                                       gg.x += gg.w + 2;
+                       }
+               }
+
+               if (win->transient != 0 || win->floating != 0)
+                       stack_floater(win);
+               else {
+                       bzero(&wc, sizeof wc);
+                       wc.border_width = 1;
+                       win->g.x = wc.x = gg.x;
+                       win->g.y = wc.y = gg.y;
+                       win->g.w = wc.width = gg.w;
+                       win->g.h = wc.height = gg.h;
                        mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
-               } else {
-                       win->x = wc.x = width / 2;
-                       win->y = wc.y = height / 2;
-                       mask = CWX | CWY | CWBorderWidth;
+                       XConfigureWindow(display, win->id, mask, &wc);
                }
-               XConfigureWindow(display, win->id, mask, &wc);
 
                if (win == ws[current_ws].focus)
                        winfocus = win;
@@ -555,28 +925,62 @@ stack(void)
        }
 
        focus_win(winfocus); /* this has to be done outside of the loop */
-       XSync(display, False);
 }
 
 void
-swap_to_main(union arg *args)
+max_focus(struct ws_win *fwin)
 {
-       struct ws_win           *tmpwin = TAILQ_FIRST(&ws[current_ws].winlist);
+       struct ws_win *win;
+
+       TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
+               if (win->transient == 0 && win->floating == 0 && win == fwin)
+                       XMapRaised(display, win->id);
+               else
+                       XUnmapWindow(display, win->id);
+       }
+}
+
+/* fullscreen view */
+void
+max_stack(struct swm_geometry *g) {
+       XWindowChanges          wc;
+       struct swm_geometry     gg = *g;
+       struct ws_win           *win, *winfocus;
+       unsigned int            mask;
 
-       DNPRINTF(SWM_D_MISC, "swap_to_main: win: %lu\n",
-           ws[current_ws].focus ? ws[current_ws].focus->id : 0);
+       DNPRINTF(SWM_D_EVENT, "max_stack: workspace: %d\n", current_ws);
 
-       if (ws[current_ws].focus == NULL || ws[current_ws].focus == tmpwin)
+       if (count_win(current_ws, 0) == 0)
                return;
 
-       TAILQ_REMOVE(&ws[current_ws].winlist, tmpwin, entry);
-       TAILQ_INSERT_AFTER(&ws[current_ws].winlist, ws[current_ws].focus,
-           tmpwin, entry);
-       TAILQ_REMOVE(&ws[current_ws].winlist, ws[current_ws].focus, entry);
-       TAILQ_INSERT_HEAD(&ws[current_ws].winlist, ws[current_ws].focus, entry);
-       ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
-       ignore_enter = 2;
-       stack();
+       winfocus = ws[current_ws].focus;
+       if (!winfocus)
+               winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
+
+       TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
+               if (win->transient != 0 || win->floating != 0) {
+                       if (win == winfocus)
+                               stack_floater(win); /* XXX maximize? */
+                       else
+                               XUnmapWindow(display, win->id);
+               } else {
+                       bzero(&wc, sizeof wc);
+                       wc.border_width = 1;
+                       win->g.x = wc.x = gg.x;
+                       win->g.y = wc.y = gg.y;
+                       win->g.w = wc.width = gg.w;
+                       win->g.h = wc.height = gg.h;
+                       mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
+                       XConfigureWindow(display, win->id, mask, &wc);
+
+                       if (winfocus == win)
+                               XMapRaised(display, win->id);
+                       else
+                               XUnmapWindow(display, win->id);
+               }
+       }
+
+       focus_win(winfocus); /* this has to be done outside of the loop */
 }
 
 void
@@ -585,19 +989,21 @@ send_to_ws(union arg *args)
        int                     wsid = args->id;
        struct ws_win           *win = ws[current_ws].focus;
 
-       DNPRINTF(SWM_D_WS, "send_to_ws: win: %lu\n", win->id);
+       DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
 
        XUnmapWindow(display, win->id);
 
        /* find a window to focus */
-       ws[current_ws].focus = TAILQ_PREV(win, ws_win_list,entry);
+       ws[current_ws].focus = TAILQ_PREV(win, ws_win_list, entry);
        if (ws[current_ws].focus == NULL)
                ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
+       if (ws[current_ws].focus == win)
+               ws[current_ws].focus = NULL;
 
        TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
 
        TAILQ_INSERT_TAIL(&ws[wsid].winlist, win, entry);
-       if (count_win(wsid, 1) == 0)
+       if (count_win(wsid, 1) == 1)
                ws[wsid].focus = win;
        ws[wsid].restack = 1;
 
@@ -612,10 +1018,19 @@ struct key {
        union arg               args;
 } keys[] = {
        /* modifier             key     function                argument */
-       { MODKEY,               XK_Return,      swap_to_main,   {0} },
+       { MODKEY,               XK_space,       cycle_layout,   {0} }, 
+       { MODKEY | ShiftMask,   XK_space,       stack_reset,    {0} }, 
+       { MODKEY,               XK_h,           resize_master,  {.id = SWM_ARG_ID_MASTERSHRINK} },
+       { MODKEY,               XK_l,           resize_master,  {.id = SWM_ARG_ID_MASTERGROW} },
+       { MODKEY,               XK_Return,      swapwin,        {.id = SWM_ARG_ID_SWAPMAIN} },
+       { MODKEY,               XK_j,           focus,          {.id = SWM_ARG_ID_FOCUSNEXT} },
+       { MODKEY,               XK_k,           focus,          {.id = SWM_ARG_ID_FOCUSPREV} },
+       { MODKEY | ShiftMask,   XK_j,           swapwin,        {.id = SWM_ARG_ID_SWAPNEXT} },
+       { MODKEY | ShiftMask,   XK_k,           swapwin,        {.id = SWM_ARG_ID_SWAPPREV} },
        { MODKEY | ShiftMask,   XK_Return,      spawn,          {.argv = spawn_term} },
        { MODKEY,               XK_p,           spawn,          {.argv = spawn_menu} },
        { MODKEY | ShiftMask,   XK_q,           quit,           {0} },
+       { MODKEY,               XK_q,           restart,        {0} },
        { MODKEY,               XK_m,           focus,          {.id = SWM_ARG_ID_FOCUSMAIN} },
        { MODKEY,               XK_1,           switchws,       {.id = 0} },
        { MODKEY,               XK_2,           switchws,       {.id = 1} },
@@ -672,9 +1087,9 @@ grabkeys(void)
        updatenumlockmask();
 
        XUngrabKey(display, AnyKey, AnyModifier, root);
-       for(i = 0; i < LENGTH(keys); i++) {
-               if((code = XKeysymToKeycode(display, keys[i].keysym)))
-                       for(j = 0; j < LENGTH(modifiers); j++)
+       for (i = 0; i < LENGTH(keys); i++) {
+               if ((code = XKeysymToKeycode(display, keys[i].keysym)))
+                       for (j = 0; j < LENGTH(modifiers); j++)
                                XGrabKey(display, code,
                                    keys[i].mod | modifiers[j], root,
                                    True, GrabModeAsync, GrabModeAsync);
@@ -696,8 +1111,8 @@ keypress(XEvent *e)
        DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
 
        keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
-       for(i = 0; i < LENGTH(keys); i++)
-               if(keysym == keys[i].keysym
+       for (i = 0; i < LENGTH(keys); i++)
+               if (keysym == keys[i].keysym
                   && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
                   && keys[i].func)
                        keys[i].func(&(keys[i].args));
@@ -735,49 +1150,97 @@ buttonpress(XEvent *e)
 }
 
 void
-configurerequest(XEvent *e)
+set_win_state(struct ws_win *win, long state)
+{
+       long                    data[] = {state, None};
+
+       DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
+
+       XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
+           (unsigned char *)data, 2);
+}
+
+struct ws_win *
+manage_window(Window id)
 {
-       XConfigureRequestEvent  *ev = &e->xconfigurerequest;
        Window                  trans;
        struct ws_win           *win;
-
-       DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window);
+       XClassHint              ch;
 
        TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
-               if (ev->window == win->id)
-                       return;
+               if (win->id == id)
+                       return (win);   /* already being managed */
        }
 
-       XSelectInput(display, ev->window, ButtonPressMask | EnterWindowMask |
-           FocusChangeMask | ExposureMask);
-
        if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
                errx(1, "calloc: failed to allocate memory for new window");
 
-       win->id = ev->window;
+       win->id = id;
        TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
        ws[current_ws].focus = win; /* make new win focused */
 
        XGetTransientForHint(display, win->id, &trans);
        if (trans) {
                win->transient = trans;
-               DNPRINTF(SWM_D_MISC, "configurerequest: win %u transient %u\n",
+               DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
                    (unsigned)win->id, win->transient);
        }
-#if 0
-       XClassHint ch = { 0 };
-       if(XGetClassHint(display, win->id, &ch)) {
-               fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name);
-               if (!strcmp(ch.res_class, "Gvim") && !strcmp(ch.res_name, "gvim")) {
-                       win->floating = 0;
+       XGetWindowAttributes(display, win->id, &win->wa);
+       XGetNormalHints(display, win->id, &win->sh);
+
+       /* XXX */
+       bzero(&ch, sizeof ch);
+       if (XGetClassHint(display, win->id, &ch)) {
+               /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
+               if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
+                       win->floating = 1;
                }
-               if(ch.res_class)
+               if (ch.res_class)
                        XFree(ch.res_class);
-               if(ch.res_name)
+               if (ch.res_name)
                        XFree(ch.res_name);
        }
-#endif
-       stack();
+
+       XSelectInput(display, id, ButtonPressMask | EnterWindowMask |
+           FocusChangeMask | ExposureMask);
+
+       set_win_state(win, NormalState);
+
+       return (win);
+}
+
+void
+configurerequest(XEvent *e)
+{
+       XConfigureRequestEvent  *ev = &e->xconfigurerequest;
+       struct ws_win           *win;
+       int                     new = 1;
+       XWindowChanges          wc;
+
+       TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
+               if (win->id == ev->window) {
+                       new = 0;
+                       break;
+               }
+       }
+
+       if (new) {
+               DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
+                   ev->window);
+               bzero(&wc, sizeof wc);
+               wc.x = ev->x;
+               wc.y = ev->y;
+               wc.width = ev->width;
+               wc.height = ev->height;
+               wc.border_width = ev->border_width;
+               wc.sibling = ev->above;
+               wc.stack_mode = ev->detail;
+               XConfigureWindow(display, ev->window, ev->value_mask, &wc);
+       } else {
+               DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
+                   ev->window);
+               config_win(win);
+       }
 }
 
 void
@@ -798,13 +1261,16 @@ destroynotify(XEvent *e)
        TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
                if (ev->window == win->id) {
                        /* find a window to focus */
-                       ws[current_ws].focus =
-                           TAILQ_PREV(win, ws_win_list,entry);
+                       ws[current_ws].focus = TAILQ_PREV(win,
+                           ws_win_list, entry);
                        if (ws[current_ws].focus == NULL)
                                ws[current_ws].focus =
                                    TAILQ_FIRST(&ws[current_ws].winlist);
+                       if (win == ws[current_ws].focus)
+                               ws[current_ws].focus = NULL;
        
                        TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
+                       set_win_state(win, WithdrawnState);
                        free(win);
                        break;
                }
@@ -821,7 +1287,7 @@ enternotify(XEvent *e)
 
        DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
 
-       if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
+       if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
            ev->window != root)
                return;
        if (ignore_enter) {
@@ -844,8 +1310,6 @@ focusin(XEvent *e)
 
        DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window);
 
-       XSync(display, False); /* not sure this helps redrawing graphic apps */
-
        if (ev->window == root)
                return;
        /*
@@ -867,15 +1331,25 @@ mappingnotify(XEvent *e)
        DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
 
        XRefreshKeyboardMapping(ev);
-       if(ev->request == MappingKeyboard)
+       if (ev->request == MappingKeyboard)
                grabkeys();
 }
 
 void
 maprequest(XEvent *e)
 {
+       XMapRequestEvent        *ev = &e->xmaprequest;
+       XWindowAttributes       wa;
+
        DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
            e->xmaprequest.window);
+
+       if (!XGetWindowAttributes(display, ev->window, &wa))
+               return;
+       if (wa.override_redirect)
+               return;
+       manage_window(e->xmaprequest.window);
+       stack();
 }
 
 void
@@ -927,8 +1401,7 @@ xerror_start(Display *d, XErrorEvent *ee)
 int
 xerror(Display *d, XErrorEvent *ee)
 {
-       fprintf(stderr, "error: %p %p\n", display, ee);
-
+       /* fprintf(stderr, "error: %p %p\n", display, ee); */
        return (-1);
 }
 
@@ -942,7 +1415,7 @@ active_wm(void)
        XSelectInput(display, DefaultRootWindow(display),
            SubstructureRedirectMask);
        XSync(display, False);
-       if(other_wm)
+       if (other_wm)
                return (1);
 
        XSetErrorHandler(xerror);
@@ -950,6 +1423,26 @@ active_wm(void)
        return (0);
 }
 
+long
+getstate(Window w)
+{
+       int                     format, status;
+       long                    result = -1;
+       unsigned char           *p = NULL;
+       unsigned long           n, extra;
+       Atom                    real;
+
+       astate = XInternAtom(display, "WM_STATE", False);
+       status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
+           &real, &format, &n, &extra, (unsigned char **)&p);
+       if (status != Success)
+               return (-1);
+       if (n != 0)
+               result = *p;
+       XFree(p);
+       return (result);
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -957,13 +1450,16 @@ main(int argc, char *argv[])
        char                    conf[PATH_MAX], *cfile = NULL;
        struct stat             sb;
        XEvent                  e;
-       int                     i;
+       unsigned int            i, j, num;
+       Window                  d1, d2, *wins = NULL;
+       XWindowAttributes       wa;
 
+       start_argv = argv;
        fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
-       if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
+       if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
                warnx("no locale support");
 
-       if(!(display = XOpenDisplay(0)))
+       if (!(display = XOpenDisplay(0)))
                errx(1, "can not open display");
 
        if (active_wm())
@@ -971,8 +1467,7 @@ main(int argc, char *argv[])
 
        screen = DefaultScreen(display);
        root = RootWindow(display, screen);
-       width = DisplayWidth(display, screen) - 2;
-       height = DisplayHeight(display, screen) - 2;
+       astate = XInternAtom(display, "WM_STATE", False);
 
        /* look for local and global conf file */
        pwd = getpwuid(getuid());
@@ -993,17 +1488,50 @@ main(int argc, char *argv[])
        if (cfile)
                conf_load(cfile);
 
-       /* make work space 1 active */
-       ws[0].visible = 1;
-       ws[0].restack = 0;
-       ws[0].focus = NULL;
-       TAILQ_INIT(&ws[0].winlist);
-       for (i = 1; i < SWM_WS_MAX; i++) {
+       /* init all workspaces */
+       for (i = 0; i < SWM_WS_MAX; i++) {
                ws[i].visible = 0;
-               ws[i].restack = 0;
+               ws[i].restack = 1;
                ws[i].focus = NULL;
+               ws[i].g.x = 0;
+               ws[i].g.y = 0;
+               ws[i].g.w = DisplayWidth(display, screen) - 2;
+               ws[i].g.h = DisplayHeight(display, screen) - 2;
                TAILQ_INIT(&ws[i].winlist);
+
+               for (j = 0; layouts[j].l_stack != NULL; j++) {
+                       if (layouts[j].l_init != NULL)
+                               layouts[j].l_init(i);
+               }
+               ws[i].cur_layout = &layouts[0];
        }
+       /* make work space 1 active */
+       ws[0].visible = 1;
+
+       /* grab existing windows */
+       if (XQueryTree(display, root, &d1, &d2, &wins, &num)) {
+               /* normal windows */
+               for (i = 0; i < num; i++) {
+                        XGetWindowAttributes(display, wins[i], &wa);
+                       if (!XGetWindowAttributes(display, wins[i], &wa) ||
+                           wa.override_redirect || XGetTransientForHint(display, wins[i], &d1))
+                               continue;
+                       if (wa.map_state == IsViewable || getstate(wins[i]) == NormalState)
+                               manage_window(wins[i]);
+               }
+               /* transient windows */
+               for (i = 0; i < num; i++) {
+                       if (!XGetWindowAttributes(display, wins[i], &wa))
+                               continue;
+                       if (XGetTransientForHint(display, wins[i], &d1) &&
+                           (wa.map_state == IsViewable || getstate(wins[i]) ==
+                           NormalState))
+                               manage_window(wins[i]);
+                }
+                if (wins)
+                        XFree(wins);
+        }
+       ws[0].focus = TAILQ_FIRST(&ws[0].winlist);
 
        /* setup status bar */
        bar_setup();
@@ -1014,6 +1542,7 @@ main(int argc, char *argv[])
            FocusChangeMask | PropertyChangeMask | ExposureMask);
 
        grabkeys();
+       stack();
 
        while (running) {
                XNextEvent(display, &e);