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.

877 lines
25KB

  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();
  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. pts = wait_frame(s, pkt);
  348. if (c->follow_mouse || c->draw_mouse) {
  349. pc = xcb_query_pointer(c->conn, c->screen->root);
  350. gc = xcb_get_geometry(c->conn, c->screen->root);
  351. p = xcb_query_pointer_reply(c->conn, pc, NULL);
  352. if (!p) {
  353. av_log(s, AV_LOG_ERROR, "Failed to query xcb pointer\n");
  354. return AVERROR_EXTERNAL;
  355. }
  356. geo = xcb_get_geometry_reply(c->conn, gc, NULL);
  357. if (!geo) {
  358. av_log(s, AV_LOG_ERROR, "Failed to get xcb geometry\n");
  359. free(p);
  360. return AVERROR_EXTERNAL;
  361. }
  362. }
  363. if (c->follow_mouse && p->same_screen)
  364. xcbgrab_reposition(s, p, geo);
  365. if (c->show_region)
  366. xcbgrab_update_region(s);
  367. #if CONFIG_LIBXCB_SHM
  368. if (c->has_shm && xcbgrab_frame_shm(s, pkt) < 0) {
  369. av_log(s, AV_LOG_WARNING, "Continuing without shared memory.\n");
  370. c->has_shm = 0;
  371. }
  372. #endif
  373. if (!c->has_shm)
  374. ret = xcbgrab_frame(s, pkt);
  375. pkt->dts = pkt->pts = pts;
  376. pkt->duration = c->frame_duration;
  377. #if CONFIG_LIBXCB_XFIXES
  378. if (ret >= 0 && c->draw_mouse && p->same_screen)
  379. xcbgrab_draw_mouse(s, pkt, p, geo);
  380. #endif
  381. free(p);
  382. free(geo);
  383. return ret;
  384. }
  385. static av_cold int xcbgrab_read_close(AVFormatContext *s)
  386. {
  387. XCBGrabContext *ctx = s->priv_data;
  388. #if CONFIG_LIBXCB_SHM
  389. av_buffer_pool_uninit(&ctx->shm_pool);
  390. #endif
  391. xcb_disconnect(ctx->conn);
  392. return 0;
  393. }
  394. static xcb_screen_t *get_screen(const xcb_setup_t *setup, int screen_num)
  395. {
  396. xcb_screen_iterator_t it = xcb_setup_roots_iterator(setup);
  397. xcb_screen_t *screen = NULL;
  398. for (; it.rem > 0; xcb_screen_next (&it)) {
  399. if (!screen_num) {
  400. screen = it.data;
  401. break;
  402. }
  403. screen_num--;
  404. }
  405. return screen;
  406. }
  407. static int pixfmt_from_pixmap_format(AVFormatContext *s, int depth,
  408. int *pix_fmt, int *bpp)
  409. {
  410. XCBGrabContext *c = s->priv_data;
  411. const xcb_setup_t *setup = xcb_get_setup(c->conn);
  412. const xcb_format_t *fmt = xcb_setup_pixmap_formats(setup);
  413. int length = xcb_setup_pixmap_formats_length(setup);
  414. *pix_fmt = 0;
  415. while (length--) {
  416. if (fmt->depth == depth) {
  417. switch (depth) {
  418. case 32:
  419. if (fmt->bits_per_pixel == 32)
  420. *pix_fmt = AV_PIX_FMT_0RGB;
  421. break;
  422. case 24:
  423. if (fmt->bits_per_pixel == 32)
  424. *pix_fmt = AV_PIX_FMT_0RGB32;
  425. else if (fmt->bits_per_pixel == 24)
  426. *pix_fmt = AV_PIX_FMT_RGB24;
  427. break;
  428. case 16:
  429. if (fmt->bits_per_pixel == 16)
  430. *pix_fmt = AV_PIX_FMT_RGB565;
  431. break;
  432. case 15:
  433. if (fmt->bits_per_pixel == 16)
  434. *pix_fmt = AV_PIX_FMT_RGB555;
  435. break;
  436. case 8:
  437. if (fmt->bits_per_pixel == 8)
  438. *pix_fmt = AV_PIX_FMT_RGB8;
  439. break;
  440. }
  441. }
  442. if (*pix_fmt) {
  443. *bpp = fmt->bits_per_pixel;
  444. return 0;
  445. }
  446. fmt++;
  447. }
  448. avpriv_report_missing_feature(s, "Mapping this pixmap format");
  449. return AVERROR_PATCHWELCOME;
  450. }
  451. static int create_stream(AVFormatContext *s)
  452. {
  453. XCBGrabContext *c = s->priv_data;
  454. AVStream *st = avformat_new_stream(s, NULL);
  455. xcb_get_geometry_cookie_t gc;
  456. xcb_get_geometry_reply_t *geo;
  457. int64_t frame_size_bits;
  458. int ret;
  459. if (!st)
  460. return AVERROR(ENOMEM);
  461. ret = av_parse_video_rate(&st->avg_frame_rate, c->framerate);
  462. if (ret < 0)
  463. return ret;
  464. avpriv_set_pts_info(st, 64, 1, 1000000);
  465. gc = xcb_get_geometry(c->conn, c->screen->root);
  466. geo = xcb_get_geometry_reply(c->conn, gc, NULL);
  467. if (!geo)
  468. return AVERROR_EXTERNAL;
  469. if (!c->width || !c->height) {
  470. c->width = geo->width;
  471. c->height = geo->height;
  472. }
  473. if (c->x + c->width > geo->width ||
  474. c->y + c->height > geo->height) {
  475. av_log(s, AV_LOG_ERROR,
  476. "Capture area %dx%d at position %d.%d "
  477. "outside the screen size %dx%d\n",
  478. c->width, c->height,
  479. c->x, c->y,
  480. geo->width, geo->height);
  481. free(geo);
  482. return AVERROR(EINVAL);
  483. }
  484. c->time_base = (AVRational){ st->avg_frame_rate.den,
  485. st->avg_frame_rate.num };
  486. c->frame_duration = av_rescale_q(1, c->time_base, AV_TIME_BASE_Q);
  487. c->time_frame = av_gettime();
  488. ret = pixfmt_from_pixmap_format(s, geo->depth, &st->codecpar->format, &c->bpp);
  489. free(geo);
  490. if (ret < 0)
  491. return ret;
  492. frame_size_bits = (int64_t)c->width * c->height * c->bpp;
  493. if (frame_size_bits / 8 + AV_INPUT_BUFFER_PADDING_SIZE > INT_MAX) {
  494. av_log(s, AV_LOG_ERROR, "Captured area is too large\n");
  495. return AVERROR_PATCHWELCOME;
  496. }
  497. c->frame_size = frame_size_bits / 8;
  498. #if CONFIG_LIBXCB_SHM
  499. c->shm_pool = av_buffer_pool_init2(c->frame_size + AV_INPUT_BUFFER_PADDING_SIZE,
  500. c->conn, allocate_shm_buffer, NULL);
  501. if (!c->shm_pool)
  502. return AVERROR(ENOMEM);
  503. #endif
  504. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  505. st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  506. st->codecpar->width = c->width;
  507. st->codecpar->height = c->height;
  508. st->codecpar->bit_rate = av_rescale(frame_size_bits, st->avg_frame_rate.num, st->avg_frame_rate.den);
  509. return ret;
  510. }
  511. static void draw_rectangle(AVFormatContext *s)
  512. {
  513. XCBGrabContext *c = s->priv_data;
  514. xcb_gcontext_t gc = xcb_generate_id(c->conn);
  515. uint32_t mask = XCB_GC_FOREGROUND |
  516. XCB_GC_BACKGROUND |
  517. XCB_GC_LINE_WIDTH |
  518. XCB_GC_LINE_STYLE |
  519. XCB_GC_FILL_STYLE;
  520. uint32_t values[] = { c->screen->black_pixel,
  521. c->screen->white_pixel,
  522. c->region_border,
  523. XCB_LINE_STYLE_DOUBLE_DASH,
  524. XCB_FILL_STYLE_SOLID };
  525. xcb_rectangle_t r = { 1, 1,
  526. c->width + c->region_border * 2 - 3,
  527. c->height + c->region_border * 2 - 3 };
  528. xcb_create_gc(c->conn, gc, c->window, mask, values);
  529. xcb_poly_rectangle(c->conn, c->window, gc, 1, &r);
  530. }
  531. static void setup_window(AVFormatContext *s)
  532. {
  533. XCBGrabContext *c = s->priv_data;
  534. uint32_t mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
  535. uint32_t values[] = { 1,
  536. XCB_EVENT_MASK_EXPOSURE |
  537. XCB_EVENT_MASK_STRUCTURE_NOTIFY };
  538. av_unused xcb_rectangle_t rect = { 0, 0, c->width, c->height };
  539. c->window = xcb_generate_id(c->conn);
  540. xcb_create_window(c->conn, XCB_COPY_FROM_PARENT,
  541. c->window,
  542. c->screen->root,
  543. c->x - c->region_border,
  544. c->y - c->region_border,
  545. c->width + c->region_border * 2,
  546. c->height + c->region_border * 2,
  547. 0,
  548. XCB_WINDOW_CLASS_INPUT_OUTPUT,
  549. XCB_COPY_FROM_PARENT,
  550. mask, values);
  551. #if CONFIG_LIBXCB_SHAPE
  552. xcb_shape_rectangles(c->conn, XCB_SHAPE_SO_SUBTRACT,
  553. XCB_SHAPE_SK_BOUNDING, XCB_CLIP_ORDERING_UNSORTED,
  554. c->window,
  555. c->region_border, c->region_border,
  556. 1, &rect);
  557. #endif
  558. xcb_map_window(c->conn, c->window);
  559. draw_rectangle(s);
  560. }
  561. #define CROSSHAIR_CURSOR 34
  562. static xcb_rectangle_t rectangle_from_corners(xcb_point_t *corner_a,
  563. xcb_point_t *corner_b)
  564. {
  565. xcb_rectangle_t rectangle;
  566. rectangle.x = FFMIN(corner_a->x, corner_b->x);
  567. rectangle.y = FFMIN(corner_a->y, corner_b->y);
  568. rectangle.width = FFABS(corner_a->x - corner_b->x);
  569. rectangle.height = FFABS(corner_a->y - corner_b->y);
  570. return rectangle;
  571. }
  572. static int select_region(AVFormatContext *s)
  573. {
  574. XCBGrabContext *c = s->priv_data;
  575. xcb_connection_t *conn = c->conn;
  576. xcb_screen_t *screen = c->screen;
  577. int ret = 0, done = 0, was_pressed = 0;
  578. xcb_cursor_t cursor;
  579. xcb_font_t cursor_font;
  580. xcb_point_t press_position;
  581. xcb_generic_event_t *event;
  582. xcb_rectangle_t rectangle = { 0 };
  583. xcb_grab_pointer_reply_t *reply;
  584. xcb_grab_pointer_cookie_t cookie;
  585. xcb_window_t root_window = screen->root;
  586. xcb_gcontext_t gc = xcb_generate_id(conn);
  587. uint32_t mask = XCB_GC_FUNCTION | XCB_GC_SUBWINDOW_MODE;
  588. uint32_t values[] = { XCB_GX_INVERT, XCB_SUBWINDOW_MODE_INCLUDE_INFERIORS };
  589. xcb_create_gc(conn, gc, root_window, mask, values);
  590. cursor_font = xcb_generate_id(conn);
  591. xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
  592. cursor = xcb_generate_id(conn);
  593. xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
  594. CROSSHAIR_CURSOR, CROSSHAIR_CURSOR + 1, 0, 0, 0,
  595. 0xFFFF, 0xFFFF, 0xFFFF);
  596. cookie = xcb_grab_pointer(conn, 0, root_window,
  597. XCB_EVENT_MASK_BUTTON_PRESS |
  598. XCB_EVENT_MASK_BUTTON_RELEASE |
  599. XCB_EVENT_MASK_BUTTON_MOTION,
  600. XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
  601. root_window, cursor, XCB_CURRENT_TIME);
  602. reply = xcb_grab_pointer_reply(conn, cookie, NULL);
  603. if (!reply || reply->status != XCB_GRAB_STATUS_SUCCESS) {
  604. av_log(s, AV_LOG_ERROR,
  605. "Failed to select region. Could not grab pointer.\n");
  606. ret = AVERROR(EIO);
  607. free(reply);
  608. goto fail;
  609. }
  610. free(reply);
  611. xcb_grab_server(conn);
  612. while (!done && (event = xcb_wait_for_event(conn))) {
  613. switch (event->response_type & ~0x80) {
  614. case XCB_BUTTON_PRESS: {
  615. xcb_button_press_event_t *press = (xcb_button_press_event_t *)event;
  616. press_position = (xcb_point_t){ press->event_x, press->event_y };
  617. rectangle.x = press_position.x;
  618. rectangle.y = press_position.y;
  619. xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
  620. was_pressed = 1;
  621. break;
  622. }
  623. case XCB_MOTION_NOTIFY: {
  624. if (was_pressed) {
  625. xcb_motion_notify_event_t *motion =
  626. (xcb_motion_notify_event_t *)event;
  627. xcb_point_t cursor_position = { motion->event_x, motion->event_y };
  628. xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
  629. rectangle = rectangle_from_corners(&press_position, &cursor_position);
  630. xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
  631. }
  632. break;
  633. }
  634. case XCB_BUTTON_RELEASE: {
  635. xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
  636. done = 1;
  637. break;
  638. }
  639. default:
  640. break;
  641. }
  642. xcb_flush(conn);
  643. free(event);
  644. }
  645. c->width = rectangle.width;
  646. c->height = rectangle.height;
  647. if (c->width && c->height) {
  648. c->x = rectangle.x;
  649. c->y = rectangle.y;
  650. } else {
  651. c->x = 0;
  652. c->y = 0;
  653. }
  654. xcb_ungrab_server(conn);
  655. xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
  656. xcb_flush(conn);
  657. fail:
  658. xcb_free_cursor(conn, cursor);
  659. xcb_close_font(conn, cursor_font);
  660. xcb_free_gc(conn, gc);
  661. return ret;
  662. }
  663. static av_cold int xcbgrab_read_header(AVFormatContext *s)
  664. {
  665. XCBGrabContext *c = s->priv_data;
  666. int screen_num, ret;
  667. const xcb_setup_t *setup;
  668. char *display_name = av_strdup(s->url);
  669. if (!display_name)
  670. return AVERROR(ENOMEM);
  671. if (!sscanf(s->url, "%[^+]+%d,%d", display_name, &c->x, &c->y)) {
  672. *display_name = 0;
  673. sscanf(s->url, "+%d,%d", &c->x, &c->y);
  674. }
  675. c->conn = xcb_connect(display_name[0] ? display_name : NULL, &screen_num);
  676. av_freep(&display_name);
  677. if ((ret = xcb_connection_has_error(c->conn))) {
  678. av_log(s, AV_LOG_ERROR, "Cannot open display %s, error %d.\n",
  679. s->url[0] ? s->url : "default", ret);
  680. return AVERROR(EIO);
  681. }
  682. setup = xcb_get_setup(c->conn);
  683. c->screen = get_screen(setup, screen_num);
  684. if (!c->screen) {
  685. av_log(s, AV_LOG_ERROR, "The screen %d does not exist.\n",
  686. screen_num);
  687. xcbgrab_read_close(s);
  688. return AVERROR(EIO);
  689. }
  690. if (c->select_region) {
  691. ret = select_region(s);
  692. if (ret < 0) {
  693. xcbgrab_read_close(s);
  694. return ret;
  695. }
  696. }
  697. ret = create_stream(s);
  698. if (ret < 0) {
  699. xcbgrab_read_close(s);
  700. return ret;
  701. }
  702. #if CONFIG_LIBXCB_SHM
  703. c->has_shm = check_shm(c->conn);
  704. #endif
  705. #if CONFIG_LIBXCB_XFIXES
  706. if (c->draw_mouse) {
  707. if (!(c->draw_mouse = check_xfixes(c->conn))) {
  708. av_log(s, AV_LOG_WARNING,
  709. "XFixes not available, cannot draw the mouse.\n");
  710. }
  711. if (c->bpp < 24) {
  712. avpriv_report_missing_feature(s, "%d bits per pixel screen",
  713. c->bpp);
  714. c->draw_mouse = 0;
  715. }
  716. }
  717. #endif
  718. if (c->show_region)
  719. setup_window(s);
  720. return 0;
  721. }
  722. AVInputFormat ff_xcbgrab_demuxer = {
  723. .name = "x11grab",
  724. .long_name = NULL_IF_CONFIG_SMALL("X11 screen capture, using XCB"),
  725. .priv_data_size = sizeof(XCBGrabContext),
  726. .read_header = xcbgrab_read_header,
  727. .read_packet = xcbgrab_read_packet,
  728. .read_close = xcbgrab_read_close,
  729. .flags = AVFMT_NOFILE,
  730. .priv_class = &xcbgrab_class,
  731. };