I'm trying to write an application that sends different dBus signals to different applications depending on what application is active. The idea is to pair it with Libinput-gestures, and allow per application gesture response. Problem is, it's impossible to tell which application is active on the client side.
I've been doing some research into detecting if an application has focus on any particular window manager under Wayland. Consensus is, Wayland doesn't know if an application has focus, and will not give that information. However the window manager itself does know.
So is there a way of creating an entirely server side routine for gnome, to send the title of the active window client side, to a select number of applications. In other words, we still have the "security" of not letting arbitrary applications know everything about the environment, but still allow some accessibility minded software retrieve that information and use it.
Almost 2 years old, but here it goes!! I don't know for sure if Mutter may have a way to do this as well, or if there is a better way to do the following. I suspect with better knowledge of gdbus and further research you can also convert this into some sort of official gdbus monitoring that can communicate with other apps so you do not have to ping for the status.
This is specific to Gnome and I suspect works on at least 3.15+. Tested on Ubuntu 19.10 Wayland. (Gnome 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
And here is a gist I made as a note to myself, same as the code above. https://gist.github.com/rbreaves/257c3edfa301786e66e964d7ac036269
I also have a non-gdbus method on how to do something similar for KDE5 Plasma under Wayland up on my github for anyone interested. (added some code to an existing Plasmoid that was retrieving the class names already.)