aboutsummaryrefslogtreecommitdiffstats
path: root/sway/tree/root.c
blob: b42371dec8ee4b57f9f34537f32ca3e0cb924ad8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#define _POSIX_C_SOURCE 200809L
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <wlr/types/wlr_output_layout.h>
#include "sway/desktop/transaction.h"
#include "sway/input/seat.h"
#include "sway/output.h"
#include "sway/tree/arrange.h"
#include "sway/tree/container.h"
#include "sway/tree/root.h"
#include "sway/tree/workspace.h"
#include "list.h"
#include "log.h"
#include "util.h"

struct sway_container root_container;

static void output_layout_handle_change(struct wl_listener *listener,
		void *data) {
	arrange_windows(&root_container);
	transaction_commit_dirty();
}

void root_create(void) {
	root_container.id = 0; // normally assigned in new_swayc()
	root_container.type = C_ROOT;
	root_container.layout = L_NONE;
	root_container.name = strdup("root");
	root_container.children = create_list();
	root_container.current.children = create_list();
	wl_signal_init(&root_container.events.destroy);

	root_container.sway_root = calloc(1, sizeof(*root_container.sway_root));
	root_container.sway_root->output_layout = wlr_output_layout_create();
	wl_list_init(&root_container.sway_root->all_outputs);
#ifdef HAVE_XWAYLAND
	wl_list_init(&root_container.sway_root->xwayland_unmanaged);
#endif
	wl_list_init(&root_container.sway_root->drag_icons);
	wl_signal_init(&root_container.sway_root->events.new_container);
	root_container.sway_root->scratchpad = create_list();
	root_container.sway_root->saved_workspaces = create_list();

	root_container.sway_root->output_layout_change.notify =
		output_layout_handle_change;
	wl_signal_add(&root_container.sway_root->output_layout->events.change,
		&root_container.sway_root->output_layout_change);
}

void root_destroy(void) {
	// sway_root
	wl_list_remove(&root_container.sway_root->output_layout_change.link);
	list_free(root_container.sway_root->scratchpad);
	list_free(root_container.sway_root->saved_workspaces);
	wlr_output_layout_destroy(root_container.sway_root->output_layout);
	free(root_container.sway_root);

	// root_container
	list_free(root_container.children);
	list_free(root_container.current.children);
	free(root_container.name);

	memset(&root_container, 0, sizeof(root_container));
}

void root_scratchpad_add_container(struct sway_container *con) {
	if (!sway_assert(!con->scratchpad, "Container is already in scratchpad")) {
		return;
	}
	con->scratchpad = true;
	list_add(root_container.sway_root->scratchpad, con);

	struct sway_container *parent = con->parent;
	container_set_floating(con, true);
	container_remove_child(con);
	arrange_windows(parent);

	struct sway_seat *seat = input_manager_current_seat(input_manager);
	seat_set_focus(seat, seat_get_focus_inactive(seat, parent));
}

void root_scratchpad_remove_container(struct sway_container *con) {
	if (!sway_assert(con->scratchpad, "Container is not in scratchpad")) {
		return;
	}
	con->scratchpad = false;
	int index = list_find(root_container.sway_root->scratchpad, con);
	if (index != -1) {
		list_del(root_container.sway_root->scratchpad, index);
	}
}

void root_scratchpad_show(struct sway_container *con) {
	struct sway_seat *seat = input_manager_current_seat(input_manager);
	struct sway_container *ws = seat_get_focus(seat);
	if (ws->type != C_WORKSPACE) {
		ws = container_parent(ws, C_WORKSPACE);
	}

    // If the current con or any of its parents are in fullscreen mode, we
    // first need to disable it before showing the scratchpad con.
	if (ws->sway_workspace->fullscreen) {
		container_set_fullscreen(ws->sway_workspace->fullscreen, false);
	}

	// Show the container
	if (con->parent) {
		container_remove_child(con);
	}
	workspace_add_floating(ws, con);

	// Make sure the container's center point overlaps this workspace
	double center_lx = con->x + con->width / 2;
	double center_ly = con->y + con->height / 2;

	struct wlr_box workspace_box;
	container_get_box(ws, &workspace_box);
	if (!wlr_box_contains_point(&workspace_box, center_lx, center_ly)) {
		// Maybe resize it
		if (con->width > ws->width || con->height > ws->height) {
			container_init_floating(con);
		}

		// Center it
		double new_lx = ws->x + (ws->width - con->width) / 2;
		double new_ly = ws->y + (ws->height - con->height) / 2;
		container_floating_move_to(con, new_lx, new_ly);
	}

	arrange_windows(ws);
	seat_set_focus(seat, seat_get_focus_inactive(seat, con));

	container_set_dirty(con->parent);
}

void root_scratchpad_hide(struct sway_container *con) {
	struct sway_seat *seat = input_manager_current_seat(input_manager);
	struct sway_container *focus = seat_get_focus(seat);
	struct sway_container *ws = container_parent(con, C_WORKSPACE);

	container_remove_child(con);
	arrange_windows(ws);
	if (con == focus) {
		seat_set_focus(seat, seat_get_focus_inactive(seat, ws));
	}
	list_move_to_end(root_container.sway_root->scratchpad, con);
}

struct pid_workspace {
	pid_t pid;
	char *workspace;
	struct timespec time_added;

	struct sway_container *output;
	struct wl_listener output_destroy;

	struct wl_list link;
};

static struct wl_list pid_workspaces;

struct sway_container *root_workspace_for_pid(pid_t pid) {
	if (!pid_workspaces.prev && !pid_workspaces.next) {
		wl_list_init(&pid_workspaces);
		return NULL;
	}

