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