3 * @brief X C Bindings video output module for VLC media player
5 /*****************************************************************************
6 * Copyright © 2009 Rémi Denis-Courmont
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.0
11 * of the License, or (at your option) any later version.
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.
18 * You should have received a copy of the GNU Lesser 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 ****************************************************************************/
30 #include <sys/types.h>
36 #include <xcb/xcb_aux.h>
37 #include <xcb/xcb_image.h>
39 #include <vlc_common.h>
40 #include <vlc_plugin.h>
42 #include <vlc_window.h>
46 #define DISPLAY_TEXT N_("X11 display")
47 #define DISPLAY_LONGTEXT N_( \
48 "X11 hardware display to use. By default VLC will " \
49 "use the value of the DISPLAY environment variable.")
51 #define SHM_TEXT N_("Use shared memory")
52 #define SHM_LONGTEXT N_( \
53 "Use shared memory to communicate between VLC and the X server.")
55 static int Open (vlc_object_t *);
56 static void Close (vlc_object_t *);
62 set_shortname (N_("XCB"))
63 set_description (N_("(Experimental) XCB video output"))
64 set_category (CAT_VIDEO)
65 set_subcategory (SUBCAT_VIDEO_VOUT)
66 set_capability ("video output", 0)
67 set_callbacks (Open, Close)
69 add_string ("x11-display", NULL, NULL,
70 DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
71 add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
76 xcb_connection_t *conn;
78 vout_window_t *embed; /* VLC window (when windowed) */
80 xcb_visualid_t vid; /* selected visual */
81 xcb_window_t parent; /* parent X window */
82 xcb_colormap_t cmap; /* colormap for selected visual */
83 xcb_window_t window; /* drawable X window */
84 xcb_gcontext_t gc; /* context to put images */
85 bool shm; /* whether to use MIT-SHM */
86 uint8_t bpp; /* bits per pixel */
87 uint8_t pad; /* scanline pad */
90 static int Init (vout_thread_t *);
91 static void Deinit (vout_thread_t *);
92 static void Render (vout_thread_t *, picture_t *);
93 static void Display (vout_thread_t *, picture_t *);
94 static int Manage (vout_thread_t *);
96 int CheckError (vout_thread_t *vout, const char *str, xcb_void_cookie_t ck)
98 xcb_generic_error_t *err;
100 err = xcb_request_check (vout->p_sys->conn, ck);
103 msg_Err (vout, "%s: X11 error %d", str, err->error_code);
112 * Probe the X server.
114 static int Open (vlc_object_t *obj)
116 vout_thread_t *vout = (vout_thread_t *)obj;
117 vout_sys_t *p_sys = malloc (sizeof (*p_sys));
128 char *display = var_CreateGetNonEmptyString (vout, "x11-display");
130 p_sys->conn = xcb_connect (display, &snum);
131 if (xcb_connection_has_error (p_sys->conn) /*== NULL*/)
133 msg_Err (vout, "cannot connect to X server %s",
134 display ? display : "");
140 const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
142 /* Get the preferred screen */
143 xcb_screen_t *scr = xcb_aux_get_screen (p_sys->conn, snum);
145 assert (p_sys->screen);
146 msg_Dbg (vout, "using screen %d (depth %"PRIu8")", snum, scr->root_depth);
148 /* Determine the visual (color depth and palette) */
149 xcb_visualtype_t *vt = NULL;
152 if ((vt = xcb_aux_find_visual_by_attrs (scr, XCB_VISUAL_CLASS_TRUE_COLOR,
153 scr->root_depth)) != NULL)
154 msg_Dbg (vout, "using TrueColor visual ID %d", (int)vt->visual_id);
157 if ((vt = xcb_aux_find_visual_by_attrs (scr, XCB_VISUAL_CLASS_STATIC_COLOR,
158 scr->root_depth)) != NULL)
159 msg_Dbg (vout, "using static color visual ID %d", (int)vt->visual_id);
162 if ((scr->root_depth == 8)
163 && (vt = xcb_aux_find_visual_by_attrs (scr, XCB_VISUAL_CLASS_STATIC_GRAY,
164 scr->root_depth)) != NULL)
166 msg_Dbg (vout, "using static gray visual ID %d", (int)vt->visual_id);
171 msg_Err (vout, "no supported visual class");
174 p_sys->vid = vt->visual_id;
176 /* Determine our input format (normally, done in Init() but X11
177 * never changes its format) */
178 vout->output.i_chroma = 0;
179 for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
180 *end = fmt + xcb_setup_pixmap_formats_length (setup);
183 if (fmt->depth != scr->root_depth)
189 if (fmt->bits_per_pixel == 32)
190 vout->output.i_chroma = VLC_FOURCC ('R', 'V', '3', '2');
191 else if (fmt->bits_per_pixel == 24)
192 vout->output.i_chroma = VLC_FOURCC ('R', 'V', '2', '4');
197 if (fmt->bits_per_pixel != 16)
199 vout->output.i_chroma = VLC_FOURCC ('R', 'V', '1', '6');
202 if (fmt->bits_per_pixel != 16)
204 vout->output.i_chroma = VLC_FOURCC ('R', 'V', '1', '5');
207 if (fmt->bits_per_pixel != 8)
209 vout->output.i_chroma = gray ? VLC_FOURCC ('G', 'R', 'E', 'Y')
210 : VLC_FOURCC ('R', 'G', 'B', '2');
213 if ((fmt->bits_per_pixel << 4) % fmt->scanline_pad)
214 continue; /* VLC pads lines to 16 pixels internally */
215 p_sys->bpp = fmt->bits_per_pixel;
216 p_sys->pad = fmt->scanline_pad;
217 msg_Dbg (vout, "using %"PRIu8" bits per pixels (line pad: %"PRIu8")",
218 p_sys->bpp, p_sys->pad);
222 if (!vout->output.i_chroma)
224 msg_Err (vout, "no supported pixmap formats");
228 vout->fmt_out.i_chroma = vout->output.i_chroma;
231 vout->output.i_rmask = vt->red_mask;
232 vout->output.i_gmask = vt->green_mask;
233 vout->output.i_bmask = vt->blue_mask;
236 /* Create colormap (needed to select non-default visual) */
237 p_sys->cmap = xcb_generate_id (p_sys->conn);
238 xcb_create_colormap (p_sys->conn, XCB_COLORMAP_ALLOC_NONE,
239 p_sys->cmap, scr->root, p_sys->vid);
241 /* Check shared memory support */
242 p_sys->shm = var_CreateGetBool (vout, "x11-shm") > 0;
245 xcb_shm_query_version_cookie_t ck;
246 xcb_shm_query_version_reply_t *r;
247 xcb_generic_error_t *err;
249 ck = xcb_shm_query_version (p_sys->conn);
250 r = xcb_shm_query_version_reply (p_sys->conn, ck, &err);
253 msg_Err (vout, "shared memory (MIT-SHM) not available");
254 msg_Warn (vout, "display will be slow");
260 /* FIXME: WTH to put as initial width/height values??? */
261 p_sys->embed = vout_RequestXWindow (vout, &(int){ 0 }, &(int){ 0 },
262 &(unsigned){ 0 }, &(unsigned){ 0 });
263 if (p_sys->embed == NULL)
265 msg_Err (vout, "parent window not available");
269 vout->pf_init = Init;
270 vout->pf_end = Deinit;
271 vout->pf_render = Render;
272 vout->pf_display = Display;
273 vout->pf_manage = Manage;
283 * Disconnect from the X server.
285 static void Close (vlc_object_t *obj)
287 vout_thread_t *vout = (vout_thread_t *)obj;
288 vout_sys_t *p_sys = vout->p_sys;
291 vout_ReleaseWindow (p_sys->embed);
292 /* colormap is garbage-ollected by X (?) */
294 xcb_disconnect (p_sys->conn);
300 xcb_connection_t *conn; /* Shared connection to X server */
301 xcb_image_t *image; /* Picture buffer */
302 xcb_image_t *native; /* Rendered picture buffer (in X server format) */
303 xcb_shm_seg_t segment; /* Shared memory segment X ID */
306 #define SHM_ERR ((void *)(intptr_t)(-1))
308 static int PictureInit (vout_thread_t *vout, picture_t *pic)
310 vout_sys_t *p_sys = vout->p_sys;
311 picture_sys_t *priv = malloc (sizeof (*p_sys));
316 assert (pic->i_status == FREE_PICTURE);
317 vout_InitPicture (vout, pic, vout->output.i_chroma,
318 vout->output.i_width, vout->output.i_height,
319 vout->output.i_aspect);
322 const size_t size = pic->p->i_pitch * pic->p->i_lines;
325 { /* Allocate shared memory segment */
326 int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0700);
330 msg_Err (vout, "shared memory allocation error: %m");
334 /* Attach the segment to VLC */
335 shm = shmat (id, NULL, 0 /* read/write */);
338 msg_Err (vout, "shared memory attachment error: %m");
339 shmctl (id, IPC_RMID, 0);
343 /* Attach the segment to X */
344 xcb_void_cookie_t ck;
346 priv->segment = xcb_generate_id (p_sys->conn);
347 ck = xcb_shm_attach_checked (p_sys->conn, priv->segment, id, 1);
348 shmctl (id, IPC_RMID, 0);
350 if (CheckError (vout, "shared memory server-side error", ck))
352 msg_Info (vout, "using buggy X (remote) server? SSH?");
360 const unsigned real_width = pic->p->i_pitch / (p_sys->bpp >> 3);
361 /* FIXME: anyway to getthing more intuitive than that?? */
364 img = xcb_image_create (real_width, pic->p->i_lines,
365 XCB_IMAGE_FORMAT_Z_PIXMAP, p_sys->pad,
366 p_sys->screen->root_depth, p_sys->bpp, p_sys->bpp,
367 #ifdef WORDS_BIGENDIAN
368 XCB_IMAGE_ORDER_MSB_FIRST,
370 XCB_IMAGE_ORDER_LSB_FIRST,
372 XCB_IMAGE_ORDER_MSB_FIRST,
374 (shm != SHM_ERR) ? size : 0,
375 (shm != SHM_ERR) ? shm : NULL);
380 xcb_shm_detach (p_sys->conn, priv->segment);
383 if (shm != SHM_ERR && xcb_image_native (p_sys->conn, img, 0) == NULL)
384 msg_Warn (vout, "incompatible X server image format");
386 priv->conn = p_sys->conn;
390 pic->p->p_pixels = img->data;
391 pic->i_status = DESTROYED_PICTURE;
392 pic->i_type = DIRECT_PICTURE;
404 * Release picture private data
406 static void PictureDeinit (picture_t *pic)
408 struct picture_sys_t *p_sys = pic->p_sys;
410 if (p_sys->segment != 0)
412 xcb_shm_detach (p_sys->conn, p_sys->segment);
413 shmdt (p_sys->image->data);
415 if ((p_sys->native != NULL) && (p_sys->native != p_sys->image))
416 xcb_image_destroy (p_sys->native);
417 xcb_image_destroy (p_sys->image);
422 * Allocate drawable window and picture buffers.
424 static int Init (vout_thread_t *vout)
426 vout_sys_t *p_sys = vout->p_sys;
427 const xcb_screen_t *screen = p_sys->screen;
428 unsigned x, y, width, height;
430 I_OUTPUTPICTURES = 0;
432 /* Determine parent window and size */
433 if (vout->b_fullscreen)
435 p_sys->parent = screen->root;
436 width = screen->width_in_pixels;
437 height = screen->height_in_pixels;
441 p_sys->parent = p_sys->embed->handle.xid;
443 /* Subscribe to parent window resize events */
444 const uint32_t value = XCB_EVENT_MASK_STRUCTURE_NOTIFY;
445 xcb_change_window_attributes (p_sys->conn, p_sys->parent,
446 XCB_CW_EVENT_MASK, &value);
448 xcb_get_geometry_cookie_t ck;
449 ck = xcb_get_geometry (p_sys->conn, p_sys->parent);
451 xcb_get_geometry_reply_t *geo;
452 xcb_generic_error_t *err;
453 geo = xcb_get_geometry_reply (p_sys->conn, ck, &err);
455 height = geo->height;
459 vout_PlacePicture (vout, width, height, &x, &y, &width, &height);
461 /* FIXME: I don't get the subtlety between output and fmt_out here */
462 vout->fmt_out.i_visible_width = width;
463 vout->fmt_out.i_visible_height = height;
464 vout->fmt_out.i_sar_num = vout->fmt_out.i_sar_den = 1;
466 vout->output.i_width = vout->fmt_out.i_width =
467 width * vout->fmt_in.i_width / vout->fmt_in.i_visible_width;
468 vout->output.i_height = vout->fmt_out.i_height =
469 height * vout->fmt_in.i_height / vout->fmt_in.i_visible_height;
470 vout->fmt_out.i_x_offset =
471 width * vout->fmt_in.i_x_offset / vout->fmt_in.i_visible_width;
472 p_vout->fmt_out.i_y_offset =
473 height * vout->fmt_in.i_y_offset / vout->fmt_in.i_visible_height;
476 vout->output.i_aspect = vout->fmt_out.i_aspect =
477 width * VOUT_ASPECT_FACTOR / height;
480 const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK
482 const uint32_t values[] = {
483 /* XCB_CW_BACK_PIXEL */
485 /* XCB_CW_EVENT_MASK */
486 XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
487 XCB_EVENT_MASK_POINTER_MOTION,
488 /* XCB_CW_COLORMAP */
492 xcb_window_t window = xcb_generate_id (p_sys->conn);
494 c = xcb_create_window_checked (p_sys->conn, screen->root_depth, window,
495 p_sys->parent, x, y, width, height, 0,
496 XCB_WINDOW_CLASS_INPUT_OUTPUT,
497 p_sys->vid, mask, values);
498 if (CheckError (vout, "cannot create X11 window", c))
500 p_sys->window = window;
501 msg_Dbg (vout, "using X11 window %08"PRIx32, p_sys->window);
502 xcb_map_window (p_sys->conn, window);
504 /* Create graphic context (I wonder why the heck do we need this) */
505 p_sys->gc = xcb_generate_id (p_sys->conn);
506 xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
507 msg_Dbg (vout, "using X11 graphic context %08"PRIx32, p_sys->gc);
508 xcb_flush (p_sys->conn);
510 /* Allocate picture buffers */
511 I_OUTPUTPICTURES = 0;
512 for (size_t index = 0; I_OUTPUTPICTURES < 2; index++)
514 picture_t *pic = vout->p_picture + index;
516 if (index > sizeof (vout->p_picture) / sizeof (pic))
518 if (pic->i_status != FREE_PICTURE)
520 if (PictureInit (vout, pic))
522 PP_OUTPUTPICTURE[I_OUTPUTPICTURES++] = pic;
533 * Free picture buffers.
535 static void Deinit (vout_thread_t *vout)
537 vout_sys_t *p_sys = vout->p_sys;
539 for (int i = 0; i < I_OUTPUTPICTURES; i++)
540 PictureDeinit (PP_OUTPUTPICTURE[i]);
542 xcb_unmap_window (p_sys->conn, p_sys->window);
543 xcb_destroy_window (p_sys->conn, p_sys->window);
547 * Prepares an image ahead of display.
549 static void Render (vout_thread_t *vout, picture_t *pic)
551 vout_sys_t *p_sys = vout->p_sys;
552 picture_sys_t *priv = pic->p_sys;
554 if ((priv->native != NULL) && (priv->native != priv->image))
555 xcb_image_destroy (priv->native);
556 priv->native = xcb_image_native (p_sys->conn, priv->image, 1);
561 * Sends an image to the X server.
563 static void Display (vout_thread_t *vout, picture_t *pic)
565 vout_sys_t *p_sys = vout->p_sys;
566 picture_sys_t *priv = pic->p_sys;
567 xcb_image_t *img = priv->image, *native = priv->native;
569 if ((native == img) && (img->base == NULL))
571 xcb_shm_segment_info_t info = {
572 .shmseg = priv->segment,
573 .shmid = -1, /* lost track of it, unimportant */
574 .shmaddr = img->data,
577 xcb_image_shm_put (p_sys->conn, p_sys->window, p_sys->gc, img, info,
578 0, 0, 0, 0, img->width, img->height, 0);
582 xcb_image_put (p_sys->conn, p_sys->window, p_sys->gc, native, 0, 0, 0);
583 xcb_flush (p_sys->conn);
587 * Process incoming X events.
589 static int Manage (vout_thread_t *vout)
591 vout_sys_t *p_sys = vout->p_sys;
592 xcb_generic_event_t *ev;
594 while ((ev = xcb_poll_for_event (p_sys->conn)) != NULL)
595 ProcessEvent (vout, p_sys->conn, p_sys->window, ev);
597 if (xcb_connection_has_error (p_sys->conn))
599 msg_Err (vout, "X server failure");