14 list_t *col; // node in wm_cols
15 list_t *row; // node in col->rows
23 split, stack, max, tab
27 list_t *rows; // of win_t
34 list_t *cols; // of col_t
40 list_t *dpys; // of dpy_t
46 list_t *tags; // of tag_t
51 static drag_t move_mode;
52 static win_t *move_win;
53 static ptr_t move_prev;
54 static struct { int v, h; } move_dir;
56 /* Window management data */
58 #define wm_focus (wm->tag->dpy->col ? wm->tag->dpy->col->row : NULL)
59 #define wm_dpy wm->tag->dpy
61 /* Helper functions */
62 static void set_mode(win_t *win, mode_t mode)
64 if (!win->wm || !win->wm->col)
66 col_t *col = win->wm->col->data;
67 printf("set_mode: %p (%p), %d -> %d\n",
68 win, col, col->mode, mode);
70 if (col->mode == split)
71 for (list_t *cur = col->rows; cur; cur = cur->next) {
72 win_t *row = cur->data;
73 row->h = wm_dpy->root->h;
78 static void set_focus(win_t *win)
80 /* - Only grab mouse button on unfocused window,
81 * this prevents stealing all mouse clicks from client windows,
82 * - A better way may be to re-send mouse clicks to client windows
83 * using the return value from wm_handle_key */
84 for (int i = key_mouse1; i < key_mouse7; i++) {
86 sys_watch(wm_focus, i, MOD());
87 sys_unwatch(win, i, MOD());
90 if (win->wm && win->wm->col)
91 ((col_t*)win->wm->col->data)->row = win;
95 static void set_move(win_t *win, ptr_t ptr, drag_t drag)
97 printf("set_move: %d - %p@%d,%d\n",
98 drag, win, ptr.rx, ptr.ry);
100 if (drag == move || drag == resize) {
103 int my = win->y + (win->h/2);
104 int mx = win->x + (win->w/2);
105 move_dir.v = ptr.ry < my ? -1 : +1;
106 move_dir.h = ptr.rx < mx ? -1 : +1;
110 static void print_txt(list_t *cols)
112 for (list_t *lcol = cols; lcol; lcol = lcol->next) {
113 col_t *col = lcol->data;
114 printf("col:\t <%-9p [%-19p] >%-9p - %dpx @ %d !!%p\n",
115 ( lcol->prev ? lcol->prev->data : NULL ),
117 ( lcol->next ? lcol->next->data : NULL ),
118 col->width, col->mode, col->row);
119 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
120 win_t *win = lrow->data;
121 printf(" win:\t^%-9p <%-9p [%p=%p] >%-9p - %4dpx focus=%d%d\n",
123 (win->wm->row->prev ? win->wm->row->prev->data : NULL),
124 win->wm->row->data, win,
125 (win->wm->row->next ? win->wm->row->next->data : NULL),
126 win->h, col->row == win, wm_focus == win);
131 static void cut_window(win_t *win)
133 list_t *lrow = win->wm->row;
134 list_t *lcol = win->wm->col;
135 col_t *col = lcol->data;
136 dpy_t *dpy = wm_dpy; // fixme
138 col->row = lrow->prev ? lrow->prev->data :
139 lrow->next ? lrow->next->data : NULL;
140 col->rows = list_remove(col->rows, lrow);
142 if (col->rows == NULL && (lcol->next || lcol->prev)) {
143 dpy->col = lcol->prev ? lcol->prev->data :
144 lcol->next ? lcol->next->data : NULL;
145 dpy->cols = list_remove(wm_dpy->cols, lcol);
149 static void put_window(win_t *win, list_t *lcol)
152 lcol = wm_dpy->cols = list_insert(wm_dpy->cols, new0(col_t));
154 col_t *col = lcol->data;
155 int nrows = list_length(col->rows);
157 list_insert_after(col->row->wm->row, win);
158 win->wm->row = col->row->wm->row->next;
160 col->rows = list_insert(col->rows, win);
161 win->wm->row = col->rows;
167 win->h = wm_dpy->root->h / MAX(nrows,1);
169 int ncols = list_length(wm_dpy->cols);
170 col->width = wm_dpy->root->w / MAX(ncols-1,1);
174 static void shift_window(win_t *win, int col, int row)
176 printf("shift_window: %p - %+d,%+d\n", win, col, row);
177 print_txt(wm_dpy->cols);
178 printf("shift_window: >>>\n");
180 list_t *src = win->wm->row, *dst = NULL;
181 if (row < 0) dst = src->prev;
182 if (row > 0) dst = src->next;
184 printf("swap: %p <-> %p\n", src->data, dst->data);
185 src->data = dst->data;
187 ((win_t*)src->data)->wm->row = src;
188 ((win_t*)dst->data)->wm->row = dst;
192 int onlyrow = !win->wm->row->prev && !win->wm->row->next;
193 list_t *src = win->wm->col, *dst = NULL;
195 if (!src->prev && !onlyrow)
196 wm_dpy->cols = list_insert(wm_dpy->cols, new0(col_t));
200 if (!src->next && !onlyrow)
201 wm_dpy->cols = list_append(wm_dpy->cols, new0(col_t));
206 put_window(win, dst);
210 print_txt(wm_dpy->cols);
213 static list_t *get_next(list_t *list, int forward)
215 list_t *next = forward ? list->next : list->prev;
218 while ((list = forward ? next->prev : next->next))
223 static void shift_focus(win_t *win, int col, int row)
225 printf("shift_focus: %p - %+d,%+d\n", win, col, row);
227 set_focus(get_next(win->wm->row, row > 0)->data);
228 if (((col_t*)win->wm->col->data)->mode != split)
232 col_t *next = get_next(win->wm->col, col > 0)->data;
233 set_focus(next->row->wm->row->data);
237 /* Window management functions */
240 int x=0, y=0; // Current window top-left position
241 int tx=0, ty=0; // Total x/y size
242 int mx=0, my=0; // Maximum x/y size (screen size)
243 int sy=0; // Size of focused stack window
245 /* Scale horizontally */
247 mx = wm_dpy->root->w - (list_length(wm_dpy->cols)+1)*MARGIN;
248 for (list_t *lx = wm_dpy->cols; lx; lx = lx->next)
249 tx += ((col_t*)lx->data)->width;
250 for (list_t *lx = wm_dpy->cols; lx; lx = lx->next)
251 ((col_t*)lx->data)->width *= (float)mx / tx;
253 /* Scale each column vertically */
254 for (list_t *lx = wm_dpy->cols; lx; lx = lx->next) {
255 col_t *col = lx->data;
257 for (list_t *ly = col->rows; ly; ly = ly->next)
258 ty += ((win_t*)ly->data)->h;
260 my = wm_dpy->root->h - (list_length(col->rows)+1)*MARGIN;
261 sy = my - (list_length(col->rows)-1)*STACK;
262 for (list_t *ly = col->rows; ly; ly = ly->next) {
263 win_t *win = ly->data;
267 sys_move(win, x+MARGIN, y+MARGIN,
268 col->width, win->h * ((float)my / ty));
272 height = col->row == win ? sy : STACK;
273 sys_move(win, x+MARGIN, y+MARGIN,
278 sys_move(win, x+MARGIN, 0+MARGIN,
279 col->width, wm_dpy->root->h-2*MARGIN);
284 y += height + MARGIN;
286 x += col->width + MARGIN;
290 int wm_handle_key(win_t *win, Key_t key, mod_t mod, ptr_t ptr)
292 if (!win || win == wm_dpy->root) return 0;
293 //printf("wm_handle_key: %p - %x %c%c%c%c%c\n", win, key,
294 // mod.up ? '^' : 'v',
295 // mod.alt ? 'a' : '-',
296 // mod.ctrl ? 'c' : '-',
297 // mod.shift ? 's' : '-',
298 // mod.win ? 'w' : '-');
301 if (key_mouse0 <= key && key <= key_mouse7 && mod.up)
302 return set_move(win,ptr,none), 1;
303 else if (key == key_mouse1 && mod.MODKEY)
304 return set_move(win,ptr,move), 1;
305 else if (key == key_mouse3 && mod.MODKEY)
306 return set_move(win,ptr,resize), 1;
308 /* Only handle key-down */
314 if (key == key_f1) return sys_raise(win), 1;
315 if (key == key_f2) return set_focus(win), 1;
316 if (key == key_f5) return wm_update(), 1;
317 if (key == key_f6) return print_txt(wm_dpy->cols), 1;
319 if (key_mouse0 <= key && key <= key_mouse7)
322 /* Movement commands */
323 if (mod.MODKEY && mod.shift) {
325 case 'h': return shift_window(win,-1, 0), 1;
326 case 'j': return shift_window(win, 0,+1), 1;
327 case 'k': return shift_window(win, 0,-1), 1;
328 case 'l': return shift_window(win,+1, 0), 1;
332 else if (mod.MODKEY) {
334 case 'h': return shift_focus(win,-1, 0), 1;
335 case 'j': return shift_focus(win, 0,+1), 1;
336 case 'k': return shift_focus(win, 0,-1), 1;
337 case 'l': return shift_focus(win,+1, 0), 1;
342 /* Column mode commands */
345 case 'd': return set_mode(win, split), 1;
346 case 's': return set_mode(win, stack), 1;
347 case 'm': return set_mode(win, max), 1;
348 case 't': return set_mode(win, tab), 1;
354 if (key == key_enter)
355 return set_focus(win), 1;
357 if (key_mouse0 <= key && key <= key_mouse7)
358 return set_focus(win), 0;
360 /* Reset focus after after focus change,
361 * not sure what is causing the focus change in the first place
362 * but preventing that would be a better solution */
363 if (key == key_focus)
369 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
371 //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
372 // cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
374 if (move_mode == none)
378 int dx = ptr.rx - move_prev.rx;
379 int dy = ptr.ry - move_prev.ry;
381 if (move_mode == resize) {
382 list_t *row = move_win->wm->row;
383 list_t *col = move_win->wm->col;
384 list_t *vert = move_dir.v < 0 ? row->prev : row->next;
385 list_t *horz = move_dir.h < 0 ? col->prev : col->next;
387 ((win_t*)row->data)->h += move_dir.v * dy;
388 ((win_t*)vert->data)->h -= move_dir.v * dy;
391 ((col_t*)col->data)->width += move_dir.h * dx;
392 ((col_t*)horz->data)->width -= move_dir.h * dx;
398 //win_t *mwin = move_win;
399 //int dx = ptr.rx - move_prev.rx;
400 //int dy = ptr.ry - move_prev.ry;
402 //if (move_mode == move)
403 // sys_move(mwin, mwin->x+dx, mwin->y+dy, mwin->w, mwin->h);
404 //else if (move_mode == resize)
405 // sys_move(mwin, mwin->x, mwin->y, mwin->w+dx, mwin->h+dy);
410 void wm_insert(win_t *win)
412 printf("wm_insert: %p\n", win);
413 print_txt(wm_dpy->cols);
415 /* Initialize window */
416 win->wm = new0(win_wm_t);
417 sys_watch(win, key_enter, MOD());
418 sys_watch(win, key_focus, MOD());
421 list_t *lcol = wm_focus && wm_focus->wm ?
422 wm_focus->wm->col : wm_dpy->cols;
423 put_window(win, lcol);
428 print_txt(wm_dpy->cols);
431 void wm_remove(win_t *win)
433 printf("wm_remove: %p - (%p,%p)\n", win,
434 win->wm->col, win->wm->row);
435 print_txt(wm_dpy->cols);
440 sys_focus(wm_dpy->root);
442 print_txt(wm_dpy->cols);
445 void wm_init(win_t *root)
447 printf("wm_init: %p\n", root);
449 tag_t *tag = new0(tag_t);
450 dpy_t *dpy = new0(dpy_t);
453 tag->dpys = list_insert(NULL, dpy);
456 wm->tags = list_insert(NULL, tag);
459 Key_t keys_e[] = {key_enter, key_focus};
460 Key_t keys_s[] = {'h', 'j', 'k', 'l'};
461 Key_t keys_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't',
462 key_f1, key_f2, key_f5, key_f6, key_mouse1, key_mouse3};
463 for (int i = 0; i < countof(keys_e); i++)
464 sys_watch(root, keys_e[i], MOD());
465 for (int i = 0; i < countof(keys_m); i++)
466 sys_watch(root, keys_m[i], MOD(.MODKEY=1));
467 for (int i = 0; i < countof(keys_s); i++)
468 sys_watch(root, keys_s[i], MOD(.MODKEY=1,.shift=1));