You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

883 lines
26KB

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