X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=wm-wmii.c;h=5a814e3fb4095c10f0d0098604285ff6c0ea2bb4;hb=306672f9422fb3be995152e37abb704a9b568726;hp=df09dd45577595903f957e9bdf6e8c48a7a88a1e;hpb=354101b66a115a36599d856fc7f3b4b350c080e4;p=wmpus diff --git a/wm-wmii.c b/wm-wmii.c index df09dd4..5a814e3 100644 --- a/wm-wmii.c +++ b/wm-wmii.c @@ -5,8 +5,9 @@ #include "sys.h" #include "wm.h" -#define MODKEY ctrl -#define MARGIN 10 +#define MODKEY alt +#define MARGIN 0 +#define STACK 25 /* Loca types */ struct win_wm { @@ -19,12 +20,12 @@ typedef enum { } drag_t; typedef enum { - stack, split, max, tab -} group_t; + split, stack, max, tab +} mode_t; typedef struct { int width; - group_t group; + mode_t mode; win_t *focus; list_t *rows; } col_t; @@ -33,6 +34,7 @@ typedef struct { static drag_t move_mode; static win_t *move_win; static ptr_t move_prev; +static struct { int v, h; } move_dir; /* Window management data */ static win_t *wm_focus; @@ -40,33 +42,68 @@ static list_t *wm_cols; static win_t *wm_root; /* Helper functions */ +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->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; + } + 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 + * using the return value from wm_handle_key */ + for (int i = key_mouse1; i < key_mouse7; i++) { + if (wm_focus) + sys_watch(wm_focus, i, MOD()); + sys_unwatch(win, i, MOD()); + } + wm_focus = win; sys_focus(win); } -static void set_mode(drag_t drag, win_t *win, ptr_t ptr) +static void set_move(win_t *win, ptr_t ptr, drag_t drag) { - printf("set_mode: %d - %p@%d,%d\n", + 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; 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; } } static void print_txt(list_t *cols) { - for (list_t *cnode = cols; cnode; cnode = cnode->next) { - col_t *col = cnode->data; - printf("col:\t%p - %dpx @ %d !!%p\n", col, col->width, col->group, col->focus); - for (list_t *wnode = col->rows; wnode; wnode = wnode->next) { - win_t *win = wnode->data; - printf(" win:\t^%-9p <%-9p [%p=%p] >%-9p %4dpx focus=%d%d\n", + for (list_t *lcol = cols; lcol; lcol = lcol->next) { + col_t *col = lcol->data; + printf("col:\t <%-9p [%-19p] >%-9p - %dpx @ %d !!%p\n", + ( lcol->prev ? lcol->prev->data : NULL ), + col, + ( lcol->next ? lcol->next->data : NULL ), + col->width, col->mode, col->focus); + 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, @@ -76,39 +113,6 @@ static void print_txt(list_t *cols) } } -static void arrange(list_t *cols) -{ - int x=0, y=0; // Current window top-left position - int tx=0, ty=0; // Total x/y size - int mx=0, my=0; // Maximum x/y size (screen size) - - - /* Scale horizontally */ - mx = wm_root->w - (list_length(wm_cols)+1)*MARGIN; - for (list_t *lx = cols; lx; lx = lx->next) - tx += ((col_t*)lx->data)->width; - for (list_t *lx = cols; lx; lx = lx->next) - ((col_t*)lx->data)->width *= (float)mx / tx; - - /* Scale each column vertically */ - for (list_t *lx = 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 = 0; - my = wm_root->h - (list_length(col->rows)+1)*MARGIN; - for (list_t *ly = col->rows; ly; ly = ly->next) { - win_t *win = ly->data; - sys_move(win, x+MARGIN, y+MARGIN, - col->width, - win->h * ((float)my / ty)); - y += win->h + MARGIN; - } - x += col->width + MARGIN; - } -} - static void cut_window(win_t *win) { list_t *lrow = win->wm->row; @@ -123,12 +127,15 @@ static void cut_window(win_t *win) lcol->next ? ((col_t*)lcol->next->data)->focus : NULL; col->rows = list_remove(col->rows, lrow); - if (col->rows == NULL) + if (col->rows == NULL && (lcol->next || lcol->prev)) wm_cols = list_remove(wm_cols, lcol); } static void put_window(win_t *win, list_t *lcol) { + if (lcol == NULL) + lcol = wm_cols = list_insert(wm_cols, new0(col_t)); + col_t *col = lcol->data; int nrows = list_length(col->rows); if (col->focus) { @@ -164,7 +171,7 @@ static void shift_window(win_t *win, int col, int row) dst->data = win; ((win_t*)src->data)->wm->row = src; ((win_t*)dst->data)->wm->row = dst; - arrange(wm_cols); + wm_update(); } } else { int onlyrow = !win->wm->row->prev && !win->wm->row->next; @@ -182,50 +189,120 @@ static void shift_window(win_t *win, int col, int row) if (src && dst) { cut_window(win); put_window(win, dst); - arrange(wm_cols); + wm_update(); } } print_txt(wm_cols); } +static list_t *get_next(list_t *list, int forward) +{ + list_t *next = forward ? list->next : list->prev; + if (next == NULL) + while ((list = forward ? list->prev : list->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; if (row != 0) { - if (row < 0) node = win->wm->row->prev; - if (row > 0) node = win->wm->row->next; - } 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; - } + set_focus(get_next(win->wm->row, row > 0)->data); + if (((col_t*)win->wm->col->data)->mode != split) + wm_update(); + } + if (col != 0) { + col_t *next = get_next(win->wm->col, col > 0)->data; + set_focus(next->focus->wm->row->data); } - if (node) - set_focus(node->data); } /* Window management functions */ +void wm_update(void) +{ + int x=0, y=0; // Current window top-left position + int tx=0, ty=0; // Total x/y size + int mx=0, my=0; // Maximum x/y size (screen size) + 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; + + /* Scale each column vertically */ + for (list_t *lx = wm_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; + for (list_t *ly = col->rows; ly; ly = ly->next) { + win_t *win = ly->data; + int height = 0; + switch (col->mode) { + case split: + sys_move(win, x+MARGIN, y+MARGIN, + col->width, win->h * ((float)my / ty)); + height = win->h; + break; + case stack: + height = col->focus == 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) + sys_raise(win); + break; + } + y += height + MARGIN; + } + x += col->width + MARGIN; + } +} + 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); + //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_cols), 1; + } if (key_mouse0 <= key && key <= key_mouse7) sys_raise(win); - /* Key commands */ + /* Movement commands */ if (mod.MODKEY && mod.shift) { switch (key) { case 'h': return shift_window(win,-1, 0), 1; @@ -245,17 +322,29 @@ 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_mode(none,win,ptr), 1; - else if (key == key_mouse1 && mod.MODKEY) - return set_mode(move,win,ptr), 1; - else if (key == key_mouse3 && mod.MODKEY) - return set_mode(resize,win,ptr), 1; + /* Column mode commands */ + if (mod.MODKEY) { + switch (key) { + case 'd': return set_mode(win, split), 1; + case 's': return set_mode(win, stack), 1; + case 'm': return set_mode(win, max), 1; + case 't': return set_mode(win, tab), 1; + default: break; + } + } /* Focus change */ if (key == key_enter) - set_focus(win); + return set_focus(win), 1; + + if (key_mouse0 <= key && key <= key_mouse7) + return set_focus(win), 0; + + /* 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 (key == key_focus) + set_focus(wm_focus); return 0; } @@ -273,19 +362,19 @@ 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 *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; + if (vert) { + ((win_t*)row->data)->h += move_dir.v * dy; + ((win_t*)vert->data)->h -= move_dir.v * dy; } - if (right) { - ((col_t*)col->data)->width += dx; - ((col_t*)right->data)->width -= dx; + if (horz) { + ((col_t*)col->data)->width += move_dir.h * dx; + ((col_t*)horz->data)->width -= move_dir.h * dx; } - arrange(wm_cols); + wm_update(); } /* Floating */ @@ -306,24 +395,19 @@ void wm_insert(win_t *win) printf("wm_insert: %p\n", win); print_txt(wm_cols); - /* Watch enter/leave */ + /* Initialize window */ + win->wm = new0(win_wm_t); sys_watch(win, key_enter, MOD()); + sys_watch(win, key_focus, MOD()); /* Add to screen */ - col_t *col = wm_cols->data; - //col_t *col = wm_focus && wm_focus->wm ? - // wm_focus->wm->col->data : wm_cols->data; - int nrows = list_length(col->rows); - col->rows = list_insert(col->rows, win); - - /* Setup window */ - win->wm = new0(win_wm_t); - win->wm->col = wm_cols; - win->wm->row = col->rows; - if (nrows) win->h = wm_root->h / nrows; + list_t *lcol = wm_focus && wm_focus->wm ? + wm_focus->wm->col : wm_cols; + put_window(win, lcol); /* Arrange */ - arrange(wm_cols); + wm_update(); + sys_focus(wm_focus); print_txt(wm_cols); } @@ -335,7 +419,9 @@ void wm_remove(win_t *win) cut_window(win); if (wm_focus) sys_focus(wm_focus); - arrange(wm_cols); + else + sys_focus(wm_root); + wm_update(); print_txt(wm_cols); } @@ -343,18 +429,14 @@ 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()); - Key_t keys[] = {'h', 'j', 'k', 'l'}; - for (int i = 0; i < countof(keys); i++) { - sys_watch(root, keys[i], MOD(.MODKEY=1)); - sys_watch(root, keys[i], MOD(.MODKEY=1,.shift=1)); - } - col_t *col = new0(col_t); - col->group = stack; - col->width = root->w; - wm_cols = list_insert(wm_cols, col); + 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++) + sys_watch(root, keys_s[i], MOD(.MODKEY=1,.shift=1)); }