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