]> git.sesse.net Git - vlc/blobdiff - modules/video_output/xcb/common.c
Merge branch 'master' into lpcm_encoder
[vlc] / modules / video_output / xcb / common.c
index e006247088d0244401a4016470d2c798b056e400..2b7588f319823000e67de25369f357ef35b912ed 100644 (file)
@@ -7,7 +7,7 @@
  *
  * 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.0
+ * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
@@ -15,7 +15,7 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU Lesser General Public
+ * 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
  ****************************************************************************/
 #include "xcb_vlc.h"
 
 /**
- * Check for an error
+ * Connect to the X server.
  */
-int CheckError (vout_display_t *vd, xcb_connection_t *conn,
-                const char *str, xcb_void_cookie_t ck)
+static xcb_connection_t *Connect (vlc_object_t *obj, const char *display)
 {
-    xcb_generic_error_t *err;
+    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 ? display : "default");
+        xcb_disconnect (conn);
+        return NULL;
+    }
 
-    err = xcb_request_check (conn, ck);
-    if (err)
+    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_Err (vd, "%s: X11 error %d", str, err->error_code);
-        return VLC_EGENERIC;
+        msg_Dbg (obj, " vendor : %s", vendor);
+        free (vendor);
     }
-    return VLC_SUCCESS;
+    msg_Dbg (obj, " version: %"PRIu32, setup->release_number);
+    return conn;
 }
 
 /**
- * Connect to the X server.
+ * Find screen matching a given root window.
  */
-xcb_connection_t *Connect (vlc_object_t *obj)
+static const xcb_screen_t *FindScreen (vlc_object_t *obj,
+                                       xcb_connection_t *conn,
+                                       xcb_window_t root)
 {
-    char *display = var_CreateGetNonEmptyString (obj, "x11-display");
-    xcb_connection_t *conn = xcb_connect (display, NULL);
+    /* Find the selected screen */
+    const xcb_setup_t *setup = xcb_get_setup (conn);
+    const xcb_screen_t *screen = NULL;
+    for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
+         i.rem > 0 && screen == NULL; xcb_screen_next (&i))
+    {
+        if (i.data->root == root)
+            screen = i.data;
+    }
 
-    free (display);
-    if (xcb_connection_has_error (conn) /*== NULL*/)
+    if (screen == NULL)
     {
-        msg_Err (obj, "cannot connect to X server");
-        xcb_disconnect (conn);
+        msg_Err (obj, "parent window screen not found");
         return NULL;
     }
-    return conn;
+    msg_Dbg (obj, "using screen 0x%"PRIx32, root);
+    return screen;
+}
+
+static const xcb_screen_t *FindWindow (vlc_object_t *obj,
+                                       xcb_connection_t *conn,
+                                       xcb_window_t xid,
+                                       uint8_t *restrict pdepth)
+{
+    xcb_get_geometry_reply_t *geo =
+        xcb_get_geometry_reply (conn, xcb_get_geometry (conn, xid), NULL);
+    if (geo == NULL)
+    {
+        msg_Err (obj, "parent window not valid");
+        return NULL;
+    }
+
+    const xcb_screen_t *screen = FindScreen (obj, conn, geo->root);
+    *pdepth = geo->depth;
+    free (geo);
+    return screen;
 }
 
 
 /**
- * Create a VLC video X window object, find the corresponding X server screen,
- * and probe the MIT-SHM extension.
+ * Create a VLC video X window object, connect to the corresponding X server,
+ * find the corresponding X server screen.
  */
 vout_window_t *GetWindow (vout_display_t *vd,
-                          xcb_connection_t *conn,
+                          xcb_connection_t **restrict pconn,
                           const xcb_screen_t **restrict pscreen,
-                          bool *restrict pshm)
+                          uint8_t *restrict pdepth)
 {
     /* Get window */
-    xcb_window_t root;
     vout_window_cfg_t wnd_cfg;
 
     memset( &wnd_cfg, 0, sizeof(wnd_cfg) );
     wnd_cfg.type = VOUT_WINDOW_TYPE_XID;
+    wnd_cfg.x = var_InheritInteger (vd, "video-x");
+    wnd_cfg.y = var_InheritInteger (vd, "video-y");
     wnd_cfg.width  = vd->cfg->display.width;
     wnd_cfg.height = vd->cfg->display.height;
 
@@ -98,51 +136,31 @@ vout_window_t *GetWindow (vout_display_t *vd,
         msg_Err (vd, "parent window not available");
         return NULL;
     }
-    else
-    {
-        xcb_get_geometry_reply_t *geo;
-        xcb_get_geometry_cookie_t ck;
-
-        ck = xcb_get_geometry (conn, wnd->handle.xid);
-        geo = xcb_get_geometry_reply (conn, ck, NULL);
-        if (geo == NULL)
-        {
-            msg_Err (vd, "parent window not valid");
-            goto error;
-        }
-        root = geo->root;
-        free (geo);
-
-        /* Subscribe to parent window resize events */
-        uint32_t value = XCB_EVENT_MASK_STRUCTURE_NOTIFY;
-        xcb_change_window_attributes (conn, wnd->handle.xid,
-                                      XCB_CW_EVENT_MASK, &value);
-        /* Try to subscribe to click events */
-        /* (only one X11 client can get them, so might not work) */
-        value |= XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE;
-        xcb_change_window_attributes (conn, wnd->handle.xid,
-                                      XCB_CW_EVENT_MASK, &value);
-    }
 
-    /* Find the selected screen */
-    const xcb_setup_t *setup = xcb_get_setup (conn);
-    const xcb_screen_t *screen = NULL;
-    for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
-         i.rem > 0 && screen == NULL; xcb_screen_next (&i))
-    {
-        if (i.data->root == root)
-            screen = i.data;
-    }
+    xcb_connection_t *conn = Connect (VLC_OBJECT(vd), wnd->display.x11);
+    if (conn == NULL)
+        goto error;
+    *pconn = conn;
 
