2 * Copyright (c) 2011-2012, 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, FULL, TAB
44 /* Window structure types */
48 win_t *win; // the window
49 int height; // win height in _this_ tag
50 state_t state; // state of window
54 list_t *rows; // of row_t
55 row_t *row; // focused row
56 int width; // column width
57 layout_t layout; // column layout
61 win_t *win; // the window
62 int x, y, w, h; // position of window (in this tag)
63 state_t state; // state of window
67 list_t *cols; // of col_t
68 col_t *col; // focused col
69 list_t *flts; // of flt_t
70 flt_t *flt; // focused flt
71 layer_t layer; // focused layer
72 win_t *geom; // display size and position
76 list_t *dpys; // of dpy_t
77 dpy_t *dpy; // focused dpy
82 list_t *tags; // of tag_t
83 tag_t *tag; // focused tag
84 win_t *root; // root/background window
85 list_t *screens; // display geometry
88 #define WIN(node) ((win_t*)(node)->data)
89 #define ROW(node) ((row_t*)(node)->data)
90 #define COL(node) ((col_t*)(node)->data)
91 #define FLT(node) ((flt_t*)(node)->data)
92 #define DPY(node) ((dpy_t*)(node)->data)
93 #define TAG(node) ((tag_t*)(node)->data)
95 #define tag_foreach_col(tag, dpy, col, row, win) \
96 for (list_t *dpy = tag ->dpys; dpy; dpy = dpy->next) \
97 for (list_t *col = DPY(dpy)->cols; col; col = col->next) \
98 for (list_t *row = COL(col)->rows; row; row = row->next) \
99 for (win_t *win = ROW(row)->win; win; win = NULL)
101 #define tag_foreach_flt(tag, dpy, flt, win) \
102 for (list_t *dpy = tag ->dpys; dpy; dpy = dpy->next) \
103 for (list_t *flt = DPY(dpy)->flts; flt; flt = flt->next) \
104 for (win_t *win = FLT(flt)->win; win; win = NULL) \
106 /* Window management data
107 * wm_* macros represent the currently focused item
108 * _only_ wm_focus protects against NULL pointers */
110 #define wm_tag wm->tag
111 #define wm_dpy wm->tag->dpy
112 #define wm_col wm->tag->dpy->col
113 #define wm_row wm->tag->dpy->col->row
114 #define wm_flt wm->tag->dpy->flt
115 #define wm_focus get_focus()
117 /* Mouse drag data */
118 static drag_t move_mode;
119 static list_t *move_lrow;
120 static list_t *move_lcol;
121 static list_t *move_lflt;
122 static ptr_t move_prev;
123 static layer_t move_layer;
124 static struct { int v, h; } move_dir;
126 /********************
128 ********************/
129 static int sort_win(void *a, void *b)
131 return ((win_t*)a)->x > ((win_t*)b)->x ? 1 :
132 ((win_t*)a)->x < ((win_t*)b)->x ? -1 : 0;
135 static win_t *get_focus(void)
137 if (!wm_tag || !wm_dpy)
139 switch (wm_dpy->layer) {
141 return wm_col && wm_row ? wm_row->win : NULL;
143 return wm_flt ? wm_flt->win : NULL;
148 /* Search for the target window in a given tag
149 * win may exist in other tags as well */
150 static int searchl(tag_t *tag, win_t *target,
151 list_t **_dpy, list_t **_col, list_t **_row, list_t **_flt)
153 tag_foreach_col(tag, dpy, col, row, win) {
155 if (_dpy) *_dpy = dpy;
156 if (_col) *_col = col;
157 if (_row) *_row = row;
161 tag_foreach_flt(tag, dpy, flt, win) {
163 if (_dpy) *_dpy = dpy;
164 if (_flt) *_flt = flt;
171 static int search(tag_t *tag, win_t *target,
172 dpy_t **_dpy, col_t **_col, row_t **_row, flt_t **_flt)
174 list_t *dpy, *col, *row, *flt;
175 switch (searchl(tag, target, &dpy, &col, &row, &flt)) {
177 if (_dpy) *_dpy = DPY(dpy);
178 if (_col) *_col = COL(col);
179 if (_row) *_row = ROW(row);
182 if (_dpy) *_dpy = DPY(dpy);
183 if (_flt) *_flt = FLT(flt);
189 /* Set the layout for the windows column in the current tag */
190 static void set_mode(win_t *win, layout_t layout)
193 if (TILING != search(wm_tag, win, NULL, &col, NULL, NULL))
195 printf("set_mode: %p, %d -> %d\n",
196 col, col->layout, layout);
197 col->layout = layout;
198 if (col->layout == SPLIT)
199 for (list_t *cur = col->rows; cur; cur = cur->next) {
200 row_t *row = cur->data;
201 row->height = wm_dpy->geom->h;
206 /* Focus the window in the current tag and record
207 * it as the currently focused window */
208 static void set_focus(win_t *win)
210 if (win == NULL || win == wm->root) {
215 dpy_t *dpy; col_t *col; row_t *row; flt_t *flt;
216 switch (search(wm_tag, win, &dpy, &col, &row, &flt)) {
226 dpy->layer = FLOATING;
232 /* Save mouse start location when moving/resizing windows */
233 static void set_move(win_t *win, ptr_t ptr, drag_t drag)
235 printf("set_move: %d - %p@%d,%d\n",
236 drag, win, ptr.rx, ptr.ry);
238 if (drag == MOVE || drag == RESIZE) {
239 move_layer = searchl(wm_tag, win, NULL,
240 &move_lcol, &move_lrow, &move_lflt);
244 int midy = win->y + (win->h/2);
245 int midx = win->x + (win->w/2);
246 move_dir.v = ptr.ry < midy ? -1 : +1;
247 move_dir.h = ptr.rx < midx ? -1 : +1;
251 /* Print a text representation of the window layout
252 * Quite useful for debugging */
253 static void print_txt(void)
255 for (list_t *ltag = wm->tags; ltag; ltag = ltag->next) {
256 tag_t *tag = ltag->data;
257 printf("tag: <%-9p [%p->%p] >%-9p d=%-9p - %d\n",
258 ltag->prev, ltag, ltag->data, ltag->next,
259 tag->dpy, tag->name);
260 for (list_t *ldpy = tag->dpys; ldpy; ldpy = ldpy->next) {
261 dpy_t *dpy = ldpy->data;
262 win_t *geom = dpy->geom;
263 printf(" dpy: <%-9p [%p->%p] >%-9p %c=%-9p - %d,%d %dx%d\n",
264 ldpy->prev, ldpy, ldpy->data, ldpy->next,
265 dpy->layer == TILING ? 'c' : 'f',
266 dpy->layer == TILING ? (void*)dpy->col : (void*)dpy->flt,
267 geom->x, geom->y, geom->h, geom->w);
268 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
269 col_t *col = lcol->data;
270 printf(" col: <%-9p [%p->%p] >%-9p r=%-9p - %dpx @ %d\n",
271 lcol->prev, lcol, lcol->data, lcol->next,
272 col->row, col->width, col->layout);
273 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
274 row_t *row = lrow->data;
275 win_t *win = row->win;
276 printf(" win: <%-9p [%p>>%p] >%-9p focus=%d%d - %4dpx \n",
277 lrow->prev, lrow, win, lrow->next,
278 col->row == row, wm_focus == win, win->h);
280 for (list_t *lflt = dpy->flts; lflt; lflt = lflt->next) {
281 flt_t *flt = lflt->data;
282 win_t *win = flt->win;
283 printf(" flt: <%-9p [%p>>%p] >%-9p focus=%d%d - %d,%d %dx%d \n",
284 lflt->prev, lflt, win, lflt->next,
285 dpy->flt == flt, wm_focus == flt->win,
286 flt->x, flt->y, flt->h, flt->w);
290 /* Cleanly remove a window from a tag
291 * Determines the new focused row/col
292 * Prunes empty lists */
293 static layer_t cut_win(win_t *win, tag_t *tag)
295 list_t *ldpy, *lcol, *lrow, *lflt;
296 layer_t layer = searchl(tag, win, &ldpy, &lcol, &lrow, &lflt);
298 if (layer == TILING) {
299 dpy_t *dpy = DPY(ldpy);
300 col_t *col = COL(lcol);
301 col->row = lrow->prev ? lrow->prev->data :
302 lrow->next ? lrow->next->data : NULL;
303 col->rows = list_remove(col->rows, lrow, 1);
304 if (col->rows == NULL && (lcol->next || lcol->prev)) {
305 dpy->col = lcol->prev ? lcol->prev->data :
306 lcol->next ? lcol->next->data : NULL;
307 dpy->cols = list_remove(dpy->cols, lcol, 1);
311 if (layer == FLOATING) {
312 dpy_t *dpy = DPY(ldpy);
313 dpy->flts = list_remove(dpy->flts, lflt, 1);
314 dpy->flt = dpy->flts ? list_last(dpy->flts)->data : NULL;
315 if (!dpy->flt && dpy->col && dpy->col->row)
322 /* Insert a window into the tiling layer
323 * The window is added immediately after the
324 * columns currently focused row */
325 static void put_win_col(win_t *win, tag_t *tag, dpy_t *dpy, col_t *col)
327 row_t *row = new0(row_t);
329 row->state = win->state ?: ST_SHOW;
333 dpy->cols = list_insert(dpy->cols, col);
336 int nrows = list_length(col->rows);
338 list_t *prev = list_find(col->rows, col->row);
339 list_insert_after(prev, row);
341 col->rows = list_insert(col->rows, row);
345 tag->dpy->col->row = row;
346 tag->dpy->layer = TILING;
348 row->height = dpy->geom->h / MAX(nrows,1);
350 int ncols = list_length(dpy->cols);
351 col->width = dpy->geom->w / MAX(ncols-1,1);
355 /* Insert a window into the floating layer */
356 static void put_win_flt(win_t *win, tag_t *tag, dpy_t *dpy)
358 flt_t *flt = new0(flt_t);
360 flt->w = dpy->geom->w / 2;
361 flt->h = dpy->geom->h / 2;
362 flt->x = dpy->geom->x + flt->w / 2;
363 flt->y = dpy->geom->y + flt->h / 2;
364 flt->state = win->state ?: ST_SHOW;
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)->layout != 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;
551 for (list_t *dpy = tag->dpys; dpy; dpy = dpy->next)
552 if (DPY(dpy)->geom->z > tag->dpy->geom->z)
553 tag->dpy = dpy->data;
558 * If it does not exist it is based on the
559 * display geometry in wm->screens */
560 static tag_t *tag_find(int name)
563 for (list_t *cur = wm->tags; cur; cur = cur->next)
564 if (name == TAG(cur)->name) {
569 tag = tag_new(wm->screens, name);
570 wm->tags = list_append(wm->tags, tag);
575 /* Move the window from the current tag to the new tag
576 * Unlike wmii, only remove the current tag, not all tags */
577 static void tag_set(win_t *win, int name)
579 printf("tag_set: %p %d\n", win, name);
580 if (wm_tag->name == name)
582 tag_t *tag = tag_find(name);
583 layer_t layer = cut_win(win, wm_tag);
584 put_win(win, tag, layer);
588 /* Switch to a different tag */
589 static void tag_switch(int name)
591 printf("tag_switch: %d\n", name);
593 if ((wm_col == NULL || wm_row == NULL) && wm_flt == NULL) {
595 dpy_t *dpy = old->dpys->data;
597 dpy->cols = list_remove(dpy->cols, dpy->cols, 1);
598 old->dpys = list_remove(old->dpys, old->dpys, 1);
600 list_t *ltag = list_find(wm->tags, old);
601 wm->tags = list_remove(wm->tags, ltag, 1);
603 wm_tag = tag_find(name);
606 /* Tile all windows in the given display
607 * This performs all the actual window tiling
608 * Currently supports split, stack and maximized modes */
609 static void wm_update_cols(dpy_t *dpy)
611 int x=0, y=0; // Current window top-left position
612 int tx=0, ty=0; // Total size (sum of initial col widths and row heights w/o margin)
613 int mx=0, my=0; // Maximum usable size (screen size minus margins)
614 int sy=0; // Stack size (height of focused stack window)
616 /* Scale horizontally */
618 mx = dpy->geom->w - (list_length(dpy->cols)+1)*margin;
619 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
620 tx += COL(lcol)->width;
621 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
622 COL(lcol)->width *= (float)mx / tx;
624 /* Scale each column vertically */
625 win_t *focus = get_focus();
626 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
627 col_t *col = lcol->data;
628 int nrows = list_length(col->rows);
630 for (list_t *lrow = col->rows; lrow; lrow = lrow->next)
631 ty += ROW(lrow)->height;
633 my = dpy->geom->h - (margin + (nrows-1)* margin + margin);
634 sy = dpy->geom->h - (margin + (nrows-1)*(margin/2) + margin)
636 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
637 win_t *win = ROW(lrow)->win;
638 if (ROW(lrow)->state != ST_SHOW) {
639 sys_show(win, ROW(lrow)->state);
642 win->h = ROW(lrow)->height;
643 state_t state = ST_SHOW;
645 switch (col->layout) {
647 sys_move(win, x+margin, y+margin,
648 col->width, win->h * ((float)my / ty));
650 y += height + margin;
653 if (lrow->next && ROW(lrow->next)->win == col->row->win) {
654 /* Hack to prevent flashing */
655 win_t *next = ROW(lrow->next)->win;
656 sys_move(next, x+margin, y+margin+stack+margin/2,
658 sys_show(next, ST_SHOW);
660 int isfocus = win == col->row->win;
661 state = isfocus ? ST_SHOW : ST_SHADE;
662 sys_show(win, state);
663 sys_move(win, x+margin, y+margin, col->width, sy);
664 y += (isfocus ? sy : stack) + (margin/2);
668 sys_move(win, x+margin, 0+margin,
669 col->width, dpy->geom->h-2*margin);
672 sys_show(win, state);
675 ROW(lrow)->height = win->h;
677 x += col->width + margin;
681 /*******************************
682 * Window management functions *
683 *******************************/
686 /* Updates window sizes */
687 for (list_t *ldpy = wm_tag->dpys; ldpy; ldpy = ldpy->next)
688 wm_update_cols(ldpy->data);
689 tag_foreach_flt(wm_tag, ldpy, lflt, win) {
690 flt_t *flt = lflt->data;
691 sys_move(win, flt->x, flt->y, flt->w, flt->h);
693 sys_show(flt->win, flt->state);
696 /* Hide other tags */
697 for (list_t *tag = wm ->tags; tag; tag = tag->next)
698 if (tag->data != wm_tag) {
699 tag_foreach_col(TAG(tag), dpy, col, row, win)
700 sys_show(win, ST_HIDE);
701 tag_foreach_flt(TAG(tag), dpy, flt, win)
702 sys_show(win, ST_HIDE);
705 /* Set focused window */
710 int wm_handle_event(win_t *win, event_t ev, mod_t mod, ptr_t ptr)
712 if (!win || win == wm_dpy->geom) return 0;
713 //printf("wm_handle_event: %p - %x %c%c%c%c%c\n", win, ev,
714 // mod.up ? '^' : 'v',
715 // mod.alt ? 'a' : '-',
716 // mod.ctrl ? 'c' : '-',
717 // mod.shift ? 's' : '-',
718 // mod.win ? 'w' : '-');
721 if (EV_MOUSE0 <= ev && ev <= EV_MOUSE7) {
722 if (ev == EV_MOUSE1 && !mod.MODKEY && !mod.up)
723 return raise_float(win), 0;
724 if (ev == EV_MOUSE1 && mod.MODKEY && !mod.up)
725 return set_move(win,ptr,MOVE), 1;
726 if (ev == EV_MOUSE3 && mod.MODKEY && !mod.up)
727 return set_move(win,ptr,RESIZE), 1;
728 if (move_mode != NONE && mod.up)
729 return set_move(win,ptr,NONE), 1;
730 if (ev == EV_MOUSE1 && !mod.up && win->state == ST_SHADE)
731 return set_focus(win), wm_update(), 0;
735 /* Only handle key-down */
742 if (ev == EV_F1) return raise_float(win), 1;
743 if (ev == EV_F2) return set_focus(win), 1;
744 if (ev == EV_F3) return sys_show(win, ST_SHOW), 1;
745 if (ev == EV_F4) return sys_show(win, ST_HIDE), 1;
746 if (ev == EV_F7) return sys_show(win, ST_SHADE), 1;
748 if (ev == EV_F5) return wm_update(), 1;
749 if (ev == EV_F6) return print_txt(), 1;
750 if (ev == 'q') return sys_exit(), 1;
751 if (ev == 'f') return wm_handle_state(win, win->state,
752 win->state == ST_FULL ? ST_SHOW : ST_FULL);
754 if (mod.MODKEY && mod.shift) {
755 if (ev == 'c') return sys_show(win, ST_CLOSE), 1;
760 if (mod.MODKEY && mod.shift)
761 return set_layer(win), 1;
763 return switch_layer(), 1;
766 /* Movement commands */
767 if (mod.MODKEY && mod.shift) {
769 case 'h': return shift_window(wm_focus,-1, 0), 1;
770 case 'j': return shift_window(wm_focus, 0,+1), 1;
771 case 'k': return shift_window(wm_focus, 0,-1), 1;
772 case 'l': return shift_window(wm_focus,+1, 0), 1;
776 else if (mod.MODKEY) {
778 case 'h': return shift_focus(-1, 0), 1;
779 case 'j': return shift_focus( 0,+1), 1;
780 case 'k': return shift_focus( 0,-1), 1;
781 case 'l': return shift_focus(+1, 0), 1;
786 /* Column layout commands */
789 case 'd': return set_mode(win, SPLIT), 1;
790 case 's': return set_mode(win, STACK), 1;
791 case 'm': return set_mode(win, FULL), 1;
792 case 't': return set_mode(win, TAB), 1;
798 if (mod.MODKEY && '0' <= ev && ev <= '9') {
808 if (ev == EV_ENTER && win->state != ST_SHADE)
809 return set_focus(win), 1;
811 /* Reset focus after after focus change,
812 * not sure what is causing the focus change in the first place
813 * but preventing that would be a better solution */
815 sys_focus(wm_focus ?: wm->root);
820 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
822 //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
823 // cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
825 if (move_mode == NONE)
828 int dx = ptr.rx - move_prev.rx;
829 int dy = ptr.ry - move_prev.ry;
832 if (move_layer == TILING && move_mode == RESIZE) {
833 list_t *vert = move_dir.v < 0 ? move_lrow->prev : move_lrow->next;
834 list_t *horz = move_dir.h < 0 ? move_lcol->prev : move_lcol->next;
836 ROW(move_lrow)->height += move_dir.v * dy;
837 ROW(vert)->height -= move_dir.v * dy;
840 COL(move_lcol)->width += move_dir.h * dx;
841 COL(horz)->width -= move_dir.h * dx;
846 if (move_layer == FLOATING) {
847 flt_t *flt = move_lflt->data;
848 win_t *win = flt->win;
849 if (move_mode == MOVE)
850 sys_move(win, win->x+dx, win->y+dy, win->w, win->h);
851 else if (move_mode == RESIZE)
853 win->x + dx * (move_dir.h < 0),
854 win->y + dy * (move_dir.v < 0),
855 win->w + dx * move_dir.h,
856 win->h + dy * move_dir.v);
857 flt->x = win->x; flt->y = win->y;
858 flt->w = win->w; flt->h = win->h;
864 int wm_handle_state(win_t *win, state_t prev, state_t next)
869 search(wm_tag, win, NULL, NULL, &row, &flt);
871 if (row) row->state = next;
872 if (flt) flt->state = next;
874 if (prev == ST_MAX || prev == ST_FULL ||
875 next == ST_MAX || next == ST_FULL)
881 void wm_insert(win_t *win)
883 printf("wm_insert: %p\n", win);
886 /* Initialize window */
887 win->wm = new0(win_wm_t);
888 sys_watch(win, EV_ENTER, MOD());
889 sys_watch(win, EV_FOCUS, MOD());
892 put_win(win, wm_tag, wm_dpy->layer);
900 void wm_remove(win_t *win)
902 printf("wm_remove: %p\n", win);
904 for (list_t *tag = wm->tags; tag; tag = tag->next)
905 cut_win(win, tag->data);
912 void wm_init(win_t *root)
914 printf("wm_init: %p\n", root);
916 /* Load configuration */
917 margin = conf_get_int("main.margin", margin);
918 stack = conf_get_int("main.stack", stack);
922 wm->screens = list_sort(sys_info(root), 0, sort_win);
923 wm->tag = tag_new(wm->screens, 1);
924 wm->tags = list_insert(NULL, wm->tag);
926 event_t ev_e[] = {EV_ENTER, EV_FOCUS};
927 event_t ev_s[] = {'h', 'j', 'k', 'l', 'c', 'q', ' ',
928 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
929 event_t ev_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't', 'f', ' ',
930 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
931 EV_F1, EV_F2, EV_F3, EV_F4, EV_F5, EV_F6,
932 EV_F7, EV_F8, EV_F9, EV_F10, EV_F11, EV_F12,
933 EV_MOUSE1, EV_MOUSE3};
934 for (int i = 0; i < countof(ev_e); i++)
935 sys_watch(root, ev_e[i], MOD());
936 for (int i = 0; i < countof(ev_m); i++)
937 sys_watch(root, ev_m[i], MOD(.MODKEY=1));
938 for (int i = 0; i < countof(ev_s); i++)
939 sys_watch(root, ev_s[i], MOD(.MODKEY=1,.shift=1));
942 void wm_free(win_t *root)
944 /* Re-show and free all windows */
945 while ( wm->tags) { tag_t *tag = wm->tags->data;
946 while (tag->dpys) { dpy_t *dpy = tag->dpys->data;
947 while (dpy->cols) { col_t *col = dpy->cols->data;
948 while (col->rows) { row_t *row = col->rows->data;
949 sys_show(row->win, ST_SHOW);
951 col->rows = list_remove(col->rows, col->rows, 1); }
952 dpy->cols = list_remove(dpy->cols, dpy->cols, 1); }
953 while (dpy->flts) { flt_t *flt = dpy->flts->data;
954 sys_show(flt->win, ST_SHOW);
956 dpy->flts = list_remove(dpy->flts, dpy->flts, 1); }
957 tag->dpys = list_remove(tag->dpys, tag->dpys, 1); }
958 wm->tags = list_remove( wm->tags, wm->tags, 1); }
960 /* Free remaining data */