diff options
Diffstat (limited to 'sway/tree/workspace.c')
-rw-r--r-- | sway/tree/workspace.c | 76 |
1 files changed, 27 insertions, 49 deletions
diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index 0177068b..cd2a7a04 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c | |||
@@ -286,8 +286,8 @@ struct sway_container *workspace_by_name(const char *name) { | |||
286 | * the end and beginning. If next is false, the previous workspace is returned, | 286 | * the end and beginning. If next is false, the previous workspace is returned, |
287 | * otherwise the next one is returned. | 287 | * otherwise the next one is returned. |
288 | */ | 288 | */ |
289 | struct sway_container *workspace_output_prev_next_impl( | 289 | static struct sway_container *workspace_output_prev_next_impl( |
290 | struct sway_container *output, bool next) { | 290 | struct sway_container *output, int dir) { |
291 | if (!output) { | 291 | if (!output) { |
292 | return NULL; | 292 | return NULL; |
293 | } | 293 | } |
@@ -302,27 +302,17 @@ struct sway_container *workspace_output_prev_next_impl( | |||
302 | focus : | 302 | focus : |
303 | container_parent(focus, C_WORKSPACE)); | 303 | container_parent(focus, C_WORKSPACE)); |
304 | 304 | ||
305 | int i; | 305 | int index = list_find(output->children, workspace); |
306 | for (i = 0; i < output->children->length; i++) { | 306 | size_t new_index = wrap(index + dir, output->children->length); |
307 | if (output->children->items[i] == workspace) { | 307 | return output->children->items[new_index]; |
308 | return output->children->items[ | ||
309 | wrap(i + (next ? 1 : -1), output->children->length)]; | ||
310 | } | ||
311 | } | ||
312 | |||
313 | // Doesn't happen, at worst the for loop returns the previously active | ||
314 | // workspace | ||
315 | return NULL; | ||
316 | } | 308 | } |
317 | 309 | ||
318 | /** | 310 | /** |
319 | * Get the previous or next workspace. If the first/last workspace on an output | 311 | * Get the previous or next workspace. If the first/last workspace on an output |
320 | * is active, proceed to the previous/next output's previous/next workspace. If | 312 | * is active, proceed to the previous/next output's previous/next workspace. |
321 | * next is false, the previous workspace is returned, otherwise the next one is | ||
322 | * returned. | ||
323 | */ | 313 | */ |
324 | struct sway_container *workspace_prev_next_impl( | 314 | static struct sway_container *workspace_prev_next_impl( |
325 | struct sway_container *workspace, bool next) { | 315 | struct sway_container *workspace, int dir) { |
326 | if (!workspace) { | 316 | if (!workspace) { |
327 | return NULL; | 317 | return NULL; |
328 | } | 318 | } |
@@ -331,52 +321,40 @@ struct sway_container *workspace_prev_next_impl( | |||
331 | return NULL; | 321 | return NULL; |
332 | } | 322 | } |
333 | 323 | ||
334 | struct sway_container *current_output = workspace->parent; | 324 | struct sway_container *output = workspace->parent; |
335 | int offset = next ? 1 : -1; | 325 | int index = list_find(output->children, workspace); |
336 | int start = next ? 0 : 1; | 326 | int new_index = index + dir; |
337 | int end; | ||
338 | if (next) { | ||
339 | end = current_output->children->length - 1; | ||
340 | } else { | ||
341 | end = current_output->children->length; | ||
342 | } | ||
343 | int i; | ||
344 | for (i = start; i < end; i++) { | ||
345 | if (current_output->children->items[i] == workspace) { | ||
346 | return current_output->children->items[i + offset]; | ||
347 | } | ||
348 | } | ||
349 | 327 | ||
350 | // Given workspace is the first/last on the output, jump to the | 328 | if (new_index >= 0 && new_index < output->children->length) { |
351 | // previous/next output | 329 | return output->children->items[index + dir]; |
352 | int num_outputs = root_container.children->length; | ||
353 | for (i = 0; i < num_outputs; i++) { | ||
354 | if (root_container.children->items[i] == current_output) { | ||
355 | struct sway_container *next_output = root_container.children->items[ | ||
356 | wrap(i + offset, num_outputs)]; | ||
357 | return workspace_output_prev_next_impl(next_output, next); | ||
358 | } | ||
359 | } | 330 | } |
360 | 331 | ||
361 | // Doesn't happen, at worst the for loop returns the previously active | 332 | // Look on a different output |
362 | // workspace on the active output | 333 | int output_index = list_find(root_container.children, output); |
363 | return NULL; | 334 | new_index = wrap(output_index + dir, root_container.children->length); |
335 | output = root_container.children->items[new_index]; | ||
336 | |||
337 | if (dir == 1) { | ||
338 | return output->children->items[0]; | ||
339 | } else { | ||
340 | return output->children->items[output->children->length - 1]; | ||
341 | } | ||
364 | } | 342 | } |
365 | 343 | ||
366 | struct sway_container *workspace_output_next(struct sway_container *current) { | 344 | struct sway_container *workspace_output_next(struct sway_container *current) { |
367 | return workspace_output_prev_next_impl(current, true); | 345 | return workspace_output_prev_next_impl(current, 1); |
368 | } | 346 | } |
369 | 347 | ||
370 | struct sway_container *workspace_next(struct sway_container *current) { | 348 | struct sway_container *workspace_next(struct sway_container *current) { |
371 | return workspace_prev_next_impl(current, true); | 349 | return workspace_prev_next_impl(current, 1); |
372 | } | 350 | } |
373 | 351 | ||
374 | struct sway_container *workspace_output_prev(struct sway_container *current) { | 352 | struct sway_container *workspace_output_prev(struct sway_container *current) { |
375 | return workspace_output_prev_next_impl(current, false); | 353 | return workspace_output_prev_next_impl(current, -1); |
376 | } | 354 | } |
377 | 355 | ||
378 | struct sway_container *workspace_prev(struct sway_container *current) { | 356 | struct sway_container *workspace_prev(struct sway_container *current) { |
379 | return workspace_prev_next_impl(current, false); | 357 | return workspace_prev_next_impl(current, -1); |
380 | } | 358 | } |
381 | 359 | ||
382 | bool workspace_switch(struct sway_container *workspace, | 360 | bool workspace_switch(struct sway_container *workspace, |