如何获得Gnome Wayland上的活动窗口?

问题描述:

背景:我正在研究一个称为ActivityWatch的软件,它记录了您在计算机上执行的操作。基本上是试图解决一些问题:RescueTime,selfspy,arbtt等。如何获得Gnome Wayland上的活动窗口?

我们做的核心事情之一是记录有关活动窗口(类和标题)的信息。过去,这已经在Linux上使用xprop和现在的python-xlib完成了,没有问题。

但现在我们有一个问题:韦兰正在上升,并且据我所知,韦兰没有一个活跃的窗口的概念。所以我担心我们必须实现对Wayland可用的每个桌面环境的支持(假设他们将提供获取有关活动窗口的信息的能力)。

希望他们会最终收敛,并有一些共同的接口来完成这件事,但我不屏住呼吸......

我一直anticipating this issue。但是今天我们得到了一个Wayland用户的first user request for Wayland support。随着越来越多的发行版将Wayland用作默认的显示服务器协议(Fedora 25已经在使用它,Ubuntu将在17.10即将发布),随着时间的推移,情况将变得更加严峻。

为ActivityWatch相关的问题:

还有其他的应用,如ActivityWatch这将需要相同的功能(RescueTime,arbtt,selfspy等),他们现在似乎不支持Wayland,我也找不到有关他们计划这样做的任何细节。

我现在有兴趣实现对Gnome的支持,从开始,随着其他人跟进,路径变得更加清晰。

关于韦斯顿类似的问题已经在这里问:get the list of active windows in wayland weston

编辑:我问#wayland Freenode上,得到了如下答复:

15:20:44 ErikBjare Hello everybody. I'm working on a piece of self-tracking software called ActivityWatch (https://github.com/ActivityWatch/activitywatch). I know this isn't exactly the right place to ask, but I was wondering if anyone knew anything about getting the active window in any Wayland-using DE. 
15:20:57 ErikBjare Created a question on SO: https://*.com/questions/45465016/how-do-i-get-the-active-window-on-gnome-wayland 
15:21:25 ErikBjare Here's the issue in my repo for it: https://github.com/ActivityWatch/activitywatch/issues/92 
15:22:54 ErikBjare There are a bunch of other applications that depend on it (RescueTime, selfspy, arbtt, ulogme, etc.) so they'd need it as well 
15:24:23 blocage  ErikBjare, in the core protocol you cannot know which windnow has the keyboard or cursor focus 
15:24:39 blocage  ErikBjare, in the wayland core protocol * 
15:25:10 blocage  ErikBjare, you can just know if your window has the focus or not, it a design choise 
15:25:23 blocage  avoid client spying each other 
15:25:25 ErikBjare blocage: I'm aware, that's my reason for concern. I'm not saying it should be included or anything, but as it looks now every DE would need to implement it themselves if these kind of applications are to be supported 
15:25:46 ErikBjare So wondering if anyone knew the teams working with Wayland on Gnome for example 
15:26:11 ErikBjare But thanks for confirming 
15:26:29 blocage  ErikBjare, DE should create a custom extension, or use D-bus or other IPC 
15:27:31 blocage  ErikBjare, I guess some compositor are around here, but I do not know myself if there is such extension already 
15:27:44 blocage  compositor developers * 
15:28:36 ErikBjare I don't think there is (I've done quite a bit of searching), so I guess I need to catch the attention of some DE developers 
15:29:16 ErikBjare Thanks a lot though 
15:29:42 ErikBjare blocage: Would you mind if I shared logs of our conversation in the issue?          
15:30:05 blocage  just use it :) it's public                        
15:30:19 ErikBjare ty :) 

编辑2:提起enhancement issue in the Gnome bugtracker

tl; dr:如何在使用Wayland时在Gnome上获得活动窗口?

我有一个名为preguiça.py的脚本,它完全符合您的要求,尽管它可能更简单一些,我还没有发布它。

对于我的脚本,我使用PyGObject的Window Navigator Construction Kit(Wnck)获得了窗口标题。

下面是它的一个简化版本,与呈香部位:

from gi.repository import Wnck 
from gi.repository import GObject 

def changed (screen, window, data): 
    print ("Changed!") 
# window = screen.get_active_window() 
    if window: 
     print ("Title: %s" % window.get_name()) 

screen = Wnck.Screen.get_default() 
screen.connect ("active-window-changed", changed, None) 

mainLoop = GObject.MainLoop() 

try: 
    mainLoop.run() 
except KeyboardInterrupt: 
    print ("Hey") 

mainLoop.unref() 

为你问的例子实际上是注释掉上面的实际代码(我并不需要捕获的窗口,因为回调已经收到它),但你可能需要它,这取决于你的实现。

我为X编写了它,当我切换到Wayland时它没有抱怨,所以它可能适合你。

请注意,它并没有从Wayland那里得到信息,但它可能实际上更好,因为它将是X/Wayland不可知的。它从我打开的xterm获得了标题,所以它也应该是工具箱不可知的。

不过,不要问我关于实施的细节。代码是至少四年:) :)

+3

乍一看,这看起来不错。我现在无法对此进行测试,但根据[screen_get_default'](https://developer.gnome.org/libwnck/stable/WnckScreen.html#wnck-screen-get-default)上的文档:“This如果不在X11上,可以返回NULL。“ 我现在遏制我的乐观态度...... – erb

+1

这是'Wnck.Screen.get_default()'给出的警告:'(。:。9342):Wnck-WARNING **:libwnck被设计为仅在X11中工作,找不到有效的显示 – sebix

在我看来,你有最好的选择不是韦兰或任何可用的图书馆(没有一个)。实际上,在gnome-wayland中知道活动窗口是Mutter,所以你需要找到一种方法来请求Mutter活动窗口。 Gnome可以开发一个API来内部请求嘀咕活动窗口并恢复功能。但是,真的,你没有一个地方去问它。 Mutter不会开发一个API来访问他的内部表示,因为这仅仅是Mutter的特定细节,而不是所有Wayland的窗口管理器。因此,需要将此需求添加到外部库中,在该库中,该库可能会与当前窗口管理器进行通信,以便以常规方式解决您的请求。

另一个可能性是添加一个Wayland插件,其中所有的Windows管理器将有一种方式来共享当前活动的窗口,并以某种方式直接与路径库直接对话来恢复funcionality。

所以,你的应用程序是一个大问题。你可以做的大部分事情是在mutter(在哪里知道活动窗口)请求这个,但在我看来,它不能在Mutter中解决。

我希望这会帮助你,你可以找到一种方法。良好的洛克。