2018-04-28 11:26:14 +10:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <wlr/types/wlr_output.h>
|
|
|
|
#include <wlr/types/wlr_output_layout.h>
|
|
|
|
#include "sway/tree/arrange.h"
|
|
|
|
#include "sway/tree/container.h"
|
|
|
|
#include "sway/tree/layout.h"
|
|
|
|
#include "sway/output.h"
|
|
|
|
#include "sway/tree/workspace.h"
|
|
|
|
#include "sway/tree/view.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
struct sway_container root_container;
|
|
|
|
|
|
|
|
static void apply_horiz_layout(struct sway_container *parent) {
|
|
|
|
size_t num_children = parent->children->length;
|
|
|
|
if (!num_children) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-19 22:54:50 +10:00
|
|
|
size_t parent_offset = 0;
|
2018-06-06 19:19:30 +10:00
|
|
|
if (parent->parent->layout == L_TABBED) {
|
2018-05-22 08:27:42 +10:00
|
|
|
parent_offset = container_titlebar_height();
|
2018-06-06 19:19:30 +10:00
|
|
|
} else if (parent->parent->layout == L_STACKED) {
|
2018-06-03 16:35:06 +10:00
|
|
|
parent_offset = container_titlebar_height() *
|
|
|
|
parent->parent->children->length;
|
2018-05-19 22:54:50 +10:00
|
|
|
}
|
2018-06-06 19:19:30 +10:00
|
|
|
size_t parent_height = parent->height - parent_offset;
|
2018-05-19 22:54:50 +10:00
|
|
|
|
2018-04-28 11:26:14 +10:00
|
|
|
// Calculate total width of children
|
|
|
|
double total_width = 0;
|
|
|
|
for (size_t i = 0; i < num_children; ++i) {
|
|
|
|
struct sway_container *child = parent->children->items[i];
|
2018-06-06 19:19:30 +10:00
|
|
|
if (child->width <= 0) {
|
2018-04-28 11:26:14 +10:00
|
|
|
if (num_children > 1) {
|
2018-06-06 19:19:30 +10:00
|
|
|
child->width = parent->width / (num_children - 1);
|
2018-04-28 11:26:14 +10:00
|
|
|
} else {
|
2018-06-06 19:19:30 +10:00
|
|
|
child->width = parent->width;
|
2018-04-28 11:26:14 +10:00
|
|
|
}
|
|
|
|
}
|
2018-06-09 23:34:56 +10:00
|
|
|
remove_gaps(child);
|
2018-06-06 19:19:30 +10:00
|
|
|
total_width += child->width;
|
2018-04-28 11:26:14 +10:00
|
|
|
}
|
2018-06-06 19:19:30 +10:00
|
|
|
double scale = parent->width / total_width;
|
2018-04-28 11:26:14 +10:00
|
|
|
|
|
|
|
// Resize windows
|
2018-07-10 07:54:30 +10:00
|
|
|
wlr_log(WLR_DEBUG, "Arranging %p horizontally", parent);
|
2018-06-06 19:19:30 +10:00
|
|
|
double child_x = parent->x;
|
2018-04-28 11:26:14 +10:00
|
|
|
for (size_t i = 0; i < num_children; ++i) {
|
2018-06-03 16:35:06 +10:00
|
|
|
struct sway_container *child = parent->children->items[i];
|
2018-07-10 07:54:30 +10:00
|
|
|
wlr_log(WLR_DEBUG,
|
2018-04-28 11:26:14 +10:00
|
|
|
"Calculating arrangement for %p:%d (will scale %f by %f)",
|
2018-06-06 19:19:30 +10:00
|
|
|
child, child->type, child->width, scale);
|
|
|
|
child->x = child_x;
|
|
|
|
child->y = parent->y + parent_offset;
|
|
|
|
child->width = floor(child->width * scale);
|
|
|
|
child->height = parent_height;
|
|
|
|
child_x += child->width;
|
2018-06-03 16:35:06 +10:00
|
|
|
|
|
|
|
// Make last child use remaining width of parent
|
|
|
|
if (i == num_children - 1) {
|
2018-06-06 19:19:30 +10:00
|
|
|
child->width = parent->x + parent->width - child->x;
|
2018-06-03 16:35:06 +10:00
|
|
|
}
|
2018-06-09 23:34:56 +10:00
|
|
|
add_gaps(child);
|
2018-04-28 11:26:14 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void apply_vert_layout(struct sway_container *parent) {
|
|
|
|
size_t num_children = parent->children->length;
|
|
|
|
if (!num_children) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-19 22:54:50 +10:00
|
|
|
size_t parent_offset = 0;
|
2018-06-06 19:19:30 +10:00
|
|
|
if (parent->parent->layout == L_TABBED) {
|
2018-05-22 08:27:42 +10:00
|
|
|
parent_offset = container_titlebar_height();
|
2018-06-06 19:19:30 +10:00
|
|
|
} else if (parent->parent->layout == L_STACKED) {
|
2018-05-22 08:27:42 +10:00
|
|
|
parent_offset =
|
|
|
|
container_titlebar_height() * parent->parent->children->length;
|
2018-05-19 22:54:50 +10:00
|
|
|
}
|
2018-06-09 23:34:56 +10:00
|
|
|
size_t parent_height = parent->height + parent_offset;
|
2018-05-19 22:54:50 +10:00
|
|
|
|
2018-04-28 11:26:14 +10:00
|
|
|
// Calculate total height of children
|
|
|
|
double total_height = 0;
|
|
|
|
for (size_t i = 0; i < num_children; ++i) {
|
|
|
|
struct sway_container *child = parent->children->items[i];
|
2018-06-06 19:19:30 +10:00
|
|
|
if (child->height <= 0) {
|
2018-04-28 11:26:14 +10:00
|
|
|
if (num_children > 1) {
|
2018-06-06 19:19:30 +10:00
|
|
|
child->height = parent_height / (num_children - 1);
|
2018-04-28 11:26:14 +10:00
|
|
|
} else {
|
2018-06-06 19:19:30 +10:00
|
|
|
child->height = parent_height;
|
2018-04-28 11:26:14 +10:00
|
|
|
}
|
|
|
|
}
|
2018-06-09 23:34:56 +10:00
|
|
|
remove_gaps(child);
|
2018-06-06 19:19:30 +10:00
|
|
|
total_height += child->height;
|
2018-04-28 11:26:14 +10:00
|
|
|
}
|
2018-05-19 22:54:50 +10:00
|
|
|
double scale = parent_height / total_height;
|
2018-04-28 11:26:14 +10:00
|
|
|
|
|
|
|
// Resize
|
2018-07-10 07:54:30 +10:00
|
|
|
wlr_log(WLR_DEBUG, "Arranging %p vertically", parent);
|
2018-06-06 19:19:30 +10:00
|
|
|
double child_y = parent->y + parent_offset;
|
2018-04-28 11:26:14 +10:00
|
|
|
for (size_t i = 0; i < num_children; ++i) {
|
2018-06-03 16:35:06 +10:00
|
|
|
struct sway_container *child = parent->children->items[i];
|
2018-07-10 07:54:30 +10:00
|
|
|
wlr_log(WLR_DEBUG,
|
2018-04-28 11:26:14 +10:00
|
|
|
"Calculating arrangement for %p:%d (will scale %f by %f)",
|
2018-06-06 19:19:30 +10:00
|
|
|
child, child->type, child->height, scale);
|
|
|
|
child->x = parent->x;
|
|
|
|
child->y = child_y;
|
|
|
|
child->width = parent->width;
|
|
|
|
child->height = floor(child->height * scale);
|
|
|
|
child_y += child->height;
|
2018-06-03 16:35:06 +10:00
|
|
|
|
|
|
|
// Make last child use remaining height of parent
|
|
|
|
if (i == num_children - 1) {
|
2018-06-06 19:19:30 +10:00
|
|
|
child->height =
|
|
|
|
parent->y + parent_offset + parent_height - child->y;
|
2018-06-03 16:35:06 +10:00
|
|
|
}
|
2018-06-09 23:34:56 +10:00
|
|
|
add_gaps(child);
|
2018-04-28 11:26:14 +10:00
|
|
|
}
|
2018-05-19 22:54:50 +10:00
|
|
|
}
|
|
|
|
|
2018-05-24 17:38:31 +10:00
|
|
|
static void apply_tabbed_or_stacked_layout(struct sway_container *parent) {
|
2018-05-19 22:54:50 +10:00
|
|
|
if (!parent->children->length) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-24 17:38:31 +10:00
|
|
|
size_t parent_offset = 0;
|
2018-06-06 19:19:30 +10:00
|
|
|
if (parent->parent->layout == L_TABBED) {
|
2018-05-24 17:38:31 +10:00
|
|
|
parent_offset = container_titlebar_height();
|
2018-06-06 19:19:30 +10:00
|
|
|
} else if (parent->parent->layout == L_STACKED) {
|
2018-05-24 17:38:31 +10:00
|
|
|
parent_offset =
|
|
|
|
container_titlebar_height() * parent->parent->children->length;
|
2018-05-21 22:58:46 +10:00
|
|
|
}
|
2018-06-06 19:19:30 +10:00
|
|
|
size_t parent_height = parent->height - parent_offset;
|
2018-05-21 22:58:46 +10:00
|
|
|
for (int i = 0; i < parent->children->length; ++i) {
|
|
|
|
struct sway_container *child = parent->children->items[i];
|
2018-06-09 23:34:56 +10:00
|
|
|
remove_gaps(child);
|
2018-06-06 19:19:30 +10:00
|
|
|
child->x = parent->x;
|
|
|
|
child->y = parent->y + parent_offset;
|
|
|
|
child->width = parent->width;
|
|
|
|
child->height = parent_height;
|
2018-06-09 23:34:56 +10:00
|
|
|
add_gaps(child);
|
2018-05-21 22:58:46 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 16:24:11 +10:00
|
|
|
/**
|
|
|
|
* If a container has been deleted from the pending tree state, we must add it
|
|
|
|
* to the transaction so it can be freed afterwards. To do this, we iterate the
|
|
|
|
* server's destroying_containers list and add all of them. We may add more than
|
|
|
|
* what we need to, but this is easy and has no negative consequences.
|
|
|
|
*/
|
|
|
|
static void add_deleted_containers(struct sway_transaction *transaction) {
|
|
|
|
for (int i = 0; i < server.destroying_containers->length; ++i) {
|
|
|
|
struct sway_container *child = server.destroying_containers->items[i];
|
|
|
|
transaction_add_container(transaction, child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-06 22:57:34 +10:00
|
|
|
static void arrange_children_of(struct sway_container *parent,
|
|
|
|
struct sway_transaction *transaction);
|
|
|
|
|
|
|
|
static void arrange_floating(struct sway_container *floating,
|
|
|
|
struct sway_transaction *transaction) {
|
|
|
|
for (int i = 0; i < floating->children->length; ++i) {
|
|
|
|
struct sway_container *floater = floating->children->items[i];
|
|
|
|
if (floater->type == C_VIEW) {
|
|
|
|
view_autoconfigure(floater->sway_view);
|
|
|
|
} else {
|
|
|
|
arrange_children_of(floater, transaction);
|
|
|
|
}
|
|
|
|
transaction_add_container(transaction, floater);
|
|
|
|
}
|
2018-06-23 16:24:11 +10:00
|
|
|
transaction_add_container(transaction, floating);
|
2018-06-06 22:57:34 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
static void arrange_children_of(struct sway_container *parent,
|
2018-06-03 16:35:06 +10:00
|
|
|
struct sway_transaction *transaction) {
|
2018-04-28 11:26:14 +10:00
|
|
|
if (config->reloading) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-10 07:54:30 +10:00
|
|
|
wlr_log(WLR_DEBUG, "Arranging layout for %p %s %fx%f+%f,%f", parent,
|
2018-06-06 19:19:30 +10:00
|
|
|
parent->name, parent->width, parent->height, parent->x, parent->y);
|
2018-04-28 11:26:14 +10:00
|
|
|
|
|
|
|
// Calculate x, y, width and height of children
|
2018-06-06 19:19:30 +10:00
|
|
|
switch (parent->layout) {
|
2018-04-28 11:26:14 +10:00
|
|
|
case L_HORIZ:
|
|
|
|
apply_horiz_layout(parent);
|
|
|
|
break;
|
|
|
|
case L_VERT:
|
|
|
|
apply_vert_layout(parent);
|
|
|
|
break;
|
2018-05-19 22:54:50 +10:00
|
|
|
case L_TABBED:
|
2018-05-21 22:58:46 +10:00
|
|
|
case L_STACKED:
|
2018-05-24 17:38:31 +10:00
|
|
|
apply_tabbed_or_stacked_layout(parent);
|
2018-05-21 22:58:46 +10:00
|
|
|
break;
|
2018-06-03 16:35:06 +10:00
|
|
|
case L_NONE:
|
2018-04-28 11:26:14 +10:00
|
|
|
apply_horiz_layout(parent);
|
|
|
|
break;
|
2018-06-03 16:35:06 +10:00
|
|
|
case L_FLOATING:
|
2018-06-06 22:57:34 +10:00
|
|
|
arrange_floating(parent, transaction);
|
|
|
|
break;
|
2018-04-28 11:26:14 +10:00
|
|
|
}
|
|
|
|
|
2018-06-03 16:35:06 +10:00
|
|
|
// Recurse into child containers
|
2018-04-28 11:26:14 +10:00
|
|
|
for (int i = 0; i < parent->children->length; ++i) {
|
|
|
|
struct sway_container *child = parent->children->items[i];
|
2018-06-09 23:34:56 +10:00
|
|
|
if (parent->has_gaps && !child->has_gaps) {
|
|
|
|
child->has_gaps = true;
|
|
|
|
child->gaps_inner = parent->gaps_inner;
|
|
|
|
child->gaps_outer = parent->gaps_outer;
|
|
|
|
}
|
2018-04-28 11:26:14 +10:00
|
|
|
if (child->type == C_VIEW) {
|
2018-04-30 21:24:13 +10:00
|
|
|
view_autoconfigure(child->sway_view);
|
2018-04-28 11:26:14 +10:00
|
|
|
} else {
|
2018-06-06 22:57:34 +10:00
|
|
|
arrange_children_of(child, transaction);
|
2018-04-28 11:26:14 +10:00
|
|
|
}
|
2018-06-03 16:35:06 +10:00
|
|
|
transaction_add_container(transaction, child);
|
2018-04-28 11:26:14 +10:00
|
|
|
}
|
2018-06-03 16:35:06 +10:00
|
|
|
}
|
2018-05-24 22:30:44 +10:00
|
|
|
|
2018-06-06 22:57:34 +10:00
|
|
|
static void arrange_workspace(struct sway_container *workspace,
|
2018-06-03 16:35:06 +10:00
|
|
|
struct sway_transaction *transaction) {
|
|
|
|
if (config->reloading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
struct sway_container *output = workspace->parent;
|
|
|
|
struct wlr_box *area = &output->sway_output->usable_area;
|
2018-07-10 07:54:30 +10:00
|
|
|
wlr_log(WLR_DEBUG, "Usable area for ws: %dx%d@%d,%d",
|
2018-06-03 16:35:06 +10:00
|
|
|
area->width, area->height, area->x, area->y);
|
2018-06-11 11:03:43 +10:00
|
|
|
remove_gaps(workspace);
|
2018-06-06 19:19:30 +10:00
|
|
|
workspace->width = area->width;
|
|
|
|
workspace->height = area->height;
|
|
|
|
workspace->x = output->x + area->x;
|
|
|
|
workspace->y = output->y + area->y;
|
2018-06-11 11:03:43 +10:00
|
|
|
add_gaps(workspace);
|
2018-06-03 16:35:06 +10:00
|
|
|
transaction_add_container(transaction, workspace);
|
2018-07-10 07:54:30 +10:00
|
|
|
wlr_log(WLR_DEBUG, "Arranging workspace '%s' at %f, %f", workspace->name,
|
2018-06-06 19:19:30 +10:00
|
|
|
workspace->x, workspace->y);
|
2018-06-06 22:57:34 +10:00
|
|
|
arrange_floating(workspace->sway_workspace->floating, transaction);
|
|
|
|
arrange_children_of(workspace, transaction);
|
2018-06-03 16:35:06 +10:00
|
|
|
}
|
|
|
|
|
2018-06-06 22:57:34 +10:00
|
|
|
static void arrange_output(struct sway_container *output,
|
2018-06-03 16:35:06 +10:00
|
|
|
struct sway_transaction *transaction) {
|
|
|
|
if (config->reloading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const struct wlr_box *output_box = wlr_output_layout_get_box(
|
|
|
|
root_container.sway_root->output_layout,
|
|
|
|
output->sway_output->wlr_output);
|
|
|
|
output->x = output_box->x;
|
|
|
|
output->y = output_box->y;
|
|
|
|
output->width = output_box->width;
|
|
|
|
output->height = output_box->height;
|
|
|
|
transaction_add_container(transaction, output);
|
2018-07-10 07:54:30 +10:00
|
|
|
wlr_log(WLR_DEBUG, "Arranging output '%s' at %f,%f",
|
2018-06-06 19:19:30 +10:00
|
|
|
output->name, output->x, output->y);
|
2018-06-03 16:35:06 +10:00
|
|
|
for (int i = 0; i < output->children->length; ++i) {
|
|
|
|
struct sway_container *workspace = output->children->items[i];
|
2018-06-06 22:57:34 +10:00
|
|
|
arrange_workspace(workspace, transaction);
|
2018-06-03 16:35:06 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-06 22:57:34 +10:00
|
|
|
static void arrange_root(struct sway_transaction *transaction) {
|
2018-06-03 16:35:06 +10:00
|
|
|
if (config->reloading) {
|
|
|
|
return;
|
2018-05-24 22:30:44 +10:00
|
|
|
}
|
2018-06-03 16:35:06 +10:00
|
|
|
struct wlr_output_layout *output_layout =
|
|
|
|
root_container.sway_root->output_layout;
|
|
|
|
const struct wlr_box *layout_box =
|
|
|
|
wlr_output_layout_get_box(output_layout, NULL);
|
|
|
|
root_container.x = layout_box->x;
|
|
|
|
root_container.y = layout_box->y;
|
|
|
|
root_container.width = layout_box->width;
|
|
|
|
root_container.height = layout_box->height;
|
|
|
|
transaction_add_container(transaction, &root_container);
|
|
|
|
for (int i = 0; i < root_container.children->length; ++i) {
|
|
|
|
struct sway_container *output = root_container.children->items[i];
|
2018-06-06 22:57:34 +10:00
|
|
|
arrange_output(output, transaction);
|
2018-06-03 16:35:06 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void arrange_windows(struct sway_container *container,
|
|
|
|
struct sway_transaction *transaction) {
|
|
|
|
switch (container->type) {
|
|
|
|
case C_ROOT:
|
2018-06-06 22:57:34 +10:00
|
|
|
arrange_root(transaction);
|
2018-06-03 16:35:06 +10:00
|
|
|
break;
|
|
|
|
case C_OUTPUT:
|
2018-06-06 22:57:34 +10:00
|
|
|
arrange_output(container, transaction);
|
2018-06-03 16:35:06 +10:00
|
|
|
break;
|
|
|
|
case C_WORKSPACE:
|
2018-06-06 22:57:34 +10:00
|
|
|
arrange_workspace(container, transaction);
|
2018-06-03 16:35:06 +10:00
|
|
|
break;
|
|
|
|
case C_CONTAINER:
|
2018-06-06 22:57:34 +10:00
|
|
|
arrange_children_of(container, transaction);
|
2018-06-03 16:35:06 +10:00
|
|
|
transaction_add_container(transaction, container);
|
|
|
|
break;
|
|
|
|
case C_VIEW:
|
2018-06-06 22:57:34 +10:00
|
|
|
view_autoconfigure(container->sway_view);
|
|
|
|
transaction_add_container(transaction, container);
|
2018-06-03 16:35:06 +10:00
|
|
|
break;
|
|
|
|
case C_TYPES:
|
|
|
|
break;
|
|
|
|
}
|
2018-06-23 16:24:11 +10:00
|
|
|
add_deleted_containers(transaction);
|
2018-06-03 16:35:06 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
void arrange_and_commit(struct sway_container *container) {
|
|
|
|
struct sway_transaction *transaction = transaction_create();
|
|
|
|
arrange_windows(container, transaction);
|
|
|
|
transaction_commit(transaction);
|
|
|
|
}
|
2018-06-11 11:03:43 +10:00
|
|
|
|
|
|
|
void remove_gaps(struct sway_container *c) {
|
|
|
|
if (c->current_gaps == 0) {
|
2018-07-10 07:54:30 +10:00
|
|
|
wlr_log(WLR_DEBUG, "Removing gaps: not gapped: %p", c);
|
2018-06-11 11:03:43 +10:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->width += c->current_gaps * 2;
|
|
|
|
c->height += c->current_gaps * 2;
|
|
|
|
c->x -= c->current_gaps;
|
|
|
|
c->y -= c->current_gaps;
|
|
|
|
|
|
|
|
c->current_gaps = 0;
|
|
|
|
|
2018-07-10 07:54:30 +10:00
|
|
|
wlr_log(WLR_DEBUG, "Removing gaps %p", c);
|
2018-06-11 11:03:43 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
void add_gaps(struct sway_container *c) {
|
|
|
|
if (c->current_gaps > 0 || c->type == C_CONTAINER) {
|
2018-07-10 07:54:30 +10:00
|
|
|
wlr_log(WLR_DEBUG, "Not adding gaps: %p", c);
|
2018-06-11 11:03:43 +10:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c->type == C_WORKSPACE &&
|
|
|
|
!(config->edge_gaps || (config->smart_gaps && c->children->length > 1))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
double gaps = c->has_gaps ? c->gaps_inner : config->gaps_inner;
|
|
|
|
|
|
|
|
c->x += gaps;
|
|
|
|
c->y += gaps;
|
|
|
|
c->width -= 2 * gaps;
|
|
|
|
c->height -= 2 * gaps;
|
|
|
|
c->current_gaps = gaps;
|
|
|
|
|
2018-07-10 07:54:30 +10:00
|
|
|
wlr_log(WLR_DEBUG, "Adding gaps: %p", c);
|
2018-04-28 11:26:14 +10:00
|
|
|
}
|