-
Notifications
You must be signed in to change notification settings - Fork 21
/
layout.c
385 lines (311 loc) · 9.61 KB
/
layout.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
/* velox: layout.c
*
* Copyright (c) 2014 Michael Forney
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "layout.h"
#include "config.h"
#include "screen.h"
#include "velox.h"
#include "window.h"
#include <math.h>
#include <stdlib.h>
#include <swc.h>
#include <sys/param.h>
const unsigned master_max = 16;
struct layout_impl {
void (*begin)(struct layout *layout, const struct swc_rectangle *area, unsigned num_windows);
void (*arrange)(struct layout *layout, struct window *window);
};
struct col {
struct swc_rectangle tile;
const struct swc_rectangle *area;
unsigned num_rows, row_index;
void (*next)(struct col *col);
};
struct grid {
struct col col;
unsigned num_windows, num_cols, col_index;
};
struct tall_layout {
struct layout base;
unsigned num_masters, num_columns, master_size;
struct grid grid;
struct swc_rectangle grid_area;
};
struct grid_layout {
struct layout base;
struct grid grid;
};
static void
tile(struct col *col, struct window *window)
{
col->tile.y = col->area->y + border_width + col->row_index * col->area->height / col->num_rows;
swc_window_set_geometry(window->swc, &col->tile);
if (++col->row_index < col->num_rows)
return;
col->row_index = 0;
col->next(col);
}
static void
grid_next(struct col *col)
{
struct grid *grid = wl_container_of(col, grid, col);
++grid->col_index;
if (grid->col_index == grid->num_windows % grid->num_cols) {
--col->num_rows;
col->tile.height = col->area->height / col->num_rows - 2 * border_width;
}
col->tile.x = col->area->x + border_width + col->area->width * grid->col_index / grid->num_cols;
}
static void
grid(struct grid *grid, const struct swc_rectangle *area, unsigned num_windows, unsigned num_cols)
{
if (num_windows == 0)
return;
grid->num_windows = num_windows;
grid->num_cols = num_cols;
grid->col.num_rows = num_windows / num_cols + (num_windows % num_cols > 0);
grid->col.row_index = 0;
grid->col_index = 0;
grid->col.tile.x = area->x + border_width;
grid->col.tile.width = area->width / num_cols - 2 * border_width;
grid->col.tile.height = area->height / grid->col.num_rows - 2 * border_width;
grid->col.area = area;
grid->col.next = &grid_next;
}
/* Tall layout */
static void
tall_next_col(struct col *col)
{
struct tall_layout *layout = wl_container_of(col, layout, grid.col);
grid(&layout->grid, &layout->grid_area, layout->grid.num_windows,
MIN(layout->grid.num_windows, layout->num_columns));
}
static void
tall_begin(struct layout *base, const struct swc_rectangle *area, unsigned num_windows)
{
struct tall_layout *layout = (void *)base;
unsigned master_width;
layout->grid.col.num_rows = MIN(num_windows, layout->num_masters);
if (layout->grid.col.num_rows == 0)
return;
layout->grid.num_windows = num_windows - layout->grid.col.num_rows;
if (num_windows > layout->num_masters) {
master_width = area->width * layout->master_size / master_max;
layout->grid_area.x = area->x + master_width;
layout->grid_area.y = area->y;
layout->grid_area.height = area->height;
layout->grid_area.width = area->width - master_width;
} else
master_width = area->width;
layout->grid.col.tile = (struct swc_rectangle){
.x = area->x + border_width,
.width = master_width - 2 * border_width,
.height = area->height / layout->grid.col.num_rows - 2 * border_width,
};
layout->grid.col.row_index = 0;
layout->grid.col.area = area;
layout->grid.col.next = &tall_next_col;
}
static void
tall_arrange(struct layout *base, struct window *window)
{
struct tall_layout *layout = (void *)base;
tile(&layout->grid.col, window);
}
static const struct layout_impl tall_impl = {
.begin = &tall_begin,
.arrange = &tall_arrange,
};
static struct tall_layout *
tall_layout(struct layout *base)
{
if (base->impl != &tall_impl)
return NULL;
return (void *)base;
}
struct layout *
tall_layout_new(void)
{
struct tall_layout *layout;
if (!(layout = malloc(sizeof(*layout))))
goto error0;
layout->base.impl = &tall_impl;
layout->master_size = master_max / 2;
layout->num_masters = 1;
layout->num_columns = 1;
return &layout->base;
error0:
return NULL;
}
/* Grid layout */
static void
grid_begin(struct layout *base, const struct swc_rectangle *area, unsigned num_windows)
{
struct grid_layout *layout = (void *)base;
grid(&layout->grid, area, num_windows, ceil(sqrt(num_windows)));
}
static void
grid_arrange(struct layout *base, struct window *window)
{
struct grid_layout *layout = (void *)base;
tile(&layout->grid.col, window);
}
static const struct layout_impl grid_impl = {
.begin = &grid_begin,
.arrange = &grid_arrange,
};
struct layout *
grid_layout_new(void)
{
struct grid_layout *layout;
if (!(layout = malloc(sizeof(*layout))))
goto error0;
layout->base.impl = &grid_impl;
return &layout->base;
error0:
return NULL;
}
static void
increase_master_size(struct config_node *node, const struct variant *v)
{
struct tall_layout *layout;
if (!(layout = tall_layout(velox.active_screen->layout[TILE])))
return;
layout->master_size = MIN(layout->master_size + 1, master_max);
arrange();
}
static void
decrease_master_size(struct config_node *node, const struct variant *v)
{
struct tall_layout *layout;
if (!(layout = tall_layout(velox.active_screen->layout[TILE])))
return;
layout->master_size = MAX(layout->master_size - 1, 0);
arrange();
}
static void
increase_num_masters(struct config_node *node, const struct variant *v)
{
struct tall_layout *layout;
if (!(layout = tall_layout(velox.active_screen->layout[TILE])))
return;
++layout->num_masters;
arrange();
}
static void
decrease_num_masters(struct config_node *node, const struct variant *v)
{
struct tall_layout *layout;
if (!(layout = tall_layout(velox.active_screen->layout[TILE])))
return;
layout->num_masters = MAX(layout->num_masters - 1, 1);
arrange();
}
static void
increase_num_columns(struct config_node *node, const struct variant *v)
{
struct tall_layout *layout;
if (!(layout = tall_layout(velox.active_screen->layout[TILE])))
return;
++layout->num_columns;
arrange();
}
static void
decrease_num_columns(struct config_node *node, const struct variant *v)
{
struct tall_layout *layout;
if (!(layout = tall_layout(velox.active_screen->layout[TILE])))
return;
layout->num_columns = MAX(layout->num_columns - 1, 1);
arrange();
}
static struct {
struct {
struct config_node group,
increase_master_size, decrease_master_size,
increase_num_masters, decrease_num_masters,
increase_num_columns, decrease_num_columns;
} config;
} tall = {
.config = {
{ "tall", CONFIG_NODE_TYPE_GROUP },
{ "increase_master_size", CONFIG_NODE_TYPE_ACTION, { .action.run = increase_master_size } },
{ "decrease_master_size", CONFIG_NODE_TYPE_ACTION, { .action.run = decrease_master_size } },
{ "increase_num_masters", CONFIG_NODE_TYPE_ACTION, { .action.run = increase_num_masters } },
{ "decrease_num_masters", CONFIG_NODE_TYPE_ACTION, { .action.run = decrease_num_masters } },
{ "increase_num_columns", CONFIG_NODE_TYPE_ACTION, { .action.run = increase_num_columns } },
{ "decrease_num_columns", CONFIG_NODE_TYPE_ACTION, { .action.run = decrease_num_columns } },
}
};
/* Stack layout */
static void
stack_begin(struct layout *layout, const struct swc_rectangle *area, unsigned num_windows)
{
}
static void
stack_arrange(struct layout *layout, struct window *window)
{
/* TODO: Place window on top of stack when swc adds support for this. */
}
static const struct layout_impl stack_impl = {
.begin = &stack_begin,
.arrange = &stack_arrange,
};
struct layout *
stack_layout_new(void)
{
struct layout *layout;
if (!(layout = malloc(sizeof(*layout))))
goto error0;
layout->impl = &stack_impl;
return layout;
error0:
return NULL;
}
void
layout_add_config_nodes(void)
{
wl_list_init(&tall.config.group.group);
wl_list_insert(&tall.config.group.group,
&tall.config.increase_master_size.link);
wl_list_insert(&tall.config.group.group,
&tall.config.decrease_master_size.link);
wl_list_insert(&tall.config.group.group,
&tall.config.increase_num_masters.link);
wl_list_insert(&tall.config.group.group,
&tall.config.decrease_num_masters.link);
wl_list_insert(&tall.config.group.group,
&tall.config.increase_num_columns.link);
wl_list_insert(&tall.config.group.group,
&tall.config.decrease_num_columns.link);
wl_list_insert(config_root, &tall.config.group.link);
}
void
layout_begin(struct layout *layout, const struct swc_rectangle *area, unsigned num_windows)
{
layout->impl->begin(layout, area, num_windows);
}
void
layout_arrange(struct layout *layout, struct window *window)
{
layout->impl->arrange(layout, window);
}