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