]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/x11.c
Merge branch 1.0-bugfix into master
[vlc] / modules / video_output / xcb / x11.c
1 /**
2  * @file x11.c
3  * @brief X C Bindings video output module for VLC media player
4  */
5 /*****************************************************************************
6  * Copyright © 2009 Rémi Denis-Courmont
7  *
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.
12  *
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.
17  *
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  ****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <assert.h>
29
30 #include <xcb/xcb.h>
31 #include <xcb/shm.h>
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_vout.h>
36 #include <vlc_window.h>
37
38 #include "xcb_vlc.h"
39
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.")
44
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.")
48
49 static int  Open (vlc_object_t *);
50 static void Close (vlc_object_t *);
51
52 /*
53  * Module descriptor
54  */
55 vlc_module_begin ()
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)
62
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)
66 vlc_module_end ()
67
68 struct vout_sys_t
69 {
70     xcb_connection_t *conn;
71     vout_window_t *embed; /* VLC window (when windowed) */
72
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 */
80 };
81
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 *);
86 static int Control (vout_thread_t *, int, va_list);
87
88 int CheckError (vout_thread_t *vout, const char *str, xcb_void_cookie_t ck)
89 {
90     xcb_generic_error_t *err;
91
92     err = xcb_request_check (vout->p_sys->conn, ck);
93     if (err)
94     {
95         msg_Err (vout, "%s: X11 error %d", str, err->error_code);
96         return VLC_EGENERIC;
97     }
98     return VLC_SUCCESS;
99 }
100
101 #define p_vout vout
102
103 /**
104  * Probe the X server.
105  */
106 static int Open (vlc_object_t *obj)
107 {
108     vout_thread_t *vout = (vout_thread_t *)obj;
109     vout_sys_t *p_sys = malloc (sizeof (*p_sys));
110     if (p_sys == NULL)
111         return VLC_ENOMEM;
112
113     vout->p_sys = p_sys;
114
115     /* Connect to X */
116     p_sys->conn = Connect (obj);
117     if (p_sys->conn == NULL)
118     {
119         free (p_sys);
120         return VLC_EGENERIC;
121     }
122
123     /* Get window */
124     const xcb_screen_t *scr;
125     p_sys->embed = GetWindow (vout, p_sys->conn, &scr, &p_sys->shm);
126     if (p_sys->embed == NULL)
127     {
128         xcb_disconnect (p_sys->conn);
129         free (p_sys);
130         return VLC_EGENERIC;
131     }
132
133     const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
134     p_sys->byte_order = setup->image_byte_order;
135
136     /* Determine our video format. Normally, this is done in pf_init(), but
137      * this plugin always uses the same format for a given X11 screen. */
138     xcb_visualid_t vid = 0;
139     uint8_t depth = 0;
140     bool gray = true;
141     for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
142              *end = fmt + xcb_setup_pixmap_formats_length (setup);
143          fmt < end; fmt++)
144     {
145         vlc_fourcc_t chroma = 0;
146
147         if (fmt->depth < depth)
148             continue; /* We already found a better format! */
149
150         /* Check that the pixmap format is supported by VLC. */
151         switch (fmt->depth)
152         {
153           case 24:
154             if (fmt->bits_per_pixel == 32)
155                 chroma = VLC_CODEC_RGB32;
156             else if (fmt->bits_per_pixel == 24)
157                 chroma = VLC_CODEC_RGB24;
158             else
159                 continue;
160             break;
161           case 16:
162             if (fmt->bits_per_pixel != 16)
163                 continue;
164             chroma = VLC_CODEC_RGB16;
165             break;
166           case 15:
167             if (fmt->bits_per_pixel != 16)
168                 continue;
169             chroma = VLC_CODEC_RGB15;
170             break;
171           case 8:
172             if (fmt->bits_per_pixel != 8)
173                 continue;
174             chroma = VLC_CODEC_RGB8;
175             break;
176           default:
177             continue;
178         }
179         if ((fmt->bits_per_pixel << 4) % fmt->scanline_pad)
180             continue; /* VLC pads lines to 16 pixels internally */
181
182         /* Byte sex is a non-issue for 8-bits. It can be worked around with
183          * RGB masks for 24-bits. Too bad for 15-bits and 16-bits. */
184         if (fmt->bits_per_pixel == 16 && setup->image_byte_order != ORDER)
185             continue;
186
187         /* Check that the selected screen supports this depth */
188         xcb_depth_iterator_t it = xcb_screen_allowed_depths_iterator (scr);
189         while (it.rem > 0 && it.data->depth != fmt->depth)
190              xcb_depth_next (&it);
191         if (!it.rem)
192             continue; /* Depth not supported on this screen */
193
194         /* Find a visual type for the selected depth */
195         const xcb_visualtype_t *vt = xcb_depth_visuals (it.data);
196         for (int i = xcb_depth_visuals_length (it.data); i > 0; i--)
197         {
198             if (vt->_class == XCB_VISUAL_CLASS_TRUE_COLOR)
199             {
200                 vid = vt->visual_id;
201                 gray = false;
202                 break;
203             }
204             if (fmt->depth == 8 && vt->_class == XCB_VISUAL_CLASS_STATIC_GRAY)
205             {
206                 if (!gray)
207                     continue; /* Prefer color over gray scale */
208                 vid = vt->visual_id;
209                 chroma = VLC_CODEC_GREY;
210             }
211         }
212
213         if (!vid)
214             continue; /* The screen does not *really* support this depth */
215
216         vout->fmt_out.i_chroma = vout->output.i_chroma = chroma;
217         if (!gray)
218         {
219             vout->fmt_out.i_rmask = vout->output.i_rmask = vt->red_mask;
220             vout->fmt_out.i_gmask = vout->output.i_gmask = vt->green_mask;
221             vout->fmt_out.i_bmask = vout->output.i_bmask = vt->blue_mask;
222         }
223         p_sys->bpp = fmt->bits_per_pixel;
224         p_sys->pad = fmt->scanline_pad;
225         p_sys->depth = depth = fmt->depth;
226     }
227
228     if (depth == 0)
229     {
230         msg_Err (vout, "no supported pixmap formats or visual types");
231         goto error;
232     }
233
234     msg_Dbg (vout, "using X11 visual ID 0x%"PRIx32, vid);
235     msg_Dbg (vout, " %"PRIu8" bits per pixels, %"PRIu8" bits line pad",
236              p_sys->bpp, p_sys->pad);
237
238     /* Create colormap (needed to select non-default visual) */
239     xcb_colormap_t cmap;
240     if (vid != scr->root_visual)
241     {
242         cmap = xcb_generate_id (p_sys->conn);
243         xcb_create_colormap (p_sys->conn, XCB_COLORMAP_ALLOC_NONE,
244                              cmap, scr->root, vid);
245     }
246     else
247         cmap = scr->default_colormap;
248
249     /* Create window */
250     {
251         const uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_COLORMAP;
252         const uint32_t values[] = {
253             /* XCB_CW_EVENT_MASK */
254             XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
255             XCB_EVENT_MASK_POINTER_MOTION,
256             /* XCB_CW_COLORMAP */
257             cmap,
258         };
259         xcb_void_cookie_t c;
260         xcb_window_t window = xcb_generate_id (p_sys->conn);
261
262         c = xcb_create_window_checked (p_sys->conn, depth, window,
263                                        p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
264                                        XCB_WINDOW_CLASS_INPUT_OUTPUT,
265                                        vid, mask, values);
266         if (CheckError (vout, "cannot create X11 window", c))
267             goto error;
268         p_sys->window = window;
269         msg_Dbg (vout, "using X11 window %08"PRIx32, p_sys->window);
270         xcb_map_window (p_sys->conn, window);
271     }
272
273     /* Create graphic context (I wonder why the heck do we need this) */
274     p_sys->gc = xcb_generate_id (p_sys->conn);
275     xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
276     msg_Dbg (vout, "using X11 graphic context %08"PRIx32, p_sys->gc);
277
278     vout->pf_init = Init;
279     vout->pf_end = Deinit;
280     vout->pf_display = Display;
281     vout->pf_manage = Manage;
282     vout->pf_control = Control;
283     return VLC_SUCCESS;
284
285 error:
286     Close (obj);
287     return VLC_EGENERIC;
288 }
289
290
291 /**
292  * Disconnect from the X server.
293  */
294 static void Close (vlc_object_t *obj)
295 {
296     vout_thread_t *vout = (vout_thread_t *)obj;
297     vout_sys_t *p_sys = vout->p_sys;
298
299     vout_ReleaseWindow (p_sys->embed);
300     /* colormap and window are garbage-collected by X */
301     xcb_disconnect (p_sys->conn);
302     free (p_sys);
303 }
304
305
306 /**
307  * Allocate drawable window and picture buffers.
308  */
309 static int Init (vout_thread_t *vout)
310 {
311     vout_sys_t *p_sys = vout->p_sys;
312     unsigned x, y, width, height;
313
314     if (GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
315         return VLC_EGENERIC;
316     vout_PlacePicture (vout, width, height, &x, &y, &width, &height);
317
318     const uint32_t values[] = { x, y, width, height, };
319     xcb_configure_window (p_sys->conn, p_sys->window,
320                           XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
321                           XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
322                           values);
323
324     /* FIXME: I don't get the subtlety between output and fmt_out here */
325     vout->fmt_out.i_visible_width = width;
326     vout->fmt_out.i_visible_height = height;
327     vout->fmt_out.i_sar_num = vout->fmt_out.i_sar_den = 1;
328
329     vout->output.i_width = vout->fmt_out.i_width =
330         width * vout->fmt_in.i_width / vout->fmt_in.i_visible_width;
331     vout->output.i_height = vout->fmt_out.i_height =
332         height * vout->fmt_in.i_height / vout->fmt_in.i_visible_height;
333     vout->fmt_out.i_x_offset =
334         width * vout->fmt_in.i_x_offset / vout->fmt_in.i_visible_width;
335     p_vout->fmt_out.i_y_offset =
336         height * vout->fmt_in.i_y_offset / vout->fmt_in.i_visible_height;
337
338     assert (height > 0);
339     vout->output.i_aspect = vout->fmt_out.i_aspect =
340         width * VOUT_ASPECT_FACTOR / height;
341
342     /* Allocate picture buffers */
343     I_OUTPUTPICTURES = 0;
344     for (size_t index = 0; I_OUTPUTPICTURES < 2; index++)
345     {
346         picture_t *pic = vout->p_picture + index;
347
348         if (index > sizeof (vout->p_picture) / sizeof (pic))
349             break;
350         if (pic->i_status != FREE_PICTURE)
351             continue;
352
353         picture_Setup (pic, vout->output.i_chroma,
354                        vout->output.i_width, vout->output.i_height,
355                        vout->output.i_aspect);
356         if (PictureAlloc (vout, pic, pic->p->i_pitch * pic->p->i_lines,
357                           p_sys->shm ? p_sys->conn : NULL))
358             break;
359         PP_OUTPUTPICTURE[I_OUTPUTPICTURES++] = pic;
360     }
361     xcb_flush (p_sys->conn);
362     return VLC_SUCCESS;
363 }
364
365 /**
366  * Free picture buffers.
367  */
368 static void Deinit (vout_thread_t *vout)
369 {
370     for (int i = 0; i < I_OUTPUTPICTURES; i++)
371         PictureFree (PP_OUTPUTPICTURE[i], vout->p_sys->conn);
372 }
373
374 /**
375  * Sends an image to the X server.
376  */
377 static void Display (vout_thread_t *vout, picture_t *pic)
378 {
379     vout_sys_t *p_sys = vout->p_sys;
380     xcb_shm_seg_t segment = (uintptr_t)pic->p_sys;
381
382     if (segment != 0)
383         xcb_shm_put_image (p_sys->conn, p_sys->window, p_sys->gc,
384           /* real width */ pic->p->i_pitch / pic->p->i_pixel_pitch,
385          /* real height */ pic->p->i_lines,
386                    /* x */ vout->fmt_out.i_x_offset,
387                    /* y */ vout->fmt_out.i_y_offset,
388                /* width */ vout->fmt_out.i_visible_width,
389               /* height */ vout->fmt_out.i_visible_height,
390                            0, 0, p_sys->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
391                            0, segment, 0);
392     else
393     {
394         const size_t offset = vout->fmt_out.i_y_offset * pic->p->i_pitch;
395         const unsigned lines = pic->p->i_lines - vout->fmt_out.i_y_offset;
396
397         xcb_put_image (p_sys->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
398                        p_sys->window, p_sys->gc,
399                        pic->p->i_pitch / pic->p->i_pixel_pitch,
400                        lines, -vout->fmt_out.i_x_offset, 0, 0, p_sys->depth,
401                        pic->p->i_pitch * lines, pic->p->p_pixels + offset);
402     }
403     xcb_flush (p_sys->conn);
404 }
405
406 /**
407  * Process incoming X events.
408  */
409 static int Manage (vout_thread_t *vout)
410 {
411     vout_sys_t *p_sys = vout->p_sys;
412     xcb_generic_event_t *ev;
413
414     while ((ev = xcb_poll_for_event (p_sys->conn)) != NULL)
415         ProcessEvent (vout, p_sys->conn, p_sys->window, ev);
416
417     if (xcb_connection_has_error (p_sys->conn))
418     {
419         msg_Err (vout, "X server failure");
420         return VLC_EGENERIC;
421     }
422
423     CommonManage (vout); /* FIXME: <-- move that to core */
424     return VLC_SUCCESS;
425 }
426
427 void
428 HandleParentStructure (vout_thread_t *vout, xcb_connection_t *conn,
429                        xcb_window_t xid, xcb_configure_notify_event_t *ev)
430 {
431     unsigned width, height, x, y;
432
433     vout_PlacePicture (vout, ev->width, ev->height, &x, &y, &width, &height);
434     if (width != vout->fmt_out.i_visible_width
435      || height != vout->fmt_out.i_visible_height)
436     {
437         vout->i_changes |= VOUT_SIZE_CHANGE;
438         return; /* vout will be reinitialized */
439     }
440
441     /* Move the picture within the window */
442     const uint32_t values[] = { x, y, };
443     xcb_configure_window (conn, xid,
444                           XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
445                           values);
446 }
447
448 static int Control (vout_thread_t *vout, int query, va_list ap)
449 {
450     return vout_ControlWindow (vout->p_sys->embed, query, ap);
451 }