]> 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 375dfd27b18d3d7c183135f5ed266a8cee0994b6..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 <stdlib.h>
 #include <assert.h>
 
-#include <sys/types.h>
-#include <sys/shm.h>
-
 #include <xcb/xcb.h>
 #include <xcb/shm.h>
 
-#include <xcb/xcb_aux.h>
-#include <xcb/xcb_image.h>
-
 #include <vlc_common.h>
 #include <vlc_plugin.h>
-#include <vlc_vout.h>
-#include <vlc_window.h>
+#include <vlc_vout_display.h>
+#include <vlc_picture_pool.h>
 
 #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.")
@@ -59,218 +48,251 @@ 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 ("video output", 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 ()
 
-struct vout_sys_t
+/* It must be large enough to absorb the server display jitter but it is
+ * useless to used a too large value, direct rendering cannot be used with
+ * xcb x11
+ */
+#define MAX_PICTURES (3)
+
+struct vout_display_sys_t
 {
     xcb_connection_t *conn;
-    xcb_screen_t *screen;
     vout_window_t *embed; /* VLC window (when windowed) */
 
-    xcb_visualid_t vid; /* selected visual */
-    xcb_window_t parent; /* parent X window */
-    xcb_colormap_t cmap; /* colormap for selected visual */
+    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 */
-};
-
-static int Init (vout_thread_t *);
-static void Deinit (vout_thread_t *);
-static void Render (vout_thread_t *, picture_t *);
-static void Display (vout_thread_t *, picture_t *);
-static int Manage (vout_thread_t *);
+    uint8_t depth; /* useful bits per pixel */
+    uint8_t byte_order; /* server byte order */
 
-int CheckError (vout_thread_t *vout, const char *str, xcb_void_cookie_t ck)
-{
-    xcb_generic_error_t *err;
+    picture_pool_t *pool; /* picture pool */
+    picture_resource_t resource[MAX_PICTURES];
+};
 
-    err = xcb_request_check (vout->p_sys->conn, ck);
-    if (err)
-    {
-        msg_Err (vout, "%s: X11 error %d", str, err->error_code);
-        return VLC_EGENERIC;
-    }
-    return VLC_SUCCESS;
-}
+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 *);
 
-#define p_vout vout
+static void ResetPictures (vout_display_t *);
 
 /**
  * Probe the X server.
  */
 static int Open (vlc_object_t *obj)
 {
-    vout_thread_t *vout = (vout_thread_t *)obj;
-    vout_sys_t *p_sys = malloc (sizeof (*p_sys));
+    vout_display_t *vd = (vout_display_t *)obj;
+    vout_display_sys_t *p_sys = malloc (sizeof (*p_sys));
     if (p_sys == NULL)
         return VLC_ENOMEM;
 
-    vout->p_sys = p_sys;
-    p_sys->conn = NULL;
-    p_sys->embed = NULL;
+    vd->sys = p_sys;
+    p_sys->pool = NULL;
 
-    /* Connect to X */
-    char *display = var_CreateGetNonEmptyString (vout, "x11-display");
-    int snum;
-    p_sys->conn = xcb_connect (display, &snum);
-    if (xcb_connection_has_error (p_sys->conn) /*== NULL*/)
+    /* Get window, connect to X server */
+    const xcb_screen_t *scr;
+    p_sys->embed = GetWindow (vd, &p_sys->conn, &scr, &p_sys->depth);
+    if (p_sys->embed == NULL)
     {
-        msg_Err (vout, "cannot connect to X server %s",
-                 display ? display : "");
-        free (display);
-        goto error;
+        free (p_sys);
+        return VLC_EGENERIC;
     }
-    free (display);
 
     const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
+    p_sys->byte_order = setup->image_byte_order;
+
+    /* */
+    video_format_t fmt_pic = vd->fmt;
+
+    /* 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 */
+    }
 
