]> Pileus Git - wmpus/blob - wm-wmii.c
Misc 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 "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         dpy_t  *dpy   = DPY(ldpy);
303         if (layer == tiling) {
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);
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);
312                 }
313         }
314
315         if (layer == floating) {
316                 dpy->flts = list_remove(dpy->flts, lflt);
317                 dpy->flt  = dpy->flts ? list_last(dpy->flts)->data : NULL;
318                 if (!dpy->flt && dpy->col && dpy->col->row)
319                         dpy->layer = tiling;
320         }
321
322         return layer;
323 }
324
325 /* Insert a window into the tiling layer
326  *   The window is added immediately after the
327  *   columns currently focused row */
328 static void put_win_col(win_t *win, tag_t *tag, dpy_t *dpy, col_t *col)
329 {
330         row_t *row = new0(row_t);
331         row->win = win;
332
333         if (col == NULL) {
334                 col = new0(col_t);
335                 dpy->cols = list_insert(dpy->cols, col);
336         }
337
338         int nrows = list_length(col->rows);
339         if (col->row) {
340                 list_t *prev = list_find(col->rows, col->row);
341                 list_insert_after(prev, row);
342         } else {
343                 col->rows = list_insert(col->rows, row);
344         }
345         tag->dpy           = dpy;
346         tag->dpy->col      = col;
347         tag->dpy->col->row = row;
348         tag->dpy->layer    = tiling;
349
350         row->height = dpy->geom->h / MAX(nrows,1);
351         if (nrows == 0) {
352                 int ncols = list_length(dpy->cols);
353                 col->width = dpy->geom->w / MAX(ncols-1,1);
354         }
355 }
356
357 /* Insert a window into the floating layer */
358 static void put_win_flt(win_t *win, tag_t *tag, dpy_t *dpy)
359 {
360         flt_t *flt = new0(flt_t);
361         flt->win = win;
362         flt->w   = dpy->geom->w / 2;
363         flt->h   = dpy->geom->h / 2;
364         flt->x   = dpy->geom->x + flt->w / 2;
365         flt->y   = dpy->geom->y + flt->h / 2;
366         if (dpy->flt) {
367                 flt->x = dpy->flt->x + 20;
368                 flt->y = dpy->flt->y + 20;
369         }
370         dpy->flts = list_append(dpy->flts, flt);
371         tag->dpy        = dpy;
372         tag->dpy->flt   = flt;
373         tag->dpy->layer = floating;
374 }
375
376 /* Insert a window into a tag */
377 static void put_win(win_t *win, tag_t *tag, layer_t layer)
378 {
379         if (layer == tiling)
380                 put_win_col(win, tag, tag->dpy, tag->dpy->col);
381         if (layer == floating)
382                 put_win_flt(win, tag, tag->dpy);
383 }
384
385 /* Move a window up, down, left, or right
386  *   This handles moving with a column, between
387  *   columns, and between multiple monitors. */
388 static void shift_window(win_t *win, int col, int row)
389 {
390         if (!win) return;
391         printf("shift_window: %p - %+d,%+d\n", win, col, row);
392         print_txt();
393         printf("shift_window: >>>\n");
394         list_t *ldpy, *lcol, *lrow, *lflt;
395         if (tiling != searchl(wm_tag, win, &ldpy, &lcol, &lrow, &lflt))
396                 return;
397         dpy_t *dpy = ldpy->data;
398         if (row != 0) {
399                 /* Move with a column, just swap rows */
400                 list_t *src = lrow, *dst = NULL;
401                 if (row < 0) dst = src->prev;
402                 if (row > 0) dst = src->next;
403                 if (src && dst) {
404                         printf("swap: %p <-> %p\n", src->data, dst->data);
405                         row_t *tmp = src->data;
406                         src->data = dst->data;
407                         dst->data = tmp;
408                         goto update;
409                 }
410         } else {
411                 /* Moving between columns */
412                 int onlyrow = !lrow->prev && !lrow->next;
413                 list_t *src = lcol, *dst = NULL;
414                 if (col < 0) {
415                         if (src->prev) {
416                                 /* Normal move between columns */
417                                 dst = src->prev;
418                         } else if (!onlyrow) {
419                                 /* Create new column */
420                                 dpy->cols = list_insert(dpy->cols, new0(col_t));
421                                 dst = src->prev;
422                         } else if (ldpy->prev) {
423                                 /* Move to next monitor */
424                                 dpy = ldpy->prev->data;
425                                 dst = list_last(dpy->cols);
426                         } else {
427                                 /* We, shall, not, be,
428                                  * we shall not be moved */
429                                 return;
430                         }
431                 }
432                 if (col > 0) {
433                         if (src->next) {
434                                 dst = src->next;
435                         } else if (!onlyrow) {
436                                 dpy->cols = list_append(dpy->cols, new0(col_t));
437                                 dst = src->next;
438                         } else if (ldpy->next) {
439                                 dpy = ldpy->next->data;
440                                 dst = dpy->cols;
441                         } else {
442                                 return;
443                         }
444                 }
445                 cut_win(win, wm_tag);
446                 put_win_col(win, wm_tag, dpy, dst ? dst->data : NULL);
447                 goto update;
448         }
449 update:
450         print_txt();
451         wm_update();
452 }
453
454 /* Get next/prev item, with wraparound */
455 static list_t *get_next(list_t *list, int forward)
456 {
457         list_t *next = forward ? list->next : list->prev;
458         if (next == NULL) {
459                 next = list;
460                 while ((list = forward ? next->prev : next->next))
461                         next = list;
462         }
463         return next;
464 }
465
466 /* Move keyboard focus in a given direction */
467 static void shift_focus(int cols, int rows)
468 {
469         printf("shift_focus: %+d,%+d\n", cols, rows);
470         if (rows != 0 && wm_focus) {
471                 /* Move focus up/down */
472                 list_t *dpy, *col, *row;
473                 if (tiling != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
474                         return;
475                 row_t *next = get_next(row, rows > 0)->data;
476                 set_focus(next->win);
477                 if (COL(col)->mode != split)
478                         wm_update();
479         }
480         if (cols != 0) {
481                 /* Move focus left/right */
482                 list_t *dpy, *col, *row, *ndpy, *ncol = NULL;
483                 if (wm_focus) {
484                         /* Currently focused on a window */
485                         if (tiling != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
486                                 return;
487                         ncol = cols > 0 ? col->next : col->prev;
488                 } else {
489                         /* Currently focused on an empty display */
490                         dpy = list_find(wm_tag->dpys, wm_dpy);
491                 }
492                 if (ncol == NULL) {
493                         /* Moving focus to a different display */
494                         ndpy = get_next(dpy, cols > 0);
495                         ncol = cols > 0 ? DPY(ndpy)->cols :
496                                 list_last(DPY(ndpy)->cols);
497                         wm_dpy = ndpy->data;
498                 }
499                 if (ncol && COL(ncol) && COL(ncol)->row)
500                         set_focus(COL(ncol)->row->win);
501                 else
502                         sys_focus(wm->root);
503         }
504 }
505
506 /* Raise the window in the floating */
507 static void raise_float(win_t *win)
508 {
509         printf("raise_float: %p\n", win);
510         list_t *cur;
511         for (cur = wm_dpy->flts; cur; cur = cur->next)
512                 if (FLT(cur)->win == win)
513                         break;
514         if (cur) {
515                 flt_t *flt = cur->data;
516                 wm_dpy->flts = list_remove(wm_dpy->flts, cur);
517                 wm_dpy->flts = list_append(wm_dpy->flts, flt);
518                 sys_raise(win);
519         }
520 }
521
522 /* Toggle between floating and tiling layers */
523 static void switch_layer(void)
524 {
525         printf("switch_float: %p %d\n",
526                         wm_dpy, wm_dpy->layer);
527         wm_dpy->layer = !wm_dpy->layer;
528         wm_update();
529 }
530
531 /* Move current window between floating and tiling layers */
532 static void set_layer(win_t *win)
533 {
534         if (!win) return;
535         printf("set_float: %p %p\n", wm_dpy, win);
536         wm_dpy->layer = !cut_win(win, wm_tag);
537         put_win(win, wm_tag, wm_dpy->layer);
538         wm_update();
539 }
540
541 /* Allocate a new tag */
542 static tag_t *tag_new(list_t *screens, int name)
543 {
544         tag_t *tag = new0(tag_t);
545         tag->name  = name;
546         for (list_t *cur = screens; cur; cur = cur->next) {
547                 dpy_t *dpy  = new0(dpy_t);
548                 dpy->geom = cur->data;
549                 tag->dpys = list_append(tag->dpys, dpy);
550         }
551         tag->dpy  = tag->dpys->data;
552         return tag;
553 }
554
555 /* Search for a tag
556  *   If it does not exist it is based on the
557  *   display geometry in wm->screens */
558 static tag_t *tag_find(int name)
559 {
560         tag_t *tag = NULL;
561         for (list_t *cur = wm->tags; cur; cur = cur->next)
562                 if (name == TAG(cur)->name) {
563                         tag = cur->data;
564                         break;
565                 }
566         if (!tag) {
567                 tag = tag_new(wm->screens, name);
568                 wm->tags = list_append(wm->tags, tag);
569         }
570         return tag;
571 }
572
573 /* Move the window from the current tag to the new tag
574  *   Unlike wmii, only remove the current tag, not all tags */
575 static void tag_set(win_t *win, int name)
576 {
577         printf("tag_set: %p %d\n", win, name);
578         if (wm_tag->name == name)
579                 return;
580         tag_t *tag = tag_find(name);
581         layer_t layer = cut_win(win, wm_tag);
582         put_win(win, tag, layer);
583         set_focus(wm_focus);
584 }
585
586 /* Switch to a different tag */
587 static void tag_switch(int name)
588 {
589         printf("tag_switch: %d\n", name);
590         if ((wm_col == NULL || wm_row == NULL) && wm_flt == NULL)
591                 wm->tags = list_remove(wm->tags,
592                                 list_find(wm->tags, wm_tag));
593         wm_tag = tag_find(name);
594 }
595
596 /* Tile all windows in the given display
597  *   This performs all the actual window tiling
598  *   Currently supports  split, stack and maximized modes */
599 static void wm_update_cols(dpy_t *dpy)
600 {
601         int  x=0,  y=0; // Current window top-left position
602         int tx=0, ty=0; // Total size (sum of initial col widths and row heights w/o margin)
603         int mx=0, my=0; // Maximum usable size (screen size minus margins)
604         int       sy=0; // Stack size (height of focused stack window)
605
606         /* Scale horizontally */
607         x  = dpy->geom->x;
608         mx = dpy->geom->w - (list_length(dpy->cols)+1)*MARGIN;
609         for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
610                 tx += COL(lcol)->width;
611         for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
612                 COL(lcol)->width *= (float)mx / tx;
613
614         /* Scale each column vertically */
615         win_t *focus = get_focus();
616         for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
617                 col_t *col = lcol->data;
618                 int nrows = list_length(col->rows);
619                 ty = 0;
620                 for (list_t *lrow = col->rows; lrow; lrow = lrow->next)
621                         ty += ROW(lrow)->height;
622                 y  = dpy->geom->y;
623                 my = dpy->geom->h - (MARGIN + (nrows-1)* MARGIN    + MARGIN);
624                 sy = dpy->geom->h - (MARGIN + (nrows-1)*(MARGIN/2) + MARGIN)
625                                   -           (nrows-1)* STACK;
626                 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
627                         win_t *win = ROW(lrow)->win;
628                         win->h = ROW(lrow)->height;
629                         int height = 0;
630                         switch (col->mode) {
631                         case split:
632                                 sys_move(win, x+MARGIN, y+MARGIN,
633                                         col->width, win->h * ((float)my / ty));
634                                 height = win->h;
635                                 y += height + MARGIN;
636                                 break;
637                         case stack:
638                                 if (lrow->next && ROW(lrow->next)->win == col->row->win) {
639                                         /* Hack to prevent flashing */
640                                         win_t *next = ROW(lrow->next)->win;
641                                         sys_move(next, x+MARGIN, y+MARGIN+STACK+MARGIN/2,
642                                                 col->width, sy);
643                                 }
644                                 height = win == col->row->win ? sy : STACK;
645                                 sys_move(win, x+MARGIN, y+MARGIN,
646                                         col->width, height);
647                                 y += height + (MARGIN/2);
648                                 break;
649                         case max:
650                         case tab:
651                                 sys_move(win, x+MARGIN, 0+MARGIN,
652                                         col->width, dpy->geom->h-2*MARGIN);
653                                 if (focus == win)
654                                         sys_raise(win);
655                                 break;
656                         }
657                         ROW(lrow)->height = win->h;
658                 }
659                 x += col->width + MARGIN;
660         }
661 }
662
663 /*******************************
664  * Window management functions *
665  *******************************/
666 void wm_update(void)
667 {
668         /* Show/hide tags */
669         tag_foreach_col(wm_tag, dpy, col, row, win)
670                 sys_show(win, st_show);
671         tag_foreach_flt(wm_tag, dpy, flt, win)
672                 sys_show(win, st_show);
673         for (list_t *tag = wm ->tags; tag; tag = tag->next)
674                 if (tag->data != wm_tag) {
675                         tag_foreach_col(TAG(tag), dpy, col, row, win)
676                                         sys_show(win, st_hide);
677                         tag_foreach_flt(TAG(tag), dpy, flt, win)
678                                         sys_show(win, st_hide);
679                 }
680
681         /* Refresh the display */
682         for (list_t *ldpy = wm_tag->dpys; ldpy; ldpy = ldpy->next)
683                 wm_update_cols(ldpy->data);
684         tag_foreach_flt(wm_tag, ldpy, lflt, win) {
685                 flt_t *flt = lflt->data;
686                 sys_move(win, flt->x, flt->y, flt->w, flt->h);
687                 sys_raise(flt->win);
688         }
689         if (wm_focus)
690                 set_focus(wm_focus);
691 }
692
693 int wm_handle_key(win_t *win, Key_t key, mod_t mod, ptr_t ptr)
694 {
695         if (!win || win == wm_dpy->geom) return 0;
696         //printf("wm_handle_key: %p - %x %c%c%c%c%c\n", win, key,
697         //      mod.up    ? '^' : 'v',
698         //      mod.alt   ? 'a' : '-',
699         //      mod.ctrl  ? 'c' : '-',
700         //      mod.shift ? 's' : '-',
701         //      mod.win   ? 'w' : '-');
702
703         /* Mouse movement */
704         if (key == key_mouse1)
705                 raise_float(win);
706         if (key_mouse0 <= key && key <= key_mouse7) {
707                 if (key == key_mouse1 && mod.MODKEY && !mod.up)
708                         return set_move(win,ptr,move),   1;
709                 if (key == key_mouse3 && mod.MODKEY && !mod.up)
710                         return set_move(win,ptr,resize), 1;
711                 if (move_mode != none && mod.up)
712                         return set_move(win,ptr,none),   1;
713                 if (key == key_mouse1 && !mod.up && win->h == STACK)
714                         return wm_update(),              0;
715                 return 0;
716         }
717
718         /* Only handle key-down */
719         if (mod.up)
720                 return 0;
721
722         /* Misc */
723         if (mod.MODKEY) {
724 #ifdef DEBUG
725                 if (key == key_f1) return raise_float(win), 1;
726                 if (key == key_f2) return set_focus(win), 1;
727                 if (key == key_f3) return sys_show(win, st_show), 1;
728                 if (key == key_f4) return sys_show(win, st_hide), 1;
729 #endif
730                 if (key == key_f5) return wm_update(),    1;
731                 if (key == key_f6) return print_txt(),    1;
732         }
733
734         /* Floating layer */
735         if (key == ' ') {
736                 if (mod.MODKEY && mod.shift)
737                         return set_layer(win), 1;
738                 if (mod.MODKEY)
739                         return switch_layer(), 1;
740         }
741
742         /* Movement commands */
743         if (mod.MODKEY && mod.shift) {
744                 switch (key) {
745                 case 'h': return shift_window(wm_focus,-1, 0), 1;
746                 case 'j': return shift_window(wm_focus, 0,+1), 1;
747                 case 'k': return shift_window(wm_focus, 0,-1), 1;
748                 case 'l': return shift_window(wm_focus,+1, 0), 1;
749                 default: break;
750                 }
751         }
752         else if (mod.MODKEY) {
753                 switch (key) {
754                 case 'h': return shift_focus(-1, 0), 1;
755                 case 'j': return shift_focus( 0,+1), 1;
756                 case 'k': return shift_focus( 0,-1), 1;
757                 case 'l': return shift_focus(+1, 0), 1;
758                 default: break;
759                 }
760         }
761
762         /* Column mode commands */
763         if (mod.MODKEY) {
764                 switch (key) {
765                 case 'd': return set_mode(win, split), 1;
766                 case 's': return set_mode(win, stack), 1;
767                 case 'm': return set_mode(win, max),   1;
768                 case 't': return set_mode(win, tab),   1;
769                 default: break;
770                 }
771         }
772
773         /* Tag switching */
774         if (mod.MODKEY && '0' <= key && key <= '9') {
775                 int name = key - '0';
776                 if (mod.shift)
777                         tag_set(win, name);
778                 else
779                         tag_switch(name);
780                 wm_update();
781         }
782
783         /* Focus change */
784         if (key == key_enter)
785                 return set_focus(win), 1;
786
787         if (key_mouse0 <= key && key <= key_mouse7)
788                 return set_focus(win), 0;
789
790         /* Reset focus after after focus change,
791          * not sure what is causing the focus change in the first place
792          * but preventing that would be a better solution */
793         if (key == key_focus)
794                 sys_focus(wm_focus ?: wm->root);
795
796         return 0;
797 }
798
799 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
800 {
801         //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
802         //              cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
803
804         if (move_mode == none)
805                 return 0;
806
807         int dx = ptr.rx - move_prev.rx;
808         int dy = ptr.ry - move_prev.ry;
809         move_prev = ptr;
810
811         if (move_layer == tiling && move_mode == resize) {
812                 list_t *vert = move_dir.v < 0 ? move_lrow->prev : move_lrow->next;
813                 list_t *horz = move_dir.h < 0 ? move_lcol->prev : move_lcol->next;
814                 if (vert) {
815                         ROW(move_lrow)->height += move_dir.v * dy;
816                         ROW(vert)->height      -= move_dir.v * dy;
817                 }
818                 if (horz) {
819                         COL(move_lcol)->width  += move_dir.h * dx;
820                         COL(horz)->width       -= move_dir.h * dx;
821                 }
822                 wm_update();
823         }
824
825         if (move_layer == floating) {
826                 flt_t *flt = move_lflt->data;
827                 win_t *win = flt->win;
828                 if (move_mode == move)
829                         sys_move(win, win->x+dx, win->y+dy, win->w, win->h);
830                 else if (move_mode == resize)
831                         sys_move(win,
832                                 win->x + dx * (move_dir.h < 0),
833                                 win->y + dy * (move_dir.v < 0),
834                                 win->w + dx *  move_dir.h,
835                                 win->h + dy *  move_dir.v);
836                 flt->x = win->x; flt->y = win->y;
837                 flt->w = win->w; flt->h = win->h;
838         }
839
840         return 0;
841 }
842
843 void wm_insert(win_t *win)
844 {
845         printf("wm_insert: %p\n", win);
846         print_txt();
847
848         /* Initialize window */
849         win->wm = new0(win_wm_t);
850         sys_watch(win, key_enter,  MOD());
851         sys_watch(win, key_focus,  MOD());
852
853         /* Add to screen */
854         put_win(win, wm_tag, wm_dpy->layer);
855
856         /* Arrange */
857         wm_update();
858         set_focus(wm_focus);
859         print_txt();
860 }
861
862 void wm_remove(win_t *win)
863 {
864         printf("wm_remove: %p\n", win);
865         print_txt();
866         for (list_t *tag = wm->tags; tag; tag = tag->next)
867                 cut_win(win, tag->data);
868         set_focus(wm_focus);
869         wm_update();
870         print_txt();
871 }
872 void wm_init(win_t *root)
873 {
874         printf("wm_init: %p\n", root);
875
876         /* Hack, fix screen order */
877         list_t *screens = sys_info(root);
878         list_t *left  = screens;
879         list_t *right = screens->next;
880         if (left && right && WIN(left)->x > WIN(right)->x) {
881                 void *tmp   = left->data;
882                 left->data  = right->data;
883                 right->data = tmp;
884         }
885
886         wm          = new0(wm_t);
887         wm->root    = root;
888         wm->screens = screens;
889         wm->tag     = tag_new(wm->screens, 1);
890         wm->tags    = list_insert(NULL, wm->tag);
891
892         Key_t keys_e[] = {key_enter, key_focus};
893         Key_t keys_s[] = {'h', 'j', 'k', 'l', ' ',
894                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
895         Key_t keys_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't', ' ',
896                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
897                 key_f1, key_f2, key_f3, key_f4, key_f5, key_f6,
898                 key_mouse1, key_mouse3};
899         for (int i = 0; i < countof(keys_e); i++)
900                 sys_watch(root, keys_e[i],  MOD());
901         for (int i = 0; i < countof(keys_m); i++)
902                 sys_watch(root, keys_m[i], MOD(.MODKEY=1));
903         for (int i = 0; i < countof(keys_s); i++)
904                 sys_watch(root, keys_s[i], MOD(.MODKEY=1,.shift=1));
905 }