2015-12-18 11:01:02 +11:00
|
|
|
#include <ctype.h>
|
2015-08-15 05:42:19 +10:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <strings.h>
|
2015-09-18 21:27:35 +10:00
|
|
|
#include <string.h>
|
2015-08-18 10:34:53 +10:00
|
|
|
#include "config.h"
|
2015-09-18 21:27:35 +10:00
|
|
|
#include "stringop.h"
|
2015-08-11 06:31:23 +10:00
|
|
|
#include "container.h"
|
2015-08-15 05:42:19 +10:00
|
|
|
#include "workspace.h"
|
2015-08-19 04:22:52 +10:00
|
|
|
#include "focus.h"
|
2015-08-11 06:31:23 +10:00
|
|
|
#include "layout.h"
|
2015-10-28 10:22:52 +11:00
|
|
|
#include "input_state.h"
|
2015-08-15 05:42:19 +10:00
|
|
|
#include "log.h"
|
2016-02-25 22:59:22 +11:00
|
|
|
#include "ipc-server.h"
|
2015-08-11 06:31:23 +10:00
|
|
|
|
2015-08-21 02:52:54 +10:00
|
|
|
#define ASSERT_NONNULL(PTR) \
|
2015-08-27 04:01:26 +10:00
|
|
|
sway_assert (PTR, #PTR "must be non-null")
|
2015-08-15 05:42:19 +10:00
|
|
|
|
|
|
|
static swayc_t *new_swayc(enum swayc_types type) {
|
|
|
|
swayc_t *c = calloc(1, sizeof(swayc_t));
|
|
|
|
c->handle = -1;
|
2015-09-05 09:14:59 +10:00
|
|
|
c->gaps = -1;
|
2015-08-15 05:42:19 +10:00
|
|
|
c->layout = L_NONE;
|
|
|
|
c->type = type;
|
2016-03-14 12:10:46 +11:00
|
|
|
c->border_type = B_PIXEL; // TODO: Load default from config
|
|
|
|
c->border_thickness = 2;
|
2015-08-15 05:42:19 +10:00
|
|
|
if (type != C_VIEW) {
|
|
|
|
c->children = create_list();
|
2016-03-14 12:10:46 +11:00
|
|
|
c->border_type = B_NONE;
|
2015-08-15 05:42:19 +10:00
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2015-08-21 02:52:54 +10:00
|
|
|
static void free_swayc(swayc_t *cont) {
|
|
|
|
if (!ASSERT_NONNULL(cont)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (cont->children) {
|
2015-08-22 05:19:29 +10: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-19 04:22:52 +10:00
|
|
|
}
|
2015-08-21 02:52:54 +10:00
|
|
|
list_free(cont->children);
|
2015-08-15 05:42:19 +10:00
|
|
|
}
|
2015-12-17 11:20:34 +11:00
|
|
|
if (cont->unmanaged) {
|
|
|
|
list_free(cont->unmanaged);
|
|
|
|
}
|
2015-08-21 02:52:54 +10:00
|
|
|
if (cont->floating) {
|
2015-09-01 01:49:24 +10:00
|
|
|
while (cont->floating->length) {
|
|
|
|
free_swayc(cont->floating->items[0]);
|
2015-08-19 04:22:52 +10:00
|
|
|
}
|
2015-08-21 02:52:54 +10:00
|
|
|
list_free(cont->floating);
|
2015-08-19 04:22:52 +10:00
|
|
|
}
|
2015-08-21 02:52:54 +10:00
|
|
|
if (cont->parent) {
|
|
|
|
remove_child(cont);
|
2015-08-15 05:42:19 +10:00
|
|
|
}
|
2015-08-21 02:52:54 +10:00
|
|
|
if (cont->name) {
|
|
|
|
free(cont->name);
|
2015-08-17 06:38:54 +10:00
|
|
|
}
|
2015-11-23 08:05:00 +11:00
|
|
|
if (cont->class) {
|
|
|
|
free(cont->class);
|
|
|
|
}
|
2015-11-23 08:09:33 +11:00
|
|
|
if (cont->app_id) {
|
|
|
|
free(cont->app_id);
|
|
|
|
}
|
2015-12-19 04:02:39 +11:00
|
|
|
if (cont->bg_pid != 0) {
|
|
|
|
terminate_swaybg(cont->bg_pid);
|
|
|
|
}
|
2015-08-21 02:52:54 +10:00
|
|
|
free(cont);
|
2015-08-15 05:42:19 +10:00
|
|
|
}
|
|
|
|
|
2015-08-21 02:52:54 +10:00
|
|
|
// New containers
|
2015-08-15 05:42:19 +10:00
|
|
|
|
|
|
|
swayc_t *new_output(wlc_handle handle) {
|
2015-09-14 09:46:16 +10:00
|
|
|
const struct wlc_size *size = wlc_output_get_resolution(handle);
|
2015-08-17 03:07:43 +10:00
|
|
|
const char *name = wlc_output_get_name(handle);
|
2015-08-28 13:32:56 +10:00
|
|
|
// Find current outputs to see if this already exists
|
2015-09-14 09:46:16 +10:00
|
|
|
{
|
|
|
|
int i, len = root_container.children->length;
|
2015-08-28 13:32:56 +10:00
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
swayc_t *op = root_container.children->items[i];
|
2015-09-14 09:46:16 +10:00
|
|
|
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);
|
2015-08-28 13:32:56 +10:00
|
|
|
return op;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 07:22:01 +11:00
|
|
|
sway_log(L_DEBUG, "New output %lu:%s", handle, name);
|
2015-08-15 05:42:19 +10:00
|
|
|
|
2016-02-12 23:22:20 +11:00
|
|
|
struct output_config *oc = NULL, *all = NULL;
|
2015-09-14 09:46:16 +10:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < config->output_configs->length; ++i) {
|
2015-12-03 08:04:50 +11:00
|
|
|
struct output_config *cur = config->output_configs->items[i];
|
|
|
|
if (strcasecmp(name, cur->name) == 0) {
|
2015-08-23 01:18:55 +10:00
|
|
|
sway_log(L_DEBUG, "Matched output config for %s", name);
|
2015-12-03 08:04:50 +11:00
|
|
|
oc = cur;
|
2015-08-23 01:18:55 +10:00
|
|
|
}
|
2015-12-03 08:04:50 +11:00
|
|
|
if (strcasecmp("*", cur->name) == 0) {
|
2015-11-29 02:18:37 +11:00
|
|
|
sway_log(L_DEBUG, "Matched wildcard output config for %s", name);
|
2016-02-12 23:22:20 +11:00
|
|
|
all = cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oc && all) {
|
2015-11-29 02:18:37 +11:00
|
|
|
break;
|
|
|
|
}
|
2015-08-23 01:18:55 +10:00
|
|
|
}
|
|
|
|
|
2016-02-12 23:22:20 +11:00
|
|
|
if (!oc) {
|
|
|
|
oc = all;
|
|
|
|
}
|
|
|
|
|
2015-08-24 03:22:45 +10:00
|
|
|
if (oc && !oc->enabled) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-24 03:22:33 +10:00
|
|
|
|
2015-08-15 05:42:19 +10:00
|
|
|
swayc_t *output = new_swayc(C_OUTPUT);
|
2015-08-16 05:10:44 +10:00
|
|
|
output->handle = handle;
|
2015-08-18 03:02:09 +10:00
|
|
|
output->name = name ? strdup(name) : NULL;
|
2015-10-22 01:34:12 +11:00
|
|
|
output->width = size->w;
|
|
|
|
output->height = size->h;
|
2015-12-17 11:20:34 +11:00
|
|
|
output->unmanaged = create_list();
|
2015-12-19 03:43:03 +11:00
|
|
|
output->bg_pid = 0;
|
2015-09-14 09:46:16 +10:00
|
|
|
|
2015-10-22 01:34:12 +11:00
|
|
|
apply_output_config(oc, output);
|
2015-09-12 19:38:03 +10:00
|
|
|
add_child(&root_container, output);
|
2015-08-18 21:19:20 +10:00
|
|
|
|
2015-09-14 09:46:16 +10:00
|
|
|
// Create workspace
|
|
|
|
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);
|
|
|
|
// Check if any other workspaces are using this name
|
|
|
|
if (workspace_by_name(wso->workspace)) {
|
|
|
|
sway_log(L_DEBUG, "But it's already taken");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sway_log(L_DEBUG, "So we're going to use it");
|
|
|
|
ws_name = strdup(wso->workspace);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ws_name) {
|
2016-03-05 02:44:49 +11:00
|
|
|
ws_name = workspace_next_name(output->name);
|
2015-09-14 09:46:16 +10:00
|
|
|
}
|
|
|
|
|
2015-08-18 21:19:20 +10:00
|
|
|
// create and initilize default workspace
|
2015-09-14 09:46:16 +10:00
|
|
|
swayc_t *ws = new_workspace(output, ws_name);
|
2015-08-18 17:28:44 +10:00
|
|
|
ws->is_focused = true;
|
|
|
|
|
2015-09-14 09:46:16 +10:00
|
|
|
free(ws_name);
|
2016-01-06 09:42:40 +11:00
|
|
|
|
2015-08-15 05:42:19 +10:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2015-08-18 10:34:53 +10:00
|
|
|
swayc_t *new_workspace(swayc_t *output, const char *name) {
|
2015-09-14 09:46:16 +10:00
|
|
|
if (!ASSERT_NONNULL(output)) {
|
|
|
|
return NULL;
|
2015-08-21 02:52:54 +10:00
|
|
|
}
|
2015-09-14 09:46:16 +10:00
|
|
|
sway_log(L_DEBUG, "Added workspace %s for output %u", name, (unsigned int)output->handle);
|
2015-08-15 05:42:19 +10:00
|
|
|
swayc_t *workspace = new_swayc(C_WORKSPACE);
|
2015-09-14 09:46:16 +10:00
|
|
|
|
|
|
|
// TODO: default_layout
|
2015-08-28 12:52:59 +10:00
|
|
|
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-19 07:53:57 +10:00
|
|
|
workspace->x = output->x;
|
|
|
|
workspace->y = output->y;
|
2015-08-16 05:10:44 +10:00
|
|
|
workspace->width = output->width;
|
|
|
|
workspace->height = output->height;
|
|
|
|
workspace->name = strdup(name);
|
2015-08-27 08:44:47 +10:00
|
|
|
workspace->visible = false;
|
2015-08-17 15:38:34 +10:00
|
|
|
workspace->floating = create_list();
|
2015-08-15 05:42:19 +10:00
|
|
|
|
2015-12-18 11:01:02 +11:00
|
|
|
if (isdigit(workspace->name[0])) {
|
|
|
|
// find position for numbered workspace
|
|
|
|
// order: ascending numbers, insert before same number
|
|
|
|
// numbers before unnumbered
|
|
|
|
int num = strtol(workspace->name, NULL, 10);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < output->children->length; ++i) {
|
|
|
|
char *name = ((swayc_t *)output->children->items[i])->name;
|
|
|
|
if (!isdigit(name[0]) || num <= strtol(name, NULL, 10)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
insert_child(output, workspace, i);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// append new unnumbered to the end
|
|
|
|
add_child(output, workspace);
|
|
|
|
}
|
2015-08-15 05:42:19 +10:00
|
|
|
return workspace;
|
|
|
|
}
|
|
|
|
|
|
|
|
swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) {
|
2015-08-22 03:28:37 +10:00
|
|
|
if (!ASSERT_NONNULL(child)
|
|
|
|
&& !sway_assert(!child->is_floating, "cannot create container around floating window")) {
|
2015-08-21 02:52:54 +10:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-15 05:42:19 +10:00
|
|
|
swayc_t *cont = new_swayc(C_CONTAINER);
|
|
|
|
|
|
|
|
sway_log(L_DEBUG, "creating container %p around %p", cont, child);
|
|
|
|
|
2015-08-16 05:10:44 +10:00
|
|
|
cont->layout = layout;
|
2015-08-16 05:12:42 +10:00
|
|
|
cont->width = child->width;
|
2015-08-16 05:10:44 +10:00
|
|
|
cont->height = child->height;
|
2015-08-18 03:02:09 +10:00
|
|
|
cont->x = child->x;
|
2015-08-16 05:10:44 +10:00
|
|
|
cont->y = child->y;
|
|
|
|
cont->visible = child->visible;
|
2015-08-15 05:42:19 +10:00
|
|
|
|
2015-08-16 04:19:44 +10:00
|
|
|
/* Container inherits all of workspaces children, layout and whatnot */
|
|
|
|
if (child->type == C_WORKSPACE) {
|
|
|
|
swayc_t *workspace = child;
|
2015-08-18 21:19:20 +10:00
|
|
|
// reorder focus
|
2015-08-16 04:19:44 +10:00
|
|
|
cont->focused = workspace->focused;
|
|
|
|
workspace->focused = cont;
|
2015-08-18 21:19:20 +10:00
|
|
|
// set all children focu to container
|
2015-08-18 19:46:14 +10:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < workspace->children->length; ++i) {
|
|
|
|
((swayc_t *)workspace->children->items[i])->parent = cont;
|
|
|
|
}
|
2015-08-18 21:19:20 +10:00
|
|
|
// Swap children
|
2015-08-16 04:19:44 +10:00
|
|
|
list_t *tmp_list = workspace->children;
|
|
|
|
workspace->children = cont->children;
|
|
|
|
cont->children = tmp_list;
|
2015-08-18 21:19:20 +10:00
|
|
|
// add container to workspace chidren
|
2015-08-16 04:19:44 +10:00
|
|
|
add_child(workspace, cont);
|
2015-08-18 21:19:20 +10:00
|
|
|
// give them proper layouts
|
2015-08-16 04:19:44 +10:00
|
|
|
cont->layout = workspace->layout;
|
|
|
|
workspace->layout = layout;
|
2015-08-20 09:15:17 +10:00
|
|
|
set_focused_container_for(workspace, get_focused_view(workspace));
|
2015-08-18 21:19:20 +10:00
|
|
|
} else { // Or is built around container
|
2015-08-16 04:19:44 +10:00
|
|
|
swayc_t *parent = replace_child(child, cont);
|
|
|
|
if (parent) {
|
|
|
|
add_child(cont, child);
|
|
|
|
}
|
2015-08-15 05:42:19 +10:00
|
|
|
}
|
|
|
|
return cont;
|
|
|
|
}
|
|
|
|
|
|
|
|
swayc_t *new_view(swayc_t *sibling, wlc_handle handle) {
|
2015-08-21 02:52:54 +10:00
|
|
|
if (!ASSERT_NONNULL(sibling)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-18 17:28:44 +10:00
|
|
|
const char *title = wlc_view_get_title(handle);
|
2015-08-15 05:42:19 +10:00
|
|
|
swayc_t *view = new_swayc(C_VIEW);
|
2015-08-18 17:28:44 +10:00
|
|
|
sway_log(L_DEBUG, "Adding new view %lu:%s to container %p %d",
|
|
|
|
handle, title, sibling, sibling ? sibling->type : 0);
|
2015-08-18 21:19:20 +10:00
|
|
|
// Setup values
|
2015-08-16 05:10:44 +10:00
|
|
|
view->handle = handle;
|
2015-08-18 03:02:09 +10:00
|
|
|
view->name = title ? strdup(title) : NULL;
|
2015-11-23 08:05:00 +11:00
|
|
|
const char *class = wlc_view_get_class(handle);
|
|
|
|
view->class = class ? strdup(class) : NULL;
|
2015-11-23 08:09:33 +11:00
|
|
|
const char *app_id = wlc_view_get_app_id(handle);
|
|
|
|
view->app_id = app_id ? strdup(app_id) : NULL;
|
2015-08-15 05:42:19 +10:00
|
|
|
view->visible = true;
|
2015-08-18 17:28:44 +10:00
|
|
|
view->is_focused = true;
|
2015-11-27 09:53:20 +11:00
|
|
|
view->sticky = false;
|
2015-08-20 22:06:22 +10:00
|
|
|
// Setup geometry
|
2015-08-21 02:52:54 +10:00
|
|
|
const struct wlc_geometry* geometry = wlc_view_get_geometry(handle);
|
2015-08-20 21:57:01 +10:00
|
|
|
view->width = 0;
|
|
|
|
view->height = 0;
|
2015-08-21 02:52:54 +10:00
|
|
|
view->desired_width = geometry->size.w;
|
|
|
|
view->desired_height = geometry->size.h;
|
2015-08-15 05:42:19 +10:00
|
|
|
|
2015-08-17 15:38:34 +10:00
|
|
|
view->is_floating = false;
|
|
|
|
|
2015-08-15 05:42:19 +10:00
|
|
|
if (sibling->type == C_WORKSPACE) {
|
2015-08-18 21:19:20 +10:00
|
|
|
// Case of focused workspace, just create as child of it
|
2015-08-15 05:42:19 +10:00
|
|
|
add_child(sibling, view);
|
2015-08-18 21:19:20 +10:00
|
|
|
} else {
|
|
|
|
// Regular case, create as sibling of current container
|
2015-08-15 05:42:19 +10:00
|
|
|
add_sibling(sibling, view);
|
|
|
|
}
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2015-08-18 01:18:06 +10:00
|
|
|
swayc_t *new_floating_view(wlc_handle handle) {
|
2015-08-22 03:28:37 +10:00
|
|
|
if (swayc_active_workspace() == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-18 21:19:20 +10:00
|
|
|
const char *title = wlc_view_get_title(handle);
|
2015-08-18 01:18:06 +10:00
|
|
|
swayc_t *view = new_swayc(C_VIEW);
|
2015-08-18 17:28:44 +10: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 21:19:20 +10:00
|
|
|
// Setup values
|
2015-08-18 01:18:06 +10:00
|
|
|
view->handle = handle;
|
2015-08-18 03:02:09 +10:00
|
|
|
view->name = title ? strdup(title) : NULL;
|
2015-11-23 08:05:00 +11:00
|
|
|
const char *class = wlc_view_get_class(handle);
|
|
|
|
view->class = class ? strdup(class) : NULL;
|
2015-11-23 08:09:33 +11:00
|
|
|
const char *app_id = wlc_view_get_app_id(handle);
|
|
|
|
view->app_id = app_id ? strdup(app_id) : NULL;
|
2015-08-18 01:18:06 +10:00
|
|
|
view->visible = true;
|
2015-11-27 09:53:20 +11:00
|
|
|
view->sticky = false;
|
2015-08-18 01:18:06 +10:00
|
|
|
|
|
|
|
// Set the geometry of the floating view
|
|
|
|
const struct wlc_geometry* geometry = wlc_view_get_geometry(handle);
|
|
|
|
|
2015-08-20 22:06:22 +10:00
|
|
|
// give it requested geometry, but place in center
|
2015-08-22 03:28:37 +10:00
|
|
|
view->x = (swayc_active_workspace()->width - geometry->size.w) / 2;
|
|
|
|
view->y = (swayc_active_workspace()->height- geometry->size.h) / 2;
|
2015-08-18 01:18:06 +10:00
|
|
|
view->width = geometry->size.w;
|
|
|
|
view->height = geometry->size.h;
|
|
|
|
|
2015-08-18 01:34:39 +10:00
|
|
|
view->desired_width = view->width;
|
|
|
|
view->desired_height = view->height;
|
2015-08-18 01:18:06 +10:00
|
|
|
|
|
|
|
view->is_floating = true;
|
|
|
|
|
2015-08-18 21:19:20 +10:00
|
|
|
// Case of focused workspace, just create as child of it
|
2015-08-22 03:28:37 +10: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-18 01:18:06 +10:00
|
|
|
}
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2015-08-21 02:52:54 +10:00
|
|
|
// Destroy container
|
2015-08-15 05:42:19 +10:00
|
|
|
|
|
|
|
swayc_t *destroy_output(swayc_t *output) {
|
2015-08-21 02:52:54 +10:00
|
|
|
if (!ASSERT_NONNULL(output)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-27 14:25:57 +10:00
|
|
|
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
|
2015-09-01 01:49:24 +10:00
|
|
|
if (root_container.children->length > 1) {
|
2015-08-27 14:25:57 +10:00
|
|
|
int p = root_container.children->items[0] == output;
|
|
|
|
// Move workspace from this output to another output
|
2015-09-01 01:49:24 +10:00
|
|
|
while (output->children->length) {
|
|
|
|
swayc_t *child = output->children->items[0];
|
2015-08-27 14:25:57 +10:00
|
|
|
remove_child(child);
|
|
|
|
add_child(root_container.children->items[p], child);
|
|
|
|
}
|
2015-08-28 01:32:06 +10:00
|
|
|
update_visibility(root_container.children->items[p]);
|
|
|
|
arrange_windows(root_container.children->items[p], -1, -1);
|
2015-08-27 14:25:57 +10:00
|
|
|
}
|
2015-08-15 05:42:19 +10:00
|
|
|
}
|
2015-08-18 19:46:14 +10:00
|
|
|
sway_log(L_DEBUG, "OUTPUT: Destroying output '%lu'", output->handle);
|
2015-08-15 05:42:19 +10:00
|
|
|
free_swayc(output);
|
|
|
|
return &root_container;
|
|
|
|
}
|
|
|
|
|
|
|
|
swayc_t *destroy_workspace(swayc_t *workspace) {
|
2015-08-21 02:52:54 +10:00
|
|
|
if (!ASSERT_NONNULL(workspace)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-01-06 09:42:40 +11:00
|
|
|
|
2015-08-18 22:20:59 +10:00
|
|
|
// Do not destroy this if it's the last workspace on this output
|
2015-08-21 02:52:54 +10:00
|
|
|
swayc_t *output = swayc_parent_by_type(workspace, C_OUTPUT);
|
|
|
|
if (output && output->children->length == 1) {
|
|
|
|
return NULL;
|
2015-08-18 22:20:59 +10:00
|
|
|
}
|
|
|
|
|
2015-12-14 04:19:24 +11:00
|
|
|
swayc_t *parent = workspace->parent;
|
|
|
|
// destroy the WS if there are no children
|
2015-08-23 11:25:05 +10:00
|
|
|
if (workspace->children->length == 0 && workspace->floating->length == 0) {
|
2015-08-28 13:55:48 +10:00
|
|
|
sway_log(L_DEBUG, "destroying workspace '%s'", workspace->name);
|
2016-02-25 22:59:22 +11:00
|
|
|
ipc_event_workspace(workspace, NULL, "empty");
|
2015-12-14 04:19:24 +11:00
|
|
|
} 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);
|
|
|
|
}
|
2015-08-15 05:42:19 +10:00
|
|
|
}
|
2015-12-14 04:19:24 +11:00
|
|
|
|
|
|
|
free_swayc(workspace);
|
|
|
|
return parent;
|
2015-08-15 05:42:19 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
swayc_t *destroy_container(swayc_t *container) {
|
2015-08-21 02:52:54 +10:00
|
|
|
if (!ASSERT_NONNULL(container)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-15 05:42:19 +10: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-21 02:52:54 +10:00
|
|
|
if (!ASSERT_NONNULL(view)) {
|
2015-08-15 05:42:19 +10:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
sway_log(L_DEBUG, "Destroying view '%p'", view);
|
|
|
|
swayc_t *parent = view->parent;
|
|
|
|
free_swayc(view);
|
|
|
|
|
2015-08-18 21:19:20 +10:00
|
|
|
// Destroy empty containers
|
2015-08-15 05:42:19 +10:00
|
|
|
if (parent->type == C_CONTAINER) {
|
|
|
|
return destroy_container(parent);
|
|
|
|
}
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
2015-08-21 02:52:54 +10:00
|
|
|
// Container lookup
|
|
|
|
|
2015-08-22 03:28:37 +10:00
|
|
|
|
2015-09-14 09:46:16 +10:00
|
|
|
swayc_t *swayc_by_test(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data) {
|
2015-08-22 03:28:37 +10:00
|
|
|
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 {
|
2015-09-14 09:46:16 +10:00
|
|
|
swayc_t *res = swayc_by_test(child, test, data);
|
2015-08-22 03:28:37 +10:00
|
|
|
if (res) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-09-14 09:46:16 +10:00
|
|
|
static bool test_name(swayc_t *view, void *data) {
|
2015-09-18 23:52:04 +10:00
|
|
|
if (!view || !view->name) {
|
2015-09-14 09:46:16 +10:00
|
|
|
return false;
|
2015-08-28 13:32:56 +10:00
|
|
|
}
|
2015-09-14 09:46:16 +10:00
|
|
|
return strcmp(view->name, data) == 0;
|
2015-08-28 13:32:56 +10:00
|
|
|
}
|
|
|
|
|
2015-09-14 09:46:16 +10:00
|
|
|
swayc_t *swayc_by_name(const char *name) {
|
|
|
|
return swayc_by_test(&root_container, test_name, (void *)name);
|
2015-08-28 13:32:56 +10:00
|
|
|
}
|
|
|
|
|
2015-08-21 02:52:54 +10:00
|
|
|
swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type) {
|
|
|
|
if (!ASSERT_NONNULL(container)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-27 04:01:26 +10:00
|
|
|
if (!sway_assert(type < C_TYPES && type >= C_ROOT, "invalid type")) {
|
2015-08-21 02:52:54 +10:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
container = container->parent;
|
2015-08-22 05:41:54 +10:00
|
|
|
} while (container && container->type != type);
|
2015-08-21 02:52:54 +10:00
|
|
|
return container;
|
|
|
|
}
|
|
|
|
|
|
|
|
swayc_t *swayc_parent_by_layout(swayc_t *container, enum swayc_layouts layout) {
|
|
|
|
if (!ASSERT_NONNULL(container)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-27 04:01:26 +10:00
|
|
|
if (!sway_assert(layout < L_LAYOUTS && layout >= L_NONE, "invalid layout")) {
|
2015-08-21 02:52:54 +10:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
container = container->parent;
|
|
|
|
} while (container && container->layout != layout);
|
|
|
|
return container;
|
|
|
|
}
|
|
|
|
|
2015-08-24 18:11:21 +10:00
|
|
|
swayc_t *swayc_focus_by_type(swayc_t *container, enum swayc_types type) {
|
|
|
|
if (!ASSERT_NONNULL(container)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-27 04:01:26 +10:00
|
|
|
if (!sway_assert(type < C_TYPES && type >= C_ROOT, "invalid type")) {
|
2015-08-24 18:11:21 +10:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
container = container->focused;
|
|
|
|
} while (container && container->type != type);
|
|
|
|
return container;
|
|
|
|
}
|
2015-08-27 04:01:26 +10:00
|
|
|
|
2015-08-24 18:11:21 +10:00
|
|
|
swayc_t *swayc_focus_by_layout(swayc_t *container, enum swayc_layouts layout) {
|
|
|
|
if (!ASSERT_NONNULL(container)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-27 04:01:26 +10:00
|
|
|
if (!sway_assert(layout < L_LAYOUTS && layout >= L_NONE, "invalid layout")) {
|
2015-08-24 18:11:21 +10:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
container = container->focused;
|
|
|
|
} while (container && container->layout != layout);
|
|
|
|
return container;
|
|
|
|
}
|
|
|
|
|
2015-09-14 09:46:16 +10:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-08-22 03:28:37 +10:00
|
|
|
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-22 05:41:54 +10:00
|
|
|
if (!cont) {
|
2015-08-22 03:28:37 +10:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
switch (cont->type) {
|
2015-08-22 05:41:54 +10:00
|
|
|
case C_ROOT:
|
|
|
|
cont = cont->focused;
|
|
|
|
/* Fallthrough */
|
|
|
|
|
|
|
|
case C_OUTPUT:
|
2015-08-26 06:29:33 +10:00
|
|
|
cont = cont ? cont->focused : NULL;
|
2015-08-22 05:41:54 +10:00
|
|
|
/* Fallthrough */
|
|
|
|
|
|
|
|
case C_WORKSPACE:
|
|
|
|
return cont;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return swayc_parent_by_type(cont, C_WORKSPACE);
|
2015-08-22 03:28:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-28 10:22:52 +11:00
|
|
|
static bool pointer_test(swayc_t *view, void *_origin) {
|
2015-11-15 03:30:23 +11:00
|
|
|
const struct wlc_point *origin = _origin;
|
2015-10-28 10:22:52 +11:00
|
|
|
// 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
|
|
|
|
if (!root_container.focused || !root_container.focused->focused) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
swayc_t *lookup = root_container.focused->focused;
|
|
|
|
// Case of empty workspace
|
|
|
|
if (lookup->children == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-11-15 03:30:23 +11:00
|
|
|
struct wlc_point origin;
|
|
|
|
wlc_pointer_get_position(&origin);
|
2015-10-28 10:22:52 +11:00
|
|
|
while (lookup->type != C_VIEW) {
|
|
|
|
int i;
|
|
|
|
int len;
|
|
|
|
// 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) {
|
2015-10-30 04:02:02 +11:00
|
|
|
if (pointer_test(lookup->floating->items[i], &origin)) {
|
2015-10-28 10:22:52 +11:00
|
|
|
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) {
|
2015-10-30 04:02:02 +11:00
|
|
|
if (pointer_test(lookup->children->items[i], &origin)) {
|
2015-10-28 10:22:52 +11:00
|
|
|
lookup = lookup->children->items[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// when border and titles are done, this could happen
|
|
|
|
if (i == len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lookup;
|
|
|
|
}
|
|
|
|
|
2015-08-22 03:28:37 +10:00
|
|
|
// Container information
|
|
|
|
|
|
|
|
bool swayc_is_fullscreen(swayc_t *view) {
|
2015-09-14 09:46:16 +10:00
|
|
|
return view && view->type == C_VIEW && (wlc_view_get_state(view->handle) & WLC_BIT_FULLSCREEN);
|
2015-08-22 03:28:37 +10:00
|
|
|
}
|
|
|
|
|
2015-08-27 04:01:26 +10:00
|
|
|
bool swayc_is_active(swayc_t *view) {
|
2015-09-14 09:46:16 +10:00
|
|
|
return view && view->type == C_VIEW && (wlc_view_get_state(view->handle) & WLC_BIT_ACTIVATED);
|
2015-08-27 04:01:26 +10:00
|
|
|
}
|
|
|
|
|
2015-08-28 16:18:28 +10:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-09-05 09:14:59 +10:00
|
|
|
int swayc_gap(swayc_t *container) {
|
|
|
|
if (container->type == C_VIEW) {
|
|
|
|
return container->gaps >= 0 ? container->gaps : config->gaps_inner;
|
|
|
|
} else if (container->type == C_WORKSPACE) {
|
2015-12-19 11:21:00 +11:00
|
|
|
int base = container->gaps >= 0 ? container->gaps : config->gaps_outer;
|
2016-03-20 22:29:47 +11:00
|
|
|
if (config->edge_gaps && !(config->smart_gaps && container->children->length == 1)) {
|
2015-12-19 11:21:00 +11:00
|
|
|
// 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;
|
|
|
|
} else {
|
|
|
|
return base;
|
|
|
|
}
|
2015-09-05 09:14:59 +10:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-22 03:28:37 +10:00
|
|
|
// Mapping
|
|
|
|
|
2015-09-14 09:46:16 +10:00
|
|
|
void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) {
|
|
|
|
if (container) {
|
|
|
|
f(container, data);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:18:28 +10:00
|
|
|
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
|
|
|
|
if (parent->type == C_OUTPUT
|
|
|
|
|| parent->layout == L_TABBED
|
|
|
|
|| parent->layout == L_STACKED) {
|
|
|
|
container->visible = parent->focused == container;
|
|
|
|
}
|
|
|
|
// 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);
|
2016-01-22 08:07:39 +11:00
|
|
|
if (!container->visible) {
|
2015-08-28 16:18:28 +10:00
|
|
|
wlc_view_send_to_back(container->handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Update visibility for children
|
2015-08-28 16:42:26 +10:00
|
|
|
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);
|
|
|
|
}
|
2015-08-17 12:06:31 +10:00
|
|
|
}
|
|
|
|
}
|
2015-08-26 02:24:15 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
void update_visibility(swayc_t *container) {
|
2015-08-28 16:18:28 +10:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
2015-08-28 13:55:48 +10:00
|
|
|
return;
|
2015-08-28 16:18:28 +10:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2015-08-28 13:55:48 +10:00
|
|
|
}
|
|
|
|
return;
|
2015-08-28 16:18:28 +10:00
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
swayc_t *op = swayc_parent_by_type(container, C_OUTPUT);
|
|
|
|
update_visibility_output(container, op->handle);
|
|
|
|
}
|
2015-08-28 13:55:48 +10:00
|
|
|
}
|
2015-08-17 12:06:31 +10:00
|
|
|
}
|
2015-08-19 12:05:58 +10:00
|
|
|
|
2015-09-14 09:46:16 +10:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-29 23:00:35 +11:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|