简体   繁体   English

Xlib 和 XCB。 一个进程中有多个窗口。 Xlib 失败而 XCB 没有

[英]Xlib and XCB. Multiple windows in one process. Xlib fails whereas XCB do not

Not so long ago, I was faced with a fact that programming with Xlib in the presence of multiple threads and multiple windows is a thorny path.不久前,我面临这样一个事实,即在多线程和多窗口的情况下使用Xlib进行编程是一条棘手的道路。 The problem I was faced with is that, that Xlib (or window manager) is forcefully closes a connection to the server after so called "top level" window is closed.我面临的问题是, Xlib (或窗口管理器)在所谓的“顶级”窗口关闭后强行关闭与服务器的连接。

The problem is easy to reproduce, you just need to create a window, then close it.问题很容易重现,你只需要创建一个窗口,然后关闭它。 Try to use use a Display after the above steps.尝试在上述步骤后使用Display The fatal error will raise and program will exit.将引发致命错误并且程序将退出。 This is a strange behaviour of Xlib .这是Xlib的奇怪行为。

The problem were faced in several SO topics: How do I gracefully exit an X11 event loop?问题在几个SO主题中遇到:如何优雅地退出 X11 事件循环? , How do you exit X11 program without Error . 你如何退出X11程序而不出错 To summarize the discussions, It is noted somewhere in Xlib documentation that windows manager will close the connection.总结讨论,在 Xlib 文档的某处指出 Windows 管理器将关闭连接。

Out of curiosity I checked XCB library for such behaviour.出于好奇,我检查了XCB库是否有这种行为。 The XCB , compared to Xlib, behaved "normally" and I was able to create and destroy several windows with connection still in place.与 Xlib 相比, XCB行为“正常”,我能够创建和销毁几个连接仍然存在的窗口。

The question is.问题是。 How does XCB library performs in the presence of multiple windows and threads, so it does not show the Xlib faulty behaviour? XCB库如何在存在多个窗口和线程的情况下执行,因此它没有显示Xlib错误行为? Does the XCB uses Xlib under the hood, or does it sends commands to the X server in some other format? XCBXCB使用Xlib ,还是以其他格式向 X 服务器发送命令? Excuse me, I am not so familiar, how to communicate to X11 protocol.请问,我不是很熟悉,如何与X11协议通信。 And in particular, how it come, that window manager does not close the connection with XCB ?特别是,窗口管理器如何不关闭与XCB的连接?

I will accept any answer that will shred a light on the matters.我会接受任何能说明问题的答案。 Will it be web-links to XCB source code, that explains the smart way of doing things.它是指向XCB源代码的网络链接,它解释了做事的聪明方式。 Or will it be explanation in terms of X11 protocol.或者是X11协议方面的解释。 Or anything else.或者别的什么。

Thanks for the long reading.感谢您的长时间阅读。 I hope I did not tire you with all this text.我希望我没有让你厌倦所有这些文字。

Update更新

I've messed things up.我把事情搞砸了。 The Xlib behaves as I expect, if WM_DELETE_WINDOW is set in WM_PROTOCOLS.如果在 WM_PROTOCOLS 中设置了 WM_DELETE_WINDOW,则 Xlib 的行为与我预期的一样。 It is just the ordering, or syncing, caused the connection to be in wrong state.只是排序或同步导致连接处于错误状态。 I think that multiple threads have interfered with Xlib state in a random way and the event loop got wrong.我认为多个线程以随机方式干扰了Xlib状态,并且事件循环出错了。 I must warn others about Xlib, that a syncing should be made by means of variables or atomic primitives.我必须警告其他人关于 Xlib,同步应该通过变量或原子原语进行。 The Xlib actual state change should be made only in the main thread and after the event loop had passed . Xlib实际状态更改应该只在主线程中并且在事件循环通过之后进行

To summarize the discussions, It is noted somewhere in Xlib documentation that windows manager will close the connection.总结讨论,在 Xlib 文档的某处指出 Windows 管理器将关闭连接。

With "somewhere" being the Xlib documentation. “某处”是 Xlib 文档。 And you should read those discussions more carefully, because you seem to have overlooked some important points.你应该更仔细地阅读这些讨论,因为你似乎忽略了一些重要的观点。

How does XCB library performs in the presence of multiple windows and threads, so it does not show the Xlib faulty behaviour? XCB 库如何在存在多个窗口和线程的情况下执行,因此它没有显示 Xlib 错误行为?

