]> Pileus Git - wmpus/blobdiff - wm-wmii.c
Add initial Xinerama support
[wmpus] / wm-wmii.c
index e2025188c6e7093229e46e248941fe9a3086305c..e5c1a6ca48a85a06e3d674b46f86615c37440621 100644 (file)
--- a/wm-wmii.c
+++ b/wm-wmii.c
@@ -5,16 +5,11 @@
 #include "sys.h"
 #include "wm.h"
 
-#define MODKEY alt
-#define MARGIN 10
-#define STACK  20
-
-/* Loca types */
-struct win_wm {
-       list_t *col; // node in wm_cols
-       list_t *row; // node in col->rows
-};
+#define MODKEY ctrl
+#define MARGIN 0
+#define STACK  25
 
+/* Enums */
 typedef enum {
        none, move, resize
 } drag_t;
@@ -23,45 +18,109 @@ typedef enum {
        split, stack, max, tab
 } mode_t;
 
+
+/* Window structure types */
+struct win_wm { };
+
+typedef struct {
+       win_t  *win;
+       int     height;
+} row_t;
+
 typedef struct {
+       list_t *rows; // of row_t
+       row_t  *row;
        int     width;
        mode_t  mode;
-       win_t  *focus;
-       list_t *rows;
 } col_t;
 
+typedef struct {
+       list_t *cols; // of col_t
+       col_t  *col;
+       win_t  *geom;
+} dpy_t;
+
+typedef struct {
+       list_t *dpys; // of dpy_t
+       dpy_t  *dpy;
+       int     name;
+} tag_t;
+
+typedef struct {
+       list_t *tags; // of tag_t
+       tag_t  *tag;
+       win_t  *root;
+       list_t *screens;
+} wm_t;
+
 /* Mouse drag data */
-static drag_t move_mode;
-static win_t *move_win;
-static ptr_t  move_prev;
+static drag_t  move_mode;
+static list_t *move_lrow;
+static list_t *move_lcol;
+static ptr_t   move_prev;
+static struct { int v, h; } move_dir;
 
 /* Window management data */
-static win_t  *wm_focus;
-static list_t *wm_cols;
-static win_t  *wm_root;
+static wm_t  *wm;
+#define wm_focus wm->tag->dpy->col->row->win
+#define wm_row   wm->tag->dpy->col->row
+#define wm_col   wm->tag->dpy->col
+#define wm_dpy   wm->tag->dpy
+#define wm_tag   wm->tag
+
+#define ROW(l) ((row_t*)(l)->data)
+#define COL(l) ((col_t*)(l)->data)
+#define DPY(l) ((dpy_t*)(l)->data)
+#define TAG(l) ((tag_t*)(l)->data)
 
 /* Helper functions */
