summaryrefslogtreecommitdiffstats
path: root/sway/tree/output.c
blob: 6601220bfaf18b64df42418e56be8c9b1146b7d5 (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
#define _POSIX_C_SOURCE 200809L
#include <ctype.h>
#include <string.h>
#include <strings.h>
#include "sway/ipc-server.h"
#include "sway/output.h"
#include "sway/tree/arrange.h"
#include "sway/tree/output.h"
#include "sway/tree/workspace.h"
#include "log.h"

static void restore_workspaces(struct sway_container *output) {
	// Workspace output priority
	for (int i = 0; i < root_container.children->length; i++) {
		struct sway_container *other = root_container.children->items[i];
		if (other == output) {
			continue;
		}

		for (int j = 0; j < other->children->length; j++) {
			struct sway_container *ws = other->children->items[j];
			struct sway_container *highest =
				workspace_output_get_highest_available(ws, NULL);
			if (highest == output) {
				container_remove_child(ws);
				container_add_child(output, ws);
				ipc_event_workspace(NULL, ws, "move");
				j--;
			}
		}
	}

	// Saved workspaces
	list_t *saved = root_container.sway_root->saved_workspaces;
	for (int i = 0; i < saved->length; ++i) {
		struct sway_container *ws = saved->items[i];
		container_add_child(output, ws);
		ipc_event_workspace(NULL, ws, "move");
	}
	saved->length = 0;

	output_sort_workspaces(output);
}

struct sway_container *output_create(
		struct sway_output *sway_output) {
	const char *name = sway_output->wlr_output->name;
	char identifier[128];
	output_get_identifier(identifier, sizeof(identifier), sway_output);

	struct output_config *oc = NULL, *all = NULL;
	for (int i = 0; i < config->output_configs->length; ++i) {
		struct output_config *cur = config->output_configs->items[i];

		if (strcasecmp(name, cur->name) == 0 ||
				strcasecmp(identifier, cur->name) == 0) {
			wlr_log(WLR_DEBUG, "Matched output config for %s", name);
			oc = cur;
		}
		if (strcasecmp("*", cur->name) == 0) {
			wlr_log(WLR_DEBUG, "Matched wildcard output config for %s", name);
			all = cur;
		}

		if (oc && all) {
			break;
		}
	}
	if (!oc) {
		oc = all;
	}

	if (oc && !oc->enabled) {
		return NULL;
	}

	struct sway_container *output = container_create(C_OUTPUT);
	output->sway_output = sway_output;
	output->name = strdup(name);
	if (output->name == NULL) {
		output_begin_destroy(output);
		return NULL;
	}

	apply_output_config(oc, output);
	container_add_child(&root_container, output);
	load_swaybars();

	struct wlr_box size;
	wlr_output_effective_resolution(sway_output->wlr_output, &size.width,
		&size.height);
	output->width = size.width;
	output->height = size.height;

	restore_workspaces(output);

	if (!output->children->length) {
		// Create workspace
		char *ws_name = workspace_next_name(output->name);
		wlr_log(WLR_DEBUG, "Creating default workspace %s", ws_name);
		struct sway_container *ws = workspace_create(output, ws_name);
		// Set each seat's focus if not already set
		struct sway_seat *seat = NULL;
		wl_list_for_each(seat, &input_manager->seats, link) {
			if (!seat->has_focus) {
				seat_set_focus(seat, ws);
			}
		}
		free(ws_name);
	}

	container_create_notify(output);
	return output;
}

static void output_evacuate(struct sway_container *output) {
	if (!output->children->length) {
		return;
	}
	struct sway_container *fallback_output = NULL;
	if (root_container.children->length > 1) {
		fallback_output = root_container.children->items[0];
		if (fallback_output == output) {
			fallback_output = root_container.children->items[1];
		}
	}

	while (output->children->length) {
		struct sway_container *workspace = output->children->items[0];

		container_remove_child(workspace);

		if (workspace_is_empty(workspace)) {
			workspace_begin_destroy(workspace);
			continue;
		}

		struct sway_container *new_output =
			workspace_output_get_highest_available(workspace, output);
		if (!new_output) {
			new_output = fallback_output;
		}

		if (new_output) {
			workspace_output_add_priority(workspace, new_output);
			container_add_child(new_output, workspace);
			output_sort_workspaces(new_output);
			ipc_event_workspace(NULL, workspace, "move");
		} else {
			list_add(root_container.sway_root->saved_workspaces, workspace);
		}
	}
}

void output_destroy(struct sway_container *output) {
	if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) {
		return;
	}
	if (!sway_assert(output->destroying,
				"Tried to free output which wasn't marked as destroying")) {
		return;
	}
	if (!sway_assert(output->ntxnrefs == 0, "Tried to free output "
				"which is still referenced by transactions")) {
		return;
	}
	free(output->name);
	free(output->formatted_title);
	wlr_texture_destroy(output->title_focused);
	wlr_texture_destroy(output->title_focused_inactive);
	wlr_texture_destroy(output->title_unfocused);
	wlr_texture_destroy(output->title_urgent);
	list_free(output->children);
	list_free(output->current.children);
	list_free(output->outputs);
	free(output);

	// NOTE: We don't actually destroy the sway_output here
}

