]> git.sesse.net Git - ffmpeg/blob - libavdevice/xcbgrab.c
avdevice/xcbgrab: don't assume xserver endianness
[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 <string.h>
26 #include <xcb/xcb.h>
27
28 #if CONFIG_LIBXCB_XFIXES
29 #include <xcb/xfixes.h>
30 #endif
31
32 #if CONFIG_LIBXCB_SHM
33 #include <sys/shm.h>
34 #include <xcb/shm.h>
35 #endif
36
37 #if CONFIG_LIBXCB_SHAPE
38 #include <xcb/shape.h>
39 #endif
40
41 #include "libavutil/internal.h"
42 #include "libavutil/mathematics.h"
43 #include "libavutil/opt.h"
44 #include "libavutil/parseutils.h"
45 #include "libavutil/time.h"
46
47 #include "libavformat/avformat.h"
48 #include "libavformat/internal.h"
49
50 typedef struct XCBGrabContext {
51     const AVClass *class;
52
53     xcb_connection_t *conn;
54     xcb_screen_t *screen;
55     xcb_window_t window;
56 #if CONFIG_LIBXCB_SHM
57     AVBufferPool *shm_pool;
58 #endif
59     int64_t time_frame;
60     AVRational time_base;
61     int64_t frame_duration;
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     int select_region;
74
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(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL }, 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     { "select_region", "Select the grabbing region graphically using the pointer.", OFFSET(select_region), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
98     { NULL },
99 };
100
101 static const AVClass xcbgrab_class = {
102     .class_name = "xcbgrab indev",
103     .item_name  = av_default_item_name,
104     .option     = options,
105     .version    = LIBAVUTIL_VERSION_INT,
106     .category   = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
107 };
108
109 static int xcbgrab_reposition(AVFormatContext *s,
110                               xcb_query_pointer_reply_t *p,
111                               xcb_get_geometry_reply_t *geo)
112 {
113     XCBGrabContext *c = s->priv_data;
114     int x = c->x, y = c->y;
115     int w = c->width, h = c->height, f = c->follow_mouse;
116     int p_x, p_y;
117
118     if (!p || !geo)
119         return AVERROR(EIO);
120
121     p_x = p->win_x;
122     p_y = p->win_y;
123
124     if (f == FOLLOW_CENTER) {
125         x = p_x - w / 2;
126         y = p_y - h / 2;
127     } else {
128         int left   = x + f;
129         int right  = x + w - f;
130         int top    = y + f;
131         int bottom = y + h - f;
132         if (p_x > right) {
133             x += p_x - right;
134         } else if (p_x < left) {
135             x -= left - p_x;
136         }
137         if (p_y > bottom) {
138             y += p_y - bottom;
139         } else if (p_y < top) {
140             y -= top - p_y;
141         }
142     }
143
144     c->x = FFMIN(FFMAX(0, x), geo->width  - w);
145     c->y = FFMIN(FFMAX(0, y), geo->height - h);
146
147     return 0;
148 }
149
150 static void xcbgrab_image_reply_free(void *opaque, uint8_t *data)
151 {
152     free(opaque);
153 }
154
155 static int xcbgrab_frame(AVFormatContext *s, AVPacket *pkt)
156 {
157     XCBGrabContext *c = s->priv_data;
158     xcb_get_image_cookie_t iq;
159     xcb_get_image_reply_t *img;
160     xcb_drawable_t drawable = c->screen->root;
161     xcb_generic_error_t *e = NULL;
162     uint8_t *data;
163     int length;
164
165     iq  = xcb_get_image(c->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
166                         c->x, c->y, c->width, c->height, ~0);
167
168     img = xcb_get_image_reply(c->conn, iq, &e);
169
170     if (e) {
171         av_log(s, AV_LOG_ERROR,
172                "Cannot get the image data "
173                "event_error: response_type:%u error_code:%u "
174                "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
175                e->response_type, e->error_code,
176                e->sequence, e->resource_id, e->minor_code, e->major_code);
177         free(e);
178         return AVERROR(EACCES);
179     }
180
181     if (!img)
182         return AVERROR(EAGAIN);
183
184     data   = xcb_get_image_data(img);
185     length = xcb_get_image_data_length(img);
186
187     av_init_packet(pkt);
188
189     pkt->buf = av_buffer_create(data, length, xcbgrab_image_reply_free, img, 0);
190     if (!pkt->buf) {
191         free(img);
192         return AVERROR(ENOMEM);
193     }
194
195     pkt->data = data;
196     pkt->size = length;
197
198     return 0;
199 }
200
201 static int64_t wait_frame(AVFormatContext *s, AVPacket *pkt)
202 {
203     XCBGrabContext *c = s->priv_data;
204     int64_t curtime, delay;
205
206     c->time_frame += c->frame_duration;
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     return 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 void free_shm_buffer(void *opaque, uint8_t *data)
235 {
236     shmdt(data);
237 }
238
239 static AVBufferRef *allocate_shm_buffer(void *opaque, int size)
240 {
241     xcb_connection_t *conn = opaque;
242     xcb_shm_seg_t segment;
243     AVBufferRef *ref;
244     uint8_t *data;
245     int id;
246
247     id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777);
248     if (id == -1)
249         return NULL;
250
251     segment = xcb_generate_id(conn);
252     xcb_shm_attach(conn, segment, id, 0);
253     data = shmat(id, NULL, 0);
254     shmctl(id, IPC_RMID, 0);
255     if ((intptr_t)data == -1 || !data)
256         return NULL;
257
258     ref = av_buffer_create(data, size, free_shm_buffer, (void *)(ptrdiff_t)segment, 0);
259     if (!ref)
260         shmdt(data);
261
262     return ref;
263 }
264
265 static int xcbgrab_frame_shm(AVFormatContext *s, AVPacket *pkt)
266 {
267     XCBGrabContext *c = s->priv_data;
268     xcb_shm_get_image_cookie_t iq;
269     xcb_shm_get_image_reply_t *img;
270     xcb_drawable_t drawable = c->screen->root;
271     xcb_generic_error_t *e = NULL;
272     AVBufferRef *buf;
273     xcb_shm_seg_t segment;
274
275     buf = av_buffer_pool_get(c->shm_pool);
276     if (!buf) {
277         av_log(s, AV_LOG_ERROR, "Could not get shared memory buffer.\n");
278         return AVERROR(ENOMEM);
279     }
280     segment = (xcb_shm_seg_t)av_buffer_pool_buffer_get_opaque(buf);
281
282     iq = xcb_shm_get_image(c->conn, drawable,
283                            c->x, c->y, c->width, c->height, ~0,
284                            XCB_IMAGE_FORMAT_Z_PIXMAP, segment, 0);
285     img = xcb_shm_get_image_reply(c->conn, iq, &e);
286
287     xcb_flush(c->conn);
288
289     if (e) {
290         av_log(s, AV_LOG_ERROR,
291                "Cannot get the image data "
292                "event_error: response_type:%u error_code:%u "
293                "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
294                e->response_type, e->error_code,
295                e->sequence, e->resource_id, e->minor_code, e->major_code);
296
297         free(e);
298         av_buffer_unref(&buf);
299         return AVERROR(EACCES);
300     }
301
302     free(img);
303
304     av_init_packet(pkt);
305
306     pkt->buf = buf;
307     pkt->data = buf->data;
308     pkt->size = c->frame_size;
309
310     return 0;
311 }
312 #endif /* CONFIG_LIBXCB_SHM */
313
314 #if CONFIG_LIBXCB_XFIXES
315 static int check_xfixes(xcb_connection_t *conn)
316 {
317     xcb_xfixes_query_version_cookie_t cookie;
318     xcb_xfixes_query_version_reply_t *reply;
319
320     cookie = xcb_xfixes_query_version(conn, XCB_XFIXES_MAJOR_VERSION,
321                                       XCB_XFIXES_MINOR_VERSION);
322     reply  = xcb_xfixes_query_version_reply(conn, cookie, NULL);
323
324     if (reply) {
325         free(reply);
326         return 1;
327     }
328     return 0;
329 }
330
331 #define BLEND(target, source, alpha) \
332     (target) + ((source) * (255 - (alpha)) + 255 / 2) / 255
333
334 static void xcbgrab_draw_mouse(AVFormatContext *s, AVPacket *pkt,
335                                xcb_query_pointer_reply_t *p,
336                                xcb_get_geometry_reply_t *geo)
337 {
338     XCBGrabContext *gr = s->priv_data;
339     uint32_t *cursor;
340     uint8_t *image = pkt->data;
341     int stride     = gr->bpp / 8;
342     xcb_xfixes_get_cursor_image_cookie_t cc;
343     xcb_xfixes_get_cursor_image_reply_t *ci;
344     int cx, cy, x, y, w, h, c_off, i_off;
345
346     cc = xcb_xfixes_get_cursor_image(gr->conn);
347     ci = xcb_xfixes_get_cursor_image_reply(gr->conn, cc, NULL);
348     if (!ci)
349         return;
350
351     cursor = xcb_xfixes_get_cursor_image_cursor_image(ci);
352     if (!cursor)
353         return;
354
355     cx = ci->x - ci->xhot;
356     cy = ci->y - ci->yhot;
357
358     x = FFMAX(cx, gr->x);
359     y = FFMAX(cy, gr->y);
360
361     w = FFMIN(cx + ci->width,  gr->x + gr->width)  - x;
362     h = FFMIN(cy + ci->height, gr->y + gr->height) - y;
363
364     c_off = x - cx;
365     i_off = x - gr->x;
366
367     cursor += (y - cy) * ci->width;
368     image  += (y - gr->y) * gr->width * stride;
369
370     for (y = 0; y < h; y++) {
371         cursor += c_off;
372         image  += i_off * stride;
373         for (x = 0; x < w; x++, cursor++, image += stride) {
374             int r, g, b, a;
375
376             r =  *cursor        & 0xff;
377             g = (*cursor >>  8) & 0xff;
378             b = (*cursor >> 16) & 0xff;
379             a = (*cursor >> 24) & 0xff;
380
381             if (!a)
382                 continue;
383
384             if (a == 255) {
385                 image[0] = r;
386                 image[1] = g;
387                 image[2] = b;
388             } else {
389                 image[0] = BLEND(r, image[0], a);
390                 image[1] = BLEND(g, image[1], a);
391                 image[2] = BLEND(b, image[2], a);
392             }
393
394         }
395         cursor +=  ci->width - w - c_off;
396         image  += (gr->width - w - i_off) * stride;
397     }
398
399     free(ci);
400 }
401 #endif /* CONFIG_LIBXCB_XFIXES */
402
403 static void xcbgrab_update_region(AVFormatContext *s)
404 {
405     XCBGrabContext *c     = s->priv_data;
406     const uint32_t args[] = { c->x - c->region_border,
407                               c->y - c->region_border };
408
409     xcb_configure_window(c->conn,
410                          c->window,
411                          XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
412                          args);
413 }
414
415 static int xcbgrab_read_packet(AVFormatContext *s, AVPacket *pkt)
416 {
417     XCBGrabContext *c = s->priv_data;
418     xcb_query_pointer_cookie_t pc;
419     xcb_get_geometry_cookie_t gc;
420     xcb_query_pointer_reply_t *p  = NULL;
421     xcb_get_geometry_reply_t *geo = NULL;
422     int ret = 0;
423     int64_t pts;
424
425     pts = wait_frame(s, pkt);
426
427     if (c->follow_mouse || c->draw_mouse) {
428         pc  = xcb_query_pointer(c->conn, c->screen->root);
429         gc  = xcb_get_geometry(c->conn, c->screen->root);
430         p   = xcb_query_pointer_reply(c->conn, pc, NULL);
431         if (!p) {
432             av_log(s, AV_LOG_ERROR, "Failed to query xcb pointer\n");
433             return AVERROR_EXTERNAL;
434         }
435         geo = xcb_get_geometry_reply(c->conn, gc, NULL);
436         if (!geo) {
437             av_log(s, AV_LOG_ERROR, "Failed to get xcb geometry\n");
438             free(p);
439             return AVERROR_EXTERNAL;
440         }
441     }
442
443     if (c->follow_mouse && p->same_screen)
444         xcbgrab_reposition(s, p, geo);
445
446     if (c->show_region)
447         xcbgrab_update_region(s);
448
449 #if CONFIG_LIBXCB_SHM
450     if (c->has_shm && xcbgrab_frame_shm(s, pkt) < 0) {
451         av_log(s, AV_LOG_WARNING, "Continuing without shared memory.\n");
452         c->has_shm = 0;
453     }
454 #endif
455     if (!c->has_shm)
456         ret = xcbgrab_frame(s, pkt);
457     pkt->dts = pkt->pts = pts;
458     pkt->duration = c->frame_duration;
459
460 #if CONFIG_LIBXCB_XFIXES
461     if (ret >= 0 && c->draw_mouse && p->same_screen)
462         xcbgrab_draw_mouse(s, pkt, p, geo);
463 #endif
464
465     free(p);
466     free(geo);
467
468     return ret;
469 }
470
471 static av_cold int xcbgrab_read_close(AVFormatContext *s)
472 {
473     XCBGrabContext *ctx = s->priv_data;
474
475 #if CONFIG_LIBXCB_SHM
476     av_buffer_pool_uninit(&ctx->shm_pool);
477 #endif
478
479     xcb_disconnect(ctx->conn);
480
481     return 0;
482 }
483
484 static xcb_screen_t *get_screen(const xcb_setup_t *setup, int screen_num)
485 {
486     xcb_screen_iterator_t it = xcb_setup_roots_iterator(setup);
487     xcb_screen_t *screen     = NULL;
488
489     for (; it.rem > 0; xcb_screen_next (&it)) {
490         if (!screen_num) {
491             screen = it.data;
492             break;
493         }
494
495         screen_num--;
496     }
497
498     return screen;
499 }
500
501 static int pixfmt_from_pixmap_format(AVFormatContext *s, int depth,
502                                      int *pix_fmt, int *bpp)
503 {
504     XCBGrabContext *c        = s->priv_data;
505     const xcb_setup_t *setup = xcb_get_setup(c->conn);
506     const xcb_format_t *fmt  = xcb_setup_pixmap_formats(setup);
507     int length               = xcb_setup_pixmap_formats_length(setup);
508
509     *pix_fmt = 0;
510
511     while (length--) {
512         if (fmt->depth == depth) {
513             switch (depth) {
514             case 32:
515                 if (fmt->bits_per_pixel == 32)
516                     *pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
517                                AV_PIX_FMT_BGR0 : AV_PIX_FMT_0RGB;
518                 break;
519             case 24:
520                 if (fmt->bits_per_pixel == 32)
521                     *pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
522                                AV_PIX_FMT_BGR0 : AV_PIX_FMT_0RGB;
523                 else if (fmt->bits_per_pixel == 24)
524                     *pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
525                                AV_PIX_FMT_BGR24 : AV_PIX_FMT_RGB24;
526                 break;
527             case 16:
528                 if (fmt->bits_per_pixel == 16)
529                     *pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
530                                AV_PIX_FMT_RGB565LE : AV_PIX_FMT_RGB565BE;
531                 break;
532             case 15:
533                 if (fmt->bits_per_pixel == 16)
534                     *pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
535                                AV_PIX_FMT_RGB555LE : AV_PIX_FMT_RGB555BE;
536                 break;
537             case 8:
538                 if (fmt->bits_per_pixel == 8)
539                     *pix_fmt = AV_PIX_FMT_RGB8;
540                 break;
541             }
542         }
543
544         if (*pix_fmt) {
545             *bpp        = fmt->bits_per_pixel;
546             return 0;
547         }
548
549         fmt++;
550     }
551     avpriv_report_missing_feature(s, "Mapping this pixmap format");
552
553     return AVERROR_PATCHWELCOME;
554 }
555
556 static int create_stream(AVFormatContext *s)
557 {
558     XCBGrabContext *c = s->priv_data;
559     AVStream *st      = avformat_new_stream(s, NULL);
560     xcb_get_geometry_cookie_t gc;
561     xcb_get_geometry_reply_t *geo;
562     int64_t frame_size_bits;
563     int ret;
564
565     if (!st)
566         return AVERROR(ENOMEM);
567
568     ret = av_parse_video_rate(&st->avg_frame_rate, c->framerate);
569     if (ret < 0)
570         return ret;
571
572     avpriv_set_pts_info(st, 64, 1, 1000000);
573
574     gc  = xcb_get_geometry(c->conn, c->screen->root);
575     geo = xcb_get_geometry_reply(c->conn, gc, NULL);
576     if (!geo)
577         return AVERROR_EXTERNAL;
578
579     if (!c->width || !c->height) {
580         c->width = geo->width;
581         c->height = geo->height;
582     }
583
584     if (c->x + c->width > geo->width ||
585         c->y + c->height > geo->height) {
586         av_log(s, AV_LOG_ERROR,
587                "Capture area %dx%d at position %d.%d "
588                "outside the screen size %dx%d\n",
589                c->width, c->height,
590                c->x, c->y,
591                geo->width, geo->height);
592         free(geo);
593         return AVERROR(EINVAL);
594     }
595
596     c->time_base  = (AVRational){ st->avg_frame_rate.den,
597                                   st->avg_frame_rate.num };
598     c->frame_duration = av_rescale_q(1, c->time_base, AV_TIME_BASE_Q);
599     c->time_frame = av_gettime();
600
601     ret = pixfmt_from_pixmap_format(s, geo->depth, &st->codecpar->format, &c->bpp);
602     free(geo);
603     if (ret < 0)
604         return ret;
605
606     frame_size_bits = (int64_t)c->width * c->height * c->bpp;
607     if (frame_size_bits / 8 + AV_INPUT_BUFFER_PADDING_SIZE > INT_MAX) {
608         av_log(s, AV_LOG_ERROR, "Captured area is too large\n");
609         return AVERROR_PATCHWELCOME;
610     }
611     c->frame_size = frame_size_bits / 8;
612
613 #if CONFIG_LIBXCB_SHM
614     c->shm_pool = av_buffer_pool_init2(c->frame_size + AV_INPUT_BUFFER_PADDING_SIZE,
615                                            c->conn, allocate_shm_buffer, NULL);
616     if (!c->shm_pool)
617         return AVERROR(ENOMEM);
618 #endif
619
620     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
621     st->codecpar->codec_id   = AV_CODEC_ID_RAWVIDEO;
622     st->codecpar->width      = c->width;
623     st->codecpar->height     = c->height;
624     st->codecpar->bit_rate   = av_rescale(frame_size_bits, st->avg_frame_rate.num, st->avg_frame_rate.den);
625
626     return ret;
627 }
628
629 static void draw_rectangle(AVFormatContext *s)
630 {
631     XCBGrabContext *c = s->priv_data;
632     xcb_gcontext_t gc = xcb_generate_id(c->conn);
633     uint32_t mask     = XCB_GC_FOREGROUND |
634                         XCB_GC_BACKGROUND |
635                         XCB_GC_LINE_WIDTH |
636                         XCB_GC_LINE_STYLE |
637                         XCB_GC_FILL_STYLE;
638     uint32_t values[] = { c->screen->black_pixel,
639                           c->screen->white_pixel,
640                           c->region_border,
641                           XCB_LINE_STYLE_DOUBLE_DASH,
642                           XCB_FILL_STYLE_SOLID };
643     xcb_rectangle_t r = { 1, 1,
644                           c->width  + c->region_border * 2 - 3,
645                           c->height + c->region_border * 2 - 3 };
646
647     xcb_create_gc(c->conn, gc, c->window, mask, values);
648
649     xcb_poly_rectangle(c->conn, c->window, gc, 1, &r);
650 }
651
652 static void setup_window(AVFormatContext *s)
653 {
654     XCBGrabContext *c = s->priv_data;
655     uint32_t mask     = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
656     uint32_t values[] = { 1,
657                           XCB_EVENT_MASK_EXPOSURE |
658                           XCB_EVENT_MASK_STRUCTURE_NOTIFY };
659     av_unused xcb_rectangle_t rect = { 0, 0, c->width, c->height };
660
661     c->window = xcb_generate_id(c->conn);
662
663     xcb_create_window(c->conn, XCB_COPY_FROM_PARENT,
664                       c->window,
665                       c->screen->root,
666                       c->x - c->region_border,
667                       c->y - c->region_border,
668                       c->width + c->region_border * 2,
669                       c->height + c->region_border * 2,
670                       0,
671                       XCB_WINDOW_CLASS_INPUT_OUTPUT,
672                       XCB_COPY_FROM_PARENT,
673                       mask, values);
674
675 #if CONFIG_LIBXCB_SHAPE
676     xcb_shape_rectangles(c->conn, XCB_SHAPE_SO_SUBTRACT,
677                          XCB_SHAPE_SK_BOUNDING, XCB_CLIP_ORDERING_UNSORTED,
678                          c->window,
679                          c->region_border, c->region_border,
680                          1, &rect);
681 #endif
682
683     xcb_map_window(c->conn, c->window);
684
685     draw_rectangle(s);
686 }
687
688 #define CROSSHAIR_CURSOR 34
689
690 static xcb_rectangle_t rectangle_from_corners(xcb_point_t *corner_a,
691                                               xcb_point_t *corner_b)
692 {
693     xcb_rectangle_t rectangle;
694     rectangle.x = FFMIN(corner_a->x, corner_b->x);
695     rectangle.y = FFMIN(corner_a->y, corner_b->y);
696     rectangle.width = FFABS(corner_a->x - corner_b->x);
697     rectangle.height = FFABS(corner_a->y - corner_b->y);
698     return rectangle;
699 }
700
701 static int select_region(AVFormatContext *s)
702 {
703     XCBGrabContext *c = s->priv_data;
704     xcb_connection_t *conn = c->conn;
705     xcb_screen_t *screen = c->screen;
706
707     int ret = 0, done = 0, was_pressed = 0;
708     xcb_cursor_t cursor;
709     xcb_font_t cursor_font;
710     xcb_point_t press_position;
711     xcb_generic_event_t *event;
712     xcb_rectangle_t rectangle = { 0 };
713     xcb_grab_pointer_reply_t *reply;
714     xcb_grab_pointer_cookie_t cookie;
715
716     xcb_window_t root_window = screen->root;
717     xcb_gcontext_t gc = xcb_generate_id(conn);
718     uint32_t mask = XCB_GC_FUNCTION | XCB_GC_SUBWINDOW_MODE;
719     uint32_t values[] = { XCB_GX_INVERT, XCB_SUBWINDOW_MODE_INCLUDE_INFERIORS };
720     xcb_create_gc(conn, gc, root_window, mask, values);
721
722     cursor_font = xcb_generate_id(conn);
723     xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
724     cursor = xcb_generate_id(conn);
725     xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
726                             CROSSHAIR_CURSOR, CROSSHAIR_CURSOR + 1, 0, 0, 0,
727                             0xFFFF, 0xFFFF, 0xFFFF);
728     cookie = xcb_grab_pointer(conn, 0, root_window,
729                               XCB_EVENT_MASK_BUTTON_PRESS |
730                               XCB_EVENT_MASK_BUTTON_RELEASE |
731                               XCB_EVENT_MASK_BUTTON_MOTION,
732                               XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
733                               root_window, cursor, XCB_CURRENT_TIME);
734     reply = xcb_grab_pointer_reply(conn, cookie, NULL);
735     if (!reply || reply->status != XCB_GRAB_STATUS_SUCCESS) {
736         av_log(s, AV_LOG_ERROR,
737                "Failed to select region. Could not grab pointer.\n");
738         ret = AVERROR(EIO);
739         free(reply);
740         goto fail;
741     }
742     free(reply);
743
744     xcb_grab_server(conn);
745
746     while (!done && (event = xcb_wait_for_event(conn))) {
747         switch (event->response_type & ~0x80) {
748         case XCB_BUTTON_PRESS: {
749             xcb_button_press_event_t *press = (xcb_button_press_event_t *)event;
750             press_position = (xcb_point_t){ press->event_x, press->event_y };
751             rectangle.x = press_position.x;
752             rectangle.y = press_position.y;
753             xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
754             was_pressed = 1;
755             break;
756         }
757         case XCB_MOTION_NOTIFY: {
758             if (was_pressed) {
759                 xcb_motion_notify_event_t *motion =
760                     (xcb_motion_notify_event_t *)event;
761                 xcb_point_t cursor_position = { motion->event_x, motion->event_y };
762                 xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
763                 rectangle = rectangle_from_corners(&press_position, &cursor_position);
764                 xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
765             }
766             break;
767         }
768         case XCB_BUTTON_RELEASE: {
769             xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
770             done = 1;
771             break;
772         }
773         default:
774             break;
775         }
776         xcb_flush(conn);
777         free(event);
778     }
779     c->width  = rectangle.width;
780     c->height = rectangle.height;
781     if (c->width && c->height) {
782         c->x = rectangle.x;
783         c->y = rectangle.y;
784     } else {
785         c->x = 0;
786         c->y = 0;
787     }
788     xcb_ungrab_server(conn);
789     xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
790     xcb_flush(conn);
791
792 fail:
793     xcb_free_cursor(conn, cursor);
794     xcb_close_font(conn, cursor_font);
795     xcb_free_gc(conn, gc);
796     return ret;
797 }
798
799 static av_cold int xcbgrab_read_header(AVFormatContext *s)
800 {
801     XCBGrabContext *c = s->priv_data;
802     int screen_num, ret;
803     const xcb_setup_t *setup;
804     char *display_name = av_strdup(s->url);
805
806     if (!display_name)
807         return AVERROR(ENOMEM);
808
809     if (!sscanf(s->url, "%[^+]+%d,%d", display_name, &c->x, &c->y)) {
810         *display_name = 0;
811         sscanf(s->url, "+%d,%d", &c->x, &c->y);
812     }
813
814     c->conn = xcb_connect(display_name[0] ? display_name : NULL, &screen_num);
815     av_freep(&display_name);
816
817     if ((ret = xcb_connection_has_error(c->conn))) {
818         av_log(s, AV_LOG_ERROR, "Cannot open display %s, error %d.\n",
819                s->url[0] ? s->url : "default", ret);
820         return AVERROR(EIO);
821     }
822
823     setup = xcb_get_setup(c->conn);
824
825     c->screen = get_screen(setup, screen_num);
826     if (!c->screen) {
827         av_log(s, AV_LOG_ERROR, "The screen %d does not exist.\n",
828                screen_num);
829         xcbgrab_read_close(s);
830         return AVERROR(EIO);
831     }
832
833     if (c->select_region) {
834         ret = select_region(s);
835         if (ret < 0) {
836             xcbgrab_read_close(s);
837             return ret;
838         }
839     }
840
841     ret = create_stream(s);
842
843     if (ret < 0) {
844         xcbgrab_read_close(s);
845         return ret;
846     }
847
848 #if CONFIG_LIBXCB_SHM
849     c->has_shm = check_shm(c->conn);
850 #endif
851
852 #if CONFIG_LIBXCB_XFIXES
853     if (c->draw_mouse) {
854         if (!(c->draw_mouse = check_xfixes(c->conn))) {
855             av_log(s, AV_LOG_WARNING,
856                    "XFixes not available, cannot draw the mouse.\n");
857         }
858         if (c->bpp < 24) {
859             avpriv_report_missing_feature(s, "%d bits per pixel screen",
860                                           c->bpp);
861             c->draw_mouse = 0;
862         }
863     }
864 #endif
865
866     if (c->show_region)
867         setup_window(s);
868
869     return 0;
870 }
871
872 AVInputFormat ff_xcbgrab_demuxer = {
873     .name           = "x11grab",
874     .long_name      = NULL_IF_CONFIG_SMALL("X11 screen capture, using XCB"),
875     .priv_data_size = sizeof(XCBGrabContext),
876     .read_header    = xcbgrab_read_header,
877     .read_packet    = xcbgrab_read_packet,
878     .read_close     = xcbgrab_read_close,
879     .flags          = AVFMT_NOFILE,
880     .priv_class     = &xcbgrab_class,
881 };