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