From be620ef43f6a37bc70331d6db69a3d0f60d2bb0d Mon Sep 17 00:00:00 2001
From: Micah Gorrell <micah.gorrell@venafi.com>
Date: Fri, 26 May 2023 08:17:20 -0600
Subject: [PATCH] Implemented support for the DRM lease protocol, as needed to
 use devices such as VR headsets

---
 dwl.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/dwl.c b/dwl.c
index 12f441e..6864f18 100644
--- a/dwl.c
+++ b/dwl.c
@@ -23,6 +23,7 @@
 #include <wlr/types/wlr_data_control_v1.h>
 #include <wlr/types/wlr_data_device.h>
 #include <wlr/types/wlr_drm.h>
+#include <wlr/types/wlr_drm_lease_v1.h>
 #include <wlr/types/wlr_export_dmabuf_v1.h>
 #include <wlr/types/wlr_fractional_scale_v1.h>
 #include <wlr/types/wlr_gamma_control_v1.h>
@@ -315,6 +316,7 @@ static void powermgrsetmode(struct wl_listener *listener, void *data);
 static void quit(const Arg *arg);
 static void rendermon(struct wl_listener *listener, void *data);
 static void requestdecorationmode(struct wl_listener *listener, void *data);
+static void requestdrmlease(struct wl_listener *listener, void *data);
 static void requeststartdrag(struct wl_listener *listener, void *data);
 static void requestmonstate(struct wl_listener *listener, void *data);
 static void resize(Client *c, struct wlr_box geo, int interact);
@@ -376,6 +378,7 @@ static struct wl_list clients; /* tiling order */
 static struct wl_list fstack;  /* focus order */
 static struct wlr_idle_notifier_v1 *idle_notifier;
 static struct wlr_idle_inhibit_manager_v1 *idle_inhibit_mgr;
+static struct wlr_drm_lease_v1_manager *drm_lease_manager;
 static struct wlr_layer_shell_v1 *layer_shell;
 static struct wlr_output_manager_v1 *output_mgr;
 static struct wlr_virtual_keyboard_manager_v1 *virtual_keyboard_mgr;
@@ -435,6 +438,7 @@ static struct wl_listener request_set_cursor_shape = {.notify = setcursorshape};
 static struct wl_listener request_start_drag = {.notify = requeststartdrag};
 static struct wl_listener start_drag = {.notify = startdrag};
 static struct wl_listener new_session_lock = {.notify = locksession};
+static struct wl_listener drm_lease_request = {.notify = requestdrmlease};
 
 #ifdef XWAYLAND
 static void activatex11(struct wl_listener *listener, void *data);
@@ -782,6 +786,9 @@ cleanuplisteners(void)
 	wl_list_remove(&request_start_drag.link);
 	wl_list_remove(&start_drag.link);
 	wl_list_remove(&new_session_lock.link);
+	if (drm_lease_manager) {
+		wl_list_remove(&drm_lease_request.link);
+	}
 #ifdef XWAYLAND
 	wl_list_remove(&new_xwayland_surface.link);
 	wl_list_remove(&xwayland_ready.link);
@@ -1049,6 +1056,14 @@ createmon(struct wl_listener *listener, void *data)
 	if (!wlr_output_init_render(wlr_output, alloc, drw))
 		return;
 
+	if (wlr_output->non_desktop) {
+		if (drm_lease_manager) {
+			wlr_drm_lease_v1_manager_offer_output(drm_lease_manager, wlr_output);
+		}
+
+		return;
+	}
+
 	m = wlr_output->data = ecalloc(1, sizeof(*m));
 	m->wlr_output = wlr_output;
 
@@ -2181,6 +2196,16 @@ requestdecorationmode(struct wl_listener *listener, void *data)
 				WLR_XDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
 }
 
+static void requestdrmlease(struct wl_listener *listener, void *data) {
+	struct wlr_drm_lease_request_v1 *req = data;
+	struct wlr_drm_lease_v1 *lease = wlr_drm_lease_request_v1_grant(req);
+
+	if (!lease) {
+		fprintf(stderr, "Failed to grant lease request");
+		wlr_drm_lease_request_v1_reject(req);
+	}
+}
+
 void
 requeststartdrag(struct wl_listener *listener, void *data)
 {
@@ -2645,10 +2670,18 @@ setup(void)
 	wl_signal_add(&output_mgr->events.apply, &output_mgr_apply);
 	wl_signal_add(&output_mgr->events.test, &output_mgr_test);
 
+	drm_lease_manager = wlr_drm_lease_v1_manager_create(dpy, backend);
+	if (drm_lease_manager) {
+		wl_signal_add(&drm_lease_manager->events.request, &drm_lease_request);
+	} else {
+		fprintf(stderr, "Failed to create wlr_drm_lease_device_v1; VR will not be available\n");
+	}
+
 	/* Make sure XWayland clients don't connect to the parent X server,
 	 * e.g when running in the x11 backend or the wayland backend and the
 	 * compositor has Xwayland support */
 	unsetenv("DISPLAY");
+
 #ifdef XWAYLAND
 	/*
 	 * Initialise the XWayland X server.
-- 
2.51.1

