• X11 Xlib截屏问题及深入分析三 —— 源码实现2


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

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

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

    • XCreateSimpleWindow源码

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

    1. Window XCreateSimpleWindow(
    2. register Display *dpy,
    3. Window parent,
    4. int x,
    5. int y,
    6. unsigned int width,
    7. unsigned int height,
    8. unsigned int borderWidth,
    9. unsigned long border,
    10. unsigned long background)
    11. {
    12. Window wid;
    13. register xCreateWindowReq *req;
    14. LockDisplay(dpy);
    15. GetReqExtra(CreateWindow, 8, req);
    16. req->parent = parent;
    17. req->x = x;
    18. req->y = y;
    19. req->width = width;
    20. req->height = height;
    21. req->borderWidth = borderWidth;
    22. req->depth = 0;
    23. req->class = CopyFromParent;
    24. req->visual = CopyFromParent;
    25. wid = req->wid = XAllocID(dpy);
    26. req->mask = CWBackPixel | CWBorderPixel;
    27. {
    28. register CARD32 *valuePtr = (CARD32 *) NEXTPTR(req,xCreateWindowReq);
    29. *valuePtr++ = background;
    30. *valuePtr = border;
    31. }
    32. UnlockDisplay(dpy);
    33. SyncHandle();
    34. return (wid);
    35. }
    • XMapWindow源码

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

    1. int
    2. XMapWindow (
    3. register Display *dpy,
    4. Window w)
    5. {
    6. register xResourceReq *req;
    7. LockDisplay (dpy);
    8. GetResReq(MapWindow, w, req);
    9. UnlockDisplay (dpy);
    10. SyncHandle();
    11. return 1;
    12. }
    •  XGetImage源码

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

    1. XImage *XGetImage (
    2. register Display *dpy,
    3. Drawable d,
    4. int x,
    5. int y,
    6. unsigned int width,
    7. unsigned int height,
    8. unsigned long plane_mask,
    9. int format) /* either XYPixmap or ZPixmap */
    10. {
    11. xGetImageReply rep;
    12. register xGetImageReq *req;
    13. char *data;
    14. unsigned long nbytes;
    15. XImage *image;
    16. int planes;
    17. LockDisplay(dpy);
    18. GetReq (GetImage, req);
    19. /*
    20. * first set up the standard stuff in the request
    21. */
    22. req->drawable = d;
    23. req->x = x;
    24. req->y = y;
    25. req->width = width;
    26. req->height = height;
    27. req->planeMask = plane_mask;
    28. req->format = format;
    29. if (_XReply (dpy, (xReply *) &rep, 0, xFalse) == 0 ||
    30. rep.length == 0) {
    31. UnlockDisplay(dpy);
    32. SyncHandle();
    33. return (XImage *)NULL;
    34. }
    35. if (rep.length < (INT_MAX >> 2)) {
    36. nbytes = (unsigned long)rep.length << 2;
    37. data = Xmalloc(nbytes);
    38. } else
    39. data = NULL;
    40. if (! data) {
    41. _XEatDataWords(dpy, rep.length);
    42. UnlockDisplay(dpy);
    43. SyncHandle();
    44. return (XImage *) NULL;
    45. }
    46. _XReadPad (dpy, data, nbytes);
    47. if (format == XYPixmap) {
    48. image = XCreateImage(dpy, _XVIDtoVisual(dpy, rep.visual),
    49. Ones (plane_mask &
    50. (((unsigned long)0xFFFFFFFF) >> (32 - rep.depth))),
    51. format, 0, data, width, height, dpy->bitmap_pad, 0);
    52. planes = image->depth;
    53. } else { /* format == ZPixmap */
    54. image = XCreateImage (dpy, _XVIDtoVisual(dpy, rep.visual),
    55. rep.depth, ZPixmap, 0, data, width, height,
    56. _XGetScanlinePad(dpy, (int) rep.depth), 0);
    57. planes = 1;
    58. }
    59. if (!image) {
    60. Xfree(data);
    61. } else {
    62. if (planes < 1 || image->height < 1 || image->bytes_per_line < 1 ||
    63. INT_MAX / image->height <= image->bytes_per_line ||
    64. INT_MAX / planes <= image->height * image->bytes_per_line ||
    65. nbytes < planes * image->height * image->bytes_per_line) {
    66. XDestroyImage(image);
    67. image = NULL;
    68. }
    69. }
    70. UnlockDisplay(dpy);
    71. SyncHandle();
    72. return (image);
    73. }
    • XPutImage源码

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

    1. int
    2. XPutImage (
    3. register Display *dpy,
    4. Drawable d,
    5. GC gc,
    6. register XImage *image,
    7. int req_xoffset,
    8. int req_yoffset,
    9. int x,
    10. int y,
    11. unsigned int req_width,
    12. unsigned int req_height)
    13. {
    14. long width = req_width;
    15. long height = req_height;
    16. int dest_bits_per_pixel, dest_scanline_pad;
    17. if (req_xoffset < 0) {
    18. width += req_xoffset;
    19. req_xoffset = 0;
    20. }
    21. if (req_yoffset < 0) {
    22. height += req_yoffset;
    23. req_yoffset = 0;
    24. }
    25. if ((req_xoffset + width) > image->width)
    26. width = image->width - req_xoffset;
    27. if ((req_yoffset + height) > image->height)
    28. height = image->height - req_yoffset;
    29. if ((width <= 0) || (height <= 0))
    30. return 0;
    31. if ((image->bits_per_pixel == 1) || (image->format != ZPixmap)) {
    32. dest_bits_per_pixel = 1;
    33. dest_scanline_pad = dpy->bitmap_pad;
    34. } else {
    35. register int n;
    36. register ScreenFormat *format;
    37. dest_bits_per_pixel = image->bits_per_pixel;
    38. dest_scanline_pad = image->bitmap_pad;
    39. for (n = dpy->nformats, format = dpy->pixmap_format; --n >= 0; format++)
    40. if (format->depth == image->depth) {
    41. dest_bits_per_pixel = format->bits_per_pixel;
    42. dest_scanline_pad = format->scanline_pad;
    43. }
    44. if (dest_bits_per_pixel != image->bits_per_pixel) {
    45. XImage img;
    46. register long i, j;
    47. /* XXX slow, but works */
    48. img.width = width;
    49. img.height = height;
    50. img.xoffset = 0;
    51. img.format = ZPixmap;
    52. img.byte_order = dpy->byte_order;
    53. img.bitmap_unit = dpy->bitmap_unit;
    54. img.bitmap_bit_order = dpy->bitmap_bit_order;
    55. img.bitmap_pad = dest_scanline_pad;
    56. img.depth = image->depth;
    57. img.bits_per_pixel = dest_bits_per_pixel;
    58. img.bytes_per_line = ROUNDUP((dest_bits_per_pixel * width),
    59. dest_scanline_pad) >> 3;
    60. img.data = Xmallocarray(height, img.bytes_per_line);
    61. if (img.data == NULL)
    62. return 0;
    63. _XInitImageFuncPtrs(&img);
    64. for (j = height; --j >= 0; )
    65. for (i = width; --i >= 0; )
    66. XPutPixel(&img, i, j, XGetPixel(image, req_xoffset + i,
    67. req_yoffset + j));
    68. LockDisplay(dpy);
    69. FlushGC(dpy, gc);
    70. PutSubImage(dpy, d, gc, &img, 0, 0, x, y,
    71. (unsigned int) width, (unsigned int) height,
    72. dest_bits_per_pixel, dest_scanline_pad);
    73. UnlockDisplay(dpy);
    74. SyncHandle();
    75. Xfree(img.data);
    76. return 0;
    77. }
    78. }
    79. LockDisplay(dpy);
    80. FlushGC(dpy, gc);
    81. PutSubImage(dpy, d, gc, image, req_xoffset, req_yoffset, x, y,
    82. (unsigned int) width, (unsigned int) height,
    83. dest_bits_per_pixel, dest_scanline_pad);
    84. UnlockDisplay(dpy);
    85. SyncHandle();
    86. #ifdef USE_DYNAMIC_XCURSOR
    87. if (image->bits_per_pixel == 1 &&
    88. x == 0 && y == 0 &&
    89. width == image->width && height == image->height &&
    90. gc->values.function == GXcopy &&
    91. (gc->values.plane_mask & 1))
    92. {
    93. _XNoticePutBitmap (dpy, d, image);
    94. }
    95. #endif
    96. return 0;
    97. }

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

  • 相关阅读:
    14. 加减乘除 取整 取余 幂运算
    JavaSE——IO流总结
    最新堆叠查询注入攻击和注入代码分析技术
    30天Python入门(第六天:深入了解Python中的元组)
    Ceres学习笔记003--使用Ceres进行曲线拟合
    1、什么是ETF?
    软件过程与管理学习之(1)Project management Methodologies & Standards(项目管理方法和标准)
    Vue模板语法
    基于stm32单片机的智能恒温箱游泳池
    分析 Base64 编码和 URL 安全 Base64 编码
  • 原文地址:https://blog.csdn.net/phmatthaus/article/details/127960789