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 */
74 static int Init (vout_thread_t *);
75 static void Deinit (vout_thread_t *);
76 static void Display (vout_thread_t *, picture_t *);
77 static int Manage (vout_thread_t *);
79 static int CheckError (vout_thread_t *vout, const char *str,
82 xcb_generic_error_t *err;
84 err = xcb_request_check (vout->p_sys->conn, ck);
87 msg_Err (vout, "%s: X11 error %d", str, err->error_code);
98 static int Open (vlc_object_t *obj)
100 vout_thread_t *vout = (vout_thread_t *)obj;
101 vout_sys_t *p_sys = malloc (sizeof (*p_sys));
112 char *display = var_CreateGetNonEmptyString (vout, "x11-display");
114 p_sys->conn = xcb_connect (display, &snum);
115 if (xcb_connection_has_error (p_sys->conn) /*== NULL*/)
117 msg_Err (vout, "cannot connect to X server %s",
118 display ? display : "");
122 /* Get the preferred screen */
123 xcb_screen_t *scr = xcb_aux_get_screen (p_sys->conn, snum);
125 assert (p_sys->screen);
126 msg_Dbg (vout, "using screen %d (depth %"PRIu8")", snum, scr->root_depth);
128 if (strchr ("\x08\x0f\x10\x18\x20", scr->root_depth) == NULL)
130 msg_Err (vout, "unsupported %"PRIu8"-bits color depth",
135 /* Determine the visual (color depth and palette) */
136 xcb_visualtype_t *vt = NULL;
137 if ((vt = xcb_aux_find_visual_by_attrs (scr, XCB_VISUAL_CLASS_TRUE_COLOR,
138 scr->root_depth)) != NULL)
139 msg_Dbg (vout, "using TrueColor visual ID %d", (int)vt->visual_id);
141 if ((vt = xcb_aux_find_visual_by_attrs (scr,XCB_VISUAL_CLASS_STATIC_COLOR,
142 scr->root_depth)) != NULL)
143 msg_Dbg (vout, "using static color visual ID %d", (int)vt->visual_id);
146 vt = xcb_aux_get_visualtype (p_sys->conn, snum, scr->root_visual);
148 msg_Err (vout, "unsupported visual class %"PRIu8, vt->_class);
151 p_sys->vid = vt->visual_id;
153 vout->pf_init = Init;
154 vout->pf_end = Deinit;
155 vout->pf_display = Display;
156 vout->pf_manage = Manage;
166 * Disconnect from the X server.
168 static void Close (vlc_object_t *obj)
170 vout_thread_t *vout = (vout_thread_t *)obj;
171 vout_sys_t *p_sys = vout->p_sys;
173 assert (p_sys->embed == NULL);
175 xcb_disconnect (p_sys->conn);
179 static void PictureRelease (picture_t *pic)
181 xcb_image_t *img = (xcb_image_t *)pic->p_sys;
182 xcb_image_destroy (img);
186 * Allocate drawable window and picture buffers.
188 static int Init (vout_thread_t *vout)
190 vout_sys_t *p_sys = vout->p_sys;
191 const xcb_screen_t *screen = p_sys->screen;
192 unsigned x, y, width, height;
194 /* Determine parent window */
195 if (vout->b_fullscreen)
198 p_sys->parent = screen->root;
199 width = screen->width_in_pixels;
200 height = screen->height_in_pixels;
204 p_sys->embed = vout_RequestWindow (vout, &(int){ 0 }, &(int){ 0 },
206 if (p_sys->embed == NULL)
208 msg_Err (vout, "cannot get window");
211 p_sys->parent = (intptr_t)p_sys->embed->handle;
214 /* Determine our input format */
215 uint8_t depth = screen->root_depth, bpp = depth;
220 case 32: /* FIXME: untested */
221 vout->output.i_chroma = VLC_FOURCC ('R', 'V', '3', '2');
225 vout->output.i_chroma = VLC_FOURCC ('R', 'V', '1', '6');
230 vout->output.i_chroma = VLC_FOURCC ('R', 'V', '1', '5');
233 case 8: /* FIXME: VLC cannot convert */
234 vout->output.i_chroma = VLC_FOURCC ('R', 'G', 'B', '2');
241 vout_PlacePicture (vout, width, height, &x, &y, &width, &height);
242 vout->output.i_width = width;
243 vout->output.i_height = height;
245 vout->output.i_aspect = width * VOUT_ASPECT_FACTOR / height;
246 /* FIXME: I don't get the subtlety between output and fmt_out here */
250 xcb_window_t window = xcb_generate_id (p_sys->conn);
252 p_sys->window = window;
253 c = xcb_create_window_checked (p_sys->conn, screen->root_depth, window,
254 p_sys->parent, x, y, width, height, 0,
255 XCB_WINDOW_CLASS_INPUT_OUTPUT,
256 screen->root_visual, 0, NULL);
257 if (CheckError (vout, "cannot create X11 window", c))
259 msg_Dbg (vout, "using X11 window %08"PRIx32, p_sys->window);
260 xcb_map_window (p_sys->conn, window);
262 /* Create graphic context (I wonder why the heck do we need this) */
263 p_sys->gc = xcb_generate_id (p_sys->conn);
264 xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
265 msg_Dbg (vout, "using X11 graphic context %08"PRIx32, p_sys->gc);
266 xcb_flush (p_sys->conn);
268 /* Allocate picture buffers */
271 picture_t *pic = vout->p_picture + I_OUTPUTPICTURES;
273 assert (pic->i_status == FREE_PICTURE);
274 vout_InitPicture (vout, pic, vout->output.i_chroma,
275 vout->output.i_width, vout->output.i_height,
276 vout->output.i_aspect);
279 const unsigned real_width = pic->p->i_pitch / (bpp >> 3);
280 /* FIXME: anyway to getthing more intuitive than that?? */
282 /* NOTE: 32-bits scanline_pad assumed, FIXME? (see xdpyinfo) */
283 img = xcb_image_create (real_width, height, XCB_IMAGE_FORMAT_Z_PIXMAP,
285 #ifdef WORDS_BIGENDIAN
286 XCB_IMAGE_ORDER_MSB_FIRST,
288 XCB_IMAGE_ORDER_LSB_FIRST,
290 XCB_IMAGE_ORDER_MSB_FIRST,
295 pic->p_sys = (picture_sys_t *)img;
296 pic->pf_release = PictureRelease;
297 pic->p->p_pixels = img->data;
299 pic->i_status = DESTROYED_PICTURE;
300 pic->i_type = DIRECT_PICTURE;
301 PP_OUTPUTPICTURE[I_OUTPUTPICTURES++] = pic;
303 while (I_OUTPUTPICTURES < 2);
313 * Free picture buffers.
315 static void Deinit (vout_thread_t *vout)
317 vout_sys_t *p_sys = vout->p_sys;
319 while (I_OUTPUTPICTURES > 0)
320 picture_Release (PP_OUTPUTPICTURE[--I_OUTPUTPICTURES]);
322 xcb_unmap_window (p_sys->conn, p_sys->window);
323 xcb_destroy_window (p_sys->conn, p_sys->window);
324 vout_ReleaseWindow (p_sys->embed);
329 * Sends an image to the X server.
331 static void Display (vout_thread_t *vout, picture_t *pic)
333 vout_sys_t *p_sys = vout->p_sys;
334 xcb_image_t *img = (xcb_image_t *)pic->p_sys;
335 xcb_image_t *native = xcb_image_native (p_sys->conn, img, 1);
339 msg_Err (vout, "image conversion failure");
343 xcb_image_put (p_sys->conn, p_sys->window, p_sys->gc, native, 0, 0, 0);
344 xcb_flush (p_sys->conn);
348 /*msg_Warn (vout, "image not in native X11 pixmap format");*/
349 xcb_image_destroy (native);
354 * Process incoming X events.
356 static int Manage (vout_thread_t *vout)
358 vout_sys_t *p_sys = vout->p_sys;
359 xcb_generic_event_t *ev;
361 while ((ev = xcb_poll_for_event (p_sys->conn)) != NULL)
363 switch (ev->response_type & ~0x80)
366 msg_Dbg (vout, "unhandled event %02x", (unsigned)ev->response_type);
371 if (xcb_connection_has_error (p_sys->conn))
373 msg_Err (vout, "X server failure");