X11 Xlib截屏问题及深入分析三 —— 源码实现2
迪丽瓦拉
2024-02-04 12:13:31
0

接上一篇文章《X11 Xlib截屏问题及深入分析二 —— 源码实现》,链接为:

X11 Xlib截屏问题及深入分析二 —— 源码实现1_蓝天居士的博客-CSDN博客

前一篇文章列出了XOpenDisplay和XCloseDisplay函数的源码,本篇列出其余几个函数的源码实现。

  • XCreateSimpleWindow源码

XCreateSimpleWindow函数对应的源码在src/CrWindow.c中,如下所示:

Window XCreateSimpleWindow(register Display *dpy,Window parent,int x,int y,unsigned int width,unsigned int height,unsigned int borderWidth,unsigned long border,unsigned long background)
{Window wid;register xCreateWindowReq *req;LockDisplay(dpy);GetReqExtra(CreateWindow, 8, req);req->parent = parent;req->x = x;req->y = y;req->width = width;req->height = height;req->borderWidth = borderWidth;req->depth = 0;req->class = CopyFromParent;req->visual = CopyFromParent;wid = req->wid = XAllocID(dpy);req->mask = CWBackPixel | CWBorderPixel;{register CARD32 *valuePtr = (CARD32 *) NEXTPTR(req,xCreateWindowReq);*valuePtr++ = background;*valuePtr = border;}UnlockDisplay(dpy);SyncHandle();return (wid);}
  • XMapWindow源码

XMapWindow函数对应的源码在src/MapWindow.c中,如下所示:

int
XMapWindow (register Display *dpy,Window w)
{register xResourceReq *req;LockDisplay (dpy);GetResReq(MapWindow, w, req);UnlockDisplay (dpy);SyncHandle();return 1;
}
  •  XGetImage源码

XGetImage函数对应的源码在src/GetImage.c中,如下所示: 

XImage *XGetImage (register Display *dpy,Drawable d,int x,int y,unsigned int width,unsigned int height,unsigned long plane_mask,int format)	/* either XYPixmap or ZPixmap */
{xGetImageReply rep;register xGetImageReq *req;char *data;unsigned long nbytes;XImage *image;int planes;LockDisplay(dpy);GetReq (GetImage, req);/** first set up the standard stuff in the request*/req->drawable = d;req->x = x;req->y = y;req->width = width;req->height = height;req->planeMask = plane_mask;req->format = format;if (_XReply (dpy, (xReply *) &rep, 0, xFalse) == 0 ||rep.length == 0) {UnlockDisplay(dpy);SyncHandle();return (XImage *)NULL;}if (rep.length < (INT_MAX >> 2)) {nbytes = (unsigned long)rep.length << 2;data = Xmalloc(nbytes);} elsedata = NULL;if (! data) {_XEatDataWords(dpy, rep.length);UnlockDisplay(dpy);SyncHandle();return (XImage *) NULL;}_XReadPad (dpy, data, nbytes);if (format == XYPixmap) {image = XCreateImage(dpy, _XVIDtoVisual(dpy, rep.visual),Ones (plane_mask &(((unsigned long)0xFFFFFFFF) >> (32 - rep.depth))),format, 0, data, width, height, dpy->bitmap_pad, 0);planes = image->depth;} else { /* format == ZPixmap */image = XCreateImage (dpy, _XVIDtoVisual(dpy, rep.visual),rep.depth, ZPixmap, 0, data, width, height,_XGetScanlinePad(dpy, (int) rep.depth), 0);planes = 1;}if (!image) {Xfree(data);} else {if (planes < 1 || image->height < 1 || image->bytes_per_line < 1 ||INT_MAX / image->height <= image->bytes_per_line ||INT_MAX / planes <= image->height * image->bytes_per_line ||nbytes < planes * image->height * image->bytes_per_line) {XDestroyImage(image);image = NULL;}}UnlockDisplay(dpy);SyncHandle();return (image);
}
  • XPutImage源码

 XPutImage函数对应的源码在src/PutImage.c中,如下所示: 

int
XPutImage (register Display *dpy,Drawable d,GC gc,register XImage *image,int req_xoffset,int req_yoffset,int x,int y,unsigned int req_width,unsigned int req_height){long width = req_width;long height = req_height;int dest_bits_per_pixel, dest_scanline_pad;if (req_xoffset < 0) {width += req_xoffset;req_xoffset = 0;}if (req_yoffset < 0) {height += req_yoffset;req_yoffset = 0;}if ((req_xoffset + width) > image->width)width = image->width - req_xoffset;if ((req_yoffset + height) > image->height)height = image->height - req_yoffset;if ((width <= 0) || (height <= 0))return 0;if ((image->bits_per_pixel == 1) || (image->format != ZPixmap)) {dest_bits_per_pixel = 1;dest_scanline_pad = dpy->bitmap_pad;} else {register int n;register ScreenFormat *format;dest_bits_per_pixel = image->bits_per_pixel;dest_scanline_pad = image->bitmap_pad;for (n = dpy->nformats, format = dpy->pixmap_format; --n >= 0; format++)if (format->depth == image->depth) {dest_bits_per_pixel = format->bits_per_pixel;dest_scanline_pad = format->scanline_pad;}if (dest_bits_per_pixel != image->bits_per_pixel) {XImage img;register long i, j;/* XXX slow, but works */img.width = width;img.height = height;img.xoffset = 0;img.format = ZPixmap;img.byte_order = dpy->byte_order;img.bitmap_unit = dpy->bitmap_unit;img.bitmap_bit_order = dpy->bitmap_bit_order;img.bitmap_pad = dest_scanline_pad;img.depth = image->depth;img.bits_per_pixel = dest_bits_per_pixel;img.bytes_per_line = ROUNDUP((dest_bits_per_pixel * width),dest_scanline_pad) >> 3;img.data = Xmallocarray(height, img.bytes_per_line);if (img.data == NULL)return 0;_XInitImageFuncPtrs(&img);for (j = height; --j >= 0; )for (i = width; --i >= 0; )XPutPixel(&img, i, j, XGetPixel(image, req_xoffset + i,req_yoffset + j));LockDisplay(dpy);FlushGC(dpy, gc);PutSubImage(dpy, d, gc, &img, 0, 0, x, y,(unsigned int) width, (unsigned int) height,dest_bits_per_pixel, dest_scanline_pad);UnlockDisplay(dpy);SyncHandle();Xfree(img.data);return 0;}}LockDisplay(dpy);FlushGC(dpy, gc);PutSubImage(dpy, d, gc, image, req_xoffset, req_yoffset, x, y,(unsigned int) width, (unsigned int) height,dest_bits_per_pixel, dest_scanline_pad);UnlockDisplay(dpy);SyncHandle();
#ifdef USE_DYNAMIC_XCURSORif (image->bits_per_pixel == 1 &&x == 0 && y == 0 &&width == image->width && height == image->height &&gc->values.function == GXcopy &&(gc->values.plane_mask & 1)){_XNoticePutBitmap (dpy, d, image);}
#endifreturn 0;
}

至此,几个接口函数的源码就已经全部列出了。从下一篇文章起开始进行代码分析。

相关内容