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
22 #include "config.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <xcb/xcb.h>
27
28 #if CONFIG_LIBXCB_XFIXES
29 #include <xcb/xfixes.h>
30 #endif
31
32 #if CONFIG_LIBXCB_SHM
33 #include <sys/shm.h>
34 #include <xcb/shm.h>
35 #endif
36
37 #if CONFIG_LIBXCB_SHAPE
38 #include <xcb/shape.h>
39 #endif
40
46
49
52
56 #if CONFIG_LIBXCB_SHM
58 #endif
62
68
75
77
80
81 #define FOLLOW_CENTER -1
82
83 #define OFFSET(x) offsetof(XCBGrabContext, x)
84 #define D AV_OPT_FLAG_DECODING_PARAM
86 {
"window_id",
"Window to capture.",
OFFSET(window_id),
AV_OPT_TYPE_INT, { .i64 = XCB_NONE }, 0, UINT32_MAX,
D },
94 { "follow_mouse", "Move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region.",
96 {
"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" },
97 {
"show_region",
"Show the grabbing region.",
OFFSET(show_region),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1,
D },
98 {
"region_border",
"Set the region border thickness.",
OFFSET(region_border),
AV_OPT_TYPE_INT, { .i64 = 3 }, 1, 128,
D },
101 };
102
109 };
110
112 xcb_query_pointer_reply_t *p,
113 xcb_get_geometry_reply_t *geo)
114 {
116 int x =
c->x, y =
c->y;
117 int w =
c->width,
h =
c->height,
f =
c->follow_mouse;
118 int p_x, p_y;
119
120 if (!p || !geo)
122
123 p_x = p->win_x;
124 p_y = p->win_y;
125
129 } else {
131 int right = x +
w -
f;
133 int bottom = y +
h -
f;
134 if (p_x > right) {
135 x += p_x - right;
136 }
else if (p_x <
left) {
138 }
139 if (p_y > bottom) {
140 y += p_y - bottom;
141 } else if (p_y < top) {
142 y -= top - p_y;
143 }
144 }
145
148
149 return 0;
150 }
151
153 {
154 free(opaque);
155 }
156
158 {
160 xcb_get_image_cookie_t iq;
161 xcb_get_image_reply_t *
img;
162 xcb_drawable_t drawable =
c->window_id;
163 xcb_generic_error_t *e =
NULL;
165 int length;
166
167 iq = xcb_get_image(
c->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
168 c->x,
c->y,
c->width,
c->height, ~0);
169
170 img = xcb_get_image_reply(
c->conn, iq, &e);
171
172 if (e) {
174 "Cannot get the image data "
175 "event_error: response_type:%u error_code:%u "
176 "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
177 e->response_type, e->error_code,
178 e->sequence, e->resource_id, e->minor_code, e->major_code);
179 free(e);
181 }
182
185
186 data = xcb_get_image_data(
img);
187 length = xcb_get_image_data_length(
img);
188
193 }
194
197
198 return 0;
199 }
200
202 {
204 int64_t curtime, delay;
205
206 c->time_frame +=
c->frame_duration;
207
208 for (;;) {
210 delay =
c->time_frame - curtime;
211 if (delay <= 0)
212 break;
214 }
215
216 return curtime;
217 }
218
219 #if CONFIG_LIBXCB_SHM
220 static int check_shm(xcb_connection_t *conn)
221 {
222 xcb_shm_query_version_cookie_t cookie = xcb_shm_query_version(conn);
223 xcb_shm_query_version_reply_t *reply;
224
225 reply = xcb_shm_query_version_reply(conn, cookie,
NULL);
226 if (reply) {
227 free(reply);
228 return 1;
229 }
230
231 return 0;
232 }
233
234 static void free_shm_buffer(
void *opaque, uint8_t *
data)
235 {
237 }
238
240 {
241 xcb_connection_t *conn = opaque;
246
247 id = shmget(IPC_PRIVATE,
size, IPC_CREAT | 0777);
248 if (id == -1)
250
251 segment = xcb_generate_id(conn);
252 xcb_shm_attach(conn,
segment,
id, 0);
254 shmctl(id, IPC_RMID, 0);
257
261
263 }
264
266 {
268 xcb_shm_get_image_cookie_t iq;
269 xcb_shm_get_image_reply_t *
img;
270 xcb_drawable_t drawable =
c->window_id;
271 xcb_generic_error_t *e =
NULL;
274
276 if (!buf) {
279 }
281
282 iq = xcb_shm_get_image(
c->conn, drawable,
283 c->x,
c->y,
c->width,
c->height, ~0,
284 XCB_IMAGE_FORMAT_Z_PIXMAP,
segment, 0);
285 img = xcb_shm_get_image_reply(
c->conn, iq, &e);
286
288
289 if (e) {
291 "Cannot get the image data "
292 "event_error: response_type:%u error_code:%u "
293 "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
294 e->response_type, e->error_code,
295 e->sequence, e->resource_id, e->minor_code, e->major_code);
296
297 free(e);
300 }
301
303
307
308 return 0;
309 }
310 #endif /* CONFIG_LIBXCB_SHM */
311
312 #if CONFIG_LIBXCB_XFIXES
313 static int check_xfixes(xcb_connection_t *conn)
314 {
315 xcb_xfixes_query_version_cookie_t cookie;
316 xcb_xfixes_query_version_reply_t *reply;
317
318 cookie = xcb_xfixes_query_version(conn, XCB_XFIXES_MAJOR_VERSION,
319 XCB_XFIXES_MINOR_VERSION);
320 reply = xcb_xfixes_query_version_reply(conn, cookie,
NULL);
321
322 if (reply) {
323 free(reply);
324 return 1;
325 }
326 return 0;
327 }
328
329 #define BLEND(target, source, alpha) \
330 (target) + ((source) * (255 - (alpha)) + 255 / 2) / 255
331
333 xcb_query_pointer_reply_t *p,
334 xcb_get_geometry_reply_t *geo,
335 int win_x, int win_y)
336 {
338 uint32_t *cursor;
341 xcb_xfixes_get_cursor_image_cookie_t cc;
342 xcb_xfixes_get_cursor_image_reply_t *ci;
343 int cx, cy, x, y,
w,
h, c_off, i_off;
344
345 cc = xcb_xfixes_get_cursor_image(gr->
conn);
346 ci = xcb_xfixes_get_cursor_image_reply(gr->
conn, cc,
NULL);
347 if (!ci)
348 return;
349
350 cursor = xcb_xfixes_get_cursor_image_cursor_image(ci);
351 if (!cursor)
352 return;
353
354 cx = ci->x - ci->xhot;
355 cy = ci->y - ci->yhot;
356
357 x =
FFMAX(cx, win_x + gr->
x);
358 y =
FFMAX(cy, win_y + gr->
y);
359
360 w =
FFMIN(cx + ci->width, win_x + gr->
x + gr->
width) - x;
362
363 c_off = x - cx;
364 i_off = x - gr->
x - win_x;
365
366 cursor += (y - cy) * ci->width;
368
369 for (y = 0; y <
h; y++) {
370 cursor += c_off;
372 for (x = 0; x <
w; x++, cursor++, image +=
stride) {
374
376 g = (*cursor >> 8) & 0xff;
377 b = (*cursor >> 16) & 0xff;
378 a = (*cursor >> 24) & 0xff;
379
381 continue;
382
387 } else {
388 image[0] = BLEND(
r, image[0],
a);
389 image[1] = BLEND(
g, image[1],
a);
390 image[2] = BLEND(
b, image[2],
a);
391 }
392
393 }
394 cursor += ci->width -
w - c_off;
396 }
397
398 free(ci);
399 }
400 #endif /* CONFIG_LIBXCB_XFIXES */
401
403 {
405 const uint32_t args[] = { win_x +
c->x -
c->region_border,
406 win_y +
c->y -
c->region_border };
407
408 xcb_configure_window(
c->conn,
410 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
411 args);
412 }
413
415 {
417 xcb_query_pointer_cookie_t pc;
418 xcb_get_geometry_cookie_t gc;
419 xcb_translate_coordinates_cookie_t
tc;
420 xcb_query_pointer_reply_t *p =
NULL;
421 xcb_get_geometry_reply_t *geo =
NULL;
422 xcb_translate_coordinates_reply_t *translate =
NULL;
425 int win_x = 0, win_y = 0;
426
429
430 if (
c->follow_mouse ||
c->draw_mouse) {
431 pc = xcb_query_pointer(
c->conn,
c->window_id);
432 gc = xcb_get_geometry(
c->conn,
c->window_id);
433 p = xcb_query_pointer_reply(
c->conn, pc,
NULL);
434 if (!p) {
437 }
438 geo = xcb_get_geometry_reply(
c->conn, gc,
NULL);
439 if (!geo) {
441 free(p);
443 }
444 }
445 if (
c->window_id !=
c->screen->root) {
446 tc = xcb_translate_coordinates(
c->conn,
c->window_id,
c->screen->root, 0, 0);
447 translate = xcb_translate_coordinates_reply(
c->conn,
tc,
NULL);
448 if (!translate) {
449 free(p);
450 free(geo);
453 }
454 win_x = translate->dst_x;
455 win_y = translate->dst_y;
456 free(translate);
457 }
458
459 if (
c->follow_mouse && p->same_screen)
461
464
465 #if CONFIG_LIBXCB_SHM
466 if (
c->has_shm && xcbgrab_frame_shm(
s,
pkt) < 0) {
469 }
470 #endif
475
476 #if CONFIG_LIBXCB_XFIXES
477 if (
ret >= 0 &&
c->draw_mouse && p->same_screen)
478 xcbgrab_draw_mouse(
s,
pkt, p, geo, win_x, win_y);
479 #endif
480
481 free(p);
482 free(geo);
483
485 }
486
488 {
490
491 #if CONFIG_LIBXCB_SHM
493 #endif
494
495 xcb_disconnect(
ctx->conn);
496
497 return 0;
498 }
499
500 static xcb_screen_t *
get_screen(
const xcb_setup_t *setup,
int screen_num)
501 {
502 xcb_screen_iterator_t
it = xcb_setup_roots_iterator(setup);
503 xcb_screen_t *screen =
NULL;
504
505 for (;
it.rem > 0; xcb_screen_next (&
it)) {
506 if (!screen_num) {
508 break;
509 }
510
511 screen_num--;
512 }
513
514 return screen;
515 }
516
519 {
521 const xcb_setup_t *setup = xcb_get_setup(
c->conn);
522 const xcb_format_t *fmt = xcb_setup_pixmap_formats(setup);
523 int length = xcb_setup_pixmap_formats_length(setup);
524
526
527 while (length--) {
528 if (fmt->depth == depth) {
529 switch (depth) {
530 case 32:
531 if (fmt->bits_per_pixel == 32)
532 *
pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
534 break;
535 case 24:
536 if (fmt->bits_per_pixel == 32)
537 *
pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
539 else if (fmt->bits_per_pixel == 24)
540 *
pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
542 break;
543 case 16:
544 if (fmt->bits_per_pixel == 16)
545 *
pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
547 break;
548 case 15:
549 if (fmt->bits_per_pixel == 16)
550 *
pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
552 break;
553 case 8:
554 if (fmt->bits_per_pixel == 8)
556 break;
557 }
558 }
559
561 *bpp = fmt->bits_per_pixel;
562 return 0;
563 }
564
565 fmt++;
566 }
568
570 }
571
573 {
576 xcb_get_geometry_cookie_t gc;
577 xcb_get_geometry_reply_t *geo;
578 int64_t frame_size_bits;
580
581 if (!st)
583
587
589
590 gc = xcb_get_geometry(
c->conn,
c->window_id);
591 geo = xcb_get_geometry_reply(
c->conn, gc,
NULL);
592 if (!geo) {
595 }
596
597 if (!
c->width || !
c->height) {
598 c->width = geo->width;
599 c->height = geo->height;
600 }
601
602 if (
c->x +
c->width > geo->width ||
603 c->y +
c->height > geo->height) {
605 "Capture area %dx%d at position %d.%d "
606 "outside the screen size %dx%d\n",
609 geo->width, geo->height);
610 free(geo);
612 }
613
618
620 free(geo);
623
624 frame_size_bits = (int64_t)
c->width *
c->height *
c->bpp;
628 }
629 c->frame_size = frame_size_bits / 8;
630
631 #if CONFIG_LIBXCB_SHM
633 c->conn, allocate_shm_buffer,
NULL);
636 #endif
637
643
645 }
646
648 {
650 xcb_gcontext_t gc = xcb_generate_id(
c->conn);
651 uint32_t
mask = XCB_GC_FOREGROUND |
652 XCB_GC_BACKGROUND |
653 XCB_GC_LINE_WIDTH |
654 XCB_GC_LINE_STYLE |
655 XCB_GC_FILL_STYLE;
656 uint32_t
values[] = {
c->screen->black_pixel,
657 c->screen->white_pixel,
659 XCB_LINE_STYLE_DOUBLE_DASH,
660 XCB_FILL_STYLE_SOLID };
661 xcb_rectangle_t
r = { 1, 1,
662 c->width +
c->region_border * 2 - 3,
663 c->height +
c->region_border * 2 - 3 };
664
666
667 xcb_poly_rectangle(
c->conn,
c->window, gc, 1, &
r);
668 }
669
671 {
673 uint32_t
mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
675 XCB_EVENT_MASK_EXPOSURE |
676 XCB_EVENT_MASK_STRUCTURE_NOTIFY };
678
679 c->window = xcb_generate_id(
c->conn);
680
681 xcb_create_window(
c->conn, XCB_COPY_FROM_PARENT,
684 c->x -
c->region_border,
685 c->y -
c->region_border,
686 c->width +
c->region_border * 2,
687 c->height +
c->region_border * 2,
688 0,
689 XCB_WINDOW_CLASS_INPUT_OUTPUT,
690 XCB_COPY_FROM_PARENT,
692
693 #if CONFIG_LIBXCB_SHAPE
694 xcb_shape_rectangles(
c->conn, XCB_SHAPE_SO_SUBTRACT,
695 XCB_SHAPE_SK_BOUNDING, XCB_CLIP_ORDERING_UNSORTED,
697 c->region_border,
c->region_border,
699 #endif
700
701 xcb_map_window(
c->conn,
c->window);
702
704 }
705
706 #define CROSSHAIR_CURSOR 34
707
709 xcb_point_t *corner_b)
710 {
711 xcb_rectangle_t rectangle;
712 rectangle.x =
FFMIN(corner_a->x, corner_b->x);
713 rectangle.y =
FFMIN(corner_a->y, corner_b->y);
714 rectangle.width =
FFABS(corner_a->x - corner_b->x);
715 rectangle.height =
FFABS(corner_a->y - corner_b->y);
716 return rectangle;
717 }
718
720 {
722 xcb_connection_t *conn =
c->conn;
723 xcb_screen_t *screen =
c->screen;
724
725 int ret = 0, done = 0, was_pressed = 0;
726 xcb_cursor_t cursor;
727 xcb_font_t cursor_font;
728 xcb_point_t press_position;
729 xcb_generic_event_t *event;
730 xcb_rectangle_t rectangle = { 0 };
731 xcb_grab_pointer_reply_t *reply;
732 xcb_grab_pointer_cookie_t cookie;
733
734 xcb_window_t root_window = screen->root;
735 xcb_gcontext_t gc = xcb_generate_id(conn);
736 uint32_t
mask = XCB_GC_FUNCTION | XCB_GC_SUBWINDOW_MODE;
737 uint32_t
values[] = { XCB_GX_INVERT, XCB_SUBWINDOW_MODE_INCLUDE_INFERIORS };
738 xcb_create_gc(conn, gc, root_window,
mask,
values);
739
740 cursor_font = xcb_generate_id(conn);
741 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
742 cursor = xcb_generate_id(conn);
743 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
745 0xFFFF, 0xFFFF, 0xFFFF);
746 cookie = xcb_grab_pointer(conn, 0, root_window,
747 XCB_EVENT_MASK_BUTTON_PRESS |
748 XCB_EVENT_MASK_BUTTON_RELEASE |
749 XCB_EVENT_MASK_BUTTON_MOTION,
750 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
751 root_window, cursor, XCB_CURRENT_TIME);
752 reply = xcb_grab_pointer_reply(conn, cookie,
NULL);
753 if (!reply || reply->status != XCB_GRAB_STATUS_SUCCESS) {
755 "Failed to select region. Could not grab pointer.\n");
757 free(reply);
759 }
760 free(reply);
761
762 xcb_grab_server(conn);
763
764 while (!done && (event = xcb_wait_for_event(conn))) {
765 switch (event->response_type & ~0x80) {
766 case XCB_BUTTON_PRESS: {
767 xcb_button_press_event_t *press = (xcb_button_press_event_t *)event;
768 press_position = (xcb_point_t){ press->event_x, press->event_y };
769 rectangle.x = press_position.x;
770 rectangle.y = press_position.y;
771 xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
772 was_pressed = 1;
773 break;
774 }
775 case XCB_MOTION_NOTIFY: {
776 if (was_pressed) {
777 xcb_motion_notify_event_t *motion =
778 (xcb_motion_notify_event_t *)event;
779 xcb_point_t cursor_position = { motion->event_x, motion->event_y };
780 xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
782 xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
783 }
784 break;
785 }
786 case XCB_BUTTON_RELEASE: {
787 xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
788 done = 1;
789 break;
790 }
791 default:
792 break;
793 }
794 xcb_flush(conn);
795 free(event);
796 }
797 c->width = rectangle.width;
798 c->height = rectangle.height;
799 if (
c->width &&
c->height) {
802 } else {
805 }
806 xcb_ungrab_server(conn);
807 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
808 xcb_flush(conn);
809
811 xcb_free_cursor(conn, cursor);
812 xcb_close_font(conn, cursor_font);
813 xcb_free_gc(conn, gc);
815 }
816
818 {
821 const xcb_setup_t *setup;
823
824 if (!display_name)
826
827 if (!sscanf(
s->url,
"%[^+]+%d,%d", display_name, &
c->x, &
c->y)) {
828 *display_name = 0;
829 sscanf(
s->url,
"+%d,%d", &
c->x, &
c->y);
830 }
831
832 c->conn = xcb_connect(display_name[0] ? display_name :
NULL, &screen_num);
834
835 if ((
ret = xcb_connection_has_error(
c->conn))) {
837 s->url[0] ?
s->url :
"default",
ret);
839 }
840
841 setup = xcb_get_setup(
c->conn);
842
846 screen_num);
849 }
850
851 if (
c->window_id == XCB_NONE)
852 c->window_id =
c->screen->root;
853 else {
854 if (
c->select_region) {
856 c->select_region = 0;
857 }
858 if (
c->follow_mouse) {
861 }
862 }
863
864 if (
c->select_region) {
869 }
870 }
871
873
877 }
878
879 #if CONFIG_LIBXCB_SHM
880 c->has_shm = check_shm(
c->conn);
881 #endif
882
883 #if CONFIG_LIBXCB_XFIXES
885 if (!(
c->draw_mouse = check_xfixes(
c->conn))) {
887 "XFixes not available, cannot draw the mouse.\n");
888 }
893 }
894 }
895 #endif
896
899
900 return 0;
901 }
902
912 };