2 * Copyright (c) 2011, Andy Spencer <andy753421@gmail.com>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
28 static int MARGIN = 0;
29 static int STACK = 25;
37 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 win_t *win; // the window
61 int x, y, w, h; // position of window (in this tag)
65 list_t *cols; // of col_t
66 col_t *col; // focused col
67 list_t *flts; // of flt_t
68 flt_t *flt; // focused flt
69 layer_t layer; // focused layer
70 win_t *geom; // display size and position
74 list_t *dpys; // of dpy_t
75 dpy_t *dpy; // focused dpy
80 list_t *tags; // of tag_t
81 tag_t *tag; // focused tag
82 win_t *root; // root/background window
83 list_t *screens; // display geometry
86 #define WIN(node) ((win_t*)(node)->data)
87 #define ROW(node) ((row_t*)(node)->data)
88 #define COL(node) ((col_t*)(node)->data)
89 #define FLT(node) ((flt_t*)(node)->data)
90 #define DPY(node) ((dpy_t*)(node)->data)
91 #define TAG(node) ((tag_t*)(node)->data)
93 #define tag_foreach_col(tag, dpy, col, row, win) \
94 for (list_t *dpy = tag ->dpys; dpy; dpy = dpy->next) \
95 for (list_t *col = DPY(dpy)->cols; col; col = col->next) \
96 for (list_t *row = COL(col)->rows; row; row = row->next) \
97 for (win_t *win = ROW(row)->win; win; win = NULL)
99 #define tag_foreach_flt(tag, dpy, flt, win) \
100 for (list_t *dpy = tag ->dpys; dpy; dpy = dpy->next) \
101 for (list_t *flt = DPY(dpy)->flts; flt; flt = flt->next) \
102 for (win_t *win = FLT(flt)->win; win; win = NULL) \
104 /* Window management data
105 * wm_* macros represent the currently focused item
106 * _only_ wm_focus protects against NULL pointers */
108 #define wm_tag wm->tag
109 #define wm_dpy wm->tag->dpy
110 #define wm_col wm->tag->dpy->col
111 #define wm_row wm->tag->dpy->col->row
112 #define wm_flt wm->tag->dpy->flt
113 #define wm_focus get_focus()
115 /* Mouse drag data */
116 static drag_t move_mode;
117 static list_t *move_lrow;
118 static list_t *move_lcol;
119 static list_t *move_lflt;
120 static ptr_t move_prev;
121 static layer_t move_layer;
122 static struct { int v, h; } move_dir;
124 /********************
126 ********************/
127 static win_t *get_focus(void)
129 if (!wm_tag || !wm_dpy)
131 switch (wm_dpy->layer) {
133 return wm_col && wm_row ? wm_row->win : NULL;
135 return wm_flt ? wm_flt->win : NULL;
140 /* Search for the target window in a given tag
141 * win may exist in other tags as well */
142 static int searchl(tag_t *tag, win_t *target,
143 list_t **_dpy, list_t **_col, list_t **_row, list_t **_flt)
145 tag_foreach_col(tag, dpy, col, row, win) {
147 if (_dpy) *_dpy = dpy;
148 if (_col) *_col = col;
149 if (_row) *_row = row;
153 tag_foreach_flt(tag, dpy, flt, win) {
155 if (_dpy) *_dpy = dpy;
156 if (_flt) *_flt = flt;
163 static int search(tag_t *tag, win_t *target,
164 dpy_t **_dpy, col_t **_col, row_t **_row, flt_t **_flt)
166 list_t *dpy, *col, *row, *flt;
167 switch (searchl(tag, target, &dpy, &col, &row, &flt)) {
169 if (_dpy) *_dpy = DPY(dpy);
170 if (_col) *_col = COL(col);
171 if (_row) *_row = ROW(row);
174 if (_dpy) *_dpy = DPY(dpy);
175 if (_flt) *_flt = FLT(flt);
181 /* Set the mode for the windows column in the current tag */
182 static void set_mode(win_t *win, mode_t mode)
185 if (tiling != search(wm_tag, win, NULL, &col, NULL, NULL))
187 printf("set_mode: %p, %d -> %d\n",
188 col, col->mode, mode);
190 if (col->mode == split)
191 for (list_t *cur = col->rows; cur; cur = cur->next) {
192 row_t *row = cur->data;
193 row->height = wm_dpy->geom->h;
198 /* Focus the window in the current tag and record
199 * it as the currently focused window */
200 static void set_focus(win_t *win)
202 if (win == NULL || win == wm->root) {
207 /* - Only grab mouse button on unfocused window,
208 * this prevents stealing all mouse clicks from client windows,
209 * - A better way may be to re-send mouse clicks to client windows
210 * using the return value from wm_handle_key */
211 for (int i = key_mouse1; i < key_mouse7; i++) {
213 sys_watch(wm_focus, i, MOD());
214 sys_unwatch(win, i, MOD());
217 dpy_t *dpy; col_t *col; row_t *row; flt_t *flt;
218 switch (search(wm_tag, win, &dpy, &col, &row, &flt)) {
228 dpy->layer = floating;
234 /* Save mouse start location when moving/resizing windows */
235 static void set_move(win_t *win, ptr_t ptr, drag_t drag)
237 printf("set_move: %d - %p@%d,%d\n",
238 drag, win, ptr.rx, ptr.ry);
240 if (drag == move || drag == resize) {
241 move_layer = searchl(wm_tag, win, NULL,
242 &move_lcol, &move_lrow, &move_lflt);
246 int midy = win->y + (win->h/2);
247 int midx = win->x + (win->w/2);
248 move_dir.v = ptr.ry < midy ? -1 : +1;
249 move_dir.h = ptr.rx < midx ? -1 : +1;
253 /* Print a text representation of the window layout
254 * Quite useful for debugging */
255 static void print_txt(void)
257 for (list_t *ltag = wm->tags; ltag; ltag = ltag->next) {
258 tag_t *tag = ltag->data;
259 printf("tag: <%-9p [%p->%p] >%-9p d=%-9p - %d\n",
260 ltag->prev, ltag, ltag->data, ltag->next,
261 tag->dpy, tag->name);
262 for (list_t *ldpy = tag->dpys; ldpy; ldpy = ldpy->next) {
263 dpy_t *dpy = ldpy->data;
264 win_t *geom = dpy->geom;
265 printf(" dpy: <%-9p [%p->%p] >%-9p %c=%-9p - %d,%d %dx%d\n",
266 ldpy->prev, ldpy, ldpy->data, ldpy->next,
267 dpy->layer == tiling ? 'c' : 'f',
268 dpy->layer == tiling ? (void*)dpy->col : (void*)dpy->flt,
269 geom->x, geom->y, geom->h, geom->w);
270 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
271 col_t *col = lcol->data;
272 printf(" col: <%-9p [%p->%p] >%-9p r=%-9p - %dpx @ %d\n",
273 lcol->prev, lcol, lcol->data, lcol->next,
274 col->row, col->width, col->mode);
275 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
276 row_t *row = lrow->data;
277 win_t *win = row->win;
278 printf(" win: <%-9p [%p>>%p] >%-9p focus=%d%d - %4dpx \n",
279 lrow->prev, lrow, win, lrow->next,
280 col->row == row, wm_focus == win, win->h);
282 for (list_t *lflt = dpy->flts; lflt; lflt = lflt->next) {
283 flt_t *flt = lflt->data;
284 win_t *win = flt->win;
285 printf(" flt: <%-9p [%p>>%p] >%-9p focus=%d%d - %d,%d %dx%d \n",
286 lflt->prev, lflt, win, lflt->next,
287 dpy->flt == flt, wm_focus == flt->win,
288 flt->x, flt->y, flt->h, flt->w);
292 /* Cleanly remove a window from a tag
293 * Determines the new focused row/col
294 * Prunes empty lists */
295 static layer_t cut_win(win_t *win, tag_t *tag)
297 list_t *ldpy, *lcol, *lrow, *lflt;
298 layer_t layer = searchl(tag, win, &ldpy, &lcol, &lrow, &lflt);
300 if (layer == tiling) {
301 dpy_t *dpy = DPY(ldpy);
302 col_t *col = COL(lcol);
303 col->row = lrow->prev ? lrow->prev->data :
304 lrow->next ? lrow->next->data : NULL;
305 col->rows = list_remove(col->rows, lrow, 1);
306 if (col->rows == NULL && (lcol->next || lcol->prev)) {
307 dpy->col = lcol->prev ? lcol->prev->data :
308 lcol->next ? lcol->next->data : NULL;
309 dpy->cols = list_remove(dpy->cols, lcol, 1);
313 if (layer == floating) {
314 dpy_t *dpy = DPY(ldpy);
315 dpy->flts = list_remove(dpy->flts, lflt, 1);
316 dpy->flt = dpy->flts ? list_last(dpy->flts)->data : NULL;
317 if (!dpy->flt && dpy->col && dpy->col->row)
324 /* Insert a window into the tiling layer
325 * The window is added immediately after the
326 * columns currently focused row */
327 static void put_win_col(win_t *win, tag_t *tag, dpy_t *dpy, col_t *col)
329 row_t *row = new0(row_t);
334 dpy->cols = list_insert(dpy->cols, col);
337 int nrows = list_length(col->rows);
339 list_t *prev = list_find(col->rows, col->row);
340 list_insert_after(prev, row);
342 col->rows = list_insert(col->rows, row);
346 tag->dpy->col->row = row;
347 tag->dpy->layer = tiling;
349 row->height = dpy->geom->h / MAX(nrows,1);
351 int ncols = list_length(dpy->cols);
352 col->width = dpy->geom->w / MAX(ncols-1,1);
356 /* Insert a window into the floating layer */
357 static void put_win_flt(win_t *win, tag_t *tag, dpy_t *dpy)
359 flt_t *flt = new0(flt_t);
361 flt->w = dpy->geom->w / 2;
362 flt->h = dpy->geom->h / 2;
363 flt->x = dpy->geom->x + flt->w / 2;
364 flt->y = dpy->geom->y + flt->h / 2;
366 flt->x = dpy->flt->x + 20;
367 flt->y = dpy->flt->y + 20;
369 dpy->flts = list_append(dpy->flts, flt);
372 tag->dpy->layer = floating;
375 /* Insert a window into a tag */
376 static void put_win(win_t *win, tag_t *tag, layer_t layer)
379 put_win_col(win, tag, tag->dpy, tag->dpy->col);
380 if (layer == floating)
381 put_win_flt(win, tag, tag->dpy);
384 /* Move a window up, down, left, or right
385 * This handles moving with a column, between
386 * columns, and between multiple monitors. */
387 static void shift_window(win_t *win, int col, int row)
390 printf("shift_window: %p - %+d,%+d\n", win, col, row);
392 printf("shift_window: >>>\n");
393 list_t *ldpy, *lcol, *lrow, *lflt;
394 if (tiling != searchl(wm_tag, win, &ldpy, &lcol, &lrow, &lflt))
396 dpy_t *dpy = ldpy->data;
398 /* Move with a column, just swap rows */
399 list_t *src = lrow, *dst = NULL;
400 if (row < 0) dst = src->prev;
401 if (row > 0) dst = src->next;
403 printf("swap: %p <-> %p\n", src->data, dst->data);
404 row_t *tmp = src->data;
405 src->data = dst->data;
410 /* Moving between columns */
411 int onlyrow = !lrow->prev && !lrow->next;
412 list_t *src = lcol, *dst = NULL;
415 /* Normal move between columns */
417 } else if (!onlyrow) {
418 /* Create new column */
419 dpy->cols = list_insert(dpy->cols, new0(col_t));
421 } else if (ldpy->prev) {
422 /* Move to next monitor */
423 dpy = ldpy->prev->data;
424 dst = list_last(dpy->cols);
426 /* We, shall, not, be,
427 * we shall not be moved */
434 } else if (!onlyrow) {
435 dpy->cols = list_append(dpy->cols, new0(col_t));
437 } else if (ldpy->next) {
438 dpy = ldpy->next->data;
444 cut_win(win, wm_tag);
445 put_win_col(win, wm_tag, dpy, dst ? dst->data : NULL);
453 /* Get next/prev item, with wraparound */
454 static list_t *get_next(list_t *list, int forward)
456 list_t *next = forward ? list->next : list->prev;
459 while ((list = forward ? next->prev : next->next))
465 /* Move keyboard focus in a given direction */
466 static void shift_focus(int cols, int rows)
468 printf("shift_focus: %+d,%+d\n", cols, rows);
469 if (rows != 0 && wm_focus) {
470 /* Move focus up/down */
471 list_t *dpy, *col, *row;
472 if (tiling != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
474 row_t *next = get_next(row, rows > 0)->data;
475 set_focus(next->win);
476 if (COL(col)->mode != split)
480 /* Move focus left/right */
481 list_t *dpy, *col, *row, *ndpy, *ncol = NULL;
483 /* Currently focused on a window */
484 if (tiling != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
486 ncol = cols > 0 ? col->next : col->prev;
488 /* Currently focused on an empty display */
489 dpy = list_find(wm_tag->dpys, wm_dpy);
492 /* Moving focus to a different display */
493 ndpy = get_next(dpy, cols > 0);
494 ncol = cols > 0 ? DPY(ndpy)->cols :
495 list_last(DPY(ndpy)->cols);
498 if (ncol && COL(ncol) && COL(ncol)->row)
499 set_focus(COL(ncol)->row->win);
505 /* Raise the window in the floating */
506 static void raise_float(win_t *win)
508 printf("raise_float: %p\n", win);
510 for (cur = wm_dpy->flts; cur; cur = cur->next)
511 if (FLT(cur)->win == win)
514 flt_t *flt = cur->data;
515 wm_dpy->flts = list_remove(wm_dpy->flts, cur, 0);
516 wm_dpy->flts = list_append(wm_dpy->flts, flt);
521 /* Toggle between floating and tiling layers */
522 static void switch_layer(void)
524 printf("switch_float: %p %d\n",
525 wm_dpy, wm_dpy->layer);
526 wm_dpy->layer = !wm_dpy->layer;
530 /* Move current window between floating and tiling layers */
531 static void set_layer(win_t *win)
534 printf("set_float: %p %p\n", wm_dpy, win);
535 wm_dpy->layer = !cut_win(win, wm_tag);
536 put_win(win, wm_tag, wm_dpy->layer);
540 /* Allocate a new tag */
541 static tag_t *tag_new(list_t *screens, int name)
543 tag_t *tag = new0(tag_t);
545 for (list_t *cur = screens; cur; cur = cur->next) {
546 dpy_t *dpy = new0(dpy_t);
547 dpy->geom = cur->data;
548 tag->dpys = list_append(tag->dpys, dpy);
550 tag->dpy = tag->dpys->data;
555 * If it does not exist it is based on the
556 * display geometry in wm->screens */
557 static tag_t *tag_find(int name)
560 for (list_t *cur = wm->tags; cur; cur = cur->next)
561 if (name == TAG(cur)->name) {
566 tag = tag_new(wm->screens, name);
567 wm->tags = list_append(wm->tags, tag);
572 /* Move the window from the current tag to the new tag
573 * Unlike wmii, only remove the current tag, not all tags */
574 static void tag_set(win_t *win, int name)
576 printf("tag_set: %p %d\n", win, name);
577 if (wm_tag->name == name)
579 tag_t *tag = tag_find(name);
580 layer_t layer = cut_win(win, wm_tag);
581 put_win(win, tag, layer);
585 /* Switch to a different tag */
586 static void tag_switch(int name)
588 printf("tag_switch: %d\n", name);
590 if ((wm_col == NULL || wm_row == NULL) && wm_flt == NULL) {
592 dpy_t *dpy = old->dpys->data;
594 dpy->cols = list_remove(dpy->cols, dpy->cols, 1);
595 old->dpys = list_remove(old->dpys, old->dpys, 1);
597 list_t *ltag = list_find(wm->tags, old);
598 wm->tags = list_remove(wm->tags, ltag, 1);
600 wm_tag = tag_find(name);
603 /* Tile all windows in the given display
604 * This performs all the actual window tiling
605 * Currently supports split, stack and maximized modes */
606 static void wm_update_cols(dpy_t *dpy)
608 int x=0, y=0; // Current window top-left position
609 int tx=0, ty=0; // Total size (sum of initial col widths and row heights w/o margin)
610 int mx=0, my=0; // Maximum usable size (screen size minus margins)
611 int sy=0; // Stack size (height of focused stack window)
613 /* Scale horizontally */
615 mx = dpy->geom->w - (list_length(dpy->cols)+1)*MARGIN;
616 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
617 tx += COL(lcol)->width;
618 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
619 COL(lcol)->width *= (float)mx / tx;
621 /* Scale each column vertically */
622 win_t *focus = get_focus();
623 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
624 col_t *col = lcol->data;
625 int nrows = list_length(col->rows);
627 for (list_t *lrow = col->rows; lrow; lrow = lrow->next)
628 ty += ROW(lrow)->height;
630 my = dpy->geom->h - (MARGIN + (nrows-1)* MARGIN + MARGIN);
631 sy = dpy->geom->h - (MARGIN + (nrows-1)*(MARGIN/2) + MARGIN)
633 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
634 win_t *win = ROW(lrow)->win;
635 win->h = ROW(lrow)->height;
639 sys_move(win, x+MARGIN, y+MARGIN,
640 col->width, win->h * ((float)my / ty));
642 y += height + MARGIN;
645 if (lrow->next && ROW(lrow->next)->win == col->row->win) {
646 /* Hack to prevent flashing */
647 win_t *next = ROW(lrow->next)->win;
648 sys_move(next, x+MARGIN, y+MARGIN+STACK+MARGIN/2,
651 height = win == col->row->win ? sy : STACK;
652 sys_move(win, x+MARGIN, y+MARGIN,
654 y += height + (MARGIN/2);
658 sys_move(win, x+MARGIN, 0+MARGIN,
659 col->width, dpy->geom->h-2*MARGIN);
664 ROW(lrow)->height = win->h;
666 x += col->width + MARGIN;
670 /*******************************
671 * Window management functions *
672 *******************************/
676 tag_foreach_col(wm_tag, dpy, col, row, win)
677 sys_show(win, st_show);
678 tag_foreach_flt(wm_tag, dpy, flt, win)
679 sys_show(win, st_show);
680 for (list_t *tag = wm ->tags; tag; tag = tag->next)
681 if (tag->data != wm_tag) {
682 tag_foreach_col(TAG(tag), dpy, col, row, win)
683 sys_show(win, st_hide);
684 tag_foreach_flt(TAG(tag), dpy, flt, win)
685 sys_show(win, st_hide);
688 /* Refresh the display */
689 for (list_t *ldpy = wm_tag->dpys; ldpy; ldpy = ldpy->next)
690 wm_update_cols(ldpy->data);
691 tag_foreach_flt(wm_tag, ldpy, lflt, win) {
692 flt_t *flt = lflt->data;
693 sys_move(win, flt->x, flt->y, flt->w, flt->h);
700 int wm_handle_key(win_t *win, Key_t key, mod_t mod, ptr_t ptr)
702 if (!win || win == wm_dpy->geom) return 0;
703 //printf("wm_handle_key: %p - %x %c%c%c%c%c\n", win, key,
704 // mod.up ? '^' : 'v',
705 // mod.alt ? 'a' : '-',
706 // mod.ctrl ? 'c' : '-',
707 // mod.shift ? 's' : '-',
708 // mod.win ? 'w' : '-');
711 if (key == key_mouse1)
713 if (key_mouse0 <= key && key <= key_mouse7) {
714 if (key == key_mouse1 && mod.MODKEY && !mod.up)
715 return set_move(win,ptr,move), 1;
716 if (key == key_mouse3 && mod.MODKEY && !mod.up)
717 return set_move(win,ptr,resize), 1;
718 if (move_mode != none && mod.up)
719 return set_move(win,ptr,none), 1;
720 if (key == key_mouse1 && !mod.up && win->h == STACK)
721 return set_focus(win), wm_update(), 0;
725 /* Only handle key-down */
732 if (key == key_f1) return raise_float(win), 1;
733 if (key == key_f2) return set_focus(win), 1;
734 if (key == key_f3) return sys_show(win, st_show), 1;
735 if (key == key_f4) return sys_show(win, st_hide), 1;
737 if (key == key_f5) return wm_update(), 1;
738 if (key == key_f6) return print_txt(), 1;
739 if (key == 'q') return sys_exit(), 1;
744 if (mod.MODKEY && mod.shift)
745 return set_layer(win), 1;
747 return switch_layer(), 1;
750 /* Movement commands */
751 if (mod.MODKEY && mod.shift) {
753 case 'h': return shift_window(wm_focus,-1, 0), 1;
754 case 'j': return shift_window(wm_focus, 0,+1), 1;
755 case 'k': return shift_window(wm_focus, 0,-1), 1;
756 case 'l': return shift_window(wm_focus,+1, 0), 1;
760 else if (mod.MODKEY) {
762 case 'h': return shift_focus(-1, 0), 1;
763 case 'j': return shift_focus( 0,+1), 1;
764 case 'k': return shift_focus( 0,-1), 1;
765 case 'l': return shift_focus(+1, 0), 1;
770 /* Column mode commands */
773 case 'd': return set_mode(win, split), 1;
774 case 's': return set_mode(win, stack), 1;
775 case 'm': return set_mode(win, max), 1;
776 case 't': return set_mode(win, tab), 1;
782 if (mod.MODKEY && '0' <= key && key <= '9') {
783 int name = key - '0';
792 if (key == key_enter && win->h != STACK)
793 return set_focus(win), 1;
795 if (key_mouse0 <= key && key <= key_mouse7)
796 return set_focus(win), 0;
798 /* Reset focus after after focus change,
799 * not sure what is causing the focus change in the first place
800 * but preventing that would be a better solution */
801 if (key == key_focus)
802 sys_focus(wm_focus ?: wm->root);
807 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
809 //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
810 // cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
812 if (move_mode == none)
815 int dx = ptr.rx - move_prev.rx;
816 int dy = ptr.ry - move_prev.ry;
819 if (move_layer == tiling && move_mode == resize) {
820 list_t *vert = move_dir.v < 0 ? move_lrow->prev : move_lrow->next;
821 list_t *horz = move_dir.h < 0 ? move_lcol->prev : move_lcol->next;
823 ROW(move_lrow)->height += move_dir.v * dy;
824 ROW(vert)->height -= move_dir.v * dy;
827 COL(move_lcol)->width += move_dir.h * dx;
828 COL(horz)->width -= move_dir.h * dx;
833 if (move_layer == floating) {
834 flt_t *flt = move_lflt->data;
835 win_t *win = flt->win;
836 if (move_mode == move)
837 sys_move(win, win->x+dx, win->y+dy, win->w, win->h);
838 else if (move_mode == resize)
840 win->x + dx * (move_dir.h < 0),
841 win->y + dy * (move_dir.v < 0),
842 win->w + dx * move_dir.h,
843 win->h + dy * move_dir.v);
844 flt->x = win->x; flt->y = win->y;
845 flt->w = win->w; flt->h = win->h;
851 void wm_insert(win_t *win)
853 printf("wm_insert: %p\n", win);
856 /* Initialize window */
857 win->wm = new0(win_wm_t);
858 sys_watch(win, key_enter, MOD());
859 sys_watch(win, key_focus, MOD());
862 put_win(win, wm_tag, wm_dpy->layer);
870 void wm_remove(win_t *win)
872 printf("wm_remove: %p\n", win);
874 for (list_t *tag = wm->tags; tag; tag = tag->next)
875 cut_win(win, tag->data);
882 void wm_init(win_t *root)
884 printf("wm_init: %p\n", root);
886 /* Load configuration */
887 MARGIN = conf_get_int("main.margin", MARGIN);
888 STACK = conf_get_int("main.stack", STACK);
890 /* Hack, fix screen order */
891 list_t *screens = sys_info(root);
892 list_t *left = screens;
893 list_t *right = screens->next;
894 if (left && right && WIN(left)->x > WIN(right)->x) {
895 void *tmp = left->data;
896 left->data = right->data;
902 wm->screens = screens;
903 wm->tag = tag_new(wm->screens, 1);
904 wm->tags = list_insert(NULL, wm->tag);
906 Key_t keys_e[] = {key_enter, key_focus};
907 Key_t keys_s[] = {'h', 'j', 'k', 'l', 'q', ' ',
908 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
909 Key_t keys_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't', ' ',
910 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
911 key_f1, key_f2, key_f3, key_f4, key_f5, key_f6,
912 key_mouse1, key_mouse3};
913 for (int i = 0; i < countof(keys_e); i++)
914 sys_watch(root, keys_e[i], MOD());
915 for (int i = 0; i < countof(keys_m); i++)
916 sys_watch(root, keys_m[i], MOD(.MODKEY=1));
917 for (int i = 0; i < countof(keys_s); i++)
918 sys_watch(root, keys_s[i], MOD(.MODKEY=1,.shift=1));
921 void wm_free(win_t *root)
923 /* Re-show and free all windows */
924 while ( wm->tags) { tag_t *tag = wm->tags->data;
925 while (tag->dpys) { dpy_t *dpy = tag->dpys->data;
926 while (dpy->cols) { col_t *col = dpy->cols->data;
927 while (col->rows) { row_t *row = col->rows->data;
928 sys_show(row->win, st_show);
930 col->rows = list_remove(col->rows, col->rows, 1); }
931 dpy->cols = list_remove(dpy->cols, dpy->cols, 1); }
932 while (dpy->flts) { flt_t *flt = dpy->flts->data;
933 sys_show(flt->win, st_show);
935 dpy->flts = list_remove(dpy->flts, dpy->flts, 1); }
936 tag->dpys = list_remove(tag->dpys, tag->dpys, 1); }
937 wm->tags = list_remove( wm->tags, wm->tags, 1); }
939 /* Free remaining data */