aboutsummaryrefslogtreecommitdiffstats
path: root/sway/tree/root.c
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 /sway/tree/root.c
parentSeparate root-related code (diff)
downloadsway-30e7e0f7c7d3d3ce2851f2e70842d735343fb402.tar.gz
sway-30e7e0f7c7d3d3ce2851f2e70842d735343fb402.tar.zst
sway-30e7e0f7c7d3d3ce2851f2e70842d735343fb402.zip
Move workspace pid code to root.c
Diffstat (limited to 'sway/tree/root.c')
-rw-r--r--sway/tree/root.c115
1 files changed, 115 insertions, 0 deletions
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}