-    /* Get the preferred screen */
-    xcb_screen_t *scr = xcb_aux_get_screen (p_sys->conn, snum);
-    p_sys->screen = scr;
-    assert (p_sys->screen);
-    msg_Dbg (vout, "using screen %d (depth %"PRIu8")", snum, scr->root_depth);
-
-    /* Determine the visual (color depth and palette) */
-    xcb_visualtype_t *vt = NULL;
-    bool gray = false;
-
-    if ((vt = xcb_aux_find_visual_by_attrs (scr, XCB_VISUAL_CLASS_TRUE_COLOR,
-                                            scr->root_depth)) != NULL)
-        msg_Dbg (vout, "using TrueColor visual ID %d", (int)vt->visual_id);
-    else
-#if 0
-    if ((vt = xcb_aux_find_visual_by_attrs (scr, XCB_VISUAL_CLASS_STATIC_COLOR,
-                                            scr->root_depth)) != NULL)
-        msg_Dbg (vout, "using static color visual ID %d", (int)vt->visual_id);
-    else
-#endif
-    if ((scr->root_depth == 8)
-     && (vt = xcb_aux_find_visual_by_attrs (scr, XCB_VISUAL_CLASS_STATIC_GRAY,
-                                            scr->root_depth)) != NULL)
+    /* Find a visual type for the selected depth */
+    xcb_visualid_t vid = 0;
+    bool gray = true;
+    const xcb_visualtype_t *vt = xcb_depth_visuals (d);
+    for (int i = xcb_depth_visuals_length (d); i > 0; i--)
     {
-        msg_Dbg (vout, "using static gray visual ID %d", (int)vt->visual_id);
-        gray = true;
+        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;
+        }
     }
-    else
+
+    if (!vid)
     {
-        msg_Err (vout, "no supported visual class");
+        msg_Err (obj, "unexpected visual type mismatch");
         goto error;
     }
-    p_sys->vid = vt->visual_id;
+    msg_Dbg (vd, "using X11 visual ID 0x%"PRIx32" (depth: %"PRIu8")", vid,
+             p_sys->depth);
 
-    /* Determine our input format (normally, done in Init() but X11
-     * never changes its format) */
-    vout->output.i_chroma = 0;
+    /* 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++)
     {
-        if (fmt->depth != scr->root_depth)
-            continue;
+        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)
-                vout->output.i_chroma = VLC_FOURCC ('R', 'V', '3', '2');
+                fmt_pic.i_chroma = VLC_CODEC_RGB32;
             else if (fmt->bits_per_pixel == 24)
-                vout->output.i_chroma = VLC_FOURCC ('R', 'V', '2', '4');
+                fmt_pic.i_chroma = VLC_CODEC_RGB24;
             else
                 continue;
             break;
           case 16:
             if (fmt->bits_per_pixel != 16)
                 continue;
-            vout->output.i_chroma = VLC_FOURCC ('R', 'V', '1', '6');
+            fmt_pic.i_chroma = VLC_CODEC_RGB16;
             break;
           case 15:
             if (fmt->bits_per_pixel != 16)
                 continue;
-            vout->output.i_chroma = VLC_FOURCC ('R', 'V', '1', '5');
+            fmt_pic.i_chroma = VLC_CODEC_RGB15;
             break;
           case 8:
             if (fmt->bits_per_pixel != 8)
                 continue;
-            vout->output.i_chroma = gray ? VLC_FOURCC ('G', 'R', 'E', 'Y')
-                                         : VLC_FOURCC ('R', 'G', 'B', '2');
+            fmt_pic.i_chroma = gray ? VLC_CODEC_GREY : VLC_CODEC_RGB8;
             break;
           default:
             continue;
         }
         if ((fmt->bits_per_pixel << 4) % fmt->scanline_pad)
             continue; /* VLC pads lines to 16 pixels internally */
+
+        /* Byte sex is a non-issue for 8-bits. It can be worked around with
+         * RGB masks for 24-bits. Too bad for 15-bits and 16-bits. */
+        if (fmt->bits_per_pixel == 16 && setup->image_byte_order != ORDER)
+            continue;
+
         p_sys->bpp = fmt->bits_per_pixel;
         p_sys->pad = fmt->scanline_pad;
-        msg_Dbg (vout, "using %"PRIu8" bits per pixels (line pad: %"PRIu8")",
+        msg_Dbg (vd, " %"PRIu8" bits per pixels, %"PRIu8" bits line pad",
                  p_sys->bpp, p_sys->pad);
-        break;
     }
 
