如何将 XEvent 转换为字符串?

How can i convert an XEvent to a string?

我目前正在接触 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 指令扩展到每个可能的事件并手动打印名称之外,还有其他方法可以做到这一点吗?

编辑: 我不认为这个问题与 重复,因为我对以任意方式打印事件不感兴趣(我已经这样做了那)但特别是以可读字符串的形式。我要求的东西类似于 strerrorXEvent.type 而不是 errno.

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+).*$//s; s/#define\s+//g; s/(\S+)\s+(\d+)/print "type[]=\"\";\n"/eg'