48 #include <X11/Xlibint.h>
49 #include <X11/Xproto.h>
50 #include <X11/Xutil.h>
52 #include <X11/extensions/shape.h>
53 #include <X11/extensions/XShm.h>
54 #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);
168 char *param, *offset;
173 offset = strchr(param,
'+');
175 sscanf(offset,
"%d,%d", &x_off, &y_off);
176 x11grab->
draw_mouse = !strstr(offset,
"nomouse");
188 av_log(s1,
AV_LOG_INFO,
"device: %s -> display: %s x: %d y: %d width: %d height: %d\n",
191 dpy = XOpenDisplay(param);
205 screen = DefaultScreen(dpy);
208 int screen_w, screen_h;
211 screen_w = DisplayWidth(dpy, screen);
212 screen_h = DisplayHeight(dpy, screen);
213 XQueryPointer(dpy, RootWindow(dpy, screen), &w, &w, &x_off, &y_off, &ret, &ret, &ret);
214 x_off -= x11grab->
width / 2;
215 y_off -= x11grab->
height / 2;
218 av_log(s1,
AV_LOG_INFO,
"followmouse is enabled, resetting grabbing region to x: %d y: %d\n", x_off, y_off);
221 use_shm = XShmQueryExtension(dpy);
222 av_log(s1,
AV_LOG_INFO,
"shared memory extension %s found\n", use_shm ?
"" :
"not");
225 int scr = XDefaultScreen(dpy);
226 image = XShmCreateImage(dpy,
227 DefaultVisual(dpy, scr),
228 DefaultDepth(dpy, scr),
233 x11grab->
shminfo.shmid = shmget(IPC_PRIVATE,
234 image->bytes_per_line * image->height,
236 if (x11grab->
shminfo.shmid == -1) {
241 x11grab->
shminfo.shmaddr = image->data = shmat(x11grab->
shminfo.shmid, 0, 0);
242 x11grab->
shminfo.readOnly = False;
244 if (!XShmAttach(dpy, &x11grab->
shminfo)) {
251 image = XGetImage(dpy, RootWindow(dpy, screen),
257 switch (image->bits_per_pixel) {
263 if ( image->red_mask == 0xf800 &&
264 image->green_mask == 0x07e0 &&
265 image->blue_mask == 0x001f ) {
268 }
else if (image->red_mask == 0x7c00 &&
269 image->green_mask == 0x03e0 &&
270 image->blue_mask == 0x001f ) {
274 av_log(s1,
AV_LOG_ERROR,
"RGB ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
275 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);
281 if ( image->red_mask == 0xff0000 &&
282 image->green_mask == 0x00ff00 &&
283 image->blue_mask == 0x0000ff ) {
285 }
else if ( image->red_mask == 0x0000ff &&
286 image->green_mask == 0x00ff00 &&
287 image->blue_mask == 0xff0000 ) {
290 av_log(s1,
AV_LOG_ERROR,
"rgb ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
291 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);
300 av_log(s1,
AV_LOG_ERROR,
"image depth %i not supported ... aborting\n", image->bits_per_pixel);
341 XFixesCursorImage *xcim;
344 int to_line, to_column;
345 int pixstride = image->bits_per_pixel >> 3;
350 uint8_t *pix = image->
data;
353 if (image->bits_per_pixel != 24 && image->bits_per_pixel != 32)
356 xcim = XFixesGetCursorImage(dpy);
358 x = xcim->x - xcim->xhot;
359 y = xcim->y - xcim->yhot;
361 to_line =
FFMIN((y + xcim->height), (height + y_off));
362 to_column =
FFMIN((x + xcim->width), (width + x_off));
364 for (line =
FFMAX(y, y_off); line < to_line; line++) {
365 for (column =
FFMAX(x, x_off); column < to_column; column++) {
366 int xcim_addr = (line - y) * xcim->width + column - x;
367 int image_addr = ((line - y_off) * width + column -
x_off) * pixstride;
368 int r = (uint8_t)(xcim->pixels[xcim_addr] >> 0);
369 int g = (uint8_t)(xcim->pixels[xcim_addr] >> 8);
370 int b = (uint8_t)(xcim->pixels[xcim_addr] >> 16);
371 int a = (uint8_t)(xcim->pixels[xcim_addr] >> 24);
374 pix[image_addr+0] =
r;
375 pix[image_addr+1] =
g;
376 pix[image_addr+2] =
b;
379 pix[image_addr+0] = r + (pix[image_addr+0]*(255-a) + 255/2) / 255;
380 pix[image_addr+1] = g + (pix[image_addr+1]*(255-a) + 255/2) / 255;
381 pix[image_addr+2] = b + (pix[image_addr+2]*(255-a) + 255/2) / 255;
413 GetReq(GetImage, req);
419 req->width = image->width;
420 req->height = image->height;
421 req->planeMask = (
unsigned int)AllPlanes;
422 req->format = ZPixmap;
424 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.length) {
430 nbytes = (long)rep.length << 2;
431 _XReadPad(dpy, image->data, nbytes);
458 int64_t curtime, delay;
474 ts.tv_sec = delay / 1000000;
475 ts.tv_nsec = (delay % 1000000) * 1000;
476 nanosleep(&ts,
NULL);
480 pkt->
data = image->data;
484 screen = DefaultScreen(dpy);
485 root = RootWindow(dpy, screen);
487 int screen_w, screen_h;
488 int pointer_x, pointer_y,
_;
491 screen_w = DisplayWidth(dpy, screen);
492 screen_h = DisplayHeight(dpy, screen);
493 XQueryPointer(dpy, root, &w, &w, &pointer_x, &pointer_y, &_, &_, &_);
494 if (follow_mouse == -1) {
496 x_off += pointer_x - s->
width / 2 - x_off;
497 y_off += pointer_y - s->
height / 2 - y_off;
501 if (pointer_x > x_off + s->
width - follow_mouse) {
502 x_off += pointer_x - (x_off + s->
width - follow_mouse);
503 }
else if (pointer_x < x_off + follow_mouse)
504 x_off -= (x_off + follow_mouse) - pointer_x;
505 if (pointer_y > y_off + s->
height - follow_mouse) {
506 y_off += pointer_y - (y_off + s->
height - follow_mouse);
507 }
else if (pointer_y < y_off + follow_mouse)
508 y_off -= (y_off + follow_mouse) - pointer_y;
524 for (evt.type = NoEventMask; XCheckMaskEvent(dpy, ExposureMask | StructureNotifyMask, &evt); );
533 if (!XShmGetImage(dpy, root, image, x_off, y_off, AllPlanes)) {
563 shmdt(x11grab->
shminfo.shmaddr);
568 if (x11grab->
image) {
569 XDestroyImage(x11grab->
image);
578 XCloseDisplay(x11grab->
dpy);
582 #define OFFSET(x) offsetof(struct x11_grab, x)
583 #define DEC AV_OPT_FLAG_DECODING_PARAM
588 {
"follow_mouse",
"Move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region.",
590 {
"centered",
"Keep the mouse pointer at the center of grabbing region when following.", 0,
AV_OPT_TYPE_CONST, { -1 }, INT_MIN, INT_MAX,
DEC,
"follow_mouse" },
606 .priv_data_size =
sizeof(
struct x11_grab),