Support graceful shutdown
[wmpus] / wm-wmii.c
1 /*
2  * Copyright (c) 2011, Andy Spencer <andy753421@gmail.com>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #include "util.h"
20 #include "sys.h"
21 #include "wm.h"
22
23 #ifndef MODKEY
24 #define MODKEY alt
25 #endif
26 #ifndef MARGIN
27 #define MARGIN 0
28 #endif
29 #ifndef STACK
30 #define STACK  25
31 #endif
32
33 /* Enums */
34 typedef enum {
35         none, move, resize
36 } drag_t;
37
38 typedef enum {
39         split, stack, max, tab
40 } mode_t;
41
42 typedef enum {
43         tiling, floating
44 } layer_t;
45
46 /* Window structure types */
47 struct win_wm { };
48
49 typedef struct {
50         win_t  *win;     // the window
51         int     height;  // win height in _this_ tag
52 } 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         mode_t  mode;    // display mode
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 } flt_t;
65
66 typedef struct {
67         list_t *cols;    // of col_t
68         col_t  *col;     // focused col
69         list_t *flts;    // of flt_t
70         flt_t  *flt;     // focused flt
71         layer_t layer;   // focused layer
72         win_t  *geom;    // display size and position
73 } dpy_t;
74
75 typedef struct {
76         list_t *dpys;    // of dpy_t
77         dpy_t  *dpy;     // focused dpy
78         int     name;    // tag name
79 } tag_t;
80
81 typedef struct {
82         list_t *tags;    // of tag_t
83         tag_t  *tag;     // focused tag
84         win_t  *root;    // root/background window
85         list_t *screens; // display geometry
86 } wm_t;
87
88 #define WIN(node) ((win_t*)(node)->data)
89 #define ROW(node) ((row_t*)(node)->data)
90 #define COL(node) ((col_t*)(node)->data)
91 #define FLT(node) ((flt_t*)(node)->data)
92 #define DPY(node) ((dpy_t*)(node)->data)
93 #define TAG(node) ((tag_t*)(node)->data)
94
95 #define tag_foreach_col(tag, dpy, col, row, win) \
96         for (list_t *dpy =     tag ->dpys; dpy; dpy = dpy->next) \
97         for (list_t *col = DPY(dpy)->cols; col; col = col->next) \
98         for (list_t *row = COL(col)->rows; row; row = row->next) \
99         for (win_t  *win = ROW(row)->win;  win; win = NULL)
100
101 #define tag_foreach_flt(tag, dpy, flt, win) \
102         for (list_t *dpy =     tag ->dpys; dpy; dpy = dpy->next) \
103         for (list_t *flt = DPY(dpy)->flts; flt; flt = flt->next) \
104         for (win_t  *win = FLT(flt)->win;  win; win = NULL)      \
105
106 /* Window management data
107  *   wm_* macros represent the currently focused item
108  *   _only_ wm_focus protects against NULL pointers */
109 static wm_t *wm;
110 #define wm_tag   wm->tag
111 #define wm_dpy   wm->tag->dpy
112 #define wm_col   wm->tag->dpy->col
113 #define wm_row   wm->tag->dpy->col->row
114 #define wm_flt   wm->tag->dpy->flt
115 #define wm_focus get_focus()
116
117 /* Mouse drag data */
118 static drag_t  move_mode;
119 static list_t *move_lrow;
120 static list_t *move_lcol;
121 static list_t *move_lflt;
122 static ptr_t   move_prev;
123 static layer_t move_layer;
124 static struct { int v, h; } move_dir;
125
126 /********************
127  * Helper functions *
128  ********************/
129 static win_t *get_focus(void)
130 {
131         if (!wm_tag || !wm_dpy)
132                 return NULL;
133         switch (wm_dpy->layer) {
134         case tiling:
135                 return wm_col && wm_row ? wm_row->win : NULL;
136         case floating:
137                 return wm_flt ? wm_flt->win : NULL;
138         }
139         return NULL;
140 }
141
142 /* Search for the target window in a given tag
143  * win may exist in other tags as well */
144 static int searchl(tag_t *tag, win_t *target,
145                 list_t **_dpy, list_t **_col, list_t **_row, list_t **_flt)
146 {
147         tag_foreach_col(tag, dpy, col, row, win) {
148                 if (win == target) {
149                         if (_dpy) *_dpy = dpy;
150                         if (_col) *_col = col;
151                         if (_row) *_row = row;
152                         return tiling;
153                 }
154         }
155         tag_foreach_flt(tag, dpy, flt, win) {
156                 if (win == target) {
157                         if (_dpy) *_dpy = dpy;
158                         if (_flt) *_flt = flt;
159                         return floating;
160                 }
161         }
162         return -1;
163 }
164
165 static int search(tag_t *tag, win_t *target,
166                 dpy_t **_dpy, col_t **_col, row_t **_row, flt_t **_flt)
167 {
168         list_t *dpy, *col, *row, *flt;
169         switch (searchl(tag, target, &dpy, &col, &row, &flt)) {
170         case tiling:
171                 if (_dpy) *_dpy = DPY(dpy);
172                 if (_col) *_col = COL(col);
173                 if (_row) *_row = ROW(row);
174                 return tiling;
175         case floating:
176                 if (_dpy) *_dpy = DPY(dpy);
177                 if (_flt) *_flt = FLT(flt);
178                 return floating;
179         }
180         return -1;
181 }
182
183 /* Set the mode for the windows column in the current tag */
184 static void set_mode(win_t *win, mode_t mode)
185 {
186         col_t *col;
187         if (tiling != search(wm_tag, win, NULL, &col, NULL, NULL))
188                 return;
189         printf("set_mode: %p, %d -> %d\n",
190                         col, col->mode, mode);
191         col->mode = mode;
192         if (col->mode == split)
193                 for (list_t *cur = col->rows; cur; cur = cur->next) {
194                         row_t *row = cur->data;
195                         row->height = wm_dpy->geom->h;
196                 }
197         wm_update();
198 }
199
200 /* Focus the window in the current tag and record
201  * it as the currently focused window */
202 static void set_focus(win_t *win)
203 {
204         if (win == NULL || win == wm->root) {
205                 sys_focus(wm->root);
206                 return;
207         }
208
209         /* - Only grab mouse button on unfocused window,
210          *   this prevents stealing all mouse clicks from client windows,
211          * - A better way may be to re-send mouse clicks to client windows
212          *   using the return value from wm_handle_key */
213         for (int i = key_mouse1; i < key_mouse7; i++) {
214                 if (wm_focus)
215                         sys_watch(wm_focus, i, MOD());
216                 sys_unwatch(win, i, MOD());
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 - %d\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->mode);
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
334         if (col == NULL) {
335                 col = new0(col_t);
336                 dpy->cols = list_insert(dpy->cols, col);
337         }
338
339         int nrows = list_length(col->rows);
340         if (col->row) {
341                 list_t *prev = list_find(col->rows, col->row);
342                 list_insert_after(prev, row);
343         } else {
344                 col->rows = list_insert(col->rows, row);
345         }
346         tag->dpy           = dpy;
347         tag->dpy->col      = col;
348         tag->dpy->col->row = row;
349         tag->dpy->layer    = tiling;
350
351         row->height = dpy->geom->h / MAX(nrows,1);
352         if (nrows == 0) {
353                 int ncols = list_length(dpy->cols);
354                 col->width = dpy->geom->w / MAX(ncols-1,1);
355         }
356 }
357
358 /* Insert a window into the floating layer */
359 static void put_win_flt(win_t *win, tag_t *tag, dpy_t *dpy)
360 {
361         flt_t *flt = new0(flt_t);
362         flt->win = win;
363         flt->w   = dpy->geom->w / 2;
364         flt->h   = dpy->geom->h / 2;
365         flt->x   = dpy->geom->x + flt->w / 2;
366         flt->y   = dpy->geom->y + flt->h / 2;
367         if (dpy->flt) {
368                 flt->x = dpy->flt->x + 20;
369                 flt->y = dpy->flt->y + 20;
370         }
371         dpy->flts = list_append(dpy->flts, flt);
372         tag->dpy        = dpy;
373         tag->dpy->flt   = flt;
374         tag->dpy->layer = floating;
375 }
376
377 /* Insert a window into a tag */
378 static void put_win(win_t *win, tag_t *tag, layer_t layer)
379 {
380         if (layer == tiling)
381                 put_win_col(win, tag, tag->dpy, tag->dpy->col);
382         if (layer == floating)
383                 put_win_flt(win, tag, tag->dpy);
384 }
385
386 /* Move a window up, down, left, or right
387  *   This handles moving with a column, between
388  *   columns, and between multiple monitors. */
389 static void shift_window(win_t *win, int col, int row)
390 {
391         if (!win) return;
392         printf("shift_window: %p - %+d,%+d\n", win, col, row);
393         print_txt();
394         printf("shift_window: >>>\n");
395         list_t *ldpy, *lcol, *lrow, *lflt;
396         if (tiling != searchl(wm_tag, win, &ldpy, &lcol, &lrow, &lflt))
397                 return;
398         dpy_t *dpy = ldpy->data;
399         if (row != 0) {
400                 /* Move with a column, just swap rows */
401                 list_t *src = lrow, *dst = NULL;
402                 if (row < 0) dst = src->prev;
403                 if (row > 0) dst = src->next;
404                 if (src && dst) {
405                         printf("swap: %p <-> %p\n", src->data, dst->data);
406                         row_t *tmp = src->data;
407                         src->data = dst->data;
408                         dst->data = tmp;
409                         goto update;
410                 }
411         } else {
412                 /* Moving between columns */
413                 int onlyrow = !lrow->prev && !lrow->next;
414                 list_t *src = lcol, *dst = NULL;
415                 if (col < 0) {
416                         if (src->prev) {
417                                 /* Normal move between columns */
418                                 dst = src->prev;
419                         } else if (!onlyrow) {
420                                 /* Create new column */
421                                 dpy->cols = list_insert(dpy->cols, new0(col_t));
422                                 dst = src->prev;
423                         } else if (ldpy->prev) {
424                                 /* Move to next monitor */
425                                 dpy = ldpy->prev->data;
426                                 dst = list_last(dpy->cols);
427                         } else {
428                                 /* We, shall, not, be,
429                                  * we shall not be moved */
430                                 return;
431                         }
432                 }
433                 if (col > 0) {
434                         if (src->next) {
435                                 dst = src->next;
436                         } else if (!onlyrow) {
437                                 dpy->cols = list_append(dpy->cols, new0(col_t));
438                                 dst = src->next;
439                         } else if (ldpy->next) {
440                                 dpy = ldpy->next->data;
441                                 dst = dpy->cols;
442                         } else {
443                                 return;
444                         }
445                 }
446                 cut_win(win, wm_tag);
447                 put_win_col(win, wm_tag, dpy, dst ? dst->data : NULL);
448                 goto update;
449         }
450 update:
451         print_txt();
452         wm_update();
453 }
454
455 /* Get next/prev item, with wraparound */
456 static list_t *get_next(list_t *list, int forward)
457 {
458         list_t *next = forward ? list->next : list->prev;
459         if (next == NULL) {
460                 next = list;
461                 while ((list = forward ? next->prev : next->next))
462                         next = list;
463         }
464         return next;
465 }
466
467 /* Move keyboard focus in a given direction */
468 static void shift_focus(int cols, int rows)
469 {
470         printf("shift_focus: %+d,%+d\n", cols, rows);
471         if (rows != 0 && wm_focus) {
472                 /* Move focus up/down */
473                 list_t *dpy, *col, *row;
474                 if (tiling != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
475                         return;
476                 row_t *next = get_next(row, rows > 0)->data;
477                 set_focus(next->win);
478                 if (COL(col)->mode != split)
479                         wm_update();
480         }
481         if (cols != 0) {
482                 /* Move focus left/right */
483                 list_t *dpy, *col, *row, *ndpy, *ncol = NULL;
484                 if (wm_focus) {
485                         /* Currently focused on a window */
486                         if (tiling != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
487                                 return;
488                         ncol = cols > 0 ? col->next : col->prev;
489                 } else {
490                         /* Currently focused on an empty display */
491                         dpy = list_find(wm_tag->dpys, wm_dpy);
492                 }
493                 if (ncol == NULL) {
494                         /* Moving focus to a different display */
495                         ndpy = get_next(dpy, cols > 0);
496                         ncol = cols > 0 ? DPY(ndpy)->cols :
497                                 list_last(DPY(ndpy)->cols);
498                         wm_dpy = ndpy->data;
499                 }
500                 if (ncol && COL(ncol) && COL(ncol)->row)
501                         set_focus(COL(ncol)->row->win);
502                 else
503                         sys_focus(wm->root);
504         }
505 }
506
507 /* Raise the window in the floating */
508 static void raise_float(win_t *win)
509 {
510         printf("raise_float: %p\n", win);
511         list_t *cur;
512         for (cur = wm_dpy->flts; cur; cur = cur->next)
513                 if (FLT(cur)->win == win)
514                         break;
515         if (cur) {
516                 flt_t *flt = cur->data;
517                 wm_dpy->flts = list_remove(wm_dpy->flts, cur, 0);
518                 wm_dpy->flts = list_append(wm_dpy->flts, flt);
519                 sys_raise(win);
520         }
521 }
522
523 /* Toggle between floating and tiling layers */
524 static void switch_layer(void)
525 {
526         printf("switch_float: %p %d\n",
527                         wm_dpy, wm_dpy->layer);
528         wm_dpy->layer = !wm_dpy->layer;
529         wm_update();
530 }
531
532 /* Move current window between floating and tiling layers */
533 static void set_layer(win_t *win)
534 {
535         if (!win) return;
536         printf("set_float: %p %p\n", wm_dpy, win);
537         wm_dpy->layer = !cut_win(win, wm_tag);
538         put_win(win, wm_tag, wm_dpy->layer);
539         wm_update();
540 }
541
542 /* Allocate a new tag */
543 static tag_t *tag_new(list_t *screens, int name)
544 {
545         tag_t *tag = new0(tag_t);
546         tag->name  = name;
547         for (list_t *cur = screens; cur; cur = cur->next) {
548                 dpy_t *dpy  = new0(dpy_t);
549                 dpy->geom = cur->data;
550                 tag->dpys = list_append(tag->dpys, dpy);
551         }
552         tag->dpy  = tag->dpys->data;
553         return tag;
554 }
555
556 /* Search for a tag
557  *   If it does not exist it is based on the
558  *   display geometry in wm->screens */
559 static tag_t *tag_find(int name)
560 {
561         tag_t *tag = NULL;
562         for (list_t *cur = wm->tags; cur; cur = cur->next)
563                 if (name == TAG(cur)->name) {
564                         tag = cur->data;
565                         break;
566                 }
567         if (!tag) {
568                 tag = tag_new(wm->screens, name);
569                 wm->tags = list_append(wm->tags, tag);
570         }
571         return tag;
572 }
573
574 /* Move the window from the current tag to the new tag
575  *   Unlike wmii, only remove the current tag, not all tags */
576 static void tag_set(win_t *win, int name)
577 {
578         printf("tag_set: %p %d\n", win, name);
579         if (wm_tag->name == name)
580                 return;
581         tag_t *tag = tag_find(name);
582         layer_t layer = cut_win(win, wm_tag);
583         put_win(win, tag, layer);
584         set_focus(wm_focus);
585 }
586
587 /* Switch to a different tag */
588 static void tag_switch(int name)
589 {
590         printf("tag_switch: %d\n", name);
591         tag_t *old = wm_tag;
592         if ((wm_col == NULL || wm_row == NULL) && wm_flt == NULL) {
593                 list_t *ltag = list_find(wm->tags, old);
594                 wm->tags = list_remove(wm->tags, ltag, 1);
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                 free(old);
602         }
603         wm_tag = tag_find(name);
604 }
605
606 /* Tile all windows in the given display
607  *   This performs all the actual window tiling
608  *   Currently supports  split, stack and maximized modes */
609 static void wm_update_cols(dpy_t *dpy)
610 {
611         int  x=0,  y=0; // Current window top-left position
612         int tx=0, ty=0; // Total size (sum of initial col widths and row heights w/o margin)
613         int mx=0, my=0; // Maximum usable size (screen size minus margins)
614         int       sy=0; // Stack size (height of focused stack window)
615
616         /* Scale horizontally */
617         x  = dpy->geom->x;
618         mx = dpy->geom->w - (list_length(dpy->cols)+1)*MARGIN;
619         for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
620                 tx += COL(lcol)->width;
621         for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
622                 COL(lcol)->width *= (float)mx / tx;
623
624         /* Scale each column vertically */
625         win_t *focus = get_focus();
626         for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
627                 col_t *col = lcol->data;
628                 int nrows = list_length(col->rows);
629                 ty = 0;
630                 for (list_t *lrow = col->rows; lrow; lrow = lrow->next)
631                         ty += ROW(lrow)->height;
632                 y  = dpy->geom->y;
633                 my = dpy->geom->h - (MARGIN + (nrows-1)* MARGIN    + MARGIN);
634                 sy = dpy->geom->h - (MARGIN + (nrows-1)*(MARGIN/2) + MARGIN)
635                                   -           (nrows-1)* STACK;
636                 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
637                         win_t *win = ROW(lrow)->win;
638                         win->h = ROW(lrow)->height;
639                         int height = 0;
640                         switch (col->mode) {
641                         case split:
642                                 sys_move(win, x+MARGIN, y+MARGIN,
643                                         col->width, win->h * ((float)my / ty));
644                                 height = win->h;
645                                 y += height + MARGIN;
646                                 break;
647                         case stack:
648                                 if (lrow->next && ROW(lrow->next)->win == col->row->win) {
649                                         /* Hack to prevent flashing */
650                                         win_t *next = ROW(lrow->next)->win;
651                                         sys_move(next, x+MARGIN, y+MARGIN+STACK+MARGIN/2,
652                                                 col->width, sy);
653                                 }
654                                 height = win == col->row->win ? sy : STACK;
655                                 sys_move(win, x+MARGIN, y+MARGIN,
656                                         col->width, height);
657                                 y += height + (MARGIN/2);
658                                 break;
659                         case max:
660                         case tab:
661                                 sys_move(win, x+MARGIN, 0+MARGIN,
662                                         col->width, dpy->geom->h-2*MARGIN);
663                                 if (focus == win)
664                                         sys_raise(win);
665                                 break;
666                         }
667                         ROW(lrow)->height = win->h;
668                 }
669                 x += col->width + MARGIN;
670         }
671 }
672
673 /*******************************
674  * Window management functions *
675  *******************************/
676 void wm_update(void)
677 {
678         /* Show/hide tags */
679         tag_foreach_col(wm_tag, dpy, col, row, win)
680                 sys_show(win, st_show);
681         tag_foreach_flt(wm_tag, dpy, flt, win)
682                 sys_show(win, st_show);
683         for (list_t *tag = wm ->tags; tag; tag = tag->next)
684                 if (tag->data != wm_tag) {
685                         tag_foreach_col(TAG(tag), dpy, col, row, win)
686                                         sys_show(win, st_hide);
687                         tag_foreach_flt(TAG(tag), dpy, flt, win)
688                                         sys_show(win, st_hide);
689                 }
690
691         /* Refresh the display */
692         for (list_t *ldpy = wm_tag->dpys; ldpy; ldpy = ldpy->next)
693                 wm_update_cols(ldpy->data);
694         tag_foreach_flt(wm_tag, ldpy, lflt, win) {
695                 flt_t *flt = lflt->data;
696                 sys_move(win, flt->x, flt->y, flt->w, flt->h);
697                 sys_raise(flt->win);
698         }
699         if (wm_focus)
700                 set_focus(wm_focus);
701 }
702
703 int wm_handle_key(win_t *win, Key_t key, mod_t mod, ptr_t ptr)
704 {
705         if (!win || win == wm_dpy->geom) return 0;
706         //printf("wm_handle_key: %p - %x %c%c%c%c%c\n", win, key,
707         //      mod.up    ? '^' : 'v',
708         //      mod.alt   ? 'a' : '-',
709         //      mod.ctrl  ? 'c' : '-',
710         //      mod.shift ? 's' : '-',
711         //      mod.win   ? 'w' : '-');
712
713         /* Mouse movement */
714         if (key == key_mouse1)
715                 raise_float(win);
716         if (key_mouse0 <= key && key <= key_mouse7) {
717                 if (key == key_mouse1 && mod.MODKEY && !mod.up)
718                         return set_move(win,ptr,move),   1;
719                 if (key == key_mouse3 && mod.MODKEY && !mod.up)
720                         return set_move(win,ptr,resize), 1;
721                 if (move_mode != none && mod.up)
722                         return set_move(win,ptr,none),   1;
723                 if (key == key_mouse1 && !mod.up && win->h == STACK)
724                         return wm_update(),              0;
725                 return 0;
726         }
727
728         /* Only handle key-down */
729         if (mod.up)
730                 return 0;
731
732         /* Misc */
733         if (mod.MODKEY) {
734 #ifdef DEBUG
735                 if (key == key_f1) return raise_float(win), 1;
736                 if (key == key_f2) return set_focus(win), 1;
737                 if (key == key_f3) return sys_show(win, st_show), 1;
738                 if (key == key_f4) return sys_show(win, st_hide), 1;
739 #endif
740                 if (key == key_f5) return wm_update(),    1;
741                 if (key == key_f6) return print_txt(),    1;
742                 if (key == 'q')    return sys_exit(),     1;
743         }
744
745         /* Floating layer */
746         if (key == ' ') {
747                 if (mod.MODKEY && mod.shift)
748                         return set_layer(win), 1;
749                 if (mod.MODKEY)
750                         return switch_layer(), 1;
751         }
752
753         /* Movement commands */
754         if (mod.MODKEY && mod.shift) {
755                 switch (key) {
756                 case 'h': return shift_window(wm_focus,-1, 0), 1;
757                 case 'j': return shift_window(wm_focus, 0,+1), 1;
758                 case 'k': return shift_window(wm_focus, 0,-1), 1;
759                 case 'l': return shift_window(wm_focus,+1, 0), 1;
760                 default: break;
761                 }
762         }
763         else if (mod.MODKEY) {
764                 switch (key) {
765                 case 'h': return shift_focus(-1, 0), 1;
766                 case 'j': return shift_focus( 0,+1), 1;
767                 case 'k': return shift_focus( 0,-1), 1;
768                 case 'l': return shift_focus(+1, 0), 1;
769                 default: break;
770                 }
771         }
772
773         /* Column mode commands */
774         if (mod.MODKEY) {
775                 switch (key) {
776                 case 'd': return set_mode(win, split), 1;
777                 case 's': return set_mode(win, stack), 1;
778                 case 'm': return set_mode(win, max),   1;
779                 case 't': return set_mode(win, tab),   1;
780                 default: break;
781                 }
782         }
783
784         /* Tag switching */
785         if (mod.MODKEY && '0' <= key && key <= '9') {
786                 int name = key - '0';
787                 if (mod.shift)
788                         tag_set(win, name);
789                 else
790                         tag_switch(name);
791                 wm_update();
792         }
793
794         /* Focus change */
795         if (key == key_enter)
796                 return set_focus(win), 1;
797
798         if (key_mouse0 <= key && key <= key_mouse7)
799                 return set_focus(win), 0;
800
801         /* Reset focus after after focus change,
802          * not sure what is causing the focus change in the first place
803          * but preventing that would be a better solution */
804         if (key == key_focus)
805                 sys_focus(wm_focus ?: wm->root);
806
807         return 0;
808 }
809
810 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
811 {
812         //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
813         //              cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
814
815         if (move_mode == none)
816                 return 0;
817
818         int dx = ptr.rx - move_prev.rx;
819         int dy = ptr.ry - move_prev.ry;
820         move_prev = ptr;
821
822         if (move_layer == tiling && move_mode == resize) {
823                 list_t *vert = move_dir.v < 0 ? move_lrow->prev : move_lrow->next;
824                 list_t *horz = move_dir.h < 0 ? move_lcol->prev : move_lcol->next;
825                 if (vert) {
826                         ROW(move_lrow)->height += move_dir.v * dy;
827                         ROW(vert)->height      -= move_dir.v * dy;
828                 }
829                 if (horz) {
830                         COL(move_lcol)->width  += move_dir.h * dx;
831                         COL(horz)->width       -= move_dir.h * dx;
832                 }
833                 wm_update();
834         }
835
836         if (move_layer == floating) {
837                 flt_t *flt = move_lflt->data;
838                 win_t *win = flt->win;
839                 if (move_mode == move)
840                         sys_move(win, win->x+dx, win->y+dy, win->w, win->h);
841                 else if (move_mode == resize)
842                         sys_move(win,
843                                 win->x + dx * (move_dir.h < 0),
844                                 win->y + dy * (move_dir.v < 0),
845                                 win->w + dx *  move_dir.h,
846                                 win->h + dy *  move_dir.v);
847                 flt->x = win->x; flt->y = win->y;
848                 flt->w = win->w; flt->h = win->h;
849         }
850
851         return 0;
852 }
853
854 void wm_insert(win_t *win)
855 {
856         printf("wm_insert: %p\n", win);
857         print_txt();
858
859         /* Initialize window */
860         win->wm = new0(win_wm_t);
861         sys_watch(win, key_enter,  MOD());
862         sys_watch(win, key_focus,  MOD());
863
864         /* Add to screen */
865         put_win(win, wm_tag, wm_dpy->layer);
866
867         /* Arrange */
868         wm_update();
869         set_focus(wm_focus);
870         print_txt();
871 }
872
873 void wm_remove(win_t *win)
874 {
875         printf("wm_remove: %p\n", win);
876         print_txt();
877         for (list_t *tag = wm->tags; tag; tag = tag->next)
878                 cut_win(win, tag->data);
879         free(win->wm);
880         set_focus(wm_focus);
881         wm_update();
882         print_txt();
883 }
884
885 void wm_init(win_t *root)
886 {
887         printf("wm_init: %p\n", root);
888
889         /* Hack, fix screen order */
890         list_t *screens = sys_info(root);
891         list_t *left  = screens;
892         list_t *right = screens->next;
893         if (left && right && WIN(left)->x > WIN(right)->x) {
894                 void *tmp   = left->data;
895                 left->data  = right->data;
896                 right->data = tmp;
897         }
898
899         wm          = new0(wm_t);
900         wm->root    = root;
901         wm->screens = screens;
902         wm->tag     = tag_new(wm->screens, 1);
903         wm->tags    = list_insert(NULL, wm->tag);
904
905         Key_t keys_e[] = {key_enter, key_focus};
906         Key_t keys_s[] = {'h', 'j', 'k', 'l', 'q', ' ',
907                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
908         Key_t keys_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't', ' ',
909                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
910                 key_f1, key_f2, key_f3, key_f4, key_f5, key_f6,
911                 key_mouse1, key_mouse3};
912         for (int i = 0; i < countof(keys_e); i++)
913                 sys_watch(root, keys_e[i],  MOD());
914         for (int i = 0; i < countof(keys_m); i++)
915                 sys_watch(root, keys_m[i], MOD(.MODKEY=1));
916         for (int i = 0; i < countof(keys_s); i++)
917                 sys_watch(root, keys_s[i], MOD(.MODKEY=1,.shift=1));
918 }