Blame gst-libs/gst/gl/x11/gstglwindow_x11.c

Packit 971217
/*
Packit 971217
 * GStreamer
Packit 971217
 * Copyright (C) 2008 Julien Isorce <julien.isorce@gmail.com>
Packit 971217
 * Copyright (C) 2012 Matthew Waters <ystreet00@gmail.com>
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
Packit 971217
#define GLIB_DISABLE_DEPRECATION_WARNINGS
Packit 971217
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#include <gst/gst.h>
Packit 971217
#include <locale.h>
Packit 971217
Packit 971217
#include "gstglwindow_x11.h"
Packit 971217
#include "gstgldisplay_x11.h"
Packit 971217
Packit 971217
#include "../gstglwindow_private.h"
Packit 971217
Packit 971217
/* for XkbKeycodeToKeysym */
Packit 971217
#include <X11/XKBlib.h>
Packit 971217
Packit 971217
#define GST_GL_WINDOW_X11_GET_PRIVATE(o)  \
Packit 971217
  (G_TYPE_INSTANCE_GET_PRIVATE((o), GST_TYPE_GL_WINDOW_X11, GstGLWindowX11Private))
Packit 971217
Packit 971217
#define GST_CAT_DEFAULT gst_gl_window_debug
Packit 971217
Packit 971217
#define gst_gl_window_x11_parent_class parent_class
Packit 971217
G_DEFINE_TYPE (GstGLWindowX11, gst_gl_window_x11, GST_TYPE_GL_WINDOW);
Packit 971217
Packit 971217
G_GNUC_INTERNAL
Packit 971217
    gboolean gst_gl_window_x11_handle_event (GstGLWindowX11 * window_x11,
Packit 971217
    xcb_generic_event_t * event);
Packit 971217
Packit 971217
/* X error trap */
Packit 971217
static int TrappedErrorCode = 0;
Packit 971217
static int (*old_error_handler) (Display *, XErrorEvent *);
Packit 971217
Packit 971217
enum
Packit 971217
{
Packit 971217
  ARG_0,
Packit 971217
  ARG_DISPLAY
Packit 971217
};
Packit 971217
Packit 971217
struct _GstGLWindowX11Private
Packit 971217
{
Packit 971217
  gboolean activate;
Packit 971217
  gboolean activate_result;
Packit 971217
Packit 971217
  gint preferred_width;
Packit 971217
  gint preferred_height;
Packit 971217
Packit 971217
  gboolean handle_events;
Packit 971217
Packit 971217
  GstVideoRectangle render_rect;
Packit 971217
};
Packit 971217
Packit 971217
static guintptr gst_gl_window_x11_get_display (GstGLWindow * window);
Packit 971217
guintptr gst_gl_window_x11_get_gl_context (GstGLWindow * window);
Packit 971217
gboolean gst_gl_window_x11_activate (GstGLWindow * window, gboolean activate);
Packit 971217
static void gst_gl_window_x11_set_window_handle (GstGLWindow * window,
Packit 971217
    guintptr handle);
Packit 971217
static gboolean gst_gl_window_x11_set_render_rectangle (GstGLWindow * window,
Packit 971217
    int x, int y, int width, int height);
Packit 971217
static guintptr gst_gl_window_x11_get_window_handle (GstGLWindow * window);
Packit 971217
static void gst_gl_window_x11_set_preferred_size (GstGLWindow * window,
Packit 971217
    gint width, gint height);
Packit 971217
static void gst_gl_window_x11_show (GstGLWindow * window);
Packit 971217
static void gst_gl_window_x11_draw (GstGLWindow * window);
Packit 971217
gboolean gst_gl_window_x11_create_context (GstGLWindow * window,
Packit 971217
    GstGLAPI gl_api, guintptr external_gl_context, GError ** error);
Packit 971217
static gboolean gst_gl_window_x11_open (GstGLWindow * window, GError ** error);
Packit 971217
static void gst_gl_window_x11_close (GstGLWindow * window);
Packit 971217
static void gst_gl_window_x11_handle_events (GstGLWindow * window,
Packit 971217
    gboolean handle_events);
