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;
128 void wm_update(void);
130 /********************
132 ********************/
133 static int sort_win(void *a, void *b)
135 return ((win_t*)a)->x > ((win_t*)b)->x ? 1 :
136 ((win_t*)a)->x < ((win_t*)b)->x ? -1 : 0;
139 static win_t *get_focus(void)
141 if (!wm_tag || !wm_dpy)
143 switch (wm_dpy->layer) {
145 return wm_col && wm_row ? wm_row->win : NULL;
147 return wm_flt ? wm_flt->win : NULL;
152 /* Search for the target window in a given tag
153 * win may exist in other tags as well */
154 static int searchl(tag_t *tag, win_t *target,
155 list_t **_dpy, list_t **_col, list_t **_row, list_t **_flt)
157 tag_foreach_col(tag, dpy, col, row, win) {
159 if (_dpy) *_dpy = dpy;
160 if (_col) *_col = col;
161 if (_row) *_row = row;
165 tag_foreach_flt(tag, dpy, flt, win) {
167 if (_dpy) *_dpy = dpy;
168 if (_flt) *_flt = flt;
175 static int search(tag_t *tag, win_t *target,
176 dpy_t **_dpy, col_t **_col, row_t **_row, flt_t **_flt)
178 list_t *dpy, *col, *row, *flt;
179 switch (searchl(tag, target, &dpy, &col, &row, &flt)) {
181 if (_dpy) *_dpy = DPY(dpy);
182 if (_col) *_col = COL(col);
183 if (_row) *_row = ROW(row);
186 if (_dpy) *_dpy = DPY(dpy);
187 if (_flt) *_flt = FLT(flt);
193 /* Set the layout for the windows column in the current tag */
194 static void set_mode(win_t *win, layout_t layout)
197 if (TILING != search(wm_tag, win, NULL, &col, NULL, NULL))
199 printf("set_mode: %p, %d -> %d\n",
200 col, col->layout, layout);
201 col->layout = layout;
202 if (col->layout == SPLIT)
203 for (list_t *cur = col->rows; cur; cur = cur->next) {
204 row_t *row = cur->data;
205 row->height = wm_dpy->geom->h;
210 /* Focus the window in the current tag and record
211 * it as the currently focused window */
212 static void set_focus(win_t *win)
214 if (win == NULL || win == wm->root) {
219 dpy_t *dpy; col_t *col; row_t *row; flt_t *flt;
220 switch (search(wm_tag, win, &dpy, &col, &row, &flt)) {
230 dpy->layer = FLOATING;
236 /* Save mouse start location when moving/resizing windows */
237 static void set_move(win_t *win, ptr_t ptr, drag_t drag)
239 printf("set_move: %d - %p@%d,%d\n",
240 drag, win, ptr.rx, ptr.ry);
242 if (drag == MOVE || drag == RESIZE) {
243 move_layer = searchl(wm_tag, win, NULL,
244 &move_lcol, &move_lrow, &move_lflt);
248 int midy = win->y + (win->h/2);
249 int midx = win->x + (win->w/2);
250 move_dir.v = ptr.ry < midy ? -1 : +1;
251 move_dir.h = ptr.rx < midx ? -1 : +1;
255 /* Print a text representation of the window layout
256 * Quite useful for debugging */
257 static void print_txt(void)
259 for (list_t *ltag = wm->tags; ltag; ltag = ltag->next) {
260 tag_t *tag = ltag->data;
261 printf("tag: <%-9p [%p->%p] >%-9p d=%-9p - %s\n",
262 ltag->prev, ltag, ltag->data, ltag->next,
263 tag->dpy, tag->name);
264 for (list_t *ldpy = tag->dpys; ldpy; ldpy = ldpy->next) {
265 dpy_t *dpy = ldpy->data;
266 win_t *geom = dpy->geom;
267 printf(" dpy: <%-9p [%p->%p] >%-9p %c=%-9p - %d,%d %dx%d\n",
268 ldpy->prev, ldpy, ldpy->data, ldpy->next,
269 dpy->layer == TILING ? 'c' : 'f',
270 dpy->layer == TILING ? (void*)dpy->col : (void*)dpy->flt,
271 geom->x, geom->y, geom->h, geom->w);
272 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
273 col_t *col = lcol->data;
274 printf(" col: <%-9p [%p->%p] >%-9p r=%-9p - %dpx @ %d\n",
275 lcol->prev, lcol, lcol->data, lcol->next,
276 col->row, col->width, col->layout);
277 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
278 row_t *row = lrow->data;
279 win_t *win = row->win;
280 printf(" row: <%-9p [%p>>%p] >%-9p focus=%d%d - %4dpx \n",
281 lrow->prev, lrow, win, lrow->next,
282 col->row == row, wm_focus == win, win->h);
284 for (list_t *lflt = dpy->flts; lflt; lflt = lflt->next) {
285 flt_t *flt = lflt->data;
286 win_t *win = flt->win;
287 printf(" flt: <%-9p [%p>>%p] >%-9p focus=%d%d - %d,%d %dx%d \n",
288 lflt->prev, lflt, win, lflt->next,
289 dpy->flt == flt, wm_focus == flt->win,
290 flt->x, flt->y, flt->h, flt->w);
294 /* Cleanly remove a window from a tag
295 * Determines the new focused row/col
296 * Prunes empty lists */
297 static layer_t cut_win(win_t *win, tag_t *tag)
299 list_t *ldpy, *lcol, *lrow, *lflt;
300 layer_t layer = searchl(tag, win, &ldpy, &lcol, &lrow, &lflt);
302 if (layer == TILING) {
303 dpy_t *dpy = DPY(ldpy);
304 col_t *col = COL(lcol);
305 col->row = lrow->prev ? lrow->prev->data :
306 lrow->next ? lrow->next->data : NULL;
307 col->rows = list_remove(col->rows, lrow, 1);
308 if (col->rows == NULL && (lcol->next || lcol->prev)) {
309 dpy->col = lcol->prev ? lcol->prev->data :
310 lcol->next ? lcol->next->data : NULL;
311 dpy->cols = list_remove(dpy->cols, lcol, 1);
315 if (layer == FLOATING) {
316 dpy_t *dpy = DPY(ldpy);
317 dpy->flts = list_remove(dpy->flts, lflt, 1);
318 dpy->flt = dpy->flts ? list_last(dpy->flts)->data : NULL;
319 if (!dpy->flt && dpy->col && dpy->col->row)
326 /* Insert a window into the tiling layer
327 * The window is added immediately after the
328 * columns currently focused row */
329 static void put_win_col(win_t *win, tag_t *tag, dpy_t *dpy, col_t *col)
331 row_t *row = new0(row_t);
333 row->state = win->state ?: ST_SHOW;
337 dpy->cols = list_insert(dpy->cols, col);
340 int nrows = list_length(col->rows);
342 list_t *prev = list_find(col->rows, col->row);
343 list_insert_after(prev, row);
345 col->rows = list_insert(col->rows, row);
349 tag->dpy->col->row = row;
350 tag->dpy->layer = TILING;
352 row->height = dpy->geom->h / MAX(nrows,1);
354 int ncols = list_length(dpy->cols);
355 col->width = dpy->geom->w / MAX(ncols-1,1);
359 /* Insert a window into the floating layer */
360 static void put_win_flt(win_t *win, tag_t *tag, dpy_t *dpy)
362 flt_t *flt = new0(flt_t);
364 flt->w = dpy->geom->w / 2;
365 flt->h = dpy->geom->h / 2;
366 flt->x = dpy->geom->x + flt->w / 2;
367 flt->y = dpy->geom->y + flt->h / 2;
368 flt->state = win->state ?: ST_SHOW;
370 flt->x = dpy->flt->x + 20;
371 flt->y = dpy->flt->y + 20;
373 dpy->flts = list_append(dpy->flts, flt);
376 tag->dpy->layer = FLOATING;
379 /* Insert a window into a tag */
380 static void put_win(win_t *win, tag_t *tag, layer_t layer)
383 put_win_col(win, tag, tag->dpy, tag->dpy->col);
384 if (layer == FLOATING)
385 put_win_flt(win, tag, tag->dpy);
388 /* Move a window up, down, left, or right
389 * This handles moving with a column, between
390 * columns, and between multiple monitors. */
391 static void shift_window(win_t *win, int col, int row)
394 printf("shift_window: %p - %+d,%+d\n", win, col, row);
396 printf("shift_window: >>>\n");
397 list_t *ldpy, *lcol, *lrow, *lflt;
398 if (TILING != searchl(wm_tag, win, &ldpy, &lcol, &lrow, &lflt))
400 dpy_t *dpy = ldpy->data;
402 /* Move with a column, just swap rows */
403 list_t *src = lrow, *dst = NULL;
404 if (row < 0) dst = src->prev;
405 if (row > 0) dst = src->next;
407 printf("swap: %p <-> %p\n", src->data, dst->data);
408 row_t *tmp = src->data;
409 src->data = dst->data;
414 /* Moving between columns */
415 int onlyrow = !lrow->prev && !lrow->next;
416 list_t *src = lcol, *dst = NULL;
419 /* Normal move between columns */
421 } else if (!onlyrow) {
422 /* Create new column */
423 dpy->cols = list_insert(dpy->cols, new0(col_t));
425 } else if (ldpy->prev) {
426 /* Move to next monitor */
427 dpy = ldpy->prev->data;
428 dst = list_last(dpy->cols);
430 /* We, shall, not, be,
431 * we shall not be moved */
438 } else if (!onlyrow) {
439 dpy->cols = list_append(dpy->cols, new0(col_t));
441 } else if (ldpy->next) {
442 dpy = ldpy->next->data;
448 cut_win(win, wm_tag);
449 put_win_col(win, wm_tag, dpy, dst ? dst->data : NULL);
457 /* Get next/prev item, with wraparound */
458 static list_t *get_next(list_t *list, int forward)
460 list_t *next = forward ? list->next : list->prev;
463 while ((list = forward ? next->prev : next->next))
469 /* Move keyboard focus in a given direction */
470 static void shift_focus(int cols, int rows)
472 printf("shift_focus: %+d,%+d\n", cols, rows);
473 if (rows != 0 && wm_focus) {
474 /* Move focus up/down */
475 list_t *dpy, *col, *row;
476 if (TILING != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
478 row_t *next = get_next(row, rows > 0)->data;
479 set_focus(next->win);
480 if (COL(col)->layout != SPLIT)
484 /* Move focus left/right */
485 list_t *dpy, *col, *row, *ndpy, *ncol = NULL;
487 /* Currently focused on a window */
488 if (TILING != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
490 ncol = cols > 0 ? col->next : col->prev;
492 /* Currently focused on an empty display */
493 dpy = list_find(wm_tag->dpys, wm_dpy);
496 /* Moving focus to a different display */
497 ndpy = get_next(dpy, cols > 0);
498 ncol = cols > 0 ? DPY(ndpy)->cols :
499 list_last(DPY(ndpy)->cols);
502 if (ncol && COL(ncol) && COL(ncol)->row)
503 set_focus(COL(ncol)->row->win);
509 /* Raise the window in the floating */
510 static void raise_float(win_t *win)
512 printf("raise_float: %p\n", win);
514 for (cur = wm_dpy->flts; cur; cur = cur->next)
515 if (FLT(cur)->win == win)
518 flt_t *flt = cur->data;
519 wm_dpy->flts = list_remove(wm_dpy->flts, cur, 0);
520 wm_dpy->flts = list_append(wm_dpy->flts, flt);
525 /* Toggle between floating and tiling layers */
526 static void switch_layer(void)
528 printf("switch_float: %p %d\n",
529 wm_dpy, wm_dpy->layer);
530 wm_dpy->layer = !wm_dpy->layer;
534 /* Move current window between floating and tiling layers */
535 static void set_layer(win_t *win)
538 printf("set_float: %p %p\n", wm_dpy, win);
539 wm_dpy->layer = !cut_win(win, wm_tag);
540 put_win(win, wm_tag, wm_dpy->layer);
544 /* Allocate a new tag */
545 static tag_t *tag_new(list_t *screens, const char *name)
547 tag_t *tag = new0(tag_t);
548 strncpy(tag->name, name, sizeof(tag->name));
549 for (list_t *cur = screens; cur; cur = cur->next) {
550 dpy_t *dpy = new0(dpy_t);
551 dpy->geom = cur->data;
552 tag->dpys = list_append(tag->dpys, dpy);
554 tag->dpy = tag->dpys->data;
555 for (list_t *dpy = tag->dpys; dpy; dpy = dpy->next)
556 if (DPY(dpy)->geom->z > tag->dpy->geom->z)
557 tag->dpy = dpy->data;
562 * If it does not exist it is based on the
563 * display geometry in wm->screens */
564 static tag_t *tag_find(const char *name)
567 for (list_t *cur = wm->tags; cur; cur = cur->next)
568 if (!strcmp(name, TAG(cur)->name)) {
573 tag = tag_new(wm->screens, name);
574 wm->tags = list_append(wm->tags, tag);
579 /* Move the window from the current tag to the new tag
580 * Unlike wmii, only remove the current tag, not all tags */
581 static void tag_set(win_t *win, const char *name)
583 printf("tag_set: %p %s\n", win, name);
584 if (!strcmp(wm_tag->name, name))
586 tag_t *tag = tag_find(name);
587 layer_t layer = cut_win(win, wm_tag);
588 put_win(win, tag, layer);
592 /* Switch to a different tag */
593 static void tag_switch(const char *name)
595 printf("tag_switch: %s\n", name);
597 if ((wm_col == NULL || wm_row == NULL) && wm_flt == NULL) {
599 dpy_t *dpy = old->dpys->data;
601 dpy->cols = list_remove(dpy->cols, dpy->cols, 1);
602 old->dpys = list_remove(old->dpys, old->dpys, 1);
604 list_t *ltag = list_find(wm->tags, old);
605 wm->tags = list_remove(wm->tags, ltag, 1);
607 wm_tag = tag_find(name);
610 /* Tile all windows in the given display
611 * This performs all the actual window tiling
612 * Currently supports split, stack and maximized modes */
613 static void wm_update_cols(dpy_t *dpy)
615 int x=0, y=0; // Current window top-left position
616 int tx=0, ty=0; // Total size (sum of initial col widths and row heights w/o margin)
617 int mx=0, my=0; // Maximum usable size (screen size minus margins)
618 int sy=0; // Stack size (height of focused stack window)
620 /* Scale horizontally */
622 mx = dpy->geom->w - (list_length(dpy->cols)+1)*margin;
623 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
624 tx += COL(lcol)->width;
625 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
626 COL(lcol)->width *= (float)mx / tx;
628 /* Scale each column vertically */
629 win_t *focus = get_focus();
630 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
631 col_t *col = lcol->data;
632 int nrows = list_length(col->rows);
634 for (list_t *lrow = col->rows; lrow; lrow = lrow->next)
635 ty += ROW(lrow)->height;
637 my = dpy->geom->h - (margin + (nrows-1)* margin + margin);
638 sy = dpy->geom->h - (margin + (nrows-1)*(margin/2) + margin)
640 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
641 win_t *win = ROW(lrow)->win;
642 if (ROW(lrow)->state != ST_SHOW) {
643 sys_show(win, ROW(lrow)->state);
646 win->h = ROW(lrow)->height;
647 state_t state = ST_SHOW;
649 switch (col->layout) {
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 sys_show(win, state);
679 ROW(lrow)->height = win->h;
681 x += col->width + margin;
685 /* Refresh the window layout */
688 /* Updates window sizes */
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);
695 sys_show(flt->win, flt->state);
698 /* Hide other tags */
699 for (list_t *tag = wm ->tags; tag; tag = tag->next)
700 if (tag->data != wm_tag) {
701 tag_foreach_col(TAG(tag), dpy, col, row, win)
702 sys_show(win, ST_HIDE);
703 tag_foreach_flt(TAG(tag), dpy, flt, win)
704 sys_show(win, ST_HIDE);
707 /* Set focused window */
712 /*******************************
713 * Window management functions *
714 *******************************/
715 int wm_handle_event(win_t *win, event_t ev, mod_t mod, ptr_t ptr)
717 if (!win || win == wm_dpy->geom)
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' : '-');
727 if (EV_MOUSE0 <= ev && ev <= EV_MOUSE7) {
728 if (ev == EV_MOUSE1 && !mod.MODKEY && !mod.up)
729 return raise_float(win), 0;
730 if ((ev == EV_MOUSE3 && mod.MODKEY && !mod.up) ||
731 (ev == EV_MOUSE1 && mod.MODKEY && !mod.up && mod.shift))
732 return set_move(win,ptr,RESIZE), 1;
733 if (ev == EV_MOUSE1 && mod.MODKEY && !mod.up)
734 return set_move(win,ptr,MOVE), 1;
735 if (move_mode != NONE && mod.up)
736 return set_move(win,ptr,NONE), 1;
737 if (ev == EV_MOUSE1 && !mod.up && win->state == ST_SHADE)
738 return set_focus(win), wm_update(), 0;
742 /* Only handle key-down */
744 return mod.MODKEY || ev == EV_ALT;
749 if (ev == EV_F1) return raise_float(win), 1;
750 if (ev == EV_F2) return set_focus(win), 1;
751 if (ev == EV_F3) return sys_show(win, ST_SHOW), 1;
752 if (ev == EV_F4) return sys_show(win, ST_HIDE), 1;
753 if (ev == EV_F7) return sys_show(win, ST_SHADE), 1;
755 if (ev == EV_F5) return wm_update(), 1;
756 if (ev == EV_F6) return print_txt(), 1;
757 if (ev == 'q') return sys_exit(), 1;
758 if (ev == 'f') return wm_handle_state(win, win->state,
759 win->state == ST_FULL ? ST_SHOW : ST_FULL);
760 if (ev == 'g') return wm_handle_state(win, win->state,
761 win->state == ST_MAX ? ST_SHOW : ST_MAX);
763 if (mod.MODKEY && mod.shift) {
764 if (ev == 'c') return sys_show(win, ST_CLOSE), 1;
769 if (mod.MODKEY && mod.shift)
770 return set_layer(win), 1;
772 return switch_layer(), 1;
775 /* Movement commands */
776 if (mod.MODKEY && mod.shift) {
778 case 'h': return shift_window(wm_focus,-1, 0), 1;
779 case 'j': return shift_window(wm_focus, 0,+1), 1;
780 case 'k': return shift_window(wm_focus, 0,-1), 1;
781 case 'l': return shift_window(wm_focus,+1, 0), 1;
785 else if (mod.MODKEY) {
787 case 'h': return shift_focus(-1, 0), 1;
788 case 'j': return shift_focus( 0,+1), 1;
789 case 'k': return shift_focus( 0,-1), 1;
790 case 'l': return shift_focus(+1, 0), 1;
795 /* Column layout commands */
798 case 'd': return set_mode(win, SPLIT), 1;
799 case 's': return set_mode(win, STACK), 1;
800 case 'm': return set_mode(win, FULL), 1;
801 case 't': return set_mode(win, TAB), 1;
807 if (mod.MODKEY && '0' <= ev && ev <= '9') {
808 char name[] = {ev, '\0'};
817 if (ev == EV_ENTER && win->state != ST_SHADE)
818 return set_focus(win), 1;
820 /* Reset focus after after focus change,
821 * not sure what is causing the focus change in the first place
822 * but preventing that would be a better solution */
824 sys_focus(wm_focus ?: wm->root);
829 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
831 //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
832 // cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
834 if (move_mode == NONE)
837 int dx = ptr.rx - move_prev.rx;
838 int dy = ptr.ry - move_prev.ry;
841 if (move_layer == TILING && move_mode == RESIZE) {
842 list_t *vert = move_dir.v < 0 ? move_lrow->prev : move_lrow->next;
843 list_t *horz = move_dir.h < 0 ? move_lcol->prev : move_lcol->next;
845 ROW(move_lrow)->height += move_dir.v * dy;
846 ROW(vert)->height -= move_dir.v * dy;
849 COL(move_lcol)->width += move_dir.h * dx;
850 COL(horz)->width -= move_dir.h * dx;
855 if (move_layer == FLOATING) {
856 flt_t *flt = move_lflt->data;
857 win_t *win = flt->win;
858 if (move_mode == MOVE)
859 sys_move(win, win->x+dx, win->y+dy, win->w, win->h);
860 else if (move_mode == RESIZE)
862 win->x + dx * (move_dir.h < 0),
863 win->y + dy * (move_dir.v < 0),
864 win->w + dx * move_dir.h,
865 win->h + dy * move_dir.v);
866 flt->x = win->x; flt->y = win->y;
867 flt->w = win->w; flt->h = win->h;
873 int wm_handle_state(win_t *win, state_t prev, state_t next)
878 printf("wm_handle_state - %p %x -> %x\n", win, prev, next);
880 search(wm_tag, win, NULL, NULL, &row, &flt);
882 if (!row && !flt && next == ST_SHOW)
883 return wm_insert(win), 1;
884 if ((row || flt) && (next == ST_HIDE || next == ST_ICON))
885 return wm_remove(win), 1;
887 if (row) row->state = next;
888 if (flt) flt->state = next;
890 if (prev == ST_MAX || prev == ST_FULL ||
891 next == ST_MAX || next == ST_FULL)
897 void wm_insert(win_t *win)
899 printf("wm_insert: %p\n", win);
902 /* Check for toolbars */
903 if (win->type == TYPE_TOOLBAR)
906 /* Initialize window */
907 sys_watch(win, EV_ENTER, MOD());
908 sys_watch(win, EV_FOCUS, MOD());
911 if (win->type == TYPE_DIALOG || win->parent)
912 wm_dpy->layer = FLOATING;
913 put_win(win, wm_tag, wm_dpy->layer);
921 void wm_remove(win_t *win)
923 printf("wm_remove: %p\n", win);
925 if (win->type == TYPE_TOOLBAR)
927 for (list_t *tag = wm->tags; tag; tag = tag->next)
928 cut_win(win, tag->data);
934 void wm_init(win_t *root)
936 printf("wm_init: %p\n", root);
938 /* Load configuration */
939 margin = conf_get_int("main.margin", margin);
940 stack = conf_get_int("main.stack", stack);
944 wm->screens = list_sort(sys_info(root), 0, sort_win);
945 wm->tag = tag_new(wm->screens, "1");
946 wm->tags = list_insert(NULL, wm->tag);
948 event_t ev_e[] = {EV_ENTER, EV_FOCUS};
949 event_t ev_s[] = {'h', 'j', 'k', 'l', 'c', 'q', ' ',
950 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
951 EV_MOUSE1, EV_MOUSE3};
952 event_t ev_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't', 'f', 'g', ' ',
953 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
954 EV_F1, EV_F2, EV_F3, EV_F4, EV_F5, EV_F6,
955 EV_F7, EV_F8, EV_F9, EV_F10, EV_F11, EV_F12,
956 EV_MOUSE1, EV_MOUSE3};
957 for (int i = 0; i < countof(ev_e); i++)
958 sys_watch(root, ev_e[i], MOD());
959 for (int i = 0; i < countof(ev_m); i++)
960 sys_watch(root, ev_m[i], MOD(.MODKEY=1));
961 for (int i = 0; i < countof(ev_s); i++)
962 sys_watch(root, ev_s[i], MOD(.MODKEY=1,.shift=1));
965 void wm_free(win_t *root)
967 /* Re-show and free all windows */
968 while ( wm->tags) { tag_t *tag = wm->tags->data;
969 while (tag->dpys) { dpy_t *dpy = tag->dpys->data;
970 while (dpy->cols) { col_t *col = dpy->cols->data;
971 while (col->rows) { row_t *row = col->rows->data;
972 sys_show(row->win, ST_SHOW);
973 col->rows = list_remove(col->rows, col->rows, 1); }
974 dpy->cols = list_remove(dpy->cols, dpy->cols, 1); }
975 while (dpy->flts) { flt_t *flt = dpy->flts->data;
976 sys_show(flt->win, ST_SHOW);
977 dpy->flts = list_remove(dpy->flts, dpy->flts, 1); }
978 tag->dpys = list_remove(tag->dpys, tag->dpys, 1); }
979 wm->tags = list_remove( wm->tags, wm->tags, 1); }
981 /* Free remaining data */