]> git.sesse.net Git - vlc/blobdiff - modules/video_output/xcb/events.c
Qt: EPGView: includes cleanup
[vlc] / modules / video_output / xcb / events.c
index 4ad765fe411d9813b729615bb69fa64ceb1c211b..797fdcf69eeebeae80ce02b4e766c6b16f3e4c46 100644 (file)
@@ -5,20 +5,20 @@
 /*****************************************************************************
  * Copyright © 2009 Rémi Denis-Courmont
  *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
+ * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- ****************************************************************************/
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
@@ -28,9 +28,6 @@
 #include <assert.h>
 
 #include <xcb/xcb.h>
-#ifndef XCB_CURSOR_NONE
-# define XCB_CURSOR_NONE ((xcb_cursor_t) 0U)
-#endif
 
 #include <vlc_common.h>
 #include <vlc_vout_display.h>
@@ -48,29 +45,145 @@ int CheckError (vout_display_t *vd, xcb_connection_t *conn,
     err = xcb_request_check (conn, ck);
     if (err)
     {
-        msg_Err (vd, "%s: X11 error %d", str, err->error_code);
+        int code = err->error_code;
+
         free (err);
-        return VLC_EGENERIC;
+        msg_Err (vd, "%s: X11 error %d", str, code);
+        assert (code != 0);
+        return code;
     }
-    return VLC_SUCCESS;
+    return 0;
+}
+
+/**
+ * Connect to the X server.
+ */
+static xcb_connection_t *Connect (vlc_object_t *obj, const char *display)
+{
+    xcb_connection_t *conn = xcb_connect (display, NULL);
+    if (xcb_connection_has_error (conn) /*== NULL*/)
+    {
+        msg_Err (obj, "cannot connect to X server (%s)",
+                 (display != NULL) ? display : "default");
+        xcb_disconnect (conn);
+        return NULL;
+    }
+
+    const xcb_setup_t *setup = xcb_get_setup (conn);
+    msg_Dbg (obj, "connected to X%"PRIu16".%"PRIu16" server",
+             setup->protocol_major_version, setup->protocol_minor_version);
+    char *vendor = strndup (xcb_setup_vendor (setup), setup->vendor_len);
+    if (vendor)
+    {
+        msg_Dbg (obj, " vendor : %s", vendor);
+        free (vendor);
+    }
+    msg_Dbg (obj, " version: %"PRIu32, setup->release_number);
+    return conn;
+}
+
+/**
+ * (Try to) register to mouse events on a window if needed.
+ */
+static void RegisterEvents (vlc_object_t *obj, xcb_connection_t *conn,
+                            xcb_window_t wnd)
+{
+    /* Subscribe to parent window resize events */
+    uint32_t value = XCB_EVENT_MASK_POINTER_MOTION
+                   | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
+    xcb_change_window_attributes (conn, wnd, XCB_CW_EVENT_MASK, &value);
+    /* Try to subscribe to click events */
+    /* (only one X11 client can get them, so might not work) */
+    if (var_InheritBool (obj, "mouse-events"))
+    {
+        value |= XCB_EVENT_MASK_BUTTON_PRESS
+               | XCB_EVENT_MASK_BUTTON_RELEASE;
+        xcb_change_window_attributes (conn, wnd,
+                                      XCB_CW_EVENT_MASK, &value);
+    }
+}
+
+/**
+ * Find screen matching a given root window.
+ */
+static const xcb_screen_t *FindScreen (vlc_object_t *obj,
+                                       xcb_connection_t *conn,
+                                       xcb_window_t root)
+{
+    /* Find the selected screen */
+    const xcb_setup_t *setup = xcb_get_setup (conn);
+    for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
+         i.rem > 0; xcb_screen_next (&i))
+    {
+        if (i.data->root == root)
+        {
+            msg_Dbg (obj, "using screen 0x%"PRIx32, root);
+            return i.data;
+        }
+    }
+    msg_Err (obj, "window screen not found");
+    return NULL;
 }
 
 /**
- * Gets the size of an X window.
+ * Create a VLC video X window object, connect to the corresponding X server,
+ * find the corresponding X server screen.
  */
