From 1efda79bf2f72a416806de3159e1c2936c5fdbe9 Mon Sep 17 00:00:00 2001 From: minus Date: Tue, 25 Aug 2015 15:17:18 +0200 Subject: [PATCH] refactored workspace_next/prev --- include/util.h | 4 +++ sway/util.c | 3 +++ sway/workspace.c | 63 +++++++++++++++++++++--------------------------- 3 files changed, 34 insertions(+), 36 deletions(-) create mode 100644 include/util.h create mode 100644 sway/util.c diff --git a/include/util.h b/include/util.h new file mode 100644 index 000000000..5d42b7806 --- /dev/null +++ b/include/util.h @@ -0,0 +1,4 @@ +/** + * Wrap i into the range [0, max[ + */ +int wrap(int i, int max); diff --git a/sway/util.c b/sway/util.c new file mode 100644 index 000000000..25aeb9f43 --- /dev/null +++ b/sway/util.c @@ -0,0 +1,3 @@ +int wrap(int i, int max) { + return ((i % max) + max) % max; +} diff --git a/sway/workspace.c b/sway/workspace.c index 4c8bb62f0..ca9f5ef06 100644 --- a/sway/workspace.c +++ b/sway/workspace.c @@ -11,6 +11,7 @@ #include "config.h" #include "stringop.h" #include "focus.h" +#include "util.h" char *workspace_next_name(void) { sway_log(L_DEBUG, "Workspace: Generating new name"); @@ -102,31 +103,27 @@ void workspace_output_next() { } void workspace_next() { - // Get the index of the workspace in the current output, and change the view to index+1 workspace. - // if we're currently focused on the last workspace in the output, change focus to there - // and call workspace_output_next(), as long as another output actually exists + // Get the index of the workspace in the current output, and change the focus to index+1 workspace. + // if we're currently focused on the last workspace in the output, change focus to the next output + // and call workspace_output_next() + swayc_t *current_output = swayc_active_workspace()->parent; int i; for (i = 0; i < current_output->children->length - 1; i++) { - if (strcmp((((swayc_t *)current_output->children->items[i])->name), swayc_active_workspace()->name) == 0) { + if (current_output->children->items[i] == swayc_active_workspace()) { workspace_switch(current_output->children->items[i + 1]); return; } } - if (root_container.children->length > 1) { - for (i = 0; i < root_container.children->length - 1; i++) { - if (root_container.children->items[i] == current_output) { - workspace_switch(((swayc_t *)root_container.children->items[i + 1])->focused); - workspace_output_next(); - return; - } + + int num_outputs = root_container.children->length; + for (i = 0; i < num_outputs; i++) { + if (root_container.children->items[i] == current_output) { + swayc_t *next_output = root_container.children->items[wrap(++i, num_outputs)]; + workspace_switch(next_output->focused); + workspace_output_next(); + return; } - // If we're at the last output, then go to the first - workspace_switch(((swayc_t *)root_container.children->items[0])->focused); - workspace_output_next(); - return; - } else { - workspace_switch(current_output->children->items[0]); } } @@ -145,34 +142,28 @@ void workspace_output_prev() { } void workspace_prev() { - // Get the index of the workspace in the current output, and change the view to index-1 workspace. - // if we're currently focused on the last workspace in the output, change focus to there - // and call workspace_output_next(), as long as another output actually exists + // Get the index of the workspace in the current output, and change the focus to index-1 workspace. + // if we're currently focused on the first workspace in the output, change focus to the previous output + // and call workspace_output_prev() swayc_t *current_output = swayc_active_workspace()->parent; int i; for (i = 1; i < current_output->children->length; i++) { - if (strcmp((((swayc_t *)current_output->children->items[i])->name), swayc_active_workspace()->name) == 0) { + if (current_output->children->items[i] == swayc_active_workspace()) { workspace_switch(current_output->children->items[i - 1]); return; } } - if (root_container.children->length > 1) { - for (i = 1; i < root_container.children->length; i++) { - if (root_container.children->items[i] == current_output) { - workspace_switch(((swayc_t *)root_container.children->items[i - 1])->focused); - workspace_output_next(); - return; - } - } - // If we're at the first output, then go to the last - workspace_switch(((swayc_t *)root_container.children->items[root_container.children->length-1])->focused); - workspace_output_next(); - return; - } else { - workspace_switch(current_output->children->items[current_output->children->length - 1]); - } + int num_outputs = root_container.children->length; + for (i = 0; i < num_outputs; i++) { + if (root_container.children->items[i] == current_output) { + swayc_t *prev_output = root_container.children->items[wrap(--i, num_outputs)]; + workspace_switch(prev_output->focused); + workspace_output_prev(); + return; + } + } } void workspace_switch(swayc_t *workspace) {