]> Pileus Git - wmpus/blobdiff - wm-wmii.c
Add tag toggling
[wmpus] / wm-wmii.c
index 06a258fd49d954af42a94a16b3521fc2a7db9c4b..c2e078884edd283debbb4352f4173c55c4f62629 100644 (file)
--- a/wm-wmii.c
+++ b/wm-wmii.c
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "util.h"
 #include "conf.h"
+#include "types.h"
 #include "sys.h"
 #include "wm.h"
 
@@ -45,44 +47,43 @@ typedef enum {
 struct win_wm { };
 
 typedef struct {
-       win_t   *win;     // the window
-       int      height;  // win height in _this_ tag
-       state_t  state;   // state of window
+       win_t   *win;      // the window
+       int      height;   // win height in _this_ tag
+       state_t  state;    // state of window
 } row_t;
 
 typedef struct {
-       list_t  *rows;    // of row_t
-       row_t   *row;     // focused row
-       int      width;   // column width
-       layout_t layout;  // column layout
+       list_t  *rows;     // of row_t
+       row_t   *row;      // focused row
+       int      width;    // column width
+       layout_t layout;   // column layout
 } col_t;
 
 typedef struct {
-       win_t   *win;     // the window
-       int x, y, w, h;   // position of window (in this tag)
-       state_t  state;   // state of window
+       win_t   *win;      // the window
+       int x, y, w, h;    // position of window (in this tag)
+       state_t  state;    // state of window
 } flt_t;
 
 typedef struct {
-       list_t  *cols;    // of col_t
-       col_t   *col;     // focused col
-       list_t  *flts;    // of flt_t
-       flt_t   *flt;     // focused flt
-       layer_t  layer;   // focused layer
-       win_t   *geom;    // display size and position
+       list_t  *cols;     // of col_t
+       col_t   *col;      // focused col
+       list_t  *flts;     // of flt_t
+       flt_t   *flt;      // focused flt
+       layer_t  layer;    // focused layer
+       win_t   *geom;     // display size and position
 } dpy_t;
 
 typedef struct {
-       list_t  *dpys;    // of dpy_t
-       dpy_t   *dpy;     // focused dpy
-       int      name;    // tag name
+       list_t  *dpys;     // of dpy_t
+       dpy_t   *dpy;      // focused dpy
+       char     name[64]; // tag name
 } tag_t;
 
 typedef struct {
-       list_t  *tags;    // of tag_t
-       tag_t   *tag;     // focused tag
-       win_t   *root;    // root/background window
-       list_t  *screens; // display geometry
+       list_t  *tags;     // of tag_t
+       tag_t   *tag;      // focused tag
+       list_t  *screens;  // display geometry
 } wm_t;
 
 #define WIN(node) ((win_t*)(node)->data)
@@ -101,7 +102,7 @@ typedef struct {
 #define tag_foreach_flt(tag, dpy, flt, win) \
        for (list_t *dpy =     tag ->dpys; dpy; dpy = dpy->next) \
        for (list_t *flt = DPY(dpy)->flts; flt; flt = flt->next) \
-       for (win_t  *win = FLT(flt)->win;  win; win = NULL)      \
+       for (win_t  *win = FLT(flt)->win;  win; win = NULL)
 
 /* Window management data
  *   wm_* macros represent the currently focused item
@@ -123,6 +124,9 @@ static ptr_t   move_prev;
 static layer_t move_layer;
 static struct { int v, h; } move_dir;
 
+/* Prototypes */
+void wm_update(void);
+
 /********************
  * Helper functions *
  ********************/
@@ -147,6 +151,17 @@ static win_t *get_focus(void)
 
 /* Search for the target window in a given tag
  * win may exist in other tags as well */
+static int search(tag_t *tag, win_t *target)
+{
+       tag_foreach_col(tag, dpy, col, row, win)
+               if (target == win)
+                       return TILING;
+       tag_foreach_flt(tag, dpy, flt, win)
+               if (target == win)
+                       return FLOATING;
+       return -1;
+}
+
 static int searchl(tag_t *tag, win_t *target,
                list_t **_dpy, list_t **_col, list_t **_row, list_t **_flt)
 {
@@ -168,7 +183,7 @@ static int searchl(tag_t *tag, win_t *target,
        return -1;
 }
 
-static int search(tag_t *tag, win_t *target,
+static int searchw(tag_t *tag, win_t *target,
                dpy_t **_dpy, col_t **_col, row_t **_row, flt_t **_flt)
 {
        list_t *dpy, *col, *row, *flt;
@@ -190,7 +205,7 @@ static int search(tag_t *tag, win_t *target,
 static void set_mode(win_t *win, layout_t layout)
 {
        col_t *col;
-       if (TILING != search(wm_tag, win, NULL, &col, NULL, NULL))
+       if (TILING != searchw(wm_tag, win, NULL, &col, NULL, NULL))
                return;
        printf("set_mode: %p, %d -> %d\n",
                        col, col->layout, layout);
@@ -207,13 +222,13 @@ static void set_mode(win_t *win, layout_t layout)
  * it as the currently focused window */
 static void set_focus(win_t *win)
 {
-       if (win == NULL || win == wm->root) {
-               sys_focus(wm->root);
+       if (win == NULL) {
+               sys_focus(NULL);
                return;
        }
 
        dpy_t *dpy; col_t *col; row_t *row; flt_t *flt;
-       switch (search(wm_tag, win, &dpy, &col, &row, &flt)) {
+       switch (searchw(wm_tag, win, &dpy, &col, &row, &flt)) {
        case TILING:
                wm_dpy = dpy;
                wm_col = col;
@@ -254,7 +269,7 @@ static void print_txt(void)
 {
        for (list_t *ltag = wm->tags; ltag; ltag = ltag->next) {
                tag_t *tag = ltag->data;
-               printf("tag:       <%-9p [%p->%p] >%-9p d=%-9p - %d\n",
+               printf("tag:       <%-9p [%p->%p] >%-9p d=%-9p - %s\n",
                                ltag->prev, ltag, ltag->data, ltag->next,
                                tag->dpy, tag->name);
        for (list_t *ldpy = tag->dpys; ldpy; ldpy = ldpy->next) {
@@ -273,7 +288,7 @@ static void print_txt(void)
        for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
                row_t *row = lrow->data;
                win_t *win = row->win;
-               printf("      win: <%-9p [%p>>%p] >%-9p focus=%d%d    - %4dpx \n",
+               printf("      row: <%-9p [%p>>%p] >%-9p focus=%d%d    - %4dpx \n",
                                lrow->prev, lrow, win, lrow->next,
                                col->row == row, wm_focus == win, win->h);
        } }
@@ -326,7 +341,7 @@ static void put_win_col(win_t *win, tag_t *tag, dpy_t *dpy, col_t *col)
 {
        row_t *row = new0(row_t);
        row->win   = win;
-       row->state = win->state ?: ST_SHOW;
+       row->state = win->state;
 
        if (col == NULL) {
                col = new0(col_t);
@@ -498,7 +513,7 @@ static void shift_focus(int cols, int rows)
                if (ncol && COL(ncol) && COL(ncol)->row)
                        set_focus(COL(ncol)->row->win);
                else
-                       sys_focus(wm->root);
+                       sys_focus(NULL);
        }
 }
 
@@ -538,10 +553,10 @@ static void set_layer(win_t *win)
 }
 
 /* Allocate a new tag */
-static tag_t *tag_new(list_t *screens, int name)
+static tag_t *tag_new(list_t *screens, const char *name)
 {
        tag_t *tag = new0(tag_t);
-       tag->name  = name;
+       strncpy(tag->name, name, sizeof(tag->name));
        for (list_t *cur = screens; cur; cur = cur->next) {
                dpy_t *dpy  = new0(dpy_t);
                dpy->geom = cur->data;
@@ -557,50 +572,95 @@ static tag_t *tag_new(list_t *screens, int name)
 /* Search for a tag
  *   If it does not exist it is based on the
  *   display geometry in wm->screens */
-static tag_t *tag_find(int name)
+static tag_t *tag_find(const char *name, int create)
 {
        tag_t *tag = NULL;
        for (list_t *cur = wm->tags; cur; cur = cur->next)
-               if (name == TAG(cur)->name) {
+               if (!strcmp(name, TAG(cur)->name)) {
                        tag = cur->data;
                        break;
                }
-       if (!tag) {
+       if (!tag && create) {
                tag = tag_new(wm->screens, name);
                wm->tags = list_append(wm->tags, tag);
        }
        return tag;
 }
 
+/* Compute the number of windows in a tag */
+static int tag_size(tag_t *tag)
+{
+       int count = 0;
+       tag_foreach_col(tag, dpy, col, row, win)
+               count++;
+       tag_foreach_flt(tag, dpy, flt, win)
+               count++;
+       return count;
+}
+
+/* Remove a tag if it contains no windows */
+static void tag_prune(tag_t *tag)
+{
+       if (tag == wm_tag || tag_size(tag) >= 1)
+               return;
+       while (tag->dpys) {
+               dpy_t *dpy = tag->dpys->data;
+               while (dpy->cols)
+                       dpy->cols = list_remove(dpy->cols, dpy->cols, 1);
+               tag->dpys = list_remove(tag->dpys, tag->dpys, 1);
+       }
+       list_t *ltag = list_find(wm->tags, tag);
+       wm->tags = list_remove(wm->tags, ltag, 1);
+}
+
 /* Move the window from the current tag to the new tag
  *   Unlike wmii, only remove the current tag, not all tags */
-static void tag_set(win_t *win, int name)
+static void tag_set(win_t *win, const char *name, int toggle)
 {
-       printf("tag_set: %p %d\n", win, name);
-       if (wm_tag->name == name)
-               return;
-       tag_t *tag = tag_find(name);
-       layer_t layer = cut_win(win, wm_tag);
-       put_win(win, tag, layer);
-       set_focus(wm_focus);
+       tag_t *tag = tag_find(name, 1);
+
+       int src_layer = search(wm_tag, win);
+       int dst_layer = search(tag,    win);
+
+       int count = 0;
+       if (toggle && dst_layer >= 0)
+               for (list_t *tag = wm->tags; tag; tag = tag->next)
+                       if (search(TAG(tag), win) >= 0)
+                               count++;
+
+       printf("tag_set: %p %s -- %s layer=%d:%d count=%d\n",
+                       win, name, toggle ? "toggle" : "change",
+                       src_layer, dst_layer, count);
+
+       if (toggle) {
+               if (dst_layer < 0)
+                       put_win(win, tag, src_layer);
+               else if (count >= 2)
+                       cut_win(win, tag);
+               if (tag == wm_tag)
+                       wm_update();
+               else
+                       tag_prune(tag);
+       } else {
+               if (tag != wm_tag)
+                       cut_win(win, wm_tag);
+               if (dst_layer < 0)
+                       put_win(win, tag, src_layer);
+               if (tag != wm_tag)
+                       wm_update();
+       }
 }
 
 /* Switch to a different tag */
-static void tag_switch(int name)
-{
-       printf("tag_switch: %d\n", name);
-       tag_t *old = wm_tag;
-       if ((wm_col == NULL || wm_row == NULL) && wm_flt == NULL) {
-               while (old->dpys) {
-                       dpy_t *dpy = old->dpys->data;
-                       while (dpy->cols)
-                               dpy->cols = list_remove(dpy->cols, dpy->cols, 1);
-                       old->dpys = list_remove(old->dpys, old->dpys, 1);
-               }
-               list_t *ltag = list_find(wm->tags, old);
-               wm->tags = list_remove(wm->tags, ltag, 1);
-       }
-       wm_tag = tag_find(name);
+static void tag_switch(const char *name)
+{
+       printf("tag_switch: %s\n", name);
+       tag_t *tag = tag_find(name, 1);
+       if (tag == wm_tag)
+               return;
+       tag_prune(wm_tag);
+       wm_tag = tag;
+       wm_update();
 }
 
 /* Tile all windows in the given display
@@ -613,13 +673,15 @@ static void wm_update_cols(dpy_t *dpy)
        int mx=0, my=0; // Maximum usable size (screen size minus margins)
        int       sy=0; // Stack size (height of focused stack window)
 
+       float rx=0, ry=0; // Residuals for floating point round off
+
        /* Scale horizontally */
        x  = dpy->geom->x;
        mx = dpy->geom->w - (list_length(dpy->cols)+1)*margin;
        for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
                tx += COL(lcol)->width;
        for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
-               COL(lcol)->width *= (float)mx / tx;
+               COL(lcol)->width = residual(COL(lcol)->width * (float)mx/tx, &rx);
 
        /* Scale each column vertically */
        win_t *focus = get_focus();
@@ -641,13 +703,11 @@ static void wm_update_cols(dpy_t *dpy)
                        }
                        win->h = ROW(lrow)->height;
                        state_t state = ST_SHOW;
-                       int height = 0;
                        switch (col->layout) {
                        case SPLIT:
-                               sys_move(win, x+margin, y+margin,
-                                       col->width, win->h * ((float)my / ty));
-                               height = win->h;
-                               y += height + margin;
+                               sys_move(win, x+margin, y+margin, col->width,
+                                       residual(win->h * ((float)my/ty), &ry));
+                               y += win->h + margin;
                                break;
                        case STACK:
                                if (lrow->next && ROW(lrow->next)->win == col->row->win) {
@@ -678,9 +738,7 @@ static void wm_update_cols(dpy_t *dpy)
        }
 }
 
-/*******************************
- * Window management functions *
- *******************************/
+/* Refresh the window layout */
 void wm_update(void)
 {
        /* Updates window sizes */
@@ -697,8 +755,10 @@ void wm_update(void)
        for (list_t *tag = wm ->tags; tag; tag = tag->next)
                if (tag->data != wm_tag) {
                        tag_foreach_col(TAG(tag), dpy, col, row, win)
+                               if (search(wm_tag, win) < 0)
                                        sys_show(win, ST_HIDE);
                        tag_foreach_flt(TAG(tag), dpy, flt, win)
+                               if (search(wm_tag, win) < 0)
                                        sys_show(win, ST_HIDE);
                }
 
@@ -707,9 +767,11 @@ void wm_update(void)
                set_focus(wm_focus);
 }
 
+/*******************************
+ * Window management functions *
+ *******************************/
 int wm_handle_event(win_t *win, event_t ev, mod_t mod, ptr_t ptr)
 {
-       if (!win || win == wm_dpy->geom) return 0;
        //printf("wm_handle_event: %p - %x %c%c%c%c%c\n", win, ev,
        //      mod.up    ? '^' : 'v',
        //      mod.alt   ? 'a' : '-',
@@ -718,13 +780,14 @@ int wm_handle_event(win_t *win, event_t ev, mod_t mod, ptr_t ptr)
        //      mod.win   ? 'w' : '-');
 
        /* Mouse events */
-       if (EV_MOUSE0 <= ev && ev <= EV_MOUSE7) {
+       if (win && EV_MOUSE0 <= ev && ev <= EV_MOUSE7) {
                if (ev == EV_MOUSE1 && !mod.MODKEY && !mod.up)
                        return raise_float(win),         0;
+               if ((ev == EV_MOUSE3 && mod.MODKEY && !mod.up) ||
+                   (ev == EV_MOUSE1 && mod.MODKEY && !mod.up && mod.shift))
+                       return set_move(win,ptr,RESIZE), 1;
                if (ev == EV_MOUSE1 && mod.MODKEY && !mod.up)
                        return set_move(win,ptr,MOVE),   1;
-               if (ev == EV_MOUSE3 && mod.MODKEY && !mod.up)
-                       return set_move(win,ptr,RESIZE), 1;
                if (move_mode != NONE && mod.up)
                        return set_move(win,ptr,NONE),   1;
                if (ev == EV_MOUSE1 && !mod.up && win->state == ST_SHADE)
@@ -734,30 +797,36 @@ int wm_handle_event(win_t *win, event_t ev, mod_t mod, ptr_t ptr)
 
        /* Only handle key-down */
        if (mod.up)
-               return 0;
+               return mod.MODKEY || ev == EV_ALT;
 
        /* Misc */
-       if (mod.MODKEY) {
 #ifdef DEBUG
+       if (win && mod.MODKEY) {
                if (ev == EV_F1) return raise_float(win), 1;
                if (ev == EV_F2) return set_focus(win), 1;
                if (ev == EV_F3) return sys_show(win, ST_SHOW),  1;
                if (ev == EV_F4) return sys_show(win, ST_HIDE),  1;
                if (ev == EV_F7) return sys_show(win, ST_SHADE), 1;
+       }
 #endif
+       if (mod.MODKEY) {
                if (ev == EV_F5) return wm_update(),    1;
                if (ev == EV_F6) return print_txt(),    1;
                if (ev == 'q')   return sys_exit(),     1;
+       }
+       if (win && mod.MODKEY) {
                if (ev == 'f')   return wm_handle_state(win, win->state,
                        win->state == ST_FULL ? ST_SHOW : ST_FULL);
+               if (ev == 'g')   return wm_handle_state(win, win->state,
+                       win->state == ST_MAX  ? ST_SHOW : ST_MAX);
        }
-       if (mod.MODKEY && mod.shift) {
+       if (win && mod.MODKEY && mod.shift) {
                if (ev == 'c')   return sys_show(win, ST_CLOSE), 1;
        }
 
        /* Floating layer */
        if (ev == ' ') {
-               if (mod.MODKEY && mod.shift)
+               if (win && mod.MODKEY && mod.shift)
                        return set_layer(win), 1;
                if (mod.MODKEY)
                        return switch_layer(), 1;
@@ -796,25 +865,23 @@ int wm_handle_event(win_t *win, event_t ev, mod_t mod, ptr_t ptr)
 
        /* Tag switching */
        if (mod.MODKEY && '0' <= ev && ev <= '9') {
-               int name = ev - '0';
-               if (mod.shift)
-                       tag_set(win, name);
-               else
-                       tag_switch(name);
-               wm_update();
+               char name[] = {ev, '\0'};
+               if (!mod.shift && !mod.ctrl)        tag_switch(name);
+               if ( mod.shift && !mod.ctrl && win) tag_set(win, name, 0);
+               if (!mod.shift &&  mod.ctrl && win) tag_set(win, name, 1);
        }
 
        /* Focus change */
-       if (ev == EV_ENTER && win->state != ST_SHADE)
+       if (win && ev == EV_ENTER && win->state != ST_SHADE)
                return set_focus(win), 1;
 
        /* Reset focus after after focus change,
         * not sure what is causing the focus change in the first place
         * but preventing that would be a better solution */
        if (ev == EV_FOCUS)
-               sys_focus(wm_focus ?: wm->root);
+               sys_focus(wm_focus);
 
-       return 0;
+       return mod.MODKEY;
 }
 
 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
@@ -866,7 +933,14 @@ int wm_handle_state(win_t *win, state_t prev, state_t next)
        row_t *row = NULL;
        flt_t *flt = NULL;
 
-       search(wm_tag, win, NULL, NULL, &row, &flt);
+       printf("wm_handle_state - %p %x -> %x\n", win, prev, next);
+
+       searchw(wm_tag, win, NULL, NULL, &row, &flt);
+
+       if (!row && !flt && next == ST_SHOW)
+               return wm_insert(win), 1;
+       if ((row || flt) && (next == ST_HIDE || next == ST_ICON))
+               return wm_remove(win), 1;
 
        if (row) row->state = next;
        if (flt) flt->state = next;
@@ -881,14 +955,24 @@ int wm_handle_state(win_t *win, state_t prev, state_t next)
 void wm_insert(win_t *win)
 {
        printf("wm_insert: %p\n", win);
+
+       /* Make sure it's visible */
+       if (win->state == ST_HIDE)
+               return;
+
+       /* Check for toolbars */
+       if (win->type == TYPE_TOOLBAR)
+               return wm_update();
+
        print_txt();
 
        /* Initialize window */
-       win->wm = new0(win_wm_t);
        sys_watch(win, EV_ENTER, MOD());
        sys_watch(win, EV_FOCUS, MOD());
 
        /* Add to screen */
+       if (win->type == TYPE_DIALOG || win->parent)
+               wm_dpy->layer = FLOATING;
        put_win(win, wm_tag, wm_dpy->layer);
 
        /* Arrange */
@@ -901,45 +985,49 @@ void wm_remove(win_t *win)
 {
        printf("wm_remove: %p\n", win);
        print_txt();
+       if (win->type == TYPE_TOOLBAR)
+               return wm_update();
        for (list_t *tag = wm->tags; tag; tag = tag->next)
                cut_win(win, tag->data);
-       free(win->wm);
        set_focus(wm_focus);
        wm_update();
        print_txt();
 }
 
-void wm_init(win_t *root)
+void wm_init(void)
 {
-       printf("wm_init: %p\n", root);
+       printf("wm_init\n");
 
        /* Load configuration */
        margin = conf_get_int("main.margin", margin);
        stack  = conf_get_int("main.stack",  stack);
 
        wm          = new0(wm_t);
-       wm->root    = root;
-       wm->screens = list_sort(sys_info(root), 0, sort_win);
-       wm->tag     = tag_new(wm->screens, 1);
+       wm->screens = list_sort(sys_info(), 0, sort_win);
+       wm->tag     = tag_new(wm->screens, "1");
        wm->tags    = list_insert(NULL, wm->tag);
 
        event_t ev_e[] = {EV_ENTER, EV_FOCUS};
+       event_t ev_c[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        event_t ev_s[] = {'h', 'j', 'k', 'l', 'c', 'q', ' ',
-               '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
-       event_t ev_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't', 'f', ' ',
+               '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+               EV_MOUSE1, EV_MOUSE3};
+       event_t ev_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't', 'f', 'g', ' ',
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                EV_F1, EV_F2, EV_F3, EV_F4,  EV_F5,  EV_F6,
                EV_F7, EV_F8, EV_F9, EV_F10, EV_F11, EV_F12,
                EV_MOUSE1, EV_MOUSE3};
        for (int i = 0; i < countof(ev_e); i++)
-               sys_watch(root, ev_e[i],  MOD());
+               sys_watch(NULL, ev_e[i],  MOD());
        for (int i = 0; i < countof(ev_m); i++)
-               sys_watch(root, ev_m[i], MOD(.MODKEY=1));
+               sys_watch(NULL, ev_m[i], MOD(.MODKEY=1));
+       for (int i = 0; i < countof(ev_c); i++)
+               sys_watch(NULL, ev_c[i], MOD(.MODKEY=1,.ctrl=1));
        for (int i = 0; i < countof(ev_s); i++)
-               sys_watch(root, ev_s[i], MOD(.MODKEY=1,.shift=1));
+               sys_watch(NULL, ev_s[i], MOD(.MODKEY=1,.shift=1));
 }
 
-void wm_free(win_t *root)
+void wm_free(void)
 {
        /* Re-show and free all windows */
        while ( wm->tags) { tag_t *tag =  wm->tags->data;
@@ -947,12 +1035,10 @@ void wm_free(win_t *root)
        while (dpy->cols) { col_t *col = dpy->cols->data;
        while (col->rows) { row_t *row = col->rows->data;
                sys_show(row->win, ST_SHOW);
-               free(row->win->wm);
        col->rows = list_remove(col->rows, col->rows, 1); }
        dpy->cols = list_remove(dpy->cols, dpy->cols, 1); }
        while (dpy->flts) { flt_t *flt = dpy->flts->data;
                sys_show(flt->win, ST_SHOW);
-               free(flt->win->wm);
        dpy->flts = list_remove(dpy->flts, dpy->flts, 1); }
        tag->dpys = list_remove(tag->dpys, tag->dpys, 1); }
         wm->tags = list_remove( wm->tags,  wm->tags, 1); }