unify workspace create functions

This commit is contained in:
Tony Crisci 2018-04-03 13:08:45 -04:00
parent cba258e16a
commit 5f4761c4f4
5 changed files with 46 additions and 44 deletions

View File

@ -7,8 +7,6 @@ extern char *prev_workspace_name;
char *workspace_next_name(const char *output_name); char *workspace_next_name(const char *output_name);
struct sway_container *workspace_create(const char *name);
bool workspace_switch(struct sway_container *workspace); bool workspace_switch(struct sway_container *workspace);
struct sway_container *workspace_by_number(const char* name); struct sway_container *workspace_by_number(const char* name);

View File

@ -74,7 +74,7 @@ static struct cmd_results *cmd_move_container(struct sway_container *current,
ws = workspace_by_name(ws_name); ws = workspace_by_name(ws_name);
} }
if (!ws) { if (!ws) {
ws = workspace_create(ws_name ? ws_name : num_name); ws = container_workspace_create(NULL, ws_name ? ws_name : num_name);
} }
free(ws_name); free(ws_name);
struct sway_container *old_parent = current->parent; struct sway_container *old_parent = current->parent;

View File

@ -61,7 +61,7 @@ struct cmd_results *cmd_workspace(int argc, char **argv) {
if (strcasecmp(argv[0], "number") == 0) { if (strcasecmp(argv[0], "number") == 0) {
if (!(ws = workspace_by_number(argv[1]))) { if (!(ws = workspace_by_number(argv[1]))) {
char *name = join_args(argv + 1, argc - 1); char *name = join_args(argv + 1, argc - 1);
ws = workspace_create(name); ws = container_workspace_create(NULL, name);
free(name); free(name);
} }
} else if (strcasecmp(argv[0], "next") == 0) { } else if (strcasecmp(argv[0], "next") == 0) {
@ -80,12 +80,12 @@ struct cmd_results *cmd_workspace(int argc, char **argv) {
ws = old_workspace; ws = old_workspace;
} else if (prev_workspace_name } else if (prev_workspace_name
&& !(ws = workspace_by_name(prev_workspace_name))) { && !(ws = workspace_by_name(prev_workspace_name))) {
ws = workspace_create(prev_workspace_name); ws = container_workspace_create(NULL, prev_workspace_name);
} }
} else { } else {
char *name = join_args(argv, argc); char *name = join_args(argv, argc);
if (!(ws = workspace_by_name(name))) { if (!(ws = workspace_by_name(name))) {
ws = workspace_create(name); ws = container_workspace_create(NULL, name);
} }
free(name); free(name);
} }

View File

