]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/x11.c
XCB: get rid of -lxcb_image
[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 <sys/types.h>
31 #include <sys/shm.h>
32
33 #include <xcb/xcb.h>
34 #include <xcb/shm.h>
35
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_vout.h>
39 #include <vlc_window.h>
40
41 #include "xcb_vlc.h"
42
43 #define DISPLAY_TEXT N_("X11 display")
44 #define DISPLAY_LONGTEXT N_( \
45     "X11 hardware display to use. By default VLC will " \
46     "use the value of the DISPLAY environment variable.")
47
48 #define SHM_TEXT N_("Use shared memory")
49 #define SHM_LONGTEXT N_( \
50     "Use shared memory to communicate between VLC and the X server.")
51
52 static int  Open (vlc_object_t *);
53 static void Close (vlc_object_t *);
54
55 /*
56  * Module descriptor
57  */
58 vlc_module_begin ()
59     set_shortname (N_("XCB"))
60     set_description (N_("(Experimental) XCB video output"))
61     set_category (CAT_VIDEO)
62     set_subcategory (SUBCAT_VIDEO_VOUT)
63     set_capability ("video output", 0)
64     set_callbacks (Open, Close)
65
66     add_string ("x11-display", NULL, NULL,
67                 DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
68     add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
69 vlc_module_end ()
70
71 struct vout_sys_t
72 {
73     xcb_connection_t *conn;
74     vout_window_t *embed; /* VLC window (when windowed) */
75
76     xcb_window_t window; /* drawable X window */
77     xcb_gcontext_t gc; /* context to put images */
78     bool shm; /* whether to use MIT-SHM */
79     uint8_t bpp; /* bits per pixel */
80     uint8_t pad; /* scanline pad */
81     uint8_t depth; /* useful bits per pixel */
82     uint8_t byte_order; /* server byte order */
83 };
84
85 static int Init (vout_thread_t *);
86 static void Deinit (vout_thread_t *);
87 static void Display (vout_thread_t *, picture_t *);
88 static int Manage (vout_thread_t *);
89
90 int CheckError (vout_thread_t *vout, const char *str, xcb_void_cookie_t ck)
91 {
92     xcb_generic_error_t *err;
93
94     err = xcb_request_check (vout->p_sys->conn, ck);
95     if (err)
96     {
97         msg_Err (vout, "%s: X11 error %d", str, err->error_code);
98         return VLC_EGENERIC;
99     }
100     return VLC_SUCCESS;
101 }
102
103 #define p_vout vout
104
105 /**
106  * Probe the X server.
107  */
108 static int Open (vlc_object_t *obj)
109 {
110     vout_thread_t *vout = (vout_thread_t *)obj;
111     vout_sys_t *p_sys = malloc (sizeof (*p_sys));
112     if (p_sys == NULL)
113         return VLC_ENOMEM;
114
115     vout->p_sys = p_sys;
116     p_sys->conn = NULL;
117     p_sys->embed = NULL;
118
119     /* Connect to X */
120     char *display = var_CreateGetNonEmptyString (vout, "x11-display");
121     p_sys->conn = xcb_connect (display, NULL);
122     if (xcb_connection_has_error (p_sys->conn) /*== NULL*/)
123     {
124         msg_Err (vout, "cannot connect to X server %s",
125                  display ? display : "");
126         free (display);
127         goto error;
128     }
129     free (display);
130
131     /* Get window */
132     p_sys->embed = vout_RequestXWindow (vout, &(int){ 0 }, &(int){ 0 },
133                                         &(unsigned){ 0 }, &(unsigned){ 0 });
134     if (p_sys->embed == NULL)
135     {
136         msg_Err (vout, "parent window not available");
137         goto error;
138     }
139     xcb_window_t root;
140     {
141         xcb_get_geometry_reply_t *geo;
142         xcb_get_geometry_cookie_t ck;
143
144         ck = xcb_get_geometry (p_sys->conn, p_sys->embed->handle.xid);
145         geo = xcb_get_geometry_reply (p_sys->conn, ck, NULL);
146         if (geo == NULL)
147         {
148             msg_Err (vout, "parent window not valid");
149             goto error;
150         }
151         root = geo->root;
152         free (geo);
153
154         /* Subscribe to parent window resize events */
155         const uint32_t value = XCB_EVENT_MASK_STRUCTURE_NOTIFY;
156         xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid,
157                                       XCB_CW_EVENT_MASK, &value);
158     }
159
160     /* Find the selected screen */
161     const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
162     p_sys->byte_order = setup->image_byte_order;
163
164     xcb_screen_t *scr = NULL;
165     for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
166          i.rem > 0 && scr == NULL; xcb_screen_next (&i))
167     {
168         if (i.data->root == root)
169             scr = i.data;
170     }
171
172     if (scr == NULL)
173     {
174         msg_Err (vout, "parent window screen not found");
175         goto error;
176     }
177     msg_Dbg (vout, "using screen 0x%"PRIx32, scr->root);
178
179     /* Determine our video format. Normally, this is done in pf_init(), but
180      * this plugin always uses the same format for a given X11 screen. */
181     xcb_visualid_t vid = 0;
182     uint8_t depth = 0;
183     bool gray = true;
184     for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
185              *end = fmt + xcb_setup_pixmap_formats_length (setup);
186          fmt < end; fmt++)
187     {
188         vlc_fourcc_t chroma = 0;
189
190         if (fmt->depth < depth)
191             continue; /* We already found a better format! */
192
193         /* Check that the pixmap format is supported by VLC. */
194         switch (fmt->depth)
195         {
196           case 24:
197             if (fmt->bits_per_pixel == 32)
198                 chroma = VLC_FOURCC ('R', 'V', '3', '2');
199             else if (fmt->bits_per_pixel == 24)
200                 chroma = VLC_FOURCC ('R', 'V', '2', '4');
201             else
202                 continue;
203             break;
204           case 16:
205             if (fmt->bits_per_pixel != 16)
206                 continue;
207             chroma = VLC_FOURCC ('R', 'V', '1', '6');
208             break;
209           case 15:
210             if (fmt->bits_per_pixel != 16)
211                 continue;
212             chroma = VLC_FOURCC ('R', 'V', '1', '5');
213             break;
214           case 8:
215             if (fmt->bits_per_pixel != 8)
216                 continue;
217             chroma = VLC_FOURCC ('R', 'G', 'B', '2');
218             break;
219           default:
220             continue;
221         }
222         if ((fmt->bits_per_pixel << 4) % fmt->scanline_pad)
223             continue; /* VLC pads lines to 16 pixels internally */
224
225         /* Byte sex is a non-issue for 8-bits. It can be worked around with
226          * RGB masks for 24-bits. Too bad for 15-bits and 16-bits. */
227 #ifdef WORDS_BIGENDIAN
228 # define ORDER XCB_IMAGE_ORDER_MSB_FIRST
229 #else
230 # define ORDER XCB_IMAGE_ORDER_LSB_FIRST
231 #endif
232         if (fmt->bits_per_pixel == 16 && setup->image_byte_order != ORDER)
233             continue;
234
235         /* Check that the selected screen supports this depth */
236         xcb_depth_iterator_t it = xcb_screen_allowed_depths_iterator (scr);
237         while (it.rem > 0 && it.data->depth != fmt->depth)
238              xcb_depth_next (&it);
239         if (!it.rem)
240             continue; /* Depth not supported on this screen */
241
242         /* Find a visual type for the selected depth */
243         const xcb_visualtype_t *vt = xcb_depth_visuals (it.data);
244         for (int i = xcb_depth_visuals_length (it.data); i > 0; i--)
245         {
246             if (vt->_class == XCB_VISUAL_CLASS_TRUE_COLOR)
247             {
248                 vid = vt->visual_id;
249                 gray = false;
250                 break;
251             }
252             if (fmt->depth == 8 && vt->_class == XCB_VISUAL_CLASS_STATIC_GRAY)
253             {
254                 if (!gray)
255                     continue; /* Prefer color over gray scale */
256                 vid = vt->visual_id;
257                 chroma = VLC_FOURCC ('G', 'R', 'E', 'Y');
258             }
259         }
260
261         if (!vid)
262             continue; /* The screen does not *really* support this depth */
263
264         vout->fmt_out.i_chroma = vout->output.i_chroma = chroma;
265         if (!gray)
266         {
267             vout->output.i_rmask = vt->red_mask;
268             vout->output.i_gmask = vt->green_mask;
269             vout->output.i_bmask = vt->blue_mask;
270         }
271         p_sys->bpp = fmt->bits_per_pixel;
272         p_sys->pad = fmt->scanline_pad;
273         p_sys->depth = depth = fmt->depth;
274     }
275
276     if (depth == 0)
277     {
278         msg_Err (vout, "no supported pixmap formats or visual types");
279         goto error;
280     }
281
282     msg_Dbg (vout, "using X11 visual ID 0x%"PRIx32, vid);
283     msg_Dbg (vout, " %"PRIu8" bits per pixels, %"PRIu8" bits line pad",
284              p_sys->bpp, p_sys->pad);
285
286     /* Create colormap (needed to select non-default visual) */
287     xcb_colormap_t cmap;
288     if (vid != scr->root_visual)
289     {
290         cmap = xcb_generate_id (p_sys->conn);
291         xcb_create_colormap (p_sys->conn, XCB_COLORMAP_ALLOC_NONE,
292                              cmap, scr->root, vid);
293     }
294     else
295         cmap = scr->default_colormap;
296
297     /* Create window */
298     {
299         const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK
300                             | XCB_CW_COLORMAP;
301         const uint32_t values[] = {
302             /* XCB_CW_BACK_PIXEL */
303             scr->black_pixel,
304             /* XCB_CW_EVENT_MASK */
305             XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
306             XCB_EVENT_MASK_POINTER_MOTION,
307             /* XCB_CW_COLORMAP */
308             cmap,
309         };
310         xcb_void_cookie_t c;
311         xcb_window_t window = xcb_generate_id (p_sys->conn);
312
313         c = xcb_create_window_checked (p_sys->conn, depth, window,
314                                        p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
315                                        XCB_WINDOW_CLASS_INPUT_OUTPUT,
316                                        vid, mask, values);
317         if (CheckError (vout, "cannot create X11 window", c))
318             goto error;
319         p_sys->window = window;
320         msg_Dbg (vout, "using X11 window %08"PRIx32, p_sys->window);
321         xcb_map_window (p_sys->conn, window);
322     }
323
324     /* Create graphic context (I wonder why the heck do we need this) */
325     p_sys->gc = xcb_generate_id (p_sys->conn);
326     xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
327     msg_Dbg (vout, "using X11 graphic context %08"PRIx32, p_sys->gc);
328
329     /* Check shared memory support */
330     p_sys->shm = var_CreateGetBool (vout, "x11-shm") > 0;
331     if (p_sys->shm)
332     {
333         xcb_shm_query_version_cookie_t ck;
334         xcb_shm_query_version_reply_t *r;
335
336         ck = xcb_shm_query_version (p_sys->conn);
337         r = xcb_shm_query_version_reply (p_sys->conn, ck, NULL);
338         if (!r)
339         {
340             msg_Err (vout, "shared memory (MIT-SHM) not available");
341             msg_Warn (vout, "display will be slow");
342             p_sys->shm = false;
343         }
344         free (r);
345     }
346
347     vout->pf_init = Init;
348     vout->pf_end = Deinit;
349     vout->pf_display = Display;
350     vout->pf_manage = Manage;
351     return VLC_SUCCESS;
352
353 error:
354     Close (obj);
355     return VLC_EGENERIC;
356 }
357
358
359 /**
360  * Disconnect from the X server.
361  */
362 static void Close (vlc_object_t *obj)
363 {
364     vout_thread_t *vout = (vout_thread_t *)obj;
365     vout_sys_t *p_sys = vout->p_sys;
366
367     if (p_sys->embed)
368         vout_ReleaseWindow (p_sys->embed);
369     /* colormap and window are garbage-collected by X */
370     if (p_sys->conn)
371         xcb_disconnect (p_sys->conn);
372     free (p_sys);
373 }
374
375 struct picture_sys_t
376 {
377     xcb_connection_t *conn; /* Shared connection to X server */
378     xcb_shm_seg_t segment; /* Shared memory segment X ID */
379 };
380
381 #define SHM_ERR ((void *)(intptr_t)(-1))
382
383 static int PictureInit (vout_thread_t *vout, picture_t *pic)
384 {
385     vout_sys_t *p_sys = vout->p_sys;
386     picture_sys_t *priv = malloc (sizeof (*p_sys));
387
388     if (priv == NULL)
389         return VLC_ENOMEM;
390
391     assert (pic->i_status == FREE_PICTURE);
392     vout_InitPicture (vout, pic, vout->output.i_chroma,
393                       vout->output.i_width, vout->output.i_height,
394                       vout->output.i_aspect);
395
396     void *shm = SHM_ERR;
397     const size_t size = pic->p->i_pitch * pic->p->i_lines;
398
399     /* Allocate shared memory segment */
400     int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0700);
401     if (id == -1)
402     {
403         msg_Err (vout, "shared memory allocation error: %m");
404         goto error;
405     }
406
407     /* Attach the segment to VLC */
408     shm = shmat (id, NULL, 0 /* read/write */);
409     if (shm == SHM_ERR)
410     {
411         msg_Err (vout, "shared memory attachment error: %m");
412         shmctl (id, IPC_RMID, 0);
413         goto error;
414     }
415
416     if (p_sys->shm)
417     {
418         /* Attach the segment to X */
419         xcb_void_cookie_t ck;
420         priv->segment = xcb_generate_id (p_sys->conn);
421         ck = xcb_shm_attach_checked (p_sys->conn, priv->segment, id, 1);
422
423         if (CheckError (vout, "shared memory server-side error", ck))
424         {
425             msg_Info (vout, "using buggy X11 server - SSH proxying?");
426             priv->segment = 0;
427         }
428     }
429     else
430         priv->segment = 0;
431
432     shmctl (id, IPC_RMID, 0);
433     priv->conn = p_sys->conn;
434     pic->p_sys = priv;
435     pic->p->p_pixels = shm;
436     pic->i_status = DESTROYED_PICTURE;
437     pic->i_type = DIRECT_PICTURE;
438     return VLC_SUCCESS;
439
440 error:
441     free (priv);
442     return VLC_EGENERIC;
443 }
444
445
446 /**
447  * Release picture private data
448  */
449 static void PictureDeinit (picture_t *pic)
450 {
451     struct picture_sys_t *p_sys = pic->p_sys;
452
453     if (p_sys->segment != 0)
454     {
455         xcb_shm_detach (p_sys->conn, p_sys->segment);
456         shmdt (pic->p->p_pixels);
457     }
458     free (p_sys);
459 }
460
461 static void get_window_size (xcb_connection_t *conn, xcb_window_t win,
462                              unsigned *width, unsigned *height)
463 {
464     xcb_get_geometry_cookie_t ck = xcb_get_geometry (conn, win);
465     xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply (conn, ck, NULL);
466
467     if (geo)
468     {
469         *width = geo->width;
470         *height = geo->height;
471         free (geo);
472     }
473     else
474         *width = *height = 0;
475 }
476
477 /**
478  * Allocate drawable window and picture buffers.
479  */
480 static int Init (vout_thread_t *vout)
481 {
482     vout_sys_t *p_sys = vout->p_sys;
483     unsigned x, y, width, height;
484
485     get_window_size (p_sys->conn, p_sys->embed->handle.xid, &width, &height);
486     vout_PlacePicture (vout, width, height, &x, &y, &width, &height);
487
488     const uint32_t values[] = { x, y, width, height, };
489     xcb_configure_window (p_sys->conn, p_sys->window,
490                           XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
491                           XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
492                           values);
493
494     /* FIXME: I don't get the subtlety between output and fmt_out here */
495     vout->fmt_out.i_visible_width = width;
496     vout->fmt_out.i_visible_height = height;
497     vout->fmt_out.i_sar_num = vout->fmt_out.i_sar_den = 1;
498
499     vout->output.i_width = vout->fmt_out.i_width =
500         width * vout->fmt_in.i_width / vout->fmt_in.i_visible_width;
501     vout->output.i_height = vout->fmt_out.i_height =
502         height * vout->fmt_in.i_height / vout->fmt_in.i_visible_height;
503     vout->fmt_out.i_x_offset =
504         width * vout->fmt_in.i_x_offset / vout->fmt_in.i_visible_width;
505     p_vout->fmt_out.i_y_offset =
506         height * vout->fmt_in.i_y_offset / vout->fmt_in.i_visible_height;
507
508     assert (height > 0);
509     vout->output.i_aspect = vout->fmt_out.i_aspect =
510         width * VOUT_ASPECT_FACTOR / height;
511
512     /* Allocate picture buffers */
513     I_OUTPUTPICTURES = 0;
514     for (size_t index = 0; I_OUTPUTPICTURES < 2; index++)
515     {
516         picture_t *pic = vout->p_picture + index;
517
518         if (index > sizeof (vout->p_picture) / sizeof (pic))
519             break;
520         if (pic->i_status != FREE_PICTURE)
521             continue;
522         if (PictureInit (vout, pic))
523             break;
524         PP_OUTPUTPICTURE[I_OUTPUTPICTURES++] = pic;
525     }
526     xcb_flush (p_sys->conn);
527     return VLC_SUCCESS;
528 }
529
530 /**
531  * Free picture buffers.
532  */
533 static void Deinit (vout_thread_t *vout)
534 {
535     for (int i = 0; i < I_OUTPUTPICTURES; i++)
536         PictureDeinit (PP_OUTPUTPICTURE[i]);
537 }
538
539 /**
540  * Sends an image to the X server.
541  */
542 static void Display (vout_thread_t *vout, picture_t *pic)
543 {
544     vout_sys_t *p_sys = vout->p_sys;
545     picture_sys_t *priv = pic->p_sys;
546
547     if (priv->segment)
548         xcb_shm_put_image (p_sys->conn, p_sys->window, p_sys->gc,
549           /* real width */ pic->p->i_pitch / pic->p->i_pixel_pitch,
550          /* real height */ pic->p->i_lines, /* x */ 0, /* y */ 0,
551                /* width */ pic->p->i_visible_pitch / pic->p->i_pixel_pitch,
552               /* height */ pic->p->i_visible_lines, /* x */ 0, /* y */ 0,
553                            p_sys->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
554                            0, priv->segment, 0);
555     else
556         xcb_put_image (p_sys->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
557                        p_sys->window, p_sys->gc,
558                        pic->p->i_pitch / pic->p->i_pixel_pitch,
559                        pic->p->i_lines, 0, 0, 0, p_sys->depth,
560                        pic->p->i_pitch * pic->p->i_lines, pic->p->p_pixels);
561     xcb_flush (p_sys->conn);
562 }
563
564 /**
565  * Process incoming X events.
566  */
567 static int Manage (vout_thread_t *vout)
568 {
569     vout_sys_t *p_sys = vout->p_sys;
570     xcb_generic_event_t *ev;
571
572     while ((ev = xcb_poll_for_event (p_sys->conn)) != NULL)
573         ProcessEvent (vout, p_sys->conn, p_sys->window, ev);
574
575     if (xcb_connection_has_error (p_sys->conn))
576     {
577         msg_Err (vout, "X server failure");
578         return VLC_EGENERIC;
579     }
580     return VLC_SUCCESS;
581 }