gpt4 book ai didi

c++ - Xlib:关闭窗口总是导致致命的 IO 错误?

转载 作者:IT王子 更新时间:2023-10-29 00:55:00 29 4
gpt4 key购买 nike

我不确定为什么会发生这种情况,但是当我尝试使用 X 按钮关闭时,我在 C++ 中使用 Xlib 创建的任何窗口都会向终端输出错误。我可以通过编程方式无误地关闭它,只需按 X 按钮即可。

错误如下:

XIO:  fatal IO error 11 (Resource temporarily unavailable) on X server ":0"
after 483 requests (483 known processed) with 0 events remaining.

请求的数量每次都不同,但总有 0 个事件剩余。为什么会这样?原因似乎不是我的代码,因为它无论如何都会这样做并且不会向队列发送关闭事件。我已经尝试拦截 Atom WM_WINDOW_DELETE,并且当我关闭窗口时它没有运行预期的代码。

编辑:添加了事件循环代码。

while(XPending(display)) {
XNextEvent(display, &event);

pthread_mutex_unlock(&mutex);

if(event.type == Expose) {
XWindowAttributes getWindowAttributes;

pthread_mutex_lock(&mutex);

XGetWindowAttributes(display, window, &getWindowAttributes);

if(state.currentState == STATE_NORMAL) {
state.normX = getWindowAttributes.x;
state.normY = getWindowAttributes.y;
state.normWidth = getWindowAttributes.width;
state.normHeight = getWindowAttributes.height;
}

pthread_mutex_unlock(&mutex);

glViewport(0, 0, getWindowAttributes.width, getWindowAttributes.height);
} else if(event.type == KeyPress) {
return false;
} else if(event.type == ClientMessage) {
std::cout<<"X Button pressed"<<std::endl; //Never run when X-ing window
if(event.xclient.message_type == XInternAtom(display, "WM_DELETE_WINDOW", True)) {
return false;
}
} else if(event.type == ButtonPress) {
if(state.currentState != STATE_FULLSCREEN) {
fullscreen();
} else {
normalize();
}
} else if(!handleEvent(event)){
return false;
}

pthread_mutex_lock(&mutex);
}

最佳答案

除了 WM_WINDOW_DELETE 之外,您还需要监听和处理 ClientMessage 事件。

修改 Rosetta Code 中的示例举例说明:

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

int main(void) {
Display *d;
Window w;
XEvent e;
const char *msg = "Hello, World!";
int s;

d = XOpenDisplay(NULL);
if (d == NULL) {
fprintf(stderr, "Cannot open display\n");
exit(1);
}

s = DefaultScreen(d);
w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 100, 100, 1, BlackPixel(d, s), WhitePixel(d, s));
XSelectInput(d, w, ExposureMask | KeyPressMask);
XMapWindow(d, w);

// I support the WM_DELETE_WINDOW protocol
Atom WM_DELETE_WINDOW = XInternAtom(d, "WM_DELETE_WINDOW", False);
XSetWMProtocols(d, w, &WM_DELETE_WINDOW, 1);

while (1) {
XNextEvent(d, &e);
if (e.type == Expose) {
XFillRectangle(d, w, DefaultGC(d, s), 20, 20, 10, 10);
XDrawString(d, w, DefaultGC(d, s), 10, 50, msg, strlen(msg));
}
else if (e.type == KeyPress)
break;
else if (e.type == ClientMessage)
// TODO Should check here for other client message types -
// however as the only protocol registered above is WM_DELETE_WINDOW
// it is safe for this small example.
break;
}

XCloseDisplay(d);
return 0;
}

关于c++ - Xlib:关闭窗口总是导致致命的 IO 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19305982/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com