aboutsummaryrefslogtreecommitdiffstats
path: root/sway/tree
diff options
context:
space:
mode:
authorLibravatar Ronan Pigott <ronan@rjp.ie>2022-11-16 15:50:34 -0700
committerLibravatar Simon Ser <contact@emersion.fr>2022-11-26 09:48:58 +0100
commiteb5021ef990fb29ff86544aea58d687ad62c757a (patch)
tree5962cd10d597a8b9cb98f1da6baf270a435af5c8 /sway/tree
parentbuild: drop intermediate libraries for protocols (diff)
downloadsway-eb5021ef990fb29ff86544aea58d687ad62c757a.tar.gz
sway-eb5021ef990fb29ff86544aea58d687ad62c757a.tar.zst
sway-eb5021ef990fb29ff86544aea58d687ad62c757a.zip
root: move the workspace matching code to its own file
This removes the pid_workspace bits from tree/root before it gets too interesting. No functional change.
Diffstat (limited to 'sway/tree')
-rw-r--r--sway/tree/root.c180
-rw-r--r--sway/tree/view.c1
2 files changed, 1 insertions, 180 deletions
diff --git a/sway/tree/root.c b/sway/tree/root.c
index 7df0b237..8934721f 100644
--- a/sway/tree/root.c
+++ b/sway/tree/root.c
@@ -184,172 +184,6 @@ void root_scratchpad_hide(struct sway_container *con) {
184 ipc_event_window(con, "move"); 184 ipc_event_window(con, "move");
185} 185}
186 186
187struct pid_workspace {
188 pid_t pid;
189 char *workspace;
190 struct timespec time_added;
191
192 struct sway_output *output;
193 struct wl_listener output_destroy;
194
195 struct wl_list link;
196};
197
198static struct wl_list pid_workspaces;
199
200/**
201 * Get the pid of a parent process given the pid of a child process.
202 *
203 * Returns the parent pid or NULL if the parent pid cannot be determined.
204 */
205static pid_t get_parent_pid(pid_t child) {
206 pid_t parent = -1;
207 char file_name[100];
208 char *buffer = NULL;
209 const char *sep = " ";
210 FILE *stat = NULL;
211 size_t buf_size = 0;
212
213 snprintf(file_name, sizeof(file_name), "/proc/%d/stat", child);
214
215 if ((stat = fopen(file_name, "r"))) {
216 if (getline(&buffer, &buf_size, stat) != -1) {
217 strtok(buffer, sep); // pid
218 strtok(NULL, sep); // executable name
219 strtok(NULL, sep); // state
220 char *token = strtok(NULL, sep); // parent pid
221 parent = strtol(token, NULL, 10);
222 }
223 free(buffer);
224 fclose(stat);
225 }
226
227 if (parent) {
228 return (parent == child) ? -1 : parent;
229 }
230
231 return -1;
232}
233
234static void pid_workspace_destroy(struct pid_workspace *pw) {
235 wl_list_remove(&pw->output_destroy.link);
236 wl_list_remove(&pw->link);
237 free(pw->workspace);
238 free(pw);
239}
240
241struct sway_workspace *root_workspace_for_pid(pid_t pid) {
242 if (!pid_workspaces.prev && !pid_workspaces.next) {
243 wl_list_init(&pid_workspaces);
244 return NULL;
245 }
246
247 struct sway_workspace *ws = NULL;
248 struct pid_workspace *pw = NULL;
249
250 sway_log(SWAY_DEBUG, "Looking up workspace for pid %d", pid);
251
252 do {
253 struct pid_workspace *_pw = NULL;
254 wl_list_for_each(_pw, &pid_workspaces, link) {
255 if (pid == _pw->pid) {
256 pw = _pw;
257 sway_log(SWAY_DEBUG,
258 "found pid_workspace for pid %d, workspace %s",
259 pid, pw->workspace);
260 goto found;
261 }
262 }
263 pid = get_parent_pid(pid);
264 } while (pid > 1);
265found:
266
267 if (pw && pw->workspace) {
268 ws = workspace_by_name(pw->workspace);
269
270 if (!ws) {
271 sway_log(SWAY_DEBUG,
272 "Creating workspace %s for pid %d because it disappeared",
273 pw->workspace, pid);
274
275 struct sway_output *output = pw->output;
276 if (pw->output && !pw->output->enabled) {
277 sway_log(SWAY_DEBUG,
278 "Workspace output %s is disabled, trying another one",
279 pw->output->wlr_output->name);
280 output = NULL;
281 }
282
283 ws = workspace_create(output, pw->workspace);
284 }
285
286 pid_workspace_destroy(pw);
287 }
288
289 return ws;
290}
291
292static void pw_handle_output_destroy(struct wl_listener *listener, void *data) {
293 struct pid_workspace *pw = wl_container_of(listener, pw, output_destroy);
294 pw->output = NULL;
295 wl_list_remove(&pw->output_destroy.link);
296 wl_list_init(&pw->output_destroy.link);
297}
298
299void root_record_workspace_pid(pid_t pid) {
300 sway_log(SWAY_DEBUG, "Recording workspace for process %d", pid);
301 if (!pid_workspaces.prev && !pid_workspaces.next) {
302 wl_list_init(&pid_workspaces);
303 }
304
305 struct sway_seat *seat = input_manager_current_seat();
306 struct sway_workspace *ws = seat_get_focused_workspace(seat);
307 if (!ws) {
308 sway_log(SWAY_DEBUG, "Bailing out, no workspace");
309 return;
310 }
311 struct sway_output *output = ws->output;
312 if (!output) {
313 sway_log(SWAY_DEBUG, "Bailing out, no output");
314 return;
315 }
316
317 struct timespec now;
318 clock_gettime(CLOCK_MONOTONIC, &now);
319
320 // Remove expired entries
321 static const int timeout = 60;
322 struct pid_workspace *old, *_old;
323 wl_list_for_each_safe(old, _old, &pid_workspaces, link) {
324 if (now.tv_sec - old->time_added.tv_sec >= timeout) {
325 pid_workspace_destroy(old);
326 }
327 }
328
329 struct pid_workspace *pw = calloc(1, sizeof(struct pid_workspace));
330 pw->workspace = strdup(ws->name);
331 pw->output = output;
332 pw->pid = pid;
333 memcpy(&pw->time_added, &now, sizeof(struct timespec));
334 pw->output_destroy.notify = pw_handle_output_destroy;
335 wl_signal_add(&output->wlr_output->events.destroy, &pw->output_destroy);
336 wl_list_insert(&pid_workspaces, &pw->link);
337}
338
339void root_remove_workspace_pid(pid_t pid) {
340 if (!pid_workspaces.prev || !pid_workspaces.next) {
341 return;
342 }
343
344 struct pid_workspace *pw, *tmp;
345 wl_list_for_each_safe(pw, tmp, &pid_workspaces, link) {
346 if (pid == pw->pid) {
347 pid_workspace_destroy(pw);
348 return;
349 }
350 }
351}
352
353void root_for_each_workspace(void (*f)(struct sway_workspace *ws, void *data), 187void root_for_each_workspace(void (*f)(struct sway_workspace *ws, void *data),
354 void *data) { 188 void *data) {
355 for (int i = 0; i < root->outputs->length; ++i) { 189 for (int i = 0; i < root->outputs->length; ++i) {
@@ -444,17 +278,3 @@ void root_get_box(struct sway_root *root, struct wlr_box *box) {
444 box->width = root->width; 278 box->width = root->width;
445 box->height = root->height; 279 box->height = root->height;
446} 280}
447
448void root_rename_pid_workspaces(const char *old_name, const char *new_name) {
449 if (!pid_workspaces.prev && !pid_workspaces.next) {
450 wl_list_init(&pid_workspaces);
451 }
452
453 struct pid_workspace *pw = NULL;
454 wl_list_for_each(pw, &pid_workspaces, link) {
455 if (strcmp(pw->workspace, old_name) == 0) {
456 free(pw->workspace);
457 pw->workspace = strdup(new_name);
458 }
459 }
460}
diff --git a/sway/tree/view.c b/sway/tree/view.c
index 589a3f7e..edc3e2af 100644
--- a/sway/tree/view.c
+++ b/sway/tree/view.c
@@ -19,6 +19,7 @@
19#include "sway/desktop.h" 19#include "sway/desktop.h"
20#include "sway/desktop/transaction.h" 20#include "sway/desktop/transaction.h"
21#include "sway/desktop/idle_inhibit_v1.h" 21#include "sway/desktop/idle_inhibit_v1.h"
22#include "sway/desktop/launcher.h"
22#include "sway/input/cursor.h" 23#include "sway/input/cursor.h"
23#include "sway/ipc-server.h" 24#include "sway/ipc-server.h"
24#include "sway/output.h" 25#include "sway/output.h"