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