相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have tried below simple program to use XShmGetImage to get the desktop image.

#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.");

Output:

 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.

Unfortunately, XShmGetImage fails, and no information is displayed. Please help.

There was a blunder from my side. Actually, it works properly, and I misinterpreted the return value of XShmGetImage API().

The correct one is

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");
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

 
推荐文章