@ -110,7 +110,8 @@ struct sway_container *container_finish(struct sway_container *cont) {
return parent; return parent;
} }
static struct sway_container *container_output_destroy(struct sway_container *output) { static struct sway_container *container_output_destroy(
struct sway_container *output) {
if (!sway_assert(output, "cannot destroy null output")) { if (!sway_assert(output, "cannot destroy null output")) {
return NULL; return NULL;
} }
@ -245,7 +246,8 @@ struct sway_container *container_destroy(struct sway_container *con) {
break; break;
case C_CONTAINER: case C_CONTAINER:
if (con->children != NULL && con->children->length) { if (con->children != NULL && con->children->length) {
assert(false && "dont destroy container containers with children"); assert(false &&
"dont destroy container containers with children");
} }
container_finish(con); container_finish(con);
// TODO return parent to arrange maybe? // TODO return parent to arrange maybe?
@ -271,7 +273,8 @@ static void container_close_func(struct sway_container *container, void *data) {
} }
struct sway_container *container_close(struct sway_container *con) { struct sway_container *container_close(struct sway_container *con) {
if (!sway_assert(con != NULL, "container_close called with a NULL container")) { if (!sway_assert(con != NULL,
"container_close called with a NULL container")) {
return NULL; return NULL;
} }
@ -359,12 +362,39 @@ struct sway_container *container_output_create(
return output; return output;
} }
struct sway_container *container_workspace_create( static struct sway_container *workspace_get_initial_output(const char *name) {
struct sway_container *output, const char *name) { struct sway_container *parent;
if (!sway_assert(output, // Search for workspace<->output pair
"container_workspace_create called with null output")) { int i, e = config->workspace_outputs->length;
return NULL; for (i = 0; i < e; ++i) {
struct workspace_output *wso = config->workspace_outputs->items[i];
if (strcasecmp(wso->workspace, name) == 0) {
// Find output to use if it exists
e = root_container.children->length;
for (i = 0; i < e; ++i) {
parent = root_container.children->items[i];
if (strcmp(parent->name, wso->output) == 0) {
return parent;
}
}
break;
}
} }
// Otherwise put it on the focused output
struct sway_seat *seat = input_manager_current_seat(input_manager);
struct sway_container *focus =
seat_get_focus_inactive(seat, &root_container);
parent = focus;
parent = container_parent(parent, C_OUTPUT);
return parent;
}
struct sway_container *container_workspace_create(struct sway_container *output,
const char *name) {
if (output == NULL) {
output = workspace_get_initial_output(name);
}
wlr_log(L_DEBUG, "Added workspace %s for output %s", name, output->name); wlr_log(L_DEBUG, "Added workspace %s for output %s", name, output->name);
struct sway_container *workspace = container_create(C_WORKSPACE); struct sway_container *workspace = container_create(C_WORKSPACE);
@ -380,6 +410,7 @@ struct sway_container *container_workspace_create(
container_add_child(output, workspace); container_add_child(output, workspace);
container_sort_workspaces(output); container_sort_workspaces(output);
notify_new_container(workspace); notify_new_container(workspace);
return workspace; return workspace;
} }

View File

@ -179,35 +179,6 @@ struct sway_container *workspace_by_name(const char *name) {
} }
} }
struct sway_container *workspace_create(const char *name) {
struct sway_container *parent;
// Search for workspace<->output pair
int i, e = config->workspace_outputs->length;
for (i = 0; i < e; ++i) {
struct workspace_output *wso = config->workspace_outputs->items[i];
if (strcasecmp(wso->workspace, name) == 0) {
// Find output to use if it exists
e = root_container.children->length;
for (i = 0; i < e; ++i) {
parent = root_container.children->items[i];
if (strcmp(parent->name, wso->output) == 0) {
return container_workspace_create(parent, name);
}
}
break;
}
}
// Otherwise create a new one
struct sway_seat *seat = input_manager_current_seat(input_manager);
struct sway_container *focus =
seat_get_focus_inactive(seat, &root_container);
parent = focus;
parent = container_parent(parent, C_OUTPUT);
struct sway_container *new_ws = container_workspace_create(parent, name);
ipc_event_workspace(NULL, new_ws, "init");
return new_ws;
}
/** /**
* Get the previous or next workspace on the specified output. Wraps around at * Get the previous or next workspace on the specified output. Wraps around at
* the end and beginning. If next is false, the previous workspace is returned, * the end and beginning. If next is false, the previous workspace is returned,
@ -319,7 +290,9 @@ bool workspace_switch(struct sway_container *workspace) {
&& active_ws == workspace && active_ws == workspace
&& prev_workspace_name) { && prev_workspace_name) {
struct sway_container *new_ws = workspace_by_name(prev_workspace_name); struct sway_container *new_ws = workspace_by_name(prev_workspace_name);
workspace = new_ws ? new_ws : workspace_create(prev_workspace_name); workspace = new_ws ?
new_ws :
container_workspace_create(NULL, prev_workspace_name);
} }
if (!prev_workspace_name || (strcmp(prev_workspace_name, active_ws->name) if (!prev_workspace_name || (strcmp(prev_workspace_name, active_ws->name)