这对我来说很奇怪。有人可以解释为什么 activate() 函数需要时间戳吗?难道 99.9% 的时间不是现在、尽快或“尽早方便”吗? 此外,如果您尝试 w.activate(0) 您会收到此警告:
Wnck-WARNING: Received a timestamp of 0; window activation may not function properly
我读到的关于此警告的每个论坛帖子都以没有答案告终。但它们似乎都表明除非您实际输入时间戳,否则代码无法正常工作。如果你输入 (0),事情就不起作用,你会收到警告。但是,对我来说,如果我输入时间戳,那就是事情不起作用的时候。如果我使用 (0),除了我收到警告(仅当我在终端窗口中运行它时)之外,该程序可以正常工作。
到底为什么 activate() 会关心“时间”?
我是唯一认为这很疯狂的人吗?
最佳答案
这实际上与 X11 和可串行化有关。时间戳用于对消息进行排序并告诉哪些消息迟到了并且可以安全地忽略。否则应该忽略的来自过去的消息,因为它们的效果已被新消息覆盖,将错误地应用它们的效果。
在这种情况下,如果一条消息说激活窗口 X 而另一条消息说激活窗口 Y 而没有时间戳,则无法判断 X 的消息是发生在 Y 之前还是之后。
参见 Why X Is Not Our Ideal Window System 中的第 3 节对于因 X 协议(protocol)中缺少时间戳和可序列化性而导致的竞争。
也不应该在 window.activate(int(time.time())) 中使用
而是从服务器发送的最后一个时间戳。int(time.time())
,它是客户端的时间
Wnck 包含这个函数。这需要服务器往返。将其转换为 Python 是可行的,并且完全是另一个问题,但 Wnck 的 Python 绑定(bind)不导出此函数是愚蠢的,因为它是唯一返回其他函数期望作为参数的时间戳的函数:
/**
* 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;
}
但是如果处理 X 事件的循环只跟踪服务器消息的时间戳,则需要往返。我认为 Wnck 或 GDK 可以做到这一点,并且具有获取值的功能。
关于python - 为什么 python Wnck window.activate(int(time.time())),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27448224/