2017-08-13 21:05:57 +00:00
|
|
|
#include <assert.h>
|
2017-06-13 02:22:40 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <wlr/backend/interface.h>
|
2017-07-11 07:18:34 +00:00
|
|
|
#include <wlr/backend/session.h>
|
2017-06-21 16:10:07 +00:00
|
|
|
#include <wlr/util/log.h>
|
2017-06-13 02:22:40 +00:00
|
|
|
#include "backend/multi.h"
|
2017-12-19 23:25:46 +00:00
|
|
|
#include "backend/drm/drm.h"
|
2017-06-13 02:22:40 +00:00
|
|
|
|
|
|
|
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;
|
2017-12-19 23:33:26 +00:00
|
|
|
struct wl_listener backend_destroy;
|
2017-10-14 19:55:45 +00:00
|
|
|
struct wl_list link;
|
2017-06-13 02:22:40 +00:00
|
|
|
};
|
|
|
|
|
2017-12-19 17:28:47 +00:00
|
|
|
static bool multi_backend_start(struct wlr_backend *wlr_backend) {
|
|
|
|
struct wlr_multi_backend *backend = (struct wlr_multi_backend *)wlr_backend;
|
2017-10-14 19:55:45 +00:00
|
|
|
struct subbackend_state *sub;
|
|
|
|
wl_list_for_each(sub, &backend->backends, link) {
|
2017-08-13 13:59:14 +00:00
|
|
|
if (!wlr_backend_start(sub->backend)) {
|
2017-10-14 19:55:45 +00:00
|
|
|
wlr_log(L_ERROR, "Failed to initialize backend.");
|
2017-06-13 02:22:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-19 23:33:26 +00:00
|
|
|
static void subbackend_state_destroy(struct subbackend_state *sub) {
|
|
|
|
wl_list_remove(&sub->input_add.link);
|
|
|
|
wl_list_remove(&sub->input_remove.link);
|
|
|
|
wl_list_remove(&sub->output_add.link);
|
|
|
|
wl_list_remove(&sub->output_remove.link);
|
|
|
|
wl_list_remove(&sub->backend_destroy.link);
|
|
|
|
wl_list_remove(&sub->link);
|
|
|
|
free(sub);
|
|
|
|
}
|
|
|
|
|
2017-12-19 17:28:47 +00:00
|
|
|
static void multi_backend_destroy(struct wlr_backend *wlr_backend) {
|
|
|
|
struct wlr_multi_backend *backend = (struct wlr_multi_backend *)wlr_backend;
|
2017-11-01 19:59:28 +00:00
|
|
|
struct subbackend_state *sub, *next;
|
|
|
|
wl_list_for_each_safe(sub, next, &backend->backends, link) {
|
2017-12-19 23:33:26 +00:00
|
|
|
// XXX do we really want to take ownership over added backends?
|
2017-06-13 02:22:40 +00:00
|
|
|
wlr_backend_destroy(sub->backend);
|
|
|
|
}
|
2017-08-12 15:43:36 +00:00
|
|
|
free(backend);
|
2017-06-13 02:22:40 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 17:28:47 +00:00
|
|
|
static struct wlr_egl *multi_backend_get_egl(struct wlr_backend *wlr_backend) {
|
|
|
|
struct wlr_multi_backend *backend = (struct wlr_multi_backend *)wlr_backend;
|
2017-10-14 19:55:45 +00:00
|
|
|
struct subbackend_state *sub;
|
|
|
|
wl_list_for_each(sub, &backend->backends, link) {
|
2017-08-11 02:15:37 +00:00
|
|
|
struct wlr_egl *egl = wlr_backend_get_egl(sub->backend);
|
|
|
|
if (egl) {
|
|
|
|
return egl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-13 02:22:40 +00:00
|
|
|
struct wlr_backend_impl backend_impl = {
|
2017-08-13 13:59:14 +00:00
|
|
|
.start = multi_backend_start,
|
2017-08-11 02:15:37 +00:00
|
|
|
.destroy = multi_backend_destroy,
|
2017-12-19 17:28:47 +00:00
|
|
|
.get_egl = multi_backend_get_egl,
|
2017-06-13 02:22:40 +00:00
|
|
|
};
|
|
|
|
|
2017-12-19 23:25:46 +00:00
|
|
|
struct wlr_backend *wlr_multi_backend_create(struct wl_display *display) {
|
2017-08-12 15:43:36 +00:00
|
|
|
struct wlr_multi_backend *backend =
|
|
|
|
calloc(1, sizeof(struct wlr_multi_backend));
|
|
|
|
if (!backend) {
|
2017-06-13 02:22:40 +00:00
|
|
|
wlr_log(L_ERROR, "Backend allocation failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-07-11 06:51:19 +00:00
|
|
|
|
2017-10-14 19:55:45 +00:00
|
|
|
wl_list_init(&backend->backends);
|
2017-08-13 13:59:14 +00:00
|
|
|
wlr_backend_init(&backend->backend, &backend_impl);
|
2017-08-12 15:43:36 +00:00
|
|
|
|
2017-12-20 10:54:41 +00:00
|
|
|
wl_signal_init(&backend->events.backend_add);
|
|
|
|
wl_signal_init(&backend->events.backend_remove);
|
|
|
|
|
2017-08-12 15:43:36 +00:00
|
|
|
return &backend->backend;
|
2017-06-13 02:22:40 +00:00
|
|
|
}
|
|
|
|
|
2017-08-06 03:08:35 +00:00
|
|
|
bool wlr_backend_is_multi(struct wlr_backend *b) {
|
|
|
|
return b->impl == &backend_impl;
|
|
|
|
}
|
|
|
|
|
2017-06-13 02:22:40 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-12-19 23:33:26 +00:00
|
|
|
static void handle_subbackend_destroy(struct wl_listener *listener,
|
|
|
|
void *data) {
|
|
|
|
struct subbackend_state *state = wl_container_of(listener,
|
|
|
|
state, backend_destroy);
|
|
|
|
subbackend_state_destroy(state);
|
|
|
|
}
|
|
|
|
|
2017-12-19 23:49:00 +00:00
|
|
|
static struct subbackend_state *multi_backend_get_subbackend(struct wlr_multi_backend *multi,
|
|
|
|
struct wlr_backend *backend) {
|
|
|
|
struct subbackend_state *sub = NULL;
|
|
|
|
wl_list_for_each(sub, &multi->backends, link) {
|
|
|
|
if (sub->backend == backend) {
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-08-12 15:43:36 +00:00
|
|
|
void wlr_multi_backend_add(struct wlr_backend *_multi,
|
2017-06-13 02:22:40 +00:00
|
|
|
struct wlr_backend *backend) {
|
2017-08-14 12:58:27 +00:00
|
|
|
assert(wlr_backend_is_multi(_multi));
|
2017-08-12 15:43:36 +00:00
|
|
|
struct wlr_multi_backend *multi = (struct wlr_multi_backend *)_multi;
|
2017-12-19 23:49:00 +00:00
|
|
|
|
|
|
|
if (multi_backend_get_subbackend(multi, backend)) {
|
|
|
|
// already added
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-15 05:56:47 +00:00
|
|
|
struct subbackend_state *sub;
|
|
|
|
if (!(sub = calloc(1, sizeof(struct subbackend_state)))) {
|
|
|
|
wlr_log(L_ERROR, "Could not add backend: allocation failed");
|
|
|
|
return;
|
|
|
|
}
|
2017-10-14 19:55:45 +00:00
|
|
|
wl_list_insert(&multi->backends, &sub->link);
|
2017-08-16 07:23:21 +00:00
|
|
|
|
2017-06-13 02:22:40 +00:00
|
|
|
sub->backend = backend;
|
2017-08-12 15:43:36 +00:00
|
|
|
sub->container = &multi->backend;
|
2017-06-13 02:22:40 +00:00
|
|
|
|
2017-12-19 23:33:26 +00:00
|
|
|
wl_signal_add(&backend->events.destroy, &sub->backend_destroy);
|
|
|
|
sub->backend_destroy.notify = handle_subbackend_destroy;
|
2017-06-13 02:22:40 +00:00
|
|
|
|
|
|
|
wl_signal_add(&backend->events.input_add, &sub->input_add);
|
2017-12-19 23:33:26 +00:00
|
|
|
sub->input_add.notify = input_add_reemit;
|
|
|
|
|
2017-06-13 02:22:40 +00:00
|
|
|
wl_signal_add(&backend->events.input_remove, &sub->input_remove);
|
2017-12-19 23:33:26 +00:00
|
|
|
sub->input_remove.notify = input_remove_reemit;
|
|
|
|
|
2017-06-13 02:22:40 +00:00
|
|
|
wl_signal_add(&backend->events.output_add, &sub->output_add);
|
2017-12-19 23:33:26 +00:00
|
|
|
sub->output_add.notify = output_add_reemit;
|
|
|
|
|
2017-06-13 02:22:40 +00:00
|
|
|
wl_signal_add(&backend->events.output_remove, &sub->output_remove);
|
2017-12-19 23:33:26 +00:00
|
|
|
sub->output_remove.notify = output_remove_reemit;
|
2017-12-20 10:54:41 +00:00
|
|
|
|
|
|
|
wl_signal_emit(&multi->events.backend_add, backend);
|
2017-06-13 02:22:40 +00:00
|
|
|
}
|
2017-08-06 01:37:49 +00:00
|
|
|
|
2017-12-19 23:49:00 +00:00
|
|
|
void wlr_multi_backend_remove(struct wlr_backend *_multi,
|
|
|
|
struct wlr_backend *backend) {
|
|
|
|
assert(wlr_backend_is_multi(_multi));
|
|
|
|
struct wlr_multi_backend *multi = (struct wlr_multi_backend *)_multi;
|
|
|
|
|
|
|
|
struct subbackend_state *sub =
|
|
|
|
multi_backend_get_subbackend(multi, backend);
|
|
|
|
|
|
|
|
if (sub) {
|
2017-12-20 10:54:41 +00:00
|
|
|
wl_signal_emit(&multi->events.backend_remove, backend);
|
2017-12-19 23:49:00 +00:00
|
|
|
subbackend_state_destroy(sub);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-12 15:43:36 +00:00
|
|
|
struct wlr_session *wlr_multi_get_session(struct wlr_backend *_backend) {
|
2017-08-13 21:05:57 +00:00
|
|
|
assert(wlr_backend_is_multi(_backend));
|
|
|
|
|
2017-08-12 15:43:36 +00:00
|
|
|
struct wlr_multi_backend *backend = (struct wlr_multi_backend *)_backend;
|
2017-12-19 23:25:46 +00:00
|
|
|
struct subbackend_state *sub;
|
|
|
|
wl_list_for_each(sub, &backend->backends, link) {
|
|
|
|
if (wlr_backend_is_drm(sub->backend)) {
|
|
|
|
return wlr_drm_backend_get_session(sub->backend);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
2017-08-06 01:37:49 +00:00
|
|
|
}
|
2017-12-20 10:51:23 +00:00
|
|
|
|
|
|
|
bool wlr_multi_is_empty(struct wlr_backend *_backend) {
|
|
|
|
assert(wlr_backend_is_multi(_backend));
|
|
|
|
struct wlr_multi_backend *backend = (struct wlr_multi_backend *)_backend;
|
|
|
|
return wl_list_length(&backend->backends) < 1;
|
|
|
|
}
|