summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Ryan Dwyer <ryandwyer1@gmail.com>2018-08-04 14:01:49 +1000
committerLibravatar Ryan Dwyer <ryandwyer1@gmail.com>2018-08-04 14:01:49 +1000
commit30e7e0f7c7d3d3ce2851f2e70842d735343fb402 (patch)
treebc82fb73e33446c5ec0b50c1bae73658e3eeb6b4
parentSeparate root-related code (diff)
downloadsway-30e7e0f7c7d3d3ce2851f2e70842d735343fb402.tar.gz
sway-30e7e0f7c7d3d3ce2851f2e70842d735343fb402.tar.zst
sway-30e7e0f7c7d3d3ce2851f2e70842d735343fb402.zip
Move workspace pid code to root.c
-rw-r--r--include/sway/tree/root.h4
-rw-r--r--include/sway/tree/workspace.h4
-rw-r--r--sway/commands/exec_always.c2
-rw-r--r--sway/tree/root.c115
-rw-r--r--sway/tree/view.c2
-rw-r--r--sway/tree/workspace.c113
6 files changed, 121 insertions, 119 deletions
diff --git a/include/sway/tree/root.h b/include/sway/tree/root.h
index ada3c73f..edb7c817 100644
--- a/include/sway/tree/root.h
+++ b/include/sway/tree/root.h
@@ -54,4 +54,8 @@ void root_scratchpad_show(struct sway_container *con);
54 */ 54 */
55void root_scratchpad_hide(struct sway_container *con); 55void root_scratchpad_hide(struct sway_container *con);
56 56
57struct sway_container *root_workspace_for_pid(pid_t pid);
58
59void root_record_workspace_pid(pid_t pid);
60
57#endif 61#endif
diff --git a/include/sway/tree/workspace.h b/include/sway/tree/workspace.h
index 5ae0ae3a..3337f2c8 100644
--- a/include/sway/tree/workspace.h
+++ b/include/sway/tree/workspace.h
@@ -44,10 +44,6 @@ void workspace_output_add_priority(struct sway_container *workspace,
44struct sway_container *workspace_output_get_highest_available( 44struct sway_container *workspace_output_get_highest_available(
45 struct sway_container *ws, struct sway_container *exclude); 45 struct sway_container *ws, struct sway_container *exclude);
46 46
47struct sway_container *workspace_for_pid(pid_t pid);
48
49void workspace_record_pid(pid_t pid);
50
51void workspace_detect_urgent(struct sway_container *workspace); 47void workspace_detect_urgent(struct sway_container *workspace);
52 48
53#endif 49#endif
diff --git a/sway/commands/exec_always.c b/sway/commands/exec_always.c
index c730cb8b..00e39ae7 100644
--- a/sway/commands/exec_always.c
+++ b/sway/commands/exec_always.c
@@ -78,7 +78,7 @@ struct cmd_results *cmd_exec_always(int argc, char **argv) {
78 waitpid(pid, NULL, 0); 78 waitpid(pid, NULL, 0);
79 if (child > 0) { 79 if (child > 0) {
80 wlr_log(WLR_DEBUG, "Child process created with pid %d", child); 80 wlr_log(WLR_DEBUG, "Child process created with pid %d", child);
81 workspace_record_pid(child); 81 root_record_workspace_pid(child);
82 } else { 82 } else {
83 return cmd_results_new(CMD_FAILURE, "exec_always", 83 return cmd_results_new(CMD_FAILURE, "exec_always",
84 "Second fork() failed"); 84 "Second fork() failed");
diff --git a/sway/tree/root.c b/sway/tree/root.c
index 9f3965be..79f2194e 100644
--- a/sway/tree/root.c
+++ b/sway/tree/root.c
@@ -4,12 +4,14 @@
4#include <string.h> 4#include <string.h>
5#include <wlr/types/wlr_output_layout.h> 5#include <wlr/types/wlr_output_layout.h>
6#include "sway/input/seat.h" 6#include "sway/input/seat.h"
7#include "sway/output.h"
7#include "sway/tree/arrange.h" 8#include "sway/tree/arrange.h"
8#include "sway/tree/container.h" 9#include "sway/tree/container.h"
9#include "sway/tree/root.h" 10#include "sway/tree/root.h"
10#include "sway/tree/workspace.h" 11#include "sway/tree/workspace.h"
11#include "list.h" 12#include "list.h"
12#include "log.h" 13#include "log.h"
14#include "util.h"
13 15
14struct sway_container root_container; 16struct sway_container root_container;
15 17
@@ -145,3 +147,116 @@ void root_scratchpad_hide(struct sway_container *con) {
145 } 147 }
146 list_move_to_end(root_container.sway_root->scratchpad, con); 148 list_move_to_end(root_container.sway_root->scratchpad, con);
147} 149}
150
151struct pid_workspace {
152 pid_t pid;
153 char *workspace;
154 struct timespec time_added;
155
156 struct sway_container *output;
157 struct wl_listener output_destroy;
158
159 struct wl_list link;
160};
161
162static struct wl_list pid_workspaces;
163
164struct sway_container *root_workspace_for_pid(pid_t pid) {
165 if (!pid_workspaces.prev && !pid_workspaces.next) {
166 wl_list_init(&pid_workspaces);
167 return NULL;
168 }
169
170 struct sway_container *ws = NULL;
171 struct pid_workspace *pw = NULL;
172
173 wlr_log(WLR_DEBUG, "Looking up workspace for pid %d", pid);
174
175 do {
176 struct pid_workspace *_pw = NULL;
177 wl_list_for_each(_pw, &pid_workspaces, link) {
178 if (pid == _pw->pid) {
179 pw = _pw;
180 wlr_log(WLR_DEBUG,
181 "found pid_workspace for pid %d, workspace %s",
182 pid, pw->workspace);
183 goto found;
184 }
185 }
186 pid = get_parent_pid(pid);
187 } while (pid > 1);
188found:
189
190 if (pw && pw->workspace) {
191 ws = workspace_by_name(pw->workspace);
192
193 if (!ws) {
194 wlr_log(WLR_DEBUG,
195 "Creating workspace %s for pid %d because it disappeared",
196 pw->workspace, pid);
197 ws = workspace_create(pw->output, pw->workspace);
198 }
199
200 wl_list_remove(&pw->output_destroy.link);
201 wl_list_remove(&pw->link);
202 free(pw->workspace);
203 free(pw);
204 }
205
206 return ws;
207}
208
209static void pw_handle_output_destroy(struct wl_listener *listener, void *data) {
210 struct pid_workspace *pw = wl_container_of(listener, pw, output_destroy);
211 pw->output = NULL;
212 wl_list_remove(&pw->output_destroy.link);
213 wl_list_init(&pw->output_destroy.link);
214}
215
216void root_record_workspace_pid(pid_t pid) {
217 wlr_log(WLR_DEBUG, "Recording workspace for process %d", pid);
218 if (!pid_workspaces.prev && !pid_workspaces.next) {
219 wl_list_init(&pid_workspaces);
220 }
221
222 struct sway_seat *seat = input_manager_current_seat(input_manager);
223 struct sway_container *ws =
224 seat_get_focus_inactive(seat, &root_container);
225 if (ws && ws->type != C_WORKSPACE) {
226 ws = container_parent(ws, C_WORKSPACE);
227 }
228 if (!ws) {
229 wlr_log(WLR_DEBUG, "Bailing out, no workspace");
230 return;
231 }
232 struct sway_container *output = ws->parent;
233 if (!output) {
234 wlr_log(WLR_DEBUG, "Bailing out, no output");
235 return;
236 }
237
238 struct timespec now;
239 clock_gettime(CLOCK_MONOTONIC, &now);
240
241 // Remove expired entries
242 static const int timeout = 60;
243 struct pid_workspace *old, *_old;
244 wl_list_for_each_safe(old, _old, &pid_workspaces, link) {
245 if (now.tv_sec - old->time_added.tv_sec >= timeout) {
246 wl_list_remove(&old->output_destroy.link);
247 wl_list_remove(&old->link);
248 free(old->workspace);
249 free(old);
250 }
251 }
252
253 struct pid_workspace *pw = calloc(1, sizeof(struct pid_workspace));
254 pw->workspace = strdup(ws->name);
255 pw->output = output;
256 pw->pid = pid;
257 memcpy(&pw->time_added, &now, sizeof(struct timespec));
258 pw->output_destroy.notify = pw_handle_output_destroy;
259 wl_signal_add(&output->sway_output->wlr_output->events.destroy,
260 &pw->output_destroy);
261 wl_list_insert(&pid_workspaces, &pw->link);
262}
diff --git a/sway/tree/view.c b/sway/tree/view.c
index 78baa705..9465b3a1 100644
--- a/sway/tree/view.c
+++ b/sway/tree/view.c
@@ -490,7 +490,7 @@ static struct sway_container *select_workspace(struct sway_view *view) {
490 wl_resource_get_client(view->surface->resource); 490 wl_resource_get_client(view->surface->resource);
491 wl_client_get_credentials(client, &pid, NULL, NULL); 491 wl_client_get_credentials(client, &pid, NULL, NULL);
492#endif 492#endif
493 ws = workspace_for_pid(pid); 493 ws = root_workspace_for_pid(pid);
494 if (ws) { 494 if (ws) {
495 return ws; 495 return ws;
496 } 496 }
diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c
index 687d9c95..cc225e79 100644
--- a/sway/tree/workspace.c
+++ b/sway/tree/workspace.c
@@ -539,116 +539,3 @@ void workspace_detect_urgent(struct sway_container *workspace) {
539 container_damage_whole(workspace); 539 container_damage_whole(workspace);
540 } 540 }
541} 541}
542
543struct pid_workspace {
544 pid_t pid;
545 char *workspace;
546 struct timespec time_added;
547
548 struct sway_container *output;
549 struct wl_listener output_destroy;
550
551 struct wl_list link;
552};
553
554static struct wl_list pid_workspaces;
555
556struct sway_container *workspace_for_pid(pid_t pid) {
557 if (!pid_workspaces.prev && !pid_workspaces.next) {
558 wl_list_init(&pid_workspaces);
559 return NULL;
560 }
561
562 struct sway_container *ws = NULL;
563 struct pid_workspace *pw = NULL;
564
565 wlr_log(WLR_DEBUG, "Looking up workspace for pid %d", pid);
566
567 do {
568 struct pid_workspace *_pw = NULL;
569 wl_list_for_each(_pw, &pid_workspaces, link) {
570 if (pid == _pw->pid) {
571 pw = _pw;
572 wlr_log(WLR_DEBUG,
573 "found pid_workspace for pid %d, workspace %s",
574 pid, pw->workspace);
575 goto found;
576 }
577 }
578 pid = get_parent_pid(pid);
579 } while (pid > 1);
580found:
581
582 if (pw && pw->workspace) {
583 ws = workspace_by_name(pw->workspace);
584
585 if (!ws) {
586 wlr_log(WLR_DEBUG,
587 "Creating workspace %s for pid %d because it disappeared",
588 pw->workspace, pid);
589 ws = workspace_create(pw->output, pw->workspace);
590 }
591
592 wl_list_remove(&pw->output_destroy.link);
593 wl_list_remove(&pw->link);
594 free(pw->workspace);
595 free(pw);
596 }
597
598 return ws;
599}
600
601static void pw_handle_output_destroy(struct wl_listener *listener, void *data) {
602 struct pid_workspace *pw = wl_container_of(listener, pw, output_destroy);
603 pw->output = NULL;
604 wl_list_remove(&pw->output_destroy.link);
605 wl_list_init(&pw->output_destroy.link);
606}
607
608void workspace_record_pid(pid_t pid) {
609 wlr_log(WLR_DEBUG, "Recording workspace for process %d", pid);
610 if (!pid_workspaces.prev && !pid_workspaces.next) {
611 wl_list_init(&pid_workspaces);
612 }
613
614 struct sway_seat *seat = input_manager_current_seat(input_manager);
615 struct sway_container *ws =
616 seat_get_focus_inactive(seat, &root_container);
617 if (ws && ws->type != C_WORKSPACE) {
618 ws = container_parent(ws, C_WORKSPACE);
619 }
620 if (!ws) {
621 wlr_log(WLR_DEBUG, "Bailing out, no workspace");
622 return;
623 }
624 struct sway_container *output = ws->parent;
625 if (!output) {
626 wlr_log(WLR_DEBUG, "Bailing out, no output");
627 return;
628 }
629
630 struct timespec now;
631 clock_gettime(CLOCK_MONOTONIC, &now);
632
633 // Remove expired entries
634 static const int timeout = 60;
635 struct pid_workspace *old, *_old;
636 wl_list_for_each_safe(old, _old, &pid_workspaces, link) {
637 if (now.tv_sec - old->time_added.tv_sec >= timeout) {
638 wl_list_remove(&old->output_destroy.link);
639 wl_list_remove(&old->link);
640 free(old->workspace);
641 free(old);
642 }
643 }
644
645 struct pid_workspace *pw = calloc(1, sizeof(struct pid_workspace));
646 pw->workspace = strdup(ws->name);
647 pw->output = output;
648 pw->pid = pid;
649 memcpy(&pw->time_added, &now, sizeof(struct timespec));
650 pw->output_destroy.notify = pw_handle_output_destroy;
651 wl_signal_add(&output->sway_output->wlr_output->events.destroy,
652 &pw->output_destroy);
653 wl_list_insert(&pid_workspaces, &pw->link);
654}