Merge pull request #978 from emersion/wlr-backend-env

backend: add WLR_BACKEND env variable
This commit is contained in:
Drew DeVault 2018-05-20 16:21:02 -04:00 committed by GitHub
commit 341af97125
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 144 additions and 36 deletions

View file

@ -1,3 +1,4 @@
#define _POSIX_C_SOURCE 200809L
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <libinput.h> #include <libinput.h>
@ -6,6 +7,7 @@
#include <unistd.h> #include <unistd.h>
#include <wayland-server.h> #include <wayland-server.h>
#include <wlr/backend/drm.h> #include <wlr/backend/drm.h>
#include <wlr/backend/headless.h>
#include <wlr/backend/interface.h> #include <wlr/backend/interface.h>
#include <wlr/backend/libinput.h> #include <wlr/backend/libinput.h>
#include <wlr/backend/multi.h> #include <wlr/backend/multi.h>
@ -101,6 +103,76 @@ static struct wlr_backend *attempt_x11_backend(struct wl_display *display,
} }
#endif #endif
static struct wlr_backend *attempt_headless_backend(
struct wl_display *display) {
struct wlr_backend *backend = wlr_headless_backend_create(display);
if (backend == NULL) {
return NULL;
}
size_t outputs = parse_outputs_env("WLR_HEADLESS_OUTPUTS");
for (size_t i = 0; i < outputs; ++i) {
wlr_headless_add_output(backend, 1280, 720);
}
return backend;
}
static struct wlr_backend *attempt_drm_backend(struct wl_display *display,
struct wlr_backend *backend, struct wlr_session *session) {
int gpus[8];
size_t num_gpus = wlr_session_find_gpus(session, 8, gpus);
struct wlr_backend *primary_drm = NULL;
wlr_log(L_INFO, "Found %zu GPUs", num_gpus);
for (size_t i = 0; i < num_gpus; ++i) {
struct wlr_backend *drm = wlr_drm_backend_create(display, session,
gpus[i], primary_drm);
if (!drm) {
wlr_log(L_ERROR, "Failed to open DRM device %d", gpus[i]);
continue;
}
if (!primary_drm) {
primary_drm = drm;
}
wlr_multi_backend_add(backend, drm);
}
return primary_drm;
}
static struct wlr_backend *attempt_backend_by_name(struct wl_display *display,
struct wlr_backend *backend, struct wlr_session **session,
const char *name) {
if (strcmp(name, "wayland") == 0) {
return attempt_wl_backend(display);
#ifdef WLR_HAS_X11_BACKEND
} else if (strcmp(name, "x11") == 0) {
return attempt_x11_backend(display, NULL);
#endif
} else if (strcmp(name, "headless") == 0) {
return attempt_headless_backend(display);
} else if (strcmp(name, "drm") == 0 || strcmp(name, "libinput") == 0) {
// DRM and libinput need a session
*session = wlr_session_create(display);
if (!*session) {
wlr_log(L_ERROR, "failed to start a session");
return NULL;
}
if (strcmp(name, "libinput") == 0) {
return wlr_libinput_backend_create(display, *session);
} else {
return attempt_drm_backend(display, backend, *session);
}
}
wlr_log(L_ERROR, "unrecognized backend '%s'", name);
return NULL;
}
struct wlr_backend *wlr_backend_autocreate(struct wl_display *display) { struct wlr_backend *wlr_backend_autocreate(struct wl_display *display) {
struct wlr_backend *backend = wlr_multi_backend_create(display); struct wlr_backend *backend = wlr_multi_backend_create(display);
if (!backend) { if (!backend) {
@ -108,6 +180,42 @@ struct wlr_backend *wlr_backend_autocreate(struct wl_display *display) {
return NULL; return NULL;
} }
struct wlr_session *session = NULL;
char *names = getenv("WLR_BACKENDS");
if (names) {
names = strdup(names);
if (names == NULL) {
wlr_log(L_ERROR, "allocation failed");
wlr_backend_destroy(backend);
return NULL;
}
char *saveptr;
char *name = strtok_r(names, ",", &saveptr);
while (name != NULL) {
struct wlr_backend *subbackend =
attempt_backend_by_name(display, backend, &session, name);
if (subbackend == NULL) {
wlr_log(L_ERROR, "failed to start backend '%s'", name);
wlr_backend_destroy(backend);
wlr_session_destroy(session);
return NULL;
}
if (!wlr_multi_backend_add(backend, subbackend)) {
wlr_log(L_ERROR, "failed to add backend '%s'", name);
wlr_backend_destroy(backend);
wlr_session_destroy(session);
return NULL;
}
name = strtok_r(NULL, ",", &saveptr);
}
return backend;
}
if (getenv("WAYLAND_DISPLAY") || getenv("_WAYLAND_DISPLAY") || if (getenv("WAYLAND_DISPLAY") || getenv("_WAYLAND_DISPLAY") ||
getenv("WAYLAND_SOCKET")) { getenv("WAYLAND_SOCKET")) {
struct wlr_backend *wl_backend = attempt_wl_backend(display); struct wlr_backend *wl_backend = attempt_wl_backend(display);
@ -130,7 +238,7 @@ struct wlr_backend *wlr_backend_autocreate(struct wl_display *display) {
#endif #endif
// Attempt DRM+libinput // Attempt DRM+libinput
struct wlr_session *session = wlr_session_create(display); session = wlr_session_create(display);
if (!session) { if (!session) {
wlr_log(L_ERROR, "Failed to start a DRM session"); wlr_log(L_ERROR, "Failed to start a DRM session");
wlr_backend_destroy(backend); wlr_backend_destroy(backend);
@ -138,40 +246,21 @@ struct wlr_backend *wlr_backend_autocreate(struct wl_display *display) {
} }
struct wlr_backend *libinput = wlr_libinput_backend_create(display, session); struct wlr_backend *libinput = wlr_libinput_backend_create(display, session);
if (libinput) { if (!libinput) {
wlr_multi_backend_add(backend, libinput);
} else {
wlr_log(L_ERROR, "Failed to start libinput backend"); wlr_log(L_ERROR, "Failed to start libinput backend");
wlr_backend_destroy(backend); wlr_backend_destroy(backend);
wlr_session_destroy(session); wlr_session_destroy(session);
return NULL; return NULL;
} }
wlr_multi_backend_add(backend, libinput);
int gpus[8]; struct wlr_backend *primary_drm =
size_t num_gpus = wlr_session_find_gpus(session, 8, gpus); attempt_drm_backend(display, backend, session);
struct wlr_backend *primary_drm = NULL;
wlr_log(L_INFO, "Found %zu GPUs", num_gpus);
for (size_t i = 0; i < num_gpus; ++i) {
struct wlr_backend *drm = wlr_drm_backend_create(display, session,
gpus[i], primary_drm);
if (!drm) {
wlr_log(L_ERROR, "Failed to open DRM device");
continue;
}
if (!primary_drm) {
primary_drm = drm;
}
wlr_multi_backend_add(backend, drm);
}
if (!primary_drm) { if (!primary_drm) {
wlr_log(L_ERROR, "Failed to open any DRM device"); wlr_log(L_ERROR, "Failed to open any DRM device");
wlr_backend_destroy(libinput); wlr_backend_destroy(libinput);
wlr_session_destroy(session);
wlr_backend_destroy(backend); wlr_backend_destroy(backend);
wlr_session_destroy(session);
return NULL; return NULL;
} }

View file

@ -130,20 +130,29 @@ static struct subbackend_state *multi_backend_get_subbackend(struct wlr_multi_ba
return NULL; return NULL;
} }
void wlr_multi_backend_add(struct wlr_backend *_multi, bool wlr_multi_backend_add(struct wlr_backend *_multi,
struct wlr_backend *backend) { struct wlr_backend *backend) {
assert(wlr_backend_is_multi(_multi)); assert(wlr_backend_is_multi(_multi));
struct wlr_multi_backend *multi = (struct wlr_multi_backend *)_multi; struct wlr_multi_backend *multi = (struct wlr_multi_backend *)_multi;
if (multi_backend_get_subbackend(multi, backend)) { if (multi_backend_get_subbackend(multi, backend)) {
// already added // already added
return; return true;
} }
struct subbackend_state *sub; struct wlr_renderer *multi_renderer =
if (!(sub = calloc(1, sizeof(struct subbackend_state)))) { multi_backend_get_renderer(&multi->backend);
struct wlr_renderer *backend_renderer = wlr_backend_get_renderer(backend);
if (multi_renderer != NULL && backend_renderer != NULL) {
wlr_log(L_ERROR, "Could not add backend: multiple renderers at the "
"same time aren't supported");
return false;
}
struct subbackend_state *sub = calloc(1, sizeof(struct subbackend_state));
if (sub == NULL) {
wlr_log(L_ERROR, "Could not add backend: allocation failed"); wlr_log(L_ERROR, "Could not add backend: allocation failed");
return; return false;
} }
wl_list_insert(&multi->backends, &sub->link); wl_list_insert(&multi->backends, &sub->link);
@ -160,6 +169,7 @@ void wlr_multi_backend_add(struct wlr_backend *_multi,
sub->new_output.notify = new_output_reemit; sub->new_output.notify = new_output_reemit;
wlr_signal_emit_safe(&multi->events.backend_add, backend); wlr_signal_emit_safe(&multi->events.backend_add, backend);
return true;
} }
void wlr_multi_backend_remove(struct wlr_backend *_multi, void wlr_multi_backend_remove(struct wlr_backend *_multi,

View file

@ -87,7 +87,9 @@ static void backend_destroy(struct wlr_backend *wlr_backend) {
free(backend->seat_name); free(backend->seat_name);
wl_event_source_remove(backend->remote_display_src); if (backend->remote_display_src) {
wl_event_source_remove(backend->remote_display_src);
}
wlr_renderer_destroy(backend->renderer); wlr_renderer_destroy(backend->renderer);
wlr_egl_finish(&backend->egl); wlr_egl_finish(&backend->egl);
if (backend->pointer) { if (backend->pointer) {

View file

@ -2,19 +2,26 @@ wlroots reads these environment variables
wlroots specific wlroots specific
---------------- ----------------
* *WLR_DRM_DEVICES*: specifies the DRM devices (as a colon separated list) instead of auto probing them. The first existing device in this list is considered the primary DRM device. * *WLR_DRM_DEVICES*: specifies the DRM devices (as a colon separated list)
* *WLR_DRM_NO_ATOMIC*: set to 1 to use legacy DRM interface instead of atomic mode setting instead of auto probing them. The first existing device in this list is
considered the primary DRM device.
* *WLR_DRM_NO_ATOMIC*: set to 1 to use legacy DRM interface instead of atomic
mode setting
* *WLR_LIBINPUT_NO_DEVICES*: set to 1 to not fail without any input devices * *WLR_LIBINPUT_NO_DEVICES*: set to 1 to not fail without any input devices
* *WLR_BACKENDS*: comma-separated list of backends to use (available backends:
wayland, x11, headless)
* *WLR_WL_OUTPUTS*: when using the wayland backend specifies the number of outputs * *WLR_WL_OUTPUTS*: when using the wayland backend specifies the number of outputs
* *WLR_X11_OUTPUTS*: when using the X11 backend specifies the number of outputs * *WLR_X11_OUTPUTS*: when using the X11 backend specifies the number of outputs
rootston specific rootston specific
------------------ ------------------
* *XKB_DEFAULT_RULES*, *XKB_DEFAULT_MODEL*, *XKB_DEFAULT_LAYOUT*, *XKB_DEFAULT_VARIANT*, *XKB_DEFAULT_OPTIONS*: xkb setup * *XKB_DEFAULT_RULES*, *XKB_DEFAULT_MODEL*, *XKB_DEFAULT_LAYOUT*,
*XKB_DEFAULT_VARIANT*, *XKB_DEFAULT_OPTIONS*: xkb setup
generic generic
------- -------
* *DISPLAY*: if set probe X11 backend in *wlr_backend_autocreate* * *DISPLAY*: if set probe X11 backend in *wlr_backend_autocreate*
* *WAYLAND_DISPLAY*, *_WAYLAND_DISPLAY*, *WAYLAND_SOCKET*: if set probe Wayland backend in *wlr_backend_autocreate* * *WAYLAND_DISPLAY*, *_WAYLAND_DISPLAY*, *WAYLAND_SOCKET*: if set probe Wayland
backend in *wlr_backend_autocreate*
* *XCURSOR_PATH*: directory where xcursors are located * *XCURSOR_PATH*: directory where xcursors are located
* *XDG_RUNTIME_DIR*: used to store non persistent temporary files * *XDG_RUNTIME_DIR*: used to store non persistent temporary files

View file

@ -13,7 +13,7 @@ struct wlr_backend *wlr_multi_backend_create(struct wl_display *display);
* Adds the given backend to the multi backend. This should be done before the * Adds the given backend to the multi backend. This should be done before the
* new backend is started. * new backend is started.
*/ */
void wlr_multi_backend_add(struct wlr_backend *multi, bool wlr_multi_backend_add(struct wlr_backend *multi,
struct wlr_backend *backend); struct wlr_backend *backend);
void wlr_multi_backend_remove(struct wlr_backend *multi, void wlr_multi_backend_remove(struct wlr_backend *multi,