JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Allow resizing (alt-h / alt-l) the master area and resetting (alt-shift-space)
[spectrwm.git] / scrotwm.c
index 9ced019..eefbd2a 100644 (file)
--- a/scrotwm.c
+++ b/scrotwm.c
@@ -103,7 +103,6 @@ u_int32_t           swm_debug = 0
 int                    (*xerrorxlib)(Display *, XErrorEvent *);
 int                    other_wm;
 int                    screen;
-int                    width, height;
 int                    running = 1;
 int                    ignore_enter = 0;
 unsigned int           numlockmask = 0;
@@ -135,23 +134,33 @@ char                              *spawn_menu[] = { "dmenu_run", NULL };
 char                           *spawn_scrotwm[] = { "scrotwm", NULL };
 
 /* layout manager data */
+struct swm_geometry {
+       int                     x;
+       int                     y;
+       int                     w;
+       int                     h;
+};
+
 void   stack(void);
-void   vertical_init(void);
-void   vertical_stack(void);
-void   horizontal_init(void);
-void   horizontal_stack(void);
-void   max_init(void);
-void   max_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_stack(struct swm_geometry *);
 
 struct layout {
-       void                    (*l_init)(void);        /* init/reset */
-       void                    (*l_stack)(void);       /* restack windows */
+       void                    (*l_init)(int); /* init/reset */
+       void                    (*l_stack)(struct swm_geometry *);
+       void                    (*l_resize)(int);
 } layouts[] =  {
-       /* init                 stack */
-       { vertical_init,        vertical_stack},
-       { horizontal_init,      horizontal_stack},
+       /* init                 stack,                  resize*/
+       { vertical_init,        vertical_stack,         vertical_resize},
+       { horizontal_init,      horizontal_stack,       horizontal_resize},
        /* XXX not working yet
-        * { max_init,          max_stack},
+        * { max_init,          max_stack,              NULL},
         */
        { NULL,                 NULL},
 };
@@ -159,13 +168,11 @@ struct layout {
 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);
@@ -175,6 +182,7 @@ TAILQ_HEAD(ws_win_list, ws_win);
 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 */
@@ -190,11 +198,12 @@ union arg {
 #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;
 };
 
-
-
+/* conf file stuff */
 #define        SWM_CONF_WS     "\n= \t"
 #define SWM_CONF_FILE  "scrotwm.conf"
 int
@@ -317,11 +326,9 @@ 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);
        }
        XSync(display, False);
@@ -346,13 +353,12 @@ 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);
        }
        DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %d\n", (int)bar_window);
@@ -387,7 +393,7 @@ quit(union arg *args)
 
 void
 restart(union arg *args)
-{      
+{
        XCloseDisplay(display);
        execvp(args->argv[0], args->argv);
        fprintf(stderr, "execvp failed\n");
@@ -573,30 +579,146 @@ cycle_layout(union arg *args)
 }
 
 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);
 
+       /* 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);
+}
 
-       ws[current_ws].cur_layout->l_stack();
+void
+stack_floater(struct ws_win *win)
+{
+       unsigned int            mask;
+       XWindowChanges          wc;
 
-       XSync(display, False);
+       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->wa.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_init(void)
+vertical_resize(int id)
 {
-       DNPRINTF(SWM_D_EVENT, "vertical_init: workspace: %d\n", current_ws);
+       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();
 }
 
 /* I know this sucks but it works well enough */
 void
