]> Pileus Git - wmpus/blob - wm-wmii.c
Use focus when moving windows
[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 10
10
11 /* Loca types */
12 struct win_wm {
13         list_t *col; // node in wm_cols
14         list_t *row; // node in col->rows
15 };
16
17 typedef enum {
18         none, move, resize
19 } drag_t;
20
21 typedef enum {
22         stack, split, max, tab
23 } group_t;
24
25 typedef struct {
26         int     width;
27         group_t group;
28         win_t  *focus;
29         list_t *rows;
30 } col_t;
31
32 /* Mouse drag data */
33 static drag_t move_mode;
34 static win_t *move_win;
35 static ptr_t  move_prev;
36
37 /* Window management data */
38 static win_t  *wm_focus;
39 static list_t *wm_cols;
40 static win_t  *wm_root;
41
42 /* Helper functions */
43 static void set_focus(win_t *win)
44 {
45         if (win->wm && win->wm->col)
46                 ((col_t*)win->wm->col->data)->focus = win;
47         wm_focus = win;
48         sys_focus(win);
49 }
50
51 static void set_mode(drag_t drag, win_t *win, ptr_t ptr)
52 {
53         printf("set_mode: %d - %p@%d,%d\n",
54                         drag, win, ptr.rx, ptr.ry);
55         move_mode = drag;
56         if (drag == move || drag == resize) {
57                 move_win  = win;
58                 move_prev = ptr;
59         }
60 }
61
62 static void print_txt(list_t *cols)
63 {
64         for (list_t *cnode = cols; cnode; cnode = cnode->next) {
65                 col_t *col = cnode->data;
66                 printf("col:\t%p - %dpx @ %d !!%p\n", col, col->width, col->group, col->focus);
67                 for (list_t *wnode = col->rows; wnode; wnode = wnode->next) {
68                         win_t *win = wnode->data;
69                         printf("  win:\t^%-9p <%-9p [%p=%p] >%-9p %4dpx focus=%d%d\n",
70                                         win->wm->col->data,
71                                         (win->wm->row->prev ? win->wm->row->prev->data : NULL),
72                                         win->wm->row->data, win,
73                                         (win->wm->row->next ? win->wm->row->next->data : NULL),
74                                         win->h, col->focus == win, wm_focus == win);
75                 }
76         }
77 }
78
79 static void arrange(list_t *cols)
80 {
81         int  x=0,  y=0; // Current window top-left position
82         int tx=0, ty=0; // Total x/y size
83         int mx=0, my=0; // Maximum x/y size (screen size)
84
85
86         /* Scale horizontally */
87         mx = wm_root->w - (list_length(wm_cols)+1)*MARGIN;
88         for (list_t *lx = cols; lx; lx = lx->next)
89                 tx += ((col_t*)lx->data)->width;
90         for (list_t *lx = cols; lx; lx = lx->next)
91                 ((col_t*)lx->data)->width *= (float)mx / tx;
92
93         /* Scale each column vertically */
94         for (list_t *lx = cols; lx; lx = lx->next) {
95                 col_t *col = lx->data;
96                 ty = 0;
97                 for (list_t *ly = col->rows; ly; ly = ly->next)
98                         ty += ((win_t*)ly->data)->h;
99                 y = 0;
100                 my = wm_root->h - (list_length(col->rows)+1)*MARGIN;
101                 for (list_t *ly = col->rows; ly; ly = ly->next) {
102                         win_t *win = ly->data;
103                         sys_move(win, x+MARGIN, y+MARGIN,
104                                 col->width,
105                                 win->h * ((float)my / ty));
106                         y += win->h + MARGIN;
107                 }
108                 x += col->width + MARGIN;
109         }
110 }
111
112 static void cut_window(win_t *win)
113 {
114         list_t *lrow = win->wm->row;
115         list_t *lcol = win->wm->col;
116         col_t  *col  = lcol->data;
117
118         col->focus = lrow->prev ? lrow->prev->data  :
119                      lrow->next ? lrow->next->data  : NULL;
120
121         wm_focus   = col->focus ? col->focus        :
122                      lcol->prev ? ((col_t*)lcol->prev->data)->focus :
123                      lcol->next ? ((col_t*)lcol->next->data)->focus : NULL;
124
125         col->rows  = list_remove(col->rows, lrow);
126         if (col->rows == NULL)
127                 wm_cols = list_remove(wm_cols, lcol);
128 }
129
130 static void put_window(win_t *win, list_t *lcol)
131 {
132         col_t *col = lcol->data;
133         int nrows = list_length(col->rows);
134         if (col->focus) {
135                 list_insert_after(col->focus->wm->row, win);
136                 win->wm->row = col->focus->wm->row->next;
137         } else {
138                 col->rows = list_insert(col->rows, win);
139                 win->wm->row = col->rows;
140         }
141         win->wm->col = lcol;
142         col->focus   = win;
143         wm_focus     = win;
144
145         win->h = wm_root->h / MAX(nrows,1);
146         if (nrows == 0) {
147                 int ncols = list_length(wm_cols);
148                 col->width = wm_root->w / MAX(ncols-1,1);
149         }
150 }
151
152 static void shift_window(win_t *win, int col, int row)
153 {
154         printf("shift_window: %p - %+d,%+d\n", win, col, row);
155         print_txt(wm_cols);
156         printf("shift_window: >>>\n");
157         if (row != 0) {
158                 list_t *src = win->wm->row, *dst = NULL;
159                 if (row < 0) dst = src->prev;
160                 if (row > 0) dst = src->next;
161                 if (src && dst) {
162                         printf("swap: %p <-> %p\n", src->data, dst->data);
163                         src->data = dst->data;
164                         dst->data = win;
165                         ((win_t*)src->data)->wm->row = src;
166                         ((win_t*)dst->data)->wm->row = dst;
167                         arrange(wm_cols);
168                 }
169         } else {
170                 int onlyrow = !win->wm->row->prev && !win->wm->row->next;
171                 list_t *src = win->wm->col, *dst = NULL;
172                 if (col < 0) {
173                         if (!src->prev && !onlyrow)
174                                 wm_cols = list_insert(wm_cols, new0(col_t));
175                         dst = src->prev;
176                 }
177                 if (col > 0) {
178                         if (!src->next && !onlyrow)
179                                 wm_cols = list_append(wm_cols, new0(col_t));
180                         dst = src->next;
181                 }
182                 if (src && dst) {
183                         cut_window(win);
184                         put_window(win, dst);
185                         arrange(wm_cols);
186                 }
187         }
188         print_txt(wm_cols);
189 }
190
191 static void shift_focus(win_t *win, int col, int row)
192 {
193         printf("shift_focus: %p - %+d,%+d\n", win, col, row);
194         list_t *node  = NULL;
195         if (row != 0) {
196                 if (row < 0) node = win->wm->row->prev;
197                 if (row > 0) node = win->wm->row->next;
198         } else {
199                 if (col < 0) node = win->wm->col->prev;
200                 if (col > 0) node = win->wm->col->next;
201                 if (node) {
202                         col_t *col = node->data;
203                         node = col->focus->wm->row;
204                 }
205         }
206         if (node)
207                 set_focus(node->data);
208 }
209
210 /* Window management functions */
211 int wm_handle_key(win_t *win, Key_t key, mod_t mod, ptr_t ptr)
212 {
213         if (!win || win == wm_root) return 0;
214         //printf("wm_handle_key: %p - %x %x\n", win, key, mod);
215
216         /* Raise */
217         if (key == key_f2)
218                 return set_focus(win), 1;
219         if (key == key_f4)
220                 return sys_raise(win), 1;
221         if (key == key_f1 && mod.MODKEY)
222                 sys_raise(win);
223         if (key == key_f12 && mod.MODKEY)
224                 print_txt(wm_cols);
225         if (key_mouse0 <= key && key <= key_mouse7)
226                 sys_raise(win);
227
228         /* Key commands */
229         if (mod.MODKEY && mod.shift) {
230                 switch (key) {
231                 case 'h': return shift_window(win,-1, 0), 1;
232                 case 'j': return shift_window(win, 0,+1), 1;
233                 case 'k': return shift_window(win, 0,-1), 1;
234                 case 'l': return shift_window(win,+1, 0), 1;
235                 default: break;
236                 }
237         }
238         else if (mod.MODKEY) {
239                 switch (key) {
240                 case 'h': return shift_focus(win,-1, 0), 1;
241                 case 'j': return shift_focus(win, 0,+1), 1;
242                 case 'k': return shift_focus(win, 0,-1), 1;
243                 case 'l': return shift_focus(win,+1, 0), 1;
244                 default: break;
245                 }
246         }
247
248         /* Mouse movement */
249         if (key_mouse0 <= key && key <= key_mouse7 && mod.up)
250                 return set_mode(none,win,ptr), 1;
251         else if (key == key_mouse1 && mod.MODKEY)
252                 return set_mode(move,win,ptr), 1;
253         else if (key == key_mouse3 && mod.MODKEY)
254                 return set_mode(resize,win,ptr), 1;
255
256         /* Focus change */
257         if (key == key_enter)
258                 set_focus(win);
259
260         return 0;
261 }
262
263 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
264 {
265         //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
266         //              cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
267
268         if (move_mode == none)
269                 return 0;
270
271         /* Tiling */
272         int dx = ptr.rx - move_prev.rx;
273         int dy = ptr.ry - move_prev.ry;
274         move_prev = ptr;
275         if (move_mode == resize) {
276                 list_t *row   = move_win->wm->row;
277                 list_t *col   = move_win->wm->col;
278                 list_t *lower = row->next;
279                 list_t *right = col->next;
280                 if (lower) {
281                         ((win_t*)row->data)->h       += dy;
282                         ((win_t*)lower->data)->h     -= dy;
283                 }
284                 if (right) {
285                         ((col_t*)col->data)->width   += dx;
286                         ((col_t*)right->data)->width -= dx;
287                 }
288                 arrange(wm_cols);
289         }
290
291         /* Floating */
292         //win_t *mwin = move_win;
293         //int dx = ptr.rx - move_prev.rx;
294         //int dy = ptr.ry - move_prev.ry;
295         //move_prev = ptr;
296         //if (move_mode == move)
297         //      sys_move(mwin, mwin->x+dx, mwin->y+dy, mwin->w, mwin->h);
298         //else if (move_mode == resize)
299         //      sys_move(mwin, mwin->x, mwin->y, mwin->w+dx, mwin->h+dy);
300
301         return 0;
302 }
303
304 void wm_insert(win_t *win)
305 {
306         printf("wm_insert: %p\n", win);
307         print_txt(wm_cols);
308
309         /* Watch enter/leave */
310         sys_watch(win, key_enter, MOD());
311
312         /* Add to screen */
313         col_t *col = wm_cols->data;
314         //col_t *col = wm_focus && wm_focus->wm ?
315         //      wm_focus->wm->col->data : wm_cols->data;
316         int nrows = list_length(col->rows);
317         col->rows = list_insert(col->rows, win);
318
319         /* Setup window */
320         win->wm = new0(win_wm_t);
321         win->wm->col = wm_cols;
322         win->wm->row = col->rows;
323         if (nrows) win->h = wm_root->h / nrows;
324
325         /* Arrange */
326         arrange(wm_cols);
327         print_txt(wm_cols);
328 }
329
330 void wm_remove(win_t *win)
331 {
332         printf("wm_remove: %p - (%p,%p)\n", win,
333                         win->wm->col, win->wm->row);
334         print_txt(wm_cols);
335         cut_window(win);
336         if (wm_focus)
337                 sys_focus(wm_focus);
338         arrange(wm_cols);
339         print_txt(wm_cols);
340 }
341
342 void wm_init(win_t *root)
343 {
344         printf("wm_init: %p\n", root);
345         wm_root = root;
346         sys_watch(root, key_f1,     MOD(.MODKEY=1));
347         sys_watch(root, key_f12,    MOD(.MODKEY=1));
348         sys_watch(root, key_mouse1, MOD(.MODKEY=1));
349         sys_watch(root, key_mouse3, MOD(.MODKEY=1));
350         sys_watch(root, key_enter,  MOD());
351         Key_t keys[] = {'h', 'j', 'k', 'l'};
352         for (int i = 0; i < countof(keys); i++) {
353                 sys_watch(root, keys[i], MOD(.MODKEY=1));
354                 sys_watch(root, keys[i], MOD(.MODKEY=1,.shift=1));
355         }
356         col_t *col = new0(col_t);
357         col->group = stack;
358         col->width = root->w;
359         wm_cols = list_insert(wm_cols, col);
360 }