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