From 762ac7f4c0168bf2ce536d94b3b44cb95939d769 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Wed, 3 May 2017 14:11:22 +1200 Subject: New session interface. --- session/session.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 session/session.c (limited to 'session/session.c') diff --git a/session/session.c b/session/session.c new file mode 100644 index 00000000..88391e6e --- /dev/null +++ b/session/session.c @@ -0,0 +1,38 @@ +#include + +#include +#include "session/interface.h" + +static const struct session_interface *ifaces[] = { +#ifdef HAS_SYSTEMD + &session_logind_iface, +#endif + NULL, +}; + +struct wlr_session *wlr_session_start(void) { + const struct session_interface **iter; + + for (iter = ifaces; *iter; ++iter) { + struct wlr_session *session = (*iter)->start(); + if (session) { + return session; + } + } + + return NULL; +} + +void wlr_session_finish(struct wlr_session *session) { + session->iface.finish(session); +}; + +int wlr_session_open_file(struct wlr_session *restrict session, + const char *restrict path) { + + return session->iface.open(session, path); +} + +void wlr_session_close_file(struct wlr_session *session, int fd) { + session->iface.close(session, fd); +} -- cgit v1.2.3 From 81998fdc98231cb1b2e050da2fcec7c75d46320c Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Wed, 3 May 2017 16:23:07 +1200 Subject: Removed old session code from DRM backend. --- backend/CMakeLists.txt | 1 - backend/drm/backend.c | 26 ++--- backend/drm/session.c | 219 ------------------------------------------ backend/drm/udev.c | 11 ++- example/CMakeLists.txt | 1 + example/example-drm.c | 9 +- include/backend/drm/backend.h | 9 +- include/backend/drm/session.h | 24 ----- include/backend/drm/udev.h | 2 +- include/wlr/backend/drm.h | 6 +- session/logind.c | 3 + session/session.c | 3 + 12 files changed, 40 insertions(+), 274 deletions(-) delete mode 100644 backend/drm/session.c delete mode 100644 include/backend/drm/session.h (limited to 'session/session.c') diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 184854fe..ee8b04d6 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 7db6e9a1..9f9a02c7 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"); - goto error_loop; - } - if (!wlr_udev_init(backend)) { wlr_log(L_ERROR, "Failed to start udev"); - goto error_session; + goto error_loop; } - 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 073f822b..00000000 --- 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 ae99bcfc..6921a998 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 dfb069b4..fb2f3823 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 4c944226..a3a2a8fd 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 1ad8e479..32182d0c 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 6936b19a..00000000 --- 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 576b5d6e..a09ee218 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 5179db4b..ede43fda 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 c81a2020..0d8a52b5 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 88391e6e..2f1b87be 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; } -- cgit v1.2.3 From 0002b8dd08fdab7da12127ad2f43238f376b1a7a Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Wed, 3 May 2017 19:26:43 +1200 Subject: Added 'direct' session backend --- include/session/interface.h | 1 + session/CMakeLists.txt | 1 + session/direct.c | 50 +++++++++++++++++++++++++++++++++++++++++++++ session/session.c | 1 + 4 files changed, 53 insertions(+) create mode 100644 session/direct.c (limited to 'session/session.c') diff --git a/include/session/interface.h b/include/session/interface.h index 5dde89f0..a815718f 100644 --- a/include/session/interface.h +++ b/include/session/interface.h @@ -16,5 +16,6 @@ struct wlr_session { }; extern const struct session_interface session_logind_iface; +extern const struct session_interface session_direct_iface; #endif diff --git a/session/CMakeLists.txt b/session/CMakeLists.txt index 304f3220..bd83068b 100644 --- a/session/CMakeLists.txt +++ b/session/CMakeLists.txt @@ -4,6 +4,7 @@ include_directories( set(sources session.c + direct.c ) set(libs diff --git a/session/direct.c b/session/direct.c new file mode 100644 index 00000000..08a9b617 --- /dev/null +++ b/session/direct.c @@ -0,0 +1,50 @@ +#define _POSIX_C_SOURCE 200809L + +#include +#include +#include +#include +#include +#include + +#include "session/interface.h" +#include "common/log.h" + +struct direct_session { + struct wlr_session base; +}; + +static int direct_session_open(struct wlr_session *restrict base, + const char *restrict path) { + return open(path, O_RDWR | O_CLOEXEC); +} + +static void direct_session_close(struct wlr_session *base, int fd) { + close(fd); +} + +static void direct_session_finish(struct wlr_session *base) { + struct direct_session *session = wl_container_of(base, session, base); + + free(session); +} + +static struct wlr_session *direct_session_start(void) { + struct direct_session *session = calloc(1, sizeof(*session)); + if (!session) { + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + return NULL; + } + + wlr_log(L_INFO, "Successfully loaded direct session"); + + session->base.iface = session_direct_iface; + return &session->base; +} + +const struct session_interface session_direct_iface = { + .start = direct_session_start, + .finish = direct_session_finish, + .open = direct_session_open, + .close = direct_session_close, +}; diff --git a/session/session.c b/session/session.c index 2f1b87be..0562dbf7 100644 --- a/session/session.c +++ b/session/session.c @@ -9,6 +9,7 @@ static const struct session_interface *ifaces[] = { #ifdef HAS_SYSTEMD &session_logind_iface, #endif + &session_direct_iface, NULL, }; -- cgit v1.2.3