]> Pileus Git - wmpus/blob - wm-wmii.c
Fake clicking on title bars in stack mode
[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 /* Window structure types */
45 struct win_wm { };
46
47 typedef struct {
48         win_t  *win;     // the window
49         int     height;  // win height in _this_ tag
50 } row_t;
51
52 typedef struct {
53         list_t *rows;    // of row_t
54         row_t  *row;     // focused row
55         int     width;   // column width
56         mode_t  mode;    // display mode
57 } col_t;
58
59 typedef struct {
60         list_t *cols;    // of col_t
61         col_t  *col;     // focused col
62         win_t  *geom;    // display size and position
63 } dpy_t;
64
65 typedef struct {
66         list_t *dpys;    // of dpy_t
67         dpy_t  *dpy;     // focused dpy
68         int     name;    // tag name
69 } tag_t;
70
71 typedef struct {
72         list_t *tags;    // of tag_t
73         tag_t  *tag;     // focused tag
74         win_t  *root;    // root/background window
75         list_t *screens; // display geometry
76 } wm_t;
77
78 #define WIN(node) ((win_t*)(node)->data)
79 #define ROW(node) ((row_t*)(node)->data)
80 #define COL(node) ((col_t*)(node)->data)
81 #define DPY(node) ((dpy_t*)(node)->data)
82 #define TAG(node) ((tag_t*)(node)->data)
83
84 #define tag_foreach(tag, dpy, col, row, win) \
85         for (list_t *dpy =     tag ->dpys; dpy; dpy = dpy->next) \
86         for (list_t *col = DPY(dpy)->cols; col; col = col->next) \
87         for (list_t *row = COL(col)->rows; row; row = row->next) \
88         for (win_t  *win = ROW(row)->win;  win; win = NULL)      \
89
90 /* Window management data
91  *   wm_* macros represent the currently focused item
92  *   _only_ wm_focus protects against NULL pointers */
93 static wm_t *wm;
94 #define wm_win   wm->tag->dpy->col->row->win
95 #define wm_row   wm->tag->dpy->col->row
96 #define wm_col   wm->tag->dpy->col
97 #define wm_dpy   wm->tag->dpy
98 #define wm_tag   wm->tag
99 #define wm_focus (wm_tag && wm_dpy && wm_col && wm_row ? wm_win : NULL)
100
101 /* Mouse drag data */
102 static drag_t  move_mode;
103 static list_t *move_lrow;
104 static list_t *move_lcol;
105 static ptr_t   move_prev;
106 static struct { int v, h; } move_dir;
107
108 /********************
109  * Helper functions *
110  ********************/
111 /* Search for the target window in a given tag
112  * win may exist in other tags as well */
113 static int searchl(tag_t *tag, win_t *target,
114                 list_t **_dpy, list_t **_col, list_t **_row)
115 {
116         tag_foreach(tag, dpy, col, row, win) {
117                 if (win == target) {
118                         if (_dpy) *_dpy = dpy;
119                         if (_col) *_col = col;
120                         if (_row) *_row = row;
121                         return 1;
122                 }
123         }
124         return 0;
125 }
126
127 static int search(tag_t *tag, win_t *target,
128                 dpy_t **_dpy, col_t **_col, row_t **_row)
129 {
130         list_t *dpy, *col, *row;
131         if (searchl(tag, target, &dpy, &col, &row)) {
132                 if (_dpy) *_dpy = DPY(dpy);
133                 if (_col) *_col = COL(col);
134                 if (_row) *_row = ROW(row);
135                 return 1;
136         }
137         return 0;
138 }
139
140 /* Set the mode for the windows column in the current tag */
141 static void set_mode(win_t *win, mode_t mode)
142 {
143         col_t *col;
144         if (!search(wm_tag, win, NULL, &col, NULL))
145                 return;
146         printf("set_mode: %p, %d -> %d\n",
147                         col, col->mode, mode);
148         col->mode = mode;
149         if (col->mode == split)
150                 for (list_t *cur = col->rows; cur; cur = cur->next) {
151                         row_t *row = cur->data;
152                         row->height = wm_dpy->geom->h;
153                 }
154         wm_update();
155 }
156
157 /* Focus the window in the current tag and record
158  * it as the currently focused window */
159 static void set_focus(win_t *win)
160 {
161         if (win == NULL || win == wm->root) {
162                 sys_focus(wm->root);
163                 return;
164         }
165
166         /* - Only grab mouse button on unfocused window,
167          *   this prevents stealing all mouse clicks from client windows,
168          * - A better way may be to re-send mouse clicks to client windows
169          *   using the return value from wm_handle_key */
170         for (int i = key_mouse1; i < key_mouse7; i++) {
171                 if (wm_focus)
172                         sys_watch(wm_focus, i, MOD());
173                 sys_unwatch(win, i, MOD());
174         }
175
176         dpy_t *dpy; col_t *col; row_t *row;
177         if (search(wm_tag, win, &dpy, &col, &row)) {
178                 wm_dpy = dpy;
179                 wm_col = col;
180                 wm_row = row;
181         }
182         sys_focus(win);
183 }
184
185 /* Save mouse start location when moving/resizing windows */
186 static void set_move(win_t *win, ptr_t ptr, drag_t drag)
187 {
188         printf("set_move: %d - %p@%d,%d\n",
189                         drag, win, ptr.rx, ptr.ry);
190         move_mode = drag;
191         if (drag == move || drag == resize) {
192                 searchl(wm_tag, win, NULL, &move_lcol, &move_lrow);
193                 move_prev = ptr;
194                 int midy = win->y + (win->h/2);
195                 int midx = win->x + (win->w/2);
196                 move_dir.v = ptr.ry < midy ? -1 : +1;
197                 move_dir.h = ptr.rx < midx ? -1 : +1;
198         }
199 }
200
201 /* Print a text representation of the window layout
202  * Quite useful for debugging */
203 static void print_txt(void)
204 {
205         for (list_t *ltag = wm->tags; ltag; ltag = ltag->next) {
206                 tag_t *tag = ltag->data;
207                 printf("tag:       <%-9p [%p->%p] >%-9p !%-9p -  %d\n",
208                                 ltag->prev, ltag, ltag->data, ltag->next,
209                                 tag->dpy, tag->name);
210         for (list_t *ldpy = tag->dpys; ldpy; ldpy = ldpy->next) {
211                 dpy_t *dpy  = ldpy->data;
212                 win_t *geom = dpy->geom;
213                 printf("  dpy:     <%-9p [%p->%p] >%-9p !%-9p -  %d,%d %dx%d\n",
214                                 ldpy->prev, ldpy, ldpy->data, ldpy->next,
215                                 dpy->col, geom->x, geom->y, geom->h, geom->w);
216         for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
217                 col_t *col = lcol->data;
218                 printf("    col:   <%-9p [%p->%p] >%-9p !%-9p -  %dpx @ %d\n",
219                                 lcol->prev, lcol, lcol->data, lcol->next,
220                                 col->row, col->width, col->mode);
221         for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
222                 row_t *row = lrow->data;
223                 win_t *win = row->win;
224                 printf("      win: <%-9p [%p>>%p] >%-9p !%-9p -  %4dpx focus=%d%d\n",
225                                 lrow->prev, lrow, win, lrow->next,
226                                 win, win->h, col->row == row, wm_focus == win);
227         } } } }
228 }
229
230 /* Cleanly remove a window from a tag
231  *   Determines the new focused row/col
232  *   Prunes empty lists */
233 static void cut_win(tag_t *tag, win_t *win)
234 {
235         list_t *ldpy, *lcol, *lrow;
236         if (!searchl(tag, win, &ldpy, &lcol, &lrow))
237                 return;
238         col_t  *col  = COL(lcol);
239         dpy_t  *dpy  = DPY(ldpy);
240
241         col->row  = lrow->prev ? lrow->prev->data :
242                     lrow->next ? lrow->next->data : NULL;
243         col->rows = list_remove(col->rows, lrow);
244
245         if (col->rows == NULL && (lcol->next || lcol->prev)) {
246                 dpy->col  = lcol->prev ? lcol->prev->data :
247                             lcol->next ? lcol->next->data : NULL;
248                 dpy->cols = list_remove(dpy->cols, lcol);
249         }
250 }
251
252 /* Insert a window at a given location
253  *   The window is added immediately after the
254  *   columns currently focused row */
255 static void put_win(win_t *win, tag_t *tag, dpy_t *dpy, col_t *col)
256 {
257         row_t *row = new0(row_t);
258         row->win = win;
259
260         if (col == NULL) {
261                 col = new0(col_t);
262                 dpy->cols = list_insert(dpy->cols, col);
263         }
264
265         int nrows = list_length(col->rows);
266         if (col->row) {
267                 list_t *prev = list_find(col->rows, col->row);
268                 list_insert_after(prev, row);
269         } else {
270                 col->rows = list_insert(col->rows, row);
271         }
272         tag->dpy           = dpy;
273         tag->dpy->col      = col;
274         tag->dpy->col->row = row;
275
276         row->height = dpy->geom->h / MAX(nrows,1);
277         if (nrows == 0) {
278                 int ncols = list_length(dpy->cols);
279                 col->width = dpy->geom->w / MAX(ncols-1,1);
280         }
281 }
282
283 /* Move a window up, down, left, or right
284  *   This handles moving with a column, between
285  *   columns, and between multiple monitors. */
286 static void shift_window(win_t *win, int col, int row)
287 {
288         if (!win) return;
289         printf("shift_window: %p - %+d,%+d\n", win, col, row);
290         print_txt();
291         printf("shift_window: >>>\n");
292         list_t *ldpy, *lcol, *lrow;
293         if (!searchl(wm_tag, win, &ldpy, &lcol, &lrow))
294                 return;
295         dpy_t *dpy = ldpy->data;
296         if (row != 0) {
297                 /* Move with a column, just swap rows */
298                 list_t *src = lrow, *dst = NULL;
299                 if (row < 0) dst = src->prev;
300                 if (row > 0) dst = src->next;
301                 if (src && dst) {
302                         printf("swap: %p <-> %p\n", src->data, dst->data);
303                         row_t *tmp = src->data;
304                         src->data = dst->data;
305                         dst->data = tmp;
306                         goto update;
307                 }
308         } else {
309                 /* Moving between columns */
310                 int onlyrow = !lrow->prev && !lrow->next;
311                 list_t *src = lcol, *dst = NULL;
312                 if (col < 0) {
313                         if (src->prev) {
314                                 /* Normal move between columns */
315                                 dst = src->prev;
316                         } else if (!onlyrow) {
317                                 /* Create new column */
318                                 dpy->cols = list_insert(dpy->cols, new0(col_t));
319                                 dst = src->prev;
320                         } else if (ldpy->prev) {
321                                 /* Move to next monitor */
322                                 dpy = ldpy->prev->data;
323                                 dst = list_last(dpy->cols);
324                         } else {
325                                 /* We, shall, not, be,
326                                  * we shall not be moved */
327                                 return;
328                         }
329                 }
330                 if (col > 0) {
331                         if (src->next) {
332                                 dst = src->next;
333                         } else if (!onlyrow) {
334                                 dpy->cols = list_append(dpy->cols, new0(col_t));
335                                 dst = src->next;
336                         } else if (ldpy->next) {
337                                 dpy = ldpy->next->data;
338                                 dst = dpy->cols;
339                         } else {
340                                 return;
341                         }
342                 }
343                 cut_win(wm_tag, win);
344                 put_win(win, wm_tag, dpy, dst ? dst->data : NULL);
345                 goto update;
346         }
347 update:
348         print_txt();
349         wm_update();
350 }
351
352 /* Get next/prev item, with wraparound */
353 static list_t *get_next(list_t *list, int forward)
354 {
355         list_t *next = forward ? list->next : list->prev;
356         if (next == NULL) {
357                 next = list;
358                 while ((list = forward ? next->prev : next->next))
359                         next = list;
360         }
361         return next;
362 }
363
364 /* Move keyboard focus in a given direction */
365 static void shift_focus(int cols, int rows)
366 {
367         printf("shift_focus: %+d,%+d\n", cols, rows);
368         if (rows != 0 && wm_focus) {
369                 /* Move focus up/down */
370                 list_t *dpy, *col, *row;
371                 if (!searchl(wm_tag, wm_focus, &dpy, &col, &row))
372                         return;
373                 row_t *next = get_next(row, rows > 0)->data;
374                 set_focus(next->win);
375                 if (COL(col)->mode != split)
376                         wm_update();
377         }
378         if (cols != 0) {
379                 /* Move focus left/right */
380                 list_t *dpy, *col, *row, *ndpy, *ncol = NULL;
381                 if (wm_focus) {
382                         /* Currently focused on a window */
383                         if (!searchl(wm_tag, wm_focus, &dpy, &col, &row))
384                                 return;
385                         ncol = cols > 0 ? col->next : col->prev;
386                 } else {
387                         /* Currently focused on an empty display */
388                         dpy = list_find(wm_tag->dpys, wm_dpy);
389                 }
390                 if (ncol == NULL) {
391                         /* Moving focus to a different display */
392                         ndpy = get_next(dpy, cols > 0);
393                         ncol = cols > 0 ? DPY(ndpy)->cols :
394                                 list_last(DPY(ndpy)->cols);
395                         wm_dpy = ndpy->data;
396                 }
397                 if (ncol && COL(ncol) && COL(ncol)->row)
398                         set_focus(COL(ncol)->row->win);
399                 else
400                         sys_focus(wm->root);
401         }
402 }
403
404 /* Allocate a new tag */
405 static tag_t *tag_new(list_t *screens, int name)
406 {
407         tag_t *tag = new0(tag_t);
408         tag->name  = name;
409         for (list_t *cur = screens; cur; cur = cur->next) {
410                 dpy_t *dpy  = new0(dpy_t);
411                 dpy->geom = cur->data;
412                 tag->dpys = list_append(tag->dpys, dpy);
413         }
414         tag->dpy  = tag->dpys->data;
415         return tag;
416 }
417
418 /* Search for a tag
419  *   If it does not exist it is based on the
420  *   display geometry in wm->screens */
421 static tag_t *tag_find(int name)
422 {
423         tag_t *tag = NULL;
424         for (list_t *cur = wm->tags; cur; cur = cur->next)
425                 if (name == TAG(cur)->name) {
426                         tag = cur->data;
427                         break;
428                 }
429         if (!tag) {
430                 tag = tag_new(wm->screens, name);
431                 wm->tags = list_append(wm->tags, tag);
432         }
433         return tag;
434 }
435
436 /* Move the window from the current tag to the new tag
437  *   Unlike wmii, only remove the current tag, not all tags */
438 static void tag_set(win_t *win, int name)
439 {
440         printf("tag_set: %p %d\n", win, name);
441         if (wm_tag->name == name)
442                 return;
443         tag_t *tag = tag_find(name);
444         cut_win(wm_tag, win);
445         put_win(win, tag, tag->dpy, tag->dpy->col);
446         set_focus(wm_focus);
447 }
448
449 /* Switch to a different tag */
450 static void tag_switch(int name)
451 {
452         printf("tag_switch: %d\n", name);
453         if (wm_col == NULL || wm_row == NULL)
454                 wm->tags = list_remove(wm->tags,
455                                 list_find(wm->tags, wm_tag));
456         wm_tag = tag_find(name);
457 }
458
459 /* Tile all windows in the given display
460  *   This performs all the actual window tiling
461  *   Currently supports  split, stack and maximized modes */
462 static void wm_update_dpy(dpy_t *dpy)
463 {
464         int  x=0,  y=0; // Current window top-left position
465         int tx=0, ty=0; // Total size (sum of initial col widths and row heights w/o margin)
466         int mx=0, my=0; // Maximum usable size (screen size minus margins)
467         int       sy=0; // Stack size (height of focused stack window)
468
469         /* Scale horizontally */
470         x  = dpy->geom->x;
471         mx = dpy->geom->w - (list_length(dpy->cols)+1)*MARGIN;
472         for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
473                 tx += COL(lcol)->width;
474         for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
475                 COL(lcol)->width *= (float)mx / tx;
476
477         /* Scale each column vertically */
478         for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
479                 col_t *col = lcol->data;
480                 int nrows = list_length(col->rows);
481                 ty = 0;
482                 for (list_t *lrow = col->rows; lrow; lrow = lrow->next)
483                         ty += ROW(lrow)->height;
484                 y  = dpy->geom->y;
485                 my = dpy->geom->h - (MARGIN + (nrows-1)* MARGIN    + MARGIN);
486                 sy = dpy->geom->h - (MARGIN + (nrows-1)*(MARGIN/2) + MARGIN)
487                                   -           (nrows-1)* STACK;
488                 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
489                         win_t *win = ROW(lrow)->win;
490                         win->h = ROW(lrow)->height;
491                         int height = 0;
492                         switch (col->mode) {
493                         case split:
494                                 sys_move(win, x+MARGIN, y+MARGIN,
495                                         col->width, win->h * ((float)my / ty));
496                                 height = win->h;
497                                 y += height + MARGIN;
498                                 break;
499                         case stack:
500                                 if (lrow->next && ROW(lrow->next)->win == col->row->win) {
501                                         /* Hack to prevent flashing */
502                                         win_t *next = ROW(lrow->next)->win;
503                                         sys_move(next, x+MARGIN, y+MARGIN+STACK+MARGIN,
504                                                 col->width, sy);
505                                 }
506                                 height = win == col->row->win ? sy : STACK;
507                                 sys_move(win, x+MARGIN, y+MARGIN,
508                                         col->width, height);
509                                 y += height + (MARGIN/2);
510                                 break;
511                         case max:
512                         case tab:
513                                 sys_move(win, x+MARGIN, 0+MARGIN,
514                                         col->width, dpy->geom->h-2*MARGIN);
515                                 if (col->row->win == win)
516                                         sys_raise(win);
517                                 break;
518                         }
519                         ROW(lrow)->height = win->h;
520                 }
521                 x += col->width + MARGIN;
522         }
523 }
524
525 /*******************************
526  * Window management functions *
527  *******************************/
528 void wm_update(void)
529 {
530         /* Show/hide tags */
531         tag_foreach(wm_tag, dpy, col, row, win)
532                 sys_show(win, st_show);
533         for (list_t *tag = wm ->tags; tag; tag = tag->next)
534                 tag_foreach(TAG(tag), dpy, col, row, win)
535                         if (tag->data != wm_tag)
536                                 sys_show(win, st_hide);
537
538         /* Refresh the display */
539         for (list_t *ldpy = wm_tag->dpys; ldpy; ldpy = ldpy->next)
540                 wm_update_dpy(ldpy->data);
541         if (wm_focus)
542                 set_focus(wm_focus);
543 }
544
545 int wm_handle_key(win_t *win, Key_t key, mod_t mod, ptr_t ptr)
546 {
547         if (!win || win == wm_dpy->geom) return 0;
548         //printf("wm_handle_key: %p - %x %c%c%c%c%c\n", win, key,
549         //      mod.up    ? '^' : 'v',
550         //      mod.alt   ? 'a' : '-',
551         //      mod.ctrl  ? 'c' : '-',
552         //      mod.shift ? 's' : '-',
553         //      mod.win   ? 'w' : '-');
554
555         /* Mouse movement */
556         if (key_mouse0 <= key && key <= key_mouse7) {
557                 if (key == key_mouse1 && mod.MODKEY && !mod.up)
558                         return set_move(win,ptr,move),   1;
559                 if (key == key_mouse3 && mod.MODKEY && !mod.up)
560                         return set_move(win,ptr,resize), 1;
561                 if (move_mode != none && mod.up)
562                         return set_move(win,ptr,none),   1;
563                 if (key == key_mouse1 && !mod.up && win->h == STACK)
564                         return wm_update(),              0;
565                 return 0;
566         }
567
568         /* Only handle key-down */
569         if (mod.up)
570                 return 0;
571
572         /* Misc */
573         if (mod.MODKEY) {
574 #ifdef DEBUG
575                 if (key == key_f1) return sys_raise(win), 1;
576                 if (key == key_f2) return set_focus(win), 1;
577                 if (key == key_f3) return sys_show(win, st_show), 1;
578                 if (key == key_f4) return sys_show(win, st_hide), 1;
579 #endif
580                 if (key == key_f5) return wm_update(),    1;
581                 if (key == key_f6) return print_txt(),    1;
582         }
583         if (key_mouse0 <= key && key <= key_mouse7)
584                 sys_raise(win);
585
586         /* Movement commands */
587         if (mod.MODKEY && mod.shift) {
588                 switch (key) {
589                 case 'h': return shift_window(wm_focus,-1, 0), 1;
590                 case 'j': return shift_window(wm_focus, 0,+1), 1;
591                 case 'k': return shift_window(wm_focus, 0,-1), 1;
592                 case 'l': return shift_window(wm_focus,+1, 0), 1;
593                 default: break;
594                 }
595         }
596         else if (mod.MODKEY) {
597                 switch (key) {
598                 case 'h': return shift_focus(-1, 0), 1;
599                 case 'j': return shift_focus( 0,+1), 1;
600                 case 'k': return shift_focus( 0,-1), 1;
601                 case 'l': return shift_focus(+1, 0), 1;
602                 default: break;
603                 }
604         }
605
606         /* Column mode commands */
607         if (mod.MODKEY) {
608                 switch (key) {
609                 case 'd': return set_mode(win, split), 1;
610                 case 's': return set_mode(win, stack), 1;
611                 case 'm': return set_mode(win, max),   1;
612                 case 't': return set_mode(win, tab),   1;
613                 default: break;
614                 }
615         }
616
617         /* Tag switching */
618         if (mod.MODKEY && '0' <= key && key <= '9') {
619                 int name = key - '0';
620                 if (mod.shift)
621                         tag_set(win, name);
622                 else
623                         tag_switch(name);
624                 wm_update();
625         }
626
627         /* Focus change */
628         if (key == key_enter)
629                 return set_focus(win), 1;
630
631         if (key_mouse0 <= key && key <= key_mouse7)
632                 return set_focus(win), 0;
633
634         /* Reset focus after after focus change,
635          * not sure what is causing the focus change in the first place
636          * but preventing that would be a better solution */
637         if (key == key_focus)
638                 sys_focus(wm_focus ?: wm->root);
639
640         return 0;
641 }
642
643 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
644 {
645         //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
646         //              cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
647
648         if (move_mode == none)
649                 return 0;
650
651         /* Tiling */
652         int dx = ptr.rx - move_prev.rx;
653         int dy = ptr.ry - move_prev.ry;
654         move_prev = ptr;
655         if (move_mode == resize) {
656                 list_t *vert = move_dir.v < 0 ? move_lrow->prev : move_lrow->next;
657                 list_t *horz = move_dir.h < 0 ? move_lcol->prev : move_lcol->next;
658                 if (vert) {
659                         ROW(move_lrow)->height += move_dir.v * dy;
660                         ROW(vert)->height      -= move_dir.v * dy;
661                 }
662                 if (horz) {
663                         COL(move_lcol)->width  += move_dir.h * dx;
664                         COL(horz)->width       -= move_dir.h * dx;
665                 }
666                 wm_update();
667         }
668
669         /* Floating */
670         //win_t *mwin = move_win;
671         //int dx = ptr.rx - move_prev.rx;
672         //int dy = ptr.ry - move_prev.ry;
673         //move_prev = ptr;
674         //if (move_mode == move)
675         //      sys_move(mwin, mwin->x+dx, mwin->y+dy, mwin->w, mwin->h);
676         //else if (move_mode == resize)
677         //      sys_move(mwin, mwin->x, mwin->y, mwin->w+dx, mwin->h+dy);
678
679         return 0;
680 }
681
682 void wm_insert(win_t *win)
683 {
684         printf("wm_insert: %p\n", win);
685         print_txt();
686
687         /* Initialize window */
688         win->wm = new0(win_wm_t);
689         sys_watch(win, key_enter, MOD());
690         sys_watch(win, key_focus, MOD());
691
692         /* Add to screen */
693         put_win(win, wm_tag, wm_dpy, wm_col);
694
695         /* Arrange */
696         wm_update();
697         set_focus(wm_focus);
698         print_txt();
699 }
700
701 void wm_remove(win_t *win)
702 {
703         printf("wm_remove: %p\n", win);
704         print_txt();
705         for (list_t *tag = wm->tags; tag; tag = tag->next)
706                 cut_win(tag->data, win);
707         set_focus(wm_focus);
708         wm_update();
709         print_txt();
710 }
711 void wm_init(win_t *root)
712 {
713         printf("wm_init: %p\n", root);
714
715         /* Hack, fix screen order */
716         list_t *screens = sys_info(root);
717         list_t *left  = screens;
718         list_t *right = screens->next;
719         if (left && right && WIN(left)->x > WIN(right)->x) {
720                 void *tmp   = left->data;
721                 left->data  = right->data;
722                 right->data = tmp;
723         }
724
725         wm          = new0(wm_t);
726         wm->root    = root;
727         wm->screens = screens;
728         wm->tag     = tag_new(wm->screens, 1);
729         wm->tags    = list_insert(NULL, wm->tag);
730
731         Key_t keys_e[] = {key_enter, key_focus};
732         Key_t keys_s[] = {'h', 'j', 'k', 'l',
733                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
734         Key_t keys_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't',
735                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
736                 key_f1, key_f2, key_f3, key_f4, key_f5, key_f6,
737                 key_mouse1, key_mouse3};
738         for (int i = 0; i < countof(keys_e); i++)
739                 sys_watch(root, keys_e[i],  MOD());
740         for (int i = 0; i < countof(keys_m); i++)
741                 sys_watch(root, keys_m[i], MOD(.MODKEY=1));
742         for (int i = 0; i < countof(keys_s); i++)
743                 sys_watch(root, keys_s[i], MOD(.MODKEY=1,.shift=1));
744 }