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 /**
22 * SECTION:gstgldisplay_x11
23 * @short_description: X11 Display connection
24 * @title: GstGLDisplayX11
25 * @see_also: #GstGLDisplay
26 *
27 * #GstGLDisplayX11 represents a connection to an X11 `Display` handle created
28 * internally (gst_gl_display_x11_new()) or wrapped by the application
29 * (gst_gl_display_x11_new_with_display())
30 */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <gst/gl/x11/gstgldisplay_x11.h>
37 #include <gst/gl/x11/gstglwindow_x11.h>
38 #include "xcb_event_source.h"
39
40 GST_DEBUG_CATEGORY_STATIC (gst_gl_display_debug);
41 #define GST_CAT_DEFAULT gst_gl_display_debug
42
43 G_DEFINE_TYPE (GstGLDisplayX11, gst_gl_display_x11, GST_TYPE_GL_DISPLAY);
44
45 static void gst_gl_display_x11_finalize (GObject * object);
46 static guintptr gst_gl_display_x11_get_handle (GstGLDisplay * display);
47
48 G_GNUC_INTERNAL
49 gboolean gst_gl_display_x11_handle_event (GstGLDisplayX11 * display_x11);
50
51 extern gboolean gst_gl_window_x11_handle_event (GstGLWindowX11 * window_x11,
52 xcb_generic_event_t * event);
53
54 static void
gst_gl_display_x11_class_init(GstGLDisplayX11Class * klass)55 gst_gl_display_x11_class_init (GstGLDisplayX11Class * klass)
56 {
57 GST_GL_DISPLAY_CLASS (klass)->get_handle =
58 GST_DEBUG_FUNCPTR (gst_gl_display_x11_get_handle);
59
60 G_OBJECT_CLASS (klass)->finalize = gst_gl_display_x11_finalize;
61 }
62
63 static void
gst_gl_display_x11_init(GstGLDisplayX11 * display_x11)64 gst_gl_display_x11_init (GstGLDisplayX11 * display_x11)
65 {
66 GstGLDisplay *display = (GstGLDisplay *) display_x11;
67
68 display->type = GST_GL_DISPLAY_TYPE_X11;
69 display_x11->foreign_display = FALSE;
70 }
71
72 static void
gst_gl_display_x11_finalize(GObject * object)73 gst_gl_display_x11_finalize (GObject * object)
74 {
75 GstGLDisplayX11 *display_x11 = GST_GL_DISPLAY_X11 (object);
76
77 g_free (display_x11->name);
78
79 if (!display_x11->foreign_display && display_x11->display) {
80 XSync (display_x11->display, FALSE);
81 XCloseDisplay (display_x11->display);
82 }
83
84 G_OBJECT_CLASS (gst_gl_display_x11_parent_class)->finalize (object);
85 }
86
87 /**
88 * gst_gl_display_x11_new:
89 * @name: (allow-none): a display name
90 *
91 * Create a new #GstGLDisplayX11 from the x11 display name. See `XOpenDisplay`()
92 * for details on what is a valid name.
93 *
94 * Returns: (transfer full): a new #GstGLDisplayX11 or %NULL
95 */
96 GstGLDisplayX11 *
gst_gl_display_x11_new(const gchar * name)97 gst_gl_display_x11_new (const gchar * name)
98 {
99 GstGLDisplayX11 *ret;
100
101 GST_DEBUG_CATEGORY_GET (gst_gl_display_debug, "gldisplay");
102
103 ret = g_object_new (GST_TYPE_GL_DISPLAY_X11, NULL);
104 gst_object_ref_sink (ret);
105 ret->name = g_strdup (name);
106 ret->display = XOpenDisplay (ret->name);
107
108 if (!ret->display) {
109 GST_INFO ("Failed to open X11 display connection with name, \'%s\'", name);
110 gst_object_unref (ret);
111 return NULL;
112 }
113
114 ret->xcb_connection = XGetXCBConnection (ret->display);
115 if (!ret->xcb_connection) {
116 GST_ERROR ("Failed to retrieve XCB connection from X11 Display");
117 gst_object_unref (ret);
118 return NULL;
119 }
120
121 XSetEventQueueOwner (ret->display, XCBOwnsEventQueue);
122
123 GST_GL_DISPLAY (ret)->event_source = xcb_event_source_new (ret);
124 g_source_attach (GST_GL_DISPLAY (ret)->event_source,
125 GST_GL_DISPLAY (ret)->main_context);
126
127 return ret;
128 }
129
130 /**
131 * gst_gl_display_x11_new_with_display: (skip)
132 * @display: an existing, x11 display
133 *
134 * Creates a new display connection from a X11 Display.
135 *
136 * Returns: (transfer full): a new #GstGLDisplayX11
137 */
138 GstGLDisplayX11 *
gst_gl_display_x11_new_with_display(Display * display)139 gst_gl_display_x11_new_with_display (Display * display)
140 {
141 GstGLDisplayX11 *ret;
142
143 g_return_val_if_fail (display != NULL, NULL);
144
145 GST_DEBUG_CATEGORY_GET (gst_gl_display_debug, "gldisplay");
146
147 ret = g_object_new (GST_TYPE_GL_DISPLAY_X11, NULL);
148 gst_object_ref_sink (ret);
149
150 ret->name = g_strdup (DisplayString (display));
151 ret->display = display;
152
153 ret->xcb_connection = XGetXCBConnection (ret->display);
154 if (!ret->xcb_connection) {
155 GST_ERROR ("Failed to retrieve XCB connection from X11 Display");
156 gst_object_unref (ret);
157 return NULL;
158 }
159
160 ret->foreign_display = TRUE;
161
162 return ret;
163 }
164
165 static guintptr
gst_gl_display_x11_get_handle(GstGLDisplay * display)166 gst_gl_display_x11_get_handle (GstGLDisplay * display)
167 {
168 return (guintptr) GST_GL_DISPLAY_X11 (display)->display;
169 }
170
171 static int
_compare_xcb_window(GstGLWindowX11 * window_x11,xcb_window_t * window_id)172 _compare_xcb_window (GstGLWindowX11 * window_x11, xcb_window_t * window_id)
173 {
174 return window_x11->internal_win_id - *window_id;
175 }
176
177 static GstGLWindowX11 *
_find_window_from_xcb_window(GstGLDisplayX11 * display_x11,xcb_window_t window_id)178 _find_window_from_xcb_window (GstGLDisplayX11 * display_x11,
179 xcb_window_t window_id)
180 {
181 GstGLDisplay *display = GST_GL_DISPLAY (display_x11);
182 GstGLWindow *window = NULL;
183
184 if (!window_id)
185 return NULL;
186
187 window = gst_gl_display_retrieve_window (display, &window_id,
188 (GCompareFunc) _compare_xcb_window);
189
190 return (GstGLWindowX11 *) window;
191 }
192
193 static GstGLWindowX11 *
_window_from_event(GstGLDisplayX11 * display_x11,xcb_generic_event_t * event)194 _window_from_event (GstGLDisplayX11 * display_x11, xcb_generic_event_t * event)
195 {
196 uint8_t event_code = event->response_type & 0x7f;
197
198 switch (event_code) {
199 /* *INDENT-OFF* */
200 #define WIN_FROM_EVENT(case_val,event_type,window_field) \
201 case case_val:{ \
202 event_type * real_event = (event_type *) event; \
203 return _find_window_from_xcb_window (display_x11, real_event->window_field); \
204 }
205 WIN_FROM_EVENT (XCB_CLIENT_MESSAGE, xcb_client_message_event_t, window)
206 WIN_FROM_EVENT (XCB_CONFIGURE_NOTIFY, xcb_configure_notify_event_t, window)
207 WIN_FROM_EVENT (XCB_EXPOSE, xcb_expose_event_t, window)
208 WIN_FROM_EVENT (XCB_KEY_PRESS, xcb_key_press_event_t, event)
209 WIN_FROM_EVENT (XCB_KEY_RELEASE, xcb_key_release_event_t, event)
210 WIN_FROM_EVENT (XCB_BUTTON_PRESS, xcb_button_press_event_t, event)
211 WIN_FROM_EVENT (XCB_BUTTON_RELEASE, xcb_button_release_event_t, event)
212 WIN_FROM_EVENT (XCB_MOTION_NOTIFY, xcb_motion_notify_event_t, event)
213 #undef WIN_FROM_EVENT
214 /* *INDENT-ON* */
215 default:
216 return NULL;
217 }
218 }
219
220 gboolean
gst_gl_display_x11_handle_event(GstGLDisplayX11 * display_x11)221 gst_gl_display_x11_handle_event (GstGLDisplayX11 * display_x11)
222 {
223 xcb_connection_t *connection = display_x11->xcb_connection;
224 xcb_generic_event_t *event;
225 gboolean ret = TRUE;
226
227 while ((event = xcb_poll_for_event (connection))) {
228 GstGLWindowX11 *window_x11 = _window_from_event (display_x11, event);
229
230 GST_TRACE_OBJECT (display_x11, "got event %p to window %" GST_PTR_FORMAT,
231 event, window_x11);
232
233 if (window_x11) {
234 ret = gst_gl_window_x11_handle_event (window_x11, event);
235 } else {
236 /* unknown window, ignore */
237 ret = TRUE;
238 }
239
240 if (window_x11)
241 gst_object_unref (window_x11);
242 g_free (event);
243 }
244
245 return ret;
246 }
247