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