From 058b8bdf2755da16d38dccc8de715a984fdd620c Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sun, 14 May 2017 01:12:47 +1200 Subject: [PATCH] Working commit --- backend/drm/backend.c | 32 +++++++++++++++++++++ backend/drm/drm.c | 28 +++++++++++++++++++ example/main.c | 2 +- include/backend/drm/backend.h | 3 ++ include/backend/drm/drm.h | 2 ++ include/session/interface.h | 6 +--- include/wlr/session.h | 9 +++++- session/direct.c | 4 ++- session/logind.c | 52 ++++++++++++++++++++++++++++++++++- session/session.c | 8 +++--- 10 files changed, 133 insertions(+), 13 deletions(-) diff --git a/backend/drm/backend.c b/backend/drm/backend.c index f202d4d6..48e207c8 100644 --- a/backend/drm/backend.c +++ b/backend/drm/backend.c @@ -37,6 +37,29 @@ static struct wlr_backend_impl backend_impl = { .destroy = wlr_drm_backend_destroy }; +static void device_paused(struct wl_listener *listener, void *data) { + struct wlr_backend_state *backend = wl_container_of(listener, backend, device_paused); + + // TODO: Actually pause the renderer or something. + // We currently just expect it to fail its next pageflip. + + (void)backend; +} + +static void device_resumed(struct wl_listener *listener, void *data) { + struct wlr_backend_state *drm = wl_container_of(listener, drm, device_paused); + int *new_fd = data; + + close(drm->fd); + drm->fd = *new_fd; + drm->renderer.fd = *new_fd; + + for (size_t i = 0; i < drm->outputs->length; ++i) { + struct wlr_output_state *output = drm->outputs->items[i]; + wlr_drm_output_draw_blank(output); + } +} + struct wlr_backend *wlr_drm_backend_create(struct wl_display *display, struct wlr_session *session) { struct wlr_backend_state *state = calloc(1, sizeof(struct wlr_backend_state)); @@ -79,6 +102,15 @@ struct wlr_backend *wlr_drm_backend_create(struct wl_display *display, goto error_fd; } + wl_list_init(&state->device_paused.link); + wl_list_init(&state->device_paused.link); + + state->device_paused.notify = device_paused; + state->device_resumed.notify = device_resumed; + + wl_signal_add(&session->device_paused, &state->device_paused); + wl_signal_add(&session->device_resumed, &state->device_resumed); + // TODO: what is the difference between the per-output renderer and this // one? if (!wlr_drm_renderer_init(&state->renderer, state->fd)) { diff --git a/backend/drm/drm.c b/backend/drm/drm.c index db75853d..bd8c6506 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -474,3 +474,31 @@ void wlr_drm_output_dpms(int fd, struct wlr_output_state *output, bool screen_on DRM_MODE_DPMS_STANDBY); } } + +void wlr_drm_output_draw_blank(struct wlr_output_state *output) { + if (output->state != DRM_OUTPUT_CONNECTED) { + return; + } + + struct wlr_drm_renderer *renderer = output->renderer; + + eglMakeCurrent(renderer->egl.display, output->egl, output->egl, renderer->egl.context); + + glViewport(0, 0, output->width, output->height); + glClearColor(0.0, 0.0, 0.0, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + + eglSwapBuffers(renderer->egl.display, output->egl); + + struct gbm_bo *bo = gbm_surface_lock_front_buffer(output->gbm); + uint32_t fb_id = get_fb_for_bo(renderer->fd, bo); + + drmModeSetCrtc(renderer->fd, output->crtc, fb_id, 0, 0, + &output->connector, 1, &output->wlr_output->current_mode->state->mode); + drmModePageFlip(renderer->fd, output->crtc, fb_id, + DRM_MODE_PAGE_FLIP_EVENT, output); + + gbm_surface_release_buffer(output->gbm, bo); + + wlr_log(L_INFO, "Drew blank frame"); +} diff --git a/example/main.c b/example/main.c index 8da27594..a7dcc308 100644 --- a/example/main.c +++ b/example/main.c @@ -129,7 +129,7 @@ int main() { struct wl_event_source *timer_vt = wl_event_loop_add_timer(event_loop, timer_change_vt, session); - wl_event_source_timer_update(timer, 30000); + wl_event_source_timer_update(timer, 15000); wl_event_source_timer_update(timer_vt, 5000); while (!done) { diff --git a/include/backend/drm/backend.h b/include/backend/drm/backend.h index 0c725ccb..4df86e21 100644 --- a/include/backend/drm/backend.h +++ b/include/backend/drm/backend.h @@ -23,6 +23,9 @@ struct wlr_backend_state { struct wlr_backend *backend; struct wl_event_source *drm_event; + struct wl_listener device_paused; + struct wl_listener device_resumed; + uint32_t taken_crtcs; list_t *outputs; diff --git a/include/backend/drm/drm.h b/include/backend/drm/drm.h index 93de4fb8..6c60e2b6 100644 --- a/include/backend/drm/drm.h +++ b/include/backend/drm/drm.h @@ -60,4 +60,6 @@ void wlr_drm_output_dpms(int fd, struct wlr_output_state *output, bool screen_on void wlr_drm_scan_connectors(struct wlr_backend_state *state); int wlr_drm_event(int fd, uint32_t mask, void *data); +void wlr_drm_output_draw_blank(struct wlr_output_state *output); + #endif diff --git a/include/session/interface.h b/include/session/interface.h index 68754280..7345c4cf 100644 --- a/include/session/interface.h +++ b/include/session/interface.h @@ -1,7 +1,7 @@ #ifndef WLR_SESSION_INTERFACE_H #define WLR_SESSION_INTERFACE_H -struct wlr_session; +#include struct session_interface { struct wlr_session *(*start)(struct wl_display *disp); @@ -12,10 +12,6 @@ struct session_interface { bool (*change_vt)(struct wlr_session *session, int vt); }; -struct wlr_session { - struct session_interface iface; -}; - extern const struct session_interface session_logind_iface; extern const struct session_interface session_direct_iface; diff --git a/include/wlr/session.h b/include/wlr/session.h index 36521a73..4dfb026c 100644 --- a/include/wlr/session.h +++ b/include/wlr/session.h @@ -3,7 +3,14 @@ #include -struct wlr_session; +struct session_interface; + +struct wlr_session { + const struct session_interface *iface; + + struct wl_signal device_paused; + struct wl_signal device_resumed; +}; struct wlr_session *wlr_session_start(struct wl_display *disp); void wlr_session_finish(struct wlr_session *session); diff --git a/session/direct.c b/session/direct.c index 7536c162..c135070f 100644 --- a/session/direct.c +++ b/session/direct.c @@ -44,7 +44,9 @@ static struct wlr_session *direct_session_start(struct wl_display *disp) { wlr_log(L_INFO, "Successfully loaded direct session"); - session->base.iface = session_direct_iface; + session->base.iface = &session_direct_iface; + wl_signal_init(&session->base.device_paused); + wl_signal_init(&session->base.device_resumed); return &session->base; } diff --git a/session/logind.c b/session/logind.c index 5dc1fa55..7799c14b 100644 --- a/session/logind.c +++ b/session/logind.c @@ -187,11 +187,59 @@ static int session_removed(sd_bus_message *msg, void *userdata, sd_bus_error *re static int pause_device(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error) { wlr_log(L_INFO, "PauseDevice signal received"); + struct logind_session *session = userdata; + int ret; + + uint32_t major, minor; + ret = sd_bus_message_read(msg, "uu", &major, &minor); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to parse D-Bus response for PauseDevice: %s", + strerror(-ret)); + goto error; + } + + dev_t dev = makedev(major, minor); + wl_signal_emit(&session->base.device_paused, &dev); + + ret = sd_bus_call_method(session->bus, "org.freedesktop.login1", + session->path, "org.freedesktop.login1.Session", "PauseDeviceComplete", + ret_error, &msg, "uu", major, minor); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to send PauseDeviceComplete signal"); + } + +error: return 0; } static int resume_device(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error) { wlr_log(L_INFO, "ResumeDevice signal received"); + struct logind_session *session = userdata; + int ret; + + int fd; + uint32_t major, minor; + ret = sd_bus_message_read(msg, "uuh", &major, &minor, &fd); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to parse D-Bus response for ResumeDevice: %s", + 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 ResumeDevice: %s", + strerror(errno)); + goto error; + } + + // TODO: Use major/minor to make sure the right devices are getting signals + + wl_signal_emit(&session->base.device_resumed, &fd); + +error: return 0; } @@ -289,7 +337,9 @@ static struct wlr_session *logind_session_start(struct wl_display *disp) { wlr_log(L_INFO, "Successfully loaded logind session"); - session->base.iface = session_logind_iface; + session->base.iface = &session_logind_iface; + wl_signal_init(&session->base.device_paused); + wl_signal_init(&session->base.device_resumed); return &session->base; error_bus: diff --git a/session/session.c b/session/session.c index 828b3e20..5df9fbd4 100644 --- a/session/session.c +++ b/session/session.c @@ -28,19 +28,19 @@ struct wlr_session *wlr_session_start(struct wl_display *disp) { } void wlr_session_finish(struct wlr_session *session) { - session->iface.finish(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); + return session->iface->open(session, path); } void wlr_session_close_file(struct wlr_session *session, int fd) { - session->iface.close(session, fd); + session->iface->close(session, fd); } bool wlr_session_change_vt(struct wlr_session *session, int vt) { - return session->iface.change_vt(session, vt); + return session->iface->change_vt(session, vt); }