-    if (!vout->output.i_chroma)
+    if (!p_sys->bpp)
     {
-        msg_Err (vout, "no supported pixmap formats");
+        msg_Err (vd, "no supported pixmap formats");
         goto error;
     }
 
-    vout->fmt_out.i_chroma = vout->output.i_chroma;
-    if (!gray)
+    /* Create colormap (needed to select non-default visual) */
+    xcb_colormap_t cmap;
+    if (vid != scr->root_visual)
     {
-        vout->output.i_rmask = vt->red_mask;
-        vout->output.i_gmask = vt->green_mask;
-        vout->output.i_bmask = vt->blue_mask;
+        cmap = xcb_generate_id (p_sys->conn);
+        xcb_create_colormap (p_sys->conn, XCB_COLORMAP_ALLOC_NONE,
+                             cmap, scr->root, vid);
     }
+    else
+        cmap = scr->default_colormap;
 
-    /* Create colormap (needed to select non-default visual) */
-    p_sys->cmap = xcb_generate_id (p_sys->conn);
-    xcb_create_colormap (p_sys->conn, XCB_COLORMAP_ALLOC_NONE,
-                         p_sys->cmap, scr->root, p_sys->vid);
+    /* Create window */
+    unsigned width, height;
+    if (GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
+        goto error;
 
-    /* Check shared memory support */
-    p_sys->shm = var_CreateGetBool (vout, "x11-shm") > 0;
-    if (p_sys->shm)
+    p_sys->window = xcb_generate_id (p_sys->conn);
+    p_sys->gc = xcb_generate_id (p_sys->conn);
     {
-        xcb_shm_query_version_cookie_t ck;
-        xcb_shm_query_version_reply_t *r;
-        xcb_generic_error_t *err;
-
-        ck = xcb_shm_query_version (p_sys->conn);
-        r = xcb_shm_query_version_reply (p_sys->conn, ck, &err);
-        if (!r)
-        {
-            msg_Err (vout, "shared memory (MIT-SHM) not available");
-            msg_Warn (vout, "display will be slow");
-            p_sys->shm = false;
-        }
+        const uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_COLORMAP;
+        const uint32_t values[] = {
+            /* XCB_CW_EVENT_MASK */
+            XCB_EVENT_MASK_VISIBILITY_CHANGE,
+            /* XCB_CW_COLORMAP */
+            cmap,
+        };
+        xcb_void_cookie_t c;
+
+        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;
     }
+    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);
 
-    /* Get window */
-    /* FIXME: WTH to put as initial width/height values??? */
-    p_sys->embed = vout_RequestXWindow (vout, &(int){ 0 }, &(int){ 0 },
-                                        &(unsigned){ 0 }, &(unsigned){ 0 });
-    if (p_sys->embed == NULL)
-    {
-        msg_Err (vout, "parent window not available");
-        goto error;
-    }
+    p_sys->visible = false;
+
+    CheckSHM (obj, p_sys->conn, &p_sys->shm);
+
+    /* */
+    vout_display_info_t info = vd->info;
+    info.has_pictures_invalid = true;
+
+    /* Setup vout_display_t once everything is fine */
+    vd->fmt = fmt_pic;
+    vd->info = info;
+
+    vd->pool = Pool;
+    vd->prepare = NULL;
+    vd->display = Display;
+    vd->control = Control;
+    vd->manage = Manage;
+
+    /* */
+    vout_display_SendEventFullscreen (vd, false);
+    vout_display_SendEventDisplaySize (vd, width, height, false);
 
-    vout->pf_init = Init;
-    vout->pf_end = Deinit;
-    vout->pf_render = Render;
-    vout->pf_display = Display;
-    vout->pf_manage = Manage;
     return VLC_SUCCESS;
 
 error:
@@ -284,320 +306,234 @@ error:
  */
 static void Close (vlc_object_t *obj)
 {
-    vout_thread_t *vout = (vout_thread_t *)obj;
-    vout_sys_t *p_sys = vout->p_sys;
-
-    if (p_sys->embed)
-        vout_ReleaseWindow (p_sys->embed);
-    /* colormap is garbage-ollected by X (?) */
-    if (p_sys->conn)
-        xcb_disconnect (p_sys->conn);
+    vout_display_t *vd = (vout_display_t *)obj;
+    vout_display_sys_t *p_sys = vd->sys;
+
+    ResetPictures (vd);
+    /* colormap, window and context are garbage-collected by X */
+    xcb_disconnect (p_sys->conn);
+    vout_display_DeleteWindow (vd, p_sys->embed);
     free (p_sys);
 }
 
