2 * Copyright (C) 2011 Andy Spencer <andy753421@gmail.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
41 split, stack, max, tab
44 /* Window structure types */
48 win_t *win; // the window
49 int height; // win height in _this_ tag
53 list_t *rows; // of row_t
54 row_t *row; // focused row
55 int width; // column width
56 mode_t mode; // display mode
60 list_t *cols; // of col_t
61 col_t *col; // focused col
62 win_t *geom; // display size and position
66 list_t *dpys; // of dpy_t
67 dpy_t *dpy; // focused dpy
72 list_t *tags; // of tag_t
73 tag_t *tag; // focused tag
74 win_t *root; // root/background window
75 list_t *screens; // display geometry
78 #define WIN(node) ((win_t*)(node)->data)
79 #define ROW(node) ((row_t*)(node)->data)
80 #define COL(node) ((col_t*)(node)->data)
81 #define DPY(node) ((dpy_t*)(node)->data)
82 #define TAG(node) ((tag_t*)(node)->data)
84 #define tag_foreach(tag, dpy, col, row, win) \
85 for (list_t *dpy = tag ->dpys; dpy; dpy = dpy->next) \
86 for (list_t *col = DPY(dpy)->cols; col; col = col->next) \
87 for (list_t *row = COL(col)->rows; row; row = row->next) \
88 for (win_t *win = ROW(row)->win; win; win = NULL) \
90 /* Window management data
91 * wm_* macros represent the currently focused item
92 * _only_ wm_focus protects against NULL pointers */
94 #define wm_win wm->tag->dpy->col->row->win
95 #define wm_row wm->tag->dpy->col->row
96 #define wm_col wm->tag->dpy->col
97 #define wm_dpy wm->tag->dpy
98 #define wm_tag wm->tag
99 #define wm_focus (wm_tag && wm_dpy && wm_col && wm_row ? wm_win : NULL)
101 /* Mouse drag data */
102 static drag_t move_mode;
103 static list_t *move_lrow;
104 static list_t *move_lcol;
105 static ptr_t move_prev;
106 static struct { int v, h; } move_dir;
108 /********************
110 ********************/
111 /* Search for the target window in a given tag
112 * win may exist in other tags as well */
113 static int searchl(tag_t *tag, win_t *target,
114 list_t **_dpy, list_t **_col, list_t **_row)
116 tag_foreach(tag, dpy, col, row, win) {
118 if (_dpy) *_dpy = dpy;
119 if (_col) *_col = col;
120 if (_row) *_row = row;
127 static int search(tag_t *tag, win_t *target,
128 dpy_t **_dpy, col_t **_col, row_t **_row)
130 list_t *dpy, *col, *row;
131 if (searchl(tag, target, &dpy, &col, &row)) {
132 if (_dpy) *_dpy = DPY(dpy);
133 if (_col) *_col = COL(col);
134 if (_row) *_row = ROW(row);
140 /* Set the mode for the windows column in the current tag */
141 static void set_mode(win_t *win, mode_t mode)
144 if (!search(wm_tag, win, NULL, &col, NULL))
146 printf("set_mode: %p, %d -> %d\n",
147 col, col->mode, mode);
149 if (col->mode == split)
150 for (list_t *cur = col->rows; cur; cur = cur->next) {
151 row_t *row = cur->data;
152 row->height = wm_dpy->geom->h;
157 /* Focus the window in the current tag and record
158 * it as the currently focused window */
159 static void set_focus(win_t *win)
161 if (win == NULL || win == wm->root) {
166 /* - Only grab mouse button on unfocused window,
167 * this prevents stealing all mouse clicks from client windows,
168 * - A better way may be to re-send mouse clicks to client windows
169 * using the return value from wm_handle_key */
170 for (int i = key_mouse1; i < key_mouse7; i++) {
172 sys_watch(wm_focus, i, MOD());
173 sys_unwatch(win, i, MOD());
176 dpy_t *dpy; col_t *col; row_t *row;
177 if (search(wm_tag, win, &dpy, &col, &row)) {
185 /* Save mouse start location when moving/resizing windows */
186 static void set_move(win_t *win, ptr_t ptr, drag_t drag)
188 printf("set_move: %d - %p@%d,%d\n",
189 drag, win, ptr.rx, ptr.ry);
191 if (drag == move || drag == resize) {
192 searchl(wm_tag, win, NULL, &move_lcol, &move_lrow);
194 int midy = win->y + (win->h/2);
195 int midx = win->x + (win->w/2);
196 move_dir.v = ptr.ry < midy ? -1 : +1;
197 move_dir.h = ptr.rx < midx ? -1 : +1;
201 /* Print a text representation of the window layout
202 * Quite useful for debugging */
203 static void print_txt(void)
205 for (list_t *ltag = wm->tags; ltag; ltag = ltag->next) {
206 tag_t *tag = ltag->data;
207 printf("tag: <%-9p [%p->%p] >%-9p !%-9p - %d\n",
208 ltag->prev, ltag, ltag->data, ltag->next,
209 tag->dpy, tag->name);
210 for (list_t *ldpy = tag->dpys; ldpy; ldpy = ldpy->next) {
211 dpy_t *dpy = ldpy->data;
212 win_t *geom = dpy->geom;
213 printf(" dpy: <%-9p [%p->%p] >%-9p !%-9p - %d,%d %dx%d\n",
214 ldpy->prev, ldpy, ldpy->data, ldpy->next,
215 dpy->col, geom->x, geom->y, geom->h, geom->w);
216 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
217 col_t *col = lcol->data;
218 printf(" col: <%-9p [%p->%p] >%-9p !%-9p - %dpx @ %d\n",
219 lcol->prev, lcol, lcol->data, lcol->next,
220 col->row, col->width, col->mode);
221 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
222 row_t *row = lrow->data;
223 win_t *win = row->win;
224 printf(" win: <%-9p [%p>>%p] >%-9p !%-9p - %4dpx focus=%d%d\n",
225 lrow->prev, lrow, win, lrow->next,
226 win, win->h, col->row == row, wm_focus == win);
230 /* Cleanly remove a window from a tag
231 * Determines the new focused row/col
232 * Prunes empty lists */
233 static void cut_win(tag_t *tag, win_t *win)
235 list_t *ldpy, *lcol, *lrow;
236 if (!searchl(tag, win, &ldpy, &lcol, &lrow))
238 col_t *col = COL(lcol);
239 dpy_t *dpy = DPY(ldpy);
241 col->row = lrow->prev ? lrow->prev->data :
242 lrow->next ? lrow->next->data : NULL;
243 col->rows = list_remove(col->rows, lrow);
245 if (col->rows == NULL && (lcol->next || lcol->prev)) {
246 dpy->col = lcol->prev ? lcol->prev->data :
247 lcol->next ? lcol->next->data : NULL;
248 dpy->cols = list_remove(dpy->cols, lcol);
252 /* Insert a window at a given location
253 * The window is added immediately after the
254 * columns currently focused row */
255 static void put_win(win_t *win, tag_t *tag, dpy_t *dpy, col_t *col)
257 row_t *row = new0(row_t);
262 dpy->cols = list_insert(dpy->cols, col);
265 int nrows = list_length(col->rows);
267 list_t *prev = list_find(col->rows, col->row);
268 list_insert_after(prev, row);
270 col->rows = list_insert(col->rows, row);
274 tag->dpy->col->row = row;
276 row->height = dpy->geom->h / MAX(nrows,1);
278 int ncols = list_length(dpy->cols);
279 col->width = dpy->geom->w / MAX(ncols-1,1);
283 /* Move a window up, down, left, or right
284 * This handles moving with a column, between
285 * columns, and between multiple monitors. */
286 static void shift_window(win_t *win, int col, int row)
289 printf("shift_window: %p - %+d,%+d\n", win, col, row);
291 printf("shift_window: >>>\n");
292 list_t *ldpy, *lcol, *lrow;
293 if (!searchl(wm_tag, win, &ldpy, &lcol, &lrow))
295 dpy_t *dpy = ldpy->data;
297 /* Move with a column, just swap rows */
298 list_t *src = lrow, *dst = NULL;
299 if (row < 0) dst = src->prev;
300 if (row > 0) dst = src->next;
302 printf("swap: %p <-> %p\n", src->data, dst->data);
303 row_t *tmp = src->data;
304 src->data = dst->data;
309 /* Moving between columns */
310 int onlyrow = !lrow->prev && !lrow->next;
311 list_t *src = lcol, *dst = NULL;
314 /* Normal move between columns */
316 } else if (!onlyrow) {
317 /* Create new column */
318 dpy->cols = list_insert(dpy->cols, new0(col_t));
320 } else if (ldpy->prev) {
321 /* Move to next monitor */
322 dpy = ldpy->prev->data;
323 dst = list_last(dpy->cols);
325 /* We, shall, not, be,
326 * we shall not be moved */
333 } else if (!onlyrow) {
334 dpy->cols = list_append(dpy->cols, new0(col_t));
336 } else if (ldpy->next) {
337 dpy = ldpy->next->data;
343 cut_win(wm_tag, win);
344 put_win(win, wm_tag, dpy, dst ? dst->data : NULL);
352 /* Get next/prev item, with wraparound */
353 static list_t *get_next(list_t *list, int forward)
355 list_t *next = forward ? list->next : list->prev;
358 while ((list = forward ? next->prev : next->next))
364 /* Move keyboard focus in a given direction */
365 static void shift_focus(int cols, int rows)
367 printf("shift_focus: %+d,%+d\n", cols, rows);
368 if (rows != 0 && wm_focus) {
369 /* Move focus up/down */
370 list_t *dpy, *col, *row;
371 if (!searchl(wm_tag, wm_focus, &dpy, &col, &row))
373 row_t *next = get_next(row, rows > 0)->data;
374 set_focus(next->win);
375 if (COL(col)->mode != split)
379 /* Move focus left/right */
380 list_t *dpy, *col, *row, *ndpy, *ncol = NULL;
382 /* Currently focused on a window */
383 if (!searchl(wm_tag, wm_focus, &dpy, &col, &row))
385 ncol = cols > 0 ? col->next : col->prev;
387 /* Currently focused on an empty display */
388 dpy = list_find(wm_tag->dpys, wm_dpy);
391 /* Moving focus to a different display */
392 ndpy = get_next(dpy, cols > 0);
393 ncol = cols > 0 ? DPY(ndpy)->cols :
394 list_last(DPY(ndpy)->cols);
397 if (ncol && COL(ncol) && COL(ncol)->row)
398 set_focus(COL(ncol)->row->win);
404 /* Allocate a new tag */
405 static tag_t *tag_new(list_t *screens, int name)
407 tag_t *tag = new0(tag_t);
409 for (list_t *cur = screens; cur; cur = cur->next) {
410 dpy_t *dpy = new0(dpy_t);
411 dpy->geom = cur->data;
412 tag->dpys = list_append(tag->dpys, dpy);
414 tag->dpy = tag->dpys->data;
419 * If it does not exist it is based on the
420 * display geometry in wm->screens */
421 static tag_t *tag_find(int name)
424 for (list_t *cur = wm->tags; cur; cur = cur->next)
425 if (name == TAG(cur)->name) {
430 tag = tag_new(wm->screens, name);
431 wm->tags = list_append(wm->tags, tag);
436 /* Move the window from the current tag to the new tag
437 * Unlike wmii, only remove the current tag, not all tags */
438 static void tag_set(win_t *win, int name)
440 printf("tag_set: %p %d\n", win, name);
441 if (wm_tag->name == name)
443 tag_t *tag = tag_find(name);
444 cut_win(wm_tag, win);
445 put_win(win, tag, tag->dpy, tag->dpy->col);
449 /* Switch to a different tag */
450 static void tag_switch(int name)
452 printf("tag_switch: %d\n", name);
453 if (wm_col == NULL || wm_row == NULL)
454 wm->tags = list_remove(wm->tags,
455 list_find(wm->tags, wm_tag));
456 wm_tag = tag_find(name);
459 /* Tile all windows in the given display
460 * This performs all the actual window tiling
461 * Currently supports split, stack and maximized modes */
462 static void wm_update_dpy(dpy_t *dpy)
464 int x=0, y=0; // Current window top-left position
465 int tx=0, ty=0; // Total size (sum of initial col widths and row heights w/o margin)
466 int mx=0, my=0; // Maximum usable size (screen size minus margins)
467 int sy=0; // Stack size (height of focused stack window)
469 /* Scale horizontally */
471 mx = dpy->geom->w - (list_length(dpy->cols)+1)*MARGIN;
472 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
473 tx += COL(lcol)->width;
474 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
475 COL(lcol)->width *= (float)mx / tx;
477 /* Scale each column vertically */
478 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
479 col_t *col = lcol->data;
480 int nrows = list_length(col->rows);
482 for (list_t *lrow = col->rows; lrow; lrow = lrow->next)
483 ty += ROW(lrow)->height;
485 my = dpy->geom->h - (MARGIN + (nrows-1)* MARGIN + MARGIN);
486 sy = dpy->geom->h - (MARGIN + (nrows-1)*(MARGIN/2) + MARGIN)
488 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
489 win_t *win = ROW(lrow)->win;
490 win->h = ROW(lrow)->height;
494 sys_move(win, x+MARGIN, y+MARGIN,
495 col->width, win->h * ((float)my / ty));
497 y += height + MARGIN;
500 if (lrow->next && ROW(lrow->next)->win == col->row->win) {
501 /* Hack to prevent flashing */
502 win_t *next = ROW(lrow->next)->win;
503 sys_move(next, x+MARGIN, y+MARGIN+STACK+MARGIN/2,
506 height = win == col->row->win ? sy : STACK;
507 sys_move(win, x+MARGIN, y+MARGIN,
509 y += height + (MARGIN/2);
513 sys_move(win, x+MARGIN, 0+MARGIN,
514 col->width, dpy->geom->h-2*MARGIN);
515 if (col->row->win == win)
519 ROW(lrow)->height = win->h;
521 x += col->width + MARGIN;
525 /*******************************
526 * Window management functions *
527 *******************************/
531 tag_foreach(wm_tag, dpy, col, row, win)
532 sys_show(win, st_show);
533 for (list_t *tag = wm ->tags; tag; tag = tag->next)
534 tag_foreach(TAG(tag), dpy, col, row, win)
535 if (tag->data != wm_tag)
536 sys_show(win, st_hide);
538 /* Refresh the display */
539 for (list_t *ldpy = wm_tag->dpys; ldpy; ldpy = ldpy->next)
540 wm_update_dpy(ldpy->data);
545 int wm_handle_key(win_t *win, Key_t key, mod_t mod, ptr_t ptr)
547 if (!win || win == wm_dpy->geom) return 0;
548 //printf("wm_handle_key: %p - %x %c%c%c%c%c\n", win, key,
549 // mod.up ? '^' : 'v',
550 // mod.alt ? 'a' : '-',
551 // mod.ctrl ? 'c' : '-',
552 // mod.shift ? 's' : '-',
553 // mod.win ? 'w' : '-');
556 if (key_mouse0 <= key && key <= key_mouse7) {
557 if (key == key_mouse1 && mod.MODKEY && !mod.up)
558 return set_move(win,ptr,move), 1;
559 if (key == key_mouse3 && mod.MODKEY && !mod.up)
560 return set_move(win,ptr,resize), 1;
561 if (move_mode != none && mod.up)
562 return set_move(win,ptr,none), 1;
563 if (key == key_mouse1 && !mod.up && win->h == STACK)
564 return wm_update(), 0;
568 /* Only handle key-down */
575 if (key == key_f1) return sys_raise(win), 1;
576 if (key == key_f2) return set_focus(win), 1;
577 if (key == key_f3) return sys_show(win, st_show), 1;
578 if (key == key_f4) return sys_show(win, st_hide), 1;
580 if (key == key_f5) return wm_update(), 1;
581 if (key == key_f6) return print_txt(), 1;
583 if (key_mouse0 <= key && key <= key_mouse7)
586 /* Movement commands */
587 if (mod.MODKEY && mod.shift) {
589 case 'h': return shift_window(wm_focus,-1, 0), 1;
590 case 'j': return shift_window(wm_focus, 0,+1), 1;
591 case 'k': return shift_window(wm_focus, 0,-1), 1;
592 case 'l': return shift_window(wm_focus,+1, 0), 1;
596 else if (mod.MODKEY) {
598 case 'h': return shift_focus(-1, 0), 1;
599 case 'j': return shift_focus( 0,+1), 1;
600 case 'k': return shift_focus( 0,-1), 1;
601 case 'l': return shift_focus(+1, 0), 1;
606 /* Column mode commands */
609 case 'd': return set_mode(win, split), 1;
610 case 's': return set_mode(win, stack), 1;
611 case 'm': return set_mode(win, max), 1;
612 case 't': return set_mode(win, tab), 1;
618 if (mod.MODKEY && '0' <= key && key <= '9') {
619 int name = key - '0';
628 if (key == key_enter)
629 return set_focus(win), 1;
631 if (key_mouse0 <= key && key <= key_mouse7)
632 return set_focus(win), 0;
634 /* Reset focus after after focus change,
635 * not sure what is causing the focus change in the first place
636 * but preventing that would be a better solution */
637 if (key == key_focus)
638 sys_focus(wm_focus ?: wm->root);
643 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
645 //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
646 // cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
648 if (move_mode == none)
652 int dx = ptr.rx - move_prev.rx;
653 int dy = ptr.ry - move_prev.ry;
655 if (move_mode == resize) {
656 list_t *vert = move_dir.v < 0 ? move_lrow->prev : move_lrow->next;
657 list_t *horz = move_dir.h < 0 ? move_lcol->prev : move_lcol->next;
659 ROW(move_lrow)->height += move_dir.v * dy;
660 ROW(vert)->height -= move_dir.v * dy;
663 COL(move_lcol)->width += move_dir.h * dx;
664 COL(horz)->width -= move_dir.h * dx;
670 //win_t *mwin = move_win;
671 //int dx = ptr.rx - move_prev.rx;
672 //int dy = ptr.ry - move_prev.ry;
674 //if (move_mode == move)
675 // sys_move(mwin, mwin->x+dx, mwin->y+dy, mwin->w, mwin->h);
676 //else if (move_mode == resize)
677 // sys_move(mwin, mwin->x, mwin->y, mwin->w+dx, mwin->h+dy);
682 void wm_insert(win_t *win)
684 printf("wm_insert: %p\n", win);
687 /* Initialize window */
688 win->wm = new0(win_wm_t);
689 sys_watch(win, key_enter, MOD());
690 sys_watch(win, key_focus, MOD());
693 put_win(win, wm_tag, wm_dpy, wm_col);
701 void wm_remove(win_t *win)
703 printf("wm_remove: %p\n", win);
705 for (list_t *tag = wm->tags; tag; tag = tag->next)
706 cut_win(tag->data, win);
711 void wm_init(win_t *root)
713 printf("wm_init: %p\n", root);
715 /* Hack, fix screen order */
716 list_t *screens = sys_info(root);
717 list_t *left = screens;
718 list_t *right = screens->next;
719 if (left && right && WIN(left)->x > WIN(right)->x) {
720 void *tmp = left->data;
721 left->data = right->data;
727 wm->screens = screens;
728 wm->tag = tag_new(wm->screens, 1);
729 wm->tags = list_insert(NULL, wm->tag);
731 Key_t keys_e[] = {key_enter, key_focus};
732 Key_t keys_s[] = {'h', 'j', 'k', 'l',
733 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
734 Key_t keys_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't',
735 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
736 key_f1, key_f2, key_f3, key_f4, key_f5, key_f6,
737 key_mouse1, key_mouse3};
738 for (int i = 0; i < countof(keys_e); i++)
739 sys_watch(root, keys_e[i], MOD());
740 for (int i = 0; i < countof(keys_m); i++)
741 sys_watch(root, keys_m[i], MOD(.MODKEY=1));
742 for (int i = 0; i < countof(keys_s); i++)
743 sys_watch(root, keys_s[i], MOD(.MODKEY=1,.shift=1));