]> Pileus Git - wmpus/blob - wm-wmii.c
Use config for sys-x11 and wm-wmii
[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                 list_t *ltag = list_find(wm->tags, old);
592                 wm->tags = list_remove(wm->tags, ltag, 1);
593                 while (old->dpys) {
594                         dpy_t *dpy = old->dpys->data;
595                         while (dpy->cols)
596                                 dpy->cols = list_remove(dpy->cols, dpy->cols, 1);
597                         old->dpys = list_remove(old->dpys, old->dpys, 1);
598                 }
599                 free(old);
600         }
601         wm_tag = tag_find(name);
602 }
603
604 /* Tile all windows in the given display
605  *   This performs all the actual window tiling
606  *   Currently supports  split, stack and maximized modes */
607 static void wm_update_cols(dpy_t *dpy)
608 {
609         int  x=0,  y=0; // Current window top-left position
610         int tx=0, ty=0; // Total size (sum of initial col widths and row heights w/o margin)
611         int mx=0, my=0; // Maximum usable size (screen size minus margins)
612         int       sy=0; // Stack size (height of focused stack window)
613
614         /* Scale horizontally */
615         x  = dpy->geom->x;
616         mx = dpy->geom->w - (list_length(dpy->cols)+1)*MARGIN;
617         for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
618                 tx += COL(lcol)->width;
619         for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
620                 COL(lcol)->width *= (float)mx / tx;
621
622         /* Scale each column vertically */
623         win_t *focus = get_focus();
624         for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
625                 col_t *col = lcol->data;
626                 int nrows = list_length(col->rows);
627                 ty = 0;
628                 for (list_t *lrow = col->rows; lrow; lrow = lrow->next)
629                         ty += ROW(lrow)->height;
630                 y  = dpy->geom->y;
631                 my = dpy->geom->h - (MARGIN + (nrows-1)* MARGIN    + MARGIN);
632                 sy = dpy->geom->h - (MARGIN + (nrows-1)*(MARGIN/2) + MARGIN)
633                                   -           (nrows-1)* STACK;
634                 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
635                         win_t *win = ROW(lrow)->win;
636                         win->h = ROW(lrow)->height;
637                         int height = 0;
638                         switch (col->mode) {
639                         case split:
640                                 sys_move(win, x+MARGIN, y+MARGIN,
641                                         col->width, win->h * ((float)my / ty));
642                                 height = win->h;
643                                 y += height + MARGIN;
644                                 break;
645                         case stack:
646                                 if (lrow->next && ROW(lrow->next)->win == col->row->win) {
647                                         /* Hack to prevent flashing */
648                                         win_t *next = ROW(lrow->next)->win;
649                                         sys_move(next, x+MARGIN, y+MARGIN+STACK+MARGIN/2,
650                                                 col->width, sy);
651                                 }
652                                 height = win == col->row->win ? sy : STACK;
653                                 sys_move(win, x+MARGIN, y+MARGIN,
654                                         col->width, height);
655                                 y += height + (MARGIN/2);
656                                 break;
657                         case max:
658                         case tab:
659                                 sys_move(win, x+MARGIN, 0+MARGIN,
660                                         col->width, dpy->geom->h-2*MARGIN);
661                                 if (focus == win)
662                                         sys_raise(win);
663                                 break;
664                         }
665                         ROW(lrow)->height = win->h;
666                 }
667                 x += col->width + MARGIN;
668         }
669 }
670
671 /*******************************
672  * Window management functions *
673  *******************************/
674 void wm_update(void)
675 {
676         /* Show/hide tags */
677         tag_foreach_col(wm_tag, dpy, col, row, win)
678                 sys_show(win, st_show);
679         tag_foreach_flt(wm_tag, dpy, flt, win)
680                 sys_show(win, st_show);
681         for (list_t *tag = wm ->tags; tag; tag = tag->next)
682                 if (tag->data != wm_tag) {
683                         tag_foreach_col(TAG(tag), dpy, col, row, win)
684                                         sys_show(win, st_hide);
685                         tag_foreach_flt(TAG(tag), dpy, flt, win)
686                                         sys_show(win, st_hide);
687                 }
688
689         /* Refresh the display */
690         for (list_t *ldpy = wm_tag->dpys; ldpy; ldpy = ldpy->next)
691                 wm_update_cols(ldpy->data);
692         tag_foreach_flt(wm_tag, ldpy, lflt, win) {
693                 flt_t *flt = lflt->data;
694                 sys_move(win, flt->x, flt->y, flt->w, flt->h);
695                 sys_raise(flt->win);
696         }
697         if (wm_focus)
698                 set_focus(wm_focus);
699 }
700
701 int wm_handle_key(win_t *win, Key_t key, mod_t mod, ptr_t ptr)
702 {
703         if (!win || win == wm_dpy->geom) return 0;
704         //printf("wm_handle_key: %p - %x %c%c%c%c%c\n", win, key,
705         //      mod.up    ? '^' : 'v',
706         //      mod.alt   ? 'a' : '-',
707         //      mod.ctrl  ? 'c' : '-',
708         //      mod.shift ? 's' : '-',
709         //      mod.win   ? 'w' : '-');
710
711         /* Mouse movement */
712         if (key == key_mouse1)
713                 raise_float(win);
714         if (key_mouse0 <= key && key <= key_mouse7) {
715                 if (key == key_mouse1 && mod.MODKEY && !mod.up)
716                         return set_move(win,ptr,move),   1;
717                 if (key == key_mouse3 && mod.MODKEY && !mod.up)
718                         return set_move(win,ptr,resize), 1;
719                 if (move_mode != none && mod.up)
720                         return set_move(win,ptr,none),   1;
721                 if (key == key_mouse1 && !mod.up && win->h == STACK)
722                         return wm_update(),              0;
723                 return 0;
724         }
725
726         /* Only handle key-down */
727         if (mod.up)
728                 return 0;
729
730         /* Misc */
731         if (mod.MODKEY) {
732 #ifdef DEBUG
733                 if (key == key_f1) return raise_float(win), 1;
734                 if (key == key_f2) return set_focus(win), 1;
735                 if (key == key_f3) return sys_show(win, st_show), 1;
736                 if (key == key_f4) return sys_show(win, st_hide), 1;
737 #endif
738                 if (key == key_f5) return wm_update(),    1;
739                 if (key == key_f6) return print_txt(),    1;
740                 if (key == 'q')    return sys_exit(),     1;
741         }
742
743         /* Floating layer */
744         if (key == ' ') {
745                 if (mod.MODKEY && mod.shift)
746                         return set_layer(win), 1;
747                 if (mod.MODKEY)
748                         return switch_layer(), 1;
749         }
750
751         /* Movement commands */
752         if (mod.MODKEY && mod.shift) {
753                 switch (key) {
754                 case 'h': return shift_window(wm_focus,-1, 0), 1;
755                 case 'j': return shift_window(wm_focus, 0,+1), 1;
756                 case 'k': return shift_window(wm_focus, 0,-1), 1;
757                 case 'l': return shift_window(wm_focus,+1, 0), 1;
758                 default: break;
759                 }
760         }
761         else if (mod.MODKEY) {
762                 switch (key) {
763                 case 'h': return shift_focus(-1, 0), 1;
764                 case 'j': return shift_focus( 0,+1), 1;
765                 case 'k': return shift_focus( 0,-1), 1;
766                 case 'l': return shift_focus(+1, 0), 1;
767                 default: break;
768                 }
769         }
770
771         /* Column mode commands */
772         if (mod.MODKEY) {
773                 switch (key) {
774                 case 'd': return set_mode(win, split), 1;
775                 case 's': return set_mode(win, stack), 1;
776                 case 'm': return set_mode(win, max),   1;
777                 case 't': return set_mode(win, tab),   1;
778                 default: break;
779                 }
780         }
781
782         /* Tag switching */
783         if (mod.MODKEY && '0' <= key && key <= '9') {
784                 int name = key - '0';
785                 if (mod.shift)
786                         tag_set(win, name);
787                 else
788                         tag_switch(name);
789                 wm_update();
790         }
791
792         /* Focus change */
793         if (key == key_enter)
794                 return set_focus(win), 1;
795
796         if (key_mouse0 <= key && key <= key_mouse7)
797                 return set_focus(win), 0;
798
799         /* Reset focus after after focus change,
800          * not sure what is causing the focus change in the first place
801          * but preventing that would be a better solution */
802         if (key == key_focus)
803                 sys_focus(wm_focus ?: wm->root);
804
805         return 0;
806 }
807
808 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
809 {
810         //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
811         //              cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
812
813         if (move_mode == none)
814                 return 0;
815
816         int dx = ptr.rx - move_prev.rx;
817         int dy = ptr.ry - move_prev.ry;
818         move_prev = ptr;
819
820         if (move_layer == tiling && move_mode == resize) {
821                 list_t *vert = move_dir.v < 0 ? move_lrow->prev : move_lrow->next;
822                 list_t *horz = move_dir.h < 0 ? move_lcol->prev : move_lcol->next;
823                 if (vert) {
824                         ROW(move_lrow)->height += move_dir.v * dy;
825                         ROW(vert)->height      -= move_dir.v * dy;
826                 }
827                 if (horz) {
828                         COL(move_lcol)->width  += move_dir.h * dx;
829                         COL(horz)->width       -= move_dir.h * dx;
830                 }
831                 wm_update();
832         }
833
834         if (move_layer == floating) {
835                 flt_t *flt = move_lflt->data;
836                 win_t *win = flt->win;
837                 if (move_mode == move)
838                         sys_move(win, win->x+dx, win->y+dy, win->w, win->h);
839                 else if (move_mode == resize)
840                         sys_move(win,
841                                 win->x + dx * (move_dir.h < 0),
842                                 win->y + dy * (move_dir.v < 0),
843                                 win->w + dx *  move_dir.h,
844                                 win->h + dy *  move_dir.v);
845                 flt->x = win->x; flt->y = win->y;
846                 flt->w = win->w; flt->h = win->h;
847         }
848
849         return 0;
850 }
851
852 void wm_insert(win_t *win)
853 {
854         printf("wm_insert: %p\n", win);
855         print_txt();
856
857         /* Initialize window */
858         win->wm = new0(win_wm_t);
859         sys_watch(win, key_enter,  MOD());
860         sys_watch(win, key_focus,  MOD());
861
862         /* Add to screen */
863         put_win(win, wm_tag, wm_dpy->layer);
864
865         /* Arrange */
866         wm_update();
867         set_focus(wm_focus);
868         print_txt();
869 }
870
871 void wm_remove(win_t *win)
872 {
873         printf("wm_remove: %p\n", win);
874         print_txt();
875         for (list_t *tag = wm->tags; tag; tag = tag->next)
876                 cut_win(win, tag->data);
877         free(win->wm);
878         set_focus(wm_focus);
879         wm_update();
880         print_txt();
881 }
882
883 void wm_init(win_t *root)
884 {
885         printf("wm_init: %p\n", root);
886
887         /* Load configuration */
888         MARGIN = conf_get_int("main.margin", MARGIN);
889         STACK  = conf_get_int("main.stack",  STACK);
890
891         /* Hack, fix screen order */
892         list_t *screens = sys_info(root);
893         list_t *left  = screens;
894         list_t *right = screens->next;
895         if (left && right && WIN(left)->x > WIN(right)->x) {
896                 void *tmp   = left->data;
897                 left->data  = right->data;
898                 right->data = tmp;
899         }
900
901         wm          = new0(wm_t);
902         wm->root    = root;
903         wm->screens = screens;
904         wm->tag     = tag_new(wm->screens, 1);
905         wm->tags    = list_insert(NULL, wm->tag);
906
907         Key_t keys_e[] = {key_enter, key_focus};
908         Key_t keys_s[] = {'h', 'j', 'k', 'l', 'q', ' ',
909                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
910         Key_t keys_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't', ' ',
911                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
912                 key_f1, key_f2, key_f3, key_f4, key_f5, key_f6,
913                 key_mouse1, key_mouse3};
914         for (int i = 0; i < countof(keys_e); i++)
915                 sys_watch(root, keys_e[i],  MOD());
916         for (int i = 0; i < countof(keys_m); i++)
917                 sys_watch(root, keys_m[i], MOD(.MODKEY=1));
918         for (int i = 0; i < countof(keys_s); i++)
919                 sys_watch(root, keys_s[i], MOD(.MODKEY=1,.shift=1));
920 }
921
922 void wm_free(win_t *root)
923 {
924         /* Re-show and free all windows */
925         while ( wm->tags) { tag_t *tag =  wm->tags->data;
926         while (tag->dpys) { dpy_t *dpy = tag->dpys->data;
927         while (dpy->cols) { col_t *col = dpy->cols->data;
928         while (col->rows) { row_t *row = col->rows->data;
929                 sys_show(row->win, st_show);
930                 free(row->win->wm);
931         col->rows = list_remove(col->rows, col->rows, 1); }
932         dpy->cols = list_remove(dpy->cols, dpy->cols, 1); }
933         while (dpy->flts) { flt_t *flt = dpy->flts->data;
934                 sys_show(flt->win, st_show);
935                 free(flt->win->wm);
936         dpy->flts = list_remove(dpy->flts, dpy->flts, 1); }
937         tag->dpys = list_remove(tag->dpys, tag->dpys, 1); }
938          wm->tags = list_remove( wm->tags,  wm->tags, 1); }
939
940         /* Free remaining data */
941         free(wm);
942 }