]> Pileus Git - wmpus/blob - wm-wmii.c
Move common types to a separate file
[wmpus] / wm-wmii.c
1 /*
2  * Copyright (c) 2011-2012, 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 #include <string.h>
19
20 #include "util.h"
21 #include "conf.h"
22 #include "types.h"
23 #include "sys.h"
24 #include "wm.h"
25
26 /* Configuration */
27 #ifndef MODKEY
28 #define MODKEY alt
29 #endif
30 static int margin = 0;
31 static int stack  = 25;
32
33 /* Enums */
34 typedef enum {
35         NONE, MOVE, RESIZE
36 } drag_t;
37
38 typedef enum {
39         SPLIT, STACK, FULL, TAB
40 } layout_t;
41
42 typedef enum {
43         TILING, FLOATING
44 } layer_t;
45
46 /* Window structure types */
47 struct win_wm { };
48
49 typedef struct {
50         win_t   *win;      // the window
51         int      height;   // win height in _this_ tag
52         state_t  state;    // state of window
53 } row_t;
54
55 typedef struct {
56         list_t  *rows;     // of row_t
57         row_t   *row;      // focused row
58         int      width;    // column width
59         layout_t layout;   // column layout
60 } col_t;
61
62 typedef struct {
63         win_t   *win;      // the window
64         int x, y, w, h;    // position of window (in this tag)
65         state_t  state;    // state of window
66 } flt_t;
67
68 typedef struct {
69         list_t  *cols;     // of col_t
70         col_t   *col;      // focused col
71         list_t  *flts;     // of flt_t
72         flt_t   *flt;      // focused flt
73         layer_t  layer;    // focused layer
74         win_t   *geom;     // display size and position
75 } dpy_t;
76
77 typedef struct {
78         list_t  *dpys;     // of dpy_t
79         dpy_t   *dpy;      // focused dpy
80         char     name[64]; // tag name
81 } tag_t;
82
83 typedef struct {
84         list_t  *tags;     // of tag_t
85         tag_t   *tag;      // focused tag
86         win_t   *root;     // root/background window
87         list_t  *screens;  // display geometry
88 } wm_t;
89
90 #define WIN(node) ((win_t*)(node)->data)
91 #define ROW(node) ((row_t*)(node)->data)
92 #define COL(node) ((col_t*)(node)->data)
93 #define FLT(node) ((flt_t*)(node)->data)
94 #define DPY(node) ((dpy_t*)(node)->data)
95 #define TAG(node) ((tag_t*)(node)->data)
96
97 #define tag_foreach_col(tag, dpy, col, row, win) \
98         for (list_t *dpy =     tag ->dpys; dpy; dpy = dpy->next) \
99         for (list_t *col = DPY(dpy)->cols; col; col = col->next) \
100         for (list_t *row = COL(col)->rows; row; row = row->next) \
101         for (win_t  *win = ROW(row)->win;  win; win = NULL)
102
103 #define tag_foreach_flt(tag, dpy, flt, win) \
104         for (list_t *dpy =     tag ->dpys; dpy; dpy = dpy->next) \
105         for (list_t *flt = DPY(dpy)->flts; flt; flt = flt->next) \
106         for (win_t  *win = FLT(flt)->win;  win; win = NULL)      \
107
108 /* Window management data
109  *   wm_* macros represent the currently focused item
110  *   _only_ wm_focus protects against NULL pointers */
111 static wm_t *wm;
112 #define wm_tag   wm->tag
113 #define wm_dpy   wm->tag->dpy
114 #define wm_col   wm->tag->dpy->col
115 #define wm_row   wm->tag->dpy->col->row
116 #define wm_flt   wm->tag->dpy->flt
117 #define wm_focus get_focus()
118
119 /* Mouse drag data */
120 static drag_t  move_mode;
121 static list_t *move_lrow;
122 static list_t *move_lcol;
123 static list_t *move_lflt;
124 static ptr_t   move_prev;
125 static layer_t move_layer;
126 static struct { int v, h; } move_dir;
127
128 /* Prototypes */
129 void wm_update(void);
130
131 /********************
132  * Helper functions *
133  ********************/
134 static int sort_win(void *a, void *b)
135 {
136         return ((win_t*)a)->x > ((win_t*)b)->x ?  1 :
137                ((win_t*)a)->x < ((win_t*)b)->x ? -1 : 0;
138 }
139
140 static win_t *get_focus(void)
141 {
142         if (!wm_tag || !wm_dpy)
143                 return NULL;
144         switch (wm_dpy->layer) {
145         case TILING:
146                 return wm_col && wm_row ? wm_row->win : NULL;
147         case FLOATING:
148                 return wm_flt ? wm_flt->win : NULL;
149         }
150         return NULL;
151 }
152
153 /* Search for the target window in a given tag
154  * win may exist in other tags as well */
155 static int searchl(tag_t *tag, win_t *target,
156                 list_t **_dpy, list_t **_col, list_t **_row, list_t **_flt)
157 {
158         tag_foreach_col(tag, dpy, col, row, win) {
159                 if (win == target) {
160                         if (_dpy) *_dpy = dpy;
161                         if (_col) *_col = col;
162                         if (_row) *_row = row;
163                         return TILING;
164                 }
165         }
166         tag_foreach_flt(tag, dpy, flt, win) {
167                 if (win == target) {
168                         if (_dpy) *_dpy = dpy;
169                         if (_flt) *_flt = flt;
170                         return FLOATING;
171                 }
172         }
173         return -1;
174 }
175
176 static int search(tag_t *tag, win_t *target,
177                 dpy_t **_dpy, col_t **_col, row_t **_row, flt_t **_flt)
178 {
179         list_t *dpy, *col, *row, *flt;
180         switch (searchl(tag, target, &dpy, &col, &row, &flt)) {
181         case TILING:
182                 if (_dpy) *_dpy = DPY(dpy);
183                 if (_col) *_col = COL(col);
184                 if (_row) *_row = ROW(row);
185                 return TILING;
186         case FLOATING:
187                 if (_dpy) *_dpy = DPY(dpy);
188                 if (_flt) *_flt = FLT(flt);
189                 return FLOATING;
190         }
191         return -1;
192 }
193
194 /* Set the layout for the windows column in the current tag */
195 static void set_mode(win_t *win, layout_t layout)
196 {
197         col_t *col;
198         if (TILING != search(wm_tag, win, NULL, &col, NULL, NULL))
199                 return;
200         printf("set_mode: %p, %d -> %d\n",
201                         col, col->layout, layout);
202         col->layout = layout;
203         if (col->layout == SPLIT)
204                 for (list_t *cur = col->rows; cur; cur = cur->next) {
205                         row_t *row = cur->data;
206                         row->height = wm_dpy->geom->h;
207                 }
208         wm_update();
209 }
210
211 /* Focus the window in the current tag and record
212  * it as the currently focused window */
213 static void set_focus(win_t *win)
214 {
215         if (win == NULL || win == wm->root) {
216                 sys_focus(wm->root);
217                 return;
218         }
219
220         dpy_t *dpy; col_t *col; row_t *row; flt_t *flt;
221         switch (search(wm_tag, win, &dpy, &col, &row, &flt)) {
222         case TILING:
223                 wm_dpy = dpy;
224                 wm_col = col;
225                 wm_row = row;
226                 dpy->layer = TILING;
227                 break;
228         case FLOATING:
229                 wm_dpy = dpy;
230                 wm_flt = flt;
231                 dpy->layer = FLOATING;
232                 break;
233         }
234         sys_focus(win);
235 }
236
237 /* Save mouse start location when moving/resizing windows */
238 static void set_move(win_t *win, ptr_t ptr, drag_t drag)
239 {
240         printf("set_move: %d - %p@%d,%d\n",
241                         drag, win, ptr.rx, ptr.ry);
242         move_mode = drag;
243         if (drag == MOVE || drag == RESIZE) {
244                 move_layer = searchl(wm_tag, win, NULL,
245                                 &move_lcol, &move_lrow, &move_lflt);
246                 if (move_layer < 0)
247                         return;
248                 move_prev = ptr;
249                 int midy = win->y + (win->h/2);
250                 int midx = win->x + (win->w/2);
251                 move_dir.v = ptr.ry < midy ? -1 : +1;
252                 move_dir.h = ptr.rx < midx ? -1 : +1;
253         }
254 }
255
256 /* Print a text representation of the window layout
257  * Quite useful for debugging */
258 static void print_txt(void)
259 {
260         for (list_t *ltag = wm->tags; ltag; ltag = ltag->next) {
261                 tag_t *tag = ltag->data;
262                 printf("tag:       <%-9p [%p->%p] >%-9p d=%-9p - %s\n",
263                                 ltag->prev, ltag, ltag->data, ltag->next,
264                                 tag->dpy, tag->name);
265         for (list_t *ldpy = tag->dpys; ldpy; ldpy = ldpy->next) {
266                 dpy_t *dpy  = ldpy->data;
267                 win_t *geom = dpy->geom;
268                 printf("  dpy:     <%-9p [%p->%p] >%-9p %c=%-9p - %d,%d %dx%d\n",
269                                 ldpy->prev, ldpy, ldpy->data, ldpy->next,
270                                 dpy->layer == TILING ? 'c' : 'f',
271                                 dpy->layer == TILING ? (void*)dpy->col : (void*)dpy->flt,
272                                 geom->x, geom->y, geom->h, geom->w);
273         for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
274                 col_t *col = lcol->data;
275                 printf("    col:   <%-9p [%p->%p] >%-9p r=%-9p - %dpx @ %d\n",
276                                 lcol->prev, lcol, lcol->data, lcol->next,
277                                 col->row, col->width, col->layout);
278         for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
279                 row_t *row = lrow->data;
280                 win_t *win = row->win;
281                 printf("      row: <%-9p [%p>>%p] >%-9p focus=%d%d    - %4dpx \n",
282                                 lrow->prev, lrow, win, lrow->next,
283                                 col->row == row, wm_focus == win, win->h);
284         } }
285         for (list_t *lflt = dpy->flts; lflt; lflt = lflt->next) {
286                 flt_t *flt = lflt->data;
287                 win_t *win = flt->win;
288                 printf("    flt:   <%-9p [%p>>%p] >%-9p focus=%d%d    - %d,%d %dx%d \n",
289                                 lflt->prev, lflt, win, lflt->next,
290                                 dpy->flt == flt, wm_focus == flt->win,
291                                 flt->x, flt->y, flt->h, flt->w);
292         }  } }
293 }
294
295 /* Cleanly remove a window from a tag
296  *   Determines the new focused row/col
297  *   Prunes empty lists */
298 static layer_t cut_win(win_t *win, tag_t *tag)
299 {
300         list_t *ldpy, *lcol, *lrow, *lflt;
301         layer_t layer = searchl(tag, win, &ldpy, &lcol, &lrow, &lflt);
302
303         if (layer == TILING) {
304                 dpy_t *dpy = DPY(ldpy);
305                 col_t *col = COL(lcol);
306                 col->row  = lrow->prev ? lrow->prev->data :
307                             lrow->next ? lrow->next->data : NULL;
308                 col->rows = list_remove(col->rows, lrow, 1);
309                 if (col->rows == NULL && (lcol->next || lcol->prev)) {
310                         dpy->col  = lcol->prev ? lcol->prev->data :
311                                     lcol->next ? lcol->next->data : NULL;
312                         dpy->cols = list_remove(dpy->cols, lcol, 1);
313                 }
314         }
315
316         if (layer == FLOATING) {
317                 dpy_t *dpy = DPY(ldpy);
318                 dpy->flts = list_remove(dpy->flts, lflt, 1);
319                 dpy->flt  = dpy->flts ? list_last(dpy->flts)->data : NULL;
320                 if (!dpy->flt && dpy->col && dpy->col->row)
321                         dpy->layer = TILING;
322         }
323
324         return layer;
325 }
326
327 /* Insert a window into the tiling layer
328  *   The window is added immediately after the
329  *   columns currently focused row */
330 static void put_win_col(win_t *win, tag_t *tag, dpy_t *dpy, col_t *col)
331 {
332         row_t *row = new0(row_t);
333         row->win   = win;
334         row->state = win->state ?: ST_SHOW;
335
336         if (col == NULL) {
337                 col = new0(col_t);
338                 dpy->cols = list_insert(dpy->cols, col);
339         }
340
341         int nrows = list_length(col->rows);
342         if (col->row) {
343                 list_t *prev = list_find(col->rows, col->row);
344                 list_insert_after(prev, row);
345         } else {
346                 col->rows = list_insert(col->rows, row);
347         }
348         tag->dpy           = dpy;
349         tag->dpy->col      = col;
350         tag->dpy->col->row = row;
351         tag->dpy->layer    = TILING;
352
353         row->height = dpy->geom->h / MAX(nrows,1);
354         if (nrows == 0) {
355                 int ncols = list_length(dpy->cols);
356                 col->width = dpy->geom->w / MAX(ncols-1,1);
357         }
358 }
359
360 /* Insert a window into the floating layer */
361 static void put_win_flt(win_t *win, tag_t *tag, dpy_t *dpy)
362 {
363         flt_t *flt = new0(flt_t);
364         flt->win   = win;
365         flt->w     = dpy->geom->w / 2;
366         flt->h     = dpy->geom->h / 2;
367         flt->x     = dpy->geom->x + flt->w / 2;
368         flt->y     = dpy->geom->y + flt->h / 2;
369         flt->state = win->state ?: ST_SHOW;
370         if (dpy->flt) {
371                 flt->x = dpy->flt->x + 20;
372                 flt->y = dpy->flt->y + 20;
373         }
374         dpy->flts = list_append(dpy->flts, flt);
375         tag->dpy        = dpy;
376         tag->dpy->flt   = flt;
377         tag->dpy->layer = FLOATING;
378 }
379
380 /* Insert a window into a tag */
381 static void put_win(win_t *win, tag_t *tag, layer_t layer)
382 {
383         if (layer == TILING)
384                 put_win_col(win, tag, tag->dpy, tag->dpy->col);
385         if (layer == FLOATING)
386                 put_win_flt(win, tag, tag->dpy);
387 }
388
389 /* Move a window up, down, left, or right
390  *   This handles moving with a column, between
391  *   columns, and between multiple monitors. */
392 static void shift_window(win_t *win, int col, int row)
393 {
394         if (!win) return;
395         printf("shift_window: %p - %+d,%+d\n", win, col, row);
396         print_txt();
397         printf("shift_window: >>>\n");
398         list_t *ldpy, *lcol, *lrow, *lflt;
399         if (TILING != searchl(wm_tag, win, &ldpy, &lcol, &lrow, &lflt))
400                 return;
401         dpy_t *dpy = ldpy->data;
402         if (row != 0) {
403                 /* Move with a column, just swap rows */
404                 list_t *src = lrow, *dst = NULL;
405                 if (row < 0) dst = src->prev;
406                 if (row > 0) dst = src->next;
407                 if (src && dst) {
408                         printf("swap: %p <-> %p\n", src->data, dst->data);
409                         row_t *tmp = src->data;
410                         src->data = dst->data;
411                         dst->data = tmp;
412                         goto update;
413                 }
414         } else {
415                 /* Moving between columns */
416                 int onlyrow = !lrow->prev && !lrow->next;
417                 list_t *src = lcol, *dst = NULL;
418                 if (col < 0) {
419                         if (src->prev) {
420                                 /* Normal move between columns */
421                                 dst = src->prev;
422                         } else if (!onlyrow) {
423                                 /* Create new column */
424                                 dpy->cols = list_insert(dpy->cols, new0(col_t));
425                                 dst = src->prev;
426                         } else if (ldpy->prev) {
427                                 /* Move to next monitor */
428                                 dpy = ldpy->prev->data;
429                                 dst = list_last(dpy->cols);
430                         } else {
431                                 /* We, shall, not, be,
432                                  * we shall not be moved */
433                                 return;
434                         }
435                 }
436                 if (col > 0) {
437                         if (src->next) {
438                                 dst = src->next;
439                         } else if (!onlyrow) {
440                                 dpy->cols = list_append(dpy->cols, new0(col_t));
441                                 dst = src->next;
442                         } else if (ldpy->next) {
443                                 dpy = ldpy->next->data;
444                                 dst = dpy->cols;
445                         } else {
446                                 return;
447                         }
448                 }
449                 cut_win(win, wm_tag);
450                 put_win_col(win, wm_tag, dpy, dst ? dst->data : NULL);
451                 goto update;
452         }
453 update:
454         print_txt();
455         wm_update();
456 }
457
458 /* Get next/prev item, with wraparound */
459 static list_t *get_next(list_t *list, int forward)
460 {
461         list_t *next = forward ? list->next : list->prev;
462         if (next == NULL) {
463                 next = list;
464                 while ((list = forward ? next->prev : next->next))
465                         next = list;
466         }
467         return next;
468 }
469
470 /* Move keyboard focus in a given direction */
471 static void shift_focus(int cols, int rows)
472 {
473         printf("shift_focus: %+d,%+d\n", cols, rows);
474         if (rows != 0 && wm_focus) {
475                 /* Move focus up/down */
476                 list_t *dpy, *col, *row;
477                 if (TILING != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
478                         return;
479                 row_t *next = get_next(row, rows > 0)->data;
480                 set_focus(next->win);
481                 if (COL(col)->layout != SPLIT)
482                         wm_update();
483         }
484         if (cols != 0) {
485                 /* Move focus left/right */
486                 list_t *dpy, *col, *row, *ndpy, *ncol = NULL;
487                 if (wm_focus) {
488                         /* Currently focused on a window */
489                         if (TILING != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
490                                 return;
491                         ncol = cols > 0 ? col->next : col->prev;
492                 } else {
493                         /* Currently focused on an empty display */
494                         dpy = list_find(wm_tag->dpys, wm_dpy);
495                 }
496                 if (ncol == NULL) {
497                         /* Moving focus to a different display */
498                         ndpy = get_next(dpy, cols > 0);
499                         ncol = cols > 0 ? DPY(ndpy)->cols :
500                                 list_last(DPY(ndpy)->cols);
501                         wm_dpy = ndpy->data;
502                 }
503                 if (ncol && COL(ncol) && COL(ncol)->row)
504                         set_focus(COL(ncol)->row->win);
505                 else
506                         sys_focus(wm->root);
507         }
508 }
509
510 /* Raise the window in the floating */
511 static void raise_float(win_t *win)
512 {
513         printf("raise_float: %p\n", win);
514         list_t *cur;
515         for (cur = wm_dpy->flts; cur; cur = cur->next)
516                 if (FLT(cur)->win == win)
517                         break;
518         if (cur) {
519                 flt_t *flt = cur->data;
520                 wm_dpy->flts = list_remove(wm_dpy->flts, cur, 0);
521                 wm_dpy->flts = list_append(wm_dpy->flts, flt);
522                 sys_raise(win);
523         }
524 }
525
526 /* Toggle between floating and tiling layers */
527 static void switch_layer(void)
528 {
529         printf("switch_float: %p %d\n",
530                         wm_dpy, wm_dpy->layer);
531         wm_dpy->layer = !wm_dpy->layer;
532         wm_update();
533 }
534
535 /* Move current window between floating and tiling layers */
536 static void set_layer(win_t *win)
537 {
538         if (!win) return;
539         printf("set_float: %p %p\n", wm_dpy, win);
540         wm_dpy->layer = !cut_win(win, wm_tag);
541         put_win(win, wm_tag, wm_dpy->layer);
542         wm_update();
543 }
544
545 /* Allocate a new tag */
546 static tag_t *tag_new(list_t *screens, const char *name)
547 {
548         tag_t *tag = new0(tag_t);
549         strncpy(tag->name, name, sizeof(tag->name));
550         for (list_t *cur = screens; cur; cur = cur->next) {
551                 dpy_t *dpy  = new0(dpy_t);
552                 dpy->geom = cur->data;
553                 tag->dpys = list_append(tag->dpys, dpy);
554         }
555         tag->dpy  = tag->dpys->data;
556         for (list_t *dpy = tag->dpys; dpy; dpy = dpy->next)
557                 if (DPY(dpy)->geom->z > tag->dpy->geom->z)
558                         tag->dpy = dpy->data;
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(const char *name)
566 {
567         tag_t *tag = NULL;
568         for (list_t *cur = wm->tags; cur; cur = cur->next)
569                 if (!strcmp(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, const char *name)
583 {
584         printf("tag_set: %p %s\n", win, name);
585         if (!strcmp(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(const char *name)
595 {
596         printf("tag_switch: %s\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                         if (ROW(lrow)->state != ST_SHOW) {
644                                 sys_show(win, ROW(lrow)->state);
645                                 continue;
646                         }
647                         win->h = ROW(lrow)->height;
648                         state_t state = ST_SHOW;
649                         int height = 0;
650                         switch (col->layout) {
651                         case SPLIT:
652                                 sys_move(win, x+margin, y+margin,
653                                         col->width, win->h * ((float)my / ty));
654                                 height = win->h;
655                                 y += height + margin;
656                                 break;
657                         case STACK:
658                                 if (lrow->next && ROW(lrow->next)->win == col->row->win) {
659                                         /* Hack to prevent flashing */
660                                         win_t *next = ROW(lrow->next)->win;
661                                         sys_move(next, x+margin, y+margin+stack+margin/2,
662                                                 col->width, sy);
663                                         sys_show(next, ST_SHOW);
664                                 }
665                                 int isfocus = win == col->row->win;
666                                 state  = isfocus ? ST_SHOW : ST_SHADE;
667                                 sys_show(win, state);
668                                 sys_move(win, x+margin, y+margin, col->width, sy);
669                                 y += (isfocus ? sy : stack) + (margin/2);
670                                 break;
671                         case FULL:
672                         case TAB:
673                                 sys_move(win, x+margin, 0+margin,
674                                         col->width, dpy->geom->h-2*margin);
675                                 break;
676                         }
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 /* Refresh the window layout */
687 void wm_update(void)
688 {
689         /* Updates window sizes */
690         for (list_t *ldpy = wm_tag->dpys; ldpy; ldpy = ldpy->next)
691                 wm_update_cols(ldpy->data);
692         tag_foreach_flt(wm_tag, ldpy, lflt, win) {
693                 flt_t *flt = lflt->data;
694                 sys_move(win, flt->x, flt->y, flt->w, flt->h);
695                 sys_raise(flt->win);
696                 sys_show(flt->win, flt->state);
697         }
698
699         /* Hide other tags */
700         for (list_t *tag = wm ->tags; tag; tag = tag->next)
701                 if (tag->data != wm_tag) {
702                         tag_foreach_col(TAG(tag), dpy, col, row, win)
703                                 sys_show(win, ST_HIDE);
704                         tag_foreach_flt(TAG(tag), dpy, flt, win)
705                                 sys_show(win, ST_HIDE);
706                 }
707
708         /* Set focused window */
709         if (wm_focus)
710                 set_focus(wm_focus);
711 }
712
713 /*******************************
714  * Window management functions *
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)
719                 return 0;
720         //printf("wm_handle_event: %p - %x %c%c%c%c%c\n", win, ev,
721         //      mod.up    ? '^' : 'v',
722         //      mod.alt   ? 'a' : '-',
723         //      mod.ctrl  ? 'c' : '-',
724         //      mod.shift ? 's' : '-',
725         //      mod.win   ? 'w' : '-');
726
727         /* Mouse events */
728         if (EV_MOUSE0 <= ev && ev <= EV_MOUSE7) {
729                 if (ev == EV_MOUSE1 && !mod.MODKEY && !mod.up)
730                         return raise_float(win),         0;
731                 if ((ev == EV_MOUSE3 && mod.MODKEY && !mod.up) ||
732                     (ev == EV_MOUSE1 && mod.MODKEY && !mod.up && mod.shift))
733                         return set_move(win,ptr,RESIZE), 1;
734                 if (ev == EV_MOUSE1 && mod.MODKEY && !mod.up)
735                         return set_move(win,ptr,MOVE),   1;
736                 if (move_mode != NONE && mod.up)
737                         return set_move(win,ptr,NONE),   1;
738                 if (ev == EV_MOUSE1 && !mod.up && win->state == ST_SHADE)
739                         return set_focus(win), wm_update(), 0;
740                 return 0;
741         }
742
743         /* Only handle key-down */
744         if (mod.up)
745                 return mod.MODKEY || ev == EV_ALT;
746
747         /* Misc */
748         if (mod.MODKEY) {
749 #ifdef DEBUG
750                 if (ev == EV_F1) return raise_float(win), 1;
751                 if (ev == EV_F2) return set_focus(win), 1;
752                 if (ev == EV_F3) return sys_show(win, ST_SHOW),  1;
753                 if (ev == EV_F4) return sys_show(win, ST_HIDE),  1;
754                 if (ev == EV_F7) return sys_show(win, ST_SHADE), 1;
755 #endif
756                 if (ev == EV_F5) return wm_update(),    1;
757                 if (ev == EV_F6) return print_txt(),    1;
758                 if (ev == 'q')   return sys_exit(),     1;
759                 if (ev == 'f')   return wm_handle_state(win, win->state,
760                         win->state == ST_FULL ? ST_SHOW : ST_FULL);
761                 if (ev == 'g')   return wm_handle_state(win, win->state,
762                         win->state == ST_MAX  ? ST_SHOW : ST_MAX);
763         }
764         if (mod.MODKEY && mod.shift) {
765                 if (ev == 'c')   return sys_show(win, ST_CLOSE), 1;
766         }
767
768         /* Floating layer */
769         if (ev == ' ') {
770                 if (mod.MODKEY && mod.shift)
771                         return set_layer(win), 1;
772                 if (mod.MODKEY)
773                         return switch_layer(), 1;
774         }
775
776         /* Movement commands */
777         if (mod.MODKEY && mod.shift) {
778                 switch (ev) {
779                 case 'h': return shift_window(wm_focus,-1, 0), 1;
780                 case 'j': return shift_window(wm_focus, 0,+1), 1;
781                 case 'k': return shift_window(wm_focus, 0,-1), 1;
782                 case 'l': return shift_window(wm_focus,+1, 0), 1;
783                 default: break;
784                 }
785         }
786         else if (mod.MODKEY) {
787                 switch (ev) {
788                 case 'h': return shift_focus(-1, 0), 1;
789                 case 'j': return shift_focus( 0,+1), 1;
790                 case 'k': return shift_focus( 0,-1), 1;
791                 case 'l': return shift_focus(+1, 0), 1;
792                 default: break;
793                 }
794         }
795
796         /* Column layout commands */
797         if (mod.MODKEY) {
798                 switch (ev) {
799                 case 'd': return set_mode(win, SPLIT), 1;
800                 case 's': return set_mode(win, STACK), 1;
801                 case 'm': return set_mode(win, FULL),  1;
802                 case 't': return set_mode(win, TAB),   1;
803                 default: break;
804                 }
805         }
806
807         /* Tag switching */
808         if (mod.MODKEY && '0' <= ev && ev <= '9') {
809                 char name[] = {ev, '\0'};
810                 if (mod.shift)
811                         tag_set(win, name);
812                 else
813                         tag_switch(name);
814                 wm_update();
815         }
816
817         /* Focus change */
818         if (ev == EV_ENTER && win->state != ST_SHADE)
819                 return set_focus(win), 1;
820
821         /* Reset focus after after focus change,
822          * not sure what is causing the focus change in the first place
823          * but preventing that would be a better solution */
824         if (ev == EV_FOCUS)
825                 sys_focus(wm_focus ?: wm->root);
826
827         return mod.MODKEY;
828 }
829
830 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
831 {
832         //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
833         //              cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
834
835         if (move_mode == NONE)
836                 return 0;
837
838         int dx = ptr.rx - move_prev.rx;
839         int dy = ptr.ry - move_prev.ry;
840         move_prev = ptr;
841
842         if (move_layer == TILING && move_mode == RESIZE) {
843                 list_t *vert = move_dir.v < 0 ? move_lrow->prev : move_lrow->next;
844                 list_t *horz = move_dir.h < 0 ? move_lcol->prev : move_lcol->next;
845                 if (vert) {
846                         ROW(move_lrow)->height += move_dir.v * dy;
847                         ROW(vert)->height      -= move_dir.v * dy;
848                 }
849                 if (horz) {
850                         COL(move_lcol)->width  += move_dir.h * dx;
851                         COL(horz)->width       -= move_dir.h * dx;
852                 }
853                 wm_update();
854         }
855
856         if (move_layer == FLOATING) {
857                 flt_t *flt = move_lflt->data;
858                 win_t *win = flt->win;
859                 if (move_mode == MOVE)
860                         sys_move(win, win->x+dx, win->y+dy, win->w, win->h);
861                 else if (move_mode == RESIZE)
862                         sys_move(win,
863                                 win->x + dx * (move_dir.h < 0),
864                                 win->y + dy * (move_dir.v < 0),
865                                 win->w + dx *  move_dir.h,
866                                 win->h + dy *  move_dir.v);
867                 flt->x = win->x; flt->y = win->y;
868                 flt->w = win->w; flt->h = win->h;
869         }
870
871         return 0;
872 }
873
874 int wm_handle_state(win_t *win, state_t prev, state_t next)
875 {
876         row_t *row = NULL;
877         flt_t *flt = NULL;
878
879         printf("wm_handle_state - %p %x -> %x\n", win, prev, next);
880
881         search(wm_tag, win, NULL, NULL, &row, &flt);
882
883         if (!row && !flt && next == ST_SHOW)
884                 return wm_insert(win), 1;
885         if ((row || flt) && (next == ST_HIDE || next == ST_ICON))
886                 return wm_remove(win), 1;
887
888         if (row) row->state = next;
889         if (flt) flt->state = next;
890
891         if (prev == ST_MAX || prev == ST_FULL ||
892             next == ST_MAX || next == ST_FULL)
893                 wm_update();
894
895         return 1;
896 }
897
898 void wm_insert(win_t *win)
899 {
900         printf("wm_insert: %p\n", win);
901         print_txt();
902
903         /* Check for toolbars */
904         if (win->type == TYPE_TOOLBAR)
905                 return wm_update();
906
907         /* Initialize window */
908         sys_watch(win, EV_ENTER, MOD());
909         sys_watch(win, EV_FOCUS, MOD());
910
911         /* Add to screen */
912         if (win->type == TYPE_DIALOG || win->parent)
913                 wm_dpy->layer = FLOATING;
914         put_win(win, wm_tag, wm_dpy->layer);
915
916         /* Arrange */
917         wm_update();
918         set_focus(win);
919         print_txt();
920 }
921
922 void wm_remove(win_t *win)
923 {
924         printf("wm_remove: %p\n", win);
925         print_txt();
926         if (win->type == TYPE_TOOLBAR)
927                 return wm_update();
928         for (list_t *tag = wm->tags; tag; tag = tag->next)
929                 cut_win(win, tag->data);
930         set_focus(wm_focus);
931         wm_update();
932         print_txt();
933 }
934
935 void wm_init(win_t *root)
936 {
937         printf("wm_init: %p\n", root);
938
939         /* Load configuration */
940         margin = conf_get_int("main.margin", margin);
941         stack  = conf_get_int("main.stack",  stack);
942
943         wm          = new0(wm_t);
944         wm->root    = root;
945         wm->screens = list_sort(sys_info(root), 0, sort_win);
946         wm->tag     = tag_new(wm->screens, "1");
947         wm->tags    = list_insert(NULL, wm->tag);
948
949         event_t ev_e[] = {EV_ENTER, EV_FOCUS};
950         event_t ev_s[] = {'h', 'j', 'k', 'l', 'c', 'q', ' ',
951                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
952                 EV_MOUSE1, EV_MOUSE3};
953         event_t ev_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't', 'f', 'g', ' ',
954                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
955                 EV_F1, EV_F2, EV_F3, EV_F4,  EV_F5,  EV_F6,
956                 EV_F7, EV_F8, EV_F9, EV_F10, EV_F11, EV_F12,
957                 EV_MOUSE1, EV_MOUSE3};
958         for (int i = 0; i < countof(ev_e); i++)
959                 sys_watch(root, ev_e[i],  MOD());
960         for (int i = 0; i < countof(ev_m); i++)
961                 sys_watch(root, ev_m[i], MOD(.MODKEY=1));
962         for (int i = 0; i < countof(ev_s); i++)
963                 sys_watch(root, ev_s[i], MOD(.MODKEY=1,.shift=1));
964 }
965
966 void wm_free(win_t *root)
967 {
968         /* Re-show and free all windows */
969         while ( wm->tags) { tag_t *tag =  wm->tags->data;
970         while (tag->dpys) { dpy_t *dpy = tag->dpys->data;
971         while (dpy->cols) { col_t *col = dpy->cols->data;
972         while (col->rows) { row_t *row = col->rows->data;
973                 sys_show(row->win, ST_SHOW);
974         col->rows = list_remove(col->rows, col->rows, 1); }
975         dpy->cols = list_remove(dpy->cols, dpy->cols, 1); }
976         while (dpy->flts) { flt_t *flt = dpy->flts->data;
977                 sys_show(flt->win, ST_SHOW);
978         dpy->flts = list_remove(dpy->flts, dpy->flts, 1); }
979         tag->dpys = list_remove(tag->dpys, tag->dpys, 1); }
980          wm->tags = list_remove( wm->tags,  wm->tags, 1); }
981
982         /* Free remaining data */
983         free(wm);
984 }