From aca13320b3e6df8cb575f64db736dc38b8b30ed2 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Mon, 1 May 2017 15:20:48 +1200 Subject: [PATCH] Inital commit of libotd. --- backend/drm/drm.c | 466 ++++++++++++++++++++++++++++++++++++++++++ backend/drm/drm.h | 54 +++++ backend/drm/event.c | 91 +++++++++ backend/drm/event.h | 21 ++ backend/drm/main.c | 160 +++++++++++++++ backend/drm/otd.c | 71 +++++++ backend/drm/otd.h | 44 ++++ backend/drm/session.c | 224 ++++++++++++++++++++ backend/drm/session.h | 25 +++ backend/drm/udev.c | 158 ++++++++++++++ backend/drm/udev.h | 9 + 11 files changed, 1323 insertions(+) create mode 100644 backend/drm/drm.c create mode 100644 backend/drm/drm.h create mode 100644 backend/drm/event.c create mode 100644 backend/drm/event.h create mode 100644 backend/drm/main.c create mode 100644 backend/drm/otd.c create mode 100644 backend/drm/otd.h create mode 100644 backend/drm/session.c create mode 100644 backend/drm/session.h create mode 100644 backend/drm/udev.c create mode 100644 backend/drm/udev.h diff --git a/backend/drm/drm.c b/backend/drm/drm.c new file mode 100644 index 00000000..bb6359c5 --- /dev/null +++ b/backend/drm/drm.c @@ -0,0 +1,466 @@ +#include "otd.h" +#include "drm.h" +#include "event.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static const char *conn_name[] = { + [DRM_MODE_CONNECTOR_Unknown] = "Unknown", + [DRM_MODE_CONNECTOR_VGA] = "VGA", + [DRM_MODE_CONNECTOR_DVII] = "DVI-I", + [DRM_MODE_CONNECTOR_DVID] = "DVI-D", + [DRM_MODE_CONNECTOR_DVIA] = "DVI-A", + [DRM_MODE_CONNECTOR_Composite] = "Composite", + [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO", + [DRM_MODE_CONNECTOR_LVDS] = "LVDS", + [DRM_MODE_CONNECTOR_Component] = "Component", + [DRM_MODE_CONNECTOR_9PinDIN] = "DIN", + [DRM_MODE_CONNECTOR_DisplayPort] = "DP", + [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A", + [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B", + [DRM_MODE_CONNECTOR_TV] = "TV", + [DRM_MODE_CONNECTOR_eDP] = "eDP", + [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual", + [DRM_MODE_CONNECTOR_DSI] = "DSI", +}; + +// EGL extensions +PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display; +PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC create_platform_window_surface; + +static bool egl_exts() +{ + get_platform_display = (PFNEGLGETPLATFORMDISPLAYEXTPROC) + eglGetProcAddress("eglGetPlatformDisplayEXT"); + + create_platform_window_surface = (PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC) + eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT"); + + return get_platform_display && create_platform_window_surface; +} + +bool egl_get_config(EGLDisplay disp, EGLConfig *out) +{ + EGLint count = 0, matched = 0, ret; + + ret = eglGetConfigs(disp, NULL, 0, &count); + if (ret == EGL_FALSE || count == 0) { + return false; + } + + EGLConfig configs[count]; + + ret = eglChooseConfig(disp, NULL, configs, count, &matched); + if (ret == EGL_FALSE) { + return false; + } + + for (int i = 0; i < matched; ++i) { + EGLint gbm_format; + + if (!eglGetConfigAttrib(disp, + configs[i], + EGL_NATIVE_VISUAL_ID, + &gbm_format)) + continue; + + if (gbm_format == GBM_FORMAT_XRGB8888) { + *out = configs[i]; + return true; + } + } + + return false; +} + +bool init_renderer(struct otd *otd) +{ + if (!egl_exts()) { + fprintf(stderr, "Could not get EGL extensions\n"); + return false; + } + + otd->gbm = gbm_create_device(otd->fd); + if (!otd->gbm) { + fprintf(stderr, "Could not create gbm device: %s\n", strerror(errno)); + return false; + } + + if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE) { + fprintf(stderr, "Could not bind GLES3 API\n"); + goto error_gbm; + } + + otd->egl.disp = get_platform_display(EGL_PLATFORM_GBM_MESA, otd->gbm, NULL); + if (otd->egl.disp == EGL_NO_DISPLAY) { + fprintf(stderr, "Could not create EGL display\n"); + goto error_gbm; + } + + if (eglInitialize(otd->egl.disp, NULL, NULL) == EGL_FALSE) { + fprintf(stderr, "Could not initalise EGL\n"); + goto error_egl; + } + + if (!egl_get_config(otd->egl.disp, &otd->egl.conf)) { + fprintf(stderr, "Could not get EGL config\n"); + goto error_egl; + } + + static const EGLint attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE}; + otd->egl.context = eglCreateContext(otd->egl.disp, otd->egl.conf, EGL_NO_CONTEXT, attribs); + if (otd->egl.context == EGL_NO_CONTEXT) { + fprintf(stderr, "Could not create EGL context\n"); + goto error_egl; + } + + return true; + +error_egl: + eglTerminate(otd->egl.disp); + eglReleaseThread(); + eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); + +error_gbm: + gbm_device_destroy(otd->gbm); + + return false; +} + +void destroy_renderer(struct otd *otd) +{ + if (!otd) + return; + + eglDestroyContext(otd->egl.disp, otd->egl.context); + eglTerminate(otd->egl.disp); + eglReleaseThread(); + eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); + + gbm_device_destroy(otd->gbm); +} + +void scan_connectors(struct otd *otd) +{ + drmModeRes *res = drmModeGetResources(otd->fd); + if (!res) + return; + + // I don't know if this needs to be allocated with realloc like this, + // as it may not even be possible for the number of connectors to change. + // I'll just have to see how DisplayPort MST works with DRM. + if ((size_t)res->count_connectors > otd->display_len) { + struct otd_display *new = realloc(otd->displays, sizeof *new * res->count_connectors); + if (!new) + goto error; + + for (int i = otd->display_len; i < res->count_connectors; ++i) { + new[i] = (struct otd_display) { + .state = OTD_DISP_INVALID, + .otd = otd, + }; + } + + otd->display_len = res->count_connectors; + otd->displays = new; + } + + for (int i = 0; i < res->count_connectors; ++i) { + struct otd_display *disp = &otd->displays[i]; + drmModeConnector *conn = drmModeGetConnector(otd->fd, res->connectors[i]); + if (!conn) + continue; + + if (otd->displays[i].state == OTD_DISP_INVALID) { + disp->state = OTD_DISP_DISCONNECTED; + disp->connector = res->connectors[i]; + snprintf(disp->name, sizeof disp->name, "%s-%"PRIu32, + conn_name[conn->connector_type], + conn->connector_type_id); + } + + if (disp->state == OTD_DISP_DISCONNECTED && + conn->connection == DRM_MODE_CONNECTED) { + disp->state = OTD_DISP_NEEDS_MODESET; + event_add(otd, disp, OTD_EV_DISPLAY_ADD); + + } else if (disp->state == OTD_DISP_CONNECTED && + conn->connection != DRM_MODE_CONNECTED) { + disp->state = OTD_DISP_DISCONNECTED; + event_add(otd, disp, OTD_EV_DISPLAY_REM); + } + + drmModeFreeConnector(conn); + } + +error: + drmModeFreeResources(res); +} + +static void free_fb(struct gbm_bo *bo, void *data) +{ + uint32_t *id = data; + + if (id && *id) + drmModeRmFB(gbm_bo_get_fd(bo), *id); + + free(id); +} + +static uint32_t get_fb_for_bo(int fd, struct gbm_bo *bo) +{ + uint32_t *id = gbm_bo_get_user_data(bo); + + if (id) + return *id; + + id = calloc(1, sizeof *id); + if (!id) + return 0; + + drmModeAddFB(fd, gbm_bo_get_width(bo), gbm_bo_get_height(bo), 24, 32, + gbm_bo_get_stride(bo), gbm_bo_get_handle(bo).u32, id); + + gbm_bo_set_user_data(bo, id, free_fb); + + return *id; +} + +static void init_display_renderer(struct otd *otd, struct otd_display *disp) +{ + disp->gbm = gbm_surface_create(otd->gbm, + disp->width, disp->height, + GBM_FORMAT_XRGB8888, + GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING); + if (!disp->gbm) + return; + + disp->egl = create_platform_window_surface(otd->egl.disp, otd->egl.conf, + disp->gbm, NULL); + if (disp->egl == EGL_NO_SURFACE) + return; + + // Render black frame + + eglMakeCurrent(otd->egl.disp, disp->egl, disp->egl, otd->egl.context); + + glViewport(0, 0, disp->width, disp->height); + glClearColor(0.0, 0.0, 0.0, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + + eglSwapBuffers(otd->egl.disp, disp->egl); + + struct gbm_bo *bo = gbm_surface_lock_front_buffer(disp->gbm); + uint32_t fb_id = get_fb_for_bo(otd->fd, bo); + + drmModeSetCrtc(otd->fd, disp->crtc, fb_id, 0, 0, + &disp->connector, 1, disp->active_mode); + drmModePageFlip(otd->fd, disp->crtc, fb_id, DRM_MODE_PAGE_FLIP_EVENT, disp); + + gbm_surface_release_buffer(disp->gbm, bo); +} + +static drmModeModeInfo *select_mode(size_t num_modes, + drmModeModeInfo modes[static num_modes], + drmModeCrtc *old_crtc, + const char *str) +{ + if (strcmp(str, "preferred") == 0) + return &modes[0]; + + if (strcmp(str, "current") == 0) { + if (!old_crtc) { + fprintf(stderr, "Display does not have currently configured mode\n"); + return NULL; + } + + for (size_t i = 0; i < num_modes; ++i) { + if (memcmp(&modes[i], &old_crtc->mode, sizeof modes[0]) == 0) + return &modes[i]; + } + + // We should never get here + assert(0); + } + + unsigned width = 0; + unsigned height = 0; + unsigned rate = 0; + int ret; + + if ((ret = sscanf(str, "%ux%u@%u", &width, &height, &rate)) != 2 && ret != 3) { + fprintf(stderr, "Invalid modesetting string\n"); + return NULL; + } + + for (size_t i = 0; i < num_modes; ++i) { + if (modes[i].hdisplay == width && + modes[i].vdisplay == height && + (!rate || modes[i].vrefresh == rate)) + return &modes[i]; + } + + return NULL; +} + +bool modeset_str(struct otd *otd, struct otd_display *disp, const char *str) +{ + drmModeConnector *conn = drmModeGetConnector(otd->fd, disp->connector); + if (!conn || conn->connection != DRM_MODE_CONNECTED || conn->count_modes == 0) + goto error; + + disp->num_modes = conn->count_modes; + disp->modes = malloc(sizeof *disp->modes * disp->num_modes); + if (!disp->modes) + goto error; + memcpy(disp->modes, conn->modes, sizeof *disp->modes * disp->num_modes); + + drmModeEncoder *curr_enc = drmModeGetEncoder(otd->fd, conn->encoder_id); + if (curr_enc) { + disp->old_crtc = drmModeGetCrtc(otd->fd, curr_enc->crtc_id); + free(curr_enc); + } + + disp->active_mode = select_mode(disp->num_modes, disp->modes, disp->old_crtc, str); + if (!disp->active_mode) { + fprintf(stderr, "Could not find mode '%s' for %s\n", str, disp->name); + goto error; + } + + fprintf(stderr, "Configuring %s with mode %ux%u@%u\n", + disp->name, disp->active_mode->hdisplay, disp->active_mode->vdisplay, + disp->active_mode->vrefresh); + + drmModeRes *res = drmModeGetResources(otd->fd); + if (!res) + goto error; + + bool success = false; + for (int i = 0; !success && i < conn->count_encoders; ++i) { + drmModeEncoder *enc = drmModeGetEncoder(otd->fd, conn->encoders[i]); + if (!enc) + continue; + + for (int j = 0; j < res->count_crtcs; ++j) { + if ((enc->possible_crtcs & (1 << j)) == 0) + continue; + + if ((otd->taken_crtcs & (1 << j)) == 0) { + otd->taken_crtcs |= 1 << j; + disp->crtc = res->crtcs[j]; + + success = true; + break; + } + } + + drmModeFreeEncoder(enc); + } + + drmModeFreeResources(res); + + if (!success) + goto error; + + disp->state = OTD_DISP_CONNECTED; + drmModeFreeConnector(conn); + + disp->width = disp->active_mode->hdisplay; + disp->height = disp->active_mode->vdisplay; + + init_display_renderer(otd, disp); + + return true; +error: + disp->state = OTD_DISP_DISCONNECTED; + drmModeFreeConnector(conn); + + event_add(otd, disp, OTD_EV_DISPLAY_REM); + + return false; +} + + +void page_flip_handler(int fd, + unsigned seq, + unsigned tv_sec, + unsigned tv_usec, + void *user) +{ + struct otd_display *disp = user; + + disp->pageflip_pending = true; + if (!disp->cleanup) + event_add(disp->otd, disp, OTD_EV_RENDER); +} + +void destroy_display_renderer(struct otd *otd, struct otd_display *disp) +{ + if (!otd || !disp || disp->state != OTD_DISP_CONNECTED) + return; + + drmModeCrtc *crtc = disp->old_crtc; + if (crtc) { + drmEventContext event = { + .version = DRM_EVENT_CONTEXT_VERSION, + .page_flip_handler = page_flip_handler, + }; + + disp->cleanup = true; + while (disp->pageflip_pending) + drmHandleEvent(otd->fd, &event); + + drmModeSetCrtc(otd->fd, crtc->crtc_id, crtc->buffer_id, + crtc->x, crtc->y, &disp->connector, + 1, &crtc->mode); + drmModeFreeCrtc(crtc); + } + + eglDestroySurface(otd->egl.disp, disp->egl); + gbm_surface_destroy(disp->gbm); + + free(disp->modes); +} + +void get_drm_event(struct otd *otd) +{ + drmEventContext event = { + .version = DRM_EVENT_CONTEXT_VERSION, + .page_flip_handler = page_flip_handler, + }; + + drmHandleEvent(otd->fd, &event); +} + +void rendering_begin(struct otd_display *disp) +{ + struct otd *otd = disp->otd; + eglMakeCurrent(otd->egl.disp, disp->egl, disp->egl, otd->egl.context); +} + +void rendering_end(struct otd_display *disp) +{ + struct otd *otd = disp->otd; + eglSwapBuffers(otd->egl.disp, disp->egl); + + struct gbm_bo *bo = gbm_surface_lock_front_buffer(disp->gbm); + uint32_t fb_id = get_fb_for_bo(otd->fd, bo); + + drmModePageFlip(otd->fd, disp->crtc, fb_id, DRM_MODE_PAGE_FLIP_EVENT, disp); + + gbm_surface_release_buffer(disp->gbm, bo); + + disp->pageflip_pending = false; +} diff --git a/backend/drm/drm.h b/backend/drm/drm.h new file mode 100644 index 00000000..8744b440 --- /dev/null +++ b/backend/drm/drm.h @@ -0,0 +1,54 @@ +#ifndef DRM_H +#define DRM_H + +#include +#include +#include +#include +#include + +enum otd_display_state { + OTD_DISP_INVALID, + OTD_DISP_DISCONNECTED, + OTD_DISP_NEEDS_MODESET, + OTD_DISP_CONNECTED, +}; + +struct otd_display { + struct otd *otd; + + enum otd_display_state state; + uint32_t connector; + char name[16]; + + size_t num_modes; + drmModeModeInfo *modes; + drmModeModeInfo *active_mode; + + uint32_t width; + uint32_t height; + + uint32_t crtc; + drmModeCrtc *old_crtc; + + struct gbm_surface *gbm; + EGLSurface *egl; + uint32_t fb_id; + + bool pageflip_pending; + bool cleanup; +}; + +bool init_renderer(struct otd *otd); +void destroy_renderer(struct otd *otd); + +void scan_connectors(struct otd *otd); +bool modeset_str(struct otd *otd, struct otd_display *disp, const char *str); +void destroy_display_renderer(struct otd *otd, struct otd_display *disp); + +void get_drm_event(struct otd *otd); + +void rendering_begin(struct otd_display *disp); +void rendering_end(struct otd_display *disp); + +#endif diff --git a/backend/drm/event.c b/backend/drm/event.c new file mode 100644 index 00000000..f20cb282 --- /dev/null +++ b/backend/drm/event.c @@ -0,0 +1,91 @@ +#include "otd.h" +#include "event.h" +#include "drm.h" +#include "udev.h" + +#include +#include +#include + +static inline void event_swap(struct otd_event *a, struct otd_event *b) +{ + struct otd_event tmp = *a; + *a = *b; + *b = tmp; +} + +bool otd_get_event(struct otd *otd, struct otd_event *restrict ret) +{ + struct pollfd fds[] = { + { .fd = otd->fd, .events = POLLIN }, + { .fd = otd->udev_fd, .events = POLLIN }, + }; + + while (poll(fds, 2, 0) > 0) { + if (fds[0].revents) + get_drm_event(otd); + if (fds[1].revents) + otd_udev_event(otd); + } + + if (otd->event_len == 0) { + ret->type = OTD_EV_NONE; + ret->display = NULL; + return false; + } + + struct otd_event *ev = otd->events; + + // Downheap + *ret = ev[0]; + ev[0] = ev[--otd->event_len]; + + size_t i = 0; + while (i < otd->event_len / 2) { + size_t left = i * 2 + 1; + size_t right = i * 2 + 2; + size_t max = (ev[left].type > ev[right].type) ? left : right; + + if (ev[i].type <= ev[max].type) { + event_swap(&ev[i], &ev[max]); + i = max; + } else { + break; + } + } + + return true; +} + +bool event_add(struct otd *otd, struct otd_display *disp, enum otd_event_type type) +{ + if (type == OTD_EV_NONE) + return true; + + if (otd->event_len == otd->event_cap) { + size_t new_size = (otd->event_cap == 0) ? 8 : otd->event_cap * 2; + + struct otd_event *new = realloc(otd->events, sizeof *new * new_size); + if (!new) { + return false; + } + + otd->event_cap = new_size; + otd->events = new; + } + + struct otd_event *ev = otd->events; + + // Upheap + size_t i = otd->event_len++; + ev[i].type = type; + ev[i].display = disp; + + size_t j; + while (i > 0 && ev[i].type > ev[(j = (i - 1) / 2)].type) { + event_swap(&ev[i], &ev[j]); + i = j; + } + + return true; +} diff --git a/backend/drm/event.h b/backend/drm/event.h new file mode 100644 index 00000000..fc4f6f9a --- /dev/null +++ b/backend/drm/event.h @@ -0,0 +1,21 @@ +#ifndef EVENT_H +#define EVENT_H + +#include + +enum otd_event_type { + OTD_EV_NONE, + OTD_EV_RENDER, + OTD_EV_DISPLAY_REM, + OTD_EV_DISPLAY_ADD, +}; + +struct otd_event { + enum otd_event_type type; + struct otd_display *display; +}; + +bool otd_get_event(struct otd *otd, struct otd_event *restrict ret); +bool event_add(struct otd *otd, struct otd_display *disp, enum otd_event_type type); + +#endif diff --git a/backend/drm/main.c b/backend/drm/main.c new file mode 100644 index 00000000..aab11e19 --- /dev/null +++ b/backend/drm/main.c @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "otd.h" +#include "drm.h" +#include "event.h" + +// I know this is pretty crappy, +// but it's just for the example's sake +struct { + char *name; + char *mode; +} displays[8]; +int num_displays = 0; + +noreturn void usage(const char *name, int ret) +{ + fprintf(stderr, "usage: %s [-o [-m ]]*\n" + "\n" + " -h \tDisplay this help text\n" + " -o \tWhich diplay to use. e.g. DVI-I-1.\n" + " -m \tWhich mode to use. It must come after an -o option.\n" + " \tMust be either 'preferred', 'current', widthxheight\n" + " \tor widthxheight@rate. Defaults to 'preferred'.\n" + "\n" + "example: %s -o DVI-I-1 -m 1920x1080@60 -o DP-1 -m 1920x1200\n", + name, name); + + exit(ret); +} + +void parse_args(int argc, char *argv[]) +{ + int c; + int i = -1; + + while ((c = getopt(argc, argv, "ho:m:")) != -1) { + switch (c) { + case 'h': + usage(argv[0], 0); + case 'o': + i = num_displays++; + + if (num_displays == 8) { + fprintf(stderr, "Too many displays\n"); + exit(1); + } + + displays[i].name = strdup(optarg); + break; + case 'm': + if (i == -1) { + fprintf(stderr, "A display is required first\n"); + exit(1); + } + + fprintf(stderr, "Assigned '%s' to '%s'\n", optarg, displays[i].name); + displays[i].mode = optarg; + i = -1; + break; + default: + usage(argv[0], 1); + } + } + + // Trailing args + if (optind != argc) { + usage(argv[0], 1); + } +} + +int main(int argc, char *argv[]) +{ + parse_args(argc, argv); + + struct otd *otd = otd_start(); + if (!otd) + return 1; + + float colour[3] = {1.0, 0.0, 0.0}; + int dec = 0; + + struct timespec start, now; + clock_gettime(CLOCK_MONOTONIC, &start); + struct timespec last = start; + + while (clock_gettime(CLOCK_MONOTONIC, &now) == 0 && + now.tv_sec < start.tv_sec + 10) { + + struct otd_event ev; + if (!otd_get_event(otd, &ev)) + continue; + + struct otd_display *disp = ev.display; + + switch (ev.type) { + case OTD_EV_RENDER: + rendering_begin(disp); + + // This is all just calculating the colours. + // It's not particularly important. + + long ms = (now.tv_sec - last.tv_sec) * 1000 + + (now.tv_nsec - last.tv_nsec) / 1000000; + + int inc = (dec + 1) % 3; + + colour[dec] -= ms / 2000.0f; + colour[inc] += ms / 2000.0f; + + if (colour[dec] < 0.0f) { + colour[dec] = 0.0f; + colour[inc] = 1.0f; + + dec = (dec + 1) % 3; + } + + last = now; + + glViewport(0, 0, disp->width, disp->height); + glClearColor(colour[0], colour[1], colour[2], 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + + rendering_end(disp); + break; + case OTD_EV_DISPLAY_REM: + printf("%s removed\n", disp->name); + break; + case OTD_EV_DISPLAY_ADD: + printf("%s added\n", disp->name); + + const char *mode = NULL; + for (int i = 0; i < num_displays; ++i) { + if (strcmp(disp->name, displays[i].name) == 0) { + mode = displays[i].mode; + } + } + if (!mode) + mode = "preferred"; + + if (!modeset_str(otd, ev.display, mode)) { + fprintf(stderr, "Modesetting %s failed\n", disp->name); + goto out; + } + break; + default: + break; + } + } + +out: + otd_finish(otd); +} diff --git a/backend/drm/otd.c b/backend/drm/otd.c new file mode 100644 index 00000000..3d5345c7 --- /dev/null +++ b/backend/drm/otd.c @@ -0,0 +1,71 @@ +#include +#include +#include + +#include "otd.h" +#include "drm.h" +#include "event.h" +#include "session.h" +#include "udev.h" + +struct otd *otd_start(void) +{ + struct otd *otd = calloc(1, sizeof *otd); + if (!otd) + return NULL; + + if (!otd_new_session(otd)) { + fprintf(stderr, "Could not create session\n"); + goto error; + } + + if (!otd_udev_start(otd)) { + fprintf(stderr, "Could not start udev\n"); + goto error_session; + } + + otd_udev_find_gpu(otd); + if (otd->fd == -1) { + fprintf(stderr, "Could not open GPU\n"); + goto error_udev; + } + + if (!init_renderer(otd)) { + fprintf(stderr, "Could not initalise renderer\n"); + goto error_fd; + } + + scan_connectors(otd); + + return otd; + +error_fd: + release_device(otd, otd->fd); +error_udev: + otd_udev_finish(otd); +error_session: + otd_close_session(otd); +error: + free(otd); + return NULL; +} + +void otd_finish(struct otd *otd) +{ + if (!otd) + return; + + for (size_t i = 0; i < otd->display_len; ++i) { + destroy_display_renderer(otd, &otd->displays[i]); + } + + destroy_renderer(otd); + otd_close_session(otd); + otd_udev_finish(otd); + + close(otd->fd); + free(otd->events); + free(otd->displays); + free(otd); +} + diff --git a/backend/drm/otd.h b/backend/drm/otd.h new file mode 100644 index 00000000..eef0e991 --- /dev/null +++ b/backend/drm/otd.h @@ -0,0 +1,44 @@ +#ifndef LIBOTD_H +#define LIBOTD_H + +#include +#include +#include +#include +#include + +#include "session.h" + +struct otd { + int fd; + bool paused; + + // Priority Queue (Max-heap) + size_t event_cap; + size_t event_len; + struct otd_event *events; + + size_t display_len; + struct otd_display *displays; + + uint32_t taken_crtcs; + + struct gbm_device *gbm; + struct { + EGLDisplay disp; + EGLConfig conf; + EGLContext context; + } egl; + + struct otd_session session; + + struct udev *udev; + struct udev_monitor *mon; + int udev_fd; + char *drm_path; +}; + +struct otd *otd_start(void); +void otd_finish(struct otd *otd); + +#endif diff --git a/backend/drm/session.c b/backend/drm/session.c new file mode 100644 index 00000000..c845fb27 --- /dev/null +++ b/backend/drm/session.c @@ -0,0 +1,224 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "session.h" +#include "otd.h" + +int take_device(struct otd *restrict otd, + const char *restrict path, + bool *restrict paused_out) +{ + int ret; + int fd = -1; + sd_bus_message *msg = NULL; + sd_bus_error error = SD_BUS_ERROR_NULL; + struct otd_session *s = &otd->session; + + struct stat st; + if (stat(path, &st) < 0) { + fprintf(stderr, "Failed to stat '%s'\n", path); + return -1; + } + + ret = sd_bus_call_method(s->bus, + "org.freedesktop.login1", + s->path, + "org.freedesktop.login1.Session", + "TakeDevice", + &error, &msg, + "uu", major(st.st_rdev), minor(st.st_rdev)); + if (ret < 0) { + fprintf(stderr, "%s\n", error.message); + goto error; + } + + int paused = 0; + ret = sd_bus_message_read(msg, "hb", &fd, &paused); + if (ret < 0) { + fprintf(stderr, "%s\n", strerror(-ret)); + 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); + + if (paused_out) + *paused_out = paused; + +error: + sd_bus_error_free(&error); + sd_bus_message_unref(msg); + return fd; +} + +void release_device(struct otd *otd, int fd) +{ + int ret; + sd_bus_message *msg = NULL; + sd_bus_error error = SD_BUS_ERROR_NULL; + struct otd_session *s = &otd->session; + + struct stat st; + if (fstat(fd, &st) < 0) { + fprintf(stderr, "Could not stat fd %d\n", fd); + return; + } + + ret = sd_bus_call_method(s->bus, + "org.freedesktop.login1", + s->path, + "org.freedesktop.login1.Session", + "ReleaseDevice", + &error, &msg, + "uu", major(st.st_rdev), minor(st.st_rdev)); + if (ret < 0) { + /* Log something */; + } + + sd_bus_error_free(&error); + sd_bus_message_unref(msg); +} + +static bool session_activate(struct otd *otd) +{ + int ret; + sd_bus_message *msg = NULL; + sd_bus_error error = SD_BUS_ERROR_NULL; + struct otd_session *s = &otd->session; + + ret = sd_bus_call_method(s->bus, + "org.freedesktop.login1", + s->path, + "org.freedesktop.login1.Session", + "Activate", + &error, &msg, + ""); + if (ret < 0) { + fprintf(stderr, "%s\n", error.message); + } + + sd_bus_error_free(&error); + sd_bus_message_unref(msg); + return ret >= 0; +} + +static bool take_control(struct otd *otd) +{ + int ret; + sd_bus_message *msg = NULL; + sd_bus_error error = SD_BUS_ERROR_NULL; + struct otd_session *s = &otd->session; + + ret = sd_bus_call_method(s->bus, + "org.freedesktop.login1", + s->path, + "org.freedesktop.login1.Session", + "TakeControl", + &error, &msg, + "b", false); + if (ret < 0) { + /* Log something */; + } + + sd_bus_error_free(&error); + sd_bus_message_unref(msg); + return ret >= 0; +} + +static void release_control(struct otd *otd) +{ + int ret; + sd_bus_message *msg = NULL; + sd_bus_error error = SD_BUS_ERROR_NULL; + struct otd_session *s = &otd->session; + + ret = sd_bus_call_method(s->bus, + "org.freedesktop.login1", + s->path, + "org.freedesktop.login1.Session", + "ReleaseControl", + &error, &msg, + ""); + if (ret < 0) { + /* Log something */; + } + + sd_bus_error_free(&error); + sd_bus_message_unref(msg); +} + +void otd_close_session(struct otd *otd) +{ + release_device(otd, otd->fd); + release_control(otd); + + sd_bus_unref(otd->session.bus); + free(otd->session.id); + free(otd->session.path); + free(otd->session.seat); +} + +bool otd_new_session(struct otd *otd) +{ + int ret; + struct otd_session *s = &otd->session; + + ret = sd_pid_get_session(getpid(), &s->id); + if (ret < 0) { + fprintf(stderr, "Could not get session\n"); + goto error; + } + + ret = sd_session_get_seat(s->id, &s->seat); + if (ret < 0) { + fprintf(stderr, "Could not get seat\n"); + 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"; + int len = snprintf(NULL, 0, fmt, s->id); + + s->path = malloc(len + 1); + if (!s->path) + goto error; + + sprintf(s->path, fmt, s->id); + + ret = sd_bus_open_system(&s->bus); + if (ret < 0) { + fprintf(stderr, "Could not open bus\n"); + goto error; + } + + if (!session_activate(otd)) { + fprintf(stderr, "Could not activate session\n"); + goto error_bus; + } + + if (!take_control(otd)) { + fprintf(stderr, "Could not take control of session\n"); + goto error_bus; + } + + return true; + +error_bus: + sd_bus_unref(s->bus); + +error: + free(s->path); + free(s->id); + free(s->seat); + return false; +} diff --git a/backend/drm/session.h b/backend/drm/session.h new file mode 100644 index 00000000..c21900e9 --- /dev/null +++ b/backend/drm/session.h @@ -0,0 +1,25 @@ +#ifndef SESSION_H +#define SESSION_H + +#include +#include + +struct otd_session { + char *id; + char *path; + char *seat; + + sd_bus *bus; +}; + +struct otd; +bool otd_new_session(struct otd *otd); +void otd_close_session(struct otd *otd); + +int take_device(struct otd *restrict otd, + const char *restrict path, + bool *restrict paused_out); + +void release_device(struct otd *otd, int fd); + +#endif diff --git a/backend/drm/udev.c b/backend/drm/udev.c new file mode 100644 index 00000000..1efa26c2 --- /dev/null +++ b/backend/drm/udev.c @@ -0,0 +1,158 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "otd.h" +#include "udev.h" +#include "session.h" +#include "drm.h" + +static bool device_is_kms(struct otd *otd, struct udev_device *dev) +{ + const char *path = udev_device_get_devnode(dev); + int fd; + + if (!path) + return false; + + fd = take_device(otd, path, &otd->paused); + if (fd < 0) + return false; + + drmModeRes *res = drmModeGetResources(fd); + if (!res) + goto out_fd; + + if (res->count_crtcs <= 0 || res->count_connectors <= 0 || + res->count_encoders <= 0) + goto out_res; + + if (otd->fd >= 0) { + release_device(otd, otd->fd); + free(otd->drm_path); + } + + otd->fd = fd; + otd->drm_path = strdup(path); + + drmModeFreeResources(res); + return true; + +out_res: + drmModeFreeResources(res); +out_fd: + release_device(otd, fd); + return false; +} + +void otd_udev_find_gpu(struct otd *otd) +{ + struct udev *udev = otd->udev; + otd->fd = -1; + + struct udev_enumerate *en = udev_enumerate_new(udev); + if (!en) + return; + + udev_enumerate_add_match_subsystem(en, "drm"); + udev_enumerate_add_match_sysname(en, "card[0-9]*"); + + udev_enumerate_scan_devices(en); + struct udev_list_entry *entry; + udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(en)) { + bool is_boot_vga = false; + + const char *path = udev_list_entry_get_name(entry); + struct udev_device *dev = udev_device_new_from_syspath(udev, path); + if (!dev) + continue; + + const char *seat = udev_device_get_property_value(dev, "ID_SEAT"); + if (!seat) + seat = "seat0"; + if (strcmp(otd->session.seat, seat) != 0) { + udev_device_unref(dev); + continue; + } + + 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; + udev_device_unref(pci); + } + + if (!is_boot_vga && otd->fd >= 0) { + udev_device_unref(dev); + continue; + } + + if (!device_is_kms(otd, dev)) { + udev_device_unref(dev); + continue; + } + + if (is_boot_vga) { + break; + } + } + + udev_enumerate_unref(en); +} + +bool otd_udev_start(struct otd *otd) +{ + otd->udev = udev_new(); + if (!otd->udev) + return false; + + otd->mon = udev_monitor_new_from_netlink(otd->udev, "udev"); + if (!otd->mon) { + udev_unref(otd->udev); + return false; + } + + udev_monitor_filter_add_match_subsystem_devtype(otd->mon, "drm", NULL); + udev_monitor_enable_receiving(otd->mon); + + otd->udev_fd = udev_monitor_get_fd(otd->mon); + + return true; +} + +void otd_udev_finish(struct otd *otd) +{ + if (!otd) + return; + + udev_monitor_unref(otd->mon); + udev_unref(otd->udev); +} + +void otd_udev_event(struct otd *otd) +{ + struct udev_device *dev = udev_monitor_receive_device(otd->mon); + if (!dev) + return; + + const char *path = udev_device_get_devnode(dev); + if (!path || strcmp(path, otd->drm_path) != 0) + goto out; + + const char *action = udev_device_get_action(dev); + if (!action || strcmp(action, "change") != 0) + goto out; + + scan_connectors(otd); + +out: + udev_device_unref(dev); +} diff --git a/backend/drm/udev.h b/backend/drm/udev.h new file mode 100644 index 00000000..9bd4c0d0 --- /dev/null +++ b/backend/drm/udev.h @@ -0,0 +1,9 @@ +#ifndef UDEV_H +#define UDEV_H + +bool otd_udev_start(struct otd *otd); +void otd_udev_finish(struct otd *otd); +void otd_udev_find_gpu(struct otd *otd); +void otd_udev_event(struct otd *otd); + +#endif