+static int searchl(tag_t *tag, win_t *target,
+               list_t **_dpy, list_t **_col, list_t **_row)
+{
+       for (list_t *dpy =     tag ->dpys; dpy; dpy = dpy->next)
+       for (list_t *col = DPY(dpy)->cols; col; col = col->next)
+       for (list_t *row = COL(col)->rows; row; row = row->next) {
+               win_t *win = ROW(row)->win;
+               if (win == target) {
+                       if (_dpy) *_dpy = dpy;
+                       if (_col) *_col = col;
+                       if (_row) *_row = row;
+                       return 1;
+               }
+       }
+       return 0;
+}
+
+static int search(tag_t *tag, win_t *target,
+               dpy_t **_dpy, col_t **_col, row_t **_row)
+{
+       list_t *dpy, *col, *row;
+       if (searchl(tag, target, &dpy, &col, &row)) {
+               if (_dpy) *_dpy = DPY(dpy);
+               if (_col) *_col = COL(col);
+               if (_row) *_row = ROW(row);
+               return 1;
+       }
+       return 0;
+}
+
 static void set_mode(win_t *win, mode_t mode)
 {
-       if (!win->wm || !win->wm->col)
-               return;
-       col_t *col = win->wm->col->data;
-       printf("set_mode: %p (%p), %d -> %d\n",
-                       win, col, col->mode, mode);
+       col_t *col;
+       search(wm_tag, win, NULL, &col, NULL);
+       printf("set_mode: %p, %d -> %d\n",
+                       col, col->mode, mode);
        col->mode = mode;
        if (col->mode == split)
                for (list_t *cur = col->rows; cur; cur = cur->next) {
-                       win_t *row = cur->data;
-                       row->h = wm_root->h;
+                       row_t *row = cur->data;
+                       row->height = wm_dpy->geom->h;
                }
        wm_update();
 }
 
 static void set_focus(win_t *win)
 {
-       if (win->wm && win->wm->col)
-               ((col_t*)win->wm->col->data)->focus = win;
-
        /* - Only grab mouse button on unfocused window,
         *   this prevents stealing all mouse clicks from client windows,
         * - A better way may be to re-send mouse clicks to client windows
@@ -72,18 +131,27 @@ static void set_focus(win_t *win)
                sys_unwatch(win, i, MOD());
        }
 
-       wm_focus = win;
+       dpy_t *dpy; col_t *col; row_t *row;
+       if (search(wm_tag, win, &dpy, &col, &row)) {
+               wm_dpy = dpy;
+               wm_col = col;
+               wm_row = row;
+       }
        sys_focus(win);
 }
 
-static void set_move(drag_t drag, win_t *win, ptr_t ptr)
+static void set_move(win_t *win, ptr_t ptr, drag_t drag)
 {
        printf("set_move: %d - %p@%d,%d\n",
                        drag, win, ptr.rx, ptr.ry);
        move_mode = drag;
        if (drag == move || drag == resize) {
-               move_win  = win;
+               searchl(wm_tag, win, NULL, &move_lcol, &move_lrow);
                move_prev = ptr;
+               int my = win->y + (win->h/2);
+               int mx = win->x + (win->w/2);
+               move_dir.v = ptr.ry < my ? -1 : +1;
+               move_dir.h = ptr.rx < mx ? -1 : +1;
        }
 }
 
@@ -91,127 +159,134 @@ static void print_txt(list_t *cols)
 {
        for (list_t *lcol = cols; lcol; lcol = lcol->next) {
                col_t *col = lcol->data;
-               printf("col:\t           <%-9p [%-19p] >%-9p  -  %dpx @ %d !!%p\n",
+               printf("col:\t<%-9p [%-20p] >%-9p  -  %dpx @ %d !!%p\n",
                                ( lcol->prev ? lcol->prev->data : NULL ),
                                col,
                                ( lcol->next ? lcol->next->data : NULL ),
-                               col->width, col->mode, col->focus);
+                               col->width, col->mode, col->row);
                for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
-                       win_t *win = lrow->data;
-                       printf("  win:\t^%-9p <%-9p [%p=%p] >%-9p  -  %4dpx focus=%d%d\n",
-                                       win->wm->col->data,
-                                       (win->wm->row->prev ? win->wm->row->prev->data : NULL),
-                                       win->wm->row->data, win,
-                                       (win->wm->row->next ? win->wm->row->next->data : NULL),
-                                       win->h, col->focus == win, wm_focus == win);
+                       row_t *row = lrow->data;
+                       win_t *win = row->win;
+                       printf("  win:\t<%-9p [%p>>%p] >%-9p  -  %4dpx focus=%d%d\n",
+                                       lrow->prev, lrow, win, lrow->next,
+                                       win->h, col->row == row, wm_focus == win);
                }
        }
 }
 
-static void cut_window(win_t *win)
+static void cut_win(win_t *win)
 {
-       list_t *lrow = win->wm->row;
-       list_t *lcol = win->wm->col;
-       col_t  *col  = lcol->data;
-
-       col->focus = lrow->prev ? lrow->prev->data  :
-                    lrow->next ? lrow->next->data  : NULL;
-
-       wm_focus   = col->focus ? col->focus        :
-                    lcol->prev ? ((col_t*)lcol->prev->data)->focus :
-                    lcol->next ? ((col_t*)lcol->next->data)->focus : NULL;
-
-       col->rows  = list_remove(col->rows, lrow);
-       if (col->rows == NULL && (lcol->next || lcol->prev))
-               wm_cols = list_remove(wm_cols, lcol);
+       list_t *ldpy, *lcol, *lrow;
+       searchl(wm_tag, win, &ldpy, &lcol, &lrow);
+       col_t  *col  = COL(lcol);
+       dpy_t  *dpy  = DPY(ldpy);
+
+       col->row  = lrow->prev ? lrow->prev->data :
+                   lrow->next ? lrow->next->data : NULL;
+       col->rows = list_remove(col->rows, lrow);
+
+       if (col->rows == NULL && (lcol->next || lcol->prev)) {
+               dpy->col  = lcol->prev ? lcol->prev->data :
+                           lcol->next ? lcol->next->data : NULL;
+               dpy->cols = list_remove(wm_dpy->cols, lcol);
+       }
 }
 
-static void put_window(win_t *win, list_t *lcol)
+static void put_win(win_t *win, col_t *col)
 {
-       if (lcol == NULL)
-               lcol = wm_cols = list_insert(wm_cols, new0(col_t));
+       row_t *row = new0(row_t);
+       row->win = win;
+
+       if (col == NULL) {
+               col = new0(col_t);
+               wm_dpy->cols = list_insert(wm_dpy->cols, col);
+       }
 
-       col_t *col = lcol->data;
        int nrows = list_length(col->rows);
-       if (col->focus) {
-               list_insert_after(col->focus->wm->row, win);
-               win->wm->row = col->focus->wm->row->next;
+       if (col->row) {
+               list_t *prev = list_find(col->rows, col->row);
+               list_insert_after(prev, row);
        } else {
-               col->rows = list_insert(col->rows, win);
-               win->wm->row = col->rows;
+               col->rows = list_insert(col->rows, row);
        }
-       win->wm->col = lcol;
-       col->focus   = win;
-       wm_focus     = win;
+       col->row    = row;
+       wm_dpy->col = col;
 
-       win->h = wm_root->h / MAX(nrows,1);
+       row->height = wm_dpy->geom->h / MAX(nrows,1);
        if (nrows == 0) {
-               int ncols = list_length(wm_cols);
-               col->width = wm_root->w / MAX(ncols-1,1);
+               int ncols = list_length(wm_dpy->cols);
+               col->width = wm_dpy->geom->w / MAX(ncols-1,1);
        }
 }
 
 static void shift_window(win_t *win, int col, int row)
 {
        printf("shift_window: %p - %+d,%+d\n", win, col, row);
-       print_txt(wm_cols);
+       print_txt(wm_dpy->cols);
        printf("shift_window: >>>\n");
+       list_t *ldpy, *lcol, *lrow;
+       searchl(wm_tag, win, &ldpy, &lcol, &lrow);
        if (row != 0) {
-               list_t *src = win->wm->row, *dst = NULL;
+               list_t *src = lrow, *dst = NULL;
                if (row < 0) dst = src->prev;
                if (row > 0) dst = src->next;
                if (src && dst) {
                        printf("swap: %p <-> %p\n", src->data, dst->data);
+                       row_t *tmp = src->data;
                        src->data = dst->data;
-                       dst->data = win;
-                       ((win_t*)src->data)->wm->row = src;
-                       ((win_t*)dst->data)->wm->row = dst;
-                       wm_update();
+                       dst->data = tmp;
+                       goto update;
                }
        } else {
-               int onlyrow = !win->wm->row->prev && !win->wm->row->next;
-               list_t *src = win->wm->col, *dst = NULL;
+               int onlyrow = !lrow->prev && !lrow->next;
+               list_t *src = lcol, *dst = NULL;
                if (col < 0) {
                        if (!src->prev && !onlyrow)
-                               wm_cols = list_insert(wm_cols, new0(col_t));
+                               wm_dpy->cols = list_insert(wm_dpy->cols, new0(col_t));
                        dst = src->prev;
                }
                if (col > 0) {
                        if (!src->next && !onlyrow)
-                               wm_cols = list_append(wm_cols, new0(col_t));
+                               wm_dpy->cols = list_append(wm_dpy->cols, new0(col_t));
                        dst = src->next;
                }
                if (src && dst) {
-                       cut_window(win);
-                       put_window(win, dst);
-                       wm_update();
+                       cut_win(win);
+                       put_win(win, COL(dst));
+                       goto update;
                }
        }
-       print_txt(wm_cols);
+       return;
+update:
+       print_txt(wm_dpy->cols);
+       wm_update();
 }
 
+static list_t *get_next(list_t *list, int forward)
+{
+       list_t *next = forward ? list->next : list->prev;
+       if (next == NULL) {
+               next = list;
+               while ((list = forward ? next->prev : next->next))
+                       next = list;
+       }
+       return next;
+}
 static void shift_focus(win_t *win, int col, int row)
 {
        printf("shift_focus: %p - %+d,%+d\n", win, col, row);
-       list_t *node  = NULL;
-       int update = 0;
+       list_t *ldpy, *lcol, *lrow;
+       searchl(wm_tag, win, &ldpy, &lcol, &lrow);
        if (row != 0) {
-               if (row < 0) node = win->wm->row->prev;
-               if (row > 0) node = win->wm->row->next;
-               if (((col_t*)win->wm->col->data)->mode != split)
-                       update = 1;
-       } else {
-               if (col < 0) node = win->wm->col->prev;
-               if (col > 0) node = win->wm->col->next;
-               if (node) {
-                       col_t *col = node->data;
-                       node = col->focus->wm->row;
-               }
+               row_t *next = get_next(lrow, row > 0)->data;
+               set_focus(next->win);
+               if (COL(lcol)->mode != split)
+                       wm_update();
+       }
+       if (col != 0) {
+               col_t *next = get_next(lcol, col > 0)->data;
+               set_focus(next->row->win);
        }
-       if (node)
-               set_focus(node->data);
-       if (update)
-               wm_update();
 }
 
 /* Window management functions */
@@ -223,24 +298,25 @@ void wm_update(void)
        int       sy=0; // Size of focused stack window
 
        /* Scale horizontally */
-       x  = wm_root->x;
-       mx = wm_root->w - (list_length(wm_cols)+1)*MARGIN;
-       for (list_t *lx = wm_cols; lx; lx = lx->next)
-               tx += ((col_t*)lx->data)->width;
-       for (list_t *lx = wm_cols; lx; lx = lx->next)
-               ((col_t*)lx->data)->width *= (float)mx / tx;
+       x  = wm_dpy->geom->x;
+       mx = wm_dpy->geom->w - (list_length(wm_dpy->cols)+1)*MARGIN;
+       for (list_t *lx = wm_dpy->cols; lx; lx = lx->next)
+               tx += COL(lx)->width;
+       for (list_t *lx = wm_dpy->cols; lx; lx = lx->next)
+               COL(lx)->width *= (float)mx / tx;
 
        /* Scale each column vertically */
-       for (list_t *lx = wm_cols; lx; lx = lx->next) {
+       for (list_t *lx = wm_dpy->cols; lx; lx = lx->next) {
                col_t *col = lx->data;
                ty = 0;
                for (list_t *ly = col->rows; ly; ly = ly->next)
-                       ty += ((win_t*)ly->data)->h;
-               y  = wm_root->y;
-               my = wm_root->h - (list_length(col->rows)+1)*MARGIN;
-               sy = my         - (list_length(col->rows)-1)*STACK;
+                       ty += ROW(ly)->height;
+               y  = wm_dpy->geom->y;
+               my = wm_dpy->geom->h - (list_length(col->rows)+1)*MARGIN;
+               sy = my              - (list_length(col->rows)-1)*STACK;
                for (list_t *ly = col->rows; ly; ly = ly->next) {
-                       win_t *win = ly->data;
+                       win_t *win = ROW(ly)->win;
+                       win->h = ROW(ly)->height;
                        int height = 0;
                        switch (col->mode) {
                        case split:
@@ -249,19 +325,20 @@ void wm_update(void)
                                height = win->h;
                                break;
                        case stack:
-                               height = col->focus == win ? sy : STACK;
+                               height = col->row->win == win ? sy : STACK;
                                sys_move(win, x+MARGIN, y+MARGIN,
                                        col->width, height);
                                break;
                        case max:
                        case tab:
                                sys_move(win, x+MARGIN, 0+MARGIN,
-                                       col->width, wm_root->h-2*MARGIN);
-                               if (col->focus == win)
+                                       col->width, wm_dpy->geom->h-2*MARGIN);
+                               if (col->row->win == win)
                                        sys_raise(win);
                                break;
                        }
                        y += height + MARGIN;
+                       ROW(ly)->height = win->h;
                }
                x += col->width + MARGIN;
        }
@@ -269,18 +346,33 @@ void wm_update(void)
 
 int wm_handle_key(win_t *win, Key_t key, mod_t mod, ptr_t ptr)
 {
-       if (!win || win == wm_root) return 0;
-       //printf("wm_handle_key: %p - %x %x\n", win, key, mod);
+       if (!win || win == wm_dpy->geom) return 0;
+       //printf("wm_handle_key: %p - %x %c%c%c%c%c\n", win, key,
+       //      mod.up    ? '^' : 'v',
+       //      mod.alt   ? 'a' : '-',
+       //      mod.ctrl  ? 'c' : '-',
+       //      mod.shift ? 's' : '-',
+       //      mod.win   ? 'w' : '-');
 
-       /* Raise */
-       if (key == key_f2)
-               return set_focus(win), 1;
-       if (key == key_f4)
-               return sys_raise(win), 1;
-       if (key == key_f1 && mod.MODKEY)
-               sys_raise(win);
-       if (key == key_f12 && mod.MODKEY)
-               print_txt(wm_cols);
+       /* Mouse movement */
+       if (key_mouse0 <= key && key <= key_mouse7 && mod.up)
+               return set_move(win,ptr,none), 1;
+       else if (key == key_mouse1 && mod.MODKEY)
+               return set_move(win,ptr,move), 1;
+       else if (key == key_mouse3 && mod.MODKEY)
+               return set_move(win,ptr,resize), 1;
+
+       /* Only handle key-down */
+       if (mod.up)
+               return 0;
+
+       /* Misc */
+       if (mod.MODKEY) {
+               if (key == key_f1) return sys_raise(win), 1;
+               if (key == key_f2) return set_focus(win), 1;
+               if (key == key_f5) return wm_update(),    1;
+               if (key == key_f6) return print_txt(wm_dpy->cols), 1;
+       }
        if (key_mouse0 <= key && key <= key_mouse7)
                sys_raise(win);
 
@@ -315,14 +407,6 @@ int wm_handle_key(win_t *win, Key_t key, mod_t mod, ptr_t ptr)
                }
        }
 
