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>
44 #define DISPLAY_TEXT N_("X11 display")
45 #define DISPLAY_LONGTEXT N_( \
46 "X11 hardware display to use. By default VLC will " \
47 "use the value of the DISPLAY environment variable.")
49 #define SHM_TEXT N_("Use shared memory")
50 #define SHM_LONGTEXT N_( \
51 "Use shared memory to communicate between VLC and the X server.")
53 static int Open (vlc_object_t *);
54 static void Close (vlc_object_t *);
60 set_shortname (_("XCB"))
61 set_description (_("(Experimental) XCB video output"))
62 set_category (CAT_VIDEO)
63 set_subcategory (SUBCAT_VIDEO_VOUT)
64 set_capability ("video output", 0)
65 set_callbacks (Open, Close)
67 add_string ("x11-display", NULL, NULL,
68 DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
69 add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
74 xcb_connection_t *conn;
76 vout_window_t *embed; /* VLC window (when windowed) */
79 xcb_window_t parent; /* parent X window */
80 xcb_window_t window; /* drawable X window */
81 xcb_gcontext_t gc; /* context to put images */
82 bool shm; /* whether to use MIT-SHM */
83 uint8_t bpp; /* bits per pixel */
86 static int Init (vout_thread_t *);
87 static void Deinit (vout_thread_t *);
88 static void Display (vout_thread_t *, picture_t *);
89 static int Manage (vout_thread_t *);
91 static int CheckError (vout_thread_t *vout, const char *str,
94 xcb_generic_error_t *err;
96 err = xcb_request_check (vout->p_sys->conn, ck);
99 msg_Err (vout, "%s: X11 error %d", str, err->error_code);
108 * Probe the X server.
110 static int Open (vlc_object_t *obj)
112 vout_thread_t *vout = (vout_thread_t *)obj;
113 vout_sys_t *p_sys = malloc (sizeof (*p_sys));
124 char *display = var_CreateGetNonEmptyString (vout, "x11-display");
126 p_sys->conn = xcb_connect (display, &snum);
127 if (xcb_connection_has_error (p_sys->conn) /*== NULL*/)
129 msg_Err (vout, "cannot connect to X server %s",
130 display ? display : "");
134 /* Get the preferred screen */
135 xcb_screen_t *scr = xcb_aux_get_screen (p_sys->conn, snum);
137 assert (p_sys->screen);
138 msg_Dbg (vout, "using screen %d (depth %"PRIu8")", snum, scr->root_depth);
140 if (strchr ("\x08\x0f\x10\x18\x20", scr->root_depth) == NULL)
142 msg_Err (vout, "unsupported %"PRIu8"-bits color depth",
147 /* Determine the visual (color depth and palette) */
148 xcb_visualtype_t *vt = NULL;
149 if ((vt = xcb_aux_find_visual_by_attrs (scr, XCB_VISUAL_CLASS_TRUE_COLOR,
150 scr->root_depth)) != NULL)
151 msg_Dbg (vout, "using TrueColor visual ID %d", (int)vt->visual_id);
153 if ((vt = xcb_aux_find_visual_by_attrs (scr,XCB_VISUAL_CLASS_STATIC_COLOR,
154 scr->root_depth)) != NULL)
155 msg_Dbg (vout, "using static color visual ID %d", (int)vt->visual_id);
158 vt = xcb_aux_get_visualtype (p_sys->conn, snum, scr->root_visual);
160 msg_Err (vout, "unsupported visual class %"PRIu8, vt->_class);
163 p_sys->vid = vt->visual_id;
165 p_sys->shm = var_CreateGetBool (vout, "x11-shm") > 0;
168 xcb_shm_query_version_cookie_t ck;
169 xcb_shm_query_version_reply_t *r;
170 xcb_generic_error_t *err;
172 ck = xcb_shm_query_version (p_sys->conn);
173 r = xcb_shm_query_version_reply (p_sys->conn, ck, &err);
176 msg_Err (vout, "shared memory (MIT-SHM) not available");
177 msg_Warn (vout, "display will be slow");
182 vout->pf_init = Init;
183 vout->pf_end = Deinit;
184 vout->pf_display = Display;
185 vout->pf_manage = Manage;
195 * Disconnect from the X server.
197 static void Close (vlc_object_t *obj)
199 vout_thread_t *vout = (vout_thread_t *)obj;
200 vout_sys_t *p_sys = vout->p_sys;
202 assert (p_sys->embed == NULL);
204 xcb_disconnect (p_sys->conn);
210 xcb_connection_t *conn;
212 xcb_shm_seg_t segment;
215 static void PictureRelease (picture_t *pic);
216 static void PictureShmRelease (picture_t *pic);
217 #define SHM_ERR ((void *)(intptr_t)(-1))
219 static int PictureInit (vout_thread_t *vout, picture_t *pic)
221 vout_sys_t *p_sys = vout->p_sys;
222 picture_sys_t *priv = malloc (sizeof (*p_sys));
227 assert (pic->i_status == FREE_PICTURE);
228 vout_InitPicture (vout, pic, vout->output.i_chroma,
229 vout->output.i_width, vout->output.i_height,
230 vout->output.i_aspect);
233 const size_t size = pic->p->i_pitch * pic->p->i_lines;
236 { /* Allocate shared memory segment */
237 int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0700);
241 msg_Err (vout, "shared memory allocation error: %m");
245 /* Attach the segment to VLC */
246 shm = shmat (id, NULL, 0 /* read/write */);
249 msg_Err (vout, "shared memory attachment error: %m");
250 shmctl (id, IPC_RMID, 0);
254 /* Attach the segment to X */
255 xcb_void_cookie_t ck;
257 priv->segment = xcb_generate_id (p_sys->conn);
258 ck = xcb_shm_attach_checked (p_sys->conn, priv->segment, id, 1);
259 shmctl (id, IPC_RMID, 0);
261 if (CheckError (vout, "shared memory server-side error", ck))
263 msg_Info (vout, "using buggy X (remote) server? SSH?");
269 const unsigned real_width = pic->p->i_pitch / (p_sys->bpp >> 3);
270 /* FIXME: anyway to getthing more intuitive than that?? */
272 /* NOTE: 32-bits scanline_pad assumed, FIXME? (see xdpyinfo) */
274 img = xcb_image_create (real_width, pic->p->i_lines,
275 XCB_IMAGE_FORMAT_Z_PIXMAP, 32,
276 p_sys->screen->root_depth, p_sys->bpp, p_sys->bpp,
277 #ifdef WORDS_BIGENDIAN
278 XCB_IMAGE_ORDER_MSB_FIRST,
280 XCB_IMAGE_ORDER_LSB_FIRST,
282 XCB_IMAGE_ORDER_MSB_FIRST,
284 (shm != SHM_ERR) ? size : 0,
285 (shm != SHM_ERR) ? shm : NULL);
290 xcb_shm_detach (p_sys->conn, priv->segment);
293 if (shm != SHM_ERR && xcb_image_native (p_sys->conn, img, 0) == NULL)
295 msg_Err (vout, "incompatible X server image format");
296 xcb_image_destroy (img);
300 priv->conn = p_sys->conn;
303 pic->p->p_pixels = img->data;
304 pic->pf_release = (shm != SHM_ERR) ? PictureShmRelease
306 pic->i_status = DESTROYED_PICTURE;
307 pic->i_type = DIRECT_PICTURE;
319 * Release picture private data
321 static void PictureRelease (picture_t *pic)
323 struct picture_sys_t *p_sys = pic->p_sys;
325 xcb_image_destroy (p_sys->image);
330 * Release shared memory picture private data
332 static void PictureShmRelease (picture_t *pic)
334 struct picture_sys_t *p_sys = pic->p_sys;
336 xcb_shm_detach (p_sys->conn, p_sys->segment);
337 shmdt (p_sys->image->data);
338 PictureRelease (pic);
342 * Allocate drawable window and picture buffers.
344 static int Init (vout_thread_t *vout)
346 vout_sys_t *p_sys = vout->p_sys;
347 const xcb_screen_t *screen = p_sys->screen;
348 unsigned x, y, width, height;
350 /* Determine parent window */
351 if (vout->b_fullscreen)
354 p_sys->parent = screen->root;
355 width = screen->width_in_pixels;
356 height = screen->height_in_pixels;
360 p_sys->embed = vout_RequestWindow (vout, &(int){ 0 }, &(int){ 0 },
362 if (p_sys->embed == NULL)
364 msg_Err (vout, "cannot get window");
367 p_sys->parent = (intptr_t)p_sys->embed->handle;
370 /* Determine our input format */
371 p_sys->bpp = screen->root_depth;
372 switch (screen->root_depth)
376 case 32: /* FIXME: untested */
377 vout->output.i_chroma = VLC_FOURCC ('R', 'V', '3', '2');
381 vout->output.i_chroma = VLC_FOURCC ('R', 'V', '1', '6');
386 vout->output.i_chroma = VLC_FOURCC ('R', 'V', '1', '5');
389 case 8: /* FIXME: VLC cannot convert */
390 vout->output.i_chroma = VLC_FOURCC ('R', 'G', 'B', '2');
397 vout_PlacePicture (vout, width, height, &x, &y, &width, &height);
398 vout->output.i_width = width;
399 vout->output.i_height = height;
401 vout->output.i_aspect = width * VOUT_ASPECT_FACTOR / height;
402 /* FIXME: I don't get the subtlety between output and fmt_out here */
406 xcb_window_t window = xcb_generate_id (p_sys->conn);
408 p_sys->window = window;
409 c = xcb_create_window_checked (p_sys->conn, screen->root_depth, window,
410 p_sys->parent, x, y, width, height, 0,
411 XCB_WINDOW_CLASS_INPUT_OUTPUT,
412 screen->root_visual, 0, NULL);
413 if (CheckError (vout, "cannot create X11 window", c))
415 msg_Dbg (vout, "using X11 window %08"PRIx32, p_sys->window);
416 xcb_map_window (p_sys->conn, window);
418 /* Create graphic context (I wonder why the heck do we need this) */
419 p_sys->gc = xcb_generate_id (p_sys->conn);
420 xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
421 msg_Dbg (vout, "using X11 graphic context %08"PRIx32, p_sys->gc);
422 xcb_flush (p_sys->conn);
424 /* Allocate picture buffers */
427 picture_t *pic = vout->p_picture + I_OUTPUTPICTURES;
429 if (PictureInit (vout, pic))
431 PP_OUTPUTPICTURE[I_OUTPUTPICTURES++] = pic;
433 while (I_OUTPUTPICTURES < 2);
443 * Free picture buffers.
445 static void Deinit (vout_thread_t *vout)
447 vout_sys_t *p_sys = vout->p_sys;
449 while (I_OUTPUTPICTURES > 0)
450 picture_Release (PP_OUTPUTPICTURE[--I_OUTPUTPICTURES]);
452 xcb_unmap_window (p_sys->conn, p_sys->window);
453 xcb_destroy_window (p_sys->conn, p_sys->window);
454 vout_ReleaseWindow (p_sys->embed);
459 * Sends an image to the X server.
461 static void Display (vout_thread_t *vout, picture_t *pic)
463 vout_sys_t *p_sys = vout->p_sys;
464 picture_sys_t *priv = pic->p_sys;
465 xcb_image_t *img = priv->image;
467 if (img->base == NULL)
469 xcb_shm_segment_info_t info = {
470 .shmseg = priv->segment,
471 .shmid = -1, /* lost track of it, unimportant */
472 .shmaddr = img->data,
475 xcb_image_shm_put (p_sys->conn, p_sys->window, p_sys->gc, img, info,
476 0, 0, 0, 0, img->width, img->height, 0);
480 xcb_image_t *native = xcb_image_native (p_sys->conn, img, 1);
485 xcb_image_put (p_sys->conn, p_sys->window, p_sys->gc, native, 0, 0, 0);
487 xcb_image_destroy (native);
489 xcb_flush (p_sys->conn);
493 * Process incoming X events.
495 static int Manage (vout_thread_t *vout)
497 vout_sys_t *p_sys = vout->p_sys;
498 xcb_generic_event_t *ev;
500 while ((ev = xcb_poll_for_event (p_sys->conn)) != NULL)
502 switch (ev->response_type & ~0x80)
505 msg_Dbg (vout, "unhandled event %02x", (unsigned)ev->response_type);
510 if (xcb_connection_has_error (p_sys->conn))
512 msg_Err (vout, "X server failure");