wlroots-hyprland/backend/drm/session.c

220 lines
4.7 KiB
C
Raw Normal View History

2017-05-01 05:50:19 +02:00
#define _POSIX_C_SOURCE 200809L
2017-05-01 05:20:48 +02:00
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
2017-05-02 03:00:25 +02:00
#include <errno.h>
2017-05-01 05:20:48 +02:00
#include <systemd/sd-bus.h>
#include <systemd/sd-login.h>
#include <unistd.h>
#include <sys/sysmacros.h>
#include <sys/stat.h>
#include <fcntl.h>
2017-05-01 07:49:18 +02:00
#include "backend/drm/backend.h"
2017-05-01 05:50:19 +02:00
#include "backend/drm/session.h"
2017-05-02 03:00:25 +02:00
#include "common/log.h"
2017-05-01 05:20:48 +02:00
2017-05-01 07:49:18 +02:00
int wlr_session_take_device(struct wlr_session *restrict session,
const char *restrict path,
bool *restrict paused_out)
2017-05-01 05:20:48 +02:00
{
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) {
2017-05-02 03:00:25 +02:00
wlr_log(L_ERROR, "Failed to stat '%s'", path);
2017-05-01 05:20:48 +02:00
return -1;
}
2017-05-01 07:49:18 +02:00
ret = sd_bus_call_method(session->bus,
2017-05-01 05:20:48 +02:00
"org.freedesktop.login1",
2017-05-01 07:49:18 +02:00
session->path,
2017-05-01 05:20:48 +02:00
"org.freedesktop.login1.Session",
"TakeDevice",
&error, &msg,
"uu", major(st.st_rdev), minor(st.st_rdev));
if (ret < 0) {
2017-05-02 03:00:25 +02:00
wlr_log(L_ERROR, "Failed to take device '%s': %s", path, error.message);
2017-05-01 05:20:48 +02:00
goto error;
}
int paused = 0;
ret = sd_bus_message_read(msg, "hb", &fd, &paused);
if (ret < 0) {
2017-05-02 03:00:25 +02:00
wlr_log(L_ERROR, "Failed to parse DBus response for '%s': %s",
path, strerror(-ret));
2017-05-01 05:20:48 +02:00
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);
2017-05-02 03:00:25 +02:00
if (fd == -1) {
wlr_log(L_ERROR, "Failed to clone file descriptor for '%s': %s",
path, strerror(errno));
goto error;
}
2017-05-01 05:20:48 +02:00
if (paused_out)
*paused_out = paused;
error:
sd_bus_error_free(&error);
sd_bus_message_unref(msg);
return fd;
}
2017-05-01 07:49:18 +02:00
void wlr_session_release_device(struct wlr_session *session, int fd)
2017-05-01 05:20:48 +02:00
{
int ret;
sd_bus_message *msg = NULL;
sd_bus_error error = SD_BUS_ERROR_NULL;
struct stat st;
if (fstat(fd, &st) < 0) {
2017-05-02 03:00:25 +02:00
wlr_log(L_ERROR, "Failed to stat device '%d'", fd);
2017-05-01 05:20:48 +02:00
return;
}
2017-05-01 07:49:18 +02:00
ret = sd_bus_call_method(session->bus,
2017-05-01 05:20:48 +02:00
"org.freedesktop.login1",
2017-05-01 07:49:18 +02:00
session->path,
2017-05-01 05:20:48 +02:00
"org.freedesktop.login1.Session",
"ReleaseDevice",
&error, &msg,
"uu", major(st.st_rdev), minor(st.st_rdev));
2017-05-02 03:00:25 +02:00
if (ret < 0)
wlr_log(L_ERROR, "Failed to release device '%d'", fd);
2017-05-01 05:20:48 +02:00
sd_bus_error_free(&error);
sd_bus_message_unref(msg);
}
2017-05-01 07:49:18 +02:00
static bool session_activate(struct wlr_session *session)
2017-05-01 05:20:48 +02:00
{
int ret;
sd_bus_message *msg = NULL;
sd_bus_error error = SD_BUS_ERROR_NULL;
2017-05-01 07:49:18 +02:00
ret = sd_bus_call_method(session->bus,
2017-05-01 05:20:48 +02:00
"org.freedesktop.login1",
2017-05-01 07:49:18 +02:00
session->path,
2017-05-01 05:20:48 +02:00
"org.freedesktop.login1.Session",
"Activate",
&error, &msg,
"");
2017-05-02 03:00:25 +02:00
if (ret < 0)
wlr_log(L_ERROR, "Failed to activate session");
2017-05-01 05:20:48 +02:00
sd_bus_error_free(&error);
sd_bus_message_unref(msg);
return ret >= 0;
}
2017-05-01 07:49:18 +02:00
static bool take_control(struct wlr_session *session)
2017-05-01 05:20:48 +02:00
{
int ret;
sd_bus_message *msg = NULL;
sd_bus_error error = SD_BUS_ERROR_NULL;
2017-05-01 07:49:18 +02:00
ret = sd_bus_call_method(session->bus,
2017-05-01 05:20:48 +02:00
"org.freedesktop.login1",
2017-05-01 07:49:18 +02:00
session->path,
2017-05-01 05:20:48 +02:00
"org.freedesktop.login1.Session",
"TakeControl",
&error, &msg,
"b", false);
2017-05-02 03:00:25 +02:00
if (ret < 0)
wlr_log(L_ERROR, "Failed to take control of session");
2017-05-01 05:20:48 +02:00
sd_bus_error_free(&error);
sd_bus_message_unref(msg);
return ret >= 0;
}
2017-05-01 07:49:18 +02:00
static void release_control(struct wlr_session *session)
2017-05-01 05:20:48 +02:00
{
int ret;
sd_bus_message *msg = NULL;
sd_bus_error error = SD_BUS_ERROR_NULL;
2017-05-01 07:49:18 +02:00
ret = sd_bus_call_method(session->bus,
2017-05-01 05:20:48 +02:00
"org.freedesktop.login1",
2017-05-01 07:49:18 +02:00
session->path,
2017-05-01 05:20:48 +02:00
"org.freedesktop.login1.Session",
"ReleaseControl",
&error, &msg,
"");
2017-05-02 03:00:25 +02:00
if (ret < 0)
wlr_log(L_ERROR, "Failed to release control of session");
2017-05-01 05:20:48 +02:00
sd_bus_error_free(&error);
sd_bus_message_unref(msg);
}
2017-05-01 07:49:18 +02:00
void wlr_session_end(struct wlr_session *session)
2017-05-01 05:20:48 +02:00
{
2017-05-01 07:49:18 +02:00
release_control(session);
2017-05-01 05:20:48 +02:00
2017-05-01 07:49:18 +02:00
sd_bus_unref(session->bus);
free(session->id);
free(session->path);
free(session->seat);
2017-05-01 05:20:48 +02:00
}
2017-05-01 07:49:18 +02:00
bool wlr_session_start(struct wlr_session *session)
2017-05-01 05:20:48 +02:00
{
int ret;
2017-05-01 07:49:18 +02:00
ret = sd_pid_get_session(getpid(), &session->id);
2017-05-01 05:20:48 +02:00
if (ret < 0) {
2017-05-02 03:00:25 +02:00
wlr_log(L_ERROR, "Failed to get session id: %s", strerror(-ret));
2017-05-01 05:20:48 +02:00
goto error;
}
2017-05-01 07:49:18 +02:00
ret = sd_session_get_seat(session->id, &session->seat);
2017-05-01 05:20:48 +02:00
if (ret < 0) {
2017-05-02 03:00:25 +02:00
wlr_log(L_ERROR, "Failed to get seat id: %s", strerror(-ret));
2017-05-01 05:20:48 +02:00
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";
2017-05-01 07:49:18 +02:00
int len = snprintf(NULL, 0, fmt, session->id);
2017-05-01 05:20:48 +02:00
2017-05-01 07:49:18 +02:00
session->path = malloc(len + 1);
if (!session->path)
2017-05-01 05:20:48 +02:00
goto error;
2017-05-01 07:49:18 +02:00
sprintf(session->path, fmt, session->id);
2017-05-01 05:20:48 +02:00
2017-05-02 08:13:17 +02:00
ret = sd_bus_default_system(&session->bus);
2017-05-01 05:20:48 +02:00
if (ret < 0) {
2017-05-02 03:00:25 +02:00
wlr_log(L_ERROR, "Failed to open DBus connection: %s", strerror(-ret));
2017-05-01 05:20:48 +02:00
goto error;
}
2017-05-02 03:00:25 +02:00
if (!session_activate(session))
2017-05-01 05:20:48 +02:00
goto error_bus;
2017-05-02 03:00:25 +02:00
if (!take_control(session))
2017-05-01 05:20:48 +02:00
goto error_bus;
return true;
error_bus:
2017-05-01 07:49:18 +02:00
sd_bus_unref(session->bus);
2017-05-01 05:20:48 +02:00
error:
2017-05-01 07:49:18 +02:00
free(session->path);
free(session->id);
free(session->seat);
2017-05-01 05:20:48 +02:00
return false;
}