如何优雅地退出X11事件循环? [英] How do I gracefully exit an X11 event loop?

查看:216
本文介绍了如何优雅地退出X11事件循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现几乎所有教程都告诉我针对事件循环执行此操作:

Almost every tutorial I find tells me to do this for my event loop:

XEvent event;

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

    switch (event.type)
    {
        case Expose:
            printf("Expose\n");
            break;

        default:
            break;
    }
}

但是,单击X关闭程序会显示此消息.

However, clicking the X to close the program results in this message.

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

这些示例建议使用无限循环,这确实让我感到奇怪.这听起来不自然,而我的其他X11程序也没有做到这一点.所以我四处搜寻.我发现了如何捕获窗口关闭事件.

It is indeed strange to me that the examples suggest using an infinite loop. That doesn't sound natural, and my other X11 programs don't do that. So I searched around. I found out how to capture the window close event.

Atom wmDeleteMessage = XInternAtom(mDisplay, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, window, &wmDeleteMessage, 1);

XEvent event;
bool running = true;

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

    switch (event.type)
    {
        case Expose:
            printf("Expose\n");
            break;

        case ClientMessage:
            if (event.xclient.data.l[0] == wmDeleteMessage)
                running = false;
            break;

        default:
            break;
    }
}

那行得通.它退出没有错误. ...但是我拒绝相信这是正常的做事方式.我的意思是,这是正确退出X11应用程序的唯一方法吗?捕获结束事件似乎需要做很多工作.如何进行适当的"事件循环?为什么关闭事件如此深入地埋葬?我想念什么?

That works. It exits without errors. ... But I refuse to believe this is the normal way to do things. I mean, is this the only way to properly exit an X11 app? It seems like a lot of work just to capture the close event. How do I make a 'proper' event loop? Why is the close event so deeply buried? What am I missing?

推荐答案

在X11中没有退出按钮"或应用程序"或关闭事件"之类的东西.这是设计使然.

There are no such things as "exit button" or "application" or "close event" in X11. This is by design.

X11并未内置窗口装饰,退出按钮以及我们依赖的许多其他功能.它们是在核心X11之上实现的.负责wmDeleteMessage的特定约定集的名称是ICCCM,请查找它.

Window decorations, exit buttons and many the other things we depend upon are not built into X11. They are implemented on top of the core X11 instead. The name of the particular set of conventions responsible for wmDeleteMessage is ICCCM, look it up.

Xlib仅处理核心X11协议.那里没有内置的关闭事件.

Xlib only deals with the core X11 protocol. No built-in close event there.

有些工具包使处理ICCCM和X11中未内置的所有其他内容(GTK,wxWindows,Qt等)变得更加容易.您可能要使用其中之一.

There are toolkits that make dealing with ICCCM and all other things that are not built into X11 easier (GTK, wxWindows, Qt, ...) You probably want to use one of those.

这篇关于如何优雅地退出X11事件循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