Flutter Linux Embedder
fl_wayland_display.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 #include <gdk/gdkwayland.h>
8 #include <wayland-client.h>
9 
10 // Key used to attach the #FlWaylandDisplay to the #GdkDisplay it was created
11 // for, so the Wayland globals are only bound once per display.
12 static const char* kWaylandDisplayKey = "fl-wayland-display";
13 
15  GObject parent_instance;
16 
17  // Wayland globals used to create subsurfaces.
18  struct wl_compositor* compositor;
19  struct wl_subcompositor* subcompositor;
20 };
21 
22 G_DEFINE_TYPE(FlWaylandDisplay, fl_wayland_display, G_TYPE_OBJECT)
23 
24 // Wayland registry handling.
25 static void registry_global(void* data,
26  struct wl_registry* registry,
27  uint32_t name,
28  const char* interface,
29  uint32_t version) {
30  FlWaylandDisplay* self = FL_WAYLAND_DISPLAY(data);
31  if (g_strcmp0(interface, wl_compositor_interface.name) == 0) {
32  self->compositor = static_cast<struct wl_compositor*>(wl_registry_bind(
33  registry, name, &wl_compositor_interface, MIN(version, 4)));
34  } else if (g_strcmp0(interface, wl_subcompositor_interface.name) == 0) {
35  self->subcompositor = static_cast<struct wl_subcompositor*>(
36  wl_registry_bind(registry, name, &wl_subcompositor_interface, 1));
37  }
38 }
39 
40 static void registry_global_remove(void* data,
41  struct wl_registry* registry,
42  uint32_t name) {}
43 
44 static const struct wl_registry_listener kRegistryListener = {
45  .global = registry_global,
46  .global_remove = registry_global_remove,
47 };
48 
49 // Binds the Wayland globals required by Flutter. Returns FALSE if any of them
50 // are not available.
51 static gboolean bind_globals(FlWaylandDisplay* self,
52  struct wl_display* display) {
53  struct wl_registry* registry = wl_display_get_registry(display);
54  wl_registry_add_listener(registry, &kRegistryListener, self);
55  wl_display_roundtrip(display);
56  wl_registry_destroy(registry);
57 
58  return self->compositor != nullptr && self->subcompositor != nullptr;
59 }
60 
61 static void fl_wayland_display_dispose(GObject* object) {
62  FlWaylandDisplay* self = FL_WAYLAND_DISPLAY(object);
63 
64  g_clear_pointer(&self->subcompositor, wl_subcompositor_destroy);
65  g_clear_pointer(&self->compositor, wl_compositor_destroy);
66 
67  G_OBJECT_CLASS(fl_wayland_display_parent_class)->dispose(object);
68 }
69 
70 static void fl_wayland_display_class_init(FlWaylandDisplayClass* klass) {
71  G_OBJECT_CLASS(klass)->dispose = fl_wayland_display_dispose;
72 }
73 
74 static void fl_wayland_display_init(FlWaylandDisplay* self) {}
75 
76 FlWaylandDisplay* fl_wayland_display_open(struct wl_display* wl_display) {
77  FlWaylandDisplay* self =
78  FL_WAYLAND_DISPLAY(g_object_new(fl_wayland_display_get_type(), nullptr));
79 
80  if (!bind_globals(self, wl_display)) {
81  g_warning("Required Wayland globals not available");
82  g_object_unref(self);
83  return nullptr;
84  }
85 
86  return self;
87 }
88 
89 FlWaylandDisplay* fl_wayland_display_get_for_display(GdkDisplay* gdk_display) {
90  g_return_val_if_fail(GDK_IS_WAYLAND_DISPLAY(gdk_display), nullptr);
91 
92  FlWaylandDisplay* self = FL_WAYLAND_DISPLAY(
93  g_object_get_data(G_OBJECT(gdk_display), kWaylandDisplayKey));
94  if (self != nullptr) {
95  return self;
96  }
97 
98  self =
99  fl_wayland_display_open(gdk_wayland_display_get_wl_display(gdk_display));
100  if (self == nullptr) {
101  return nullptr;
102  }
103 
104  // Owned by the #GdkDisplay, so the globals are shared by everything using
105  // that display and released with it.
106  g_object_set_data_full(G_OBJECT(gdk_display), kWaylandDisplayKey, self,
107  g_object_unref);
108 
109  return self;
110 }
111 
113  FlWaylandDisplay* self,
114  struct wl_surface* parent_surface) {
115  g_return_val_if_fail(FL_IS_WAYLAND_DISPLAY(self), nullptr);
116 
117  return fl_subsurface_new(self->compositor, self->subcompositor,
118  parent_surface);
119 }
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
FlSubsurface * fl_subsurface_new(struct wl_compositor *compositor, struct wl_subcompositor *subcompositor, struct wl_surface *parent_surface)
static void registry_global_remove(void *data, struct wl_registry *registry, uint32_t name)
static const char * kWaylandDisplayKey
FlSubsurface * fl_wayland_display_create_subsurface(FlWaylandDisplay *self, struct wl_surface *parent_surface)
static void registry_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version)
FlWaylandDisplay * fl_wayland_display_get_for_display(GdkDisplay *gdk_display)
static const struct wl_registry_listener kRegistryListener
FlWaylandDisplay * fl_wayland_display_open(struct wl_display *wl_display)
static gboolean bind_globals(FlWaylandDisplay *self, struct wl_display *display)
static void fl_wayland_display_dispose(GObject *object)
static void fl_wayland_display_init(FlWaylandDisplay *self)
static void fl_wayland_display_class_init(FlWaylandDisplayClass *klass)
struct wl_subcompositor * subcompositor
struct wl_compositor * compositor