-vertical_stack(void) {
+vertical_stack(struct swm_geometry *g) {
        XWindowChanges          wc;
+       struct swm_geometry     gg = *g;
        struct ws_win           wf, *win, *winfocus = &wf;
-       int                     i, h, w, x, y, hrh, winno;
-       int floater = 0;
-       unsigned int mask;
+       int                     i, hrh, winno;
+       unsigned int            mask;
 
        DNPRINTF(SWM_D_EVENT, "vertical_stack: workspace: %d\n", current_ws);
 
@@ -607,66 +729,45 @@ vertical_stack(void) {
                return;
 
        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;
-               else
-                       floater = 0;
-
-               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;
+                       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 {
-                       /* make sure we don't clobber the screen */
-                       if (win->wa.width > width)
-                               win->wa.width = width;
-                       if (win->wa.height > height)
-                               win->wa.width = height;
-                       win->x = wc.x = (width - win->wa.width) / 2;
-                       win->y = wc.y = (height - win->wa.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;
@@ -679,19 +780,47 @@ vertical_stack(void) {
        focus_win(winfocus); /* this has to be done outside of the loop */
 }
 
+int horizontal_msize[SWM_WS_MAX];
+
 void
-horizontal_init(void)
+horizontal_init(int ws_idx)
 {
-       DNPRINTF(SWM_D_EVENT, "horizontal_init: workspace: %d\n", current_ws);
+       DNPRINTF(SWM_D_MISC, "horizontal_init: workspace: %d\n", current_ws);
+
+       horizontal_msize[ws_idx] = ws[ws_idx].g.h / 2;
 }
 
 void
-horizontal_stack(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, h, w, x, y, hrw, winno;
-       int floater = 0;
-       unsigned int mask;
+       int                     i, hrw, winno;
+       unsigned int            mask;
 
        DNPRINTF(SWM_D_EVENT, "horizontal_stack: workspace: %d\n", current_ws);
 
@@ -702,66 +831,45 @@ horizontal_stack(void) {
                return;
 
        if (winno > 1)
-               h = height / 2;
-       else
-               h = height;
+               gg.h = horizontal_msize[current_ws];
 
        if (winno > 2)
-               hrw = width / (winno - 1);
+               hrw = g->w / (winno - 1);
        else
                hrw = 0;
 
-       x = 0;
-       y = bar_enabled ? bar_height : 0;
-       w = width;
        i = 0;
        TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
                if (i == 1) {
-                       y += h + 2;
-                       h -= 2;
+                       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)) {
-                               w = width - (i * hrw);
-                               if (w == 0)
-                                       w = hrw;
-                               else
-                                       w += hrw;
-                               x += hrw;
+                               gg.w = hrw + (g->w - (i * hrw));
+                               gg.x += hrw;
                        } else {
-                               w = hrw - 2;
+                               gg.w = hrw - 2;
                                /* leave first bottom window at x = 0 */
                                if (i > 1)
-                                       x += w + 2;
+                                       gg.x += gg.w + 2;
                        }
                }
 
                if (win->transient != 0 || win->floating != 0)
-                       floater = 1;
-               else
-                       floater = 0;
-
-               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;
+                       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 {
-                       /* make sure we don't clobber the screen */
-                       if (win->wa.width > width)
-                               win->wa.width = width;
-                       if (win->wa.height > height)
-                               win->wa.width = height;
-                       win->x = wc.x = (width - win->wa.width) / 2;
-                       win->y = wc.y = (height - win->wa.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;
@@ -776,16 +884,17 @@ horizontal_stack(void) {
 
 /* fullscreen view */
 void
-max_init(void)
+max_init(int ws_idx)
 {
-       DNPRINTF(SWM_D_EVENT, "max_init: workspace: %d\n", current_ws);
+       DNPRINTF(SWM_D_EVENT, "max_init: workspace: %d\n", ws_idx);
 }
 
 void
-max_stack(void) {
+max_stack(struct swm_geometry *g) {
        XWindowChanges          wc;
+       struct swm_geometry     gg = *g;
        struct ws_win           wf, *win, *winfocus = &wf;
-       int                     i, h, w, x, y, winno;
+       int                     i, winno;
        unsigned int mask;
 
        DNPRINTF(SWM_D_EVENT, "max_stack: workspace: %d\n", current_ws);
@@ -796,21 +905,19 @@ max_stack(void) {
        if (winno == 0)
                return;
 
-       x = 0;
-       y = bar_enabled ? bar_height : 0;
        TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
                if (i == 1 && win->transient == 0 && win->floating != 0) {
-                       h = height - 2;
-                       w = width - 2;
+                       gg.h -= 2;
+                       gg.w -= 2;
 
                        winfocus = win;
 
                        bzero(&wc, sizeof wc);
                        wc.border_width = 1;
-                       win->x = wc.x = x;
-                       win->y = wc.y = y;
-                       win->width = wc.width = w;
-                       win->height = wc.height = h;
+                       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);
 
@@ -845,7 +952,7 @@ send_to_ws(union arg *args)
        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;
 
@@ -860,8 +967,10 @@ struct key {
        union arg               args;
 } keys[] = {
        /* modifier             key     function                argument */
-       /* XXX alt-c is temporary, until I figure out how to grab spacebar */
-       { MODKEY,               XK_c,           cycle_layout,   {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} },
@@ -1011,7 +1120,6 @@ manage_window(Window id)
        return win;
 }
 
-
 void
 configurerequest(XEvent *e)
 {
@@ -1032,6 +1140,7 @@ configurerequest(XEvent *e)
                    (unsigned)win->id, win->transient);
        }
        XGetWindowAttributes(display, win->id, &win->wa);
+       XGetNormalHints(display, win->id, &win->sh);
 #if 0
        XClassHint ch = { 0 };
        if(XGetClassHint(display, win->id, &ch)) {
@@ -1051,6 +1160,7 @@ configurerequest(XEvent *e)
 void
 configurenotify(XEvent *e)
 {
+
        DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
            e->xconfigure.window);
 }
@@ -1230,7 +1340,7 @@ main(int argc, char *argv[])
        char                    conf[PATH_MAX], *cfile = NULL;
        struct stat             sb;
        XEvent                  e;
-       unsigned int            i, num;
+       unsigned int            i, j, num;
        Window                  d1, d2, *wins = NULL;
        XWindowAttributes       wa;
 
@@ -1246,8 +1356,6 @@ main(int argc, char *argv[])
 
        screen = DefaultScreen(display);
        root = RootWindow(display, screen);
-       width = DisplayWidth(display, screen) - 2;
-       height = DisplayHeight(display, screen) - 2;
 
        /* look for local and global conf file */
        pwd = getpwuid(getuid());
@@ -1273,7 +1381,16 @@ main(int argc, char *argv[])
                ws[i].visible = 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 */