]> Pileus Git - wmpus/blob - wm-wmii.c
Add support for windows being in multiple tags
[wmpus] / wm-wmii.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "util.h"
5 #include "sys.h"
6 #include "wm.h"
7
8 #define MODKEY ctrl
9 #define MARGIN 0
10 #define STACK  25
11
12 /* Enums */
13 typedef enum {
14         none, move, resize
15 } drag_t;
16
17 typedef enum {
18         split, stack, max, tab
19 } mode_t;
20
21
22 /* Window structure types */
23 struct win_wm { };
24
25 typedef struct {
26         win_t  *win;
27         int     height;
28 } row_t;
29
30 typedef struct {
31         list_t *rows; // of row_t
32         row_t  *row;
33         int     width;
34         mode_t  mode;
35 } col_t;
36
37 typedef struct {
38         list_t *cols; // of col_t
39         col_t  *col;
40         win_t  *root;
41 } dpy_t;
42
43 typedef struct {
44         list_t *dpys; // of dpy_t
45         dpy_t  *dpy;
46         int     name;
47 } tag_t;
48
49 typedef struct {
50         list_t *tags; // of tag_t
51         tag_t  *tag;
52 } wm_t;
53
54 /* Mouse drag data */
55 static drag_t  move_mode;
56 static list_t *move_lrow;
57 static list_t *move_lcol;
58 static ptr_t   move_prev;
59 static struct { int v, h; } move_dir;
60
61 /* Window management data */
62 static wm_t  *wm;
63 #define wm_focus wm->tag->dpy->col->row->win
64 #define wm_row   wm->tag->dpy->col->row
65 #define wm_col   wm->tag->dpy->col
66 #define wm_dpy   wm->tag->dpy
67 #define wm_tag   wm->tag
68
69 #define ROW(l) ((row_t*)(l)->data)
70 #define COL(l) ((col_t*)(l)->data)
71 #define DPY(l) ((dpy_t*)(l)->data)
72 #define TAG(l) ((tag_t*)(l)->data)
73
74 /* Helper functions */
75 static int searchl(tag_t *tag, win_t *target,
76                 list_t **_dpy, list_t **_col, list_t **_row)
77 {
78         for (list_t *dpy =     tag ->dpys; dpy; dpy = dpy->next)
79         for (list_t *col = DPY(dpy)->cols; col; col = col->next)
80         for (list_t *row = COL(col)->rows; row; row = row->next) {
81                 win_t *win = ROW(row)->win;
82                 if (win == target) {
83                         if (_dpy) *_dpy = dpy;
84                         if (_col) *_col = col;
85                         if (_row) *_row = row;
86                         return 1;
87                 }
88         }
89         return 0;
90 }
91
92 static int search(tag_t *tag, win_t *target,
93                 dpy_t **_dpy, col_t **_col, row_t **_row)
94 {
95         list_t *dpy, *col, *row;
96         if (searchl(tag, target, &dpy, &col, &row)) {
97                 if (_dpy) *_dpy = DPY(dpy);
98                 if (_col) *_col = COL(col);
99                 if (_row) *_row = ROW(row);
100                 return 1;
101         }
102         return 0;
103 }
104
105 static void set_mode(win_t *win, mode_t mode)
106 {
107         col_t *col;
108         search(wm_tag, win, NULL, &col, NULL);
109         printf("set_mode: %p, %d -> %d\n",
110                         col, col->mode, mode);
111         col->mode = mode;
112         if (col->mode == split)
113                 for (list_t *cur = col->rows; cur; cur = cur->next) {
114                         row_t *row = cur->data;
115                         row->height = wm_dpy->root->h;
116                 }
117         wm_update();
118 }
119
120 static void set_focus(win_t *win)
121 {
122         /* - Only grab mouse button on unfocused window,
123          *   this prevents stealing all mouse clicks from client windows,
124          * - A better way may be to re-send mouse clicks to client windows
125          *   using the return value from wm_handle_key */
126         for (int i = key_mouse1; i < key_mouse7; i++) {
127                 if (wm_focus)
128                         sys_watch(wm_focus, i, MOD());
129                 sys_unwatch(win, i, MOD());
130         }
131
132         dpy_t *dpy; col_t *col; row_t *row;
133         if (search(wm_tag, win, &dpy, &col, &row)) {
134                 wm_dpy = dpy;
135                 wm_col = col;
136                 wm_row = row;
137         }
138         sys_focus(win);
139 }
140
141 static void set_move(win_t *win, ptr_t ptr, drag_t drag)
142 {
143         printf("set_move: %d - %p@%d,%d\n",
144                         drag, win, ptr.rx, ptr.ry);
145         move_mode = drag;
146         if (drag == move || drag == resize) {
147                 searchl(wm_tag, win, NULL, &move_lcol, &move_lrow);
148                 move_prev = ptr;
149                 int my = win->y + (win->h/2);
150                 int mx = win->x + (win->w/2);
151                 move_dir.v = ptr.ry < my ? -1 : +1;
152                 move_dir.h = ptr.rx < mx ? -1 : +1;
153         }
154 }
155
156 static void print_txt(list_t *cols)
157 {
158         for (list_t *lcol = cols; lcol; lcol = lcol->next) {
159                 col_t *col = lcol->data;
160                 printf("col:\t<%-9p [%-20p] >%-9p  -  %dpx @ %d !!%p\n",
161                                 ( lcol->prev ? lcol->prev->data : NULL ),
162                                 col,
163                                 ( lcol->next ? lcol->next->data : NULL ),
164                                 col->width, col->mode, col->row);
165                 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
166                         row_t *row = lrow->data;
167                         win_t *win = row->win;
168                         printf("  win:\t<%-9p [%p>>%p] >%-9p  -  %4dpx focus=%d%d\n",
169                                         lrow->prev, lrow, win, lrow->next,
170                                         win->h, col->row == row, wm_focus == win);
171                 }
172         }
173 }
174
175 static void cut_win(win_t *win)
176 {
177         list_t *ldpy, *lcol, *lrow;
178         searchl(wm_tag, win, &ldpy, &lcol, &lrow);
179         col_t  *col  = COL(lcol);
180         dpy_t  *dpy  = DPY(ldpy);
181
182         col->row  = lrow->prev ? lrow->prev->data :
183                     lrow->next ? lrow->next->data : NULL;
184         col->rows = list_remove(col->rows, lrow);
185
186         if (col->rows == NULL && (lcol->next || lcol->prev)) {
187                 dpy->col  = lcol->prev ? lcol->prev->data :
188                             lcol->next ? lcol->next->data : NULL;
189                 dpy->cols = list_remove(wm_dpy->cols, lcol);
190         }
191 }
192
193 static void put_win(win_t *win, col_t *col)
194 {
195         row_t *row = new0(row_t);
196         row->win = win;
197
198         if (col == NULL) {
199                 col = new0(col_t);
200                 wm_dpy->cols = list_insert(wm_dpy->cols, col);
201         }
202
203         int nrows = list_length(col->rows);
204         if (col->row) {
205                 list_t *prev = list_find(col->rows, col->row);
206                 list_insert_after(prev, row);
207         } else {
208                 col->rows = list_insert(col->rows, row);
209         }
210         col->row    = row;
211         wm_dpy->col = col;
212
213         row->height = wm_dpy->root->h / MAX(nrows,1);
214         if (nrows == 0) {
215                 int ncols = list_length(wm_dpy->cols);
216                 col->width = wm_dpy->root->w / MAX(ncols-1,1);
217         }
218 }
219
220 static void shift_window(win_t *win, int col, int row)
221 {
222         printf("shift_window: %p - %+d,%+d\n", win, col, row);
223         print_txt(wm_dpy->cols);
224         printf("shift_window: >>>\n");
225         list_t *ldpy, *lcol, *lrow;
226         searchl(wm_tag, win, &ldpy, &lcol, &lrow);
227         if (row != 0) {
228                 list_t *src = lrow, *dst = NULL;
229                 if (row < 0) dst = src->prev;
230                 if (row > 0) dst = src->next;
231                 if (src && dst) {
232                         printf("swap: %p <-> %p\n", src->data, dst->data);
233                         row_t *tmp = src->data;
234                         src->data = dst->data;
235                         dst->data = tmp;
236                         goto update;
237                 }
238         } else {
239                 int onlyrow = !lrow->prev && !lrow->next;
240                 list_t *src = lcol, *dst = NULL;
241                 if (col < 0) {
242                         if (!src->prev && !onlyrow)
243                                 wm_dpy->cols = list_insert(wm_dpy->cols, new0(col_t));
244                         dst = src->prev;
245                 }
246                 if (col > 0) {
247                         if (!src->next && !onlyrow)
248                                 wm_dpy->cols = list_append(wm_dpy->cols, new0(col_t));
249                         dst = src->next;
250                 }
251                 if (src && dst) {
252                         cut_win(win);
253                         put_win(win, COL(dst));
254                         goto update;
255                 }
256         }
257         return;
258 update:
259         print_txt(wm_dpy->cols);
260         wm_update();
261 }
262
263 static list_t *get_next(list_t *list, int forward)
264 {
265         list_t *next = forward ? list->next : list->prev;
266         if (next == NULL) {
267                 next = list;
268                 while ((list = forward ? next->prev : next->next))
269                         next = list;
270         }
271         return next;
272 }
273 static void shift_focus(win_t *win, int col, int row)
274 {
275         printf("shift_focus: %p - %+d,%+d\n", win, col, row);
276         list_t *ldpy, *lcol, *lrow;
277         searchl(wm_tag, win, &ldpy, &lcol, &lrow);
278         if (row != 0) {
279                 row_t *next = get_next(lrow, row > 0)->data;
280                 set_focus(next->win);
281                 if (COL(lcol)->mode != split)
282                         wm_update();
283         }
284         if (col != 0) {
285                 col_t *next = get_next(lcol, col > 0)->data;
286                 set_focus(next->row->win);
287         }
288 }
289
290 /* Window management functions */
291 void wm_update(void)
292 {
293         int  x=0,  y=0; // Current window top-left position
294         int tx=0, ty=0; // Total x/y size
295         int mx=0, my=0; // Maximum x/y size (screen size)
296         int       sy=0; // Size of focused stack window
297
298         /* Scale horizontally */
299         x  = wm_dpy->root->x;
300         mx = wm_dpy->root->w - (list_length(wm_dpy->cols)+1)*MARGIN;
301         for (list_t *lx = wm_dpy->cols; lx; lx = lx->next)
302                 tx += COL(lx)->width;
303         for (list_t *lx = wm_dpy->cols; lx; lx = lx->next)
304                 COL(lx)->width *= (float)mx / tx;
305
306         /* Scale each column vertically */
307         for (list_t *lx = wm_dpy->cols; lx; lx = lx->next) {
308                 col_t *col = lx->data;
309                 ty = 0;
310                 for (list_t *ly = col->rows; ly; ly = ly->next)
311                         ty += ROW(ly)->height;
312                 y  = wm_dpy->root->y;
313                 my = wm_dpy->root->h - (list_length(col->rows)+1)*MARGIN;
314                 sy = my              - (list_length(col->rows)-1)*STACK;
315                 for (list_t *ly = col->rows; ly; ly = ly->next) {
316                         win_t *win = ROW(ly)->win;
317                         win->h = ROW(ly)->height;
318                         int height = 0;
319                         switch (col->mode) {
320                         case split:
321                                 sys_move(win, x+MARGIN, y+MARGIN,
322                                         col->width, win->h * ((float)my / ty));
323                                 height = win->h;
324                                 break;
325                         case stack:
326                                 height = col->row->win == win ? sy : STACK;
327                                 sys_move(win, x+MARGIN, y+MARGIN,
328                                         col->width, height);
329                                 break;
330                         case max:
331                         case tab:
332                                 sys_move(win, x+MARGIN, 0+MARGIN,
333                                         col->width, wm_dpy->root->h-2*MARGIN);
334                                 if (col->row->win == win)
335                                         sys_raise(win);
336                                 break;
337                         }
338                         y += height + MARGIN;
339                         ROW(ly)->height = win->h;
340                 }
341                 x += col->width + MARGIN;
342         }
343 }
344
345 int wm_handle_key(win_t *win, Key_t key, mod_t mod, ptr_t ptr)
346 {
347         if (!win || win == wm_dpy->root) return 0;
348         //printf("wm_handle_key: %p - %x %c%c%c%c%c\n", win, key,
349         //      mod.up    ? '^' : 'v',
350         //      mod.alt   ? 'a' : '-',
351         //      mod.ctrl  ? 'c' : '-',
352         //      mod.shift ? 's' : '-',
353         //      mod.win   ? 'w' : '-');
354
355         /* Mouse movement */
356         if (key_mouse0 <= key && key <= key_mouse7 && mod.up)
357                 return set_move(win,ptr,none), 1;
358         else if (key == key_mouse1 && mod.MODKEY)
359                 return set_move(win,ptr,move), 1;
360         else if (key == key_mouse3 && mod.MODKEY)
361                 return set_move(win,ptr,resize), 1;
362
363         /* Only handle key-down */
364         if (mod.up)
365                 return 0;
366
367         /* Misc */
368         if (mod.MODKEY) {
369                 if (key == key_f1) return sys_raise(win), 1;
370                 if (key == key_f2) return set_focus(win), 1;
371                 if (key == key_f5) return wm_update(),    1;
372                 if (key == key_f6) return print_txt(wm_dpy->cols), 1;
373         }
374         if (key_mouse0 <= key && key <= key_mouse7)
375                 sys_raise(win);
376
377         /* Movement commands */
378         if (mod.MODKEY && mod.shift) {
379                 switch (key) {
380                 case 'h': return shift_window(win,-1, 0), 1;
381                 case 'j': return shift_window(win, 0,+1), 1;
382                 case 'k': return shift_window(win, 0,-1), 1;
383                 case 'l': return shift_window(win,+1, 0), 1;
384                 default: break;
385                 }
386         }
387         else if (mod.MODKEY) {
388                 switch (key) {
389                 case 'h': return shift_focus(win,-1, 0), 1;
390                 case 'j': return shift_focus(win, 0,+1), 1;
391                 case 'k': return shift_focus(win, 0,-1), 1;
392                 case 'l': return shift_focus(win,+1, 0), 1;
393                 default: break;
394                 }
395         }
396
397         /* Column mode commands */
398         if (mod.MODKEY) {
399                 switch (key) {
400                 case 'd': return set_mode(win, split), 1;
401                 case 's': return set_mode(win, stack), 1;
402                 case 'm': return set_mode(win, max),   1;
403                 case 't': return set_mode(win, tab),   1;
404                 default: break;
405                 }
406         }
407
408         /* Focus change */
409         if (key == key_enter)
410                 return set_focus(win), 1;
411
412         if (key_mouse0 <= key && key <= key_mouse7)
413                 return set_focus(win), 0;
414
415         /* Reset focus after after focus change,
416          * not sure what is causing the focus change in the first place
417          * but preventing that would be a better solution */
418         if (key == key_focus)
419                 set_focus(wm_focus);
420
421         return 0;
422 }
423
424 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
425 {
426         //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
427         //              cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
428
429         if (move_mode == none)
430                 return 0;
431
432         /* Tiling */
433         int dx = ptr.rx - move_prev.rx;
434         int dy = ptr.ry - move_prev.ry;
435         move_prev = ptr;
436         if (move_mode == resize) {
437                 list_t *vert = move_dir.v < 0 ? move_lrow->prev : move_lrow->next;
438                 list_t *horz = move_dir.h < 0 ? move_lcol->prev : move_lcol->next;
439                 if (vert) {
440                         ROW(move_lrow)->height += move_dir.v * dy;
441                         ROW(vert)->height      -= move_dir.v * dy;
442                 }
443                 if (horz) {
444                         COL(move_lcol)->width  += move_dir.h * dx;
445                         COL(horz)->width       -= move_dir.h * dx;
446                 }
447                 wm_update();
448         }
449
450         /* Floating */
451         //win_t *mwin = move_win;
452         //int dx = ptr.rx - move_prev.rx;
453         //int dy = ptr.ry - move_prev.ry;
454         //move_prev = ptr;
455         //if (move_mode == move)
456         //      sys_move(mwin, mwin->x+dx, mwin->y+dy, mwin->w, mwin->h);
457         //else if (move_mode == resize)
458         //      sys_move(mwin, mwin->x, mwin->y, mwin->w+dx, mwin->h+dy);
459
460         return 0;
461 }
462
463 void wm_insert(win_t *win)
464 {
465         printf("wm_insert: %p\n", win);
466         print_txt(wm_dpy->cols);
467
468         /* Initialize window */
469         win->wm = new0(win_wm_t);
470         sys_watch(win, key_enter, MOD());
471         sys_watch(win, key_focus, MOD());
472
473         /* Add to screen */
474         put_win(win, wm_col);
475
476         /* Arrange */
477         wm_update();
478         sys_focus(wm_focus);
479         print_txt(wm_dpy->cols);
480 }
481
482 void wm_remove(win_t *win)
483 {
484         printf("wm_remove: %p\n", win);
485         print_txt(wm_dpy->cols);
486         cut_win(win);
487         if (wm_focus)
488                 sys_focus(wm_focus);
489         else
490                 sys_focus(wm_dpy->root);
491         wm_update();
492         print_txt(wm_dpy->cols);
493 }
494
495 void wm_init(win_t *root)
496 {
497         printf("wm_init: %p\n", root);
498                wm  = new0(wm_t);
499         tag_t *tag = new0(tag_t);
500         dpy_t *dpy = new0(dpy_t);
501
502         dpy->root  = root;
503         tag->dpys  = list_insert(NULL, dpy);
504         tag->dpy   = dpy;
505         tag->name  = 1;
506         wm->tags   = list_insert(NULL, tag);
507         wm->tag    = tag;
508
509         Key_t keys_e[] = {key_enter, key_focus};
510         Key_t keys_s[] = {'h', 'j', 'k', 'l'};
511         Key_t keys_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't',
512                 key_f1, key_f2, key_f5, key_f6, key_mouse1, key_mouse3};
513         for (int i = 0; i < countof(keys_e); i++)
514                 sys_watch(root, keys_e[i],  MOD());
515         for (int i = 0; i < countof(keys_m); i++)
516                 sys_watch(root, keys_m[i], MOD(.MODKEY=1));
517         for (int i = 0; i < countof(keys_s); i++)
518                 sys_watch(root, keys_s[i], MOD(.MODKEY=1,.shift=1));
519 }