Packit 971217
Packit 971217
static void
Packit 971217
gst_gl_window_x11_finalize (GObject * object)
Packit 971217
{
Packit 971217
  G_OBJECT_CLASS (parent_class)->finalize (object);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_gl_window_x11_class_init (GstGLWindowX11Class * klass)
Packit 971217
{
Packit 971217
  GObjectClass *obj_class = G_OBJECT_CLASS (klass);
Packit 971217
  GstGLWindowClass *window_class = (GstGLWindowClass *) klass;
Packit 971217
Packit 971217
  g_type_class_add_private (klass, sizeof (GstGLWindowX11Private));
Packit 971217
Packit 971217
  obj_class->finalize = gst_gl_window_x11_finalize;
Packit 971217
Packit 971217
  window_class->get_display = GST_DEBUG_FUNCPTR (gst_gl_window_x11_get_display);
Packit 971217
  window_class->set_window_handle =
Packit 971217
      GST_DEBUG_FUNCPTR (gst_gl_window_x11_set_window_handle);
Packit 971217
  window_class->set_render_rectangle =
Packit 971217
      GST_DEBUG_FUNCPTR (gst_gl_window_x11_set_render_rectangle);
Packit 971217
  window_class->get_window_handle =
Packit 971217
      GST_DEBUG_FUNCPTR (gst_gl_window_x11_get_window_handle);
Packit 971217
  window_class->draw = GST_DEBUG_FUNCPTR (gst_gl_window_x11_draw);
Packit 971217
  window_class->open = GST_DEBUG_FUNCPTR (gst_gl_window_x11_open);
Packit 971217
  window_class->close = GST_DEBUG_FUNCPTR (gst_gl_window_x11_close);
Packit 971217
  window_class->handle_events =
Packit 971217
      GST_DEBUG_FUNCPTR (gst_gl_window_x11_handle_events);
Packit 971217
  window_class->set_preferred_size =
Packit 971217
      GST_DEBUG_FUNCPTR (gst_gl_window_x11_set_preferred_size);
Packit 971217
  window_class->show = GST_DEBUG_FUNCPTR (gst_gl_window_x11_show);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_gl_window_x11_init (GstGLWindowX11 * window)
Packit 971217
{
Packit 971217
  window->priv = GST_GL_WINDOW_X11_GET_PRIVATE (window);
Packit 971217
}
Packit 971217
Packit 971217
/* Must be called in the gl thread */
Packit 971217
GstGLWindowX11 *
Packit 971217
gst_gl_window_x11_new (GstGLDisplay * display)
Packit 971217
{
Packit 971217
  GstGLWindowX11 *window;
Packit 971217
Packit 971217
  if ((gst_gl_display_get_handle_type (display) & GST_GL_DISPLAY_TYPE_X11)
Packit 971217
      == GST_GL_DISPLAY_TYPE_NONE) {
Packit 971217
    GST_INFO ("Wrong display type %u for this window type %u", display->type,
Packit 971217
        GST_GL_DISPLAY_TYPE_X11);
Packit 971217
    return NULL;
Packit 971217
  }
Packit 971217
Packit 971217
  window = g_object_new (GST_TYPE_GL_WINDOW_X11, NULL);
Packit 971217
  gst_object_ref_sink (window);
Packit 971217
Packit 971217
  return window;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
gst_gl_window_x11_open (GstGLWindow * window, GError ** error)
Packit 971217
{
Packit 971217
  GstGLWindowX11 *window_x11 = GST_GL_WINDOW_X11 (window);
Packit 971217
  GstGLDisplayX11 *display_x11 = (GstGLDisplayX11 *) window->display;
Packit 971217
Packit 971217
  window_x11->device = display_x11->display;
Packit 971217
//  window_x11->device = XOpenDisplay (display_x11->name);
Packit 971217
  if (window_x11->device == NULL) {
Packit 971217
    g_set_error (error, GST_GL_WINDOW_ERROR,
Packit 971217
        GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
Packit 971217
        "Failed to connect to X display server");
Packit 971217
    goto failure;
Packit 971217
  }
Packit 971217
Packit 971217
  GST_LOG ("gl device id: %ld", (gulong) window_x11->device);
Packit 971217
Packit 971217
  window_x11->screen = DefaultScreenOfDisplay (window_x11->device);
Packit 971217
  window_x11->screen_num = DefaultScreen (window_x11->device);
Packit 971217
  window_x11->visual =
Packit 971217
      DefaultVisual (window_x11->device, window_x11->screen_num);
Packit 971217
  window_x11->root = DefaultRootWindow (window_x11->device);
Packit 971217
  window_x11->white = XWhitePixel (window_x11->device, window_x11->screen_num);
Packit 971217
  window_x11->black = XBlackPixel (window_x11->device, window_x11->screen_num);
Packit 971217
  window_x11->depth = DefaultDepthOfScreen (window_x11->screen);
Packit 971217
Packit 971217
  GST_LOG ("gl root id: %lud", (gulong) window_x11->root);
Packit 971217
Packit 971217
  window_x11->device_width =
Packit 971217
      DisplayWidth (window_x11->device, window_x11->screen_num);
Packit 971217
  window_x11->device_height =
Packit 971217
      DisplayHeight (window_x11->device, window_x11->screen_num);
Packit 971217
Packit 971217
  window_x11->allow_extra_expose_events = TRUE;
Packit 971217
Packit 971217
  return GST_GL_WINDOW_CLASS (parent_class)->open (window, error);
Packit 971217
Packit 971217
failure:
Packit 971217
  return FALSE;
Packit 971217
}
Packit 971217
Packit 971217
gboolean
Packit 971217
gst_gl_window_x11_create_window (GstGLWindowX11 * window_x11)
Packit 971217
{
Packit 971217
  XSetWindowAttributes win_attr;
Packit 971217
  XTextProperty text_property;
Packit 971217
  XWMHints wm_hints;
Packit 971217
  unsigned long mask;
Packit 971217
  const gchar *title = "OpenGL renderer";
Packit 971217
  Atom wm_atoms[1];
Packit 971217
  gint x = 0, y = 0, width = 1, height = 1;
Packit 971217
Packit 971217
  if (window_x11->visual_info->visual != window_x11->visual)
Packit 971217
    GST_LOG ("selected visual is different from the default");
Packit 971217
Packit 971217
  GST_LOG ("visual XID:%d, screen:%d, visualid:%d, depth:%d, class:%d, "
Packit 971217
      "red_mask:%ld, green_mask:%ld, blue_mask:%ld bpp:%d",
Packit 971217
      (gint) XVisualIDFromVisual (window_x11->visual_info->visual),
Packit 971217
      window_x11->visual_info->screen, (gint) window_x11->visual_info->visualid,
Packit 971217
      window_x11->visual_info->depth, window_x11->visual_info->class,
Packit 971217
      window_x11->visual_info->red_mask, window_x11->visual_info->green_mask,
Packit 971217
      window_x11->visual_info->blue_mask,
Packit 971217
      window_x11->visual_info->bits_per_rgb);
Packit 971217
Packit 971217
  win_attr.event_mask =
Packit 971217
      StructureNotifyMask | ExposureMask | VisibilityChangeMask;
Packit 971217
  win_attr.do_not_propagate_mask = NoEventMask;
Packit 971217
Packit 971217
  win_attr.background_pixmap = None;
Packit 971217
  win_attr.background_pixel = 0;
Packit 971217
  win_attr.border_pixel = 0;
Packit 971217
Packit 971217
  win_attr.colormap =
Packit 971217
      XCreateColormap (window_x11->device, window_x11->root,
Packit 971217
      window_x11->visual_info->visual, AllocNone);
Packit 971217
Packit 971217
  mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
Packit 971217
Packit 971217
  window_x11->internal_win_id =
Packit 971217
      XCreateWindow (window_x11->device,
Packit 971217
      window_x11->parent_win ? window_x11->parent_win : window_x11->root,
Packit 971217
      x, y, width, height, 0,
Packit 971217
      window_x11->visual_info->depth, InputOutput,
Packit 971217
      window_x11->visual_info->visual, mask, &win_attr);
Packit 971217
Packit 971217
  gst_gl_window_x11_handle_events (GST_GL_WINDOW (window_x11),
Packit 971217
      window_x11->priv->handle_events);
Packit 971217
Packit 971217
  XSync (window_x11->device, FALSE);
Packit 971217
Packit 971217
  XSetWindowBackgroundPixmap (window_x11->device,
Packit 971217
      window_x11->internal_win_id, None);
Packit 971217
Packit 971217
  GST_LOG ("gl window id: %lud", (gulong) window_x11->internal_win_id);
Packit 971217
  GST_LOG ("gl window props: x:%d y:%d", x, y);
Packit 971217
Packit 971217
  wm_atoms[0] = XInternAtom (window_x11->device, "WM_DELETE_WINDOW", True);
Packit 971217
  if (wm_atoms[0] == None)
Packit 971217
    GST_DEBUG ("Cannot create WM_DELETE_WINDOW");
Packit 971217
Packit 971217
  XSetWMProtocols (window_x11->device, window_x11->internal_win_id,
Packit 971217
      wm_atoms, 1);
Packit 971217
Packit 971217
  wm_hints.flags = StateHint;
Packit 971217
  wm_hints.initial_state = NormalState;
Packit 971217
  wm_hints.input = False;
Packit 971217
Packit 971217
  XStringListToTextProperty ((char **) &title, 1, &text_property);
Packit 971217
Packit 971217
  XSetWMProperties (window_x11->device, window_x11->internal_win_id,
Packit 971217
      &text_property, &text_property, 0, 0, NULL, &wm_hints, NULL);
Packit 971217
Packit 971217
  XFree (text_property.value);
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_gl_window_x11_close (GstGLWindow * window)
Packit 971217
{
Packit 971217
  GstGLWindowX11 *window_x11 = GST_GL_WINDOW_X11 (window);
Packit 971217
Packit 971217
  if (window_x11->device) {
Packit 971217
    if (window_x11->internal_win_id) {
Packit 971217
      XUnmapWindow (window_x11->device, window_x11->internal_win_id);
Packit 971217
Packit 971217
      XDestroyWindow (window_x11->device, window_x11->internal_win_id);
Packit 971217
    }
Packit 971217
    XFree (window_x11->visual_info);
Packit 971217
Packit 971217
    GST_DEBUG ("display receiver closed");
Packit 971217
  }
Packit 971217
Packit 971217
  window_x11->running = FALSE;
Packit 971217
Packit 971217
  GST_GL_WINDOW_CLASS (parent_class)->close (window);
Packit 971217
}
Packit 971217
Packit 971217
/* called by the gl thread */
Packit 971217
static void
Packit 971217
gst_gl_window_x11_set_window_handle (GstGLWindow * window, guintptr id)
Packit 971217
{
Packit 971217
  GstGLWindowX11 *window_x11;
Packit 971217
  gint x, y, width, height;
Packit 971217
Packit 971217
  window_x11 = GST_GL_WINDOW_X11 (window);
Packit 971217
Packit 971217
  window_x11->parent_win = (Window) id;
Packit 971217
Packit 971217
  if (window_x11->priv->render_rect.w > 0 &&
Packit 971217
      window_x11->priv->render_rect.h > 0) {
Packit 971217
    x = window_x11->priv->render_rect.x;
Packit 971217
    y = window_x11->priv->render_rect.y;
Packit 971217
    width = window_x11->priv->render_rect.w;
Packit 971217
    height = window_x11->priv->render_rect.h;
Packit 971217
  } else {
Packit 971217
    x = y = 0;
Packit 971217
    if (window_x11->parent_win) {
Packit 971217
      XWindowAttributes attr;
Packit 971217
Packit 971217
      XGetWindowAttributes (window_x11->device, window_x11->parent_win, &attr);
Packit 971217
      width = attr.width;
Packit 971217
      height = attr.height;
Packit 971217
    } else {
Packit 971217
      width = window_x11->priv->preferred_width;
Packit 971217
      height = window_x11->priv->preferred_height;
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
  XResizeWindow (window_x11->device, window_x11->internal_win_id,
Packit 971217
      width, height);
Packit 971217
Packit 971217
  XReparentWindow (window_x11->device, window_x11->internal_win_id,
Packit 971217
      window_x11->parent_win, x, y);
Packit 971217
Packit 971217
  XSync (window_x11->device, FALSE);
Packit 971217
}
Packit 971217
Packit 971217
struct SetRenderRectangle
Packit 971217
{
Packit 971217
  GstGLWindowX11 *window_x11;
Packit 971217
  GstVideoRectangle rect;
Packit 971217
};
Packit 971217
Packit 971217
static void
Packit 971217
_free_set_render_rectangle (struct SetRenderRectangle *render)
Packit 971217
{
Packit 971217
  if (render) {
Packit 971217
    if (render->window_x11)
Packit 971217
      gst_object_unref (render->window_x11);
Packit 971217
    g_free (render);
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
_set_render_rectangle (gpointer data)
Packit 971217
{
Packit 971217
  struct SetRenderRectangle *render = data;
Packit 971217
Packit 971217
  GST_LOG_OBJECT (render->window_x11, "setting render rectangle %i,%i+%ix%i",
Packit 971217
      render->rect.x, render->rect.y, render->rect.w, render->rect.h);
Packit 971217
Packit 971217
  if (render->window_x11->internal_win_id)
Packit 971217
    XMoveResizeWindow (render->window_x11->device,
Packit 971217
        render->window_x11->internal_win_id, render->rect.x, render->rect.y,
Packit 971217
        render->rect.w, render->rect.h);
Packit 971217
Packit 971217
  if (render->window_x11->device)
Packit 971217
    XSync (render->window_x11->device, FALSE);
Packit 971217
Packit 971217
  render->window_x11->priv->render_rect = render->rect;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
gst_gl_window_x11_set_render_rectangle (GstGLWindow * window,
Packit 971217
    int x, int y, int width, int height)
Packit 971217
{
Packit 971217
  GstGLWindowX11 *window_x11 = GST_GL_WINDOW_X11 (window);
Packit 971217
  struct SetRenderRectangle *render;
Packit 971217
Packit 971217
  render = g_new0 (struct SetRenderRectangle, 1);
Packit 971217
  render->window_x11 = gst_object_ref (window_x11);
Packit 971217
  render->rect.x = x;
Packit 971217
  render->rect.y = y;
Packit 971217
  render->rect.w = width;
Packit 971217
  render->rect.h = height;
Packit 971217
Packit 971217
  gst_gl_window_send_message_async (window,
Packit 971217
      (GstGLWindowCB) _set_render_rectangle, render,
Packit 971217
      (GDestroyNotify) _free_set_render_rectangle);
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
static guintptr
Packit 971217
gst_gl_window_x11_get_window_handle (GstGLWindow * window)
Packit 971217
{
Packit 971217
  GstGLWindowX11 *window_x11;
Packit 971217
Packit 971217
  window_x11 = GST_GL_WINDOW_X11 (window);
Packit 971217
Packit 971217
  return window_x11->internal_win_id;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_gl_window_x11_set_preferred_size (GstGLWindow * window, gint width,
Packit 971217
    gint height)
Packit 971217
{
Packit 971217
  GstGLWindowX11 *window_x11 = GST_GL_WINDOW_X11 (window);
Packit 971217
Packit 971217
  window_x11->priv->preferred_width = width;
Packit 971217
  window_x11->priv->preferred_height = height;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
_show_window (GstGLWindow * window)
Packit 971217
{
Packit 971217
  GstGLWindowX11 *window_x11 = GST_GL_WINDOW_X11 (window);
Packit 971217
  guint width = window_x11->priv->preferred_width;
Packit 971217
  guint height = window_x11->priv->preferred_height;
Packit 971217
Packit 971217
  if (!window_x11->visible) {
Packit 971217
    if (!window_x11->parent_win) {
Packit 971217
      XResizeWindow (window_x11->device, window_x11->internal_win_id,
Packit 971217
          width, height);
Packit 971217
    }
Packit 971217
Packit 971217
    XMapWindow (window_x11->device, window_x11->internal_win_id);
Packit 971217
    XSync (window_x11->device, FALSE);
Packit 971217
    window_x11->visible = TRUE;
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_gl_window_x11_show (GstGLWindow * window)
Packit 971217
{
Packit 971217
  gst_gl_window_send_message (window, (GstGLWindowCB) _show_window, window);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
_context_draw (GstGLContext * context, GstGLWindow * window)
Packit 971217
{
Packit 971217
  window->draw (window->draw_data);
Packit 971217
  gst_gl_context_swap_buffers (context);
Packit 971217
Packit 971217
  gst_object_unref (context);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
draw_cb (gpointer data)
Packit 971217
{
Packit 971217
  GstGLWindowX11 *window_x11 = data;
Packit 971217
  GstGLWindow *window = GST_GL_WINDOW (window_x11);
Packit 971217
  guint width, height;
Packit 971217
  XWindowAttributes attr;
Packit 971217
Packit 971217
  if (window_x11->internal_win_id) {
Packit 971217
    gboolean need_resize = FALSE;
Packit 971217
Packit 971217
    XGetWindowAttributes (window_x11->device, window_x11->internal_win_id,
Packit 971217
        &attr);
Packit 971217
    GST_TRACE_OBJECT (window, "window size %ux%u", attr.width, attr.height);
Packit 971217
Packit 971217
    if (window_x11->parent_win &&
Packit 971217
        (window_x11->priv->render_rect.w < 0 ||
Packit 971217
            window_x11->priv->render_rect.h < 0)) {
Packit 971217
      XWindowAttributes attr_parent;
Packit 971217
      XGetWindowAttributes (window_x11->device, window_x11->parent_win,
Packit 971217
          &attr_parent);
Packit 971217
      GST_TRACE_OBJECT (window, "parent window size %ux%u", attr_parent.width,
Packit 971217
          attr_parent.height);
Packit 971217
Packit 971217
      if (attr.width != attr_parent.width || attr.height != attr_parent.height) {
Packit 971217
        XMoveResizeWindow (window_x11->device, window_x11->internal_win_id,
Packit 971217
            0, 0, attr_parent.width, attr_parent.height);
Packit 971217
        XSync (window_x11->device, FALSE);
Packit 971217
Packit 971217
        attr.width = attr_parent.width;
Packit 971217
        attr.height = attr_parent.height;
Packit 971217
Packit 971217
        GST_LOG ("parent resize:  %d, %d",
Packit 971217
            attr_parent.width, attr_parent.height);
Packit 971217
        need_resize = TRUE;
Packit 971217
      }
Packit 971217
    }
Packit 971217
Packit 971217
    gst_gl_window_get_surface_dimensions (window, &width, &height);
Packit 971217
    if (attr.width != width || attr.height != height)
Packit 971217
      need_resize = TRUE;
Packit 971217
Packit 971217
    if (need_resize)
Packit 971217
      gst_gl_window_queue_resize (window);
Packit 971217
Packit 971217
    if (window_x11->allow_extra_expose_events) {
Packit 971217
      if (window->queue_resize)
Packit 971217
        gst_gl_window_resize (window, width, height);
Packit 971217
Packit 971217
      if (window->draw) {
Packit 971217
        GstGLContext *context = gst_gl_window_get_context (window);
Packit 971217
Packit 971217
        _context_draw (context, window);
Packit 971217
      }
Packit 971217
    }
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
/* Not called by the gl thread */
Packit 971217
static void
Packit 971217
gst_gl_window_x11_draw (GstGLWindow * window)
Packit 971217
{
Packit 971217
  gst_gl_window_send_message (window, (GstGLWindowCB) draw_cb, window);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_gl_window_x11_handle_events (GstGLWindow * window, gboolean handle_events)
Packit 971217
{
Packit 971217
  GstGLWindowX11 *window_x11;
Packit 971217
Packit 971217
  g_return_if_fail (window != NULL);
Packit 971217
Packit 971217
  window_x11 = GST_GL_WINDOW_X11 (window);
Packit 971217
Packit 971217
  window_x11->priv->handle_events = handle_events;
Packit 971217
Packit 971217
  if (window_x11->internal_win_id) {
Packit 971217
    if (handle_events) {
Packit 971217
      XSelectInput (window_x11->device, window_x11->internal_win_id,
Packit 971217
          StructureNotifyMask | ExposureMask | VisibilityChangeMask |
Packit 971217
          PointerMotionMask | KeyPressMask | KeyReleaseMask | ButtonPressMask |
Packit 971217
          ButtonReleaseMask);
Packit 971217
    } else {
Packit 971217
      XSelectInput (window_x11->device, window_x11->internal_win_id,
Packit 971217
          StructureNotifyMask | ExposureMask | VisibilityChangeMask);
Packit 971217
    }
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
gboolean
Packit 971217
gst_gl_window_x11_handle_event (GstGLWindowX11 * window_x11,
Packit 971217
    xcb_generic_event_t * event)
Packit 971217
{
Packit 971217
  GstGLWindow *window = GST_GL_WINDOW (window_x11);
Packit 971217
  GstGLDisplayX11 *display_x11 = GST_GL_DISPLAY_X11 (window->display);
Packit 971217
  xcb_connection_t *connection = display_x11->xcb_connection;
Packit 971217
  uint8_t event_code = event->response_type & 0x7f;
Packit 971217
Packit 971217
  switch (event_code) {
Packit 971217
    case XCB_CLIENT_MESSAGE:{
Packit 971217
      xcb_client_message_event_t *client_event;
Packit 971217
      xcb_intern_atom_cookie_t cookie;
Packit 971217
      xcb_intern_atom_reply_t *reply;
Packit 971217
Packit 971217
      client_event = (xcb_client_message_event_t *) event;
Packit 971217
      cookie = xcb_intern_atom (connection, 0, 16, "WM_DELETE_WINDOW");
Packit 971217
      reply = xcb_intern_atom_reply (connection, cookie, 0);
Packit 971217
Packit 971217
      if (client_event->data.data32[0] == reply->atom) {
Packit 971217
        GST_INFO_OBJECT (window_x11, "Close requested");
Packit 971217
Packit 971217
        if (window->close)
Packit 971217
          window->close (window->close_data);
Packit 971217
Packit 971217
        gst_gl_display_remove_window (GST_GL_DISPLAY (display_x11),
Packit 971217
            GST_GL_WINDOW (window_x11));
Packit 971217
      }
Packit 971217
Packit 971217
      g_free (reply);
Packit 971217
      break;
Packit 971217
    }
Packit 971217
    case XCB_CONFIGURE_NOTIFY:{
Packit 971217
      xcb_configure_notify_event_t *configure_event;
Packit 971217
Packit 971217
      configure_event = (xcb_configure_notify_event_t *) event;
Packit 971217
Packit 971217
      gst_gl_window_resize (window, configure_event->width,
Packit 971217
          configure_event->height);
Packit 971217
Packit 971217
      gst_gl_window_draw (window);
Packit 971217
      break;
Packit 971217
    }
Packit 971217
    case XCB_EXPOSE:{
Packit 971217
      xcb_expose_event_t *expose_event = (xcb_expose_event_t *) event;
Packit 971217
      /* non-zero means that other Expose follows
Packit 971217
       * so just wait for the last one
Packit 971217
       * in theory we should not receive non-zero because
Packit 971217
       * we have no sub areas here but just in case */
Packit 971217
      if (expose_event->count != 0)
Packit 971217
        break;
Packit 971217
Packit 971217
      gst_gl_window_draw (window);
Packit 971217
      break;
Packit 971217
    }
Packit 971217
    case XCB_KEY_PRESS:
Packit 971217
    case XCB_KEY_RELEASE:{
Packit 971217
      xcb_key_press_event_t *kp = (xcb_key_press_event_t *) event;
Packit 971217
      const gchar *event_type_str;
Packit 971217
      gchar *key_str;
Packit 971217
      KeySym keysym;
Packit 971217
Packit 971217
      keysym = XkbKeycodeToKeysym (window_x11->device, kp->detail, 0, 0);
Packit 971217
      key_str = XKeysymToString (keysym);
Packit 971217
Packit 971217
      if (event_code == XCB_KEY_PRESS)
Packit 971217
        event_type_str = "key-press";
Packit 971217
      else
Packit 971217
        event_type_str = "key-release";
Packit 971217
Packit 971217
      gst_gl_window_send_key_event (window, event_type_str, key_str);
Packit 971217
      break;
Packit 971217
    }
Packit 971217
    case XCB_BUTTON_PRESS:
Packit 971217
    case XCB_BUTTON_RELEASE:{
Packit 971217
      xcb_button_press_event_t *bp = (xcb_button_press_event_t *) event;
Packit 971217
      const gchar *event_type_str;
Packit 971217
Packit 971217
      if (event_code == XCB_BUTTON_PRESS)
Packit 971217
        event_type_str = "mouse-button-press";
Packit 971217
      else
Packit 971217
        event_type_str = "mouse-button-release";
Packit 971217
Packit 971217
      gst_gl_window_send_mouse_event (window, event_type_str, bp->detail,
Packit 971217
          (double) bp->event_x, (double) bp->event_y);
Packit 971217
      break;
Packit 971217
    }
Packit 971217
    case XCB_MOTION_NOTIFY:{
Packit 971217
      xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
Packit 971217
Packit 971217
      gst_gl_window_send_mouse_event (window, "mouse-move", 0,
Packit 971217
          (double) motion->event_x, (double) motion->event_y);
Packit 971217
      break;
Packit 971217
    }
Packit 971217
    default:
Packit 971217
      GST_TRACE ("unhandled XCB event: %u", event_code);
Packit 971217
      break;
Packit 971217
  }
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
static int
Packit 971217
error_handler (Display * xdpy, XErrorEvent * error)
Packit 971217
{
Packit 971217
  TrappedErrorCode = error->error_code;
Packit 971217
  return 0;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_gl_window_x11_trap_x_errors:
Packit 971217
 *
Packit 971217
 * Traps every X error until gst_gl_window_x11_untrap_x_errors() is called.
Packit 971217
 */
Packit 971217
void
Packit 971217
gst_gl_window_x11_trap_x_errors (void)
Packit 971217
{
Packit 971217
  TrappedErrorCode = 0;
Packit 971217
  old_error_handler = XSetErrorHandler (error_handler);
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_gl_window_x11_untrap_x_errors:
Packit 971217
 *
Packit 971217
 * Removes the X error trap and returns the current status.
Packit 971217
 *
Packit 971217
 * Return value: the trapped error code, or 0 for success
Packit 971217
 */
Packit 971217
gint
Packit 971217
gst_gl_window_x11_untrap_x_errors (void)
Packit 971217
{
Packit 971217
  XSetErrorHandler (old_error_handler);
Packit 971217
Packit 971217
  return TrappedErrorCode;
Packit 971217
}
Packit 971217
Packit 971217
static guintptr
Packit 971217
gst_gl_window_x11_get_display (GstGLWindow * window)
Packit 971217
{
Packit 971217
  GstGLWindowX11 *window_x11 = GST_GL_WINDOW_X11 (window);
Packit 971217
Packit 971217
  return (guintptr) window_x11->device;
Packit 971217
}