Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

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