-struct picture_sys_t
+/**
+ * Return a direct buffer
+ */
+static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
 {
-    xcb_connection_t *conn; /* Shared connection to X server */
-    xcb_image_t *image;  /* Picture buffer */
-    xcb_image_t *native; /* Rendered picture buffer (in X server format) */
-    xcb_shm_seg_t segment; /* Shared memory segment X ID */
-};
+    vout_display_sys_t *p_sys = vd->sys;
+    (void)requested_count;
 
-#define SHM_ERR ((void *)(intptr_t)(-1))
-
-static int PictureInit (vout_thread_t *vout, picture_t *pic)
-{
-    vout_sys_t *p_sys = vout->p_sys;
-    picture_sys_t *priv = malloc (sizeof (*p_sys));
+    if (!p_sys->pool)
+    {
+        vout_display_place_t place;
 
-    if (priv == NULL)
-        return VLC_ENOMEM;
+        vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
 
-    assert (pic->i_status == FREE_PICTURE);
-    vout_InitPicture (vout, pic, vout->output.i_chroma,
-                      vout->output.i_width, vout->output.i_height,
-                      vout->output.i_aspect);
+        /* */
+        const uint32_t values[] = { place.x, place.y, place.width, place.height };
+        xcb_configure_window (p_sys->conn, p_sys->window,
+                              XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
+                              XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
+                              values);
 
-    void *shm = SHM_ERR;
-    const size_t size = pic->p->i_pitch * pic->p->i_lines;
+        picture_t *pic = picture_NewFromFormat (&vd->fmt);
+        if (!pic)
+            return NULL;
 
-    if (p_sys->shm)
-    {   /* Allocate shared memory segment */
-        int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0700);
+        assert (pic->i_planes == 1);
+        memset (p_sys->resource, 0, sizeof(p_sys->resource));
 
-        if (id == -1)
+        unsigned count;
+        picture_t *pic_array[MAX_PICTURES];
+        for (count = 0; count < MAX_PICTURES; count++)
         {
-            msg_Err (vout, "shared memory allocation error: %m");
-            goto error;
+            picture_resource_t *res = &p_sys->resource[count];
+
+            res->p->i_lines = pic->p->i_lines;
+            res->p->i_pitch = pic->p->i_pitch;
+            if (PictureResourceAlloc (vd, res, res->p->i_pitch * res->p->i_lines,
+                                      p_sys->conn, p_sys->shm))
+                break;
+            pic_array[count] = picture_NewFromResource (&vd->fmt, res);
+            if (!pic_array[count])
+            {
+                PictureResourceFree (res, p_sys->conn);
+                memset (res, 0, sizeof(*res));
+                break;
+            }
         }
+        picture_Release (pic);
 
-        /* Attach the segment to VLC */
-        shm = shmat (id, NULL, 0 /* read/write */);
-        if (shm == SHM_ERR)
-        {
-            msg_Err (vout, "shared memory attachment error: %m");
-            shmctl (id, IPC_RMID, 0);
-            goto error;
-        }
+        if (count == 0)
+            return NULL;
 
-        /* Attach the segment to X */
-        xcb_void_cookie_t ck;
-
-        priv->segment = xcb_generate_id (p_sys->conn);
-        ck = xcb_shm_attach_checked (p_sys->conn, priv->segment, id, 1);
-        shmctl (id, IPC_RMID, 0);
-
-        if (CheckError (vout, "shared memory server-side error", ck))
-        {
-            msg_Info (vout, "using buggy X (remote) server? SSH?");
-            shmdt (shm);
-            shm = SHM_ERR;
-        }
+        p_sys->pool = picture_pool_New (count, pic_array);
+        /* TODO release picture resources if NULL */
+        xcb_flush (p_sys->conn);
     }
-    else
-        priv->segment = 0;
-
-    const unsigned real_width = pic->p->i_pitch / (p_sys->bpp >> 3);
-    /* FIXME: anyway to getthing more intuitive than that?? */
-
-    xcb_image_t *img;
-    img = xcb_image_create (real_width, pic->p->i_lines,
-                            XCB_IMAGE_FORMAT_Z_PIXMAP, p_sys->pad,
-                            p_sys->screen->root_depth, p_sys->bpp, p_sys->bpp,
-#ifdef WORDS_BIGENDIAN
-                            XCB_IMAGE_ORDER_MSB_FIRST,
-#else
-                            XCB_IMAGE_ORDER_LSB_FIRST,
-#endif
-                            XCB_IMAGE_ORDER_MSB_FIRST,
-                            NULL,
-                            (shm != SHM_ERR) ? size : 0,
-                            (shm != SHM_ERR) ? shm : NULL);
 
-    if (img == NULL)
-    {
-        if (shm != SHM_ERR)
-            xcb_shm_detach (p_sys->conn, priv->segment);
-        goto error;
-    }
-    if (shm != SHM_ERR && xcb_image_native (p_sys->conn, img, 0) == NULL)
-        msg_Warn (vout, "incompatible X server image format");
-
-    priv->conn = p_sys->conn;
-    priv->image = img;
-    priv->native = NULL;
-    pic->p_sys = priv;
-    pic->p->p_pixels = img->data;
-    pic->i_status = DESTROYED_PICTURE;
-    pic->i_type = DIRECT_PICTURE;
-    return VLC_SUCCESS;
-
-error:
-    if (shm != SHM_ERR)
-        shmdt (shm);
-    free (priv);
-    return VLC_EGENERIC;
+    return p_sys->pool;
 }
 
