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