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