Skip to content

Commit

Permalink
launcher: export X startup ids 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 30ad4dc commit 28fda4c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/sway/tree/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ struct sway_xwayland_view {
struct wl_listener set_title;
struct wl_listener set_class;
struct wl_listener set_role;
struct wl_listener set_startup_id;
struct wl_listener set_window_type;
struct wl_listener set_hints;
struct wl_listener set_decorations;
Expand Down
11 changes: 10 additions & 1 deletion sway/commands/exec_always.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ static void export_xdga_token(struct launcher_ctx *ctx) {
setenv("XDG_ACTIVATION_TOKEN", token, 1);
}

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

struct cmd_results *cmd_exec_process(int argc, char **argv) {
struct cmd_results *error = NULL;
char *cmd = NULL;
bool no_startup_id = false;
if (strcmp(argv[0], "--no-startup-id") == 0) {
sway_log(SWAY_INFO, "exec switch '--no-startup-id' not supported, ignored.");
no_startup_id = true;
--argc; ++argv;
if ((error = checkarg(argc, argv[-1], EXPECTED_AT_LEAST, 1))) {
return error;
Expand Down Expand Up @@ -74,6 +80,9 @@ struct cmd_results *cmd_exec_process(int argc, char **argv) {
if (ctx) {
export_xdga_token(ctx);
}
if (ctx && !no_startup_id) {
export_startup_id(ctx);
}
execlp("sh", "sh", "-c", cmd, (void *)NULL);
sway_log_errno(SWAY_ERROR, "execlp failed");
_exit(1);
Expand Down
32 changes: 32 additions & 0 deletions sway/desktop/xwayland.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <wayland-server-core.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_xdg_activation_v1.h>
#include <wlr/xwayland.h>
#include <xcb/xcb_icccm.h>
#include "log.h"
Expand All @@ -16,6 +17,7 @@
#include "sway/output.h"
#include "sway/tree/arrange.h"
#include "sway/tree/container.h"
#include "sway/server.h"
#include "sway/tree/view.h"
#include "sway/tree/workspace.h"

Expand Down Expand Up @@ -466,6 +468,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) {
wl_list_remove(&xwayland_view->set_title.link);
wl_list_remove(&xwayland_view->set_class.link);
wl_list_remove(&xwayland_view->set_role.link);
wl_list_remove(&xwayland_view->set_startup_id.link);
wl_list_remove(&xwayland_view->set_window_type.link);
wl_list_remove(&xwayland_view->set_hints.link);
wl_list_remove(&xwayland_view->set_decorations.link);
Expand Down Expand Up @@ -666,6 +669,31 @@ static void handle_set_role(struct wl_listener *listener, void *data) {
view_execute_criteria(view);
}

static void handle_set_startup_id(struct wl_listener *listener, void *data) {
struct sway_xwayland_view *xwayland_view =
wl_container_of(listener, xwayland_view, set_startup_id);
struct sway_view *view = &xwayland_view->view;
struct wlr_xwayland_surface *xsurface = view->wlr_xwayland_surface;
if (xsurface->startup_id == NULL) {
return;
}

struct wlr_xdg_activation_token_v1 *token =
wlr_xdg_activation_v1_find_token(
server.xdg_activation_v1, xsurface->startup_id);
if (token == NULL) {
// Tried to activate with an unknown or expired token
return;
}

struct launcher_ctx *ctx = token->data;
if (token->data == NULL) {
// TODO: support external launchers in X
return;
}
view_assign_ctx(view, ctx);
}

static void handle_set_window_type(struct wl_listener *listener, void *data) {
struct sway_xwayland_view *xwayland_view =
wl_container_of(listener, xwayland_view, set_window_type);
Expand Down Expand Up @@ -751,6 +779,10 @@ struct sway_xwayland_view *create_xwayland_view(struct wlr_xwayland_surface *xsu
wl_signal_add(&xsurface->events.set_role, &xwayland_view->set_role);
xwayland_view->set_role.notify = handle_set_role;

wl_signal_add(&xsurface->events.set_startup_id,
&xwayland_view->set_startup_id);
xwayland_view->set_startup_id.notify = handle_set_startup_id;

wl_signal_add(&xsurface->events.set_window_type,
&xwayland_view->set_window_type);
xwayland_view->set_window_type.notify = handle_set_window_type;
Expand Down

0 comments on commit 28fda4c

Please sign in to comment.