]> git.sesse.net Git - vlc/blob - modules/access/screen/xcb.c
Dead code
[vlc] / modules / access / screen / xcb.c
1 /**
2  * @file xcb.c
3  * @brief X11 C Bindings screen capture demux 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 Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1
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 #include <stdarg.h>
27 #include <assert.h>
28 #include <xcb/xcb.h>
29 #include <xcb/composite.h>
30 #include <vlc_common.h>
31 #include <vlc_demux.h>
32 #include <vlc_plugin.h>
33
34 #define CACHING_TEXT N_("Caching value in ms")
35 #define CACHING_LONGTEXT N_( \
36     "Caching value for screen capture. " \
37     "This value should be set in milliseconds.")
38
39 #define FPS_TEXT N_("Frame rate")
40 #define FPS_LONGTEXT N_( \
41     "How many times the screen content should be refreshed per second.")
42
43 #define LEFT_TEXT N_("Region left column")
44 #define LEFT_LONGTEXT N_( \
45     "Abscissa of the capture reion in pixels.")
46
47 #define TOP_TEXT N_("Region top row")
48 #define TOP_LONGTEXT N_( \
49     "Ordinate of the capture region in pixels.")
50
51 #define WIDTH_TEXT N_("Capture region width")
52 #define WIDTH_LONGTEXT N_( \
53     "Pixel width of the capture region, or 0 for full width")
54
55 #define HEIGHT_TEXT N_("Capture region height")
56 #define HEIGHT_LONGTEXT N_( \
57     "Pixel height of the capture region, or 0 for full height")
58
59 #define FOLLOW_MOUSE_TEXT N_( "Follow the mouse" )
60 #define FOLLOW_MOUSE_LONGTEXT N_( \
61     "Follow the mouse when capturing a subscreen." )
62
63 static int  Open (vlc_object_t *);
64 static void Close (vlc_object_t *);
65
66 /*
67  * Module descriptor
68  */
69 vlc_module_begin ()
70     set_shortname (N_("Screen"))
71     set_description (N_("Screen capture (with X11/XCB)"))
72     set_category (CAT_INPUT)
73     set_subcategory (SUBCAT_INPUT_ACCESS)
74     set_capability ("access_demux", 0)
75     set_callbacks (Open, Close)
76
77     add_integer ("screen-caching", DEFAULT_PTS_DELAY * 1000 / CLOCK_FREQ,
78                  CACHING_TEXT, CACHING_LONGTEXT, true)
79     add_float ("screen-fps", 2.0, FPS_TEXT, FPS_LONGTEXT, true)
80     add_integer ("screen-left", 0, LEFT_TEXT, LEFT_LONGTEXT, true)
81         change_integer_range (-32768, 32767)
82         change_safe ()
83     add_integer ("screen-top", 0, TOP_TEXT, TOP_LONGTEXT, true)
84         change_integer_range (-32768, 32767)
85         change_safe ()
86     add_integer ("screen-width", 0, WIDTH_TEXT, WIDTH_LONGTEXT, true)
87         change_integer_range (0, 65535)
88         change_safe ()
89     add_integer ("screen-height", 0, HEIGHT_TEXT, HEIGHT_LONGTEXT, true)
90         change_integer_range (0, 65535)
91         change_safe ()
92     add_bool ("screen-follow-mouse", false, FOLLOW_MOUSE_TEXT,
93               FOLLOW_MOUSE_LONGTEXT, true)
94
95     add_shortcut ("screen", "window")
96 vlc_module_end ()
97
98 /*
99  * Local prototypes
100  */
101 static void Demux (void *);
102 static int Control (demux_t *, int, va_list);
103 static es_out_id_t *InitES (demux_t *, uint_fast16_t, uint_fast16_t,
104                             uint_fast8_t);
105
106 struct demux_sys_t
107 {
108     /* All owned by timer thread while timer is armed: */
109     xcb_connection_t *conn;
110     es_out_id_t      *es;
111     mtime_t           pts, interval;
112     float             rate;
113     xcb_window_t      window;
114     xcb_pixmap_t      pixmap;
115     int16_t           x, y;
116     uint16_t          w, h;
117     uint16_t          cur_w, cur_h;
118     bool              follow_mouse;
119     /* Timer does not use this, only input thread: */
120     vlc_timer_t       timer;
121 };
122
123 /**
124  * Probes and initializes.
125  */
126 static int Open (vlc_object_t *obj)
127 {
128     demux_t *demux = (demux_t *)obj;
129     demux_sys_t *p_sys = malloc (sizeof (*p_sys));
130
131     if (p_sys == NULL)
132         return VLC_ENOMEM;
133     demux->p_sys = p_sys;
134
135     /* Connect to X server */
136     char *display = var_InheritString (obj, "x11-display");
137     int snum;
138     xcb_connection_t *conn = xcb_connect (display, &snum);
139     free (display);
140     if (xcb_connection_has_error (conn))
141     {
142         free (p_sys);
143         return VLC_EGENERIC;
144     }
145     p_sys->conn = conn;
146
147    /* Find configured screen */
148     if (!strcmp (demux->psz_access, "screen"))
149     {
150         const xcb_setup_t *setup = xcb_get_setup (conn);
151         const xcb_screen_t *scr = NULL;
152         for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
153              i.rem > 0; xcb_screen_next (&i))
154         {
155             if (snum == 0)
156             {
157                scr = i.data;
158                 break;
159             }
160             snum--;
161         }
162         if (scr == NULL)
163         {
164             msg_Err (obj, "bad X11 screen number");
165             goto error;
166         }
167         p_sys->window = scr->root;
168     }
169     else
170     /* Determine capture window */
171     if (!strcmp (demux->psz_access, "window"))
172     {
173         char *end;
174         unsigned long ul = strtoul (demux->psz_location, &end, 0);
175         if (*end || ul > 0xffffffff)
176         {
177             msg_Err (obj, "bad X11 drawable %s", demux->psz_location);
178             goto error;
179         }
180         p_sys->window = ul;
181
182         xcb_composite_query_version_reply_t *r =
183             xcb_composite_query_version_reply (conn,
184                 xcb_composite_query_version (conn, 0, 4), NULL);
185         if (r == NULL || r->minor_version < 2)
186         {
187             msg_Err (obj, "X Composite extension not available");
188             free (r);
189             goto error;
190         }
191         msg_Dbg (obj, "using Composite extension v%"PRIu32".%"PRIu32,
192                  r->major_version, r->minor_version);
193         free (r);
194
195         xcb_composite_redirect_window (conn, p_sys->window,
196                                        XCB_COMPOSITE_REDIRECT_AUTOMATIC);
197     }
198     else
199         goto error;
200
201     /* Window properties */
202     p_sys->pixmap = xcb_generate_id (conn);
203     p_sys->x = var_InheritInteger (obj, "screen-left");
204     p_sys->y = var_InheritInteger (obj, "screen-top");
205     p_sys->w = var_InheritInteger (obj, "screen-width");
206     p_sys->h = var_InheritInteger (obj, "screen-height");
207     p_sys->follow_mouse = var_InheritBool (obj, "screen-follow-mouse");
208
209     /* Initializes format */
210     p_sys->rate = var_InheritFloat (obj, "screen-fps");
211     if (!p_sys->rate)
212         goto error;
213     p_sys->interval = (float)CLOCK_FREQ / p_sys->rate;
214     if (!p_sys->interval)
215         goto error;
216     var_Create (obj, "screen-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
217
218     p_sys->cur_w = 0;
219     p_sys->cur_h = 0;
220     p_sys->es = NULL;
221     p_sys->pts = VLC_TS_INVALID;
222     if (vlc_timer_create (&p_sys->timer, Demux, demux))
223         goto error;
224     vlc_timer_schedule (p_sys->timer, false, 1, p_sys->interval);
225
226     /* Initializes demux */
227     demux->pf_demux   = NULL;
228     demux->pf_control = Control;
229     return VLC_SUCCESS;
230
231 error:
232     xcb_disconnect (p_sys->conn);
233     free (p_sys);
234     return VLC_EGENERIC;
235 }
236
237
238 /**
239  * Releases resources
240  */
241 static void Close (vlc_object_t *obj)
242 {
243     demux_t *demux = (demux_t *)obj;
244     demux_sys_t *p_sys = demux->p_sys;
245
246     vlc_timer_destroy (p_sys->timer);
247     xcb_disconnect (p_sys->conn);
248     free (p_sys);
249 }
250
251
252 /**
253  * Control callback
254  */
255 static int Control (demux_t *demux, int query, va_list args)
256 {
257     switch (query)
258     {
259         case DEMUX_GET_POSITION:
260         {
261             float *v = va_arg (args, float *);
262             *v = 0.;
263             return VLC_SUCCESS;
264         }
265
266         case DEMUX_GET_LENGTH:
267         case DEMUX_GET_TIME:
268         {
269             int64_t *v = va_arg (args, int64_t *);
270             *v = 0;
271             return VLC_SUCCESS;
272         }
273
274         /* TODO: get title info -> crawl visible windows */
275
276         case DEMUX_GET_PTS_DELAY:
277         {
278             int64_t *v = va_arg (args, int64_t *);
279             *v = var_GetInteger (demux, "screen-caching") * UINT64_C(1000);
280             return VLC_SUCCESS;
281         }
282
283         case DEMUX_CAN_PAUSE:
284         case DEMUX_CAN_CONTROL_PACE:
285         case DEMUX_CAN_CONTROL_RATE:
286         case DEMUX_CAN_SEEK:
287         {
288             bool *v = (bool*)va_arg( args, bool * );
289             *v = false;
290             return VLC_SUCCESS;
291         }
292
293         case DEMUX_SET_PAUSE_STATE:
294             return VLC_SUCCESS; /* should not happen */
295     }
296
297     return VLC_EGENERIC;
298 }
299
300
301 /**
302  * Processing callback
303  */
304 static void Demux (void *data)
305 {
306     demux_t *demux = data;
307     demux_sys_t *sys = demux->p_sys;
308     xcb_connection_t *conn = sys->conn;
309
310     /* Determine capture region */
311     xcb_get_geometry_cookie_t gc;
312     xcb_query_pointer_cookie_t qc;
313
314     gc = xcb_get_geometry (conn, sys->window);
315     if (sys->follow_mouse)
316         qc = xcb_query_pointer (conn, sys->window);
317
318     xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply (conn, gc, NULL);
319     if (geo == NULL)
320     {
321         msg_Err (demux, "bad X11 drawable 0x%08"PRIx32, sys->window);
322         if (sys->follow_mouse)
323             xcb_discard_reply (conn, gc.sequence);
324         return;
325     }
326
327     /* FIXME: review for signed overflow */
328     int16_t x = sys->x, y = sys->y;
329     uint16_t w = sys->w, h = sys->h;
330
331     uint16_t ow = geo->width - sys->x;
332     uint16_t oh = geo->height - sys->y;
333     if (w == 0 || w > ow)
334         w = ow;
335     if (h == 0 || h > oh)
336         h = oh;
337
338     if (sys->follow_mouse)
339     {
340         xcb_query_pointer_reply_t *ptr =
341             xcb_query_pointer_reply (conn, qc, NULL);
342         if (ptr != NULL)
343         {
344             int16_t min_x = x + (w / 2);
345             int16_t min_y = y + (h / 2);
346             int16_t max_x = x + ow - ((w + 1) / 2);
347             int16_t max_y = y + oh - ((h + 1) / 2);
348
349             assert (max_x >= min_x); /* max_x - min_x = ow - w >= 0 */
350             if (ptr->root_x > max_x)
351                 x += ow - w;
352             else if (ptr->root_x > min_x)
353                 x += ptr->root_x - min_x;
354
355             assert (max_y >= min_y);
356             if (ptr->root_y > max_y)
357                 y += oh - h;
358             else if (ptr->root_y > min_y)
359                 y += ptr->root_y - min_y;
360             free (ptr);
361         }
362     }
363
364     /* Update elementary stream format (if needed) */
365     if (w != sys->cur_w || h != sys->cur_h)
366     {
367         if (sys->es != NULL)
368             es_out_Del (demux->out, sys->es);
369
370         /* Update composite pixmap */
371         if (sys->window != geo->root)
372         {
373             xcb_free_pixmap (conn, sys->pixmap); /* no-op first time */
374             xcb_composite_name_window_pixmap (conn, sys->window, sys->pixmap);
375             xcb_create_pixmap (conn, geo->depth, sys->pixmap,
376                                geo->root, geo->width, geo->height);
377         }
378
379         sys->es = InitES (demux, w, h, geo->depth);
380         if (sys->es != NULL)
381         {
382             sys->cur_w = w;
383             sys->cur_h = h;
384         }
385     }
386
387     /* Capture screen */
388     xcb_drawable_t drawable =
389         (sys->window != geo->root) ? sys->pixmap : sys->window;
390     free (geo);
391
392     xcb_get_image_reply_t *img;
393     img = xcb_get_image_reply (conn,
394         xcb_get_image (conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
395                        x, y, w, h, ~0), NULL);
396     if (img == NULL)
397         return;
398
399     block_t *block = block_heap_Alloc (img, xcb_get_image_data (img),
400                                        xcb_get_image_data_length (img));
401     if (block == NULL)
402         return;
403
404     /* Send block - zero copy */
405     if (sys->es != NULL)
406     {
407         if (sys->pts == VLC_TS_INVALID)
408             sys->pts = mdate ();
409         block->i_pts = block->i_dts = sys->pts;
410
411         es_out_Control (demux->out, ES_OUT_SET_PCR, sys->pts);
412         es_out_Send (demux->out, sys->es, block);
413         sys->pts += sys->interval;
414     }
415 }
416
417 static es_out_id_t *InitES (demux_t *demux, uint_fast16_t width,
418                             uint_fast16_t height, uint_fast8_t depth)
419 {
420     demux_sys_t *p_sys = demux->p_sys;
421     const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
422     uint32_t chroma = 0;
423     uint8_t bpp;
424
425     for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
426              *end = fmt + xcb_setup_pixmap_formats_length (setup);
427          fmt < end; fmt++)
428     {
429         if (fmt->depth != depth)
430             continue;
431         bpp = fmt->depth;
432         switch (fmt->depth)
433         {
434             case 32:
435                 if (fmt->bits_per_pixel == 32)
436                     chroma = VLC_CODEC_RGBA;
437                 break;
438             case 24:
439                 if (fmt->bits_per_pixel == 32)
440                 {
441                     chroma = VLC_CODEC_RGB32;
442                     bpp = 32;
443                 }
444                 else if (fmt->bits_per_pixel == 24)
445                     chroma = VLC_CODEC_RGB24;
446                 break;
447             case 16:
448                 if (fmt->bits_per_pixel == 16)
449                     chroma = VLC_CODEC_RGB16;
450                 break;
451             case 15:
452                 if (fmt->bits_per_pixel == 16)
453                     chroma = VLC_CODEC_RGB15;
454                 break;
455             case 8: /* XXX: screw grey scale! */
456                 if (fmt->bits_per_pixel == 8)
457                     chroma = VLC_CODEC_RGB8;
458                 break;
459         }
460         if (chroma != 0)
461             break;
462     }
463
464     if (!chroma)
465     {
466         msg_Err (demux, "unsupported pixmap formats");
467         return NULL;
468     }
469
470     es_format_t fmt;
471
472     es_format_Init (&fmt, VIDEO_ES, chroma);
473     fmt.video.i_chroma = chroma;
474     fmt.video.i_bits_per_pixel = bpp;
475     fmt.video.i_sar_num = fmt.video.i_sar_den = 1;
476     fmt.video.i_frame_rate = 1000 * p_sys->rate;
477     fmt.video.i_frame_rate_base = 1000;
478     fmt.video.i_visible_width = fmt.video.i_width = width;
479     fmt.video.i_visible_height = fmt.video.i_height = height;
480
481     return es_out_Add (demux->out, &fmt);
482 }