Skip to content

Instantly share code, notes, and snippets.

@rbreaves
Last active October 10, 2024 09:00
Show Gist options
  • Save rbreaves/257c3edfa301786e66e964d7ac036269 to your computer and use it in GitHub Desktop.
Save rbreaves/257c3edfa301786e66e964d7ac036269 to your computer and use it in GitHub Desktop.
Grab wmclass name or Window Name/Title under Wayland with Gnome 3.x
# 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
@DiegoMagdaleno
Copy link

Btw, as for the extension code above, is missing the "init" function, so the code needs to have this at the end

function init() {
     return new Extension();
}

Now with that out of the way, wouldn't the best way to implement this on GNOME, be:

Implement a gdbus "client" inside xkeysnail, this would have to be async I guess, then, we could have our extension, but what we could try to do (I don't know is GNOME has an API or an event for this), but we can detect if the user changed windows, after that, we can emit a dbus event (This example if from the command line, but I could make one programatically) and since we are connected to the system/user bus via our client, we could just "listen" for this signal, the moment the signal is emited, we could just grab the window that is marked as "active" and return that as the window title

@rbreaves
Copy link
Author

rbreaves commented Jan 5, 2022

Maybe I’d have to look into it more. Xremap has solved it apparently for gnome & wayland. Kinto could be redone to use xremap or we can yes patch xkeysnail & do a PR on it.

https://github.com/k0kubun/xremap

@johan-bjareholt
Copy link

@rbreaves Xremap seem to do a similar dbus Eval call according to the README

busctl --user call org.gnome.Shell /org/gnome/Shell org.gnome.Shell Eval s 'global.get_window_actors().map(a => a.get_meta_window().get_wm_class());'

@DiegoMagdaleno
Copy link

@rbreaves I like how xremap looks and I think it is a little bit more modern than xkeysnail, however, I don't know how much effort it would take to rewrite or redo Kinto to use xremap.

@rbreaves
Copy link
Author

rbreaves commented Jan 5, 2022

@DiegoMagdaleno probably not as much as you think. The syntax for it looks quite similar to a yaml config format I’ve been wanting to make part of either Kinto or xkeysnail actually. That would have been a simple translation layer though.

@canadaduane
Copy link

Just wanted to highlight some cool work that rvaiya is doing in keyd here: https://github.com/rvaiya/keyd/blob/master/scripts/keyd-application-mapper

In this latest beta, keyd now offers its hot-swappable keyboard layouts and layers with application focus conveyed by that python script via a socket to the keyd daemon. Works on X, Wayland+Sway, and Wayland+Gnome.

@rbreaves
Copy link
Author

That is very interesting, I've not used sway any.. and sadly my distro of choice, Ubuntu Budgie, does not have access to gnome-extensions even though it works with gnome-shell still.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment