]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/x11.c
Error handling fix
[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_display.h>
36 #include <vlc_picture_pool.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 ("vout display", 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 /* It must be large enough to absorb the server display jitter but it is
69  * useless to used a too large value, direct rendering cannot be used with
70  * xcb x11
71  */
72 #define MAX_PICTURES (3)
73
74 struct vout_display_sys_t
75 {
76     xcb_connection_t *conn;
77     vout_window_t *embed; /* VLC window (when windowed) */
78
79     xcb_window_t window; /* drawable X window */
80     xcb_gcontext_t gc; /* context to put images */
81     bool shm; /* whether to use MIT-SHM */
82     uint8_t bpp; /* bits per pixel */
83     uint8_t pad; /* scanline pad */
84     uint8_t depth; /* useful bits per pixel */
85     uint8_t byte_order; /* server byte order */
86
87     picture_pool_t *pool; /* picture pool */
88     picture_resource_t resource[MAX_PICTURES];
89 };
90
91 static picture_t *Get (vout_display_t *);
92 static void Display (vout_display_t *, picture_t *);
93 static int Control (vout_display_t *, int, va_list);
94 static void Manage (vout_display_t *);
95
96 static void ResetPictures (vout_display_t *);
97
98 /**
99  * Probe the X server.
100  */
101 static int Open (vlc_object_t *obj)
102 {
103     vout_display_t *vd = (vout_display_t *)obj;
104     vout_display_sys_t *p_sys = malloc (sizeof (*p_sys));
105     if (p_sys == NULL)
106         return VLC_ENOMEM;
107
108     vd->sys = p_sys;
109     p_sys->pool = NULL;
110
111     /* Connect to X */
112     p_sys->conn = Connect (obj);
113     if (p_sys->conn == NULL)
114     {
115         free (p_sys);
116         return VLC_EGENERIC;
117     }
118
119     /* Get window */
120     const xcb_screen_t *scr;
121     p_sys->embed = GetWindow (vd, p_sys->conn, &scr, &p_sys->shm);
122     if (p_sys->embed == NULL)
123     {
124         xcb_disconnect (p_sys->conn);
125         free (p_sys);
126         return VLC_EGENERIC;
127     }
128
129     const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
130     p_sys->byte_order = setup->image_byte_order;
131
132     /* */
133     video_format_t fmt_pic = vd->fmt;
134
135     /* Determine our video format. Normally, this is done in pf_init(), but
136      * this plugin always uses the same format for a given X11 screen. */
137     xcb_visualid_t vid = 0;
138     uint8_t depth = 0;
139     bool gray = true;
140     for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
141              *end = fmt + xcb_setup_pixmap_formats_length (setup);
142          fmt < end; fmt++)
143     {
144         vlc_fourcc_t chroma = 0;
145
146         if (fmt->depth < depth)
147             continue; /* We already found a better format! */
148
149         /* Check that the pixmap format is supported by VLC. */
150         switch (fmt->depth)
151         {
152           case 24:
153             if (fmt->bits_per_pixel == 32)
154                 chroma = VLC_CODEC_RGB32;
155             else if (fmt->bits_per_pixel == 24)
156                 chroma = VLC_CODEC_RGB24;
157             else
158                 continue;
159             break;
160           case 16:
161             if (fmt->bits_per_pixel != 16)
162                 continue;
163             chroma = VLC_CODEC_RGB16;
164             break;
165           case 15:
166             if (fmt->bits_per_pixel != 16)
167                 continue;
168             chroma = VLC_CODEC_RGB15;
169             break;
170           case 8:
171             if (fmt->bits_per_pixel != 8)
172                 continue;
173             chroma = VLC_CODEC_RGB8;
174             break;
175           default:
176             continue;
177         }
178         if ((fmt->bits_per_pixel << 4) % fmt->scanline_pad)
179             continue; /* VLC pads lines to 16 pixels internally */
180
181         /* Byte sex is a non-issue for 8-bits. It can be worked around with
182          * RGB masks for 24-bits. Too bad for 15-bits and 16-bits. */
183         if (fmt->bits_per_pixel == 16 && setup->image_byte_order != ORDER)
184             continue;
185
186         /* Check that the selected screen supports this depth */
187         xcb_depth_iterator_t it = xcb_screen_allowed_depths_iterator (scr);
188         while (it.rem > 0 && it.data->depth != fmt->depth)
189              xcb_depth_next (&it);
190         if (!it.rem)
191             continue; /* Depth not supported on this screen */
192
193         /* Find a visual type for the selected depth */
194         const xcb_visualtype_t *vt = xcb_depth_visuals (it.data);
195         for (int i = xcb_depth_visuals_length (it.data); i > 0; i--)
196         {
197             if (vt->_class == XCB_VISUAL_CLASS_TRUE_COLOR)
198             {
199                 vid = vt->visual_id;
200                 gray = false;
201                 break;
202             }
203             if (fmt->depth == 8 && vt->_class == XCB_VISUAL_CLASS_STATIC_GRAY)
204             {
205                 if (!gray)
206                     continue; /* Prefer color over gray scale */
207                 vid = vt->visual_id;
208                 chroma = VLC_CODEC_GREY;
209             }
210         }
211
212         if (!vid)
213             continue; /* The screen does not *really* support this depth */
214
215         fmt_pic.i_chroma = chroma;
216         if (!gray)
217         {
218             fmt_pic.i_rmask = vt->red_mask;
219             fmt_pic.i_gmask = vt->green_mask;
220             fmt_pic.i_bmask = vt->blue_mask;
221         }
222         p_sys->bpp = fmt->bits_per_pixel;
223         p_sys->pad = fmt->scanline_pad;
224         p_sys->depth = depth = fmt->depth;
225     }
226
227     if (depth == 0)
228     {
229         msg_Err (vd, "no supported pixmap formats or visual types");
230         goto error;
231     }
232
233     msg_Dbg (vd, "using X11 visual ID 0x%"PRIx32, vid);
234     msg_Dbg (vd, " %"PRIu8" bits per pixels, %"PRIu8" bits line pad",
235              p_sys->bpp, p_sys->pad);
236
237     /* Create colormap (needed to select non-default visual) */
238     xcb_colormap_t cmap;
239     if (vid != scr->root_visual)
240     {
241         cmap = xcb_generate_id (p_sys->conn);
242         xcb_create_colormap (p_sys->conn, XCB_COLORMAP_ALLOC_NONE,
243                              cmap, scr->root, vid);
244     }
245     else
246         cmap = scr->default_colormap;
247
248     /* Create window */
249     {
250         const uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_COLORMAP;
251         const uint32_t values[] = {
252             /* XCB_CW_EVENT_MASK */
253             XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
254             XCB_EVENT_MASK_POINTER_MOTION,
255             /* XCB_CW_COLORMAP */
256             cmap,
257         };
258         xcb_void_cookie_t c;
259         xcb_window_t window = xcb_generate_id (p_sys->conn);
260
261         c = xcb_create_window_checked (p_sys->conn, depth, window,
262                                        p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
263                                        XCB_WINDOW_CLASS_INPUT_OUTPUT,
264                                        vid, mask, values);
265         if (CheckError (vd, p_sys->conn, "cannot create X11 window", c))
266             goto error;
267         p_sys->window = window;
268         msg_Dbg (vd, "using X11 window %08"PRIx32, p_sys->window);
269         xcb_map_window (p_sys->conn, window);
270     }
271
272     /* Create graphic context (I wonder why the heck do we need this) */
273     p_sys->gc = xcb_generate_id (p_sys->conn);
274     xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
275     msg_Dbg (vd, "using X11 graphic context %08"PRIx32, p_sys->gc);
276
277     /* */
278     vout_display_info_t info = vd->info;
279     info.has_pictures_invalid = true;
280
281     /* Setup vout_display_t once everything is fine */
282     vd->fmt = fmt_pic;
283     vd->info = info;
284
285     vd->get = Get;
286     vd->prepare = NULL;
287     vd->display = Display;
288     vd->control = Control;
289     vd->manage = Manage;
290
291     /* */
292     unsigned width, height;
293     if (!GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
294         vout_display_SendEventDisplaySize (vd, width, height);
295     vout_display_SendEventFullscreen (vd, false);
296
297     return VLC_SUCCESS;
298
299 error:
300     Close (obj);
301     return VLC_EGENERIC;
302 }
303
304
305 /**
306  * Disconnect from the X server.
307  */
308 static void Close (vlc_object_t *obj)
309 {
310     vout_display_t *vd = (vout_display_t *)obj;
311     vout_display_sys_t *p_sys = vd->sys;
312
313     ResetPictures (vd);
314     vout_display_DeleteWindow (vd, p_sys->embed);
315     /* colormap and window are garbage-collected by X */
316     xcb_disconnect (p_sys->conn);
317     free (p_sys);
318 }
319
320 /**
321  * Return a direct buffer
322  */
323 static picture_t *Get (vout_display_t *vd)
324 {
325     vout_display_sys_t *p_sys = vd->sys;
326
327     if (!p_sys->pool)
328     {
329         vout_display_place_t place;
330
331         vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
332
333         /* */
334         const uint32_t values[] = { place.x, place.y, place.width, place.height };
335         xcb_configure_window (p_sys->conn, p_sys->window,
336                               XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
337                               XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
338                               values);
339
340         picture_t *pic = picture_NewFromFormat (&vd->fmt);
341         if (!pic)
342             return NULL;
343
344         assert (pic->i_planes == 1);
345         memset (p_sys->resource, 0, sizeof(p_sys->resource));
346
347         unsigned count;
348         picture_t *pic_array[MAX_PICTURES];
349         for (count = 0; count < MAX_PICTURES; count++)
350         {
351             picture_resource_t *res = &p_sys->resource[count];
352
353             res->p->i_lines = pic->p->i_lines;
354             res->p->i_pitch = pic->p->i_pitch;
355             if (PictureResourceAlloc (vd, res, res->p->i_pitch * res->p->i_lines,
356                                       p_sys->conn, p_sys->shm))
357                 break;
358             pic_array[count] = picture_NewFromResource (&vd->fmt, res);
359             if (!pic_array[count])
360             {
361                 PictureResourceFree (res, p_sys->conn);
362                 memset (res, 0, sizeof(*res));
363                 break;
364             }
365         }
366         picture_Release (pic);
367
368         if (count == 0)
369             return NULL;
370
371         p_sys->pool = picture_pool_New (count, pic_array);
372         if (!p_sys->pool)
373         {
374             /* TODO release picture resources */
375             return NULL;
376         }
377         /* FIXME should also do it in case of error ? */
378         xcb_flush (p_sys->conn);
379     }
380
381     return picture_pool_Get (p_sys->pool);
382 }
383
384 /**
385  * Sends an image to the X server.
386  */
387 static void Display (vout_display_t *vd, picture_t *pic)
388 {
389     vout_display_sys_t *p_sys = vd->sys;
390     xcb_shm_seg_t segment = pic->p_sys->segment;
391
392     if (segment != 0)
393         xcb_shm_put_image (p_sys->conn, p_sys->window, p_sys->gc,
394           /* real width */ pic->p->i_pitch / pic->p->i_pixel_pitch,
395          /* real height */ pic->p->i_lines,
396                    /* x */ vd->fmt.i_x_offset,
397                    /* y */ vd->fmt.i_y_offset,
398                /* width */ vd->fmt.i_visible_width,
399               /* height */ vd->fmt.i_visible_height,
400                            0, 0, p_sys->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
401                            0, segment, 0);
402     else
403     {
404         const size_t offset = vd->fmt.i_y_offset * pic->p->i_pitch;
405         const unsigned lines = pic->p->i_lines - vd->fmt.i_y_offset;
406
407         xcb_put_image (p_sys->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
408                        p_sys->window, p_sys->gc,
409                        pic->p->i_pitch / pic->p->i_pixel_pitch,
410                        lines, -vd->fmt.i_x_offset, 0, 0, p_sys->depth,
411                        pic->p->i_pitch * lines, pic->p->p_pixels + offset);
412     }
413     xcb_flush (p_sys->conn);
414
415     /* FIXME might be WAY better to wait in some case (be carefull with
416      * VOUT_DISPLAY_RESET_PICTURES if done) + does not work with
417      * vout_display wrapper. */
418     picture_Release (pic);
419 }
420
421 static int Control (vout_display_t *vd, int query, va_list ap)
422 {
423     vout_display_sys_t *p_sys = vd->sys;
424
425     switch (query)
426     {
427     case VOUT_DISPLAY_CHANGE_FULLSCREEN:
428     {
429         const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
430         return vout_window_SetFullScreen (p_sys->embed, c->is_fullscreen);
431     }
432
433     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
434     {
435         const vout_display_cfg_t *p_cfg =
436             (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
437
438         if (vout_window_SetSize (p_sys->embed,
439                                   p_cfg->display.width,
440                                   p_cfg->display.height))
441             return VLC_EGENERIC;
442
443         vout_display_place_t place;
444         vout_display_PlacePicture (&place, &vd->source, p_cfg, false);
445
446         if (place.width  != vd->fmt.i_visible_width ||
447             place.height != vd->fmt.i_visible_height)
448         {
449             vout_display_SendEventPicturesInvalid (vd);
450             return VLC_SUCCESS;
451         }
452
453         /* Move the picture within the window */
454         const uint32_t values[] = { place.x, place.y };
455         xcb_configure_window (p_sys->conn, p_sys->window,
456                               XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
457                               values);
458         return VLC_SUCCESS;
459     }
460     case VOUT_DISPLAY_CHANGE_ON_TOP:
461     {
462         int b_on_top = (int)va_arg (ap, int);
463         return vout_window_SetOnTop (p_sys->embed, b_on_top);
464     }
465
466     case VOUT_DISPLAY_CHANGE_ZOOM:
467     case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
468     case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
469     case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
470         /* I am not sure it is always necessary, but it is way simpler ... */
471         vout_display_SendEventPicturesInvalid (vd);
472         return VLC_SUCCESS;
473
474     case VOUT_DISPLAY_RESET_PICTURES:
475     {
476         ResetPictures (vd);
477
478         vout_display_place_t place;
479         vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
480
481         vd->fmt.i_width  = vd->source.i_width  * place.width  / vd->source.i_visible_width;
482         vd->fmt.i_height = vd->source.i_height * place.height / vd->source.i_visible_height;
483
484         vd->fmt.i_visible_width  = place.width;
485         vd->fmt.i_visible_height = place.height;
486         vd->fmt.i_x_offset = vd->source.i_x_offset * place.width  / vd->source.i_visible_width;
487         vd->fmt.i_y_offset = vd->source.i_y_offset * place.height / vd->source.i_visible_height;
488         return VLC_SUCCESS;
489     }
490
491     /* TODO */
492 #if 0
493     /* Hide the mouse. It will be send when
494      * vout_display_t::info.b_hide_mouse is false */
495     VOUT_DISPLAY_HIDE_MOUSE,
496 #endif
497     default:
498         msg_Err (vd, "Unknown request in XCB vout display");
499         return VLC_EGENERIC;
500     }
501 }
502
503 static void Manage (vout_display_t *vd)
504 {
505     vout_display_sys_t *p_sys = vd->sys;
506
507     ManageEvent (vd, p_sys->conn, p_sys->window);
508 }
509
510 static void ResetPictures (vout_display_t *vd)
511 {
512     vout_display_sys_t *p_sys = vd->sys;
513
514     if (!p_sys->pool)
515         return;
516
517     for (unsigned i = 0; i < MAX_PICTURES; i++)
518     {
519         picture_resource_t *res = &p_sys->resource[i];
520
521         if (!res->p->p_pixels)
522             break;
523         PictureResourceFree (res, p_sys->conn);
524     }
525     picture_pool_Delete (p_sys->pool);
526     p_sys->pool = NULL;
527 }