1 #include <assert.h>
2 #include <limits.h>
3 #include <stdint.h>
4 #include <stdlib.h>
5 
6 #include <wlr/config.h>
7 
8 #include <drm_fourcc.h>
9 #include <wayland-server-core.h>
10 
11 #include <wlr/backend/interface.h>
12 #include <wlr/interfaces/wlr_input_device.h>
13 #include <wlr/interfaces/wlr_output.h>
14 #include <wlr/render/egl.h>
15 #include <wlr/render/gles2.h>
16 #include <wlr/util/log.h>
17 
18 #include "backend/wayland.h"
19 #include "util/signal.h"
20 #include "linux-dmabuf-unstable-v1-client-protocol.h"
21 #include "pointer-gestures-unstable-v1-client-protocol.h"
22 #include "presentation-time-client-protocol.h"
23 #include "xdg-decoration-unstable-v1-client-protocol.h"
24 #include "xdg-shell-client-protocol.h"
25 #include "tablet-unstable-v2-client-protocol.h"
26 #include "relative-pointer-unstable-v1-client-protocol.h"
27 
get_wl_backend_from_backend(struct wlr_backend * backend)28 struct wlr_wl_backend *get_wl_backend_from_backend(struct wlr_backend *backend) {
29 	assert(wlr_backend_is_wl(backend));
30 	return (struct wlr_wl_backend *)backend;
31 }
32 
dispatch_events(int fd,uint32_t mask,void * data)33 static int dispatch_events(int fd, uint32_t mask, void *data) {
34 	struct wlr_wl_backend *wl = data;
35 
36 	if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
37 		if (mask & WL_EVENT_ERROR) {
38 			wlr_log(WLR_ERROR, "Failed to read from remote Wayland display");
39 		}
40 		wl_display_terminate(wl->local_display);
41 		return 0;
42 	}
43 
44 	int count = 0;
45 	if (mask & WL_EVENT_READABLE) {
46 		count = wl_display_dispatch(wl->remote_display);
47 	}
48 	if (mask & WL_EVENT_WRITABLE) {
49 		wl_display_flush(wl->remote_display);
50 	}
51 	if (mask == 0) {
52 		count = wl_display_dispatch_pending(wl->remote_display);
53 		wl_display_flush(wl->remote_display);
54 	}
55 
56 	if (count < 0) {
57 		wlr_log(WLR_ERROR, "Failed to dispatch remote Wayland display");
58 		wl_display_terminate(wl->local_display);
59 		return 0;
60 	}
61 	return count;
62 }
63 
xdg_wm_base_handle_ping(void * data,struct xdg_wm_base * base,uint32_t serial)64 static void xdg_wm_base_handle_ping(void *data,
65 		struct xdg_wm_base *base, uint32_t serial) {
66 	xdg_wm_base_pong(base, serial);
67 }
68 
69 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
70 	xdg_wm_base_handle_ping,
71 };
72 
linux_dmabuf_v1_handle_format(void * data,struct zwp_linux_dmabuf_v1 * linux_dmabuf_v1,uint32_t format)73 static void linux_dmabuf_v1_handle_format(void *data,
74 		struct zwp_linux_dmabuf_v1 *linux_dmabuf_v1, uint32_t format) {
75 	// Note, this event is deprecated
76 	struct wlr_wl_backend *wl = data;
77 
78 	wlr_drm_format_set_add(&wl->linux_dmabuf_v1_formats, format,
79 		DRM_FORMAT_MOD_INVALID);
80 }
81 
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)82 static void linux_dmabuf_v1_handle_modifier(void *data,
83 		struct zwp_linux_dmabuf_v1 *linux_dmabuf_v1, uint32_t format,
84 		uint32_t modifier_hi, uint32_t modifier_lo) {
85 	struct wlr_wl_backend *wl = data;
86 
87 	uint64_t modifier = ((uint64_t)modifier_hi << 32) | modifier_lo;
88 	wlr_drm_format_set_add(&wl->linux_dmabuf_v1_formats, format, modifier);
89 }
90 
91 static const struct zwp_linux_dmabuf_v1_listener linux_dmabuf_v1_listener = {
92 	.format = linux_dmabuf_v1_handle_format,
93 	.modifier = linux_dmabuf_v1_handle_modifier,
94 };
95 
registry_global(void * data,struct wl_registry * registry,uint32_t name,const char * iface,uint32_t version)96 static void registry_global(void *data, struct wl_registry *registry,
97 		uint32_t name, const char *iface, uint32_t version) {
98 	struct wlr_wl_backend *wl = data;
99 
100 	wlr_log(WLR_DEBUG, "Remote wayland global: %s v%" PRIu32, iface, version);
101 
102 	if (strcmp(iface, wl_compositor_interface.name) == 0) {
103 		wl->compositor = wl_registry_bind(registry, name,
104 			&wl_compositor_interface, 4);
105 	} else if (strcmp(iface, wl_seat_interface.name) == 0) {
106 		struct wl_seat *wl_seat = wl_registry_bind(registry, name,
107 			&wl_seat_interface, 5);
108 		if (!create_wl_seat(wl_seat, wl)) {
109 			wl_seat_destroy(wl_seat);
110 		}
111 	} else if (strcmp(iface, xdg_wm_base_interface.name) == 0) {
112 		wl->xdg_wm_base = wl_registry_bind(registry, name,
113 			&xdg_wm_base_interface, 1);
114 		xdg_wm_base_add_listener(wl->xdg_wm_base, &xdg_wm_base_listener, NULL);
115 	} else if (strcmp(iface, zxdg_decoration_manager_v1_interface.name) == 0) {
116 		wl->zxdg_decoration_manager_v1 = wl_registry_bind(registry, name,
117 			&zxdg_decoration_manager_v1_interface, 1);
118 	} else if (strcmp(iface, zwp_pointer_gestures_v1_interface.name) == 0) {
119 		wl->zwp_pointer_gestures_v1 = wl_registry_bind(registry, name,
120 			&zwp_pointer_gestures_v1_interface, 1);
121 	} else if (strcmp(iface, wp_presentation_interface.name) == 0) {
122 		wl->presentation = wl_registry_bind(registry, name,
123 			&wp_presentation_interface, 1);
124 	} else if (strcmp(iface, zwp_tablet_manager_v2_interface.name) == 0) {
125 		wl->tablet_manager = wl_registry_bind(registry, name,
126 			&zwp_tablet_manager_v2_interface, 1);
127 	} else if (strcmp(iface, zwp_linux_dmabuf_v1_interface.name) == 0 &&
128 			version >= 3) {
129 		wl->zwp_linux_dmabuf_v1 = wl_registry_bind(registry, name,
130 			&zwp_linux_dmabuf_v1_interface, 3);
131 		zwp_linux_dmabuf_v1_add_listener(wl->zwp_linux_dmabuf_v1,
132 			&linux_dmabuf_v1_listener, wl);
133 	} else if (strcmp(iface, zwp_relative_pointer_manager_v1_interface.name) == 0) {
134 		wl->zwp_relative_pointer_manager_v1 = wl_registry_bind(registry, name,
135 			&zwp_relative_pointer_manager_v1_interface, 1);
136 	}
137 }
138 
registry_global_remove(void * data,struct wl_registry * registry,uint32_t name)139 static void registry_global_remove(void *data, struct wl_registry *registry,
140 		uint32_t name) {
141 	// TODO
142 }
143 
144 static const struct wl_registry_listener registry_listener = {
145 	.global = registry_global,
146 	.global_remove = registry_global_remove
147 };
148 
149 /*
150  * Initializes the wayland backend. Opens a connection to a remote wayland
151  * compositor and creates surfaces for each output, then registers globals on
152  * the specified display.
153  */
backend_start(struct wlr_backend * backend)154 static bool backend_start(struct wlr_backend *backend) {
155 	struct wlr_wl_backend *wl = get_wl_backend_from_backend(backend);
156 	wlr_log(WLR_INFO, "Initializating wayland backend");
157 
158 	wl->started = true;
159 
160 	struct wlr_wl_seat *seat = wl->seat;
161 	if (seat != NULL) {
162 		if (seat->keyboard) {
163 			create_wl_keyboard(seat->keyboard, wl);
164 		}
165 
166 		if (wl->tablet_manager) {
167 			wl_add_tablet_seat(wl->tablet_manager,
168 				seat->wl_seat, wl);
169 		}
170 	}
171 
172 	for (size_t i = 0; i < wl->requested_outputs; ++i) {
173 		wlr_wl_output_create(&wl->backend);
174 	}
175 
176 	return true;
177 }
178 
backend_destroy(struct wlr_backend * backend)179 static void backend_destroy(struct wlr_backend *backend) {
180 	if (!backend) {
181 		return;
182 	}
183 
184 	struct wlr_wl_backend *wl = get_wl_backend_from_backend(backend);
185 
186 	struct wlr_wl_output *output, *tmp_output;
187 	wl_list_for_each_safe(output, tmp_output, &wl->outputs, link) {
188 		wlr_output_destroy(&output->wlr_output);
189 	}
190 
191 	struct wlr_input_device *input_device, *tmp_input_device;
192 	wl_list_for_each_safe(input_device, tmp_input_device, &wl->devices, link) {
193 		wlr_input_device_destroy(input_device);
194 	}
195 
196 	wlr_signal_emit_safe(&wl->backend.events.destroy, &wl->backend);
197 
198 	wl_list_remove(&wl->local_display_destroy.link);
199 
200 	wl_event_source_remove(wl->remote_display_src);
201 
202 	wlr_renderer_destroy(wl->renderer);
203 	wlr_egl_finish(&wl->egl);
204 
205 	wlr_drm_format_set_finish(&wl->linux_dmabuf_v1_formats);
206 
207 	destroy_wl_seats(wl);
208 	if (wl->zxdg_decoration_manager_v1) {
209 		zxdg_decoration_manager_v1_destroy(wl->zxdg_decoration_manager_v1);
210 	}
211 	if (wl->zwp_pointer_gestures_v1) {
212 		zwp_pointer_gestures_v1_destroy(wl->zwp_pointer_gestures_v1);
213 	}
214 	if (wl->presentation) {
215 		wp_presentation_destroy(wl->presentation);
216 	}
217 	if (wl->zwp_linux_dmabuf_v1) {
218 		zwp_linux_dmabuf_v1_destroy(wl->zwp_linux_dmabuf_v1);
219 	}
220 	if (wl->zwp_relative_pointer_manager_v1) {
221 		zwp_relative_pointer_manager_v1_destroy(wl->zwp_relative_pointer_manager_v1);
222 	}
223 	xdg_wm_base_destroy(wl->xdg_wm_base);
224 	wl_compositor_destroy(wl->compositor);
225 	wl_registry_destroy(wl->registry);
226 	wl_display_disconnect(wl->remote_display);
227 	free(wl);
228 }
229 
backend_get_renderer(struct wlr_backend * backend)230 static struct wlr_renderer *backend_get_renderer(struct wlr_backend *backend) {
231 	struct wlr_wl_backend *wl = get_wl_backend_from_backend(backend);
232 	return wl->renderer;
233 }
234 
235 static struct wlr_backend_impl backend_impl = {
236 	.start = backend_start,
237 	.destroy = backend_destroy,
238 	.get_renderer = backend_get_renderer,
239 };
240 
wlr_backend_is_wl(struct wlr_backend * b)241 bool wlr_backend_is_wl(struct wlr_backend *b) {
242 	return b->impl == &backend_impl;
243 }
244 
handle_display_destroy(struct wl_listener * listener,void * data)245 static void handle_display_destroy(struct wl_listener *listener, void *data) {
246 	struct wlr_wl_backend *wl =
247 		wl_container_of(listener, wl, local_display_destroy);
248 	backend_destroy(&wl->backend);
249 }
250 
wlr_wl_backend_create(struct wl_display * display,const char * remote,wlr_renderer_create_func_t create_renderer_func)251 struct wlr_backend *wlr_wl_backend_create(struct wl_display *display,
252 		const char *remote, wlr_renderer_create_func_t create_renderer_func) {
253 	wlr_log(WLR_INFO, "Creating wayland backend");
254 
255 	struct wlr_wl_backend *wl = calloc(1, sizeof(*wl));
256 	if (!wl) {
257 		wlr_log_errno(WLR_ERROR, "Allocation failed");
258 		return NULL;
259 	}
260 
261 	wlr_backend_init(&wl->backend, &backend_impl);
262 
263 	wl->local_display = display;
264 	wl_list_init(&wl->devices);
265 	wl_list_init(&wl->outputs);
266 
267 	wl->remote_display = wl_display_connect(remote);
268 	if (!wl->remote_display) {
269 		wlr_log_errno(WLR_ERROR, "Could not connect to remote display");
270 		goto error_wl;
271 	}
272 
273 	wl->registry = wl_display_get_registry(wl->remote_display);
274 	if (!wl->registry) {
275 		wlr_log_errno(WLR_ERROR, "Could not obtain reference to remote registry");
276 		goto error_display;
277 	}
278 
279 	wl_registry_add_listener(wl->registry, &registry_listener, wl);
280 	wl_display_roundtrip(wl->remote_display);
281 
282 	if (!wl->compositor) {
283 		wlr_log(WLR_ERROR,
284 			"Remote Wayland compositor does not support wl_compositor");
285 		goto error_registry;
286 	}
287 	if (!wl->xdg_wm_base) {
288 		wlr_log(WLR_ERROR,
289 			"Remote Wayland compositor does not support xdg-shell");
290 		goto error_registry;
291 	}
292 
293 	struct wl_event_loop *loop = wl_display_get_event_loop(wl->local_display);
294 	int fd = wl_display_get_fd(wl->remote_display);
295 	int events = WL_EVENT_READABLE | WL_EVENT_ERROR | WL_EVENT_HANGUP;
296 	wl->remote_display_src = wl_event_loop_add_fd(loop, fd, events,
297 		dispatch_events, wl);
298 	if (!wl->remote_display_src) {
299 		wlr_log(WLR_ERROR, "Failed to create event source");
300 		goto error_registry;
301 	}
302 	wl_event_source_check(wl->remote_display_src);
303 
304 	static EGLint config_attribs[] = {
305 		EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
306 		EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
307 		EGL_RED_SIZE, 1,
308 		EGL_GREEN_SIZE, 1,
309 		EGL_BLUE_SIZE, 1,
310 		EGL_ALPHA_SIZE, 1,
311 		EGL_NONE,
312 	};
313 
314 	if (!create_renderer_func) {
315 		create_renderer_func = wlr_renderer_autocreate;
316 	}
317 
318 	wl->renderer = create_renderer_func(&wl->egl, EGL_PLATFORM_WAYLAND_EXT,
319 		wl->remote_display, config_attribs, WL_SHM_FORMAT_ARGB8888);
320 
321 	if (!wl->renderer) {
322 		wlr_log(WLR_ERROR, "Could not create renderer");
323 		goto error_event;
324 	}
325 
326 	wl->local_display_destroy.notify = handle_display_destroy;
327 	wl_display_add_destroy_listener(display, &wl->local_display_destroy);
328 
329 	return &wl->backend;
330 
331 error_event:
332 	wl_event_source_remove(wl->remote_display_src);
333 error_registry:
334 	if (wl->compositor) {
335 		wl_compositor_destroy(wl->compositor);
336 	}
337 	if (wl->xdg_wm_base) {
338 		xdg_wm_base_destroy(wl->xdg_wm_base);
339 	}
340 	wl_registry_destroy(wl->registry);
341 error_display:
342 	wl_display_disconnect(wl->remote_display);
343 error_wl:
344 	free(wl);
345 	return NULL;
346 }
347 
wlr_wl_backend_get_remote_display(struct wlr_backend * backend)348 struct wl_display *wlr_wl_backend_get_remote_display(struct wlr_backend *backend) {
349 	struct wlr_wl_backend *wl = get_wl_backend_from_backend(backend);
350 	return wl->remote_display;
351 }
352