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