diff --git a/include/output.h b/include/output.h index 1307ead81..12bc478f1 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/container.c b/sway/container.c index b49b32ee1..20b7905b8 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 cf8ed9a5c..046dee8cd 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,27 @@ 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; + } else { + retval = strcmp(a->name, b->name); + } + + return retval; +} + +void sort_workspaces(swayc_t *output) { + list_qsort(output->children, sort_workspace_cmp_qsort); +}