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 ****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <vlc_window.h>
40 #define DISPLAY_TEXT N_("X11 display")
41 #define DISPLAY_LONGTEXT N_( \
42 "X11 hardware display to use. By default VLC will " \
43 "use the value of the DISPLAY environment variable.")
45 #define SHM_TEXT N_("Use shared memory")
46 #define SHM_LONGTEXT N_( \
47 "Use shared memory to communicate between VLC and the X server.")
49 static int Open (vlc_object_t *);
50 static void Close (vlc_object_t *);
56 set_shortname (N_("XCB"))
57 set_description (N_("(Experimental) XCB video output"))
58 set_category (CAT_VIDEO)
59 set_subcategory (SUBCAT_VIDEO_VOUT)
60 set_capability ("video output", 0)
61 set_callbacks (Open, Close)
63 add_string ("x11-display", NULL, NULL,
64 DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
65 add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
70 xcb_connection_t *conn;
71 vout_window_t *embed; /* VLC window (when windowed) */
73 xcb_window_t window; /* drawable X window */
74 xcb_gcontext_t gc; /* context to put images */
75 bool shm; /* whether to use MIT-SHM */
76 uint8_t bpp; /* bits per pixel */
77 uint8_t pad; /* scanline pad */
78 uint8_t depth; /* useful bits per pixel */
79 uint8_t byte_order; /* server byte order */
82 static int Init (vout_thread_t *);
83 static void Deinit (vout_thread_t *);
84 static void Display (vout_thread_t *, picture_t *);
85 static int Manage (vout_thread_t *);
87 int CheckError (vout_thread_t *vout, const char *str, xcb_void_cookie_t ck)
89 xcb_generic_error_t *err;
91 err = xcb_request_check (vout->p_sys->conn, ck);
94 msg_Err (vout, "%s: X11 error %d", str, err->error_code);
103 * Probe the X server.
105 static int Open (vlc_object_t *obj)
107 vout_thread_t *vout = (vout_thread_t *)obj;
108 vout_sys_t *p_sys = malloc (sizeof (*p_sys));
115 p_sys->conn = Connect (obj);
116 if (p_sys->conn == NULL)
120 const xcb_screen_t *scr;
121 p_sys->embed = GetWindow (vout, p_sys->conn, &scr, &p_sys->shm);
122 if (p_sys->embed == NULL)
124 xcb_disconnect (p_sys->conn);
128 const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
129 p_sys->byte_order = setup->image_byte_order;
131 /* Determine our video format. Normally, this is done in pf_init(), but
132 * this plugin always uses the same format for a given X11 screen. */
133 xcb_visualid_t vid = 0;
136 for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
137 *end = fmt + xcb_setup_pixmap_formats_length (setup);
140 vlc_fourcc_t chroma = 0;
142 if (fmt->depth < depth)
143 continue; /* We already found a better format! */
145 /* Check that the pixmap format is supported by VLC. */
149 if (fmt->bits_per_pixel == 32)
150 chroma = VLC_FOURCC ('R', 'V', '3', '2');
151 else if (fmt->bits_per_pixel == 24)
152 chroma = VLC_FOURCC ('R', 'V', '2', '4');
157 if (fmt->bits_per_pixel != 16)
159 chroma = VLC_FOURCC ('R', 'V', '1', '6');
162 if (fmt->bits_per_pixel != 16)
164 chroma = VLC_FOURCC ('R', 'V', '1', '5');
167 if (fmt->bits_per_pixel != 8)
169 chroma = VLC_FOURCC ('R', 'G', 'B', '2');
174 if ((fmt->bits_per_pixel << 4) % fmt->scanline_pad)
175 continue; /* VLC pads lines to 16 pixels internally */
177 /* Byte sex is a non-issue for 8-bits. It can be worked around with
178 * RGB masks for 24-bits. Too bad for 15-bits and 16-bits. */
179 if (fmt->bits_per_pixel == 16 && setup->image_byte_order != ORDER)
182 /* Check that the selected screen supports this depth */
183 xcb_depth_iterator_t it = xcb_screen_allowed_depths_iterator (scr);
184 while (it.rem > 0 && it.data->depth != fmt->depth)
185 xcb_depth_next (&it);
187 continue; /* Depth not supported on this screen */
189 /* Find a visual type for the selected depth */
190 const xcb_visualtype_t *vt = xcb_depth_visuals (it.data);
191 for (int i = xcb_depth_visuals_length (it.data); i > 0; i--)
193 if (vt->_class == XCB_VISUAL_CLASS_TRUE_COLOR)
199 if (fmt->depth == 8 && vt->_class == XCB_VISUAL_CLASS_STATIC_GRAY)
202 continue; /* Prefer color over gray scale */
204 chroma = VLC_FOURCC ('G', 'R', 'E', 'Y');
209 continue; /* The screen does not *really* support this depth */
211 vout->fmt_out.i_chroma = vout->output.i_chroma = chroma;
214 vout->output.i_rmask = vt->red_mask;
215 vout->output.i_gmask = vt->green_mask;
216 vout->output.i_bmask = vt->blue_mask;
218 p_sys->bpp = fmt->bits_per_pixel;
219 p_sys->pad = fmt->scanline_pad;
220 p_sys->depth = depth = fmt->depth;
225 msg_Err (vout, "no supported pixmap formats or visual types");
229 msg_Dbg (vout, "using X11 visual ID 0x%"PRIx32, vid);
230 msg_Dbg (vout, " %"PRIu8" bits per pixels, %"PRIu8" bits line pad",
231 p_sys->bpp, p_sys->pad);
233 /* Create colormap (needed to select non-default visual) */
235 if (vid != scr->root_visual)
237 cmap = xcb_generate_id (p_sys->conn);
238 xcb_create_colormap (p_sys->conn, XCB_COLORMAP_ALLOC_NONE,
239 cmap, scr->root, vid);
242 cmap = scr->default_colormap;
246 const uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_COLORMAP;
247 const uint32_t values[] = {
248 /* XCB_CW_EVENT_MASK */
249 XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
250 XCB_EVENT_MASK_POINTER_MOTION,
251 /* XCB_CW_COLORMAP */
255 xcb_window_t window = xcb_generate_id (p_sys->conn);
257 c = xcb_create_window_checked (p_sys->conn, depth, window,
258 p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
259 XCB_WINDOW_CLASS_INPUT_OUTPUT,
261 if (CheckError (vout, "cannot create X11 window", c))
263 p_sys->window = window;
264 msg_Dbg (vout, "using X11 window %08"PRIx32, p_sys->window);
265 xcb_map_window (p_sys->conn, window);
268 /* Create graphic context (I wonder why the heck do we need this) */
269 p_sys->gc = xcb_generate_id (p_sys->conn);
270 xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
271 msg_Dbg (vout, "using X11 graphic context %08"PRIx32, p_sys->gc);
273 vout->pf_init = Init;
274 vout->pf_end = Deinit;
275 vout->pf_display = Display;
276 vout->pf_manage = Manage;
286 * Disconnect from the X server.
288 static void Close (vlc_object_t *obj)
290 vout_thread_t *vout = (vout_thread_t *)obj;
291 vout_sys_t *p_sys = vout->p_sys;
293 vout_ReleaseWindow (p_sys->embed);
294 /* colormap and window are garbage-collected by X */
295 xcb_disconnect (p_sys->conn);
301 * Allocate drawable window and picture buffers.
303 static int Init (vout_thread_t *vout)
305 vout_sys_t *p_sys = vout->p_sys;
306 unsigned x, y, width, height;
308 if (GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
310 vout_PlacePicture (vout, width, height, &x, &y, &width, &height);
312 const uint32_t values[] = { x, y, width, height, };
313 xcb_configure_window (p_sys->conn, p_sys->window,
314 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
315 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
318 /* FIXME: I don't get the subtlety between output and fmt_out here */
319 vout->fmt_out.i_visible_width = width;
320 vout->fmt_out.i_visible_height = height;
321 vout->fmt_out.i_sar_num = vout->fmt_out.i_sar_den = 1;
323 vout->output.i_width = vout->fmt_out.i_width =
324 width * vout->fmt_in.i_width / vout->fmt_in.i_visible_width;
325 vout->output.i_height = vout->fmt_out.i_height =
326 height * vout->fmt_in.i_height / vout->fmt_in.i_visible_height;
327 vout->fmt_out.i_x_offset =
328 width * vout->fmt_in.i_x_offset / vout->fmt_in.i_visible_width;
329 p_vout->fmt_out.i_y_offset =
330 height * vout->fmt_in.i_y_offset / vout->fmt_in.i_visible_height;
333 vout->output.i_aspect = vout->fmt_out.i_aspect =
334 width * VOUT_ASPECT_FACTOR / height;
336 /* Allocate picture buffers */
337 I_OUTPUTPICTURES = 0;
338 for (size_t index = 0; I_OUTPUTPICTURES < 2; index++)
340 picture_t *pic = vout->p_picture + index;
342 if (index > sizeof (vout->p_picture) / sizeof (pic))
344 if (pic->i_status != FREE_PICTURE)
346 if (PictureInit (vout, pic, p_sys->shm ? p_sys->conn : NULL))
348 PP_OUTPUTPICTURE[I_OUTPUTPICTURES++] = pic;
350 xcb_flush (p_sys->conn);
355 * Free picture buffers.
357 static void Deinit (vout_thread_t *vout)
359 for (int i = 0; i < I_OUTPUTPICTURES; i++)
360 PictureDeinit (PP_OUTPUTPICTURE[i], vout->p_sys->conn);
364 * Sends an image to the X server.
366 static void Display (vout_thread_t *vout, picture_t *pic)
368 vout_sys_t *p_sys = vout->p_sys;
369 xcb_shm_seg_t segment = (uintptr_t)pic->p_sys;
372 xcb_shm_put_image (p_sys->conn, p_sys->window, p_sys->gc,
373 /* real width */ pic->p->i_pitch / pic->p->i_pixel_pitch,
374 /* real height */ pic->p->i_lines, /* x */ 0, /* y */ 0,
375 /* width */ pic->p->i_visible_pitch / pic->p->i_pixel_pitch,
376 /* height */ pic->p->i_visible_lines, /* x */ 0, /* y */ 0,
377 p_sys->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
380 xcb_put_image (p_sys->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
381 p_sys->window, p_sys->gc,
382 pic->p->i_pitch / pic->p->i_pixel_pitch,
383 pic->p->i_lines, 0, 0, 0, p_sys->depth,
384 pic->p->i_pitch * pic->p->i_lines, pic->p->p_pixels);
385 xcb_flush (p_sys->conn);
389 * Process incoming X events.
391 static int Manage (vout_thread_t *vout)
393 vout_sys_t *p_sys = vout->p_sys;
394 xcb_generic_event_t *ev;
396 while ((ev = xcb_poll_for_event (p_sys->conn)) != NULL)
397 ProcessEvent (vout, p_sys->conn, p_sys->window, ev);
399 if (xcb_connection_has_error (p_sys->conn))
401 msg_Err (vout, "X server failure");
408 HandleParentStructure (vout_thread_t *vout, xcb_connection_t *conn,
409 xcb_window_t xid, xcb_configure_notify_event_t *ev)
411 unsigned width, height, x, y;
413 vout_PlacePicture (vout, ev->width, ev->height, &x, &y, &width, &height);
414 if (width != vout->fmt_out.i_visible_width
415 || height != vout->fmt_out.i_visible_height)
417 vout->i_changes |= VOUT_SIZE_CHANGE;
418 return; /* vout will be reinitialized */
421 /* Move the picture within the window */
422 const uint32_t values[] = { x, y, };
423 xcb_configure_window (conn, xid,
424 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,