1 /*
2  * GStreamer
3  * Copyright (C) 2008 Julien Isorce <julien.isorce@gmail.com>
4  * Copyright (C) 2012 Matthew Waters <ystreet00@gmail.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #define GLIB_DISABLE_DEPRECATION_WARNINGS
23 
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 
28 #include <gst/gst.h>
29 #include <locale.h>
30 
31 #include "gstglwindow_x11.h"
32 #include "gstgldisplay_x11.h"
33 
34 #include "../gstglwindow_private.h"
35 
36 /* for XkbKeycodeToKeysym */
37 #include <X11/XKBlib.h>
38 
39 #define GST_CAT_DEFAULT gst_gl_window_debug
40 
41 G_GNUC_INTERNAL
42     gboolean gst_gl_window_x11_handle_event (GstGLWindowX11 * window_x11,
43     xcb_generic_event_t * event);
44 
45 /* X error trap */
46 static int TrappedErrorCode = 0;
47 static int (*old_error_handler) (Display *, XErrorEvent *);
48 
49 enum
50 {
51   ARG_0,
52   ARG_DISPLAY
53 };
54 
55 struct _GstGLWindowX11Private
56 {
57   gboolean activate;
58   gboolean activate_result;
59 
60   gint preferred_width;
61   gint preferred_height;
62 
63   gboolean handle_events;
64 
65   GstVideoRectangle render_rect;
66 };
67 
68 #define gst_gl_window_x11_parent_class parent_class
69 G_DEFINE_TYPE_WITH_PRIVATE (GstGLWindowX11, gst_gl_window_x11,
70     GST_TYPE_GL_WINDOW);
71 
72 static guintptr gst_gl_window_x11_get_display (GstGLWindow * window);
73 guintptr gst_gl_window_x11_get_gl_context (GstGLWindow * window);
74 gboolean gst_gl_window_x11_activate (GstGLWindow * window, gboolean activate);
75 static void gst_gl_window_x11_set_window_handle (GstGLWindow * window,
76     guintptr handle);
77 static gboolean gst_gl_window_x11_set_render_rectangle (GstGLWindow * window,
78     int x, int y, int width, int height);
79 static guintptr gst_gl_window_x11_get_window_handle (GstGLWindow * window);
80 static void gst_gl_window_x11_set_preferred_size (GstGLWindow * window,
81     gint width, gint height);
82 static void gst_gl_window_x11_show (GstGLWindow * window);
83 static void gst_gl_window_x11_draw (GstGLWindow * window);
84 gboolean gst_gl_window_x11_create_context (GstGLWindow * window,
85     GstGLAPI gl_api, guintptr external_gl_context, GError ** error);
86 static gboolean gst_gl_window_x11_open (GstGLWindow * window, GError ** error);
87 static void gst_gl_window_x11_close (GstGLWindow * window);
88 static void gst_gl_window_x11_handle_events (GstGLWindow * window,
89     gboolean handle_events);
90 
91 static void
gst_gl_window_x11_finalize(GObject * object)92 gst_gl_window_x11_finalize (GObject * object)
93 {
94   G_OBJECT_CLASS (parent_class)->finalize (object);
95 }
96 
97 static void
gst_gl_window_x11_class_init(GstGLWindowX11Class * klass)98 gst_gl_window_x11_class_init (GstGLWindowX11Class * klass)
99 {
100   GObjectClass *obj_class = G_OBJECT_CLASS (klass);
101   GstGLWindowClass *window_class = (GstGLWindowClass *) klass;
102 
103   obj_class->finalize = gst_gl_window_x11_finalize;
104 
105   window_class->get_display = GST_DEBUG_FUNCPTR (gst_gl_window_x11_get_display);
106   window_class->set_window_handle =
107       GST_DEBUG_FUNCPTR (gst_gl_window_x11_set_window_handle);
108   window_class->set_render_rectangle =
109       GST_DEBUG_FUNCPTR (gst_gl_window_x11_set_render_rectangle);
110   window_class->get_window_handle =
111       GST_DEBUG_FUNCPTR (gst_gl_window_x11_get_window_handle);
112   window_class->draw = GST_DEBUG_FUNCPTR (gst_gl_window_x11_draw);
113   window_class->open = GST_DEBUG_FUNCPTR (gst_gl_window_x11_open);
114   window_class->close = GST_DEBUG_FUNCPTR (gst_gl_window_x11_close);
115   window_class->handle_events =
116       GST_DEBUG_FUNCPTR (gst_gl_window_x11_handle_events);
117   window_class->set_preferred_size =
118       GST_DEBUG_FUNCPTR (gst_gl_window_x11_set_preferred_size);
119   window_class->show = GST_DEBUG_FUNCPTR (gst_gl_window_x11_show);
120 }
121 
122 static void
gst_gl_window_x11_init(GstGLWindowX11 * window)123 gst_gl_window_x11_init (GstGLWindowX11 * window)
124 {
125   window->priv = gst_gl_window_x11_get_instance_private (window);
126 }
127 
128 /* Must be called in the gl thread */
129 GstGLWindowX11 *
gst_gl_window_x11_new(GstGLDisplay * display)130 gst_gl_window_x11_new (GstGLDisplay * display)
131 {
132   GstGLWindowX11 *window;
133 
134   if ((gst_gl_display_get_handle_type (display) & GST_GL_DISPLAY_TYPE_X11)
135       == GST_GL_DISPLAY_TYPE_NONE) {
136     GST_INFO ("Wrong display type %u for this window type %u", display->type,
137         GST_GL_DISPLAY_TYPE_X11);
138     return NULL;
139   }
140 
141   window = g_object_new (GST_TYPE_GL_WINDOW_X11, NULL);
142   gst_object_ref_sink (window);
143 
144   return window;
145 }
146 
147 static gboolean
gst_gl_window_x11_open(GstGLWindow * window,GError ** error)148 gst_gl_window_x11_open (GstGLWindow * window, GError ** error)
149 {
150   GstGLWindowX11 *window_x11 = GST_GL_WINDOW_X11 (window);
151   GstGLDisplayX11 *display_x11 = (GstGLDisplayX11 *) window->display;
152 
153   window_x11->device = display_x11->display;
154 //  window_x11->device = XOpenDisplay (display_x11->name);
155   if (window_x11->device == NULL) {
156     g_set_error (error, GST_GL_WINDOW_ERROR,
157         GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
158         "Failed to connect to X display server");
159     goto failure;
160   }
161 
162   GST_LOG ("gl device id: %ld", (gulong) window_x11->device);
163 
164   window_x11->screen = DefaultScreenOfDisplay (window_x11->device);
165   window_x11->screen_num = DefaultScreen (window_x11->device);
166   window_x11->visual =
167       DefaultVisual (window_x11->device, window_x11->screen_num);
168   window_x11->root = DefaultRootWindow (window_x11->device);
169   window_x11->white = XWhitePixel (window_x11->device, window_x11->screen_num);
170   window_x11->black = XBlackPixel (window_x11->device, window_x11->screen_num);
171   window_x11->depth = DefaultDepthOfScreen (window_x11->screen);
172 
173   GST_LOG ("gl root id: %lud", (gulong) window_x11->root);
174 
175   window_x11->device_width =
176       DisplayWidth (window_x11->device, window_x11->screen_num);
177   window_x11->device_height =
178       DisplayHeight (window_x11->device, window_x11->screen_num);
179 
180   window_x11->allow_extra_expose_events = TRUE;
181 
182   return GST_GL_WINDOW_CLASS (parent_class)->open (window, error);
183 
184 failure:
185   return FALSE;
186 }
187 
188 gboolean
gst_gl_window_x11_create_window(GstGLWindowX11 * window_x11)189 gst_gl_window_x11_create_window (GstGLWindowX11 * window_x11)
190 {
191   XSetWindowAttributes win_attr;
192   XTextProperty text_property;
193   XWMHints wm_hints;
194   unsigned long mask;
195   const gchar *title = "OpenGL renderer";
196   Atom wm_atoms[1];
197   gint x = 0, y = 0, width = 1, height = 1;
198 
199   if (window_x11->visual_info->visual != window_x11->visual)
200     GST_LOG ("selected visual is different from the default");
201 
202   GST_LOG ("visual XID:%d, screen:%d, visualid:%d, depth:%d, class:%d, "
203       "red_mask:%ld, green_mask:%ld, blue_mask:%ld bpp:%d",
204       (gint) XVisualIDFromVisual (window_x11->visual_info->visual),
205       window_x11->visual_info->screen, (gint) window_x11->visual_info->visualid,
206       window_x11->visual_info->depth, window_x11->visual_info->class,
207       window_x11->visual_info->red_mask, window_x11->visual_info->green_mask,
208       window_x11->visual_info->blue_mask,
209       window_x11->visual_info->bits_per_rgb);
210 
211   win_attr.event_mask =
212       StructureNotifyMask | ExposureMask | VisibilityChangeMask;
213   win_attr.do_not_propagate_mask = NoEventMask;
214 
215   win_attr.background_pixmap = None;
216   win_attr.background_pixel = 0;
217   win_attr.border_pixel = 0;
218 
219   win_attr.colormap =
220       XCreateColormap (window_x11->device, window_x11->root,
221       window_x11->visual_info->visual, AllocNone);
222 
223   mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
224 
225   window_x11->internal_win_id =
226       XCreateWindow (window_x11->device,
227       window_x11->parent_win ? window_x11->parent_win : window_x11->root,
228       x, y, width, height, 0,
229       window_x11->visual_info->depth, InputOutput,
230       window_x11->visual_info->visual, mask, &win_attr);
231 
232   gst_gl_window_x11_handle_events (GST_GL_WINDOW (window_x11),
233       window_x11->priv->handle_events);
234 
235   XSync (window_x11->device, FALSE);
236 
237   XSetWindowBackgroundPixmap (window_x11->device,
238       window_x11->internal_win_id, None);
239 
240   GST_LOG ("gl window id: %lud", (gulong) window_x11->internal_win_id);
241   GST_LOG ("gl window props: x:%d y:%d", x, y);
242 
243   wm_atoms[0] = XInternAtom (window_x11->device, "WM_DELETE_WINDOW", True);
244   if (wm_atoms[0] == None)
245     GST_DEBUG ("Cannot create WM_DELETE_WINDOW");
246 
247   XSetWMProtocols (window_x11->device, window_x11->internal_win_id,
248       wm_atoms, 1);
249 
250   wm_hints.flags = StateHint;
251   wm_hints.initial_state = NormalState;
252   wm_hints.input = False;
253 
254   XStringListToTextProperty ((char **) &title, 1, &text_property);
255 
256   XSetWMProperties (window_x11->device, window_x11->internal_win_id,
257       &text_property, &text_property, 0, 0, NULL, &wm_hints, NULL);
258 
259   XFree (text_property.value);
260 
261   return TRUE;
262 }
263 
264 static void
gst_gl_window_x11_close(GstGLWindow * window)265 gst_gl_window_x11_close (GstGLWindow * window)
266 {
267   GstGLWindowX11 *window_x11 = GST_GL_WINDOW_X11 (window);
268 
269   if (window_x11->device) {
270     if (window_x11->internal_win_id) {
271       XUnmapWindow (window_x11->device, window_x11->internal_win_id);
272 
273       XDestroyWindow (window_x11->device, window_x11->internal_win_id);
274     }
275     XFree (window_x11->visual_info);
276 
277     GST_DEBUG ("display receiver closed");
278   }
279 
280   window_x11->running = FALSE;
281 
282   GST_GL_WINDOW_CLASS (parent_class)->close (window);
283 }
284 
285 /* called by the gl thread */
286 static void
gst_gl_window_x11_set_window_handle(GstGLWindow * window,guintptr id)287 gst_gl_window_x11_set_window_handle (GstGLWindow * window, guintptr id)
288 {
289   GstGLWindowX11 *window_x11;
290   gint x, y, width, height;
291 
292   window_x11 = GST_GL_WINDOW_X11 (window);
293 
294   window_x11->parent_win = (Window) id;
295 
296   if (window_x11->priv->render_rect.w > 0 &&
297       window_x11->priv->render_rect.h > 0) {
298     x = window_x11->priv->render_rect.x;
299     y = window_x11->priv->render_rect.y;
300     width = window_x11->priv->render_rect.w;
301     height = window_x11->priv->render_rect.h;
302   } else {
303     x = y = 0;
304     if (window_x11->parent_win) {
305       XWindowAttributes attr;
306 
307       XGetWindowAttributes (window_x11->device, window_x11->parent_win, &attr);
308       width = attr.width;
309       height = attr.height;
310     } else {
311       width = window_x11->priv->preferred_width;
312       height = window_x11->priv->preferred_height;
313     }
314   }
315 
316   XResizeWindow (window_x11->device, window_x11->internal_win_id,
317       width, height);
318 
319   XReparentWindow (window_x11->device, window_x11->internal_win_id,
320       window_x11->parent_win, x, y);
321 
322   XSync (window_x11->device, FALSE);
323 }
324 
325 struct SetRenderRectangle
326 {
327   GstGLWindowX11 *window_x11;
328   GstVideoRectangle rect;
329 };
330 
331 static void
_free_set_render_rectangle(struct SetRenderRectangle * render)332 _free_set_render_rectangle (struct SetRenderRectangle *render)
333 {
334   if (render) {
335     if (render->window_x11)
336       gst_object_unref (render->window_x11);
337     g_free (render);
338   }
339 }
340 
341 static void
_set_render_rectangle(gpointer data)342 _set_render_rectangle (gpointer data)
343 {
344   struct SetRenderRectangle *render = data;
345 
346   GST_LOG_OBJECT (render->window_x11, "setting render rectangle %i,%i+%ix%i",
347       render->rect.x, render->rect.y, render->rect.w, render->rect.h);
348 
349   if (render->window_x11->internal_win_id)
350     XMoveResizeWindow (render->window_x11->device,
351         render->window_x11->internal_win_id, render->rect.x, render->rect.y,
352         render->rect.w, render->rect.h);
353 
354   if (render->window_x11->device)
355     XSync (render->window_x11->device, FALSE);
356 
357   render->window_x11->priv->render_rect = render->rect;
358 }
359 
360 static gboolean
gst_gl_window_x11_set_render_rectangle(GstGLWindow * window,int x,int y,int width,int height)361 gst_gl_window_x11_set_render_rectangle (GstGLWindow * window,
362     int x, int y, int width, int height)
363 {
364   GstGLWindowX11 *window_x11 = GST_GL_WINDOW_X11 (window);
365   struct SetRenderRectangle *render;
366 
367   render = g_new0 (struct SetRenderRectangle, 1);
368   render->window_x11 = gst_object_ref (window_x11);
369   render->rect.x = x;
370   render->rect.y = y;
371   render->rect.w = width;
372   render->rect.h = height;
373 
374   gst_gl_window_send_message_async (window,
375       (GstGLWindowCB) _set_render_rectangle, render,
376       (GDestroyNotify) _free_set_render_rectangle);
377 
378   return TRUE;
379 }
380 
381 static guintptr
gst_gl_window_x11_get_window_handle(GstGLWindow * window)382 gst_gl_window_x11_get_window_handle (GstGLWindow * window)
383 {
384   GstGLWindowX11 *window_x11;
385 
386   window_x11 = GST_GL_WINDOW_X11 (window);
387 
388   return window_x11->internal_win_id;
389 }
390 
391 static void
gst_gl_window_x11_set_preferred_size(GstGLWindow * window,gint width,gint height)392 gst_gl_window_x11_set_preferred_size (GstGLWindow * window, gint width,
393     gint height)
394 {
395   GstGLWindowX11 *window_x11 = GST_GL_WINDOW_X11 (window);
396 
397   window_x11->priv->preferred_width = width;
398   window_x11->priv->preferred_height = height;
399 }
400 
401 static void
_show_window(GstGLWindow * window)402 _show_window (GstGLWindow * window)
403 {
404   GstGLWindowX11 *window_x11 = GST_GL_WINDOW_X11 (window);
405   guint width = window_x11->priv->preferred_width;
406   guint height = window_x11->priv->preferred_height;
407 
408   if (!window_x11->visible) {
409     if (!window_x11->parent_win) {
410       XResizeWindow (window_x11->device, window_x11->internal_win_id,
411           width, height);
412 
413       gst_gl_window_resize (window, width, height);
414     }
415 
416     XMapWindow (window_x11->device, window_x11->internal_win_id);
417     XSync (window_x11->device, FALSE);
418     window_x11->visible = TRUE;
419   }
420 }
421 
422 static void
gst_gl_window_x11_show(GstGLWindow * window)423 gst_gl_window_x11_show (GstGLWindow * window)
424 {
425   gst_gl_window_send_message (window, (GstGLWindowCB) _show_window, window);
426 }
427 
428 static void
_context_draw(GstGLContext * context,GstGLWindow * window)429 _context_draw (GstGLContext * context, GstGLWindow * window)
430 {
431   window->draw (window->draw_data);
432   gst_gl_context_swap_buffers (context);
433 
434   gst_object_unref (context);
435 }
436 
437 static void
draw_cb(gpointer data)438 draw_cb (gpointer data)
439 {
440   GstGLWindowX11 *window_x11 = data;
441   GstGLWindow *window = GST_GL_WINDOW (window_x11);
442   guint width, height;
443   XWindowAttributes attr;
444 
445   if (window_x11->internal_win_id) {
446     gboolean need_resize = FALSE;
447 
448     XGetWindowAttributes (window_x11->device, window_x11->internal_win_id,
449         &attr);
450     GST_TRACE_OBJECT (window, "window size %ux%u", attr.width, attr.height);
451 
452     if (window_x11->parent_win &&
453         (window_x11->priv->render_rect.w < 0 ||
454             window_x11->priv->render_rect.h < 0)) {
455       XWindowAttributes attr_parent;
456       XGetWindowAttributes (window_x11->device, window_x11->parent_win,
457           &attr_parent);
458       GST_TRACE_OBJECT (window, "parent window size %ux%u", attr_parent.width,
459           attr_parent.height);
460 
461       if (attr.width != attr_parent.width || attr.height != attr_parent.height) {
462         XMoveResizeWindow (window_x11->device, window_x11->internal_win_id,
463             0, 0, attr_parent.width, attr_parent.height);
464         XSync (window_x11->device, FALSE);
465 
466         attr.width = attr_parent.width;
467         attr.height = attr_parent.height;
468 
469         GST_LOG ("parent resize:  %d, %d",
470             attr_parent.width, attr_parent.height);
471         need_resize = TRUE;
472       }
473     }
474 
475     gst_gl_window_get_surface_dimensions (window, &width, &height);
476     if (attr.width != width || attr.height != height)
477       need_resize = TRUE;
478 
479     if (need_resize)
480       gst_gl_window_queue_resize (window);
481 
482     if (window_x11->allow_extra_expose_events) {
483       if (window->queue_resize)
484         gst_gl_window_resize (window, width, height);
485 
486       if (window->draw) {
487         GstGLContext *context = gst_gl_window_get_context (window);
488 
489         _context_draw (context, window);
490       }
491     }
492   }
493 }
494 
495 /* Not called by the gl thread */
496 static void
gst_gl_window_x11_draw(GstGLWindow * window)497 gst_gl_window_x11_draw (GstGLWindow * window)
498 {
499   gst_gl_window_send_message (window, (GstGLWindowCB) draw_cb, window);
500 }
501 
502 static void
gst_gl_window_x11_handle_events(GstGLWindow * window,gboolean handle_events)503 gst_gl_window_x11_handle_events (GstGLWindow * window, gboolean handle_events)
504 {
505   GstGLWindowX11 *window_x11;
506 
507   g_return_if_fail (window != NULL);
508 
509   window_x11 = GST_GL_WINDOW_X11 (window);
510 
511   window_x11->priv->handle_events = handle_events;
512 
513   if (window_x11->internal_win_id) {
514     if (handle_events) {
515       XSelectInput (window_x11->device, window_x11->internal_win_id,
516           StructureNotifyMask | ExposureMask | VisibilityChangeMask |
517           PointerMotionMask | KeyPressMask | KeyReleaseMask | ButtonPressMask |
518           ButtonReleaseMask);
519     } else {
520       XSelectInput (window_x11->device, window_x11->internal_win_id,
521           StructureNotifyMask | ExposureMask | VisibilityChangeMask);
522     }
523   }
524 }
525 
526 gboolean
gst_gl_window_x11_handle_event(GstGLWindowX11 * window_x11,xcb_generic_event_t * event)527 gst_gl_window_x11_handle_event (GstGLWindowX11 * window_x11,
528     xcb_generic_event_t * event)
529 {
530   GstGLWindow *window = GST_GL_WINDOW (window_x11);
531   GstGLDisplayX11 *display_x11 = GST_GL_DISPLAY_X11 (window->display);
532   xcb_connection_t *connection = display_x11->xcb_connection;
533   uint8_t event_code = event->response_type & 0x7f;
534 
535   switch (event_code) {
536     case XCB_CLIENT_MESSAGE:{
537       xcb_client_message_event_t *client_event;
538       xcb_intern_atom_cookie_t cookie;
539       xcb_intern_atom_reply_t *reply;
540 
541       client_event = (xcb_client_message_event_t *) event;
542       cookie = xcb_intern_atom (connection, 0, 16, "WM_DELETE_WINDOW");
543       reply = xcb_intern_atom_reply (connection, cookie, 0);
544 
545       if (client_event->data.data32[0] == reply->atom) {
546         GST_INFO_OBJECT (window_x11, "Close requested");
547 
548         if (window->close)
549           window->close (window->close_data);
550 
551         gst_gl_display_remove_window (GST_GL_DISPLAY (display_x11),
552             GST_GL_WINDOW (window_x11));
553       }
554 
555       g_free (reply);
556       break;
557     }
558     case XCB_CONFIGURE_NOTIFY:{
559       xcb_configure_notify_event_t *configure_event;
560 
561       configure_event = (xcb_configure_notify_event_t *) event;
562 
563       gst_gl_window_resize (window, configure_event->width,
564           configure_event->height);
565 
566       gst_gl_window_draw (window);
567       break;
568     }
569     case XCB_EXPOSE:{
570       xcb_expose_event_t *expose_event = (xcb_expose_event_t *) event;
571       /* non-zero means that other Expose follows
572        * so just wait for the last one
573        * in theory we should not receive non-zero because
574        * we have no sub areas here but just in case */
575       if (expose_event->count != 0)
576         break;
577 
578       gst_gl_window_draw (window);
579       break;
580     }
581     case XCB_KEY_PRESS:
582     case XCB_KEY_RELEASE:{
583       xcb_key_press_event_t *kp = (xcb_key_press_event_t *) event;
584       const gchar *event_type_str;
585       gchar *key_str;
586       KeySym keysym;
587 
588       keysym = XkbKeycodeToKeysym (window_x11->device, kp->detail, 0, 0);
589       key_str = XKeysymToString (keysym);
590 
591       if (event_code == XCB_KEY_PRESS)
592         event_type_str = "key-press";
593       else
594         event_type_str = "key-release";
595 
596       gst_gl_window_send_key_event (window, event_type_str, key_str);
597       break;
598     }
599     case XCB_BUTTON_PRESS:
600     case XCB_BUTTON_RELEASE:{
601       xcb_button_press_event_t *bp = (xcb_button_press_event_t *) event;
602       const gchar *event_type_str;
603 
604       if (event_code == XCB_BUTTON_PRESS)
605         event_type_str = "mouse-button-press";
606       else
607         event_type_str = "mouse-button-release";
608 
609       gst_gl_window_send_mouse_event (window, event_type_str, bp->detail,
610           (double) bp->event_x, (double) bp->event_y);
611       break;
612     }
613     case XCB_MOTION_NOTIFY:{
614       xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
615 
616       gst_gl_window_send_mouse_event (window, "mouse-move", 0,
617           (double) motion->event_x, (double) motion->event_y);
618       break;
619     }
620     default:
621       GST_TRACE ("unhandled XCB event: %u", event_code);
622       break;
623   }
624 
625   return TRUE;
626 }
627 
628 static int
error_handler(Display * xdpy,XErrorEvent * error)629 error_handler (Display * xdpy, XErrorEvent * error)
630 {
631   TrappedErrorCode = error->error_code;
632   return 0;
633 }
634 
635 /**
636  * gst_gl_window_x11_trap_x_errors:
637  *
638  * Traps every X error until gst_gl_window_x11_untrap_x_errors() is called.
639  */
640 void
gst_gl_window_x11_trap_x_errors(void)641 gst_gl_window_x11_trap_x_errors (void)
642 {
643   TrappedErrorCode = 0;
644   old_error_handler = XSetErrorHandler (error_handler);
645 }
646 
647 /**
648  * gst_gl_window_x11_untrap_x_errors:
649  *
650  * Removes the X error trap and returns the current status.
651  *
652  * Return value: the trapped error code, or 0 for success
653  */
654 gint
gst_gl_window_x11_untrap_x_errors(void)655 gst_gl_window_x11_untrap_x_errors (void)
656 {
657   XSetErrorHandler (old_error_handler);
658 
659   return TrappedErrorCode;
660 }
661 
662 static guintptr
gst_gl_window_x11_get_display(GstGLWindow * window)663 gst_gl_window_x11_get_display (GstGLWindow * window)
664 {
665   GstGLWindowX11 *window_x11 = GST_GL_WINDOW_X11 (window);
666 
667   return (guintptr) window_x11->device;
668 }
669