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