aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--backend/drm/backend.c49
-rw-r--r--backend/drm/drm.c41
-rw-r--r--example/main.c2
-rw-r--r--include/backend/drm/backend.h4
-rw-r--r--include/backend/drm/drm.h2
-rw-r--r--include/session/interface.h9
-rw-r--r--include/wlr/session.h21
-rw-r--r--session/direct.c13
-rw-r--r--session/logind.c150
-rw-r--r--session/session.c14
10 files changed, 267 insertions, 38 deletions
diff --git a/backend/drm/backend.c b/backend/drm/backend.c
index a7e736bb..255384fb 100644
--- a/backend/drm/backend.c
+++ b/backend/drm/backend.c
@@ -4,6 +4,7 @@
#include <string.h>
#include <errno.h>
#include <wayland-server.h>
+#include <sys/stat.h>
#include <wlr/session.h>
#include <wlr/types.h>
@@ -41,6 +42,39 @@ 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 *drm = wl_container_of(listener, drm, device_paused);
+ struct device_arg *arg = data;
+
+ // TODO: Actually pause the renderer or something.
+ // We currently just expect it to fail its next pageflip.
+
+ if (arg->dev == drm->dev) {
+ wlr_log(L_INFO, "DRM fd paused");
+ }
+}
+
+static void device_resumed(struct wl_listener *listener, void *data) {
+ struct wlr_backend_state *drm = wl_container_of(listener, drm, device_resumed);
+ struct device_arg *arg = data;
+
+ if (arg->dev != drm->dev) {
+ return;
+ }
+
+ if (dup2(arg->fd, drm->fd) < 0) {
+ wlr_log(L_ERROR, "dup2 failed: %s", strerror(errno));
+ return;
+ }
+
+ wlr_log(L_INFO, "DRM fd resumed");
+
+ for (size_t i = 0; i < drm->outputs->length; ++i) {
+ struct wlr_output_state *output = drm->outputs->items[i];
+ wlr_drm_output_start_renderer(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));
@@ -74,6 +108,12 @@ struct wlr_backend *wlr_drm_backend_create(struct wl_display *display,
goto error_udev;
}
+ struct stat st;
+ if (fstat(state->fd, &st) < 0) {
+ wlr_log(L_ERROR, "Stat failed: %s", strerror(errno));
+ }
+ state->dev = st.st_rdev;
+
struct wl_event_loop *event_loop = wl_display_get_event_loop(display);
state->drm_event = wl_event_loop_add_fd(event_loop, state->fd,
@@ -83,6 +123,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 fa91745f..483c923c 100644
--- a/backend/drm/drm.c
+++ b/backend/drm/drm.c
@@ -116,23 +116,13 @@ static void wlr_drm_output_end(struct wlr_output_state *output) {
output->pageflip_pending = true;
}
-static bool display_init_renderer(struct wlr_drm_renderer *renderer,
- struct wlr_output_state *output) {
- struct wlr_output_mode *mode = output->wlr_output->current_mode;
- output->renderer = renderer;
- output->gbm = gbm_surface_create(renderer->gbm, mode->width,
- mode->height, GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
- if (!output->gbm) {
- wlr_log(L_ERROR, "Failed to create GBM surface for %s: %s", output->name,
- strerror(errno));
- return false;
+void wlr_drm_output_start_renderer(struct wlr_output_state *output) {
+ if (output->state != DRM_OUTPUT_CONNECTED) {
+ return;
}
- output->egl = wlr_egl_create_surface(&renderer->egl, output->gbm);
- if (output->egl == EGL_NO_SURFACE) {
- wlr_log(L_ERROR, "Failed to create EGL surface for %s", output->name);
- return false;
- }
+ struct wlr_drm_renderer *renderer = output->renderer;
+ struct wlr_output_mode *mode = output->wlr_output->current_mode;
// Render black frame
eglMakeCurrent(renderer->egl.display, output->egl, output->egl, renderer->egl.context);
@@ -152,6 +142,27 @@ static bool display_init_renderer(struct wlr_drm_renderer *renderer,
DRM_MODE_PAGE_FLIP_EVENT, output);
gbm_surface_release_buffer(output->gbm, bo);
+}
+
+static bool display_init_renderer(struct wlr_drm_renderer *renderer,
+ struct wlr_output_state *output) {
+ struct wlr_output_mode *mode = output->wlr_output->current_mode;
+ output->renderer = renderer;
+ output->gbm = gbm_surface_create(renderer->gbm, mode->width,
+ mode->height, GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
+ if (!output->gbm) {
+ wlr_log(L_ERROR, "Failed to create GBM surface for %s: %s", output->name,
+ strerror(errno));
+ return false;
+ }
+
+ output->egl = wlr_egl_create_surface(&renderer->egl, output->gbm);
+ if (output->egl == EGL_NO_SURFACE) {
+ wlr_log(L_ERROR, "Failed to create EGL surface for %s", output->name);
+ return false;
+ }
+
+ wlr_drm_output_start_renderer(output);
return true;
}
diff --git a/example/main.c b/example/main.c
index ae602903..1395e077 100644
--- a/example/main.c
+++ b/example/main.c
@@ -132,7 +132,7 @@ int main() {
struct wl_display *display = wl_display_create();
struct wl_event_loop *event_loop = wl_display_get_event_loop(display);
- struct wlr_session *session = wlr_session_start();
+ struct wlr_session *session = wlr_session_start(display);
if (!session) {
return 1;
}
diff --git a/include/backend/drm/backend.h b/include/backend/drm/backend.h
index 0c725ccb..e603bccd 100644
--- a/include/backend/drm/backend.h
+++ b/include/backend/drm/backend.h
@@ -19,10 +19,14 @@
struct wlr_backend_state {
int fd;
+ dev_t dev;
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 52ca65f0..004f3a10 100644
--- a/include/backend/drm/drm.h
+++ b/include/backend/drm/drm.h
@@ -59,4 +59,6 @@ void wlr_drm_output_cleanup(struct wlr_output_state *output, bool restore);
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_start_renderer(struct wlr_output_state *output);
+
#endif
diff --git a/include/session/interface.h b/include/session/interface.h
index a815718f..7345c4cf 100644
--- a/include/session/interface.h
+++ b/include/session/interface.h
@@ -1,18 +1,15 @@
#ifndef WLR_SESSION_INTERFACE_H
#define WLR_SESSION_INTERFACE_H
-struct wlr_session;
+#include <wlr/session.h>
struct session_interface {
- struct wlr_session *(*start)(void);
+ struct wlr_session *(*start)(struct wl_display *disp);
void (*finish)(struct wlr_session *session);
int (*open)(struct wlr_session *restrict session,
const char *restrict path);
void (*close)(struct wlr_session *session, int fd);
-};
-
-struct wlr_session {
- struct session_interface iface;
+ bool (*change_vt)(struct wlr_session *session, int vt);
};
extern const struct session_interface session_logind_iface;
diff --git a/include/wlr/session.h b/include/wlr/session.h
index cf04f1da..5f210f10 100644
--- a/include/wlr/session.h
+++ b/include/wlr/session.h
@@ -1,12 +1,29 @@
#ifndef WLR_SESSION_H
#define WLR_SESSION_H
-struct wlr_session;
+#include <wayland-server.h>
+#include <sys/types.h>
-struct wlr_session *wlr_session_start(void);
+struct session_interface;
+
+// Passed to the listeners of device_paused/resumed
+struct device_arg {
+ dev_t dev;
+ int fd; // Only for device_resumed
+};
+
+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);
int wlr_session_open_file(struct wlr_session *restrict session,
const char *restrict path);
void wlr_session_close_file(struct wlr_session *session, int fd);
+bool wlr_session_change_vt(struct wlr_session *session, int vt);
#endif
diff --git a/session/direct.c b/session/direct.c
index 08a9b617..c135070f 100644
--- a/session/direct.c
+++ b/session/direct.c
@@ -2,6 +2,7 @@
#include <errno.h>
#include <stdlib.h>
+#include <stdbool.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
@@ -23,13 +24,18 @@ static void direct_session_close(struct wlr_session *base, int fd) {
close(fd);
}
+static bool direct_change_vt(struct wlr_session *base, int vt) {
+ // TODO
+ return false;
+}
+
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) {
+static struct wlr_session *direct_session_start(struct wl_display *disp) {
struct direct_session *session = calloc(1, sizeof(*session));
if (!session) {
wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno));
@@ -38,7 +44,9 @@ static struct wlr_session *direct_session_start(void) {
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;
}
@@ -47,4 +55,5 @@ const struct session_interface session_direct_iface = {
.finish = direct_session_finish,
.open = direct_session_open,
.close = direct_session_close,
+ .change_vt = direct_change_vt,
};
diff --git a/session/logind.c b/session/logind.c
index 052c7454..6f0327e6 100644
--- a/session/logind.c
+++ b/session/logind.c
@@ -20,6 +20,7 @@ struct logind_session {
struct wlr_session base;
sd_bus *bus;
+ struct wl_event_source *event;
char *id;
char *path;
@@ -53,7 +54,7 @@ static int logind_take_device(struct wlr_session *restrict base,
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",
+ wlr_log(L_ERROR, "Failed to parse D-Bus response for '%s': %s",
path, strerror(-ret));
goto error;
}
@@ -97,6 +98,25 @@ static void logind_release_device(struct wlr_session *base, int fd) {
sd_bus_message_unref(msg);
}
+static bool logind_change_vt(struct wlr_session *base, int vt) {
+ struct logind_session *session = wl_container_of(base, session, base);
+
+ 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",
+ "/org/freedesktop/login1/seat/self", "org.freedesktop.login1.Seat", "SwitchTo",
+ &error, &msg, "u", (uint32_t)vt);
+ if (ret < 0) {
+ wlr_log(L_ERROR, "Failed to change to vt '%d'", vt);
+ }
+
+ sd_bus_error_free(&error);
+ sd_bus_message_unref(msg);
+ return ret >= 0;
+}
+
static bool session_activate(struct logind_session *session) {
int ret;
sd_bus_message *msg = NULL;
@@ -152,6 +172,7 @@ static void logind_session_finish(struct wlr_session *base) {
release_control(session);
+ wl_event_source_remove(session->event);
sd_bus_unref(session->bus);
free(session->id);
free(session->path);
@@ -159,7 +180,108 @@ static void logind_session_finish(struct wlr_session *base) {
free(session);
}
-static struct wlr_session *logind_session_start(void) {
+static int session_removed(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error) {
+ wlr_log(L_INFO, "SessionRemoved signal received");
+ return 0;
+}
+
+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;
+ }
+
+ struct device_arg arg = {
+ .dev = makedev(major, minor),
+ .fd = -1,
+ };
+
+ wl_signal_emit(&session->base.device_paused, &arg);
+
+ 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;
+ }
+
+ struct device_arg arg = {
+ .dev = makedev(major, minor),
+ .fd = fd,
+ };
+
+ wl_signal_emit(&session->base.device_resumed, &arg);
+
+error:
+ return 0;
+}
+
+static bool add_signal_matches(struct logind_session *session) {
+ int ret;
+
+ char str[256];
+ const char *fmt = "type='signal',"
+ "sender='org.freedesktop.login1',"
+ "interface='org.freedesktop.login1.%s',"
+ "member='%s',"
+ "path='%s'";
+
+ snprintf(str, sizeof(str), fmt, "Manager", "SesssionRemoved", "/org/freedesktop/login1");
+ ret = sd_bus_add_match(session->bus, NULL, str, session_removed, session);
+ if (ret < 0) {
+ wlr_log(L_ERROR, "Failed to add D-Bus match: %s", strerror(-ret));
+ return false;
+ }
+
+ snprintf(str, sizeof(str), fmt, "Session", "PauseDevice", session->path);
+ ret = sd_bus_add_match(session->bus, NULL, str, pause_device, session);
+ if (ret < 0) {
+ wlr_log(L_ERROR, "Failed to add D-Bus match: %s", strerror(-ret));
+ return false;
+ }
+
+ snprintf(str, sizeof(str), fmt, "Session", "ResumeDevice", session->path);
+ ret = sd_bus_add_match(session->bus, NULL, str, resume_device, session);
+ if (ret < 0) {
+ wlr_log(L_ERROR, "Failed to add D-Bus match: %s", strerror(-ret));
+ return false;
+ }
+
+ return true;
+}
+
+static int dbus_event(int fd, uint32_t mask, void *data) {
+ sd_bus *bus = data;
+ while (sd_bus_process(bus, NULL) > 0);
+ return 1;
+}
+
+static struct wlr_session *logind_session_start(struct wl_display *disp) {
int ret;
struct logind_session *session = calloc(1, sizeof(*session));
if (!session) {
@@ -183,26 +305,39 @@ static struct wlr_session *logind_session_start(void) {
int len = snprintf(NULL, 0, fmt, session->id);
session->path = malloc(len + 1);
- if (!session->path)
+ 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));
+ wlr_log(L_ERROR, "Failed to open D-Bus connection: %s", strerror(-ret));
goto error;
}
- if (!session_activate(session))
+ if (!add_signal_matches(session)) {
+ goto error_bus;
+ }
+
+ struct wl_event_loop *event_loop = wl_display_get_event_loop(disp);
+ session->event = wl_event_loop_add_fd(event_loop, sd_bus_get_fd(session->bus),
+ WL_EVENT_READABLE, dbus_event, session->bus);
+
+ if (!session_activate(session)) {
goto error_bus;
+ }
- if (!take_control(session))
+ if (!take_control(session)) {
goto error_bus;
+ }
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:
@@ -220,4 +355,5 @@ const struct session_interface session_logind_iface = {
.finish = logind_session_finish,
.open = logind_take_device,
.close = logind_release_device,
+ .change_vt = logind_change_vt,
};
diff --git a/session/session.c b/session/session.c
index 0562dbf7..5df9fbd4 100644
--- a/session/session.c
+++ b/session/session.c
@@ -13,11 +13,11 @@ static const struct session_interface *ifaces[] = {
NULL,
};
-struct wlr_session *wlr_session_start(void) {
+struct wlr_session *wlr_session_start(struct wl_display *disp) {
const struct session_interface **iter;
for (iter = ifaces; *iter; ++iter) {
- struct wlr_session *session = (*iter)->start();
+ struct wlr_session *session = (*iter)->start(disp);
if (session) {
return session;
}
@@ -28,15 +28,19 @@ struct wlr_session *wlr_session_start(void) {
}
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);
}