2015-08-14 19:42:19 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <strings.h>
|
2015-08-18 00:34:53 +00:00
|
|
|
#include "config.h"
|
2015-08-10 20:31:23 +00:00
|
|
|
#include "container.h"
|
2015-08-14 19:42:19 +00:00
|
|
|
#include "workspace.h"
|
2015-08-18 18:22:52 +00:00
|
|
|
#include "focus.h"
|
2015-08-10 20:31:23 +00:00
|
|
|
#include "layout.h"
|
2015-08-14 19:42:19 +00:00
|
|
|
#include "log.h"
|
2015-08-10 20:31:23 +00:00
|
|
|
|
2015-08-20 16:52:54 +00:00
|
|
|
#define ASSERT_NONNULL(PTR) \
|
2015-08-26 18:01:26 +00:00
|
|
|
sway_assert (PTR, #PTR "must be non-null")
|
2015-08-14 19:42:19 +00:00
|
|
|
|
|
|
|
static swayc_t *new_swayc(enum swayc_types type) {
|
|
|
|
swayc_t *c = calloc(1, sizeof(swayc_t));
|
|
|
|
c->handle = -1;
|
|
|
|
c->layout = L_NONE;
|
|
|
|
c->type = type;
|
|
|
|
if (type != C_VIEW) {
|
|
|
|
c->children = create_list();
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2015-08-20 16:52:54 +00:00
|
|
|
static void free_swayc(swayc_t *cont) {
|
|
|
|
if (!ASSERT_NONNULL(cont)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (cont->children) {
|
2015-08-21 19:19:29 +00:00
|
|
|
// 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]);
|
2015-08-18 18:22:52 +00:00
|
|
|
}
|
2015-08-20 16:52:54 +00:00
|
|
|
list_free(cont->children);
|
2015-08-14 19:42:19 +00:00
|
|
|
}
|
2015-08-20 16:52:54 +00:00
|
|
|
if (cont->floating) {
|
|
|
|
if (cont->floating->length) {
|
2015-08-18 18:22:52 +00:00
|
|
|
int i;
|
2015-08-20 16:52:54 +00:00
|
|
|
for (i = 0; i < cont->floating->length; ++i) {
|
|
|
|
free_swayc(cont->floating->items[i]);
|
2015-08-18 18:22:52 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-20 16:52:54 +00:00
|
|
|
list_free(cont->floating);
|
2015-08-18 18:22:52 +00:00
|
|
|
}
|
2015-08-20 16:52:54 +00:00
|
|
|
if (cont->parent) {
|
|
|
|
remove_child(cont);
|
2015-08-14 19:42:19 +00:00
|
|
|
}
|
2015-08-20 16:52:54 +00:00
|
|
|
if (cont->name) {
|
|
|
|
free(cont->name);
|
2015-08-16 20:38:54 +00:00
|
|
|
}
|
2015-08-20 16:52:54 +00:00
|
|
|
free(cont);
|
2015-08-14 19:42:19 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 16:52:54 +00:00
|
|
|
// New containers
|
2015-08-14 19:42:19 +00:00
|
|
|
|
|
|
|
swayc_t *new_output(wlc_handle handle) {
|
2015-08-22 15:18:55 +00:00
|
|
|
const struct wlc_size *size = wlc_output_get_resolution(handle);
|
2015-08-16 17:07:43 +00:00
|
|
|
const char *name = wlc_output_get_name(handle);
|
2015-08-28 03:32:56 +00:00
|
|
|
// Find current outputs to see if this already exists
|
|
|
|
{
|
|
|
|
int i, len = root_container.children->length;
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
swayc_t *op = root_container.children->items[i];
|
|
|
|
const char *op_name = op->name;
|
|
|
|
if (op_name && name && strcmp(op_name, name) == 0) {
|
|
|
|
sway_log(L_DEBUG, "restoring output %lu:%s", handle, op_name);
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 07:28:44 +00:00
|
|
|
sway_log(L_DEBUG, "Added output %lu:%s", handle, name);
|
2015-08-14 19:42:19 +00:00
|
|
|
|
2015-08-22 18:44:47 +00:00
|
|
|
struct output_config *oc = NULL;
|
2015-08-22 15:18:55 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < config->output_configs->length; ++i) {
|
|
|
|
oc = config->output_configs->items[i];
|
|
|
|
if (strcasecmp(name, oc->name) == 0) {
|
|
|
|
sway_log(L_DEBUG, "Matched output config for %s", name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
oc = NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-23 17:22:45 +00:00
|
|
|
if (oc && !oc->enabled) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-23 17:22:33 +00:00
|
|
|
|
2015-08-14 19:42:19 +00:00
|
|
|
swayc_t *output = new_swayc(C_OUTPUT);
|
2015-08-22 15:18:55 +00:00
|
|
|
if (oc && oc->width != -1 && oc->height != -1) {
|
|
|
|
output->width = oc->width;
|
|
|
|
output->height = oc->height;
|
2015-08-22 18:03:51 +00:00
|
|
|
struct wlc_size new_size = { .w = oc->width, .h = oc->height };
|
2015-08-22 15:18:55 +00:00
|
|
|
wlc_output_set_resolution(handle, &new_size);
|
|
|
|
} else {
|
|
|
|
output->width = size->w;
|
|
|
|
output->height = size->h;
|
|
|
|
}
|
2015-08-15 19:10:44 +00:00
|
|
|
output->handle = handle;
|
2015-08-17 17:02:09 +00:00
|
|
|
output->name = name ? strdup(name) : NULL;
|
2015-08-26 20:35:22 +00:00
|
|
|
output->gaps = config->gaps_outer;
|
2015-08-22 18:44:47 +00:00
|
|
|
|
|
|
|
// Find position for it
|
|
|
|
if (oc && oc->x != -1 && oc->y != -1) {
|
2015-08-22 19:19:02 +00:00
|
|
|
sway_log(L_DEBUG, "Set %s position to %d, %d", name, oc->x, oc->y);
|
2015-08-22 18:44:47 +00:00
|
|
|
output->x = oc->x;
|
|
|
|
output->y = oc->y;
|
|
|
|
} else {
|
|
|
|
int x = 0;
|
|
|
|
for (i = 0; i < root_container.children->length; ++i) {
|
|
|
|
swayc_t *c = root_container.children->items[i];
|
|
|
|
if (c->type == C_OUTPUT) {
|
|
|
|
if (c->width + c->x > x) {
|
|
|
|
x = c->width + c->x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output->x = x;
|
|
|
|
}
|
2015-08-14 19:42:19 +00:00
|
|
|
|
2015-08-16 07:46:58 +00:00
|
|
|
add_child(&root_container, output);
|
2015-08-16 12:10:56 +00:00
|
|
|
|
2015-08-18 11:19:20 +00:00
|
|
|
// Create workspace
|
2015-08-18 00:34:53 +00:00
|
|
|
char *ws_name = NULL;
|
|
|
|
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);
|
2015-08-18 12:20:59 +00:00
|
|
|
// Check if any other workspaces are using this name
|
2015-08-21 17:28:37 +00:00
|
|
|
if (workspace_by_name(wso->workspace)) {
|
2015-08-19 23:29:35 +00:00
|
|
|
sway_log(L_DEBUG, "But it's already taken");
|
2015-08-18 12:20:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-08-19 23:29:35 +00:00
|
|
|
sway_log(L_DEBUG, "So we're going to use it");
|
2015-08-18 00:34:53 +00:00
|
|
|
ws_name = strdup(wso->workspace);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ws_name) {
|
|
|
|
ws_name = workspace_next_name();
|
|
|
|
}
|
2015-08-18 11:19:20 +00:00
|
|
|
|
|
|
|
// create and initilize default workspace
|
2015-08-18 07:28:44 +00:00
|
|
|
swayc_t *ws = new_workspace(output, ws_name);
|
|
|
|
ws->is_focused = true;
|
|
|
|
|
2015-08-14 19:42:19 +00:00
|
|
|
free(ws_name);
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2015-08-18 00:34:53 +00:00
|
|
|
swayc_t *new_workspace(swayc_t *output, const char *name) {
|
2015-08-20 16:52:54 +00:00
|
|
|
if (!ASSERT_NONNULL(output)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-17 02:16:09 +00:00
|
|
|
sway_log(L_DEBUG, "Added workspace %s for output %u", name, (unsigned int)output->handle);
|
2015-08-14 19:42:19 +00:00
|
|
|
swayc_t *workspace = new_swayc(C_WORKSPACE);
|
|
|
|
|
2015-08-28 02:52:59 +00:00
|
|
|
// TODO: default_layout
|
|
|
|
if (config->default_layout != L_NONE) {
|
|
|
|
workspace->layout = config->default_layout;
|
|
|
|
} else if (config->default_orientation != L_NONE) {
|
|
|
|
workspace->layout = config->default_orientation;
|
|
|
|
} else if (output->width >= output->height) {
|
|
|
|
workspace->layout = L_HORIZ;
|
|
|
|
} else {
|
|
|
|
workspace->layout = L_VERT;
|
|
|
|
}
|
2015-08-18 21:53:57 +00:00
|
|
|
workspace->x = output->x;
|
|
|
|
workspace->y = output->y;
|
2015-08-15 19:10:44 +00:00
|
|
|
workspace->width = output->width;
|
|
|
|
workspace->height = output->height;
|
|
|
|
workspace->name = strdup(name);
|
2015-08-26 22:44:47 +00:00
|
|
|
workspace->visible = false;
|
2015-08-17 05:38:34 +00:00
|
|
|
workspace->floating = create_list();
|
2015-08-14 19:42:19 +00:00
|
|
|
|
|
|
|
add_child(output, workspace);
|
|
|
|
return workspace;
|
|
|
|
}
|
|
|
|
|
|
|
|
swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) {
|
2015-08-21 17:28:37 +00:00
|
|
|
if (!ASSERT_NONNULL(child)
|
|
|
|
&& !sway_assert(!child->is_floating, "cannot create container around floating window")) {
|
2015-08-20 16:52:54 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-14 19:42:19 +00:00
|
|
|
swayc_t *cont = new_swayc(C_CONTAINER);
|
|
|
|
|
|
|
|
sway_log(L_DEBUG, "creating container %p around %p", cont, child);
|
|
|
|
|
2015-08-15 19:10:44 +00:00
|
|
|
cont->layout = layout;
|
2015-08-15 19:12:42 +00:00
|
|
|
cont->width = child->width;
|
2015-08-15 19:10:44 +00:00
|
|
|
cont->height = child->height;
|
2015-08-17 17:02:09 +00:00
|
|
|
cont->x = child->x;
|
2015-08-15 19:10:44 +00:00
|
|
|
cont->y = child->y;
|
|
|
|
cont->visible = child->visible;
|
2015-08-14 19:42:19 +00:00
|
|
|
|
2015-08-15 18:19:44 +00:00
|
|
|
/* Container inherits all of workspaces children, layout and whatnot */
|
|
|
|
if (child->type == C_WORKSPACE) {
|
|
|
|
swayc_t *workspace = child;
|
2015-08-18 11:19:20 +00:00
|
|
|
// reorder focus
|
2015-08-15 18:19:44 +00:00
|
|
|
cont->focused = workspace->focused;
|
|
|
|
workspace->focused = cont;
|
2015-08-18 11:19:20 +00:00
|
|
|
// set all children focu to container
|
2015-08-18 09:46:14 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < workspace->children->length; ++i) {
|
|
|
|
((swayc_t *)workspace->children->items[i])->parent = cont;
|
|
|
|
}
|
2015-08-18 11:19:20 +00:00
|
|
|
// Swap children
|
2015-08-15 18:19:44 +00:00
|
|
|
list_t *tmp_list = workspace->children;
|
|
|
|
workspace->children = cont->children;
|
|
|
|
cont->children = tmp_list;
|
2015-08-18 11:19:20 +00:00
|
|
|
// add container to workspace chidren
|
2015-08-15 18:19:44 +00:00
|
|
|
add_child(workspace, cont);
|
2015-08-18 11:19:20 +00:00
|
|
|
// give them proper layouts
|
2015-08-15 18:19:44 +00:00
|
|
|
cont->layout = workspace->layout;
|
|
|
|
workspace->layout = layout;
|
2015-08-19 23:15:17 +00:00
|
|
|
set_focused_container_for(workspace, get_focused_view(workspace));
|
2015-08-18 11:19:20 +00:00
|
|
|
} else { // Or is built around container
|
2015-08-15 18:19:44 +00:00
|
|
|
swayc_t *parent = replace_child(child, cont);
|
|
|
|
if (parent) {
|
|
|
|
add_child(cont, child);
|
|
|
|
}
|
2015-08-14 19:42:19 +00:00
|
|
|
}
|
|
|
|
return cont;
|
|
|
|
}
|
|
|
|
|
|
|
|
swayc_t *new_view(swayc_t *sibling, wlc_handle handle) {
|
2015-08-20 16:52:54 +00:00
|
|
|
if (!ASSERT_NONNULL(sibling)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-18 07:28:44 +00:00
|
|
|
const char *title = wlc_view_get_title(handle);
|
2015-08-14 19:42:19 +00:00
|
|
|
swayc_t *view = new_swayc(C_VIEW);
|
2015-08-18 07:28:44 +00:00
|
|
|
sway_log(L_DEBUG, "Adding new view %lu:%s to container %p %d",
|
|
|
|
handle, title, sibling, sibling ? sibling->type : 0);
|
2015-08-18 11:19:20 +00:00
|
|
|
// Setup values
|
2015-08-15 19:10:44 +00:00
|
|
|
view->handle = handle;
|
2015-08-17 17:02:09 +00:00
|
|
|
view->name = title ? strdup(title) : NULL;
|
2015-08-14 19:42:19 +00:00
|
|
|
view->visible = true;
|
2015-08-18 07:28:44 +00:00
|
|
|
view->is_focused = true;
|
2015-08-20 12:06:22 +00:00
|
|
|
// Setup geometry
|
2015-08-20 16:52:54 +00:00
|
|
|
const struct wlc_geometry* geometry = wlc_view_get_geometry(handle);
|
2015-08-20 11:57:01 +00:00
|
|
|
view->width = 0;
|
|
|
|
view->height = 0;
|
2015-08-20 16:52:54 +00:00
|
|
|
view->desired_width = geometry->size.w;
|
|
|
|
view->desired_height = geometry->size.h;
|
2015-08-14 19:42:19 +00:00
|
|
|
|
2015-08-18 21:53:57 +00:00
|
|
|
view->gaps = config->gaps_inner;
|
|
|
|
|
2015-08-17 05:38:34 +00:00
|
|
|
view->is_floating = false;
|
|
|
|
|
2015-08-14 19:42:19 +00:00
|
|
|
if (sibling->type == C_WORKSPACE) {
|
2015-08-18 11:19:20 +00:00
|
|
|
// Case of focused workspace, just create as child of it
|
2015-08-14 19:42:19 +00:00
|
|
|
add_child(sibling, view);
|
2015-08-18 11:19:20 +00:00
|
|
|
} else {
|
|
|
|
// Regular case, create as sibling of current container
|
2015-08-14 19:42:19 +00:00
|
|
|
add_sibling(sibling, view);
|
|
|
|
}
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2015-08-17 15:18:06 +00:00
|
|
|
swayc_t *new_floating_view(wlc_handle handle) {
|
2015-08-21 17:28:37 +00:00
|
|
|
if (swayc_active_workspace() == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-18 11:19:20 +00:00
|
|
|
const char *title = wlc_view_get_title(handle);
|
2015-08-17 15:18:06 +00:00
|
|
|
swayc_t *view = new_swayc(C_VIEW);
|
2015-08-18 07:28:44 +00:00
|
|
|
sway_log(L_DEBUG, "Adding new view %lu:%x:%s as a floating view",
|
|
|
|
handle, wlc_view_get_type(handle), title);
|
2015-08-18 11:19:20 +00:00
|
|
|
// Setup values
|
2015-08-17 15:18:06 +00:00
|
|
|
view->handle = handle;
|
2015-08-17 17:02:09 +00:00
|
|
|
view->name = title ? strdup(title) : NULL;
|
2015-08-17 15:18:06 +00:00
|
|
|
view->visible = true;
|
|
|
|
|
|
|
|
// Set the geometry of the floating view
|
|
|
|
const struct wlc_geometry* geometry = wlc_view_get_geometry(handle);
|
|
|
|
|
2015-08-20 12:06:22 +00:00
|
|
|
// give it requested geometry, but place in center
|
2015-08-21 17:28:37 +00:00
|
|
|
view->x = (swayc_active_workspace()->width - geometry->size.w) / 2;
|
|
|
|
view->y = (swayc_active_workspace()->height- geometry->size.h) / 2;
|
2015-08-17 15:18:06 +00:00
|
|
|
view->width = geometry->size.w;
|
|
|
|
view->height = geometry->size.h;
|
|
|
|
|
2015-08-17 15:34:39 +00:00
|
|
|
view->desired_width = view->width;
|
|
|
|
view->desired_height = view->height;
|
2015-08-17 15:18:06 +00:00
|
|
|
|
|
|
|
view->is_floating = true;
|
|
|
|
|
2015-08-18 11:19:20 +00:00
|
|
|
// Case of focused workspace, just create as child of it
|
2015-08-21 17:28:37 +00:00
|
|
|
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);
|
2015-08-17 15:18:06 +00:00
|
|
|
}
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2015-08-20 16:52:54 +00:00
|
|
|
// Destroy container
|
2015-08-14 19:42:19 +00:00
|
|
|
|
|
|
|
swayc_t *destroy_output(swayc_t *output) {
|
2015-08-20 16:52:54 +00:00
|
|
|
if (!ASSERT_NONNULL(output)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-27 04:25:57 +00:00
|
|
|
if (output->children->length > 0) {
|
|
|
|
int i, len = root_container.children->length;
|
|
|
|
// TODO save workspaces when there are no outputs.
|
|
|
|
// TODO also check if there will ever be no outputs except for exiting
|
|
|
|
// program
|
|
|
|
if (len > 1) {
|
|
|
|
len = output->children->length;
|
|
|
|
int p = root_container.children->items[0] == output;
|
|
|
|
// Move workspace from this output to another output
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
swayc_t *child = output->children->items[i];
|
|
|
|
remove_child(child);
|
|
|
|
add_child(root_container.children->items[p], child);
|
|
|
|
}
|
2015-08-27 15:32:06 +00:00
|
|
|
update_visibility(root_container.children->items[p]);
|
|
|
|
arrange_windows(root_container.children->items[p], -1, -1);
|
2015-08-27 04:25:57 +00:00
|
|
|
}
|
2015-08-14 19:42:19 +00:00
|
|
|
}
|
2015-08-18 09:46:14 +00:00
|
|
|
sway_log(L_DEBUG, "OUTPUT: Destroying output '%lu'", output->handle);
|
2015-08-14 19:42:19 +00:00
|
|
|
free_swayc(output);
|
|
|
|
return &root_container;
|
|
|
|
}
|
|
|
|
|
|
|
|
swayc_t *destroy_workspace(swayc_t *workspace) {
|
2015-08-20 16:52:54 +00:00
|
|
|
if (!ASSERT_NONNULL(workspace)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-18 11:19:20 +00:00
|
|
|
// NOTE: This is called from elsewhere without checking children length
|
|
|
|
// TODO move containers to other workspaces?
|
|
|
|
// for now just dont delete
|
2015-08-18 12:20:59 +00:00
|
|
|
|
|
|
|
// Do not destroy this if it's the last workspace on this output
|
2015-08-20 16:52:54 +00:00
|
|
|
swayc_t *output = swayc_parent_by_type(workspace, C_OUTPUT);
|
|
|
|
if (output && output->children->length == 1) {
|
|
|
|
return NULL;
|
2015-08-18 12:20:59 +00:00
|
|
|
}
|
|
|
|
|
2015-08-23 01:25:05 +00:00
|
|
|
// Do not destroy if there are children
|
|
|
|
if (workspace->children->length == 0 && workspace->floating->length == 0) {
|
2015-08-28 03:55:48 +00:00
|
|
|
sway_log(L_DEBUG, "destroying workspace '%s'", workspace->name);
|
2015-08-14 19:42:19 +00:00
|
|
|
swayc_t *parent = workspace->parent;
|
|
|
|
free_swayc(workspace);
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
swayc_t *destroy_container(swayc_t *container) {
|
2015-08-20 16:52:54 +00:00
|
|
|
if (!ASSERT_NONNULL(container)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-14 19:42:19 +00:00
|
|
|
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) {
|
2015-08-20 16:52:54 +00:00
|
|
|
if (!ASSERT_NONNULL(view)) {
|
2015-08-14 19:42:19 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
sway_log(L_DEBUG, "Destroying view '%p'", view);
|
|
|
|
swayc_t *parent = view->parent;
|
|
|
|
free_swayc(view);
|
|
|
|
|
2015-08-18 11:19:20 +00:00
|
|
|
// Destroy empty containers
|
2015-08-14 19:42:19 +00:00
|
|
|
if (parent->type == C_CONTAINER) {
|
|
|
|
return destroy_container(parent);
|
|
|
|
}
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
2015-08-20 16:52:54 +00:00
|
|
|
// Container lookup
|
|
|
|
|
2015-08-21 17:28:37 +00:00
|
|
|
|
|
|
|
swayc_t *swayc_by_test(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data) {
|
|
|
|
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 {
|
|
|
|
swayc_t *res = swayc_by_test(child, test, data);
|
|
|
|
if (res) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-28 03:32:56 +00:00
|
|
|
static bool test_name(swayc_t *view, void *data) {
|
|
|
|
if (!view && !view->name) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return strcmp(view->name, data) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
swayc_t *swayc_by_name(const char *name) {
|
|
|
|
return swayc_by_test(&root_container, test_name, (void *)name);
|
|
|
|
}
|
|
|
|
|
2015-08-20 16:52:54 +00:00
|
|
|
swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type) {
|
|
|
|
if (!ASSERT_NONNULL(container)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-26 18:01:26 +00:00
|
|
|
if (!sway_assert(type < C_TYPES && type >= C_ROOT, "invalid type")) {
|
2015-08-20 16:52:54 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
container = container->parent;
|
2015-08-21 19:41:54 +00:00
|
|
|
} while (container && container->type != type);
|
2015-08-20 16:52:54 +00:00
|
|
|
return container;
|
|
|
|
}
|
|
|
|
|
|
|
|
swayc_t *swayc_parent_by_layout(swayc_t *container, enum swayc_layouts layout) {
|
|
|
|
if (!ASSERT_NONNULL(container)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-26 18:01:26 +00:00
|
|
|
if (!sway_assert(layout < L_LAYOUTS && layout >= L_NONE, "invalid layout")) {
|
2015-08-20 16:52:54 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
container = container->parent;
|
|
|
|
} while (container && container->layout != layout);
|
|
|
|
return container;
|
|
|
|
}
|
|
|
|
|
2015-08-24 08:11:21 +00:00
|
|
|
swayc_t *swayc_focus_by_type(swayc_t *container, enum swayc_types type) {
|
|
|
|
if (!ASSERT_NONNULL(container)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-26 18:01:26 +00:00
|
|
|
if (!sway_assert(type < C_TYPES && type >= C_ROOT, "invalid type")) {
|
2015-08-24 08:11:21 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
container = container->focused;
|
|
|
|
} while (container && container->type != type);
|
|
|
|
return container;
|
|
|
|
}
|
2015-08-26 18:01:26 +00:00
|
|
|
|
2015-08-24 08:11:21 +00:00
|
|
|
swayc_t *swayc_focus_by_layout(swayc_t *container, enum swayc_layouts layout) {
|
|
|
|
if (!ASSERT_NONNULL(container)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-26 18:01:26 +00:00
|
|
|
if (!sway_assert(layout < L_LAYOUTS && layout >= L_NONE, "invalid layout")) {
|
2015-08-24 08:11:21 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
container = container->focused;
|
|
|
|
} while (container && container->layout != layout);
|
|
|
|
return container;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-21 17:28:37 +00:00
|
|
|
static swayc_t *_swayc_by_handle_helper(wlc_handle handle, swayc_t *parent) {
|
|
|
|
if (!parent || !parent->children) {
|
2015-08-14 19:42:19 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-21 17:28:37 +00:00
|
|
|
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;
|
2015-08-17 05:38:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-21 17:28:37 +00:00
|
|
|
|
|
|
|
len = parent->children->length;
|
|
|
|
child = (swayc_t**)parent->children->items;
|
|
|
|
for (i = 0; i < len; ++i, ++child) {
|
|
|
|
if ((*child)->handle == handle) {
|
|
|
|
return *child;
|
2015-08-14 19:42:19 +00:00
|
|
|
} else {
|
2015-08-21 17:28:37 +00:00
|
|
|
swayc_t *res;
|
|
|
|
if ((res = _swayc_by_handle_helper(handle, *child))) {
|
2015-08-17 05:38:34 +00:00
|
|
|
return res;
|
2015-08-14 19:42:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-21 17:28:37 +00:00
|
|
|
swayc_t *swayc_by_handle(wlc_handle handle) {
|
|
|
|
return _swayc_by_handle_helper(handle, &root_container);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2015-08-21 19:41:54 +00:00
|
|
|
if (!cont) {
|
2015-08-21 17:28:37 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
switch (cont->type) {
|
2015-08-21 19:41:54 +00:00
|
|
|
case C_ROOT:
|
|
|
|
cont = cont->focused;
|
|
|
|
/* Fallthrough */
|
|
|
|
|
|
|
|
case C_OUTPUT:
|
2015-08-25 20:29:33 +00:00
|
|
|
cont = cont ? cont->focused : NULL;
|
2015-08-21 19:41:54 +00:00
|
|
|
/* Fallthrough */
|
|
|
|
|
|
|
|
case C_WORKSPACE:
|
|
|
|
return cont;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return swayc_parent_by_type(cont, C_WORKSPACE);
|
2015-08-21 17:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Container information
|
|
|
|
|
|
|
|
bool swayc_is_fullscreen(swayc_t *view) {
|
|
|
|
return view && view->type == C_VIEW && (wlc_view_get_state(view->handle) & WLC_BIT_FULLSCREEN);
|
|
|
|
}
|
|
|
|
|
2015-08-26 18:01:26 +00:00
|
|
|
bool swayc_is_active(swayc_t *view) {
|
|
|
|
return view && view->type == C_VIEW && (wlc_view_get_state(view->handle) & WLC_BIT_ACTIVATED);
|
|
|
|
}
|
|
|
|
|
2015-08-21 17:28:37 +00:00
|
|
|
// Mapping
|
|
|
|
|
2015-08-14 19:42:19 +00:00
|
|
|
void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) {
|
2015-08-23 01:25:05 +00:00
|
|
|
if (container) {
|
2015-08-26 22:44:47 +00:00
|
|
|
f(container, data);
|
2015-08-20 16:52:54 +00:00
|
|
|
int i;
|
2015-08-23 01:25:05 +00:00
|
|
|
if (container->children) {
|
|
|
|
for (i = 0; i < container->children->length; ++i) {
|
|
|
|
swayc_t *child = container->children->items[i];
|
|
|
|
container_map(child, f, data);
|
|
|
|
}
|
2015-08-18 18:22:52 +00:00
|
|
|
}
|
2015-08-23 01:25:05 +00:00
|
|
|
if (container->floating) {
|
2015-08-20 16:52:54 +00:00
|
|
|
for (i = 0; i < container->floating->length; ++i) {
|
|
|
|
swayc_t *child = container->floating->items[i];
|
|
|
|
container_map(child, f, data);
|
|
|
|
}
|
|
|
|
}
|
2015-08-18 18:22:52 +00:00
|
|
|
}
|
2015-08-10 20:31:23 +00:00
|
|
|
}
|
|
|
|
|
2015-08-17 02:06:31 +00:00
|
|
|
void set_view_visibility(swayc_t *view, void *data) {
|
2015-08-20 16:52:54 +00:00
|
|
|
if (!ASSERT_NONNULL(view)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-26 23:50:47 +00:00
|
|
|
// TODO add something like this.
|
|
|
|
// if (container->type == C_ROOT) {
|
|
|
|
// container->visible = true;
|
|
|
|
// } else {
|
|
|
|
// // Inherit visibility
|
|
|
|
// swayc_t *parent = container->parent;
|
|
|
|
// container->visible = parent->visible;
|
|
|
|
// // special cases where visibility depends on focus
|
|
|
|
// if (parent->type == C_OUTPUT || parent->layout == L_TABBED ||
|
|
|
|
// parent->layout == L_STACKED) {
|
|
|
|
// container->visible = parent->focused == container;
|
|
|
|
// }
|
|
|
|
// }
|
2015-08-25 18:13:35 +00:00
|
|
|
bool visible = *(bool *)data;
|
2015-08-17 02:06:31 +00:00
|
|
|
if (view->type == C_VIEW) {
|
2015-08-25 18:43:37 +00:00
|
|
|
wlc_view_set_output(view->handle, swayc_parent_by_type(view, C_OUTPUT)->handle);
|
2015-08-25 18:13:35 +00:00
|
|
|
wlc_view_set_mask(view->handle, visible ? VISIBLE : 0);
|
|
|
|
if (visible) {
|
2015-08-17 02:06:31 +00:00
|
|
|
wlc_view_bring_to_front(view->handle);
|
|
|
|
} else {
|
|
|
|
wlc_view_send_to_back(view->handle);
|
|
|
|
}
|
|
|
|
}
|
2015-08-25 18:13:35 +00:00
|
|
|
view->visible = visible;
|
|
|
|
sway_log(L_DEBUG, "Container %p is now %s", view, visible ? "visible" : "invisible");
|
2015-08-25 16:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void update_visibility(swayc_t *container) {
|
2015-08-28 03:55:48 +00:00
|
|
|
if (!container) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
swayc_t *ws;
|
|
|
|
if (container->type == C_ROOT || container->type == C_OUTPUT) {
|
|
|
|
int i, len = container->children->length;
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
update_visibility(container->children->items[i]);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (container->type == C_WORKSPACE) {
|
|
|
|
container->visible = container->parent->focused == container;
|
|
|
|
ws = container;
|
|
|
|
} else {
|
|
|
|
ws = swayc_active_workspace_for(container);
|
|
|
|
}
|
2015-08-26 18:01:26 +00:00
|
|
|
// TODO better visibility setting
|
2015-08-25 18:43:37 +00:00
|
|
|
bool visible = (ws->parent->focused == ws);
|
2015-08-25 16:24:15 +00:00
|
|
|
sway_log(L_DEBUG, "Setting visibility of container %p to %s", container, visible ? "visible" : "invisible");
|
2015-08-25 18:13:35 +00:00
|
|
|
container_map(ws, set_view_visibility, &visible);
|
2015-08-17 02:06:31 +00:00
|
|
|
}
|
2015-08-19 02:05:58 +00:00
|
|
|
|
|
|
|
void reset_gaps(swayc_t *view, void *data) {
|
2015-08-20 16:52:54 +00:00
|
|
|
if (!ASSERT_NONNULL(view)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-19 02:05:58 +00:00
|
|
|
if (view->type == C_OUTPUT) {
|
|
|
|
view->gaps = config->gaps_outer;
|
|
|
|
}
|
|
|
|
if (view->type == C_VIEW) {
|
|
|
|
view->gaps = config->gaps_inner;
|
|
|
|
}
|
|
|
|
}
|