From 63792b38e46ae1741a6492d3c0183cb9566e4c9f Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Fri, 11 Aug 2023 19:28:07 +0200 Subject: [PATCH] backend/session: take wl_event_loop instead of wl_display wl_display holds a lot more than wlr_session needs: wlr_session only needs to wait for a FD to become readable, but wl_display provides full access to the Wayland client and protocol objects. Switch to wl_event_loop to better reflect the above. --- backend/backend.c | 7 +++---- backend/drm/monitor.c | 8 ++++---- backend/session/session.c | 28 ++++++++++++---------------- include/backend/drm/monitor.h | 3 ++- include/wlr/backend/session.h | 6 +++--- 5 files changed, 24 insertions(+), 28 deletions(-) diff --git a/backend/backend.c b/backend/backend.c index e5b8db14..1e57e832 100644 --- a/backend/backend.c +++ b/backend/backend.c @@ -73,7 +73,8 @@ void wlr_backend_destroy(struct wlr_backend *backend) { static struct wlr_session *session_create_and_wait(struct wl_display *disp) { #if WLR_HAS_SESSION - struct wlr_session *session = wlr_session_create(disp); + struct wl_event_loop *event_loop = wl_display_get_event_loop(disp); + struct wlr_session *session = wlr_session_create(event_loop); if (!session) { wlr_log(WLR_ERROR, "Failed to start a session"); @@ -85,8 +86,6 @@ static struct wlr_session *session_create_and_wait(struct wl_display *disp) { int64_t started_at = get_current_time_msec(); int64_t timeout = WAIT_SESSION_TIMEOUT; - struct wl_event_loop *event_loop = - wl_display_get_event_loop(session->display); while (!session->active) { int ret = wl_event_loop_dispatch(event_loop, (int)timeout); @@ -234,7 +233,7 @@ static bool attempt_drm_backend(struct wl_display *display, } if (getenv("WLR_DRM_DEVICES") == NULL) { - drm_backend_monitor_create(backend, primary_drm, session); + drm_backend_monitor_create(backend, primary_drm, session, display); } return true; diff --git a/backend/drm/monitor.c b/backend/drm/monitor.c index 4b1eeaa9..939a00ff 100644 --- a/backend/drm/monitor.c +++ b/backend/drm/monitor.c @@ -26,7 +26,7 @@ static void handle_add_drm_card(struct wl_listener *listener, void *data) { wlr_log(WLR_DEBUG, "Creating DRM backend for %s after hotplug", event->path); struct wlr_backend *child_drm = wlr_drm_backend_create( - backend_monitor->session->display, backend_monitor->session, + backend_monitor->display, backend_monitor->session, dev, backend_monitor->primary_drm); if (!child_drm) { wlr_log(WLR_ERROR, "Failed to create DRM backend after hotplug"); @@ -64,9 +64,8 @@ static void handle_multi_destroy(struct wl_listener *listener, void *data) { } struct wlr_drm_backend_monitor *drm_backend_monitor_create( - struct wlr_backend *multi, - struct wlr_backend *primary_drm, - struct wlr_session *session) { + struct wlr_backend *multi, struct wlr_backend *primary_drm, + struct wlr_session *session, struct wl_display *display) { struct wlr_drm_backend_monitor *monitor = calloc(1, sizeof(*monitor)); if (!monitor) { wlr_log_errno(WLR_ERROR, "Allocation failed"); @@ -76,6 +75,7 @@ struct wlr_drm_backend_monitor *drm_backend_monitor_create( monitor->multi = multi; monitor->primary_drm = primary_drm; monitor->session = session; + monitor->display = display; monitor->session_add_drm_card.notify = handle_add_drm_card; wl_signal_add(&session->events.add_drm_card, &monitor->session_add_drm_card); diff --git a/backend/session/session.c b/backend/session/session.c index 7b8b0bc2..52b2654b 100644 --- a/backend/session/session.c +++ b/backend/session/session.c @@ -39,7 +39,7 @@ static int libseat_event(int fd, uint32_t mask, void *data) { struct wlr_session *session = data; if (libseat_dispatch(session->seat_handle, 0) == -1) { wlr_log_errno(WLR_ERROR, "Failed to dispatch libseat"); - wl_display_terminate(session->display); + wlr_session_destroy(session); } return 1; } @@ -71,7 +71,8 @@ static void log_libseat(enum libseat_log_level level, _wlr_vlog(importance, wlr_fmt, args); } -static int libseat_session_init(struct wlr_session *session, struct wl_display *disp) { +static int libseat_session_init(struct wlr_session *session, + struct wl_event_loop *event_loop) { libseat_set_log_handler(log_libseat); libseat_set_log_level(LIBSEAT_LOG_LEVEL_INFO); @@ -91,7 +92,6 @@ static int libseat_session_init(struct wlr_session *session, struct wl_display * } snprintf(session->seat, sizeof(session->seat), "%s", seat_name); - struct wl_event_loop *event_loop = wl_display_get_event_loop(disp); session->libseat_event = wl_event_loop_add_fd(event_loop, libseat_get_fd(session->seat_handle), WL_EVENT_READABLE, libseat_event, session); if (session->libseat_event == NULL) { @@ -226,25 +226,26 @@ out: return 1; } -static void handle_display_destroy(struct wl_listener *listener, void *data) { +static void handle_event_loop_destroy(struct wl_listener *listener, void *data) { struct wlr_session *session = - wl_container_of(listener, session, display_destroy); + wl_container_of(listener, session, event_loop_destroy); wlr_session_destroy(session); } -struct wlr_session *wlr_session_create(struct wl_display *disp) { +struct wlr_session *wlr_session_create(struct wl_event_loop *event_loop) { struct wlr_session *session = calloc(1, sizeof(*session)); if (!session) { wlr_log_errno(WLR_ERROR, "Allocation failed"); return NULL; } + session->event_loop = event_loop; wl_signal_init(&session->events.active); wl_signal_init(&session->events.add_drm_card); wl_signal_init(&session->events.destroy); wl_list_init(&session->devices); - if (libseat_session_init(session, disp) == -1) { + if (libseat_session_init(session, event_loop) == -1) { wlr_log(WLR_ERROR, "Failed to load session backend"); goto error_open; } @@ -264,7 +265,6 @@ struct wlr_session *wlr_session_create(struct wl_display *disp) { udev_monitor_filter_add_match_subsystem_devtype(session->mon, "drm", NULL); udev_monitor_enable_receiving(session->mon); - struct wl_event_loop *event_loop = wl_display_get_event_loop(disp); int fd = udev_monitor_get_fd(session->mon); session->udev_event = wl_event_loop_add_fd(event_loop, fd, @@ -274,10 +274,8 @@ struct wlr_session *wlr_session_create(struct wl_display *disp) { goto error_mon; } - session->display = disp; - - session->display_destroy.notify = handle_display_destroy; - wl_display_add_destroy_listener(disp, &session->display_destroy); + session->event_loop_destroy.notify = handle_event_loop_destroy; + wl_event_loop_add_destroy_listener(event_loop, &session->event_loop_destroy); return session; @@ -298,7 +296,7 @@ void wlr_session_destroy(struct wlr_session *session) { } wl_signal_emit_mutable(&session->events.destroy, session); - wl_list_remove(&session->display_destroy.link); + wl_list_remove(&session->event_loop_destroy.link); wl_event_source_remove(session->udev_event); udev_monitor_unref(session->mon); @@ -472,10 +470,8 @@ ssize_t wlr_session_find_gpus(struct wlr_session *session, int64_t started_at = get_current_time_msec(); int64_t timeout = WAIT_GPU_TIMEOUT; - struct wl_event_loop *event_loop = - wl_display_get_event_loop(session->display); while (!handler.added) { - int ret = wl_event_loop_dispatch(event_loop, (int)timeout); + int ret = wl_event_loop_dispatch(session->event_loop, (int)timeout); if (ret < 0) { wlr_log_errno(WLR_ERROR, "Failed to wait for DRM card device: " "wl_event_loop_dispatch failed"); diff --git a/include/backend/drm/monitor.h b/include/backend/drm/monitor.h index 51817193..bf5bda2d 100644 --- a/include/backend/drm/monitor.h +++ b/include/backend/drm/monitor.h @@ -10,6 +10,7 @@ struct wlr_drm_backend_monitor { struct wlr_backend *multi; struct wlr_backend *primary_drm; struct wlr_session *session; + struct wl_display *display; struct wl_listener multi_destroy; struct wl_listener primary_drm_destroy; @@ -19,6 +20,6 @@ struct wlr_drm_backend_monitor { struct wlr_drm_backend_monitor *drm_backend_monitor_create( struct wlr_backend *multi, struct wlr_backend *primary_drm, - struct wlr_session *session); + struct wlr_session *session, struct wl_display *display); #endif diff --git a/include/wlr/backend/session.h b/include/wlr/backend/session.h index 3583b317..b61de852 100644 --- a/include/wlr/backend/session.h +++ b/include/wlr/backend/session.h @@ -42,8 +42,8 @@ struct wlr_session { struct wl_list devices; - struct wl_display *display; - struct wl_listener display_destroy; + struct wl_event_loop *event_loop; + struct wl_listener event_loop_destroy; struct { struct wl_signal active; @@ -80,7 +80,7 @@ struct wlr_device_change_event { * * Returns NULL on error. */ -struct wlr_session *wlr_session_create(struct wl_display *disp); +struct wlr_session *wlr_session_create(struct wl_event_loop *loop); /* * Closes a previously opened session and restores the virtual terminal.