]> Pileus Git - wmpus/blob - wm-wmii.c
Use focus for new 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 *lcol = cols; lcol; lcol = lcol->next) {
65                 col_t *col = lcol->data;
66                 printf("col:\t           <%-9p [%-19p] >%-9p  -  %dpx @ %d !!%p\n",
67                                 ( lcol->prev ? lcol->prev->data : NULL ),
68                                 col,
69                                 ( lcol->next ? lcol->next->data : NULL ),
70                                 col->width, col->group, col->focus);
71                 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
72                         win_t *win = lrow->data;
73                         printf("  win:\t^%-9p <%-9p [%p=%p] >%-9p  -  %4dpx focus=%d%d\n",
74                                         win->wm->col->data,
75                                         (win->wm->row->prev ? win->wm->row->prev->data : NULL),
76                                         win->wm->row->data, win,
77                                         (win->wm->row->next ? win->wm->row->next->data : NULL),
78                                         win->h, col->focus == win, wm_focus == win);
79                 }
80         }
81 }
82
83 static void arrange(list_t *cols)
84 {
85         int  x=0,  y=0; // Current window top-left position
86         int tx=0, ty=0; // Total x/y size
87         int mx=0, my=0; // Maximum x/y size (screen size)
88
89
90         /* Scale horizontally */
91         mx = wm_root->w - (list_length(wm_cols)+1)*MARGIN;
92         for (list_t *lx = cols; lx; lx = lx->next)
93                 tx += ((col_t*)lx->data)->width;
94         for (list_t *lx = cols; lx; lx = lx->next)
95                 ((col_t*)lx->data)->width *= (float)mx / tx;
96
97         /* Scale each column vertically */
98         for (list_t *lx = cols; lx; lx = lx->next) {
99                 col_t *col = lx->data;
100                 ty = 0;
101                 for (list_t *ly = col->rows; ly; ly = ly->next)
102                         ty += ((win_t*)ly->data)->h;
103                 y = 0;
104                 my = wm_root->h - (list_length(col->rows)+1)*MARGIN;
105                 for (list_t *ly = col->rows; ly; ly = ly->next) {
106                         win_t *win = ly->data;
107                         sys_move(win, x+MARGIN, y+MARGIN,
108                                 col->width,
109                                 win->h * ((float)my / ty));
110                         y += win->h + MARGIN;
111                 }
112                 x += col->width + MARGIN;
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                         arrange(wm_cols);
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                         arrange(wm_cols);
193                 }
194         }
195         print_txt(wm_cols);
196 }
197
198 static void shift_focus(win_t *win, int col, int row)
199 {
200         printf("shift_focus: %p - %+d,%+d\n", win, col, row);
201         list_t *node  = NULL;
202         if (row != 0) {
203                 if (row < 0) node = win->wm->row->prev;
204                 if (row > 0) node = win->wm->row->next;
205         } else {
206                 if (col < 0) node = win->wm->col->prev;
207                 if (col > 0) node = win->wm->col->next;
208                 if (node) {
209                         col_t *col = node->data;
210                         node = col->focus->wm->row;
211                 }
212         }
213         if (node)
214                 set_focus(node->data);
215 }
216
217 /* Window management functions */
218 int wm_handle_key(win_t *win, Key_t key, mod_t mod, ptr_t ptr)
219 {
220         if (!win || win == wm_root) return 0;
221         //printf("wm_handle_key: %p - %x %x\n", win, key, mod);
222
223         /* Raise */
224         if (key == key_f2)
225                 return set_focus(win), 1;
226         if (key == key_f4)
227                 return sys_raise(win), 1;
228         if (key == key_f1 && mod.MODKEY)
229                 sys_raise(win);
230         if (key == key_f12 && mod.MODKEY)
231                 print_txt(wm_cols);
232         if (key_mouse0 <= key && key <= key_mouse7)
233                 sys_raise(win);
234
235         /* Key commands */
236         if (mod.MODKEY && mod.shift) {
237                 switch (key) {
238                 case 'h': return shift_window(win,-1, 0), 1;
239                 case 'j': return shift_window(win, 0,+1), 1;
240                 case 'k': return shift_window(win, 0,-1), 1;
241                 case 'l': return shift_window(win,+1, 0), 1;
242                 default: break;
243                 }
244         }
245         else if (mod.MODKEY) {
246                 switch (key) {
247                 case 'h': return shift_focus(win,-1, 0), 1;
248                 case 'j': return shift_focus(win, 0,+1), 1;
249                 case 'k': return shift_focus(win, 0,-1), 1;
250                 case 'l': return shift_focus(win,+1, 0), 1;
251                 default: break;
252                 }
253         }
254
255         /* Mouse movement */
256         if (key_mouse0 <= key && key <= key_mouse7 && mod.up)
257                 return set_mode(none,win,ptr), 1;
258         else if (key == key_mouse1 && mod.MODKEY)
259                 return set_mode(move,win,ptr), 1;
260         else if (key == key_mouse3 && mod.MODKEY)
261                 return set_mode(resize,win,ptr), 1;
262
263         /* Focus change */
264         if (key == key_enter)
265                 set_focus(win);
266
267         return 0;
268 }
269
270 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
271 {
272         //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
273         //              cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
274
275         if (move_mode == none)
276                 return 0;
277
278         /* Tiling */
279         int dx = ptr.rx - move_prev.rx;
280         int dy = ptr.ry - move_prev.ry;
281         move_prev = ptr;
282         if (move_mode == resize) {
283                 list_t *row   = move_win->wm->row;
284                 list_t *col   = move_win->wm->col;
285                 list_t *lower = row->next;
286                 list_t *right = col->next;
287                 if (lower) {
288                         ((win_t*)row->data)->h       += dy;
289                         ((win_t*)lower->data)->h     -= dy;
290                 }
291                 if (right) {
292                         ((col_t*)col->data)->width   += dx;
293                         ((col_t*)right->data)->width -= dx;
294                 }
295                 arrange(wm_cols);
296         }
297
298         /* Floating */
299         //win_t *mwin = move_win;
300         //int dx = ptr.rx - move_prev.rx;
301         //int dy = ptr.ry - move_prev.ry;
302         //move_prev = ptr;
303         //if (move_mode == move)
304         //      sys_move(mwin, mwin->x+dx, mwin->y+dy, mwin->w, mwin->h);
305         //else if (move_mode == resize)
306         //      sys_move(mwin, mwin->x, mwin->y, mwin->w+dx, mwin->h+dy);
307
308         return 0;
309 }
310
311 void wm_insert(win_t *win)
312 {
313         printf("wm_insert: %p\n", win);
314         print_txt(wm_cols);
315
316         /* Initialize window */
317         win->wm = new0(win_wm_t);
318         sys_watch(win, key_enter, MOD());
319
320         /* Add to screen */
321         list_t *lcol = wm_focus && wm_focus->wm ?
322                 wm_focus->wm->col : wm_cols;
323         put_window(win, lcol);
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         else
339                 sys_focus(wm_root);
340         arrange(wm_cols);
341         print_txt(wm_cols);
342 }
343
344 void wm_init(win_t *root)
345 {
346         printf("wm_init: %p\n", root);
347         wm_root = root;
348         sys_watch(root, key_f1,     MOD(.MODKEY=1));
349         sys_watch(root, key_f12,    MOD(.MODKEY=1));
350         sys_watch(root, key_mouse1, MOD(.MODKEY=1));
351         sys_watch(root, key_mouse3, MOD(.MODKEY=1));
352         sys_watch(root, key_enter,  MOD());
353         Key_t keys[] = {'h', 'j', 'k', 'l'};
354         for (int i = 0; i < countof(keys); i++) {
355                 sys_watch(root, keys[i], MOD(.MODKEY=1));
356                 sys_watch(root, keys[i], MOD(.MODKEY=1,.shift=1));
357         }
358 }