launcher: track workspaces by node

This removes the need to rename the pid_workspaces when a workspace
is renamed.

It also opens the possibility of tracking other node types. Tracking
containers would allow application to be placed correctly in the
container tree even if the user has moved their focus elsewhere since
it was launched.
This commit is contained in:
Ronan Pigott 2022-11-16 15:50:34 -07:00 committed by Simon Ser
parent 1c4b94ae3c
commit 3b49f2782e
3 changed files with 77 additions and 57 deletions

View file

@ -9,6 +9,4 @@ void root_record_workspace_pid(pid_t pid);
void root_remove_workspace_pid(pid_t pid); void root_remove_workspace_pid(pid_t pid);
void root_rename_pid_workspaces(const char *old_name, const char *new_name);
#endif #endif

View file

@ -92,8 +92,6 @@ struct cmd_results *cmd_rename(int argc, char **argv) {
sway_log(SWAY_DEBUG, "renaming workspace '%s' to '%s'", workspace->name, new_name); sway_log(SWAY_DEBUG, "renaming workspace '%s' to '%s'", workspace->name, new_name);
root_rename_pid_workspaces(workspace->name, new_name);
free(workspace->name); free(workspace->name);
workspace->name = new_name; workspace->name = new_name;

View file

@ -4,19 +4,21 @@
#include "sway/input/seat.h" #include "sway/input/seat.h"
#include "sway/output.h" #include "sway/output.h"
#include "sway/desktop/launcher.h" #include "sway/desktop/launcher.h"
#include "sway/tree/node.h"
#include "sway/tree/container.h" #include "sway/tree/container.h"
#include "sway/tree/workspace.h" #include "sway/tree/workspace.h"
#include "sway/tree/root.h"
#include "log.h" #include "log.h"
static struct wl_list pid_workspaces; static struct wl_list pid_workspaces;
struct pid_workspace { struct pid_workspace {
pid_t pid; pid_t pid;
char *workspace; char *name;
struct timespec time_added; struct timespec time_added;
struct sway_output *output; struct sway_node *node;
struct wl_listener output_destroy; struct wl_listener node_destroy;
struct wl_list link; struct wl_list link;
}; };
@ -56,9 +58,9 @@ static pid_t get_parent_pid(pid_t child) {
} }
static void pid_workspace_destroy(struct pid_workspace *pw) { static void pid_workspace_destroy(struct pid_workspace *pw) {
wl_list_remove(&pw->output_destroy.link); wl_list_remove(&pw->node_destroy.link);
wl_list_remove(&pw->link); wl_list_remove(&pw->link);
free(pw->workspace); free(pw->name);
free(pw); free(pw);
} }
@ -69,6 +71,7 @@ struct sway_workspace *root_workspace_for_pid(pid_t pid) {
} }
struct sway_workspace *ws = NULL; struct sway_workspace *ws = NULL;
struct sway_output *output = NULL;
struct pid_workspace *pw = NULL; struct pid_workspace *pw = NULL;
sway_log(SWAY_DEBUG, "Looking up workspace for pid %d", pid); sway_log(SWAY_DEBUG, "Looking up workspace for pid %d", pid);
@ -79,45 +82,84 @@ struct sway_workspace *root_workspace_for_pid(pid_t pid) {
if (pid == _pw->pid) { if (pid == _pw->pid) {
pw = _pw; pw = _pw;
sway_log(SWAY_DEBUG, sway_log(SWAY_DEBUG,
"found pid_workspace for pid %d, workspace %s", "found %s match for pid %d: %s",
pid, pw->workspace); node_type_to_str(pw->node->type), pid, node_get_name(pw->node));
goto found; break;
} }
} }
pid = get_parent_pid(pid); pid = get_parent_pid(pid);
} while (pid > 1); } while (pid > 1);
found: if (pw) {
if (pw && pw->workspace) { switch (pw->node->type) {
ws = workspace_by_name(pw->workspace); case N_CONTAINER:
// Unimplemented
if (!ws) { // TODO: add container matching?
sway_log(SWAY_DEBUG, ws = pw->node->sway_container->pending.workspace;
"Creating workspace %s for pid %d because it disappeared", break;
pw->workspace, pid); case N_WORKSPACE:
ws = pw->node->sway_workspace;
struct sway_output *output = pw->output; break;
if (pw->output && !pw->output->enabled) { case N_OUTPUT:
output = pw->node->sway_output;
ws = workspace_by_name(pw->name);
if (!ws) {
sway_log(SWAY_DEBUG, sway_log(SWAY_DEBUG,
"Workspace output %s is disabled, trying another one", "Creating workspace %s for pid %d because it disappeared",
pw->output->wlr_output->name); pw->name, pid);
output = NULL; if (!output->enabled) {
sway_log(SWAY_DEBUG,
"Workspace output %s is disabled, trying another one",
output->wlr_output->name);
output = NULL;
}
ws = workspace_create(output, pw->name);
} }
break;
ws = workspace_create(output, pw->workspace); case N_ROOT:
ws = workspace_create(NULL, pw->name);
break;
} }
pid_workspace_destroy(pw); pid_workspace_destroy(pw);
} }
return ws; return ws;
} }
static void pw_handle_output_destroy(struct wl_listener *listener, void *data) { static void pw_handle_node_destroy(struct wl_listener *listener, void *data) {
struct pid_workspace *pw = wl_container_of(listener, pw, output_destroy); struct pid_workspace *pw = wl_container_of(listener, pw, node_destroy);
pw->output = NULL; switch (pw->node->type) {
wl_list_remove(&pw->output_destroy.link); case N_CONTAINER:
wl_list_init(&pw->output_destroy.link); // Unimplemented
break;
case N_WORKSPACE:;
struct sway_workspace *ws = pw->node->sway_workspace;
wl_list_remove(&pw->node_destroy.link);
wl_list_init(&pw->node_destroy.link);
// We want to save this ws name to recreate later, hopefully on the
// same output
free(pw->name);
pw->name = strdup(ws->name);
if (!ws->output || ws->output->node.destroying) {
// If the output is being destroyed it would be pointless to track
// If the output is being disabled, we'll find out if it's still
// disabled when we try to match it.
pw->node = &root->node;
break;
}
pw->node = &ws->output->node;
wl_signal_add(&pw->node->events.destroy, &pw->node_destroy);
break;
case N_OUTPUT:
wl_list_remove(&pw->node_destroy.link);
wl_list_init(&pw->node_destroy.link);
// We'll make the ws pw->name somewhere else
pw->node = &root->node;
break;
case N_ROOT:
// Unreachable
break;
}
} }
void root_record_workspace_pid(pid_t pid) { void root_record_workspace_pid(pid_t pid) {
@ -132,11 +174,6 @@ void root_record_workspace_pid(pid_t pid) {
sway_log(SWAY_DEBUG, "Bailing out, no workspace"); sway_log(SWAY_DEBUG, "Bailing out, no workspace");
return; return;
} }
struct sway_output *output = ws->output;
if (!output) {
sway_log(SWAY_DEBUG, "Bailing out, no output");
return;
}
struct timespec now; struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now); clock_gettime(CLOCK_MONOTONIC, &now);
@ -151,12 +188,13 @@ void root_record_workspace_pid(pid_t pid) {
} }
struct pid_workspace *pw = calloc(1, sizeof(struct pid_workspace)); struct pid_workspace *pw = calloc(1, sizeof(struct pid_workspace));
pw->workspace = strdup(ws->name); pw->name = strdup(ws->name);
pw->output = output; pw->node = &ws->node;
pw->pid = pid; pw->pid = pid;
memcpy(&pw->time_added, &now, sizeof(struct timespec)); memcpy(&pw->time_added, &now, sizeof(struct timespec));
pw->output_destroy.notify = pw_handle_output_destroy; pw->node_destroy.notify = pw_handle_node_destroy;
wl_signal_add(&output->wlr_output->events.destroy, &pw->output_destroy); wl_signal_add(&pw->node->events.destroy, &pw->node_destroy);
wl_list_insert(&pid_workspaces, &pw->link); wl_list_insert(&pid_workspaces, &pw->link);
} }
@ -173,17 +211,3 @@ void root_remove_workspace_pid(pid_t pid) {
} }
} }
} }
void root_rename_pid_workspaces(const char *old_name, const char *new_name) {
if (!pid_workspaces.prev && !pid_workspaces.next) {
wl_list_init(&pid_workspaces);
}
struct pid_workspace *pw = NULL;
wl_list_for_each(pw, &pid_workspaces, link) {
if (strcmp(pw->workspace, old_name) == 0) {
free(pw->workspace);
pw->workspace = strdup(new_name);
}
}
}