]> git.sesse.net Git - vlc/blobdiff - modules/video_output/xcb/common.c
XCB: unset the cursor when we detect pointer motion
[vlc] / modules / video_output / xcb / common.c
index 6bfd35f3292815886e13edeeb1255c9e982948f8..e9e55acea4fd9c4dd9df19bd50677ce0348f334e 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/shm.h>
 
 #include <vlc_common.h>
-#include <vlc_vout.h>
-#include <vlc_vout_window.h>
+#include <vlc_vout_display.h>
 
 #include "xcb_vlc.h"
 
+/**
+ * Check for an error
+ */
+int CheckError (vout_display_t *vd, xcb_connection_t *conn,
+                const char *str, xcb_void_cookie_t ck)
+{
+    xcb_generic_error_t *err;
+
+    err = xcb_request_check (conn, ck);
+    if (err)
+    {
+        msg_Err (vd, "%s: X11 error %d", str, err->error_code);
+        free (err);
+        return VLC_EGENERIC;
+    }
+    return VLC_SUCCESS;
+}
+
 /**
  * Connect to the X server.
  */
@@ -54,6 +71,17 @@ xcb_connection_t *Connect (vlc_object_t *obj)
         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;
 }
 
@@ -62,7 +90,7 @@ xcb_connection_t *Connect (vlc_object_t *obj)
  * Create a VLC video X window object, find the corresponding X server screen,
  * and probe the MIT-SHM extension.
  */
