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>
85 #define REGION_WIN_BORDER 3
94 Display *dpy = s->
dpy;
99 screen = DefaultScreen(dpy);
100 gc = XCreateGC(dpy, win, 0, 0);
101 XSetForeground(dpy, gc, WhitePixel(dpy, screen));
102 XSetBackground(dpy, gc, BlackPixel(dpy, screen));
104 XDrawRectangle(dpy, win, gc,
119 Display *dpy = s->
dpy;
121 XSetWindowAttributes attribs;
124 screen = DefaultScreen(dpy);
125 attribs.override_redirect = True;
126 s->
region_win = XCreateWindow(dpy, RootWindow(dpy, screen),
132 InputOutput, CopyFromParent,
133 CWOverrideRedirect, &attribs);
136 rect.width = s->
width;
140 &rect, 1, ShapeSubtract, 0);
142 XSelectInput(dpy, s->
region_win, ExposureMask | StructureNotifyMask);
176 offset = strchr(dpyname,
'+');
178 sscanf(offset,
"%d,%d", &x_off, &y_off);
179 if (strstr(offset,
"nomouse")) {
181 "'nomouse' specification in argument is deprecated: "
182 "use 'draw_mouse' option with value 0 instead\n");
192 av_log(s1,
AV_LOG_INFO,
"device: %s -> display: %s x: %d y: %d width: %d height: %d\n",
195 dpy = XOpenDisplay(dpyname);
210 screen = DefaultScreen(dpy);
213 int screen_w, screen_h;
216 screen_w = DisplayWidth(dpy, screen);
217 screen_h = DisplayHeight(dpy, screen);
218 XQueryPointer(dpy, RootWindow(dpy, screen), &w, &w, &x_off, &y_off, &ret, &ret, &ret);
219 x_off -= x11grab->
width / 2;
220 y_off -= x11grab->
height / 2;
223 av_log(s1,
AV_LOG_INFO,
"followmouse is enabled, resetting grabbing region to x: %d y: %d\n", x_off, y_off);
226 use_shm = XShmQueryExtension(dpy);
227 av_log(s1,
AV_LOG_INFO,
"shared memory extension%s found\n", use_shm ?
"" :
" not");
230 int scr = XDefaultScreen(dpy);
231 image = XShmCreateImage(dpy,
232 DefaultVisual(dpy, scr),
233 DefaultDepth(dpy, scr),
238 x11grab->
shminfo.shmid = shmget(IPC_PRIVATE,
239 image->bytes_per_line * image->height,
241 if (x11grab->
shminfo.shmid == -1) {
246 x11grab->
shminfo.shmaddr = image->data = shmat(x11grab->
shminfo.shmid, 0, 0);
247 x11grab->
shminfo.readOnly = False;
249 if (!XShmAttach(dpy, &x11grab->
shminfo)) {
256 image = XGetImage(dpy, RootWindow(dpy, screen),
262 switch (image->bits_per_pixel) {
268 if ( image->red_mask == 0xf800 &&
269 image->green_mask == 0x07e0 &&
270 image->blue_mask == 0x001f ) {
273 }
else if (image->red_mask == 0x7c00 &&
274 image->green_mask == 0x03e0 &&
275 image->blue_mask == 0x001f ) {
279 av_log(s1,
AV_LOG_ERROR,
"RGB ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
280 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);
286 if ( image->red_mask == 0xff0000 &&
287 image->green_mask == 0x00ff00 &&
288 image->blue_mask == 0x0000ff ) {
290 }
else if ( image->red_mask == 0x0000ff &&
291 image->green_mask == 0x00ff00 &&
292 image->blue_mask == 0xff0000 ) {
295 av_log(s1,
AV_LOG_ERROR,
"rgb ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
296 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);
305 av_log(s1,
AV_LOG_ERROR,
"image depth %i not supported ... aborting\n", image->bits_per_pixel);
347 XFixesCursorImage *xcim;
350 int to_line, to_column;
351 int pixstride = image->bits_per_pixel >> 3;
358 XSetWindowAttributes attr;
361 if (image->bits_per_pixel != 24 && image->bits_per_pixel != 32)
365 s->
c = XCreateFontCursor(dpy, XC_left_ptr);
366 w = DefaultRootWindow(dpy);
368 XChangeWindowAttributes(dpy, w, CWCursor, &attr);
370 xcim = XFixesGetCursorImage(dpy);
372 x = xcim->x - xcim->xhot;
373 y = xcim->y - xcim->yhot;
375 to_line =
FFMIN((y + xcim->height), (height + y_off));
376 to_column =
FFMIN((x + xcim->width), (width + x_off));
378 for (line =
FFMAX(y, y_off); line < to_line; line++) {
379 for (column =
FFMAX(x, x_off); column < to_column; column++) {
380 int xcim_addr = (line -
y) * xcim->width + column - x;
381 int image_addr = ((line - y_off) * width + column -
x_off) * pixstride;
382 int r = (
uint8_t)(xcim->pixels[xcim_addr] >> 0);
383 int g = (
uint8_t)(xcim->pixels[xcim_addr] >> 8);
384 int b = (
uint8_t)(xcim->pixels[xcim_addr] >> 16);
385 int a = (
uint8_t)(xcim->pixels[xcim_addr] >> 24);
388 pix[image_addr+0] =
r;
389 pix[image_addr+1] =
g;
390 pix[image_addr+2] =
b;
393 pix[image_addr+0] = r + (pix[image_addr+0]*(255-
a) + 255/2) / 255;
394 pix[image_addr+1] = g + (pix[image_addr+1]*(255-
a) + 255/2) / 255;
395 pix[image_addr+2] = b + (pix[image_addr+2]*(255-
a) + 255/2) / 255;
427 GetReq(GetImage, req);
433 req->width = image->width;
434 req->height = image->height;
435 req->planeMask = (
unsigned int)AllPlanes;
436 req->format = ZPixmap;
438 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.length) {
444 nbytes = (long)rep.length << 2;
445 _XReadPad(dpy, image->data, nbytes);
472 int64_t curtime, delay;
488 ts.tv_sec = delay / 1000000;
489 ts.tv_nsec = (delay % 1000000) * 1000;
490 nanosleep(&ts, NULL);
494 pkt->
data = image->data;
498 screen = DefaultScreen(dpy);
499 root = RootWindow(dpy, screen);
501 int screen_w, screen_h;
502 int pointer_x, pointer_y,
_;
505 screen_w = DisplayWidth(dpy, screen);
506 screen_h = DisplayHeight(dpy, screen);
507 XQueryPointer(dpy, root, &w, &w, &pointer_x, &pointer_y, &_, &_, &_);
508 if (follow_mouse == -1) {
510 x_off += pointer_x - s->
width / 2 - x_off;
511 y_off += pointer_y - s->
height / 2 - y_off;
515 if (pointer_x > x_off + s->
width - follow_mouse) {
516 x_off += pointer_x - (x_off + s->
width - follow_mouse);
517 }
else if (pointer_x < x_off + follow_mouse)
518 x_off -= (x_off + follow_mouse) - pointer_x;
519 if (pointer_y > y_off + s->
height - follow_mouse) {
520 y_off += pointer_y - (y_off + s->
height - follow_mouse);
521 }
else if (pointer_y < y_off + follow_mouse)
522 y_off -= (y_off + follow_mouse) - pointer_y;
538 for (evt.type = NoEventMask; XCheckMaskEvent(dpy, ExposureMask | StructureNotifyMask, &evt); );
547 if (!XShmGetImage(dpy, root, image, x_off, y_off, AllPlanes)) {
577 shmdt(x11grab->
shminfo.shmaddr);
578 shmctl(x11grab->
shminfo.shmid, IPC_RMID, NULL);
582 if (x11grab->
image) {
583 XDestroyImage(x11grab->
image);
584 x11grab->
image = NULL;
592 XCloseDisplay(x11grab->
dpy);
596 #define OFFSET(x) offsetof(struct x11grab, x)
597 #define DEC AV_OPT_FLAG_DECODING_PARAM
601 {
"follow_mouse",
"move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region",
603 {
"centered",
"keep the mouse pointer at the center of grabbing region when following",
623 .priv_data_size =
sizeof(
struct x11grab),