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