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 (_("XCB"))
63 set_description (_("(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) */
81 xcb_window_t parent; /* parent X window */
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 */
88 static int Init (vout_thread_t *);
89 static void Deinit (vout_thread_t *);
90 static void Display (vout_thread_t *, picture_t *);
91 static int Manage (vout_thread_t *);
93 int CheckError (vout_thread_t *vout, const char *str, xcb_void_cookie_t ck)
95 xcb_generic_error_t *err;
97 err = xcb_request_check (vout->p_sys->conn, ck);
100 msg_Err (vout, "%s: X11 error %d", str, err->error_code);
109 * Probe the X server.
111 static int Open (vlc_object_t *obj)
113 vout_thread_t *vout = (vout_thread_t *)obj;
114 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 : "");
135 /* Get the preferred screen */
136 xcb_screen_t *scr = xcb_aux_get_screen (p_sys->conn, snum);
138 assert (p_sys->screen);
139 msg_Dbg (vout, "using screen %d (depth %"PRIu8")", snum, scr->root_depth);
141 if (strchr ("\x08\x0f\x10\x18\x20", scr->root_depth) == NULL)
143 msg_Err (vout, "unsupported %"PRIu8"-bits color depth",
148 /* Determine the visual (color depth and palette) */
149 xcb_visualtype_t *vt = NULL;
150 if ((vt = xcb_aux_find_visual_by_attrs (scr, XCB_VISUAL_CLASS_TRUE_COLOR,
151 scr->root_depth)) != NULL)
152 msg_Dbg (vout, "using TrueColor visual ID %d", (int)vt->visual_id);
154 if ((vt = xcb_aux_find_visual_by_attrs (scr,XCB_VISUAL_CLASS_STATIC_COLOR,
155 scr->root_depth)) != NULL)
156 msg_Dbg (vout, "using static color visual ID %d", (int)vt->visual_id);
159 vt = xcb_aux_get_visualtype (p_sys->conn, snum, scr->root_visual);
161 msg_Err (vout, "unsupported visual class %"PRIu8, vt->_class);
164 p_sys->vid = vt->visual_id;
166 p_sys->shm = var_CreateGetBool (vout, "x11-shm") > 0;
169 xcb_shm_query_version_cookie_t ck;
170 xcb_shm_query_version_reply_t *r;
171 xcb_generic_error_t *err;
173 ck = xcb_shm_query_version (p_sys->conn);
174 r = xcb_shm_query_version_reply (p_sys->conn, ck, &err);
177 msg_Err (vout, "shared memory (MIT-SHM) not available");
178 msg_Warn (vout, "display will be slow");
183 vout->pf_init = Init;
184 vout->pf_end = Deinit;
185 vout->pf_display = Display;
186 vout->pf_manage = Manage;
196 * Disconnect from the X server.
198 static void Close (vlc_object_t *obj)
200 vout_thread_t *vout = (vout_thread_t *)obj;
201 vout_sys_t *p_sys = vout->p_sys;
203 assert (p_sys->embed == NULL);
205 xcb_disconnect (p_sys->conn);
211 xcb_connection_t *conn;
213 xcb_shm_seg_t segment;
216 static void PictureRelease (picture_t *pic);
217 static void PictureShmRelease (picture_t *pic);
218 #define SHM_ERR ((void *)(intptr_t)(-1))
220 static int PictureInit (vout_thread_t *vout, picture_t *pic)
222 vout_sys_t *p_sys = vout->p_sys;
223 picture_sys_t *priv = malloc (sizeof (*p_sys));
228 assert (pic->i_status == FREE_PICTURE);
229 vout_InitPicture (vout, pic, vout->output.i_chroma,
230 vout->output.i_width, vout->output.i_height,
231 vout->output.i_aspect);
234 const size_t size = pic->p->i_pitch * pic->p->i_lines;
237 { /* Allocate shared memory segment */
238 int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0700);
242 msg_Err (vout, "shared memory allocation error: %m");
246 /* Attach the segment to VLC */
247 shm = shmat (id, NULL, 0 /* read/write */);
250 msg_Err (vout, "shared memory attachment error: %m");
251 shmctl (id, IPC_RMID, 0);
255 /* Attach the segment to X */
256 xcb_void_cookie_t ck;
258 priv->segment = xcb_generate_id (p_sys->conn);
259 ck = xcb_shm_attach_checked (p_sys->conn, priv->segment, id, 1);
260 shmctl (id, IPC_RMID, 0);
262 if (CheckError (vout, "shared memory server-side error", ck))
264 msg_Info (vout, "using buggy X (remote) server? SSH?");
270 const unsigned real_width = pic->p->i_pitch / (p_sys->bpp >> 3);
271 /* FIXME: anyway to getthing more intuitive than that?? */
273 /* NOTE: 32-bits scanline_pad assumed, FIXME? (see xdpyinfo) */
275 img = xcb_image_create (real_width, pic->p->i_lines,
276 XCB_IMAGE_FORMAT_Z_PIXMAP, 32,
277 p_sys->screen->root_depth, p_sys->bpp, p_sys->bpp,
278 #ifdef WORDS_BIGENDIAN
279 XCB_IMAGE_ORDER_MSB_FIRST,
281 XCB_IMAGE_ORDER_LSB_FIRST,
283 XCB_IMAGE_ORDER_MSB_FIRST,
285 (shm != SHM_ERR) ? size : 0,
286 (shm != SHM_ERR) ? shm : NULL);
291 xcb_shm_detach (p_sys->conn, priv->segment);
294 if (shm != SHM_ERR && xcb_image_native (p_sys->conn, img, 0) == NULL)
296 msg_Err (vout, "incompatible X server image format");
297 xcb_image_destroy (img);
301 priv->conn = p_sys->conn;
304 pic->p->p_pixels = img->data;
305 pic->pf_release = (shm != SHM_ERR) ? PictureShmRelease
307 pic->i_status = DESTROYED_PICTURE;
308 pic->i_type = DIRECT_PICTURE;
320 * Release picture private data
322 static void PictureRelease (picture_t *pic)
324 struct picture_sys_t *p_sys = pic->p_sys;
326 xcb_image_destroy (p_sys->image);
331 * Release shared memory picture private data
333 static void PictureShmRelease (picture_t *pic)
335 struct picture_sys_t *p_sys = pic->p_sys;
337 xcb_shm_detach (p_sys->conn, p_sys->segment);
338 shmdt (p_sys->image->data);
339 PictureRelease (pic);
343 * Allocate drawable window and picture buffers.
345 static int Init (vout_thread_t *vout)
347 vout_sys_t *p_sys = vout->p_sys;
348 const xcb_screen_t *screen = p_sys->screen;
349 unsigned x, y, width, height;
351 /* Determine parent window */
352 if (vout->b_fullscreen)
355 p_sys->parent = screen->root;
356 width = screen->width_in_pixels;
357 height = screen->height_in_pixels;
361 p_sys->embed = vout_RequestWindow (vout, &(int){ 0 }, &(int){ 0 },
363 if (p_sys->embed == NULL)
365 msg_Err (vout, "cannot get window");
368 p_sys->parent = (intptr_t)p_sys->embed->handle;
371 /* Determine our input format */
372 p_sys->bpp = screen->root_depth;
373 switch (screen->root_depth)
377 case 32: /* FIXME: untested */
378 vout->output.i_chroma = VLC_FOURCC ('R', 'V', '3', '2');
382 vout->output.i_chroma = VLC_FOURCC ('R', 'V', '1', '6');
387 vout->output.i_chroma = VLC_FOURCC ('R', 'V', '1', '5');
390 case 8: /* FIXME: VLC cannot convert */
391 vout->output.i_chroma = VLC_FOURCC ('R', 'G', 'B', '2');
398 vout_PlacePicture (vout, width, height, &x, &y, &width, &height);
399 vout->output.i_width = width;
400 vout->output.i_height = height;
402 vout->output.i_aspect = width * VOUT_ASPECT_FACTOR / height;
403 /* FIXME: I don't get the subtlety between output and fmt_out here */
406 const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
407 uint32_t values[2] = {
408 /* XCB_CW_BACK_PIXEL */
410 /* XCB_CW_EVENT_MASK */
411 XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
412 XCB_EVENT_MASK_POINTER_MOTION,
415 xcb_window_t window = xcb_generate_id (p_sys->conn);
417 c = xcb_create_window_checked (p_sys->conn, screen->root_depth, window,
418 p_sys->parent, x, y, width, height, 0,
419 XCB_WINDOW_CLASS_INPUT_OUTPUT,
420 screen->root_visual, mask, values);
421 if (CheckError (vout, "cannot create X11 window", c))
423 p_sys->window = window;
424 msg_Dbg (vout, "using X11 window %08"PRIx32, p_sys->window);
425 xcb_map_window (p_sys->conn, window);
427 /* Create graphic context (I wonder why the heck do we need this) */
428 p_sys->gc = xcb_generate_id (p_sys->conn);
429 xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
430 msg_Dbg (vout, "using X11 graphic context %08"PRIx32, p_sys->gc);
431 xcb_flush (p_sys->conn);
433 /* Allocate picture buffers */
436 picture_t *pic = vout->p_picture + I_OUTPUTPICTURES;
438 if (PictureInit (vout, pic))
440 PP_OUTPUTPICTURE[I_OUTPUTPICTURES++] = pic;
442 while (I_OUTPUTPICTURES < 2);
452 * Free picture buffers.
454 static void Deinit (vout_thread_t *vout)
456 vout_sys_t *p_sys = vout->p_sys;
458 while (I_OUTPUTPICTURES > 0)
459 picture_Release (PP_OUTPUTPICTURE[--I_OUTPUTPICTURES]);
461 xcb_unmap_window (p_sys->conn, p_sys->window);
462 xcb_destroy_window (p_sys->conn, p_sys->window);
463 vout_ReleaseWindow (p_sys->embed);
468 * Sends an image to the X server.
470 static void Display (vout_thread_t *vout, picture_t *pic)
472 vout_sys_t *p_sys = vout->p_sys;
473 picture_sys_t *priv = pic->p_sys;
474 xcb_image_t *img = priv->image;
476 if (img->base == NULL)
478 xcb_shm_segment_info_t info = {
479 .shmseg = priv->segment,
480 .shmid = -1, /* lost track of it, unimportant */
481 .shmaddr = img->data,
484 xcb_image_shm_put (p_sys->conn, p_sys->window, p_sys->gc, img, info,
485 0, 0, 0, 0, img->width, img->height, 0);
489 xcb_image_t *native = xcb_image_native (p_sys->conn, img, 1);
494 xcb_image_put (p_sys->conn, p_sys->window, p_sys->gc, native, 0, 0, 0);
496 xcb_image_destroy (native);
498 xcb_flush (p_sys->conn);
502 * Process incoming X events.
504 static int Manage (vout_thread_t *vout)
506 vout_sys_t *p_sys = vout->p_sys;
507 xcb_generic_event_t *ev;
509 while ((ev = xcb_poll_for_event (p_sys->conn)) != NULL)
510 ProcessEvent (vout, ev);
512 if (xcb_connection_has_error (p_sys->conn))
514 msg_Err (vout, "X server failure");