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
30 static int margin = 0;
31 static int stack = 25;
39 SPLIT, STACK, FULL, TAB
46 /* Window structure types */
50 win_t *win; // the window
51 int height; // win height in _this_ tag
52 state_t state; // state of window
56 list_t *rows; // of row_t
57 row_t *row; // focused row
58 int width; // column width
59 layout_t layout; // column layout
63 win_t *win; // the window
64 int x, y, w, h; // position of window (in this tag)
65 state_t state; // state of window
69 list_t *cols; // of col_t
70 col_t *col; // focused col
71 list_t *flts; // of flt_t
72 flt_t *flt; // focused flt
73 layer_t layer; // focused layer
74 win_t *geom; // display size and position
78 list_t *dpys; // of dpy_t
79 dpy_t *dpy; // focused dpy
80 char name[64]; // tag name
84 list_t *tags; // of tag_t
85 tag_t *tag; // focused tag
86 win_t *root; // root/background window
87 list_t *screens; // display geometry
90 #define WIN(node) ((win_t*)(node)->data)
91 #define ROW(node) ((row_t*)(node)->data)
92 #define COL(node) ((col_t*)(node)->data)
93 #define FLT(node) ((flt_t*)(node)->data)
94 #define DPY(node) ((dpy_t*)(node)->data)
95 #define TAG(node) ((tag_t*)(node)->data)
97 #define tag_foreach_col(tag, dpy, col, row, win) \
98 for (list_t *dpy = tag ->dpys; dpy; dpy = dpy->next) \
99 for (list_t *col = DPY(dpy)->cols; col; col = col->next) \
100 for (list_t *row = COL(col)->rows; row; row = row->next) \
101 for (win_t *win = ROW(row)->win; win; win = NULL)
103 #define tag_foreach_flt(tag, dpy, flt, win) \
104 for (list_t *dpy = tag ->dpys; dpy; dpy = dpy->next) \
105 for (list_t *flt = DPY(dpy)->flts; flt; flt = flt->next) \
106 for (win_t *win = FLT(flt)->win; win; win = NULL) \
108 /* Window management data
109 * wm_* macros represent the currently focused item
110 * _only_ wm_focus protects against NULL pointers */
112 #define wm_tag wm->tag
113 #define wm_dpy wm->tag->dpy
114 #define wm_col wm->tag->dpy->col
115 #define wm_row wm->tag->dpy->col->row
116 #define wm_flt wm->tag->dpy->flt
117 #define wm_focus get_focus()
119 /* Mouse drag data */
120 static drag_t move_mode;
121 static list_t *move_lrow;
122 static list_t *move_lcol;
123 static list_t *move_lflt;
124 static ptr_t move_prev;
125 static layer_t move_layer;
126 static struct { int v, h; } move_dir;
129 void wm_update(void);
131 /********************
133 ********************/
134 static int sort_win(void *a, void *b)
136 return ((win_t*)a)->x > ((win_t*)b)->x ? 1 :
137 ((win_t*)a)->x < ((win_t*)b)->x ? -1 : 0;
140 static win_t *get_focus(void)
142 if (!wm_tag || !wm_dpy)
144 switch (wm_dpy->layer) {
146 return wm_col && wm_row ? wm_row->win : NULL;
148 return wm_flt ? wm_flt->win : NULL;
153 /* Search for the target window in a given tag
154 * win may exist in other tags as well */
155 static int searchl(tag_t *tag, win_t *target,
156 list_t **_dpy, list_t **_col, list_t **_row, list_t **_flt)
158 tag_foreach_col(tag, dpy, col, row, win) {
160 if (_dpy) *_dpy = dpy;
161 if (_col) *_col = col;
162 if (_row) *_row = row;
166 tag_foreach_flt(tag, dpy, flt, win) {
168 if (_dpy) *_dpy = dpy;
169 if (_flt) *_flt = flt;
176 static int search(tag_t *tag, win_t *target,
177 dpy_t **_dpy, col_t **_col, row_t **_row, flt_t **_flt)
179 list_t *dpy, *col, *row, *flt;
180 switch (searchl(tag, target, &dpy, &col, &row, &flt)) {
182 if (_dpy) *_dpy = DPY(dpy);
183 if (_col) *_col = COL(col);
184 if (_row) *_row = ROW(row);
187 if (_dpy) *_dpy = DPY(dpy);
188 if (_flt) *_flt = FLT(flt);
194 /* Set the layout for the windows column in the current tag */
195 static void set_mode(win_t *win, layout_t layout)
198 if (TILING != search(wm_tag, win, NULL, &col, NULL, NULL))
200 printf("set_mode: %p, %d -> %d\n",
201 col, col->layout, layout);
202 col->layout = layout;
203 if (col->layout == SPLIT)
204 for (list_t *cur = col->rows; cur; cur = cur->next) {
205 row_t *row = cur->data;
206 row->height = wm_dpy->geom->h;
211 /* Focus the window in the current tag and record
212 * it as the currently focused window */
213 static void set_focus(win_t *win)
215 if (win == NULL || win == wm->root) {
220 dpy_t *dpy; col_t *col; row_t *row; flt_t *flt;
221 switch (search(wm_tag, win, &dpy, &col, &row, &flt)) {
231 dpy->layer = FLOATING;
237 /* Save mouse start location when moving/resizing windows */
238 static void set_move(win_t *win, ptr_t ptr, drag_t drag)
240 printf("set_move: %d - %p@%d,%d\n",
241 drag, win, ptr.rx, ptr.ry);
243 if (drag == MOVE || drag == RESIZE) {
244 move_layer = searchl(wm_tag, win, NULL,
245 &move_lcol, &move_lrow, &move_lflt);
249 int midy = win->y + (win->h/2);
250 int midx = win->x + (win->w/2);
251 move_dir.v = ptr.ry < midy ? -1 : +1;
252 move_dir.h = ptr.rx < midx ? -1 : +1;
256 /* Print a text representation of the window layout
257 * Quite useful for debugging */
258 static void print_txt(void)
260 for (list_t *ltag = wm->tags; ltag; ltag = ltag->next) {
261 tag_t *tag = ltag->data;
262 printf("tag: <%-9p [%p->%p] >%-9p d=%-9p - %s\n",
263 ltag->prev, ltag, ltag->data, ltag->next,
264 tag->dpy, tag->name);
265 for (list_t *ldpy = tag->dpys; ldpy; ldpy = ldpy->next) {
266 dpy_t *dpy = ldpy->data;
267 win_t *geom = dpy->geom;
268 printf(" dpy: <%-9p [%p->%p] >%-9p %c=%-9p - %d,%d %dx%d\n",
269 ldpy->prev, ldpy, ldpy->data, ldpy->next,
270 dpy->layer == TILING ? 'c' : 'f',
271 dpy->layer == TILING ? (void*)dpy->col : (void*)dpy->flt,
272 geom->x, geom->y, geom->h, geom->w);
273 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
274 col_t *col = lcol->data;
275 printf(" col: <%-9p [%p->%p] >%-9p r=%-9p - %dpx @ %d\n",
276 lcol->prev, lcol, lcol->data, lcol->next,
277 col->row, col->width, col->layout);
278 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
279 row_t *row = lrow->data;
280 win_t *win = row->win;
281 printf(" row: <%-9p [%p>>%p] >%-9p focus=%d%d - %4dpx \n",
282 lrow->prev, lrow, win, lrow->next,
283 col->row == row, wm_focus == win, win->h);
285 for (list_t *lflt = dpy->flts; lflt; lflt = lflt->next) {
286 flt_t *flt = lflt->data;
287 win_t *win = flt->win;
288 printf(" flt: <%-9p [%p>>%p] >%-9p focus=%d%d - %d,%d %dx%d \n",
289 lflt->prev, lflt, win, lflt->next,
290 dpy->flt == flt, wm_focus == flt->win,
291 flt->x, flt->y, flt->h, flt->w);
295 /* Cleanly remove a window from a tag
296 * Determines the new focused row/col
297 * Prunes empty lists */
298 static layer_t cut_win(win_t *win, tag_t *tag)
300 list_t *ldpy, *lcol, *lrow, *lflt;
301 layer_t layer = searchl(tag, win, &ldpy, &lcol, &lrow, &lflt);
303 if (layer == TILING) {
304 dpy_t *dpy = DPY(ldpy);
305 col_t *col = COL(lcol);
306 col->row = lrow->prev ? lrow->prev->data :
307 lrow->next ? lrow->next->data : NULL;
308 col->rows = list_remove(col->rows, lrow, 1);
309 if (col->rows == NULL && (lcol->next || lcol->prev)) {
310 dpy->col = lcol->prev ? lcol->prev->data :
311 lcol->next ? lcol->next->data : NULL;
312 dpy->cols = list_remove(dpy->cols, lcol, 1);
316 if (layer == FLOATING) {
317 dpy_t *dpy = DPY(ldpy);
318 dpy->flts = list_remove(dpy->flts, lflt, 1);
319 dpy->flt = dpy->flts ? list_last(dpy->flts)->data : NULL;
320 if (!dpy->flt && dpy->col && dpy->col->row)
327 /* Insert a window into the tiling layer
328 * The window is added immediately after the
329 * columns currently focused row */
330 static void put_win_col(win_t *win, tag_t *tag, dpy_t *dpy, col_t *col)
332 row_t *row = new0(row_t);
334 row->state = win->state ?: ST_SHOW;
338 dpy->cols = list_insert(dpy->cols, col);
341 int nrows = list_length(col->rows);
343 list_t *prev = list_find(col->rows, col->row);
344 list_insert_after(prev, row);
346 col->rows = list_insert(col->rows, row);
350 tag->dpy->col->row = row;
351 tag->dpy->layer = TILING;
353 row->height = dpy->geom->h / MAX(nrows,1);
355 int ncols = list_length(dpy->cols);
356 col->width = dpy->geom->w / MAX(ncols-1,1);
360 /* Insert a window into the floating layer */
361 static void put_win_flt(win_t *win, tag_t *tag, dpy_t *dpy)
363 flt_t *flt = new0(flt_t);
365 flt->w = dpy->geom->w / 2;
366 flt->h = dpy->geom->h / 2;
367 flt->x = dpy->geom->x + flt->w / 2;
368 flt->y = dpy->geom->y + flt->h / 2;
369 flt->state = win->state ?: ST_SHOW;
371 flt->x = dpy->flt->x + 20;
372 flt->y = dpy->flt->y + 20;
374 dpy->flts = list_append(dpy->flts, flt);
377 tag->dpy->layer = FLOATING;
380 /* Insert a window into a tag */
381 static void put_win(win_t *win, tag_t *tag, layer_t layer)
384 put_win_col(win, tag, tag->dpy, tag->dpy->col);
385 if (layer == FLOATING)
386 put_win_flt(win, tag, tag->dpy);
389 /* Move a window up, down, left, or right
390 * This handles moving with a column, between
391 * columns, and between multiple monitors. */
392 static void shift_window(win_t *win, int col, int row)
395 printf("shift_window: %p - %+d,%+d\n", win, col, row);
397 printf("shift_window: >>>\n");
398 list_t *ldpy, *lcol, *lrow, *lflt;
399 if (TILING != searchl(wm_tag, win, &ldpy, &lcol, &lrow, &lflt))
401 dpy_t *dpy = ldpy->data;
403 /* Move with a column, just swap rows */
404 list_t *src = lrow, *dst = NULL;
405 if (row < 0) dst = src->prev;
406 if (row > 0) dst = src->next;
408 printf("swap: %p <-> %p\n", src->data, dst->data);
409 row_t *tmp = src->data;
410 src->data = dst->data;
415 /* Moving between columns */
416 int onlyrow = !lrow->prev && !lrow->next;
417 list_t *src = lcol, *dst = NULL;
420 /* Normal move between columns */
422 } else if (!onlyrow) {
423 /* Create new column */
424 dpy->cols = list_insert(dpy->cols, new0(col_t));
426 } else if (ldpy->prev) {
427 /* Move to next monitor */
428 dpy = ldpy->prev->data;
429 dst = list_last(dpy->cols);
431 /* We, shall, not, be,
432 * we shall not be moved */
439 } else if (!onlyrow) {
440 dpy->cols = list_append(dpy->cols, new0(col_t));
442 } else if (ldpy->next) {
443 dpy = ldpy->next->data;
449 cut_win(win, wm_tag);
450 put_win_col(win, wm_tag, dpy, dst ? dst->data : NULL);
458 /* Get next/prev item, with wraparound */
459 static list_t *get_next(list_t *list, int forward)
461 list_t *next = forward ? list->next : list->prev;
464 while ((list = forward ? next->prev : next->next))
470 /* Move keyboard focus in a given direction */
471 static void shift_focus(int cols, int rows)
473 printf("shift_focus: %+d,%+d\n", cols, rows);
474 if (rows != 0 && wm_focus) {
475 /* Move focus up/down */
476 list_t *dpy, *col, *row;
477 if (TILING != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
479 row_t *next = get_next(row, rows > 0)->data;
480 set_focus(next->win);
481 if (COL(col)->layout != SPLIT)
485 /* Move focus left/right */
486 list_t *dpy, *col, *row, *ndpy, *ncol = NULL;
488 /* Currently focused on a window */
489 if (TILING != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
491 ncol = cols > 0 ? col->next : col->prev;
493 /* Currently focused on an empty display */
494 dpy = list_find(wm_tag->dpys, wm_dpy);
497 /* Moving focus to a different display */
498 ndpy = get_next(dpy, cols > 0);
499 ncol = cols > 0 ? DPY(ndpy)->cols :
500 list_last(DPY(ndpy)->cols);
503 if (ncol && COL(ncol) && COL(ncol)->row)
504 set_focus(COL(ncol)->row->win);
510 /* Raise the window in the floating */
511 static void raise_float(win_t *win)
513 printf("raise_float: %p\n", win);
515 for (cur = wm_dpy->flts; cur; cur = cur->next)
516 if (FLT(cur)->win == win)
519 flt_t *flt = cur->data;
520 wm_dpy->flts = list_remove(wm_dpy->flts, cur, 0);
521 wm_dpy->flts = list_append(wm_dpy->flts, flt);
526 /* Toggle between floating and tiling layers */
527 static void switch_layer(void)
529 printf("switch_float: %p %d\n",
530 wm_dpy, wm_dpy->layer);
531 wm_dpy->layer = !wm_dpy->layer;
535 /* Move current window between floating and tiling layers */
536 static void set_layer(win_t *win)
539 printf("set_float: %p %p\n", wm_dpy, win);
540 wm_dpy->layer = !cut_win(win, wm_tag);
541 put_win(win, wm_tag, wm_dpy->layer);
545 /* Allocate a new tag */
546 static tag_t *tag_new(list_t *screens, const char *name)
548 tag_t *tag = new0(tag_t);
549 strncpy(tag->name, name, sizeof(tag->name));
550 for (list_t *cur = screens; cur; cur = cur->next) {
551 dpy_t *dpy = new0(dpy_t);
552 dpy->geom = cur->data;
553 tag->dpys = list_append(tag->dpys, dpy);
555 tag->dpy = tag->dpys->data;
556 for (list_t *dpy = tag->dpys; dpy; dpy = dpy->next)
557 if (DPY(dpy)->geom->z > tag->dpy->geom->z)
558 tag->dpy = dpy->data;
563 * If it does not exist it is based on the
564 * display geometry in wm->screens */
565 static tag_t *tag_find(const char *name)
568 for (list_t *cur = wm->tags; cur; cur = cur->next)
569 if (!strcmp(name, TAG(cur)->name)) {
574 tag = tag_new(wm->screens, name);
575 wm->tags = list_append(wm->tags, tag);
580 /* Move the window from the current tag to the new tag
581 * Unlike wmii, only remove the current tag, not all tags */
582 static void tag_set(win_t *win, const char *name)
584 printf("tag_set: %p %s\n", win, name);
585 if (!strcmp(wm_tag->name, name))
587 tag_t *tag = tag_find(name);
588 layer_t layer = cut_win(win, wm_tag);
589 put_win(win, tag, layer);
593 /* Switch to a different tag */
594 static void tag_switch(const char *name)
596 printf("tag_switch: %s\n", name);
598 if ((wm_col == NULL || wm_row == NULL) && wm_flt == NULL) {
600 dpy_t *dpy = old->dpys->data;
602 dpy->cols = list_remove(dpy->cols, dpy->cols, 1);
603 old->dpys = list_remove(old->dpys, old->dpys, 1);
605 list_t *ltag = list_find(wm->tags, old);
606 wm->tags = list_remove(wm->tags, ltag, 1);
608 wm_tag = tag_find(name);
611 /* Tile all windows in the given display
612 * This performs all the actual window tiling
613 * Currently supports split, stack and maximized modes */
614 static void wm_update_cols(dpy_t *dpy)
616 int x=0, y=0; // Current window top-left position
617 int tx=0, ty=0; // Total size (sum of initial col widths and row heights w/o margin)
618 int mx=0, my=0; // Maximum usable size (screen size minus margins)
619 int sy=0; // Stack size (height of focused stack window)
621 /* Scale horizontally */
623 mx = dpy->geom->w - (list_length(dpy->cols)+1)*margin;
624 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
625 tx += COL(lcol)->width;
626 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
627 COL(lcol)->width *= (float)mx / tx;
629 /* Scale each column vertically */
630 win_t *focus = get_focus();
631 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
632 col_t *col = lcol->data;
633 int nrows = list_length(col->rows);
635 for (list_t *lrow = col->rows; lrow; lrow = lrow->next)
636 ty += ROW(lrow)->height;
638 my = dpy->geom->h - (margin + (nrows-1)* margin + margin);
639 sy = dpy->geom->h - (margin + (nrows-1)*(margin/2) + margin)
641 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
642 win_t *win = ROW(lrow)->win;
643 if (ROW(lrow)->state != ST_SHOW) {
644 sys_show(win, ROW(lrow)->state);
647 win->h = ROW(lrow)->height;
648 state_t state = ST_SHOW;
650 switch (col->layout) {
652 sys_move(win, x+margin, y+margin,
653 col->width, win->h * ((float)my / ty));
655 y += height + margin;
658 if (lrow->next && ROW(lrow->next)->win == col->row->win) {
659 /* Hack to prevent flashing */
660 win_t *next = ROW(lrow->next)->win;
661 sys_move(next, x+margin, y+margin+stack+margin/2,
663 sys_show(next, ST_SHOW);
665 int isfocus = win == col->row->win;
666 state = isfocus ? ST_SHOW : ST_SHADE;
667 sys_show(win, state);
668 sys_move(win, x+margin, y+margin, col->width, sy);
669 y += (isfocus ? sy : stack) + (margin/2);
673 sys_move(win, x+margin, 0+margin,
674 col->width, dpy->geom->h-2*margin);
677 sys_show(win, state);
680 ROW(lrow)->height = win->h;
682 x += col->width + margin;
686 /* Refresh the window layout */
689 /* Updates window sizes */
690 for (list_t *ldpy = wm_tag->dpys; ldpy; ldpy = ldpy->next)
691 wm_update_cols(ldpy->data);
692 tag_foreach_flt(wm_tag, ldpy, lflt, win) {
693 flt_t *flt = lflt->data;
694 sys_move(win, flt->x, flt->y, flt->w, flt->h);
696 sys_show(flt->win, flt->state);
699 /* Hide other tags */
700 for (list_t *tag = wm ->tags; tag; tag = tag->next)
701 if (tag->data != wm_tag) {
702 tag_foreach_col(TAG(tag), dpy, col, row, win)
703 sys_show(win, ST_HIDE);
704 tag_foreach_flt(TAG(tag), dpy, flt, win)
705 sys_show(win, ST_HIDE);
708 /* Set focused window */
713 /*******************************
714 * Window management functions *
715 *******************************/
716 int wm_handle_event(win_t *win, event_t ev, mod_t mod, ptr_t ptr)
718 if (!win || win == wm_dpy->geom)
720 //printf("wm_handle_event: %p - %x %c%c%c%c%c\n", win, ev,
721 // mod.up ? '^' : 'v',
722 // mod.alt ? 'a' : '-',
723 // mod.ctrl ? 'c' : '-',
724 // mod.shift ? 's' : '-',
725 // mod.win ? 'w' : '-');
728 if (EV_MOUSE0 <= ev && ev <= EV_MOUSE7) {
729 if (ev == EV_MOUSE1 && !mod.MODKEY && !mod.up)
730 return raise_float(win), 0;
731 if ((ev == EV_MOUSE3 && mod.MODKEY && !mod.up) ||
732 (ev == EV_MOUSE1 && mod.MODKEY && !mod.up && mod.shift))
733 return set_move(win,ptr,RESIZE), 1;
734 if (ev == EV_MOUSE1 && mod.MODKEY && !mod.up)
735 return set_move(win,ptr,MOVE), 1;
736 if (move_mode != NONE && mod.up)
737 return set_move(win,ptr,NONE), 1;
738 if (ev == EV_MOUSE1 && !mod.up && win->state == ST_SHADE)
739 return set_focus(win), wm_update(), 0;
743 /* Only handle key-down */
745 return mod.MODKEY || ev == EV_ALT;
750 if (ev == EV_F1) return raise_float(win), 1;
751 if (ev == EV_F2) return set_focus(win), 1;
752 if (ev == EV_F3) return sys_show(win, ST_SHOW), 1;
753 if (ev == EV_F4) return sys_show(win, ST_HIDE), 1;
754 if (ev == EV_F7) return sys_show(win, ST_SHADE), 1;
756 if (ev == EV_F5) return wm_update(), 1;
757 if (ev == EV_F6) return print_txt(), 1;
758 if (ev == 'q') return sys_exit(), 1;
759 if (ev == 'f') return wm_handle_state(win, win->state,
760 win->state == ST_FULL ? ST_SHOW : ST_FULL);
761 if (ev == 'g') return wm_handle_state(win, win->state,
762 win->state == ST_MAX ? ST_SHOW : ST_MAX);
764 if (mod.MODKEY && mod.shift) {
765 if (ev == 'c') return sys_show(win, ST_CLOSE), 1;
770 if (mod.MODKEY && mod.shift)
771 return set_layer(win), 1;
773 return switch_layer(), 1;
776 /* Movement commands */
777 if (mod.MODKEY && mod.shift) {
779 case 'h': return shift_window(wm_focus,-1, 0), 1;
780 case 'j': return shift_window(wm_focus, 0,+1), 1;
781 case 'k': return shift_window(wm_focus, 0,-1), 1;
782 case 'l': return shift_window(wm_focus,+1, 0), 1;
786 else if (mod.MODKEY) {
788 case 'h': return shift_focus(-1, 0), 1;
789 case 'j': return shift_focus( 0,+1), 1;
790 case 'k': return shift_focus( 0,-1), 1;
791 case 'l': return shift_focus(+1, 0), 1;
796 /* Column layout commands */
799 case 'd': return set_mode(win, SPLIT), 1;
800 case 's': return set_mode(win, STACK), 1;
801 case 'm': return set_mode(win, FULL), 1;
802 case 't': return set_mode(win, TAB), 1;
808 if (mod.MODKEY && '0' <= ev && ev <= '9') {
809 char name[] = {ev, '\0'};
818 if (ev == EV_ENTER && win->state != ST_SHADE)
819 return set_focus(win), 1;
821 /* Reset focus after after focus change,
822 * not sure what is causing the focus change in the first place
823 * but preventing that would be a better solution */
825 sys_focus(wm_focus ?: wm->root);
830 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
832 //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
833 // cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
835 if (move_mode == NONE)
838 int dx = ptr.rx - move_prev.rx;
839 int dy = ptr.ry - move_prev.ry;
842 if (move_layer == TILING && move_mode == RESIZE) {
843 list_t *vert = move_dir.v < 0 ? move_lrow->prev : move_lrow->next;
844 list_t *horz = move_dir.h < 0 ? move_lcol->prev : move_lcol->next;
846 ROW(move_lrow)->height += move_dir.v * dy;
847 ROW(vert)->height -= move_dir.v * dy;
850 COL(move_lcol)->width += move_dir.h * dx;
851 COL(horz)->width -= move_dir.h * dx;
856 if (move_layer == FLOATING) {
857 flt_t *flt = move_lflt->data;
858 win_t *win = flt->win;
859 if (move_mode == MOVE)
860 sys_move(win, win->x+dx, win->y+dy, win->w, win->h);
861 else if (move_mode == RESIZE)
863 win->x + dx * (move_dir.h < 0),
864 win->y + dy * (move_dir.v < 0),
865 win->w + dx * move_dir.h,
866 win->h + dy * move_dir.v);
867 flt->x = win->x; flt->y = win->y;
868 flt->w = win->w; flt->h = win->h;
874 int wm_handle_state(win_t *win, state_t prev, state_t next)
879 printf("wm_handle_state - %p %x -> %x\n", win, prev, next);
881 search(wm_tag, win, NULL, NULL, &row, &flt);
883 if (!row && !flt && next == ST_SHOW)
884 return wm_insert(win), 1;
885 if ((row || flt) && (next == ST_HIDE || next == ST_ICON))
886 return wm_remove(win), 1;
888 if (row) row->state = next;
889 if (flt) flt->state = next;
891 if (prev == ST_MAX || prev == ST_FULL ||
892 next == ST_MAX || next == ST_FULL)
898 void wm_insert(win_t *win)
900 printf("wm_insert: %p\n", win);
903 /* Check for toolbars */
904 if (win->type == TYPE_TOOLBAR)
907 /* Initialize window */
908 sys_watch(win, EV_ENTER, MOD());
909 sys_watch(win, EV_FOCUS, MOD());
912 if (win->type == TYPE_DIALOG || win->parent)
913 wm_dpy->layer = FLOATING;
914 put_win(win, wm_tag, wm_dpy->layer);
922 void wm_remove(win_t *win)
924 printf("wm_remove: %p\n", win);
926 if (win->type == TYPE_TOOLBAR)
928 for (list_t *tag = wm->tags; tag; tag = tag->next)
929 cut_win(win, tag->data);
935 void wm_init(win_t *root)
937 printf("wm_init: %p\n", root);
939 /* Load configuration */
940 margin = conf_get_int("main.margin", margin);
941 stack = conf_get_int("main.stack", stack);
945 wm->screens = list_sort(sys_info(root), 0, sort_win);
946 wm->tag = tag_new(wm->screens, "1");
947 wm->tags = list_insert(NULL, wm->tag);
949 event_t ev_e[] = {EV_ENTER, EV_FOCUS};
950 event_t ev_s[] = {'h', 'j', 'k', 'l', 'c', 'q', ' ',
951 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
952 EV_MOUSE1, EV_MOUSE3};
953 event_t ev_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't', 'f', 'g', ' ',
954 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
955 EV_F1, EV_F2, EV_F3, EV_F4, EV_F5, EV_F6,
956 EV_F7, EV_F8, EV_F9, EV_F10, EV_F11, EV_F12,
957 EV_MOUSE1, EV_MOUSE3};
958 for (int i = 0; i < countof(ev_e); i++)
959 sys_watch(root, ev_e[i], MOD());
960 for (int i = 0; i < countof(ev_m); i++)
961 sys_watch(root, ev_m[i], MOD(.MODKEY=1));
962 for (int i = 0; i < countof(ev_s); i++)
963 sys_watch(root, ev_s[i], MOD(.MODKEY=1,.shift=1));
966 void wm_free(win_t *root)
968 /* Re-show and free all windows */
969 while ( wm->tags) { tag_t *tag = wm->tags->data;
970 while (tag->dpys) { dpy_t *dpy = tag->dpys->data;
971 while (dpy->cols) { col_t *col = dpy->cols->data;
972 while (col->rows) { row_t *row = col->rows->data;
973 sys_show(row->win, ST_SHOW);
974 col->rows = list_remove(col->rows, col->rows, 1); }
975 dpy->cols = list_remove(dpy->cols, dpy->cols, 1); }
976 while (dpy->flts) { flt_t *flt = dpy->flts->data;
977 sys_show(flt->win, ST_SHOW);
978 dpy->flts = list_remove(dpy->flts, dpy->flts, 1); }
979 tag->dpys = list_remove(tag->dpys, tag->dpys, 1); }
980 wm->tags = list_remove( wm->tags, wm->tags, 1); }
982 /* Free remaining data */