2015-08-06 14:24:14 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <wlc/wlc.h>
|
|
|
|
#include "layout.h"
|
2015-08-18 20:22:52 +02:00
|
|
|
#include "log.h"
|
|
|
|
#include "list.h"
|
2015-08-19 00:44:50 +02:00
|
|
|
#include "config.h"
|
2015-08-11 05:47:14 +02:00
|
|
|
#include "container.h"
|
2015-08-10 22:31:23 +02:00
|
|
|
#include "workspace.h"
|
2015-08-18 20:22:52 +02:00
|
|
|
#include "focus.h"
|
2015-08-06 14:24:14 +02:00
|
|
|
|
2015-08-08 23:44:51 +02:00
|
|
|
swayc_t root_container;
|
2015-08-08 23:01:22 +02:00
|
|
|
|
2015-08-14 21:42:19 +02:00
|
|
|
void init_layout(void) {
|
|
|
|
root_container.type = C_ROOT;
|
|
|
|
root_container.layout = L_NONE;
|
|
|
|
root_container.children = create_list();
|
|
|
|
root_container.handle = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int index_child(swayc_t *parent, swayc_t *child) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < parent->children->length; ++i) {
|
|
|
|
if (parent->children->items[i] == child) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_child(swayc_t *parent, swayc_t *child) {
|
|
|
|
sway_log(L_DEBUG, "Adding %p (%d, %dx%d) to %p (%d, %dx%d)", child, child->type,
|
|
|
|
child->width, child->height, parent, parent->type, parent->width, parent->height);
|
|
|
|
list_add(parent->children, child);
|
|
|
|
child->parent = parent;
|
2015-08-20 14:06:22 +02:00
|
|
|
// set focus for this container
|
2015-08-19 07:20:31 +02:00
|
|
|
if (parent->children->length == 1) {
|
2015-08-20 01:15:17 +02:00
|
|
|
set_focused_container_for(parent, child);
|
2015-08-19 07:20:31 +02:00
|
|
|
}
|
2015-08-14 21:42:19 +02:00
|
|
|
}
|
|
|
|
|
2015-08-19 09:28:53 +02:00
|
|
|
void add_floating(swayc_t *ws, swayc_t *child) {
|
|
|
|
sway_log(L_DEBUG, "Adding %p (%d, %dx%d) to %p (%d, %dx%d)", child, child->type,
|
|
|
|
child->width, child->height, ws, ws->type, ws->width, ws->height);
|
|
|
|
list_add(ws->floating, child);
|
|
|
|
child->parent = ws;
|
|
|
|
child->is_floating = true;
|
|
|
|
if (!ws->focused) {
|
2015-08-20 01:15:17 +02:00
|
|
|
set_focused_container_for(ws, child);
|
2015-08-19 09:28:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-14 21:42:19 +02:00
|
|
|
swayc_t *add_sibling(swayc_t *sibling, swayc_t *child) {
|
|
|
|
swayc_t *parent = sibling->parent;
|
|
|
|
int i = index_child(parent, sibling);
|
|
|
|
if (i == parent->children->length) {
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
list_insert(parent->children, i+1, child);
|
|
|
|
child->parent = parent;
|
|
|
|
return child->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
swayc_t *replace_child(swayc_t *child, swayc_t *new_child) {
|
|
|
|
swayc_t *parent = child->parent;
|
|
|
|
if (parent == NULL) {
|
2015-08-09 15:23:10 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-14 21:42:19 +02:00
|
|
|
int i = index_child(parent, child);
|
|
|
|
parent->children->items[i] = new_child;
|
|
|
|
new_child->parent = child->parent;
|
|
|
|
|
|
|
|
if (child->parent->focused == child) {
|
2015-08-20 01:15:17 +02:00
|
|
|
set_focused_container_for(child->parent, new_child);
|
2015-08-14 21:42:19 +02:00
|
|
|
}
|
|
|
|
child->parent = NULL;
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
2015-08-18 11:46:14 +02:00
|
|
|
swayc_t *remove_child(swayc_t *child) {
|
2015-08-09 15:23:10 +02:00
|
|
|
int i;
|
2015-08-18 11:46:14 +02:00
|
|
|
swayc_t *parent = child->parent;
|
2015-08-17 07:38:34 +02:00
|
|
|
if (child->is_floating) {
|
2015-08-18 13:19:20 +02:00
|
|
|
// Special case for floating views
|
2015-08-17 07:38:34 +02:00
|
|
|
for (i = 0; i < parent->floating->length; ++i) {
|
|
|
|
if (parent->floating->items[i] == child) {
|
|
|
|
list_del(parent->floating, i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-08-19 09:28:53 +02:00
|
|
|
i = 0;
|
2015-08-17 17:18:06 +02:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < parent->children->length; ++i) {
|
|
|
|
if (parent->children->items[i] == child) {
|
|
|
|
list_del(parent->children, i);
|
|
|
|
break;
|
|
|
|
}
|
2015-08-09 15:23:10 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-20 14:06:22 +02:00
|
|
|
// Set focused to new container
|
2015-08-14 21:42:19 +02:00
|
|
|
if (parent->focused == child) {
|
2015-08-18 11:46:14 +02:00
|
|
|
if (parent->children->length > 0) {
|
2015-08-18 20:22:52 +02:00
|
|
|
set_focused_container_for(parent, parent->children->items[i?i-1:0]);
|
2015-08-18 11:46:14 +02:00
|
|
|
} else {
|
|
|
|
parent->focused = NULL;
|
|
|
|
}
|
2015-08-14 21:42:19 +02:00
|
|
|
}
|
|
|
|
return parent;
|
2015-08-09 15:23:10 +02:00
|
|
|
}
|
|
|
|
|
2015-08-14 21:42:19 +02:00
|
|
|
|
2015-08-09 00:17:08 +02:00
|
|
|
void arrange_windows(swayc_t *container, int width, int height) {
|
|
|
|
int i;
|
|
|
|
if (width == -1 || height == -1) {
|
|
|
|
sway_log(L_DEBUG, "Arranging layout for %p", container);
|
|
|
|
width = container->width;
|
|
|
|
height = container->height;
|
|
|
|
}
|
|
|
|
|
2015-08-11 05:47:14 +02:00
|
|
|
int x = 0, y = 0;
|
2015-08-09 00:34:33 +02:00
|
|
|
switch (container->type) {
|
|
|
|
case C_ROOT:
|
2015-08-09 00:17:08 +02:00
|
|
|
for (i = 0; i < container->children->length; ++i) {
|
2015-08-11 05:47:14 +02:00
|
|
|
swayc_t *child = container->children->items[i];
|
|
|
|
sway_log(L_DEBUG, "Arranging output at %d", x);
|
|
|
|
child->x = x;
|
|
|
|
child->y = y;
|
2015-08-11 05:54:23 +02:00
|
|
|
arrange_windows(child, -1, -1);
|
2015-08-15 23:13:08 +02:00
|
|
|
// Removed for now because wlc works with relative positions
|
|
|
|
// Addition can be reconsidered once wlc positions are changed
|
|
|
|
// x += child->width;
|
2015-08-09 00:17:08 +02:00
|
|
|
}
|
|
|
|
return;
|
2015-08-11 05:54:23 +02:00
|
|
|
case C_OUTPUT:
|
|
|
|
container->width = width;
|
|
|
|
container->height = height;
|
2015-08-15 23:13:08 +02:00
|
|
|
// These lines make x/y negative and result in stuff glitching out
|
|
|
|
// Their addition can be reconsidered once wlc positions are changed
|
|
|
|
// x -= container->x;
|
|
|
|
// y -= container->y;
|
|
|
|
for (i = 0; i < container->children->length; ++i) {
|
|
|
|
swayc_t *child = container->children->items[i];
|
2015-08-19 00:44:50 +02:00
|
|
|
child->x = x + container->gaps;
|
|
|
|
child->y = y + container->gaps;
|
|
|
|
child->width = width - container->gaps * 2;
|
|
|
|
child->height = height - container->gaps * 2;
|
|
|
|
sway_log(L_DEBUG, "Arranging workspace #%d at %d, %d", i, child->x, child->y);
|
2015-08-15 23:13:08 +02:00
|
|
|
arrange_windows(child, -1, -1);
|
|
|
|
}
|
|
|
|
return;
|
2015-08-09 00:34:33 +02:00
|
|
|
case C_VIEW:
|
2015-08-10 02:58:03 +02:00
|
|
|
{
|
|
|
|
struct wlc_geometry geometry = {
|
|
|
|
.origin = {
|
2015-08-20 08:46:06 +02:00
|
|
|
.x = container->x + container->gaps / 2,
|
|
|
|
.y = container->y + container->gaps / 2
|
2015-08-10 02:58:03 +02:00
|
|
|
},
|
|
|
|
.size = {
|
2015-08-20 08:46:06 +02:00
|
|
|
.w = width - container->gaps,
|
|
|
|
.h = height - container->gaps
|
2015-08-10 02:58:03 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if (wlc_view_get_state(container->handle) & WLC_BIT_FULLSCREEN) {
|
|
|
|
swayc_t *parent = container;
|
2015-08-14 21:44:35 +02:00
|
|
|
while (parent->type != C_OUTPUT) {
|
2015-08-10 02:58:03 +02:00
|
|
|
parent = parent->parent;
|
|
|
|
}
|
2015-08-19 00:44:50 +02:00
|
|
|
geometry.origin.x = 0;
|
|
|
|
geometry.origin.y = 0;
|
|
|
|
geometry.size.w = parent->width;
|
|
|
|
geometry.size.h = parent->height;
|
2015-08-18 23:24:01 +02:00
|
|
|
wlc_view_set_geometry(container->handle, 0, &geometry);
|
2015-08-10 02:58:03 +02:00
|
|
|
wlc_view_bring_to_front(container->handle);
|
|
|
|
} else {
|
2015-08-18 23:24:01 +02:00
|
|
|
wlc_view_set_geometry(container->handle, 0, &geometry);
|
2015-08-10 02:58:03 +02:00
|
|
|
container->width = width;
|
|
|
|
container->height = height;
|
2015-08-09 00:17:08 +02:00
|
|
|
}
|
2015-08-10 02:58:03 +02:00
|
|
|
sway_log(L_DEBUG, "Set view to %d x %d @ %d, %d", geometry.size.w, geometry.size.h,
|
|
|
|
geometry.origin.x, geometry.origin.y);
|
2015-08-10 02:49:19 +02:00
|
|
|
}
|
2015-08-09 00:17:08 +02:00
|
|
|
return;
|
2015-08-09 00:34:33 +02:00
|
|
|
default:
|
|
|
|
container->width = width;
|
|
|
|
container->height = height;
|
|
|
|
break;
|
2015-08-09 00:17:08 +02:00
|
|
|
}
|
|
|
|
|
2015-08-20 13:47:36 +02:00
|
|
|
x = y = 0;
|
|
|
|
double scale = 0;
|
2015-08-09 00:17:08 +02:00
|
|
|
switch (container->layout) {
|
|
|
|
case L_HORIZ:
|
|
|
|
default:
|
2015-08-20 14:06:22 +02:00
|
|
|
// Calculate total width
|
2015-08-09 00:17:08 +02:00
|
|
|
for (i = 0; i < container->children->length; ++i) {
|
2015-08-20 13:47:36 +02:00
|
|
|
int *old_width = &((swayc_t *)container->children->items[i])->width;
|
2015-08-20 13:57:01 +02:00
|
|
|
if (*old_width <= 0) {
|
2015-08-20 13:47:36 +02:00
|
|
|
if (container->children->length > 1) {
|
|
|
|
*old_width = width / (container->children->length - 1);
|
|
|
|
} else {
|
|
|
|
*old_width = width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
scale += *old_width;
|
|
|
|
}
|
2015-08-20 14:06:22 +02:00
|
|
|
// Resize windows
|
2015-08-20 13:47:36 +02:00
|
|
|
if (scale > 0.1) {
|
|
|
|
scale = width / scale;
|
|
|
|
sway_log(L_DEBUG, "Arranging %p horizontally", container);
|
|
|
|
for (i = 0; i < container->children->length; ++i) {
|
|
|
|
swayc_t *child = container->children->items[i];
|
|
|
|
sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %d by %f)", child, child->type, width, scale);
|
|
|
|
child->x = x + container->x;
|
|
|
|
child->y = y + container->y;
|
|
|
|
arrange_windows(child, child->width * scale, height);
|
|
|
|
x += child->width;
|
|
|
|
}
|
2015-08-09 00:17:08 +02:00
|
|
|
}
|
|
|
|
break;
|
2015-08-09 00:34:33 +02:00
|
|
|
case L_VERT:
|
2015-08-20 14:06:22 +02:00
|
|
|
// Calculate total height
|
2015-08-09 00:34:33 +02:00
|
|
|
for (i = 0; i < container->children->length; ++i) {
|
2015-08-20 13:47:36 +02:00
|
|
|
int *old_height = &((swayc_t *)container->children->items[i])->height;
|
2015-08-20 13:57:01 +02:00
|
|
|
if (*old_height <= 0) {
|
2015-08-20 13:47:36 +02:00
|
|
|
if (container->children->length > 1) {
|
|
|
|
*old_height = height / (container->children->length - 1);
|
|
|
|
} else {
|
|
|
|
*old_height = height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
scale += *old_height;
|
|
|
|
}
|
2015-08-20 14:06:22 +02:00
|
|
|
// Resize
|
2015-08-20 13:47:36 +02:00
|
|
|
if (scale > 0.1) {
|
|
|
|
scale = height / scale;
|
|
|
|
sway_log(L_DEBUG, "Arranging %p vertically", container);
|
|
|
|
for (i = 0; i < container->children->length; ++i) {
|
|
|
|
swayc_t *child = container->children->items[i];
|
|
|
|
sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %d by %f)", child, child->type, height, scale);
|
|
|
|
child->x = x + container->x;
|
|
|
|
child->y = y + container->y;
|
|
|
|
arrange_windows(child, width, child->height * scale);
|
|
|
|
y += child->height;
|
|
|
|
}
|
2015-08-09 00:34:33 +02:00
|
|
|
}
|
|
|
|
break;
|
2015-08-09 00:17:08 +02:00
|
|
|
}
|
2015-08-17 07:38:34 +02:00
|
|
|
|
|
|
|
// Arrage floating layouts for workspaces last
|
|
|
|
if (container->type == C_WORKSPACE) {
|
|
|
|
for (i = 0; i < container->floating->length; ++i) {
|
2015-08-18 09:28:44 +02:00
|
|
|
swayc_t *view = container->floating->items[i];
|
2015-08-18 20:22:52 +02:00
|
|
|
if (view->type == C_VIEW) {
|
|
|
|
// Set the geometry
|
|
|
|
struct wlc_geometry geometry = {
|
|
|
|
.origin = {
|
|
|
|
.x = view->x,
|
|
|
|
.y = view->y
|
|
|
|
},
|
|
|
|
.size = {
|
|
|
|
.w = view->width,
|
|
|
|
.h = view->height
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (wlc_view_get_state(view->handle) & WLC_BIT_FULLSCREEN) {
|
|
|
|
swayc_t *parent = view;
|
|
|
|
while (parent->type != C_OUTPUT) {
|
|
|
|
parent = parent->parent;
|
|
|
|
}
|
|
|
|
geometry.origin.x = 0;
|
|
|
|
geometry.origin.y = 0;
|
|
|
|
geometry.size.w = parent->width;
|
|
|
|
geometry.size.h = parent->height;
|
2015-08-18 23:24:01 +02:00
|
|
|
wlc_view_set_geometry(view->handle, 0, &geometry);
|
2015-08-18 20:22:52 +02:00
|
|
|
wlc_view_bring_to_front(view->handle);
|
|
|
|
} else {
|
2015-08-18 23:24:01 +02:00
|
|
|
wlc_view_set_geometry(view->handle, 0, &geometry);
|
2015-08-18 20:22:52 +02:00
|
|
|
// Bring the views to the front in order of the list, the list
|
|
|
|
// will be kept up to date so that more recently focused views
|
|
|
|
// have higher indexes
|
|
|
|
// This is conditional on there not being a fullscreen view in the workspace
|
|
|
|
if (!container->focused
|
|
|
|
|| !(wlc_view_get_state(container->focused->handle) & WLC_BIT_FULLSCREEN)) {
|
|
|
|
wlc_view_bring_to_front(view->handle);
|
|
|
|
}
|
2015-08-17 07:38:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-15 20:55:06 +02:00
|
|
|
layout_log(&root_container, 0);
|
2015-08-08 23:01:22 +02:00
|
|
|
}
|
2015-08-06 14:40:16 +02:00
|
|
|
|
2015-08-08 23:44:51 +02:00
|
|
|
swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent) {
|
|
|
|
if (parent->children == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-17 07:38:34 +02:00
|
|
|
|
|
|
|
// Search for floating workspaces
|
2015-08-08 23:44:51 +02:00
|
|
|
int i;
|
2015-08-17 07:38:34 +02:00
|
|
|
if (parent->type == C_WORKSPACE) {
|
|
|
|
for (i = 0; i < parent->floating->length; ++i) {
|
|
|
|
swayc_t *child = parent->floating->items[i];
|
|
|
|
if (child->handle == handle) {
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-08 23:44:51 +02:00
|
|
|
for (i = 0; i < parent->children->length; ++i) {
|
|
|
|
swayc_t *child = parent->children->items[i];
|
|
|
|
if (child->handle == handle) {
|
|
|
|
return child;
|
2015-08-08 23:01:22 +02:00
|
|
|
} else {
|
2015-08-08 23:44:51 +02:00
|
|
|
swayc_t *res;
|
|
|
|
if ((res = get_swayc_for_handle(handle, child))) {
|
2015-08-08 23:01:22 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-20 05:22:15 +02:00
|
|
|
swayc_t *get_swayc_in_direction(swayc_t *container, enum movement_direction dir) {
|
|
|
|
swayc_t *parent = container->parent;
|
|
|
|
|
|
|
|
if (dir == MOVE_PARENT) {
|
|
|
|
if (parent->type == C_OUTPUT) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (true) {
|
|
|
|
// Test if we can even make a difference here
|
|
|
|
bool can_move = false;
|
|
|
|
int diff = 0;
|
|
|
|
if (dir == MOVE_LEFT || dir == MOVE_RIGHT) {
|
|
|
|
if (parent->layout == L_HORIZ || parent->type == C_ROOT) {
|
|
|
|
can_move = true;
|
|
|
|
diff = dir == MOVE_LEFT ? -1 : 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (parent->layout == L_VERT) {
|
|
|
|
can_move = true;
|
|
|
|
diff = dir == MOVE_UP ? -1 : 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (can_move) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < parent->children->length; ++i) {
|
|
|
|
swayc_t *child = parent->children->items[i];
|
|
|
|
if (child == container) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int desired = i + diff;
|
|
|
|
if (desired < 0 || desired >= parent->children->length) {
|
|
|
|
can_move = false;
|
|
|
|
} else {
|
|
|
|
return parent->children->items[desired];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!can_move) {
|
|
|
|
container = parent;
|
|
|
|
parent = parent->parent;
|
|
|
|
if (!parent) {
|
|
|
|
// Nothing we can do
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-20 18:30:32 +02:00
|
|
|
|
|
|
|
void recursive_resize(swayc_t *container, double amount, enum movement_direction dir) {
|
|
|
|
int i;
|
|
|
|
bool layout_match = true;
|
|
|
|
if (dir == MOVE_LEFT) {
|
|
|
|
container->x += (int) amount;
|
|
|
|
container->width += (int) amount;
|
|
|
|
layout_match = container->layout == L_HORIZ;
|
2015-08-20 18:42:01 +02:00
|
|
|
} else if (dir == MOVE_RIGHT) {
|
2015-08-20 18:30:32 +02:00
|
|
|
container->width += (int) amount;
|
|
|
|
layout_match = container->layout == L_HORIZ;
|
2015-08-20 18:42:01 +02:00
|
|
|
} else if (dir == MOVE_UP) {
|
2015-08-20 18:30:32 +02:00
|
|
|
container->y += (int) amount;
|
|
|
|
container->height += (int) amount;
|
|
|
|
layout_match = container->layout == L_VERT;
|
2015-08-20 18:42:01 +02:00
|
|
|
} else if (dir == MOVE_DOWN) {
|
2015-08-20 18:30:32 +02:00
|
|
|
container->height += (int) amount;
|
|
|
|
layout_match = container->layout == L_VERT;
|
|
|
|
}
|
|
|
|
if (container->type == C_VIEW) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (layout_match) {
|
|
|
|
for (i = 0; i < container->children->length; i++) {
|
|
|
|
recursive_resize(container->children->items[i], amount/container->children->length, dir);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < container->children->length; i++) {
|
|
|
|
recursive_resize(container->children->items[i], amount, dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|