【问题标题】:XShmGetImage fails without any error displayedXShmGetImage 失败且未显示任何错误
【发布时间】:2017-09-12 15:40:53
【问题描述】:

我尝试了下面的简单程序来使用 XShmGetImage 来获取桌面图像。

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <X11/extensions/XShm.h>
#include <X11/extensions/Xfixes.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>

int main(int argc, const char *argv[])
{
    int screen;
    Window root;
    Display* display;
    XImage* img,

    int shm=0;
    XShmSegmentInfo shminfo;

    /* My Desktop Screen Resolution */

    int width=1360;
    int height=768;

    display = XOpenDisplay(getenv("DISPLAY"));

    shm = XShmQueryExtension(display);

    if ( shm) {
        printf ("Ha... QueryExtension Successful..\n");
        int scr = XDefaultScreen (display);
        printf ("\n Screen Number is %d ", scr);
        img = XShmCreateImage (display, DefaultVisual(display, scr),
                            DefaultDepth ( display, scr),
                        ZPixmap,
                        NULL,
                       &shminfo,
                        width,
                        height);
        printf ("\n Bytes Per Line %d ", img->bytes_per_line);

        shminfo.shmid = shmget (IPC_PRIVATE, img->bytes_per_line * img->height, IPC_CREAT | 0777);

        if ( shminfo.shmid == -1 ) {
            printf ("\n Can not get the shared Memory ...");
        } else {
            printf ("\n Greate I am able to get shared memory..");
        }

       shminfo.shmaddr = img->data =shmat (shminfo.shmid, 0,0);
       shminfo.readOnly = False;

       if (!XShmAttach (display, &shminfo)) {
           printf ("\n i am unable to attach now..");
       } else {
           printf ("\n Super.. i am able to attach Shared memory to extension ");
       }


       if ( !XShmGetImage (display, RootWindow(display, DefaultScreen(display)), img, 0,0, AllPlanes)){
           printf ("\n Now you should have your image in XImage");
       } else {
           printf ("\n Ooops.. Something wrong.");

       }
    }

输出:

 Ha... QueryExtension Successful..
 Screen Number is 0 
 Bytes Per Line 5440 
 Greate I am able to get shared memory..
 Super.. i am able to attach Shared memory to extension 
 Ooops.. Something wrong.

很遗憾,XShmGetImage 失败,没有显示任何信息。请帮忙。

【问题讨论】:

    标签: x11 xlib xcb


    【解决方案1】:

    我这边犯了一个错误。实际上,它工作正常,我误解了 XShmGetImage API() 的返回值。

    正确的是

    if ( !XShmGetImage (display, RootWindow(display, DefaultScreen(display)), img, 0,0, AllPlanes)){
               printf ("\n Ooops.. Something wrong.");
    
           } else {
               printf ("\n Now you should have your image in XImage");
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-24
      • 2015-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多