如何将XEvent转换为字符串?

时间:2016-03-02 11:11:56

标签: c++ x11 xlib

我目前正在和XLib擦肩而过。在我的测试程序中,我有以下循环:

while (!done) {
    XEvent e;
    XNextEvent(cairo_xlib_surface_get_display(mImpl->surface), &e);

    switch (e.type) {
        case KeyPress:
            done = true;
            break;
        case ClientMessage:
            if (static_cast<Atom>(e.xclient.data.l[0]) == mImpl->deleteEvent) done = true;
            break;
        default:
            std::cerr << "Uncatched X event: " << e.type << std::endl;
    }
}

我得到了很多像Uncatched X Event: 65这样的输出。有没有办法让这个输出更具可读性?我希望有类似Uncatched X event: KeyPress之类的东西,甚至像Uncatched X event (letter 'e')之类的东西。

除了读取头文件并将switch指令扩展到每个可能的事件并手动打印名称之外,还有办法做到这一点吗?

编辑:我认为这个问题不是xlib - print event name的重复,因为我对以任意方式打印事件不感兴趣(我已经这样做了)但特别是可读字符串的形式。我要求的内容类似于strerror,但XEvent.type代替errno

1 个答案:

答案 0 :(得分:0)

const char *type[37] = {0};
type[2] = "KeyPress";
type[3] = "KeyRelease";
type[4] = "ButtonPress";
type[5] = "ButtonRelease";
type[6] = "MotionNotify";
type[7] = "EnterNotify";
type[8] = "LeaveNotify";
type[9] = "FocusIn";
type[10] = "FocusOut";
type[11] = "KeymapNotify";
type[12] = "Expose";
type[13] = "GraphicsExpose";
type[14] = "NoExpose";
type[15] = "VisibilityNotify";
type[16] = "CreateNotify";
type[17] = "DestroyNotify";
type[18] = "UnmapNotify";
type[19] = "MapNotify";
type[20] = "MapRequest";
type[21] = "ReparentNotify";
type[22] = "ConfigureNotify";
type[23] = "ConfigureRequest";
type[24] = "GravityNotify";
type[25] = "ResizeRequest";
type[26] = "CirculateNotify";
type[27] = "CirculateRequest";
type[28] = "PropertyNotify";
type[29] = "SelectionClear";
type[30] = "SelectionRequest";
type[31] = "SelectionNotify";
type[32] = "ColormapNotify";
type[33] = "ClientMessage";
type[34] = "MappingNotify";
type[35] = "GenericEvent";
type[36] = "LASTEvent";

生产者:

cat /usr/include/X11/X.h | perl -e 'local $/;$_=<STDIN>; s/^.*(KeyPress.*LASTEvent\s+\d+).*$/$1/s; s/#define\s+//g; s/(\S+)\s+(\d+)/print "type[$2]=\"$1\";\n"/eg'