aboutsummaryrefslogtreecommitdiff
path: root/sway/container.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/container.c')
-rw-r--r--sway/container.c173
1 files changed, 142 insertions, 31 deletions
diff --git a/sway/container.c b/sway/container.c
index e679e823..7ccc2e09 100644
--- a/sway/container.c
+++ b/sway/container.c
@@ -4,38 +4,62 @@
#include "config.h"
#include "container.h"
#include "workspace.h"
+#include "focus.h"
#include "layout.h"
#include "log.h"
+#define ASSERT_NONNULL(PTR) \
+ sway_assert (PTR, "%s: " #PTR "must be non-null", __func__)
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) {
- //TODO does not properly handle containers with children,
- //TODO but functions that call this usually check for that
- if (c->children) {
- list_free(c->children);
+static void free_swayc(swayc_t *cont) {
+ if (!ASSERT_NONNULL(cont)) {
+ return;
+ }
+ // TODO does not properly handle containers with children,
+ // TODO but functions that call this usually check for that
+ if (cont->children) {
+ if (cont->children->length) {
+ int i;
+ for (i = 0; i < cont->children->length; ++i) {
+ free_swayc(cont->children->items[i]);
+ }
+ }
+ list_free(cont->children);
}
- if (c->parent) {
- remove_child(c);
+ if (cont->floating) {
+ if (cont->floating->length) {
+ int i;
+ for (i = 0; i < cont->floating->length; ++i) {
+ free_swayc(cont->floating->items[i]);
+ }
+ }
+ list_free(cont->floating);
}
- if (c->name) {
- free(c->name);
+ if (cont->parent) {
+ remove_child(cont);
}
- free(c);
+ if (cont->name) {
+ free(cont->name);
+ }
+ free(cont);
}
-/* New containers */
+// New containers
+
+static bool workspace_test(swayc_t *view, void *name) {
+ return strcasecmp(view->name, (char *)name) == 0;
+}
swayc_t *new_output(wlc_handle handle) {
const struct wlc_size* size = wlc_output_get_resolution(handle);
@@ -47,6 +71,7 @@ swayc_t *new_output(wlc_handle handle) {
output->height = size->h;
output->handle = handle;
output->name = name ? strdup(name) : NULL;
+ output->gaps = config->gaps_outer + config->gaps_inner / 2;
add_child(&root_container, output);
@@ -58,6 +83,12 @@ swayc_t *new_output(wlc_handle handle) {
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 (find_container(&root_container, workspace_test, 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;
}
@@ -77,10 +108,15 @@ swayc_t *new_output(wlc_handle handle) {
}
swayc_t *new_workspace(swayc_t *output, const char *name) {
+ if (!ASSERT_NONNULL(output)) {
+ return NULL;
+ }
sway_log(L_DEBUG, "Added workspace %s for output %u", name, (unsigned int)output->handle);
swayc_t *workspace = new_swayc(C_WORKSPACE);
workspace->layout = L_HORIZ; // TODO: default layout
+ workspace->x = output->x;
+ workspace->y = output->y;
workspace->width = output->width;
workspace->height = output->height;
workspace->name = strdup(name);
@@ -92,6 +128,9 @@ swayc_t *new_workspace(swayc_t *output, const char *name) {
}
swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) {
+ if (!ASSERT_NONNULL(child)) {
+ return NULL;
+ }
swayc_t *cont = new_swayc(C_CONTAINER);
sway_log(L_DEBUG, "creating container %p around %p", cont, child);
@@ -123,6 +162,7 @@ swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) {
// give them proper layouts
cont->layout = workspace->layout;
workspace->layout = layout;
+ set_focused_container_for(workspace, get_focused_view(workspace));
} else { // Or is built around container
swayc_t *parent = replace_child(child, cont);
if (parent) {
@@ -133,6 +173,9 @@ swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) {
}
swayc_t *new_view(swayc_t *sibling, wlc_handle handle) {
+ if (!ASSERT_NONNULL(sibling)) {
+ return NULL;
+ }
const char *title = wlc_view_get_title(handle);
swayc_t *view = new_swayc(C_VIEW);
sway_log(L_DEBUG, "Adding new view %lu:%s to container %p %d",
@@ -142,11 +185,15 @@ swayc_t *new_view(swayc_t *sibling, wlc_handle handle) {
view->name = title ? strdup(title) : NULL;
view->visible = true;
view->is_focused = true;
+ // Setup geometry
+ const struct wlc_geometry* geometry = wlc_view_get_geometry(handle);
+ view->width = 0;
+ view->height = 0;
+ view->desired_width = geometry->size.w;
+ view->desired_height = geometry->size.h;
- view->desired_width = -1;
- view->desired_height = -1;
+ view->gaps = config->gaps_inner;
- // TODO: properly set this
view->is_floating = false;
if (sibling->type == C_WORKSPACE) {
@@ -172,8 +219,9 @@ swayc_t *new_floating_view(wlc_handle handle) {
// 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;
+ // give it requested geometry, but place in center
+ view->x = (active_workspace->width - geometry->size.w) / 2;
+ view->y = (active_workspace->height- geometry->size.h) / 2;
view->width = geometry->size.w;
view->height = geometry->size.h;
@@ -186,14 +234,17 @@ swayc_t *new_floating_view(wlc_handle handle) {
list_add(active_workspace->floating, view);
view->parent = active_workspace;
if (active_workspace->focused == NULL) {
- active_workspace->focused = view;
+ set_focused_container_for(active_workspace, view);
}
return view;
}
-/* Destroy container */
+// Destroy container
swayc_t *destroy_output(swayc_t *output) {
+ if (!ASSERT_NONNULL(output)) {
+ return NULL;
+ }
if (output->children->length == 0) {
// TODO move workspaces to other outputs
}
@@ -203,11 +254,21 @@ swayc_t *destroy_output(swayc_t *output) {
}
swayc_t *destroy_workspace(swayc_t *workspace) {
+ if (!ASSERT_NONNULL(workspace)) {
+ return NULL;
+ }
// NOTE: This is called from elsewhere without checking children length
// TODO move containers to other workspaces?
// for now just dont delete
+
+ // Do not destroy this if it's the last workspace on this output
+ swayc_t *output = swayc_parent_by_type(workspace, C_OUTPUT);
+ if (output && output->children->length == 1) {
+ return NULL;
+ }
+
if (workspace->children->length == 0) {
- sway_log(L_DEBUG, "Workspace: Destroying workspace '%s'", workspace->name);
+ sway_log(L_DEBUG, "%s: '%s'", __func__, workspace->name);
swayc_t *parent = workspace->parent;
free_swayc(workspace);
return parent;
@@ -216,19 +277,20 @@ swayc_t *destroy_workspace(swayc_t *workspace) {
}
swayc_t *destroy_container(swayc_t *container) {
+ if (!ASSERT_NONNULL(container)) {
+ return NULL;
+ }
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");
+ if (!ASSERT_NONNULL(view)) {
return NULL;
}
sway_log(L_DEBUG, "Destroying view '%p'", view);
@@ -242,6 +304,34 @@ swayc_t *destroy_view(swayc_t *view) {
return parent;
}
+// Container lookup
+
+swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type) {
+ if (!ASSERT_NONNULL(container)) {
+ return NULL;
+ }
+ if (!sway_assert(type < C_TYPES && type >= C_ROOT, "%s: invalid type", __func__)) {
+ return NULL;
+ }
+ do {
+ container = container->parent;
+ } while(container && container->type != type);
+ return container;
+}
+
+swayc_t *swayc_parent_by_layout(swayc_t *container, enum swayc_layouts layout) {
+ if (!ASSERT_NONNULL(container)) {
+ return NULL;
+ }
+ if (!sway_assert(layout < L_LAYOUTS && layout >= L_NONE, "%s: invalid layout", __func__)) {
+ return NULL;
+ }
+ do {
+ container = container->parent;
+ } while (container && container->layout != layout);
+ return container;
+}
+
swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data) {
if (!container->children) {
return NULL;
@@ -271,18 +361,27 @@ swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *da
}
void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) {
- if (!container->children || !container->children->length) {
- return;
- }
- int i;
- for (i = 0; i < container->children->length; ++i) {
- swayc_t *child = container->children->items[i];
- f(child, data);
- container_map(child, f, data);
+ if (container && container->children && container->children->length) {
+ int i;
+ for (i = 0; i < container->children->length; ++i) {
+ swayc_t *child = container->children->items[i];
+ f(child, data);
+ container_map(child, f, data);
+ }
+ 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);
+ }
+ }
}
}
void set_view_visibility(swayc_t *view, void *data) {
+ if (!ASSERT_NONNULL(view)) {
+ return;
+ }
uint32_t *p = data;
if (view->type == C_VIEW) {
wlc_view_set_mask(view->handle, *p);
@@ -294,3 +393,15 @@ void set_view_visibility(swayc_t *view, void *data) {
}
view->visible = (*p == 2);
}
+
+void reset_gaps(swayc_t *view, void *data) {
+ if (!ASSERT_NONNULL(view)) {
+ return;
+ }
+ if (view->type == C_OUTPUT) {
+ view->gaps = config->gaps_outer;
+ }
+ if (view->type == C_VIEW) {
+ view->gaps = config->gaps_inner;
+ }
+}