Added public fields to wlr_session

master
Scott Anderson 7 years ago
parent f413a67bb1
commit bd5bca5283

@ -9,16 +9,18 @@ struct session_impl;
struct wlr_session { struct wlr_session {
const struct session_impl *impl; const struct session_impl *impl;
bool active;
struct wl_signal session_signal; struct wl_signal session_signal;
bool active;
int drm_fd;
unsigned vtnr;
char seat[8];
}; };
struct wlr_session *wlr_session_start(struct wl_display *disp); 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 *session, const char *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); bool wlr_session_change_vt(struct wlr_session *session, unsigned vt);
#endif #endif

@ -6,10 +6,9 @@
struct session_impl { struct session_impl {
struct wlr_session *(*start)(struct wl_display *disp); 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 *session, const char *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); bool (*change_vt)(struct wlr_session *session, unsigned vt);
}; };
#endif #endif

@ -30,8 +30,7 @@ const struct session_impl session_direct;
struct direct_session { struct direct_session {
struct wlr_session base; struct wlr_session base;
int tty_fd; int tty_fd;
int drm_fd; int old_kbmode;
int kb_mode;
int sock; int sock;
pid_t child; pid_t child;
@ -93,8 +92,7 @@ static int send_message(int sock, enum session_message_type type, const char *pa
return err ? -err : fd; return err ? -err : fd;
} }
static int direct_session_open(struct wlr_session *restrict base, static int direct_session_open(struct wlr_session *base, const char *path) {
const char *restrict path) {
struct direct_session *session = wl_container_of(base, session, base); struct direct_session *session = wl_container_of(base, session, base);
struct stat st; struct stat st;
@ -115,7 +113,7 @@ static int direct_session_open(struct wlr_session *restrict base,
} }
if (maj == DRM_MAJOR) { if (maj == DRM_MAJOR) {
session->drm_fd = fd; session->base.drm_fd = fd;
} }
return fd; return fd;
@ -124,17 +122,17 @@ static int direct_session_open(struct wlr_session *restrict base,
static void direct_session_close(struct wlr_session *base, int fd) { static void direct_session_close(struct wlr_session *base, int fd) {
struct direct_session *session = wl_container_of(base, session, base); struct direct_session *session = wl_container_of(base, session, base);
if (fd == session->drm_fd) { if (fd == session->base.drm_fd) {
send_message(session->sock, SESSION_DROPMASTER, NULL); send_message(session->sock, SESSION_DROPMASTER, NULL);
session->drm_fd = -1; session->base.drm_fd = -1;
} }
close(fd); close(fd);
} }
static bool direct_change_vt(struct wlr_session *base, int vt) { static bool direct_change_vt(struct wlr_session *base, unsigned vt) {
struct direct_session *session = wl_container_of(base, session, base); struct direct_session *session = wl_container_of(base, session, base);
return ioctl(session->tty_fd, VT_ACTIVATE, vt) == 0; return ioctl(session->tty_fd, VT_ACTIVATE, (int)vt) == 0;
} }
static void direct_session_finish(struct wlr_session *base) { static void direct_session_finish(struct wlr_session *base) {
@ -143,10 +141,16 @@ static void direct_session_finish(struct wlr_session *base) {
.mode = VT_AUTO, .mode = VT_AUTO,
}; };
ioctl(session->tty_fd, KDSKBMODE, session->kb_mode); errno = 0;
ioctl(session->tty_fd, KDSKBMODE, session->old_kbmode);
ioctl(session->tty_fd, KDSETMODE, KD_TEXT); ioctl(session->tty_fd, KDSETMODE, KD_TEXT);
ioctl(session->tty_fd, VT_SETMODE, &mode); ioctl(session->tty_fd, VT_SETMODE, &mode);
if (errno) {
wlr_log(L_ERROR, "Failed to restore tty");
}
send_message(session->sock, SESSION_END, NULL); send_message(session->sock, SESSION_END, NULL);
close(session->sock); close(session->sock);
wait(NULL); wait(NULL);
@ -175,8 +179,11 @@ static int vt_handler(int signo, void *data) {
} }
static bool setup_tty(struct direct_session *session, struct wl_display *display) { static bool setup_tty(struct direct_session *session, struct wl_display *display) {
// TODO: Change this to accept any TTY, instead of just the current one
session->tty_fd = dup(STDIN_FILENO); session->tty_fd = dup(STDIN_FILENO);
if (session->tty_fd == -1) {
wlr_log_errno(L_ERROR, "Cannot open tty");
return false;
}
struct stat st; struct stat st;
if (fstat(session->tty_fd, &st) == -1 || major(st.st_rdev) != TTY_MAJOR || if (fstat(session->tty_fd, &st) == -1 || major(st.st_rdev) != TTY_MAJOR ||
@ -185,9 +192,10 @@ static bool setup_tty(struct direct_session *session, struct wl_display *display
goto error; goto error;
} }
int ret; int tty = minor(st.st_rdev);
int ret, kd_mode;
session->base.vtnr = tty;
int kd_mode;
ret = ioctl(session->tty_fd, KDGETMODE, &kd_mode); ret = ioctl(session->tty_fd, KDGETMODE, &kd_mode);
if (ret) { if (ret) {
wlr_log_errno(L_ERROR, "Failed to get tty mode"); wlr_log_errno(L_ERROR, "Failed to get tty mode");
@ -200,10 +208,10 @@ static bool setup_tty(struct direct_session *session, struct wl_display *display
goto error; goto error;
} }
ioctl(session->tty_fd, VT_ACTIVATE, minor(st.st_rdev)); ioctl(session->tty_fd, VT_ACTIVATE, tty);
ioctl(session->tty_fd, VT_WAITACTIVE, minor(st.st_rdev)); ioctl(session->tty_fd, VT_WAITACTIVE, tty);
if (ioctl(session->tty_fd, KDGKBMODE, &session->kb_mode)) { if (ioctl(session->tty_fd, KDGKBMODE, &session->old_kbmode)) {
wlr_log_errno(L_ERROR, "Failed to read keyboard mode"); wlr_log_errno(L_ERROR, "Failed to read keyboard mode");
goto error; goto error;
} }
@ -311,8 +319,6 @@ static void communicate(int sock) {
return; return;
} }
} }
} }
#ifdef HAS_LIBCAP #ifdef HAS_LIBCAP
@ -379,8 +385,16 @@ static struct wlr_session *direct_session_start(struct wl_display *disp) {
goto error_session; goto error_session;
} }
// XXX: Is it okay to trust the environment like this?
const char *seat = getenv("XDG_SEAT");
if (!seat) {
seat = "seat0";
}
wlr_log(L_INFO, "Successfully loaded direct session"); wlr_log(L_INFO, "Successfully loaded direct session");
snprintf(session->base.seat, sizeof(session->base.seat), "%s", seat);
session->base.drm_fd = -1;
session->base.impl = &session_direct; session->base.impl = &session_direct;
session->base.active = true; session->base.active = true;
wl_signal_init(&session->base.session_signal); wl_signal_init(&session->base.session_signal);

@ -26,13 +26,9 @@ struct logind_session {
char *id; char *id;
char *path; char *path;
char *seat;
int drm_fd;
}; };
static int logind_take_device(struct wlr_session *restrict base, static int logind_take_device(struct wlr_session *base, const char *path) {
const char *restrict path) {
struct logind_session *session = wl_container_of(base, session, base); struct logind_session *session = wl_container_of(base, session, base);
int ret; int ret;
@ -72,7 +68,7 @@ static int logind_take_device(struct wlr_session *restrict base,
} }
if (major(st.st_rdev) == DRM_MAJOR) { if (major(st.st_rdev) == DRM_MAJOR) {
session->drm_fd = fd; session->base.drm_fd = fd;
} }
error: error:
@ -102,14 +98,14 @@ static void logind_release_device(struct wlr_session *base, int fd) {
} }
if (major(st.st_rdev) == DRM_MAJOR) { if (major(st.st_rdev) == DRM_MAJOR) {
session->drm_fd = -1; session->base.drm_fd = -1;
} }
sd_bus_error_free(&error); sd_bus_error_free(&error);
sd_bus_message_unref(msg); sd_bus_message_unref(msg);
} }
static bool logind_change_vt(struct wlr_session *base, int vt) { static bool logind_change_vt(struct wlr_session *base, unsigned vt) {
struct logind_session *session = wl_container_of(base, session, base); struct logind_session *session = wl_container_of(base, session, base);
int ret; int ret;
@ -187,7 +183,6 @@ static void logind_session_finish(struct wlr_session *base) {
sd_bus_unref(session->bus); sd_bus_unref(session->bus);
free(session->id); free(session->id);
free(session->path); free(session->path);
free(session->seat);
free(session); free(session);
} }
@ -242,7 +237,7 @@ static int resume_device(sd_bus_message *msg, void *userdata, sd_bus_error *ret_
} }
if (major == DRM_MAJOR) { if (major == DRM_MAJOR) {
dup2(fd, session->drm_fd); dup2(fd, session->base.drm_fd);
session->base.active = true; session->base.active = true;
wl_signal_emit(&session->base.session_signal, session); wl_signal_emit(&session->base.session_signal, session);
} }
@ -305,11 +300,20 @@ static struct wlr_session *logind_session_start(struct wl_display *disp) {
goto error; goto error;
} }
ret = sd_session_get_seat(session->id, &session->seat); ret = sd_session_get_vt(session->id, &session->base.vtnr);
if (ret < 0) {
wlr_log(L_ERROR, "Session not running in virtual terminal");
goto error;
}
char *seat;
ret = sd_session_get_seat(session->id, &seat);
if (ret < 0) { if (ret < 0) {
wlr_log(L_ERROR, "Failed to get seat id: %s", strerror(-ret)); wlr_log(L_ERROR, "Failed to get seat id: %s", strerror(-ret));
goto error; goto error;
} }
snprintf(session->base.seat, sizeof(session->base.seat), "%s", seat);
free(seat);
const char *fmt = "/org/freedesktop/login1/session/%s"; const char *fmt = "/org/freedesktop/login1/session/%s";
int len = snprintf(NULL, 0, fmt, session->id); int len = snprintf(NULL, 0, fmt, session->id);
@ -345,7 +349,7 @@ static struct wlr_session *logind_session_start(struct wl_display *disp) {
wlr_log(L_INFO, "Successfully loaded logind session"); wlr_log(L_INFO, "Successfully loaded logind session");
session->drm_fd = -1; session->base.drm_fd = -1;
session->base.impl = &session_logind; session->base.impl = &session_logind;
session->base.active = true; session->base.active = true;
wl_signal_init(&session->base.session_signal); wl_signal_init(&session->base.session_signal);
@ -357,7 +361,6 @@ error_bus:
error: error:
free(session->path); free(session->path);
free(session->id); free(session->id);
free(session->seat);
return NULL; return NULL;
} }

@ -33,9 +33,7 @@ void wlr_session_finish(struct wlr_session *session) {
session->impl->finish(session); session->impl->finish(session);
}; };
int wlr_session_open_file(struct wlr_session *restrict session, int wlr_session_open_file(struct wlr_session *session, const char *path) {
const char *restrict path) {
return session->impl->open(session, path); return session->impl->open(session, path);
} }
@ -43,6 +41,6 @@ void wlr_session_close_file(struct wlr_session *session, int fd) {
session->impl->close(session, fd); session->impl->close(session, fd);
} }
bool wlr_session_change_vt(struct wlr_session *session, int vt) { bool wlr_session_change_vt(struct wlr_session *session, unsigned vt) {
return session->impl->change_vt(session, vt); return session->impl->change_vt(session, vt);
} }

Loading…
Cancel
Save