]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/xvideo.c
03e9670bb6a3becbfcd7d3996bd89755fe0fa448
[vlc] / modules / video_output / xcb / xvideo.c
1 /**
2  * @file xvideo.c
3  * @brief X C Bindings video output module for VLC media player
4  */
5 /*****************************************************************************
6  * Copyright © 2009 Rémi Denis-Courmont
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  ****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <assert.h>
29
30 #include <xcb/xcb.h>
31 #include <xcb/shm.h>
32 #include <xcb/xv.h>
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_vout_display.h>
37 #include <vlc_picture_pool.h>
38 #include <vlc_dialog.h>
39
40 #include "xcb_vlc.h"
41
42 #define ADAPTOR_TEXT N_("XVideo adaptor number")
43 #define ADAPTOR_LONGTEXT N_( \
44     "XVideo hardware adaptor to use. By default, VLC will " \
45     "use the first functional adaptor.")
46
47 #define SHM_TEXT N_("Use shared memory")
48 #define SHM_LONGTEXT N_( \
49     "Use shared memory to communicate between VLC and the X server.")
50
51 static int  Open (vlc_object_t *);
52 static void Close (vlc_object_t *);
53
54 /*
55  * Module descriptor
56  */
57 vlc_module_begin ()
58     set_shortname (N_("XVideo"))
59     set_description (N_("XVideo output (XCB)"))
60     set_category (CAT_VIDEO)
61     set_subcategory (SUBCAT_VIDEO_VOUT)
62     set_capability ("vout display", 155)
63     set_callbacks (Open, Close)
64
65     add_integer ("xvideo-adaptor", -1, NULL,
66                  ADAPTOR_TEXT, ADAPTOR_LONGTEXT, true)
67     add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
68         add_deprecated_alias ("xvideo-shm")
69     add_shortcut ("xcb-xv")
70     add_shortcut ("xv")
71     add_shortcut ("xvideo")
72 vlc_module_end ()
73
74 #define MAX_PICTURES (VOUT_MAX_PICTURES)
75
76 struct vout_display_sys_t
77 {
78     xcb_connection_t *conn;
79     vout_window_t *embed;/* VLC window */
80
81     xcb_cursor_t cursor; /* blank cursor */
82     xcb_window_t window; /* drawable X window */
83     xcb_gcontext_t gc;   /* context to put images */
84     xcb_xv_port_t port;  /* XVideo port */
85     uint32_t id;         /* XVideo format */
86     uint16_t width;      /* display width */
87     uint16_t height;     /* display height */
88     uint32_t data_size;  /* picture byte size (for non-SHM) */
89     bool shm;            /* whether to use MIT-SHM */
90     bool visible;        /* whether it makes sense to draw at all */
91
92     xcb_xv_query_image_attributes_reply_t *att;
93     picture_pool_t *pool; /* picture pool */
94     picture_resource_t resource[MAX_PICTURES];
95 };
96
97 static picture_pool_t *Pool (vout_display_t *, unsigned);
98 static void Display (vout_display_t *, picture_t *);
99 static int Control (vout_display_t *, int, va_list);
100 static void Manage (vout_display_t *);
101
102 /**
103  * Check that the X server supports the XVideo extension.
104  */
105 static bool CheckXVideo (vout_display_t *vd, xcb_connection_t *conn)
106 {
107     xcb_xv_query_extension_reply_t *r;
108     xcb_xv_query_extension_cookie_t ck = xcb_xv_query_extension (conn);
109     bool ok = false;
110
111     /* We need XVideo 2.2 for PutImage */
112     r = xcb_xv_query_extension_reply (conn, ck, NULL);
113     if (r == NULL)
114         msg_Dbg (vd, "XVideo extension not available");
115     else
116     if (r->major != 2)
117         msg_Dbg (vd, "XVideo extension v%"PRIu8".%"PRIu8" unknown",
118                  r->major, r->minor);
119     else
120     if (r->minor < 2)
121         msg_Dbg (vd, "XVideo extension v%"PRIu8".%"PRIu8" too old",
122                  r->major, r->minor);
123     else
124     {
125         msg_Dbg (vd, "using XVideo extension v%"PRIu8".%"PRIu8,
126                  r->major, r->minor);
127         ok = true;
128     }
129     free (r);
130     return ok;
131 }
132
133 static vlc_fourcc_t ParseFormat (vout_display_t *vd,
134                                  const xcb_xv_image_format_info_t *restrict f)
135 {
136     if (f->byte_order != ORDER && f->bpp != 8)
137         return 0; /* Argh! */
138
139     switch (f->type)
140     {
141       case XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB:
142         switch (f->num_planes)
143         {
144           case 1:
145             switch (f->bpp)
146             {
147               case 32:
148                 if (f->depth == 24)
149                     return VLC_CODEC_RGB32;
150                 break;
151               case 24:
152                 if (f->depth == 24)
153                     return VLC_CODEC_RGB24;
154                 break;
155               case 16:
156                 if (f->depth == 16)
157                     return VLC_CODEC_RGB16;
158                 if (f->depth == 15)
159                     return VLC_CODEC_RGB15;
160                 break;
161               case 8:
162                 if (f->depth == 8)
163                     return VLC_CODEC_RGB8;
164                 break;
165             }
166             break;
167         }
168         msg_Err (vd, "unknown XVideo RGB format %"PRIx32" (%.4s)",
169                  f->id, f->guid);
170         msg_Dbg (vd, " %"PRIu8" planes, %"PRIu8" bits/pixel, "
171                  "depth %"PRIu8, f->num_planes, f->bpp, f->depth);
172         break;
173
174       case XCB_XV_IMAGE_FORMAT_INFO_TYPE_YUV:
175         if (f->u_sample_bits != f->v_sample_bits
176          || f->vhorz_u_period != f->vhorz_v_period
177          || f->vvert_u_period != f->vvert_v_period
178          || f->y_sample_bits != 8 || f->u_sample_bits != 8
179          || f->vhorz_y_period != 1 || f->vvert_y_period != 1)
180             goto bad;
181         switch (f->num_planes)
182         {
183           case 1:
184             switch (f->bpp)
185             {
186               /*untested: case 24:
187                 if (f->vhorz_u_period == 1 && f->vvert_u_period == 1)
188                     return VLC_CODEC_I444;
189                 break;*/
190               case 16:
191                 if (f->vhorz_u_period == 2 && f->vvert_u_period == 1)
192                 {
193                     if (!strcmp ((const char *)f->vcomp_order, "YUYV"))
194                         return VLC_CODEC_YUYV;
195                     if (!strcmp ((const char *)f->vcomp_order, "UYVY"))
196                         return VLC_CODEC_UYVY;
197                 }
198                 break;
199             }
200             break;
201           case 3:
202             switch (f->bpp)
203             {
204               case 12:
205                 if (f->vhorz_u_period == 2 && f->vvert_u_period == 2)
206                 {
207                     if (!strcmp ((const char *)f->vcomp_order, "YVU"))
208                         return VLC_CODEC_YV12;
209                     if (!strcmp ((const char *)f->vcomp_order, "YUV"))
210                         return VLC_CODEC_I420;
211                 }
212             }
213             break;
214         }
215     bad:
216         msg_Err (vd, "unknown XVideo YUV format %"PRIx32" (%.4s)", f->id,
217                  f->guid);
218         msg_Dbg (vd, " %"PRIu8" planes, %"PRIu32" bits/pixel, "
219                  "%"PRIu32"/%"PRIu32"/%"PRIu32" bits/sample", f->num_planes,
220                  f->bpp, f->y_sample_bits, f->u_sample_bits, f->v_sample_bits);
221         msg_Dbg (vd, " period: %"PRIu32"/%"PRIu32"/%"PRIu32"x"
222                  "%"PRIu32"/%"PRIu32"/%"PRIu32,
223                  f->vhorz_y_period, f->vhorz_u_period, f->vhorz_v_period,
224                  f->vvert_y_period, f->vvert_u_period, f->vvert_v_period);
225         msg_Warn (vd, " order: %.32s", f->vcomp_order);
226         break;
227     }
228     return 0;
229 }
230
231
232 static const xcb_xv_image_format_info_t *
233 FindFormat (vout_display_t *vd,
234             vlc_fourcc_t chroma, const video_format_t *fmt,
235             xcb_xv_port_t port,
236             const xcb_xv_list_image_formats_reply_t *list,
237             xcb_xv_query_image_attributes_reply_t **restrict pa)
238 {
239     xcb_connection_t *conn = vd->sys->conn;
240     const xcb_xv_image_format_info_t *f, *end;
241
242 #ifndef XCB_XV_OLD
243     f = xcb_xv_list_image_formats_format (list);
244 #else
245     f = (xcb_xv_image_format_info_t *) (list + 1);
246 #endif
247     end = f + xcb_xv_list_image_formats_format_length (list);
248     for (; f < end; f++)
249     {
250         if (chroma != ParseFormat (vd, f))
251             continue;
252
253         /* VLC pads scanline to 16 pixels internally */
254         unsigned width = (fmt->i_width + 15) & ~15;
255         unsigned height = (fmt->i_height + 15) & ~15;
256         xcb_xv_query_image_attributes_reply_t *i;
257         i = xcb_xv_query_image_attributes_reply (conn,
258             xcb_xv_query_image_attributes (conn, port, f->id,
259                                            width, height), NULL);
260         if (i == NULL)
261             continue;
262
263         if (i->width != width || i->height != height)
264         {
265             msg_Warn (vd, "incompatible size %ux%u -> %"PRIu32"x%"PRIu32,
266                       fmt->i_width, fmt->i_height,
267                       i->width, i->height);
268             var_Create (vd->p_libvlc, "xvideo-resolution-error", VLC_VAR_BOOL);
269             if (!var_GetBool (vd->p_libvlc, "xvideo-resolution-error"))
270             {
271                 dialog_FatalWait (vd, _("Video acceleration not available"),
272                     _("Your video output acceleration driver does not support "
273                       "the required resolution: %ux%u pixels. The maximum "
274                       "supported resolution is %"PRIu32"x%"PRIu32".\n"
275                       "Video output acceleration will be disabled. However, "
276                       "rendering videos with overly large resolution "
277                       "may cause severe performance degration."),
278                                   width, height, i->width, i->height);
279                 var_SetBool (vd->p_libvlc, "xvideo-resolution-error", true);
280             }
281             free (i);
282             continue;
283         }
284         *pa = i;
285         return f;
286     }
287     return NULL;
288 }
289
290
291 /**
292  * Probe the X server.
293  */
294 static int Open (vlc_object_t *obj)
295 {
296     vout_display_t *vd = (vout_display_t *)obj;
297     vout_display_sys_t *p_sys = malloc (sizeof (*p_sys));
298     if (p_sys == NULL)
299         return VLC_ENOMEM;
300
301     vd->sys = p_sys;
302
303     /* Connect to X */
304     xcb_connection_t *conn;
305     const xcb_screen_t *screen;
306     uint8_t depth;
307     p_sys->embed = GetWindow (vd, &conn, &screen, &depth);
308     if (p_sys->embed == NULL)
309     {
310         free (p_sys);
311         return VLC_EGENERIC;
312     }
313
314     p_sys->conn = conn;
315     p_sys->att = NULL;
316     p_sys->pool = NULL;
317
318     if (!CheckXVideo (vd, conn))
319     {
320         msg_Warn (vd, "Please enable XVideo 2.2 for faster video display");
321         goto error;
322     }
323
324     p_sys->window = xcb_generate_id (conn);
325
326     /* Cache adaptors infos */
327     xcb_xv_query_adaptors_reply_t *adaptors =
328         xcb_xv_query_adaptors_reply (conn,
329             xcb_xv_query_adaptors (conn, p_sys->embed->xid), NULL);
330     if (adaptors == NULL)
331         goto error;
332
333     int forced_adaptor = var_CreateGetInteger (obj, "xvideo-adaptor");
334
335     /* */
336     video_format_t fmt = vd->fmt;
337     bool found_adaptor = false;
338
339     xcb_xv_adaptor_info_iterator_t it;
340     for (it = xcb_xv_query_adaptors_info_iterator (adaptors);
341          it.rem > 0 && !found_adaptor;
342          xcb_xv_adaptor_info_next (&it))
343     {
344         const xcb_xv_adaptor_info_t *a = it.data;
345         char *name;
346
347         if (forced_adaptor != -1 && forced_adaptor != 0)
348         {
349             forced_adaptor--;
350             continue;
351         }
352
353         if (!(a->type & XCB_XV_TYPE_IMAGE_MASK))
354             continue;
355
356         xcb_xv_list_image_formats_reply_t *r =
357             xcb_xv_list_image_formats_reply (conn,
358                 xcb_xv_list_image_formats (conn, a->base_id), NULL);
359         if (r == NULL)
360             continue;
361
362         /* Look for an image format */
363         const xcb_xv_image_format_info_t *xfmt = NULL;
364         const vlc_fourcc_t *chromas, chromas_default[] = {
365             fmt.i_chroma,
366             VLC_CODEC_RGB32,
367             VLC_CODEC_RGB24,
368             VLC_CODEC_RGB16,
369             VLC_CODEC_RGB15,
370             VLC_CODEC_YUYV,
371             0
372         };
373         if (vlc_fourcc_IsYUV (fmt.i_chroma))
374             chromas = vlc_fourcc_GetYUVFallback (fmt.i_chroma);
375         else
376             chromas = chromas_default;
377
378         vlc_fourcc_t chroma;
379         for (size_t i = 0; chromas[i]; i++)
380         {
381             chroma = chromas[i];
382
383             /* Oink oink! */
384             if ((chroma == VLC_CODEC_I420 || chroma == VLC_CODEC_YV12)
385              && a->name_size >= 4
386              && !memcmp ("OMAP", xcb_xv_adaptor_info_name (a), 4))
387             {
388                 msg_Dbg (vd, "skipping slow I420 format");
389                 continue; /* OMAP framebuffer sucks at YUV 4:2:0 */
390             }
391
392             xfmt = FindFormat (vd, chroma, &fmt, a->base_id, r, &p_sys->att);
393             if (xfmt != NULL)
394             {
395                 p_sys->id = xfmt->id;
396                 fmt.i_chroma = chroma;
397                 if (xfmt->type == XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB)
398                 {
399                     fmt.i_rmask = xfmt->red_mask;
400                     fmt.i_gmask = xfmt->green_mask;
401                     fmt.i_bmask = xfmt->blue_mask;
402                 }
403                 break;
404             }
405         }
406         free (r);
407         if (xfmt == NULL) /* No acceptable image formats */
408             continue;
409
410         /* Grab a port */
411         for (unsigned i = 0; i < a->num_ports; i++)
412         {
413              xcb_xv_port_t port = a->base_id + i;
414              xcb_xv_grab_port_reply_t *gr =
415                  xcb_xv_grab_port_reply (conn,
416                      xcb_xv_grab_port (conn, port, XCB_CURRENT_TIME), NULL);
417              uint8_t result = gr ? gr->result : 0xff;
418
419              free (gr);
420              if (result == 0)
421              {
422                  p_sys->port = port;
423                  goto grabbed_port;
424              }
425              msg_Dbg (vd, "cannot grab port %"PRIu32, port);
426         }
427         continue; /* No usable port */
428
429     grabbed_port:
430         /* Found port - initialize selected format */
431         name = strndup (xcb_xv_adaptor_info_name (a), a->name_size);
432         if (name != NULL)
433         {
434             msg_Dbg (vd, "using adaptor %s", name);
435             free (name);
436         }
437         msg_Dbg (vd, "using port %"PRIu32, p_sys->port);
438         msg_Dbg (vd, "using image format 0x%"PRIx32, p_sys->id);
439
440         /* Look for an X11 visual, create a window */
441         xcb_xv_format_t *f = xcb_xv_adaptor_info_formats (a);
442         for (uint_fast16_t i = a->num_formats; i > 0; i--, f++)
443         {
444             if (f->depth != depth)
445                 continue; /* this would fail anyway */
446
447             const uint32_t mask =
448                 /* XCB_CW_EVENT_MASK */
449                 XCB_EVENT_MASK_VISIBILITY_CHANGE;
450             xcb_void_cookie_t c;
451
452             c = xcb_create_window_checked (conn, f->depth, p_sys->window,
453                  p_sys->embed->xid, 0, 0, 1, 1, 0,
454                  XCB_WINDOW_CLASS_INPUT_OUTPUT, f->visual,
455                  XCB_CW_EVENT_MASK, &mask);
456
457             if (!CheckError (vd, conn, "cannot create X11 window", c))
458             {
459                 msg_Dbg (vd, "using X11 visual ID 0x%"PRIx32
460                          " (depth: %"PRIu8")", f->visual, f->depth);
461                 msg_Dbg (vd, "using X11 window 0x%08"PRIx32, p_sys->window);
462                 goto created_window;
463             }
464         }
465         xcb_xv_ungrab_port (conn, p_sys->port, XCB_CURRENT_TIME);
466         continue; /* No workable XVideo format (visual/depth) */
467
468     created_window:
469         found_adaptor = true;
470         break;
471     }
472     free (adaptors);
473     if (!found_adaptor)
474     {
475         msg_Err (vd, "no available XVideo adaptor");
476         goto error;
477     }
478     else
479     {
480         xcb_map_window (conn, p_sys->window);
481
482         vout_display_place_t place;
483
484         vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
485         p_sys->width  = place.width;
486         p_sys->height = place.height;
487
488         /* */
489         const uint32_t values[] = {
490             place.x, place.y, place.width, place.height };
491         xcb_configure_window (conn, p_sys->window,
492                               XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
493                               XCB_CONFIG_WINDOW_WIDTH |
494                               XCB_CONFIG_WINDOW_HEIGHT,
495                               values);
496     }
497     p_sys->visible = false;
498
499     /* Create graphic context */
500     p_sys->gc = xcb_generate_id (conn);
501     xcb_create_gc (conn, p_sys->gc, p_sys->window, 0, NULL);
502     msg_Dbg (vd, "using X11 graphic context 0x%08"PRIx32, p_sys->gc);
503
504     /* Create cursor */
505     p_sys->cursor = CreateBlankCursor (conn, screen);
506
507     CheckSHM (obj, conn, &p_sys->shm);
508
509     /* */
510     vout_display_info_t info = vd->info;
511     info.has_pictures_invalid = false;
512
513     /* Setup vout_display_t once everything is fine */
514     vd->fmt = fmt;
515     vd->info = info;
516
517     vd->pool = Pool;
518     vd->prepare = NULL;
519     vd->display = Display;
520     vd->control = Control;
521     vd->manage = Manage;
522
523     /* */
524     vout_display_SendEventFullscreen (vd, false);
525     unsigned width, height;
526     if (!GetWindowSize (p_sys->embed, conn, &width, &height))
527         vout_display_SendEventDisplaySize (vd, width, height, false);
528
529     return VLC_SUCCESS;
530
531 error:
532     Close (obj);
533     return VLC_EGENERIC;
534 }
535
536
537 /**
538  * Disconnect from the X server.
539  */
540 static void Close (vlc_object_t *obj)
541 {
542     vout_display_t *vd = (vout_display_t *)obj;
543     vout_display_sys_t *p_sys = vd->sys;
544
545     if (p_sys->pool)
546     {
547         for (unsigned i = 0; i < MAX_PICTURES; i++)
548         {
549             picture_resource_t *res = &p_sys->resource[i];
550
551             if (!res->p->p_pixels)
552                 break;
553             PictureResourceFree (res, NULL);
554         }
555         picture_pool_Delete (p_sys->pool);
556     }
557
558     free (p_sys->att);
559     xcb_disconnect (p_sys->conn);
560     vout_display_DeleteWindow (vd, p_sys->embed);
561     free (p_sys);
562 }
563
564 /**
565  * Return a direct buffer
566  */
567 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
568 {
569     vout_display_sys_t *p_sys = vd->sys;
570     (void)requested_count;
571
572     if (!p_sys->pool)
573     {
574         picture_t *pic = picture_New (vd->fmt.i_chroma, p_sys->att->width,
575                                       p_sys->att->height, 0);
576         if (!pic)
577             return NULL;
578
579         memset (p_sys->resource, 0, sizeof(p_sys->resource));
580
581         const uint32_t *offsets =
582             xcb_xv_query_image_attributes_offsets (p_sys->att);
583         p_sys->data_size = p_sys->att->data_size;
584
585         unsigned count;
586         picture_t *pic_array[MAX_PICTURES];
587         for (count = 0; count < MAX_PICTURES; count++)
588         {
589             picture_resource_t *res = &p_sys->resource[count];
590
591             for (int i = 0; i < pic->i_planes; i++)
592             {
593                 res->p[i].i_lines = pic->p[i].i_lines; /* FIXME seems wrong*/
594                 res->p[i].i_pitch = pic->p[i].i_pitch;
595             }
596             if (PictureResourceAlloc (vd, res, p_sys->att->data_size,
597                                       p_sys->conn, p_sys->shm))
598                 break;
599
600             /* Allocate further planes as specified by XVideo */
601             /* We assume that offsets[0] is zero */
602             for (int i = 1; i < pic->i_planes; i++)
603                 res->p[i].p_pixels = res->p[0].p_pixels + offsets[i];
604             if (vd->fmt.i_chroma == VLC_CODEC_YV12)
605             {   /* YVU: swap U and V planes */
606                 uint8_t *buf = res->p[2].p_pixels;
607                 res->p[2].p_pixels = res->p[1].p_pixels;
608                 res->p[1].p_pixels = buf;
609             }
610
611             pic_array[count] = picture_NewFromResource (&vd->fmt, res);
612             if (!pic_array[count])
613             {
614                 PictureResourceFree (res, p_sys->conn);
615                 memset (res, 0, sizeof(*res));
616                 break;
617             }
618         }
619         picture_Release (pic);
620
621         if (count == 0)
622             return NULL;
623
624         p_sys->pool = picture_pool_New (count, pic_array);
625         /* TODO release picture resources if NULL */
626         xcb_flush (p_sys->conn);
627     }
628
629     return p_sys->pool;
630 }
631
632 /**
633  * Sends an image to the X server.
634  */
635 static void Display (vout_display_t *vd, picture_t *pic)
636 {
637     vout_display_sys_t *p_sys = vd->sys;
638     xcb_shm_seg_t segment = pic->p_sys->segment;
639     xcb_void_cookie_t ck;
640
641     if (!p_sys->visible)
642         goto out;
643     if (segment)
644         ck = xcb_xv_shm_put_image_checked (p_sys->conn, p_sys->port,
645                               p_sys->window, p_sys->gc, segment, p_sys->id, 0,
646                    /* Src: */ vd->source.i_x_offset,
647                               vd->source.i_y_offset,
648                               vd->source.i_visible_width,
649                               vd->source.i_visible_height,
650                    /* Dst: */ 0, 0, p_sys->width, p_sys->height,
651                 /* Memory: */ pic->p->i_pitch / pic->p->i_pixel_pitch,
652                               pic->p->i_lines, false);
653     else
654         ck = xcb_xv_put_image_checked (p_sys->conn, p_sys->port, p_sys->window,
655                           p_sys->gc, p_sys->id,
656                           vd->source.i_x_offset,
657                           vd->source.i_y_offset,
658                           vd->source.i_visible_width,
659                           vd->source.i_visible_height,
660                           0, 0, p_sys->width, p_sys->height,
661                           pic->p->i_pitch / pic->p->i_pixel_pitch,
662                           pic->p->i_lines,
663                           p_sys->data_size, pic->p->p_pixels);
664
665     /* Wait for reply. See x11.c for rationale. */
666     xcb_generic_error_t *e = xcb_request_check (p_sys->conn, ck);
667     if (e != NULL)
668     {
669         msg_Dbg (vd, "%s: X11 error %d", "cannot put image", e->error_code);
670         free (e);
671     }
672 out:
673     picture_Release (pic);
674 }
675
676 static int Control (vout_display_t *vd, int query, va_list ap)
677 {
678     vout_display_sys_t *p_sys = vd->sys;
679
680     switch (query)
681     {
682     case VOUT_DISPLAY_CHANGE_FULLSCREEN:
683     {
684         const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
685         return vout_window_SetFullScreen (p_sys->embed, c->is_fullscreen);
686     }
687
688     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
689     case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
690     case VOUT_DISPLAY_CHANGE_ZOOM:
691     case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
692     case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
693     {
694         const vout_display_cfg_t *cfg;
695         const video_format_t *source;
696         bool is_forced = false;
697
698         if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
699          || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
700         {
701             source = (const video_format_t *)va_arg (ap, const video_format_t *);
702             cfg = vd->cfg;
703         }
704         else
705         {
706             source = &vd->source;
707             cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
708             if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
709                 is_forced = (bool)va_arg (ap, int);
710         }
711
712         /* */
713         if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
714          && is_forced
715          && (cfg->display.width  != vd->cfg->display.width
716            ||cfg->display.height != vd->cfg->display.height)
717          && vout_window_SetSize (p_sys->embed,
718                                   cfg->display.width,
719                                   cfg->display.height))
720             return VLC_EGENERIC;
721
722         vout_display_place_t place;
723         vout_display_PlacePicture (&place, source, cfg, false);
724         p_sys->width  = place.width;
725         p_sys->height = place.height;
726
727         /* Move the picture within the window */
728         const uint32_t values[] = { place.x, place.y,
729                                     place.width, place.height, };
730         xcb_configure_window (p_sys->conn, p_sys->window,
731                               XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
732                             | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
733                               values);
734         xcb_flush (p_sys->conn);
735         return VLC_SUCCESS;
736     }
737     case VOUT_DISPLAY_CHANGE_ON_TOP:
738     {
739         int on_top = (int)va_arg (ap, int);
740         return vout_window_SetOnTop (p_sys->embed, on_top);
741     }
742
743     /* Hide the mouse. It will be send when
744      * vout_display_t::info.b_hide_mouse is false */
745     case VOUT_DISPLAY_HIDE_MOUSE:
746         xcb_change_window_attributes (p_sys->conn, p_sys->embed->xid,
747                                   XCB_CW_CURSOR, &(uint32_t){ p_sys->cursor });
748         return VLC_SUCCESS;
749     case VOUT_DISPLAY_RESET_PICTURES:
750         assert(0);
751     default:
752         msg_Err (vd, "Unknown request in XCB vout display");
753         return VLC_EGENERIC;
754     }
755 }
756
757 static void Manage (vout_display_t *vd)
758 {
759     vout_display_sys_t *p_sys = vd->sys;
760
761     ManageEvent (vd, p_sys->conn, &p_sys->visible);
762 }
763