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, 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 mode_t mode; // display mode
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 mode for the windows column in the current tag */
190 static void set_mode(win_t *win, mode_t mode)
193 if (TILING != search(wm_tag, win, NULL, &col, NULL, NULL))
195 printf("set_mode: %p, %d -> %d\n",
196 col, col->mode, mode);
198 if (col->mode == 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 /* - Only grab mouse button on unfocused window,
216 * this prevents stealing all mouse clicks from client windows,
217 * - A better way may be to re-send mouse clicks to client windows
218 * using the return value from wm_handle_key */
219 for (int i = EV_MOUSE1; i < EV_MOUSE7; i++) {
221 sys_watch(wm_focus, i, MOD());
222 sys_unwatch(win, i, MOD());
225 dpy_t *dpy; col_t *col; row_t *row; flt_t *flt;
226 switch (search(wm_tag, win, &dpy, &col, &row, &flt)) {
236 dpy->layer = FLOATING;
242 /* Save mouse start location when moving/resizing windows */
243 static void set_move(win_t *win, ptr_t ptr, drag_t drag)
245 printf("set_move: %d - %p@%d,%d\n",
246 drag, win, ptr.rx, ptr.ry);
248 if (drag == MOVE || drag == RESIZE) {
249 move_layer = searchl(wm_tag, win, NULL,
250 &move_lcol, &move_lrow, &move_lflt);
254 int midy = win->y + (win->h/2);
255 int midx = win->x + (win->w/2);
256 move_dir.v = ptr.ry < midy ? -1 : +1;
257 move_dir.h = ptr.rx < midx ? -1 : +1;
261 /* Print a text representation of the window layout
262 * Quite useful for debugging */
263 static void print_txt(void)
265 for (list_t *ltag = wm->tags; ltag; ltag = ltag->next) {
266 tag_t *tag = ltag->data;
267 printf("tag: <%-9p [%p->%p] >%-9p d=%-9p - %d\n",
268 ltag->prev, ltag, ltag->data, ltag->next,
269 tag->dpy, tag->name);
270 for (list_t *ldpy = tag->dpys; ldpy; ldpy = ldpy->next) {
271 dpy_t *dpy = ldpy->data;
272 win_t *geom = dpy->geom;
273 printf(" dpy: <%-9p [%p->%p] >%-9p %c=%-9p - %d,%d %dx%d\n",
274 ldpy->prev, ldpy, ldpy->data, ldpy->next,
275 dpy->layer == TILING ? 'c' : 'f',
276 dpy->layer == TILING ? (void*)dpy->col : (void*)dpy->flt,
277 geom->x, geom->y, geom->h, geom->w);
278 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
279 col_t *col = lcol->data;
280 printf(" col: <%-9p [%p->%p] >%-9p r=%-9p - %dpx @ %d\n",
281 lcol->prev, lcol, lcol->data, lcol->next,
282 col->row, col->width, col->mode);
283 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
284 row_t *row = lrow->data;
285 win_t *win = row->win;
286 printf(" win: <%-9p [%p>>%p] >%-9p focus=%d%d - %4dpx \n",
287 lrow->prev, lrow, win, lrow->next,
288 col->row == row, wm_focus == win, win->h);
290 for (list_t *lflt = dpy->flts; lflt; lflt = lflt->next) {
291 flt_t *flt = lflt->data;
292 win_t *win = flt->win;
293 printf(" flt: <%-9p [%p>>%p] >%-9p focus=%d%d - %d,%d %dx%d \n",
294 lflt->prev, lflt, win, lflt->next,
295 dpy->flt == flt, wm_focus == flt->win,
296 flt->x, flt->y, flt->h, flt->w);
300 /* Cleanly remove a window from a tag
301 * Determines the new focused row/col
302 * Prunes empty lists */
303 static layer_t cut_win(win_t *win, tag_t *tag)
305 list_t *ldpy, *lcol, *lrow, *lflt;
306 layer_t layer = searchl(tag, win, &ldpy, &lcol, &lrow, &lflt);
308 if (layer == TILING) {
309 dpy_t *dpy = DPY(ldpy);
310 col_t *col = COL(lcol);
311 col->row = lrow->prev ? lrow->prev->data :
312 lrow->next ? lrow->next->data : NULL;
313 col->rows = list_remove(col->rows, lrow, 1);
314 if (col->rows == NULL && (lcol->next || lcol->prev)) {
315 dpy->col = lcol->prev ? lcol->prev->data :
316 lcol->next ? lcol->next->data : NULL;
317 dpy->cols = list_remove(dpy->cols, lcol, 1);
321 if (layer == FLOATING) {
322 dpy_t *dpy = DPY(ldpy);
323 dpy->flts = list_remove(dpy->flts, lflt, 1);
324 dpy->flt = dpy->flts ? list_last(dpy->flts)->data : NULL;
325 if (!dpy->flt && dpy->col && dpy->col->row)
332 /* Insert a window into the tiling layer
333 * The window is added immediately after the
334 * columns currently focused row */
335 static void put_win_col(win_t *win, tag_t *tag, dpy_t *dpy, col_t *col)
337 row_t *row = new0(row_t);
342 dpy->cols = list_insert(dpy->cols, col);
345 int nrows = list_length(col->rows);
347 list_t *prev = list_find(col->rows, col->row);
348 list_insert_after(prev, row);
350 col->rows = list_insert(col->rows, row);
354 tag->dpy->col->row = row;
355 tag->dpy->layer = TILING;
357 row->height = dpy->geom->h / MAX(nrows,1);
359 int ncols = list_length(dpy->cols);
360 col->width = dpy->geom->w / MAX(ncols-1,1);
364 /* Insert a window into the floating layer */
365 static void put_win_flt(win_t *win, tag_t *tag, dpy_t *dpy)
367 flt_t *flt = new0(flt_t);
369 flt->w = dpy->geom->w / 2;
370 flt->h = dpy->geom->h / 2;
371 flt->x = dpy->geom->x + flt->w / 2;
372 flt->y = dpy->geom->y + flt->h / 2;
374 flt->x = dpy->flt->x + 20;
375 flt->y = dpy->flt->y + 20;
377 dpy->flts = list_append(dpy->flts, flt);
380 tag->dpy->layer = FLOATING;
383 /* Insert a window into a tag */
384 static void put_win(win_t *win, tag_t *tag, layer_t layer)
387 put_win_col(win, tag, tag->dpy, tag->dpy->col);
388 if (layer == FLOATING)
389 put_win_flt(win, tag, tag->dpy);
392 /* Move a window up, down, left, or right
393 * This handles moving with a column, between
394 * columns, and between multiple monitors. */
395 static void shift_window(win_t *win, int col, int row)
398 printf("shift_window: %p - %+d,%+d\n", win, col, row);
400 printf("shift_window: >>>\n");
401 list_t *ldpy, *lcol, *lrow, *lflt;
402 if (TILING != searchl(wm_tag, win, &ldpy, &lcol, &lrow, &lflt))
404 dpy_t *dpy = ldpy->data;
406 /* Move with a column, just swap rows */
407 list_t *src = lrow, *dst = NULL;
408 if (row < 0) dst = src->prev;
409 if (row > 0) dst = src->next;
411 printf("swap: %p <-> %p\n", src->data, dst->data);
412 row_t *tmp = src->data;
413 src->data = dst->data;
418 /* Moving between columns */
419 int onlyrow = !lrow->prev && !lrow->next;
420 list_t *src = lcol, *dst = NULL;
423 /* Normal move between columns */
425 } else if (!onlyrow) {
426 /* Create new column */
427 dpy->cols = list_insert(dpy->cols, new0(col_t));
429 } else if (ldpy->prev) {
430 /* Move to next monitor */
431 dpy = ldpy->prev->data;
432 dst = list_last(dpy->cols);
434 /* We, shall, not, be,
435 * we shall not be moved */
442 } else if (!onlyrow) {
443 dpy->cols = list_append(dpy->cols, new0(col_t));
445 } else if (ldpy->next) {
446 dpy = ldpy->next->data;
452 cut_win(win, wm_tag);
453 put_win_col(win, wm_tag, dpy, dst ? dst->data : NULL);
461 /* Get next/prev item, with wraparound */
462 static list_t *get_next(list_t *list, int forward)
464 list_t *next = forward ? list->next : list->prev;
467 while ((list = forward ? next->prev : next->next))
473 /* Move keyboard focus in a given direction */
474 static void shift_focus(int cols, int rows)
476 printf("shift_focus: %+d,%+d\n", cols, rows);
477 if (rows != 0 && wm_focus) {
478 /* Move focus up/down */
479 list_t *dpy, *col, *row;
480 if (TILING != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
482 row_t *next = get_next(row, rows > 0)->data;
483 set_focus(next->win);
484 if (COL(col)->mode != SPLIT)
488 /* Move focus left/right */
489 list_t *dpy, *col, *row, *ndpy, *ncol = NULL;
491 /* Currently focused on a window */
492 if (TILING != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
494 ncol = cols > 0 ? col->next : col->prev;
496 /* Currently focused on an empty display */
497 dpy = list_find(wm_tag->dpys, wm_dpy);
500 /* Moving focus to a different display */
501 ndpy = get_next(dpy, cols > 0);
502 ncol = cols > 0 ? DPY(ndpy)->cols :
503 list_last(DPY(ndpy)->cols);
506 if (ncol && COL(ncol) && COL(ncol)->row)
507 set_focus(COL(ncol)->row->win);
513 /* Raise the window in the floating */
514 static void raise_float(win_t *win)
516 printf("raise_float: %p\n", win);
518 for (cur = wm_dpy->flts; cur; cur = cur->next)
519 if (FLT(cur)->win == win)
522 flt_t *flt = cur->data;
523 wm_dpy->flts = list_remove(wm_dpy->flts, cur, 0);
524 wm_dpy->flts = list_append(wm_dpy->flts, flt);
529 /* Toggle between floating and tiling layers */
530 static void switch_layer(void)
532 printf("switch_float: %p %d\n",
533 wm_dpy, wm_dpy->layer);
534 wm_dpy->layer = !wm_dpy->layer;
538 /* Move current window between floating and tiling layers */
539 static void set_layer(win_t *win)
542 printf("set_float: %p %p\n", wm_dpy, win);
543 wm_dpy->layer = !cut_win(win, wm_tag);
544 put_win(win, wm_tag, wm_dpy->layer);
548 /* Allocate a new tag */
549 static tag_t *tag_new(list_t *screens, int name)
551 tag_t *tag = new0(tag_t);
553 for (list_t *cur = screens; cur; cur = cur->next) {
554 dpy_t *dpy = new0(dpy_t);
555 dpy->geom = cur->data;
556 tag->dpys = list_append(tag->dpys, dpy);
558 tag->dpy = tag->dpys->data;
559 for (list_t *dpy = tag->dpys; dpy; dpy = dpy->next)
560 if (DPY(dpy)->geom->z > tag->dpy->geom->z)
561 tag->dpy = dpy->data;
566 * If it does not exist it is based on the
567 * display geometry in wm->screens */
568 static tag_t *tag_find(int name)
571 for (list_t *cur = wm->tags; cur; cur = cur->next)
572 if (name == TAG(cur)->name) {
577 tag = tag_new(wm->screens, name);
578 wm->tags = list_append(wm->tags, tag);
583 /* Move the window from the current tag to the new tag
584 * Unlike wmii, only remove the current tag, not all tags */
585 static void tag_set(win_t *win, int name)
587 printf("tag_set: %p %d\n", win, name);
588 if (wm_tag->name == name)
590 tag_t *tag = tag_find(name);
591 layer_t layer = cut_win(win, wm_tag);
592 put_win(win, tag, layer);
596 /* Switch to a different tag */
597 static void tag_switch(int name)
599 printf("tag_switch: %d\n", name);
601 if ((wm_col == NULL || wm_row == NULL) && wm_flt == NULL) {
603 dpy_t *dpy = old->dpys->data;
605 dpy->cols = list_remove(dpy->cols, dpy->cols, 1);
606 old->dpys = list_remove(old->dpys, old->dpys, 1);
608 list_t *ltag = list_find(wm->tags, old);
609 wm->tags = list_remove(wm->tags, ltag, 1);
611 wm_tag = tag_find(name);
614 /* Tile all windows in the given display
615 * This performs all the actual window tiling
616 * Currently supports split, stack and maximized modes */
617 static void wm_update_cols(dpy_t *dpy)
619 int x=0, y=0; // Current window top-left position
620 int tx=0, ty=0; // Total size (sum of initial col widths and row heights w/o margin)
621 int mx=0, my=0; // Maximum usable size (screen size minus margins)
622 int sy=0; // Stack size (height of focused stack window)
624 /* Scale horizontally */
626 mx = dpy->geom->w - (list_length(dpy->cols)+1)*margin;
627 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
628 tx += COL(lcol)->width;
629 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
630 COL(lcol)->width *= (float)mx / tx;
632 /* Scale each column vertically */
633 win_t *focus = get_focus();
634 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
635 col_t *col = lcol->data;
636 int nrows = list_length(col->rows);
638 for (list_t *lrow = col->rows; lrow; lrow = lrow->next)
639 ty += ROW(lrow)->height;
641 my = dpy->geom->h - (margin + (nrows-1)* margin + margin);
642 sy = dpy->geom->h - (margin + (nrows-1)*(margin/2) + margin)
644 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
645 win_t *win = ROW(lrow)->win;
646 win->h = ROW(lrow)->height;
647 state_t state = ST_SHOW;
651 sys_move(win, x+margin, y+margin,
652 col->width, win->h * ((float)my / ty));
654 y += height + margin;
657 if (lrow->next && ROW(lrow->next)->win == col->row->win) {
658 /* Hack to prevent flashing */
659 win_t *next = ROW(lrow->next)->win;
660 sys_move(next, x+margin, y+margin+stack+margin/2,
662 sys_show(next, ST_SHOW);
664 int isfocus = win == col->row->win;
665 state = isfocus ? ST_SHOW : ST_SHADE;
666 sys_show(win, state);
667 sys_move(win, x+margin, y+margin, col->width, sy);
668 y += (isfocus ? sy : stack) + (margin/2);
672 sys_move(win, x+margin, 0+margin,
673 col->width, dpy->geom->h-2*margin);
676 ROW(lrow)->state = state;
677 sys_show(win, state);
680 ROW(lrow)->height = win->h;
682 x += col->width + margin;
686 /*******************************
687 * Window management functions *
688 *******************************/
692 tag_foreach_col(wm_tag, dpy, col, row, win)
693 sys_show(win, ROW(row)->state);
694 tag_foreach_flt(wm_tag, dpy, flt, win)
695 sys_show(win, FLT(flt)->state);
696 for (list_t *tag = wm ->tags; tag; tag = tag->next)
697 if (tag->data != wm_tag) {
698 tag_foreach_col(TAG(tag), dpy, col, row, win)
699 sys_show(win, ST_HIDE);
700 tag_foreach_flt(TAG(tag), dpy, flt, win)
701 sys_show(win, ST_HIDE);
704 /* Refresh the display */
705 for (list_t *ldpy = wm_tag->dpys; ldpy; ldpy = ldpy->next)
706 wm_update_cols(ldpy->data);
707 tag_foreach_flt(wm_tag, ldpy, lflt, win) {
708 flt_t *flt = lflt->data;
709 sys_move(win, flt->x, flt->y, flt->w, flt->h);
716 int wm_handle_event(win_t *win, event_t ev, mod_t mod, ptr_t ptr)
718 if (!win || win == wm_dpy->geom) return 0;
719 //printf("wm_handle_event: %p - %x %c%c%c%c%c\n", win, ev,
720 // mod.up ? '^' : 'v',
721 // mod.alt ? 'a' : '-',
722 // mod.ctrl ? 'c' : '-',
723 // mod.shift ? 's' : '-',
724 // mod.win ? 'w' : '-');
729 if (EV_MOUSE0 <= ev && ev <= EV_MOUSE7) {
730 if (ev == EV_MOUSE1 && mod.MODKEY && !mod.up)
731 return set_move(win,ptr,MOVE), 1;
732 if (ev == EV_MOUSE3 && mod.MODKEY && !mod.up)
733 return set_move(win,ptr,RESIZE), 1;
734 if (move_mode != NONE && mod.up)
735 return set_move(win,ptr,NONE), 1;
736 if (ev == EV_MOUSE1 && !mod.up && win->state == ST_SHADE)
737 return set_focus(win), wm_update(), 0;
741 /* Only handle key-down */
748 if (ev == EV_F1) return raise_float(win), 1;
749 if (ev == EV_F2) return set_focus(win), 1;
750 if (ev == EV_F3) return sys_show(win, ST_SHOW), 1;
751 if (ev == EV_F4) return sys_show(win, ST_HIDE), 1;
752 if (ev == EV_F7) return sys_show(win, ST_SHADE), 1;
754 if (ev == EV_F5) return wm_update(), 1;
755 if (ev == EV_F6) return print_txt(), 1;
756 if (ev == 'q') return sys_exit(), 1;
758 if (mod.MODKEY && mod.shift) {
759 if (ev == 'c') return sys_show(win, ST_CLOSE), 1;
764 if (mod.MODKEY && mod.shift)
765 return set_layer(win), 1;
767 return switch_layer(), 1;
770 /* Movement commands */
771 if (mod.MODKEY && mod.shift) {
773 case 'h': return shift_window(wm_focus,-1, 0), 1;
774 case 'j': return shift_window(wm_focus, 0,+1), 1;
775 case 'k': return shift_window(wm_focus, 0,-1), 1;
776 case 'l': return shift_window(wm_focus,+1, 0), 1;
780 else if (mod.MODKEY) {
782 case 'h': return shift_focus(-1, 0), 1;
783 case 'j': return shift_focus( 0,+1), 1;
784 case 'k': return shift_focus( 0,-1), 1;
785 case 'l': return shift_focus(+1, 0), 1;
790 /* Column mode commands */
793 case 'd': return set_mode(win, SPLIT), 1;
794 case 's': return set_mode(win, STACK), 1;
795 case 'm': return set_mode(win, FULL), 1;
796 case 't': return set_mode(win, TAB), 1;
802 if (mod.MODKEY && '0' <= ev && ev <= '9') {
812 if (ev == EV_ENTER && win->state != ST_SHADE)
813 return set_focus(win), 1;
815 if (EV_MOUSE0 <= ev && ev <= EV_MOUSE7)
816 return set_focus(win), 0;
818 /* Reset focus after after focus change,
819 * not sure what is causing the focus change in the first place
820 * but preventing that would be a better solution */
822 sys_focus(wm_focus ?: wm->root);
827 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
829 //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
830 // cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
832 if (move_mode == NONE)
835 int dx = ptr.rx - move_prev.rx;
836 int dy = ptr.ry - move_prev.ry;
839 if (move_layer == TILING && move_mode == RESIZE) {
840 list_t *vert = move_dir.v < 0 ? move_lrow->prev : move_lrow->next;
841 list_t *horz = move_dir.h < 0 ? move_lcol->prev : move_lcol->next;
843 ROW(move_lrow)->height += move_dir.v * dy;
844 ROW(vert)->height -= move_dir.v * dy;
847 COL(move_lcol)->width += move_dir.h * dx;
848 COL(horz)->width -= move_dir.h * dx;
853 if (move_layer == FLOATING) {
854 flt_t *flt = move_lflt->data;
855 win_t *win = flt->win;
856 if (move_mode == MOVE)
857 sys_move(win, win->x+dx, win->y+dy, win->w, win->h);
858 else if (move_mode == RESIZE)
860 win->x + dx * (move_dir.h < 0),
861 win->y + dy * (move_dir.v < 0),
862 win->w + dx * move_dir.h,
863 win->h + dy * move_dir.v);
864 flt->x = win->x; flt->y = win->y;
865 flt->w = win->w; flt->h = win->h;
871 void wm_insert(win_t *win)
873 printf("wm_insert: %p\n", win);
876 /* Initialize window */
877 win->wm = new0(win_wm_t);
878 sys_watch(win, EV_ENTER, MOD());
879 sys_watch(win, EV_FOCUS, MOD());
882 put_win(win, wm_tag, wm_dpy->layer);
890 void wm_remove(win_t *win)
892 printf("wm_remove: %p\n", win);
894 for (list_t *tag = wm->tags; tag; tag = tag->next)
895 cut_win(win, tag->data);
902 void wm_init(win_t *root)
904 printf("wm_init: %p\n", root);
906 /* Load configuration */
907 margin = conf_get_int("main.margin", margin);
908 stack = conf_get_int("main.stack", stack);
912 wm->screens = list_sort(sys_info(root), 0, sort_win);
913 wm->tag = tag_new(wm->screens, 1);
914 wm->tags = list_insert(NULL, wm->tag);
916 event_t ev_e[] = {EV_ENTER, EV_FOCUS};
917 event_t ev_s[] = {'h', 'j', 'k', 'l', 'c', 'q', ' ',
918 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
919 event_t ev_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't', ' ',
920 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
921 EV_F1, EV_F2, EV_F3, EV_F4, EV_F5, EV_F6,
922 EV_F7, EV_F8, EV_F9, EV_F10, EV_F11, EV_F12,
923 EV_MOUSE1, EV_MOUSE3};
924 for (int i = 0; i < countof(ev_e); i++)
925 sys_watch(root, ev_e[i], MOD());
926 for (int i = 0; i < countof(ev_m); i++)
927 sys_watch(root, ev_m[i], MOD(.MODKEY=1));
928 for (int i = 0; i < countof(ev_s); i++)
929 sys_watch(root, ev_s[i], MOD(.MODKEY=1,.shift=1));
932 void wm_free(win_t *root)
934 /* Re-show and free all windows */
935 while ( wm->tags) { tag_t *tag = wm->tags->data;
936 while (tag->dpys) { dpy_t *dpy = tag->dpys->data;
937 while (dpy->cols) { col_t *col = dpy->cols->data;
938 while (col->rows) { row_t *row = col->rows->data;
939 sys_show(row->win, ST_SHOW);
941 col->rows = list_remove(col->rows, col->rows, 1); }
942 dpy->cols = list_remove(dpy->cols, dpy->cols, 1); }
943 while (dpy->flts) { flt_t *flt = dpy->flts->data;
944 sys_show(flt->win, ST_SHOW);
946 dpy->flts = list_remove(dpy->flts, dpy->flts, 1); }
947 tag->dpys = list_remove(tag->dpys, tag->dpys, 1); }
948 wm->tags = list_remove( wm->tags, wm->tags, 1); }
950 /* Free remaining data */