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-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;
|
|
|
|
c->weight = 1;
|
|
|
|
if (type != C_VIEW) {
|
|
|
|
c->children = create_list();
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_swayc(swayc_t *c) {
|
2015-08-18 18:22:52 +00:00
|
|
|
// TODO does not properly handle containers with children,
|
|
|
|
// TODO but functions that call this usually check for that
|
2015-08-14 19:42:19 +00:00
|
|
|
if (c->children) {
|
2015-08-18 18:22:52 +00:00
|
|
|
if (c->children->length) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < c->children->length; ++i) {
|
|
|
|
free_swayc(c->children->items[i]);
|
|
|
|
}
|
|
|
|
}
|
2015-08-14 19:42:19 +00:00
|
|
|
list_free(c->children);
|
|
|
|
}
|
2015-08-18 18:22:52 +00:00
|
|
|
if (c->floating) {
|
|
|
|
if (c->floating->length) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < c->floating->length; ++i) {
|
|
|
|
free_swayc(c->floating->items[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list_free(c->floating);
|
|
|
|
}
|
2015-08-14 19:42:19 +00:00
|
|
|
if (c->parent) {
|
2015-08-18 09:46:14 +00:00
|
|
|
remove_child(c);
|
2015-08-14 19:42:19 +00:00
|
|
|
}
|
2015-08-16 20:38:54 +00:00
|
|
|
if (c->name) {
|
|
|
|
free(c->name);
|
|
|
|
}
|
2015-08-14 19:42:19 +00:00
|
|
|
free(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* New containers */
|
|
|
|
|
2015-08-18 12:20:59 +00:00
|
|
|
static bool workspace_test(swayc_t *view, void *name) {
|
|
|
|
return strcasecmp(view->name, (char *)name);
|
|
|
|
}
|
|
|
|
|
2015-08-14 19:42:19 +00:00
|
|
|
swayc_t *new_output(wlc_handle handle) {
|
|
|
|
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-18 07:28:44 +00:00
|
|
|
sway_log(L_DEBUG, "Added output %lu:%s", handle, name);
|
2015-08-14 19:42:19 +00:00
|
|
|
|
|
|
|
swayc_t *output = new_swayc(C_OUTPUT);
|
2015-08-18 22:44:50 +00:00
|
|
|
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-18 22:44:50 +00:00
|
|
|
output->gaps = config->gaps_outer;
|
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) {
|
|
|
|
int i;
|
|
|
|
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
|
|
|
|
if (find_container(&root_container, workspace_test, wso->workspace)) {
|
|
|
|
break;
|
|
|
|
}
|
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-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-18 11:19:20 +00:00
|
|
|
workspace->layout = L_HORIZ; // TODO: default layout
|
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-14 19:42:19 +00:00
|
|
|
workspace->visible = true;
|
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) {
|
|
|
|
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-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-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-14 19:42:19 +00:00
|
|
|
|
2015-08-18 21:53:57 +00:00
|
|
|
view->gaps = config->gaps_inner;
|
|
|
|
|
2015-08-17 15:18:06 +00:00
|
|
|
view->desired_width = -1;
|
|
|
|
view->desired_height = -1;
|
|
|
|
|
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-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);
|
|
|
|
|
|
|
|
view->x = geometry->origin.x;
|
|
|
|
view->y = geometry->origin.y;
|
|
|
|
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-17 15:18:06 +00:00
|
|
|
list_add(active_workspace->floating, view);
|
|
|
|
view->parent = active_workspace;
|
|
|
|
if (active_workspace->focused == NULL) {
|
2015-08-18 18:22:52 +00:00
|
|
|
set_focused_container_for(active_workspace, view);
|
2015-08-17 15:18:06 +00:00
|
|
|
}
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2015-08-18 07:28:44 +00:00
|
|
|
/* Destroy container */
|
2015-08-14 19:42:19 +00:00
|
|
|
|
|
|
|
swayc_t *destroy_output(swayc_t *output) {
|
|
|
|
if (output->children->length == 0) {
|
2015-08-18 11:19:20 +00:00
|
|
|
// TODO move workspaces to other outputs
|
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-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
|
|
|
|
swayc_t *output = workspace->parent;
|
|
|
|
while (output && output->type != C_OUTPUT) {
|
|
|
|
output = output->parent;
|
|
|
|
}
|
|
|
|
if (output) {
|
|
|
|
if (output->children->length == 1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-14 19:42:19 +00:00
|
|
|
if (workspace->children->length == 0) {
|
|
|
|
sway_log(L_DEBUG, "Workspace: Destroying workspace '%s'", workspace->name);
|
|
|
|
swayc_t *parent = workspace->parent;
|
|
|
|
free_swayc(workspace);
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
swayc_t *destroy_container(swayc_t *container) {
|
|
|
|
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) {
|
|
|
|
if (view == NULL) {
|
|
|
|
sway_log(L_DEBUG, "Warning: NULL passed into destroy_view");
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data) {
|
2015-08-10 20:31:23 +00:00
|
|
|
if (!container->children) {
|
2015-08-14 19:42:19 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-17 05:38:34 +00:00
|
|
|
// Special case for checking floating stuff
|
2015-08-14 19:42:19 +00:00
|
|
|
int i;
|
2015-08-17 05:38:34 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-14 19:42:19 +00:00
|
|
|
for (i = 0; i < container->children->length; ++i) {
|
|
|
|
swayc_t *child = container->children->items[i];
|
|
|
|
if (test(child, data)) {
|
|
|
|
return child;
|
|
|
|
} else {
|
2015-08-17 05:38:34 +00:00
|
|
|
swayc_t *res = find_container(child, test, data);
|
|
|
|
if (res) {
|
|
|
|
return res;
|
2015-08-14 19:42:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) {
|
2015-08-18 18:22:52 +00:00
|
|
|
if (!container || !container->children || !container->children->length) {
|
2015-08-10 22:50:47 +00:00
|
|
|
return;
|
2015-08-10 20:31:23 +00:00
|
|
|
}
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < container->children->length; ++i) {
|
|
|
|
swayc_t *child = container->children->items[i];
|
|
|
|
f(child, data);
|
2015-08-14 19:42:19 +00:00
|
|
|
container_map(child, f, data);
|
2015-08-10 20:31:23 +00:00
|
|
|
}
|
2015-08-18 18:22:52 +00:00
|
|
|
if (container->type == C_WORKSPACE) {
|
|
|
|
for (i = 0; i < container->floating->length; ++i) {
|
|
|
|
swayc_t *child = container->floating->items[i];
|
|
|
|
f(child, data);
|
|
|
|
container_map(child, f, data);
|
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
uint32_t *p = data;
|
|
|
|
if (view->type == C_VIEW) {
|
|
|
|
wlc_view_set_mask(view->handle, *p);
|
|
|
|
if (*p == 2) {
|
|
|
|
wlc_view_bring_to_front(view->handle);
|
|
|
|
} else {
|
|
|
|
wlc_view_send_to_back(view->handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
view->visible = (*p == 2);
|
|
|
|
}
|