Debian Codegraph @

gdk/wayland/gdkdisplay-wayland.c

raw
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "config.h"
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25
26 #ifdef HAVE_LINUX_MEMFD_H
27 #include <linux/memfd.h>
28 #endif
29
30 #include <sys/mman.h>
31 #include <sys/syscall.h>
32
33 #include <glib.h>
34 #include "gdkwayland.h"
35 #include "gdkdisplay.h"
36 #include "gdkdisplay-wayland.h"
37 #include "gdkscreen.h"
38 #include "gdkinternals.h"
39 #include "gdkdeviceprivate.h"
40 #include "gdkdevicemanager.h"
41 #include "gdkkeysprivate.h"
42 #include "gdkprivate-wayland.h"
43 #include "gdkglcontext-wayland.h"
44 #include "gdkwaylandmonitor.h"
45 #include "gdk-private.h"
46 #include "pointer-gestures-unstable-v1-client-protocol.h"
47 #include "tablet-unstable-v2-client-protocol.h"
48 #include "xdg-shell-unstable-v6-client-protocol.h"
49 #include "xdg-foreign-unstable-v1-client-protocol.h"
50 #include "xdg-foreign-unstable-v2-client-protocol.h"
51 #include "server-decoration-client-protocol.h"
52
53 #ifdef HAVE_TOPLEVEL_STATE_SUSPENDED
54 #define XDG_WM_BASE_VERSION 6
55 #else
56 #define XDG_WM_BASE_VERSION 2
57 #endif
58
59 /**
60 * SECTION:wayland_interaction
61 * @Short_description: Wayland backend-specific functions
62 * @Title: Wayland Interaction
63 *
64 * The functions in this section are specific to the GDK Wayland backend.
65 * To use them, you need to include the `<gdk/gdkwayland.h>` header and use
66 * the Wayland-specific pkg-config files to build your application (either
67 * `gdk-wayland-3.0` or `gtk+-wayland-3.0`).
68 *
69 * To make your code compile with other GDK backends, guard backend-specific
70 * calls by an ifdef as follows. Since GDK may be built with multiple
71 * backends, you should also check for the backend that is in use (e.g. by
72 * using the GDK_IS_WAYLAND_DISPLAY() macro).
73 * |[<!-- language="C" -->
74 * #ifdef GDK_WINDOWING_WAYLAND
75 * if (GDK_IS_WAYLAND_DISPLAY (display))
76 * {
77 * // make Wayland-specific calls here
78 * }
79 * else
80 * #endif
81 * #ifdef GDK_WINDOWING_X11
82 * if (GDK_IS_X11_DISPLAY (display))
83 * {
84 * // make X11-specific calls here
85 * }
86 * else
87 * #endif
88 * g_error ("Unsupported GDK backend");
89 * ]|
90 */
91
92 #define MIN_SYSTEM_BELL_DELAY_MS 20
93
94 #define GTK_SHELL1_VERSION 5
95 #ifdef HAVE_XDG_ACTIVATION
96 #define XDG_ACTIVATION_VERSION 1
97 #endif
98
99 static void _gdk_wayland_display_load_cursor_theme (GdkWaylandDisplay *display_wayland);
100
101 G_DEFINE_TYPE (GdkWaylandDisplay, gdk_wayland_display, GDK_TYPE_DISPLAY)
102
103 static void
104 async_roundtrip_callback (void *data,
105 struct wl_callback *callback,
106 uint32_t time)
107 {
108 GdkWaylandDisplay *display_wayland = data;
109
110 display_wayland->async_roundtrips =
111 g_list_remove (display_wayland->async_roundtrips, callback);
112 wl_callback_destroy (callback);
113 }
114
115 static const struct wl_callback_listener async_roundtrip_listener = {
116 async_roundtrip_callback
117 };
118
119 static void
120 _gdk_wayland_display_async_roundtrip (GdkWaylandDisplay *display_wayland)
121 {
122 struct wl_callback *callback;
123
124 callback = wl_display_sync (display_wayland->wl_display);
125 wl_callback_add_listener (callback,
126 &async_roundtrip_listener,
127 display_wayland);
128 display_wayland->async_roundtrips =
129 g_list_append (display_wayland->async_roundtrips, callback);
130 }
131
132 static void
133 xdg_wm_base_ping (void *data,
134 struct xdg_wm_base *xdg_wm_base,
135 uint32_t serial)
136 {
137 GDK_NOTE (EVENTS,
138 g_message ("ping, shell %p, serial %u\n", xdg_wm_base, serial));
139
140 xdg_wm_base_pong (xdg_wm_base, serial);
141 }
142
143 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
144 xdg_wm_base_ping,
145 };
146
147 static void
148 zxdg_shell_v6_ping (void *data,
149 struct zxdg_shell_v6 *xdg_shell,
150 uint32_t serial)
151 {
152 GdkWaylandDisplay *display_wayland = data;
153
154 _gdk_wayland_display_update_serial (display_wayland, serial);
155
156 GDK_NOTE (EVENTS,
157 g_message ("ping, shell %p, serial %u\n", xdg_shell, serial));
158
159 zxdg_shell_v6_pong (xdg_shell, serial);
160 }
161
162 static const struct zxdg_shell_v6_listener zxdg_shell_v6_listener = {
163 zxdg_shell_v6_ping,
164 };
165
166 static gboolean
167 is_known_global (gpointer key, gpointer value, gpointer user_data)
168 {
169 const char *required_global = user_data;
170 const char *known_global = value;
171
172 return g_strcmp0 (required_global, known_global) == 0;
173 }
174
175 static gboolean
176 has_required_globals (GdkWaylandDisplay *display_wayland,
177 const char *required_globals[])
178 {
179 int i = 0;
180
181 while (required_globals[i])
182 {
183 if (g_hash_table_find (display_wayland->known_globals,
184 is_known_global,
185 (gpointer)required_globals[i]) == NULL)
186 return FALSE;
187
188 i++;
189 }
190
191 return TRUE;
192 }
193
194 typedef struct _OnHasGlobalsClosure OnHasGlobalsClosure;
195
196 typedef void (*HasGlobalsCallback) (GdkWaylandDisplay *display_wayland,
197 OnHasGlobalsClosure *closure);
198
199 struct _OnHasGlobalsClosure
200 {
201 HasGlobalsCallback handler;
202 const char **required_globals;
203 };
204
205 static void
206 process_on_globals_closures (GdkWaylandDisplay *display_wayland)
207 {
208 GList *iter;
209
210 iter = display_wayland->on_has_globals_closures;
211 while (iter != NULL)
212 {
213 GList *next = iter->next;
214 OnHasGlobalsClosure *closure = iter->data;
215
216 if (has_required_globals (display_wayland,
217 closure->required_globals))
218 {
219 closure->handler (display_wayland, closure);
220 g_free (closure);
221 display_wayland->on_has_globals_closures =
222 g_list_delete_link (display_wayland->on_has_globals_closures, iter);
223 }
224
225 iter = next;
226 }
227 }
228
229 typedef struct
230 {
231 OnHasGlobalsClosure base;
232 uint32_t id;
233 uint32_t version;
234 } SeatAddedClosure;
235
236 static void
237 _gdk_wayland_display_add_seat (GdkWaylandDisplay *display_wayland,
238 uint32_t id,
239 uint32_t version)
240 {
241 GdkDisplay *gdk_display = GDK_DISPLAY_OBJECT (display_wayland);
242 struct wl_seat *seat;
243
244 display_wayland->seat_version = MIN (version, 5);
245 seat = wl_registry_bind (display_wayland->wl_registry,
246 id, &wl_seat_interface,
247 display_wayland->seat_version);
248 _gdk_wayland_device_manager_add_seat (gdk_display->device_manager,
249 id, seat);
250 _gdk_wayland_display_async_roundtrip (display_wayland);
251 }
252
253 static void
254 seat_added_closure_run (GdkWaylandDisplay *display_wayland,
255 OnHasGlobalsClosure *closure)
256 {
257 SeatAddedClosure *seat_added_closure = (SeatAddedClosure*)closure;
258
259 _gdk_wayland_display_add_seat (display_wayland,
260 seat_added_closure->id,
261 seat_added_closure->version);
262 }
263
264 static void
265 postpone_on_globals_closure (GdkWaylandDisplay *display_wayland,
266 OnHasGlobalsClosure *closure)
267 {
268 display_wayland->on_has_globals_closures =
269 g_list_append (display_wayland->on_has_globals_closures, closure);
270 }
271
272 #ifdef G_ENABLE_DEBUG
273
274 static const char *
275 get_format_name (enum wl_shm_format format)
276 {
277 int i;
278 #define FORMAT(s) { WL_SHM_FORMAT_ ## s, #s }
279 struct { int format; const char *name; } formats[] = {
280 FORMAT(ARGB8888),
281 FORMAT(XRGB8888),
282 FORMAT(C8),
283 FORMAT(RGB332),
284 FORMAT(BGR233),
285 FORMAT(XRGB4444),
286 FORMAT(XBGR4444),
287 FORMAT(RGBX4444),
288 FORMAT(BGRX4444),
289 FORMAT(ARGB4444),
290 FORMAT(ABGR4444),
291 FORMAT(RGBA4444),
292 FORMAT(BGRA4444),
293 FORMAT(XRGB1555),
294 FORMAT(XBGR1555),
295 FORMAT(RGBX5551),
296 FORMAT(BGRX5551),
297 FORMAT(ARGB1555),
298 FORMAT(ABGR1555),
299 FORMAT(RGBA5551),
300 FORMAT(BGRA5551),
301 FORMAT(RGB565),
302 FORMAT(BGR565),
303 FORMAT(RGB888),
304 FORMAT(BGR888),
305 FORMAT(XBGR8888),
306 FORMAT(RGBX8888),
307 FORMAT(BGRX8888),
308 FORMAT(ABGR8888),
309 FORMAT(RGBA8888),
310 FORMAT(BGRA8888),
311 FORMAT(XRGB2101010),
312 FORMAT(XBGR2101010),
313 FORMAT(RGBX1010102),
314 FORMAT(BGRX1010102),
315 FORMAT(ARGB2101010),
316 FORMAT(ABGR2101010),
317 FORMAT(RGBA1010102),
318 FORMAT(BGRA1010102),
319 FORMAT(YUYV),
320 FORMAT(YVYU),
321 FORMAT(UYVY),
322 FORMAT(VYUY),
323 FORMAT(AYUV),
324 FORMAT(NV12),
325 FORMAT(NV21),
326 FORMAT(NV16),
327 FORMAT(NV61),
328 FORMAT(YUV410),
329 FORMAT(YVU410),
330 FORMAT(YUV411),
331 FORMAT(YVU411),
332 FORMAT(YUV420),
333 FORMAT(YVU420),
334 FORMAT(YUV422),
335 FORMAT(YVU422),
336 FORMAT(YUV444),
337 FORMAT(YVU444),
338 { 0xffffffff, NULL }
339 };
340 #undef FORMAT
341
342 for (i = 0; formats[i].name; i++)
343 {
344 if (formats[i].format == format)
345 return formats[i].name;
346 }
347 return NULL;
348 }
349 #endif
350
351 static void
352 wl_shm_format (void *data,
353 struct wl_shm *wl_shm,
354 uint32_t format)
355 {
356 GDK_NOTE (MISC, g_message ("supported pixel format %s", get_format_name (format)));
357 }
358
359 static const struct wl_shm_listener wl_shm_listener = {
360 wl_shm_format
361 };
362
363 static void
364 server_decoration_manager_default_mode (void *data,
365 struct org_kde_kwin_server_decoration_manager *manager,
366 uint32_t mode)
367 {
368 g_assert (mode <= ORG_KDE_KWIN_SERVER_DECORATION_MANAGER_MODE_SERVER);
369 const char *modes[] = {
370 [ORG_KDE_KWIN_SERVER_DECORATION_MANAGER_MODE_NONE] = "none",
371 [ORG_KDE_KWIN_SERVER_DECORATION_MANAGER_MODE_CLIENT] = "client",
372 [ORG_KDE_KWIN_SERVER_DECORATION_MANAGER_MODE_SERVER] = "server",
373 };
374 GdkWaylandDisplay *display_wayland = data;
375 g_debug ("Compositor prefers decoration mode '%s'", modes[mode]);
376 display_wayland->server_decoration_mode = mode;
377 }
378
379 static const struct org_kde_kwin_server_decoration_manager_listener server_decoration_listener = {
380 .default_mode = server_decoration_manager_default_mode
381 };
382
383 gboolean
384 gdk_wayland_display_prefers_ssd (GdkDisplay *display)
385 {
386 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
387 if (display_wayland->server_decoration_manager)
388 return display_wayland->server_decoration_mode == ORG_KDE_KWIN_SERVER_DECORATION_MANAGER_MODE_SERVER;
389 return FALSE;
390 }
391
392 static void
393 gdk_registry_handle_global (void *data,
394 struct wl_registry *registry,
395 uint32_t id,
396 const char *interface,
397 uint32_t version)
398 {
399 GdkWaylandDisplay *display_wayland = data;
400 struct wl_output *output;
401
402 GDK_NOTE (MISC,
403 g_message ("add global %u, interface %s, version %u", id, interface, version));
404
405 if (strcmp (interface, "wl_compositor") == 0)
406 {
407 display_wayland->compositor =
408 wl_registry_bind (display_wayland->wl_registry, id, &wl_compositor_interface, MIN (version, 3));
409 display_wayland->compositor_version = MIN (version, 3);
410 }
411 else if (strcmp (interface, "wl_shm") == 0)
412 {
413 display_wayland->shm =
414 wl_registry_bind (display_wayland->wl_registry, id, &wl_shm_interface, 1);
415 wl_shm_add_listener (display_wayland->shm, &wl_shm_listener, display_wayland);
416 }
417 else if (strcmp (interface, "xdg_wm_base") == 0)
418 {
419 display_wayland->xdg_wm_base_id = id;
420 display_wayland->xdg_wm_base_version = version;
421 }
422 else if (strcmp (interface, "zxdg_shell_v6") == 0)
423 {
424 display_wayland->zxdg_shell_v6_id = id;
425 }
426 else if (strcmp (interface, "gtk_shell1") == 0)
427 {
428 display_wayland->gtk_shell =
429 wl_registry_bind(display_wayland->wl_registry, id,
430 &gtk_shell1_interface,
431 MIN (version, GTK_SHELL1_VERSION));
432 _gdk_wayland_screen_set_has_gtk_shell (display_wayland->screen);
433 display_wayland->gtk_shell_version = version;
434 }
435 else if (strcmp (interface, "wl_output") == 0)
436 {
437 output =
438 wl_registry_bind (display_wayland->wl_registry, id, &wl_output_interface, MIN (version, 2));
439 _gdk_wayland_screen_add_output (display_wayland->screen, id, output, MIN (version, 2));
440 _gdk_wayland_display_async_roundtrip (display_wayland);
441 }
442 else if (strcmp (interface, "wl_seat") == 0)
443 {
444 SeatAddedClosure *closure;
445 static const char *required_device_manager_globals[] = {
446 "wl_compositor",
447 "wl_data_device_manager",
448 NULL
449 };
450
451 closure = g_new0 (SeatAddedClosure, 1);
452 closure->base.handler = seat_added_closure_run;
453 closure->base.required_globals = required_device_manager_globals;
454 closure->id = id;
455 closure->version = version;
456 postpone_on_globals_closure (display_wayland, &closure->base);
457 }
458 else if (strcmp (interface, "wl_data_device_manager") == 0)
459 {
460 display_wayland->data_device_manager_version = MIN (version, 3);
461 display_wayland->data_device_manager =
462 wl_registry_bind (display_wayland->wl_registry, id, &wl_data_device_manager_interface,
463 display_wayland->data_device_manager_version);
464 }
465 else if (strcmp (interface, "wl_subcompositor") == 0)
466 {
467 display_wayland->subcompositor =
468 wl_registry_bind (display_wayland->wl_registry, id, &wl_subcompositor_interface, 1);
469 }
470 else if (strcmp (interface, "zwp_pointer_gestures_v1") == 0)
471 {
472 display_wayland->pointer_gestures =
473 wl_registry_bind (display_wayland->wl_registry,
474 id, &zwp_pointer_gestures_v1_interface,
475 MIN (version, GDK_ZWP_POINTER_GESTURES_V1_VERSION));
476 }
477 else if (strcmp (interface, "gtk_primary_selection_device_manager") == 0)
478 {
479 display_wayland->gtk_primary_selection_manager =
480 wl_registry_bind(display_wayland->wl_registry, id,
481 &gtk_primary_selection_device_manager_interface, 1);
482 }
483 else if (strcmp (interface, "zwp_primary_selection_device_manager_v1") == 0)
484 {
485 display_wayland->zwp_primary_selection_manager_v1 =
486 wl_registry_bind(display_wayland->wl_registry, id,
487 &zwp_primary_selection_device_manager_v1_interface, 1);
488 }
489 else if (strcmp (interface, "zwp_tablet_manager_v2") == 0)
490 {
491 display_wayland->tablet_manager =
492 wl_registry_bind(display_wayland->wl_registry, id,
493 &zwp_tablet_manager_v2_interface, 1);
494 }
495 else if (strcmp (interface, "zxdg_exporter_v1") == 0)
496 {
497 display_wayland->xdg_exporter_v1 =
498 wl_registry_bind (display_wayland->wl_registry, id,
499 &zxdg_exporter_v1_interface, 1);
500 }
501 else if (strcmp (interface, "zxdg_importer_v1") == 0)
502 {
503 display_wayland->xdg_importer_v1 =
504 wl_registry_bind (display_wayland->wl_registry, id,
505 &zxdg_importer_v1_interface, 1);
506 }
507 else if (strcmp (interface, "zxdg_exporter_v2") == 0)
508 {
509 display_wayland->xdg_exporter_v2 =
510 wl_registry_bind (display_wayland->wl_registry, id,
511 &zxdg_exporter_v2_interface, 1);
512 }
513 else if (strcmp (interface, "zxdg_importer_v2") == 0)
514 {
515 display_wayland->xdg_importer_v2 =
516 wl_registry_bind (display_wayland->wl_registry, id,
517 &zxdg_importer_v2_interface, 1);
518 }
519 else if (strcmp (interface, "zwp_keyboard_shortcuts_inhibit_manager_v1") == 0)
520 {
521 display_wayland->keyboard_shortcuts_inhibit =
522 wl_registry_bind (display_wayland->wl_registry, id,
523 &zwp_keyboard_shortcuts_inhibit_manager_v1_interface, 1);
524 }
525 else if (strcmp (interface, "org_kde_kwin_server_decoration_manager") == 0)
526 {
527 display_wayland->server_decoration_manager =
528 wl_registry_bind (display_wayland->wl_registry, id,
529 &org_kde_kwin_server_decoration_manager_interface, 1);
530 org_kde_kwin_server_decoration_manager_add_listener (display_wayland->server_decoration_manager,
531 &server_decoration_listener,
532 display_wayland);
533 }
534 else if (strcmp(interface, "zxdg_output_manager_v1") == 0)
535 {
536 display_wayland->xdg_output_manager_version = MIN (version, 3);
537 display_wayland->xdg_output_manager =
538 wl_registry_bind (display_wayland->wl_registry, id,
539 &zxdg_output_manager_v1_interface,
540 display_wayland->xdg_output_manager_version);
541 display_wayland->xdg_output_version = version;
542 _gdk_wayland_screen_init_xdg_output (display_wayland->screen);
543 _gdk_wayland_display_async_roundtrip (display_wayland);
544 }
545 #ifdef HAVE_XDG_ACTIVATION
546 else if (strcmp (interface, "xdg_activation_v1") == 0)
547 {
548 display_wayland->xdg_activation_version =
549 MIN (version, XDG_ACTIVATION_VERSION);
550 display_wayland->xdg_activation =
551 wl_registry_bind (display_wayland->wl_registry, id,
552 &xdg_activation_v1_interface,
553 display_wayland->xdg_activation_version);
554 }
555 #endif
556 else if (strcmp (interface, wp_cursor_shape_manager_v1_interface.name) == 0)
557 {
558 display_wayland->cursor_shape =
559 wl_registry_bind (display_wayland->wl_registry, id,
560 &wp_cursor_shape_manager_v1_interface, 1);
561 }
562
563 g_hash_table_insert (display_wayland->known_globals,
564 GUINT_TO_POINTER (id), g_strdup (interface));
565 }
566
567 static void
568 gdk_registry_handle_global_remove (void *data,
569 struct wl_registry *registry,
570 uint32_t id)
571 {
572 GdkWaylandDisplay *display_wayland = data;
573 GdkDisplay *display = GDK_DISPLAY (display_wayland);
574
575 GDK_NOTE (MISC, g_message ("remove global %u", id));
576 _gdk_wayland_device_manager_remove_seat (display->device_manager, id);
577 _gdk_wayland_screen_remove_output (display_wayland->screen, id);
578
579 g_hash_table_remove (display_wayland->known_globals, GUINT_TO_POINTER (id));
580
581 /* FIXME: the object needs to be destroyed here, we're leaking */
582 }
583
584 static const struct wl_registry_listener registry_listener = {
585 gdk_registry_handle_global,
586 gdk_registry_handle_global_remove
587 };
588
589 static void
590 log_handler (const char *format, va_list args)
591 {
592 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
593 }
594
595 static void
596 load_cursor_theme_closure_run (GdkWaylandDisplay *display_wayland,
597 OnHasGlobalsClosure *closure)
598 {
599 _gdk_wayland_display_load_cursor_theme (display_wayland);
600 }
601
602 static void
603 _gdk_wayland_display_prepare_cursor_themes (GdkWaylandDisplay *display_wayland)
604 {
605 OnHasGlobalsClosure *closure;
606 static const char *required_cursor_theme_globals[] = {
607 "wl_shm",
608 NULL
609 };
610
611 closure = g_new0 (OnHasGlobalsClosure, 1);
612 closure->handler = load_cursor_theme_closure_run;
613 closure->required_globals = required_cursor_theme_globals;
614 postpone_on_globals_closure (display_wayland, closure);
615 }
616
617 GdkDisplay *
618 _gdk_wayland_display_open (const gchar *display_name)
619 {
620 struct wl_display *wl_display;
621 GdkDisplay *display;
622 GdkWaylandDisplay *display_wayland;
623
624 GDK_NOTE (MISC, g_message ("opening display %s", display_name ? display_name : ""));
625
626 wl_log_set_handler_client (log_handler);
627
628 wl_display = wl_display_connect (display_name);
629 if (!wl_display)
630 return NULL;
631
632 display = g_object_new (GDK_TYPE_WAYLAND_DISPLAY, NULL);
633 display->device_manager = _gdk_wayland_device_manager_new (display);
634
635 display_wayland = GDK_WAYLAND_DISPLAY (display);
636 display_wayland->wl_display = wl_display;
637 display_wayland->screen = _gdk_wayland_screen_new (display);
638 display_wayland->event_source = _gdk_wayland_display_event_source_new (display);
639
640 display_wayland->known_globals =
641 g_hash_table_new_full (NULL, NULL, NULL, g_free);
642
643 _gdk_wayland_display_init_cursors (display_wayland);
644 _gdk_wayland_display_prepare_cursor_themes (display_wayland);
645
646 display_wayland->wl_registry = wl_display_get_registry (display_wayland->wl_display);
647 wl_registry_add_listener (display_wayland->wl_registry, &registry_listener, display_wayland);
648 if (wl_display_roundtrip (display_wayland->wl_display) < 0)
649 {
650 g_object_unref (display);
651 return NULL;
652 }
653
654 process_on_globals_closures (display_wayland);
655 display_wayland->selection = gdk_wayland_selection_new ();
656
657 /* Wait for initializing to complete. This means waiting for all
658 * asynchrounous roundtrips that were triggered during initial roundtrip. */
659 while (g_list_length (display_wayland->async_roundtrips) > 0)
660 {
661 if (wl_display_dispatch (display_wayland->wl_display) < 0)
662 {
663 g_object_unref (display);
664 return NULL;
665 }
666 }
667
668 if (display_wayland->xdg_wm_base_id)
669 {
670 display_wayland->shell_variant = GDK_WAYLAND_SHELL_VARIANT_XDG_SHELL;
671 display_wayland->xdg_wm_base =
672 wl_registry_bind (display_wayland->wl_registry,
673 display_wayland->xdg_wm_base_id,
674 &xdg_wm_base_interface,
675 MIN (display_wayland->xdg_wm_base_version, XDG_WM_BASE_VERSION));
676 xdg_wm_base_add_listener (display_wayland->xdg_wm_base,
677 &xdg_wm_base_listener,
678 display_wayland);
679 }
680 else if (display_wayland->zxdg_shell_v6_id)
681 {
682 display_wayland->shell_variant = GDK_WAYLAND_SHELL_VARIANT_ZXDG_SHELL_V6;
683 display_wayland->zxdg_shell_v6 =
684 wl_registry_bind (display_wayland->wl_registry,
685 display_wayland->zxdg_shell_v6_id,
686 &zxdg_shell_v6_interface, 1);
687 zxdg_shell_v6_add_listener (display_wayland->zxdg_shell_v6,
688 &zxdg_shell_v6_listener,
689 display_wayland);
690 }
691 else
692 {
693 g_warning ("The Wayland compositor does not provide any supported shell interface, "
694 "not using Wayland display");
695 g_object_unref (display);
696
697 return NULL;
698 }
699
700 g_signal_emit_by_name (display, "opened");
701
702 return display;
703 }
704
705 static void
706 gdk_wayland_display_dispose (GObject *object)
707 {
708 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (object);
709
710 _gdk_screen_close (display_wayland->screen);
711
712 if (display_wayland->event_source)
713 {
714 g_source_destroy (display_wayland->event_source);
715 g_source_unref (display_wayland->event_source);
716 display_wayland->event_source = NULL;
717 }
718
719 if (display_wayland->selection)
720 {
721 gdk_wayland_selection_free (display_wayland->selection);
722 display_wayland->selection = NULL;
723 }
724
725 g_clear_list (&display_wayland->async_roundtrips,
726 (GDestroyNotify) wl_callback_destroy);
727
728 if (display_wayland->known_globals)
729 {
730 g_hash_table_destroy (display_wayland->known_globals);
731 display_wayland->known_globals = NULL;
732 }
733
734 g_clear_list (&display_wayland->on_has_globals_closures, g_free);
735
736 G_OBJECT_CLASS (gdk_wayland_display_parent_class)->dispose (object);
737 }
738
739 static void
740 gdk_wayland_display_finalize (GObject *object)
741 {
742 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (object);
743 guint i;
744
745 _gdk_wayland_display_finalize_cursors (display_wayland);
746
747 g_object_unref (display_wayland->screen);
748
749 g_free (display_wayland->startup_notification_id);
750 g_free (display_wayland->cursor_theme_name);
751 xkb_context_unref (display_wayland->xkb_context);
752
753 for (i = 0; i < GDK_WAYLAND_THEME_SCALES_COUNT; i++)
754 {
755 if (display_wayland->scaled_cursor_themes[i])
756 {
757 wl_cursor_theme_destroy (display_wayland->scaled_cursor_themes[i]);
758 display_wayland->scaled_cursor_themes[i] = NULL;
759 }
760 }
761
762 g_ptr_array_free (display_wayland->monitors, TRUE);
763
764 wl_display_disconnect(display_wayland->wl_display);
765
766 G_OBJECT_CLASS (gdk_wayland_display_parent_class)->finalize (object);
767 }
768
769 static const gchar *
770 gdk_wayland_display_get_name (GdkDisplay *display)
771 {
772 const gchar *name;
773
774 name = g_getenv ("WAYLAND_DISPLAY");
775 if (name == NULL)
776 name = "wayland-0";
777
778 return name;
779 }
780
781 static GdkScreen *
782 gdk_wayland_display_get_default_screen (GdkDisplay *display)
783 {
784 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
785
786 return GDK_WAYLAND_DISPLAY (display)->screen;
787 }
788
789 void
790 gdk_wayland_display_system_bell (GdkDisplay *display,
791 GdkWindow *window)
792 {
793 GdkWaylandDisplay *display_wayland;
794 struct gtk_surface1 *gtk_surface;
795 gint64 now_ms;
796
797 g_return_if_fail (GDK_IS_DISPLAY (display));
798
799 display_wayland = GDK_WAYLAND_DISPLAY (display);
800
801 if (!display_wayland->gtk_shell)
802 return;
803
804 if (window)
805 gtk_surface = gdk_wayland_window_get_gtk_surface (window);
806 else
807 gtk_surface = NULL;
808
809 now_ms = g_get_monotonic_time () / 1000;
810 if (now_ms - display_wayland->last_bell_time_ms < MIN_SYSTEM_BELL_DELAY_MS)
811 return;
812
813 display_wayland->last_bell_time_ms = now_ms;
814
815 gtk_shell1_system_bell (display_wayland->gtk_shell, gtk_surface);
816 }
817
818 static void
819 gdk_wayland_display_beep (GdkDisplay *display)
820 {
821 gdk_wayland_display_system_bell (display, NULL);
822 }
823
824 static void
825 gdk_wayland_display_sync (GdkDisplay *display)
826 {
827 GdkWaylandDisplay *display_wayland;
828
829 g_return_if_fail (GDK_IS_DISPLAY (display));
830
831 display_wayland = GDK_WAYLAND_DISPLAY (display);
832
833 wl_display_roundtrip (display_wayland->wl_display);
834 }
835
836 static void
837 gdk_wayland_display_flush (GdkDisplay *display)
838 {
839 g_return_if_fail (GDK_IS_DISPLAY (display));
840
841 if (!display->closed)
842 wl_display_flush (GDK_WAYLAND_DISPLAY (display)->wl_display);
843 }
844
845 static void
846 gdk_wayland_display_make_default (GdkDisplay *display)
847 {
848 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
849 const gchar *startup_id;
850
851 g_free (display_wayland->startup_notification_id);
852 display_wayland->startup_notification_id = NULL;
853
854 startup_id = gdk_get_desktop_startup_id ();
855 if (startup_id)
856 display_wayland->startup_notification_id = g_strdup (startup_id);
857 }
858
859 static gboolean
860 gdk_wayland_display_has_pending (GdkDisplay *display)
861 {
862 return FALSE;
863 }
864
865 static GdkWindow *
866 gdk_wayland_display_get_default_group (GdkDisplay *display)
867 {
868 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
869
870 return NULL;
871 }
872
873
874 static gboolean
875 gdk_wayland_display_supports_selection_notification (GdkDisplay *display)
876 {
877 return FALSE;
878 }
879
880 static gboolean
881 gdk_wayland_display_request_selection_notification (GdkDisplay *display,
882 GdkAtom selection)
883
884 {
885 return FALSE;
886 }
887
888 static gboolean
889 gdk_wayland_display_supports_clipboard_persistence (GdkDisplay *display)
890 {
891 return FALSE;
892 }
893
894 static void
895 gdk_wayland_display_store_clipboard (GdkDisplay *display,
896 GdkWindow *clipboard_window,
897 guint32 time_,
898 const GdkAtom *targets,
899 gint n_targets)
900 {
901 }
902
903 static gboolean
904 gdk_wayland_display_supports_shapes (GdkDisplay *display)
905 {
906 return FALSE;
907 }
908
909 static gboolean
910 gdk_wayland_display_supports_input_shapes (GdkDisplay *display)
911 {
912 return TRUE;
913 }
914
915 static gboolean
916 gdk_wayland_display_supports_composite (GdkDisplay *display)
917 {
918 return FALSE;
919 }
920
921 static void
922 gdk_wayland_display_before_process_all_updates (GdkDisplay *display)
923 {
924 }
925
926 static void
927 gdk_wayland_display_after_process_all_updates (GdkDisplay *display)
928 {
929 /* Post the damage here instead? */
930 }
931
932 static gulong
933 gdk_wayland_display_get_next_serial (GdkDisplay *display)
934 {
935 static gulong serial = 0;
936 return ++serial;
937 }
938
939 /**
940 * gdk_wayland_display_set_startup_notification_id:
941 * @display: (type GdkWaylandDisplay): a #GdkDisplay
942 * @startup_id: the startup notification ID (must be valid utf8)
943 *
944 * Sets the startup notification ID for a display.
945 *
946 * This is usually taken from the value of the DESKTOP_STARTUP_ID
947 * environment variable, but in some cases (such as the application not
948 * being launched using exec()) it can come from other sources.
949 *
950 * The startup ID is also what is used to signal that the startup is
951 * complete (for example, when opening a window or when calling
952 * gdk_notify_startup_complete()).
953 *
954 * Since: 3.22
955 **/
956 void
957 gdk_wayland_display_set_startup_notification_id (GdkDisplay *display,
958 const char *startup_id)
959 {
960 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
961
962 g_free (display_wayland->startup_notification_id);
963 display_wayland->startup_notification_id = g_strdup (startup_id);
964 }
965
966 static void
967 gdk_wayland_display_notify_startup_complete (GdkDisplay *display,
968 const gchar *startup_id)
969 {
970 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
971
972 #ifdef HAVE_XDG_ACTIVATION
973 /* Will be signaled with focus activation */
974 if (display_wayland->xdg_activation)
975 return;
976 #endif
977
978 if (startup_id == NULL)
979 {
980 startup_id = display_wayland->startup_notification_id;
981
982 if (startup_id == NULL)
983 return;
984 }
985
986 #ifdef HAVE_XDG_ACTIVATION
987 if (display_wayland->xdg_activation) /* FIXME: Isn't this redundant? */
988 return;
989 #endif
990 if (display_wayland->gtk_shell)
991 gtk_shell1_set_startup_id (display_wayland->gtk_shell, startup_id);
992 }
993
994 static GdkKeymap *
995 _gdk_wayland_display_get_keymap (GdkDisplay *display)
996 {
997 GdkDevice *core_keyboard = NULL;
998 static GdkKeymap *tmp_keymap = NULL;
999
1000 core_keyboard = gdk_seat_get_keyboard (gdk_display_get_default_seat (display));
1001
1002 if (core_keyboard && tmp_keymap)
1003 {
1004 g_object_unref (tmp_keymap);
1005 tmp_keymap = NULL;
1006 }
1007
1008 if (core_keyboard)
1009 return _gdk_wayland_device_get_keymap (core_keyboard);
1010
1011 if (!tmp_keymap)
1012 tmp_keymap = _gdk_wayland_keymap_new ();
1013
1014 return tmp_keymap;
1015 }
1016
1017 static void
1018 gdk_wayland_display_push_error_trap (GdkDisplay *display)
1019 {
1020 }
1021
1022 static gint
1023 gdk_wayland_display_pop_error_trap (GdkDisplay *display,
1024 gboolean ignored)
1025 {
1026 return 0;
1027 }
1028
1029 static int
1030 gdk_wayland_display_get_n_monitors (GdkDisplay *display)
1031 {
1032 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
1033
1034 return display_wayland->monitors->len;
1035 }
1036
1037 static GdkMonitor *
1038 gdk_wayland_display_get_monitor (GdkDisplay *display,
1039 int monitor_num)
1040 {
1041 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
1042
1043 if (monitor_num < 0 || monitor_num >= display_wayland->monitors->len)
1044 return NULL;
1045
1046 return (GdkMonitor *)display_wayland->monitors->pdata[monitor_num];
1047 }
1048
1049 static GdkMonitor *
1050 gdk_wayland_display_get_monitor_at_window (GdkDisplay *display,
1051 GdkWindow *window)
1052 {
1053 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
1054 struct wl_output *output;
1055 int i;
1056
1057 g_return_val_if_fail (GDK_IS_WAYLAND_WINDOW (window), NULL);
1058
1059 output = gdk_wayland_window_get_wl_output (window);
1060 if (output == NULL)
1061 return NULL;
1062
1063 for (i = 0; i < display_wayland->monitors->len; i++)
1064 {
1065 GdkMonitor *monitor = display_wayland->monitors->pdata[i];
1066
1067 if (gdk_wayland_monitor_get_wl_output (monitor) == output)
1068 return monitor;
1069 }
1070
1071 return NULL;
1072 }
1073
1074 static void
1075 gdk_wayland_display_class_init (GdkWaylandDisplayClass *class)
1076 {
1077 GObjectClass *object_class = G_OBJECT_CLASS (class);
1078 GdkDisplayClass *display_class = GDK_DISPLAY_CLASS (class);
1079
1080 object_class->dispose = gdk_wayland_display_dispose;
1081 object_class->finalize = gdk_wayland_display_finalize;
1082
1083 display_class->window_type = gdk_wayland_window_get_type ();
1084 display_class->get_name = gdk_wayland_display_get_name;
1085 display_class->get_default_screen = gdk_wayland_display_get_default_screen;
1086 display_class->beep = gdk_wayland_display_beep;
1087 display_class->sync = gdk_wayland_display_sync;
1088 display_class->flush = gdk_wayland_display_flush;
1089 display_class->make_default = gdk_wayland_display_make_default;
1090 display_class->has_pending = gdk_wayland_display_has_pending;
1091 display_class->queue_events = _gdk_wayland_display_queue_events;
1092 display_class->get_default_group = gdk_wayland_display_get_default_group;
1093 display_class->supports_selection_notification = gdk_wayland_display_supports_selection_notification;
1094 display_class->request_selection_notification = gdk_wayland_display_request_selection_notification;
1095 display_class->supports_clipboard_persistence = gdk_wayland_display_supports_clipboard_persistence;
1096 display_class->store_clipboard = gdk_wayland_display_store_clipboard;
1097 display_class->supports_shapes = gdk_wayland_display_supports_shapes;
1098 display_class->supports_input_shapes = gdk_wayland_display_supports_input_shapes;
1099 display_class->supports_composite = gdk_wayland_display_supports_composite;
1100 display_class->get_app_launch_context = _gdk_wayland_display_get_app_launch_context;
1101 display_class->get_default_cursor_size = _gdk_wayland_display_get_default_cursor_size;
1102 display_class->get_maximal_cursor_size = _gdk_wayland_display_get_maximal_cursor_size;
1103 display_class->get_cursor_for_type = _gdk_wayland_display_get_cursor_for_type;
1104 display_class->get_cursor_for_name = _gdk_wayland_display_get_cursor_for_name;
1105 display_class->get_cursor_for_surface = _gdk_wayland_display_get_cursor_for_surface;
1106 display_class->supports_cursor_alpha = _gdk_wayland_display_supports_cursor_alpha;
1107 display_class->supports_cursor_color = _gdk_wayland_display_supports_cursor_color;
1108 display_class->before_process_all_updates = gdk_wayland_display_before_process_all_updates;
1109 display_class->after_process_all_updates = gdk_wayland_display_after_process_all_updates;
1110 display_class->get_next_serial = gdk_wayland_display_get_next_serial;
1111 display_class->notify_startup_complete = gdk_wayland_display_notify_startup_complete;
1112 display_class->create_window_impl = _gdk_wayland_display_create_window_impl;
1113 display_class->get_keymap = _gdk_wayland_display_get_keymap;
1114 display_class->push_error_trap = gdk_wayland_display_push_error_trap;
1115 display_class->pop_error_trap = gdk_wayland_display_pop_error_trap;
1116 display_class->get_selection_owner = _gdk_wayland_display_get_selection_owner;
1117 display_class->set_selection_owner = _gdk_wayland_display_set_selection_owner;
1118 display_class->send_selection_notify = _gdk_wayland_display_send_selection_notify;
1119 display_class->get_selection_property = _gdk_wayland_display_get_selection_property;
1120 display_class->convert_selection = _gdk_wayland_display_convert_selection;
1121 display_class->text_property_to_utf8_list = _gdk_wayland_display_text_property_to_utf8_list;
1122 display_class->utf8_to_string_target = _gdk_wayland_display_utf8_to_string_target;
1123
1124 display_class->is_gl_context_current = gdk_wayland_display_is_gl_context_current;
1125 display_class->make_gl_context_current = gdk_wayland_display_make_gl_context_current;
1126
1127 display_class->get_n_monitors = gdk_wayland_display_get_n_monitors;
1128 display_class->get_monitor = gdk_wayland_display_get_monitor;
1129 display_class->get_monitor_at_window = gdk_wayland_display_get_monitor_at_window;
1130 }
1131
1132 static void
1133 gdk_wayland_display_init (GdkWaylandDisplay *display)
1134 {
1135 display->xkb_context = xkb_context_new (0);
1136 if (G_UNLIKELY (!display->xkb_context))
1137 g_error ("Failed to create XKB context");
1138
1139 display->monitors = g_ptr_array_new_with_free_func (g_object_unref);
1140 }
1141
1142 void
1143 gdk_wayland_display_set_cursor_theme (GdkDisplay *display,
1144 const gchar *name,
1145 gint size)
1146 {
1147 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY(display);
1148 struct wl_cursor_theme *theme;
1149 int i;
1150
1151 g_assert (display_wayland);
1152 g_assert (display_wayland->shm);
1153
1154 if (size == 0)
1155 size = 24;
1156
1157 if (g_strcmp0 (name, display_wayland->cursor_theme_name) == 0 &&
1158 display_wayland->cursor_theme_size == size)
1159 return;
1160
1161 theme = wl_cursor_theme_load (name, size, display_wayland->shm);
1162 if (theme == NULL)
1163 {
1164 g_warning ("Failed to load cursor theme %s", name);
1165 return;
1166 }
1167
1168 for (i = 0; i < GDK_WAYLAND_THEME_SCALES_COUNT; i++)
1169 {
1170 if (display_wayland->scaled_cursor_themes[i])
1171 {
1172 wl_cursor_theme_destroy (display_wayland->scaled_cursor_themes[i]);
1173 display_wayland->scaled_cursor_themes[i] = NULL;
1174 }
1175 }
1176 display_wayland->scaled_cursor_themes[0] = theme;
1177 if (display_wayland->cursor_theme_name != NULL)
1178 g_free (display_wayland->cursor_theme_name);
1179 display_wayland->cursor_theme_name = g_strdup (name);
1180 display_wayland->cursor_theme_size = size;
1181
1182 _gdk_wayland_display_update_cursors (display_wayland);
1183 }
1184
1185 struct wl_cursor_theme *
1186 _gdk_wayland_display_get_scaled_cursor_theme (GdkWaylandDisplay *display_wayland,
1187 guint scale)
1188 {
1189 struct wl_cursor_theme *theme;
1190
1191 g_assert (display_wayland->cursor_theme_name);
1192 g_assert (scale <= GDK_WAYLAND_MAX_THEME_SCALE);
1193 g_assert (scale >= 1);
1194
1195 theme = display_wayland->scaled_cursor_themes[scale - 1];
1196 if (!theme)
1197 {
1198 theme = wl_cursor_theme_load (display_wayland->cursor_theme_name,
1199 display_wayland->cursor_theme_size * scale,
1200 display_wayland->shm);
1201 if (theme == NULL)
1202 {
1203 g_warning ("Failed to load cursor theme %s with scale %u",
1204 display_wayland->cursor_theme_name, scale);
1205 return NULL;
1206 }
1207 display_wayland->scaled_cursor_themes[scale - 1] = theme;
1208 }
1209
1210 return theme;
1211 }
1212
1213 static void
1214 _gdk_wayland_display_load_cursor_theme (GdkWaylandDisplay *display_wayland)
1215 {
1216 guint size;
1217 const gchar *name;
1218 GValue v = G_VALUE_INIT;
1219
1220 g_assert (display_wayland);
1221 g_assert (display_wayland->shm);
1222
1223 g_value_init (&v, G_TYPE_INT);
1224 if (gdk_screen_get_setting (display_wayland->screen, "gtk-cursor-theme-size", &v))
1225 size = g_value_get_int (&v);
1226 else
1227 size = 24;
1228 g_value_unset (&v);
1229
1230 g_value_init (&v, G_TYPE_STRING);
1231 if (gdk_screen_get_setting (display_wayland->screen, "gtk-cursor-theme-name", &v))
1232 name = g_value_get_string (&v);
1233 else
1234 name = "default";
1235
1236 gdk_wayland_display_set_cursor_theme (GDK_DISPLAY (display_wayland), name, size);
1237 g_value_unset (&v);
1238 }
1239
1240 guint32
1241 _gdk_wayland_display_get_serial (GdkWaylandDisplay *display_wayland)
1242 {
1243 return display_wayland->serial;
1244 }
1245
1246 void
1247 _gdk_wayland_display_update_serial (GdkWaylandDisplay *display_wayland,
1248 guint32 serial)
1249 {
1250 display_wayland->serial = serial;
1251 }
1252
1253 /**
1254 * gdk_wayland_display_get_wl_display:
1255 * @display: (type GdkWaylandDisplay): a #GdkDisplay
1256 *
1257 * Returns the Wayland wl_display of a #GdkDisplay.
1258 *
1259 * Returns: (transfer none): a Wayland wl_display
1260 *
1261 * Since: 3.8
1262 */
1263 struct wl_display *
1264 gdk_wayland_display_get_wl_display (GdkDisplay *display)
1265 {
1266 g_return_val_if_fail (GDK_IS_WAYLAND_DISPLAY (display), NULL);
1267
1268 return GDK_WAYLAND_DISPLAY (display)->wl_display;
1269 }
1270
1271 /**
1272 * gdk_wayland_display_get_wl_compositor:
1273 * @display: (type GdkWaylandDisplay): a #GdkDisplay
1274 *
1275 * Returns the Wayland global singleton compositor of a #GdkDisplay.
1276 *
1277 * Returns: (transfer none): a Wayland wl_compositor
1278 *
1279 * Since: 3.8
1280 */
1281 struct wl_compositor *
1282 gdk_wayland_display_get_wl_compositor (GdkDisplay *display)
1283 {
1284 g_return_val_if_fail (GDK_IS_WAYLAND_DISPLAY (display), NULL);
1285
1286 return GDK_WAYLAND_DISPLAY (display)->compositor;
1287 }
1288
1289 static const cairo_user_data_key_t gdk_wayland_shm_surface_cairo_key;
1290
1291 typedef struct _GdkWaylandCairoSurfaceData {
1292 gpointer buf;
1293 size_t buf_length;
1294 struct wl_shm_pool *pool;
1295 struct wl_buffer *buffer;
1296 GdkWaylandDisplay *display;
1297 uint32_t scale;
1298 } GdkWaylandCairoSurfaceData;
1299
1300 static int
1301 open_shared_memory (void)
1302 {
1303 static gboolean force_shm_open = FALSE;
1304 int ret = -1;
1305
1306 #if !defined (__NR_memfd_create)
1307 force_shm_open = TRUE;
1308 #endif
1309
1310 do
1311 {
1312 #if defined (__NR_memfd_create)
1313 if (!force_shm_open)
1314 {
1315 ret = syscall (__NR_memfd_create, "gdk-wayland", MFD_CLOEXEC);
1316
1317 /* fall back to shm_open until debian stops shipping 3.16 kernel
1318 * See bug 766341
1319 */
1320 if (ret < 0 && errno == ENOSYS)
1321 force_shm_open = TRUE;
1322 }
1323 #endif
1324
1325 if (force_shm_open)
1326 {
1327 #if defined (__FreeBSD__)
1328 ret = shm_open (SHM_ANON, O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC, 0600);
1329 #else
1330 char name[NAME_MAX - 1] = "";
1331
1332 sprintf (name, "/gdk-wayland-%x", g_random_int ());
1333
1334 ret = shm_open (name, O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC, 0600);
1335
1336 if (ret >= 0)
1337 shm_unlink (name);
1338 else if (errno == EEXIST)
1339 continue;
1340 #endif
1341 }
1342 }
1343 while (ret < 0 && errno == EINTR);
1344
1345 if (ret < 0)
1346 g_critical (G_STRLOC ": creating shared memory file (using %s) failed: %m",
1347 force_shm_open? "shm_open" : "memfd_create");
1348
1349 return ret;
1350 }
1351
1352 static struct wl_shm_pool *
1353 create_shm_pool (struct wl_shm *shm,
1354 int size,
1355 size_t *buf_length,
1356 void **data_out)
1357 {
1358 struct wl_shm_pool *pool;
1359 int fd;
1360 void *data;
1361
1362 fd = open_shared_memory ();
1363
1364 if (fd < 0)
1365 return NULL;
1366
1367 if (ftruncate (fd, size) < 0)
1368 {
1369 g_critical (G_STRLOC ": Truncating shared memory file failed: %m");
1370 close (fd);
1371 return NULL;
1372 }
1373
1374 data = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1375
1376 if (data == MAP_FAILED)
1377 {
1378 g_critical (G_STRLOC ": mmap'ping shared memory file failed: %m");
1379 close (fd);
1380 return NULL;
1381 }
1382
1383 pool = wl_shm_create_pool (shm, fd, size);
1384
1385 close (fd);
1386
1387 *data_out = data;
1388 *buf_length = size;
1389
1390 return pool;
1391 }
1392
1393 static void
1394 gdk_wayland_cairo_surface_destroy (void *p)
1395 {
1396 GdkWaylandCairoSurfaceData *data = p;
1397
1398 if (data->buffer)
1399 wl_buffer_destroy (data->buffer);
1400
1401 if (data->pool)
1402 wl_shm_pool_destroy (data->pool);
1403
1404 munmap (data->buf, data->buf_length);
1405 g_free (data);
1406 }
1407
1408 cairo_surface_t *
1409 _gdk_wayland_display_create_shm_surface (GdkWaylandDisplay *display,
1410 int width,
1411 int height,
1412 guint scale)
1413 {
1414 GdkWaylandCairoSurfaceData *data;
1415 cairo_surface_t *surface = NULL;
1416 cairo_status_t status;
1417 int stride;
1418
1419 data = g_new (GdkWaylandCairoSurfaceData, 1);
1420 data->display = display;
1421 data->buffer = NULL;
1422 data->scale = scale;
1423
1424 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, width*scale);
1425
1426 data->pool = create_shm_pool (display->shm,
1427 height*scale*stride,
1428 &data->buf_length,
1429 &data->buf);
1430
1431 surface = cairo_image_surface_create_for_data (data->buf,
1432 CAIRO_FORMAT_ARGB32,
1433 width*scale,
1434 height*scale,
1435 stride);
1436
1437 data->buffer = wl_shm_pool_create_buffer (data->pool, 0,
1438 width*scale, height*scale,
1439 stride, WL_SHM_FORMAT_ARGB8888);
1440
1441 cairo_surface_set_user_data (surface, &gdk_wayland_shm_surface_cairo_key,
1442 data, gdk_wayland_cairo_surface_destroy);
1443
1444 cairo_surface_set_device_scale (surface, scale, scale);
1445
1446 status = cairo_surface_status (surface);
1447 if (status != CAIRO_STATUS_SUCCESS)
1448 {
1449 g_critical (G_STRLOC ": Unable to create Cairo image surface: %s",
1450 cairo_status_to_string (status));
1451 }
1452
1453 return surface;
1454 }
1455
1456 struct wl_buffer *
1457 _gdk_wayland_shm_surface_get_wl_buffer (cairo_surface_t *surface)
1458 {
1459 GdkWaylandCairoSurfaceData *data = cairo_surface_get_user_data (surface, &gdk_wayland_shm_surface_cairo_key);
1460 return data->buffer;
1461 }
1462
1463 gboolean
1464 _gdk_wayland_is_shm_surface (cairo_surface_t *surface)
1465 {
1466 return cairo_surface_get_user_data (surface, &gdk_wayland_shm_surface_cairo_key) != NULL;
1467 }
1468
1469 GdkWaylandSelection *
1470 gdk_wayland_display_get_selection (GdkDisplay *display)
1471 {
1472 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
1473
1474 return display_wayland->selection;
1475 }
1476
1477 /**
1478 * gdk_wayland_display_query_registry:
1479 * @display: a wayland #GdkDisplay
1480 * @interface: global interface to query in the registry
1481 *
1482 * Returns %TRUE if the the interface was found in the display
1483 * wl_registry.global handler.
1484 *
1485 * Returns: %TRUE if the global is offered by the compositor
1486 *
1487 * Since: 3.22.27
1488 **/
1489 gboolean
1490 gdk_wayland_display_query_registry (GdkDisplay *display,
1491 const gchar *global)
1492 {
1493 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
1494 GHashTableIter iter;
1495 gchar *value;
1496
1497 g_return_val_if_fail (GDK_IS_WAYLAND_DISPLAY (display), FALSE);
1498 g_return_val_if_fail (global != NULL, FALSE);
1499
1500 g_hash_table_iter_init (&iter, display_wayland->known_globals);
1501
1502 while (g_hash_table_iter_next (&iter, NULL, (gpointer*) &value))
1503 {
1504 if (strcmp (value, global) == 0)
1505 return TRUE;
1506 }
1507
1508 return FALSE;
1509 }