]> git.sesse.net Git - ffmpeg/blob - libavdevice/xcbgrab.c
avdevice/xcbgrab: wrap non-shm image replies in a buffer ref
[ffmpeg] / libavdevice / xcbgrab.c
1 /*
2  * XCB input grabber
3  * Copyright (C) 2014 Luca Barbato <lu_zero@gentoo.org>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "config.h"
23
24 #include <stdlib.h>
25 #include <xcb/xcb.h>
26
27 #if CONFIG_LIBXCB_XFIXES
28 #include <xcb/xfixes.h>
29 #endif
30
31 #if CONFIG_LIBXCB_SHM
32 #include <sys/shm.h>
33 #include <xcb/shm.h>
34 #endif
35
36 #if CONFIG_LIBXCB_SHAPE
37 #include <xcb/shape.h>
38 #endif
39
40 #include "libavutil/internal.h"
41 #include "libavutil/mathematics.h"
42 #include "libavutil/opt.h"
43 #include "libavutil/parseutils.h"
44 #include "libavutil/time.h"
45
46 #include "libavformat/avformat.h"
47 #include "libavformat/internal.h"
48
49 typedef struct XCBGrabContext {
50     const AVClass *class;
51
52     uint8_t *buffer;
53
54     xcb_connection_t *conn;
55     xcb_screen_t *screen;
56     xcb_window_t window;
57 #if CONFIG_LIBXCB_SHM
58     xcb_shm_seg_t segment;
59 #endif
60     int64_t time_frame;
61     AVRational time_base;
62
63     int x, y;
64     int width, height;
65     int frame_size;
66     int bpp;
67
68     int draw_mouse;
69     int follow_mouse;
70     int show_region;
71     int region_border;
72     int centered;
73
74     const char *video_size;
75     const char *framerate;
76
77     int has_shm;
78 } XCBGrabContext;
79
80 #define FOLLOW_CENTER -1
81
82 #define OFFSET(x) offsetof(XCBGrabContext, x)
83 #define D AV_OPT_FLAG_DECODING_PARAM
84 static const AVOption options[] = {
85     { "x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
86     { "y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
87     { "grab_x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
88     { "grab_y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
89     { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "vga" }, 0, 0, D },
90     { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc" }, 0, 0, D },
91     { "draw_mouse", "Draw the mouse pointer.", OFFSET(draw_mouse), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, D },
92     { "follow_mouse", "Move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region.",
93       OFFSET(follow_mouse), AV_OPT_TYPE_INT, { .i64 = 0 },  FOLLOW_CENTER, INT_MAX, D, "follow_mouse" },
94     { "centered", "Keep the mouse pointer at the center of grabbing region when following.", 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, D, "follow_mouse" },
95     { "show_region", "Show the grabbing region.", OFFSET(show_region), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D },
96     { "region_border", "Set the region border thickness.", OFFSET(region_border), AV_OPT_TYPE_INT, { .i64 = 3 }, 1, 128, D },
97     { NULL },
98 };
99
100 static const AVClass xcbgrab_class = {
101     .class_name = "xcbgrab indev",
102     .item_name  = av_default_item_name,
103     .option     = options,
104     .version    = LIBAVUTIL_VERSION_INT,
105     .category   = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
106 };
107
108 static int xcbgrab_reposition(AVFormatContext *s,
109                               xcb_query_pointer_reply_t *p,
110                               xcb_get_geometry_reply_t *geo)
111 {
112     XCBGrabContext *c = s->priv_data;
113     int x = c->x, y = c->y;
114     int w = c->width, h = c->height, f = c->follow_mouse;
115     int p_x, p_y;
116
117     if (!p || !geo)
118         return AVERROR(EIO);
119
120     p_x = p->win_x;
121     p_y = p->win_y;
122
123     if (f == FOLLOW_CENTER) {
124         x = p_x - w / 2;
125         y = p_y - h / 2;
126     } else {
127         int left   = x + f;
128         int right  = x + w - f;
129         int top    = y + f;
130         int bottom = y + h - f;
131         if (p_x > right) {
132             x += p_x - right;
133         } else if (p_x < left) {
134             x -= left - p_x;
135         }
136         if (p_y > bottom) {
137             y += p_y - bottom;
138         } else if (p_y < top) {
139             y -= top - p_y;
140         }
141     }
142
143     c->x = FFMIN(FFMAX(0, x), geo->width  - w);
144     c->y = FFMIN(FFMAX(0, y), geo->height - h);
145
146     return 0;
147 }
148
149 static void xcbgrab_image_reply_free(void *opaque, uint8_t *data)
150 {
151     free(opaque);
152 }
153
154 static int xcbgrab_frame(AVFormatContext *s, AVPacket *pkt)
155 {
156     XCBGrabContext *c = s->priv_data;
157     xcb_get_image_cookie_t iq;
158     xcb_get_image_reply_t *img;
159     xcb_drawable_t drawable = c->screen->root;
160     xcb_generic_error_t *e = NULL;
161     uint8_t *data;
162     int length;
163
164     iq  = xcb_get_image(c->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
165                         c->x, c->y, c->width, c->height, ~0);
166
167     img = xcb_get_image_reply(c->conn, iq, &e);
168
169     if (e) {
170         av_log(s, AV_LOG_ERROR,
171                "Cannot get the image data "
172                "event_error: response_type:%u error_code:%u "
173                "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
174                e->response_type, e->error_code,
175                e->sequence, e->resource_id, e->minor_code, e->major_code);
176         free(e);
177         return AVERROR(EACCES);
178     }
179
180     if (!img)
181         return AVERROR(EAGAIN);
182
183     data   = xcb_get_image_data(img);
184     length = xcb_get_image_data_length(img);
185
186     av_init_packet(pkt);
187
188     pkt->buf = av_buffer_create(data, length, xcbgrab_image_reply_free, img, 0);
189     if (!pkt->buf) {
190         free(img);
191         return AVERROR(ENOMEM);
192     }
193
194     pkt->data = data;
195     pkt->size = length;
196
197     return 0;
198 }
199
200 static void wait_frame(AVFormatContext *s, AVPacket *pkt)
201 {
202     XCBGrabContext *c = s->priv_data;
203     int64_t curtime, delay;
204     int64_t frame_time = av_rescale_q(1, c->time_base, AV_TIME_BASE_Q);
205
206     c->time_frame += frame_time;
207
208     for (;;) {
209         curtime = av_gettime();
210         delay   = c->time_frame - curtime;
211         if (delay <= 0)
212             break;
213         av_usleep(delay);
214     }
215
216     pkt->pts = curtime;
217 }
218
219 #if CONFIG_LIBXCB_SHM
220 static int check_shm(xcb_connection_t *conn)
221 {
222     xcb_shm_query_version_cookie_t cookie = xcb_shm_query_version(conn);
223     xcb_shm_query_version_reply_t *reply;
224
225     reply = xcb_shm_query_version_reply(conn, cookie, NULL);
226     if (reply) {
227         free(reply);
228         return 1;
229     }
230
231     return 0;
232 }
233
234 static int allocate_shm(AVFormatContext *s)
235 {
236     XCBGrabContext *c = s->priv_data;
237     int size = c->frame_size + AV_INPUT_BUFFER_PADDING_SIZE;
238     uint8_t *data;
239     int id;
240
241     if (c->buffer)
242         return 0;
243     id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777);
244     if (id == -1) {
245         char errbuf[1024];
246         int err = AVERROR(errno);
247         av_strerror(err, errbuf, sizeof(errbuf));
248         av_log(s, AV_LOG_ERROR, "Cannot get %d bytes of shared memory: %s.\n",
249                size, errbuf);
250         return err;
251     }
252     xcb_shm_attach(c->conn, c->segment, id, 0);
253     data = shmat(id, NULL, 0);
254     shmctl(id, IPC_RMID, 0);
255     if ((intptr_t)data == -1 || !data)
256         return AVERROR(errno);
257     c->buffer = data;
258     return 0;
259 }
260
261 static int xcbgrab_frame_shm(AVFormatContext *s, AVPacket *pkt)
262 {
263     XCBGrabContext *c = s->priv_data;
264     xcb_shm_get_image_cookie_t iq;
265     xcb_shm_get_image_reply_t *img;
266     xcb_drawable_t drawable = c->screen->root;
267     xcb_generic_error_t *e = NULL;
268     int ret;
269
270     ret = allocate_shm(s);
271     if (ret < 0)
272         return ret;
273
274     iq = xcb_shm_get_image(c->conn, drawable,
275                            c->x, c->y, c->width, c->height, ~0,
276                            XCB_IMAGE_FORMAT_Z_PIXMAP, c->segment, 0);
277     img = xcb_shm_get_image_reply(c->conn, iq, &e);
278
279     xcb_flush(c->conn);
280
281     if (e) {
282         av_log(s, AV_LOG_ERROR,
283                "Cannot get the image data "
284                "event_error: response_type:%u error_code:%u "
285                "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
286                e->response_type, e->error_code,
287                e->sequence, e->resource_id, e->minor_code, e->major_code);
288
289         free(e);
290         return AVERROR(EACCES);
291     }
292
293     free(img);
294
295     pkt->data = c->buffer;
296     pkt->size = c->frame_size;
297
298     return 0;
299 }
300 #endif /* CONFIG_LIBXCB_SHM */
301
302 #if CONFIG_LIBXCB_XFIXES
303 static int check_xfixes(xcb_connection_t *conn)
304 {
305     xcb_xfixes_query_version_cookie_t cookie;
306     xcb_xfixes_query_version_reply_t *reply;
307
308     cookie = xcb_xfixes_query_version(conn, XCB_XFIXES_MAJOR_VERSION,
309                                       XCB_XFIXES_MINOR_VERSION);
310     reply  = xcb_xfixes_query_version_reply(conn, cookie, NULL);
311
312     if (reply) {
313         free(reply);
314         return 1;
315     }
316     return 0;
317 }
318
319 #define BLEND(target, source, alpha) \
320     (target) + ((source) * (255 - (alpha)) + 255 / 2) / 255
321
322 static void xcbgrab_draw_mouse(AVFormatContext *s, AVPacket *pkt,
323                                xcb_query_pointer_reply_t *p,
324                                xcb_get_geometry_reply_t *geo)
325 {
326     XCBGrabContext *gr = s->priv_data;
327     uint32_t *cursor;
328     uint8_t *image = pkt->data;
329     int stride     = gr->bpp / 8;
330     xcb_xfixes_get_cursor_image_cookie_t cc;
331     xcb_xfixes_get_cursor_image_reply_t *ci;
332     int cx, cy, x, y, w, h, c_off, i_off;
333
334     cc = xcb_xfixes_get_cursor_image(gr->conn);
335     ci = xcb_xfixes_get_cursor_image_reply(gr->conn, cc, NULL);
336     if (!ci)
337         return;
338
339     cursor = xcb_xfixes_get_cursor_image_cursor_image(ci);
340     if (!cursor)
341         return;
342
343     cx = ci->x - ci->xhot;
344     cy = ci->y - ci->yhot;
345
346     x = FFMAX(cx, gr->x);
347     y = FFMAX(cy, gr->y);
348
349     w = FFMIN(cx + ci->width,  gr->x + gr->width)  - x;
350     h = FFMIN(cy + ci->height, gr->y + gr->height) - y;
351
352     c_off = x - cx;
353     i_off = x - gr->x;
354
355     cursor += (y - cy) * ci->width;
356     image  += (y - gr->y) * gr->width * stride;
357
358     for (y = 0; y < h; y++) {
359         cursor += c_off;
360         image  += i_off * stride;
361         for (x = 0; x < w; x++, cursor++, image += stride) {
362             int r, g, b, a;
363
364             r =  *cursor        & 0xff;
365             g = (*cursor >>  8) & 0xff;
366             b = (*cursor >> 16) & 0xff;
367             a = (*cursor >> 24) & 0xff;
368
369             if (!a)
370                 continue;
371
372             if (a == 255) {
373                 image[0] = r;
374                 image[1] = g;
375                 image[2] = b;
376             } else {
377                 image[0] = BLEND(r, image[0], a);
378                 image[1] = BLEND(g, image[1], a);
379                 image[2] = BLEND(b, image[2], a);
380             }
381
382         }
383         cursor +=  ci->width - w - c_off;
384         image  += (gr->width - w - i_off) * stride;
385     }
386
387     free(ci);
388 }
389 #endif /* CONFIG_LIBXCB_XFIXES */
390
391 static void xcbgrab_update_region(AVFormatContext *s)
392 {
393     XCBGrabContext *c     = s->priv_data;
394     const uint32_t args[] = { c->x - c->region_border,
395                               c->y - c->region_border };
396
397     xcb_configure_window(c->conn,
398                          c->window,
399                          XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
400                          args);
401 }
402
403 static int xcbgrab_read_packet(AVFormatContext *s, AVPacket *pkt)
404 {
405     XCBGrabContext *c = s->priv_data;
406     xcb_query_pointer_cookie_t pc;
407     xcb_get_geometry_cookie_t gc;
408     xcb_query_pointer_reply_t *p  = NULL;
409     xcb_get_geometry_reply_t *geo = NULL;
410     int ret = 0;
411
412     wait_frame(s, pkt);
413
414     if (c->follow_mouse || c->draw_mouse) {
415         pc  = xcb_query_pointer(c->conn, c->screen->root);
416         gc  = xcb_get_geometry(c->conn, c->screen->root);
417         p   = xcb_query_pointer_reply(c->conn, pc, NULL);
418         geo = xcb_get_geometry_reply(c->conn, gc, NULL);
419     }
420
421     if (c->follow_mouse && p->same_screen)
422         xcbgrab_reposition(s, p, geo);
423
424     if (c->show_region)
425         xcbgrab_update_region(s);
426
427 #if CONFIG_LIBXCB_SHM
428     if (c->has_shm && xcbgrab_frame_shm(s, pkt) < 0)
429         c->has_shm = 0;
430 #endif
431     if (!c->has_shm)
432         ret = xcbgrab_frame(s, pkt);
433
434 #if CONFIG_LIBXCB_XFIXES
435     if (ret >= 0 && c->draw_mouse && p->same_screen)
436         xcbgrab_draw_mouse(s, pkt, p, geo);
437 #endif
438
439     free(p);
440     free(geo);
441
442     return ret;
443 }
444
445 static av_cold int xcbgrab_read_close(AVFormatContext *s)
446 {
447     XCBGrabContext *ctx = s->priv_data;
448
449 #if CONFIG_LIBXCB_SHM
450     if (ctx->buffer) {
451         shmdt(ctx->buffer);
452     }
453 #endif
454
455     xcb_disconnect(ctx->conn);
456
457     return 0;
458 }
459
460 static xcb_screen_t *get_screen(const xcb_setup_t *setup, int screen_num)
461 {
462     xcb_screen_iterator_t it = xcb_setup_roots_iterator(setup);
463     xcb_screen_t *screen     = NULL;
464
465     for (; it.rem > 0; xcb_screen_next (&it)) {
466         if (!screen_num) {
467             screen = it.data;
468             break;
469         }
470
471         screen_num--;
472     }
473
474     return screen;
475 }
476
477 static int pixfmt_from_pixmap_format(AVFormatContext *s, int depth,
478                                      int *pix_fmt)
479 {
480     XCBGrabContext *c        = s->priv_data;
481     const xcb_setup_t *setup = xcb_get_setup(c->conn);
482     const xcb_format_t *fmt  = xcb_setup_pixmap_formats(setup);
483     int length               = xcb_setup_pixmap_formats_length(setup);
484
485     *pix_fmt = 0;
486
487     while (length--) {
488         if (fmt->depth == depth) {
489             switch (depth) {
490             case 32:
491                 if (fmt->bits_per_pixel == 32)
492                     *pix_fmt = AV_PIX_FMT_0RGB;
493                 break;
494             case 24:
495                 if (fmt->bits_per_pixel == 32)
496                     *pix_fmt = AV_PIX_FMT_0RGB32;
497                 else if (fmt->bits_per_pixel == 24)
498                     *pix_fmt = AV_PIX_FMT_RGB24;
499                 break;
500             case 16:
501                 if (fmt->bits_per_pixel == 16)
502                     *pix_fmt = AV_PIX_FMT_RGB565;
503                 break;
504             case 15:
505                 if (fmt->bits_per_pixel == 16)
506                     *pix_fmt = AV_PIX_FMT_RGB555;
507                 break;
508             case 8:
509                 if (fmt->bits_per_pixel == 8)
510                     *pix_fmt = AV_PIX_FMT_RGB8;
511                 break;
512             }
513         }
514
515         if (*pix_fmt) {
516             c->bpp        = fmt->bits_per_pixel;
517             c->frame_size = c->width * c->height * fmt->bits_per_pixel / 8;
518             return 0;
519         }
520
521         fmt++;
522     }
523     avpriv_report_missing_feature(s, "Mapping this pixmap format");
524
525     return AVERROR_PATCHWELCOME;
526 }
527
528 static int create_stream(AVFormatContext *s)
529 {
530     XCBGrabContext *c = s->priv_data;
531     AVStream *st      = avformat_new_stream(s, NULL);
532     xcb_get_geometry_cookie_t gc;
533     xcb_get_geometry_reply_t *geo;
534     int ret;
535
536     if (!st)
537         return AVERROR(ENOMEM);
538
539     ret = av_parse_video_size(&c->width, &c->height, c->video_size);
540     if (ret < 0)
541         return ret;
542
543     ret = av_parse_video_rate(&st->avg_frame_rate, c->framerate);
544     if (ret < 0)
545         return ret;
546
547     avpriv_set_pts_info(st, 64, 1, 1000000);
548
549     gc  = xcb_get_geometry(c->conn, c->screen->root);
550     geo = xcb_get_geometry_reply(c->conn, gc, NULL);
551     if (!geo)
552         return AVERROR_EXTERNAL;
553
554     if (c->x + c->width > geo->width ||
555         c->y + c->height > geo->height) {
556         av_log(s, AV_LOG_ERROR,
557                "Capture area %dx%d at position %d.%d "
558                "outside the screen size %dx%d\n",
559                c->width, c->height,
560                c->x, c->y,
561                geo->width, geo->height);
562         free(geo);
563         return AVERROR(EINVAL);
564     }
565
566     c->time_base  = (AVRational){ st->avg_frame_rate.den,
567                                   st->avg_frame_rate.num };
568     c->time_frame = av_gettime();
569
570     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
571     st->codecpar->codec_id   = AV_CODEC_ID_RAWVIDEO;
572     st->codecpar->width      = c->width;
573     st->codecpar->height     = c->height;
574
575     ret = pixfmt_from_pixmap_format(s, geo->depth, &st->codecpar->format);
576
577     free(geo);
578
579     return ret;
580 }
581
582 static void draw_rectangle(AVFormatContext *s)
583 {
584     XCBGrabContext *c = s->priv_data;
585     xcb_gcontext_t gc = xcb_generate_id(c->conn);
586     uint32_t mask     = XCB_GC_FOREGROUND |
587                         XCB_GC_BACKGROUND |
588                         XCB_GC_LINE_WIDTH |
589                         XCB_GC_LINE_STYLE |
590                         XCB_GC_FILL_STYLE;
591     uint32_t values[] = { c->screen->black_pixel,
592                           c->screen->white_pixel,
593                           c->region_border,
594                           XCB_LINE_STYLE_DOUBLE_DASH,
595                           XCB_FILL_STYLE_SOLID };
596     xcb_rectangle_t r = { 1, 1,
597                           c->width  + c->region_border * 2 - 3,
598                           c->height + c->region_border * 2 - 3 };
599
600     xcb_create_gc(c->conn, gc, c->window, mask, values);
601
602     xcb_poly_rectangle(c->conn, c->window, gc, 1, &r);
603 }
604
605 static void setup_window(AVFormatContext *s)
606 {
607     XCBGrabContext *c = s->priv_data;
608     uint32_t mask     = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
609     uint32_t values[] = { 1,
610                           XCB_EVENT_MASK_EXPOSURE |
611                           XCB_EVENT_MASK_STRUCTURE_NOTIFY };
612     av_unused xcb_rectangle_t rect = { 0, 0, c->width, c->height };
613
614     c->window = xcb_generate_id(c->conn);
615
616     xcb_create_window(c->conn, XCB_COPY_FROM_PARENT,
617                       c->window,
618                       c->screen->root,
619                       c->x - c->region_border,
620                       c->y - c->region_border,
621                       c->width + c->region_border * 2,
622                       c->height + c->region_border * 2,
623                       0,
624                       XCB_WINDOW_CLASS_INPUT_OUTPUT,
625                       XCB_COPY_FROM_PARENT,
626                       mask, values);
627
628 #if CONFIG_LIBXCB_SHAPE
629     xcb_shape_rectangles(c->conn, XCB_SHAPE_SO_SUBTRACT,
630                          XCB_SHAPE_SK_BOUNDING, XCB_CLIP_ORDERING_UNSORTED,
631                          c->window,
632                          c->region_border, c->region_border,
633                          1, &rect);
634 #endif
635
636     xcb_map_window(c->conn, c->window);
637
638     draw_rectangle(s);
639 }
640
641 static av_cold int xcbgrab_read_header(AVFormatContext *s)
642 {
643     XCBGrabContext *c = s->priv_data;
644     int screen_num, ret;
645     const xcb_setup_t *setup;
646     char *display_name = av_strdup(s->url);
647
648     if (!display_name)
649         return AVERROR(ENOMEM);
650
651     if (!sscanf(s->url, "%[^+]+%d,%d", display_name, &c->x, &c->y)) {
652         *display_name = 0;
653         sscanf(s->url, "+%d,%d", &c->x, &c->y);
654     }
655
656     c->conn = xcb_connect(display_name[0] ? display_name : NULL, &screen_num);
657     av_freep(&display_name);
658
659     if ((ret = xcb_connection_has_error(c->conn))) {
660         av_log(s, AV_LOG_ERROR, "Cannot open display %s, error %d.\n",
661                s->url[0] ? s->url : "default", ret);
662         return AVERROR(EIO);
663     }
664
665     setup = xcb_get_setup(c->conn);
666
667     c->screen = get_screen(setup, screen_num);
668     if (!c->screen) {
669         av_log(s, AV_LOG_ERROR, "The screen %d does not exist.\n",
670                screen_num);
671         xcbgrab_read_close(s);
672         return AVERROR(EIO);
673     }
674
675     ret = create_stream(s);
676
677     if (ret < 0) {
678         xcbgrab_read_close(s);
679         return ret;
680     }
681
682 #if CONFIG_LIBXCB_SHM
683     if ((c->has_shm = check_shm(c->conn)))
684         c->segment = xcb_generate_id(c->conn);
685 #endif
686
687 #if CONFIG_LIBXCB_XFIXES
688     if (c->draw_mouse) {
689         if (!(c->draw_mouse = check_xfixes(c->conn))) {
690             av_log(s, AV_LOG_WARNING,
691                    "XFixes not available, cannot draw the mouse.\n");
692         }
693         if (c->bpp < 24) {
694             avpriv_report_missing_feature(s, "%d bits per pixel screen",
695                                           c->bpp);
696             c->draw_mouse = 0;
697         }
698     }
699 #endif
700
701     if (c->show_region)
702         setup_window(s);
703
704     return 0;
705 }
706
707 AVInputFormat ff_xcbgrab_demuxer = {
708     .name           = "x11grab",
709     .long_name      = NULL_IF_CONFIG_SMALL("X11 screen capture, using XCB"),
710     .priv_data_size = sizeof(XCBGrabContext),
711     .read_header    = xcbgrab_read_header,
712     .read_packet    = xcbgrab_read_packet,
713     .read_close     = xcbgrab_read_close,
714     .flags          = AVFMT_NOFILE,
715     .priv_class     = &xcbgrab_class,
716 };