houmain / keymapper

A cross-platform context-aware key remapper.
GNU General Public License v3.0
298 stars 25 forks source link

KDE Wayland: not detecting window attributes #80

Closed 1player closed 9 months ago

1player commented 10 months ago
% ./keymapper -v -u
Loading configuration file '/var/home/sph/.config/keymapper.conf'
Connecting to keymapperd
Sending configuration
Initializing focused window detection:
  X11 support: initialized
  D-BUS support: initialized
  wlroots support: not available
Entering update loop
Configuration updated
Detected focused window changed:
  class = ''
  title = ''
  path = ''

The focused window is GNU Emacs with class emacs. Wayland support is compiled correctly, as shown by cmake.

-- Checking for module 'dbus-1'
--   Found dbus-1, version 1.14.10
-- Checking for module 'wayland-client>=1.13.0'
--   Found wayland-client, version 1.22.0

Tested with the latest code from master.

1player commented 10 months ago

Ah, looks like KDE has its own Wayland protocol to do that... https://wayland.app/protocols/kde-plasma-window-management

If I was more comfortable with C++ I would attempt to write a PR, but for now I leave it to more experienced hands.

1player commented 10 months ago

I've spent a few minutes trying to add support for that protocol, but I do not see it exported on a live KDE system. Perhaps because it says it can only be bound to one client at a time.

I noticed there's a org.kde.KWin.queryWindowInfo DBus method:

Screenshot_20231116_113204

Which returns all the information we need in the caption and resourceClass attributes (title and app_id respectively). Sadly this is only for interactive use: you need to manually click a window to get its information back. There is a getWindowInfo method that requires a window UUID parameter, but I don't see any way of listing all windows in the first place. Why is this so needlessly complex...

I reckon the viable approach is to replicate what you're doing for GNOME, and create a custom KWin script that interacts with the Keymapper DBus server. Determining the attributes of the active window from there should be easy enough.

See also:

I'm not sure when I'll have the time to work on that, but this should be enough information to get started if anyone wants to have a crack at it.

houmain commented 10 months ago

Hi, thank you very much for your contributions! This was already on my TODO list.

I stumbled over this kwin-script.js from the xremap project.

For keymapper it likely will be:

workspace.clientActivated.connect(function(client){
    callDBus(
        "com.github.houmain.Keymapper",
        "/com/github/houmain/Keymapper",
        "com.github.houmain.Keymapper",
        "WindowFocus",
        "caption" in client ? client.caption : "",
        "resourceClass" in client ? client.resourceClass : ""
    );
});

But there is also some rust code, so I do not know if putting the .js somewhere is enough... From what I can see here a script and a metadata.json should be enough. Maybe you could figure that out?

alchzh commented 9 months ago

I confirmed in plasma-interactiveconsole that this script works. The extra code is there to load the script into KWin, but another option is packaging the script per https://develop.kde.org/docs/plasma/kwin/ into a regular script and installing it from system settings.

houmain commented 9 months ago

Hi Albert, thanks for trying it out! It is now in the 3.3.0 release.