This commit changes the meaning of sway_container so that it only refers to layout containers and view containers. Workspaces, outputs and the root are no longer known as containers. Instead, root, outputs, workspaces and containers are all a type of node, and containers come in two types: layout containers and view containers. In addition to the above, this implements type safe variables. This means we use specific types such as sway_output and sway_workspace instead of generic containers or nodes. However, it's worth noting that in a few places places (eg. seat focus and transactions) referring to them in a generic way is unavoidable which is why we still use nodes in some places. If you want a TL;DR, look at node.h, as well as the struct definitions for root, output, workspace and container. Note that sway_output now contains a workspaces list, and workspaces now contain a tiling and floating list, and containers now contain a pointer back to the workspace. There are now functions for seat_get_focused_workspace and seat_get_focused_container. The latter will return NULL if a workspace itself is focused. Most other seat functions like seat_get_focus and seat_set_focus now accept and return nodes. In the config->handler_context struct, current_container has been replaced with three pointers: node, container and workspace. node is the same as what current_container was, while workspace is the workspace that the node resides on and container is the actual container, which may be NULL if a workspace itself is focused. The global root_container variable has been replaced with one simply called root, which is a pointer to the sway_root instance. The way outputs are created, enabled, disabled and destroyed has changed. Previously we'd wrap the sway_output in a container when it is enabled, but as we don't have containers any more it needs a different approach. The output_create and output_destroy functions previously created/destroyed the container, but now they create/destroy the sway_output. There is a new function output_disable to disable an output without destroying it. Containers have a new view property. If this is populated then the container is a view container, otherwise it's a layout container. Like before, this property is immutable for the life of the container. Containers have both a `sway_container *parent` and `sway_workspace *workspace`. As we use specific types now, parent cannot point to a workspace so it'll be NULL for containers which are direct children of the workspace. The workspace property is set for all containers, except those which are hidden in the scratchpad as they have no workspace. In some cases we need to refer to workspaces in a container-like way. For example, workspaces have layout and children, but when using specific types this makes it difficult. Likewise, it's difficult for a container to get its parent's layout when the parent could be another container or a workspace. To make it easier, some helper functions have been created: container_parent_layout and container_get_siblings. container_remove_child has been renamed to container_detach and container_replace_child has been renamed to container_replace. `container_handle_fullscreen_reparent(con, old_parent)` has had the old_parent removed. We now unfullscreen the workspace when detaching the container, so this function is simplified and only needs one argument now. container_notify_subtree_changed has been renamed to container_update_representation. This is more descriptive of its purpose. I also wanted to be able to call it with whatever container was changed rather than the container's parent, which makes bubbling up to the workspace easier. There are now state structs per node thing. ie. sway_output_state, sway_workspace_state and sway_container_state. The focus, move and layout commands have been completely refactored to work with the specific types. I considered making these a separate PR, but I'd be backporting my changes only to replace them again, and it's easier just to test everything at once.
276 lines
7.6 KiB
C
276 lines
7.6 KiB
C
#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/output.h"
|
|
#include "sway/tree/workspace.h"
|
|
#include "sway/tree/view.h"
|
|
#include "list.h"
|
|
#include "log.h"
|
|
|
|
static void apply_horiz_layout(list_t *children, struct wlr_box *parent) {
|
|
if (!children->length) {
|
|
return;
|
|
}
|
|
|
|
// Calculate total width of children
|
|
double total_width = 0;
|
|
for (int i = 0; i < children->length; ++i) {
|
|
struct sway_container *child = children->items[i];
|
|
if (child->width <= 0) {
|
|
if (children->length > 1) {
|
|
child->width = parent->width / (children->length - 1);
|
|
} else {
|
|
child->width = parent->width;
|
|
}
|
|
}
|
|
container_remove_gaps(child);
|
|
total_width += child->width;
|
|
}
|
|
double scale = parent->width / total_width;
|
|
|
|
// Resize windows
|
|
wlr_log(WLR_DEBUG, "Arranging %p horizontally", parent);
|
|
double child_x = parent->x;
|
|
for (int i = 0; i < children->length; ++i) {
|
|
struct sway_container *child = children->items[i];
|
|
child->x = child_x;
|
|
child->y = parent->y;
|
|
child->width = floor(child->width * scale);
|
|
child->height = parent->height;
|
|
child_x += child->width;
|
|
|
|
// Make last child use remaining width of parent
|
|
if (i == children->length - 1) {
|
|
child->width = parent->x + parent->width - child->x;
|
|
}
|
|
container_add_gaps(child);
|
|
}
|
|
}
|
|
|
|
static void apply_vert_layout(list_t *children, struct wlr_box *parent) {
|
|
if (!children->length) {
|
|
return;
|
|
}
|
|
|
|
// Calculate total height of children
|
|
double total_height = 0;
|
|
for (int i = 0; i < children->length; ++i) {
|
|
struct sway_container *child = children->items[i];
|
|
if (child->height <= 0) {
|
|
if (children->length > 1) {
|
|
child->height = parent->height / (children->length - 1);
|
|
} else {
|
|
child->height = parent->height;
|
|
}
|
|
}
|
|
container_remove_gaps(child);
|
|
total_height += child->height;
|
|
}
|
|
double scale = parent->height / total_height;
|
|
|
|
// Resize
|
|
wlr_log(WLR_DEBUG, "Arranging %p vertically", parent);
|
|
double child_y = parent->y;
|
|
for (int i = 0; i < children->length; ++i) {
|
|
struct sway_container *child = children->items[i];
|
|
child->x = parent->x;
|
|
child->y = child_y;
|
|
child->width = parent->width;
|
|
child->height = floor(child->height * scale);
|
|
child_y += child->height;
|
|
|
|
// Make last child use remaining height of parent
|
|
if (i == children->length - 1) {
|
|
child->height = parent->y + parent->height - child->y;
|
|
}
|
|
container_add_gaps(child);
|
|
}
|
|
}
|
|
|
|
static void apply_tabbed_layout(list_t *children, struct wlr_box *parent) {
|
|
if (!children->length) {
|
|
return;
|
|
}
|
|
size_t parent_offset = container_titlebar_height();
|
|
size_t parent_height = parent->height - parent_offset;
|
|
for (int i = 0; i < children->length; ++i) {
|
|
struct sway_container *child = children->items[i];
|
|
container_remove_gaps(child);
|
|
child->x = parent->x;
|
|
child->y = parent->y + parent_offset;
|
|
child->width = parent->width;
|
|
child->height = parent_height;
|
|
container_add_gaps(child);
|
|
}
|
|
}
|
|
|
|
static void apply_stacked_layout(list_t *children, struct wlr_box *parent) {
|
|
if (!children->length) {
|
|
return;
|
|
}
|
|
size_t parent_offset = container_titlebar_height() * children->length;
|
|
size_t parent_height = parent->height - parent_offset;
|
|
for (int i = 0; i < children->length; ++i) {
|
|
struct sway_container *child = children->items[i];
|
|
container_remove_gaps(child);
|
|
child->x = parent->x;
|
|
child->y = parent->y + parent_offset;
|
|
child->width = parent->width;
|
|
child->height = parent_height;
|
|
container_add_gaps(child);
|
|
}
|
|
}
|
|
|
|
static void arrange_floating(list_t *floating) {
|
|
for (int i = 0; i < floating->length; ++i) {
|
|
struct sway_container *floater = floating->items[i];
|
|
arrange_container(floater);
|
|
}
|
|
}
|
|
|
|
static void arrange_children(list_t *children,
|
|
enum sway_container_layout layout, struct wlr_box *parent) {
|
|
// Calculate x, y, width and height of children
|
|
switch (layout) {
|
|
case L_HORIZ:
|
|
apply_horiz_layout(children, parent);
|
|
break;
|
|
case L_VERT:
|
|
apply_vert_layout(children, parent);
|
|
break;
|
|
case L_TABBED:
|
|
apply_tabbed_layout(children, parent);
|
|
break;
|
|
case L_STACKED:
|
|
apply_stacked_layout(children, parent);
|
|
break;
|
|
case L_NONE:
|
|
apply_horiz_layout(children, parent);
|
|
break;
|
|
}
|
|
|
|
// Recurse into child containers
|
|
for (int i = 0; i < children->length; ++i) {
|
|
struct sway_container *child = children->items[i];
|
|
arrange_container(child);
|
|
}
|
|
}
|
|
|
|
void arrange_container(struct sway_container *container) {
|
|
if (config->reloading) {
|
|
return;
|
|
}
|
|
if (container->view) {
|
|
view_autoconfigure(container->view);
|
|
node_set_dirty(&container->node);
|
|
return;
|
|
}
|
|
struct wlr_box box;
|
|
container_get_box(container, &box);
|
|
arrange_children(container->children, container->layout, &box);
|
|
node_set_dirty(&container->node);
|
|
}
|
|
|
|
void arrange_workspace(struct sway_workspace *workspace) {
|
|
if (config->reloading) {
|
|
return;
|
|
}
|
|
struct sway_output *output = workspace->output;
|
|
struct wlr_box *area = &output->usable_area;
|
|
wlr_log(WLR_DEBUG, "Usable area for ws: %dx%d@%d,%d",
|
|
area->width, area->height, area->x, area->y);
|
|
workspace_remove_gaps(workspace);
|
|
|
|
double prev_x = workspace->x;
|
|
double prev_y = workspace->y;
|
|
workspace->width = area->width;
|
|
workspace->height = area->height;
|
|
workspace->x = output->wlr_output->lx + area->x;
|
|
workspace->y = output->wlr_output->ly + area->y;
|
|
|
|
// Adjust any floating containers
|
|
double diff_x = workspace->x - prev_x;
|
|
double diff_y = workspace->y - prev_y;
|
|
if (diff_x != 0 || diff_y != 0) {
|
|
for (int i = 0; i < workspace->floating->length; ++i) {
|
|
struct sway_container *floater = workspace->floating->items[i];
|
|
container_floating_translate(floater, diff_x, diff_y);
|
|
double center_x = floater->x + floater->width / 2;
|
|
double center_y = floater->y + floater->height / 2;
|
|
struct wlr_box workspace_box;
|
|
workspace_get_box(workspace, &workspace_box);
|
|
if (!wlr_box_contains_point(&workspace_box, center_x, center_y)) {
|
|
container_floating_move_to_center(floater);
|
|
}
|
|
}
|
|
}
|
|
|
|
workspace_add_gaps(workspace);
|
|
node_set_dirty(&workspace->node);
|
|
wlr_log(WLR_DEBUG, "Arranging workspace '%s' at %f, %f", workspace->name,
|
|
workspace->x, workspace->y);
|
|
if (workspace->fullscreen) {
|
|
struct sway_container *fs = workspace->fullscreen;
|
|
fs->x = output->wlr_output->lx;
|
|
fs->y = output->wlr_output->ly;
|
|
fs->width = output->wlr_output->width;
|
|
fs->height = output->wlr_output->height;
|
|
arrange_container(fs);
|
|
} else {
|
|
struct wlr_box box;
|
|
workspace_get_box(workspace, &box);
|
|
arrange_children(workspace->tiling, workspace->layout, &box);
|
|
arrange_floating(workspace->floating);
|
|
}
|
|
}
|
|
|
|
void arrange_output(struct sway_output *output) {
|
|
if (config->reloading) {
|
|
return;
|
|
}
|
|
// Outputs have no pending x/y/width/height,
|
|
// so all we do here is arrange the workspaces.
|
|
for (int i = 0; i < output->workspaces->length; ++i) {
|
|
struct sway_workspace *workspace = output->workspaces->items[i];
|
|
arrange_workspace(workspace);
|
|
}
|
|
}
|
|
|
|
void arrange_root(void) {
|
|
if (config->reloading) {
|
|
return;
|
|
}
|
|
const struct wlr_box *layout_box =
|
|
wlr_output_layout_get_box(root->output_layout, NULL);
|
|
root->x = layout_box->x;
|
|
root->y = layout_box->y;
|
|
root->width = layout_box->width;
|
|
root->height = layout_box->height;
|
|
for (int i = 0; i < root->outputs->length; ++i) {
|
|
struct sway_output *output = root->outputs->items[i];
|
|
arrange_output(output);
|
|
}
|
|
}
|
|
|
|
void arrange_node(struct sway_node *node) {
|
|
switch (node->type) {
|
|
case N_ROOT:
|
|
arrange_root();
|
|
break;
|
|
case N_OUTPUT:
|
|
arrange_output(node->sway_output);
|
|
break;
|
|
case N_WORKSPACE:
|
|
arrange_workspace(node->sway_workspace);
|
|
break;
|
|
case N_CONTAINER:
|
|
arrange_container(node->sway_container);
|
|
break;
|
|
}
|
|
}
|