How do I get the active window on Gnome Wayland?

不想你离开。 提交于 2019-12-02 20:10:19

In my opinion the best choise you have is not Wayland or any available library (there are not one). Actually who know in gnome-wayland about the active windows is Mutter, so you need to find a way to ask to Mutter the active windows. Gnome can develp an API to internally ask to mutter the active window and restore the funcionality. But really, you have not a place to ask for it. Mutter will not develop an API to access to his internal representation, becasue this will be pretty specific of Mutter only and not to all Wayland windows manager. So this need to be added to an external library, where this library could talk probably with the current window manager that it's in use to resolve your request in a general way.

Another posibility is add a Wayland plugin where all windows manager will have a way to share the current active windows and in some way a library to talk directly with wayland to restore the funcionality.

So, your app is in a big problem. Most you can do is request this on mutter (where is know the active windows), but in my opinion it can not be resolved in Mutter.

I hope this will help you and you can find a way. Good loock.

I have a script called preguiça.py, that does exactly what you're doing, though it is probably a lot simpler and I haven't released it.

For my script, I acquired the window title using PyGObject's Window Navigator Construction Kit (Wnck).

Here's a simplified version of it, with the essencial parts:

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 ()

The actual code for what you're asking is actually commented out on the example above (I didn't need to capture the window, as the callback already receives it), but you may need it depending on your implementation.

I wrote it for X, and it didn't complain when I switched to Wayland, so it should probably work for you.

Note it doesn't get the information from Wayland, as you asked, but it is probably actually better, as it will be X/Wayland-agnostic. It got the title from an xterm I opened, so it should be toolkit-agnostic, as well.

Don't ask me on the details of the implementation, though. The code is at least four years old :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!