-
 /**
- * Release picture private data
+ * Sends an image to the X server.
  */
-static void PictureDeinit (picture_t *pic)
+static void Display (vout_display_t *vd, picture_t *pic)
 {
-    struct picture_sys_t *p_sys = pic->p_sys;
+    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)
+        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,
+                   /* y */ vd->fmt.i_y_offset,
+               /* width */ vd->fmt.i_visible_width,
+              /* height */ vd->fmt.i_visible_height,
+                           0, 0, p_sys->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
+                           0, segment, 0);
+    else
+    {
+        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;
+
+        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);
+    }
 
-    if (p_sys->segment != 0)
+    /* 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)
     {
-        xcb_shm_detach (p_sys->conn, p_sys->segment);
-        shmdt (p_sys->image->data);
+        msg_Dbg (vd, "%s: X11 error %d", "cannot put image", e->error_code);
+        free (e);
     }
-    if ((p_sys->native != NULL) && (p_sys->native != p_sys->image))
-        xcb_image_destroy (p_sys->native);
-    xcb_image_destroy (p_sys->image);
-    free (p_sys);
+
+    /* 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);
 }
 
-/**
- * Allocate drawable window and picture buffers.
- */
-static int Init (vout_thread_t *vout)
+static int Control (vout_display_t *vd, int query, va_list ap)
 {
-    vout_sys_t *p_sys = vout->p_sys;
-    const xcb_screen_t *screen = p_sys->screen;
-    unsigned x, y, width, height;
+    vout_display_sys_t *p_sys = vd->sys;
 
-    I_OUTPUTPICTURES = 0;
-
-    /* Determine parent window and size */
-    if (vout->b_fullscreen)
+    switch (query)
     {
-        p_sys->parent = screen->root;
-        width = screen->width_in_pixels;
-        height = screen->height_in_pixels;
-    }
-    else
+    case VOUT_DISPLAY_CHANGE_FULLSCREEN:
     {
-        p_sys->parent = p_sys->embed->handle.xid;
-
-        /* Subscribe to parent window resize events */
-        const uint32_t value = XCB_EVENT_MASK_STRUCTURE_NOTIFY;
-        xcb_change_window_attributes (p_sys->conn, p_sys->parent,
-                                      XCB_CW_EVENT_MASK, &value);
-
-        xcb_get_geometry_cookie_t ck;
-        ck = xcb_get_geometry (p_sys->conn, p_sys->parent);
-
-        xcb_get_geometry_reply_t *geo;
-        xcb_generic_error_t *err;
-        geo = xcb_get_geometry_reply (p_sys->conn, ck, &err);
-        width = geo->width;
-        height = geo->height;
-        free (geo);
+        const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
+        return vout_window_SetFullScreen (p_sys->embed, c->is_fullscreen);
     }
 
-    vout_PlacePicture (vout, width, height, &x, &y, &width, &height);
-
-    /* FIXME: I don't get the subtlety between output and fmt_out here */
-    vout->fmt_out.i_visible_width = width;
-    vout->fmt_out.i_visible_height = height;
-    vout->fmt_out.i_sar_num = vout->fmt_out.i_sar_den = 1;
-
-    vout->output.i_width = vout->fmt_out.i_width =
-        width * vout->fmt_in.i_width / vout->fmt_in.i_visible_width;
-    vout->output.i_height = vout->fmt_out.i_height =
-        height * vout->fmt_in.i_height / vout->fmt_in.i_visible_height;
-    vout->fmt_out.i_x_offset =
-        width * vout->fmt_in.i_x_offset / vout->fmt_in.i_visible_width;
-    p_vout->fmt_out.i_y_offset =
-        height * vout->fmt_in.i_y_offset / vout->fmt_in.i_visible_height;
+    case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
+    {
+        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);
 
-    assert (height > 0);
-    vout->output.i_aspect = vout->fmt_out.i_aspect =
-        width * VOUT_ASPECT_FACTOR / height;
+        if (is_forced
+         && vout_window_SetSize (p_sys->embed,
+                                 p_cfg->display.width,
+                                 p_cfg->display.height))
+            return VLC_EGENERIC;
 
-    /* Create window */
-    const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK
-                        | XCB_CW_COLORMAP;
-    const uint32_t values[] = {
-        /* XCB_CW_BACK_PIXEL */
-        screen->black_pixel,
-        /* XCB_CW_EVENT_MASK */
-        XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
-        XCB_EVENT_MASK_POINTER_MOTION,
-        /* XCB_CW_COLORMAP */
-        p_sys->cmap,
-    };
-    xcb_void_cookie_t c;
-    xcb_window_t window = xcb_generate_id (p_sys->conn);
-
-    c = xcb_create_window_checked (p_sys->conn, screen->root_depth, window,
-                                   p_sys->parent, x, y, width, height, 0,
-                                   XCB_WINDOW_CLASS_INPUT_OUTPUT,
-                                   p_sys->vid, mask, values);
-    if (CheckError (vout, "cannot create X11 window", c))
-        goto error;
-    p_sys->window = window;
-    msg_Dbg (vout, "using X11 window %08"PRIx32, p_sys->window);
-    xcb_map_window (p_sys->conn, window);
+        vout_display_place_t place;
+        vout_display_PlacePicture (&place, &vd->source, p_cfg, false);
 
-    /* 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 (vout, "using X11 graphic context %08"PRIx32, p_sys->gc);
-    xcb_flush (p_sys->conn);
+        if (place.width  != vd->fmt.i_visible_width ||
+            place.height != vd->fmt.i_visible_height)
+        {
+            vout_display_SendEventPicturesInvalid (vd);
+            return VLC_SUCCESS;
+        }
 
-    /* Allocate picture buffers */
-    I_OUTPUTPICTURES = 0;
-    for (size_t index = 0; I_OUTPUTPICTURES < 2; index++)
+        /* Move the picture within the window */
+        const uint32_t values[] = { place.x, place.y };
+        xcb_configure_window (p_sys->conn, p_sys->window,
+                              XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
+                              values);
+        return VLC_SUCCESS;
+    }
+    case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
     {
-        picture_t *pic = vout->p_picture + index;
-
-        if (index > sizeof (vout->p_picture) / sizeof (pic))
-            break;
-        if (pic->i_status != FREE_PICTURE)
-            continue;
-        if (PictureInit (vout, pic))
-            break;
-        PP_OUTPUTPICTURE[I_OUTPUTPICTURES++] = pic;
+        unsigned state = va_arg (ap, unsigned);
+        return vout_window_SetState (p_sys->embed, state);
     }
 
