How do I get the active window on Gnome Wayland?

前端 未结 3 625
春和景丽
春和景丽 2020-12-24 12:38

Background: I\'m working on a piece of software called ActivityWatch that logs what you do on your computer. Basically an attempt at addressing some of the

3条回答
  •  醉话见心
    2020-12-24 13:05

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

提交回复
热议问题