我如何将XEvent转换为字符串?

问题描述 投票:1回答:1

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

Edit:我不认为这个问题是xlib - print event name的重复,因为我不希望以任意方式(我已经这样做)而是以可读字符串的形式打印事件。我要的是类似于strerror的内容,但要XEvent.type而不是errno

c++ x11 xlib
1个回答
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'
© www.soinside.com 2019 - 2024. All rights reserved.