	struct sway_container *ws = NULL;
	struct pid_workspace *pw = NULL;

	wlr_log(WLR_DEBUG, "Looking up workspace for pid %d", pid);

	do {
		struct pid_workspace *_pw = NULL;
		wl_list_for_each(_pw, &pid_workspaces, link) {
			if (pid == _pw->pid) {
				pw = _pw;
				wlr_log(WLR_DEBUG,
						"found pid_workspace for pid %d, workspace %s",
						pid, pw->workspace);
				goto found;
			}
		}
		pid = get_parent_pid(pid);
	} while (pid > 1);
found:

	if (pw && pw->workspace) {
		ws = workspace_by_name(pw->workspace);

		if (!ws) {
			wlr_log(WLR_DEBUG,
					"Creating workspace %s for pid %d because it disappeared",
					pw->workspace, pid);
			ws = workspace_create(pw->output, pw->workspace);
		}

		wl_list_remove(&pw->output_destroy.link);
		wl_list_remove(&pw->link);
		free(pw->workspace);
		free(pw);
	}

	return ws;
}

static void pw_handle_output_destroy(struct wl_listener *listener, void *data) {
	struct pid_workspace *pw = wl_container_of(listener, pw, output_destroy);
	pw->output = NULL;
	wl_list_remove(&pw->output_destroy.link);
	wl_list_init(&pw->output_destroy.link);
}

void root_record_workspace_pid(pid_t pid) {
	wlr_log(WLR_DEBUG, "Recording workspace for process %d", pid);
	if (!pid_workspaces.prev && !pid_workspaces.next) {
		wl_list_init(&pid_workspaces);
	}

	struct sway_seat *seat = input_manager_current_seat(input_manager);
	struct sway_container *ws =
		seat_get_focus_inactive(seat, &root_container);
	if (ws && ws->type != C_WORKSPACE) {
		ws = container_parent(ws, C_WORKSPACE);
	}
	if (!ws) {
		wlr_log(WLR_DEBUG, "Bailing out, no workspace");
		return;
	}
	struct sway_container *output = ws->parent;
	if (!output) {
		wlr_log(WLR_DEBUG, "Bailing out, no output");
		return;
	}

	struct timespec now;
	clock_gettime(CLOCK_MONOTONIC, &now);

	// Remove expired entries
	static const int timeout = 60;
	struct pid_workspace *old, *_old;
	wl_list_for_each_safe(old, _old, &pid_workspaces, link) {
		if (now.tv_sec - old->time_added.tv_sec >= timeout) {
			wl_list_remove(&old->output_destroy.link);
			wl_list_remove(&old->link);
			free(old->workspace);
			free(old);
		}
	}

	struct pid_workspace *pw = calloc(1, sizeof(struct pid_workspace));
	pw->workspace = strdup(ws->name);
	pw->output = output;
	pw->pid = pid;
	memcpy(&pw->time_added, &now, sizeof(struct timespec));
	pw->output_destroy.notify = pw_handle_output_destroy;
	wl_signal_add(&output->sway_output->wlr_output->events.destroy,
			&pw->output_destroy);
	wl_list_insert(&pid_workspaces, &pw->link);
}

void root_for_each_workspace(void (*f)(struct sway_container *con, void *data),
		void *data) {
	for (int i = 0; i < root_container.children->length; ++i) {
		struct sway_container *output = root_container.children->items[i];
		output_for_each_workspace(output, f, data);
	}
}

void root_for_each_container(void (*f)(struct sway_container *con, void *data),
		void *data) {
	for (int i = 0; i < root_container.children->length; ++i) {
		struct sway_container *output = root_container.children->items[i];
		output_for_each_container(output, f, data);
	}

	// Scratchpad
	for (int i = 0; i < root_container.sway_root->scratchpad->length; ++i) {
		struct sway_container *container =
			root_container.sway_root->scratchpad->items[i];
		// If the container has a parent then it's visible on a workspace
		// and will have been iterated in the previous for loop. So we only
		// iterate the hidden scratchpad containers here.
		if (!container->parent) {
			f(container, data);
			container_for_each_child(container, f, data);
		}
	}
}

struct sway_container *root_find_output(
		bool (*test)(struct sway_container *con, void *data), void *data) {
	for (int i = 0; i < root_container.children->length; ++i) {
		struct sway_container *output = root_container.children->items[i];
		if (test(output, data)) {
			return output;
		}
	}
	return NULL;
}

struct sway_container *root_find_workspace(
		bool (*test)(struct sway_container *con, void *data), void *data) {
	struct sway_container *result = NULL;
	for (int i = 0; i < root_container.children->length; ++i) {
		struct sway_container *output = root_container.children->items[i];
		if ((result = output_find_workspace(output, test, data))) {
			return result;
		}
	}
	return NULL;
}

struct sway_container *root_find_container(
		bool (*test)(struct sway_container *con, void *data), void *data) {
	struct sway_container *result = NULL;
	for (int i = 0; i < root_container.children->length; ++i) {
		struct sway_container *output = root_container.children->items[i];
		if ((result = output_find_container(output, test, data))) {
			return result;
		}
	}

	// Scratchpad
	for (int i = 0; i < root_container.sway_root->scratchpad->length; ++i) {
		struct sway_container *container =
			root_container.sway_root->scratchpad->items[i];
		if (!container->parent) {
			if (test(container, data)) {
				return container;
			}
			if ((result = container_find_child(container, test, data))) {
				return result;
			}
		}
	}
	return NULL;
}