]> git.sesse.net Git - vlc/blobdiff - modules/video_output/xcb/x11.c
Revert "vout_window_t: simplify via anynomous union"
[vlc] / modules / video_output / xcb / x11.c
index 88970cd271bbeb4eac979416f6165bf72456b38d..b4641f426774164426a6c53360d19e3a072c180e 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"
 
-#define DISPLAY_TEXT N_("X11 display")
-#define DISPLAY_LONGTEXT N_( \
-    "X11 hardware display to use. By default VLC will " \
-    "use the value of the DISPLAY environment variable.")
-
 #define SHM_TEXT N_("Use shared memory")
 #define SHM_LONGTEXT N_( \
     "Use shared memory to communicate between VLC and the X server.")
@@ -53,15 +48,15 @@ static void Close (vlc_object_t *);
  * Module descriptor
  */
 vlc_module_begin ()
-    set_shortname (N_("XCB"))
-    set_description (N_("(Experimental) XCB video output"))
+    set_shortname (N_("X11"))
+    set_description (N_("X11 video output (XCB)"))
     set_category (CAT_VIDEO)
     set_subcategory (SUBCAT_VIDEO_VOUT)
-    set_capability ("vout display", 0)
+    set_capability ("vout display", 75)
     set_callbacks (Open, Close)
+    add_shortcut ("xcb-x11")
+    add_shortcut ("x11")
 
-    add_string ("x11-display", NULL, NULL,
-                DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
     add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
 vlc_module_end ()
 
@@ -76,9 +71,11 @@ struct vout_display_sys_t
     xcb_connection_t *conn;
     vout_window_t *embed; /* VLC window (when windowed) */
 
+    xcb_cursor_t cursor; /* blank cursor */
     xcb_window_t window; /* drawable X window */
     xcb_gcontext_t gc; /* context to put images */
     bool shm; /* whether to use MIT-SHM */
+    bool visible; /* whether to draw */
     uint8_t bpp; /* bits per pixel */
     uint8_t pad; /* scanline pad */
     uint8_t depth; /* useful bits per pixel */
@@ -88,7 +85,7 @@ struct vout_display_sys_t
     picture_resource_t resource[MAX_PICTURES];
 };
 
-static picture_t *Get (vout_display_t *);
+static picture_pool_t *Pool (vout_display_t *, unsigned);
 static void Display (vout_display_t *, picture_t *);
 static int Control (vout_display_t *, int, va_list);
 static void Manage (vout_display_t *);
@@ -106,21 +103,13 @@ static int Open (vlc_object_t *obj)
         return VLC_ENOMEM;
 
     vd->sys = p_sys;
+    p_sys->pool = NULL;
 
-    /* Connect to X */
-    p_sys->conn = Connect (obj);
-    if (p_sys->conn == NULL)
-    {
-        free (p_sys);
-        return VLC_EGENERIC;
-    }
-
-    /* Get window */
+    /* Get window, connect to X server */
     const xcb_screen_t *scr;
-    p_sys->embed = GetWindow (vd, p_sys->conn, &scr, &p_sys->shm);
+    p_sys->embed = GetWindow (vd, &p_sys->conn, &scr, &p_sys->depth);
     if (p_sys->embed == NULL)
     {
-        xcb_disconnect (p_sys->conn);
         free (p_sys);
         return VLC_EGENERIC;
     }
@@ -131,45 +120,88 @@ static int Open (vlc_object_t *obj)
     /* */
     video_format_t fmt_pic = vd->fmt;
 
-    /* Determine our video format. Normally, this is done in pf_init(), but
-     * this plugin always uses the same format for a given X11 screen. */
+    /* Check that the selected screen supports this depth */
+    xcb_depth_t *d = NULL;
+    for (xcb_depth_iterator_t it = xcb_screen_allowed_depths_iterator (scr);
+         it.rem > 0 && d == NULL;
+         xcb_depth_next (&it))
+        if (it.data->depth == p_sys->depth)
+            d = it.data;
+    if (d == NULL)
+    {
+        msg_Err (obj, "unexpected color depth msimatch");
+        goto error; /* WTH? depth not supported on screen */
+    }
+
+    /* Find a visual type for the selected depth */
     xcb_visualid_t vid = 0;
-    uint8_t depth = 0;
     bool gray = true;
