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);
167 char *param, *offset;
175 offset = strchr(param,
'+');
177 sscanf(offset,
"%d,%d", &x_off, &y_off);
178 x11grab->
draw_mouse = !strstr(offset,
"nomouse");
190 av_log(s1,
AV_LOG_INFO,
"device: %s -> display: %s x: %d y: %d width: %d height: %d\n",
193 dpy = XOpenDisplay(param);
207 screen = DefaultScreen(dpy);
210 int screen_w, screen_h;
213 screen_w = DisplayWidth(dpy, screen);
214 screen_h = DisplayHeight(dpy, screen);
215 XQueryPointer(dpy, RootWindow(dpy, screen), &w, &w, &x_off, &y_off, &ret, &ret, &ret);
216 x_off -= x11grab->
width / 2;
217 y_off -= x11grab->
height / 2;
220 av_log(s1,
AV_LOG_INFO,
"followmouse is enabled, resetting grabbing region to x: %d y: %d\n", x_off, y_off);
223 use_shm = XShmQueryExtension(dpy);
224 av_log(s1,
AV_LOG_INFO,
"shared memory extension %s found\n", use_shm ?
"" :
"not");
227 int scr = XDefaultScreen(dpy);
228 image = XShmCreateImage(dpy,
229 DefaultVisual(dpy, scr),
230 DefaultDepth(dpy, scr),
235 x11grab->
shminfo.shmid = shmget(IPC_PRIVATE,
236 image->bytes_per_line * image->height,
238 if (x11grab->
shminfo.shmid == -1) {
243 x11grab->
shminfo.shmaddr = image->data = shmat(x11grab->
shminfo.shmid, 0, 0);
244 x11grab->
shminfo.readOnly = False;
246 if (!XShmAttach(dpy, &x11grab->
shminfo)) {
253 image = XGetImage(dpy, RootWindow(dpy, screen),
259 switch (image->bits_per_pixel) {
265 if ( image->red_mask == 0xf800 &&
266 image->green_mask == 0x07e0 &&
267 image->blue_mask == 0x001f ) {
270 }
else if (image->red_mask == 0x7c00 &&
271 image->green_mask == 0x03e0 &&
272 image->blue_mask == 0x001f ) {
276 av_log(s1,
AV_LOG_ERROR,
"RGB ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
277 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);
283 if ( image->red_mask == 0xff0000 &&
284 image->green_mask == 0x00ff00 &&
285 image->blue_mask == 0x0000ff ) {
287 }
else if ( image->red_mask == 0x0000ff &&
288 image->green_mask == 0x00ff00 &&
289 image->blue_mask == 0xff0000 ) {
292 av_log(s1,
AV_LOG_ERROR,
"rgb ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
293 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);
302 av_log(s1,
AV_LOG_ERROR,
"image depth %i not supported ... aborting\n", image->bits_per_pixel);
344 XFixesCursorImage *xcim;
347 int to_line, to_column;
348 int pixstride = image->bits_per_pixel >> 3;
356 if (image->bits_per_pixel != 24 && image->bits_per_pixel != 32)
359 xcim = XFixesGetCursorImage(dpy);
361 x = xcim->x - xcim->xhot;
362 y = xcim->y - xcim->yhot;
364 to_line =
FFMIN((y + xcim->height), (height + y_off));
365 to_column =
FFMIN((x + xcim->width), (width + x_off));
367 for (line =
FFMAX(y, y_off); line < to_line; line++) {
368 for (column =
FFMAX(x, x_off); column < to_column; column++) {
369 int xcim_addr = (line - y) * xcim->width + column - x;
370 int image_addr = ((line - y_off) * width + column -
x_off) * pixstride;
371 int r = (
uint8_t)(xcim->pixels[xcim_addr] >> 0);
372 int g = (
uint8_t)(xcim->pixels[xcim_addr] >> 8);
373 int b = (
uint8_t)(xcim->pixels[xcim_addr] >> 16);
374 int a = (
uint8_t)(xcim->pixels[xcim_addr] >> 24);
377 pix[image_addr+0] =
r;
378 pix[image_addr+1] =
g;
379 pix[image_addr+2] =
b;
382 pix[image_addr+0] = r + (pix[image_addr+0]*(255-a) + 255/2) / 255;
383 pix[image_addr+1] = g + (pix[image_addr+1]*(255-a) + 255/2) / 255;
384 pix[image_addr+2] = b + (pix[image_addr+2]*(255-a) + 255/2) / 255;
416 GetReq(GetImage, req);
422 req->width = image->width;
423 req->height = image->height;
424 req->planeMask = (
unsigned int)AllPlanes;
425 req->format = ZPixmap;
427 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.length) {
433 nbytes = (long)rep.length << 2;
434 _XReadPad(dpy, image->data, nbytes);
461 int64_t curtime, delay;
477 ts.tv_sec = delay / 1000000;
478 ts.tv_nsec = (delay % 1000000) * 1000;
479 nanosleep(&ts,
NULL);
483 pkt->
data = image->data;
487 screen = DefaultScreen(dpy);
488 root = RootWindow(dpy, screen);
490 int screen_w, screen_h;
491 int pointer_x, pointer_y,
_;
494 screen_w = DisplayWidth(dpy, screen);
495 screen_h = DisplayHeight(dpy, screen);
496 XQueryPointer(dpy, root, &w, &w, &pointer_x, &pointer_y, &_, &_, &_);
497 if (follow_mouse == -1) {
499 x_off += pointer_x - s->
width / 2 - x_off;
500 y_off += pointer_y - s->
height / 2 - y_off;
504 if (pointer_x > x_off + s->
width - follow_mouse) {
505 x_off += pointer_x - (x_off + s->
width - follow_mouse);
506 }
else if (pointer_x < x_off + follow_mouse)
507 x_off -= (x_off + follow_mouse) - pointer_x;
508 if (pointer_y > y_off + s->
height - follow_mouse) {
509 y_off += pointer_y - (y_off + s->
height - follow_mouse);
510 }
else if (pointer_y < y_off + follow_mouse)
511 y_off -= (y_off + follow_mouse) - pointer_y;
527 for (evt.type = NoEventMask; XCheckMaskEvent(dpy, ExposureMask | StructureNotifyMask, &evt); );
536 if (!XShmGetImage(dpy, root, image, x_off, y_off, AllPlanes)) {
566 shmdt(x11grab->
shminfo.shmaddr);
571 if (x11grab->
image) {
572 XDestroyImage(x11grab->
image);
581 XCloseDisplay(x11grab->
dpy);
585 #define OFFSET(x) offsetof(struct x11grab, x)
586 #define DEC AV_OPT_FLAG_DECODING_PARAM
591 {
"follow_mouse",
"Move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region.",
593 {
"centered",
"Keep the mouse pointer at the center of grabbing region when following.", 0,
AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX,
DEC,
"follow_mouse" },
609 .priv_data_size =
sizeof(
struct x11grab),