From 96b1b9eac408004c68b01194c0f5ad269c3434a5 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Sun, 25 Sep 2011 09:48:16 +0000 Subject: [PATCH] Add support for windows being in multiple tags wm_t -> tag_t -> dpy_t -> col_t -> row_t -> win_t -> win_wm_t Add separate rows as the lowest level wm container: - Each row points to a single window - Each window may be pointed to by multiple rows, as long as the rows are in separate tags. Add search functions for windows: - Windows can no longer have direct parents because they can be in multiple tags. - Use search function to find a window from the current tag, then return it's path (the list of parent containers). - This is slower, if it becomes a problem, the windows parents can be cached either in the win_wm_t structure, or parent pointers can be added to each of the containers. - Using search functions also alleviates some headaches involving updating the parent pointers. --- util.c | 8 ++ util.h | 2 + wm-wmii.c | 216 +++++++++++++++++++++++++++++++++--------------------- 3 files changed, 143 insertions(+), 83 deletions(-) diff --git a/util.c b/util.c index a91d1bd..8495a1c 100644 --- a/util.c +++ b/util.c @@ -57,6 +57,14 @@ int list_length(list_t *node) return len; } +list_t *list_find(list_t *list, void *data) +{ + for (list_t *cur = list; cur; cur = cur->next) + if (cur->data == data) + return cur; + return NULL; +} + /* Misc */ int error(char *fmt, ...) { diff --git a/util.h b/util.h index c477272..a538717 100644 --- a/util.h +++ b/util.h @@ -34,5 +34,7 @@ list_t *list_remove(list_t *head, list_t *item); int list_length(list_t *item); +list_t *list_find(list_t *list, void *data); + /* Misc */ int error(char *fmt, ...); diff --git a/wm-wmii.c b/wm-wmii.c index 3ba6fe6..c616951 100644 --- a/wm-wmii.c +++ b/wm-wmii.c @@ -9,12 +9,7 @@ #define MARGIN 0 #define STACK 25 -/* Loca types */ -struct win_wm { - list_t *col; // node in wm_cols - list_t *row; // node in col->rows -}; - +/* Enums */ typedef enum { none, move, resize } drag_t; @@ -23,9 +18,18 @@ typedef enum { split, stack, max, tab } mode_t; + +/* Window structure types */ +struct win_wm { }; + typedef struct { - list_t *rows; // of win_t - win_t *row; + win_t *win; + int height; +} row_t; + +typedef struct { + list_t *rows; // of row_t + row_t *row; int width; mode_t mode; } col_t; @@ -48,29 +52,67 @@ typedef struct { } 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 wm_t *wm; -#define wm_focus (wm->tag->dpy->col ? wm->tag->dpy->col->row : NULL) -#define wm_dpy wm->tag->dpy +#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_dpy->root->h; + row_t *row = cur->data; + row->height = wm_dpy->root->h; } wm_update(); } @@ -87,8 +129,12 @@ static void set_focus(win_t *win) sys_unwatch(win, i, MOD()); } - if (win->wm && win->wm->col) - ((col_t*)win->wm->col->data)->row = 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); } @@ -98,7 +144,7 @@ static void set_move(win_t *win, ptr_t ptr, drag_t drag) 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); @@ -111,29 +157,27 @@ 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->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->row == 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; - dpy_t *dpy = wm_dpy; // fixme + 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; @@ -146,25 +190,27 @@ static void cut_window(win_t *win) } } -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_dpy->cols = list_insert(wm_dpy->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->row) { - list_insert_after(col->row->wm->row, win); - win->wm->row = col->row->wm->row->next; + 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->row = win; - wm_dpy->col = col; + col->row = row; + wm_dpy->col = col; - win->h = wm_dpy->root->h / MAX(nrows,1); + row->height = wm_dpy->root->h / MAX(nrows,1); if (nrows == 0) { int ncols = list_length(wm_dpy->cols); col->width = wm_dpy->root->w / MAX(ncols-1,1); @@ -176,21 +222,22 @@ static void shift_window(win_t *win, int col, int row) printf("shift_window: %p - %+d,%+d\n", win, col, row); 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_dpy->cols = list_insert(wm_dpy->cols, new0(col_t)); @@ -202,12 +249,15 @@ static void shift_window(win_t *win, int col, int row) 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; } } + return; +update: print_txt(wm_dpy->cols); + wm_update(); } static list_t *get_next(list_t *list, int forward) @@ -223,14 +273,17 @@ static list_t *get_next(list_t *list, int forward) static void shift_focus(win_t *win, int col, int row) { printf("shift_focus: %p - %+d,%+d\n", win, col, row); + list_t *ldpy, *lcol, *lrow; + searchl(wm_tag, win, &ldpy, &lcol, &lrow); if (row != 0) { - set_focus(get_next(win->wm->row, row > 0)->data); - if (((col_t*)win->wm->col->data)->mode != split) + 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(win->wm->col, col > 0)->data; - set_focus(next->row->wm->row->data); + col_t *next = get_next(lcol, col > 0)->data; + set_focus(next->row->win); } } @@ -246,21 +299,22 @@ void wm_update(void) x = wm_dpy->root->x; mx = wm_dpy->root->w - (list_length(wm_dpy->cols)+1)*MARGIN; for (list_t *lx = wm_dpy->cols; lx; lx = lx->next) - tx += ((col_t*)lx->data)->width; + tx += COL(lx)->width; for (list_t *lx = wm_dpy->cols; lx; lx = lx->next) - ((col_t*)lx->data)->width *= (float)mx / tx; + COL(lx)->width *= (float)mx / tx; /* Scale each column vertically */ 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; + ty += ROW(ly)->height; y = wm_dpy->root->y; my = wm_dpy->root->h - (list_length(col->rows)+1)*MARGIN; - sy = my - (list_length(col->rows)-1)*STACK; + 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: @@ -269,7 +323,7 @@ void wm_update(void) height = win->h; break; case stack: - height = col->row == win ? sy : STACK; + height = col->row->win == win ? sy : STACK; sys_move(win, x+MARGIN, y+MARGIN, col->width, height); break; @@ -277,11 +331,12 @@ void wm_update(void) case tab: sys_move(win, x+MARGIN, 0+MARGIN, col->width, wm_dpy->root->h-2*MARGIN); - if (col->row == win) + if (col->row->win == win) sys_raise(win); break; } y += height + MARGIN; + ROW(ly)->height = win->h; } x += col->width + MARGIN; } @@ -379,17 +434,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 *vert = move_dir.v < 0 ? row->prev : row->next; - list_t *horz = move_dir.h < 0 ? col->prev : col->next; + 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) { - ((win_t*)row->data)->h += move_dir.v * dy; - ((win_t*)vert->data)->h -= move_dir.v * dy; + ROW(move_lrow)->height += move_dir.v * dy; + ROW(vert)->height -= move_dir.v * dy; } if (horz) { - ((col_t*)col->data)->width += move_dir.h * dx; - ((col_t*)horz->data)->width -= move_dir.h * dx; + COL(move_lcol)->width += move_dir.h * dx; + COL(horz)->width -= move_dir.h * dx; } wm_update(); } @@ -418,9 +471,7 @@ 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_dpy->cols; - put_window(win, lcol); + put_win(win, wm_col); /* Arrange */ wm_update(); @@ -430,10 +481,9 @@ void wm_insert(win_t *win) void wm_remove(win_t *win) { - printf("wm_remove: %p - (%p,%p)\n", win, - win->wm->col, win->wm->row); + printf("wm_remove: %p\n", win); print_txt(wm_dpy->cols); - cut_window(win); + cut_win(win); if (wm_focus) sys_focus(wm_focus); else -- 2.43.2