-       /* Mouse movement */
-       if (key_mouse0 <= key && key <= key_mouse7 && mod.up)
-               return set_move(none,win,ptr), 1;
-       else if (key == key_mouse1 && mod.MODKEY)
-               return set_move(move,win,ptr), 1;
-       else if (key == key_mouse3 && mod.MODKEY)
-               return set_move(resize,win,ptr), 1;
-
        /* Focus change */
        if (key == key_enter)
                return set_focus(win), 1;
@@ -352,17 +436,15 @@ int wm_handle_ptr(win_t *cwin, ptr_t ptr)
        int dy = ptr.ry - move_prev.ry;
        move_prev = ptr;
        if (move_mode == resize) {
-               list_t *row   = move_win->wm->row;
-               list_t *col   = move_win->wm->col;
-               list_t *lower = row->next;
-               list_t *right = col->next;
-               if (lower) {
-                       ((win_t*)row->data)->h       += dy;
-                       ((win_t*)lower->data)->h     -= dy;
+               list_t *vert = move_dir.v < 0 ? move_lrow->prev : move_lrow->next;
+               list_t *horz = move_dir.h < 0 ? move_lcol->prev : move_lcol->next;
+               if (vert) {
+                       ROW(move_lrow)->height += move_dir.v * dy;
+                       ROW(vert)->height      -= move_dir.v * dy;
                }
-               if (right) {
-                       ((col_t*)col->data)->width   += dx;
-                       ((col_t*)right->data)->width -= dx;
+               if (horz) {
+                       COL(move_lcol)->width  += move_dir.h * dx;
+                       COL(horz)->width       -= move_dir.h * dx;
                }
                wm_update();
        }
@@ -383,7 +465,7 @@ int wm_handle_ptr(win_t *cwin, ptr_t ptr)
 void wm_insert(win_t *win)
 {
        printf("wm_insert: %p\n", win);
-       print_txt(wm_cols);
+       print_txt(wm_dpy->cols);
 
        /* Initialize window */
        win->wm = new0(win_wm_t);
@@ -391,42 +473,56 @@ void wm_insert(win_t *win)
        sys_watch(win, key_focus, MOD());
 
        /* Add to screen */
-       list_t *lcol = wm_focus && wm_focus->wm ?
-               wm_focus->wm->col : wm_cols;
-       put_window(win, lcol);
+       put_win(win, wm_col);
 
        /* Arrange */
        wm_update();
        sys_focus(wm_focus);
-       print_txt(wm_cols);
+       print_txt(wm_dpy->cols);
 }
 
 void wm_remove(win_t *win)
 {
-       printf("wm_remove: %p - (%p,%p)\n", win,
-                       win->wm->col, win->wm->row);
-       print_txt(wm_cols);
-       cut_window(win);
+       printf("wm_remove: %p\n", win);
+       print_txt(wm_dpy->cols);
+       cut_win(win);
        if (wm_focus)
                sys_focus(wm_focus);
        else
-               sys_focus(wm_root);
+               sys_focus(wm->root);
        wm_update();
-       print_txt(wm_cols);
+       print_txt(wm_dpy->cols);
+}
+
+tag_t *tag_new(list_t *screens, int name)
+{
+       tag_t *tag = new0(tag_t);
+       tag->name  = name;
+       for (list_t *cur = screens; cur; cur = cur->next) {
+               dpy_t *dpy  = new0(dpy_t);
+               dpy->geom = cur->data;
+               tag->dpys = list_append(tag->dpys, dpy);
+       }
+       tag->dpy  = tag->dpys->data;
+       return tag;
 }
 
 void wm_init(win_t *root)
 {
        printf("wm_init: %p\n", root);
-       wm_root = root;
-       sys_watch(root, key_f1,     MOD(.MODKEY=1));
-       sys_watch(root, key_f12,    MOD(.MODKEY=1));
-       sys_watch(root, key_mouse1, MOD(.MODKEY=1));
-       sys_watch(root, key_mouse3, MOD(.MODKEY=1));
-       sys_watch(root, key_enter,  MOD());
-       sys_watch(root, key_focus,  MOD());
-       Key_t keys_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't'};
+
+       wm          = new0(wm_t);
+       wm->root    = root;
+       wm->screens = sys_info(root);
+       wm->tag     = tag_new(wm->screens, 1);
+       wm->tags    = list_insert(NULL, wm->tag);
+
+       Key_t keys_e[] = {key_enter, key_focus};
        Key_t keys_s[] = {'h', 'j', 'k', 'l'};
+       Key_t keys_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't',
+               key_f1, key_f2, key_f5, key_f6, key_mouse1, key_mouse3};
+       for (int i = 0; i < countof(keys_e); i++)
+               sys_watch(root, keys_e[i],  MOD());
        for (int i = 0; i < countof(keys_m); i++)
                sys_watch(root, keys_m[i], MOD(.MODKEY=1));
        for (int i = 0; i < countof(keys_s); i++)