find_parent_by_type

master
taiyu 9 years ago
parent 84f01a67bd
commit 36e07e9ebc

@ -55,6 +55,7 @@ struct sway_container {
struct sway_container *focused;
};
// Container Creation
swayc_t *new_output(wlc_handle handle);
swayc_t *new_workspace(swayc_t *output, const char *name);
@ -65,13 +66,23 @@ swayc_t *new_view(swayc_t *sibling, wlc_handle handle);
// Creates view as a new floating view which is in the active workspace
swayc_t *new_floating_view(wlc_handle handle);
// Container Destroying
swayc_t *destroy_output(swayc_t *output);
// Destroys workspace if empty and returns parent pointer, else returns NULL
swayc_t *destroy_workspace(swayc_t *workspace);
// Destroyes container and all parent container if they are empty, returns
// topmost non-empty parent. returns NULL otherwise
swayc_t *destroy_container(swayc_t *container);
// Destroys view and all empty parent containers. return topmost non-empty
// parent
swayc_t *destroy_view(swayc_t *view);
// Container Lookup
swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types);
swayc_t *swayc_parent_by_layout(swayc_t *container, enum swayc_layouts);
swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data);
void container_map(swayc_t *, void (*f)(swayc_t *, void *), void *);

@ -390,7 +390,6 @@ static bool cmd_layout(struct sway_config *config, int argc, char **argv) {
return false;
}
swayc_t *parent = get_focused_container(&root_container);
while (parent->type == C_VIEW) {
parent = parent->parent;
}
@ -512,9 +511,7 @@ static bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) {
// Resize workspace if going from fullscreen -> notfullscreen
// otherwise just resize container
if (current) {
while (container->type != C_WORKSPACE) {
container = container->parent;
}
container = swayc_parent_by_type(container, C_WORKSPACE);
}
// Only resize container when going into fullscreen
arrange_windows(container, -1, -1);

@ -8,6 +8,8 @@
#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));
@ -20,37 +22,40 @@ static swayc_t *new_swayc(enum swayc_types type) {
return c;
}
static void free_swayc(swayc_t *c) {
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 (c->children) {
if (c->children->length) {
if (cont->children) {
if (cont->children->length) {
int i;
for (i = 0; i < c->children->length; ++i) {
free_swayc(c->children->items[i]);
for (i = 0; i < cont->children->length; ++i) {
free_swayc(cont->children->items[i]);
}
}
list_free(c->children);
list_free(cont->children);
}
if (c->floating) {
if (c->floating->length) {
if (cont->floating) {
if (cont->floating->length) {
int i;
for (i = 0; i < c->floating->length; ++i) {
free_swayc(c->floating->items[i]);
for (i = 0; i < cont->floating->length; ++i) {
free_swayc(cont->floating->items[i]);
}
}
list_free(c->floating);
list_free(cont->floating);
}
if (c->parent) {
remove_child(c);
if (cont->parent) {
remove_child(cont);
}
if (c->name) {
free(c->name);
if (cont->name) {
free(cont->name);
}
free(c);
free(cont);
}
/* New containers */
// New containers
static bool workspace_test(swayc_t *view, void *name) {
return strcasecmp(view->name, (char *)name) == 0;
@ -103,6 +108,9 @@ 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);
@ -120,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);
@ -162,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",
@ -172,14 +186,14 @@ swayc_t *new_view(swayc_t *sibling, wlc_handle handle) {
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->gaps = config->gaps_inner;
view->desired_width = -1;
view->desired_height = -1;
view->is_floating = false;
if (sibling->type == C_WORKSPACE) {
@ -225,9 +239,12 @@ swayc_t *new_floating_view(wlc_handle handle) {
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
}
@ -237,23 +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 = workspace->parent;
while (output && output->type != C_OUTPUT) {
output = output->parent;
}
if (output) {
if (output->children->length == 1) {
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;
@ -262,6 +277,9 @@ 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;
@ -272,8 +290,7 @@ swayc_t *destroy_container(swayc_t *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);
@ -287,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;
@ -316,9 +361,7 @@ 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 || !container->children || !container->children->length) {
return;
}
if (container && container->children && container->children->length) {
int i;
for (i = 0; i < container->children->length; ++i) {
swayc_t *child = container->children->items[i];
@ -332,9 +375,13 @@ void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), voi
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);
@ -348,6 +395,9 @@ void set_view_visibility(swayc_t *view, void *data) {
}
void reset_gaps(swayc_t *view, void *data) {
if (!ASSERT_NONNULL(view)) {
return;
}
if (view->type == C_OUTPUT) {
view->gaps = config->gaps_outer;
}

@ -20,10 +20,7 @@ static struct wlc_origin mouse_origin;
static bool pointer_test(swayc_t *view, void *_origin) {
const struct wlc_origin *origin = _origin;
// Determine the output that the view is under
swayc_t *parent = view;
while (parent->type != C_OUTPUT) {
parent = parent->parent;
}
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) {
@ -191,10 +188,7 @@ static bool handle_view_created(wlc_handle handle) {
if (newview) {
set_focused_container(newview);
swayc_t *output = newview->parent;
while (output && output->type != C_OUTPUT) {
output = output->parent;
}
swayc_t *output = swayc_parent_by_type(newview, C_OUTPUT);
arrange_windows(output, -1, -1);
}
return true;
@ -262,10 +256,7 @@ static void handle_view_state_request(wlc_handle view, enum wlc_view_state_bit s
arrange_windows(c->parent, -1, -1);
// Set it as focused window for that workspace if its going fullscreen
if (toggle) {
swayc_t *ws = c;
while (ws->type != C_WORKSPACE) {
ws = ws->parent;
}
swayc_t *ws = swayc_parent_by_type(c, C_WORKSPACE);
// Set ws focus to c
set_focused_container_for(ws, c);
}

@ -161,10 +161,7 @@ void arrange_windows(swayc_t *container, int width, int height) {
}
};
if (wlc_view_get_state(container->handle) & WLC_BIT_FULLSCREEN) {
swayc_t *parent = container;
while (parent->type != C_OUTPUT) {
parent = parent->parent;
}
swayc_t *parent = swayc_parent_by_type(container, C_OUTPUT);
geometry.origin.x = 0;
geometry.origin.y = 0;
geometry.size.w = parent->width;
@ -263,10 +260,7 @@ void arrange_windows(swayc_t *container, int width, int height) {
}
};
if (wlc_view_get_state(view->handle) & WLC_BIT_FULLSCREEN) {
swayc_t *parent = view;
while (parent->type != C_OUTPUT) {
parent = parent->parent;
}
swayc_t *parent = swayc_parent_by_type(view, C_OUTPUT);
geometry.origin.x = 0;
geometry.origin.y = 0;
geometry.size.w = parent->width;

@ -75,9 +75,7 @@ char *workspace_next_name(void) {
swayc_t *workspace_create(const char* name) {
swayc_t *parent = get_focused_container(&root_container);
while (parent->type != C_OUTPUT) {
parent = parent->parent;
}
parent = swayc_parent_by_type(parent, C_OUTPUT);
return new_workspace(parent, name);
}

Loading…
Cancel
Save