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 <vlc_common.h>
37 #include <vlc_plugin.h>
39 #include <vlc_window.h>
43 #define DISPLAY_TEXT N_("X11 display")
44 #define DISPLAY_LONGTEXT N_( \
45 "X11 hardware display to use. By default VLC will " \
46 "use the value of the DISPLAY environment variable.")
48 #define SHM_TEXT N_("Use shared memory")
49 #define SHM_LONGTEXT N_( \
50 "Use shared memory to communicate between VLC and the X server.")
52 static int Open (vlc_object_t *);
53 static void Close (vlc_object_t *);
59 set_shortname (N_("XCB"))
60 set_description (N_("(Experimental) XCB video output"))
61 set_category (CAT_VIDEO)
62 set_subcategory (SUBCAT_VIDEO_VOUT)
63 set_capability ("video output", 0)
64 set_callbacks (Open, Close)
66 add_string ("x11-display", NULL, NULL,
67 DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
68 add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
73 xcb_connection_t *conn;
74 vout_window_t *embed; /* VLC window (when windowed) */
76 xcb_window_t window; /* drawable X window */
77 xcb_gcontext_t gc; /* context to put images */
78 bool shm; /* whether to use MIT-SHM */
79 uint8_t bpp; /* bits per pixel */
80 uint8_t pad; /* scanline pad */
81 uint8_t depth; /* useful bits per pixel */
82 uint8_t byte_order; /* server byte order */
85 static int Init (vout_thread_t *);
86 static void Deinit (vout_thread_t *);
87 static void Display (vout_thread_t *, picture_t *);
88 static int Manage (vout_thread_t *);
90 int CheckError (vout_thread_t *vout, const char *str, xcb_void_cookie_t ck)
92 xcb_generic_error_t *err;
94 err = xcb_request_check (vout->p_sys->conn, ck);
97 msg_Err (vout, "%s: X11 error %d", str, err->error_code);
106 * Probe the X server.
108 static int Open (vlc_object_t *obj)
110 vout_thread_t *vout = (vout_thread_t *)obj;
111 vout_sys_t *p_sys = malloc (sizeof (*p_sys));
120 char *display = var_CreateGetNonEmptyString (vout, "x11-display");
121 p_sys->conn = xcb_connect (display, NULL);
122 if (xcb_connection_has_error (p_sys->conn) /*== NULL*/)
124 msg_Err (vout, "cannot connect to X server %s",
125 display ? display : "");
132 p_sys->embed = vout_RequestXWindow (vout, &(int){ 0 }, &(int){ 0 },
133 &(unsigned){ 0 }, &(unsigned){ 0 });
134 if (p_sys->embed == NULL)
136 msg_Err (vout, "parent window not available");
141 xcb_get_geometry_reply_t *geo;
142 xcb_get_geometry_cookie_t ck;
144 ck = xcb_get_geometry (p_sys->conn, p_sys->embed->handle.xid);
145 geo = xcb_get_geometry_reply (p_sys->conn, ck, NULL);
148 msg_Err (vout, "parent window not valid");
154 /* Subscribe to parent window resize events */
155 const uint32_t value = XCB_EVENT_MASK_STRUCTURE_NOTIFY;
156 xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid,
157 XCB_CW_EVENT_MASK, &value);
160 /* Find the selected screen */
161 const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
162 p_sys->byte_order = setup->image_byte_order;
164 xcb_screen_t *scr = NULL;
165 for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
166 i.rem > 0 && scr == NULL; xcb_screen_next (&i))
168 if (i.data->root == root)
174 msg_Err (vout, "parent window screen not found");
177 msg_Dbg (vout, "using screen 0x%"PRIx32, scr->root);
179 /* Determine our video format. Normally, this is done in pf_init(), but
180 * this plugin always uses the same format for a given X11 screen. */
181 xcb_visualid_t vid = 0;
184 for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
185 *end = fmt + xcb_setup_pixmap_formats_length (setup);
188 vlc_fourcc_t chroma = 0;
190 if (fmt->depth < depth)
191 continue; /* We already found a better format! */
193 /* Check that the pixmap format is supported by VLC. */
197 if (fmt->bits_per_pixel == 32)
198 chroma = VLC_FOURCC ('R', 'V', '3', '2');
199 else if (fmt->bits_per_pixel == 24)
200 chroma = VLC_FOURCC ('R', 'V', '2', '4');
205 if (fmt->bits_per_pixel != 16)
207 chroma = VLC_FOURCC ('R', 'V', '1', '6');
210 if (fmt->bits_per_pixel != 16)
212 chroma = VLC_FOURCC ('R', 'V', '1', '5');
215 if (fmt->bits_per_pixel != 8)
217 chroma = VLC_FOURCC ('R', 'G', 'B', '2');
222 if ((fmt->bits_per_pixel << 4) % fmt->scanline_pad)
223 continue; /* VLC pads lines to 16 pixels internally */
225 /* Byte sex is a non-issue for 8-bits. It can be worked around with
226 * RGB masks for 24-bits. Too bad for 15-bits and 16-bits. */
227 #ifdef WORDS_BIGENDIAN
228 # define ORDER XCB_IMAGE_ORDER_MSB_FIRST
230 # define ORDER XCB_IMAGE_ORDER_LSB_FIRST
232 if (fmt->bits_per_pixel == 16 && setup->image_byte_order != ORDER)
235 /* Check that the selected screen supports this depth */
236 xcb_depth_iterator_t it = xcb_screen_allowed_depths_iterator (scr);
237 while (it.rem > 0 && it.data->depth != fmt->depth)
238 xcb_depth_next (&it);
240 continue; /* Depth not supported on this screen */
242 /* Find a visual type for the selected depth */
243 const xcb_visualtype_t *vt = xcb_depth_visuals (it.data);
244 for (int i = xcb_depth_visuals_length (it.data); i > 0; i--)
246 if (vt->_class == XCB_VISUAL_CLASS_TRUE_COLOR)
252 if (fmt->depth == 8 && vt->_class == XCB_VISUAL_CLASS_STATIC_GRAY)
255 continue; /* Prefer color over gray scale */
257 chroma = VLC_FOURCC ('G', 'R', 'E', 'Y');
262 continue; /* The screen does not *really* support this depth */
264 vout->fmt_out.i_chroma = vout->output.i_chroma = chroma;
267 vout->output.i_rmask = vt->red_mask;
268 vout->output.i_gmask = vt->green_mask;
269 vout->output.i_bmask = vt->blue_mask;
271 p_sys->bpp = fmt->bits_per_pixel;
272 p_sys->pad = fmt->scanline_pad;
273 p_sys->depth = depth = fmt->depth;
278 msg_Err (vout, "no supported pixmap formats or visual types");
282 msg_Dbg (vout, "using X11 visual ID 0x%"PRIx32, vid);
283 msg_Dbg (vout, " %"PRIu8" bits per pixels, %"PRIu8" bits line pad",
284 p_sys->bpp, p_sys->pad);
286 /* Create colormap (needed to select non-default visual) */
288 if (vid != scr->root_visual)
290 cmap = xcb_generate_id (p_sys->conn);
291 xcb_create_colormap (p_sys->conn, XCB_COLORMAP_ALLOC_NONE,
292 cmap, scr->root, vid);
295 cmap = scr->default_colormap;
299 const uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_COLORMAP;
300 const uint32_t values[] = {
301 /* XCB_CW_EVENT_MASK */
302 XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
303 XCB_EVENT_MASK_POINTER_MOTION,
304 /* XCB_CW_COLORMAP */
308 xcb_window_t window = xcb_generate_id (p_sys->conn);
310 c = xcb_create_window_checked (p_sys->conn, depth, window,
311 p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
312 XCB_WINDOW_CLASS_INPUT_OUTPUT,
314 if (CheckError (vout, "cannot create X11 window", c))
316 p_sys->window = window;
317 msg_Dbg (vout, "using X11 window %08"PRIx32, p_sys->window);
318 xcb_map_window (p_sys->conn, window);
321 /* Create graphic context (I wonder why the heck do we need this) */
322 p_sys->gc = xcb_generate_id (p_sys->conn);
323 xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
324 msg_Dbg (vout, "using X11 graphic context %08"PRIx32, p_sys->gc);
326 /* Check shared memory support */
327 p_sys->shm = var_CreateGetBool (vout, "x11-shm") > 0;
330 xcb_shm_query_version_cookie_t ck;
331 xcb_shm_query_version_reply_t *r;
333 ck = xcb_shm_query_version (p_sys->conn);
334 r = xcb_shm_query_version_reply (p_sys->conn, ck, NULL);
337 msg_Err (vout, "shared memory (MIT-SHM) not available");
338 msg_Warn (vout, "display will be slow");
344 vout->pf_init = Init;
345 vout->pf_end = Deinit;
346 vout->pf_display = Display;
347 vout->pf_manage = Manage;
357 * Disconnect from the X server.
359 static void Close (vlc_object_t *obj)
361 vout_thread_t *vout = (vout_thread_t *)obj;
362 vout_sys_t *p_sys = vout->p_sys;
365 vout_ReleaseWindow (p_sys->embed);
366 /* colormap and window are garbage-collected by X */
368 xcb_disconnect (p_sys->conn);
374 xcb_connection_t *conn; /* Shared connection to X server */
375 xcb_shm_seg_t segment; /* Shared memory segment X ID */
378 #define SHM_ERR ((void *)(intptr_t)(-1))
380 static int PictureInit (vout_thread_t *vout, picture_t *pic)
382 vout_sys_t *p_sys = vout->p_sys;
383 picture_sys_t *priv = malloc (sizeof (*p_sys));
388 assert (pic->i_status == FREE_PICTURE);
389 vout_InitPicture (vout, pic, vout->output.i_chroma,
390 vout->output.i_width, vout->output.i_height,
391 vout->output.i_aspect);
394 const size_t size = pic->p->i_pitch * pic->p->i_lines;
396 /* Allocate shared memory segment */
397 int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0700);
400 msg_Err (vout, "shared memory allocation error: %m");
404 /* Attach the segment to VLC */
405 shm = shmat (id, NULL, 0 /* read/write */);
408 msg_Err (vout, "shared memory attachment error: %m");
409 shmctl (id, IPC_RMID, 0);
415 /* Attach the segment to X */
416 xcb_void_cookie_t ck;
417 priv->segment = xcb_generate_id (p_sys->conn);
418 ck = xcb_shm_attach_checked (p_sys->conn, priv->segment, id, 1);
420 if (CheckError (vout, "shared memory server-side error", ck))
422 msg_Info (vout, "using buggy X11 server - SSH proxying?");
429 shmctl (id, IPC_RMID, 0);
430 priv->conn = p_sys->conn;
432 pic->p->p_pixels = shm;
433 pic->i_status = DESTROYED_PICTURE;
434 pic->i_type = DIRECT_PICTURE;
444 * Release picture private data
446 static void PictureDeinit (picture_t *pic)
448 struct picture_sys_t *p_sys = pic->p_sys;
450 if (p_sys->segment != 0)
451 xcb_shm_detach (p_sys->conn, p_sys->segment);
452 shmdt (pic->p->p_pixels);
456 static void get_window_size (xcb_connection_t *conn, xcb_window_t win,
457 unsigned *width, unsigned *height)
459 xcb_get_geometry_cookie_t ck = xcb_get_geometry (conn, win);
460 xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply (conn, ck, NULL);
465 *height = geo->height;
469 *width = *height = 0;
473 * Allocate drawable window and picture buffers.
475 static int Init (vout_thread_t *vout)
477 vout_sys_t *p_sys = vout->p_sys;
478 unsigned x, y, width, height;
480 get_window_size (p_sys->conn, p_sys->embed->handle.xid, &width, &height);
481 vout_PlacePicture (vout, width, height, &x, &y, &width, &height);
483 const uint32_t values[] = { x, y, width, height, };
484 xcb_configure_window (p_sys->conn, p_sys->window,
485 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
486 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
489 /* FIXME: I don't get the subtlety between output and fmt_out here */
490 vout->fmt_out.i_visible_width = width;
491 vout->fmt_out.i_visible_height = height;
492 vout->fmt_out.i_sar_num = vout->fmt_out.i_sar_den = 1;
494 vout->output.i_width = vout->fmt_out.i_width =
495 width * vout->fmt_in.i_width / vout->fmt_in.i_visible_width;
496 vout->output.i_height = vout->fmt_out.i_height =
497 height * vout->fmt_in.i_height / vout->fmt_in.i_visible_height;
498 vout->fmt_out.i_x_offset =
499 width * vout->fmt_in.i_x_offset / vout->fmt_in.i_visible_width;
500 p_vout->fmt_out.i_y_offset =
501 height * vout->fmt_in.i_y_offset / vout->fmt_in.i_visible_height;
504 vout->output.i_aspect = vout->fmt_out.i_aspect =
505 width * VOUT_ASPECT_FACTOR / height;
507 /* Allocate picture buffers */
508 I_OUTPUTPICTURES = 0;
509 for (size_t index = 0; I_OUTPUTPICTURES < 2; index++)
511 picture_t *pic = vout->p_picture + index;
513 if (index > sizeof (vout->p_picture) / sizeof (pic))
515 if (pic->i_status != FREE_PICTURE)
517 if (PictureInit (vout, pic))
519 PP_OUTPUTPICTURE[I_OUTPUTPICTURES++] = pic;
521 xcb_flush (p_sys->conn);
526 * Free picture buffers.
528 static void Deinit (vout_thread_t *vout)
530 for (int i = 0; i < I_OUTPUTPICTURES; i++)
531 PictureDeinit (PP_OUTPUTPICTURE[i]);
535 * Sends an image to the X server.
537 static void Display (vout_thread_t *vout, picture_t *pic)
539 vout_sys_t *p_sys = vout->p_sys;
540 picture_sys_t *priv = pic->p_sys;
543 xcb_shm_put_image (p_sys->conn, p_sys->window, p_sys->gc,
544 /* real width */ pic->p->i_pitch / pic->p->i_pixel_pitch,
545 /* real height */ pic->p->i_lines, /* x */ 0, /* y */ 0,
546 /* width */ pic->p->i_visible_pitch / pic->p->i_pixel_pitch,
547 /* height */ pic->p->i_visible_lines, /* x */ 0, /* y */ 0,
548 p_sys->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
549 0, priv->segment, 0);
551 xcb_put_image (p_sys->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
552 p_sys->window, p_sys->gc,
553 pic->p->i_pitch / pic->p->i_pixel_pitch,
554 pic->p->i_lines, 0, 0, 0, p_sys->depth,
555 pic->p->i_pitch * pic->p->i_lines, pic->p->p_pixels);
556 xcb_flush (p_sys->conn);
560 * Process incoming X events.
562 static int Manage (vout_thread_t *vout)
564 vout_sys_t *p_sys = vout->p_sys;
565 xcb_generic_event_t *ev;
567 while ((ev = xcb_poll_for_event (p_sys->conn)) != NULL)
568 ProcessEvent (vout, p_sys->conn, p_sys->window, ev);
570 if (xcb_connection_has_error (p_sys->conn))
572 msg_Err (vout, "X server failure");