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 ****************************************************************************/
31 #include <xcb/xcb_aux.h>
32 #include <xcb/xcb_image.h>
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
37 #include <vlc_window.h>
39 #define DISPLAY_TEXT N_("X11 display")
40 #define DISPLAY_LONGTEXT N_( \
41 "X11 hardware display to use. By default VLC will " \
42 "use the value of the DISPLAY environment variable.")
44 static int Open (vlc_object_t *);
45 static void Close (vlc_object_t *);
51 set_shortname (_("XCB"))
52 set_description (_("(Experimental) XCB video output"))
53 set_category (CAT_VIDEO)
54 set_subcategory (SUBCAT_VIDEO_VOUT)
55 set_capability ("video output", 0)
56 set_callbacks (Open, Close)
58 add_string ("x11-display", NULL, NULL,
59 DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
64 xcb_connection_t *conn;
66 vout_window_t *embed; /* VLC window (when windowed) */
69 xcb_window_t parent; /* parent X window */
70 xcb_window_t window; /* drawable X window */
71 xcb_gcontext_t gc; /* context to put images */
72 uint8_t bpp; /* bits per pixel */
75 static int Init (vout_thread_t *);
76 static void Deinit (vout_thread_t *);
77 static void Display (vout_thread_t *, picture_t *);
78 static int Manage (vout_thread_t *);
80 static int CheckError (vout_thread_t *vout, const char *str,
83 xcb_generic_error_t *err;
85 err = xcb_request_check (vout->p_sys->conn, ck);
88 msg_Err (vout, "%s: X11 error %d", str, err->error_code);
99 static int Open (vlc_object_t *obj)
101 vout_thread_t *vout = (vout_thread_t *)obj;
102 vout_sys_t *p_sys = malloc (sizeof (*p_sys));
113 char *display = var_CreateGetNonEmptyString (vout, "x11-display");
115 p_sys->conn = xcb_connect (display, &snum);
116 if (xcb_connection_has_error (p_sys->conn) /*== NULL*/)
118 msg_Err (vout, "cannot connect to X server %s",
119 display ? display : "");
123 /* Get the preferred screen */
124 xcb_screen_t *scr = xcb_aux_get_screen (p_sys->conn, snum);
126 assert (p_sys->screen);
127 msg_Dbg (vout, "using screen %d (depth %"PRIu8")", snum, scr->root_depth);
129 if (strchr ("\x08\x0f\x10\x18\x20", scr->root_depth) == NULL)
131 msg_Err (vout, "unsupported %"PRIu8"-bits color depth",
136 /* Determine the visual (color depth and palette) */
137 xcb_visualtype_t *vt = NULL;
138 if ((vt = xcb_aux_find_visual_by_attrs (scr, XCB_VISUAL_CLASS_TRUE_COLOR,
139 scr->root_depth)) != NULL)
140 msg_Dbg (vout, "using TrueColor visual ID %d", (int)vt->visual_id);
142 if ((vt = xcb_aux_find_visual_by_attrs (scr,XCB_VISUAL_CLASS_STATIC_COLOR,
143 scr->root_depth)) != NULL)
144 msg_Dbg (vout, "using static color visual ID %d", (int)vt->visual_id);
147 vt = xcb_aux_get_visualtype (p_sys->conn, snum, scr->root_visual);
149 msg_Err (vout, "unsupported visual class %"PRIu8, vt->_class);
152 p_sys->vid = vt->visual_id;
154 vout->pf_init = Init;
155 vout->pf_end = Deinit;
156 vout->pf_display = Display;
157 vout->pf_manage = Manage;
167 * Disconnect from the X server.
169 static void Close (vlc_object_t *obj)
171 vout_thread_t *vout = (vout_thread_t *)obj;
172 vout_sys_t *p_sys = vout->p_sys;
174 assert (p_sys->embed == NULL);
176 xcb_disconnect (p_sys->conn);
185 static void PictureRelease (picture_t *pic);
187 static int PictureInit (vout_thread_t *vout, picture_t *pic)
189 vout_sys_t *p_sys = vout->p_sys;
190 picture_sys_t *priv = malloc (sizeof (*p_sys));
195 assert (pic->i_status == FREE_PICTURE);
196 vout_InitPicture (vout, pic, vout->output.i_chroma,
197 vout->output.i_width, vout->output.i_height,
198 vout->output.i_aspect);
200 const unsigned real_width = pic->p->i_pitch / (p_sys->bpp >> 3);
201 /* FIXME: anyway to getthing more intuitive than that?? */
203 /* NOTE: 32-bits scanline_pad assumed, FIXME? (see xdpyinfo) */
205 img = xcb_image_create (real_width, pic->p->i_lines,
206 XCB_IMAGE_FORMAT_Z_PIXMAP, 32,
207 p_sys->screen->root_depth, p_sys->bpp, p_sys->bpp,
208 #ifdef WORDS_BIGENDIAN
209 XCB_IMAGE_ORDER_MSB_FIRST,
211 XCB_IMAGE_ORDER_LSB_FIRST,
213 XCB_IMAGE_ORDER_MSB_FIRST,
221 pic->p->p_pixels = img->data;
222 pic->pf_release = PictureRelease;
223 pic->i_status = DESTROYED_PICTURE;
224 pic->i_type = DIRECT_PICTURE;
234 * Release picture private data
236 static void PictureRelease (picture_t *pic)
238 struct picture_sys_t *p_sys = pic->p_sys;
240 xcb_image_destroy (p_sys->image);
245 * Allocate drawable window and picture buffers.
247 static int Init (vout_thread_t *vout)
249 vout_sys_t *p_sys = vout->p_sys;
250 const xcb_screen_t *screen = p_sys->screen;
251 unsigned x, y, width, height;
253 /* Determine parent window */
254 if (vout->b_fullscreen)
257 p_sys->parent = screen->root;
258 width = screen->width_in_pixels;
259 height = screen->height_in_pixels;
263 p_sys->embed = vout_RequestWindow (vout, &(int){ 0 }, &(int){ 0 },
265 if (p_sys->embed == NULL)
267 msg_Err (vout, "cannot get window");
270 p_sys->parent = (intptr_t)p_sys->embed->handle;
273 /* Determine our input format */
274 p_sys->bpp = screen->root_depth;
275 switch (screen->root_depth)
279 case 32: /* FIXME: untested */
280 vout->output.i_chroma = VLC_FOURCC ('R', 'V', '3', '2');
284 vout->output.i_chroma = VLC_FOURCC ('R', 'V', '1', '6');
289 vout->output.i_chroma = VLC_FOURCC ('R', 'V', '1', '5');
292 case 8: /* FIXME: VLC cannot convert */
293 vout->output.i_chroma = VLC_FOURCC ('R', 'G', 'B', '2');
300 vout_PlacePicture (vout, width, height, &x, &y, &width, &height);
301 vout->output.i_width = width;
302 vout->output.i_height = height;
304 vout->output.i_aspect = width * VOUT_ASPECT_FACTOR / height;
305 /* FIXME: I don't get the subtlety between output and fmt_out here */
309 xcb_window_t window = xcb_generate_id (p_sys->conn);
311 p_sys->window = window;
312 c = xcb_create_window_checked (p_sys->conn, screen->root_depth, window,
313 p_sys->parent, x, y, width, height, 0,
314 XCB_WINDOW_CLASS_INPUT_OUTPUT,
315 screen->root_visual, 0, NULL);
316 if (CheckError (vout, "cannot create X11 window", c))
318 msg_Dbg (vout, "using X11 window %08"PRIx32, p_sys->window);
319 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);
325 xcb_flush (p_sys->conn);
327 /* Allocate picture buffers */
330 picture_t *pic = vout->p_picture + I_OUTPUTPICTURES;
332 if (PictureInit (vout, pic))
334 PP_OUTPUTPICTURE[I_OUTPUTPICTURES++] = pic;
336 while (I_OUTPUTPICTURES < 2);
346 * Free picture buffers.
348 static void Deinit (vout_thread_t *vout)
350 vout_sys_t *p_sys = vout->p_sys;
352 while (I_OUTPUTPICTURES > 0)
353 picture_Release (PP_OUTPUTPICTURE[--I_OUTPUTPICTURES]);
355 xcb_unmap_window (p_sys->conn, p_sys->window);
356 xcb_destroy_window (p_sys->conn, p_sys->window);
357 vout_ReleaseWindow (p_sys->embed);
362 * Sends an image to the X server.
364 static void Display (vout_thread_t *vout, picture_t *pic)
366 vout_sys_t *p_sys = vout->p_sys;
367 xcb_image_t *img = pic->p_sys->image;
368 xcb_image_t *native = xcb_image_native (p_sys->conn, img, 1);
372 msg_Err (vout, "image conversion failure");
376 xcb_image_put (p_sys->conn, p_sys->window, p_sys->gc, native, 0, 0, 0);
377 xcb_flush (p_sys->conn);
381 /*msg_Warn (vout, "image not in native X11 pixmap format");*/
382 xcb_image_destroy (native);
387 * Process incoming X events.
389 static int Manage (vout_thread_t *vout)
391 vout_sys_t *p_sys = vout->p_sys;
392 xcb_generic_event_t *ev;
394 while ((ev = xcb_poll_for_event (p_sys->conn)) != NULL)
396 switch (ev->response_type & ~0x80)
399 msg_Dbg (vout, "unhandled event %02x", (unsigned)ev->response_type);
404 if (xcb_connection_has_error (p_sys->conn))
406 msg_Err (vout, "X server failure");