This commit is contained in:
taiyu 2015-08-31 08:29:25 -07:00
parent 4d9b05e649
commit 7dcca7a4b4
2 changed files with 65 additions and 87 deletions

View file

@ -14,4 +14,13 @@ void list_insert(list_t *list, int index, void *item);
void list_del(list_t *list, int index); void list_del(list_t *list, int index);
void list_cat(list_t *list, list_t *source); void list_cat(list_t *list, list_t *source);
// ITEM must be a pointer, LIST is list_t*
// ITEM will be NULL after empty list
#define list_for(ITEM, LIST) \
ITEM = LIST->length ? LIST->items[0] : NULL; \
for(int list_it = 0, list_len = LIST->length; \
list_it < list_len; \
ITEM = LIST->items[++list_it])
#endif #endif

View file

@ -35,11 +35,8 @@ static void free_swayc(swayc_t *cont) {
list_free(cont->children); list_free(cont->children);
} }
if (cont->floating) { if (cont->floating) {
if (cont->floating->length) { while (cont->children->length) {
int i; free_swayc(cont->floating->items[0]);
for (i = 0; i < cont->floating->length; ++i) {
free_swayc(cont->floating->items[i]);
}
} }
list_free(cont->floating); list_free(cont->floating);
} }
@ -57,37 +54,29 @@ static void free_swayc(swayc_t *cont) {
swayc_t *new_output(wlc_handle handle) { swayc_t *new_output(wlc_handle handle) {
const struct wlc_size *size = wlc_output_get_resolution(handle); const struct wlc_size *size = wlc_output_get_resolution(handle);
const char *name = wlc_output_get_name(handle); const char *name = wlc_output_get_name(handle);
swayc_t *output;
// Find current outputs to see if this already exists // Find current outputs to see if this already exists
{ list_for (output, root_container.children) {
int i, len = root_container.children->length; if (output->name && strcmp(output->name, name) == 0) {
for (i = 0; i < len; ++i) { sway_log(L_DEBUG, "Restoring output %lu:%s", handle, output->name);
swayc_t *op = root_container.children->items[i]; return output;
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);
return op;
}
} }
} }
sway_log(L_DEBUG, "Added output %lu:%s", handle, name); sway_log(L_DEBUG, "Added output %lu:%s", handle, name);
struct output_config *oc = NULL; struct output_config *oc;
int i; list_for (oc, config->output_configs) {
for (i = 0; i < config->output_configs->length; ++i) {
oc = config->output_configs->items[i];
if (strcasecmp(name, oc->name) == 0) { if (strcasecmp(name, oc->name) == 0) {
sway_log(L_DEBUG, "Matched output config for %s", name); sway_log(L_DEBUG, "Matched output config for %s", name);
break; break;
} }
oc = NULL; oc = NULL;
} }
if (oc && !oc->enabled) { if (oc && !oc->enabled) {
return NULL; return NULL;
} }
swayc_t *output = new_swayc(C_OUTPUT); output = new_swayc(C_OUTPUT);
if (oc && oc->width != -1 && oc->height != -1) { if (oc && oc->width != -1 && oc->height != -1) {
output->width = oc->width; output->width = oc->width;
output->height = oc->height; output->height = oc->height;
@ -102,17 +91,17 @@ swayc_t *new_output(wlc_handle handle) {
output->gaps = config->gaps_outer; output->gaps = config->gaps_outer;
// Find position for it // Find position for it
if (oc && oc->x != -1 && oc->y != -1) { if (oc && oc->x > 0 && oc->y > 0) {
sway_log(L_DEBUG, "Set %s position to %d, %d", name, oc->x, oc->y); sway_log(L_DEBUG, "Set %s position to %d, %d", name, oc->x, oc->y);
output->x = oc->x; output->x = oc->x;
output->y = oc->y; output->y = oc->y;
} else { } else {
int x = 0; int x = 0;
for (i = 0; i < root_container.children->length; ++i) { swayc_t *child;
swayc_t *c = root_container.children->items[i]; list_for (child, root_container.children) {
if (c->type == C_OUTPUT) { if (child->type == C_OUTPUT) {
if (c->width + c->x > x) { if (child->width + child->x > x) {
x = c->width + c->x; x = child->width + child->x;
} }
} }
} }
@ -124,8 +113,9 @@ swayc_t *new_output(wlc_handle handle) {
// Create workspace // Create workspace
char *ws_name = NULL; char *ws_name = NULL;
if (name) { if (name) {
for (i = 0; i < config->workspace_outputs->length; ++i) { // Find matching workspace
struct workspace_output *wso = config->workspace_outputs->items[i]; struct workspace_output *wso;
list_for (wso, config->workspace_outputs) {
if (strcasecmp(wso->output, name) == 0) { if (strcasecmp(wso->output, name) == 0) {
sway_log(L_DEBUG, "Matched workspace to output: %s for %s", wso->workspace, wso->output); sway_log(L_DEBUG, "Matched workspace to output: %s for %s", wso->workspace, wso->output);
// Check if any other workspaces are using this name // Check if any other workspaces are using this name
@ -204,9 +194,9 @@ swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) {
cont->focused = workspace->focused; cont->focused = workspace->focused;
workspace->focused = cont; workspace->focused = cont;
// set all children focu to container // set all children focu to container
int i; swayc_t *child;
for (i = 0; i < workspace->children->length; ++i) { list_for (child, workspace->children) {
((swayc_t *)workspace->children->items[i])->parent = cont; child->parent = cont;
} }
// Swap children // Swap children
list_t *tmp_list = workspace->children; list_t *tmp_list = workspace->children;
@ -304,16 +294,14 @@ swayc_t *destroy_output(swayc_t *output) {
return NULL; return NULL;
} }
if (output->children->length > 0) { if (output->children->length > 0) {
int i, len = root_container.children->length;
// TODO save workspaces when there are no outputs. // TODO save workspaces when there are no outputs.
// TODO also check if there will ever be no outputs except for exiting // TODO also check if there will ever be no outputs except for exiting
// program // program
if (len > 1) { if (root_container.children->length > 1) {
len = output->children->length;
int p = root_container.children->items[0] == output; int p = root_container.children->items[0] == output;
// Move workspace from this output to another output // Move workspace from this output to another output
for (i = 0; i < len; ++i) { while (output->children->length) {
swayc_t *child = output->children->items[i]; swayc_t *child = output->children->items[0];
remove_child(child); remove_child(child);
add_child(root_container.children->items[p], child); add_child(root_container.children->items[p], child);
} }
@ -385,25 +373,20 @@ swayc_t *swayc_by_test(swayc_t *container, bool (*test)(swayc_t *view, void *dat
if (!container->children) { if (!container->children) {
return NULL; return NULL;
} }
swayc_t *child;
// Special case for checking floating stuff // Special case for checking floating stuff
int i;
if (container->type == C_WORKSPACE) { if (container->type == C_WORKSPACE) {
for (i = 0; i < container->floating->length; ++i) { list_for (child, container->floating) {
swayc_t *child = container->floating->items[i];
if (test(child, data)) { if (test(child, data)) {
return child; return child;
} }
} }
} }
for (i = 0; i < container->children->length; ++i) { list_for (child, container->children) {
swayc_t *child = container->children->items[i];
if (test(child, data)) { if (test(child, data)) {
return child; return child;
} else { } else if ((child = swayc_by_test(child, test, data))) {
swayc_t *res = swayc_by_test(child, test, data); return child;
if (res) {
return res;
}
} }
} }
return NULL; return NULL;
@ -477,28 +460,19 @@ static swayc_t *_swayc_by_handle_helper(wlc_handle handle, swayc_t *parent) {
if (!parent || !parent->children) { if (!parent || !parent->children) {
return NULL; return NULL;
} }
int i, len; swayc_t *child;
swayc_t **child;
if (parent->type == C_WORKSPACE) { if (parent->type == C_WORKSPACE) {
len = parent->floating->length; list_for(child, parent->floating) {
child = (swayc_t **)parent->floating->items; if (child->handle == handle) {
for (i = 0; i < len; ++i, ++child) { return child;
if ((*child)->handle == handle) {
return *child;
} }
} }
} }
list_for(child, parent->children) {
len = parent->children->length; if (child->handle == handle) {
child = (swayc_t**)parent->children->items; return child;
for (i = 0; i < len; ++i, ++child) { } else if ((child = _swayc_by_handle_helper(handle, child))) {
if ((*child)->handle == handle) { return child;
return *child;
} else {
swayc_t *res;
if ((res = _swayc_by_handle_helper(handle, *child))) {
return res;
}
} }
} }
return NULL; return NULL;
@ -566,16 +540,14 @@ bool swayc_is_child_of(swayc_t *child, swayc_t *parent) {
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) { if (container) {
f(container, data); f(container, data);
int i; swayc_t *child;
if (container->children) { if (container->children) {
for (i = 0; i < container->children->length; ++i) { list_for (child, container->children) {
swayc_t *child = container->children->items[i];
container_map(child, f, data); container_map(child, f, data);
} }
} }
if (container->floating) { if (container->floating) {
for (i = 0; i < container->floating->length; ++i) { list_for (child, container->floating) {
swayc_t *child = container->floating->items[i];
container_map(child, f, data); container_map(child, f, data);
} }
} }
@ -604,16 +576,15 @@ void update_visibility_output(swayc_t *container, wlc_handle output) {
} }
// Update visibility for children // Update visibility for children
else { else {
swayc_t *child;
if (container->children) { if (container->children) {
int i, len = container->children->length; list_for (child, container->children) {
for (i = 0; i < len; ++i) { update_visibility_output(child, output);
update_visibility_output(container->children->items[i], output);
} }
} }
if (container->floating) { if (container->floating) {
int i, len = container->floating->length; list_for (child, container->floating) {
for (i = 0; i < len; ++i) { update_visibility_output(child, output);
update_visibility_output(container->floating->items[i], output);
} }
} }
} }
@ -621,13 +592,13 @@ void update_visibility_output(swayc_t *container, wlc_handle output) {
void update_visibility(swayc_t *container) { void update_visibility(swayc_t *container) {
if (!container) return; if (!container) return;
swayc_t *child;
switch (container->type) { switch (container->type) {
case C_ROOT: case C_ROOT:
container->visible = true; container->visible = true;
if (container->children) { if (container->children) {
int i, len = container->children->length; list_for (child, container->children) {
for (i = 0; i < len; ++i) { update_visibility(child);
update_visibility(container->children->items[i]);
} }
} }
return; return;
@ -635,22 +606,20 @@ void update_visibility(swayc_t *container) {
case C_OUTPUT: case C_OUTPUT:
container->visible = true; container->visible = true;
if (container->children) { if (container->children) {
int i, len = container->children->length; list_for (child, container->children) {
for (i = 0; i < len; ++i) { update_visibility_output(child, container->handle);
update_visibility_output(container->children->items[i], container->handle);
} }
} }
return; return;
default: default:
{ child = swayc_parent_by_type(container, C_OUTPUT);
swayc_t *op = swayc_parent_by_type(container, C_OUTPUT); update_visibility_output(container, child->handle);
update_visibility_output(container, op->handle);
}
} }
} }
void reset_gaps(swayc_t *view, void *data) { void reset_gaps(swayc_t *view, void *data) {
(void) data;
if (!ASSERT_NONNULL(view)) { if (!ASSERT_NONNULL(view)) {
return; return;
} }