-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkspace.c
429 lines (389 loc) · 13.1 KB
/
workspace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include <stdlib.h>
#include "venowm.h"
#include "workspace.h"
#include "split.h"
#include "window.h"
#include "backend.h"
workspace_t *workspace_new(backend_t *be){
workspace_t *ws = malloc(sizeof(*ws));
if(!ws) return NULL;
ws->windows = kh_init(wswl);
if(!ws) goto cu_malloc;
// no hidden windows yet
ws->hidden_first = NULL;
ws->hidden_last = NULL;
int err;
INIT_PTR(ws->roots, ws->roots_size, ws->nroots, 8, err);
if(err) goto cu_windows;
ws->be = be;
return ws;
cu_windows:
kh_destroy(wswl, ws->windows);
cu_malloc:
free(ws);
return NULL;
}
static void hidden_prepend(workspace_t *ws, ws_win_info_t *info){
// if list is empty, info becomes first and last
if(ws->hidden_first == NULL){
ws->hidden_first = info;
ws->hidden_last = info;
info->prev = NULL;
info->next = NULL;
return;
}
// otherwise just prepend
info->next = ws->hidden_first;
ws->hidden_first->prev = info;
info->prev = NULL;
ws->hidden_first = info;
}
static void hidden_append(workspace_t *ws, ws_win_info_t *info){
// if list is empty, info becomes first and last
if(ws->hidden_last == NULL){
ws->hidden_first = info;
ws->hidden_last = info;
info->prev = NULL;
info->next = NULL;
return;
}
// otherwise just append
info->prev = ws->hidden_last;
ws->hidden_last->next = info;
info->next = NULL;
ws->hidden_last = info;
}
static ws_win_info_t *hidden_pop_first(workspace_t *ws){
// check if list is empty
if(ws->hidden_first == NULL) return NULL;
ws_win_info_t *out = ws->hidden_first;
// check if there is only one element
if(ws->hidden_first == ws->hidden_last){
ws->hidden_first = NULL;
ws->hidden_last = NULL;
}else{
out->next->prev = NULL;
ws->hidden_first = out->next;
}
out->prev = NULL;
out->next = NULL;
return out;
}
static ws_win_info_t *hidden_pop_last(workspace_t *ws){
// check if list is empty
if(ws->hidden_last == NULL) return NULL;
ws_win_info_t *out = ws->hidden_last;
// check if there is only one element
if(ws->hidden_first == ws->hidden_last){
ws->hidden_first = NULL;
ws->hidden_last = NULL;
}else{
out->prev->next = NULL;
ws->hidden_last = out->prev;
}
out->prev = NULL;
out->next = NULL;
return out;
}
static void hidden_remove(workspace_t *ws, ws_win_info_t *info){
// was it the first element?
if(ws->hidden_first == info)
ws->hidden_first = info->next;
// was it the last element?
if(ws->hidden_last == info)
ws->hidden_last = info->prev;
// had prev?
if(info->prev)
info->prev->next = info->next;
// had next?
if(info->next)
info->next->prev = info->prev;
info->prev = NULL;
info->next = NULL;
}
// frees all of its roots, downrefs all of its windows
void workspace_free(workspace_t *ws){
// iterate through all windows via hashmap
khiter_t k;
for (k = kh_begin(ws->windows); k != kh_end(ws->windows); ++k){
if (kh_exist(ws->windows, k)){
ws_win_info_t *info = kh_value(ws->windows, k);
// remove from hashmap
kh_del(wswl, ws->windows, k);
// remove from frame
workspace_remove_window_from_frame(ws, info->frame, false);
// remove from hidden list
hidden_remove(ws, info);
// no more references from this workspace
window_ref_down(info->window);
free(info);
}
}
kh_destroy(wswl, ws->windows);
// now free all of the ws->nroots
for(size_t i = 0; i < ws->nroots; i++){
split_free(ws->roots[i]);
}
FREE_PTR(ws->roots, ws->roots_size, ws->nroots);
free(ws);
}
static void redraw_frame(split_t *frame, screen_t *screen,
float t, float b, float l, float r){
// pull out screen geometry
int32_t x, y;
uint32_t w, h;
be_screen_get_geometry(screen->be_screen, &x, &y, &w, &h);
// build window geometry
// TODO: decide how to do the offsets to avoid skipping/overlapping pixels
int32_t xmin = x + frac_of(l, (int)w);
int32_t xmax = x + frac_of(r, (int)w);
int32_t ymin = y + frac_of(t, (int)h);
int32_t ymax = y + frac_of(b, (int)h);
uint32_t wout = xmax - xmin;
uint32_t hout = ymax - ymin;
// make window visible and set the geometry
be_window_t *be_window = frame->win_info->window->be_window;
be_window_geometry(be_window, xmin, ymin, wout, hout);
logmsg("x,y = %d,%d w,h = %u,%u\n", xmin, ymin, wout, hout);
be_window_show(be_window, screen->be_screen);
}
static void draw_window(ws_win_info_t *info, split_t *frame){
// "draw window in NULL" -> noop
if(!frame) return;
// "draw NULL in frame" -> erase win_info in that frame
frame->win_info = info;
if(!info) return;
info->frame = frame;
// don't actually redraw the window unless it is on screen
if(!frame->screen) return;
// get the geometry of the window
sides_t sides = get_sides(frame);
float t = sides.t, b = sides.b, l = sides.l, r = sides.r;
// draw the window
redraw_frame(frame, frame->screen, t, b, l, r);
}
void workspace_add_window(workspace_t *ws, window_t *window, bool map_now){
window_ref_up(window);
// allocate/init win_info struct
ws_win_info_t *info = malloc(sizeof(*info));
if(!info){
window_ref_down(window);
return;
}
*info = (ws_win_info_t){.window = window};
// pack info into hash table
khint_t k;
int ret;
// get index
k = kh_put(wswl, ws->windows, window, &ret);
if(ret < 0){
window_ref_down(window);
free(info);
return;
}
// write to index
kh_value(ws->windows, k) = info;
if(map_now){
// hide whatever window is currently in the focused frame
workspace_remove_window_from_frame(ws, ws->focus, false);
// draw this window
draw_window(info, ws->focus);
// give the window focus
workspace_focus_frame(ws, ws->focus);
}else{
// append window to hidden windows
hidden_append(ws, info);
}
}
// removes a window from the workspace and downrefs it
void workspace_remove_window(workspace_t *ws, window_t *window){
// get backreferences to window via win_info struct
khiter_t k;
k = kh_get(wswl, ws->windows, window);
if(k == kh_end(ws->windows)){
// this window isn't part of this workspace
return;
}
ws_win_info_t *info = kh_value(ws->windows, k);
kh_del(wswl, ws->windows, k);
split_t *frame = info->frame;
// remove from frame
workspace_remove_window_from_frame(ws, info->frame, false);
// remove from hidden list
hidden_remove(ws, info);
// replace with another window
workspace_next_hidden_win_at(ws, frame);
// no more references from this workspace
window_ref_down(info->window);
free(info);
}
void workspace_remove_window_from_frame(workspace_t *ws, split_t *split,
bool prepend_old_window){
if(!split) return;
ws_win_info_t *info = split->win_info;
if(!info) return;
// hide window if workspace is active
if(g_workspace == ws){
be_window_hide(info->window->be_window);
}
split->win_info = NULL;
info->frame = NULL;
if(prepend_old_window)
hidden_prepend(ws, info);
else
hidden_append(ws, info);
}
static int hide_cb(split_t *split, void *data,
float t, float b, float l, float r){
(void)data; (void)t; (void)b; (void)l; (void)r;
// split is no longer associated with a screen
split->screen = NULL;
// that's all we do for non-leaves
if(!split->isleaf) return 0;
// do nothing if this leaf has no window
if(!split->win_info) return 0;
// hide window
be_window_hide(split->win_info->window->be_window);
return 0;
}
// unmap all windows in workspace
void workspace_hide(workspace_t *ws){
for(size_t i = 0; i < ws->nroots; i++){
split_do_at_each(ws->roots[i], hide_cb, NULL);
}
}
static int restore_cb(split_t *split, void *data,
float t, float b, float l, float r){
// dereference screen
screen_t *screen = data;
// save screen
split->screen = screen;
// that's all we do for non-leaves
if(!split->isleaf) return 0;
// do nothing if this leaf has no window
if(!split->win_info) return 0;
// draw the window
redraw_frame(split, screen, t, b, l, r);
return 0;
}
static int pre_rm_root_cb(split_t *split, void *data,
float t, float b, float l, float r){
(void)t; (void)b; (void)l; (void)r;
// dereference workspace
workspace_t *ws = data;
// remove any window in the frame (and list it as hidden)
workspace_remove_window_from_frame(ws, split, false);
return 0;
}
// restore windows to their frames, and render to the existing screens
void workspace_restore(workspace_t *ws){
/* TODO: think of a better way to do weakly-persistent root-screen mappings
(this strategy is OK only in the trivial case (no changes) and
it is at least safe in more complex cases) */
// Step 1: too many roots?
while(ws->nroots > g_nscreens){
// pull out one root
split_t *root = ws->roots[ws->nroots - 1];
ws->nroots--;
// remove windows from frame and list them as hidden
split_do_at_each(root, pre_rm_root_cb, ws);
split_free(root);
}
/* if this is called during the screen_destroy handler, it's possible that
we are about to exit and there are no screens left. Stop here. */
if(!g_nscreens) return;
// Step 2: too few roots?
while(ws->nroots < g_nscreens){
split_t *newroot = split_new(NULL);
int err;
APPEND_PTR(ws->roots, ws->roots_size, ws->nroots, newroot, err);
if(err){
logmsg("no memory to restore workspace\n");
// just don't draw on that screen I guess
break;
}
}
// Step 3: now map everything in place
for(size_t i = 0; i < ws->nroots; i++){
split_do_at_each(ws->roots[i], restore_cb, g_screens[i]);
}
// TODO: don't reset the focus frame all the time
ws->focus = ws->roots[0];
while(!ws->focus->isleaf) ws->focus = ws->focus->frames[0];
}
// trigger workspace to update window focus
void workspace_focus_frame(workspace_t *ws, split_t *frame){
// store this frame as the focus of the workspace
ws->focus = frame;
// focus on the window if this workspace is active
if(g_workspace == ws){
if(frame->win_info){
be_window_focus(frame->win_info->window->be_window);
}else{
be_unfocus_all(ws->be);
}
}
}
static void workspace_do_split(workspace_t *ws, split_t *split, bool vertical,
float fraction){
// first child inherits whatever was in the old split (window, focus)
int ret = split_do_split(split, vertical, fraction);
if(ret) return;
// redraw window if there was one
draw_window(split->frames[0]->win_info, split->frames[0]);
// second child gets a window if one was hidden
draw_window(hidden_pop_first(ws), split->frames[1]);
}
void workspace_vsplit(workspace_t *ws, split_t *split, float fraction){
workspace_do_split(ws, split, true, fraction);
}
void workspace_hsplit(workspace_t *ws, split_t *split, float fraction){
workspace_do_split(ws, split, false, fraction);
}
void workspace_remove_frame(workspace_t *ws, split_t *split){
// don't do this to root frames
if(!split->parent) return;
// remove any window
workspace_remove_window_from_frame(ws, split, false);
// remove the frame
split_t *remains = split_do_remove(split);
// fix focus if necessary
if(ws->focus == split){
workspace_focus_frame(ws, remains);
}
if(g_workspace == ws){
// rerender workspace (recursive redraw necessary in some cases)
logmsg("rerender!\n");
workspace_rerender(ws);
}
}
void workspace_swap_windows_from_frames(split_t *src, split_t *dst){
if(!src || !dst || src == dst) return;
ws_win_info_t *src_info = src->win_info;
ws_win_info_t *dst_info = dst->win_info;
// place the windows in their new frames
draw_window(src_info, dst);
draw_window(dst_info, src);
// dst might inherit focus from src
if(g_workspace->focus == src) g_workspace->focus = dst;
}
void workspace_next_hidden_win_at(workspace_t *ws, split_t *split){
if(!split) return;
ws_win_info_t *info = hidden_pop_first(ws);
if(!info) return;
// remove any window
workspace_remove_window_from_frame(ws, split, false);
// place new window
draw_window(info, split);
workspace_focus_frame(ws, split);
}
void workspace_prev_hidden_win_at(workspace_t *ws, split_t *split){
if(!split) return;
ws_win_info_t *info = hidden_pop_last(ws);
if(!info) return;
// remove any window
workspace_remove_window_from_frame(ws, split, true);
// place new window
draw_window(info, split);
workspace_focus_frame(ws, split);
}