aboutsummaryrefslogtreecommitdiff
path: root/backend/multi/backend.c
blob: 19839629f80bb1fd45d43e47ca32eb4d73faccef (plain)
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
#include <stdbool.h>
#include <stdlib.h>
#include <wlr/backend/interface.h>
#include <wlr/backend/udev.h>
#include <wlr/backend/session.h>
#include <wlr/util/log.h>
#include "backend/multi.h"

struct subbackend_state {
	struct wlr_backend *backend;
	struct wlr_backend *container;
	struct wl_listener input_add;
	struct wl_listener input_remove;
	struct wl_listener output_add;
	struct wl_listener output_remove;
};

static bool multi_backend_init(struct wlr_backend_state *state) {
	for (size_t i = 0; i < state->backends->length; ++i) {
		struct subbackend_state *sub = state->backends->items[i];
		if (!wlr_backend_init(sub->backend)) {
			wlr_log(L_ERROR, "Failed to initialize backend %zd", i);
			return false;
		}
	}
	return true;
}

static void multi_backend_destroy(struct wlr_backend_state *state) {
	for (size_t i = 0; i < state->backends->length; ++i) {
		struct subbackend_state *sub = state->backends->items[i];
		wlr_backend_destroy(sub->backend);
		free(sub);
	}
	list_free(state->backends);
	wlr_session_finish(state->session);
	wlr_udev_destroy(state->udev);
	free(state);
}

struct wlr_backend_impl backend_impl = {
	.init = multi_backend_init,
	.destroy = multi_backend_destroy
};

struct wlr_backend *wlr_multi_backend_create(struct wlr_session *session,
		struct wlr_udev *udev) {
	struct wlr_backend_state *state =
		calloc(1, sizeof(struct wlr_backend_state));
	if (!state) {
		wlr_log(L_ERROR, "Backend allocation failed");
		return NULL;
	}

	state->backends = list_create();
	if (!state->backends) {
		free(state);
		wlr_log(L_ERROR, "Backend allocation failed");
		return NULL;
	}

	struct wlr_backend *backend = wlr_backend_create(&backend_impl, state);
	state->backend = backend;
	state->session = session;
	state->udev = udev;
	return backend;
}

bool wlr_backend_is_multi(struct wlr_backend *b) {
	return b->impl == &backend_impl;
}

static void input_add_reemit(struct wl_listener *listener, void *data) {
	struct subbackend_state *state = wl_container_of(listener,
			state, input_add);
	wl_signal_emit(&state->container->events.input_add, data);
}

static void input_remove_reemit(struct wl_listener *listener, void *data) {
	struct subbackend_state *state = wl_container_of(listener,
			state, input_remove);
	wl_signal_emit(&state->container->events.input_remove, data);
}

static void output_add_reemit(struct wl_listener *listener, void *data) {
	struct subbackend_state *state = wl_container_of(listener,
			state, output_add);
	wl_signal_emit(&state->container->events.output_add, data);
}

static void output_remove_reemit(struct wl_listener *listener, void *data) {
	struct subbackend_state *state = wl_container_of(listener,
			state, output_remove);
	wl_signal_emit(&state->container->events.output_remove, data);
}

void wlr_multi_backend_add(struct wlr_backend *multi,
		struct wlr_backend *backend) {
	struct subbackend_state *sub = calloc(1, sizeof(struct subbackend_state));
	sub->backend = backend;
	sub->container = multi;

	sub->input_add.notify = input_add_reemit;
	sub->input_remove.notify = input_remove_reemit;
	sub->output_add.notify = output_add_reemit;
	sub->output_remove.notify = output_remove_reemit;

	wl_list_init(&sub->input_add.link);
	wl_list_init(&sub->input_remove.link);
	wl_list_init(&sub->output_add.link);
	wl_list_init(&sub->output_remove.link);

	wl_signal_add(&backend->events.input_add, &sub->input_add);
	wl_signal_add(&backend->events.input_remove, &sub->input_remove);
	wl_signal_add(&backend->events.output_add, &sub->output_add);
	wl_signal_add(&backend->events.output_remove, &sub->output_remove);

	list_add(multi->state->backends, sub);
}

struct wlr_session *wlr_multi_get_session(struct wlr_backend *base) {
	if (base->impl != &backend_impl)
		return NULL;

	struct wlr_backend_state *multi = base->state;
	return multi->session;
}