]> Pileus Git - wmpus/blob - wm-wmii.c
Add floating layer
[wmpus] / wm-wmii.c
1 /*
2  * Copyright (C) 2011 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20
21 #include "util.h"
22 #include "sys.h"
23 #include "wm.h"
24
25 #ifndef MODKEY
26 #define MODKEY alt
27 #endif
28 #ifndef MARGIN
29 #define MARGIN 0
30 #endif
31 #ifndef STACK
32 #define STACK  25
33 #endif
34
35 /* Enums */
36 typedef enum {
37         none, move, resize
38 } drag_t;
39
40 typedef enum {
41         split, stack, max, tab
42 } mode_t;
43
44 typedef enum {
45         tiling, floating
46 } layer_t;
47
48 /* Window structure types */
49 struct win_wm { };
50
51 typedef struct {
52         win_t  *win;     // the window
53         int     height;  // win height in _this_ tag
54 } row_t;
55
56 typedef struct {
57         list_t *rows;    // of row_t
58         row_t  *row;     // focused row
59         int     width;   // column width
60         mode_t  mode;    // display mode
61 } col_t;
62
63 typedef struct {
64         win_t *win;      // the window
65         int x, y, w, h;  // position of window (in this tag)
66 } flt_t;
67
68 typedef struct {
69         list_t *cols;    // of col_t
70         col_t  *col;     // focused col
71         list_t *flts;    // of flt_t
72         flt_t  *flt;     // focused flt
73         layer_t layer;   // focused layer
74         win_t  *geom;    // display size and position
75 } dpy_t;
76
77 typedef struct {
78         list_t *dpys;    // of dpy_t
79         dpy_t  *dpy;     // focused dpy
80         int     name;    // tag name
81 } tag_t;
82
83 typedef struct {
84         list_t *tags;    // of tag_t
85         tag_t  *tag;     // focused tag
86         win_t  *root;    // root/background window
87         list_t *screens; // display geometry
88 } wm_t;
89
90 #define WIN(node) ((win_t*)(node)->data)
91 #define ROW(node) ((row_t*)(node)->data)
92 #define COL(node) ((col_t*)(node)->data)
93 #define FLT(node) ((flt_t*)(node)->data)
94 #define DPY(node) ((dpy_t*)(node)->data)
95 #define TAG(node) ((tag_t*)(node)->data)
96
97 #define tag_foreach_col(tag, dpy, col, row, win) \
98         for (list_t *dpy =     tag ->dpys; dpy; dpy = dpy->next) \
99         for (list_t *col = DPY(dpy)->cols; col; col = col->next) \
100         for (list_t *row = COL(col)->rows; row; row = row->next) \
101         for (win_t  *win = ROW(row)->win;  win; win = NULL)
102
103 #define tag_foreach_flt(tag, dpy, flt, win) \
104         for (list_t *dpy =     tag ->dpys; dpy; dpy = dpy->next) \
105         for (list_t *flt = DPY(dpy)->flts; flt; flt = flt->next) \
106         for (win_t  *win = FLT(flt)->win;  win; win = NULL)      \
107
108 /* Window management data
109  *   wm_* macros represent the currently focused item
110  *   _only_ wm_focus protects against NULL pointers */
111 static wm_t *wm;
112 #define wm_tag   wm->tag
113 #define wm_dpy   wm->tag->dpy
114 #define wm_col   wm->tag->dpy->col
115 #define wm_row   wm->tag->dpy->col->row
116 #define wm_flt   wm->tag->dpy->flt
117 #define wm_focus get_focus()
118
119 /* Mouse drag data */
120 static drag_t  move_mode;
121 static list_t *move_lrow;
122 static list_t *move_lcol;
123 static list_t *move_lflt;
124 static ptr_t   move_prev;
125 static layer_t move_layer;
126 static struct { int v, h; } move_dir;
127
128 /********************
129  * Helper functions *
130  ********************/
131 static win_t *get_focus(void)
132 {
133         if (!wm_tag || !wm_dpy)
134                 return NULL;
135         switch (wm_dpy->layer) {
136         case tiling:
137                 return wm_col && wm_row ? wm_row->win : NULL;
138         case floating:
139                 return wm_flt ? wm_flt->win : NULL;
140         }
141         return NULL;
142 }
143
144 /* Search for the target window in a given tag
145  * win may exist in other tags as well */
146 static int searchl(tag_t *tag, win_t *target,
147                 list_t **_dpy, list_t **_col, list_t **_row, list_t **_flt)
148 {
149         tag_foreach_col(tag, dpy, col, row, win) {
150                 if (win == target) {
151                         if (_dpy) *_dpy = dpy;
152                         if (_col) *_col = col;
153                         if (_row) *_row = row;
154                         return tiling;
155                 }
156         }
157         tag_foreach_flt(tag, dpy, flt, win) {
158                 if (win == target) {
159                         if (_dpy) *_dpy = dpy;
160                         if (_flt) *_flt = flt;
161                         return floating;
162                 }
163         }
164         return -1;
165 }
166
167 static int search(tag_t *tag, win_t *target,
168                 dpy_t **_dpy, col_t **_col, row_t **_row, flt_t **_flt)
169 {
170         list_t *dpy, *col, *row, *flt;
171         switch (searchl(tag, target, &dpy, &col, &row, &flt)) {
172         case tiling:
173                 if (_dpy) *_dpy = DPY(dpy);
174                 if (_col) *_col = COL(col);
175                 if (_row) *_row = ROW(row);
176                 return tiling;
177         case floating:
178                 if (_dpy) *_dpy = DPY(dpy);
179                 if (_flt) *_flt = FLT(flt);
180                 return floating;
181         }
182         return -1;
183 }
184
185 /* Set the mode for the windows column in the current tag */
186 static void set_mode(win_t *win, mode_t mode)
187 {
188         col_t *col;
189         if (tiling != search(wm_tag, win, NULL, &col, NULL, NULL))
190                 return;
191         printf("set_mode: %p, %d -> %d\n",
192                         col, col->mode, mode);
193         col->mode = mode;
194         if (col->mode == split)
195                 for (list_t *cur = col->rows; cur; cur = cur->next) {
196                         row_t *row = cur->data;
197                         row->height = wm_dpy->geom->h;
198                 }
199         wm_update();
200 }
201
202 /* Focus the window in the current tag and record
203  * it as the currently focused window */
204 static void set_focus(win_t *win)
205 {
206         if (win == NULL || win == wm->root) {
207                 sys_focus(wm->root);
208                 return;
209         }
210
211         /* - Only grab mouse button on unfocused window,
212          *   this prevents stealing all mouse clicks from client windows,
213          * - A better way may be to re-send mouse clicks to client windows
214          *   using the return value from wm_handle_key */
215         for (int i = key_mouse1; i < key_mouse7; i++) {
216                 if (wm_focus)
217                         sys_watch(wm_focus, i, MOD());
218                 sys_unwatch(win, i, MOD());
219         }
220
221         dpy_t *dpy; col_t *col; row_t *row; flt_t *flt;
222         switch (search(wm_tag, win, &dpy, &col, &row, &flt)) {
223         case tiling:
224                 wm_dpy = dpy;
225                 wm_col = col;
226                 wm_row = row;
227                 dpy->layer = tiling;
228                 break;
229         case floating:
230                 wm_dpy = dpy;
231                 wm_flt = flt;
232                 dpy->layer = floating;
233                 break;
234         }
235         sys_focus(win);
236 }
237
238 /* Save mouse start location when moving/resizing windows */
239 static void set_move(win_t *win, ptr_t ptr, drag_t drag)
240 {
241         printf("set_move: %d - %p@%d,%d\n",
242                         drag, win, ptr.rx, ptr.ry);
243         move_mode = drag;
244         if (drag == move || drag == resize) {
245                 move_layer = searchl(wm_tag, win, NULL,
246                                 &move_lcol, &move_lrow, &move_lflt);
247                 if (move_layer < 0)
248                         return;
249                 move_prev = ptr;
250                 int midy = win->y + (win->h/2);
251                 int midx = win->x + (win->w/2);
252                 move_dir.v = ptr.ry < midy ? -1 : +1;
253                 move_dir.h = ptr.rx < midx ? -1 : +1;
254         }
255 }
256
257 /* Print a text representation of the window layout
258  * Quite useful for debugging */
259 static void print_txt(void)
260 {
261         for (list_t *ltag = wm->tags; ltag; ltag = ltag->next) {
262                 tag_t *tag = ltag->data;
263                 printf("tag:       <%-9p [%p->%p] >%-9p d=%-9p - %d\n",
264                                 ltag->prev, ltag, ltag->data, ltag->next,
265                                 tag->dpy, tag->name);
266         for (list_t *ldpy = tag->dpys; ldpy; ldpy = ldpy->next) {
267                 dpy_t *dpy  = ldpy->data;
268                 win_t *geom = dpy->geom;
269                 printf("  dpy:     <%-9p [%p->%p] >%-9p %c=%-9p - %d,%d %dx%d\n",
270                                 ldpy->prev, ldpy, ldpy->data, ldpy->next,
271                                 dpy->layer == tiling   ? 'c' : 'f',
272                                 dpy->layer == tiling   ? (void*)dpy->col : (void*)dpy->flt,
273                                 geom->x, geom->y, geom->h, geom->w);
274         for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
275                 col_t *col = lcol->data;
276                 printf("    col:   <%-9p [%p->%p] >%-9p r=%-9p - %dpx @ %d\n",
277                                 lcol->prev, lcol, lcol->data, lcol->next,
278                                 col->row, col->width, col->mode);
279         for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
280                 row_t *row = lrow->data;
281                 win_t *win = row->win;
282                 printf("      win: <%-9p [%p>>%p] >%-9p focus=%d%d    - %4dpx \n",
283                                 lrow->prev, lrow, win, lrow->next,
284                                 col->row == row, wm_focus == win, win->h);
285         } }
286         for (list_t *lflt = dpy->flts; lflt; lflt = lflt->next) {
287                 flt_t *flt = lflt->data;
288                 printf("    flt:   <%-9p [%p->%p] >%-9p focus=%d%d    - %d,%d %dx%d \n",
289                                 lflt->prev, lflt, lflt->data, lflt->next,
290                                 dpy->flt == flt, wm_focus == flt->win,
291                                 flt->x, flt->y, flt->h, flt->w);
292         }  } }
293 }
294
295 /* Cleanly remove a window from a tag
296  *   Determines the new focused row/col
297  *   Prunes empty lists */
298 static layer_t cut_win(win_t *win, tag_t *tag)
299 {
300         list_t *ldpy, *lcol, *lrow, *lflt;
301         layer_t layer = searchl(tag, win, &ldpy, &lcol, &lrow, &lflt);
302
303         dpy_t  *dpy   = DPY(ldpy);
304         if (layer == tiling) {
305                 col_t  *col  = COL(lcol);
306                 col->row  = lrow->prev ? lrow->prev->data :
307                             lrow->next ? lrow->next->data : NULL;
308                 col->rows = list_remove(col->rows, lrow);
309                 if (col->rows == NULL && (lcol->next || lcol->prev)) {
310                         dpy->col  = lcol->prev ? lcol->prev->data :
311                                     lcol->next ? lcol->next->data : NULL;
312                         dpy->cols = list_remove(dpy->cols, lcol);
313                 }
314         }
315
316         if (layer == floating) {
317                 dpy->flts = list_remove(dpy->flts, lflt);
318                 dpy->flt  = dpy->flts ? list_last(dpy->flts)->data : NULL;
319                 if (!dpy->flt && dpy->col && dpy->col->row)
320                         dpy->layer = tiling;
321         }
322
323         return layer;
324 }
325
326 /* Insert a window into the tiling layer
327  *   The window is added immediately after the
328  *   columns currently focused row */
329 static void put_win_col(win_t *win, tag_t *tag, dpy_t *dpy, col_t *col)
330 {
331         row_t *row = new0(row_t);
332         row->win = win;
333
334         if (col == NULL) {
335                 col = new0(col_t);
336                 dpy->cols = list_insert(dpy->cols, col);
337         }
338
339         int nrows = list_length(col->rows);
340         if (col->row) {
341                 list_t *prev = list_find(col->rows, col->row);
342                 list_insert_after(prev, row);
343         } else {
344                 col->rows = list_insert(col->rows, row);
345         }
346         tag->dpy           = dpy;
347         tag->dpy->col      = col;
348         tag->dpy->col->row = row;
349         tag->dpy->layer    = tiling;
350
351         row->height = dpy->geom->h / MAX(nrows,1);
352         if (nrows == 0) {
353                 int ncols = list_length(dpy->cols);
354                 col->width = dpy->geom->w / MAX(ncols-1,1);
355         }
356 }
357
358 /* Insert a window into the floating layer */
359 static void put_win_flt(win_t *win, tag_t *tag, dpy_t *dpy)
360 {
361         flt_t *flt = new0(flt_t);
362         flt->win = win;
363         flt->w   = dpy->geom->w / 2;
364         flt->h   = dpy->geom->h / 2;
365         flt->x   = dpy->geom->x + flt->w / 2;
366         flt->y   = dpy->geom->y + flt->h / 2;
367         if (dpy->flt) {
368                 flt->x = dpy->flt->x + 20;
369                 flt->y = dpy->flt->y + 20;
370         }
371         dpy->flts = list_append(dpy->flts, flt);
372         tag->dpy        = dpy;
373         tag->dpy->flt   = flt;
374         tag->dpy->layer = floating;
375 }
376
377 /* Insert a window into a tag */
378 static void put_win(win_t *win, tag_t *tag, layer_t layer)
379 {
380         if (layer == tiling)
381                 put_win_col(win, tag, tag->dpy, tag->dpy->col);
382         if (layer == floating)
383                 put_win_flt(win, tag, tag->dpy);
384 }
385
386 /* Move a window up, down, left, or right
387  *   This handles moving with a column, between
388  *   columns, and between multiple monitors. */
389 static void shift_window(win_t *win, int col, int row)
390 {
391         if (!win) return;
392         printf("shift_window: %p - %+d,%+d\n", win, col, row);
393         print_txt();
394         printf("shift_window: >>>\n");
395         list_t *ldpy, *lcol, *lrow, *lflt;
396         if (tiling != searchl(wm_tag, win, &ldpy, &lcol, &lrow, &lflt))
397                 return;
398         dpy_t *dpy = ldpy->data;
399         if (row != 0) {
400                 /* Move with a column, just swap rows */
401                 list_t *src = lrow, *dst = NULL;
402                 if (row < 0) dst = src->prev;
403                 if (row > 0) dst = src->next;
404                 if (src && dst) {
405                         printf("swap: %p <-> %p\n", src->data, dst->data);
406                         row_t *tmp = src->data;
407                         src->data = dst->data;
408                         dst->data = tmp;
409                         goto update;
410                 }
411         } else {
412                 /* Moving between columns */
413                 int onlyrow = !lrow->prev && !lrow->next;
414                 list_t *src = lcol, *dst = NULL;
415                 if (col < 0) {
416                         if (src->prev) {
417                                 /* Normal move between columns */
418                                 dst = src->prev;
419                         } else if (!onlyrow) {
420                                 /* Create new column */
421                                 dpy->cols = list_insert(dpy->cols, new0(col_t));
422                                 dst = src->prev;
423                         } else if (ldpy->prev) {
424                                 /* Move to next monitor */
425                                 dpy = ldpy->prev->data;
426                                 dst = list_last(dpy->cols);
427                         } else {
428                                 /* We, shall, not, be,
429                                  * we shall not be moved */
430                                 return;
431                         }
432                 }
433                 if (col > 0) {
434                         if (src->next) {
435                                 dst = src->next;
436                         } else if (!onlyrow) {
437                                 dpy->cols = list_append(dpy->cols, new0(col_t));
438                                 dst = src->next;
439                         } else if (ldpy->next) {
440                                 dpy = ldpy->next->data;
441                                 dst = dpy->cols;
442                         } else {
443                                 return;
444                         }
445                 }
446                 cut_win(win, wm_tag);
447                 put_win_col(win, wm_tag, dpy, dst ? dst->data : NULL);
448                 goto update;
449         }
450 update:
451         print_txt();
452         wm_update();
453 }
454
455 /* Get next/prev item, with wraparound */
456 static list_t *get_next(list_t *list, int forward)
457 {
458         list_t *next = forward ? list->next : list->prev;
459         if (next == NULL) {
460                 next = list;
461                 while ((list = forward ? next->prev : next->next))
462                         next = list;
463         }
464         return next;
465 }
466
467 /* Move keyboard focus in a given direction */
468 static void shift_focus(int cols, int rows)
469 {
470         printf("shift_focus: %+d,%+d\n", cols, rows);
471         if (rows != 0 && wm_focus) {
472                 /* Move focus up/down */
473                 list_t *dpy, *col, *row;
474                 if (tiling != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
475                         return;
476                 row_t *next = get_next(row, rows > 0)->data;
477                 set_focus(next->win);
478                 if (COL(col)->mode != split)
479                         wm_update();
480         }
481         if (cols != 0) {
482                 /* Move focus left/right */
483                 list_t *dpy, *col, *row, *ndpy, *ncol = NULL;
484                 if (wm_focus) {
485                         /* Currently focused on a window */
486                         if (tiling != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
487                                 return;
488                         ncol = cols > 0 ? col->next : col->prev;
489                 } else {
490                         /* Currently focused on an empty display */
491                         dpy = list_find(wm_tag->dpys, wm_dpy);
492                 }
493                 if (ncol == NULL) {
494                         /* Moving focus to a different display */
495                         ndpy = get_next(dpy, cols > 0);
496                         ncol = cols > 0 ? DPY(ndpy)->cols :
497                                 list_last(DPY(ndpy)->cols);
498                         wm_dpy = ndpy->data;
499                 }
500                 if (ncol && COL(ncol) && COL(ncol)->row)
501                         set_focus(COL(ncol)->row->win);
502                 else
503                         sys_focus(wm->root);
504         }
505 }
506
507 /* Raise the window in the floating */
508 static void raise_float(win_t *win)
509 {
510         printf("raise_float: %p\n", win);
511         list_t *cur;
512         for (cur = wm_dpy->flts; cur; cur = cur->next)
513                 if (FLT(cur)->win == win)
514                         break;
515         if (cur) {
516                 flt_t *flt = cur->data;
517                 wm_dpy->flts = list_remove(wm_dpy->flts, cur);
518                 wm_dpy->flts = list_append(wm_dpy->flts, flt);
519         }
520         sys_raise(win);
521 }
522
523 /* Toggle between floating and tiling layers */
524 static void switch_layer(void)
525 {
526         printf("switch_float: %p %d\n",
527                         wm_dpy, wm_dpy->layer);
528         wm_dpy->layer = !wm_dpy->layer;
529         wm_update();
530 }
531
532 /* Move current window between floating and tiling layers */
533 static void set_layer(win_t *win)
534 {
535         if (!win) return;
536         printf("set_float: %p %p\n", wm_dpy, win);
537         wm_dpy->layer = !cut_win(win, wm_tag);
538         put_win(win, wm_tag, wm_dpy->layer);
539         wm_update();
540 }
541
542 /* Allocate a new tag */
543 static tag_t *tag_new(list_t *screens, int name)
544 {
545         tag_t *tag = new0(tag_t);
546         tag->name  = name;
547         for (list_t *cur = screens; cur; cur = cur->next) {
548                 dpy_t *dpy  = new0(dpy_t);
549                 dpy->geom = cur->data;
550                 tag->dpys = list_append(tag->dpys, dpy);
551         }
552         tag->dpy  = tag->dpys->data;
553         return tag;
554 }
555
556 /* Search for a tag
557  *   If it does not exist it is based on the
558  *   display geometry in wm->screens */
559 static tag_t *tag_find(int name)
560 {
561         tag_t *tag = NULL;
562         for (list_t *cur = wm->tags; cur; cur = cur->next)
563                 if (name == TAG(cur)->name) {
564                         tag = cur->data;
565                         break;
566                 }
567         if (!tag) {
568                 tag = tag_new(wm->screens, name);
569                 wm->tags = list_append(wm->tags, tag);
570         }
571         return tag;
572 }
573
574 /* Move the window from the current tag to the new tag
575  *   Unlike wmii, only remove the current tag, not all tags */
576 static void tag_set(win_t *win, int name)
577 {
578         printf("tag_set: %p %d\n", win, name);
579         if (wm_tag->name == name)
580                 return;
581         tag_t *tag = tag_find(name);
582         layer_t layer = cut_win(win, wm_tag);
583         put_win(win, tag, layer);
584         set_focus(wm_focus);
585 }
586
587 /* Switch to a different tag */
588 static void tag_switch(int name)
589 {
590         printf("tag_switch: %d\n", name);
591         if ((wm_col == NULL || wm_row == NULL) && wm_flt == NULL)
592                 wm->tags = list_remove(wm->tags,
593                                 list_find(wm->tags, wm_tag));
594         wm_tag = tag_find(name);
595 }
596
597 /* Tile all windows in the given display
598  *   This performs all the actual window tiling
599  *   Currently supports  split, stack and maximized modes */
600 static void wm_update_cols(dpy_t *dpy)
601 {
602         int  x=0,  y=0; // Current window top-left position
603         int tx=0, ty=0; // Total size (sum of initial col widths and row heights w/o margin)
604         int mx=0, my=0; // Maximum usable size (screen size minus margins)
605         int       sy=0; // Stack size (height of focused stack window)
606
607         /* Scale horizontally */
608         x  = dpy->geom->x;
609         mx = dpy->geom->w - (list_length(dpy->cols)+1)*MARGIN;
610         for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
611                 tx += COL(lcol)->width;
612         for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
613                 COL(lcol)->width *= (float)mx / tx;
614
615         /* Scale each column vertically */
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 (col->row->win == 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         sys_watch(win, key_mouse1, MOD());
853
854         /* Add to screen */
855         put_win(win, wm_tag, wm_dpy->layer);
856
857         /* Arrange */
858         wm_update();
859         set_focus(wm_focus);
860         print_txt();
861 }
862
863 void wm_remove(win_t *win)
864 {
865         printf("wm_remove: %p\n", win);
866         print_txt();
867         for (list_t *tag = wm->tags; tag; tag = tag->next)
868                 cut_win(win, tag->data);
869         set_focus(wm_focus);
870         wm_update();
871         print_txt();
872 }
873 void wm_init(win_t *root)
874 {
875         printf("wm_init: %p\n", root);
876
877         /* Hack, fix screen order */
878         list_t *screens = sys_info(root);
879         list_t *left  = screens;
880         list_t *right = screens->next;
881         if (left && right && WIN(left)->x > WIN(right)->x) {
882                 void *tmp   = left->data;
883                 left->data  = right->data;
884                 right->data = tmp;
885         }
886
887         wm          = new0(wm_t);
888         wm->root    = root;
889         wm->screens = screens;
890         wm->tag     = tag_new(wm->screens, 1);
891         wm->tags    = list_insert(NULL, wm->tag);
892
893         Key_t keys_e[] = {key_enter, key_focus};
894         Key_t keys_s[] = {'h', 'j', 'k', 'l', ' ',
895                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
896         Key_t keys_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't', ' ',
897                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
898                 key_f1, key_f2, key_f3, key_f4, key_f5, key_f6,
899                 key_mouse1, key_mouse3};
900         for (int i = 0; i < countof(keys_e); i++)
901                 sys_watch(root, keys_e[i],  MOD());
902         for (int i = 0; i < countof(keys_m); i++)
903                 sys_watch(root, keys_m[i], MOD(.MODKEY=1));
904         for (int i = 0; i < countof(keys_s); i++)
905                 sys_watch(root, keys_s[i], MOD(.MODKEY=1,.shift=1));
906 }