3 * Copyright (C) 2013 Matthew Waters <ystreet00@gmail.com>
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.
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.
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.
22 * SECTION:gstgldisplay_x11
23 * @short_description: X11 Display connection
24 * @title: GstGLDisplayX11
25 * @see_also: #GstGLDisplay
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())
36 #include <gst/gl/x11/gstgldisplay_x11.h>
37 #include <gst/gl/x11/gstglwindow_x11.h>
38 #include "xcb_event_source.h"
40 GST_DEBUG_CATEGORY_STATIC (gst_gl_display_debug);
41 #define GST_CAT_DEFAULT gst_gl_display_debug
43 G_DEFINE_TYPE (GstGLDisplayX11, gst_gl_display_x11, GST_TYPE_GL_DISPLAY);
45 static void gst_gl_display_x11_finalize (GObject * object);
46 static guintptr gst_gl_display_x11_get_handle (GstGLDisplay * display);
47 static gboolean gst_gl_display_x11_get_foreign_display (GstGLDisplay * display);
50 gboolean gst_gl_display_x11_handle_event (GstGLDisplayX11 * display_x11);
52 extern gboolean gst_gl_window_x11_handle_event (GstGLWindowX11 * window_x11,
53 xcb_generic_event_t * event);
56 gst_gl_display_x11_class_init (GstGLDisplayX11Class * klass)
58 GST_GL_DISPLAY_CLASS (klass)->get_handle =
59 GST_DEBUG_FUNCPTR (gst_gl_display_x11_get_handle);
60 GST_GL_DISPLAY_CLASS (klass)->get_foreign_display =
61 GST_DEBUG_FUNCPTR (gst_gl_display_x11_get_foreign_display);
63 G_OBJECT_CLASS (klass)->finalize = gst_gl_display_x11_finalize;
67 gst_gl_display_x11_init (GstGLDisplayX11 * display_x11)
69 GstGLDisplay *display = (GstGLDisplay *) display_x11;
71 display->type = GST_GL_DISPLAY_TYPE_X11;
72 display_x11->foreign_display = FALSE;
76 gst_gl_display_x11_finalize (GObject * object)
78 GstGLDisplayX11 *display_x11 = GST_GL_DISPLAY_X11 (object);
80 g_free (display_x11->name);
82 if (!display_x11->foreign_display && display_x11->display) {
83 XSync (display_x11->display, FALSE);
84 XCloseDisplay (display_x11->display);
87 G_OBJECT_CLASS (gst_gl_display_x11_parent_class)->finalize (object);
91 * gst_gl_display_x11_new:
92 * @name: (allow-none): a display name
94 * Create a new #GstGLDisplayX11 from the x11 display name. See `XOpenDisplay`()
95 * for details on what is a valid name.
97 * Returns: (transfer full): a new #GstGLDisplayX11 or %NULL
100 gst_gl_display_x11_new (const gchar * name)
102 GstGLDisplayX11 *ret;
104 GST_DEBUG_CATEGORY_GET (gst_gl_display_debug, "gldisplay");
106 ret = g_object_new (GST_TYPE_GL_DISPLAY_X11, NULL);
107 gst_object_ref_sink (ret);
108 ret->name = g_strdup (name);
109 ret->display = XOpenDisplay (ret->name);
112 GST_INFO ("Failed to open X11 display connection with name, \'%s\'", name);
113 gst_object_unref (ret);
117 ret->xcb_connection = XGetXCBConnection (ret->display);
118 if (!ret->xcb_connection) {
119 GST_ERROR ("Failed to retrieve XCB connection from X11 Display");
120 gst_object_unref (ret);
124 XSetEventQueueOwner (ret->display, XCBOwnsEventQueue);
126 GST_GL_DISPLAY (ret)->event_source = xcb_event_source_new (ret);
127 g_source_attach (GST_GL_DISPLAY (ret)->event_source,
128 GST_GL_DISPLAY (ret)->main_context);
134 * gst_gl_display_x11_new_with_display: (skip)
135 * @display: an existing, x11 display
137 * Creates a new display connection from a X11 Display.
139 * Returns: (transfer full): a new #GstGLDisplayX11
142 gst_gl_display_x11_new_with_display (Display * display)
144 GstGLDisplayX11 *ret;
146 g_return_val_if_fail (display != NULL, NULL);
148 GST_DEBUG_CATEGORY_GET (gst_gl_display_debug, "gldisplay");
150 ret = g_object_new (GST_TYPE_GL_DISPLAY_X11, NULL);
151 gst_object_ref_sink (ret);
153 ret->name = g_strdup (DisplayString (display));
154 ret->display = display;
156 ret->xcb_connection = XGetXCBConnection (ret->display);
157 if (!ret->xcb_connection) {
158 GST_ERROR ("Failed to retrieve XCB connection from X11 Display");
159 gst_object_unref (ret);
163 ret->foreign_display = TRUE;
169 gst_gl_display_x11_get_handle (GstGLDisplay * display)
171 return (guintptr) GST_GL_DISPLAY_X11 (display)->display;
175 gst_gl_display_x11_get_foreign_display (GstGLDisplay * display)
177 return GST_GL_DISPLAY_X11 (display)->foreign_display;
181 _compare_xcb_window (GstGLWindowX11 * window_x11, xcb_window_t * window_id)
183 return window_x11->internal_win_id - *window_id;
186 static GstGLWindowX11 *
187 _find_window_from_xcb_window (GstGLDisplayX11 * display_x11,
188 xcb_window_t window_id)
190 GstGLDisplay *display = GST_GL_DISPLAY (display_x11);
191 GstGLWindow *window = NULL;
196 window = gst_gl_display_retrieve_window (display, &window_id,
197 (GCompareFunc) _compare_xcb_window);
199 return (GstGLWindowX11 *) window;
202 static GstGLWindowX11 *
203 _window_from_event (GstGLDisplayX11 * display_x11, xcb_generic_event_t * event)
205 uint8_t event_code = event->response_type & 0x7f;
207 switch (event_code) {
209 #define WIN_FROM_EVENT(case_val,event_type,window_field) \
211 event_type * real_event = (event_type *) event; \
212 return _find_window_from_xcb_window (display_x11, real_event->window_field); \
214 WIN_FROM_EVENT (XCB_CLIENT_MESSAGE, xcb_client_message_event_t, window)
215 WIN_FROM_EVENT (XCB_CONFIGURE_NOTIFY, xcb_configure_notify_event_t, window)
216 WIN_FROM_EVENT (XCB_EXPOSE, xcb_expose_event_t, window)
217 WIN_FROM_EVENT (XCB_KEY_PRESS, xcb_key_press_event_t, event)
218 WIN_FROM_EVENT (XCB_KEY_RELEASE, xcb_key_release_event_t, event)
219 WIN_FROM_EVENT (XCB_BUTTON_PRESS, xcb_button_press_event_t, event)
220 WIN_FROM_EVENT (XCB_BUTTON_RELEASE, xcb_button_release_event_t, event)
221 WIN_FROM_EVENT (XCB_MOTION_NOTIFY, xcb_motion_notify_event_t, event)
222 #undef WIN_FROM_EVENT
230 gst_gl_display_x11_handle_event (GstGLDisplayX11 * display_x11)
232 xcb_connection_t *connection = display_x11->xcb_connection;
233 xcb_generic_event_t *event;
236 while ((event = xcb_poll_for_event (connection))) {
237 GstGLWindowX11 *window_x11 = _window_from_event (display_x11, event);
239 GST_TRACE_OBJECT (display_x11, "got event %p to window %" GST_PTR_FORMAT,
243 ret = gst_gl_window_x11_handle_event (window_x11, event);
245 /* unknown window, ignore */
250 gst_object_unref (window_x11);