]> git.sesse.net Git - ffmpeg/blob - libavdevice/xcbgrab.c
avdevice/xcbgrab: Handle reply and error properly
[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 int xcbgrab_frame(AVFormatContext *s, AVPacket *pkt)
150 {
151     XCBGrabContext *c = s->priv_data;
152     xcb_get_image_cookie_t iq;
153     xcb_get_image_reply_t *img;
154     xcb_drawable_t drawable = c->screen->root;
155     xcb_generic_error_t *e = NULL;
156     uint8_t *data;
157     int length, ret;
158
159     iq  = xcb_get_image(c->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
160                         c->x, c->y, c->width, c->height, ~0);
161
162     img = xcb_get_image_reply(c->conn, iq, &e);
163
164     if (e) {
165         av_log(s, AV_LOG_ERROR,
166                "Cannot get the image data "
167                "event_error: response_type:%u error_code:%u "
168                "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
169                e->response_type, e->error_code,
170                e->sequence, e->resource_id, e->minor_code, e->major_code);
171         free(e);
172         return AVERROR(EACCES);
173     }
174
175     if (!img)
176         return AVERROR(EAGAIN);
177
178     data   = xcb_get_image_data(img);
179     length = xcb_get_image_data_length(img);
180
181     ret = av_new_packet(pkt, length);
182
183     if (!ret)
184         memcpy(pkt->data, data, length);
185
186     free(img);
187
188     return ret;
189 }
190
191 static void wait_frame(AVFormatContext *s, AVPacket *pkt)
192 {
193     XCBGrabContext *c = s->priv_data;
194     int64_t curtime, delay;
195     int64_t frame_time = av_rescale_q(1, c->time_base, AV_TIME_BASE_Q);
196
197     c->time_frame += frame_time;
198
199     for (;;) {
200         curtime = av_gettime();
201         delay   = c->time_frame - curtime;
202         if (delay <= 0)
203             break;
204         av_usleep(delay);
205     }
206
207     pkt->pts = curtime;
208 }
209
210 #if CONFIG_LIBXCB_SHM
211 static int check_shm(xcb_connection_t *conn)
212 {
213     xcb_shm_query_version_cookie_t cookie = xcb_shm_query_version(conn);
214     xcb_shm_query_version_reply_t *reply;
215
216     reply = xcb_shm_query_version_reply(conn, cookie, NULL);
217     if (reply) {
218         free(reply);
219         return 1;
220     }
221
222     return 0;
223 }
224
225 static int allocate_shm(AVFormatContext *s)
226 {
227     XCBGrabContext *c = s->priv_data;
228     int size = c->frame_size + AV_INPUT_BUFFER_PADDING_SIZE;
229     uint8_t *data;
230     int id;
231
232     if (c->buffer)
233         return 0;
234     id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777);
235     if (id == -1) {
236         char errbuf[1024];
237         int err = AVERROR(errno);
238         av_strerror(err, errbuf, sizeof(errbuf));
239         av_log(s, AV_LOG_ERROR, "Cannot get %d bytes of shared memory: %s.\n",
240                size, errbuf);
241         return err;
242     }
243     xcb_shm_attach(c->conn, c->segment, id, 0);
244     data = shmat(id, NULL, 0);
245     shmctl(id, IPC_RMID, 0);
246     if ((intptr_t)data == -1 || !data)
247         return AVERROR(errno);
248     c->buffer = data;
249     return 0;
250 }
251
252 static int xcbgrab_frame_shm(AVFormatContext *s, AVPacket *pkt)
253 {
254     XCBGrabContext *c = s->priv_data;
255     xcb_shm_get_image_cookie_t iq;
256     xcb_shm_get_image_reply_t *img;
257     xcb_drawable_t drawable = c->screen->root;
258     xcb_generic_error_t *e = NULL;
259     int ret;
260
261     ret = allocate_shm(s);
262     if (ret < 0)
263         return ret;
264
265     iq = xcb_shm_get_image(c->conn, drawable,
266                            c->x, c->y, c->width, c->height, ~0,
267                            XCB_IMAGE_FORMAT_Z_PIXMAP, c->segment, 0);
268     img = xcb_shm_get_image_reply(c->conn, iq, &e);
269
270     xcb_flush(c->conn);
271
272     if (e) {
273         av_log(s, AV_LOG_ERROR,
274                "Cannot get the image data "
275                "event_error: response_type:%u error_code:%u "
276                "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
277                e->response_type, e->error_code,
278                e->sequence, e->resource_id, e->minor_code, e->major_code);
279
280         free(e);
281         return AVERROR(EACCES);
282     }
283
284     free(img);
285
286     pkt->data = c->buffer;
287     pkt->size = c->frame_size;
288
289     return 0;
290 }
291 #endif /* CONFIG_LIBXCB_SHM */
292
293 #if CONFIG_LIBXCB_XFIXES
294 static int check_xfixes(xcb_connection_t *conn)
295 {
296     xcb_xfixes_query_version_cookie_t cookie;
297     xcb_xfixes_query_version_reply_t *reply;
298
299     cookie = xcb_xfixes_query_version(conn, XCB_XFIXES_MAJOR_VERSION,
300                                       XCB_XFIXES_MINOR_VERSION);
301     reply  = xcb_xfixes_query_version_reply(conn, cookie, NULL);
302
303     if (reply) {
304         free(reply);
305         return 1;
306     }
307     return 0;
308 }
309
310 #define BLEND(target, source, alpha) \
311     (target) + ((source) * (255 - (alpha)) + 255 / 2) / 255
312
313 static void xcbgrab_draw_mouse(AVFormatContext *s, AVPacket *pkt,
314                                xcb_query_pointer_reply_t *p,
315                                xcb_get_geometry_reply_t *geo)
316 {
317     XCBGrabContext *gr = s->priv_data;
318     uint32_t *cursor;
319     uint8_t *image = pkt->data;
320     int stride     = gr->bpp / 8;
321     xcb_xfixes_get_cursor_image_cookie_t cc;
322     xcb_xfixes_get_cursor_image_reply_t *ci;
323     int cx, cy, x, y, w, h, c_off, i_off;
324
325     cc = xcb_xfixes_get_cursor_image(gr->conn);
326     ci = xcb_xfixes_get_cursor_image_reply(gr->conn, cc, NULL);
327     if (!ci)
328         return;
329
330     cursor = xcb_xfixes_get_cursor_image_cursor_image(ci);
331     if (!cursor)
332         return;
333
334     cx = ci->x - ci->xhot;
335     cy = ci->y - ci->yhot;
336
337     x = FFMAX(cx, gr->x);
338     y = FFMAX(cy, gr->y);
339
340     w = FFMIN(cx + ci->width,  gr->x + gr->width)  - x;
341     h = FFMIN(cy + ci->height, gr->y + gr->height) - y;
342
343     c_off = x - cx;
344     i_off = x - gr->x;
345
346     cursor += (y - cy) * ci->width;
347     image  += (y - gr->y) * gr->width * stride;
348
349     for (y = 0; y < h; y++) {
350         cursor += c_off;
351         image  += i_off * stride;
352         for (x = 0; x < w; x++, cursor++, image += stride) {
353             int r, g, b, a;
354
355             r =  *cursor        & 0xff;
356             g = (*cursor >>  8) & 0xff;
357             b = (*cursor >> 16) & 0xff;
358             a = (*cursor >> 24) & 0xff;
359
360             if (!a)
361                 continue;
362
363             if (a == 255) {
364                 image[0] = r;
365                 image[1] = g;
366                 image[2] = b;
367             } else {
368                 image[0] = BLEND(r, image[0], a);
369                 image[1] = BLEND(g, image[1], a);
370                 image[2] = BLEND(b, image[2], a);
371             }
372
373         }
374         cursor +=  ci->width - w - c_off;
375         image  += (gr->width - w - i_off) * stride;
376     }
377
378     free(ci);
379 }
380 #endif /* CONFIG_LIBXCB_XFIXES */
381
382 static void xcbgrab_update_region(AVFormatContext *s)
383 {
384     XCBGrabContext *c     = s->priv_data;
385     const uint32_t args[] = { c->x - c->region_border,
386                               c->y - c->region_border };
387
388     xcb_configure_window(c->conn,
389                          c->window,
390                          XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
391                          args);
392 }
393
394 static int xcbgrab_read_packet(AVFormatContext *s, AVPacket *pkt)
395 {
396     XCBGrabContext *c = s->priv_data;
397     xcb_query_pointer_cookie_t pc;
398     xcb_get_geometry_cookie_t gc;
399     xcb_query_pointer_reply_t *p  = NULL;
400     xcb_get_geometry_reply_t *geo = NULL;
401     int ret = 0;
402
403     wait_frame(s, pkt);
404
405     if (c->follow_mouse || c->draw_mouse) {
406         pc  = xcb_query_pointer(c->conn, c->screen->root);
407         gc  = xcb_get_geometry(c->conn, c->screen->root);
408         p   = xcb_query_pointer_reply(c->conn, pc, NULL);
409         geo = xcb_get_geometry_reply(c->conn, gc, NULL);
410     }
411
412     if (c->follow_mouse && p->same_screen)
413         xcbgrab_reposition(s, p, geo);
414
415     if (c->show_region)
416         xcbgrab_update_region(s);
417
418 #if CONFIG_LIBXCB_SHM
419     if (c->has_shm && xcbgrab_frame_shm(s, pkt) < 0)
420         c->has_shm = 0;
421 #endif
422     if (!c->has_shm)
423         ret = xcbgrab_frame(s, pkt);
424
425 #if CONFIG_LIBXCB_XFIXES
426     if (ret >= 0 && c->draw_mouse && p->same_screen)
427         xcbgrab_draw_mouse(s, pkt, p, geo);
428 #endif
429
430     free(p);
431     free(geo);
432
433     return ret;
434 }
435
436 static av_cold int xcbgrab_read_close(AVFormatContext *s)
437 {
438     XCBGrabContext *ctx = s->priv_data;
439
440 #if CONFIG_LIBXCB_SHM
441     if (ctx->buffer) {
442         shmdt(ctx->buffer);
443     }
444 #endif
445
446     xcb_disconnect(ctx->conn);
447
448     return 0;
449 }
450
451 static xcb_screen_t *get_screen(const xcb_setup_t *setup, int screen_num)
452 {
453     xcb_screen_iterator_t it = xcb_setup_roots_iterator(setup);
454     xcb_screen_t *screen     = NULL;
455
456     for (; it.rem > 0; xcb_screen_next (&it)) {
457         if (!screen_num) {
458             screen = it.data;
459             break;
460         }
461
462         screen_num--;
463     }
464
465     return screen;
466 }
467
468 static int pixfmt_from_pixmap_format(AVFormatContext *s, int depth,
469                                      int *pix_fmt)
470 {
471     XCBGrabContext *c        = s->priv_data;
472     const xcb_setup_t *setup = xcb_get_setup(c->conn);
473     const xcb_format_t *fmt  = xcb_setup_pixmap_formats(setup);
474     int length               = xcb_setup_pixmap_formats_length(setup);
475
476     *pix_fmt = 0;
477
478     while (length--) {
479         if (fmt->depth == depth) {
480             switch (depth) {
481             case 32:
482                 if (fmt->bits_per_pixel == 32)
483                     *pix_fmt = AV_PIX_FMT_0RGB;
484                 break;
485             case 24:
486                 if (fmt->bits_per_pixel == 32)
487                     *pix_fmt = AV_PIX_FMT_0RGB32;
488                 else if (fmt->bits_per_pixel == 24)
489                     *pix_fmt = AV_PIX_FMT_RGB24;
490                 break;
491             case 16:
492                 if (fmt->bits_per_pixel == 16)
493                     *pix_fmt = AV_PIX_FMT_RGB565;
494                 break;
495             case 15:
496                 if (fmt->bits_per_pixel == 16)
497                     *pix_fmt = AV_PIX_FMT_RGB555;
498                 break;
499             case 8:
500                 if (fmt->bits_per_pixel == 8)
501                     *pix_fmt = AV_PIX_FMT_RGB8;
502                 break;
503             }
504         }
505
506         if (*pix_fmt) {
507             c->bpp        = fmt->bits_per_pixel;
508             c->frame_size = c->width * c->height * fmt->bits_per_pixel / 8;
509             return 0;
510         }
511
512         fmt++;
513     }
514     avpriv_report_missing_feature(s, "Mapping this pixmap format");
515
516     return AVERROR_PATCHWELCOME;
517 }
518
519 static int create_stream(AVFormatContext *s)
520 {
521     XCBGrabContext *c = s->priv_data;
522     AVStream *st      = avformat_new_stream(s, NULL);
523     xcb_get_geometry_cookie_t gc;
524     xcb_get_geometry_reply_t *geo;
525     int ret;
526
527     if (!st)
528         return AVERROR(ENOMEM);
529
530     ret = av_parse_video_size(&c->width, &c->height, c->video_size);
531     if (ret < 0)
532         return ret;
533
534     ret = av_parse_video_rate(&st->avg_frame_rate, c->framerate);
535     if (ret < 0)
536         return ret;
537
538     avpriv_set_pts_info(st, 64, 1, 1000000);
539
540     gc  = xcb_get_geometry(c->conn, c->screen->root);
541     geo = xcb_get_geometry_reply(c->conn, gc, NULL);
542     if (!geo)
543         return AVERROR_EXTERNAL;
544
545     if (c->x + c->width > geo->width ||
546         c->y + c->height > geo->height) {
547         av_log(s, AV_LOG_ERROR,
548                "Capture area %dx%d at position %d.%d "
549                "outside the screen size %dx%d\n",
550                c->width, c->height,
551                c->x, c->y,
552                geo->width, geo->height);
553         free(geo);
554         return AVERROR(EINVAL);
555     }
556
557     c->time_base  = (AVRational){ st->avg_frame_rate.den,
558                                   st->avg_frame_rate.num };
559     c->time_frame = av_gettime();
560
561     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
562     st->codecpar->codec_id   = AV_CODEC_ID_RAWVIDEO;
563     st->codecpar->width      = c->width;
564     st->codecpar->height     = c->height;
565
566     ret = pixfmt_from_pixmap_format(s, geo->depth, &st->codecpar->format);
567
568     free(geo);
569
570     return ret;
571 }
572
573 static void draw_rectangle(AVFormatContext *s)
574 {
575     XCBGrabContext *c = s->priv_data;
576     xcb_gcontext_t gc = xcb_generate_id(c->conn);
577     uint32_t mask     = XCB_GC_FOREGROUND |
578                         XCB_GC_BACKGROUND |
579                         XCB_GC_LINE_WIDTH |
580                         XCB_GC_LINE_STYLE |
581                         XCB_GC_FILL_STYLE;
582     uint32_t values[] = { c->screen->black_pixel,
583                           c->screen->white_pixel,
584                           c->region_border,
585                           XCB_LINE_STYLE_DOUBLE_DASH,
586                           XCB_FILL_STYLE_SOLID };
587     xcb_rectangle_t r = { 1, 1,
588                           c->width  + c->region_border * 2 - 3,
589                           c->height + c->region_border * 2 - 3 };
590
591     xcb_create_gc(c->conn, gc, c->window, mask, values);
592
593     xcb_poly_rectangle(c->conn, c->window, gc, 1, &r);
594 }
595
596 static void setup_window(AVFormatContext *s)
597 {
598     XCBGrabContext *c = s->priv_data;
599     uint32_t mask     = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
600     uint32_t values[] = { 1,
601                           XCB_EVENT_MASK_EXPOSURE |
602                           XCB_EVENT_MASK_STRUCTURE_NOTIFY };
603     av_unused xcb_rectangle_t rect = { 0, 0, c->width, c->height };
604
605     c->window = xcb_generate_id(c->conn);
606
607     xcb_create_window(c->conn, XCB_COPY_FROM_PARENT,
608                       c->window,
609                       c->screen->root,
610                       c->x - c->region_border,
611                       c->y - c->region_border,
612                       c->width + c->region_border * 2,
613                       c->height + c->region_border * 2,
614                       0,
615                       XCB_WINDOW_CLASS_INPUT_OUTPUT,
616                       XCB_COPY_FROM_PARENT,
617                       mask, values);
618
619 #if CONFIG_LIBXCB_SHAPE
620     xcb_shape_rectangles(c->conn, XCB_SHAPE_SO_SUBTRACT,
621                          XCB_SHAPE_SK_BOUNDING, XCB_CLIP_ORDERING_UNSORTED,
622                          c->window,
623                          c->region_border, c->region_border,
624                          1, &rect);
625 #endif
626
627     xcb_map_window(c->conn, c->window);
628
629     draw_rectangle(s);
630 }
631
632 static av_cold int xcbgrab_read_header(AVFormatContext *s)
633 {
634     XCBGrabContext *c = s->priv_data;
635     int screen_num, ret;
636     const xcb_setup_t *setup;
637     char *display_name = av_strdup(s->url);
638
639     if (!display_name)
640         return AVERROR(ENOMEM);
641
642     if (!sscanf(s->url, "%[^+]+%d,%d", display_name, &c->x, &c->y)) {
643         *display_name = 0;
644         sscanf(s->url, "+%d,%d", &c->x, &c->y);
645     }
646
647     c->conn = xcb_connect(display_name[0] ? display_name : NULL, &screen_num);
648     av_freep(&display_name);
649
650     if ((ret = xcb_connection_has_error(c->conn))) {
651         av_log(s, AV_LOG_ERROR, "Cannot open display %s, error %d.\n",
652                s->url[0] ? s->url : "default", ret);
653         return AVERROR(EIO);
654     }
655
656     setup = xcb_get_setup(c->conn);
657
658     c->screen = get_screen(setup, screen_num);
659     if (!c->screen) {
660         av_log(s, AV_LOG_ERROR, "The screen %d does not exist.\n",
661                screen_num);
662         xcbgrab_read_close(s);
663         return AVERROR(EIO);
664     }
665
666     ret = create_stream(s);
667
668     if (ret < 0) {
669         xcbgrab_read_close(s);
670         return ret;
671     }
672
673 #if CONFIG_LIBXCB_SHM
674     if ((c->has_shm = check_shm(c->conn)))
675         c->segment = xcb_generate_id(c->conn);
676 #endif
677
678 #if CONFIG_LIBXCB_XFIXES
679     if (c->draw_mouse) {
680         if (!(c->draw_mouse = check_xfixes(c->conn))) {
681             av_log(s, AV_LOG_WARNING,
682                    "XFixes not available, cannot draw the mouse.\n");
683         }
684         if (c->bpp < 24) {
685             avpriv_report_missing_feature(s, "%d bits per pixel screen",
686                                           c->bpp);
687             c->draw_mouse = 0;
688         }
689     }
690 #endif
691
692     if (c->show_region)
693         setup_window(s);
694
695     return 0;
696 }
697
698 AVInputFormat ff_xcbgrab_demuxer = {
699     .name           = "x11grab",
700     .long_name      = NULL_IF_CONFIG_SMALL("X11 screen capture, using XCB"),
701     .priv_data_size = sizeof(XCBGrabContext),
702     .read_header    = xcbgrab_read_header,
703     .read_packet    = xcbgrab_read_packet,
704     .read_close     = xcbgrab_read_close,
705     .flags          = AVFMT_NOFILE,
706     .priv_class     = &xcbgrab_class,
707 };