Har*_*vey 5 python linux x11 wnck
这对我来说很奇怪。有人可以解释为什么 activate() 函数需要时间戳吗?99.9% 的时间不是现在或尽快或“在您方便的时候”吗?此外,如果你尝试 w.activate(0) 你会收到这个警告:
Wnck-WARNING: Received a timestamp of 0; window activation may not function properly
Run Code Online (Sandbox Code Playgroud)
我读过的有关此警告的每个论坛帖子都没有答案。但它们似乎都表明代码无法正常工作,除非您实际输入时间戳。如果你输入 (0),事情就不起作用了,你会收到警告。但是,对我来说,如果我输入时间戳,那就是事情不起作用的时候。如果我使用 (0),则程序可以正常工作,但会收到警告(仅当我在终端窗口中运行它时)。
到底为什么 activate() 关心“时间”?
只有我觉得这很疯狂吗?
这实际上与 X11 和可串行性有关。时间戳用于对消息进行排序并告知哪些消息迟到并且可以安全地忽略。否则,应该忽略的过去消息(因为它们的效果已被较新的消息覆盖)将错误地应用其效果。
在这种情况下,如果一条消息说激活窗口 X,而另一条消息说激活窗口 Y,但没有时间戳,则无法判断 X 的消息是发生在 Y 之前还是之后。
请参阅为什么 X 不是我们理想的窗口系统中的第 3 节,了解由于 X 协议中缺乏时间戳和可序列化性而导致的竞争。
另外,不应使用int(time.time()),即客户端上的时间,而window.activate(int(time.time()))应使用从服务器发送的最后一个时间戳。
Wnck 包含这个函数。这需要服务器往返。将其转换为 Python 是可行的,并且完全是另一个问题,但 Wnck 的 Python 绑定不导出此函数是愚蠢的,因为它是唯一返回其他函数期望作为参数的时间戳的函数:
/**
* get_server_time:
* @display: display from which to get the time
* @window: a #Window, used for communication with the server.
* The window must have PropertyChangeMask in its
* events mask or a hang will result.
*
* Routine to get the current X server time stamp.
*
* Return value: the time stamp.
**/
static Time
get_server_time (Window window)
{
unsigned char c = 'a';
XEvent xevent;
TimeStampInfo info;
info.timestamp_prop_atom = _wnck_atom_get ("_TIMESTAMP_PROP");
info.window = window;
XChangeProperty (_wnck_get_default_display (), window,
info.timestamp_prop_atom, info.timestamp_prop_atom,
8, PropModeReplace, &c, 1);
XIfEvent (_wnck_get_default_display (), &xevent,
timestamp_predicate, (XPointer)&info);
return xevent.xproperty.time;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果处理 X 事件的循环只是跟踪来自服务器的消息的时间戳,则需要往返。我认为 Wnck 或 GDK 做到了这一点,并且有一个获取值的函数。
使用 python 包含有效时间戳的一种简单方法是使用以下命令:
现在 = gtk.gdk.x11_get_server_time(gtk.gdk.get_default_root_window())
w.激活(现在)
这会提供wnck一个时间戳,以便不会打印警告。