find_parent_by_type

This commit is contained in:
taiyu 2015-08-20 09:52:54 -07:00
parent 84f01a67bd
commit 36e07e9ebc
6 changed files with 112 additions and 71 deletions

View file

@ -55,6 +55,7 @@ struct sway_container {
struct sway_container *focused; struct sway_container *focused;
}; };
// Container Creation
swayc_t *new_output(wlc_handle handle); swayc_t *new_output(wlc_handle handle);
swayc_t *new_workspace(swayc_t *output, const char *name); 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 // Creates view as a new floating view which is in the active workspace
swayc_t *new_floating_view(wlc_handle handle); swayc_t *new_floating_view(wlc_handle handle);
// Container Destroying
swayc_t *destroy_output(swayc_t *output); swayc_t *destroy_output(swayc_t *output);
// Destroys workspace if empty and returns parent pointer, else returns NULL // Destroys workspace if empty and returns parent pointer, else returns NULL
swayc_t *destroy_workspace(swayc_t *workspace); 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); 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); 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); 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 *); void container_map(swayc_t *, void (*f)(swayc_t *, void *), void *);

View file

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

View file

@ -8,6 +8,8 @@
#include "layout.h" #include "layout.h"
#include "log.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) { static swayc_t *new_swayc(enum swayc_types type) {
swayc_t *c = calloc(1, sizeof(swayc_t)); swayc_t *c = calloc(1, sizeof(swayc_t));
@ -20,37 +22,40 @@ static swayc_t *new_swayc(enum swayc_types type) {
return c; 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 does not properly handle containers with children,
// TODO but functions that call this usually check for that // TODO but functions that call this usually check for that
if (c->children) { if (cont->children) {
if (c->children->length) { if (cont->children->length) {
int i; int i;
for (i = 0; i < c->children->length; ++i) { for (i = 0; i < cont->children->length; ++i) {
free_swayc(c->children->items[i]); free_swayc(cont->children->items[i]);
} }
} }
list_free(c->children); list_free(cont->children);
} }
if (c->floating) { if (cont->floating) {
if (c->floating->length) { if (cont->floating->length) {
int i; int i;
for (i = 0; i < c->floating->length; ++i) { for (i = 0; i < cont->floating->length; ++i) {
free_swayc(c->floating->items[i]); free_swayc(cont->floating->items[i]);
} }
} }
list_free(c->floating); list_free(cont->floating);
} }
if (c->parent) { if (cont->parent) {
remove_child(c); remove_child(cont);
} }
if (c->name) { if (cont->name) {
free(c->name); free(cont->name);
} }
free(c); free(cont);
} }
/* New containers */ // New containers
static bool workspace_test(swayc_t *view, void *name) { static bool workspace_test(swayc_t *view, void *name) {
return strcasecmp(view->name, (char *)name) == 0; 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) { 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); sway_log(L_DEBUG, "Added workspace %s for output %u", name, (unsigned int)output->handle);
swayc_t *workspace = new_swayc(C_WORKSPACE); 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) { swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) {
if (!ASSERT_NONNULL(child)) {
return NULL;
}
swayc_t *cont = new_swayc(C_CONTAINER); swayc_t *cont = new_swayc(C_CONTAINER);
sway_log(L_DEBUG, "creating container %p around %p", cont, child); 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) { swayc_t *new_view(swayc_t *sibling, wlc_handle handle) {
if (!ASSERT_NONNULL(sibling)) {
return NULL;
}
const char *title = wlc_view_get_title(handle); const char *title = wlc_view_get_title(handle);
swayc_t *view = new_swayc(C_VIEW); swayc_t *view = new_swayc(C_VIEW);
sway_log(L_DEBUG, "Adding new view %lu:%s to container %p %d", 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->visible = true;
view->is_focused = true; view->is_focused = true;
// Setup geometry // Setup geometry
const struct wlc_geometry* geometry = wlc_view_get_geometry(handle);
view->width = 0; view->width = 0;
view->height = 0; view->height = 0;
view->desired_width = geometry->size.w;
view->desired_height = geometry->size.h;
view->gaps = config->gaps_inner; view->gaps = config->gaps_inner;
view->desired_width = -1;
view->desired_height = -1;
view->is_floating = false; view->is_floating = false;
if (sibling->type == C_WORKSPACE) { if (sibling->type == C_WORKSPACE) {
@ -225,9 +239,12 @@ swayc_t *new_floating_view(wlc_handle handle) {
return view; return view;
} }
/* Destroy container */ // Destroy container
swayc_t *destroy_output(swayc_t *output) { swayc_t *destroy_output(swayc_t *output) {
if (!ASSERT_NONNULL(output)) {
return NULL;
}
if (output->children->length == 0) { if (output->children->length == 0) {
// TODO move workspaces to other outputs // TODO move workspaces to other outputs
} }
@ -237,23 +254,21 @@ swayc_t *destroy_output(swayc_t *output) {
} }
swayc_t *destroy_workspace(swayc_t *workspace) { swayc_t *destroy_workspace(swayc_t *workspace) {
if (!ASSERT_NONNULL(workspace)) {
return NULL;
}
// NOTE: This is called from elsewhere without checking children length // NOTE: This is called from elsewhere without checking children length
// TODO move containers to other workspaces? // TODO move containers to other workspaces?
// for now just dont delete // for now just dont delete
// Do not destroy this if it's the last workspace on this output // Do not destroy this if it's the last workspace on this output
swayc_t *output = workspace->parent; swayc_t *output = swayc_parent_by_type(workspace, C_OUTPUT);
while (output && output->type != C_OUTPUT) { if (output && output->children->length == 1) {
output = output->parent;
}
if (output) {
if (output->children->length == 1) {
return NULL; return NULL;
} }
}
if (workspace->children->length == 0) { 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; swayc_t *parent = workspace->parent;
free_swayc(workspace); free_swayc(workspace);
return parent; return parent;
@ -262,6 +277,9 @@ swayc_t *destroy_workspace(swayc_t *workspace) {
} }
swayc_t *destroy_container(swayc_t *container) { swayc_t *destroy_container(swayc_t *container) {
if (!ASSERT_NONNULL(container)) {
return NULL;
}
while (container->children->length == 0 && container->type == C_CONTAINER) { while (container->children->length == 0 && container->type == C_CONTAINER) {
sway_log(L_DEBUG, "Container: Destroying container '%p'", container); sway_log(L_DEBUG, "Container: Destroying container '%p'", container);
swayc_t *parent = container->parent; swayc_t *parent = container->parent;
@ -272,8 +290,7 @@ swayc_t *destroy_container(swayc_t *container) {
} }
swayc_t *destroy_view(swayc_t *view) { swayc_t *destroy_view(swayc_t *view) {
if (view == NULL) { if (!ASSERT_NONNULL(view)) {
sway_log(L_DEBUG, "Warning: NULL passed into destroy_view");
return NULL; return NULL;
} }
sway_log(L_DEBUG, "Destroying view '%p'", view); sway_log(L_DEBUG, "Destroying view '%p'", view);
@ -287,6 +304,34 @@ swayc_t *destroy_view(swayc_t *view) {
return parent; 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) { swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data) {
if (!container->children) { if (!container->children) {
return NULL; 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) { void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) {
if (!container || !container->children || !container->children->length) { if (container && container->children && container->children->length) {
return;
}
int i; int i;
for (i = 0; i < container->children->length; ++i) { for (i = 0; i < container->children->length; ++i) {
swayc_t *child = container->children->items[i]; swayc_t *child = container->children->items[i];
@ -333,8 +376,12 @@ void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), voi
} }
} }
} }
}
void set_view_visibility(swayc_t *view, void *data) { void set_view_visibility(swayc_t *view, void *data) {
if (!ASSERT_NONNULL(view)) {
return;
}
uint32_t *p = data; uint32_t *p = data;
if (view->type == C_VIEW) { if (view->type == C_VIEW) {
wlc_view_set_mask(view->handle, *p); 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) { void reset_gaps(swayc_t *view, void *data) {
if (!ASSERT_NONNULL(view)) {
return;
}
if (view->type == C_OUTPUT) { if (view->type == C_OUTPUT) {
view->gaps = config->gaps_outer; view->gaps = config->gaps_outer;
} }

View file

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

View file

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

View file

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