-    if (screen == NULL)
+    *pscreen = FindWindow (VLC_OBJECT(vd), conn, wnd->handle.xid, pdepth);
+    if (*pscreen == NULL)
     {
-        msg_Err (vd, "parent window screen not found");
+        xcb_disconnect (conn);
         goto error;
     }
-    msg_Dbg (vd, "using screen 0x%"PRIx32, root);
 
-    /* Check MIT-SHM shared memory support */
-    bool shm = var_CreateGetBool (vd, "x11-shm") > 0;
+    RegisterMouseEvents (VLC_OBJECT(vd), conn, wnd->handle.xid);
+    return wnd;
+
+error:
+    vout_display_DeleteWindow (vd, wnd);
+    return NULL;
+}
+
+/** Check MIT-SHM shared memory support */
+void CheckSHM (vlc_object_t *obj, xcb_connection_t *conn, bool *restrict pshm)
+{
+    bool shm = var_InheritBool (obj, "x11-shm") > 0;
     if (shm)
     {
         xcb_shm_query_version_cookie_t ck;
@@ -152,38 +170,13 @@ vout_window_t *GetWindow (vout_display_t *vd,
         r = xcb_shm_query_version_reply (conn, ck, NULL);
         if (!r)
         {
-            msg_Err (vd, "shared memory (MIT-SHM) not available");
-            msg_Warn (vd, "display will be slow");
+            msg_Err (obj, "shared memory (MIT-SHM) not available");
+            msg_Warn (obj, "display will be slow");
             shm = false;
         }
         free (r);
     }
-
-    *pscreen = screen;
     *pshm = shm;
-    return wnd;
-
-error:
-    vout_display_DeleteWindow (vd, wnd);
-    return NULL;
-}
-
-/**
- * Gets the size of an X window.
- */
-int GetWindowSize (struct vout_window_t *wnd, xcb_connection_t *conn,
-                   unsigned *restrict width, unsigned *restrict height)
-{
-    xcb_get_geometry_cookie_t ck = xcb_get_geometry (conn, wnd->handle.xid);
-    xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply (conn, ck, NULL);
-
-    if (!geo)
-        return -1;
-
-    *width = geo->width;
-    *height = geo->height;
-    free (geo);
-    return 0;
 }
 
 /**
@@ -248,11 +241,8 @@ void PictureResourceFree (picture_resource_t *res, xcb_connection_t *conn)
 {
     xcb_shm_seg_t segment = res->p_sys->segment;
 
-    if (segment != 0)
-    {
-        assert (conn != NULL);
+    if (conn != NULL && segment != 0)
         xcb_shm_detach (conn, segment);
-    }
     shmdt (res->p->p_pixels);
 }