-    return VLC_SUCCESS;
+    case VOUT_DISPLAY_CHANGE_ZOOM:
+    case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
+    case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
+    case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
+        /* I am not sure it is always necessary, but it is way simpler ... */
+        vout_display_SendEventPicturesInvalid (vd);
+        return VLC_SUCCESS;
 
-error:
-    Deinit (vout);
-    return VLC_EGENERIC;
-}
+    case VOUT_DISPLAY_RESET_PICTURES:
+    {
+        ResetPictures (vd);
 
-/**
- * Free picture buffers.
- */
-static void Deinit (vout_thread_t *vout)
-{
-    vout_sys_t *p_sys = vout->p_sys;
+        vout_display_place_t place;
+        vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
 
-    for (int i = 0; i < I_OUTPUTPICTURES; i++)
-        PictureDeinit (PP_OUTPUTPICTURE[i]);
+        vd->fmt.i_width  = vd->source.i_width  * place.width  / vd->source.i_visible_width;
+        vd->fmt.i_height = vd->source.i_height * place.height / vd->source.i_visible_height;
 
-    xcb_unmap_window (p_sys->conn, p_sys->window);
-    xcb_destroy_window (p_sys->conn, p_sys->window);
-}
+        vd->fmt.i_visible_width  = place.width;
+        vd->fmt.i_visible_height = place.height;
+        vd->fmt.i_x_offset = vd->source.i_x_offset * place.width  / vd->source.i_visible_width;
+        vd->fmt.i_y_offset = vd->source.i_y_offset * place.height / vd->source.i_visible_height;
+        return VLC_SUCCESS;
+    }
 
