2020-12-15 19:52:53 +00:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2017-04-25 19:06:58 +00:00
|
|
|
#include <assert.h>
|
2020-12-15 19:52:53 +00:00
|
|
|
#include <fcntl.h>
|
2018-02-12 20:29:23 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
2021-12-08 00:43:15 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/types.h>
|
2020-11-26 10:40:23 +00:00
|
|
|
#include <unistd.h>
|
2018-11-11 05:14:48 +00:00
|
|
|
|
2019-09-24 12:33:15 +00:00
|
|
|
#include <drm_fourcc.h>
|
2019-07-27 08:53:54 +00:00
|
|
|
#include <wayland-server-core.h>
|
2021-01-04 09:51:34 +00:00
|
|
|
#include <xf86drm.h>
|
2018-11-11 05:14:48 +00:00
|
|
|
|
2017-06-19 15:46:50 +00:00
|
|
|
#include <wlr/backend/interface.h>
|
2018-02-12 20:29:23 +00:00
|
|
|
#include <wlr/interfaces/wlr_output.h>
|
2017-06-21 16:10:07 +00:00
|
|
|
#include <wlr/util/log.h>
|
2018-11-11 05:14:48 +00:00
|
|
|
|
2017-04-25 15:32:52 +00:00
|
|
|
#include "backend/wayland.h"
|
2020-11-26 10:40:23 +00:00
|
|
|
#include "render/drm_format_set.h"
|
2021-02-09 22:29:44 +00:00
|
|
|
#include "render/pixel_format.h"
|
2018-02-12 20:29:23 +00:00
|
|
|
#include "util/signal.h"
|
2020-11-26 10:40:23 +00:00
|
|
|
|
2021-01-04 09:51:34 +00:00
|
|
|
#include "drm-client-protocol.h"
|
2019-09-24 12:33:15 +00:00
|
|
|
#include "linux-dmabuf-unstable-v1-client-protocol.h"
|
2019-06-20 22:48:19 +00:00
|
|
|
#include "pointer-gestures-unstable-v1-client-protocol.h"
|
2019-11-16 20:05:46 +00:00
|
|
|
#include "presentation-time-client-protocol.h"
|
2021-10-26 06:31:48 +00:00
|
|
|
#include "xdg-activation-v1-client-protocol.h"
|
2019-09-24 12:33:15 +00:00
|
|
|
#include "xdg-decoration-unstable-v1-client-protocol.h"
|
2019-01-10 14:17:14 +00:00
|
|
|
#include "xdg-shell-client-protocol.h"
|
2019-05-10 06:36:19 +00:00
|
|
|
#include "tablet-unstable-v2-client-protocol.h"
|
2019-11-12 14:32:08 +00:00
|
|
|
#include "relative-pointer-unstable-v1-client-protocol.h"
|
2019-05-10 06:36:19 +00:00
|
|
|
|
2021-12-08 00:43:15 +00:00
|
|
|
struct wlr_wl_linux_dmabuf_feedback_v1 {
|
|
|
|
struct wlr_wl_backend *backend;
|
|
|
|
dev_t main_device_id;
|
|
|
|
struct wlr_wl_linux_dmabuf_v1_table_entry *format_table;
|
|
|
|
size_t format_table_size;
|
|
|
|
|
|
|
|
dev_t tranche_target_device_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct wlr_wl_linux_dmabuf_v1_table_entry {
|
|
|
|
uint32_t format;
|
|
|
|
uint32_t pad; /* unused */
|
|
|
|
uint64_t modifier;
|
|
|
|
};
|
|
|
|
|
2018-11-11 05:09:04 +00:00
|
|
|
struct wlr_wl_backend *get_wl_backend_from_backend(struct wlr_backend *backend) {
|
|
|
|
assert(wlr_backend_is_wl(backend));
|
|
|
|
return (struct wlr_wl_backend *)backend;
|
2018-09-17 20:10:34 +00:00
|
|
|
}
|
|
|
|
|
2017-06-20 16:27:05 +00:00
|
|
|
static int dispatch_events(int fd, uint32_t mask, void *data) {
|
2018-11-11 05:09:04 +00:00
|
|
|
struct wlr_wl_backend *wl = data;
|
2017-11-16 08:33:47 +00:00
|
|
|
|
|
|
|
if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
|
2020-01-09 12:52:36 +00:00
|
|
|
if (mask & WL_EVENT_ERROR) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to read from remote Wayland display");
|
|
|
|
}
|
2018-11-11 05:09:04 +00:00
|
|
|
wl_display_terminate(wl->local_display);
|
2017-11-16 08:33:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-09 12:52:36 +00:00
|
|
|
int count = 0;
|
2017-06-20 21:51:45 +00:00
|
|
|
if (mask & WL_EVENT_READABLE) {
|
2020-01-09 12:52:36 +00:00
|
|
|
count = wl_display_dispatch(wl->remote_display);
|
2018-11-11 20:11:15 +00:00
|
|
|
}
|
|
|
|
if (mask & WL_EVENT_WRITABLE) {
|
|
|
|
wl_display_flush(wl->remote_display);
|
2017-06-20 21:51:45 +00:00
|
|
|
}
|
2017-06-20 16:27:05 +00:00
|
|
|
if (mask == 0) {
|
2020-01-09 12:52:36 +00:00
|
|
|
count = wl_display_dispatch_pending(wl->remote_display);
|
2018-11-11 05:09:04 +00:00
|
|
|
wl_display_flush(wl->remote_display);
|
2017-06-20 16:27:05 +00:00
|
|
|
}
|
2018-11-11 20:11:15 +00:00
|
|
|
|
2020-01-09 12:52:36 +00:00
|
|
|
if (count < 0) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to dispatch remote Wayland display");
|
|
|
|
wl_display_terminate(wl->local_display);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return count;
|
2017-06-20 16:27:05 +00:00
|
|
|
}
|
|
|
|
|
2019-01-10 14:17:14 +00:00
|
|
|
static void xdg_wm_base_handle_ping(void *data,
|
|
|
|
struct xdg_wm_base *base, uint32_t serial) {
|
|
|
|
xdg_wm_base_pong(base, serial);
|
2018-11-11 03:47:30 +00:00
|
|
|
}
|
|
|
|
|
2019-01-10 14:17:14 +00:00
|
|
|
static const struct xdg_wm_base_listener xdg_wm_base_listener = {
|
|
|
|
xdg_wm_base_handle_ping,
|
2018-11-11 03:47:30 +00:00
|
|
|
};
|
|
|
|
|
2021-11-22 18:29:30 +00:00
|
|
|
static void presentation_handle_clock_id(void *data,
|
|
|
|
struct wp_presentation *presentation, uint32_t clock) {
|
|
|
|
struct wlr_wl_backend *wl = data;
|
|
|
|
wl->presentation_clock = clock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wp_presentation_listener presentation_listener = {
|
|
|
|
.clock_id = presentation_handle_clock_id,
|
|
|
|
};
|
|
|
|
|
2019-09-24 12:33:15 +00:00
|
|
|
static void linux_dmabuf_v1_handle_format(void *data,
|
|
|
|
struct zwp_linux_dmabuf_v1 *linux_dmabuf_v1, uint32_t format) {
|
|
|
|
// Note, this event is deprecated
|
|
|
|
struct wlr_wl_backend *wl = data;
|
|
|
|
|
|
|
|
wlr_drm_format_set_add(&wl->linux_dmabuf_v1_formats, format,
|
|
|
|
DRM_FORMAT_MOD_INVALID);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void linux_dmabuf_v1_handle_modifier(void *data,
|
|
|
|
struct zwp_linux_dmabuf_v1 *linux_dmabuf_v1, uint32_t format,
|
|
|
|
uint32_t modifier_hi, uint32_t modifier_lo) {
|
|
|
|
struct wlr_wl_backend *wl = data;
|
|
|
|
|
|
|
|
uint64_t modifier = ((uint64_t)modifier_hi << 32) | modifier_lo;
|
|
|
|
wlr_drm_format_set_add(&wl->linux_dmabuf_v1_formats, format, modifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct zwp_linux_dmabuf_v1_listener linux_dmabuf_v1_listener = {
|
|
|
|
.format = linux_dmabuf_v1_handle_format,
|
|
|
|
.modifier = linux_dmabuf_v1_handle_modifier,
|
|
|
|
};
|
|
|
|
|
2021-12-08 00:43:15 +00:00
|
|
|
static void linux_dmabuf_feedback_v1_handle_done(void *data,
|
|
|
|
struct zwp_linux_dmabuf_feedback_v1 *feedback) {
|
|
|
|
// This space is intentionally left blank
|
|
|
|
}
|
|
|
|
|
|
|
|
static void linux_dmabuf_feedback_v1_handle_format_table(void *data,
|
|
|
|
struct zwp_linux_dmabuf_feedback_v1 *feedback, int fd, uint32_t size) {
|
|
|
|
struct wlr_wl_linux_dmabuf_feedback_v1 *feedback_data = data;
|
|
|
|
|
|
|
|
feedback_data->format_table = NULL;
|
|
|
|
|
|
|
|
void *table_data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
if (table_data == MAP_FAILED) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "failed to mmap DMA-BUF format table");
|
|
|
|
} else {
|
|
|
|
feedback_data->format_table = table_data;
|
|
|
|
feedback_data->format_table_size = size;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void linux_dmabuf_feedback_v1_handle_main_device(void *data,
|
|
|
|
struct zwp_linux_dmabuf_feedback_v1 *feedback,
|
|
|
|
struct wl_array *dev_id_arr) {
|
|
|
|
struct wlr_wl_linux_dmabuf_feedback_v1 *feedback_data = data;
|
|
|
|
|
|
|
|
dev_t dev_id;
|
|
|
|
assert(dev_id_arr->size == sizeof(dev_id));
|
|
|
|
memcpy(&dev_id, dev_id_arr->data, sizeof(dev_id));
|
|
|
|
|
|
|
|
feedback_data->main_device_id = dev_id;
|
|
|
|
|
|
|
|
drmDevice *device = NULL;
|
|
|
|
if (drmGetDeviceFromDevId(dev_id, 0, &device) != 0) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "drmGetDeviceFromDevId failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *name = NULL;
|
|
|
|
if (device->available_nodes & (1 << DRM_NODE_RENDER)) {
|
|
|
|
name = device->nodes[DRM_NODE_RENDER];
|
|
|
|
} else {
|
|
|
|
// Likely a split display/render setup. Pick the primary node and hope
|
|
|
|
// Mesa will open the right render node under-the-hood.
|
|
|
|
assert(device->available_nodes & (1 << DRM_NODE_PRIMARY));
|
|
|
|
name = device->nodes[DRM_NODE_PRIMARY];
|
|
|
|
wlr_log(WLR_DEBUG, "DRM device %s has no render node, "
|
|
|
|
"falling back to primary node", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
feedback_data->backend->drm_render_name = strdup(name);
|
|
|
|
|
|
|
|
drmFreeDevice(&device);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void linux_dmabuf_feedback_v1_handle_tranche_done(void *data,
|
|
|
|
struct zwp_linux_dmabuf_feedback_v1 *feedback) {
|
|
|
|
struct wlr_wl_linux_dmabuf_feedback_v1 *feedback_data = data;
|
|
|
|
feedback_data->tranche_target_device_id = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void linux_dmabuf_feedback_v1_handle_tranche_target_device(void *data,
|
|
|
|
struct zwp_linux_dmabuf_feedback_v1 *feedback,
|
|
|
|
struct wl_array *dev_id_arr) {
|
|
|
|
struct wlr_wl_linux_dmabuf_feedback_v1 *feedback_data = data;
|
|
|
|
|
|
|
|
dev_t dev_id;
|
|
|
|
assert(dev_id_arr->size == sizeof(dev_id));
|
|
|
|
memcpy(&dev_id, dev_id_arr->data, sizeof(dev_id));
|
|
|
|
|
|
|
|
feedback_data->tranche_target_device_id = dev_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void linux_dmabuf_feedback_v1_handle_tranche_formats(void *data,
|
|
|
|
struct zwp_linux_dmabuf_feedback_v1 *feedback,
|
|
|
|
struct wl_array *indices_arr) {
|
|
|
|
struct wlr_wl_linux_dmabuf_feedback_v1 *feedback_data = data;
|
|
|
|
|
|
|
|
if (feedback_data->format_table == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (feedback_data->tranche_target_device_id != feedback_data->main_device_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t table_cap = feedback_data->format_table_size /
|
|
|
|
sizeof(struct wlr_wl_linux_dmabuf_v1_table_entry);
|
|
|
|
uint16_t *index_ptr;
|
|
|
|
wl_array_for_each(index_ptr, indices_arr) {
|
|
|
|
assert(*index_ptr < table_cap);
|
|
|
|
const struct wlr_wl_linux_dmabuf_v1_table_entry *entry =
|
|
|
|
&feedback_data->format_table[*index_ptr];
|
|
|
|
wlr_drm_format_set_add(&feedback_data->backend->linux_dmabuf_v1_formats,
|
|
|
|
entry->format, entry->modifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void linux_dmabuf_feedback_v1_handle_tranche_flags(void *data,
|
|
|
|
struct zwp_linux_dmabuf_feedback_v1 *feedback, uint32_t flags) {
|
|
|
|
// TODO: handle SCANOUT flag
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct zwp_linux_dmabuf_feedback_v1_listener
|
|
|
|
linux_dmabuf_feedback_v1_listener = {
|
|
|
|
.done = linux_dmabuf_feedback_v1_handle_done,
|
|
|
|
.format_table = linux_dmabuf_feedback_v1_handle_format_table,
|
|
|
|
.main_device = linux_dmabuf_feedback_v1_handle_main_device,
|
|
|
|
.tranche_done = linux_dmabuf_feedback_v1_handle_tranche_done,
|
|
|
|
.tranche_target_device = linux_dmabuf_feedback_v1_handle_tranche_target_device,
|
|
|
|
.tranche_formats = linux_dmabuf_feedback_v1_handle_tranche_formats,
|
|
|
|
.tranche_flags = linux_dmabuf_feedback_v1_handle_tranche_flags,
|
|
|
|
};
|
|
|
|
|
2021-01-04 09:51:34 +00:00
|
|
|
static bool device_has_name(const drmDevice *device, const char *name) {
|
|
|
|
for (size_t i = 0; i < DRM_NODE_MAX; i++) {
|
|
|
|
if (!(device->available_nodes & (1 << i))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strcmp(device->nodes[i], name) == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *get_render_name(const char *name) {
|
|
|
|
uint32_t flags = 0;
|
|
|
|
int devices_len = drmGetDevices2(flags, NULL, 0);
|
|
|
|
if (devices_len < 0) {
|
2021-04-26 14:27:24 +00:00
|
|
|
wlr_log(WLR_ERROR, "drmGetDevices2 failed: %s", strerror(-devices_len));
|
2021-01-04 09:51:34 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
drmDevice **devices = calloc(devices_len, sizeof(drmDevice *));
|
|
|
|
if (devices == NULL) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
devices_len = drmGetDevices2(flags, devices, devices_len);
|
|
|
|
if (devices_len < 0) {
|
|
|
|
free(devices);
|
2021-04-26 14:27:24 +00:00
|
|
|
wlr_log(WLR_ERROR, "drmGetDevices2 failed: %s", strerror(-devices_len));
|
2021-01-04 09:51:34 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const drmDevice *match = NULL;
|
|
|
|
for (int i = 0; i < devices_len; i++) {
|
|
|
|
if (device_has_name(devices[i], name)) {
|
|
|
|
match = devices[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *render_name = NULL;
|
|
|
|
if (match == NULL) {
|
|
|
|
wlr_log(WLR_ERROR, "Cannot find DRM device %s", name);
|
|
|
|
} else if (!(match->available_nodes & (1 << DRM_NODE_RENDER))) {
|
2021-02-23 10:41:47 +00:00
|
|
|
// Likely a split display/render setup. Pick the primary node and hope
|
|
|
|
// Mesa will open the right render node under-the-hood.
|
|
|
|
wlr_log(WLR_DEBUG, "DRM device %s has no render node, "
|
|
|
|
"falling back to primary node", name);
|
|
|
|
assert(match->available_nodes & (1 << DRM_NODE_PRIMARY));
|
|
|
|
render_name = strdup(match->nodes[DRM_NODE_PRIMARY]);
|
2021-01-04 09:51:34 +00:00
|
|
|
} else {
|
|
|
|
render_name = strdup(match->nodes[DRM_NODE_RENDER]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < devices_len; i++) {
|
|
|
|
drmFreeDevice(&devices[i]);
|
|
|
|
}
|
|
|
|
free(devices);
|
|
|
|
|
|
|
|
return render_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void legacy_drm_handle_device(void *data, struct wl_drm *drm,
|
|
|
|
const char *name) {
|
|
|
|
struct wlr_wl_backend *wl = data;
|
|
|
|
wl->drm_render_name = get_render_name(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void legacy_drm_handle_format(void *data, struct wl_drm *drm,
|
|
|
|
uint32_t format) {
|
|
|
|
// This space is intentionally left blank
|
|
|
|
}
|
|
|
|
|
|
|
|
static void legacy_drm_handle_authenticated(void *data, struct wl_drm *drm) {
|
|
|
|
// This space is intentionally left blank
|
|
|
|
}
|
|
|
|
|
|
|
|
static void legacy_drm_handle_capabilities(void *data, struct wl_drm *drm,
|
|
|
|
uint32_t caps) {
|
|
|
|
// This space is intentionally left blank
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_drm_listener legacy_drm_listener = {
|
|
|
|
.device = legacy_drm_handle_device,
|
|
|
|
.format = legacy_drm_handle_format,
|
|
|
|
.authenticated = legacy_drm_handle_authenticated,
|
|
|
|
.capabilities = legacy_drm_handle_capabilities,
|
|
|
|
};
|
|
|
|
|
2021-02-09 22:29:44 +00:00
|
|
|
static void shm_handle_format(void *data, struct wl_shm *shm,
|
|
|
|
uint32_t shm_format) {
|
|
|
|
struct wlr_wl_backend *wl = data;
|
|
|
|
uint32_t drm_format = convert_wl_shm_format_to_drm(shm_format);
|
|
|
|
wlr_drm_format_set_add(&wl->shm_formats, drm_format, DRM_FORMAT_MOD_INVALID);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_shm_listener shm_listener = {
|
|
|
|
.format = shm_handle_format,
|
|
|
|
};
|
|
|
|
|
2018-11-11 03:47:30 +00:00
|
|
|
static void registry_global(void *data, struct wl_registry *registry,
|
2018-11-11 04:00:51 +00:00
|
|
|
uint32_t name, const char *iface, uint32_t version) {
|
|
|
|
struct wlr_wl_backend *wl = data;
|
|
|
|
|
2020-06-04 12:33:32 +00:00
|
|
|
wlr_log(WLR_DEBUG, "Remote wayland global: %s v%" PRIu32, iface, version);
|
2018-11-11 04:00:51 +00:00
|
|
|
|
|
|
|
if (strcmp(iface, wl_compositor_interface.name) == 0) {
|
|
|
|
wl->compositor = wl_registry_bind(registry, name,
|
|
|
|
&wl_compositor_interface, 4);
|
|
|
|
} else if (strcmp(iface, wl_seat_interface.name) == 0) {
|
2020-10-03 15:34:32 +00:00
|
|
|
struct wl_seat *wl_seat = wl_registry_bind(registry, name,
|
2019-01-26 10:04:05 +00:00
|
|
|
&wl_seat_interface, 5);
|
2020-10-18 17:12:01 +00:00
|
|
|
if (!create_wl_seat(wl_seat, wl)) {
|
|
|
|
wl_seat_destroy(wl_seat);
|
|
|
|
}
|
2019-01-10 14:17:14 +00:00
|
|
|
} else if (strcmp(iface, xdg_wm_base_interface.name) == 0) {
|
|
|
|
wl->xdg_wm_base = wl_registry_bind(registry, name,
|
|
|
|
&xdg_wm_base_interface, 1);
|
|
|
|
xdg_wm_base_add_listener(wl->xdg_wm_base, &xdg_wm_base_listener, NULL);
|
2019-05-06 16:06:31 +00:00
|
|
|
} else if (strcmp(iface, zxdg_decoration_manager_v1_interface.name) == 0) {
|
|
|
|
wl->zxdg_decoration_manager_v1 = wl_registry_bind(registry, name,
|
|
|
|
&zxdg_decoration_manager_v1_interface, 1);
|
2019-06-20 22:48:19 +00:00
|
|
|
} else if (strcmp(iface, zwp_pointer_gestures_v1_interface.name) == 0) {
|
|
|
|
wl->zwp_pointer_gestures_v1 = wl_registry_bind(registry, name,
|
2021-07-12 17:51:21 +00:00
|
|
|
&zwp_pointer_gestures_v1_interface, version < 3 ? version : 3);
|
2019-11-16 20:05:46 +00:00
|
|
|
} else if (strcmp(iface, wp_presentation_interface.name) == 0) {
|
|
|
|
wl->presentation = wl_registry_bind(registry, name,
|
|
|
|
&wp_presentation_interface, 1);
|
2021-11-22 18:29:30 +00:00
|
|
|
wp_presentation_add_listener(wl->presentation,
|
|
|
|
&presentation_listener, wl);
|
2019-05-10 06:36:19 +00:00
|
|
|
} else if (strcmp(iface, zwp_tablet_manager_v2_interface.name) == 0) {
|
|
|
|
wl->tablet_manager = wl_registry_bind(registry, name,
|
|
|
|
&zwp_tablet_manager_v2_interface, 1);
|
2019-09-24 12:33:15 +00:00
|
|
|
} else if (strcmp(iface, zwp_linux_dmabuf_v1_interface.name) == 0 &&
|
|
|
|
version >= 3) {
|
|
|
|
wl->zwp_linux_dmabuf_v1 = wl_registry_bind(registry, name,
|
2021-12-08 00:43:15 +00:00
|
|
|
&zwp_linux_dmabuf_v1_interface, version >= 4 ? 4 : version);
|
2019-09-24 12:33:15 +00:00
|
|
|
zwp_linux_dmabuf_v1_add_listener(wl->zwp_linux_dmabuf_v1,
|
|
|
|
&linux_dmabuf_v1_listener, wl);
|
2019-11-12 14:32:08 +00:00
|
|
|
} else if (strcmp(iface, zwp_relative_pointer_manager_v1_interface.name) == 0) {
|
|
|
|
wl->zwp_relative_pointer_manager_v1 = wl_registry_bind(registry, name,
|
|
|
|
&zwp_relative_pointer_manager_v1_interface, 1);
|
2021-01-04 09:51:34 +00:00
|
|
|
} else if (strcmp(iface, wl_drm_interface.name) == 0) {
|
|
|
|
wl->legacy_drm = wl_registry_bind(registry, name, &wl_drm_interface, 1);
|
|
|
|
wl_drm_add_listener(wl->legacy_drm, &legacy_drm_listener, wl);
|
2021-02-09 22:29:44 +00:00
|
|
|
} else if (strcmp(iface, wl_shm_interface.name) == 0) {
|
|
|
|
wl->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
|
|
|
|
wl_shm_add_listener(wl->shm, &shm_listener, wl);
|
2021-10-26 06:31:48 +00:00
|
|
|
} else if (strcmp(iface, xdg_activation_v1_interface.name) == 0) {
|
|
|
|
wl->activation_v1 = wl_registry_bind(registry, name,
|
|
|
|
&xdg_activation_v1_interface, 1);
|
2018-11-11 03:47:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void registry_global_remove(void *data, struct wl_registry *registry,
|
|
|
|
uint32_t name) {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_registry_listener registry_listener = {
|
|
|
|
.global = registry_global,
|
|
|
|
.global_remove = registry_global_remove
|
|
|
|
};
|
|
|
|
|
2017-06-19 15:46:50 +00:00
|
|
|
/*
|
|
|
|
* Initializes the wayland backend. Opens a connection to a remote wayland
|
|
|
|
* compositor and creates surfaces for each output, then registers globals on
|
|
|
|
* the specified display.
|
|
|
|
*/
|
2018-11-11 05:09:04 +00:00
|
|
|
static bool backend_start(struct wlr_backend *backend) {
|
|
|
|
struct wlr_wl_backend *wl = get_wl_backend_from_backend(backend);
|
2021-08-19 18:47:36 +00:00
|
|
|
wlr_log(WLR_INFO, "Starting Wayland backend");
|
2017-06-19 17:40:58 +00:00
|
|
|
|
2018-11-11 05:09:04 +00:00
|
|
|
wl->started = true;
|
2018-11-11 03:47:30 +00:00
|
|
|
|
2020-10-03 21:06:10 +00:00
|
|
|
struct wlr_wl_seat *seat;
|
|
|
|
wl_list_for_each(seat, &wl->seats, link) {
|
2022-03-03 18:23:08 +00:00
|
|
|
if (seat->wl_keyboard) {
|
|
|
|
init_seat_keyboard(seat);
|
2020-10-03 15:34:32 +00:00
|
|
|
}
|
2017-09-23 14:13:05 +00:00
|
|
|
|
2020-10-03 15:34:32 +00:00
|
|
|
if (wl->tablet_manager) {
|
2020-10-03 18:48:11 +00:00
|
|
|
wl_add_tablet_seat(wl->tablet_manager, seat);
|
2020-10-03 15:34:32 +00:00
|
|
|
}
|
2019-05-10 06:36:19 +00:00
|
|
|
}
|
|
|
|
|
2018-11-11 05:09:04 +00:00
|
|
|
for (size_t i = 0; i < wl->requested_outputs; ++i) {
|
|
|
|
wlr_wl_output_create(&wl->backend);
|
2017-06-19 20:36:40 +00:00
|
|
|
}
|
|
|
|
|
2017-06-19 15:46:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-11 05:09:04 +00:00
|
|
|
static void backend_destroy(struct wlr_backend *backend) {
|
|
|
|
if (!backend) {
|
2017-04-25 19:06:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-06-19 15:46:50 +00:00
|
|
|
|
2018-11-11 05:09:04 +00:00
|
|
|
struct wlr_wl_backend *wl = get_wl_backend_from_backend(backend);
|
|
|
|
|
2018-04-29 11:16:31 +00:00
|
|
|
struct wlr_wl_output *output, *tmp_output;
|
2018-11-11 05:09:04 +00:00
|
|
|
wl_list_for_each_safe(output, tmp_output, &wl->outputs, link) {
|
2017-10-14 20:33:00 +00:00
|
|
|
wlr_output_destroy(&output->wlr_output);
|
2017-04-26 01:26:29 +00:00
|
|
|
}
|
2017-06-19 15:46:50 +00:00
|
|
|
|
2021-11-22 21:30:40 +00:00
|
|
|
struct wlr_wl_input_device *input_device, *tmp_input_device;
|
2018-11-11 05:09:04 +00:00
|
|
|
wl_list_for_each_safe(input_device, tmp_input_device, &wl->devices, link) {
|
2022-02-09 19:08:20 +00:00
|
|
|
destroy_wl_input_device(input_device);
|
2017-06-20 17:01:20 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 22:16:34 +00:00
|
|
|
struct wlr_wl_buffer *buffer, *tmp_buffer;
|
|
|
|
wl_list_for_each_safe(buffer, tmp_buffer, &wl->buffers, link) {
|
|
|
|
destroy_wl_buffer(buffer);
|
|
|
|
}
|
|
|
|
|
2021-04-28 22:07:31 +00:00
|
|
|
wlr_backend_finish(backend);
|
2018-01-30 18:45:57 +00:00
|
|
|
|
2018-11-11 05:09:04 +00:00
|
|
|
wl_list_remove(&wl->local_display_destroy.link);
|
2017-12-07 22:44:59 +00:00
|
|
|
|
2018-11-11 05:09:04 +00:00
|
|
|
wl_event_source_remove(wl->remote_display_src);
|
|
|
|
|
2020-12-04 15:38:55 +00:00
|
|
|
close(wl->drm_fd);
|
2018-11-11 05:09:04 +00:00
|
|
|
|
2021-02-09 22:29:44 +00:00
|
|
|
wlr_drm_format_set_finish(&wl->shm_formats);
|
2019-09-24 12:33:15 +00:00
|
|
|
wlr_drm_format_set_finish(&wl->linux_dmabuf_v1_formats);
|
|
|
|
|
2020-10-03 15:34:32 +00:00
|
|
|
destroy_wl_seats(wl);
|
2019-05-06 16:06:31 +00:00
|
|
|
if (wl->zxdg_decoration_manager_v1) {
|
|
|
|
zxdg_decoration_manager_v1_destroy(wl->zxdg_decoration_manager_v1);
|
|
|
|
}
|
2019-06-20 22:48:19 +00:00
|
|
|
if (wl->zwp_pointer_gestures_v1) {
|
|
|
|
zwp_pointer_gestures_v1_destroy(wl->zwp_pointer_gestures_v1);
|
|
|
|
}
|
2019-11-16 20:05:46 +00:00
|
|
|
if (wl->presentation) {
|
|
|
|
wp_presentation_destroy(wl->presentation);
|
|
|
|
}
|
2019-09-24 12:33:15 +00:00
|
|
|
if (wl->zwp_linux_dmabuf_v1) {
|
|
|
|
zwp_linux_dmabuf_v1_destroy(wl->zwp_linux_dmabuf_v1);
|
|
|
|
}
|
2021-02-09 22:29:44 +00:00
|
|
|
if (wl->shm) {
|
|
|
|
wl_shm_destroy(wl->shm);
|
|
|
|
}
|
2019-11-12 14:32:08 +00:00
|
|
|
if (wl->zwp_relative_pointer_manager_v1) {
|
|
|
|
zwp_relative_pointer_manager_v1_destroy(wl->zwp_relative_pointer_manager_v1);
|
|
|
|
}
|
2021-01-04 09:51:34 +00:00
|
|
|
free(wl->drm_render_name);
|
2021-10-26 06:31:48 +00:00
|
|
|
free(wl->activation_token);
|
2019-01-10 14:17:14 +00:00
|
|
|
xdg_wm_base_destroy(wl->xdg_wm_base);
|
2018-11-11 05:09:04 +00:00
|
|
|
wl_compositor_destroy(wl->compositor);
|
|
|
|
wl_registry_destroy(wl->registry);
|
2021-01-29 09:06:16 +00:00
|
|
|
wl_display_flush(wl->remote_display);
|
2018-11-11 05:09:04 +00:00
|
|
|
wl_display_disconnect(wl->remote_display);
|
|
|
|
free(wl);
|
2017-04-25 19:06:58 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 18:29:30 +00:00
|
|
|
static clockid_t backend_get_presentation_clock(struct wlr_backend *backend) {
|
|
|
|
struct wlr_wl_backend *wl = get_wl_backend_from_backend(backend);
|
|
|
|
return wl->presentation_clock;
|
|
|
|
}
|
|
|
|
|
2020-12-04 15:38:55 +00:00
|
|
|
static int backend_get_drm_fd(struct wlr_backend *backend) {
|
|
|
|
struct wlr_wl_backend *wl = get_wl_backend_from_backend(backend);
|
|
|
|
return wl->drm_fd;
|
|
|
|
}
|
|
|
|
|
2021-04-28 22:16:34 +00:00
|
|
|
static uint32_t get_buffer_caps(struct wlr_backend *backend) {
|
2021-04-28 14:09:33 +00:00
|
|
|
struct wlr_wl_backend *wl = get_wl_backend_from_backend(backend);
|
|
|
|
return (wl->zwp_linux_dmabuf_v1 ? WLR_BUFFER_CAP_DMABUF : 0)
|
|
|
|
| (wl->shm ? WLR_BUFFER_CAP_SHM : 0);
|
|
|
|
}
|
|
|
|
|
2021-02-04 13:57:18 +00:00
|
|
|
static const struct wlr_backend_impl backend_impl = {
|
2018-04-25 22:51:00 +00:00
|
|
|
.start = backend_start,
|
|
|
|
.destroy = backend_destroy,
|
2021-11-22 18:29:30 +00:00
|
|
|
.get_presentation_clock = backend_get_presentation_clock,
|
2020-12-04 15:38:55 +00:00
|
|
|
.get_drm_fd = backend_get_drm_fd,
|
2021-04-28 22:16:34 +00:00
|
|
|
.get_buffer_caps = get_buffer_caps,
|
2017-06-19 15:46:50 +00:00
|
|
|
};
|
|
|
|
|
2017-06-20 22:22:21 +00:00
|
|
|
bool wlr_backend_is_wl(struct wlr_backend *b) {
|
|
|
|
return b->impl == &backend_impl;
|
|
|
|
}
|
2017-06-19 15:46:50 +00:00
|
|
|
|
2017-12-07 22:44:59 +00:00
|
|
|
static void handle_display_destroy(struct wl_listener *listener, void *data) {
|
2018-11-11 05:09:04 +00:00
|
|
|
struct wlr_wl_backend *wl =
|
|
|
|
wl_container_of(listener, wl, local_display_destroy);
|
|
|
|
backend_destroy(&wl->backend);
|
2017-12-07 22:44:59 +00:00
|
|
|
}
|
|
|
|
|
2018-04-28 11:38:03 +00:00
|
|
|
struct wlr_backend *wlr_wl_backend_create(struct wl_display *display,
|
2020-12-19 10:34:28 +00:00
|
|
|
const char *remote) {
|
2018-07-09 21:49:54 +00:00
|
|
|
wlr_log(WLR_INFO, "Creating wayland backend");
|
2017-06-19 15:46:50 +00:00
|
|
|
|
2018-11-11 05:09:04 +00:00
|
|
|
struct wlr_wl_backend *wl = calloc(1, sizeof(*wl));
|
|
|
|
if (!wl) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
2017-06-19 15:46:50 +00:00
|
|
|
return NULL;
|
2017-04-25 19:06:58 +00:00
|
|
|
}
|
2017-06-19 15:46:50 +00:00
|
|
|
|
2018-11-11 05:09:04 +00:00
|
|
|
wlr_backend_init(&wl->backend, &backend_impl);
|
2017-06-19 17:40:58 +00:00
|
|
|
|
2018-11-11 05:09:04 +00:00
|
|
|
wl->local_display = display;
|
|
|
|
wl_list_init(&wl->devices);
|
|
|
|
wl_list_init(&wl->outputs);
|
2020-10-03 21:06:10 +00:00
|
|
|
wl_list_init(&wl->seats);
|
2020-12-11 17:14:58 +00:00
|
|
|
wl_list_init(&wl->buffers);
|
2021-11-22 18:29:30 +00:00
|
|
|
wl->presentation_clock = CLOCK_MONOTONIC;
|
2017-09-23 04:29:53 +00:00
|
|
|
|
2018-11-11 05:09:04 +00:00
|
|
|
wl->remote_display = wl_display_connect(remote);
|
|
|
|
if (!wl->remote_display) {
|
2018-07-09 21:49:54 +00:00
|
|
|
wlr_log_errno(WLR_ERROR, "Could not connect to remote display");
|
2018-11-11 05:09:04 +00:00
|
|
|
goto error_wl;
|
2017-09-23 04:29:53 +00:00
|
|
|
}
|
|
|
|
|
2018-11-11 05:09:04 +00:00
|
|
|
wl->registry = wl_display_get_registry(wl->remote_display);
|
|
|
|
if (!wl->registry) {
|
2018-07-09 21:49:54 +00:00
|
|
|
wlr_log_errno(WLR_ERROR, "Could not obtain reference to remote registry");
|
2018-11-11 05:09:04 +00:00
|
|
|
goto error_display;
|
|
|
|
}
|
|
|
|
wl_registry_add_listener(wl->registry, ®istry_listener, wl);
|
2021-12-08 00:43:15 +00:00
|
|
|
|
2020-11-26 10:40:23 +00:00
|
|
|
wl_display_roundtrip(wl->remote_display); // get globals
|
2018-11-11 05:09:04 +00:00
|
|
|
|
|
|
|
if (!wl->compositor) {
|
|
|
|
wlr_log(WLR_ERROR,
|
|
|
|
"Remote Wayland compositor does not support wl_compositor");
|
|
|
|
goto error_registry;
|
|
|
|
}
|
2019-01-10 14:17:14 +00:00
|
|
|
if (!wl->xdg_wm_base) {
|
2018-11-11 05:09:04 +00:00
|
|
|
wlr_log(WLR_ERROR,
|
2019-01-10 14:17:14 +00:00
|
|
|
"Remote Wayland compositor does not support xdg-shell");
|
2018-04-28 11:38:03 +00:00
|
|
|
goto error_registry;
|
2017-09-23 04:29:53 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 00:43:15 +00:00
|
|
|
struct zwp_linux_dmabuf_feedback_v1 *linux_dmabuf_feedback_v1 = NULL;
|
|
|
|
struct wlr_wl_linux_dmabuf_feedback_v1 feedback_data = { .backend = wl };
|
|
|
|
if (wl->zwp_linux_dmabuf_v1 != NULL &&
|
|
|
|
zwp_linux_dmabuf_v1_get_version(wl->zwp_linux_dmabuf_v1) >=
|
|
|
|
ZWP_LINUX_DMABUF_V1_GET_DEFAULT_FEEDBACK_SINCE_VERSION) {
|
|
|
|
linux_dmabuf_feedback_v1 =
|
|
|
|
zwp_linux_dmabuf_v1_get_default_feedback(wl->zwp_linux_dmabuf_v1);
|
|
|
|
if (linux_dmabuf_feedback_v1 == NULL) {
|
|
|
|
wlr_log(WLR_ERROR, "Allocation failed");
|
|
|
|
goto error_registry;
|
|
|
|
}
|
|
|
|
zwp_linux_dmabuf_feedback_v1_add_listener(linux_dmabuf_feedback_v1,
|
|
|
|
&linux_dmabuf_feedback_v1_listener, &feedback_data);
|
|
|
|
|
|
|
|
if (wl->legacy_drm != NULL) {
|
|
|
|
wl_drm_destroy(wl->legacy_drm);
|
|
|
|
wl->legacy_drm = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wl_display_roundtrip(wl->remote_display); // get linux-dmabuf formats
|
|
|
|
|
|
|
|
if (feedback_data.format_table != NULL) {
|
|
|
|
munmap(feedback_data.format_table, feedback_data.format_table_size);
|
|
|
|
}
|
|
|
|
if (linux_dmabuf_feedback_v1 != NULL) {
|
|
|
|
zwp_linux_dmabuf_feedback_v1_destroy(linux_dmabuf_feedback_v1);
|
|
|
|
}
|
|
|
|
|
2018-11-11 05:09:04 +00:00
|
|
|
struct wl_event_loop *loop = wl_display_get_event_loop(wl->local_display);
|
|
|
|
int fd = wl_display_get_fd(wl->remote_display);
|
2021-01-05 18:52:06 +00:00
|
|
|
wl->remote_display_src = wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
|
2018-11-11 05:09:04 +00:00
|
|
|
dispatch_events, wl);
|
|
|
|
if (!wl->remote_display_src) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to create event source");
|
|
|
|
goto error_registry;
|
|
|
|
}
|
|
|
|
wl_event_source_check(wl->remote_display_src);
|
|
|
|
|
2021-02-09 22:37:49 +00:00
|
|
|
if (wl->drm_render_name != NULL) {
|
|
|
|
wlr_log(WLR_DEBUG, "Opening DRM render node %s", wl->drm_render_name);
|
|
|
|
wl->drm_fd = open(wl->drm_render_name, O_RDWR | O_NONBLOCK | O_CLOEXEC);
|
|
|
|
if (wl->drm_fd < 0) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Failed to open DRM render node %s",
|
|
|
|
wl->drm_render_name);
|
|
|
|
goto error_remote_display_src;
|
|
|
|
}
|
|
|
|
} else {
|
2021-04-28 14:11:09 +00:00
|
|
|
wl->drm_fd = -1;
|
2020-11-26 10:40:23 +00:00
|
|
|
}
|
|
|
|
|
2018-11-11 05:09:04 +00:00
|
|
|
wl->local_display_destroy.notify = handle_display_destroy;
|
|
|
|
wl_display_add_destroy_listener(display, &wl->local_display_destroy);
|
2017-12-07 22:44:59 +00:00
|
|
|
|
2021-10-26 06:31:48 +00:00
|
|
|
const char *token = getenv("XDG_ACTIVATION_TOKEN");
|
|
|
|
if (token != NULL) {
|
|
|
|
wl->activation_token = strdup(token);
|
|
|
|
unsetenv("XDG_ACTIVATION_TOKEN");
|
|
|
|
}
|
|
|
|
|
2018-11-11 05:09:04 +00:00
|
|
|
return &wl->backend;
|
2018-04-28 11:38:03 +00:00
|
|
|
|
2020-12-04 15:38:55 +00:00
|
|
|
error_remote_display_src:
|
2018-11-11 05:09:04 +00:00
|
|
|
wl_event_source_remove(wl->remote_display_src);
|
2018-04-28 11:38:03 +00:00
|
|
|
error_registry:
|
2021-01-04 09:51:34 +00:00
|
|
|
free(wl->drm_render_name);
|
2018-11-11 05:09:04 +00:00
|
|
|
if (wl->compositor) {
|
|
|
|
wl_compositor_destroy(wl->compositor);
|
|
|
|
}
|
2019-01-10 14:17:14 +00:00
|
|
|
if (wl->xdg_wm_base) {
|
|
|
|
xdg_wm_base_destroy(wl->xdg_wm_base);
|
2018-11-11 05:09:04 +00:00
|
|
|
}
|
|
|
|
wl_registry_destroy(wl->registry);
|
|
|
|
error_display:
|
|
|
|
wl_display_disconnect(wl->remote_display);
|
|
|
|
error_wl:
|
2021-04-28 22:16:34 +00:00
|
|
|
wlr_backend_finish(&wl->backend);
|
2018-11-11 05:09:04 +00:00
|
|
|
free(wl);
|
2018-04-28 11:38:03 +00:00
|
|
|
return NULL;
|
2017-04-25 15:32:52 +00:00
|
|
|
}
|
2019-11-12 16:56:57 +00:00
|
|
|
|
|
|
|
struct wl_display *wlr_wl_backend_get_remote_display(struct wlr_backend *backend) {
|
|
|
|
struct wlr_wl_backend *wl = get_wl_backend_from_backend(backend);
|
|
|
|
return wl->remote_display;
|
|
|
|
}
|