From 68258785acebaa648bacf4ef8b6e44f098f4e396 Mon Sep 17 00:00:00 2001 From: Orestis Floros Date: Tue, 10 Nov 2020 10:24:15 +0100 Subject: [PATCH 1/2] create_workspace_on_output: Prevent duplicate workspace nums MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When going through the `binding_workspace_names` to prioritize user-specified names, we only check if the workspace exists, not the workspace number. If the user specified a `bindsym … workspace number X` directive, then it is appended in `binding_workspace_names` and a workspace is created using that number even though from the POV of a user that uses numbers to change workspaces, that workspace already exists. In similar code here: https://github.com/i3/i3/blob/1d9160f2d247dbaa83fb62f02fd7041dec767fc2/src/workspace.c#L997 we do the check. Fixes #4238 --- RELEASE-NOTES-next | 3 +-- src/workspace.c | 7 +++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/RELEASE-NOTES-next b/RELEASE-NOTES-next index e896b54e..c24c82a0 100644 --- a/RELEASE-NOTES-next +++ b/RELEASE-NOTES-next @@ -17,5 +17,4 @@ strongly encouraged to upgrade. │ Bugfixes │ └────────────────────────────┘ - • placeholder - + • when initializing new outputs, avoid duplicating workspace numbers diff --git a/src/workspace.c b/src/workspace.c index f3ddf01c..db3afe4a 100644 --- a/src/workspace.c +++ b/src/workspace.c @@ -254,12 +254,15 @@ Con *create_workspace_on_output(Output *output, Con *content) { continue; } - exists = (get_existing_workspace_by_name(target_name) != NULL); + const int num = ws_name_to_number(target_name); + exists = (num == -1) + ? get_existing_workspace_by_name(target_name) + : get_existing_workspace_by_num(num); if (!exists) { ws->name = sstrdup(target_name); /* Set ->num to the number of the workspace, if the name actually * is a number or starts with a number */ - ws->num = ws_name_to_number(ws->name); + ws->num = num; LOG("Used number %d for workspace with name %s\n", ws->num, ws->name); break; From 13757ac1ca707e0b35105d0c499848bae4460766 Mon Sep 17 00:00:00 2001 From: Orestis Floros Date: Wed, 11 Nov 2020 09:47:25 +0100 Subject: [PATCH 2/2] workspace.c: Make small consistency changes --- src/workspace.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/workspace.c b/src/workspace.c index db3afe4a..739358a8 100644 --- a/src/workspace.c +++ b/src/workspace.c @@ -134,7 +134,7 @@ Con *workspace_get(const char *num) { /* We set workspace->num to the number if this workspace’s name begins with * a positive number. Otherwise it’s a named ws and num will be 1. */ - const long parsed_num = ws_name_to_number(num); + const int parsed_num = ws_name_to_number(num); Con *output = get_assigned_output(num, parsed_num); /* if an assignment is not found, we create this workspace on the current output */ @@ -238,7 +238,6 @@ void extract_workspace_names_from_bindings(void) { */ Con *create_workspace_on_output(Output *output, Con *content) { /* add a workspace to this output */ - char *name; bool exists = true; Con *ws = con_new(NULL, NULL); ws->type = CT_WORKSPACE; @@ -263,7 +262,7 @@ Con *create_workspace_on_output(Output *output, Con *content) { /* Set ->num to the number of the workspace, if the name actually * is a number or starts with a number */ ws->num = num; - LOG("Used number %d for workspace with name %s\n", ws->num, ws->name); + DLOG("Used number %d for workspace with name %s\n", ws->num, ws->name); break; } @@ -284,6 +283,7 @@ Con *create_workspace_on_output(Output *output, Con *content) { } con_attach(ws, content, false); + char *name; sasprintf(&name, "[i3 con] workspace %s", ws->name); x_set_name(ws, name); free(name); @@ -990,14 +990,14 @@ void workspace_move_to_output(Con *ws, Output *output) { bool used_assignment = false; struct Workspace_Assignment *assignment; TAILQ_FOREACH (assignment, &ws_assignments, ws_assignments) { - bool attached; - int num; if (!output_triggers_assignment(current_output, assignment)) { continue; } /* check if this workspace's name or num is already attached to the tree */ - num = ws_name_to_number(assignment->name); - attached = ((num == -1) ? get_existing_workspace_by_name(assignment->name) : get_existing_workspace_by_num(num)) != NULL; + const int num = ws_name_to_number(assignment->name); + const bool attached = (num == -1) + ? get_existing_workspace_by_name(assignment->name) + : get_existing_workspace_by_num(num); if (attached) { continue; }