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