xcbgrab.c 20 KB

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