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