diff options
author | Brian Ashworth <bosrsf04@gmail.com> | 2018-12-20 13:02:45 -0500 |
---|---|---|
committer | emersion <contact@emersion.fr> | 2018-12-20 19:55:29 +0100 |
commit | 88d96bc41ff638bdc767e30bf6ccbbd530258420 (patch) | |
tree | 5d822da99812aab4e849e566c53d3a6ba87ba496 | |
parent | 477bca5e288cc3155c536265272490b413328778 (diff) |
Combine output_by_name and output_by_identifier
This combines `output_by_name` and `output_by_identifier` into a single
function called `output_by_name_or_id`. This allows for output
identifiers to be used in all commands, simplifies the logic of the
callers, and is more efficient since worst case is a single pass through
the output list.
-rw-r--r-- | include/sway/output.h | 4 | ||||
-rw-r--r-- | sway/commands/focus.c | 2 | ||||
-rw-r--r-- | sway/commands/move.c | 2 | ||||
-rw-r--r-- | sway/commands/output/transform.c | 2 | ||||
-rw-r--r-- | sway/desktop/output.c | 20 | ||||
-rw-r--r-- | sway/input/seat.c | 2 | ||||
-rw-r--r-- | sway/tree/view.c | 2 | ||||
-rw-r--r-- | sway/tree/workspace.c | 25 |
8 files changed, 22 insertions, 37 deletions
diff --git a/include/sway/output.h b/include/sway/output.h index 43c1ab96..f7c5ceba 100644 --- a/include/sway/output.h +++ b/include/sway/output.h @@ -84,9 +84,7 @@ void output_damage_box(struct sway_output *output, struct wlr_box *box); void output_damage_whole_container(struct sway_output *output, struct sway_container *con); -struct sway_output *output_by_name(const char *name); - -struct sway_output *output_by_identifier(const char *identifier); +struct sway_output *output_by_name_or_id(const char *name_or_id); void output_sort_workspaces(struct sway_output *output); diff --git a/sway/commands/focus.c b/sway/commands/focus.c index 689edfec..97ffe91c 100644 --- a/sway/commands/focus.c +++ b/sway/commands/focus.c @@ -193,7 +193,7 @@ static struct cmd_results *focus_output(struct sway_seat *seat, "Expected 'focus output <direction|name>'"); } char *identifier = join_args(argv, argc); - struct sway_output *output = output_by_name(identifier); + struct sway_output *output = output_by_name_or_id(identifier); if (!output) { enum wlr_direction direction; diff --git a/sway/commands/move.c b/sway/commands/move.c index 4dc547db..09f19c3f 100644 --- a/sway/commands/move.c +++ b/sway/commands/move.c @@ -64,7 +64,7 @@ static struct sway_output *output_in_direction(const char *direction_string, } } - return output_by_name(direction_string); + return output_by_name_or_id(direction_string); } static bool is_parallel(enum sway_container_layout layout, diff --git a/sway/commands/output/transform.c b/sway/commands/output/transform.c index c1555323..ca6f73a4 100644 --- a/sway/commands/output/transform.c +++ b/sway/commands/output/transform.c @@ -45,7 +45,7 @@ struct cmd_results *output_cmd_transform(int argc, char **argv) { return cmd_results_new(CMD_INVALID, "output", "Cannot apply relative transform to all outputs."); } - struct sway_output *s_output = output_by_name(output->name); + struct sway_output *s_output = output_by_name_or_id(output->name); if (s_output == NULL) { return cmd_results_new(CMD_INVALID, "output", "Cannot apply relative transform to unknown output %s", output->name); diff --git a/sway/desktop/output.c b/sway/desktop/output.c index 246f4438..96ceea86 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -29,23 +29,13 @@ #include "sway/tree/view.h" #include "sway/tree/workspace.h" -struct sway_output *output_by_name(const char *name) { +struct sway_output *output_by_name_or_id(const char *name_or_id) { for (int i = 0; i < root->outputs->length; ++i) { struct sway_output *output = root->outputs->items[i]; - if (strcasecmp(output->wlr_output->name, name) == 0) { - return output; - } - } - return NULL; -} - -struct sway_output *output_by_identifier(const char *identifier) { - for (int i = 0; i < root->outputs->length; ++i) { - struct sway_output *output = root->outputs->items[i]; - char output_identifier[128]; - snprintf(output_identifier, sizeof(output_identifier), "%s %s %s", output->wlr_output->make, - output->wlr_output->model, output->wlr_output->serial); - if (strcasecmp(output_identifier, identifier) == 0) { + char identifier[128]; + output_get_identifier(identifier, sizeof(identifier), output); + if (strcasecmp(identifier, name_or_id) == 0 + || strcasecmp(output->wlr_output->name, name_or_id) == 0) { return output; } } diff --git a/sway/input/seat.c b/sway/input/seat.c index 776d5766..e0f0db1d 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -428,7 +428,7 @@ static void seat_apply_input_config(struct sway_seat *seat, if (mapped_to_output != NULL) { wlr_log(WLR_DEBUG, "Mapping input device %s to output %s", sway_device->input_device->identifier, mapped_to_output); - struct sway_output *output = output_by_name(mapped_to_output); + struct sway_output *output = output_by_name_or_id(mapped_to_output); if (output) { wlr_cursor_map_input_to_output(seat->cursor->cursor, sway_device->input_device->wlr_device, output->wlr_output); diff --git a/sway/tree/view.c b/sway/tree/view.c index e890f4f3..deb20676 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -459,7 +459,7 @@ static struct sway_workspace *select_workspace(struct sway_view *view) { for (int i = 0; i < criterias->length; ++i) { struct criteria *criteria = criterias->items[i]; if (criteria->type == CT_ASSIGN_OUTPUT) { - struct sway_output *output = output_by_name(criteria->target); + struct sway_output *output = output_by_name_or_id(criteria->target); if (output) { ws = output_get_active_workspace(output); break; diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index 4f1c4a64..7f18046d 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -35,10 +35,8 @@ struct sway_output *workspace_get_initial_output(const char *name) { struct workspace_config *wsc = workspace_find_config(name); if (wsc) { for (int i = 0; i < wsc->outputs->length; i++) { - struct sway_output *output = output_by_name(wsc->outputs->items[i]); - if (!output) { - output = output_by_identifier(wsc->outputs->items[i]); - } + struct sway_output *output = + output_by_name_or_id(wsc->outputs->items[i]); if (output) { return output; } @@ -185,11 +183,11 @@ static bool workspace_valid_on_output(const char *output_name, const char *ws_name) { struct workspace_config *wsc = workspace_find_config(ws_name); char identifier[128]; - struct sway_output *output = output_by_name(output_name); + struct sway_output *output = output_by_name_or_id(output_name); if (!output) { - output = output_by_identifier(output_name); - output_name = output->wlr_output->name; + return false; } + output_name = output->wlr_output->name; output_get_identifier(identifier, sizeof(identifier), output); if (!wsc) { @@ -295,7 +293,11 @@ char *workspace_next_name(const char *output_name) { struct sway_mode *mode = config->current_mode; char identifier[128]; - struct sway_output *output = output_by_name(output_name); + struct sway_output *output = output_by_name_or_id(output_name); + if (!output) { + return NULL; + } + output_name = output->wlr_output->name; output_get_identifier(identifier, sizeof(identifier), output); int order = INT_MAX; @@ -551,12 +553,7 @@ struct sway_output *workspace_output_get_highest_available( continue; } - struct sway_output *output = output_by_name(name); - if (output) { - return output; - } - - output = output_by_identifier(name); + struct sway_output *output = output_by_name_or_id(name); if (output) { return output; } |