46 #include <X11/cursorfont.h>
49 #include <X11/Xlibint.h>
50 #include <X11/Xproto.h>
51 #include <X11/Xutil.h>
53 #include <X11/extensions/shape.h>
54 #include <X11/extensions/XShm.h>
55 #include <X11/extensions/Xfixes.h>
84 #define REGION_WIN_BORDER 3
93 Display *dpy = s->
dpy;
98 screen = DefaultScreen(dpy);
99 gc = XCreateGC(dpy, win, 0, 0);
100 XSetForeground(dpy, gc, WhitePixel(dpy, screen));
101 XSetBackground(dpy, gc, BlackPixel(dpy, screen));
103 XDrawRectangle(dpy, win, gc,
118 Display *dpy = s->
dpy;
120 XSetWindowAttributes attribs;
123 screen = DefaultScreen(dpy);
124 attribs.override_redirect = True;
125 s->
region_win = XCreateWindow(dpy, RootWindow(dpy, screen),
131 InputOutput, CopyFromParent,
132 CWOverrideRedirect, &attribs);
135 rect.width = s->
width;
139 &rect, 1, ShapeSubtract, 0);
141 XSelectInput(dpy, s->
region_win, ExposureMask | StructureNotifyMask);
175 offset = strchr(dpyname,
'+');
177 sscanf(offset,
"%d,%d", &x_off, &y_off);
178 if (strstr(offset,
"nomouse")) {
180 "'nomouse' specification in argument is deprecated: "
181 "use 'draw_mouse' option with value 0 instead\n");
191 av_log(s1,
AV_LOG_INFO,
"device: %s -> display: %s x: %d y: %d width: %d height: %d\n",
194 dpy = XOpenDisplay(dpyname);
209 screen = DefaultScreen(dpy);
212 int screen_w, screen_h;
215 screen_w = DisplayWidth(dpy, screen);
216 screen_h = DisplayHeight(dpy, screen);
217 XQueryPointer(dpy, RootWindow(dpy, screen), &w, &w, &x_off, &y_off, &ret, &ret, &ret);
218 x_off -= x11grab->
width / 2;
219 y_off -= x11grab->
height / 2;
222 av_log(s1,
AV_LOG_INFO,
"followmouse is enabled, resetting grabbing region to x: %d y: %d\n", x_off, y_off);
225 use_shm = XShmQueryExtension(dpy);
226 av_log(s1,
AV_LOG_INFO,
"shared memory extension%s found\n", use_shm ?
"" :
" not");
229 int scr = XDefaultScreen(dpy);
230 image = XShmCreateImage(dpy,
231 DefaultVisual(dpy, scr),
232 DefaultDepth(dpy, scr),
237 x11grab->
shminfo.shmid = shmget(IPC_PRIVATE,
238 image->bytes_per_line * image->height,
240 if (x11grab->
shminfo.shmid == -1) {
245 x11grab->
shminfo.shmaddr = image->data = shmat(x11grab->
shminfo.shmid, 0, 0);
246 x11grab->
shminfo.readOnly = False;
248 if (!XShmAttach(dpy, &x11grab->
shminfo)) {
255 image = XGetImage(dpy, RootWindow(dpy, screen),
261 switch (image->bits_per_pixel) {
267 if ( image->red_mask == 0xf800 &&
268 image->green_mask == 0x07e0 &&
269 image->blue_mask == 0x001f ) {
272 }
else if (image->red_mask == 0x7c00 &&
273 image->green_mask == 0x03e0 &&
274 image->blue_mask == 0x001f ) {
278 av_log(s1,
AV_LOG_ERROR,
"RGB ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
279 av_log(s1,
AV_LOG_ERROR,
"color masks: r 0x%.6lx g 0x%.6lx b 0x%.6lx\n", image->red_mask, image->green_mask, image->blue_mask);
285 if ( image->red_mask == 0xff0000 &&
286 image->green_mask == 0x00ff00 &&
287 image->blue_mask == 0x0000ff ) {
289 }
else if ( image->red_mask == 0x0000ff &&
290 image->green_mask == 0x00ff00 &&
291 image->blue_mask == 0xff0000 ) {
294 av_log(s1,
AV_LOG_ERROR,
"rgb ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
295 av_log(s1,
AV_LOG_ERROR,
"color masks: r 0x%.6lx g 0x%.6lx b 0x%.6lx\n", image->red_mask, image->green_mask, image->blue_mask);
304 av_log(s1,
AV_LOG_ERROR,
"image depth %i not supported ... aborting\n", image->bits_per_pixel);
346 XFixesCursorImage *xcim;
349 int to_line, to_column;
350 int pixstride = image->bits_per_pixel >> 3;
358 XSetWindowAttributes attr;
361 if (image->bits_per_pixel != 24 && image->bits_per_pixel != 32)
364 c = XCreateFontCursor(dpy, XC_left_ptr);
365 w = DefaultRootWindow(dpy);
367 XChangeWindowAttributes(dpy, w, CWCursor, &attr);
369 xcim = XFixesGetCursorImage(dpy);
371 x = xcim->x - xcim->xhot;
372 y = xcim->y - xcim->yhot;
374 to_line =
FFMIN((y + xcim->height), (height + y_off));
375 to_column =
FFMIN((x + xcim->width), (width + x_off));
377 for (line =
FFMAX(y, y_off); line < to_line; line++) {
378 for (column =
FFMAX(x, x_off); column < to_column; column++) {
379 int xcim_addr = (line - y) * xcim->width + column - x;
380 int image_addr = ((line - y_off) * width + column -
x_off) * pixstride;
381 int r = (
uint8_t)(xcim->pixels[xcim_addr] >> 0);
382 int g = (
uint8_t)(xcim->pixels[xcim_addr] >> 8);
383 int b = (
uint8_t)(xcim->pixels[xcim_addr] >> 16);
384 int a = (
uint8_t)(xcim->pixels[xcim_addr] >> 24);
387 pix[image_addr+0] =
r;
388 pix[image_addr+1] =
g;
389 pix[image_addr+2] =
b;
392 pix[image_addr+0] = r + (pix[image_addr+0]*(255-
a) + 255/2) / 255;
393 pix[image_addr+1] = g + (pix[image_addr+1]*(255-
a) + 255/2) / 255;
394 pix[image_addr+2] = b + (pix[image_addr+2]*(255-
a) + 255/2) / 255;
426 GetReq(GetImage, req);
432 req->width = image->width;
433 req->height = image->height;
434 req->planeMask = (
unsigned int)AllPlanes;
435 req->format = ZPixmap;
437 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.length) {
443 nbytes = (long)rep.length << 2;
444 _XReadPad(dpy, image->data, nbytes);
471 int64_t curtime, delay;
487 ts.tv_sec = delay / 1000000;
488 ts.tv_nsec = (delay % 1000000) * 1000;
489 nanosleep(&ts,
NULL);
493 pkt->
data = image->data;
497 screen = DefaultScreen(dpy);
498 root = RootWindow(dpy, screen);
500 int screen_w, screen_h;
501 int pointer_x, pointer_y,
_;
504 screen_w = DisplayWidth(dpy, screen);
505 screen_h = DisplayHeight(dpy, screen);
506 XQueryPointer(dpy, root, &w, &w, &pointer_x, &pointer_y, &_, &_, &_);
507 if (follow_mouse == -1) {
509 x_off += pointer_x - s->
width / 2 - x_off;
510 y_off += pointer_y - s->
height / 2 - y_off;
514 if (pointer_x > x_off + s->
width - follow_mouse) {
515 x_off += pointer_x - (x_off + s->
width - follow_mouse);
516 }
else if (pointer_x < x_off + follow_mouse)
517 x_off -= (x_off + follow_mouse) - pointer_x;
518 if (pointer_y > y_off + s->
height - follow_mouse) {
519 y_off += pointer_y - (y_off + s->
height - follow_mouse);
520 }
else if (pointer_y < y_off + follow_mouse)
521 y_off -= (y_off + follow_mouse) - pointer_y;
537 for (evt.type = NoEventMask; XCheckMaskEvent(dpy, ExposureMask | StructureNotifyMask, &evt); );
546 if (!XShmGetImage(dpy, root, image, x_off, y_off, AllPlanes)) {
576 shmdt(x11grab->
shminfo.shmaddr);
581 if (x11grab->
image) {
582 XDestroyImage(x11grab->
image);
591 XCloseDisplay(x11grab->
dpy);
595 #define OFFSET(x) offsetof(struct x11grab, x)
596 #define DEC AV_OPT_FLAG_DECODING_PARAM
600 {
"follow_mouse",
"move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region",
602 {
"centered",
"keep the mouse pointer at the center of grabbing region when following",
622 .priv_data_size =
sizeof(
struct x11grab),