---
libavdevice/x11grab.c | 248 ++++++++++++++++++++++++++------------------------
1 file changed, 127 insertions(+), 121 deletions(-)
diff --git a/libavdevice/x11grab.c b/libavdevice/x11grab.c
index 9958ae7..af027bd 100644
--- a/libavdevice/x11grab.c
+++ b/libavdevice/x11grab.c
@@ -91,16 +91,13 @@ struct x11grab {
*
* @param s x11grab context
*/
-static void
-x11grab_draw_region_win(struct x11grab *s)
+static void x11grab_draw_region_win(struct x11grab *s)
{
Display *dpy = s->dpy;
- int screen;
- Window win = s->region_win;
- GC gc;
+ Window win = s->region_win;
+ int screen = DefaultScreen(dpy);
+ GC gc = XCreateGC(dpy, win, 0, 0);
- screen = DefaultScreen(dpy);
- gc = XCreateGC(dpy, win, 0, 0);
XSetForeground(dpy, gc, WhitePixel(dpy, screen));
XSetBackground(dpy, gc, BlackPixel(dpy, screen));
XSetLineAttributes(dpy, gc, REGION_WIN_BORDER, LineDoubleDash, 0, 0);
@@ -116,26 +113,24 @@ x11grab_draw_region_win(struct x11grab *s)
*
* @param s x11grab context
*/
-static void
-x11grab_region_win_init(struct x11grab *s)
+static void x11grab_region_win_init(X11GrabContext *s)
{
Display *dpy = s->dpy;
- int screen;
+ int screen = DefaultScreen(dpy);
XSetWindowAttributes attribs;
XRectangle rect;
- screen = DefaultScreen(dpy);
attribs.override_redirect = True;
- s->region_win = XCreateWindow(dpy, RootWindow(dpy, screen),
- s->x_off - REGION_WIN_BORDER,
- s->y_off - REGION_WIN_BORDER,
- s->width + REGION_WIN_BORDER * 2,
- s->height + REGION_WIN_BORDER * 2,
- 0, CopyFromParent,
- InputOutput, CopyFromParent,
- CWOverrideRedirect, &attribs);
- rect.x = 0;
- rect.y = 0;
+ s->region_win = XCreateWindow(dpy, RootWindow(dpy, screen),
+ s->x_off - REGION_WIN_BORDER,
+ s->y_off - REGION_WIN_BORDER,
+ s->width + REGION_WIN_BORDER * 2,
+ s->height + REGION_WIN_BORDER *
2,
+ 0, CopyFromParent,
+ InputOutput, CopyFromParent,
+ CWOverrideRedirect, &attribs);
+ rect.x = 0;
+ rect.y = 0;
rect.width = s->width;
rect.height = s->height;
XShapeCombineRectangles(dpy, s->region_win,
@@ -156,8 +151,7 @@ x11grab_region_win_init(struct x11grab *s)
* <li>0 success</li>
* </ul>
*/
-static int
-x11grab_read_header(AVFormatContext *s1)
+static int x11grab_read_header(AVFormatContext *s1)
{
struct x11grab *x11grab = s1->priv_data;
Display *dpy;
@@ -180,22 +174,26 @@ x11grab_read_header(AVFormatContext *s1)
if (offset) {
sscanf(offset, "%d,%d", &x_off, &y_off);
x11grab->draw_mouse = !strstr(offset, "nomouse");
- *offset= 0;
+ *offset = 0;
}
- if ((ret = av_parse_video_size(&x11grab->width, &x11grab->height,
x11grab->video_size)) < 0) {
+ ret = av_parse_video_size(&x11grab->width, &x11grab->height,
+ x11grab->video_size);
+ if (ret < 0) {
av_log(s1, AV_LOG_ERROR, "Couldn't parse video size.\n");
goto out;
}
if ((ret = av_parse_video_rate(&framerate, x11grab->framerate)) < 0) {
- av_log(s1, AV_LOG_ERROR, "Could not parse framerate: %s.\n",
x11grab->framerate);
+ av_log(s1, AV_LOG_ERROR, "Could not parse framerate: %s.\n",
+ x11grab->framerate);
goto out;
}
- av_log(s1, AV_LOG_INFO, "device: %s -> display: %s x: %d y: %d width: %d
height: %d\n",
+ av_log(s1, AV_LOG_INFO,
+ "device: %s -> display: %s x: %d y: %d width: %d height: %d\n",
s1->filename, param, x_off, y_off, x11grab->width, x11grab->height);
dpy = XOpenDisplay(param);
- if(!dpy) {
+ if (!dpy) {
av_log(s1, AV_LOG_ERROR, "Could not open X display.\n");
ret = AVERROR(EIO);
goto out;
@@ -216,18 +214,21 @@ x11grab_read_header(AVFormatContext *s1)
screen_w = DisplayWidth(dpy, screen);
screen_h = DisplayHeight(dpy, screen);
- XQueryPointer(dpy, RootWindow(dpy, screen), &w, &w, &x_off, &y_off,
&ret, &ret, &ret);
+ XQueryPointer(dpy, RootWindow(dpy, screen), &w, &w, &x_off, &y_off,
+ &ret, &ret, &ret);
x_off -= x11grab->width / 2;
y_off -= x11grab->height / 2;
- x_off = FFMIN(FFMAX(x_off, 0), screen_w - x11grab->width);
- y_off = FFMIN(FFMAX(y_off, 0), screen_h - x11grab->height);
- av_log(s1, AV_LOG_INFO, "followmouse is enabled, resetting grabbing
region to x: %d y: %d\n", x_off, y_off);
+ x_off = FFMIN(FFMAX(x_off, 0), screen_w - x11grab->width);
+ y_off = FFMIN(FFMAX(y_off, 0), screen_h - x11grab->height);
+ av_log(s1, AV_LOG_INFO,
+ "followmouse is enabled, resetting grabbing region to x: %d y:
%d\n",
+ x_off, y_off);
}
use_shm = XShmQueryExtension(dpy);
- av_log(s1, AV_LOG_INFO, "shared memory extension %s found\n", use_shm ? ""
: "not");
+ av_log(s1, AV_LOG_INFO, "shared memory extension %sfound\n", use_shm ? ""
: "not ");
- if(use_shm) {
+ if (use_shm) {
int scr = XDefaultScreen(dpy);
image = XShmCreateImage(dpy,
DefaultVisual(dpy, scr),
@@ -238,13 +239,13 @@ x11grab_read_header(AVFormatContext *s1)
x11grab->width, x11grab->height);
x11grab->shminfo.shmid = shmget(IPC_PRIVATE,
image->bytes_per_line * image->height,
- IPC_CREAT|0777);
+ IPC_CREAT | 0777);
if (x11grab->shminfo.shmid == -1) {
av_log(s1, AV_LOG_ERROR, "Fatal: Can't get shared memory!\n");
ret = AVERROR(ENOMEM);
goto out;
}
- x11grab->shminfo.shmaddr = image->data = shmat(x11grab->shminfo.shmid,
0, 0);
+ x11grab->shminfo.shmaddr = image->data =
shmat(x11grab->shminfo.shmid, 0, 0);
x11grab->shminfo.readOnly = False;
if (!XShmAttach(dpy, &x11grab->shminfo)) {
@@ -255,46 +256,54 @@ x11grab_read_header(AVFormatContext *s1)
}
} else {
image = XGetImage(dpy, RootWindow(dpy, screen),
- x_off,y_off,
+ x_off, y_off,
x11grab->width, x11grab->height,
AllPlanes, ZPixmap);
}
switch (image->bits_per_pixel) {
case 8:
- av_log (s1, AV_LOG_DEBUG, "8 bit palette\n");
+ av_log(s1, AV_LOG_DEBUG, "8 bit palette\n");
input_pixfmt = AV_PIX_FMT_PAL8;
break;
case 16:
- if ( image->red_mask == 0xf800 &&
- image->green_mask == 0x07e0 &&
- image->blue_mask == 0x001f ) {
- av_log (s1, AV_LOG_DEBUG, "16 bit RGB565\n");
+ if (image->red_mask == 0xf800 &&
+ image->green_mask == 0x07e0 &&
+ image->blue_mask == 0x001f) {
+ av_log(s1, AV_LOG_DEBUG, "16 bit RGB565\n");
input_pixfmt = AV_PIX_FMT_RGB565;
- } else if (image->red_mask == 0x7c00 &&
+ } else if (image->red_mask == 0x7c00 &&
image->green_mask == 0x03e0 &&
- image->blue_mask == 0x001f ) {
+ image->blue_mask == 0x001f) {
av_log(s1, AV_LOG_DEBUG, "16 bit RGB555\n");
input_pixfmt = AV_PIX_FMT_RGB555;
} else {
- av_log(s1, AV_LOG_ERROR, "RGB ordering at image depth %i not
supported ... aborting\n", image->bits_per_pixel);
- 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);
+ av_log(s1, AV_LOG_ERROR,
+ "RGB ordering at image depth %i not supported ...
aborting\n",
+ image->bits_per_pixel);
+ 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);
ret = AVERROR(EIO);
goto out;
}
break;
case 24:
- if ( image->red_mask == 0xff0000 &&
- image->green_mask == 0x00ff00 &&
- image->blue_mask == 0x0000ff ) {
+ if (image->red_mask == 0xff0000 &&
+ image->green_mask == 0x00ff00 &&
+ image->blue_mask == 0x0000ff) {
input_pixfmt = AV_PIX_FMT_BGR24;
- } else if ( image->red_mask == 0x0000ff &&
- image->green_mask == 0x00ff00 &&
- image->blue_mask == 0xff0000 ) {
+ } else if (image->red_mask == 0x0000ff &&
+ image->green_mask == 0x00ff00 &&
+ image->blue_mask == 0xff0000) {
input_pixfmt = AV_PIX_FMT_RGB24;
} else {
- av_log(s1, AV_LOG_ERROR,"rgb ordering at image depth %i not
supported ... aborting\n", image->bits_per_pixel);
- 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);
+ av_log(s1, AV_LOG_ERROR,
+ "rgb ordering at image depth %i not supported ...
aborting\n",
+ image->bits_per_pixel);
+ 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);
ret = AVERROR(EIO);
goto out;
}
@@ -303,27 +312,29 @@ x11grab_read_header(AVFormatContext *s1)
input_pixfmt = AV_PIX_FMT_RGB32;
break;
default:
- av_log(s1, AV_LOG_ERROR, "image depth %i not supported ...
aborting\n", image->bits_per_pixel);
+ av_log(s1, AV_LOG_ERROR,
+ "image depth %i not supported ... aborting\n",
+ image->bits_per_pixel);
ret = AVERROR(EINVAL);
goto out;
}
- x11grab->frame_size = x11grab->width * x11grab->height *
image->bits_per_pixel/8;
- x11grab->dpy = dpy;
- x11grab->time_base = (AVRational){framerate.den, framerate.num};
+ x11grab->frame_size = x11grab->width * x11grab->height *
image->bits_per_pixel / 8;
+ x11grab->dpy = dpy;
+ x11grab->time_base = (AVRational) { framerate.den, framerate.num };
x11grab->time_frame = av_gettime() / av_q2d(x11grab->time_base);
- x11grab->x_off = x_off;
- x11grab->y_off = y_off;
- x11grab->image = image;
- x11grab->use_shm = use_shm;
+ x11grab->x_off = x_off;
+ x11grab->y_off = y_off;
+ x11grab->image = image;
+ x11grab->use_shm = use_shm;
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
- st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
- st->codec->width = x11grab->width;
- st->codec->height = x11grab->height;
- st->codec->pix_fmt = input_pixfmt;
- st->codec->time_base = x11grab->time_base;
- st->codec->bit_rate = x11grab->frame_size * 1/av_q2d(x11grab->time_base) *
8;
+ st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
+ st->codec->width = x11grab->width;
+ st->codec->height = x11grab->height;
+ st->codec->pix_fmt = input_pixfmt;
+ st->codec->time_base = x11grab->time_base;
+ st->codec->bit_rate = x11grab->frame_size * 1 /
av_q2d(x11grab->time_base) * 8;
out:
av_free(param);
@@ -337,13 +348,12 @@ out:
* @param s context used to retrieve original grabbing rectangle
* coordinates
*/
-static void
-paint_mouse_pointer(XImage *image, struct x11grab *s)
+static void paint_mouse_pointer(XImage *image, struct x11grab *s)
{
- int x_off = s->x_off;
- int y_off = s->y_off;
- int width = s->width;
- int height = s->height;
+ int x_off = s->x_off;
+ int y_off = s->y_off;
+ int width = s->width;
+ int height = s->height;
Display *dpy = s->dpy;
XFixesCursorImage *xcim;
int x, y;
@@ -365,36 +375,34 @@ paint_mouse_pointer(XImage *image, struct x11grab *s)
x = xcim->x - xcim->xhot;
y = xcim->y - xcim->yhot;
- to_line = FFMIN((y + xcim->height), (height + y_off));
+ to_line = FFMIN((y + xcim->height), (height + y_off));
to_column = FFMIN((x + xcim->width), (width + x_off));
- for (line = FFMAX(y, y_off); line < to_line; line++) {
+ for (line = FFMAX(y, y_off); line < to_line; line++)
for (column = FFMAX(x, x_off); column < to_column; column++) {
- int xcim_addr = (line - y) * xcim->width + column - x;
+ int xcim_addr = (line - y) * xcim->width + column - x;
int image_addr = ((line - y_off) * width + column - x_off) *
pixstride;
- int r = (uint8_t)(xcim->pixels[xcim_addr] >> 0);
- int g = (uint8_t)(xcim->pixels[xcim_addr] >> 8);
- int b = (uint8_t)(xcim->pixels[xcim_addr] >> 16);
- int a = (uint8_t)(xcim->pixels[xcim_addr] >> 24);
+ int r = (uint8_t) (xcim->pixels[xcim_addr] >> 0);
+ int g = (uint8_t) (xcim->pixels[xcim_addr] >> 8);
+ int b = (uint8_t) (xcim->pixels[xcim_addr] >> 16);
+ int a = (uint8_t) (xcim->pixels[xcim_addr] >> 24);
if (a == 255) {
- pix[image_addr+0] = r;
- pix[image_addr+1] = g;
- pix[image_addr+2] = b;
+ pix[image_addr + 0] = r;
+ pix[image_addr + 1] = g;
+ pix[image_addr + 2] = b;
} else if (a) {
/* pixel values from XFixesGetCursorImage come premultiplied
by alpha */
- pix[image_addr+0] = r + (pix[image_addr+0]*(255-a) + 255/2) /
255;
- pix[image_addr+1] = g + (pix[image_addr+1]*(255-a) + 255/2) /
255;
- pix[image_addr+2] = b + (pix[image_addr+2]*(255-a) + 255/2) /
255;
+ pix[image_addr + 0] = r + (pix[image_addr + 0] * (255 - a) +
255 / 2) / 255;
+ pix[image_addr + 1] = g + (pix[image_addr + 1] * (255 - a) +
255 / 2) / 255;
+ pix[image_addr + 2] = b + (pix[image_addr + 2] * (255 - a) +
255 / 2) / 255;
}
}
- }
XFree(xcim);
xcim = NULL;
}
-
/**
* Read new data in the image structure.
*
@@ -405,8 +413,7 @@ paint_mouse_pointer(XImage *image, struct x11grab *s)
* @param y Top-Left grabbing rectangle vertical coordinate
* @return 0 if error, !0 if successful
*/
-static int
-xget_zpixmap(Display *dpy, Drawable d, XImage *image, int x, int y)
+static int xget_zpixmap(Display *dpy, Drawable d, XImage *image, int x, int y)
{
xGetImageReply rep;
xGetImageReq *req;
@@ -420,21 +427,21 @@ xget_zpixmap(Display *dpy, Drawable d, XImage *image, int
x, int y)
GetReq(GetImage, req);
/* First set up the standard stuff in the request */
- req->drawable = d;
- req->x = x;
- req->y = y;
- req->width = image->width;
- req->height = image->height;
- req->planeMask = (unsigned int)AllPlanes;
- req->format = ZPixmap;
-
- if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.length) {
+ req->drawable = d;
+ req->x = x;
+ req->y = y;
+ req->width = image->width;
+ req->height = image->height;
+ req->planeMask = (unsigned int) AllPlanes;
+ req->format = ZPixmap;
+
+ if (!_XReply(dpy, (xReply *) &rep, 0, xFalse) || !rep.length) {
UnlockDisplay(dpy);
SyncHandle();
return 0;
}
- nbytes = (long)rep.length << 2;
+ nbytes = (long) rep.length << 2;
_XReadPad(dpy, image->data, nbytes);
UnlockDisplay(dpy);
@@ -449,14 +456,13 @@ xget_zpixmap(Display *dpy, Drawable d, XImage *image, int
x, int y)
* @param pkt Packet holding the brabbed frame
* @return frame size in bytes
*/
-static int
-x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
+static int x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
{
struct x11grab *s = s1->priv_data;
- Display *dpy = s->dpy;
- XImage *image = s->image;
- int x_off = s->x_off;
- int y_off = s->y_off;
+ Display *dpy = s->dpy;
+ XImage *image = s->image;
+ int x_off = s->x_off;
+ int y_off = s->y_off;
int screen;
Window root;
@@ -469,16 +475,16 @@ x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
s->time_frame += INT64_C(1000000);
/* wait based on the frame rate */
- for(;;) {
+ for (;;) {
curtime = av_gettime();
- delay = s->time_frame * av_q2d(s->time_base) - curtime;
+ delay = s->time_frame * av_q2d(s->time_base) - curtime;
if (delay <= 0) {
if (delay < INT64_C(-1000000) * av_q2d(s->time_base)) {
s->time_frame += INT64_C(1000000);
}
break;
}
- ts.tv_sec = delay / 1000000;
+ ts.tv_sec = delay / 1000000;
ts.tv_nsec = (delay % 1000000) * 1000;
nanosleep(&ts, NULL);
}
@@ -486,10 +492,10 @@ x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
av_init_packet(pkt);
pkt->data = image->data;
pkt->size = s->frame_size;
- pkt->pts = curtime;
+ pkt->pts = curtime;
screen = DefaultScreen(dpy);
- root = RootWindow(dpy, screen);
+ root = RootWindow(dpy, screen);
if (follow_mouse) {
int screen_w, screen_h;
int pointer_x, pointer_y, _;
@@ -500,7 +506,7 @@ x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
XQueryPointer(dpy, root, &w, &w, &pointer_x, &pointer_y, &_, &_, &_);
if (follow_mouse == -1) {
// follow the mouse, put it at center of grabbing region
- x_off += pointer_x - s->width / 2 - x_off;
+ x_off += pointer_x - s->width / 2 - x_off;
y_off += pointer_y - s->height / 2 - y_off;
} else {
// follow the mouse, but only move the grabbing region when mouse
@@ -526,9 +532,10 @@ x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
if (s->show_region) {
if (s->region_win) {
- XEvent evt;
- // clean up the events, and do the initinal draw or redraw.
- for (evt.type = NoEventMask; XCheckMaskEvent(dpy, ExposureMask |
StructureNotifyMask, &evt); );
+ XEvent evt = { .type = NoEventMask };
+ // clean up the events, and do the initial draw or redraw.
+ while (XCheckMaskEvent(dpy, ExposureMask | StructureNotifyMask,
+ &evt));
if (evt.type)
x11grab_draw_region_win(s);
} else {
@@ -536,13 +543,13 @@ x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
}
}
- if(s->use_shm) {
+ if (s->use_shm) {
if (!XShmGetImage(dpy, root, image, x_off, y_off, AllPlanes)) {
- av_log (s1, AV_LOG_INFO, "XShmGetImage() failed\n");
+ av_log(s1, AV_LOG_INFO, "XShmGetImage() failed\n");
}
} else {
if (!xget_zpixmap(dpy, root, image, x_off, y_off)) {
- av_log (s1, AV_LOG_INFO, "XGetZPixmap() failed\n");
+ av_log(s1, AV_LOG_INFO, "XGetZPixmap() failed\n");
}
}
@@ -559,8 +566,7 @@ x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
* @param s1 Context from avformat core
* @return 0 success, !0 failure
*/
-static int
-x11grab_read_close(AVFormatContext *s1)
+static int x11grab_read_close(AVFormatContext *s1)
{
struct x11grab *x11grab = s1->priv_data;
--
1.8.5.1
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel