1 /*
2 * GStreamer
3 * Copyright (C) 2013 Matthew Waters <ystreet00@gmail.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "gstgldisplay_wayland.h"
26 #include "gstgldisplay_wayland_private.h"
27
28 GST_DEBUG_CATEGORY_STATIC (gst_gl_display_debug);
29 #define GST_CAT_DEFAULT gst_gl_display_debug
30
31 /* We can't define these in the public struct, or we'd break ABI */
32 typedef struct _GstGLDisplayWaylandPrivate
33 {
34 struct xdg_wm_base *xdg_wm_base;
35 } GstGLDisplayWaylandPrivate;
36
37 G_DEFINE_TYPE_WITH_PRIVATE (GstGLDisplayWayland, gst_gl_display_wayland,
38 GST_TYPE_GL_DISPLAY);
39
40 static void gst_gl_display_wayland_finalize (GObject * object);
41 static guintptr gst_gl_display_wayland_get_handle (GstGLDisplay * display);
42
43 static void
handle_xdg_wm_base_ping(void * user_data,struct xdg_wm_base * xdg_wm_base,uint32_t serial)44 handle_xdg_wm_base_ping (void *user_data, struct xdg_wm_base *xdg_wm_base,
45 uint32_t serial)
46 {
47 xdg_wm_base_pong (xdg_wm_base, serial);
48 }
49
50 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
51 handle_xdg_wm_base_ping
52 };
53
54 static void
registry_handle_global(void * data,struct wl_registry * registry,uint32_t name,const char * interface,uint32_t version)55 registry_handle_global (void *data, struct wl_registry *registry,
56 uint32_t name, const char *interface, uint32_t version)
57 {
58 GstGLDisplayWayland *display = data;
59 GstGLDisplayWaylandPrivate *priv =
60 gst_gl_display_wayland_get_instance_private (display);
61
62 GST_DEBUG_CATEGORY_GET (gst_gl_display_debug, "gldisplay");
63
64 GST_TRACE_OBJECT (display, "registry_handle_global with registry %p, "
65 "interface %s, version %u", registry, interface, version);
66
67 if (g_strcmp0 (interface, "wl_compositor") == 0) {
68 display->compositor =
69 wl_registry_bind (registry, name, &wl_compositor_interface, 1);
70 } else if (g_strcmp0 (interface, "wl_subcompositor") == 0) {
71 display->subcompositor =
72 wl_registry_bind (registry, name, &wl_subcompositor_interface, 1);
73 } else if (g_strcmp0 (interface, "xdg_wm_base") == 0) {
74 priv->xdg_wm_base =
75 wl_registry_bind (registry, name, &xdg_wm_base_interface, 1);
76 xdg_wm_base_add_listener (priv->xdg_wm_base, &xdg_wm_base_listener,
77 display);
78 } else if (g_strcmp0 (interface, "wl_shell") == 0) {
79 display->shell = wl_registry_bind (registry, name, &wl_shell_interface, 1);
80 }
81 }
82
83 static const struct wl_registry_listener registry_listener = {
84 registry_handle_global
85 };
86
87 static void
_connect_listeners(GstGLDisplayWayland * display)88 _connect_listeners (GstGLDisplayWayland * display)
89 {
90 display->registry = wl_display_get_registry (display->display);
91 wl_registry_add_listener (display->registry, ®istry_listener, display);
92
93 wl_display_roundtrip (display->display);
94 }
95
96 static void
gst_gl_display_wayland_class_init(GstGLDisplayWaylandClass * klass)97 gst_gl_display_wayland_class_init (GstGLDisplayWaylandClass * klass)
98 {
99 GST_GL_DISPLAY_CLASS (klass)->get_handle =
100 GST_DEBUG_FUNCPTR (gst_gl_display_wayland_get_handle);
101
102 G_OBJECT_CLASS (klass)->finalize = gst_gl_display_wayland_finalize;
103 }
104
105 static void
gst_gl_display_wayland_init(GstGLDisplayWayland * display_wayland)106 gst_gl_display_wayland_init (GstGLDisplayWayland * display_wayland)
107 {
108 GstGLDisplay *display = (GstGLDisplay *) display_wayland;
109
110 display->type = GST_GL_DISPLAY_TYPE_WAYLAND;
111 display_wayland->foreign_display = FALSE;
112 }
113
114 static void
gst_gl_display_wayland_finalize(GObject * object)115 gst_gl_display_wayland_finalize (GObject * object)
116 {
117 GstGLDisplayWayland *display_wayland = GST_GL_DISPLAY_WAYLAND (object);
118 GstGLDisplayWaylandPrivate *priv =
119 gst_gl_display_wayland_get_instance_private (display_wayland);
120
121 g_clear_pointer (&display_wayland->shell, wl_shell_destroy);
122 g_clear_pointer (&priv->xdg_wm_base, xdg_wm_base_destroy);
123
124 /* Cause eglTerminate() to occur before wl_display_disconnect()
125 * https://bugzilla.gnome.org/show_bug.cgi?id=787293 */
126 g_object_set_data (object, "gst.gl.display.egl", NULL);
127
128 if (!display_wayland->foreign_display && display_wayland->display) {
129 wl_display_flush (display_wayland->display);
130 wl_display_disconnect (display_wayland->display);
131 }
132
133 G_OBJECT_CLASS (gst_gl_display_wayland_parent_class)->finalize (object);
134 }
135
136 /**
137 * gst_gl_display_wayland_new:
138 * @name: (allow-none): a display name
139 *
140 * Create a new #GstGLDisplayWayland from the wayland display name. See wl_display_connect()
141 * for details on what is a valid name.
142 *
143 * Returns: (transfer full): a new #GstGLDisplayWayland or %NULL
144 */
145 GstGLDisplayWayland *
gst_gl_display_wayland_new(const gchar * name)146 gst_gl_display_wayland_new (const gchar * name)
147 {
148 GstGLDisplayWayland *ret;
149
150 GST_DEBUG_CATEGORY_GET (gst_gl_display_debug, "gldisplay");
151
152 ret = g_object_new (GST_TYPE_GL_DISPLAY_WAYLAND, NULL);
153 gst_object_ref_sink (ret);
154 ret->display = wl_display_connect (name);
155
156 if (!ret->display) {
157 if (name != NULL) {
158 GST_ERROR ("Failed to open Wayland display connection with name \'%s\'",
159 name);
160 } else {
161 GST_INFO ("Failed to open Wayland display connection.");
162 }
163 gst_object_unref (ret);
164 return NULL;
165 }
166
167 _connect_listeners (ret);
168
169 return ret;
170 }
171
172 /**
173 * gst_gl_display_wayland_new_with_display:
174 * @display: an existing, wayland display
175 *
176 * Creates a new display connection from a wl_display Display.
177 *
178 * Returns: (transfer full): a new #GstGLDisplayWayland
179 */
180 GstGLDisplayWayland *
gst_gl_display_wayland_new_with_display(struct wl_display * display)181 gst_gl_display_wayland_new_with_display (struct wl_display * display)
182 {
183 GstGLDisplayWayland *ret;
184
185 g_return_val_if_fail (display != NULL, NULL);
186
187 GST_DEBUG_CATEGORY_GET (gst_gl_display_debug, "gldisplay");
188
189 ret = g_object_new (GST_TYPE_GL_DISPLAY_WAYLAND, NULL);
190 gst_object_ref_sink (ret);
191
192 ret->display = display;
193 ret->foreign_display = TRUE;
194
195 _connect_listeners (ret);
196
197 return ret;
198 }
199
200 static guintptr
gst_gl_display_wayland_get_handle(GstGLDisplay * display)201 gst_gl_display_wayland_get_handle (GstGLDisplay * display)
202 {
203 return (guintptr) GST_GL_DISPLAY_WAYLAND (display)->display;
204 }
205
206 struct xdg_wm_base *
gst_gl_display_wayland_get_xdg_wm_base(GstGLDisplayWayland * display)207 gst_gl_display_wayland_get_xdg_wm_base (GstGLDisplayWayland * display)
208 {
209 GstGLDisplayWaylandPrivate *priv =
210 gst_gl_display_wayland_get_instance_private (display);
211
212 return priv->xdg_wm_base;
213 }
214