在 Gnome/Wayland session 从 Mutter 检索活动 window

Retrieving active window from Mutter on Gnome/Wayland session

我正在尝试编写一个应用程序,它根据处于活动状态的应用程序向不同的应用程序发送不同的 dBus 信号。这个想法是将它与 Libinput-gestures 配对,并允许每个应用程序手势响应。问题是,不可能知道哪个应用程序在客户端处于活动状态。

我一直在做一些研究来检测应用程序是否关注 Wayland 下的任何特定 window 管理器。共识是,Wayland 不知道应用程序是否有焦点,并且不会提供该信息。然而 window 经理本身确实知道。

那么有没有办法为 gnome 创建一个完全服务器端的例程,将活动 window 客户端的标题发送到 select 数量的应用程序。换句话说,我们仍然有 "security" 不让任意应用程序了解环境的所有信息,但仍然允许一些具有可访问性的软件检索该信息并使用它。

快 2 岁了,但现在开始了!!我不确定 Mutter 是否也有办法做到这一点,或者是否有更好的方法来做到以下几点。我怀疑如果对 gdbus 有更好的了解并进行进一步研究,您还可以将其转换为某种可以与其他应用程序通信的官方 gdbus 监控,这样您就不必通过 ping 来获取状态。

这是 Gnome 特有的,我怀疑至少适用于 3.15+。在 Ubuntu 19.10 Wayland 上测试。 (侏儒 3.34.2)

# Single Command, runs 2 calls to gdbus to get the currently active Window from Gnome 3.x
# Escaped so you can copy and paste into terminal directly
gdbus call -e -d org.gnome.Shell -o /org/gnome/Shell -m org.gnome.Shell.Eval global.get_window_actors\(\)[`gdbus call -e -d org.gnome.Shell -o /org/gnome/Shell -m org.gnome.Shell.Eval global.get_window_actors\(\).findIndex\(a\=\>a.meta_window.has_focus\(\)===true\) | cut -d"'" -f 2`].get_meta_window\(\).get_wm_class\(\) | cut -d'"' -f 2

# Unescaped version, will not run
# Broken down into 2 commands.

# Call to Gnome to get the array location of the active Application
gdbus call -e -d org.gnome.Shell -o /org/gnome/Shell -m org.gnome.Shell.Eval \
global.get_window_actors().findIndex(a=>a.meta_window.has_focus()===true) \
| cut -d"'" -f 2

# Replace the array number 2 with the one from the previous command and you will get the App Name of the actively focused Window
gdbus call -e -d org.gnome.Shell -o /org/gnome/Shell -m org.gnome.Shell.Eval \
global.get_window_actors()[2].get_meta_window().get_wm_class() \
| cut -d'"' -f 2

这是我做的一个要点,作为给自己的笔记,与上面的代码相同。 https://gist.github.com/rbreaves/257c3edfa301786e66e964d7ac036269

我也有一个非 gdbus 方法,关于如何在我的 github 上为 Wayland 下的 KDE5 Plasma 做类似的事情,供任何感兴趣的人使用。 (向已经检索 class 个名称的现有 Plasmoid 添加了一些代码。)