2014-10-26 219 views
0

我是一个xlib的初学者。我尝试使用基于此示例的select()编写Xwindow应用程序:how to quit the blocking of xlib's XNextEventC和xlib:XPending()在一个非常简单的程序中崩溃

但上面提到的代码在XPending函数中崩溃。所以我试图隔离问题,并在我的系统上,甚至从我的角度来看,非常简单的示例崩溃在第28行。由于几个测试,我认为通过调用XFlush()内部崩溃。在标有“// < - CRASH”的行上,我认为XPending应返回0.是否正确?

#include <stdio.h> 
#include <stdlib.h> 
#include <X11/Xlib.h> 
#include <X11/Xutil.h> 

Display *dis; 
Window win; 
int x11_fd; 
fd_set in_fds; 

struct timeval tv; 
XKeyEvent ev; 

int main() { 
    dis = XOpenDisplay(NULL); 
    win = XCreateSimpleWindow(dis, RootWindow(dis, 0), 1, 1, 256, 256, \ 
     0, BlackPixel (dis, 0), BlackPixel(dis, 0)); 

    XSelectInput(dis, win, 
     KeyPressMask | KeyReleaseMask); 

    XMapWindow(dis, win); 
    XFlush(dis); 
    printf("Xpending: %d\n",XPending(dis)); 
    printf("Xpending: %d\n",XPending(dis)); 
    XPeekEvent(dis, (XEvent*) &ev); 
    printf("type: %d button: 0x%X state: 0x%x\n",ev.type, ev.keycode, ev.state); 
    int j = XPending(dis); // <- CRASH 
    printf("XPending: %d\n",j); 
    return 0; 
} 

这是一个错误还是我误解了xlib中的一个主要概念?

回答

0

我已经解决了这个问题。虽然我不明白为什么它的工作原理:

而不是使用我宣布它的主要功能体中的全局变量EV的:

XKeyEvent *ev = (XKeyEvent*) malloc(sizeof(XKeyEvent)); 

,并调整其指针性质的节目。