Initial D-Bus signal support.

This commit is contained in:
Scott Anderson 2017-05-13 23:56:40 +12:00
parent 66e02e97ec
commit c88f252580
6 changed files with 122 additions and 28 deletions

View File

@ -81,15 +81,9 @@ int timer_done(void *data) {
return 1; return 1;
} }
int dpms_on(void *data) { int timer_change_vt(void *data) {
struct wlr_backend *backend = data; struct wlr_session *session = data;
wlr_drm_backend_dpms(backend, false); wlr_session_change_vt(session, 7);
return 1;
}
int dpms_off(void *data) {
struct wlr_backend *backend = data;
wlr_drm_backend_dpms(backend, true);
return 1; return 1;
} }
@ -117,7 +111,7 @@ int main() {
struct wl_display *display = wl_display_create(); struct wl_display *display = wl_display_create();
struct wl_event_loop *event_loop = wl_display_get_event_loop(display); 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) { if (!session) {
return 1; return 1;
} }
@ -132,14 +126,11 @@ int main() {
bool done = false; bool done = false;
struct wl_event_source *timer = wl_event_loop_add_timer(event_loop, struct wl_event_source *timer = wl_event_loop_add_timer(event_loop,
timer_done, &done); timer_done, &done);
struct wl_event_source *timer_dpms_on = wl_event_loop_add_timer(event_loop, struct wl_event_source *timer_vt = wl_event_loop_add_timer(event_loop,
dpms_on, wlr); timer_change_vt, session);
struct wl_event_source *timer_dpms_off = wl_event_loop_add_timer(event_loop,
dpms_off, wlr);
wl_event_source_timer_update(timer, 20000); wl_event_source_timer_update(timer, 30000);
wl_event_source_timer_update(timer_dpms_on, 5000); wl_event_source_timer_update(timer_vt, 5000);
wl_event_source_timer_update(timer_dpms_off, 10000);
while (!done) { while (!done) {
wl_event_loop_dispatch(event_loop, 0); wl_event_loop_dispatch(event_loop, 0);

View File

@ -4,11 +4,12 @@
struct wlr_session; struct wlr_session;
struct session_interface { struct session_interface {
struct wlr_session *(*start)(void); struct wlr_session *(*start)(struct wl_display *disp);
void (*finish)(struct wlr_session *session); void (*finish)(struct wlr_session *session);
int (*open)(struct wlr_session *restrict session, int (*open)(struct wlr_session *restrict session,
const char *restrict path); const char *restrict path);
void (*close)(struct wlr_session *session, int fd); void (*close)(struct wlr_session *session, int fd);
bool (*change_vt)(struct wlr_session *session, int vt);
}; };
struct wlr_session { struct wlr_session {

View File

@ -1,12 +1,15 @@
#ifndef WLR_SESSION_H #ifndef WLR_SESSION_H
#define WLR_SESSION_H #define WLR_SESSION_H
#include <wayland-server.h>
struct wlr_session; struct wlr_session;
struct wlr_session *wlr_session_start(void); struct wlr_session *wlr_session_start(struct wl_display *disp);
void wlr_session_finish(struct wlr_session *session); void wlr_session_finish(struct wlr_session *session);
int wlr_session_open_file(struct wlr_session *restrict session, int wlr_session_open_file(struct wlr_session *restrict session,
const char *restrict path); const char *restrict path);
void wlr_session_close_file(struct wlr_session *session, int fd); void wlr_session_close_file(struct wlr_session *session, int fd);
bool wlr_session_change_vt(struct wlr_session *session, int vt);
#endif #endif

View File

@ -2,6 +2,7 @@
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
#include <string.h> #include <string.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
@ -23,13 +24,18 @@ static void direct_session_close(struct wlr_session *base, int fd) {
close(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) { static void direct_session_finish(struct wlr_session *base) {
struct direct_session *session = wl_container_of(base, session, base); struct direct_session *session = wl_container_of(base, session, base);
free(session); 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)); struct direct_session *session = calloc(1, sizeof(*session));
if (!session) { if (!session) {
wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno));
@ -47,4 +53,5 @@ const struct session_interface session_direct_iface = {
.finish = direct_session_finish, .finish = direct_session_finish,
.open = direct_session_open, .open = direct_session_open,
.close = direct_session_close, .close = direct_session_close,
.change_vt = direct_change_vt,
}; };

View File

@ -20,6 +20,7 @@ struct logind_session {
struct wlr_session base; struct wlr_session base;
sd_bus *bus; sd_bus *bus;
struct wl_event_source *event;
char *id; char *id;
char *path; char *path;
@ -53,7 +54,7 @@ static int logind_take_device(struct wlr_session *restrict base,
int paused = 0; int paused = 0;
ret = sd_bus_message_read(msg, "hb", &fd, &paused); ret = sd_bus_message_read(msg, "hb", &fd, &paused);
if (ret < 0) { 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)); path, strerror(-ret));
goto error; goto error;
} }
@ -97,6 +98,25 @@ static void logind_release_device(struct wlr_session *base, int fd) {
sd_bus_message_unref(msg); 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) { static bool session_activate(struct logind_session *session) {
int ret; int ret;
sd_bus_message *msg = NULL; sd_bus_message *msg = NULL;
@ -152,6 +172,7 @@ static void logind_session_finish(struct wlr_session *base) {
release_control(session); release_control(session);
wl_event_source_remove(session->event);
sd_bus_unref(session->bus); sd_bus_unref(session->bus);
free(session->id); free(session->id);
free(session->path); free(session->path);
@ -159,7 +180,62 @@ static void logind_session_finish(struct wlr_session *base) {
free(session); 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");
return 0;
}
static int resume_device(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error) {
wlr_log(L_INFO, "ResumeDevice signal received");
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; int ret;
struct logind_session *session = calloc(1, sizeof(*session)); struct logind_session *session = calloc(1, sizeof(*session));
if (!session) { if (!session) {
@ -183,22 +259,33 @@ static struct wlr_session *logind_session_start(void) {
int len = snprintf(NULL, 0, fmt, session->id); int len = snprintf(NULL, 0, fmt, session->id);
session->path = malloc(len + 1); session->path = malloc(len + 1);
if (!session->path) if (!session->path) {
goto error; goto error;
}
sprintf(session->path, fmt, session->id); sprintf(session->path, fmt, session->id);
ret = sd_bus_default_system(&session->bus); ret = sd_bus_default_system(&session->bus);
if (ret < 0) { 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; goto error;
} }
if (!session_activate(session)) if (!add_signal_matches(session)) {
goto error_bus; goto error_bus;
}
if (!take_control(session)) 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; goto error_bus;
}
if (!take_control(session)) {
goto error_bus;
}
wlr_log(L_INFO, "Successfully loaded logind session"); wlr_log(L_INFO, "Successfully loaded logind session");
@ -220,4 +307,5 @@ const struct session_interface session_logind_iface = {
.finish = logind_session_finish, .finish = logind_session_finish,
.open = logind_take_device, .open = logind_take_device,
.close = logind_release_device, .close = logind_release_device,
.change_vt = logind_change_vt,
}; };

View File

@ -13,11 +13,11 @@ static const struct session_interface *ifaces[] = {
NULL, NULL,
}; };
struct wlr_session *wlr_session_start(void) { struct wlr_session *wlr_session_start(struct wl_display *disp) {
const struct session_interface **iter; const struct session_interface **iter;
for (iter = ifaces; *iter; ++iter) { for (iter = ifaces; *iter; ++iter) {
struct wlr_session *session = (*iter)->start(); struct wlr_session *session = (*iter)->start(disp);
if (session) { if (session) {
return session; return session;
} }
@ -40,3 +40,7 @@ int wlr_session_open_file(struct wlr_session *restrict session,
void wlr_session_close_file(struct wlr_session *session, int fd) { 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);
}