]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/x11.c
XCB: follow mouse-events variable
[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 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_("X11"))
57     set_description (N_("X11 video output (XCB)"))
58     set_category (CAT_VIDEO)
59     set_subcategory (SUBCAT_VIDEO_VOUT)
60     set_capability ("vout display", 75)
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_cursor_t cursor; /* blank cursor */
80     xcb_window_t window; /* drawable X window */
81     xcb_gcontext_t gc; /* context to put images */
82     bool shm; /* whether to use MIT-SHM */
83     bool visible; /* whether to draw */
84     uint8_t bpp; /* bits per pixel */
85     uint8_t pad; /* scanline pad */
86     uint8_t depth; /* useful bits per pixel */
87     uint8_t byte_order; /* server byte order */
88
89     picture_pool_t *pool; /* picture pool */
90     picture_resource_t resource[MAX_PICTURES];
91 };
92
93 static picture_t *Get (vout_display_t *);
94 static void Display (vout_display_t *, picture_t *);
95 static int Control (vout_display_t *, int, va_list);
96 static void Manage (vout_display_t *);
97
98 static void ResetPictures (vout_display_t *);
99
100 /**
101  * Probe the X server.
102  */
103 static int Open (vlc_object_t *obj)
104 {
105     vout_display_t *vd = (vout_display_t *)obj;
106     vout_display_sys_t *p_sys = malloc (sizeof (*p_sys));
107     if (p_sys == NULL)
108         return VLC_ENOMEM;
109
110     vd->sys = p_sys;
111     p_sys->pool = NULL;
112
113     /* Connect to X */
114     p_sys->conn = Connect (obj);
115     if (p_sys->conn == NULL)
116     {
117         free (p_sys);
118         return VLC_EGENERIC;
119     }
120
121     /* Get window */
122     const xcb_screen_t *scr;
123     p_sys->embed = GetWindow (vd, p_sys->conn, &scr, &p_sys->shm);
124     if (p_sys->embed == NULL)
125     {
126         xcb_disconnect (p_sys->conn);
127         free (p_sys);
128         return VLC_EGENERIC;
129     }
130
131     const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
132     p_sys->byte_order = setup->image_byte_order;
133
134     /* */
135     video_format_t fmt_pic = vd->fmt;
136
137     /* Determine our video format. */
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                 gray = false;
201                 goto found_vt;
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                 chroma = VLC_CODEC_GREY;
208                 goto found_vt;
209             }
210         }
211         continue; /* The screen does not *really* support this depth */
212
213     found_vt:
214         fmt_pic.i_chroma = chroma;
215         vid = vt->visual_id;
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" (depth: %"PRIu8")", vid,
234              p_sys->depth);
235     msg_Dbg (vd, " %"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     unsigned width, height;
251     if (GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
252         goto error;
253
254     p_sys->window = xcb_generate_id (p_sys->conn);
255     p_sys->gc = xcb_generate_id (p_sys->conn);
256     {
257         const uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_COLORMAP;
258         const uint32_t values[] = {
259             /* XCB_CW_EVENT_MASK */
260             XCB_EVENT_MASK_VISIBILITY_CHANGE,
261             /* XCB_CW_COLORMAP */
262             cmap,
263         };
264         xcb_void_cookie_t c;
265
266         c = xcb_create_window_checked (p_sys->conn, depth, p_sys->window,
267                                        p_sys->embed->handle.xid, 0, 0,
268                                        width, height, 0,
269                                        XCB_WINDOW_CLASS_INPUT_OUTPUT,
270                                        vid, mask, values);
271         xcb_map_window (p_sys->conn, p_sys->window);
272         /* Create graphic context (I wonder why the heck do we need this) */
273         xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
274
275         if (CheckError (vd, p_sys->conn, "cannot create X11 window", c))
276             goto error;
277     }
278     msg_Dbg (vd, "using X11 window %08"PRIx32, p_sys->window);
279     msg_Dbg (vd, "using X11 graphic context %08"PRIx32, p_sys->gc);
280     p_sys->cursor = CreateBlankCursor (p_sys->conn, scr);
281
282     p_sys->visible = false;
283
284     /* */
285     vout_display_info_t info = vd->info;
286     info.has_pictures_invalid = true;
287
288     /* Setup vout_display_t once everything is fine */
289     vd->fmt = fmt_pic;
290     vd->info = info;
291
292     vd->get = Get;
293     vd->prepare = NULL;
294     vd->display = Display;
295     vd->control = Control;
296     vd->manage = Manage;
297
298     /* */
299     vout_display_SendEventFullscreen (vd, false);
300     vout_display_SendEventDisplaySize (vd, width, height, false);
301
302     return VLC_SUCCESS;
303
304 error:
305     Close (obj);
306     return VLC_EGENERIC;
307 }
308
309
310 /**
311  * Disconnect from the X server.
312  */
313 static void Close (vlc_object_t *obj)
314 {
315     vout_display_t *vd = (vout_display_t *)obj;
316     vout_display_sys_t *p_sys = vd->sys;
317
318     ResetPictures (vd);
319     vout_display_DeleteWindow (vd, p_sys->embed);
320     /* colormap, window and context are garbage-collected by X */
321     xcb_disconnect (p_sys->conn);
322     free (p_sys);
323 }
324
325 /**
326  * Return a direct buffer
327  */
328 static picture_t *Get (vout_display_t *vd)
329 {
330     vout_display_sys_t *p_sys = vd->sys;
331
332     if (!p_sys->pool)
333     {
334         vout_display_place_t place;
335
336         vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
337
338         /* */
339         const uint32_t values[] = { place.x, place.y, place.width, place.height };
340         xcb_configure_window (p_sys->conn, p_sys->window,
341                               XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
342                               XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
343                               values);
344
345         picture_t *pic = picture_NewFromFormat (&vd->fmt);
346         if (!pic)
347             return NULL;
348
349         assert (pic->i_planes == 1);
350         memset (p_sys->resource, 0, sizeof(p_sys->resource));
351
352         unsigned count;
353         picture_t *pic_array[MAX_PICTURES];
354         for (count = 0; count < MAX_PICTURES; count++)
355         {
356             picture_resource_t *res = &p_sys->resource[count];
357
358             res->p->i_lines = pic->p->i_lines;
359             res->p->i_pitch = pic->p->i_pitch;
360             if (PictureResourceAlloc (vd, res, res->p->i_pitch * res->p->i_lines,
361                                       p_sys->conn, p_sys->shm))
362                 break;
363             pic_array[count] = picture_NewFromResource (&vd->fmt, res);
364             if (!pic_array[count])
365             {
366                 PictureResourceFree (res, p_sys->conn);
367                 memset (res, 0, sizeof(*res));
368                 break;
369             }
370         }
371         picture_Release (pic);
372
373         if (count == 0)
374             return NULL;
375
376         p_sys->pool = picture_pool_New (count, pic_array);
377         if (!p_sys->pool)
378         {
379             /* TODO release picture resources */
380             return NULL;
381         }
382         /* FIXME should also do it in case of error ? */
383         xcb_flush (p_sys->conn);
384     }
385
386     return picture_pool_Get (p_sys->pool);
387 }
388
389 /**
390  * Sends an image to the X server.
391  */
392 static void Display (vout_display_t *vd, picture_t *pic)
393 {
394     vout_display_sys_t *p_sys = vd->sys;
395     xcb_shm_seg_t segment = pic->p_sys->segment;
396     xcb_void_cookie_t ck;
397
398     if (!p_sys->visible)
399         goto out;
400     if (segment != 0)
401         ck = xcb_shm_put_image_checked (p_sys->conn, p_sys->window, p_sys->gc,
402           /* real width */ pic->p->i_pitch / pic->p->i_pixel_pitch,
403          /* real height */ pic->p->i_lines,
404                    /* x */ vd->fmt.i_x_offset,
405                    /* y */ vd->fmt.i_y_offset,
406                /* width */ vd->fmt.i_visible_width,
407               /* height */ vd->fmt.i_visible_height,
408                            0, 0, p_sys->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
409                            0, segment, 0);
410     else
411     {
412         const size_t offset = vd->fmt.i_y_offset * pic->p->i_pitch;
413         const unsigned lines = pic->p->i_lines - vd->fmt.i_y_offset;
414
415         ck = xcb_put_image_checked (p_sys->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
416                        p_sys->window, p_sys->gc,
417                        pic->p->i_pitch / pic->p->i_pixel_pitch,
418                        lines, -vd->fmt.i_x_offset, 0, 0, p_sys->depth,
419                        pic->p->i_pitch * lines, pic->p->p_pixels + offset);
420     }
421
422     /* Wait for reply. This makes sure that the X server gets CPU time to
423      * display the picture. xcb_flush() is *not* sufficient: especially with
424      * shared memory the PUT requests are so short that many of them can fit in
425      * X11 socket output buffer before the kernel preempts VLC. */
426     xcb_generic_error_t *e = xcb_request_check (p_sys->conn, ck);
427     if (e != NULL)
428     {
429         msg_Dbg (vd, "%s: X11 error %d", "cannot put image", e->error_code);
430         free (e);
431     }
432
433     /* FIXME might be WAY better to wait in some case (be carefull with
434      * VOUT_DISPLAY_RESET_PICTURES if done) + does not work with
435      * vout_display wrapper. */
436 out:
437     picture_Release (pic);
438 }
439
440 static int Control (vout_display_t *vd, int query, va_list ap)
441 {
442     vout_display_sys_t *p_sys = vd->sys;
443
444     switch (query)
445     {
446     case VOUT_DISPLAY_CHANGE_FULLSCREEN:
447     {
448         const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
449         return vout_window_SetFullScreen (p_sys->embed, c->is_fullscreen);
450     }
451
452     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
453     {
454         const vout_display_cfg_t *p_cfg =
455             (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
456         const bool is_forced = (bool)va_arg (ap, int);
457
458         if (is_forced
459          && vout_window_SetSize (p_sys->embed,
460                                  p_cfg->display.width,
461                                  p_cfg->display.height))
462             return VLC_EGENERIC;
463
464         vout_display_place_t place;
465         vout_display_PlacePicture (&place, &vd->source, p_cfg, false);
466
467         if (place.width  != vd->fmt.i_visible_width ||
468             place.height != vd->fmt.i_visible_height)
469         {
470             vout_display_SendEventPicturesInvalid (vd);
471             return VLC_SUCCESS;
472         }
473
474         /* Move the picture within the window */
475         const uint32_t values[] = { place.x, place.y };
476         xcb_configure_window (p_sys->conn, p_sys->window,
477                               XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
478                               values);
479         return VLC_SUCCESS;
480     }
481     case VOUT_DISPLAY_CHANGE_ON_TOP:
482     {
483         int b_on_top = (int)va_arg (ap, int);
484         return vout_window_SetOnTop (p_sys->embed, b_on_top);
485     }
486
487     case VOUT_DISPLAY_CHANGE_ZOOM:
488     case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
489     case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
490     case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
491         /* I am not sure it is always necessary, but it is way simpler ... */
492         vout_display_SendEventPicturesInvalid (vd);
493         return VLC_SUCCESS;
494
495     case VOUT_DISPLAY_RESET_PICTURES:
496     {
497         ResetPictures (vd);
498
499         vout_display_place_t place;
500         vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
501
502         vd->fmt.i_width  = vd->source.i_width  * place.width  / vd->source.i_visible_width;
503         vd->fmt.i_height = vd->source.i_height * place.height / vd->source.i_visible_height;
504
505         vd->fmt.i_visible_width  = place.width;
506         vd->fmt.i_visible_height = place.height;
507         vd->fmt.i_x_offset = vd->source.i_x_offset * place.width  / vd->source.i_visible_width;
508         vd->fmt.i_y_offset = vd->source.i_y_offset * place.height / vd->source.i_visible_height;
509         return VLC_SUCCESS;
510     }
511
512     /* Hide the mouse. It will be send when
513      * vout_display_t::info.b_hide_mouse is false */
514     case VOUT_DISPLAY_HIDE_MOUSE:
515         xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid,
516                                   XCB_CW_CURSOR, &(uint32_t){ p_sys->cursor });
517         return VLC_SUCCESS;
518
519     default:
520         msg_Err (vd, "Unknown request in XCB vout display");
521         return VLC_EGENERIC;
522     }
523 }
524
525 static void Manage (vout_display_t *vd)
526 {
527     vout_display_sys_t *p_sys = vd->sys;
528
529     ManageEvent (vd, p_sys->conn, &p_sys->visible);
530 }
531
532 static void ResetPictures (vout_display_t *vd)
533 {
534     vout_display_sys_t *p_sys = vd->sys;
535
536     if (!p_sys->pool)
537         return;
538
539     for (unsigned i = 0; i < MAX_PICTURES; i++)
540     {
541         picture_resource_t *res = &p_sys->resource[i];
542
543         if (!res->p->p_pixels)
544             break;
545         PictureResourceFree (res, p_sys->conn);
546     }
547     picture_pool_Delete (p_sys->pool);
548     p_sys->pool = NULL;
549 }