+    const xcb_visualtype_t *vt = xcb_depth_visuals (d);
+    for (int i = xcb_depth_visuals_length (d); i > 0; i--)
+    {
+        if (vt->_class == XCB_VISUAL_CLASS_TRUE_COLOR)
+        {
+            vid = vt->visual_id;
+            fmt_pic.i_rmask = vt->red_mask;
+            fmt_pic.i_gmask = vt->green_mask;
+            fmt_pic.i_bmask = vt->blue_mask;
+            gray = false;
+            break;
+        }
+        if (p_sys->depth == 8 && vt->_class == XCB_VISUAL_CLASS_STATIC_GRAY
+         && vid == 0)
+        {
+            vid = vt->visual_id;
+        }
+    }
+
+    if (!vid)
+    {
+        msg_Err (obj, "unexpected visual type mismatch");
+        goto error;
+    }
+    msg_Dbg (vd, "using X11 visual ID 0x%"PRIx32" (depth: %"PRIu8")", vid,
+             p_sys->depth);
+
+    /* Determine our pixel format */
+    p_sys->bpp = 0;
     for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
              *end = fmt + xcb_setup_pixmap_formats_length (setup);
-         fmt < end; fmt++)
+         fmt < end && !p_sys->bpp; fmt++)
     {
-        vlc_fourcc_t chroma = 0;
-
-        if (fmt->depth < depth)
-            continue; /* We already found a better format! */
+        if (fmt->depth != p_sys->depth)
+            continue; /* Wrong depth! */
 
         /* Check that the pixmap format is supported by VLC. */
         switch (fmt->depth)
         {
+          case 32:
+            if (fmt->bits_per_pixel != 32)
+                continue;
+            fmt_pic.i_chroma = VLC_CODEC_RGBA;
+            break;
           case 24:
             if (fmt->bits_per_pixel == 32)
-                chroma = VLC_CODEC_RGB32;
+                fmt_pic.i_chroma = VLC_CODEC_RGB32;
             else if (fmt->bits_per_pixel == 24)
-                chroma = VLC_CODEC_RGB24;
+                fmt_pic.i_chroma = VLC_CODEC_RGB24;
             else
                 continue;
             break;
           case 16:
             if (fmt->bits_per_pixel != 16)
                 continue;
-            chroma = VLC_CODEC_RGB16;
+            fmt_pic.i_chroma = VLC_CODEC_RGB16;
             break;
           case 15:
             if (fmt->bits_per_pixel != 16)
                 continue;
-            chroma = VLC_CODEC_RGB15;
+            fmt_pic.i_chroma = VLC_CODEC_RGB15;
             break;
           case 8:
             if (fmt->bits_per_pixel != 8)
                 continue;
-            chroma = VLC_CODEC_RGB8;
+            fmt_pic.i_chroma = gray ? VLC_CODEC_GREY : VLC_CODEC_RGB8;
             break;
           default:
             continue;
@@ -182,57 +214,18 @@ static int Open (vlc_object_t *obj)
         if (fmt->bits_per_pixel == 16 && setup->image_byte_order != ORDER)
             continue;
 
-        /* Check that the selected screen supports this depth */
-        xcb_depth_iterator_t it = xcb_screen_allowed_depths_iterator (scr);
-        while (it.rem > 0 && it.data->depth != fmt->depth)
-             xcb_depth_next (&it);
-        if (!it.rem)
-            continue; /* Depth not supported on this screen */
-
-        /* Find a visual type for the selected depth */
-        const xcb_visualtype_t *vt = xcb_depth_visuals (it.data);
-        for (int i = xcb_depth_visuals_length (it.data); i > 0; i--)
-        {
-            if (vt->_class == XCB_VISUAL_CLASS_TRUE_COLOR)
-            {
-                vid = vt->visual_id;
-                gray = false;
-                break;
-            }
-            if (fmt->depth == 8 && vt->_class == XCB_VISUAL_CLASS_STATIC_GRAY)
-            {
-                if (!gray)
-                    continue; /* Prefer color over gray scale */
-                vid = vt->visual_id;
-                chroma = VLC_CODEC_GREY;
-            }
-        }
-
-        if (!vid)
-            continue; /* The screen does not *really* support this depth */
-
-        fmt_pic.i_chroma = chroma;
-        if (!gray)
-        {
-            fmt_pic.i_rmask = vt->red_mask;
-            fmt_pic.i_gmask = vt->green_mask;
-            fmt_pic.i_bmask = vt->blue_mask;
-        }
         p_sys->bpp = fmt->bits_per_pixel;
         p_sys->pad = fmt->scanline_pad;
-        p_sys->depth = depth = fmt->depth;
+        msg_Dbg (vd, " %"PRIu8" bits per pixels, %"PRIu8" bits line pad",
+                 p_sys->bpp, p_sys->pad);
     }
 
-    if (depth == 0)
+    if (!p_sys->bpp)
     {
-        msg_Err (vd, "no supported pixmap formats or visual types");
+        msg_Err (vd, "no supported pixmap formats");
         goto error;
     }
 
-    msg_Dbg (vd, "using X11 visual ID 0x%"PRIx32, vid);
-    msg_Dbg (vd, " %"PRIu8" bits per pixels, %"PRIu8" bits line pad",
-             p_sys->bpp, p_sys->pad);
-
     /* Create colormap (needed to select non-default visual) */
     xcb_colormap_t cmap;
     if (vid != scr->root_visual)
@@ -245,36 +238,42 @@ static int Open (vlc_object_t *obj)
         cmap = scr->default_colormap;
 
     /* Create window */
+    unsigned width, height;
+    if (GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
+        goto error;
+
+    p_sys->window = xcb_generate_id (p_sys->conn);
+    p_sys->gc = xcb_generate_id (p_sys->conn);
     {
         const uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_COLORMAP;
         const uint32_t values[] = {
             /* XCB_CW_EVENT_MASK */
-            XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
-            XCB_EVENT_MASK_POINTER_MOTION,
+            XCB_EVENT_MASK_VISIBILITY_CHANGE,
             /* XCB_CW_COLORMAP */
             cmap,
         };
         xcb_void_cookie_t c;
-        xcb_window_t window = xcb_generate_id (p_sys->conn);
 
-        c = xcb_create_window_checked (p_sys->conn, depth, window,
-                                       p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
+        c = xcb_create_window_checked (p_sys->conn, p_sys->depth,
+                                       p_sys->window,
+                                       p_sys->embed->handle.xid, 0, 0,
+                                       width, height, 0,
                                        XCB_WINDOW_CLASS_INPUT_OUTPUT,
                                        vid, mask, values);
+        xcb_map_window (p_sys->conn, p_sys->window);
+        /* Create graphic context (I wonder why the heck do we need this) */
+        xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
+
         if (CheckError (vd, p_sys->conn, "cannot create X11 window", c))
             goto error;
-        p_sys->window = window;
-        msg_Dbg (vd, "using X11 window %08"PRIx32, p_sys->window);
-        xcb_map_window (p_sys->conn, window);
     }
-
-    /* Create graphic context (I wonder why the heck do we need this) */
-    p_sys->gc = xcb_generate_id (p_sys->conn);
-    xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
+    msg_Dbg (vd, "using X11 window %08"PRIx32, p_sys->window);
     msg_Dbg (vd, "using X11 graphic context %08"PRIx32, p_sys->gc);
+    p_sys->cursor = CreateBlankCursor (p_sys->conn, scr);
 
-    /* */
-    p_sys->pool = NULL;
+    p_sys->visible = false;
+
+    CheckSHM (obj, p_sys->conn, &p_sys->shm);
 
     /* */
     vout_display_info_t info = vd->info;
@@ -284,17 +283,15 @@ static int Open (vlc_object_t *obj)
     vd->fmt = fmt_pic;
     vd->info = info;
 
-    vd->get = Get;
+    vd->pool = Pool;
     vd->prepare = NULL;
     vd->display = Display;
     vd->control = Control;
     vd->manage = Manage;
 
     /* */
-    unsigned width, height;
-    if (!GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
-        vout_display_SendEventDisplaySize (vd, width, height);
     vout_display_SendEventFullscreen (vd, false);
+    vout_display_SendEventDisplaySize (vd, width, height, false);
 
     return VLC_SUCCESS;
 
@@ -313,18 +310,19 @@ static void Close (vlc_object_t *obj)
     vout_display_sys_t *p_sys = vd->sys;
 
     ResetPictures (vd);
-    vout_display_DeleteWindow (vd, p_sys->embed);
-    /* colormap and window are garbage-collected by X */
+    /* colormap, window and context are garbage-collected by X */
     xcb_disconnect (p_sys->conn);
+    vout_display_DeleteWindow (vd, p_sys->embed);
     free (p_sys);
 }
 
 /**
  * Return a direct buffer
  */
-static picture_t *Get (vout_display_t *vd)
+static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
 {
     vout_display_sys_t *p_sys = vd->sys;
+    (void)requested_count;
 
     if (!p_sys->pool)
     {
@@ -371,16 +369,11 @@ static picture_t *Get (vout_display_t *vd)
             return NULL;
 
         p_sys->pool = picture_pool_New (count, pic_array);
-        if (!p_sys->pool)
-        {
-            /* TODO release picture resources */
-            return NULL;
-        }
-        /* FIXME should also do it in case of error ? */
+        /* TODO release picture resources if NULL */
         xcb_flush (p_sys->conn);
     }
 
-    return picture_pool_Get (p_sys->pool);
+    return p_sys->pool;
 }
 
 /**
@@ -390,9 +383,12 @@ static void Display (vout_display_t *vd, picture_t *pic)
 {
     vout_display_sys_t *p_sys = vd->sys;
     xcb_shm_seg_t segment = pic->p_sys->segment;
+    xcb_void_cookie_t ck;
 
+    if (!p_sys->visible)
+        goto out;
     if (segment != 0)
-        xcb_shm_put_image (p_sys->conn, p_sys->window, p_sys->gc,
+        ck = xcb_shm_put_image_checked (p_sys->conn, p_sys->window, p_sys->gc,
           /* real width */ pic->p->i_pitch / pic->p->i_pixel_pitch,
          /* real height */ pic->p->i_lines,
                    /* x */ vd->fmt.i_x_offset,
@@ -406,17 +402,28 @@ static void Display (vout_display_t *vd, picture_t *pic)
         const size_t offset = vd->fmt.i_y_offset * pic->p->i_pitch;
         const unsigned lines = pic->p->i_lines - vd->fmt.i_y_offset;
 
-        xcb_put_image (p_sys->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
+        ck = xcb_put_image_checked (p_sys->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
                        p_sys->window, p_sys->gc,
                        pic->p->i_pitch / pic->p->i_pixel_pitch,
                        lines, -vd->fmt.i_x_offset, 0, 0, p_sys->depth,
                        pic->p->i_pitch * lines, pic->p->p_pixels + offset);
     }
-    xcb_flush (p_sys->conn);
+
+    /* Wait for reply. This makes sure that the X server gets CPU time to
+     * display the picture. xcb_flush() is *not* sufficient: especially with
+     * shared memory the PUT requests are so short that many of them can fit in
+     * X11 socket output buffer before the kernel preempts VLC. */
+    xcb_generic_error_t *e = xcb_request_check (p_sys->conn, ck);
+    if (e != NULL)
+    {
+        msg_Dbg (vd, "%s: X11 error %d", "cannot put image", e->error_code);
+        free (e);
+    }
 
     /* FIXME might be WAY better to wait in some case (be carefull with
      * VOUT_DISPLAY_RESET_PICTURES if done) + does not work with
      * vout_display wrapper. */
+out:
     picture_Release (pic);
 }
 
@@ -436,10 +443,12 @@ static int Control (vout_display_t *vd, int query, va_list ap)
     {
         const vout_display_cfg_t *p_cfg =
             (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
+        const bool is_forced = (bool)va_arg (ap, int);
 
-        if (vout_window_SetSize (p_sys->embed,
-                                  p_cfg->display.width,
-                                  p_cfg->display.height))
+        if (is_forced
+         && vout_window_SetSize (p_sys->embed,
+                                 p_cfg->display.width,
+                                 p_cfg->display.height))
             return VLC_EGENERIC;
 
         vout_display_place_t place;
@@ -459,10 +468,10 @@ static int Control (vout_display_t *vd, int query, va_list ap)
                               values);
         return VLC_SUCCESS;
     }
-    case VOUT_DISPLAY_CHANGE_ON_TOP:
+    case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
     {
-        int b_on_top = (int)va_arg (ap, int);
-        return vout_window_SetOnTop (p_sys->embed, b_on_top);
+        unsigned state = va_arg (ap, unsigned);
+        return vout_window_SetState (p_sys->embed, state);
     }
 
     case VOUT_DISPLAY_CHANGE_ZOOM:
@@ -490,12 +499,13 @@ static int Control (vout_display_t *vd, int query, va_list ap)
         return VLC_SUCCESS;
     }
 
-    /* TODO */
-#if 0
     /* Hide the mouse. It will be send when
      * vout_display_t::info.b_hide_mouse is false */
-    VOUT_DISPLAY_HIDE_MOUSE,
-#endif
+    case VOUT_DISPLAY_HIDE_MOUSE:
+        xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid,
+                                  XCB_CW_CURSOR, &(uint32_t){ p_sys->cursor });
+        return VLC_SUCCESS;
+
     default:
         msg_Err (vd, "Unknown request in XCB vout display");
         return VLC_EGENERIC;
@@ -506,7 +516,7 @@ static void Manage (vout_display_t *vd)
 {
     vout_display_sys_t *p_sys = vd->sys;
 
-    ManageEvent (vd, p_sys->conn, p_sys->window);
+    ManageEvent (vd, p_sys->conn, &p_sys->visible);
 }
 
 static void ResetPictures (vout_display_t *vd)