You may not like Xlib's behavior in this area, but it is not "faulty".您可能不喜欢 Xlib 在这方面的行为,但它并不是“错误的”。 It is documented, and it is useful for many programs.它被记录在案,对许多程序都很有用。 Moreover, although closing the connection is the default behavior under Xlib when a top-level window is closed, it is not the only possible behavior.此外,虽然关闭连接是关闭顶级窗口时 Xlib 下的默认行为,但这并不是唯一可能的行为。 Among the answers to the questions you linked, there is a description (and documentation citation) describing the relatively simple thing you need to do to avoid the X connection being closed when a top-level window is closed via its close button.在您链接的问题的答案中,有一个描述(和文档引用)描述了您需要做的相对简单的事情,以避免在通过关闭按钮关闭顶级窗口时关闭 X 连接。

Considering, then, that we're talking about a default behavior of Xlib, not an unavoidable one, it should not be overly surprising that XCB does things differently, because ....那么,考虑到我们谈论的是 Xlib 的默认行为,而不是不可避免的行为,XCB 以不同的方式做事也就不足为奇了,因为......

Does the XCB uses Xlib under the hood, or does it sends commands to the X server in some other format? XCB 是在幕后使用 Xlib,还是以其他格式向 X 服务器发送命令?

No, XCB does not use Xlib.不,XCB 不使用 Xlib。 It is an alternative to Xlib, a younger sibling written with wisdom that came from experience with Xlib.它是 Xlib 的替代品,Xlib 是一个年轻的兄弟姐妹,其智慧源于 Xlib 的经验。 Both Xlib and XCB communicate with the X server via the X protocol. Xlib 和 XCB 都通过 X 协议与 X 服务器通信。 * They are both low-level, but XCB is more or less a straight protocol wrapper, whereas Xlib presents an API facade hiding X's client / server nature. *它们都是低级的,但 XCB 或多或少是一个直接的协议包装器,而 Xlib 提供了一个 API 外观,隐藏了 X 的客户端/服务器性质。

And that difference in orientation presents a plausible explanation for the difference in default behavior with respect to window-close buttons.方向上的差异为窗口关闭按钮的默认行为差异提供了合理的解释。 Xlib tries to guess what the programmer of a GUI application is most likely to want, and it chooses defaults based on that. Xlib 试图猜测 GUI 应用程序的程序员最有可能想要什么,并基于此选择默认值。 Most X applications present a single top-level window, where closing the window terminates the application, and closing the X connection automatically is a convenience for such applications.大多数 X 应用程序都呈现一个单一的顶级窗口,关闭窗口会终止应用程序,而自动关闭 X 连接对于此类应用程序来说很方便。 XCB, on the other hand, doesn't attempt to hide the X protocol or to layer on policy (though toolkits built on top of it may do).另一方面,XCB 不会试图隐藏 X 协议或策略分层(尽管建立在它之上的工具包可能会这样做)。 XCB is all about being transparent about what it does. XCB 就是要对其所做的事情保持透明。

Excuse me, I am not so familiar, how to communicate to X11 protocol.请问,我不是很熟悉,如何与X11协议通信。

Generally speaking, you communicate by using Xlib or XCB.一般而言,您使用 Xlib 或 XCB 进行通信。 It is possible in principle to speak raw X11 to an X server -- after all, that's what Xlib and XCB do -- but doing so is complicated.原则上可以将原始 X11 与 X 服务器对话——毕竟,这就是 Xlib 和 XCB 所做的——但这样做很复杂。 That's why Xlib and XCB exist.这就是 Xlib 和 XCB 存在的原因。 And even those are not so easy to use, which is why they have higher-level toolkits built on top of them.甚至那些也不是那么容易使用,这就是为什么它们在它们之上构建了更高级别的工具包。

And in particular, how it come, that window manager does not close the connection with XCB?特别是,窗口管理器如何不关闭与 XCB 的连接?

XCB does not instruct the window manager to do so (at least, not by default). XCB 不指示窗口管理器这样做(至少,默认情况下不是)。 Xlib doesn't do so either, if you tell it not to do. Xlib 也不会这样做,如果您告诉它不要这样做。


* Or so it was initially. *最初是这样。 As one commenter observed, these days, Xlib does not speak the X protocol directly, but rather uses XCB (as opposed to the other way around).正如一位评论者所观察到的,如今,Xlib 不直接使用 X 协议,而是使用 XCB(与其他方式相反)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM