d05170429d6645489f12fd9843ffc812c9bc67d7
[platform/upstream/gstreamer.git] / gst-libs / gst / gl / x11 / gstgldisplay_x11.c
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 static gboolean gst_gl_display_x11_get_foreign_display (GstGLDisplay * display);
48
49 G_GNUC_INTERNAL
50     gboolean gst_gl_display_x11_handle_event (GstGLDisplayX11 * display_x11);
51
52 extern gboolean gst_gl_window_x11_handle_event (GstGLWindowX11 * window_x11,
53     xcb_generic_event_t * event);
54
55 static void
56 gst_gl_display_x11_class_init (GstGLDisplayX11Class * klass)
57 {
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);
62
63   G_OBJECT_CLASS (klass)->finalize = gst_gl_display_x11_finalize;
64 }
65
66 static void
67 gst_gl_display_x11_init (GstGLDisplayX11 * display_x11)
68 {
69   GstGLDisplay *display = (GstGLDisplay *) display_x11;
70
71   display->type = GST_GL_DISPLAY_TYPE_X11;
72   display_x11->foreign_display = FALSE;
73 }
74
75 static void
76 gst_gl_display_x11_finalize (GObject * object)
77 {
78   GstGLDisplayX11 *display_x11 = GST_GL_DISPLAY_X11 (object);
79
80   g_free (display_x11->name);
81
82   if (!display_x11->foreign_display && display_x11->display) {
83     XSync (display_x11->display, FALSE);
84     XCloseDisplay (display_x11->display);
85   }
86
87   G_OBJECT_CLASS (gst_gl_display_x11_parent_class)->finalize (object);
88 }
89
90 /**
91  * gst_gl_display_x11_new:
92  * @name: (allow-none): a display name
93  *
94  * Create a new #GstGLDisplayX11 from the x11 display name.  See `XOpenDisplay`()
95  * for details on what is a valid name.
96  *
97  * Returns: (transfer full): a new #GstGLDisplayX11 or %NULL
98  */
99 GstGLDisplayX11 *
100 gst_gl_display_x11_new (const gchar * name)
101 {
102   GstGLDisplayX11 *ret;
103
104   GST_DEBUG_CATEGORY_GET (gst_gl_display_debug, "gldisplay");
105
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);
110
111   if (!ret->display) {
112     GST_INFO ("Failed to open X11 display connection with name, \'%s\'", name);
113     gst_object_unref (ret);
114     return NULL;
115   }
116
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);
121     return NULL;
122   }
123
124   XSetEventQueueOwner (ret->display, XCBOwnsEventQueue);
125
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);
129
130   return ret;
131 }
132
133 /**
134  * gst_gl_display_x11_new_with_display: (skip)
135  * @display: an existing, x11 display
136  *
137  * Creates a new display connection from a X11 Display.
138  *
139  * Returns: (transfer full): a new #GstGLDisplayX11
140  */
141 GstGLDisplayX11 *
142 gst_gl_display_x11_new_with_display (Display * display)
143 {
144   GstGLDisplayX11 *ret;
145
146   g_return_val_if_fail (display != NULL, NULL);
147
148   GST_DEBUG_CATEGORY_GET (gst_gl_display_debug, "gldisplay");
149
150   ret = g_object_new (GST_TYPE_GL_DISPLAY_X11, NULL);
151   gst_object_ref_sink (ret);
152
153   ret->name = g_strdup (DisplayString (display));
154   ret->display = display;
155
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);
160     return NULL;
161   }
162
163   ret->foreign_display = TRUE;
164
165   return ret;
166 }
167
168 static guintptr
169 gst_gl_display_x11_get_handle (GstGLDisplay * display)
170 {
171   return (guintptr) GST_GL_DISPLAY_X11 (display)->display;
172 }
173
174 static gboolean
175 gst_gl_display_x11_get_foreign_display (GstGLDisplay * display)
176 {
177   return GST_GL_DISPLAY_X11 (display)->foreign_display;
178 }
179
180 static int
181 _compare_xcb_window (GstGLWindowX11 * window_x11, xcb_window_t * window_id)
182 {
183   return window_x11->internal_win_id - *window_id;
184 }
185
186 static GstGLWindowX11 *
187 _find_window_from_xcb_window (GstGLDisplayX11 * display_x11,
188     xcb_window_t window_id)
189 {
190   GstGLDisplay *display = GST_GL_DISPLAY (display_x11);
191   GstGLWindow *window = NULL;
192
193   if (!window_id)
194     return NULL;
195
196   window = gst_gl_display_retrieve_window (display, &window_id,
197       (GCompareFunc) _compare_xcb_window);
198
199   return (GstGLWindowX11 *) window;
200 }
201
202 static GstGLWindowX11 *
203 _window_from_event (GstGLDisplayX11 * display_x11, xcb_generic_event_t * event)
204 {
205   uint8_t event_code = event->response_type & 0x7f;
206
207   switch (event_code) {
208 /* *INDENT-OFF* */
209 #define WIN_FROM_EVENT(case_val,event_type,window_field) \
210     case case_val:{ \
211       event_type * real_event = (event_type *) event; \
212       return _find_window_from_xcb_window (display_x11, real_event->window_field); \
213     }
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
223 /* *INDENT-ON* */
224     default:
225       return NULL;
226   }
227 }
228
229 gboolean
230 gst_gl_display_x11_handle_event (GstGLDisplayX11 * display_x11)
231 {
232   xcb_connection_t *connection = display_x11->xcb_connection;
233   xcb_generic_event_t *event;
234   gboolean ret = TRUE;
235
236   while ((event = xcb_poll_for_event (connection))) {
237     GstGLWindowX11 *window_x11 = _window_from_event (display_x11, event);
238
239     GST_TRACE_OBJECT (display_x11, "got event %p to window %" GST_PTR_FORMAT,
240         event, window_x11);
241
242     if (window_x11) {
243       ret = gst_gl_window_x11_handle_event (window_x11, event);
244     } else {
245       /* unknown window, ignore */
246       ret = TRUE;
247     }
248
249     if (window_x11)
250       gst_object_unref (window_x11);
251     g_free (event);
252   }
253
254   return ret;
255 }