From 5f4761c4f40f5d6ec550ccabaebe0f990b6e8bbc Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Tue, 3 Apr 2018 13:08:45 -0400 Subject: unify workspace create functions --- sway/commands/workspace.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'sway/commands/workspace.c') diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c index aa4096f7..a3702803 100644 --- a/sway/commands/workspace.c +++ b/sway/commands/workspace.c @@ -61,7 +61,7 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { if (strcasecmp(argv[0], "number") == 0) { if (!(ws = workspace_by_number(argv[1]))) { char *name = join_args(argv + 1, argc - 1); - ws = workspace_create(name); + ws = container_workspace_create(NULL, name); free(name); } } else if (strcasecmp(argv[0], "next") == 0) { @@ -80,12 +80,12 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { ws = old_workspace; } else if (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 { char *name = join_args(argv, argc); if (!(ws = workspace_by_name(name))) { - ws = workspace_create(name); + ws = container_workspace_create(NULL, name); } free(name); } -- cgit v1.2.3 From a001890fb8a9fc8c7f0b8eac03ca5912be2de479 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Tue, 3 Apr 2018 19:52:17 -0400 Subject: move workspace create to workspace.c --- include/sway/tree/container.h | 8 +++--- sway/commands/move.c | 2 +- sway/commands/workspace.c | 6 ++--- sway/desktop/output.c | 2 +- sway/tree/container.c | 62 ++++--------------------------------------- sway/tree/layout.c | 2 +- sway/tree/workspace.c | 54 ++++++++++++++++++++++++++++++++++++- 7 files changed, 69 insertions(+), 67 deletions(-) (limited to 'sway/commands/workspace.c') diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h index f36b4bd1..277165ea 100644 --- a/include/sway/tree/container.h +++ b/include/sway/tree/container.h @@ -95,7 +95,7 @@ struct sway_container *container_create(enum sway_container_type type); const char *container_type_to_str(enum sway_container_type type); -struct sway_container *container_output_create( +struct sway_container *output_create( struct sway_output *sway_output); /** @@ -108,13 +108,13 @@ struct sway_container *container_container_create(); * Create a new output. Outputs are children of the root container and have no * order in the tree structure. */ -struct sway_container *container_output_create(struct sway_output *sway_output); +struct sway_container *output_create(struct sway_output *sway_output); /** * Create a new workspace container. Workspaces are children of an output * container and are ordered alphabetically by name. */ -struct sway_container *container_workspace_create(struct sway_container *output, const char *name); +struct sway_container *workspace_create(struct sway_container *output, const char *name); /* * Create a new view container. A view can be a child of a workspace container @@ -178,4 +178,6 @@ bool container_has_anscestor(struct sway_container *container, bool container_has_child(struct sway_container *con, struct sway_container *child); +void container_create_notify(struct sway_container *container); + #endif diff --git a/sway/commands/move.c b/sway/commands/move.c index 7ac5f009..c954ab94 100644 --- a/sway/commands/move.c +++ b/sway/commands/move.c @@ -74,7 +74,7 @@ static struct cmd_results *cmd_move_container(struct sway_container *current, ws = workspace_by_name(ws_name); } if (!ws) { - ws = container_workspace_create(NULL, ws_name ? ws_name : num_name); + ws = workspace_create(NULL, ws_name ? ws_name : num_name); } free(ws_name); struct sway_container *old_parent = current->parent; diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c index a3702803..958b3222 100644 --- a/sway/commands/workspace.c +++ b/sway/commands/workspace.c @@ -61,7 +61,7 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { if (strcasecmp(argv[0], "number") == 0) { if (!(ws = workspace_by_number(argv[1]))) { char *name = join_args(argv + 1, argc - 1); - ws = container_workspace_create(NULL, name); + ws = workspace_create(NULL, name); free(name); } } else if (strcasecmp(argv[0], "next") == 0) { @@ -80,12 +80,12 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { ws = old_workspace; } else if (prev_workspace_name && !(ws = workspace_by_name(prev_workspace_name))) { - ws = container_workspace_create(NULL, prev_workspace_name); + ws = workspace_create(NULL, prev_workspace_name); } } else { char *name = join_args(argv, argc); if (!(ws = workspace_by_name(name))) { - ws = container_workspace_create(NULL, name); + ws = workspace_create(NULL, name); } free(name); } diff --git a/sway/desktop/output.c b/sway/desktop/output.c index de5076d8..96f23291 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -396,7 +396,7 @@ void handle_new_output(struct wl_listener *listener, void *data) { output->damage = wlr_output_damage_create(wlr_output); - output->swayc = container_output_create(output); + output->swayc = output_create(output); if (!output->swayc) { free(output); return; diff --git a/sway/tree/container.c b/sway/tree/container.c index fdbb2922..8ed30b44 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -51,7 +51,7 @@ const char *container_type_to_str(enum sway_container_type type) { } } -static void notify_new_container(struct sway_container *container) { +void container_create_notify(struct sway_container *container) { wl_signal_emit(&root_container.sway_root->events.new_container, container); ipc_event_window(container, "new"); } @@ -312,7 +312,7 @@ struct sway_container *container_close(struct sway_container *con) { return parent; } -struct sway_container *container_output_create( +struct sway_container *output_create( struct sway_output *sway_output) { struct wlr_box size; wlr_output_effective_resolution(sway_output->wlr_output, &size.width, @@ -363,7 +363,7 @@ struct sway_container *container_output_create( // Create workspace char *ws_name = workspace_next_name(output->name); wlr_log(L_DEBUG, "Creating default workspace %s", ws_name); - struct sway_container *ws = container_workspace_create(output, ws_name); + struct sway_container *ws = workspace_create(output, ws_name); // Set each seat's focus if not already set struct sway_seat *seat = NULL; wl_list_for_each(seat, &input_manager->seats, link) { @@ -373,62 +373,10 @@ struct sway_container *container_output_create( } free(ws_name); - notify_new_container(output); + container_create_notify(output); return output; } -static struct sway_container *get_workspace_initial_output(const char *name) { - struct sway_container *parent; - // Search for workspace<->output pair - int e = config->workspace_outputs->length; - for (int i = 0; i < config->workspace_outputs->length; ++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 = get_workspace_initial_output(name); - } - - wlr_log(L_DEBUG, "Added workspace %s for output %s", name, output->name); - struct sway_container *workspace = container_create(C_WORKSPACE); - - workspace->x = output->x; - workspace->y = output->y; - workspace->width = output->width; - workspace->height = output->height; - workspace->name = !name ? NULL : strdup(name); - workspace->prev_layout = L_NONE; - workspace->layout = container_get_default_layout(output); - workspace->workspace_layout = workspace->layout; - - container_add_child(output, workspace); - container_sort_workspaces(output); - notify_new_container(workspace); - - return workspace; -} - struct sway_container *container_view_create(struct sway_container *sibling, struct sway_view *sway_view) { if (!sway_assert(sibling, @@ -452,7 +400,7 @@ struct sway_container *container_view_create(struct sway_container *sibling, // Regular case, create as sibling of current container container_add_sibling(sibling, swayc); } - notify_new_container(swayc); + container_create_notify(swayc); return swayc; } diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 3cbbf3b4..a46359bd 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -149,7 +149,7 @@ void container_move_to(struct sway_container *container, if (old_parent->children->length == 0) { char *ws_name = workspace_next_name(old_parent->name); struct sway_container *ws = - container_workspace_create(old_parent, ws_name); + workspace_create(old_parent, ws_name); free(ws_name); seat_set_focus(seat, ws); } diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index d5a16410..6ba3d973 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -14,6 +14,58 @@ #include "log.h" #include "util.h" +static struct sway_container *get_workspace_initial_output(const char *name) { + struct sway_container *parent; + // Search for workspace<->output pair + int e = config->workspace_outputs->length; + for (int i = 0; i < config->workspace_outputs->length; ++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 *workspace_create(struct sway_container *output, + const char *name) { + if (output == NULL) { + output = get_workspace_initial_output(name); + } + + wlr_log(L_DEBUG, "Added workspace %s for output %s", name, output->name); + struct sway_container *workspace = container_create(C_WORKSPACE); + + workspace->x = output->x; + workspace->y = output->y; + workspace->width = output->width; + workspace->height = output->height; + workspace->name = !name ? NULL : strdup(name); + workspace->prev_layout = L_NONE; + workspace->layout = container_get_default_layout(output); + workspace->workspace_layout = workspace->layout; + + container_add_child(output, workspace); + container_sort_workspaces(output); + container_create_notify(workspace); + + return workspace; +} + char *prev_workspace_name = NULL; struct workspace_by_number_data { int len; @@ -292,7 +344,7 @@ bool workspace_switch(struct sway_container *workspace) { struct sway_container *new_ws = workspace_by_name(prev_workspace_name); workspace = new_ws ? new_ws : - container_workspace_create(NULL, prev_workspace_name); + workspace_create(NULL, prev_workspace_name); } if (!prev_workspace_name || (strcmp(prev_workspace_name, active_ws->name) -- cgit v1.2.3