diff options
author | 2022-11-16 15:50:34 -0700 | |
---|---|---|
committer | 2022-11-26 09:48:58 +0100 | |
commit | eb5021ef990fb29ff86544aea58d687ad62c757a (patch) | |
tree | 5962cd10d597a8b9cb98f1da6baf270a435af5c8 | |
parent | build: drop intermediate libraries for protocols (diff) | |
download | sway-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.
-rw-r--r-- | include/sway/desktop/launcher.h | 14 | ||||
-rw-r--r-- | include/sway/tree/root.h | 8 | ||||
-rw-r--r-- | sway/commands/exec_always.c | 1 | ||||
-rw-r--r-- | sway/commands/rename.c | 1 | ||||
-rw-r--r-- | sway/desktop/launcher.c | 189 | ||||
-rw-r--r-- | sway/meson.build | 1 | ||||
-rw-r--r-- | sway/tree/root.c | 180 | ||||
-rw-r--r-- | sway/tree/view.c | 1 |
8 files changed, 207 insertions, 188 deletions
diff --git a/include/sway/desktop/launcher.h b/include/sway/desktop/launcher.h new file mode 100644 index 00000000..cb22eb98 --- /dev/null +++ b/include/sway/desktop/launcher.h | |||
@@ -0,0 +1,14 @@ | |||
1 | #ifndef _SWAY_LAUNCHER_H | ||
2 | #define _SWAY_LAUNCHER_H | ||
3 | |||
4 | #include <stdlib.h> | ||
5 | |||
6 | struct sway_workspace *root_workspace_for_pid(pid_t pid); | ||
7 | |||
8 | void root_record_workspace_pid(pid_t pid); | ||
9 | |||
10 | void root_remove_workspace_pid(pid_t pid); | ||
11 | |||
12 | void root_rename_pid_workspaces(const char *old_name, const char *new_name); | ||
13 | |||
14 | #endif | ||
diff --git a/include/sway/tree/root.h b/include/sway/tree/root.h index af4124a1..a2c088e7 100644 --- a/include/sway/tree/root.h +++ b/include/sway/tree/root.h | |||
@@ -69,12 +69,6 @@ void root_scratchpad_show(struct sway_container *con); | |||
69 | */ | 69 | */ |
70 | void root_scratchpad_hide(struct sway_container *con); | 70 | void root_scratchpad_hide(struct sway_container *con); |
71 | 71 | ||
72 | struct sway_workspace *root_workspace_for_pid(pid_t pid); | ||
73 | |||
74 | void root_record_workspace_pid(pid_t pid); | ||
75 | |||
76 | void root_remove_workspace_pid(pid_t pid); | ||
77 | |||
78 | void root_for_each_workspace(void (*f)(struct sway_workspace *ws, void *data), | 72 | void root_for_each_workspace(void (*f)(struct sway_workspace *ws, void *data), |
79 | void *data); | 73 | void *data); |
80 | 74 | ||
@@ -92,6 +86,4 @@ struct sway_container *root_find_container( | |||
92 | 86 | ||
93 | void root_get_box(struct sway_root *root, struct wlr_box *box); | 87 | void root_get_box(struct sway_root *root, struct wlr_box *box); |
94 | 88 | ||
95 | void root_rename_pid_workspaces(const char *old_name, const char *new_name); | ||
96 | |||
97 | #endif | 89 | #endif |
diff --git a/sway/commands/exec_always.c b/sway/commands/exec_always.c index b35065c1..fab9a402 100644 --- a/sway/commands/exec_always.c +++ b/sway/commands/exec_always.c | |||
@@ -8,6 +8,7 @@ | |||
8 | #include "sway/commands.h" | 8 | #include "sway/commands.h" |
9 | #include "sway/config.h" | 9 | #include "sway/config.h" |
10 | #include "sway/server.h" | 10 | #include "sway/server.h" |
11 | #include "sway/desktop/launcher.h" | ||
11 | #include "sway/tree/container.h" | 12 | #include "sway/tree/container.h" |
12 | #include "sway/tree/root.h" | 13 | #include "sway/tree/root.h" |
13 | #include "sway/tree/workspace.h" | 14 | #include "sway/tree/workspace.h" |
diff --git a/sway/commands/rename.c b/sway/commands/rename.c index 3b855fdf..4656a410 100644 --- a/sway/commands/rename.c +++ b/sway/commands/rename.c | |||
@@ -7,6 +7,7 @@ | |||
7 | #include "sway/config.h" | 7 | #include "sway/config.h" |
8 | #include "sway/ipc-server.h" | 8 | #include "sway/ipc-server.h" |
9 | #include "sway/output.h" | 9 | #include "sway/output.h" |
10 | #include "sway/desktop/launcher.h" | ||
10 | #include "sway/tree/container.h" | 11 | #include "sway/tree/container.h" |
11 | #include "sway/tree/workspace.h" | 12 | #include "sway/tree/workspace.h" |
12 | #include "sway/tree/root.h" | 13 | #include "sway/tree/root.h" |
diff --git a/sway/desktop/launcher.c b/sway/desktop/launcher.c new file mode 100644 index 00000000..4e0d9dc1 --- /dev/null +++ b/sway/desktop/launcher.c | |||
@@ -0,0 +1,189 @@ | |||
1 | #define _POSIX_C_SOURCE 200809L | ||
2 | #include <stdlib.h> | ||
3 | #include <string.h> | ||
4 | #include "sway/input/seat.h" | ||
5 | #include "sway/output.h" | ||
6 | #include "sway/desktop/launcher.h" | ||
7 | #include "sway/tree/container.h" | ||
8 | #include "sway/tree/workspace.h" | ||
9 | #include "log.h" | ||
10 | |||
11 | static struct wl_list pid_workspaces; | ||
12 | |||
13 | struct pid_workspace { | ||
14 | pid_t pid; | ||
15 | char *workspace; | ||
16 | struct timespec time_added; | ||
17 | |||
18 | struct sway_output *output; | ||
19 | struct wl_listener output_destroy; | ||
20 | |||
21 | struct wl_list link; | ||
22 | }; | ||
23 | |||
24 | /** | ||
25 | * Get the pid of a parent process given the pid of a child process. | ||
26 | * | ||
27 | * Returns the parent pid or NULL if the parent pid cannot be determined. | ||
28 | */ | ||
29 | static pid_t get_parent_pid(pid_t child) { | ||
30 | pid_t parent = -1; | ||
31 | char file_name[100]; | ||
32 | char *buffer = NULL; | ||
33 | const char *sep = " "; | ||
34 | FILE *stat = NULL; | ||
35 | size_t buf_size = 0; | ||
36 | |||
37 | snprintf(file_name, sizeof(file_name), "/proc/%d/stat", child); | ||
38 | |||
39 | if ((stat = fopen(file_name, "r"))) { | ||
40 | if (getline(&buffer, &buf_size, stat) != -1) { | ||
41 | strtok(buffer, sep); // pid | ||
42 | strtok(NULL, sep); // executable name | ||
43 | strtok(NULL, sep); // state | ||
44 | char *token = strtok(NULL, sep); // parent pid | ||
45 | parent = strtol(token, NULL, 10); | ||
46 | } | ||
47 | free(buffer); | ||
48 | fclose(stat); | ||
49 | } | ||
50 | |||
51 | if (parent) { | ||
52 | return (parent == child) ? -1 : parent; | ||
53 | } | ||
54 | |||
55 | return -1; | ||
56 | } | ||
57 | |||
58 | static void pid_workspace_destroy(struct pid_workspace *pw) { | ||
59 | wl_list_remove(&pw->output_destroy.link); | ||
60 | wl_list_remove(&pw->link); | ||
61 | free(pw->workspace); | ||
62 | free(pw); | ||
63 | } | ||
64 | |||
65 | struct sway_workspace *root_workspace_for_pid(pid_t pid) { | ||
66 | if (!pid_workspaces.prev && !pid_workspaces.next) { | ||
67 | wl_list_init(&pid_workspaces); | ||
68 | return NULL; | ||
69 | } | ||
70 | |||
71 | struct sway_workspace *ws = NULL; | ||
72 | struct pid_workspace *pw = NULL; | ||
73 | |||
74 | sway_log(SWAY_DEBUG, "Looking up workspace for pid %d", pid); | ||
75 | |||
76 | do { | ||
77 | struct pid_workspace *_pw = NULL; | ||
78 | wl_list_for_each(_pw, &pid_workspaces, link) { | ||
79 | if (pid == _pw->pid) { | ||
80 | pw = _pw; | ||
81 | sway_log(SWAY_DEBUG, | ||
82 | "found pid_workspace for pid %d, workspace %s", | ||
83 | pid, pw->workspace); | ||
84 | goto found; | ||
85 | } | ||
86 | } | ||
87 | pid = get_parent_pid(pid); | ||
88 | } while (pid > 1); | ||
89 | |||
90 | found: | ||
91 | if (pw && pw->workspace) { | ||
92 | ws = workspace_by_name(pw->workspace); | ||
93 | |||
94 | if (!ws) { | ||
95 | sway_log(SWAY_DEBUG, | ||
96 | "Creating workspace %s for pid %d because it disappeared", | ||
97 | pw->workspace, pid); | ||
98 | |||
99 | struct sway_output *output = pw->output; | ||
100 | if (pw->output && !pw->output->enabled) { | ||
101 | sway_log(SWAY_DEBUG, | ||
102 | "Workspace output %s is disabled, trying another one", | ||
103 | pw->output->wlr_output->name); | ||
104 | output = NULL; | ||
105 | } | ||
106 | |||
107 | ws = workspace_create(output, pw->workspace); | ||
108 | } | ||
109 | |||
110 | pid_workspace_destroy(pw); | ||
111 | } | ||
112 | |||
113 | return ws; | ||
114 | } | ||
115 | |||
116 | static void pw_handle_output_destroy(struct wl_listener *listener, void *data) { | ||
117 | struct pid_workspace *pw = wl_container_of(listener, pw, output_destroy); | ||
118 | pw->output = NULL; | ||
119 | wl_list_remove(&pw->output_destroy.link); | ||
120 | wl_list_init(&pw->output_destroy.link); | ||
121 | } | ||
122 | |||
123 | void root_record_workspace_pid(pid_t pid) { | ||
124 | sway_log(SWAY_DEBUG, "Recording workspace for process %d", pid); | ||
125 | if (!pid_workspaces.prev && !pid_workspaces.next) { | ||
126 | wl_list_init(&pid_workspaces); | ||
127 | } | ||
128 | |||
129 | struct sway_seat *seat = input_manager_current_seat(); | ||
130 | struct sway_workspace *ws = seat_get_focused_workspace(seat); | ||
131 | if (!ws) { | ||
132 | sway_log(SWAY_DEBUG, "Bailing out, no workspace"); | ||
133 | return; | ||
134 | } | ||
135 | struct sway_output *output = ws->output; | ||
136 | if (!output) { | ||
137 | sway_log(SWAY_DEBUG, "Bailing out, no output"); | ||
138 | return; | ||
139 | } | ||
140 | |||
141 | struct timespec now; | ||
142 | clock_gettime(CLOCK_MONOTONIC, &now); | ||
143 | |||
144 | // Remove expired entries | ||
145 | static const int timeout = 60; | ||
146 | struct pid_workspace *old, *_old; | ||
147 | wl_list_for_each_safe(old, _old, &pid_workspaces, link) { | ||
148 | if (now.tv_sec - old->time_added.tv_sec >= timeout) { | ||
149 | pid_workspace_destroy(old); | ||
150 | } | ||
151 | } | ||
152 | |||
153 | struct pid_workspace *pw = calloc(1, sizeof(struct pid_workspace)); | ||
154 | pw->workspace = strdup(ws->name); | ||
155 | pw->output = output; | ||
156 | pw->pid = pid; | ||
157 | memcpy(&pw->time_added, &now, sizeof(struct timespec)); | ||
158 | pw->output_destroy.notify = pw_handle_output_destroy; | ||
159 | wl_signal_add(&output->wlr_output->events.destroy, &pw->output_destroy); | ||
160 | wl_list_insert(&pid_workspaces, &pw->link); | ||
161 | } | ||
162 | |||
163 | void root_remove_workspace_pid(pid_t pid) { | ||
164 | if (!pid_workspaces.prev || !pid_workspaces.next) { | ||
165 | return; | ||
166 | } | ||
167 | |||
168 | struct pid_workspace *pw, *tmp; | ||
169 | wl_list_for_each_safe(pw, tmp, &pid_workspaces, link) { | ||
170 | if (pid == pw->pid) { | ||
171 | pid_workspace_destroy(pw); | ||
172 | return; | ||
173 | } | ||
174 | } | ||
175 | } | ||
176 | |||
177 | void root_rename_pid_workspaces(const char *old_name, const char *new_name) { | ||
178 | if (!pid_workspaces.prev && !pid_workspaces.next) { | ||
179 | wl_list_init(&pid_workspaces); | ||
180 | } | ||
181 | |||
182 | struct pid_workspace *pw = NULL; | ||
183 | wl_list_for_each(pw, &pid_workspaces, link) { | ||
184 | if (strcmp(pw->workspace, old_name) == 0) { | ||
185 | free(pw->workspace); | ||
186 | pw->workspace = strdup(new_name); | ||
187 | } | ||
188 | } | ||
189 | } | ||
diff --git a/sway/meson.build b/sway/meson.build index bb6bf88c..8a73cc86 100644 --- a/sway/meson.build +++ b/sway/meson.build | |||
@@ -21,6 +21,7 @@ sway_sources = files( | |||
21 | 'desktop/surface.c', | 21 | 'desktop/surface.c', |
22 | 'desktop/transaction.c', | 22 | 'desktop/transaction.c', |
23 | 'desktop/xdg_shell.c', | 23 | 'desktop/xdg_shell.c', |
24 | 'desktop/launcher.c', | ||
24 | 25 | ||
25 | 'input/input-manager.c', | 26 | 'input/input-manager.c', |
26 | 'input/cursor.c', | 27 | 'input/cursor.c', |
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 | ||
187 | struct 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 | |||
198 | static 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 | */ | ||
205 | static 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 | |||
234 | static 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 | |||
241 | struct 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); | ||
265 | found: | ||
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 | |||
292 | static 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 | |||
299 | void 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 | |||
339 | void 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 | |||
353 | void root_for_each_workspace(void (*f)(struct sway_workspace *ws, void *data), | 187 | void 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 | |||
448 | void 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" |