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_colormap_t cmap; /* colormap for selected visual */
82 xcb_window_t window; /* drawable X window */
83 xcb_gcontext_t gc; /* context to put images */
84 bool shm; /* whether to use MIT-SHM */
85 uint8_t bpp; /* bits per pixel */
86 uint8_t pad; /* scanline pad */
87 uint8_t byte_order; /* server byte order */
90 static int Init (vout_thread_t *);
91 static void Deinit (vout_thread_t *);
92 static void Display (vout_thread_t *, picture_t *);
93 static int Manage (vout_thread_t *);
95 int CheckError (vout_thread_t *vout, const char *str, xcb_void_cookie_t ck)
97 xcb_generic_error_t *err;
99 err = xcb_request_check (vout->p_sys->conn, ck);
102 msg_Err (vout, "%s: X11 error %d", str, err->error_code);
111 * Probe the X server.
113 static int Open (vlc_object_t *obj)
115 vout_thread_t *vout = (vout_thread_t *)obj;
116 vout_sys_t *p_sys = malloc (sizeof (*p_sys));
125 char *display = var_CreateGetNonEmptyString (vout, "x11-display");
127 p_sys->conn = xcb_connect (display, &snum);
128 if (xcb_connection_has_error (p_sys->conn) /*== NULL*/)
130 msg_Err (vout, "cannot connect to X server %s",
131 display ? display : "");
137 const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
138 p_sys->byte_order = setup->image_byte_order;
140 /* Get the preferred screen */
141 xcb_screen_t *scr = xcb_aux_get_screen (p_sys->conn, snum);
143 assert (p_sys->screen);
144 msg_Dbg (vout, "using screen %d", snum);
146 /* Determine our video format. Normally, this is done in pf_init(), but
147 * this plugin always uses the same format for a given X11 screen. */
150 for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
151 *end = fmt + xcb_setup_pixmap_formats_length (setup);
154 vlc_fourcc_t chroma = 0;
156 if (fmt->depth < depth)
157 continue; /* We already found a better format! */
159 /* Check that the pixmap format is supported by VLC. */
163 if (fmt->bits_per_pixel == 32)
164 chroma = VLC_FOURCC ('R', 'V', '3', '2');
165 else if (fmt->bits_per_pixel == 24)
166 chroma = VLC_FOURCC ('R', 'V', '2', '4');
171 if (fmt->bits_per_pixel != 16)
173 chroma = VLC_FOURCC ('R', 'V', '1', '6');
176 if (fmt->bits_per_pixel != 16)
178 chroma = VLC_FOURCC ('R', 'V', '1', '5');
181 if (fmt->bits_per_pixel != 8)
183 chroma = VLC_FOURCC ('R', 'G', 'B', '2');
188 if ((fmt->bits_per_pixel << 4) % fmt->scanline_pad)
189 continue; /* VLC pads lines to 16 pixels internally */
191 /* Byte sex is a non-issue for 8-bits. It can be worked around with
192 * RGB masks for 24-bits. Too bad for 15-bits and 16-bits. */
193 #ifdef WORDS_BIGENDIAN
194 # define ORDER XCB_IMAGE_ORDER_MSB_FIRST
196 # define ORDER XCB_IMAGE_ORDER_LSB_FIRST
198 if (fmt->bits_per_pixel == 16 && setup->image_byte_order != ORDER)
201 /* Check that the selected screen supports this depth */
202 xcb_depth_iterator_t it = xcb_screen_allowed_depths_iterator (scr);
203 while (it.rem > 0 && it.data->depth != fmt->depth)
204 xcb_depth_next (&it);
206 continue; /* Depth not supported on this screen */
208 /* Find a visual type for the selected depth */
209 const xcb_visualtype_t *vt = xcb_depth_visuals (it.data);
210 xcb_visualid_t vid = 0;
211 for (int i = xcb_depth_visuals_length (it.data); (i > 0) && !vid; i--)
213 if (vt->_class == XCB_VISUAL_CLASS_TRUE_COLOR)
216 "using TrueColor %"PRIu8"-bits visual ID 0x%0"PRIx32,
217 fmt->depth, vt->visual_id);
222 if (fmt->depth == 8 && vt->_class == XCB_VISUAL_CLASS_STATIC_GRAY)
225 continue; /* Prefer color over gray scale */
227 chroma = VLC_FOURCC ('G', 'R', 'E', 'Y');
229 "using static gray 8-bits visual ID 0x%x"PRIx32,
236 continue; /* The screen does not *really* support this depth */
238 vout->fmt_out.i_chroma = vout->output.i_chroma = chroma;
241 vout->output.i_rmask = vt->red_mask;
242 vout->output.i_gmask = vt->green_mask;
243 vout->output.i_bmask = vt->blue_mask;
246 p_sys->bpp = fmt->bits_per_pixel;
247 p_sys->pad = fmt->scanline_pad;
254 msg_Err (vout, "no supported pixmap formats");
258 msg_Dbg (vout, "using %"PRIu8" bits per pixels (line pad: %"PRIu8")",
259 p_sys->bpp, p_sys->pad);
261 /* Create colormap (needed to select non-default visual) */
262 if (p_sys->vid != scr->root_visual)
264 p_sys->cmap = xcb_generate_id (p_sys->conn);
265 xcb_create_colormap (p_sys->conn, XCB_COLORMAP_ALLOC_NONE,
266 p_sys->cmap, scr->root, p_sys->vid);
269 p_sys->cmap = scr->default_colormap;
271 /* Check shared memory support */
272 p_sys->shm = var_CreateGetBool (vout, "x11-shm") > 0;
275 xcb_shm_query_version_cookie_t ck;
276 xcb_shm_query_version_reply_t *r;
278 ck = xcb_shm_query_version (p_sys->conn);
279 r = xcb_shm_query_version_reply (p_sys->conn, ck, NULL);
282 msg_Err (vout, "shared memory (MIT-SHM) not available");
283 msg_Warn (vout, "display will be slow");
290 /* FIXME: WTH to put as initial width/height values??? */
291 p_sys->embed = vout_RequestXWindow (vout, &(int){ 0 }, &(int){ 0 },
292 &(unsigned){ 0 }, &(unsigned){ 0 });
293 if (p_sys->embed == NULL)
295 msg_Err (vout, "parent window not available");
298 /* Subscribe to parent window resize events */
299 xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid,
300 XCB_CW_EVENT_MASK, &(uint32_t){ XCB_EVENT_MASK_STRUCTURE_NOTIFY });
302 vout->pf_init = Init;
303 vout->pf_end = Deinit;
304 vout->pf_display = Display;
305 vout->pf_manage = Manage;
315 * Disconnect from the X server.
317 static void Close (vlc_object_t *obj)
319 vout_thread_t *vout = (vout_thread_t *)obj;
320 vout_sys_t *p_sys = vout->p_sys;
323 vout_ReleaseWindow (p_sys->embed);
324 /* colormap is garbage-ollected by X (?) */
326 xcb_disconnect (p_sys->conn);
332 xcb_connection_t *conn; /* Shared connection to X server */
333 xcb_image_t *image; /* Picture buffer */
334 xcb_shm_seg_t segment; /* Shared memory segment X ID */
337 #define SHM_ERR ((void *)(intptr_t)(-1))
339 static int PictureInit (vout_thread_t *vout, picture_t *pic)
341 vout_sys_t *p_sys = vout->p_sys;
342 picture_sys_t *priv = malloc (sizeof (*p_sys));
347 assert (pic->i_status == FREE_PICTURE);
348 vout_InitPicture (vout, pic, vout->output.i_chroma,
349 vout->output.i_width, vout->output.i_height,
350 vout->output.i_aspect);
353 const size_t size = pic->p->i_pitch * pic->p->i_lines;
356 { /* Allocate shared memory segment */
357 int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0700);
361 msg_Err (vout, "shared memory allocation error: %m");
365 /* Attach the segment to VLC */
366 shm = shmat (id, NULL, 0 /* read/write */);
369 msg_Err (vout, "shared memory attachment error: %m");
370 shmctl (id, IPC_RMID, 0);
374 /* Attach the segment to X */
375 xcb_void_cookie_t ck;
377 priv->segment = xcb_generate_id (p_sys->conn);
378 ck = xcb_shm_attach_checked (p_sys->conn, priv->segment, id, 1);
379 shmctl (id, IPC_RMID, 0);
381 if (CheckError (vout, "shared memory server-side error", ck))
383 msg_Info (vout, "using buggy X (remote) server? SSH?");
391 const unsigned real_width = pic->p->i_pitch / (p_sys->bpp >> 3);
392 /* FIXME: anyway to getthing more intuitive than that?? */
395 img = xcb_image_create (real_width, pic->p->i_lines,
396 XCB_IMAGE_FORMAT_Z_PIXMAP, p_sys->pad,
397 p_sys->screen->root_depth, p_sys->bpp, p_sys->bpp,
398 p_sys->byte_order, XCB_IMAGE_ORDER_MSB_FIRST,
400 (shm != SHM_ERR) ? size : 0,
401 (shm != SHM_ERR) ? shm : NULL);
406 xcb_shm_detach (p_sys->conn, priv->segment);
410 priv->conn = p_sys->conn;
413 pic->p->p_pixels = img->data;
414 pic->i_status = DESTROYED_PICTURE;
415 pic->i_type = DIRECT_PICTURE;
427 * Release picture private data
429 static void PictureDeinit (picture_t *pic)
431 struct picture_sys_t *p_sys = pic->p_sys;
433 if (p_sys->segment != 0)
435 xcb_shm_detach (p_sys->conn, p_sys->segment);
436 shmdt (p_sys->image->data);
438 xcb_image_destroy (p_sys->image);
442 static void get_window_size (xcb_connection_t *conn, xcb_window_t win,
443 unsigned *width, unsigned *height)
445 xcb_get_geometry_cookie_t ck = xcb_get_geometry (conn, win);
446 xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply (conn, ck, NULL);
451 *height = geo->height;
455 *width = *height = 0;
459 * Allocate drawable window and picture buffers.
461 static int Init (vout_thread_t *vout)
463 vout_sys_t *p_sys = vout->p_sys;
464 const xcb_screen_t *screen = p_sys->screen;
465 unsigned x, y, width, height;
466 xcb_window_t parent = p_sys->embed->handle.xid;
468 I_OUTPUTPICTURES = 0;
470 get_window_size (p_sys->conn, parent, &width, &height);
471 vout_PlacePicture (vout, width, height, &x, &y, &width, &height);
473 /* FIXME: I don't get the subtlety between output and fmt_out here */
474 vout->fmt_out.i_visible_width = width;
475 vout->fmt_out.i_visible_height = height;
476 vout->fmt_out.i_sar_num = vout->fmt_out.i_sar_den = 1;
478 vout->output.i_width = vout->fmt_out.i_width =
479 width * vout->fmt_in.i_width / vout->fmt_in.i_visible_width;
480 vout->output.i_height = vout->fmt_out.i_height =
481 height * vout->fmt_in.i_height / vout->fmt_in.i_visible_height;
482 vout->fmt_out.i_x_offset =
483 width * vout->fmt_in.i_x_offset / vout->fmt_in.i_visible_width;
484 p_vout->fmt_out.i_y_offset =
485 height * vout->fmt_in.i_y_offset / vout->fmt_in.i_visible_height;
488 vout->output.i_aspect = vout->fmt_out.i_aspect =
489 width * VOUT_ASPECT_FACTOR / height;
492 const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK
494 const uint32_t values[] = {
495 /* XCB_CW_BACK_PIXEL */
497 /* XCB_CW_EVENT_MASK */
498 XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
499 XCB_EVENT_MASK_POINTER_MOTION,
500 /* XCB_CW_COLORMAP */
504 xcb_window_t window = xcb_generate_id (p_sys->conn);
506 c = xcb_create_window_checked (p_sys->conn, screen->root_depth, window,
507 parent, x, y, width, height, 0,
508 XCB_WINDOW_CLASS_INPUT_OUTPUT,
509 p_sys->vid, mask, values);
510 if (CheckError (vout, "cannot create X11 window", c))
512 p_sys->window = window;
513 msg_Dbg (vout, "using X11 window %08"PRIx32, p_sys->window);
514 xcb_map_window (p_sys->conn, window);
516 /* Create graphic context (I wonder why the heck do we need this) */
517 p_sys->gc = xcb_generate_id (p_sys->conn);
518 xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
519 msg_Dbg (vout, "using X11 graphic context %08"PRIx32, p_sys->gc);
520 xcb_flush (p_sys->conn);
522 /* Allocate picture buffers */
523 I_OUTPUTPICTURES = 0;
524 for (size_t index = 0; I_OUTPUTPICTURES < 2; index++)
526 picture_t *pic = vout->p_picture + index;
528 if (index > sizeof (vout->p_picture) / sizeof (pic))
530 if (pic->i_status != FREE_PICTURE)
532 if (PictureInit (vout, pic))
534 PP_OUTPUTPICTURE[I_OUTPUTPICTURES++] = pic;
545 * Free picture buffers.
547 static void Deinit (vout_thread_t *vout)
549 vout_sys_t *p_sys = vout->p_sys;
551 for (int i = 0; i < I_OUTPUTPICTURES; i++)
552 PictureDeinit (PP_OUTPUTPICTURE[i]);
554 xcb_unmap_window (p_sys->conn, p_sys->window);
555 xcb_destroy_window (p_sys->conn, p_sys->window);
559 * Sends an image to the X server.
561 static void Display (vout_thread_t *vout, picture_t *pic)
563 vout_sys_t *p_sys = vout->p_sys;
564 picture_sys_t *priv = pic->p_sys;
565 xcb_image_t *img = priv->image;
567 if (img->base == NULL)
569 xcb_shm_segment_info_t info = {
570 .shmseg = priv->segment,
571 .shmid = -1, /* lost track of it, unimportant */
572 .shmaddr = img->data,
575 xcb_image_shm_put (p_sys->conn, p_sys->window, p_sys->gc, img, info,
576 0, 0, 0, 0, img->width, img->height, 0);
579 xcb_image_put (p_sys->conn, p_sys->window, p_sys->gc, img, 0, 0, 0);
580 xcb_flush (p_sys->conn);
584 * Process incoming X events.
586 static int Manage (vout_thread_t *vout)
588 vout_sys_t *p_sys = vout->p_sys;
589 xcb_generic_event_t *ev;
591 while ((ev = xcb_poll_for_event (p_sys->conn)) != NULL)
592 ProcessEvent (vout, p_sys->conn, p_sys->window, ev);
594 if (xcb_connection_has_error (p_sys->conn))
596 msg_Err (vout, "X server failure");