diff --git a/include/output.h b/include/output.h index 1307ead8..12bc478f 100644 --- a/include/output.h +++ b/include/output.h @@ -16,4 +16,7 @@ void get_absolute_position(swayc_t *container, struct wlc_point *point); // given wlc_point. void get_absolute_center_position(swayc_t *container, struct wlc_point *point); +int sort_workspace_cmp_qsort(const void *a, const void *b); +void sort_workspaces(swayc_t *output); + #endif diff --git a/sway/commands.c b/sway/commands.c index fbdb5fb8..487b6a8e 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -2328,7 +2328,7 @@ static struct cmd_results *cmd_workspace(int argc, char **argv) { if (!(ws=workspace_by_number(argv[1]))) { ws = workspace_create(argv[1]); } - }else if (strcasecmp(argv[0], "next") == 0) { + } else if (strcasecmp(argv[0], "next") == 0) { ws = workspace_next(); } else if (strcasecmp(argv[0], "prev") == 0) { ws = workspace_prev(); @@ -2343,7 +2343,7 @@ static struct cmd_results *cmd_workspace(int argc, char **argv) { } } } else { - if (!(ws= workspace_by_name(argv[0]))) { + if (!(ws = workspace_by_name(argv[0]))) { ws = workspace_create(argv[0]); } } diff --git a/sway/container.c b/sway/container.c index b49b32ee..20b7905b 100644 --- a/sway/container.c +++ b/sway/container.c @@ -13,6 +13,7 @@ #include "input_state.h" #include "log.h" #include "ipc-server.h" +#include "output.h" #define ASSERT_NONNULL(PTR) \ sway_assert (PTR, #PTR "must be non-null") @@ -180,24 +181,9 @@ swayc_t *new_workspace(swayc_t *output, const char *name) { workspace->visible = false; workspace->floating = create_list(); - if (isdigit(workspace->name[0])) { - // find position for numbered workspace - // order: ascending numbers, insert before same number - // numbers before unnumbered - int num = strtol(workspace->name, NULL, 10); - int i; - for (i = 0; i < output->children->length; ++i) { - char *name = ((swayc_t *)output->children->items[i])->name; - if (!isdigit(name[0]) || num <= strtol(name, NULL, 10)) { - break; - } - } - insert_child(output, workspace, i); + add_child(output, workspace); + sort_workspaces(output); - } else { - // append new unnumbered to the end - add_child(output, workspace); - } return workspace; } @@ -353,6 +339,7 @@ swayc_t *destroy_output(swayc_t *output) { remove_child(child); add_child(root_container.children->items[p], child); } + sort_workspaces(root_container.children->items[p]); update_visibility(root_container.children->items[p]); arrange_windows(root_container.children->items[p], -1, -1); } diff --git a/sway/output.c b/sway/output.c index cf8ed9a5..53b24232 100644 --- a/sway/output.c +++ b/sway/output.c @@ -1,4 +1,6 @@ #include +#include +#include #include "output.h" #include "log.h" @@ -177,3 +179,25 @@ void get_absolute_center_position(swayc_t *container, struct wlc_point *point) { point->x += container->width/2; point->y += container->height/2; } + +int sort_workspace_cmp_qsort(const void *_a, const void *_b) { + swayc_t *a = *(void **)_a; + swayc_t *b = *(void **)_b; + int retval = 0; + + if (isdigit(a->name[0]) && isdigit(b->name[0])) { + int a_num = strtol(a->name, NULL, 10); + int b_num = strtol(b->name, NULL, 10); + retval = (a_num < b_num) ? -1 : (a_num > b_num); + } else if (isdigit(a->name[0])) { + retval = -1; + } else if (isdigit(b->name[0])) { + retval = 1; + } + + return retval; +} + +void sort_workspaces(swayc_t *output) { + list_qsort(output->children, sort_workspace_cmp_qsort); +}