]> Pileus Git - wmpus/blob - wm-wmii.c
Fix get_next
[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         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                 next = list;
203                 while ((list = forward ? next->prev : next->next))
204                         next = list;
205         }
206         return next;
207 }
208 static void shift_focus(win_t *win, int col, int row)
209 {
210         printf("shift_focus: %p - %+d,%+d\n", win, col, row);
211         if (row != 0) {
212                 set_focus(get_next(win->wm->row, row > 0)->data);
213                 if (((col_t*)win->wm->col->data)->mode != split)
214                         wm_update();
215         }
216         if (col != 0) {
217                 col_t *next = get_next(win->wm->col, col > 0)->data;
218                 set_focus(next->focus->wm->row->data);
219         }
220 }
221
222 /* Window management functions */
223 void wm_update(void)
224 {
225         int  x=0,  y=0; // Current window top-left position
226         int tx=0, ty=0; // Total x/y size
227         int mx=0, my=0; // Maximum x/y size (screen size)
228         int       sy=0; // Size of focused stack window
229
230         /* Scale horizontally */
231         x  = wm_root->x;
232         mx = wm_root->w - (list_length(wm_cols)+1)*MARGIN;
233         for (list_t *lx = wm_cols; lx; lx = lx->next)
234                 tx += ((col_t*)lx->data)->width;
235         for (list_t *lx = wm_cols; lx; lx = lx->next)
236                 ((col_t*)lx->data)->width *= (float)mx / tx;
237
238         /* Scale each column vertically */
239         for (list_t *lx = wm_cols; lx; lx = lx->next) {
240                 col_t *col = lx->data;
241                 ty = 0;
242                 for (list_t *ly = col->rows; ly; ly = ly->next)
243                         ty += ((win_t*)ly->data)->h;
244                 y  = wm_root->y;
245                 my = wm_root->h - (list_length(col->rows)+1)*MARGIN;
246                 sy = my         - (list_length(col->rows)-1)*STACK;
247                 for (list_t *ly = col->rows; ly; ly = ly->next) {
248                         win_t *win = ly->data;
249                         int height = 0;
250                         switch (col->mode) {
251                         case split:
252                                 sys_move(win, x+MARGIN, y+MARGIN,
253                                         col->width, win->h * ((float)my / ty));
254                                 height = win->h;
255                                 break;
256                         case stack:
257                                 height = col->focus == win ? sy : STACK;
258                                 sys_move(win, x+MARGIN, y+MARGIN,
259                                         col->width, height);
260                                 break;
261                         case max:
262                         case tab:
263                                 sys_move(win, x+MARGIN, 0+MARGIN,
264                                         col->width, wm_root->h-2*MARGIN);
265                                 if (col->focus == win)
266                                         sys_raise(win);
267                                 break;
268                         }
269                         y += height + MARGIN;
270                 }
271                 x += col->width + MARGIN;
272         }
273 }
274
275 int wm_handle_key(win_t *win, Key_t key, mod_t mod, ptr_t ptr)
276 {
277         if (!win || win == wm_root) return 0;
278         //printf("wm_handle_key: %p - %x %c%c%c%c%c\n", win, key,
279         //      mod.up    ? '^' : 'v',
280         //      mod.alt   ? 'a' : '-',
281         //      mod.ctrl  ? 'c' : '-',
282         //      mod.shift ? 's' : '-',
283         //      mod.win   ? 'w' : '-');
284
285         /* Mouse movement */
286         if (key_mouse0 <= key && key <= key_mouse7 && mod.up)
287                 return set_move(win,ptr,none), 1;
288         else if (key == key_mouse1 && mod.MODKEY)
289                 return set_move(win,ptr,move), 1;
290         else if (key == key_mouse3 && mod.MODKEY)
291                 return set_move(win,ptr,resize), 1;
292
293         /* Only handle key-down */
294         if (mod.up)
295                 return 0;
296
297         /* Misc */
298         if (mod.MODKEY) {
299                 if (key == key_f1) return sys_raise(win), 1;
300                 if (key == key_f2) return set_focus(win), 1;
301                 if (key == key_f5) return wm_update(),    1;
302                 if (key == key_f6) return print_txt(wm_cols), 1;
303         }
304         if (key_mouse0 <= key && key <= key_mouse7)
305                 sys_raise(win);
306
307         /* Movement commands */
308         if (mod.MODKEY && mod.shift) {
309                 switch (key) {
310                 case 'h': return shift_window(win,-1, 0), 1;
311                 case 'j': return shift_window(win, 0,+1), 1;
312                 case 'k': return shift_window(win, 0,-1), 1;
313                 case 'l': return shift_window(win,+1, 0), 1;
314                 default: break;
315                 }
316         }
317         else if (mod.MODKEY) {
318                 switch (key) {
319                 case 'h': return shift_focus(win,-1, 0), 1;
320                 case 'j': return shift_focus(win, 0,+1), 1;
321                 case 'k': return shift_focus(win, 0,-1), 1;
322                 case 'l': return shift_focus(win,+1, 0), 1;
323                 default: break;
324                 }
325         }
326
327         /* Column mode commands */
328         if (mod.MODKEY) {
329                 switch (key) {
330                 case 'd': return set_mode(win, split), 1;
331                 case 's': return set_mode(win, stack), 1;
332                 case 'm': return set_mode(win, max),   1;
333                 case 't': return set_mode(win, tab),   1;
334                 default: break;
335                 }
336         }
337
338         /* Focus change */
339         if (key == key_enter)
340                 return set_focus(win), 1;
341
342         if (key_mouse0 <= key && key <= key_mouse7)
343                 return set_focus(win), 0;
344
345         /* Reset focus after after focus change,
346          * not sure what is causing the focus change in the first place
347          * but preventing that would be a better solution */
348         if (key == key_focus)
349                 set_focus(wm_focus);
350
351         return 0;
352 }
353
354 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
355 {
356         //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
357         //              cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
358
359         if (move_mode == none)
360                 return 0;
361
362         /* Tiling */
363         int dx = ptr.rx - move_prev.rx;
364         int dy = ptr.ry - move_prev.ry;
365         move_prev = ptr;
366         if (move_mode == resize) {
367                 list_t *row  = move_win->wm->row;
368                 list_t *col  = move_win->wm->col;
369                 list_t *vert = move_dir.v < 0 ? row->prev : row->next;
370                 list_t *horz = move_dir.h < 0 ? col->prev : col->next;
371                 if (vert) {
372                         ((win_t*)row->data)->h      += move_dir.v * dy;
373                         ((win_t*)vert->data)->h     -= move_dir.v * dy;
374                 }
375                 if (horz) {
376                         ((col_t*)col->data)->width  += move_dir.h * dx;
377                         ((col_t*)horz->data)->width -= move_dir.h * dx;
378                 }
379                 wm_update();
380         }
381
382         /* Floating */
383         //win_t *mwin = move_win;
384         //int dx = ptr.rx - move_prev.rx;
385         //int dy = ptr.ry - move_prev.ry;
386         //move_prev = ptr;
387         //if (move_mode == move)
388         //      sys_move(mwin, mwin->x+dx, mwin->y+dy, mwin->w, mwin->h);
389         //else if (move_mode == resize)
390         //      sys_move(mwin, mwin->x, mwin->y, mwin->w+dx, mwin->h+dy);
391
392         return 0;
393 }
394
395 void wm_insert(win_t *win)
396 {
397         printf("wm_insert: %p\n", win);
398         print_txt(wm_cols);
399
400         /* Initialize window */
401         win->wm = new0(win_wm_t);
402         sys_watch(win, key_enter, MOD());
403         sys_watch(win, key_focus, MOD());
404
405         /* Add to screen */
406         list_t *lcol = wm_focus && wm_focus->wm ?
407                 wm_focus->wm->col : wm_cols;
408         put_window(win, lcol);
409
410         /* Arrange */
411         wm_update();
412         sys_focus(wm_focus);
413         print_txt(wm_cols);
414 }
415
416 void wm_remove(win_t *win)
417 {
418         printf("wm_remove: %p - (%p,%p)\n", win,
419                         win->wm->col, win->wm->row);
420         print_txt(wm_cols);
421         cut_window(win);
422         if (wm_focus)
423                 sys_focus(wm_focus);
424         else
425                 sys_focus(wm_root);
426         wm_update();
427         print_txt(wm_cols);
428 }
429
430 void wm_init(win_t *root)
431 {
432         printf("wm_init: %p\n", root);
433         wm_root = root;
434         Key_t keys_e[] = {key_enter, key_focus};
435         Key_t keys_s[] = {'h', 'j', 'k', 'l'};
436         Key_t keys_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't',
437                 key_f1, key_f2, key_f5, key_f6, key_mouse1, key_mouse3};
438         for (int i = 0; i < countof(keys_e); i++)
439                 sys_watch(root, keys_e[i],  MOD());
440         for (int i = 0; i < countof(keys_m); i++)
441                 sys_watch(root, keys_m[i], MOD(.MODKEY=1));
442         for (int i = 0; i < countof(keys_s); i++)
443                 sys_watch(root, keys_s[i], MOD(.MODKEY=1,.shift=1));
444 }