我在使用XSendEvent()发送程序特定事件时遇到问题。 我将XEvent设置如下,并将其发送:
XEvent evt;
evt.xclient.type = ClientMessage;
evt.xclient.serial = 0;
evt.xclient.send_event = true;
evt.xclient.message_type = XInternAtom(xconn, "_APP_EVT", false);
evt.xclient.format = 32;
evt.xclient.window = xwin;
evt.xclient.data.l[0] = XInternAtom(xconn, "_APP_EVT_PTR", false);
*reinterpet_cast<void**>(evt.xclient.data.l+1) = <pointer_value>
XSendEvent(xconn, xwin, false, NoEventMask, &evt);
稍后在事件循环中,我尝试从读取读取的已发送指针 收到消息,但读取指针值始终为错误。 到目前为止,我还不知道为什么Xlib甚至会破坏evt.xclient.data.l数组, 因为在XLib文档中,X根本不会干扰数据。
for(;;) {
XEvent evt;
XNextEvent(xconn, &evt);
if(evt.type == ClientMessage) {
if(evt.xclient.message_type == XInternAtom(xconn, "_APP_EVT", false)) {
if(evt.xclient.data.l[0] == XInternAtom(xconn, "_APP_EVT_PTR", false)) {
// Prints pointers,
// that have only partially corrects bits in place:
printf("_APP_EVT_PTR:%p", *reinterpet_cast<void**>(evt.xclient.data.l+1));
}
}
}
这里的另一个细节是消息是从主线程发送的, 并且消息循环在单独的GUI线程中。 在使用Xlib进行其他操作之前,我先在程序上调用XInitThreads()。