static void untrack_output(struct sway_container *con, void *data) {
	struct sway_output *output = data;
	int index = list_find(con->outputs, output);
	if (index != -1) {
		list_del(con->outputs, index);
	}
}

void output_begin_destroy(struct sway_container *output) {
	if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) {
		return;
	}
	wlr_log(WLR_DEBUG, "OUTPUT: Destroying output '%s'", output->name);
	wl_signal_emit(&output->events.destroy, output);

	output_evacuate(output);

	output->destroying = true;
	container_set_dirty(output);

	root_for_each_container(untrack_output, output->sway_output);

	wl_list_remove(&output->sway_output->mode.link);
	wl_list_remove(&output->sway_output->transform.link);
	wl_list_remove(&output->sway_output->scale.link);
	wl_list_remove(&output->sway_output->damage_destroy.link);
	wl_list_remove(&output->sway_output->damage_frame.link);

	output->sway_output->swayc = NULL;
	output->sway_output = NULL;

	if (output->parent) {
		container_remove_child(output);
	}
}

struct sway_container *output_from_wlr_output(struct wlr_output *output) {
	if (output == NULL) {
		return NULL;
	}
	for (int i = 0; i < root_container.children->length; ++i) {
		struct sway_container *o = root_container.children->items[i];
		if (o->type == C_OUTPUT && o->sway_output->wlr_output == output) {
			return o;
		}
	}
	return NULL;
}

void output_for_each_workspace(struct sway_container *output,
		void (*f)(struct sway_container *con, void *data), void *data) {
	if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) {
		return;
	}
	for (int i = 0; i < output->children->length; ++i) {
		struct sway_container *workspace = output->children->items[i];
		f(workspace, data);
	}
}

void output_for_each_container(struct sway_container *output,
		void (*f)(struct sway_container *con, void *data), void *data) {
	if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) {
		return;
	}
	for (int i = 0; i < output->children->length; ++i) {
		struct sway_container *workspace = output->children->items[i];
		workspace_for_each_container(workspace, f, data);
	}
}

struct sway_container *output_find_workspace(struct sway_container *output,
		bool (*test)(struct sway_container *con, void *data), void *data) {
	if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) {
		return NULL;
	}
	for (int i = 0; i < output->children->length; ++i) {
		struct sway_container *workspace = output->children->items[i];
		if (test(workspace, data)) {
			return workspace;
		}
	}
	return NULL;
}

struct sway_container *output_find_container(struct sway_container *output,
		bool (*test)(struct sway_container *con, void *data), void *data) {
	if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) {
		return NULL;
	}
	struct sway_container *result = NULL;
	for (int i = 0; i < output->children->length; ++i) {
		struct sway_container *workspace = output->children->items[i];
		if ((result = workspace_find_container(workspace, test, data))) {
			return result;
		}
	}
	return NULL;
}

static int sort_workspace_cmp_qsort(const void *_a, const void *_b) {
	struct sway_container *a = *(void **)_a;
	struct sway_container *b = *(void **)_b;

	if (isdigit(a->name[0]) && isdigit(b->name[0])) {
		int a_num = strtol(a->name, NULL, 10);
		int b_num = strtol(b->name, NULL, 10);
		return (a_num < b_num) ? -1 : (a_num > b_num);
	} else if (isdigit(a->name[0])) {
		return -1;
	} else if (isdigit(b->name[0])) {
		return 1;
	}
	return 0;
}

void output_sort_workspaces(struct sway_container *output) {
	list_stable_sort(output->children, sort_workspace_cmp_qsort);
}