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
29 static int margin = 0;
30 static int stack = 25;
38 SPLIT, STACK, FULL, TAB
45 /* Window structure types */
49 win_t *win; // the window
50 int height; // win height in _this_ tag
51 state_t state; // state of window
55 list_t *rows; // of row_t
56 row_t *row; // focused row
57 int width; // column width
58 layout_t layout; // column layout
62 win_t *win; // the window
63 int x, y, w, h; // position of window (in this tag)
64 state_t state; // state of window
68 list_t *cols; // of col_t
69 col_t *col; // focused col
70 list_t *flts; // of flt_t
71 flt_t *flt; // focused flt
72 layer_t layer; // focused layer
73 win_t *geom; // display size and position
77 list_t *dpys; // of dpy_t
78 dpy_t *dpy; // focused dpy
79 char name[64]; // tag name
83 list_t *tags; // of tag_t
84 tag_t *tag; // focused tag
85 win_t *root; // root/background window
86 list_t *screens; // display geometry
89 #define WIN(node) ((win_t*)(node)->data)
90 #define ROW(node) ((row_t*)(node)->data)
91 #define COL(node) ((col_t*)(node)->data)
92 #define FLT(node) ((flt_t*)(node)->data)
93 #define DPY(node) ((dpy_t*)(node)->data)
94 #define TAG(node) ((tag_t*)(node)->data)
96 #define tag_foreach_col(tag, dpy, col, row, win) \
97 for (list_t *dpy = tag ->dpys; dpy; dpy = dpy->next) \
98 for (list_t *col = DPY(dpy)->cols; col; col = col->next) \
99 for (list_t *row = COL(col)->rows; row; row = row->next) \
100 for (win_t *win = ROW(row)->win; win; win = NULL)
102 #define tag_foreach_flt(tag, dpy, flt, win) \
103 for (list_t *dpy = tag ->dpys; dpy; dpy = dpy->next) \
104 for (list_t *flt = DPY(dpy)->flts; flt; flt = flt->next) \
105 for (win_t *win = FLT(flt)->win; win; win = NULL) \
107 /* Window management data
108 * wm_* macros represent the currently focused item
109 * _only_ wm_focus protects against NULL pointers */
111 #define wm_tag wm->tag
112 #define wm_dpy wm->tag->dpy
113 #define wm_col wm->tag->dpy->col
114 #define wm_row wm->tag->dpy->col->row
115 #define wm_flt wm->tag->dpy->flt
116 #define wm_focus get_focus()
118 /* Mouse drag data */
119 static drag_t move_mode;
120 static list_t *move_lrow;
121 static list_t *move_lcol;
122 static list_t *move_lflt;
123 static ptr_t move_prev;
124 static layer_t move_layer;
125 static struct { int v, h; } move_dir;
127 /********************
129 ********************/
130 static int sort_win(void *a, void *b)
132 return ((win_t*)a)->x > ((win_t*)b)->x ? 1 :
133 ((win_t*)a)->x < ((win_t*)b)->x ? -1 : 0;
136 static win_t *get_focus(void)
138 if (!wm_tag || !wm_dpy)
140 switch (wm_dpy->layer) {
142 return wm_col && wm_row ? wm_row->win : NULL;
144 return wm_flt ? wm_flt->win : NULL;
149 /* Search for the target window in a given tag
150 * win may exist in other tags as well */
151 static int searchl(tag_t *tag, win_t *target,
152 list_t **_dpy, list_t **_col, list_t **_row, list_t **_flt)
154 tag_foreach_col(tag, dpy, col, row, win) {
156 if (_dpy) *_dpy = dpy;
157 if (_col) *_col = col;
158 if (_row) *_row = row;
162 tag_foreach_flt(tag, dpy, flt, win) {
164 if (_dpy) *_dpy = dpy;
165 if (_flt) *_flt = flt;
172 static int search(tag_t *tag, win_t *target,
173 dpy_t **_dpy, col_t **_col, row_t **_row, flt_t **_flt)
175 list_t *dpy, *col, *row, *flt;
176 switch (searchl(tag, target, &dpy, &col, &row, &flt)) {
178 if (_dpy) *_dpy = DPY(dpy);
179 if (_col) *_col = COL(col);
180 if (_row) *_row = ROW(row);
183 if (_dpy) *_dpy = DPY(dpy);
184 if (_flt) *_flt = FLT(flt);
190 /* Set the layout for the windows column in the current tag */
191 static void set_mode(win_t *win, layout_t layout)
194 if (TILING != search(wm_tag, win, NULL, &col, NULL, NULL))
196 printf("set_mode: %p, %d -> %d\n",
197 col, col->layout, layout);
198 col->layout = layout;
199 if (col->layout == SPLIT)
200 for (list_t *cur = col->rows; cur; cur = cur->next) {
201 row_t *row = cur->data;
202 row->height = wm_dpy->geom->h;
207 /* Focus the window in the current tag and record
208 * it as the currently focused window */
209 static void set_focus(win_t *win)
211 if (win == NULL || win == wm->root) {
216 dpy_t *dpy; col_t *col; row_t *row; flt_t *flt;
217 switch (search(wm_tag, win, &dpy, &col, &row, &flt)) {
227 dpy->layer = FLOATING;
233 /* Save mouse start location when moving/resizing windows */
234 static void set_move(win_t *win, ptr_t ptr, drag_t drag)
236 printf("set_move: %d - %p@%d,%d\n",
237 drag, win, ptr.rx, ptr.ry);
239 if (drag == MOVE || drag == RESIZE) {
240 move_layer = searchl(wm_tag, win, NULL,
241 &move_lcol, &move_lrow, &move_lflt);
245 int midy = win->y + (win->h/2);
246 int midx = win->x + (win->w/2);
247 move_dir.v = ptr.ry < midy ? -1 : +1;
248 move_dir.h = ptr.rx < midx ? -1 : +1;
252 /* Print a text representation of the window layout
253 * Quite useful for debugging */
254 static void print_txt(void)
256 for (list_t *ltag = wm->tags; ltag; ltag = ltag->next) {
257 tag_t *tag = ltag->data;
258 printf("tag: <%-9p [%p->%p] >%-9p d=%-9p - %s\n",
259 ltag->prev, ltag, ltag->data, ltag->next,
260 tag->dpy, tag->name);
261 for (list_t *ldpy = tag->dpys; ldpy; ldpy = ldpy->next) {
262 dpy_t *dpy = ldpy->data;
263 win_t *geom = dpy->geom;
264 printf(" dpy: <%-9p [%p->%p] >%-9p %c=%-9p - %d,%d %dx%d\n",
265 ldpy->prev, ldpy, ldpy->data, ldpy->next,
266 dpy->layer == TILING ? 'c' : 'f',
267 dpy->layer == TILING ? (void*)dpy->col : (void*)dpy->flt,
268 geom->x, geom->y, geom->h, geom->w);
269 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
270 col_t *col = lcol->data;
271 printf(" col: <%-9p [%p->%p] >%-9p r=%-9p - %dpx @ %d\n",
272 lcol->prev, lcol, lcol->data, lcol->next,
273 col->row, col->width, col->layout);
274 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
275 row_t *row = lrow->data;
276 win_t *win = row->win;
277 printf(" win: <%-9p [%p>>%p] >%-9p focus=%d%d - %4dpx \n",
278 lrow->prev, lrow, win, lrow->next,
279 col->row == row, wm_focus == win, win->h);
281 for (list_t *lflt = dpy->flts; lflt; lflt = lflt->next) {
282 flt_t *flt = lflt->data;
283 win_t *win = flt->win;
284 printf(" flt: <%-9p [%p>>%p] >%-9p focus=%d%d - %d,%d %dx%d \n",
285 lflt->prev, lflt, win, lflt->next,
286 dpy->flt == flt, wm_focus == flt->win,
287 flt->x, flt->y, flt->h, flt->w);
291 /* Cleanly remove a window from a tag
292 * Determines the new focused row/col
293 * Prunes empty lists */
294 static layer_t cut_win(win_t *win, tag_t *tag)
296 list_t *ldpy, *lcol, *lrow, *lflt;
297 layer_t layer = searchl(tag, win, &ldpy, &lcol, &lrow, &lflt);
299 if (layer == TILING) {
300 dpy_t *dpy = DPY(ldpy);
301 col_t *col = COL(lcol);
302 col->row = lrow->prev ? lrow->prev->data :
303 lrow->next ? lrow->next->data : NULL;
304 col->rows = list_remove(col->rows, lrow, 1);
305 if (col->rows == NULL && (lcol->next || lcol->prev)) {
306 dpy->col = lcol->prev ? lcol->prev->data :
307 lcol->next ? lcol->next->data : NULL;
308 dpy->cols = list_remove(dpy->cols, lcol, 1);
312 if (layer == FLOATING) {
313 dpy_t *dpy = DPY(ldpy);
314 dpy->flts = list_remove(dpy->flts, lflt, 1);
315 dpy->flt = dpy->flts ? list_last(dpy->flts)->data : NULL;
316 if (!dpy->flt && dpy->col && dpy->col->row)
323 /* Insert a window into the tiling layer
324 * The window is added immediately after the
325 * columns currently focused row */
326 static void put_win_col(win_t *win, tag_t *tag, dpy_t *dpy, col_t *col)
328 row_t *row = new0(row_t);
330 row->state = win->state ?: ST_SHOW;
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;
365 flt->state = win->state ?: ST_SHOW;
367 flt->x = dpy->flt->x + 20;
368 flt->y = dpy->flt->y + 20;
370 dpy->flts = list_append(dpy->flts, flt);
373 tag->dpy->layer = FLOATING;
376 /* Insert a window into a tag */
377 static void put_win(win_t *win, tag_t *tag, layer_t layer)
380 put_win_col(win, tag, tag->dpy, tag->dpy->col);
381 if (layer == FLOATING)
382 put_win_flt(win, tag, tag->dpy);
385 /* Move a window up, down, left, or right
386 * This handles moving with a column, between
387 * columns, and between multiple monitors. */
388 static void shift_window(win_t *win, int col, int row)
391 printf("shift_window: %p - %+d,%+d\n", win, col, row);
393 printf("shift_window: >>>\n");
394 list_t *ldpy, *lcol, *lrow, *lflt;
395 if (TILING != searchl(wm_tag, win, &ldpy, &lcol, &lrow, &lflt))
397 dpy_t *dpy = ldpy->data;
399 /* Move with a column, just swap rows */
400 list_t *src = lrow, *dst = NULL;
401 if (row < 0) dst = src->prev;
402 if (row > 0) dst = src->next;
404 printf("swap: %p <-> %p\n", src->data, dst->data);
405 row_t *tmp = src->data;
406 src->data = dst->data;
411 /* Moving between columns */
412 int onlyrow = !lrow->prev && !lrow->next;
413 list_t *src = lcol, *dst = NULL;
416 /* Normal move between columns */
418 } else if (!onlyrow) {
419 /* Create new column */
420 dpy->cols = list_insert(dpy->cols, new0(col_t));
422 } else if (ldpy->prev) {
423 /* Move to next monitor */
424 dpy = ldpy->prev->data;
425 dst = list_last(dpy->cols);
427 /* We, shall, not, be,
428 * we shall not be moved */
435 } else if (!onlyrow) {
436 dpy->cols = list_append(dpy->cols, new0(col_t));
438 } else if (ldpy->next) {
439 dpy = ldpy->next->data;
445 cut_win(win, wm_tag);
446 put_win_col(win, wm_tag, dpy, dst ? dst->data : NULL);
454 /* Get next/prev item, with wraparound */
455 static list_t *get_next(list_t *list, int forward)
457 list_t *next = forward ? list->next : list->prev;
460 while ((list = forward ? next->prev : next->next))
466 /* Move keyboard focus in a given direction */
467 static void shift_focus(int cols, int rows)
469 printf("shift_focus: %+d,%+d\n", cols, rows);
470 if (rows != 0 && wm_focus) {
471 /* Move focus up/down */
472 list_t *dpy, *col, *row;
473 if (TILING != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
475 row_t *next = get_next(row, rows > 0)->data;
476 set_focus(next->win);
477 if (COL(col)->layout != SPLIT)
481 /* Move focus left/right */
482 list_t *dpy, *col, *row, *ndpy, *ncol = NULL;
484 /* Currently focused on a window */
485 if (TILING != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
487 ncol = cols > 0 ? col->next : col->prev;
489 /* Currently focused on an empty display */
490 dpy = list_find(wm_tag->dpys, wm_dpy);
493 /* Moving focus to a different display */
494 ndpy = get_next(dpy, cols > 0);
495 ncol = cols > 0 ? DPY(ndpy)->cols :
496 list_last(DPY(ndpy)->cols);
499 if (ncol && COL(ncol) && COL(ncol)->row)
500 set_focus(COL(ncol)->row->win);
506 /* Raise the window in the floating */
507 static void raise_float(win_t *win)
509 printf("raise_float: %p\n", win);
511 for (cur = wm_dpy->flts; cur; cur = cur->next)
512 if (FLT(cur)->win == win)
515 flt_t *flt = cur->data;
516 wm_dpy->flts = list_remove(wm_dpy->flts, cur, 0);
517 wm_dpy->flts = list_append(wm_dpy->flts, flt);
522 /* Toggle between floating and tiling layers */
523 static void switch_layer(void)
525 printf("switch_float: %p %d\n",
526 wm_dpy, wm_dpy->layer);
527 wm_dpy->layer = !wm_dpy->layer;
531 /* Move current window between floating and tiling layers */
532 static void set_layer(win_t *win)
535 printf("set_float: %p %p\n", wm_dpy, win);
536 wm_dpy->layer = !cut_win(win, wm_tag);
537 put_win(win, wm_tag, wm_dpy->layer);
541 /* Allocate a new tag */
542 static tag_t *tag_new(list_t *screens, const char *name)
544 tag_t *tag = new0(tag_t);
545 strncpy(tag->name, name, sizeof(tag->name));
546 for (list_t *cur = screens; cur; cur = cur->next) {
547 dpy_t *dpy = new0(dpy_t);
548 dpy->geom = cur->data;
549 tag->dpys = list_append(tag->dpys, dpy);
551 tag->dpy = tag->dpys->data;
552 for (list_t *dpy = tag->dpys; dpy; dpy = dpy->next)
553 if (DPY(dpy)->geom->z > tag->dpy->geom->z)
554 tag->dpy = dpy->data;
559 * If it does not exist it is based on the
560 * display geometry in wm->screens */
561 static tag_t *tag_find(const char *name)
564 for (list_t *cur = wm->tags; cur; cur = cur->next)
565 if (!strcmp(name, TAG(cur)->name)) {
570 tag = tag_new(wm->screens, name);
571 wm->tags = list_append(wm->tags, tag);
576 /* Move the window from the current tag to the new tag
577 * Unlike wmii, only remove the current tag, not all tags */
578 static void tag_set(win_t *win, const char *name)
580 printf("tag_set: %p %s\n", win, name);
581 if (!strcmp(wm_tag->name, name))
583 tag_t *tag = tag_find(name);
584 layer_t layer = cut_win(win, wm_tag);
585 put_win(win, tag, layer);
589 /* Switch to a different tag */
590 static void tag_switch(const char *name)
592 printf("tag_switch: %s\n", name);
594 if ((wm_col == NULL || wm_row == NULL) && wm_flt == NULL) {
596 dpy_t *dpy = old->dpys->data;
598 dpy->cols = list_remove(dpy->cols, dpy->cols, 1);
599 old->dpys = list_remove(old->dpys, old->dpys, 1);
601 list_t *ltag = list_find(wm->tags, old);
602 wm->tags = list_remove(wm->tags, ltag, 1);
604 wm_tag = tag_find(name);
607 /* Tile all windows in the given display
608 * This performs all the actual window tiling
609 * Currently supports split, stack and maximized modes */
610 static void wm_update_cols(dpy_t *dpy)
612 int x=0, y=0; // Current window top-left position
613 int tx=0, ty=0; // Total size (sum of initial col widths and row heights w/o margin)
614 int mx=0, my=0; // Maximum usable size (screen size minus margins)
615 int sy=0; // Stack size (height of focused stack window)
617 /* Scale horizontally */
619 mx = dpy->geom->w - (list_length(dpy->cols)+1)*margin;
620 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
621 tx += COL(lcol)->width;
622 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
623 COL(lcol)->width *= (float)mx / tx;
625 /* Scale each column vertically */
626 win_t *focus = get_focus();
627 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
628 col_t *col = lcol->data;
629 int nrows = list_length(col->rows);
631 for (list_t *lrow = col->rows; lrow; lrow = lrow->next)
632 ty += ROW(lrow)->height;
634 my = dpy->geom->h - (margin + (nrows-1)* margin + margin);
635 sy = dpy->geom->h - (margin + (nrows-1)*(margin/2) + margin)
637 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
638 win_t *win = ROW(lrow)->win;
639 if (ROW(lrow)->state != ST_SHOW) {
640 sys_show(win, ROW(lrow)->state);
643 win->h = ROW(lrow)->height;
644 state_t state = ST_SHOW;
646 switch (col->layout) {
648 sys_move(win, x+margin, y+margin,
649 col->width, win->h * ((float)my / ty));
651 y += height + margin;
654 if (lrow->next && ROW(lrow->next)->win == col->row->win) {
655 /* Hack to prevent flashing */
656 win_t *next = ROW(lrow->next)->win;
657 sys_move(next, x+margin, y+margin+stack+margin/2,
659 sys_show(next, ST_SHOW);
661 int isfocus = win == col->row->win;
662 state = isfocus ? ST_SHOW : ST_SHADE;
663 sys_show(win, state);
664 sys_move(win, x+margin, y+margin, col->width, sy);
665 y += (isfocus ? sy : stack) + (margin/2);
669 sys_move(win, x+margin, 0+margin,
670 col->width, dpy->geom->h-2*margin);
673 sys_show(win, state);
676 ROW(lrow)->height = win->h;
678 x += col->width + margin;
682 /*******************************
683 * Window management functions *
684 *******************************/
687 /* Updates window sizes */
688 for (list_t *ldpy = wm_tag->dpys; ldpy; ldpy = ldpy->next)
689 wm_update_cols(ldpy->data);
690 tag_foreach_flt(wm_tag, ldpy, lflt, win) {
691 flt_t *flt = lflt->data;
692 sys_move(win, flt->x, flt->y, flt->w, flt->h);
694 sys_show(flt->win, flt->state);
697 /* Hide other tags */
698 for (list_t *tag = wm ->tags; tag; tag = tag->next)
699 if (tag->data != wm_tag) {
700 tag_foreach_col(TAG(tag), dpy, col, row, win)
701 sys_show(win, ST_HIDE);
702 tag_foreach_flt(TAG(tag), dpy, flt, win)
703 sys_show(win, ST_HIDE);
706 /* Set focused window */
711 int wm_handle_event(win_t *win, event_t ev, mod_t mod, ptr_t ptr)
713 if (!win || win == wm_dpy->geom) return 0;
714 //printf("wm_handle_event: %p - %x %c%c%c%c%c\n", win, ev,
715 // mod.up ? '^' : 'v',
716 // mod.alt ? 'a' : '-',
717 // mod.ctrl ? 'c' : '-',
718 // mod.shift ? 's' : '-',
719 // mod.win ? 'w' : '-');
722 if (EV_MOUSE0 <= ev && ev <= EV_MOUSE7) {
723 if (ev == EV_MOUSE1 && !mod.MODKEY && !mod.up)
724 return raise_float(win), 0;
725 if ((ev == EV_MOUSE3 && mod.MODKEY && !mod.up) ||
726 (ev == EV_MOUSE1 && mod.MODKEY && !mod.up && mod.shift))
727 return set_move(win,ptr,RESIZE), 1;
728 if (ev == EV_MOUSE1 && mod.MODKEY && !mod.up)
729 return set_move(win,ptr,MOVE), 1;
730 if (move_mode != NONE && mod.up)
731 return set_move(win,ptr,NONE), 1;
732 if (ev == EV_MOUSE1 && !mod.up && win->state == ST_SHADE)
733 return set_focus(win), wm_update(), 0;
737 /* Only handle key-down */
744 if (ev == EV_F1) return raise_float(win), 1;
745 if (ev == EV_F2) return set_focus(win), 1;
746 if (ev == EV_F3) return sys_show(win, ST_SHOW), 1;
747 if (ev == EV_F4) return sys_show(win, ST_HIDE), 1;
748 if (ev == EV_F7) return sys_show(win, ST_SHADE), 1;
750 if (ev == EV_F5) return wm_update(), 1;
751 if (ev == EV_F6) return print_txt(), 1;
752 if (ev == 'q') return sys_exit(), 1;
753 if (ev == 'f') return wm_handle_state(win, win->state,
754 win->state == ST_FULL ? ST_SHOW : ST_FULL);
756 if (mod.MODKEY && mod.shift) {
757 if (ev == 'c') return sys_show(win, ST_CLOSE), 1;
762 if (mod.MODKEY && mod.shift)
763 return set_layer(win), 1;
765 return switch_layer(), 1;
768 /* Movement commands */
769 if (mod.MODKEY && mod.shift) {
771 case 'h': return shift_window(wm_focus,-1, 0), 1;
772 case 'j': return shift_window(wm_focus, 0,+1), 1;
773 case 'k': return shift_window(wm_focus, 0,-1), 1;
774 case 'l': return shift_window(wm_focus,+1, 0), 1;
778 else if (mod.MODKEY) {
780 case 'h': return shift_focus(-1, 0), 1;
781 case 'j': return shift_focus( 0,+1), 1;
782 case 'k': return shift_focus( 0,-1), 1;
783 case 'l': return shift_focus(+1, 0), 1;
788 /* Column layout commands */
791 case 'd': return set_mode(win, SPLIT), 1;
792 case 's': return set_mode(win, STACK), 1;
793 case 'm': return set_mode(win, FULL), 1;
794 case 't': return set_mode(win, TAB), 1;
800 if (mod.MODKEY && '0' <= ev && ev <= '9') {
801 char name[] = {ev, '\0'};
810 if (ev == EV_ENTER && win->state != ST_SHADE)
811 return set_focus(win), 1;
813 /* Reset focus after after focus change,
814 * not sure what is causing the focus change in the first place
815 * but preventing that would be a better solution */
817 sys_focus(wm_focus ?: wm->root);
822 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
824 //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
825 // cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
827 if (move_mode == NONE)
830 int dx = ptr.rx - move_prev.rx;
831 int dy = ptr.ry - move_prev.ry;
834 if (move_layer == TILING && move_mode == RESIZE) {
835 list_t *vert = move_dir.v < 0 ? move_lrow->prev : move_lrow->next;
836 list_t *horz = move_dir.h < 0 ? move_lcol->prev : move_lcol->next;
838 ROW(move_lrow)->height += move_dir.v * dy;
839 ROW(vert)->height -= move_dir.v * dy;
842 COL(move_lcol)->width += move_dir.h * dx;
843 COL(horz)->width -= move_dir.h * dx;
848 if (move_layer == FLOATING) {
849 flt_t *flt = move_lflt->data;
850 win_t *win = flt->win;
851 if (move_mode == MOVE)
852 sys_move(win, win->x+dx, win->y+dy, win->w, win->h);
853 else if (move_mode == RESIZE)
855 win->x + dx * (move_dir.h < 0),
856 win->y + dy * (move_dir.v < 0),
857 win->w + dx * move_dir.h,
858 win->h + dy * move_dir.v);
859 flt->x = win->x; flt->y = win->y;
860 flt->w = win->w; flt->h = win->h;
866 int wm_handle_state(win_t *win, state_t prev, state_t next)
871 search(wm_tag, win, NULL, NULL, &row, &flt);
873 if (row) row->state = next;
874 if (flt) flt->state = next;
876 if (prev == ST_MAX || prev == ST_FULL ||
877 next == ST_MAX || next == ST_FULL)
883 void wm_insert(win_t *win)
885 printf("wm_insert: %p\n", win);
888 /* Initialize window */
889 win->wm = new0(win_wm_t);
890 sys_watch(win, EV_ENTER, MOD());
891 sys_watch(win, EV_FOCUS, MOD());
894 if (win->type == TYPE_DIALOG || win->parent)
895 wm_dpy->layer = FLOATING;
896 put_win(win, wm_tag, wm_dpy->layer);
904 void wm_remove(win_t *win)
906 printf("wm_remove: %p\n", win);
908 for (list_t *tag = wm->tags; tag; tag = tag->next)
909 cut_win(win, tag->data);
916 void wm_init(win_t *root)
918 printf("wm_init: %p\n", root);
920 /* Load configuration */
921 margin = conf_get_int("main.margin", margin);
922 stack = conf_get_int("main.stack", stack);
926 wm->screens = list_sort(sys_info(root), 0, sort_win);
927 wm->tag = tag_new(wm->screens, "1");
928 wm->tags = list_insert(NULL, wm->tag);
930 event_t ev_e[] = {EV_ENTER, EV_FOCUS};
931 event_t ev_s[] = {'h', 'j', 'k', 'l', 'c', 'q', ' ',
932 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
933 EV_MOUSE1, EV_MOUSE3};
934 event_t ev_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't', 'f', ' ',
935 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
936 EV_F1, EV_F2, EV_F3, EV_F4, EV_F5, EV_F6,
937 EV_F7, EV_F8, EV_F9, EV_F10, EV_F11, EV_F12,
938 EV_MOUSE1, EV_MOUSE3};
939 for (int i = 0; i < countof(ev_e); i++)
940 sys_watch(root, ev_e[i], MOD());
941 for (int i = 0; i < countof(ev_m); i++)
942 sys_watch(root, ev_m[i], MOD(.MODKEY=1));
943 for (int i = 0; i < countof(ev_s); i++)
944 sys_watch(root, ev_s[i], MOD(.MODKEY=1,.shift=1));
947 void wm_free(win_t *root)
949 /* Re-show and free all windows */
950 while ( wm->tags) { tag_t *tag = wm->tags->data;
951 while (tag->dpys) { dpy_t *dpy = tag->dpys->data;
952 while (dpy->cols) { col_t *col = dpy->cols->data;
953 while (col->rows) { row_t *row = col->rows->data;
954 sys_show(row->win, ST_SHOW);
956 col->rows = list_remove(col->rows, col->rows, 1); }
957 dpy->cols = list_remove(dpy->cols, dpy->cols, 1); }
958 while (dpy->flts) { flt_t *flt = dpy->flts->data;
959 sys_show(flt->win, ST_SHOW);
961 dpy->flts = list_remove(dpy->flts, dpy->flts, 1); }
962 tag->dpys = list_remove(tag->dpys, tag->dpys, 1); }
963 wm->tags = list_remove( wm->tags, wm->tags, 1); }
965 /* Free remaining data */