wlroots/backend/drm/backend.c

114 lines
2.7 KiB
C
Raw Normal View History

2017-05-01 05:49:18 +00:00
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
2017-05-02 01:00:25 +00:00
#include <string.h>
#include <errno.h>
2017-05-03 09:28:44 +00:00
#include <wayland-server.h>
2017-05-01 05:49:18 +00:00
#include <wlr/session.h>
2017-05-02 02:34:33 +00:00
#include <wlr/common/list.h>
2017-05-01 05:49:18 +00:00
#include "backend/drm/backend.h"
#include "backend/drm/drm.h"
#include "backend/drm/udev.h"
2017-05-02 01:00:25 +00:00
#include "common/log.h"
2017-05-01 05:49:18 +00:00
2017-05-03 09:28:44 +00:00
struct wlr_drm_backend *wlr_drm_backend_init(struct wl_display *display,
struct wlr_session *session, struct wl_listener *add, struct wl_listener *rem,
struct wl_listener *render) {
2017-05-01 05:49:18 +00:00
struct wlr_drm_backend *backend = calloc(1, sizeof *backend);
2017-05-02 01:00:25 +00:00
if (!backend) {
wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno));
2017-05-01 05:49:18 +00:00
return NULL;
2017-05-02 01:00:25 +00:00
}
2017-05-01 05:49:18 +00:00
backend->session = session;
backend->outputs = list_create();
if (!backend->outputs) {
2017-05-02 02:34:33 +00:00
wlr_log(L_ERROR, "Failed to allocate list");
goto error_backend;
}
2017-05-03 09:28:44 +00:00
if (!wlr_udev_init(display, &backend->udev)) {
2017-05-02 01:00:25 +00:00
wlr_log(L_ERROR, "Failed to start udev");
2017-05-03 09:28:44 +00:00
goto error_list;
2017-05-01 05:49:18 +00:00
}
backend->fd = wlr_udev_find_gpu(&backend->udev, backend->session);
2017-05-01 05:49:18 +00:00
if (backend->fd == -1) {
2017-05-02 01:00:25 +00:00
wlr_log(L_ERROR, "Failed to open DRM device");
2017-05-01 05:49:18 +00:00
goto error_udev;
}
2017-05-03 09:28:44 +00:00
struct wl_event_loop *event_loop = wl_display_get_event_loop(display);
backend->drm_event = wl_event_loop_add_fd(event_loop, backend->fd,
WL_EVENT_READABLE, wlr_drm_event, NULL);
if (!backend->drm_event) {
wlr_log(L_ERROR, "Failed to create DRM event source");
2017-05-01 05:49:18 +00:00
goto error_fd;
}
2017-05-03 09:28:44 +00:00
if (!wlr_drm_renderer_init(&backend->renderer, backend->fd)) {
wlr_log(L_ERROR, "Failed to initialize renderer");
goto error_event;
}
wl_signal_init(&backend->signals.output_add);
wl_signal_init(&backend->signals.output_rem);
wl_signal_init(&backend->signals.output_render);
2017-05-03 09:28:44 +00:00
if (add) {
wl_signal_add(&backend->signals.output_add, add);
2017-05-03 09:28:44 +00:00
}
if (rem) {
wl_signal_add(&backend->signals.output_rem, rem);
2017-05-03 09:28:44 +00:00
}
if (render) {
wl_signal_add(&backend->signals.output_render, render);
2017-05-03 09:28:44 +00:00
}
2017-05-02 06:13:17 +00:00
2017-05-01 05:49:18 +00:00
wlr_drm_scan_connectors(backend);
return backend;
2017-05-03 09:28:44 +00:00
error_event:
wl_event_source_remove(backend->drm_event);
2017-05-01 05:49:18 +00:00
error_fd:
wlr_session_close_file(backend->session, backend->fd);
2017-05-01 05:49:18 +00:00
error_udev:
wlr_udev_free(&backend->udev);
2017-05-02 02:34:33 +00:00
error_list:
list_free(backend->outputs);
error_backend:
2017-05-01 05:49:18 +00:00
free(backend);
return NULL;
}
static void free_output(void *item)
2017-05-02 02:34:33 +00:00
{
struct wlr_drm_output *out = item;
2017-05-03 10:40:19 +00:00
wlr_drm_output_cleanup(out, true);
free(out);
2017-05-02 02:34:33 +00:00
}
2017-05-01 05:49:18 +00:00
void wlr_drm_backend_free(struct wlr_drm_backend *backend)
{
if (!backend)
return;
list_foreach(backend->outputs, free_output);
2017-05-01 05:49:18 +00:00
wlr_drm_renderer_free(&backend->renderer);
wlr_udev_free(&backend->udev);
wlr_session_close_file(backend->session, backend->fd);
wlr_session_finish(backend->session);
2017-05-01 05:49:18 +00:00
2017-05-03 09:28:44 +00:00
wl_event_source_remove(backend->drm_event);
list_free(backend->outputs);
2017-05-01 05:49:18 +00:00
free(backend);
}