14 list_t *col; // node in wm_cols
15 list_t *row; // node in col->rows
23 split, stack, max, tab
34 static drag_t move_mode;
35 static win_t *move_win;
36 static ptr_t move_prev;
38 /* Window management data */
39 static win_t *wm_focus;
40 static list_t *wm_cols;
41 static win_t *wm_root;
43 /* Helper functions */
44 static void set_mode(win_t *win, mode_t mode)
46 if (!win->wm || !win->wm->col)
48 col_t *col = win->wm->col->data;
49 printf("set_mode: %p (%p), %d -> %d\n",
50 win, col, col->mode, mode);
52 if (col->mode == split)
53 for (list_t *cur = col->rows; cur; cur = cur->next) {
54 win_t *row = cur->data;
60 static void set_focus(win_t *win)
62 if (win->wm && win->wm->col)
63 ((col_t*)win->wm->col->data)->focus = win;
65 /* - Only grab mouse button on unfocused window,
66 * this prevents stealing all mouse clicks from client windows,
67 * - A better way may be to re-send mouse clicks to client windows
68 * using the return value from wm_handle_key */
69 for (int i = key_mouse1; i < key_mouse7; i++) {
71 sys_watch(wm_focus, i, MOD());
72 sys_unwatch(win, i, MOD());
79 static void set_move(drag_t drag, win_t *win, ptr_t ptr)
81 printf("set_move: %d - %p@%d,%d\n",
82 drag, win, ptr.rx, ptr.ry);
84 if (drag == move || drag == resize) {
90 static void print_txt(list_t *cols)
92 for (list_t *lcol = cols; lcol; lcol = lcol->next) {
93 col_t *col = lcol->data;
94 printf("col:\t <%-9p [%-19p] >%-9p - %dpx @ %d !!%p\n",
95 ( lcol->prev ? lcol->prev->data : NULL ),
97 ( lcol->next ? lcol->next->data : NULL ),
98 col->width, col->mode, col->focus);
99 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
100 win_t *win = lrow->data;
101 printf(" win:\t^%-9p <%-9p [%p=%p] >%-9p - %4dpx focus=%d%d\n",
103 (win->wm->row->prev ? win->wm->row->prev->data : NULL),
104 win->wm->row->data, win,
105 (win->wm->row->next ? win->wm->row->next->data : NULL),
106 win->h, col->focus == win, wm_focus == win);
111 static void cut_window(win_t *win)
113 list_t *lrow = win->wm->row;
114 list_t *lcol = win->wm->col;
115 col_t *col = lcol->data;
117 col->focus = lrow->prev ? lrow->prev->data :
118 lrow->next ? lrow->next->data : NULL;
120 wm_focus = col->focus ? col->focus :
121 lcol->prev ? ((col_t*)lcol->prev->data)->focus :
122 lcol->next ? ((col_t*)lcol->next->data)->focus : NULL;
124 col->rows = list_remove(col->rows, lrow);
125 if (col->rows == NULL && (lcol->next || lcol->prev))
126 wm_cols = list_remove(wm_cols, lcol);
129 static void put_window(win_t *win, list_t *lcol)
132 lcol = wm_cols = list_insert(wm_cols, new0(col_t));
134 col_t *col = lcol->data;
135 int nrows = list_length(col->rows);
137 list_insert_after(col->focus->wm->row, win);
138 win->wm->row = col->focus->wm->row->next;
140 col->rows = list_insert(col->rows, win);
141 win->wm->row = col->rows;
147 win->h = wm_root->h / MAX(nrows,1);
149 int ncols = list_length(wm_cols);
150 col->width = wm_root->w / MAX(ncols-1,1);
154 static void shift_window(win_t *win, int col, int row)
156 printf("shift_window: %p - %+d,%+d\n", win, col, row);
158 printf("shift_window: >>>\n");
160 list_t *src = win->wm->row, *dst = NULL;
161 if (row < 0) dst = src->prev;
162 if (row > 0) dst = src->next;
164 printf("swap: %p <-> %p\n", src->data, dst->data);
165 src->data = dst->data;
167 ((win_t*)src->data)->wm->row = src;
168 ((win_t*)dst->data)->wm->row = dst;
172 int onlyrow = !win->wm->row->prev && !win->wm->row->next;
173 list_t *src = win->wm->col, *dst = NULL;
175 if (!src->prev && !onlyrow)
176 wm_cols = list_insert(wm_cols, new0(col_t));
180 if (!src->next && !onlyrow)
181 wm_cols = list_append(wm_cols, new0(col_t));
186 put_window(win, dst);
193 static void shift_focus(win_t *win, int col, int row)
195 printf("shift_focus: %p - %+d,%+d\n", win, col, row);
199 if (row < 0) node = win->wm->row->prev;
200 if (row > 0) node = win->wm->row->next;
201 if (((col_t*)win->wm->col->data)->mode != split)
204 if (col < 0) node = win->wm->col->prev;
205 if (col > 0) node = win->wm->col->next;
207 col_t *col = node->data;
208 node = col->focus->wm->row;
212 set_focus(node->data);
217 /* Window management functions */
220 int x=0, y=0; // Current window top-left position
221 int tx=0, ty=0; // Total x/y size
222 int mx=0, my=0; // Maximum x/y size (screen size)
223 int sy=0; // Size of focused stack window
225 /* Scale horizontally */
227 mx = wm_root->w - (list_length(wm_cols)+1)*MARGIN;
228 for (list_t *lx = wm_cols; lx; lx = lx->next)
229 tx += ((col_t*)lx->data)->width;
230 for (list_t *lx = wm_cols; lx; lx = lx->next)
231 ((col_t*)lx->data)->width *= (float)mx / tx;
233 /* Scale each column vertically */
234 for (list_t *lx = wm_cols; lx; lx = lx->next) {
235 col_t *col = lx->data;
237 for (list_t *ly = col->rows; ly; ly = ly->next)
238 ty += ((win_t*)ly->data)->h;
240 my = wm_root->h - (list_length(col->rows)+1)*MARGIN;
241 sy = my - (list_length(col->rows)-1)*STACK;
242 for (list_t *ly = col->rows; ly; ly = ly->next) {
243 win_t *win = ly->data;
247 sys_move(win, x+MARGIN, y+MARGIN,
248 col->width, win->h * ((float)my / ty));
252 height = col->focus == win ? sy : STACK;
253 sys_move(win, x+MARGIN, y+MARGIN,
258 sys_move(win, x+MARGIN, 0+MARGIN,
259 col->width, wm_root->h-2*MARGIN);
260 if (col->focus == win)
264 y += height + MARGIN;
266 x += col->width + MARGIN;
270 int wm_handle_key(win_t *win, Key_t key, mod_t mod, ptr_t ptr)
272 if (!win || win == wm_root) return 0;
273 //printf("wm_handle_key: %p - %x %x\n", win, key, mod);
277 return set_focus(win), 1;
279 return sys_raise(win), 1;
280 if (key == key_f1 && mod.MODKEY)
282 if (key == key_f12 && mod.MODKEY)
284 if (key_mouse0 <= key && key <= key_mouse7)
287 /* Movement commands */
288 if (mod.MODKEY && mod.shift) {
290 case 'h': return shift_window(win,-1, 0), 1;
291 case 'j': return shift_window(win, 0,+1), 1;
292 case 'k': return shift_window(win, 0,-1), 1;
293 case 'l': return shift_window(win,+1, 0), 1;
297 else if (mod.MODKEY) {
299 case 'h': return shift_focus(win,-1, 0), 1;
300 case 'j': return shift_focus(win, 0,+1), 1;
301 case 'k': return shift_focus(win, 0,-1), 1;
302 case 'l': return shift_focus(win,+1, 0), 1;
307 /* Column mode commands */
310 case 'd': return set_mode(win, split), 1;
311 case 's': return set_mode(win, stack), 1;
312 case 'm': return set_mode(win, max), 1;
313 case 't': return set_mode(win, tab), 1;
319 if (key_mouse0 <= key && key <= key_mouse7 && mod.up)
320 return set_move(none,win,ptr), 1;
321 else if (key == key_mouse1 && mod.MODKEY)
322 return set_move(move,win,ptr), 1;
323 else if (key == key_mouse3 && mod.MODKEY)
324 return set_move(resize,win,ptr), 1;
327 if (key == key_enter)
328 return set_focus(win), 1;
330 if (key_mouse0 <= key && key <= key_mouse7)
331 return set_focus(win), 0;
333 /* Reset focus after after focus change,
334 * not sure what is causing the focus change in the first place
335 * but preventing that would be a better solution */
336 if (key == key_focus)
342 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
344 //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
345 // cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
347 if (move_mode == none)
351 int dx = ptr.rx - move_prev.rx;
352 int dy = ptr.ry - move_prev.ry;
354 if (move_mode == resize) {
355 list_t *row = move_win->wm->row;
356 list_t *col = move_win->wm->col;
357 list_t *lower = row->next;
358 list_t *right = col->next;
360 ((win_t*)row->data)->h += dy;
361 ((win_t*)lower->data)->h -= dy;
364 ((col_t*)col->data)->width += dx;
365 ((col_t*)right->data)->width -= dx;
371 //win_t *mwin = move_win;
372 //int dx = ptr.rx - move_prev.rx;
373 //int dy = ptr.ry - move_prev.ry;
375 //if (move_mode == move)
376 // sys_move(mwin, mwin->x+dx, mwin->y+dy, mwin->w, mwin->h);
377 //else if (move_mode == resize)
378 // sys_move(mwin, mwin->x, mwin->y, mwin->w+dx, mwin->h+dy);
383 void wm_insert(win_t *win)
385 printf("wm_insert: %p\n", win);
388 /* Initialize window */
389 win->wm = new0(win_wm_t);
390 sys_watch(win, key_enter, MOD());
391 sys_watch(win, key_focus, MOD());
394 list_t *lcol = wm_focus && wm_focus->wm ?
395 wm_focus->wm->col : wm_cols;
396 put_window(win, lcol);
404 void wm_remove(win_t *win)
406 printf("wm_remove: %p - (%p,%p)\n", win,
407 win->wm->col, win->wm->row);
418 void wm_init(win_t *root)
420 printf("wm_init: %p\n", root);
422 sys_watch(root, key_f1, MOD(.MODKEY=1));
423 sys_watch(root, key_f12, MOD(.MODKEY=1));
424 sys_watch(root, key_mouse1, MOD(.MODKEY=1));
425 sys_watch(root, key_mouse3, MOD(.MODKEY=1));
426 sys_watch(root, key_enter, MOD());
427 sys_watch(root, key_focus, MOD());
428 Key_t keys_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't'};
429 Key_t keys_s[] = {'h', 'j', 'k', 'l'};
430 for (int i = 0; i < countof(keys_m); i++)
431 sys_watch(root, keys_m[i], MOD(.MODKEY=1));
432 for (int i = 0; i < countof(keys_s); i++)
433 sys_watch(root, keys_s[i], MOD(.MODKEY=1,.shift=1));