-/**
- * Prepares an image ahead of display.
- */
-static void Render (vout_thread_t *vout, picture_t *pic)
-{
-    vout_sys_t *p_sys = vout->p_sys;
-    picture_sys_t *priv = pic->p_sys;
+    /* Hide the mouse. It will be send when
+     * vout_display_t::info.b_hide_mouse is false */
+    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;
 
-    if ((priv->native != NULL) && (priv->native != priv->image))
-        xcb_image_destroy (priv->native);
-    priv->native = xcb_image_native (p_sys->conn, priv->image, 1);
+    default:
+        msg_Err (vd, "Unknown request in XCB vout display");
+        return VLC_EGENERIC;
+    }
 }
 
-
-/**
- * Sends an image to the X server.
- */
-static void Display (vout_thread_t *vout, picture_t *pic)
+static void Manage (vout_display_t *vd)
 {
-    vout_sys_t *p_sys = vout->p_sys;
-    picture_sys_t *priv = pic->p_sys;
-    xcb_image_t *img = priv->image, *native = priv->native;
-
-    if ((native == img) && (img->base == NULL))
-    {
-        xcb_shm_segment_info_t info = {
-            .shmseg = priv->segment,
-            .shmid = -1, /* lost track of it, unimportant */
-            .shmaddr = img->data,
-        };
+    vout_display_sys_t *p_sys = vd->sys;
 
-        xcb_image_shm_put (p_sys->conn, p_sys->window, p_sys->gc, img, info,
-                        0, 0, 0, 0, img->width, img->height, 0);
-    }
-    else
-    if (native != NULL)
-        xcb_image_put (p_sys->conn, p_sys->window, p_sys->gc, native, 0, 0, 0);
-    xcb_flush (p_sys->conn);
+    ManageEvent (vd, p_sys->conn, &p_sys->visible);
 }
 
-/**
- * Process incoming X events.
- */
-static int Manage (vout_thread_t *vout)
+static void ResetPictures (vout_display_t *vd)
 {
-    vout_sys_t *p_sys = vout->p_sys;
-    xcb_generic_event_t *ev;
+    vout_display_sys_t *p_sys = vd->sys;
 
-    while ((ev = xcb_poll_for_event (p_sys->conn)) != NULL)
-        ProcessEvent (vout, p_sys->conn, p_sys->window, ev);
+    if (!p_sys->pool)
+        return;
 
-    if (xcb_connection_has_error (p_sys->conn))
+    for (unsigned i = 0; i < MAX_PICTURES; i++)
     {
-        msg_Err (vout, "X server failure");
-        return VLC_EGENERIC;
+        picture_resource_t *res = &p_sys->resource[i];
+
+        if (!res->p->p_pixels)
+            break;
+        PictureResourceFree (res, p_sys->conn);
     }
-    return VLC_SUCCESS;
+    picture_pool_Delete (p_sys->pool);
+    p_sys->pool = NULL;
 }