diff --git a/backend/backend.c b/backend/backend.c index 4c886af67..367a5229c 100644 --- a/backend/backend.c +++ b/backend/backend.c @@ -245,7 +245,8 @@ static struct wlr_backend *attempt_drm_backend(struct wl_display *display, #endif static bool attempt_backend_by_name(struct wl_display *display, - struct wlr_multi_backend *multi, char *name) { + struct wlr_backend *multi, char *name, + struct wlr_session **session_ptr) { struct wlr_backend *backend = NULL; if (strcmp(name, "wayland") == 0) { backend = attempt_wl_backend(display); @@ -257,9 +258,9 @@ static bool attempt_backend_by_name(struct wl_display *display, backend = attempt_headless_backend(display); } else if (strcmp(name, "drm") == 0 || strcmp(name, "libinput") == 0) { // DRM and libinput need a session - if (multi->session == NULL) { - multi->session = session_create_and_wait(display); - if (multi->session == NULL) { + if (*session_ptr == NULL) { + *session_ptr = session_create_and_wait(display); + if (*session_ptr == NULL) { wlr_log(WLR_ERROR, "failed to start a session"); return false; } @@ -267,13 +268,13 @@ static bool attempt_backend_by_name(struct wl_display *display, if (strcmp(name, "libinput") == 0) { #if WLR_HAS_LIBINPUT_BACKEND - backend = wlr_libinput_backend_create(display, multi->session); + backend = wlr_libinput_backend_create(display, *session_ptr); #endif } else { #if WLR_HAS_DRM_BACKEND // attempt_drm_backend adds the multi drm backends itself - return attempt_drm_backend(display, &multi->backend, - multi->session) != NULL; + return attempt_drm_backend(display, multi, + *session_ptr) != NULL; #endif } } else { @@ -281,12 +282,17 @@ static bool attempt_backend_by_name(struct wl_display *display, return false; } - return wlr_multi_backend_add(&multi->backend, backend); + return wlr_multi_backend_add(multi, backend); } -struct wlr_backend *wlr_backend_autocreate(struct wl_display *display) { +struct wlr_backend *wlr_backend_autocreate(struct wl_display *display, + struct wlr_session **session_ptr) { + if (session_ptr != NULL) { + *session_ptr = NULL; + } + + struct wlr_session *session = NULL; struct wlr_backend *backend = wlr_multi_backend_create(display); - struct wlr_multi_backend *multi = (struct wlr_multi_backend *)backend; if (!backend) { wlr_log(WLR_ERROR, "could not allocate multibackend"); return NULL; @@ -300,26 +306,23 @@ struct wlr_backend *wlr_backend_autocreate(struct wl_display *display) { names = strdup(names); if (names == NULL) { wlr_log(WLR_ERROR, "allocation failed"); - wlr_backend_destroy(backend); - return NULL; + goto error; } char *saveptr; char *name = strtok_r(names, ",", &saveptr); while (name != NULL) { - if (!attempt_backend_by_name(display, multi, name)) { + if (!attempt_backend_by_name(display, backend, name, &session)) { wlr_log(WLR_ERROR, "failed to add backend '%s'", name); - wlr_session_destroy(multi->session); - wlr_backend_destroy(backend); free(names); - return NULL; + goto error; } name = strtok_r(NULL, ",", &saveptr); } free(names); - return backend; + goto success; } if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET")) { @@ -329,7 +332,7 @@ struct wlr_backend *wlr_backend_autocreate(struct wl_display *display) { } wlr_multi_backend_add(backend, wl_backend); - return backend; + goto success; } #if WLR_HAS_X11_BACKEND @@ -342,26 +345,23 @@ struct wlr_backend *wlr_backend_autocreate(struct wl_display *display) { } wlr_multi_backend_add(backend, x11_backend); - return backend; + goto success; } #endif // Attempt DRM+libinput - multi->session = session_create_and_wait(display); - if (!multi->session) { + session = session_create_and_wait(display); + if (!session) { wlr_log(WLR_ERROR, "Failed to start a DRM session"); - wlr_backend_destroy(backend); - return NULL; + goto error; } #if WLR_HAS_LIBINPUT_BACKEND - struct wlr_backend *libinput = wlr_libinput_backend_create(display, - multi->session); + struct wlr_backend *libinput = + wlr_libinput_backend_create(display, session); if (!libinput) { wlr_log(WLR_ERROR, "Failed to start libinput backend"); - wlr_session_destroy(multi->session); - wlr_backend_destroy(backend); - return NULL; + goto error; } wlr_multi_backend_add(backend, libinput); #else @@ -372,28 +372,31 @@ struct wlr_backend *wlr_backend_autocreate(struct wl_display *display) { wlr_log(WLR_ERROR, "libinput support is not compiled in, " "refusing to start"); wlr_log(WLR_ERROR, "Set WLR_LIBINPUT_NO_DEVICES=1 to suppress this check"); - wlr_session_destroy(multi->session); - wlr_backend_destroy(backend); - return NULL; + goto error; } #endif #if WLR_HAS_DRM_BACKEND struct wlr_backend *primary_drm = - attempt_drm_backend(display, backend, multi->session); + attempt_drm_backend(display, backend, session); if (!primary_drm) { wlr_log(WLR_ERROR, "Failed to open any DRM device"); - wlr_session_destroy(multi->session); - wlr_backend_destroy(backend); - return NULL; + goto error; } - drm_backend_monitor_create(backend, primary_drm, multi->session); - - return backend; + drm_backend_monitor_create(backend, primary_drm, session); #endif +success: + struct wlr_multi_backend *multi = wl_container_of(backend, multi, backend); + multi->session = session; + if (session_ptr != NULL) { + *session_ptr = session; + } + return backend; + error: wlr_backend_destroy(backend); + wlr_session_destroy(session); return NULL; } diff --git a/examples/fullscreen-shell.c b/examples/fullscreen-shell.c index bc4bb7a86..cba5a4cb0 100644 --- a/examples/fullscreen-shell.c +++ b/examples/fullscreen-shell.c @@ -205,7 +205,7 @@ int main(int argc, char *argv[]) { struct fullscreen_server server = {0}; server.wl_display = wl_display_create(); - server.backend = wlr_backend_autocreate(server.wl_display); + server.backend = wlr_backend_autocreate(server.wl_display, NULL); server.renderer = wlr_renderer_autocreate(server.backend); wlr_renderer_init_wl_display(server.renderer, server.wl_display); server.allocator = wlr_allocator_autocreate(server.backend, diff --git a/examples/multi-pointer.c b/examples/multi-pointer.c index 26311319c..920ee0fca 100644 --- a/examples/multi-pointer.c +++ b/examples/multi-pointer.c @@ -272,7 +272,7 @@ int main(int argc, char *argv[]) { .clear_color = { 0.25f, 0.25f, 0.25f, 1 }, .display = display, }; - struct wlr_backend *wlr = wlr_backend_autocreate(display); + struct wlr_backend *wlr = wlr_backend_autocreate(display, NULL); if (!wlr) { exit(1); } diff --git a/examples/output-layout.c b/examples/output-layout.c index 97939953f..8a40e83f7 100644 --- a/examples/output-layout.c +++ b/examples/output-layout.c @@ -268,7 +268,7 @@ int main(int argc, char *argv[]) { state.layout = wlr_output_layout_create(); clock_gettime(CLOCK_MONOTONIC, &state.ts_last); - struct wlr_backend *wlr = wlr_backend_autocreate(display); + struct wlr_backend *wlr = wlr_backend_autocreate(display, NULL); if (!wlr) { exit(1); } diff --git a/examples/pointer.c b/examples/pointer.c index 6eb328ac2..db85b13a7 100644 --- a/examples/pointer.c +++ b/examples/pointer.c @@ -333,7 +333,7 @@ int main(int argc, char *argv[]) { .display = display }; - struct wlr_backend *wlr = wlr_backend_autocreate(display); + struct wlr_backend *wlr = wlr_backend_autocreate(display, NULL); if (!wlr) { exit(1); } diff --git a/examples/quads.c b/examples/quads.c index a9c7a14b8..2b6f5e9bc 100644 --- a/examples/quads.c +++ b/examples/quads.c @@ -189,7 +189,7 @@ int main(int argc, char *argv[]) { }; wl_list_init(&state.outputs); - struct wlr_backend *wlr = wlr_backend_autocreate(display); + struct wlr_backend *wlr = wlr_backend_autocreate(display, NULL); if (!wlr) { exit(1); } diff --git a/examples/rotation.c b/examples/rotation.c index 087fa8d18..33a4ac862 100644 --- a/examples/rotation.c +++ b/examples/rotation.c @@ -239,7 +239,7 @@ int main(int argc, char *argv[]) { }; wl_list_init(&state.outputs); - struct wlr_backend *wlr = wlr_backend_autocreate(display); + struct wlr_backend *wlr = wlr_backend_autocreate(display, NULL); if (!wlr) { exit(1); } diff --git a/examples/scene-graph.c b/examples/scene-graph.c index ba5d2d756..5821c9064 100644 --- a/examples/scene-graph.c +++ b/examples/scene-graph.c @@ -157,7 +157,7 @@ int main(int argc, char *argv[]) { struct server server = {0}; server.surface_offset = 0; server.display = wl_display_create(); - server.backend = wlr_backend_autocreate(server.display); + server.backend = wlr_backend_autocreate(server.display, NULL); server.scene = wlr_scene_create(); server.renderer = wlr_renderer_autocreate(server.backend); diff --git a/examples/simple.c b/examples/simple.c index 5a41d19e4..68308c7fe 100644 --- a/examples/simple.c +++ b/examples/simple.c @@ -172,7 +172,7 @@ int main(void) { .last_frame = { 0 }, .display = display }; - struct wlr_backend *backend = wlr_backend_autocreate(display); + struct wlr_backend *backend = wlr_backend_autocreate(display, NULL); if (!backend) { exit(1); } diff --git a/examples/tablet.c b/examples/tablet.c index 1fb0233bb..3e18636ad 100644 --- a/examples/tablet.c +++ b/examples/tablet.c @@ -355,7 +355,7 @@ int main(int argc, char *argv[]) { }; wl_list_init(&state.tablet_pads); wl_list_init(&state.tablet_tools); - struct wlr_backend *wlr = wlr_backend_autocreate(display); + struct wlr_backend *wlr = wlr_backend_autocreate(display, NULL); if (!wlr) { exit(1); } diff --git a/examples/touch.c b/examples/touch.c index d33e148aa..7c340e3c1 100644 --- a/examples/touch.c +++ b/examples/touch.c @@ -248,7 +248,7 @@ int main(int argc, char *argv[]) { wl_list_init(&state.touch_points); wl_list_init(&state.touch); - struct wlr_backend *wlr = wlr_backend_autocreate(display); + struct wlr_backend *wlr = wlr_backend_autocreate(display, NULL); if (!wlr) { exit(1); } diff --git a/include/wlr/backend.h b/include/wlr/backend.h index 86decad41..5cafad66b 100644 --- a/include/wlr/backend.h +++ b/include/wlr/backend.h @@ -34,8 +34,12 @@ struct wlr_backend { * Automatically initializes the most suitable backend given the environment. * Will always return a multi-backend. The backend is created but not started. * Returns NULL on failure. + * + * If session_ptr is not NULL, it's populated with the session which has been + * created with the backend, if any. */ -struct wlr_backend *wlr_backend_autocreate(struct wl_display *display); +struct wlr_backend *wlr_backend_autocreate(struct wl_display *display, + struct wlr_session **session_ptr); /** * Start the backend. This may signal new_input or new_output immediately, but * may also wait until the display's event loop begins. Returns false on diff --git a/tinywl/tinywl.c b/tinywl/tinywl.c index 7e3c8c1e0..ef993869f 100644 --- a/tinywl/tinywl.c +++ b/tinywl/tinywl.c @@ -828,7 +828,7 @@ int main(int argc, char *argv[]) { * output hardware. The autocreate option will choose the most suitable * backend based on the current environment, such as opening an X11 window * if an X11 server is running. */ - server.backend = wlr_backend_autocreate(server.wl_display); + server.backend = wlr_backend_autocreate(server.wl_display, NULL); if (server.backend == NULL) { wlr_log(WLR_ERROR, "failed to create wlr_backend"); return 1;