]> Pileus Git - wmpus/blobdiff - wm-wmii.c
Add support for _NEW_WM_STRUT
[wmpus] / wm-wmii.c
index 800d0d3b71c2768744ac5d64946796209e13021c..e432363d667876480f4eb006dfcaa2f4a559a3ce 100644 (file)
--- a/wm-wmii.c
+++ b/wm-wmii.c
@@ -5,7 +5,7 @@
 #include "sys.h"
 #include "wm.h"
 
-#define MODKEY ctrl
+#define MODKEY alt
 #define MARGIN 10
 
 /* Loca types */
@@ -61,13 +61,21 @@ static void set_mode(drag_t drag, win_t *win, ptr_t ptr)
 
 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\n", col, col->width, col->group);
-               for (list_t *wnode = col->rows; wnode; wnode = wnode->next) {
-                       win_t *win = wnode->data;
-                       printf("  win:\t%p - %4dpx  focus=%d%d\n", win, win->h,
-                                       col->focus == win, wm_focus == win);
+       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->group, 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,
+                                       (win->wm->row->next ? win->wm->row->next->data : NULL),
+                                       win->h, col->focus == win, wm_focus == win);
                }
        }
 }
@@ -78,8 +86,8 @@ static void arrange(list_t *cols)
        int tx=0, ty=0; // Total x/y size
        int mx=0, my=0; // Maximum x/y size (screen size)
 
-
        /* Scale horizontally */
+       x  = wm_root->x;
        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;
@@ -92,7 +100,7 @@ static void arrange(list_t *cols)
                ty = 0;
                for (list_t *ly = col->rows; ly; ly = ly->next)
                        ty += ((win_t*)ly->data)->h;
-               y = 0;
+               y  = wm_root->y;
                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;
@@ -118,23 +126,31 @@ static void cut_window(win_t *win)
                     lcol->prev ? ((col_t*)lcol->prev->data)->focus :
                     lcol->next ? ((col_t*)lcol->next->data)->focus : NULL;
 
-       col->rows  = list_remove(col->rows, win->wm->row);
-       if (col->rows == NULL)
+       col->rows  = list_remove(col->rows, lrow);
+       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;
-       col->rows    = list_insert(col->rows, win);
-       win->wm->row = col->rows;
+       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;
+       } else {
+               col->rows = list_insert(col->rows, win);
+               win->wm->row = col->rows;
+       }
        win->wm->col = lcol;
        col->focus   = win;
        wm_focus     = win;
 
-       int nrows = list_length(col->rows);
-       win->h = wm_root->h / MAX(nrows-1,1);
-       if (nrows == 1) { // new column
+       win->h = wm_root->h / MAX(nrows,1);
+       if (nrows == 0) {
                int ncols = list_length(wm_cols);
                col->width = wm_root->w / MAX(ncols-1,1);
        }
@@ -199,6 +215,11 @@ static void shift_focus(win_t *win, int col, int row)
 }
 
 /* Window management functions */
+void wm_update(void)
+{
+       arrange(wm_cols);
+}
+
 int wm_handle_key(win_t *win, Key_t key, mod_t mod, ptr_t ptr)
 {
        if (!win || win == wm_root) return 0;
@@ -211,6 +232,8 @@ int wm_handle_key(win_t *win, Key_t key, mod_t mod, ptr_t ptr)
                return sys_raise(win), 1;
        if (key == key_f1 && mod.MODKEY)
                sys_raise(win);
+       if (key == key_f12 && mod.MODKEY)
+               print_txt(wm_cols);
        if (key_mouse0 <= key && key <= key_mouse7)
                sys_raise(win);
 
@@ -246,6 +269,12 @@ int wm_handle_key(win_t *win, Key_t key, mod_t mod, ptr_t ptr)
        if (key == key_enter)
                set_focus(win);
 
+       /* 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;
 }
 
@@ -295,24 +324,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);
+       sys_focus(wm_focus);
        print_txt(wm_cols);
 }
 
@@ -324,6 +348,8 @@ void wm_remove(win_t *win)
        cut_window(win);
        if (wm_focus)
                sys_focus(wm_focus);
+       else
+               sys_focus(wm_root);
        arrange(wm_cols);
        print_txt(wm_cols);
 }
@@ -333,16 +359,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());
+       sys_watch(root, key_focus,  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);
 }