You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1025 lines
27 KiB
1025 lines
27 KiB
8 years ago
|
#define _XOPEN_SOURCE 500
|
||
9 years ago
|
#include <ctype.h>
|
||
10 years ago
|
#include <stdlib.h>
|
||
|
#include <stdbool.h>
|
||
|
#include <strings.h>
|
||
9 years ago
|
#include <string.h>
|
||
7 years ago
|
#include <wlr/types/wlr_box.h>
|
||
|
#include <wlr/types/wlr_output.h>
|
||
8 years ago
|
#include "sway/config.h"
|
||
|
#include "sway/container.h"
|
||
|
#include "sway/workspace.h"
|
||
|
#include "sway/focus.h"
|
||
|
#include "sway/border.h"
|
||
|
#include "sway/layout.h"
|
||
|
#include "sway/input_state.h"
|
||
|
#include "sway/ipc-server.h"
|
||
|
#include "sway/output.h"
|
||
7 years ago
|
#include "sway/view.h"
|
||
10 years ago
|
#include "log.h"
|
||
8 years ago
|
#include "stringop.h"
|
||
10 years ago
|
|
||
9 years ago
|
#define ASSERT_NONNULL(PTR) \
|
||
9 years ago
|
sway_assert (PTR, #PTR "must be non-null")
|
||
10 years ago
|
|
||
|
static swayc_t *new_swayc(enum swayc_types type) {
|
||
8 years ago
|
// next id starts at 1 because 0 is assigned to root_container in layout.c
|
||
|
static size_t next_id = 1;
|
||
10 years ago
|
swayc_t *c = calloc(1, sizeof(swayc_t));
|
||
8 years ago
|
if (!c) {
|
||
|
return NULL;
|
||
|
}
|
||
8 years ago
|
c->id = next_id++;
|
||
10 years ago
|
c->handle = -1;
|
||
9 years ago
|
c->gaps = -1;
|
||
10 years ago
|
c->layout = L_NONE;
|
||
8 years ago
|
c->workspace_layout = L_NONE;
|
||
10 years ago
|
c->type = type;
|
||
8 years ago
|
c->nb_master = 1;
|
||
|
c->nb_slave_groups = 1;
|
||
10 years ago
|
if (type != C_VIEW) {
|
||
|
c->children = create_list();
|
||
|
}
|
||
|
return c;
|
||
|
}
|
||
|
|
||
9 years ago
|
static void free_swayc(swayc_t *cont) {
|
||
|
if (!ASSERT_NONNULL(cont)) {
|
||
|
return;
|
||
|
}
|
||
|
if (cont->children) {
|
||
9 years ago
|
// remove children until there are no more, free_swayc calls
|
||
|
// remove_child, which removes child from this container
|
||
|
while (cont->children->length) {
|
||
|
free_swayc(cont->children->items[0]);
|
||
9 years ago
|
}
|
||
9 years ago
|
list_free(cont->children);
|
||
10 years ago
|
}
|
||
9 years ago
|
if (cont->unmanaged) {
|
||
|
list_free(cont->unmanaged);
|
||
|
}
|
||
9 years ago
|
if (cont->floating) {
|
||
9 years ago
|
while (cont->floating->length) {
|
||
|
free_swayc(cont->floating->items[0]);
|
||
9 years ago
|
}
|
||
9 years ago
|
list_free(cont->floating);
|
||
9 years ago
|
}
|
||
8 years ago
|
if (cont->marks) {
|
||
|
list_foreach(cont->marks, free);
|
||
|
list_free(cont->marks);
|
||
|
}
|
||
9 years ago
|
if (cont->parent) {
|
||
|
remove_child(cont);
|
||
10 years ago
|
}
|
||
9 years ago
|
if (cont->name) {
|
||
|
free(cont->name);
|
||
9 years ago
|
}
|
||
9 years ago
|
if (cont->class) {
|
||
|
free(cont->class);
|
||
|
}
|
||
8 years ago
|
if (cont->instance) {
|
||
|
free(cont->instance);
|
||
|
}
|
||
9 years ago
|
if (cont->app_id) {
|
||
|
free(cont->app_id);
|
||
|
}
|
||
9 years ago
|
if (cont->bg_pid != 0) {
|
||
|
terminate_swaybg(cont->bg_pid);
|
||
|
}
|
||
9 years ago
|
if (cont->border) {
|
||
|
if (cont->border->buffer) {
|
||
|
free(cont->border->buffer);
|
||
|
}
|
||
|
free(cont->border);
|
||
|
}
|
||
9 years ago
|
free(cont);
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
static void update_root_geometry() {
|
||
|
int width = 0;
|
||
|
int height = 0;
|
||
|
swayc_t *child;
|
||
|
int child_width;
|
||
|
int child_height;
|
||
|
|
||
|
for (int i = 0; i < root_container.children->length; ++i) {
|
||
|
child = root_container.children->items[i];
|
||
|
child_width = child->width + child->x;
|
||
|
child_height = child->height + child->y;
|
||
|
if (child_width > width) {
|
||
|
width = child_width;
|
||
|
}
|
||
|
|
||
|
if (child_height > height) {
|
||
|
height = child_height;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
root_container.width = width;
|
||
|
root_container.height = height;
|
||
|
}
|
||
|
|
||
9 years ago
|
// New containers
|
||
10 years ago
|
|
||
7 years ago
|
swayc_t *new_output(struct sway_output *sway_output) {
|
||
7 years ago
|
struct wlr_box size;
|
||
7 years ago
|
wlr_output_effective_resolution(sway_output->wlr_output,
|
||
|
&size.width, &size.height);
|
||
|
const char *name = sway_output->wlr_output->name;
|
||
9 years ago
|
// Find current outputs to see if this already exists
|
||
9 years ago
|
{
|
||
|
int i, len = root_container.children->length;
|
||
9 years ago
|
for (i = 0; i < len; ++i) {
|
||
|
swayc_t *op = root_container.children->items[i];
|
||
9 years ago
|
const char *op_name = op->name;
|
||
|
if (op_name && name && strcmp(op_name, name) == 0) {
|
||
7 years ago
|
sway_log(L_DEBUG, "restoring output %p: %s",
|
||
|
sway_output, op_name);
|
||
9 years ago
|
return op;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
struct output_config *oc = NULL, *all = NULL;
|
||
9 years ago
|
int i;
|
||
|
for (i = 0; i < config->output_configs->length; ++i) {
|
||
9 years ago
|
struct output_config *cur = config->output_configs->items[i];
|
||
|
if (strcasecmp(name, cur->name) == 0) {
|
||
9 years ago
|
sway_log(L_DEBUG, "Matched output config for %s", name);
|
||
9 years ago
|
oc = cur;
|
||
9 years ago
|
}
|
||
9 years ago
|
if (strcasecmp("*", cur->name) == 0) {
|
||
9 years ago
|
sway_log(L_DEBUG, "Matched wildcard output config for %s", name);
|
||
9 years ago
|
all = cur;
|
||
|
}
|
||
|
|
||
|
if (oc && all) {
|
||
9 years ago
|
break;
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
if (!oc) {
|
||
|
oc = all;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (oc && !oc->enabled) {
|
||
|
return NULL;
|
||
|
}
|
||
9 years ago
|
|
||
10 years ago
|
swayc_t *output = new_swayc(C_OUTPUT);
|
||
7 years ago
|
output->_handle.output = sway_output;
|
||
9 years ago
|
output->name = name ? strdup(name) : NULL;
|
||
7 years ago
|
output->width = size.width;
|
||
|
output->height = size.width;
|
||
9 years ago
|
output->unmanaged = create_list();
|
||
9 years ago
|
output->bg_pid = 0;
|
||
9 years ago
|
|
||
9 years ago
|
apply_output_config(oc, output);
|
||
9 years ago
|
add_child(&root_container, output);
|
||
8 years ago
|
load_swaybars();
|
||
9 years ago
|
|
||
9 years ago
|
// Create workspace
|
||
|
char *ws_name = NULL;
|
||
9 years ago
|
swayc_t *ws = NULL;
|
||
|
|
||
9 years ago
|
if (name) {
|
||
|
for (i = 0; i < config->workspace_outputs->length; ++i) {
|
||
|
struct workspace_output *wso = config->workspace_outputs->items[i];
|
||
|
if (strcasecmp(wso->output, name) == 0) {
|
||
|
sway_log(L_DEBUG, "Matched workspace to output: %s for %s", wso->workspace, wso->output);
|
||
|
// Check if any other workspaces are using this name
|
||
9 years ago
|
if ((ws = workspace_by_name(wso->workspace))) {
|
||
|
// if yes, move those to this output, because they should be here
|
||
|
move_workspace_to(ws, output);
|
||
|
} else if (!ws_name) {
|
||
|
// set a workspace name in case we need to create a default one
|
||
|
ws_name = strdup(wso->workspace);
|
||
9 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
if (output->children->length == 0) {
|
||
|
if (!ws_name) {
|
||
|
ws_name = workspace_next_name(output->name);
|
||
|
}
|
||
|
// create and initialize default workspace
|
||
|
sway_log(L_DEBUG, "Creating default workspace %s", ws_name);
|
||
|
ws = new_workspace(output, ws_name);
|
||
|
ws->is_focused = true;
|
||
|
} else {
|
||
|
sort_workspaces(output);
|
||
|
set_focused_container(output->children->items[0]);
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
free(ws_name);
|
||
9 years ago
|
update_root_geometry();
|
||
10 years ago
|
return output;
|
||
|
}
|
||
|
|
||
9 years ago
|
swayc_t *new_workspace(swayc_t *output, const char *name) {
|
||
9 years ago
|
if (!ASSERT_NONNULL(output)) {
|
||
|
return NULL;
|
||
9 years ago
|
}
|
||
9 years ago
|
sway_log(L_DEBUG, "Added workspace %s for output %u", name, (unsigned int)output->handle);
|
||
10 years ago
|
swayc_t *workspace = new_swayc(C_WORKSPACE);
|
||
9 years ago
|
|
||
9 years ago
|
workspace->prev_layout = L_NONE;
|
||
8 years ago
|
workspace->layout = default_layout(output);
|
||
8 years ago
|
workspace->workspace_layout = default_layout(output);
|
||
9 years ago
|
|
||
9 years ago
|
workspace->x = output->x;
|
||
|
workspace->y = output->y;
|
||
10 years ago
|
workspace->width = output->width;
|
||
|
workspace->height = output->height;
|
||
8 years ago
|
workspace->name = !name ? NULL : strdup(name);
|
||
9 years ago
|
workspace->visible = false;
|
||
9 years ago
|
workspace->floating = create_list();
|
||
10 years ago
|
|
||
9 years ago
|
add_child(output, workspace);
|
||
|
sort_workspaces(output);
|
||
9 years ago
|
|
||
10 years ago
|
return workspace;
|
||
|
}
|
||
|
|
||
|
swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) {
|
||
9 years ago
|
if (!ASSERT_NONNULL(child)
|
||
|
&& !sway_assert(!child->is_floating, "cannot create container around floating window")) {
|
||
9 years ago
|
return NULL;
|
||
|
}
|
||
10 years ago
|
swayc_t *cont = new_swayc(C_CONTAINER);
|
||
|
|
||
|
sway_log(L_DEBUG, "creating container %p around %p", cont, child);
|
||
|
|
||
9 years ago
|
cont->prev_layout = L_NONE;
|
||
10 years ago
|
cont->layout = layout;
|
||
10 years ago
|
cont->width = child->width;
|
||
10 years ago
|
cont->height = child->height;
|
||
9 years ago
|
cont->x = child->x;
|
||
10 years ago
|
cont->y = child->y;
|
||
|
cont->visible = child->visible;
|
||
9 years ago
|
cont->cached_geometry = child->cached_geometry;
|
||
9 years ago
|
cont->gaps = child->gaps;
|
||
10 years ago
|
|
||
10 years ago
|
/* Container inherits all of workspaces children, layout and whatnot */
|
||
|
if (child->type == C_WORKSPACE) {
|
||
|
swayc_t *workspace = child;
|
||
9 years ago
|
// reorder focus
|
||
10 years ago
|
cont->focused = workspace->focused;
|
||
|
workspace->focused = cont;
|
||
9 years ago
|
// set all children focu to container
|
||
9 years ago
|
int i;
|
||
|
for (i = 0; i < workspace->children->length; ++i) {
|
||
|
((swayc_t *)workspace->children->items[i])->parent = cont;
|
||
|
}
|
||
9 years ago
|
// Swap children
|
||
10 years ago
|
list_t *tmp_list = workspace->children;
|
||
|
workspace->children = cont->children;
|
||
|
cont->children = tmp_list;
|
||
9 years ago
|
// add container to workspace chidren
|
||
10 years ago
|
add_child(workspace, cont);
|
||
9 years ago
|
// give them proper layouts
|
||
8 years ago
|
cont->layout = workspace->workspace_layout;
|
||
9 years ago
|
cont->prev_layout = workspace->prev_layout;
|
||
9 years ago
|
/* TODO: might break shit in move_container!!! workspace->layout = layout; */
|
||
9 years ago
|
set_focused_container_for(workspace, get_focused_view(workspace));
|
||
9 years ago
|
} else { // Or is built around container
|
||
10 years ago
|
swayc_t *parent = replace_child(child, cont);
|
||
|
if (parent) {
|
||
|
add_child(cont, child);
|
||
|
}
|
||
10 years ago
|
}
|
||
|
return cont;
|
||
|
}
|
||
|
|
||
7 years ago
|
swayc_t *new_view(swayc_t *sibling, struct sway_view *view) {
|
||
9 years ago
|
if (!ASSERT_NONNULL(sibling)) {
|
||
|
return NULL;
|
||
|
}
|
||
7 years ago
|
const char *title = view->iface.get_prop(view, VIEW_PROP_TITLE);
|
||
|
swayc_t *swayc = new_swayc(C_VIEW);
|
||
|
sway_log(L_DEBUG, "Adding new view %p:%s to container %p %d",
|
||
|
swayc, title, sibling, sibling ? sibling->type : 0);
|
||
9 years ago
|
// Setup values
|
||
7 years ago
|
swayc->_handle.view = view;
|
||
|
|
||
|
swayc->name = title ? strdup(title) : NULL;
|
||
|
|
||
|
const char *class = view->iface.get_prop(view, VIEW_PROP_CLASS);
|
||
|
swayc->class = class ? strdup(class) : NULL;
|
||
|
|
||
|
const char *instance = view->iface.get_prop(view, VIEW_PROP_INSTANCE);
|
||
|
swayc->instance = instance ? strdup(instance) : NULL;
|
||
|
|
||
|
const char *app_id = view->iface.get_prop(view, VIEW_PROP_APP_ID);
|
||
|
swayc->app_id = app_id ? strdup(app_id) : NULL;
|
||
|
|
||
|
swayc->visible = true;
|
||
|
swayc->is_focused = true;
|
||
|
swayc->sticky = false;
|
||
|
swayc->width = 0;
|
||
|
swayc->height = 0;
|
||
|
swayc->desired_width = -1;
|
||
|
swayc->desired_height = -1;
|
||
9 years ago
|
// setup border
|
||
7 years ago
|
swayc->border_type = config->border;
|
||
|
swayc->border_thickness = config->border_thickness;
|
||
10 years ago
|
|
||
7 years ago
|
swayc->is_floating = false;
|
||
9 years ago
|
|
||
10 years ago
|
if (sibling->type == C_WORKSPACE) {
|
||
9 years ago
|
// Case of focused workspace, just create as child of it
|
||
7 years ago
|
add_child(sibling, swayc);
|
||
9 years ago
|
} else {
|
||
|
// Regular case, create as sibling of current container
|
||
7 years ago
|
add_sibling(sibling, swayc);
|
||
10 years ago
|
}
|
||
7 years ago
|
return swayc;
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
swayc_t *new_floating_view(wlc_handle handle) {
|
||
9 years ago
|
if (swayc_active_workspace() == NULL) {
|
||
|
return NULL;
|
||
|
}
|
||
9 years ago
|
const char *title = wlc_view_get_title(handle);
|
||
9 years ago
|
swayc_t *view = new_swayc(C_VIEW);
|
||
9 years ago
|
sway_log(L_DEBUG, "Adding new view %" PRIuPTR ":%x:%s as a floating view",
|
||
9 years ago
|
handle, wlc_view_get_type(handle), title);
|
||
9 years ago
|
// Setup values
|
||
9 years ago
|
view->handle = handle;
|
||
9 years ago
|
view->name = title ? strdup(title) : NULL;
|
||
9 years ago
|
const char *class = wlc_view_get_class(handle);
|
||
|
view->class = class ? strdup(class) : NULL;
|
||
8 years ago
|
const char *instance = wlc_view_get_instance(handle);
|
||
|
view->instance = instance ? strdup(instance) : NULL;
|
||
9 years ago
|
const char *app_id = wlc_view_get_app_id(handle);
|
||
|
view->app_id = app_id ? strdup(app_id) : NULL;
|
||
9 years ago
|
view->visible = true;
|
||
9 years ago
|
view->sticky = false;
|
||
9 years ago
|
|
||
|
// Set the geometry of the floating view
|
||
8 years ago
|
const struct wlc_geometry *geometry = wlc_view_get_geometry(handle);
|
||
9 years ago
|
|
||
8 years ago
|
// give it requested geometry, but place in center if possible
|
||
|
// in top left otherwise
|
||
8 years ago
|
if (geometry->size.w != 0) {
|
||
|
view->x = (swayc_active_workspace()->width - geometry->size.w) / 2;
|
||
8 years ago
|
} else {
|
||
|
view->x = 0;
|
||
|
}
|
||
8 years ago
|
if (geometry->size.h != 0) {
|
||
|
view->y = (swayc_active_workspace()->height - geometry->size.h) / 2;
|
||
8 years ago
|
} else {
|
||
|
view->y = 0;
|
||
|
}
|
||
8 years ago
|
|
||
|
view->width = geometry->size.w;
|
||
|
view->height = geometry->size.h;
|
||
9 years ago
|
|
||
9 years ago
|
view->desired_width = view->width;
|
||
|
view->desired_height = view->height;
|
||
9 years ago
|
|
||
9 years ago
|
// setup border
|
||
|
view->border_type = config->floating_border;
|
||
|
view->border_thickness = config->floating_border_thickness;
|
||
|
|
||
9 years ago
|
view->is_floating = true;
|
||
|
|
||
9 years ago
|
// Case of focused workspace, just create as child of it
|
||
9 years ago
|
list_add(swayc_active_workspace()->floating, view);
|
||
|
view->parent = swayc_active_workspace();
|
||
|
if (swayc_active_workspace()->focused == NULL) {
|
||
|
set_focused_container_for(swayc_active_workspace(), view);
|
||
9 years ago
|
}
|
||
|
return view;
|
||
|
}
|
||
|
|
||
9 years ago
|
void floating_view_sane_size(swayc_t *view) {
|
||
9 years ago
|
// floating_minimum is used as sane value.
|
||
|
// floating_maximum has priority in case of conflict
|
||
|
// TODO: implement total_outputs_dimensions()
|
||
9 years ago
|
if (config->floating_minimum_height != -1 &&
|
||
|
view->desired_height < config->floating_minimum_height) {
|
||
|
view->desired_height = config->floating_minimum_height;
|
||
|
}
|
||
|
if (config->floating_minimum_width != -1 &&
|
||
|
view->desired_width < config->floating_minimum_width) {
|
||
|
view->desired_width = config->floating_minimum_width;
|
||
|
}
|
||
|
|
||
9 years ago
|
// if 0 do not resize, only enforce max value
|
||
|
if (config->floating_maximum_height == 0) {
|
||
|
// Missing total_outputs_dimensions() using swayc_active_workspace()
|
||
|
config->floating_maximum_height = swayc_active_workspace()->height;
|
||
|
|
||
|
} else if (config->floating_maximum_height != -1 &&
|
||
8 years ago
|
view->desired_height > config->floating_maximum_height) {
|
||
|
view->desired_height = config->floating_maximum_height;
|
||
|
}
|
||
9 years ago
|
|
||
|
// if 0 do not resize, only enforce max value
|
||
|
if (config->floating_maximum_width == 0) {
|
||
|
// Missing total_outputs_dimensions() using swayc_active_workspace()
|
||
|
config->floating_maximum_width = swayc_active_workspace()->width;
|
||
|
|
||
|
} else if (config->floating_maximum_width != -1 &&
|
||
9 years ago
|
view->desired_width > config->floating_maximum_width) {
|
||
|
view->desired_width = config->floating_maximum_width;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
sway_log(L_DEBUG, "Sane values for view to %d x %d @ %.f, %.f",
|
||
9 years ago
|
view->desired_width, view->desired_height, view->x, view->y);
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
|
||
9 years ago
|
// Destroy container
|
||
10 years ago
|
|
||
|
swayc_t *destroy_output(swayc_t *output) {
|
||
9 years ago
|
if (!ASSERT_NONNULL(output)) {
|
||
|
return NULL;
|
||
|
}
|
||
9 years ago
|
if (output->children->length > 0) {
|
||
|
// TODO save workspaces when there are no outputs.
|
||
|
// TODO also check if there will ever be no outputs except for exiting
|
||
|
// program
|
||
9 years ago
|
if (root_container.children->length > 1) {
|
||
9 years ago
|
int p = root_container.children->items[0] == output;
|
||
|
// Move workspace from this output to another output
|
||
9 years ago
|
while (output->children->length) {
|
||
|
swayc_t *child = output->children->items[0];
|
||
9 years ago
|
remove_child(child);
|
||
|
add_child(root_container.children->items[p], child);
|
||
|
}
|
||
9 years ago
|
sort_workspaces(root_container.children->items[p]);
|
||
9 years ago
|
update_visibility(root_container.children->items[p]);
|
||
|
arrange_windows(root_container.children->items[p], -1, -1);
|
||
9 years ago
|
}
|
||
10 years ago
|
}
|
||
9 years ago
|
sway_log(L_DEBUG, "OUTPUT: Destroying output '%" PRIuPTR "'", output->handle);
|
||
10 years ago
|
free_swayc(output);
|
||
9 years ago
|
update_root_geometry();
|
||
10 years ago
|
return &root_container;
|
||
|
}
|
||
|
|
||
|
swayc_t *destroy_workspace(swayc_t *workspace) {
|
||
9 years ago
|
if (!ASSERT_NONNULL(workspace)) {
|
||
|
return NULL;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// Do not destroy this if it's the last workspace on this output
|
||
9 years ago
|
swayc_t *output = swayc_parent_by_type(workspace, C_OUTPUT);
|
||
|
if (output && output->children->length == 1) {
|
||
|
return NULL;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
swayc_t *parent = workspace->parent;
|
||
|
// destroy the WS if there are no children
|
||
9 years ago
|
if (workspace->children->length == 0 && workspace->floating->length == 0) {
|
||
9 years ago
|
sway_log(L_DEBUG, "destroying workspace '%s'", workspace->name);
|
||
9 years ago
|
ipc_event_workspace(workspace, NULL, "empty");
|
||
9 years ago
|
} else {
|
||
|
// Move children to a different workspace on this output
|
||
|
swayc_t *new_workspace = NULL;
|
||
|
int i;
|
||
|
for(i = 0; i < output->children->length; i++) {
|
||
|
if(output->children->items[i] != workspace) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
new_workspace = output->children->items[i];
|
||
|
|
||
|
sway_log(L_DEBUG, "moving children to different workspace '%s' -> '%s'",
|
||
|
workspace->name, new_workspace->name);
|
||
|
|
||
|
for(i = 0; i < workspace->children->length; i++) {
|
||
|
move_container_to(workspace->children->items[i], new_workspace);
|
||
|
}
|
||
|
|
||
|
for(i = 0; i < workspace->floating->length; i++) {
|
||
|
move_container_to(workspace->floating->items[i], new_workspace);
|
||
|
}
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
|
free_swayc(workspace);
|
||
|
return parent;
|
||
10 years ago
|
}
|
||
|
|
||
|
swayc_t *destroy_container(swayc_t *container) {
|
||
9 years ago
|
if (!ASSERT_NONNULL(container)) {
|
||
|
return NULL;
|
||
|
}
|
||
10 years ago
|
while (container->children->length == 0 && container->type == C_CONTAINER) {
|
||
|
sway_log(L_DEBUG, "Container: Destroying container '%p'", container);
|
||
|
swayc_t *parent = container->parent;
|
||
|
free_swayc(container);
|
||
|
container = parent;
|
||
|
}
|
||
|
return container;
|
||
|
}
|
||
|
|
||
|
swayc_t *destroy_view(swayc_t *view) {
|
||
9 years ago
|
if (!ASSERT_NONNULL(view)) {
|
||
10 years ago
|
return NULL;
|
||
|
}
|
||
|
sway_log(L_DEBUG, "Destroying view '%p'", view);
|
||
7 years ago
|
swayc_t *parent = view->parent;
|
||
10 years ago
|
free_swayc(view);
|
||
|
|
||
9 years ago
|
// Destroy empty containers
|
||
7 years ago
|
if (parent && parent->type == C_CONTAINER) {
|
||
10 years ago
|
return destroy_container(parent);
|
||
|
}
|
||
|
return parent;
|
||
|
}
|
||
|
|
||
9 years ago
|
// Container lookup
|
||
|
|
||
9 years ago
|
|
||
9 years ago
|
swayc_t *swayc_by_test(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data) {
|
||
9 years ago
|
if (!container->children) {
|
||
|
return NULL;
|
||
|
}
|
||
|
// Special case for checking floating stuff
|
||
|
int i;
|
||
|
if (container->type == C_WORKSPACE) {
|
||
|
for (i = 0; i < container->floating->length; ++i) {
|
||
|
swayc_t *child = container->floating->items[i];
|
||
|
if (test(child, data)) {
|
||
|
return child;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
for (i = 0; i < container->children->length; ++i) {
|
||
|
swayc_t *child = container->children->items[i];
|
||
|
if (test(child, data)) {
|
||
|
return child;
|
||
|
} else {
|
||
9 years ago
|
swayc_t *res = swayc_by_test(child, test, data);
|
||
9 years ago
|
if (res) {
|
||
|
return res;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
9 years ago
|
static bool test_name(swayc_t *view, void *data) {
|
||
9 years ago
|
if (!view || !view->name) {
|
||
9 years ago
|
return false;
|
||
9 years ago
|
}
|
||
9 years ago
|
return strcmp(view->name, data) == 0;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
swayc_t *swayc_by_name(const char *name) {
|
||
|
return swayc_by_test(&root_container, test_name, (void *)name);
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type) {
|
||
|
if (!ASSERT_NONNULL(container)) {
|
||
|
return NULL;
|
||
|
}
|
||
9 years ago
|
if (!sway_assert(type < C_TYPES && type >= C_ROOT, "invalid type")) {
|
||
9 years ago
|
return NULL;
|
||
|
}
|
||
|
do {
|
||
|
container = container->parent;
|
||
9 years ago
|
} while (container && container->type != type);
|
||
9 years ago
|
return container;
|
||
|
}
|
||
|
|
||
|
swayc_t *swayc_parent_by_layout(swayc_t *container, enum swayc_layouts layout) {
|
||
|
if (!ASSERT_NONNULL(container)) {
|
||
|
return NULL;
|
||
|
}
|
||
9 years ago
|
if (!sway_assert(layout < L_LAYOUTS && layout >= L_NONE, "invalid layout")) {
|
||
9 years ago
|
return NULL;
|
||
|
}
|
||
|
do {
|
||
|
container = container->parent;
|
||
|
} while (container && container->layout != layout);
|
||
|
return container;
|
||
|
}
|
||
|
|
||
9 years ago
|
swayc_t *swayc_focus_by_type(swayc_t *container, enum swayc_types type) {
|
||
|
if (!ASSERT_NONNULL(container)) {
|
||
|
return NULL;
|
||
|
}
|
||
9 years ago
|
if (!sway_assert(type < C_TYPES && type >= C_ROOT, "invalid type")) {
|
||
9 years ago
|
return NULL;
|
||
|
}
|
||
|
do {
|
||
|
container = container->focused;
|
||
|
} while (container && container->type != type);
|
||
|
return container;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
swayc_t *swayc_focus_by_layout(swayc_t *container, enum swayc_layouts layout) {
|
||
|
if (!ASSERT_NONNULL(container)) {
|
||
|
return NULL;
|
||
|
}
|
||
9 years ago
|
if (!sway_assert(layout < L_LAYOUTS && layout >= L_NONE, "invalid layout")) {
|
||
9 years ago
|
return NULL;
|
||
|
}
|
||
|
do {
|
||
|
container = container->focused;
|
||
|
} while (container && container->layout != layout);
|
||
|
return container;
|
||
|
}
|
||
|
|
||
9 years ago
|
|
||
|
static swayc_t *_swayc_by_handle_helper(wlc_handle handle, swayc_t *parent) {
|
||
|
if (!parent || !parent->children) {
|
||
|
return NULL;
|
||
|
}
|
||
|
int i, len;
|
||
|
swayc_t **child;
|
||
|
if (parent->type == C_WORKSPACE) {
|
||
|
len = parent->floating->length;
|
||
|
child = (swayc_t **)parent->floating->items;
|
||
|
for (i = 0; i < len; ++i, ++child) {
|
||
|
if ((*child)->handle == handle) {
|
||
|
return *child;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
len = parent->children->length;
|
||
|
child = (swayc_t**)parent->children->items;
|
||
|
for (i = 0; i < len; ++i, ++child) {
|
||
|
if ((*child)->handle == handle) {
|
||
|
return *child;
|
||
|
} else {
|
||
|
swayc_t *res;
|
||
|
if ((res = _swayc_by_handle_helper(handle, *child))) {
|
||
|
return res;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
swayc_t *swayc_by_handle(wlc_handle handle) {
|
||
|
return _swayc_by_handle_helper(handle, &root_container);
|
||
|
}
|
||
|
|
||
9 years ago
|
swayc_t *swayc_active_output(void) {
|
||
|
return root_container.focused;
|
||
|
}
|
||
|
|
||
|
swayc_t *swayc_active_workspace(void) {
|
||
|
return root_container.focused ? root_container.focused->focused : NULL;
|
||
|
}
|
||
|
|
||
|
swayc_t *swayc_active_workspace_for(swayc_t *cont) {
|
||
9 years ago
|
if (!cont) {
|
||
9 years ago
|
return NULL;
|
||
|
}
|
||
|
switch (cont->type) {
|
||
9 years ago
|
case C_ROOT:
|
||
|
cont = cont->focused;
|
||
|
/* Fallthrough */
|
||
|
|
||
|
case C_OUTPUT:
|
||
9 years ago
|
cont = cont ? cont->focused : NULL;
|
||
9 years ago
|
/* Fallthrough */
|
||
|
|
||
|
case C_WORKSPACE:
|
||
|
return cont;
|
||
|
|
||
|
default:
|
||
|
return swayc_parent_by_type(cont, C_WORKSPACE);
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
static bool pointer_test(swayc_t *view, void *_origin) {
|
||
9 years ago
|
const struct wlc_point *origin = _origin;
|
||
9 years ago
|
// Determine the output that the view is under
|
||
|
swayc_t *parent = swayc_parent_by_type(view, C_OUTPUT);
|
||
|
if (origin->x >= view->x && origin->y >= view->y
|
||
|
&& origin->x < view->x + view->width && origin->y < view->y + view->height
|
||
|
&& view->visible && parent == root_container.focused) {
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
swayc_t *container_under_pointer(void) {
|
||
|
// root.output->workspace
|
||
8 years ago
|
if (!root_container.focused) {
|
||
9 years ago
|
return NULL;
|
||
|
}
|
||
8 years ago
|
swayc_t *lookup = root_container.focused;
|
||
9 years ago
|
// Case of empty workspace
|
||
8 years ago
|
if (lookup->children && !lookup->unmanaged) {
|
||
9 years ago
|
return NULL;
|
||
|
}
|
||
8 years ago
|
double x, y;
|
||
|
wlc_pointer_get_position_v2(&x, &y);
|
||
|
struct wlc_point origin = { .x = x, .y = y };
|
||
|
|
||
9 years ago
|
while (lookup && lookup->type != C_VIEW) {
|
||
9 years ago
|
int i;
|
||
|
int len;
|
||
8 years ago
|
for (int _i = 0; lookup->unmanaged && _i < lookup->unmanaged->length; ++_i) {
|
||
|
wlc_handle *handle = lookup->unmanaged->items[_i];
|
||
|
const struct wlc_geometry *geo = wlc_view_get_geometry(*handle);
|
||
|
if (origin.x >= geo->origin.x && origin.y >= geo->origin.y
|
||
|
&& origin.x < geo->origin.x + (int)geo->size.w
|
||
|
&& origin.y < geo->origin.y + (int)geo->size.h) {
|
||
|
// Hack: we force focus upon unmanaged views here
|
||
|
wlc_view_focus(*handle);
|
||
|
return NULL;
|
||
|
}
|
||
|
}
|
||
9 years ago
|
// if tabbed/stacked go directly to focused container, otherwise search
|
||
|
// children
|
||
|
if (lookup->layout == L_TABBED || lookup->layout == L_STACKED) {
|
||
|
lookup = lookup->focused;
|
||
|
continue;
|
||
|
}
|
||
|
// if workspace, search floating
|
||
|
if (lookup->type == C_WORKSPACE) {
|
||
|
i = len = lookup->floating->length;
|
||
|
bool got_floating = false;
|
||
|
while (--i > -1) {
|
||
9 years ago
|
if (pointer_test(lookup->floating->items[i], &origin)) {
|
||
9 years ago
|
lookup = lookup->floating->items[i];
|
||
|
got_floating = true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
if (got_floating) {
|
||
|
continue;
|
||
|
}
|
||
|
}
|
||
|
// search children
|
||
|
len = lookup->children->length;
|
||
|
for (i = 0; i < len; ++i) {
|
||
9 years ago
|
if (pointer_test(lookup->children->items[i], &origin)) {
|
||
9 years ago
|
lookup = lookup->children->items[i];
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
// when border and titles are done, this could happen
|
||
|
if (i == len) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
return lookup;
|
||
|
}
|
||
|
|
||
8 years ago
|
swayc_t *container_find(swayc_t *container, bool (*f)(swayc_t *, const void *), const void *data) {
|
||
|
if (container->children == NULL || container->children->length == 0) {
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
8 years ago
|
swayc_t *con;
|
||
|
if (container->type == C_WORKSPACE) {
|
||
|
for (int i = 0; i < container->floating->length; ++i) {
|
||
|
con = container->floating->items[i];
|
||
|
if (f(con, data)) {
|
||
|
return con;
|
||
|
}
|
||
|
con = container_find(con, f, data);
|
||
|
if (con != NULL) {
|
||
|
return con;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
for (int i = 0; i < container->children->length; ++i) {
|
||
8 years ago
|
con = container->children->items[i];
|
||
|
if (f(con, data)) {
|
||
|
return con;
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
con = container_find(con, f, data);
|
||
|
if (con != NULL) {
|
||
|
return con;
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
9 years ago
|
// Container information
|
||
|
|
||
|
bool swayc_is_fullscreen(swayc_t *view) {
|
||
9 years ago
|
return view && view->type == C_VIEW && (wlc_view_get_state(view->handle) & WLC_BIT_FULLSCREEN);
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
bool swayc_is_active(swayc_t *view) {
|
||
9 years ago
|
return view && view->type == C_VIEW && (wlc_view_get_state(view->handle) & WLC_BIT_ACTIVATED);
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
bool swayc_is_parent_of(swayc_t *parent, swayc_t *child) {
|
||
|
while (child != &root_container) {
|
||
|
child = child->parent;
|
||
|
if (child == parent) {
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
bool swayc_is_child_of(swayc_t *child, swayc_t *parent) {
|
||
|
return swayc_is_parent_of(parent, child);
|
||
|
}
|
||
|
|
||
9 years ago
|
bool swayc_is_empty_workspace(swayc_t *container) {
|
||
|
return container->type == C_WORKSPACE && container->children->length == 0;
|
||
|
}
|
||
|
|
||
9 years ago
|
int swayc_gap(swayc_t *container) {
|
||
9 years ago
|
if (container->type == C_VIEW || container->type == C_CONTAINER) {
|
||
9 years ago
|
return container->gaps >= 0 ? container->gaps : config->gaps_inner;
|
||
|
} else if (container->type == C_WORKSPACE) {
|
||
9 years ago
|
int base = container->gaps >= 0 ? container->gaps : config->gaps_outer;
|
||
9 years ago
|
if (config->edge_gaps && !(config->smart_gaps && container->children->length == 1)) {
|
||
9 years ago
|
// the inner gap is created via a margin around each window which
|
||
|
// is half the gap size, so the workspace also needs half a gap
|
||
|
// size to make the outermost gap the same size (excluding the
|
||
|
// actual "outer gap" size which is handled independently)
|
||
|
return base + config->gaps_inner / 2;
|
||
9 years ago
|
} else if (config->smart_gaps && container->children->length == 1) {
|
||
|
return 0;
|
||
9 years ago
|
} else {
|
||
|
return base;
|
||
|
}
|
||
9 years ago
|
} else {
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
// Mapping
|
||
|
|
||
9 years ago
|
void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) {
|
||
|
if (container) {
|
||
|
int i;
|
||
|
if (container->children) {
|
||
|
for (i = 0; i < container->children->length; ++i) {
|
||
|
swayc_t *child = container->children->items[i];
|
||
|
container_map(child, f, data);
|
||
|
}
|
||
|
}
|
||
|
if (container->floating) {
|
||
|
for (i = 0; i < container->floating->length; ++i) {
|
||
|
swayc_t *child = container->floating->items[i];
|
||
|
container_map(child, f, data);
|
||
|
}
|
||
|
}
|
||
7 years ago
|
f(container, data);
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
void update_visibility_output(swayc_t *container, wlc_handle output) {
|
||
|
// Inherit visibility
|
||
|
swayc_t *parent = container->parent;
|
||
|
container->visible = parent->visible;
|
||
|
// special cases where visibility depends on focus
|
||
9 years ago
|
if (parent->type == C_OUTPUT || parent->layout == L_TABBED ||
|
||
|
parent->layout == L_STACKED) {
|
||
9 years ago
|
container->visible = parent->focused == container && parent->visible;
|
||
9 years ago
|
}
|
||
|
// Set visibility and output for view
|
||
|
if (container->type == C_VIEW) {
|
||
|
wlc_view_set_output(container->handle, output);
|
||
|
wlc_view_set_mask(container->handle, container->visible ? VISIBLE : 0);
|
||
|
}
|
||
|
// Update visibility for children
|
||
9 years ago
|
else {
|
||
|
if (container->children) {
|
||
|
int i, len = container->children->length;
|
||
|
for (i = 0; i < len; ++i) {
|
||
|
update_visibility_output(container->children->items[i], output);
|
||
|
}
|
||
|
}
|
||
|
if (container->floating) {
|
||
|
int i, len = container->floating->length;
|
||
|
for (i = 0; i < len; ++i) {
|
||
|
update_visibility_output(container->floating->items[i], output);
|
||
|
}
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
|
void update_visibility(swayc_t *container) {
|
||
9 years ago
|
if (!container) return;
|
||
|
switch (container->type) {
|
||
|
case C_ROOT:
|
||
|
container->visible = true;
|
||
|
if (container->children) {
|
||
|
int i, len = container->children->length;
|
||
|
for (i = 0; i < len; ++i) {
|
||
|
update_visibility(container->children->items[i]);
|
||
|
}
|
||
|
}
|
||
9 years ago
|
return;
|
||
9 years ago
|
|
||
|
case C_OUTPUT:
|
||
|
container->visible = true;
|
||
|
if (container->children) {
|
||
|
int i, len = container->children->length;
|
||
|
for (i = 0; i < len; ++i) {
|
||
|
update_visibility_output(container->children->items[i], container->handle);
|
||
|
}
|
||
9 years ago
|
}
|
||
|
return;
|
||
9 years ago
|
|
||
|
default:
|
||
|
{
|
||
|
swayc_t *op = swayc_parent_by_type(container, C_OUTPUT);
|
||
|
update_visibility_output(container, op->handle);
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
void set_gaps(swayc_t *view, void *_data) {
|
||
|
int *data = _data;
|
||
|
if (!ASSERT_NONNULL(view)) {
|
||
|
return;
|
||
|
}
|
||
|
if (view->type == C_WORKSPACE || view->type == C_VIEW) {
|
||
|
view->gaps = *data;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void add_gaps(swayc_t *view, void *_data) {
|
||
|
int *data = _data;
|
||
|
if (!ASSERT_NONNULL(view)) {
|
||
|
return;
|
||
|
}
|
||
|
if (view->type == C_WORKSPACE || view->type == C_VIEW) {
|
||
|
if ((view->gaps += *data) < 0) {
|
||
|
view->gaps = 0;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
|
static void close_view(swayc_t *container, void *data) {
|
||
|
if (container->type == C_VIEW) {
|
||
|
wlc_view_close(container->handle);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void close_views(swayc_t *container) {
|
||
|
container_map(container, close_view, NULL);
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
swayc_t *swayc_tabbed_stacked_ancestor(swayc_t *view) {
|
||
9 years ago
|
swayc_t *parent = NULL;
|
||
|
if (!ASSERT_NONNULL(view)) {
|
||
|
return NULL;
|
||
|
}
|
||
9 years ago
|
while (view->type != C_WORKSPACE && view->parent && view->parent->type != C_WORKSPACE) {
|
||
9 years ago
|
view = view->parent;
|
||
9 years ago
|
if (view->layout == L_TABBED || view->layout == L_STACKED) {
|
||
|
parent = view;
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
return parent;
|
||
|
}
|
||
9 years ago
|
|
||
|
swayc_t *swayc_tabbed_stacked_parent(swayc_t *con) {
|
||
|
if (!ASSERT_NONNULL(con)) {
|
||
|
return NULL;
|
||
|
}
|
||
|
if (con->parent && (con->parent->layout == L_TABBED || con->parent->layout == L_STACKED)) {
|
||
|
return con->parent;
|
||
|
}
|
||
|
return NULL;
|
||
|
}
|
||
8 years ago
|
|
||
|
swayc_t *swayc_change_layout(swayc_t *container, enum swayc_layouts layout) {
|
||
8 years ago
|
// if layout change modifies the auto layout's major axis, swap width and height
|
||
|
// to preserve current ratios.
|
||
|
if (is_auto_layout(layout) && is_auto_layout(container->layout)) {
|
||
8 years ago
|
enum swayc_layouts prev_major =
|
||
|
container->layout == L_AUTO_LEFT || container->layout == L_AUTO_RIGHT
|
||
8 years ago
|
? L_HORIZ : L_VERT;
|
||
8 years ago
|
enum swayc_layouts new_major =
|
||
|
layout == L_AUTO_LEFT || layout == L_AUTO_RIGHT
|
||
8 years ago
|
? L_HORIZ : L_VERT;
|
||
|
if (new_major != prev_major) {
|
||
|
for (int i = 0; i < container->children->length; ++i) {
|
||
|
swayc_t *child = container->children->items[i];
|
||
|
double h = child->height;
|
||
|
child->height = child->width;
|
||
|
child->width = h;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
8 years ago
|
if (container->type == C_WORKSPACE) {
|
||
|
container->workspace_layout = layout;
|
||
8 years ago
|
if (layout == L_HORIZ || layout == L_VERT || is_auto_layout(layout)) {
|
||
|
container->layout = layout;
|
||
|
}
|
||
8 years ago
|
} else {
|
||
|
container->layout = layout;
|
||
|
}
|
||
|
return container;
|
||
|
}
|