]> Pileus Git - wmpus/blob - wm-wmii.c
Properly sort monitors left->right
[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, max, 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 = key_mouse1; i < key_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         return tag;
560 }
561
562 /* Search for a tag
563  *   If it does not exist it is based on the
564  *   display geometry in wm->screens */
565 static tag_t *tag_find(int name)
566 {
567         tag_t *tag = NULL;
568         for (list_t *cur = wm->tags; cur; cur = cur->next)
569                 if (name == TAG(cur)->name) {
570                         tag = cur->data;
571                         break;
572                 }
573         if (!tag) {
574                 tag = tag_new(wm->screens, name);
575                 wm->tags = list_append(wm->tags, tag);
576         }
577         return tag;
578 }
579
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, int name)
583 {
584         printf("tag_set: %p %d\n", win, name);
585         if (wm_tag->name == name)
586                 return;
587         tag_t *tag = tag_find(name);
588         layer_t layer = cut_win(win, wm_tag);
589         put_win(win, tag, layer);
590         set_focus(wm_focus);
591 }
592
593 /* Switch to a different tag */
594 static void tag_switch(int name)
595 {
596         printf("tag_switch: %d\n", name);
597         tag_t *old = wm_tag;
598         if ((wm_col == NULL || wm_row == NULL) && wm_flt == NULL) {
599                 while (old->dpys) {
600                         dpy_t *dpy = old->dpys->data;
601                         while (dpy->cols)
602                                 dpy->cols = list_remove(dpy->cols, dpy->cols, 1);
603                         old->dpys = list_remove(old->dpys, old->dpys, 1);
604                 }
605                 list_t *ltag = list_find(wm->tags, old);
606                 wm->tags = list_remove(wm->tags, ltag, 1);
607         }
608         wm_tag = tag_find(name);
609 }
610
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)
615 {
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)
620
621         /* Scale horizontally */
622         x  = dpy->geom->x;
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;
628
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);
634                 ty = 0;
635                 for (list_t *lrow = col->rows; lrow; lrow = lrow->next)
636                         ty += ROW(lrow)->height;
637                 y  = dpy->geom->y;
638                 my = dpy->geom->h - (MARGIN + (nrows-1)* MARGIN    + MARGIN);
639                 sy = dpy->geom->h - (MARGIN + (nrows-1)*(MARGIN/2) + MARGIN)
640                                   -           (nrows-1)* STACK;
641                 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
642                         win_t *win = ROW(lrow)->win;
643                         win->h = ROW(lrow)->height;
644                         state_t state = st_show;
645                         int height = 0;
646                         switch (col->mode) {
647                         case split:
648                                 sys_move(win, x+MARGIN, y+MARGIN,
649                                         col->width, win->h * ((float)my / ty));
650                                 height = win->h;
651                                 y += height + MARGIN;
652                                 break;
653                         case stack:
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,
658                                                 col->width, sy);
659                                         sys_show(next, st_show);
660                                 }
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);
666                                 break;
667                         case max:
668                         case tab:
669                                 sys_move(win, x+MARGIN, 0+MARGIN,
670                                         col->width, dpy->geom->h-2*MARGIN);
671                                 break;
672                         }
673                         ROW(lrow)->state = state;
674                         sys_show(win, state);
675                         if (focus == win)
676                                 sys_raise(win);
677                         ROW(lrow)->height = win->h;
678                 }
679                 x += col->width + MARGIN;
680         }
681 }
682
683 /*******************************
684  * Window management functions *
685  *******************************/
686 void wm_update(void)
687 {
688         /* Show/hide tags */
689         tag_foreach_col(wm_tag, dpy, col, row, win)
690                 sys_show(win, ROW(row)->state);
691         tag_foreach_flt(wm_tag, dpy, flt, win)
692                 sys_show(win, FLT(flt)->state);
693         for (list_t *tag = wm ->tags; tag; tag = tag->next)
694                 if (tag->data != wm_tag) {
695                         tag_foreach_col(TAG(tag), dpy, col, row, win)
696                                         sys_show(win, st_hide);
697                         tag_foreach_flt(TAG(tag), dpy, flt, win)
698                                         sys_show(win, st_hide);
699                 }
700
701         /* Refresh the display */
702         for (list_t *ldpy = wm_tag->dpys; ldpy; ldpy = ldpy->next)
703                 wm_update_cols(ldpy->data);
704         tag_foreach_flt(wm_tag, ldpy, lflt, win) {
705                 flt_t *flt = lflt->data;
706                 sys_move(win, flt->x, flt->y, flt->w, flt->h);
707                 sys_raise(flt->win);
708         }
709         if (wm_focus)
710                 set_focus(wm_focus);
711 }
712
713 int wm_handle_key(win_t *win, Key_t key, mod_t mod, ptr_t ptr)
714 {
715         if (!win || win == wm_dpy->geom) return 0;
716         //printf("wm_handle_key: %p - %x %c%c%c%c%c\n", win, key,
717         //      mod.up    ? '^' : 'v',
718         //      mod.alt   ? 'a' : '-',
719         //      mod.ctrl  ? 'c' : '-',
720         //      mod.shift ? 's' : '-',
721         //      mod.win   ? 'w' : '-');
722
723         /* Mouse movement */
724         if (key == key_mouse1)
725                 raise_float(win);
726         if (key_mouse0 <= key && key <= key_mouse7) {
727                 if (key == key_mouse1 && mod.MODKEY && !mod.up)
728                         return set_move(win,ptr,move),   1;
729                 if (key == key_mouse3 && mod.MODKEY && !mod.up)
730                         return set_move(win,ptr,resize), 1;
731                 if (move_mode != none && mod.up)
732                         return set_move(win,ptr,none),   1;
733                 if (key == key_mouse1 && !mod.up && win->state == st_shade)
734                         return set_focus(win), wm_update(), 0;
735                 return 0;
736         }
737
738         /* Only handle key-down */
739         if (mod.up)
740                 return 0;
741
742         /* Misc */
743         if (mod.MODKEY) {
744 #ifdef DEBUG
745                 if (key == key_f1) return raise_float(win), 1;
746                 if (key == key_f2) return set_focus(win), 1;
747                 if (key == key_f3) return sys_show(win, st_show),  1;
748                 if (key == key_f4) return sys_show(win, st_hide),  1;
749                 if (key == key_f7) return sys_show(win, st_shade), 1;
750 #endif
751                 if (key == key_f5) return wm_update(),    1;
752                 if (key == key_f6) return print_txt(),    1;
753                 if (key == 'q')    return sys_exit(),     1;
754         }
755
756         /* Floating layer */
757         if (key == ' ') {
758                 if (mod.MODKEY && mod.shift)
759                         return set_layer(win), 1;
760                 if (mod.MODKEY)
761                         return switch_layer(), 1;
762         }
763
764         /* Movement commands */
765         if (mod.MODKEY && mod.shift) {
766                 switch (key) {
767                 case 'h': return shift_window(wm_focus,-1, 0), 1;
768                 case 'j': return shift_window(wm_focus, 0,+1), 1;
769                 case 'k': return shift_window(wm_focus, 0,-1), 1;
770                 case 'l': return shift_window(wm_focus,+1, 0), 1;
771                 default: break;
772                 }
773         }
774         else if (mod.MODKEY) {
775                 switch (key) {
776                 case 'h': return shift_focus(-1, 0), 1;
777                 case 'j': return shift_focus( 0,+1), 1;
778                 case 'k': return shift_focus( 0,-1), 1;
779                 case 'l': return shift_focus(+1, 0), 1;
780                 default: break;
781                 }
782         }
783
784         /* Column mode commands */
785         if (mod.MODKEY) {
786                 switch (key) {
787                 case 'd': return set_mode(win, split), 1;
788                 case 's': return set_mode(win, stack), 1;
789                 case 'm': return set_mode(win, max),   1;
790                 case 't': return set_mode(win, tab),   1;
791                 default: break;
792                 }
793         }
794
795         /* Tag switching */
796         if (mod.MODKEY && '0' <= key && key <= '9') {
797                 int name = key - '0';
798                 if (mod.shift)
799                         tag_set(win, name);
800                 else
801                         tag_switch(name);
802                 wm_update();
803         }
804
805         /* Focus change */
806         if (key == key_enter && win->state != st_shade)
807                 return set_focus(win), 1;
808
809         if (key_mouse0 <= key && key <= key_mouse7)
810                 return set_focus(win), 0;
811
812         /* Reset focus after after focus change,
813          * not sure what is causing the focus change in the first place
814          * but preventing that would be a better solution */
815         if (key == key_focus)
816                 sys_focus(wm_focus ?: wm->root);
817
818         return 0;
819 }
820
821 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
822 {
823         //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
824         //              cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
825
826         if (move_mode == none)
827                 return 0;
828
829         int dx = ptr.rx - move_prev.rx;
830         int dy = ptr.ry - move_prev.ry;
831         move_prev = ptr;
832
833         if (move_layer == tiling && move_mode == resize) {
834                 list_t *vert = move_dir.v < 0 ? move_lrow->prev : move_lrow->next;
835                 list_t *horz = move_dir.h < 0 ? move_lcol->prev : move_lcol->next;
836                 if (vert) {
837                         ROW(move_lrow)->height += move_dir.v * dy;
838                         ROW(vert)->height      -= move_dir.v * dy;
839                 }
840                 if (horz) {
841                         COL(move_lcol)->width  += move_dir.h * dx;
842                         COL(horz)->width       -= move_dir.h * dx;
843                 }
844                 wm_update();
845         }
846
847         if (move_layer == floating) {
848                 flt_t *flt = move_lflt->data;
849                 win_t *win = flt->win;
850                 if (move_mode == move)
851                         sys_move(win, win->x+dx, win->y+dy, win->w, win->h);
852                 else if (move_mode == resize)
853                         sys_move(win,
854                                 win->x + dx * (move_dir.h < 0),
855                                 win->y + dy * (move_dir.v < 0),
856                                 win->w + dx *  move_dir.h,
857                                 win->h + dy *  move_dir.v);
858                 flt->x = win->x; flt->y = win->y;
859                 flt->w = win->w; flt->h = win->h;
860         }
861
862         return 0;
863 }
864
865 void wm_insert(win_t *win)
866 {
867         printf("wm_insert: %p\n", win);
868         print_txt();
869
870         /* Initialize window */
871         win->wm = new0(win_wm_t);
872         sys_watch(win, key_enter,  MOD());
873         sys_watch(win, key_focus,  MOD());
874
875         /* Add to screen */
876         put_win(win, wm_tag, wm_dpy->layer);
877
878         /* Arrange */
879         wm_update();
880         set_focus(wm_focus);
881         print_txt();
882 }
883
884 void wm_remove(win_t *win)
885 {
886         printf("wm_remove: %p\n", win);
887         print_txt();
888         for (list_t *tag = wm->tags; tag; tag = tag->next)
889                 cut_win(win, tag->data);
890         free(win->wm);
891         set_focus(wm_focus);
892         wm_update();
893         print_txt();
894 }
895
896 void wm_init(win_t *root)
897 {
898         printf("wm_init: %p\n", root);
899
900         /* Load configuration */
901         MARGIN = conf_get_int("main.margin", MARGIN);
902         STACK  = conf_get_int("main.stack",  STACK);
903
904         wm          = new0(wm_t);
905         wm->root    = root;
906         wm->screens = list_sort(sys_info(root), 0, sort_win);
907         wm->tag     = tag_new(wm->screens, 1);
908         wm->tags    = list_insert(NULL, wm->tag);
909
910         Key_t keys_e[] = {key_enter, key_focus};
911         Key_t keys_s[] = {'h', 'j', 'k', 'l', 'q', ' ',
912                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
913         Key_t keys_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't', ' ',
914                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
915                 key_f1, key_f2, key_f3, key_f4,  key_f5,  key_f6,
916                 key_f7, key_f8, key_f9, key_f10, key_f11, key_f12,
917                 key_mouse1, key_mouse3};
918         for (int i = 0; i < countof(keys_e); i++)
919                 sys_watch(root, keys_e[i],  MOD());
920         for (int i = 0; i < countof(keys_m); i++)
921                 sys_watch(root, keys_m[i], MOD(.MODKEY=1));
922         for (int i = 0; i < countof(keys_s); i++)
923                 sys_watch(root, keys_s[i], MOD(.MODKEY=1,.shift=1));
924 }
925
926 void wm_free(win_t *root)
927 {
928         /* Re-show and free all windows */
929         while ( wm->tags) { tag_t *tag =  wm->tags->data;
930         while (tag->dpys) { dpy_t *dpy = tag->dpys->data;
931         while (dpy->cols) { col_t *col = dpy->cols->data;
932         while (col->rows) { row_t *row = col->rows->data;
933                 sys_show(row->win, st_show);
934                 free(row->win->wm);
935         col->rows = list_remove(col->rows, col->rows, 1); }
936         dpy->cols = list_remove(dpy->cols, dpy->cols, 1); }
937         while (dpy->flts) { flt_t *flt = dpy->flts->data;
938                 sys_show(flt->win, st_show);
939                 free(flt->win->wm);
940         dpy->flts = list_remove(dpy->flts, dpy->flts, 1); }
941         tag->dpys = list_remove(tag->dpys, tag->dpys, 1); }
942          wm->tags = list_remove( wm->tags,  wm->tags, 1); }
943
944         /* Free remaining data */
945         free(wm);
946 }