如果窗口存在,如何检查xlib? [英] How can I check in xlib if window exists?
问题描述
我正在使用gcc在Linux中编写c ++库.程序使用
I am writing c++ library in Linux using gcc. Program opens web pages in new browser windows with
system("firefox https://www.webpage.com");
之后,我使用xlib确定每个打开的浏览器窗口的标识符.然后程序使用
After that I use xlib to determine identifiers of each opened browser window. Then program raises firefox windows one by one using
XRaiseWindow(display, window)
为了制作每个窗口和OCR的屏幕截图.
in order to make a screenshot of each window and OCR.
但是,如果我手动关闭浏览器窗口并尝试使用带有相应标识符的 XRaiseWindow
会生成 BadWindow 错误并终止程序.
But if I close browser window manually and try to use
XRaiseWindow
with the corresponding identifier it generates BadWindow error and terminates the program.
那么我该如何在xlib中检查是否存在具有给定 window
标识符的窗口?
So how can I check in xlib if window with given window
identifier exists?
推荐答案
来自 X常见问题解答:
169)如何检查窗口ID是否有效?
我的程序在远程显示器上有一个窗口的ID.我想先检查窗口是否存在.
169) How do I check whether a window ID is valid?
My program has the ID of a window on a remote display. I want to check whether the window exists before doing anything with it.
由于X是异步的,因此无法保证窗口在您获得ID的时间和您的时间之间仍然存在将事件发送到窗口或对其进行了其他处理.你应该做什么要做的是不检查就发送事件,而是将错误处理程序安装到捕获任何 BadWindow
错误,这表明该窗口没有不再存在.该计划将在少数情况下起作用,原始窗口已被破坏,其ID重新分配给另一个窗口窗口.
Because X is asynchronous, there isn't a guarantee that the window
would still exist between the time that you got the ID and the time you
sent an event to the window or otherwise manipulated it. What you should
do is send the event without checking, but install an error handler to
catch any BadWindow
errors, which would indicate that the window no
longer exists. This scheme will work except on the [rare] occasion that
the original window has been destroyed and its ID reallocated to another
window.
您可以使用此方案创建一个检查有效性的函数窗户您可以通过调用使该操作几乎同步请求后的 XSync()
,尽管仍然不能保证结果之后将存在一个窗口(除非抓住了sterver).在总体而言,捕获错误而不是预先检查是更可取的.
You can use this scheme to make a function which checks the validity
of a window; you can make this operation almost synchronous by calling
XSync()
after the request, although there is still no guarantee that the
window will exist after the result (unless the sterver is grabbed). On the
whole, catching the error rather than pre-checking is preferable.
长话短说:你不能:P你只能破坏控制权.
Long story short: You can't :P You can only damage control.
这篇关于如果窗口存在,如何检查xlib?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!