-vout_window_t *GetWindow (vout_thread_t *obj,
+vout_window_t *GetWindow (vout_display_t *vd,
                           xcb_connection_t *conn,
                           const xcb_screen_t **restrict pscreen,
                           bool *restrict pshm)
@@ -72,15 +100,14 @@ vout_window_t *GetWindow (vout_thread_t *obj,
     vout_window_cfg_t wnd_cfg;
 
     memset( &wnd_cfg, 0, sizeof(wnd_cfg) );
-    wnd_cfg.type = VOUT_WINDOW_TYPE_XWINDOW;
-    wnd_cfg.width  = obj->i_window_width;
-    wnd_cfg.height = obj->i_window_height;
-
-    vout_window_t *wnd = vout_window_New (VLC_OBJECT(obj), NULL, &wnd_cfg);
+    wnd_cfg.type = VOUT_WINDOW_TYPE_XID;
+    wnd_cfg.width  = vd->cfg->display.width;
+    wnd_cfg.height = vd->cfg->display.height;
 
+    vout_window_t *wnd = vout_display_NewWindow (vd, &wnd_cfg);
     if (wnd == NULL)
     {
-        msg_Err (obj, "parent window not available");
+        msg_Err (vd, "parent window not available");
         return NULL;
     }
     else
@@ -92,14 +119,20 @@ vout_window_t *GetWindow (vout_thread_t *obj,
         geo = xcb_get_geometry_reply (conn, ck, NULL);
         if (geo == NULL)
         {
-            msg_Err (obj, "parent window not valid");
+            msg_Err (vd, "parent window not valid");
             goto error;
         }
         root = geo->root;
         free (geo);
 
         /* Subscribe to parent window resize events */
-        const uint32_t value = XCB_EVENT_MASK_STRUCTURE_NOTIFY;
+        uint32_t value = XCB_EVENT_MASK_POINTER_MOTION
+                       | 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);
     }
@@ -116,13 +149,13 @@ vout_window_t *GetWindow (vout_thread_t *obj,
 
     if (screen == NULL)
     {
-        msg_Err (obj, "parent window screen not found");
+        msg_Err (vd, "parent window screen not found");
         goto error;
     }
-    msg_Dbg (obj, "using screen 0x%"PRIx32, root);
+    msg_Dbg (vd, "using screen 0x%"PRIx32, root);
 
     /* Check MIT-SHM shared memory support */
-    bool shm = var_CreateGetBool (obj, "x11-shm") > 0;
+    bool shm = var_CreateGetBool (vd, "x11-shm") > 0;
     if (shm)
     {
         xcb_shm_query_version_cookie_t ck;
@@ -132,8 +165,8 @@ vout_window_t *GetWindow (vout_thread_t *obj,
         r = xcb_shm_query_version_reply (conn, ck, NULL);
         if (!r)
         {
-            msg_Err (obj, "shared memory (MIT-SHM) not available");
-            msg_Warn (obj, "display will be slow");
+            msg_Err (vd, "shared memory (MIT-SHM) not available");
+            msg_Warn (vd, "display will be slow");
             shm = false;
         }
         free (r);
@@ -144,7 +177,7 @@ vout_window_t *GetWindow (vout_thread_t *obj,
     return wnd;
 
 error:
-    vout_window_Delete (wnd);
+    vout_display_DeleteWindow (vd, wnd);
     return NULL;
 }
 
@@ -166,21 +199,56 @@ int GetWindowSize (struct vout_window_t *wnd, xcb_connection_t *conn,
     return 0;
 }
 
+/**
+ * Create a blank cursor.
+ * Note that the pixmaps are leaked (until the X disconnection). Hence, this
+ * function should be called no more than once per X connection.
+ * @param conn XCB connection
+ * @param scr target XCB screen
+ */
+xcb_cursor_t CreateBlankCursor (xcb_connection_t *conn,
+                                const xcb_screen_t *scr)
+{
+    xcb_cursor_t cur = xcb_generate_id (conn);
+    xcb_pixmap_t pix = xcb_generate_id (conn);
+    xcb_void_cookie_t ck;
+    xcb_generic_error_t *err;
+
+    ck = xcb_create_pixmap_checked (conn, 1, pix, scr->root, 1, 1);
+    err = xcb_request_check (conn, ck);
+    if (err)
+    {
+        fprintf (stderr, "Cannot create pixmap: %d", err->error_code);
+        free (err);
+    }
+    ck = xcb_create_cursor_checked (conn, cur, pix, pix, 0, 0, 0, 0, 0, 0, 0, 0);
+    err = xcb_request_check (conn, ck);
+    if (err)
+    {
+        fprintf (stderr, "Cannot create pixmap: %d", err->error_code);
+        free (err);
+    }
+    return cur;
+}
+
 /**
  * Initialize a picture buffer as shared memory, according to the video output
- * format. If a XCB connection pointer is supplied, the segment is attached to
+ * format. If a attach is true, the segment is attached to
  * the X server (MIT-SHM extension).
  */
-int PictureAlloc (vout_thread_t *vout, picture_t *pic, size_t size,
-                  xcb_connection_t *conn)
+int PictureResourceAlloc (vout_display_t *vd, picture_resource_t *res, size_t size,
+                          xcb_connection_t *conn, bool attach)
 {
-    assert (pic->i_status == FREE_PICTURE);
+    res->p_sys = malloc (sizeof(*res->p_sys));
+    if (!res->p_sys)
+        return VLC_EGENERIC;
 
     /* Allocate shared memory segment */
     int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0700);
     if (id == -1)
     {
-        msg_Err (vout, "shared memory allocation error: %m");
+        msg_Err (vd, "shared memory allocation error: %m");
+        free (res->p_sys);
         return VLC_EGENERIC;
     }
 
@@ -188,13 +256,14 @@ int PictureAlloc (vout_thread_t *vout, picture_t *pic, size_t size,
     void *shm = shmat (id, NULL, 0 /* read/write */);
     if (-1 == (intptr_t)shm)
     {
-        msg_Err (vout, "shared memory attachment error: %m");
+        msg_Err (vd, "shared memory attachment error: %m");
         shmctl (id, IPC_RMID, 0);
+        free (res->p_sys);
         return VLC_EGENERIC;
     }
 
     xcb_shm_seg_t segment;
-    if (conn != NULL)
+    if (attach)
     {
         /* Attach the segment to X */
         xcb_void_cookie_t ck;
@@ -202,9 +271,9 @@ int PictureAlloc (vout_thread_t *vout, picture_t *pic, size_t size,
         segment = xcb_generate_id (conn);
         ck = xcb_shm_attach_checked (conn, segment, id, 1);
 
-        if (CheckError (vout, "shared memory server-side error", ck))
+        if (CheckError (vd, conn, "shared memory server-side error", ck))
         {
-            msg_Info (vout, "using buggy X11 server - SSH proxying?");
+            msg_Info (vd, "using buggy X11 server - SSH proxying?");
             segment = 0;
         }
     }
@@ -212,67 +281,20 @@ int PictureAlloc (vout_thread_t *vout, picture_t *pic, size_t size,
         segment = 0;
 
     shmctl (id, IPC_RMID, 0);
-    pic->p_sys = (void *)(uintptr_t)segment;
-    pic->p->p_pixels = shm;
-    pic->i_status = DESTROYED_PICTURE;
-    pic->i_type = DIRECT_PICTURE;
+    res->p_sys->segment = segment;
+    res->p->p_pixels = shm;
     return VLC_SUCCESS;
 }
 
 /**
  * Release picture private data: detach the shared memory segment.
  */
-void PictureFree (picture_t *pic, xcb_connection_t *conn)
+void PictureResourceFree (picture_resource_t *res, xcb_connection_t *conn)
 {
-    xcb_shm_seg_t segment = (uintptr_t)pic->p_sys;
+    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 (pic->p->p_pixels);
+    shmdt (res->p->p_pixels);
 }
 
-/**
- * Video output thread management stuff.
- * FIXME: Much of this should move to core
- */
-void CommonManage (vout_thread_t *vout)
-{
-    if (vout->i_changes & VOUT_SCALE_CHANGE)
-    {
-        vout->b_autoscale = var_GetBool (vout, "autoscale");
-        vout->i_zoom = ZOOM_FP_FACTOR;
-        vout->i_changes &= ~VOUT_SCALE_CHANGE;
-        vout->i_changes |= VOUT_SIZE_CHANGE;
-    }
-
-    if (vout->i_changes & VOUT_ZOOM_CHANGE)
-    {
-        vout->b_autoscale = false;
-        vout->i_zoom = var_GetFloat (vout, "scale") * ZOOM_FP_FACTOR;
-        vout->i_changes &= ~VOUT_ZOOM_CHANGE;
-        vout->i_changes |= VOUT_SIZE_CHANGE;
-    }
-
-    if (vout->i_changes & VOUT_CROP_CHANGE)
-    {
-        vout->fmt_out.i_x_offset = vout->fmt_in.i_x_offset;
-        vout->fmt_out.i_y_offset = vout->fmt_in.i_y_offset;
-        vout->fmt_out.i_visible_width = vout->fmt_in.i_visible_width;
-        vout->fmt_out.i_visible_height = vout->fmt_in.i_visible_height;
-        vout->i_changes &= ~VOUT_CROP_CHANGE;
-        vout->i_changes |= VOUT_SIZE_CHANGE;
-    }
-
-    if (vout->i_changes & VOUT_ASPECT_CHANGE)
-    {
-        vout->fmt_out.i_aspect = vout->fmt_in.i_aspect;
-        vout->fmt_out.i_sar_num = vout->fmt_in.i_sar_num;
-        vout->fmt_out.i_sar_den = vout->fmt_in.i_sar_den;
-        vout->output.i_aspect = vout->fmt_in.i_aspect;
-        vout->i_changes &= ~VOUT_ASPECT_CHANGE;
-        vout->i_changes |= VOUT_SIZE_CHANGE;
-    }
-}