2020-07-31 00:22:18 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "backend.h"
|
|
|
|
#include "libseat.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
2020-08-05 23:33:01 +02:00
|
|
|
extern const struct seat_impl seatd_impl;
|
|
|
|
extern const struct seat_impl logind_impl;
|
|
|
|
extern const struct seat_impl builtin_impl;
|
2021-03-26 10:05:12 +01:00
|
|
|
extern const struct seat_impl noop_impl;
|
2020-07-31 00:22:18 +02:00
|
|
|
|
|
|
|
static const struct named_backend impls[] = {
|
|
|
|
#ifdef SEATD_ENABLED
|
|
|
|
{"seatd", &seatd_impl},
|
|
|
|
#endif
|
|
|
|
#ifdef LOGIND_ENABLED
|
|
|
|
{"logind", &logind_impl},
|
|
|
|
#endif
|
|
|
|
#ifdef BUILTIN_ENABLED
|
|
|
|
{"builtin", &builtin_impl},
|
|
|
|
#endif
|
2021-03-26 10:05:12 +01:00
|
|
|
{"noop", &noop_impl},
|
2020-07-31 00:22:18 +02:00
|
|
|
{NULL, NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
#if !defined(SEATD_ENABLED) && !defined(LOGIND_ENABLED) && !defined(BUILTIN_ENABLED)
|
|
|
|
#error At least one backend must be enabled
|
|
|
|
#endif
|
|
|
|
|
2020-08-03 00:54:43 +02:00
|
|
|
struct libseat *libseat_open_seat(struct libseat_seat_listener *listener, void *data) {
|
2020-08-29 20:27:02 +02:00
|
|
|
if (listener == NULL || listener->enable_seat == NULL || listener->disable_seat == NULL) {
|
2020-07-31 00:22:18 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-08-27 15:56:16 +00:00
|
|
|
log_init();
|
2020-07-31 00:22:18 +02:00
|
|
|
|
|
|
|
char *backend_type = getenv("LIBSEAT_BACKEND");
|
2020-10-11 20:30:24 +02:00
|
|
|
if (backend_type != NULL) {
|
|
|
|
const struct named_backend *iter = impls;
|
|
|
|
while (iter->backend != NULL && strcmp(backend_type, iter->name) != 0) {
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
if (iter == NULL || iter->backend == NULL) {
|
|
|
|
log_errorf("No backend matched name '%s'", backend_type);
|
|
|
|
errno = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
struct libseat *backend = iter->backend->open_seat(listener, data);
|
|
|
|
if (backend == NULL) {
|
|
|
|
log_errorf("Backend '%s' failed to open seat: %s", iter->name,
|
|
|
|
strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
log_infof("Seat opened with backend '%s'", iter->name);
|
|
|
|
return backend;
|
|
|
|
}
|
|
|
|
|
2020-07-31 00:22:18 +02:00
|
|
|
struct libseat *backend = NULL;
|
|
|
|
for (const struct named_backend *iter = impls; iter->backend != NULL; iter++) {
|
2021-03-26 10:05:12 +01:00
|
|
|
if (iter->backend == &noop_impl) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-07-31 00:22:18 +02:00
|
|
|
backend = iter->backend->open_seat(listener, data);
|
|
|
|
if (backend != NULL) {
|
2020-08-28 19:29:03 +02:00
|
|
|
log_infof("Seat opened with backend '%s'", iter->name);
|
2020-10-11 20:30:24 +02:00
|
|
|
return backend;
|
2020-07-31 00:22:18 +02:00
|
|
|
}
|
2020-10-11 20:30:24 +02:00
|
|
|
log_infof("Backend '%s' failed to open seat, skipping", iter->name);
|
2020-07-31 00:22:18 +02:00
|
|
|
}
|
2020-10-11 20:30:24 +02:00
|
|
|
|
|
|
|
log_error("No backend was able to open a seat");
|
|
|
|
errno = ENOSYS;
|
|
|
|
return NULL;
|
2020-07-31 00:22:18 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 00:54:43 +02:00
|
|
|
int libseat_disable_seat(struct libseat *seat) {
|
2020-07-31 00:22:18 +02:00
|
|
|
assert(seat && seat->impl);
|
|
|
|
return seat->impl->disable_seat(seat);
|
|
|
|
}
|
|
|
|
|
2020-08-03 00:54:43 +02:00
|
|
|
int libseat_close_seat(struct libseat *seat) {
|
2020-07-31 00:22:18 +02:00
|
|
|
assert(seat && seat->impl);
|
|
|
|
return seat->impl->close_seat(seat);
|
|
|
|
}
|
|
|
|
|
2020-08-03 00:54:43 +02:00
|
|
|
const char *libseat_seat_name(struct libseat *seat) {
|
2020-07-31 00:22:18 +02:00
|
|
|
assert(seat && seat->impl);
|
|
|
|
return seat->impl->seat_name(seat);
|
|
|
|
}
|
|
|
|
|
2020-08-03 00:54:43 +02:00
|
|
|
int libseat_open_device(struct libseat *seat, const char *path, int *fd) {
|
2020-07-31 00:22:18 +02:00
|
|
|
assert(seat && seat->impl);
|
|
|
|
return seat->impl->open_device(seat, path, fd);
|
|
|
|
}
|
|
|
|
|
2020-08-03 00:54:43 +02:00
|
|
|
int libseat_close_device(struct libseat *seat, int device_id) {
|
2020-07-31 00:22:18 +02:00
|
|
|
assert(seat && seat->impl);
|
|
|
|
return seat->impl->close_device(seat, device_id);
|
|
|
|
}
|
|
|
|
|
2020-08-03 00:54:43 +02:00
|
|
|
int libseat_get_fd(struct libseat *seat) {
|
2020-07-31 00:22:18 +02:00
|
|
|
assert(seat && seat->impl);
|
|
|
|
return seat->impl->get_fd(seat);
|
|
|
|
}
|
|
|
|
|
2020-08-03 00:54:43 +02:00
|
|
|
int libseat_dispatch(struct libseat *seat, int timeout) {
|
2020-07-31 00:22:18 +02:00
|
|
|
assert(seat && seat->impl);
|
|
|
|
return seat->impl->dispatch(seat, timeout);
|
|
|
|
}
|
|
|
|
|
2020-08-03 00:54:43 +02:00
|
|
|
int libseat_switch_session(struct libseat *seat, int session) {
|
2020-07-31 00:22:18 +02:00
|
|
|
assert(seat && seat->impl);
|
|
|
|
return seat->impl->switch_session(seat, session);
|
|
|
|
}
|