Skip to content

Commit

Permalink
launcher: export xdga tokens and use them for workspace matching
Browse files Browse the repository at this point in the history
  • Loading branch information
rpigott authored and emersion committed Nov 26, 2022
1 parent bdeb9f9 commit 30ad4dc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions include/sway/desktop/launcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ struct launcher_ctx {
struct wlr_xdg_activation_token_v1 *token;
struct wl_listener token_destroy;

bool activated;

struct sway_node *node;
struct wl_listener node_destroy;

Expand All @@ -25,4 +27,6 @@ void launcher_ctx_destroy(struct launcher_ctx *ctx);

struct launcher_ctx *launcher_ctx_create(void);

const char *launcher_ctx_get_token_name(struct launcher_ctx *ctx);

#endif
8 changes: 8 additions & 0 deletions sway/commands/exec_always.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ struct cmd_results *cmd_exec_validate(int argc, char **argv) {
return error;
}

static void export_xdga_token(struct launcher_ctx *ctx) {
const char *token = launcher_ctx_get_token_name(ctx);
setenv("XDG_ACTIVATION_TOKEN", token, 1);
}

struct cmd_results *cmd_exec_process(int argc, char **argv) {
struct cmd_results *error = NULL;
char *cmd = NULL;
Expand Down Expand Up @@ -66,6 +71,9 @@ struct cmd_results *cmd_exec_process(int argc, char **argv) {
close(fd[0]);
if ((child = fork()) == 0) {
close(fd[1]);
if (ctx) {
export_xdga_token(ctx);
}
execlp("sh", "sh", "-c", cmd, (void *)NULL);
sway_log_errno(SWAY_ERROR, "execlp failed");
_exit(1);
Expand Down
10 changes: 9 additions & 1 deletion sway/desktop/launcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ void launcher_ctx_consume(struct launcher_ctx *ctx) {
wl_list_remove(&ctx->token_destroy.link);
wl_list_init(&ctx->token_destroy.link);

wlr_xdg_activation_token_v1_destroy(ctx->token);
if (!ctx->activated) {
// An unactivated token hasn't been destroyed yet
wlr_xdg_activation_token_v1_destroy(ctx->token);
}
ctx->token = NULL;

// Prevent additional matches
Expand Down Expand Up @@ -201,3 +204,8 @@ struct launcher_ctx *launcher_ctx_create() {
wl_list_insert(&server.pending_launcher_ctxs, &ctx->link);
return ctx;
}

const char *launcher_ctx_get_token_name(struct launcher_ctx *ctx) {
const char *token = wlr_xdg_activation_token_v1_get_name(ctx->token);
return token;
}
18 changes: 17 additions & 1 deletion sway/xdg_activation_v1.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <wlr/types/wlr_xdg_activation_v1.h>
#include "sway/desktop/launcher.h"
#include "sway/tree/view.h"

void xdg_activation_v1_handle_request_activate(struct wl_listener *listener,
Expand All @@ -15,7 +16,22 @@ void xdg_activation_v1_handle_request_activate(struct wl_listener *listener,
return;
}
struct sway_view *view = xdg_surface->data;
if (!xdg_surface->mapped || view == NULL) {
if (view == NULL) {
return;
}

if (!xdg_surface->mapped) {
// This is a startup notification. If we are tracking it, the data
// field is a launcher_ctx.
struct launcher_ctx *ctx = event->token->data;
if (!ctx || ctx->activated) {
// This ctx has already been activated and cannot be used again
// for a startup notification. It will be destroyed
return;
} else {
ctx->activated = true;
view_assign_ctx(view, ctx);
}
return;
}

Expand Down

0 comments on commit 30ad4dc

Please sign in to comment.