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