2015-01-02 5 views
5

xcb 라이브러리에서 공유 메모리 픽스맵을 사용하는 방법을 배우려고합니다. 당신이 경험이 있고 예제 코드 및/또는 정보를 공유하고 싶습니까? 이것은 매우 도움이 될 것입니다. 내가 XCB에 공유 메모리 픽스맵을 사용하는 방법을 발견 몇 가지 조사 후shm pixmap을 xcb와 함께 사용하는 방법?

감사

답변

6

.

#include <stdlib.h> 
#include <stdio.h> 

#include <sys/ipc.h> 
#include <sys/shm.h> 

#include <xcb/xcb.h> 
#include <xcb/shm.h> 
#include <xcb/xcb_image.h> 


#define WID 512 
#define HEI 512 



int main(){ 
    xcb_connection_t*  connection; 
    xcb_window_t   window; 
    xcb_screen_t*   screen; 
    xcb_gcontext_t   gcontext; 
    xcb_generic_event_t* event; 

    uint32_t value_mask; 
    uint32_t value_list[2]; 

    //connect to the X server and get screen 

    connection = xcb_connect(NULL, NULL); 
    screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data; 

    //create a window 

    value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; 
    value_list[0] = screen->black_pixel; 
    value_list[1] = XCB_EVENT_MASK_EXPOSURE; 

    window = xcb_generate_id(connection); 

    xcb_create_window(
     connection,    
     screen->root_depth, 
     window,  
     screen->root,  
     0, 0,   
     WID, HEI,  
     0,    
     XCB_WINDOW_CLASS_INPUT_OUTPUT, 
     screen->root_visual,  
     value_mask, value_list 
    ); 

    //create a graphic context 

    value_mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES; 
    value_list[0] = screen->black_pixel; 
    value_list[1] = 0; 

    gcontext = xcb_generate_id(connection); 
    xcb_create_gc(connection, gcontext, window, value_mask, value_list); 

    //map the window onto the screen 

    xcb_map_window(connection, window); 
    xcb_flush(connection); 


    //Shm test 
    xcb_shm_query_version_reply_t* reply; 
    xcb_shm_segment_info_t   info; 

    reply = xcb_shm_query_version_reply(
     connection, 
     xcb_shm_query_version(connection), 
     NULL 
    ); 

    if(!reply || !reply->shared_pixmaps){ 
     printf("Shm error...\n"); 
     exit(0); 
    } 

    info.shmid = shmget(IPC_PRIVATE, WID*HEI*4, IPC_CREAT | 0777); 
    info.shmaddr = shmat(info.shmid, 0, 0); 

    info.shmseg = xcb_generate_id(connection); 
    xcb_shm_attach(connection, info.shmseg, info.shmid, 0); 
    shmctl(info.shmid, IPC_RMID, 0); 

    uint32_t* data = info.shmaddr; 

    xcb_pixmap_t pix = xcb_generate_id(connection); 
    xcb_shm_create_pixmap(
     connection, 
     pix, 
     window, 
     WID, HEI, 
     screen->root_depth, 
     info.shmseg, 
     0 
    ); 

    int i = 0; 
    while(1){ 
     usleep(10000); 

     data[i] = 0xFFFFFF; 
     i++; 

     xcb_copy_area(
      connection, 
      pix, 
      window, 
      gcontext, 
      0, 0, 0, 0, 
      WID, HEI 
     ); 

     xcb_flush(connection); 
    } 

    xcb_shm_detach(connection, info.shmseg); 
    shmdt(info.shmaddr); 

    xcb_free_pixmap(connection, pix); 

    xcb_destroy_window(connection, window); 
    xcb_disconnect(connection); 

    return 0; 
} 
+1

'설명 shmctl (..., IPC_RMID, ...) X 서버가 부착 후', 호출 할 필요가 일부 비 리눅스 시스템에서 당신이를 첨부 할 수 없습니다 : 여기

내 testcode입니다 공유 메모리 세그먼트를 삭제 표시합니다. 'xcb_shm_attach'는 xcb 보내는 메시지 큐에 요청을 추가하기 때문에'xcb_shm_attach'가 반환 될 때 X 서버가 아직 연결되지 않았을 것입니다. 'shmctl' 호출을 정리 코드로 옮기는 것이 좋습니다. http://man7.org/linux/man-pages/man2/shmctl.2.html#NOTES – programmerjake

+0

공유 메모리 세그먼트에 대한 액세스 권한을 참조하십시오. shmget (IPC_PRIVATE, WID * HEI * 4, IPC_CREAT | 0600);'현재 사용자로만 제한하는 것이 가장 좋습니다. –