aboutsummaryrefslogtreecommitdiffstats
path: root/sway/desktop/launcher.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/desktop/launcher.c')
-rw-r--r--sway/desktop/launcher.c190
1 files changed, 100 insertions, 90 deletions
diff --git a/sway/desktop/launcher.c b/sway/desktop/launcher.c
index 532d22ac..337ca975 100644
--- a/sway/desktop/launcher.c
+++ b/sway/desktop/launcher.c
@@ -11,9 +11,9 @@
11#include "sway/tree/root.h" 11#include "sway/tree/root.h"
12#include "log.h" 12#include "log.h"
13 13
14static struct wl_list pid_workspaces; 14static struct wl_list launcher_ctxs;
15 15
16struct pid_workspace { 16struct launcher_ctx {
17 pid_t pid; 17 pid_t pid;
18 char *name; 18 char *name;
19 struct wlr_xdg_activation_token_v1 *token; 19 struct wlr_xdg_activation_token_v1 *token;
@@ -59,106 +59,121 @@ static pid_t get_parent_pid(pid_t child) {
59 return -1; 59 return -1;
60} 60}
61 61
62static void pid_workspace_destroy(struct pid_workspace *pw) { 62static void launcher_ctx_destroy(struct launcher_ctx *ctx) {
63 wl_list_remove(&pw->node_destroy.link); 63 if (ctx == NULL) {
64 wl_list_remove(&pw->token_destroy.link); 64 return;
65 wl_list_remove(&pw->link); 65 }
66 wlr_xdg_activation_token_v1_destroy(pw->token); 66 wl_list_remove(&ctx->node_destroy.link);
67 free(pw->name); 67 wl_list_remove(&ctx->token_destroy.link);
68 free(pw); 68 wl_list_remove(&ctx->link);
69 wlr_xdg_activation_token_v1_destroy(ctx->token);
70 free(ctx->name);
71 free(ctx);
69} 72}
70 73
71struct sway_workspace *root_workspace_for_pid(pid_t pid) { 74static struct launcher_ctx *launcher_ctx_find_pid(pid_t pid) {
72 if (!pid_workspaces.prev && !pid_workspaces.next) { 75 if (!launcher_ctxs.prev && !launcher_ctxs.next) {
73 wl_list_init(&pid_workspaces); 76 wl_list_init(&launcher_ctxs);
74 return NULL; 77 return NULL;
75 } 78 }
76 79
77 struct sway_workspace *ws = NULL; 80 struct launcher_ctx *ctx = NULL;
78 struct sway_output *output = NULL;
79 struct pid_workspace *pw = NULL;
80
81 sway_log(SWAY_DEBUG, "Looking up workspace for pid %d", pid); 81 sway_log(SWAY_DEBUG, "Looking up workspace for pid %d", pid);
82 82
83 do { 83 do {
84 struct pid_workspace *_pw = NULL; 84 struct launcher_ctx *_ctx = NULL;
85 wl_list_for_each(_pw, &pid_workspaces, link) { 85 wl_list_for_each(_ctx, &launcher_ctxs, link) {
86 if (pid == _pw->pid) { 86 if (pid == _ctx->pid) {
87 pw = _pw; 87 ctx = _ctx;
88 sway_log(SWAY_DEBUG, 88 sway_log(SWAY_DEBUG,
89 "found %s match for pid %d: %s", 89 "found %s match for pid %d: %s",
90 node_type_to_str(pw->node->type), pid, node_get_name(pw->node)); 90 node_type_to_str(ctx->node->type), pid, node_get_name(ctx->node));
91 break; 91 break;
92 } 92 }
93 } 93 }
94 pid = get_parent_pid(pid); 94 pid = get_parent_pid(pid);
95 } while (pid > 1); 95 } while (pid > 1);
96 96
97 if (pw) { 97 return ctx;
98 switch (pw->node->type) { 98}
99 case N_CONTAINER: 99
100 // Unimplemented 100static struct sway_workspace *launcher_ctx_get_workspace(
101 // TODO: add container matching? 101 struct launcher_ctx *ctx) {
102 ws = pw->node->sway_container->pending.workspace; 102 struct sway_workspace *ws = NULL;
103 break; 103 struct sway_output *output = NULL;
104 case N_WORKSPACE: 104
105 ws = pw->node->sway_workspace; 105 switch (ctx->node->type) {
106 break; 106 case N_CONTAINER:
107 case N_OUTPUT: 107 // Unimplemented
108 output = pw->node->sway_output; 108 // TODO: add container matching?
109 ws = workspace_by_name(pw->name); 109 ws = ctx->node->sway_container->pending.workspace;
110 if (!ws) { 110 break;
111 case N_WORKSPACE:
112 ws = ctx->node->sway_workspace;
113 break;
114 case N_OUTPUT:
115 output = ctx->node->sway_output;
116 ws = workspace_by_name(ctx->name);
117 if (!ws) {
118 sway_log(SWAY_DEBUG,
119 "Creating workspace %s for pid %d because it disappeared",
120 ctx->name, ctx->pid);
121 if (!output->enabled) {
111 sway_log(SWAY_DEBUG, 122 sway_log(SWAY_DEBUG,
112 "Creating workspace %s for pid %d because it disappeared", 123 "Workspace output %s is disabled, trying another one",
113 pw->name, pid); 124 output->wlr_output->name);
114 if (!output->enabled) { 125 output = NULL;
115 sway_log(SWAY_DEBUG,
116 "Workspace output %s is disabled, trying another one",
117 output->wlr_output->name);
118 output = NULL;
119 }
120 ws = workspace_create(output, pw->name);
121 } 126 }
122 break; 127 ws = workspace_create(output, ctx->name);
123 case N_ROOT:
124 ws = workspace_create(NULL, pw->name);
125 break;
126 } 128 }
127 pid_workspace_destroy(pw); 129 break;
130 case N_ROOT:
131 ws = workspace_create(NULL, ctx->name);
132 break;
128 } 133 }
129 134
130 return ws; 135 return ws;
131} 136}
132 137
133static void pw_handle_node_destroy(struct wl_listener *listener, void *data) { 138struct sway_workspace *workspace_for_pid(pid_t pid) {
134 struct pid_workspace *pw = wl_container_of(listener, pw, node_destroy); 139 struct launcher_ctx *ctx = launcher_ctx_find_pid(pid);
135 switch (pw->node->type) { 140 if (ctx == NULL) {
141 return NULL;
142 }
143 struct sway_workspace *ws = launcher_ctx_get_workspace(ctx);
144 launcher_ctx_destroy(ctx);
145 return ws;
146}
147
148static void ctx_handle_node_destroy(struct wl_listener *listener, void *data) {
149 struct launcher_ctx *ctx = wl_container_of(listener, ctx, node_destroy);
150 switch (ctx->node->type) {
136 case N_CONTAINER: 151 case N_CONTAINER:
137 // Unimplemented 152 // Unimplemented
138 break; 153 break;
139 case N_WORKSPACE:; 154 case N_WORKSPACE:;
140 struct sway_workspace *ws = pw->node->sway_workspace; 155 struct sway_workspace *ws = ctx->node->sway_workspace;
141 wl_list_remove(&pw->node_destroy.link); 156 wl_list_remove(&ctx->node_destroy.link);
142 wl_list_init(&pw->node_destroy.link); 157 wl_list_init(&ctx->node_destroy.link);
143 // We want to save this ws name to recreate later, hopefully on the 158 // We want to save this ws name to recreate later, hopefully on the
144 // same output 159 // same output
145 free(pw->name); 160 free(ctx->name);
146 pw->name = strdup(ws->name); 161 ctx->name = strdup(ws->name);
147 if (!ws->output || ws->output->node.destroying) { 162 if (!ws->output || ws->output->node.destroying) {
148 // If the output is being destroyed it would be pointless to track 163 // If the output is being destroyed it would be pointless to track
149 // If the output is being disabled, we'll find out if it's still 164 // If the output is being disabled, we'll find out if it's still
150 // disabled when we try to match it. 165 // disabled when we try to match it.
151 pw->node = &root->node; 166 ctx->node = &root->node;
152 break; 167 break;
153 } 168 }
154 pw->node = &ws->output->node; 169 ctx->node = &ws->output->node;
155 wl_signal_add(&pw->node->events.destroy, &pw->node_destroy); 170 wl_signal_add(&ctx->node->events.destroy, &ctx->node_destroy);
156 break; 171 break;
157 case N_OUTPUT: 172 case N_OUTPUT:
158 wl_list_remove(&pw->node_destroy.link); 173 wl_list_remove(&ctx->node_destroy.link);
159 wl_list_init(&pw->node_destroy.link); 174 wl_list_init(&ctx->node_destroy.link);
160 // We'll make the ws pw->name somewhere else 175 // We'll make the ws ctx->name somewhere else
161 pw->node = &root->node; 176 ctx->node = &root->node;
162 break; 177 break;
163 case N_ROOT: 178 case N_ROOT:
164 // Unreachable 179 // Unreachable
@@ -167,15 +182,15 @@ static void pw_handle_node_destroy(struct wl_listener *listener, void *data) {
167} 182}
168 183
169static void token_handle_destroy(struct wl_listener *listener, void *data) { 184static void token_handle_destroy(struct wl_listener *listener, void *data) {
170 struct pid_workspace *pw = wl_container_of(listener, pw, token_destroy); 185 struct launcher_ctx *ctx = wl_container_of(listener, ctx, token_destroy);
171 pw->token = NULL; 186 ctx->token = NULL;
172 pid_workspace_destroy(pw); 187 launcher_ctx_destroy(ctx);
173} 188}
174 189
175void root_record_workspace_pid(pid_t pid) { 190void launcher_ctx_create(pid_t pid) {
176 sway_log(SWAY_DEBUG, "Recording workspace for process %d", pid); 191 sway_log(SWAY_DEBUG, "Recording workspace for process %d", pid);
177 if (!pid_workspaces.prev && !pid_workspaces.next) { 192 if (!launcher_ctxs.prev && !launcher_ctxs.next) {
178 wl_list_init(&pid_workspaces); 193 wl_list_init(&launcher_ctxs);
179 } 194 }
180 195
181 struct sway_seat *seat = input_manager_current_seat(); 196 struct sway_seat *seat = input_manager_current_seat();
@@ -185,34 +200,29 @@ void root_record_workspace_pid(pid_t pid) {
185 return; 200 return;
186 } 201 }
187 202
188 struct pid_workspace *pw = calloc(1, sizeof(struct pid_workspace)); 203 struct launcher_ctx *ctx = calloc(1, sizeof(struct launcher_ctx));
189 struct wlr_xdg_activation_token_v1 *token = 204 struct wlr_xdg_activation_token_v1 *token =
190 wlr_xdg_activation_token_v1_create(server.xdg_activation_v1); 205 wlr_xdg_activation_token_v1_create(server.xdg_activation_v1);
191 token->data = pw; 206 token->data = ctx;
192 pw->name = strdup(ws->name); 207 ctx->name = strdup(ws->name);
193 pw->token = token; 208 ctx->token = token;
194 pw->node = &ws->node; 209 ctx->node = &ws->node;
195 pw->pid = pid; 210 ctx->pid = pid;
196 211
197 pw->node_destroy.notify = pw_handle_node_destroy; 212 ctx->node_destroy.notify = ctx_handle_node_destroy;
198 wl_signal_add(&pw->node->events.destroy, &pw->node_destroy); 213 wl_signal_add(&ctx->node->events.destroy, &ctx->node_destroy);
199 214
200 pw->token_destroy.notify = token_handle_destroy; 215 ctx->token_destroy.notify = token_handle_destroy;
201 wl_signal_add(&token->events.destroy, &pw->token_destroy); 216 wl_signal_add(&token->events.destroy, &ctx->token_destroy);
202 217
203 wl_list_insert(&pid_workspaces, &pw->link); 218 wl_list_insert(&launcher_ctxs, &ctx->link);
204} 219}
205 220
206void root_remove_workspace_pid(pid_t pid) { 221void remove_workspace_pid(pid_t pid) {
207 if (!pid_workspaces.prev || !pid_workspaces.next) { 222 if (!launcher_ctxs.prev || !launcher_ctxs.next) {
208 return; 223 return;
209 } 224 }
210 225
211 struct pid_workspace *pw, *tmp; 226 struct launcher_ctx *ctx = launcher_ctx_find_pid(pid);
212 wl_list_for_each_safe(pw, tmp, &pid_workspaces, link) { 227 launcher_ctx_destroy(ctx);
213 if (pid == pw->pid) {
214 pid_workspace_destroy(pw);
215 return;
216 }
217 }
218} 228}