summaryrefslogtreecommitdiffstats
path: root/sway/tree/output.c
blob: 1ab8bed210ded8e8339e8a3c017598a97b266935 (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
#define _POSIX_C_SOURCE 200809L
#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_workspace(struct sway_container *ws, void *output) {
	if (ws->parent == output) {
		return;
	}

	struct sway_container *highest = workspace_output_get_highest_available(
			ws, NULL);
	if (!highest) {
		return;
	}

	if (highest == output) {
		struct sway_container *other = container_remove_child(ws);
		container_add_child(output, ws);
		ipc_event_workspace(ws, NULL, "move");

		container_sort_workspaces(output);
		arrange_output(output);
		arrange_output(other);
	}
}

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(L_DEBUG, "Matched output config for %s", name);
			oc = cur;
		}
		if (strcasecmp("*", cur->name) == 0) {
			wlr_log(L_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) {
		container_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;

	container_descendants(&root_container, C_WORKSPACE, restore_workspace,
			output);

	if (!output->children->length) {
		// Create workspace
		char *ws_name = workspace_next_name(output->name);
		wlr_log(L_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;
}