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
50
53
57 #if CONFIG_LIBXCB_SHM
59 #endif
63
69
76
78
81
82 #define FOLLOW_CENTER -1
83
84 #define OFFSET(x) offsetof(XCBGrabContext, x)
85 #define D AV_OPT_FLAG_DECODING_PARAM
87 {
"window_id",
"Window to capture.",
OFFSET(window_id),
AV_OPT_TYPE_INT, { .i64 = XCB_NONE }, 0, UINT32_MAX,
D },
95 { "follow_mouse", "Move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region.",
97 {
"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, .unit =
"follow_mouse" },
98 {
"show_region",
"Show the grabbing region.",
OFFSET(show_region),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1,
D },
99 {
"region_border",
"Set the region border thickness.",
OFFSET(region_border),
AV_OPT_TYPE_INT, { .i64 = 3 }, 1, 128,
D },
102 };
103
110 };
111
113 xcb_query_pointer_reply_t *p,
114 xcb_get_geometry_reply_t *geo)
115 {
117 int x =
c->x, y =
c->y;
118 int w =
c->width,
h =
c->height,
f =
c->follow_mouse;
119 int p_x, p_y;
120
121 if (!p || !geo)
123
124 p_x = p->win_x;
125 p_y = p->win_y;
126
130 } else {
132 int right = x +
w -
f;
134 int bottom = y +
h -
f;
135 if (p_x > right) {
136 x += p_x - right;
137 }
else if (p_x <
left) {
139 }
140 if (p_y > bottom) {
141 y += p_y - bottom;
142 } else if (p_y < top) {
143 y -= top - p_y;
144 }
145 }
146
149
150 return 0;
151 }
152
154 {
155 free(opaque);
156 }
157
159 {
161 xcb_get_image_cookie_t iq;
162 xcb_get_image_reply_t *
img;
163 xcb_drawable_t drawable =
c->window_id;
164 xcb_generic_error_t *e =
NULL;
166 int length;
167
168 iq = xcb_get_image(
c->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
169 c->x,
c->y,
c->width,
c->height, ~0);
170
171 img = xcb_get_image_reply(
c->conn, iq, &e);
172
173 if (e) {
175 "Cannot get the image data "
176 "event_error: response_type:%u error_code:%u "
177 "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
178 e->response_type, e->error_code,
179 e->sequence, e->resource_id, e->minor_code, e->major_code);
180 free(e);
182 }
183
186
187 data = xcb_get_image_data(
img);
188 length = xcb_get_image_data_length(
img);
189
194 }
195
198
199 return 0;
200 }
201
203 {
206
207 c->time_frame +=
c->frame_duration;
208
209 for (;;) {
211 delay =
c->time_frame - curtime;
212 if (delay <= 0)
213 break;
215 }
216
217 return curtime;
218 }
219
220 #if CONFIG_LIBXCB_SHM
221 static int check_shm(xcb_connection_t *conn)
222 {
223 xcb_shm_query_version_cookie_t cookie = xcb_shm_query_version(conn);
224 xcb_shm_query_version_reply_t *reply;
225
226 reply = xcb_shm_query_version_reply(conn, cookie,
NULL);
227 if (reply) {
228 free(reply);
229 return 1;
230 }
231
232 return 0;
233 }
234
235 static void free_shm_buffer(
void *opaque, uint8_t *
data)
236 {
238 }
239
241 {
242 xcb_connection_t *conn = opaque;
247
248 id = shmget(IPC_PRIVATE,
size, IPC_CREAT | 0777);
249 if (id == -1)
251
252 segment = xcb_generate_id(conn);
253 xcb_shm_attach(conn,
segment,
id, 0);
255 shmctl(id, IPC_RMID, 0);
258
262
264 }
265
267 {
269 xcb_shm_get_image_cookie_t iq;
270 xcb_shm_get_image_reply_t *
img;
271 xcb_drawable_t drawable =
c->window_id;
272 xcb_generic_error_t *e =
NULL;
275
277 if (!buf) {
280 }
282
283 iq = xcb_shm_get_image(
c->conn, drawable,
284 c->x,
c->y,
c->width,
c->height, ~0,
285 XCB_IMAGE_FORMAT_Z_PIXMAP,
segment, 0);
286 img = xcb_shm_get_image_reply(
c->conn, iq, &e);
287
289
290 if (e) {
292 "Cannot get the image data "
293 "event_error: response_type:%u error_code:%u "
294 "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
295 e->response_type, e->error_code,
296 e->sequence, e->resource_id, e->minor_code, e->major_code);
297
298 free(e);
301 }
302
304
308
309 return 0;
310 }
311 #endif /* CONFIG_LIBXCB_SHM */
312
313 #if CONFIG_LIBXCB_XFIXES
314 static int check_xfixes(xcb_connection_t *conn)
315 {
316 xcb_xfixes_query_version_cookie_t cookie;
317 xcb_xfixes_query_version_reply_t *reply;
318
319 cookie = xcb_xfixes_query_version(conn, XCB_XFIXES_MAJOR_VERSION,
320 XCB_XFIXES_MINOR_VERSION);
321 reply = xcb_xfixes_query_version_reply(conn, cookie,
NULL);
322
323 if (reply) {
324 free(reply);
325 return 1;
326 }
327 return 0;
328 }
329
330 #define BLEND(target, source, alpha) \
331 (target) + ((source) * (255 - (alpha)) + 255 / 2) / 255
332
334 xcb_query_pointer_reply_t *p,
335 xcb_get_geometry_reply_t *geo,
336 int win_x, int win_y)
337 {
339 uint32_t *cursor;
342 xcb_xfixes_get_cursor_image_cookie_t cc;
343 xcb_xfixes_get_cursor_image_reply_t *ci;
344 int cx, cy, x, y,
w,
h, c_off, i_off;
345
346 cc = xcb_xfixes_get_cursor_image(gr->
conn);
347 ci = xcb_xfixes_get_cursor_image_reply(gr->
conn, cc,
NULL);
348 if (!ci)
349 return;
350
351 cursor = xcb_xfixes_get_cursor_image_cursor_image(ci);
352 if (!cursor)
353 return;
354
355 cx = ci->x - ci->xhot;
356 cy = ci->y - ci->yhot;
357
358 x =
FFMAX(cx, win_x + gr->
x);
359 y =
FFMAX(cy, win_y + gr->
y);
360
361 w =
FFMIN(cx + ci->width, win_x + gr->
x + gr->
width) - x;
363
364 c_off = x - cx;
365 i_off = x - gr->
x - win_x;
366
367 cursor += (y - cy) * ci->width;
369
370 for (y = 0; y <
h; y++) {
371 cursor += c_off;
373 for (x = 0; x <
w; x++, cursor++, image +=
stride) {
375
377 g = (*cursor >> 8) & 0xff;
378 b = (*cursor >> 16) & 0xff;
379 a = (*cursor >> 24) & 0xff;
380
382 continue;
383
388 } else {
389 image[0] = BLEND(
r, image[0],
a);
390 image[1] = BLEND(
g, image[1],
a);
391 image[2] = BLEND(
b, image[2],
a);
392 }
393
394 }
395 cursor += ci->width -
w - c_off;
397 }
398
399 free(ci);
400 }
401 #endif /* CONFIG_LIBXCB_XFIXES */
402
404 {
406 const uint32_t args[] = { win_x +
c->x -
c->region_border,
407 win_y +
c->y -
c->region_border };
408
409 xcb_configure_window(
c->conn,
411 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
412 args);
413 }
414
416 {
418 xcb_query_pointer_cookie_t pc;
419 xcb_get_geometry_cookie_t gc;
420 xcb_translate_coordinates_cookie_t
tc;
421 xcb_query_pointer_reply_t *p =
NULL;
422 xcb_get_geometry_reply_t *geo =
NULL;
423 xcb_translate_coordinates_reply_t *translate =
NULL;
426 int win_x = 0, win_y = 0;
427
430
431 if (
c->follow_mouse ||
c->draw_mouse) {
432 pc = xcb_query_pointer(
c->conn,
c->window_id);
433 gc = xcb_get_geometry(
c->conn,
c->window_id);
434 p = xcb_query_pointer_reply(
c->conn, pc,
NULL);
435 if (!p) {
438 }
439 geo = xcb_get_geometry_reply(
c->conn, gc,
NULL);
440 if (!geo) {
442 free(p);
444 }
445 }
446 if (
c->window_id !=
c->screen->root) {
447 tc = xcb_translate_coordinates(
c->conn,
c->window_id,
c->screen->root, 0, 0);
448 translate = xcb_translate_coordinates_reply(
c->conn,
tc,
NULL);
449 if (!translate) {
450 free(p);
451 free(geo);
454 }
455 win_x = translate->dst_x;
456 win_y = translate->dst_y;
457 free(translate);
458 }
459
460 if (
c->follow_mouse && p->same_screen)
462
465
466 #if CONFIG_LIBXCB_SHM
467 if (
c->has_shm && xcbgrab_frame_shm(
s,
pkt) < 0) {
470 }
471 #endif
476
477 #if CONFIG_LIBXCB_XFIXES
478 if (
ret >= 0 &&
c->draw_mouse && p->same_screen)
479 xcbgrab_draw_mouse(
s,
pkt, p, geo, win_x, win_y);
480 #endif
481
482 free(p);
483 free(geo);
484
486 }
487
489 {
491
492 #if CONFIG_LIBXCB_SHM
494 #endif
495
496 xcb_disconnect(
ctx->conn);
497
498 return 0;
499 }
500
501 static xcb_screen_t *
get_screen(
const xcb_setup_t *setup,
int screen_num)
502 {
503 xcb_screen_iterator_t
it = xcb_setup_roots_iterator(setup);
504 xcb_screen_t *screen =
NULL;
505
506 for (;
it.rem > 0; xcb_screen_next (&
it)) {
507 if (!screen_num) {
509 break;
510 }
511
512 screen_num--;
513 }
514
515 return screen;
516 }
517
520 {
522 const xcb_setup_t *setup = xcb_get_setup(
c->conn);
523 const xcb_format_t *fmt = xcb_setup_pixmap_formats(setup);
524 int length = xcb_setup_pixmap_formats_length(setup);
525
527
528 while (length--) {
529 if (fmt->depth == depth) {
530 switch (depth) {
531 case 32:
532 if (fmt->bits_per_pixel == 32)
533 *
pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
535 break;
536 case 24:
537 if (fmt->bits_per_pixel == 32)
538 *
pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
540 else if (fmt->bits_per_pixel == 24)
541 *
pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
543 break;
544 case 16:
545 if (fmt->bits_per_pixel == 16)
546 *
pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
548 break;
549 case 15:
550 if (fmt->bits_per_pixel == 16)
551 *
pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
553 break;
554 case 8:
555 if (fmt->bits_per_pixel == 8)
557 break;
558 }
559 }
560
562 *bpp = fmt->bits_per_pixel;
563 return 0;
564 }
565
566 fmt++;
567 }
569
571 }
572
574 {
577 xcb_get_geometry_cookie_t gc;
578 xcb_get_geometry_reply_t *geo;
581
582 if (!st)
584
588
590
591 gc = xcb_get_geometry(
c->conn,
c->window_id);
592 geo = xcb_get_geometry_reply(
c->conn, gc,
NULL);
593 if (!geo) {
596 }
597
598 if (!
c->width || !
c->height) {
599 c->width = geo->width;
600 c->height = geo->height;
601 }
602
603 if (
c->x +
c->width > geo->width ||
604 c->y +
c->height > geo->height) {
606 "Capture area %dx%d at position %d.%d "
607 "outside the screen size %dx%d\n",
610 geo->width, geo->height);
611 free(geo);
613 }
614
619
621 free(geo);
624
625 frame_size_bits = (
int64_t)
c->width *
c->height *
c->bpp;
629 }
630 c->frame_size = frame_size_bits / 8;
631
632 #if CONFIG_LIBXCB_SHM
634 c->conn, allocate_shm_buffer,
NULL);
637 #endif
638
644
646 }
647
649 {
651 xcb_gcontext_t gc = xcb_generate_id(
c->conn);
652 uint32_t
mask = XCB_GC_FOREGROUND |
653 XCB_GC_BACKGROUND |
654 XCB_GC_LINE_WIDTH |
655 XCB_GC_LINE_STYLE |
656 XCB_GC_FILL_STYLE;
657 uint32_t
values[] = {
c->screen->black_pixel,
658 c->screen->white_pixel,
660 XCB_LINE_STYLE_DOUBLE_DASH,
661 XCB_FILL_STYLE_SOLID };
662 xcb_rectangle_t
r = { 1, 1,
663 c->width +
c->region_border * 2 - 3,
664 c->height +
c->region_border * 2 - 3 };
665
667
668 xcb_poly_rectangle(
c->conn,
c->window, gc, 1, &
r);
669 }
670
672 {
674 uint32_t
mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
676 XCB_EVENT_MASK_EXPOSURE |
677 XCB_EVENT_MASK_STRUCTURE_NOTIFY };
679
680 c->window = xcb_generate_id(
c->conn);
681
682 xcb_create_window(
c->conn, XCB_COPY_FROM_PARENT,
685 c->x -
c->region_border,
686 c->y -
c->region_border,
687 c->width +
c->region_border * 2,
688 c->height +
c->region_border * 2,
689 0,
690 XCB_WINDOW_CLASS_INPUT_OUTPUT,
691 XCB_COPY_FROM_PARENT,
693
694 #if CONFIG_LIBXCB_SHAPE
695 xcb_shape_rectangles(
c->conn, XCB_SHAPE_SO_SUBTRACT,
696 XCB_SHAPE_SK_BOUNDING, XCB_CLIP_ORDERING_UNSORTED,
698 c->region_border,
c->region_border,
700 #endif
701
702 xcb_map_window(
c->conn,
c->window);
703
705 }
706
707 #define CROSSHAIR_CURSOR 34
708
710 xcb_point_t *corner_b)
711 {
712 xcb_rectangle_t rectangle;
713 rectangle.x =
FFMIN(corner_a->x, corner_b->x);
714 rectangle.y =
FFMIN(corner_a->y, corner_b->y);
715 rectangle.width =
FFABS(corner_a->x - corner_b->x);
716 rectangle.height =
FFABS(corner_a->y - corner_b->y);
717 return rectangle;
718 }
719
721 {
723 xcb_connection_t *conn =
c->conn;
724 xcb_screen_t *screen =
c->screen;
725
726 int ret = 0, done = 0, was_pressed = 0;
727 xcb_cursor_t cursor;
728 xcb_font_t cursor_font;
729 xcb_point_t press_position;
730 xcb_generic_event_t *event;
731 xcb_rectangle_t rectangle = { 0 };
732 xcb_grab_pointer_reply_t *reply;
733 xcb_grab_pointer_cookie_t cookie;
734
735 xcb_window_t root_window = screen->root;
736 xcb_gcontext_t gc = xcb_generate_id(conn);
737 uint32_t
mask = XCB_GC_FUNCTION | XCB_GC_SUBWINDOW_MODE;
738 uint32_t
values[] = { XCB_GX_INVERT, XCB_SUBWINDOW_MODE_INCLUDE_INFERIORS };
739 xcb_create_gc(conn, gc, root_window,
mask,
values);
740
741 cursor_font = xcb_generate_id(conn);
742 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
743 cursor = xcb_generate_id(conn);
744 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
746 0xFFFF, 0xFFFF, 0xFFFF);
747 cookie = xcb_grab_pointer(conn, 0, root_window,
748 XCB_EVENT_MASK_BUTTON_PRESS |
749 XCB_EVENT_MASK_BUTTON_RELEASE |
750 XCB_EVENT_MASK_BUTTON_MOTION,
751 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
752 root_window, cursor, XCB_CURRENT_TIME);
753 reply = xcb_grab_pointer_reply(conn, cookie,
NULL);
754 if (!reply || reply->status != XCB_GRAB_STATUS_SUCCESS) {
756 "Failed to select region. Could not grab pointer.\n");
758 free(reply);
760 }
761 free(reply);
762
763 xcb_grab_server(conn);
764
765 while (!done && (event = xcb_wait_for_event(conn))) {
766 switch (event->response_type & ~0x80) {
767 case XCB_BUTTON_PRESS: {
768 xcb_button_press_event_t *press = (xcb_button_press_event_t *)event;
769 press_position = (xcb_point_t){ press->event_x, press->event_y };
770 rectangle.x = press_position.x;
771 rectangle.y = press_position.y;
772 xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
773 was_pressed = 1;
774 break;
775 }
776 case XCB_MOTION_NOTIFY: {
777 if (was_pressed) {
778 xcb_motion_notify_event_t *motion =
779 (xcb_motion_notify_event_t *)event;
780 xcb_point_t cursor_position = { motion->event_x, motion->event_y };
781 xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
783 xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
784 }
785 break;
786 }
787 case XCB_BUTTON_RELEASE: {
788 xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
789 done = 1;
790 break;
791 }
792 default:
793 break;
794 }
795 xcb_flush(conn);
796 free(event);
797 }
798 c->width = rectangle.width;
799 c->height = rectangle.height;
800 if (
c->width &&
c->height) {
803 } else {
806 }
807 xcb_ungrab_server(conn);
808 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
809 xcb_flush(conn);
810
812 xcb_free_cursor(conn, cursor);
813 xcb_close_font(conn, cursor_font);
814 xcb_free_gc(conn, gc);
816 }
817
819 {
822 const xcb_setup_t *setup;
824
825 if (!display_name)
827
828 if (!sscanf(
s->url,
"%[^+]+%d,%d", display_name, &
c->x, &
c->y)) {
829 *display_name = 0;
830 if(sscanf(
s->url,
"+%d,%d", &
c->x, &
c->y) != 2) {
833 }
834 }
835
836 c->conn = xcb_connect(display_name[0] ? display_name :
NULL, &screen_num);
838
839 if ((
ret = xcb_connection_has_error(
c->conn))) {
841 s->url[0] ?
s->url :
"default",
ret);
843 }
844
845 setup = xcb_get_setup(
c->conn);
846
850 screen_num);
853 }
854
855 if (
c->window_id == XCB_NONE)
856 c->window_id =
c->screen->root;
857 else {
858 if (
c->select_region) {
860 c->select_region = 0;
861 }
862 if (
c->follow_mouse) {
865 }
866 }
867
868 if (
c->select_region) {
873 }
874 }
875
877
881 }
882
883 #if CONFIG_LIBXCB_SHM
884 c->has_shm = check_shm(
c->conn);
885 #endif
886
887 #if CONFIG_LIBXCB_XFIXES
889 if (!(
c->draw_mouse = check_xfixes(
c->conn))) {
891 "XFixes not available, cannot draw the mouse.\n");
892 }
897 }
898 }
899 #endif
900
903
904 return 0;
905 }
906
916 };