-int GetWindowSize (struct vout_window_t *wnd, xcb_connection_t *conn,
-                   unsigned *restrict width, unsigned *restrict height)
+vout_window_t *GetWindow (vout_display_t *vd,
+                          xcb_connection_t **restrict pconn,
+                          const xcb_screen_t **restrict pscreen,
+                          uint8_t *restrict pdepth,
+                          uint16_t *restrict pwidth,
+                          uint16_t *restrict pheight)
 {
-    xcb_get_geometry_cookie_t ck = xcb_get_geometry (conn, wnd->xid);
-    xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply (conn, ck, NULL);
+    vout_window_cfg_t cfg = {
+        .type = VOUT_WINDOW_TYPE_XID,
+        .x = var_InheritInteger (vd, "video-x"),
+        .y = var_InheritInteger (vd, "video-y"),
+        .width  = vd->cfg->display.width,
+        .height = vd->cfg->display.height,
+    };
+
+    vout_window_t *wnd = vout_display_NewWindow (vd, &cfg);
+    if (wnd == NULL)
+    {
+        msg_Err (vd, "window not available");
+        return NULL;
+    }
+
+    xcb_connection_t *conn = Connect (VLC_OBJECT(vd), wnd->display.x11);
+    if (conn == NULL)
+        goto error;
+    *pconn = conn;
 
-    if (!geo)
-        return -1;
+    /* Events must be registered before the window geometry is queried, so as
+     * to avoid missing impeding resize events. */
+    RegisterEvents (VLC_OBJECT(vd), conn, wnd->handle.xid);
 
-    *width = geo->width;
-    *height = geo->height;
+    xcb_get_geometry_reply_t *geo =
+        xcb_get_geometry_reply (conn, xcb_get_geometry (conn, wnd->handle.xid),
+                                NULL);
+    if (geo == NULL)
+    {
+        msg_Err (vd, "window not valid");
+        goto error;
+    }
+    *pdepth = geo->depth;
+    *pwidth = geo->width;
+    *pheight = geo->height;
+
+    const xcb_screen_t *screen = FindScreen (VLC_OBJECT(vd), conn, geo->root);
     free (geo);
-    return 0;
+    if (screen == NULL)
+        goto error;
+    *pscreen = screen;
+    return wnd;
+
+error:
+    if (conn != NULL)
+        xcb_disconnect (conn);
+    vout_display_DeleteWindow (vd, wnd);
+    return NULL;
 }
 
 /**
@@ -87,7 +200,7 @@ xcb_cursor_t CreateBlankCursor (xcb_connection_t *conn,
     xcb_pixmap_t pix = xcb_generate_id (conn);
 
     xcb_create_pixmap (conn, 1, pix, scr->root, 1, 1);
-    xcb_create_cursor (conn, cur, pix, pix, 0, 0, 0, 1, 1, 1, 0, 0);
+    xcb_create_cursor (conn, cur, pix, pix, 0, 0, 0, 0, 0, 0, 1, 1);
     return cur;
 }
 
@@ -117,6 +230,7 @@ static void HandleMotionNotify (vout_display_t *vd, xcb_connection_t *conn,
     /* show the default cursor */
     xcb_change_window_attributes (conn, ev->event, XCB_CW_CURSOR,
                                   &(uint32_t) { XCB_CURSOR_NONE });
+    xcb_flush (conn);
 
     /* TODO it could be saved */
     vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
@@ -143,9 +257,7 @@ static void
 HandleParentStructure (vout_display_t *vd,
                        const xcb_configure_notify_event_t *ev)
 {
-    if (ev->width  != vd->cfg->display.width ||
-        ev->height != vd->cfg->display.height)
-        vout_display_SendEventDisplaySize (vd, ev->width, ev->height, vd->cfg->is_fullscreen);
+    vout_display_SendEventDisplaySize (vd, ev->width, ev->height, vd->cfg->is_fullscreen);
 }
 
 /**