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