launcher: rename pid_workspace to launcher_ctx

Soon we will match views with more than just a pid.
This commit is contained in:
Ronan Pigott 2022-11-16 15:50:34 -07:00 committed by Simon Ser
parent bd66f4943d
commit d75c9f9722
4 changed files with 106 additions and 96 deletions

View file

@ -3,10 +3,10 @@
#include <stdlib.h> #include <stdlib.h>
struct sway_workspace *root_workspace_for_pid(pid_t pid); struct sway_workspace *workspace_for_pid(pid_t pid);
void root_record_workspace_pid(pid_t pid); void launcher_ctx_create(pid_t pid);
void root_remove_workspace_pid(pid_t pid); void remove_workspace_pid(pid_t pid);
#endif #endif

View file

@ -91,7 +91,7 @@ struct cmd_results *cmd_exec_process(int argc, char **argv) {
waitpid(pid, NULL, 0); waitpid(pid, NULL, 0);
if (child > 0) { if (child > 0) {
sway_log(SWAY_DEBUG, "Child process created with pid %d", child); sway_log(SWAY_DEBUG, "Child process created with pid %d", child);
root_record_workspace_pid(child); launcher_ctx_create(child);
} else { } else {
return cmd_results_new(CMD_FAILURE, "Second fork() failed"); return cmd_results_new(CMD_FAILURE, "Second fork() failed");
} }

View file

@ -11,9 +11,9 @@
#include "sway/tree/root.h" #include "sway/tree/root.h"
#include "log.h" #include "log.h"
static struct wl_list pid_workspaces; static struct wl_list launcher_ctxs;
struct pid_workspace { struct launcher_ctx {
pid_t pid; pid_t pid;
char *name; char *name;
struct wlr_xdg_activation_token_v1 *token; struct wlr_xdg_activation_token_v1 *token;
@ -59,106 +59,121 @@ static pid_t get_parent_pid(pid_t child) {
return -1; return -1;
} }
static void pid_workspace_destroy(struct pid_workspace *pw) { static void launcher_ctx_destroy(struct launcher_ctx *ctx) {
wl_list_remove(&pw->node_destroy.link); if (ctx == NULL) {
wl_list_remove(&pw->token_destroy.link); return;
wl_list_remove(&pw->link); }
wlr_xdg_activation_token_v1_destroy(pw->token); wl_list_remove(&ctx->node_destroy.link);
free(pw->name); wl_list_remove(&ctx->token_destroy.link);
free(pw); wl_list_remove(&ctx->link);
wlr_xdg_activation_token_v1_destroy(ctx->token);
free(ctx->name);
free(ctx);
} }
struct sway_workspace *root_workspace_for_pid(pid_t pid) { static struct launcher_ctx *launcher_ctx_find_pid(pid_t pid) {
if (!pid_workspaces.prev && !pid_workspaces.next) { if (!launcher_ctxs.prev && !launcher_ctxs.next) {
wl_list_init(&pid_workspaces); wl_list_init(&launcher_ctxs);
return NULL; return NULL;
} }
struct sway_workspace *ws = NULL; struct launcher_ctx *ctx = NULL;
struct sway_output *output = 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);
do { do {
struct pid_workspace *_pw = NULL; struct launcher_ctx *_ctx = NULL;
wl_list_for_each(_pw, &pid_workspaces, link) { wl_list_for_each(_ctx, &launcher_ctxs, link) {
if (pid == _pw->pid) { if (pid == _ctx->pid) {
pw = _pw; ctx = _ctx;
sway_log(SWAY_DEBUG, sway_log(SWAY_DEBUG,
"found %s match for pid %d: %s", "found %s match for pid %d: %s",
node_type_to_str(pw->node->type), pid, node_get_name(pw->node)); node_type_to_str(ctx->node->type), pid, node_get_name(ctx->node));
break; break;
} }
} }
pid = get_parent_pid(pid); pid = get_parent_pid(pid);
} while (pid > 1); } while (pid > 1);
if (pw) { return ctx;
switch (pw->node->type) { }
static struct sway_workspace *launcher_ctx_get_workspace(
struct launcher_ctx *ctx) {
struct sway_workspace *ws = NULL;
struct sway_output *output = NULL;
switch (ctx->node->type) {
case N_CONTAINER: case N_CONTAINER:
// Unimplemented // Unimplemented
// TODO: add container matching? // TODO: add container matching?
ws = pw->node->sway_container->pending.workspace; ws = ctx->node->sway_container->pending.workspace;
break; break;
case N_WORKSPACE: case N_WORKSPACE:
ws = pw->node->sway_workspace; ws = ctx->node->sway_workspace;
break; break;
case N_OUTPUT: case N_OUTPUT:
output = pw->node->sway_output; output = ctx->node->sway_output;
ws = workspace_by_name(pw->name); ws = workspace_by_name(ctx->name);
if (!ws) { if (!ws) {
sway_log(SWAY_DEBUG, sway_log(SWAY_DEBUG,
"Creating workspace %s for pid %d because it disappeared", "Creating workspace %s for pid %d because it disappeared",
pw->name, pid); ctx->name, ctx->pid);
if (!output->enabled) { if (!output->enabled) {
sway_log(SWAY_DEBUG, sway_log(SWAY_DEBUG,
"Workspace output %s is disabled, trying another one", "Workspace output %s is disabled, trying another one",
output->wlr_output->name); output->wlr_output->name);
output = NULL; output = NULL;
} }
ws = workspace_create(output, pw->name); ws = workspace_create(output, ctx->name);
} }
break; break;
case N_ROOT: case N_ROOT:
ws = workspace_create(NULL, pw->name); ws = workspace_create(NULL, ctx->name);
break; break;
} }
pid_workspace_destroy(pw);
}
return ws; return ws;
} }
static void pw_handle_node_destroy(struct wl_listener *listener, void *data) { struct sway_workspace *workspace_for_pid(pid_t pid) {
struct pid_workspace *pw = wl_container_of(listener, pw, node_destroy); struct launcher_ctx *ctx = launcher_ctx_find_pid(pid);
switch (pw->node->type) { if (ctx == NULL) {
return NULL;
}
struct sway_workspace *ws = launcher_ctx_get_workspace(ctx);
launcher_ctx_destroy(ctx);
return ws;
}
static void ctx_handle_node_destroy(struct wl_listener *listener, void *data) {
struct launcher_ctx *ctx = wl_container_of(listener, ctx, node_destroy);
switch (ctx->node->type) {
case N_CONTAINER: case N_CONTAINER:
// Unimplemented // Unimplemented
break; break;
case N_WORKSPACE:; case N_WORKSPACE:;
struct sway_workspace *ws = pw->node->sway_workspace; struct sway_workspace *ws = ctx->node->sway_workspace;
wl_list_remove(&pw->node_destroy.link); wl_list_remove(&ctx->node_destroy.link);
wl_list_init(&pw->node_destroy.link); wl_list_init(&ctx->node_destroy.link);
// We want to save this ws name to recreate later, hopefully on the // We want to save this ws name to recreate later, hopefully on the
// same output // same output
free(pw->name); free(ctx->name);
pw->name = strdup(ws->name); ctx->name = strdup(ws->name);
if (!ws->output || ws->output->node.destroying) { if (!ws->output || ws->output->node.destroying) {
// If the output is being destroyed it would be pointless to track // 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 // If the output is being disabled, we'll find out if it's still
// disabled when we try to match it. // disabled when we try to match it.
pw->node = &root->node; ctx->node = &root->node;
break; break;
} }
pw->node = &ws->output->node; ctx->node = &ws->output->node;
wl_signal_add(&pw->node->events.destroy, &pw->node_destroy); wl_signal_add(&ctx->node->events.destroy, &ctx->node_destroy);
break; break;
case N_OUTPUT: case N_OUTPUT:
wl_list_remove(&pw->node_destroy.link); wl_list_remove(&ctx->node_destroy.link);
wl_list_init(&pw->node_destroy.link); wl_list_init(&ctx->node_destroy.link);
// We'll make the ws pw->name somewhere else // We'll make the ws ctx->name somewhere else
pw->node = &root->node; ctx->node = &root->node;
break; break;
case N_ROOT: case N_ROOT:
// Unreachable // Unreachable
@ -167,15 +182,15 @@ static void pw_handle_node_destroy(struct wl_listener *listener, void *data) {
} }
static void token_handle_destroy(struct wl_listener *listener, void *data) { static void token_handle_destroy(struct wl_listener *listener, void *data) {
struct pid_workspace *pw = wl_container_of(listener, pw, token_destroy); struct launcher_ctx *ctx = wl_container_of(listener, ctx, token_destroy);
pw->token = NULL; ctx->token = NULL;
pid_workspace_destroy(pw); launcher_ctx_destroy(ctx);
} }
void root_record_workspace_pid(pid_t pid) { void launcher_ctx_create(pid_t pid) {
sway_log(SWAY_DEBUG, "Recording workspace for process %d", pid); sway_log(SWAY_DEBUG, "Recording workspace for process %d", pid);
if (!pid_workspaces.prev && !pid_workspaces.next) { if (!launcher_ctxs.prev && !launcher_ctxs.next) {
wl_list_init(&pid_workspaces); wl_list_init(&launcher_ctxs);
} }
struct sway_seat *seat = input_manager_current_seat(); struct sway_seat *seat = input_manager_current_seat();
@ -185,34 +200,29 @@ void root_record_workspace_pid(pid_t pid) {
return; return;
} }
struct pid_workspace *pw = calloc(1, sizeof(struct pid_workspace)); struct launcher_ctx *ctx = calloc(1, sizeof(struct launcher_ctx));
struct wlr_xdg_activation_token_v1 *token = struct wlr_xdg_activation_token_v1 *token =
wlr_xdg_activation_token_v1_create(server.xdg_activation_v1); wlr_xdg_activation_token_v1_create(server.xdg_activation_v1);
token->data = pw; token->data = ctx;
pw->name = strdup(ws->name); ctx->name = strdup(ws->name);
pw->token = token; ctx->token = token;
pw->node = &ws->node; ctx->node = &ws->node;
pw->pid = pid; ctx->pid = pid;
pw->node_destroy.notify = pw_handle_node_destroy; ctx->node_destroy.notify = ctx_handle_node_destroy;
wl_signal_add(&pw->node->events.destroy, &pw->node_destroy); wl_signal_add(&ctx->node->events.destroy, &ctx->node_destroy);
pw->token_destroy.notify = token_handle_destroy; ctx->token_destroy.notify = token_handle_destroy;
wl_signal_add(&token->events.destroy, &pw->token_destroy); wl_signal_add(&token->events.destroy, &ctx->token_destroy);
wl_list_insert(&pid_workspaces, &pw->link); wl_list_insert(&launcher_ctxs, &ctx->link);
} }
void root_remove_workspace_pid(pid_t pid) { void remove_workspace_pid(pid_t pid) {
if (!pid_workspaces.prev || !pid_workspaces.next) { if (!launcher_ctxs.prev || !launcher_ctxs.next) {
return; return;
} }
struct pid_workspace *pw, *tmp; struct launcher_ctx *ctx = launcher_ctx_find_pid(pid);
wl_list_for_each_safe(pw, tmp, &pid_workspaces, link) { launcher_ctx_destroy(ctx);
if (pid == pw->pid) {
pid_workspace_destroy(pw);
return;
}
}
} }

View file

@ -569,12 +569,12 @@ static struct sway_workspace *select_workspace(struct sway_view *view) {
} }
list_free(criterias); list_free(criterias);
if (ws) { if (ws) {
root_remove_workspace_pid(view->pid); remove_workspace_pid(view->pid);
return ws; return ws;
} }
// Check if there's a PID mapping // Check if there's a PID mapping
ws = root_workspace_for_pid(view->pid); ws = workspace_for_pid(view->pid);
if (ws) { if (ws) {
return ws; return ws;
} }