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>
87 #define REGION_WIN_BORDER 3
96 Display *dpy = s->
dpy;
101 screen = DefaultScreen(dpy);
102 gc = XCreateGC(dpy, win, 0, 0);
103 XSetForeground(dpy, gc, WhitePixel(dpy, screen));
104 XSetBackground(dpy, gc, BlackPixel(dpy, screen));
106 XDrawRectangle(dpy, win, gc,
121 Display *dpy = s->
dpy;
123 XSetWindowAttributes attribs;
126 screen = DefaultScreen(dpy);
127 attribs.override_redirect = True;
128 s->
region_win = XCreateWindow(dpy, RootWindow(dpy, screen),
134 InputOutput, CopyFromParent,
135 CWOverrideRedirect, &attribs);
138 rect.width = s->
width;
142 &rect, 1, ShapeSubtract, 0);
144 XSelectInput(dpy, s->
region_win, ExposureMask | StructureNotifyMask);
180 offset = strchr(dpyname,
'+');
182 sscanf(offset,
"%d,%d", &x_off, &y_off);
183 if (strstr(offset,
"nomouse")) {
185 "'nomouse' specification in argument is deprecated: "
186 "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) {
266 color_map = DefaultColormap(dpy, screen);
267 for (i = 0; i < 256; ++i)
269 XQueryColors(dpy, color_map, color, 256);
270 for (i = 0; i < 256; ++i)
271 x11grab->
palette[i] = (color[i].red & 0xFF00) << 8 |
272 (color[i].green & 0xFF00) |
273 (color[i].blue & 0xFF00) >> 8;
277 if ( image->red_mask == 0xf800 &&
278 image->green_mask == 0x07e0 &&
279 image->blue_mask == 0x001f ) {
282 }
else if (image->red_mask == 0x7c00 &&
283 image->green_mask == 0x03e0 &&
284 image->blue_mask == 0x001f ) {
288 av_log(s1,
AV_LOG_ERROR,
"RGB ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
289 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);
295 if ( image->red_mask == 0xff0000 &&
296 image->green_mask == 0x00ff00 &&
297 image->blue_mask == 0x0000ff ) {
299 }
else if ( image->red_mask == 0x0000ff &&
300 image->green_mask == 0x00ff00 &&
301 image->blue_mask == 0xff0000 ) {
304 av_log(s1,
AV_LOG_ERROR,
"rgb ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
305 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);
314 av_log(s1,
AV_LOG_ERROR,
"image depth %i not supported ... aborting\n", image->bits_per_pixel);
356 XFixesCursorImage *xcim;
359 int to_line, to_column;
360 int pixstride = image->bits_per_pixel >> 3;
367 XSetWindowAttributes attr;
370 if (image->bits_per_pixel != 24 && image->bits_per_pixel != 32)
374 s->
c = XCreateFontCursor(dpy, XC_left_ptr);
375 w = DefaultRootWindow(dpy);
377 XChangeWindowAttributes(dpy, w, CWCursor, &attr);
379 xcim = XFixesGetCursorImage(dpy);
381 x = xcim->x - xcim->xhot;
382 y = xcim->y - xcim->yhot;
384 to_line =
FFMIN((y + xcim->height), (height + y_off));
385 to_column =
FFMIN((x + xcim->width), (width + x_off));
387 for (line =
FFMAX(y, y_off); line < to_line; line++) {
388 for (column =
FFMAX(x, x_off); column < to_column; column++) {
389 int xcim_addr = (line -
y) * xcim->width + column - x;
390 int image_addr = ((line - y_off) * width + column -
x_off) * pixstride;
391 int r = (
uint8_t)(xcim->pixels[xcim_addr] >> 0);
392 int g = (
uint8_t)(xcim->pixels[xcim_addr] >> 8);
393 int b = (
uint8_t)(xcim->pixels[xcim_addr] >> 16);
394 int a = (
uint8_t)(xcim->pixels[xcim_addr] >> 24);
397 pix[image_addr+0] =
r;
398 pix[image_addr+1] =
g;
399 pix[image_addr+2] =
b;
402 pix[image_addr+0] = r + (pix[image_addr+0]*(255-
a) + 255/2) / 255;
403 pix[image_addr+1] = g + (pix[image_addr+1]*(255-
a) + 255/2) / 255;
404 pix[image_addr+2] = b + (pix[image_addr+2]*(255-
a) + 255/2) / 255;
436 GetReq(GetImage, req);
442 req->width = image->width;
443 req->height = image->height;
444 req->planeMask = (
unsigned int)AllPlanes;
445 req->format = ZPixmap;
447 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.length) {
453 nbytes = (long)rep.length << 2;
454 _XReadPad(dpy, image->data, nbytes);
481 int64_t curtime, delay;
497 ts.tv_sec = delay / 1000000;
498 ts.tv_nsec = (delay % 1000000) * 1000;
499 nanosleep(&ts, NULL);
503 pkt->
data = image->data;
517 screen = DefaultScreen(dpy);
518 root = RootWindow(dpy, screen);
520 int screen_w, screen_h;
521 int pointer_x, pointer_y,
_;
524 screen_w = DisplayWidth(dpy, screen);
525 screen_h = DisplayHeight(dpy, screen);
526 XQueryPointer(dpy, root, &w, &w, &pointer_x, &pointer_y, &_, &_, &_);
527 if (follow_mouse == -1) {
529 x_off += pointer_x - s->
width / 2 - x_off;
530 y_off += pointer_y - s->
height / 2 - y_off;
534 if (pointer_x > x_off + s->
width - follow_mouse) {
535 x_off += pointer_x - (x_off + s->
width - follow_mouse);
536 }
else if (pointer_x < x_off + follow_mouse)
537 x_off -= (x_off + follow_mouse) - pointer_x;
538 if (pointer_y > y_off + s->
height - follow_mouse) {
539 y_off += pointer_y - (y_off + s->
height - follow_mouse);
540 }
else if (pointer_y < y_off + follow_mouse)
541 y_off -= (y_off + follow_mouse) - pointer_y;
557 for (evt.type = NoEventMask; XCheckMaskEvent(dpy, ExposureMask | StructureNotifyMask, &evt); );
566 if (!XShmGetImage(dpy, root, image, x_off, y_off, AllPlanes)) {
596 shmdt(x11grab->
shminfo.shmaddr);
597 shmctl(x11grab->
shminfo.shmid, IPC_RMID, NULL);
601 if (x11grab->
image) {
602 XDestroyImage(x11grab->
image);
603 x11grab->
image = NULL;
611 XCloseDisplay(x11grab->
dpy);
615 #define OFFSET(x) offsetof(struct x11grab, x)
616 #define DEC AV_OPT_FLAG_DECODING_PARAM
620 {
"follow_mouse",
"move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region",
622 {
"centered",
"keep the mouse pointer at the center of grabbing region when following",
643 .priv_data_size =
sizeof(
struct x11grab),