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