]> Pileus Git - wmpus/blob - wm-wmii.c
Add window-close function
[wmpus] / wm-wmii.c
1 /*
2  * Copyright (c) 2011, Andy Spencer <andy753421@gmail.com>
3  *
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.
7  *
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
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #include "util.h"
20 #include "conf.h"
21 #include "sys.h"
22 #include "wm.h"
23
24 /* Configuration */
25 #ifndef MODKEY
26 #define MODKEY alt
27 #endif
28 static int margin = 0;
29 static int stack  = 25;
30
31 /* Enums */
32 typedef enum {
33         NONE, MOVE, RESIZE
34 } drag_t;
35
36 typedef enum {
37         SPLIT, STACK, FULL, TAB
38 } mode_t;
39
40 typedef enum {
41         TILING, FLOATING
42 } layer_t;
43
44 /* Window structure types */
45 struct win_wm { };
46
47 typedef struct {
48         win_t  *win;     // the window
49         int     height;  // win height in _this_ tag
50         state_t state;   // state of window
51 } row_t;
52
53 typedef struct {
54         list_t *rows;    // of row_t
55         row_t  *row;     // focused row
56         int     width;   // column width
57         mode_t  mode;    // display mode
58 } col_t;
59
60 typedef struct {
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
64 } flt_t;
65
66 typedef struct {
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
73 } dpy_t;
74
75 typedef struct {
76         list_t *dpys;    // of dpy_t
77         dpy_t  *dpy;     // focused dpy
78         int     name;    // tag name
79 } tag_t;
80
81 typedef struct {
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
86 } wm_t;
87
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)
94
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)
100
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)      \
105
106 /* Window management data
107  *   wm_* macros represent the currently focused item
108  *   _only_ wm_focus protects against NULL pointers */
109 static wm_t *wm;
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()
116
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;
125
126 /********************
127  * Helper functions *
128  ********************/
129 static int sort_win(void *a, void *b)
130 {
131         return ((win_t*)a)->x > ((win_t*)b)->x ?  1 :
132                ((win_t*)a)->x < ((win_t*)b)->x ? -1 : 0;
133 }
134
135 static win_t *get_focus(void)
136 {
137         if (!wm_tag || !wm_dpy)
138                 return NULL;
139         switch (wm_dpy->layer) {
140         case TILING:
141                 return wm_col && wm_row ? wm_row->win : NULL;
142         case FLOATING:
143                 return wm_flt ? wm_flt->win : NULL;
144         }
145         return NULL;
146 }
147
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)
152 {
153         tag_foreach_col(tag, dpy, col, row, win) {
154                 if (win == target) {
155                         if (_dpy) *_dpy = dpy;
156                         if (_col) *_col = col;
157                         if (_row) *_row = row;
158                         return TILING;
159                 }
160         }
161         tag_foreach_flt(tag, dpy, flt, win) {
162                 if (win == target) {
163                         if (_dpy) *_dpy = dpy;
164                         if (_flt) *_flt = flt;
165                         return FLOATING;
166                 }
167         }
168         return -1;
169 }
170
171 static int search(tag_t *tag, win_t *target,
172                 dpy_t **_dpy, col_t **_col, row_t **_row, flt_t **_flt)
173 {
174         list_t *dpy, *col, *row, *flt;
175         switch (searchl(tag, target, &dpy, &col, &row, &flt)) {
176         case TILING:
177                 if (_dpy) *_dpy = DPY(dpy);
178                 if (_col) *_col = COL(col);
179                 if (_row) *_row = ROW(row);
180                 return TILING;
181         case FLOATING:
182                 if (_dpy) *_dpy = DPY(dpy);
183                 if (_flt) *_flt = FLT(flt);
184                 return FLOATING;
185         }
186         return -1;
187 }
188
189 /* Set the mode for the windows column in the current tag */
190 static void set_mode(win_t *win, mode_t mode)
191 {
192         col_t *col;
193         if (TILING != search(wm_tag, win, NULL, &col, NULL, NULL))
194                 return;
195         printf("set_mode: %p, %d -> %d\n",
196                         col, col->mode, mode);
197         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;
202                 }
203         wm_update();
204 }
205
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)
209 {
210         if (win == NULL || win == wm->root) {
211                 sys_focus(wm->root);
212                 return;
213         }
214
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++) {
220                 if (wm_focus)
221                         sys_watch(wm_focus, i, MOD());
222                 sys_unwatch(win, i, MOD());
223         }
224
225         dpy_t *dpy; col_t *col; row_t *row; flt_t *flt;
226         switch (search(wm_tag, win, &dpy, &col, &row, &flt)) {
227         case TILING:
228                 wm_dpy = dpy;
229                 wm_col = col;
230                 wm_row = row;
231                 dpy->layer = TILING;
232                 break;
233         case FLOATING:
234                 wm_dpy = dpy;
235                 wm_flt = flt;
236                 dpy->layer = FLOATING;
237                 break;
238         }
239         sys_focus(win);
240 }
241
242 /* Save mouse start location when moving/resizing windows */
243 static void set_move(win_t *win, ptr_t ptr, drag_t drag)
244 {
245         printf("set_move: %d - %p@%d,%d\n",
246                         drag, win, ptr.rx, ptr.ry);
247         move_mode = drag;
248         if (drag == MOVE || drag == RESIZE) {
249                 move_layer = searchl(wm_tag, win, NULL,
250                                 &move_lcol, &move_lrow, &move_lflt);
251                 if (move_layer < 0)
252                         return;
253                 move_prev = ptr;
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;
258         }
259 }
260
261 /* Print a text representation of the window layout
262  * Quite useful for debugging */
263 static void print_txt(void)
264 {
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);
289         } }
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);
297         }  } }
298 }
299
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)
304 {
305         list_t *ldpy, *lcol, *lrow, *lflt;
306         layer_t layer = searchl(tag, win, &ldpy, &lcol, &lrow, &lflt);
307
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);
318                 }
319         }
320
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)
326                         dpy->layer = TILING;
327         }
328
329         return layer;
330 }
331
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)
336 {
337         row_t *row = new0(row_t);
338         row->win = win;
339
340         if (col == NULL) {
341                 col = new0(col_t);
342                 dpy->cols = list_insert(dpy->cols, col);
343         }
344
345         int nrows = list_length(col->rows);
346         if (col->row) {
347                 list_t *prev = list_find(col->rows, col->row);
348                 list_insert_after(prev, row);
349         } else {
350                 col->rows = list_insert(col->rows, row);
351         }
352         tag->dpy           = dpy;
353         tag->dpy->col      = col;
354         tag->dpy->col->row = row;
355         tag->dpy->layer    = TILING;
356
357         row->height = dpy->geom->h / MAX(nrows,1);
358         if (nrows == 0) {
359                 int ncols = list_length(dpy->cols);
360                 col->width = dpy->geom->w / MAX(ncols-1,1);
361         }
362 }
363
364 /* Insert a window into the floating layer */
365 static void put_win_flt(win_t *win, tag_t *tag, dpy_t *dpy)
366 {
367         flt_t *flt = new0(flt_t);
368         flt->win = win;
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;
373         if (dpy->flt) {
374                 flt->x = dpy->flt->x + 20;
375                 flt->y = dpy->flt->y + 20;
376         }
377         dpy->flts = list_append(dpy->flts, flt);
378         tag->dpy        = dpy;
379         tag->dpy->flt   = flt;
380         tag->dpy->layer = FLOATING;
381 }
382
383 /* Insert a window into a tag */
384 static void put_win(win_t *win, tag_t *tag, layer_t layer)
385 {
386         if (layer == TILING)
387                 put_win_col(win, tag, tag->dpy, tag->dpy->col);
388         if (layer == FLOATING)
389                 put_win_flt(win, tag, tag->dpy);
390 }
391
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)
396 {
397         if (!win) return;
398         printf("shift_window: %p - %+d,%+d\n", win, col, row);
399         print_txt();
400         printf("shift_window: >>>\n");
401         list_t *ldpy, *lcol, *lrow, *lflt;
402         if (TILING != searchl(wm_tag, win, &ldpy, &lcol, &lrow, &lflt))
403                 return;
404         dpy_t *dpy = ldpy->data;
405         if (row != 0) {
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;
410                 if (src && dst) {
411                         printf("swap: %p <-> %p\n", src->data, dst->data);
412                         row_t *tmp = src->data;
413                         src->data = dst->data;
414                         dst->data = tmp;
415                         goto update;
416                 }
417         } else {
418                 /* Moving between columns */
419                 int onlyrow = !lrow->prev && !lrow->next;
420                 list_t *src = lcol, *dst = NULL;
421                 if (col < 0) {
422                         if (src->prev) {
423                                 /* Normal move between columns */
424                                 dst = src->prev;
425                         } else if (!onlyrow) {
426                                 /* Create new column */
427                                 dpy->cols = list_insert(dpy->cols, new0(col_t));
428                                 dst = src->prev;
429                         } else if (ldpy->prev) {
430                                 /* Move to next monitor */
431                                 dpy = ldpy->prev->data;
432                                 dst = list_last(dpy->cols);
433                         } else {
434                                 /* We, shall, not, be,
435                                  * we shall not be moved */
436                                 return;
437                         }
438                 }
439                 if (col > 0) {
440                         if (src->next) {
441                                 dst = src->next;
442                         } else if (!onlyrow) {
443                                 dpy->cols = list_append(dpy->cols, new0(col_t));
444                                 dst = src->next;
445                         } else if (ldpy->next) {
446                                 dpy = ldpy->next->data;
447                                 dst = dpy->cols;
448                         } else {
449                                 return;
450                         }
451                 }
452                 cut_win(win, wm_tag);
453                 put_win_col(win, wm_tag, dpy, dst ? dst->data : NULL);
454                 goto update;
455         }
456 update:
457         print_txt();
458         wm_update();
459 }
460
461 /* Get next/prev item, with wraparound */
462 static list_t *get_next(list_t *list, int forward)
463 {
464         list_t *next = forward ? list->next : list->prev;
465         if (next == NULL) {
466                 next = list;
467                 while ((list = forward ? next->prev : next->next))
468                         next = list;
469         }
470         return next;
471 }
472
473 /* Move keyboard focus in a given direction */
474 static void shift_focus(int cols, int rows)
475 {
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))
481                         return;
482                 row_t *next = get_next(row, rows > 0)->data;
483                 set_focus(next->win);
484                 if (COL(col)->mode != SPLIT)
485                         wm_update();
486         }
487         if (cols != 0) {
488                 /* Move focus left/right */
489                 list_t *dpy, *col, *row, *ndpy, *ncol = NULL;
490                 if (wm_focus) {
491                         /* Currently focused on a window */
492                         if (TILING != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
493                                 return;
494                         ncol = cols > 0 ? col->next : col->prev;
495                 } else {
496                         /* Currently focused on an empty display */
497                         dpy = list_find(wm_tag->dpys, wm_dpy);
498                 }
499                 if (ncol == NULL) {
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);
504                         wm_dpy = ndpy->data;
505                 }
506                 if (ncol && COL(ncol) && COL(ncol)->row)
507                         set_focus(COL(ncol)->row->win);
508                 else
509                         sys_focus(wm->root);
510         }
511 }
512
513 /* Raise the window in the floating */
514 static void raise_float(win_t *win)
515 {
516         printf("raise_float: %p\n", win);
517         list_t *cur;
518         for (cur = wm_dpy->flts; cur; cur = cur->next)
519                 if (FLT(cur)->win == win)
520                         break;
521         if (cur) {
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);
525                 sys_raise(win);
526         }
527 }
528
529 /* Toggle between floating and tiling layers */
530 static void switch_layer(void)
531 {
532         printf("switch_float: %p %d\n",
533                         wm_dpy, wm_dpy->layer);
534         wm_dpy->layer = !wm_dpy->layer;
535         wm_update();
536 }
537
538 /* Move current window between floating and tiling layers */
539 static void set_layer(win_t *win)
540 {
541         if (!win) return;
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);
545         wm_update();
546 }
547
548 /* Allocate a new tag */
549 static tag_t *tag_new(list_t *screens, int name)
550 {
551         tag_t *tag = new0(tag_t);
552         tag->name  = name;
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);
557         }
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;
562         return tag;
563 }
564
565 /* Search for a tag
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)
569 {
570         tag_t *tag = NULL;
571         for (list_t *cur = wm->tags; cur; cur = cur->next)
572                 if (name == TAG(cur)->name) {
573                         tag = cur->data;
574                         break;
575                 }
576         if (!tag) {
577                 tag = tag_new(wm->screens, name);
578                 wm->tags = list_append(wm->tags, tag);
579         }
580         return tag;
581 }
582
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)
586 {
587         printf("tag_set: %p %d\n", win, name);
588         if (wm_tag->name == name)
589                 return;
590         tag_t *tag = tag_find(name);
591         layer_t layer = cut_win(win, wm_tag);
592         put_win(win, tag, layer);
593         set_focus(wm_focus);
594 }
595
596 /* Switch to a different tag */
597 static void tag_switch(int name)
598 {
599         printf("tag_switch: %d\n", name);
600         tag_t *old = wm_tag;
601         if ((wm_col == NULL || wm_row == NULL) && wm_flt == NULL) {
602                 while (old->dpys) {
603                         dpy_t *dpy = old->dpys->data;
604                         while (dpy->cols)
605                                 dpy->cols = list_remove(dpy->cols, dpy->cols, 1);
606                         old->dpys = list_remove(old->dpys, old->dpys, 1);
607                 }
608                 list_t *ltag = list_find(wm->tags, old);
609                 wm->tags = list_remove(wm->tags, ltag, 1);
610         }
611         wm_tag = tag_find(name);
612 }
613
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)
618 {
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)
623
624         /* Scale horizontally */
625         x  = dpy->geom->x;
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;
631
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);
637                 ty = 0;
638                 for (list_t *lrow = col->rows; lrow; lrow = lrow->next)
639                         ty += ROW(lrow)->height;
640                 y  = dpy->geom->y;
641                 my = dpy->geom->h - (margin + (nrows-1)* margin    + margin);
642                 sy = dpy->geom->h - (margin + (nrows-1)*(margin/2) + margin)
643                                   -           (nrows-1)* stack;
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;
648                         int height = 0;
649                         switch (col->mode) {
650                         case SPLIT:
651                                 sys_move(win, x+margin, y+margin,
652                                         col->width, win->h * ((float)my / ty));
653                                 height = win->h;
654                                 y += height + margin;
655                                 break;
656                         case STACK:
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,
661                                                 col->width, sy);
662                                         sys_show(next, ST_SHOW);
663                                 }
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);
669                                 break;
670                         case FULL:
671                         case TAB:
672                                 sys_move(win, x+margin, 0+margin,
673                                         col->width, dpy->geom->h-2*margin);
674                                 break;
675                         }
676                         ROW(lrow)->state = state;
677                         sys_show(win, state);
678                         if (focus == win)
679                                 sys_raise(win);
680                         ROW(lrow)->height = win->h;
681                 }
682                 x += col->width + margin;
683         }
684 }
685
686 /*******************************
687  * Window management functions *
688  *******************************/
689 void wm_update(void)
690 {
691         /* Show/hide tags */
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);
702                 }
703
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);
710                 sys_raise(flt->win);
711         }
712         if (wm_focus)
713                 set_focus(wm_focus);
714 }
715
716 int wm_handle_event(win_t *win, event_t ev, mod_t mod, ptr_t ptr)
717 {
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' : '-');
725
726         /* Mouse movement */
727         if (ev == EV_MOUSE1)
728                 raise_float(win);
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;
738                 return 0;
739         }
740
741         /* Only handle key-down */
742         if (mod.up)
743                 return 0;
744
745         /* Misc */
746         if (mod.MODKEY) {
747 #ifdef DEBUG
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;
753 #endif
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;
757         }
758         if (mod.MODKEY && mod.shift) {
759                 if (ev == 'c')   return sys_show(win, ST_CLOSE), 1;
760         }
761
762         /* Floating layer */
763         if (ev == ' ') {
764                 if (mod.MODKEY && mod.shift)
765                         return set_layer(win), 1;
766                 if (mod.MODKEY)
767                         return switch_layer(), 1;
768         }
769
770         /* Movement commands */
771         if (mod.MODKEY && mod.shift) {
772                 switch (ev) {
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;
777                 default: break;
778                 }
779         }
780         else if (mod.MODKEY) {
781                 switch (ev) {
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;
786                 default: break;
787                 }
788         }
789
790         /* Column mode commands */
791         if (mod.MODKEY) {
792                 switch (ev) {
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;
797                 default: break;
798                 }
799         }
800
801         /* Tag switching */
802         if (mod.MODKEY && '0' <= ev && ev <= '9') {
803                 int name = ev - '0';
804                 if (mod.shift)
805                         tag_set(win, name);
806                 else
807                         tag_switch(name);
808                 wm_update();
809         }
810
811         /* Focus change */
812         if (ev == EV_ENTER && win->state != ST_SHADE)
813                 return set_focus(win), 1;
814
815         if (EV_MOUSE0 <= ev && ev <= EV_MOUSE7)
816                 return set_focus(win), 0;
817
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 */
821         if (ev == EV_FOCUS)
822                 sys_focus(wm_focus ?: wm->root);
823
824         return 0;
825 }
826
827 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
828 {
829         //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
830         //              cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
831
832         if (move_mode == NONE)
833                 return 0;
834
835         int dx = ptr.rx - move_prev.rx;
836         int dy = ptr.ry - move_prev.ry;
837         move_prev = ptr;
838
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;
842                 if (vert) {
843                         ROW(move_lrow)->height += move_dir.v * dy;
844                         ROW(vert)->height      -= move_dir.v * dy;
845                 }
846                 if (horz) {
847                         COL(move_lcol)->width  += move_dir.h * dx;
848                         COL(horz)->width       -= move_dir.h * dx;
849                 }
850                 wm_update();
851         }
852
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)
859                         sys_move(win,
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;
866         }
867
868         return 0;
869 }
870
871 void wm_insert(win_t *win)
872 {
873         printf("wm_insert: %p\n", win);
874         print_txt();
875
876         /* Initialize window */
877         win->wm = new0(win_wm_t);
878         sys_watch(win, EV_ENTER, MOD());
879         sys_watch(win, EV_FOCUS, MOD());
880
881         /* Add to screen */
882         put_win(win, wm_tag, wm_dpy->layer);
883
884         /* Arrange */
885         wm_update();
886         set_focus(wm_focus);
887         print_txt();
888 }
889
890 void wm_remove(win_t *win)
891 {
892         printf("wm_remove: %p\n", win);
893         print_txt();
894         for (list_t *tag = wm->tags; tag; tag = tag->next)
895                 cut_win(win, tag->data);
896         free(win->wm);
897         set_focus(wm_focus);
898         wm_update();
899         print_txt();
900 }
901
902 void wm_init(win_t *root)
903 {
904         printf("wm_init: %p\n", root);
905
906         /* Load configuration */
907         margin = conf_get_int("main.margin", margin);
908         stack  = conf_get_int("main.stack",  stack);
909
910         wm          = new0(wm_t);
911         wm->root    = root;
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);
915
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));
930 }
931
932 void wm_free(win_t *root)
933 {
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);
940                 free(row->win->wm);
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);
945                 free(flt->win->wm);
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); }
949
950         /* Free remaining data */
951         free(wm);
952 }