aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar minus <minus@mnus.de>2015-08-25 15:17:18 +0200
committerLibravatar minus <minus@mnus.de>2015-08-25 15:17:18 +0200
commit1efda79bf2f72a416806de3159e1c2936c5fdbe9 (patch)
tree836048691c78219dc0656d7a4a51128a27c70a02
parentMerge pull request #125 from Luminarys/master (diff)
downloadsway-1efda79bf2f72a416806de3159e1c2936c5fdbe9.tar.gz
sway-1efda79bf2f72a416806de3159e1c2936c5fdbe9.tar.zst
sway-1efda79bf2f72a416806de3159e1c2936c5fdbe9.zip
refactored workspace_next/prev
-rw-r--r--include/util.h4
-rw-r--r--sway/util.c3
-rw-r--r--sway/workspace.c61
3 files changed, 33 insertions, 35 deletions
diff --git a/include/util.h b/include/util.h
new file mode 100644
index 00000000..5d42b780
--- /dev/null
+++ b/include/util.h
@@ -0,0 +1,4 @@
1/**
2 * Wrap i into the range [0, max[
3 */
4int wrap(int i, int max);
diff --git a/sway/util.c b/sway/util.c
new file mode 100644
index 00000000..25aeb9f4
--- /dev/null
+++ b/sway/util.c
@@ -0,0 +1,3 @@
1int wrap(int i, int max) {
2 return ((i % max) + max) % max;
3}
diff --git a/sway/workspace.c b/sway/workspace.c
index 4c8bb62f..ca9f5ef0 100644
--- a/sway/workspace.c
+++ b/sway/workspace.c
@@ -11,6 +11,7 @@
11#include "config.h" 11#include "config.h"
12#include "stringop.h" 12#include "stringop.h"
13#include "focus.h" 13#include "focus.h"
14#include "util.h"
14 15
15char *workspace_next_name(void) { 16char *workspace_next_name(void) {
16 sway_log(L_DEBUG, "Workspace: Generating new name"); 17 sway_log(L_DEBUG, "Workspace: Generating new name");
@@ -102,31 +103,27 @@ void workspace_output_next() {
102} 103}
103 104
104void workspace_next() { 105void workspace_next() {
105 // Get the index of the workspace in the current output, and change the view to index+1 workspace. 106 // Get the index of the workspace in the current output, and change the focus to index+1 workspace.
106 // if we're currently focused on the last workspace in the output, change focus to there 107 // if we're currently focused on the last workspace in the output, change focus to the next output
107 // and call workspace_output_next(), as long as another output actually exists 108 // and call workspace_output_next()
109
108 swayc_t *current_output = swayc_active_workspace()->parent; 110 swayc_t *current_output = swayc_active_workspace()->parent;
109 int i; 111 int i;
110 for (i = 0; i < current_output->children->length - 1; i++) { 112 for (i = 0; i < current_output->children->length - 1; i++) {
111 if (strcmp((((swayc_t *)current_output->children->items[i])->name), swayc_active_workspace()->name) == 0) { 113 if (current_output->children->items[i] == swayc_active_workspace()) {
112 workspace_switch(current_output->children->items[i + 1]); 114 workspace_switch(current_output->children->items[i + 1]);
113 return; 115 return;
114 } 116 }
115 } 117 }
116 if (root_container.children->length > 1) { 118
117 for (i = 0; i < root_container.children->length - 1; i++) { 119 int num_outputs = root_container.children->length;
118 if (root_container.children->items[i] == current_output) { 120 for (i = 0; i < num_outputs; i++) {
119 workspace_switch(((swayc_t *)root_container.children->items[i + 1])->focused); 121 if (root_container.children->items[i] == current_output) {
120 workspace_output_next(); 122 swayc_t *next_output = root_container.children->items[wrap(++i, num_outputs)];
121 return; 123 workspace_switch(next_output->focused);
122 } 124 workspace_output_next();
125 return;
123 } 126 }
124 // If we're at the last output, then go to the first
125 workspace_switch(((swayc_t *)root_container.children->items[0])->focused);
126 workspace_output_next();
127 return;
128 } else {
129 workspace_switch(current_output->children->items[0]);
130 } 127 }
131} 128}
132 129
@@ -145,34 +142,28 @@ void workspace_output_prev() {
145} 142}
146 143
147void workspace_prev() { 144void workspace_prev() {
148 // Get the index of the workspace in the current output, and change the view to index-1 workspace. 145 // Get the index of the workspace in the current output, and change the focus to index-1 workspace.
149 // if we're currently focused on the last workspace in the output, change focus to there 146 // if we're currently focused on the first workspace in the output, change focus to the previous output
150 // and call workspace_output_next(), as long as another output actually exists 147 // and call workspace_output_prev()
151 148
152 swayc_t *current_output = swayc_active_workspace()->parent; 149 swayc_t *current_output = swayc_active_workspace()->parent;
153 int i; 150 int i;
154 for (i = 1; i < current_output->children->length; i++) { 151 for (i = 1; i < current_output->children->length; i++) {
155 if (strcmp((((swayc_t *)current_output->children->items[i])->name), swayc_active_workspace()->name) == 0) { 152 if (current_output->children->items[i] == swayc_active_workspace()) {
156 workspace_switch(current_output->children->items[i - 1]); 153 workspace_switch(current_output->children->items[i - 1]);
157 return; 154 return;
158 } 155 }
159 } 156 }
160 if (root_container.children->length > 1) { 157
161 for (i = 1; i < root_container.children->length; i++) { 158 int num_outputs = root_container.children->length;
162 if (root_container.children->items[i] == current_output) { 159 for (i = 0; i < num_outputs; i++) {
163 workspace_switch(((swayc_t *)root_container.children->items[i - 1])->focused); 160 if (root_container.children->items[i] == current_output) {
164 workspace_output_next(); 161 swayc_t *prev_output = root_container.children->items[wrap(--i, num_outputs)];
165 return; 162 workspace_switch(prev_output->focused);
166 } 163 workspace_output_prev();
164 return;
167 } 165 }
168 // If we're at the first output, then go to the last
169 workspace_switch(((swayc_t *)root_container.children->items[root_container.children->length-1])->focused);
170 workspace_output_next();
171 return;
172 } else {
173 workspace_switch(current_output->children->items[current_output->children->length - 1]);
174 } 166 }
175
176} 167}
177 168
178void workspace_switch(swayc_t *workspace) { 169void workspace_switch(swayc_t *workspace) {