2017-09-12 1 views
1

XShmGetImage를 사용하여 바탕 화면 이미지를 얻는 간단한 프로그램을 아래에서 시도했습니다.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이 실패하고 어떤 정보가 표시되지 않습니다. 도와주세요.

답변

0

내 쪽에서 큰 실수가있었습니다. 사실, 제대로 작동하고 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"); 
    } 
이다
관련 문제