diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 184854fe1..ee8b04d66 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -11,7 +11,6 @@ add_library(wlr-backend wayland/wl_output.c drm/backend.c drm/drm.c - drm/session.c drm/udev.c ) diff --git a/backend/drm/backend.c b/backend/drm/backend.c index 7db6e9a10..9f9a02c72 100644 --- a/backend/drm/backend.c +++ b/backend/drm/backend.c @@ -4,17 +4,16 @@ #include #include +#include #include #include "backend/drm/backend.h" #include "backend/drm/drm.h" -#include "backend/drm/session.h" #include "backend/drm/udev.h" #include "common/log.h" -struct wlr_drm_backend *wlr_drm_backend_init(struct wl_listener *add, - struct wl_listener *rem, - struct wl_listener *render) +struct wlr_drm_backend *wlr_drm_backend_init(struct wlr_session *session, + struct wl_listener *add, struct wl_listener *rem, struct wl_listener *render) { struct wlr_drm_backend *backend = calloc(1, sizeof *backend); if (!backend) { @@ -22,6 +21,8 @@ struct wlr_drm_backend *wlr_drm_backend_init(struct wl_listener *add, return NULL; } + backend->session = session; + backend->displays = list_create(); if (!backend->displays) { wlr_log(L_ERROR, "Failed to allocate list"); @@ -34,17 +35,12 @@ struct wlr_drm_backend *wlr_drm_backend_init(struct wl_listener *add, goto error_list; } - if (!wlr_session_start(&backend->session)) { - wlr_log(L_ERROR, "Failed to start session"); + if (!wlr_udev_init(backend)) { + wlr_log(L_ERROR, "Failed to start udev"); goto error_loop; } - if (!wlr_udev_init(backend)) { - wlr_log(L_ERROR, "Failed to start udev"); - goto error_session; - } - - backend->fd = wlr_udev_find_gpu(&backend->udev, &backend->session); + backend->fd = wlr_udev_find_gpu(&backend->udev, backend->session); if (backend->fd == -1) { wlr_log(L_ERROR, "Failed to open DRM device"); goto error_udev; @@ -71,11 +67,9 @@ struct wlr_drm_backend *wlr_drm_backend_init(struct wl_listener *add, return backend; error_fd: - wlr_session_release_device(&backend->session, backend->fd); + wlr_session_close_file(backend->session, backend->fd); error_udev: wlr_udev_free(&backend->udev); -error_session: - wlr_session_end(&backend->session); error_loop: wl_event_loop_destroy(backend->event_loop); error_list: @@ -101,8 +95,8 @@ void wlr_drm_backend_free(struct wlr_drm_backend *backend) wlr_drm_renderer_free(&backend->renderer); wlr_udev_free(&backend->udev); - wlr_session_release_device(&backend->session, backend->fd); - wlr_session_end(&backend->session); + wlr_session_close_file(backend->session, backend->fd); + wlr_session_finish(backend->session); wl_event_source_remove(backend->event_src.drm); wl_event_source_remove(backend->event_src.udev); diff --git a/backend/drm/session.c b/backend/drm/session.c deleted file mode 100644 index 073f822b7..000000000 --- a/backend/drm/session.c +++ /dev/null @@ -1,219 +0,0 @@ -#define _POSIX_C_SOURCE 200809L - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "backend/drm/backend.h" -#include "backend/drm/session.h" -#include "common/log.h" - -int wlr_session_take_device(struct wlr_session *restrict session, - const char *restrict path, - bool *restrict paused_out) -{ - int ret; - int fd = -1; - sd_bus_message *msg = NULL; - sd_bus_error error = SD_BUS_ERROR_NULL; - - struct stat st; - if (stat(path, &st) < 0) { - wlr_log(L_ERROR, "Failed to stat '%s'", path); - return -1; - } - - ret = sd_bus_call_method(session->bus, - "org.freedesktop.login1", - session->path, - "org.freedesktop.login1.Session", - "TakeDevice", - &error, &msg, - "uu", major(st.st_rdev), minor(st.st_rdev)); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to take device '%s': %s", path, error.message); - goto error; - } - - int paused = 0; - ret = sd_bus_message_read(msg, "hb", &fd, &paused); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to parse DBus response for '%s': %s", - path, strerror(-ret)); - goto error; - } - - // The original fd seem to be closed when the message is freed - // so we just clone it. - fd = fcntl(fd, F_DUPFD_CLOEXEC, 0); - if (fd == -1) { - wlr_log(L_ERROR, "Failed to clone file descriptor for '%s': %s", - path, strerror(errno)); - goto error; - } - - if (paused_out) - *paused_out = paused; - -error: - sd_bus_error_free(&error); - sd_bus_message_unref(msg); - return fd; -} - -void wlr_session_release_device(struct wlr_session *session, int fd) -{ - int ret; - sd_bus_message *msg = NULL; - sd_bus_error error = SD_BUS_ERROR_NULL; - - struct stat st; - if (fstat(fd, &st) < 0) { - wlr_log(L_ERROR, "Failed to stat device '%d'", fd); - return; - } - - ret = sd_bus_call_method(session->bus, - "org.freedesktop.login1", - session->path, - "org.freedesktop.login1.Session", - "ReleaseDevice", - &error, &msg, - "uu", major(st.st_rdev), minor(st.st_rdev)); - if (ret < 0) - wlr_log(L_ERROR, "Failed to release device '%d'", fd); - - sd_bus_error_free(&error); - sd_bus_message_unref(msg); -} - -static bool session_activate(struct wlr_session *session) -{ - int ret; - sd_bus_message *msg = NULL; - sd_bus_error error = SD_BUS_ERROR_NULL; - - ret = sd_bus_call_method(session->bus, - "org.freedesktop.login1", - session->path, - "org.freedesktop.login1.Session", - "Activate", - &error, &msg, - ""); - if (ret < 0) - wlr_log(L_ERROR, "Failed to activate session"); - - sd_bus_error_free(&error); - sd_bus_message_unref(msg); - return ret >= 0; -} - -static bool take_control(struct wlr_session *session) -{ - int ret; - sd_bus_message *msg = NULL; - sd_bus_error error = SD_BUS_ERROR_NULL; - - ret = sd_bus_call_method(session->bus, - "org.freedesktop.login1", - session->path, - "org.freedesktop.login1.Session", - "TakeControl", - &error, &msg, - "b", false); - if (ret < 0) - wlr_log(L_ERROR, "Failed to take control of session"); - - sd_bus_error_free(&error); - sd_bus_message_unref(msg); - return ret >= 0; -} - -static void release_control(struct wlr_session *session) -{ - int ret; - sd_bus_message *msg = NULL; - sd_bus_error error = SD_BUS_ERROR_NULL; - - ret = sd_bus_call_method(session->bus, - "org.freedesktop.login1", - session->path, - "org.freedesktop.login1.Session", - "ReleaseControl", - &error, &msg, - ""); - if (ret < 0) - wlr_log(L_ERROR, "Failed to release control of session"); - - sd_bus_error_free(&error); - sd_bus_message_unref(msg); -} - -void wlr_session_end(struct wlr_session *session) -{ - release_control(session); - - sd_bus_unref(session->bus); - free(session->id); - free(session->path); - free(session->seat); -} - -bool wlr_session_start(struct wlr_session *session) -{ - int ret; - - ret = sd_pid_get_session(getpid(), &session->id); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to get session id: %s", strerror(-ret)); - goto error; - } - - ret = sd_session_get_seat(session->id, &session->seat); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to get seat id: %s", strerror(-ret)); - goto error; - } - - // This could be done using asprintf, but I don't want to define _GNU_SOURCE - - const char *fmt = "/org/freedesktop/login1/session/%s"; - int len = snprintf(NULL, 0, fmt, session->id); - - session->path = malloc(len + 1); - if (!session->path) - goto error; - - sprintf(session->path, fmt, session->id); - - ret = sd_bus_default_system(&session->bus); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to open DBus connection: %s", strerror(-ret)); - goto error; - } - - if (!session_activate(session)) - goto error_bus; - - if (!take_control(session)) - goto error_bus; - - return true; - -error_bus: - sd_bus_unref(session->bus); - -error: - free(session->path); - free(session->id); - free(session->seat); - return false; -} diff --git a/backend/drm/udev.c b/backend/drm/udev.c index ae99bcfc1..6921a9982 100644 --- a/backend/drm/udev.c +++ b/backend/drm/udev.c @@ -9,9 +9,10 @@ #include #include +#include + #include "backend/drm/backend.h" #include "backend/drm/udev.h" -#include "backend/drm/session.h" #include "backend/drm/drm.h" #include "common/log.h" @@ -26,7 +27,7 @@ static bool device_is_kms(struct wlr_udev *udev, if (!path) return false; - fd = wlr_session_take_device(session, path, NULL); + fd = wlr_session_open_file(session, path); if (fd < 0) return false; @@ -39,7 +40,7 @@ static bool device_is_kms(struct wlr_udev *udev, goto out_res; if (*fd_out >= 0) { - wlr_session_release_device(session, *fd_out); + wlr_session_close_file(session, *fd_out); free(udev->drm_path); } @@ -52,7 +53,7 @@ static bool device_is_kms(struct wlr_udev *udev, out_res: drmModeFreeResources(res); out_fd: - wlr_session_release_device(session, fd); + wlr_session_close_file(session, fd); return false; } @@ -79,6 +80,7 @@ int wlr_udev_find_gpu(struct wlr_udev *udev, struct wlr_session *session) if (!dev) continue; + /* const char *seat = udev_device_get_property_value(dev, "ID_SEAT"); if (!seat) seat = "seat0"; @@ -86,6 +88,7 @@ int wlr_udev_find_gpu(struct wlr_udev *udev, struct wlr_session *session) udev_device_unref(dev); continue; } + */ struct udev_device *pci = udev_device_get_parent_with_subsystem_devtype(dev, "pci", NULL); diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index dfb069b4b..fb2f3823d 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -12,4 +12,5 @@ add_executable(example-drm target_link_libraries(example-drm wlr-backend + wlr-session ) diff --git a/example/example-drm.c b/example/example-drm.c index 4c9442266..a3a2a8fda 100644 --- a/example/example-drm.c +++ b/example/example-drm.c @@ -6,6 +6,7 @@ #include #include +#include struct state { float color[3]; @@ -92,7 +93,13 @@ int main() wl_list_init(&state.render.link); clock_gettime(CLOCK_MONOTONIC, &state.last_frame); - struct wlr_drm_backend *wlr = wlr_drm_backend_init(&state.add, &state.rem, &state.render); + struct wlr_session *session = wlr_session_start(); + if (!session) { + return 1; + } + + struct wlr_drm_backend *wlr = wlr_drm_backend_init(session, + &state.add, &state.rem, &state.render); bool done = false; struct wl_event_loop *event_loop = wlr_drm_backend_get_event_loop(wlr); diff --git a/include/backend/drm/backend.h b/include/backend/drm/backend.h index 1ad8e479b..32182d0c7 100644 --- a/include/backend/drm/backend.h +++ b/include/backend/drm/backend.h @@ -8,10 +8,10 @@ #include #include +#include #include #include -#include "session.h" #include "udev.h" #include "event.h" #include "drm.h" @@ -38,13 +38,12 @@ struct wlr_drm_backend { uint32_t taken_crtcs; struct wlr_drm_renderer renderer; - struct wlr_session session; + struct wlr_session *session; struct wlr_udev udev; }; -struct wlr_drm_backend *wlr_drm_backend_init(struct wl_listener *add, - struct wl_listener *rem, - struct wl_listener *render); +struct wlr_drm_backend *wlr_drm_backend_init(struct wlr_session *session, + struct wl_listener *add, struct wl_listener *rem, struct wl_listener *render); void wlr_drm_backend_free(struct wlr_drm_backend *backend); #endif diff --git a/include/backend/drm/session.h b/include/backend/drm/session.h deleted file mode 100644 index 6936b19ad..000000000 --- a/include/backend/drm/session.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef SESSION_H -#define SESSION_H - -#include -#include - -struct wlr_session { - sd_bus *bus; - - char *id; - char *path; - char *seat; -}; - -bool wlr_session_start(struct wlr_session *session); -void wlr_session_end(struct wlr_session *session); - -int wlr_session_take_device(struct wlr_session *restrict session, - const char *restrict path, - bool *restrict paused_out); - -void wlr_session_release_device(struct wlr_session *session, int fd); - -#endif diff --git a/include/backend/drm/udev.h b/include/backend/drm/udev.h index 576b5d6e5..a09ee2183 100644 --- a/include/backend/drm/udev.h +++ b/include/backend/drm/udev.h @@ -3,7 +3,7 @@ #include -#include "backend/drm/session.h" +#include struct wlr_udev { struct udev *udev; diff --git a/include/wlr/backend/drm.h b/include/wlr/backend/drm.h index 5179db4bf..ede43fda5 100644 --- a/include/wlr/backend/drm.h +++ b/include/wlr/backend/drm.h @@ -2,13 +2,13 @@ #define WLR_BACKEND_DRM_H #include +#include struct wlr_drm_backend; struct wlr_drm_display; -struct wlr_drm_backend *wlr_drm_backend_init(struct wl_listener *add, - struct wl_listener *rem, - struct wl_listener *render); +struct wlr_drm_backend *wlr_drm_backend_init(struct wlr_session *session, + struct wl_listener *add, struct wl_listener *rem, struct wl_listener *render); void wlr_drm_backend_free(struct wlr_drm_backend *backend); struct wl_event_loop *wlr_drm_backend_get_event_loop(struct wlr_drm_backend *backend); diff --git a/session/logind.c b/session/logind.c index c81a20206..0d8a52b54 100644 --- a/session/logind.c +++ b/session/logind.c @@ -200,6 +200,9 @@ static struct wlr_session *logind_session_start(void) { if (!take_control(session)) goto error_bus; + wl_log(L_INFO, "Successfully loaded logind session"); + + session->base.iface = session_logind_iface; return &session->base; error_bus: diff --git a/session/session.c b/session/session.c index 88391e6eb..2f1b87be0 100644 --- a/session/session.c +++ b/session/session.c @@ -1,6 +1,8 @@ #include #include +#include +#include "common/log.h" #include "session/interface.h" static const struct session_interface *ifaces[] = { @@ -20,6 +22,7 @@ struct wlr_session *wlr_session_start(void) { } } + wlr_log(L_ERROR, "Failed to load session backend"); return NULL; }