2017-10-02 10:57:14 +02:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2017-08-26 04:02:04 +02:00
|
|
|
#include <assert.h>
|
2018-02-12 21:29:23 +01:00
|
|
|
#include <libudev.h>
|
2021-04-11 16:27:46 +02:00
|
|
|
#include <stdarg.h>
|
2017-05-03 04:11:22 +02:00
|
|
|
#include <stddef.h>
|
2017-08-26 04:02:04 +02:00
|
|
|
#include <stdlib.h>
|
2021-04-11 16:27:46 +02:00
|
|
|
#include <stdio.h>
|
2017-10-02 10:57:14 +02:00
|
|
|
#include <string.h>
|
2017-08-26 04:02:04 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2020-11-06 09:57:03 +01:00
|
|
|
#include <unistd.h>
|
2020-11-06 13:37:26 +01:00
|
|
|
#include <time.h>
|
2019-07-27 10:53:54 +02:00
|
|
|
#include <wayland-server-core.h>
|
2017-07-11 09:18:34 +02:00
|
|
|
#include <wlr/backend/session.h>
|
2018-07-21 15:44:20 +02:00
|
|
|
#include <wlr/config.h>
|
2017-06-21 18:10:07 +02:00
|
|
|
#include <wlr/util/log.h>
|
2018-02-12 21:29:23 +01:00
|
|
|
#include <xf86drm.h>
|
|
|
|
#include <xf86drmMode.h>
|
2020-07-25 21:53:06 +02:00
|
|
|
#include "backend/session/session.h"
|
2018-02-12 19:45:58 +01:00
|
|
|
#include "util/signal.h"
|
2017-05-03 04:11:22 +02:00
|
|
|
|
2021-04-11 16:27:46 +02:00
|
|
|
#include <libseat.h>
|
|
|
|
|
2020-11-06 13:37:26 +01:00
|
|
|
#define WAIT_GPU_TIMEOUT 10000 // ms
|
|
|
|
|
2021-04-11 16:27:46 +02:00
|
|
|
static void handle_enable_seat(struct libseat *seat, void *data) {
|
|
|
|
struct wlr_session *session = data;
|
|
|
|
session->active = true;
|
|
|
|
wlr_signal_emit_safe(&session->events.active, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_disable_seat(struct libseat *seat, void *data) {
|
|
|
|
struct wlr_session *session = data;
|
|
|
|
session->active = false;
|
|
|
|
wlr_signal_emit_safe(&session->events.active, NULL);
|
|
|
|
libseat_disable_seat(session->seat_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int libseat_event(int fd, uint32_t mask, void *data) {
|
|
|
|
struct wlr_session *session = data;
|
|
|
|
if (libseat_dispatch(session->seat_handle, 0) == -1) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Failed to dispatch libseat");
|
|
|
|
wl_display_terminate(session->display);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2017-06-05 01:30:37 +02:00
|
|
|
|
2021-04-11 16:27:46 +02:00
|
|
|
static struct libseat_seat_listener seat_listener = {
|
|
|
|
.enable_seat = handle_enable_seat,
|
|
|
|
.disable_seat = handle_disable_seat,
|
2017-05-03 04:11:22 +02:00
|
|
|
};
|
|
|
|
|
2021-04-11 16:27:46 +02:00
|
|
|
static enum wlr_log_importance libseat_log_level_to_wlr(
|
|
|
|
enum libseat_log_level level) {
|
|
|
|
switch (level) {
|
|
|
|
case LIBSEAT_LOG_LEVEL_ERROR:
|
|
|
|
return WLR_ERROR;
|
|
|
|
case LIBSEAT_LOG_LEVEL_INFO:
|
|
|
|
return WLR_INFO;
|
|
|
|
default:
|
|
|
|
return WLR_DEBUG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void log_libseat(enum libseat_log_level level,
|
|
|
|
const char *fmt, va_list args) {
|
|
|
|
enum wlr_log_importance importance = libseat_log_level_to_wlr(level);
|
|
|
|
|
|
|
|
static char wlr_fmt[1024];
|
|
|
|
snprintf(wlr_fmt, sizeof(wlr_fmt), "[libseat] %s", fmt);
|
|
|
|
|
|
|
|
_wlr_vlog(importance, wlr_fmt, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int libseat_session_init(struct wlr_session *session, struct wl_display *disp) {
|
|
|
|
libseat_set_log_handler(log_libseat);
|
|
|
|
libseat_set_log_level(LIBSEAT_LOG_LEVEL_INFO);
|
|
|
|
|
|
|
|
// libseat will take care of updating the logind state if necessary
|
|
|
|
setenv("XDG_SESSION_TYPE", "wayland", 1);
|
|
|
|
|
|
|
|
session->seat_handle = libseat_open_seat(&seat_listener, session);
|
|
|
|
if (session->seat_handle == NULL) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Unable to create seat");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *seat_name = libseat_seat_name(session->seat_handle);
|
|
|
|
if (seat_name == NULL) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Unable to get seat info");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
snprintf(session->seat, sizeof(session->seat), "%s", seat_name);
|
|
|
|
|
|
|
|
struct wl_event_loop *event_loop = wl_display_get_event_loop(disp);
|
|
|
|
session->libseat_event = wl_event_loop_add_fd(event_loop, libseat_get_fd(session->seat_handle),
|
|
|
|
WL_EVENT_READABLE, libseat_event, session);
|
|
|
|
if (session->libseat_event == NULL) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to create libseat event source");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We may have received enable_seat immediately after the open_seat result,
|
|
|
|
// so, dispatch once without timeout to speed up activation.
|
|
|
|
if (libseat_dispatch(session->seat_handle, 0) == -1) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "libseat dispatch failed");
|
|
|
|
goto error_dispatch;
|
|
|
|
}
|
|
|
|
|
|
|
|
wlr_log(WLR_INFO, "Successfully loaded libseat session");
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error_dispatch:
|
|
|
|
wl_event_source_remove(session->libseat_event);
|
|
|
|
session->libseat_event = NULL;
|
|
|
|
error:
|
|
|
|
libseat_close_seat(session->seat_handle);
|
|
|
|
session->seat_handle = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void libseat_session_finish(struct wlr_session *session) {
|
|
|
|
libseat_close_seat(session->seat_handle);
|
|
|
|
wl_event_source_remove(session->libseat_event);
|
|
|
|
session->seat_handle = NULL;
|
|
|
|
session->libseat_event = NULL;
|
|
|
|
}
|
|
|
|
|
2020-11-06 10:42:43 +01:00
|
|
|
static bool is_drm_card(const char *sysname) {
|
|
|
|
const char prefix[] = "card";
|
|
|
|
if (strncmp(sysname, prefix, strlen(prefix)) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (size_t i = strlen(prefix); sysname[i] != '\0'; i++) {
|
|
|
|
if (sysname[i] < '0' || sysname[i] > '9') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-26 04:02:04 +02:00
|
|
|
static int udev_event(int fd, uint32_t mask, void *data) {
|
|
|
|
struct wlr_session *session = data;
|
|
|
|
|
|
|
|
struct udev_device *udev_dev = udev_monitor_receive_device(session->mon);
|
|
|
|
if (!udev_dev) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-11-06 10:42:43 +01:00
|
|
|
const char *sysname = udev_device_get_sysname(udev_dev);
|
2020-11-06 10:56:13 +01:00
|
|
|
const char *devnode = udev_device_get_devnode(udev_dev);
|
2017-08-26 04:02:04 +02:00
|
|
|
const char *action = udev_device_get_action(udev_dev);
|
2020-11-06 10:42:43 +01:00
|
|
|
wlr_log(WLR_DEBUG, "udev event for %s (%s)", sysname, action);
|
2017-08-26 04:02:04 +02:00
|
|
|
|
2020-11-06 10:56:13 +01:00
|
|
|
if (!is_drm_card(sysname) || !action || !devnode) {
|
2017-08-26 04:02:04 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-11-06 10:56:13 +01:00
|
|
|
const char *seat = udev_device_get_property_value(udev_dev, "ID_SEAT");
|
|
|
|
if (!seat) {
|
|
|
|
seat = "seat0";
|
|
|
|
}
|
|
|
|
if (session->seat[0] != '\0' && strcmp(session->seat, seat) != 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(action, "add") == 0) {
|
|
|
|
wlr_log(WLR_DEBUG, "DRM device %s added", sysname);
|
|
|
|
struct wlr_session_add_event event = {
|
|
|
|
.path = devnode,
|
|
|
|
};
|
|
|
|
wlr_signal_emit_safe(&session->events.add_drm_card, &event);
|
|
|
|
} else if (strcmp(action, "change") == 0) {
|
|
|
|
dev_t devnum = udev_device_get_devnum(udev_dev);
|
|
|
|
struct wlr_device *dev;
|
|
|
|
wl_list_for_each(dev, &session->devices, link) {
|
|
|
|
if (dev->dev == devnum) {
|
|
|
|
wlr_log(WLR_DEBUG, "DRM device %s changed", sysname);
|
|
|
|
wlr_signal_emit_safe(&dev->events.change, NULL);
|
|
|
|
break;
|
|
|
|
}
|
2017-08-26 04:02:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
udev_device_unref(udev_dev);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-12-08 11:17:56 +01:00
|
|
|
static void handle_display_destroy(struct wl_listener *listener, void *data) {
|
|
|
|
struct wlr_session *session =
|
|
|
|
wl_container_of(listener, session, display_destroy);
|
|
|
|
wlr_session_destroy(session);
|
|
|
|
}
|
|
|
|
|
2021-04-11 16:27:46 +02:00
|
|
|
struct wlr_session *wlr_session_create(struct wl_display *disp) {
|
|
|
|
struct wlr_session *session = calloc(1, sizeof(*session));
|
|
|
|
if (!session) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-11-06 11:34:55 +01:00
|
|
|
wl_signal_init(&session->events.active);
|
2020-11-06 10:56:13 +01:00
|
|
|
wl_signal_init(&session->events.add_drm_card);
|
2020-07-25 21:53:06 +02:00
|
|
|
wl_signal_init(&session->events.destroy);
|
|
|
|
wl_list_init(&session->devices);
|
|
|
|
|
2021-04-11 16:27:46 +02:00
|
|
|
if (libseat_session_init(session, disp) == -1) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_ERROR, "Failed to load session backend");
|
2021-04-11 16:27:46 +02:00
|
|
|
goto error_open;
|
2017-08-26 04:02:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
session->udev = udev_new();
|
|
|
|
if (!session->udev) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log_errno(WLR_ERROR, "Failed to create udev context");
|
2017-08-26 04:02:04 +02:00
|
|
|
goto error_session;
|
2017-05-03 04:11:22 +02:00
|
|
|
}
|
|
|
|
|
2017-08-26 04:02:04 +02:00
|
|
|
session->mon = udev_monitor_new_from_netlink(session->udev, "udev");
|
|
|
|
if (!session->mon) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log_errno(WLR_ERROR, "Failed to create udev monitor");
|
2017-08-26 04:02:04 +02:00
|
|
|
goto error_udev;
|
|
|
|
}
|
|
|
|
|
|
|
|
udev_monitor_filter_add_match_subsystem_devtype(session->mon, "drm", NULL);
|
|
|
|
udev_monitor_enable_receiving(session->mon);
|
|
|
|
|
|
|
|
struct wl_event_loop *event_loop = wl_display_get_event_loop(disp);
|
|
|
|
int fd = udev_monitor_get_fd(session->mon);
|
|
|
|
|
|
|
|
session->udev_event = wl_event_loop_add_fd(event_loop, fd,
|
|
|
|
WL_EVENT_READABLE, udev_event, session);
|
|
|
|
if (!session->udev_event) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log_errno(WLR_ERROR, "Failed to create udev event source");
|
2017-08-26 04:02:04 +02:00
|
|
|
goto error_mon;
|
|
|
|
}
|
|
|
|
|
2020-11-06 11:14:49 +01:00
|
|
|
session->display = disp;
|
|
|
|
|
2017-12-08 11:17:56 +01:00
|
|
|
session->display_destroy.notify = handle_display_destroy;
|
|
|
|
wl_display_add_destroy_listener(disp, &session->display_destroy);
|
|
|
|
|
2017-08-26 04:02:04 +02:00
|
|
|
return session;
|
|
|
|
|
|
|
|
error_mon:
|
|
|
|
udev_monitor_unref(session->mon);
|
|
|
|
error_udev:
|
|
|
|
udev_unref(session->udev);
|
|
|
|
error_session:
|
2021-04-11 16:27:46 +02:00
|
|
|
libseat_session_finish(session);
|
|
|
|
error_open:
|
|
|
|
free(session);
|
2017-05-03 04:11:22 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-08-26 01:56:43 +02:00
|
|
|
void wlr_session_destroy(struct wlr_session *session) {
|
2017-07-11 08:51:19 +02:00
|
|
|
if (!session) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-26 23:23:12 +02:00
|
|
|
wlr_signal_emit_safe(&session->events.destroy, session);
|
2017-12-08 11:17:56 +01:00
|
|
|
wl_list_remove(&session->display_destroy.link);
|
|
|
|
|
2017-08-26 04:02:04 +02:00
|
|
|
wl_event_source_remove(session->udev_event);
|
|
|
|
udev_monitor_unref(session->mon);
|
|
|
|
udev_unref(session->udev);
|
|
|
|
|
2021-04-11 16:27:46 +02:00
|
|
|
libseat_session_finish(session);
|
|
|
|
free(session);
|
2017-08-26 04:02:04 +02:00
|
|
|
}
|
2017-05-03 04:11:22 +02:00
|
|
|
|
2020-11-06 10:16:07 +01:00
|
|
|
struct wlr_device *wlr_session_open_file(struct wlr_session *session,
|
|
|
|
const char *path) {
|
2021-04-11 16:27:46 +02:00
|
|
|
int fd;
|
|
|
|
int device_id = libseat_open_device(session->seat_handle, path, &fd);
|
|
|
|
if (device_id == -1) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Failed to open device: '%s'", path);
|
2020-11-06 10:16:07 +01:00
|
|
|
return NULL;
|
2017-08-26 04:02:04 +02:00
|
|
|
}
|
|
|
|
|
2017-08-26 04:50:16 +02:00
|
|
|
struct wlr_device *dev = malloc(sizeof(*dev));
|
2017-08-26 04:02:04 +02:00
|
|
|
if (!dev) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
2017-08-26 04:02:04 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
if (fstat(fd, &st) < 0) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log_errno(WLR_ERROR, "Stat failed");
|
2017-08-26 04:02:04 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->fd = fd;
|
|
|
|
dev->dev = st.st_rdev;
|
2021-04-11 16:27:46 +02:00
|
|
|
dev->device_id = device_id;
|
2020-11-06 10:16:07 +01:00
|
|
|
wl_signal_init(&dev->events.change);
|
2017-08-26 04:02:04 +02:00
|
|
|
wl_list_insert(&session->devices, &dev->link);
|
|
|
|
|
2020-11-06 10:16:07 +01:00
|
|
|
return dev;
|
2017-08-26 04:02:04 +02:00
|
|
|
|
|
|
|
error:
|
2021-04-11 16:27:46 +02:00
|
|
|
libseat_close_device(session->seat_handle, device_id);
|
2017-08-26 04:02:04 +02:00
|
|
|
free(dev);
|
2020-11-06 09:57:03 +01:00
|
|
|
close(fd);
|
2019-08-09 15:36:35 +02:00
|
|
|
return NULL;
|
2017-05-03 04:11:22 +02:00
|
|
|
}
|
|
|
|
|
2020-11-06 10:16:07 +01:00
|
|
|
void wlr_session_close_file(struct wlr_session *session,
|
|
|
|
struct wlr_device *dev) {
|
2021-04-11 16:27:46 +02:00
|
|
|
if (libseat_close_device(session->seat_handle, dev->device_id) == -1) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Failed to close device %d", dev->device_id);
|
|
|
|
}
|
2017-08-26 04:02:04 +02:00
|
|
|
wl_list_remove(&dev->link);
|
|
|
|
free(dev);
|
|
|
|
}
|
|
|
|
|
2017-07-09 07:53:13 +02:00
|
|
|
bool wlr_session_change_vt(struct wlr_session *session, unsigned vt) {
|
2017-07-11 08:51:19 +02:00
|
|
|
if (!session) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-11 16:27:46 +02:00
|
|
|
return libseat_switch_session(session->seat_handle, vt) == 0;
|
2017-05-13 13:56:40 +02:00
|
|
|
}
|
2017-08-26 04:02:04 +02:00
|
|
|
|
|
|
|
/* Tests if 'path' is KMS compatible by trying to open it.
|
|
|
|
* It leaves the open device in *fd_out it it succeeds.
|
|
|
|
*/
|
2020-11-06 10:16:07 +01:00
|
|
|
static struct wlr_device *open_if_kms(struct wlr_session *restrict session,
|
2019-03-01 22:37:35 +01:00
|
|
|
const char *restrict path) {
|
2017-08-26 04:02:04 +02:00
|
|
|
if (!path) {
|
2020-11-06 10:16:07 +01:00
|
|
|
return NULL;
|
2017-08-26 04:02:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-06 10:16:07 +01:00
|
|
|
struct wlr_device *dev = wlr_session_open_file(session, path);
|
|
|
|
if (!dev) {
|
|
|
|
return NULL;
|
2017-08-26 04:02:04 +02:00
|
|
|
}
|
|
|
|
|
2021-02-23 20:53:50 +01:00
|
|
|
// The kernel errors out with EOPNOTSUPP if DRIVER_MODESET isn't set
|
|
|
|
drmModeRes *res = drmModeGetResources(dev->fd);
|
|
|
|
if (!res) {
|
|
|
|
if (errno != EOPNOTSUPP) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "drmModeGetResources(%s) failed", path);
|
|
|
|
}
|
|
|
|
goto out_dev;
|
|
|
|
}
|
|
|
|
if (res->count_crtcs == 0) {
|
|
|
|
drmModeFreeResources(res);
|
2020-11-06 10:16:07 +01:00
|
|
|
goto out_dev;
|
2017-08-26 04:02:04 +02:00
|
|
|
}
|
2021-02-23 20:53:50 +01:00
|
|
|
drmModeFreeResources(res);
|
2017-08-26 04:02:04 +02:00
|
|
|
|
2020-11-06 10:16:07 +01:00
|
|
|
return dev;
|
2017-08-26 04:02:04 +02:00
|
|
|
|
2020-11-06 10:16:07 +01:00
|
|
|
out_dev:
|
|
|
|
wlr_session_close_file(session, dev);
|
|
|
|
return NULL;
|
2017-08-26 04:02:04 +02:00
|
|
|
}
|
|
|
|
|
2020-12-28 10:52:40 +01:00
|
|
|
static ssize_t explicit_find_gpus(struct wlr_session *session,
|
2020-11-06 10:16:07 +01:00
|
|
|
size_t ret_len, struct wlr_device *ret[static ret_len], const char *str) {
|
2017-10-02 10:57:14 +02:00
|
|
|
char *gpus = strdup(str);
|
|
|
|
if (!gpus) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
2020-12-28 10:52:40 +01:00
|
|
|
return -1;
|
2017-10-02 10:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
char *save;
|
|
|
|
char *ptr = strtok_r(gpus, ":", &save);
|
|
|
|
do {
|
|
|
|
if (i >= ret_len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret[i] = open_if_kms(session, ptr);
|
2020-11-06 10:16:07 +01:00
|
|
|
if (!ret[i]) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_ERROR, "Unable to open %s as DRM device", ptr);
|
2017-10-02 10:57:14 +02:00
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
} while ((ptr = strtok_r(NULL, ":", &save)));
|
|
|
|
|
2017-11-16 10:30:54 +01:00
|
|
|
free(gpus);
|
2017-10-02 10:57:14 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2020-11-06 11:14:49 +01:00
|
|
|
static struct udev_enumerate *enumerate_drm_cards(struct udev *udev) {
|
|
|
|
struct udev_enumerate *en = udev_enumerate_new(udev);
|
|
|
|
if (!en) {
|
|
|
|
wlr_log(WLR_ERROR, "udev_enumerate_new failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
udev_enumerate_add_match_subsystem(en, "drm");
|
|
|
|
udev_enumerate_add_match_sysname(en, "card[0-9]*");
|
|
|
|
|
|
|
|
if (udev_enumerate_scan_devices(en) != 0) {
|
|
|
|
wlr_log(WLR_ERROR, "udev_enumerate_scan_devices failed");
|
|
|
|
udev_enumerate_unref(en);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return en;
|
|
|
|
}
|
|
|
|
|
2020-11-06 13:37:26 +01:00
|
|
|
static uint64_t get_current_time_ms(void) {
|
|
|
|
struct timespec ts = {0};
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
|
|
|
|
}
|
|
|
|
|
2020-11-06 11:14:49 +01:00
|
|
|
struct find_gpus_add_handler {
|
|
|
|
bool added;
|
|
|
|
struct wl_listener listener;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void find_gpus_handle_add(struct wl_listener *listener, void *data) {
|
|
|
|
struct find_gpus_add_handler *handler =
|
|
|
|
wl_container_of(listener, handler, listener);
|
|
|
|
handler->added = true;
|
|
|
|
}
|
|
|
|
|
2017-08-26 04:02:04 +02:00
|
|
|
/* Tries to find the primary GPU by checking for the "boot_vga" attribute.
|
|
|
|
* If it's not found, it returns the first valid GPU it finds.
|
|
|
|
*/
|
2020-12-28 10:52:40 +01:00
|
|
|
ssize_t wlr_session_find_gpus(struct wlr_session *session,
|
2020-11-06 10:16:07 +01:00
|
|
|
size_t ret_len, struct wlr_device **ret) {
|
2017-10-02 10:57:14 +02:00
|
|
|
const char *explicit = getenv("WLR_DRM_DEVICES");
|
|
|
|
if (explicit) {
|
|
|
|
return explicit_find_gpus(session, ret_len, ret, explicit);
|
|
|
|
}
|
|
|
|
|
2020-11-06 11:14:49 +01:00
|
|
|
struct udev_enumerate *en = enumerate_drm_cards(session->udev);
|
2017-08-26 04:02:04 +02:00
|
|
|
if (!en) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-11-06 11:14:49 +01:00
|
|
|
if (udev_enumerate_get_list_entry(en) == NULL) {
|
|
|
|
udev_enumerate_unref(en);
|
|
|
|
wlr_log(WLR_INFO, "Waiting for a DRM card device");
|
|
|
|
|
|
|
|
struct find_gpus_add_handler handler = {0};
|
|
|
|
handler.listener.notify = find_gpus_handle_add;
|
|
|
|
wl_signal_add(&session->events.add_drm_card, &handler.listener);
|
|
|
|
|
2020-11-06 13:37:26 +01:00
|
|
|
uint64_t started_at = get_current_time_ms();
|
|
|
|
uint64_t timeout = WAIT_GPU_TIMEOUT;
|
2020-11-06 11:14:49 +01:00
|
|
|
struct wl_event_loop *event_loop =
|
|
|
|
wl_display_get_event_loop(session->display);
|
|
|
|
while (!handler.added) {
|
2020-11-06 13:37:26 +01:00
|
|
|
int ret = wl_event_loop_dispatch(event_loop, (int)timeout);
|
2020-11-06 11:14:49 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Failed to wait for DRM card device: "
|
|
|
|
"wl_event_loop_dispatch failed");
|
|
|
|
udev_enumerate_unref(en);
|
|
|
|
return -1;
|
|
|
|
}
|
2020-11-06 13:37:26 +01:00
|
|
|
|
|
|
|
uint64_t now = get_current_time_ms();
|
|
|
|
if (now >= started_at + WAIT_GPU_TIMEOUT) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
timeout = started_at + WAIT_GPU_TIMEOUT - now;
|
2020-11-06 11:14:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
wl_list_remove(&handler.listener.link);
|
|
|
|
|
|
|
|
en = enumerate_drm_cards(session->udev);
|
|
|
|
if (!en) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2017-08-26 04:02:04 +02:00
|
|
|
|
|
|
|
struct udev_list_entry *entry;
|
2017-10-01 05:47:05 +02:00
|
|
|
size_t i = 0;
|
2017-08-26 04:02:04 +02:00
|
|
|
|
|
|
|
udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(en)) {
|
2017-10-01 05:47:05 +02:00
|
|
|
if (i == ret_len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-08-26 04:02:04 +02:00
|
|
|
bool is_boot_vga = false;
|
|
|
|
|
|
|
|
const char *path = udev_list_entry_get_name(entry);
|
|
|
|
struct udev_device *dev = udev_device_new_from_syspath(session->udev, path);
|
|
|
|
if (!dev) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *seat = udev_device_get_property_value(dev, "ID_SEAT");
|
2018-01-21 16:28:21 +01:00
|
|
|
if (!seat) {
|
2017-08-26 04:02:04 +02:00
|
|
|
seat = "seat0";
|
2018-01-21 16:28:21 +01:00
|
|
|
}
|
2017-10-01 05:47:05 +02:00
|
|
|
if (session->seat[0] && strcmp(session->seat, seat) != 0) {
|
2017-08-26 04:02:04 +02:00
|
|
|
udev_device_unref(dev);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is owned by 'dev', so we don't need to free it
|
|
|
|
struct udev_device *pci =
|
|
|
|
udev_device_get_parent_with_subsystem_devtype(dev, "pci", NULL);
|
|
|
|
|
|
|
|
if (pci) {
|
|
|
|
const char *id = udev_device_get_sysattr_value(pci, "boot_vga");
|
|
|
|
if (id && strcmp(id, "1") == 0) {
|
|
|
|
is_boot_vga = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 10:16:07 +01:00
|
|
|
struct wlr_device *wlr_dev =
|
|
|
|
open_if_kms(session, udev_device_get_devnode(dev));
|
|
|
|
if (!wlr_dev) {
|
2017-08-26 04:02:04 +02:00
|
|
|
udev_device_unref(dev);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
udev_device_unref(dev);
|
|
|
|
|
2020-11-06 10:16:07 +01:00
|
|
|
ret[i] = wlr_dev;
|
2017-08-26 04:02:04 +02:00
|
|
|
if (is_boot_vga) {
|
2020-11-06 10:16:07 +01:00
|
|
|
struct wlr_device *tmp = ret[0];
|
2017-10-01 05:47:05 +02:00
|
|
|
ret[0] = ret[i];
|
|
|
|
ret[i] = tmp;
|
2017-08-26 04:02:04 +02:00
|
|
|
}
|
2017-10-01 05:47:05 +02:00
|
|
|
|
|
|
|
++i;
|
2017-08-26 04:02:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
udev_enumerate_unref(en);
|
|
|
|
|
2017-10-01 05:47:05 +02:00
|
|
|
return i;
|
2017-08-26 04:02:04 +02:00
|
|
|
}
|