Does KWin emit a signal (via DBus) on window change?

When I select a new window, I want to be able to consume a signal from the compositor / window / display server. However, KWin doesn’t appear to emit one.

However, this is such a fundamental feature that I don’t imagine that such a signal doesn’t exist. Consequently, is it merely not emitted via qdbus-qt6? If so, how does one consume it? (kdotool?)

I doubt this is done automatically.

Without any information about the window this would not be very valuable and information about the window, e.g. title, would leak information in an uncontrollable way.

Obviously KWin itself knows about the switch and its plugins and scripts can react to that.

You could likely “push” such an event via D-Bus from within a script

1 Like

I use a Javascript KWin script to do just that:

const SERVICE_NAME = 'com.example.blah';
const OBJECT_PATH = '/com/example/blah';
const INTERFACE_NAME = 'com.example.blah';

workspace.windowActivated.connect(function(client){
    if (client) {
        callDBus(
            SERVICE_NAME,
            OBJECT_PATH,
            INTERFACE_NAME,
            'ActiveWindowChanged',
            client.resourceClass,
            client.caption
        );
    }
});

In my case, resourceClass and caption of the window are the things I care about, and therefore the arguments of the DBus method that consumes the message - but you can obviously choose your own.

You can just use dbus-monitor to listen if you like - see Execute a command from a kwin script - #4 by pg-tips

2 Likes

@krake, thanks. That’s a shame, though, because doesn’t that require that I register a .desktop, main.js, and create a DBus signal, socket, or file to communicate outside of it with? For comparison, on Windows 24H2, I could call SetWinEventHook with C# code inline inside the PowerShell CLI, and subscribe to events from that.


@pg-tips, I commented this before I saw your comment. Thanks, too.

However, does it not also require a significant amount of preparation? The reason that I ask is because I’m trying to prepare a short script, to send to someone else, so the more that I can automate, the better. Certainly, I could touch each file to create a valid KWin Script, but that’s difficult. Can I somehow pipe this to KWin?

I guess Windows has some kind of permission system which asks the user for consent for such an operation, maybe even at install time.

Or it doesn’t care if random applications look at user activity without them knowing or able to detect.

I think KWin has a D-Bus API for loading scripts

What’s needed is:

  1. Make a KWin script package - this is just a zip file (conventionally with extension .kwinscript rather than .zip) whose contents have the structure:
myscript
├── contents
│   ├── code
│   │   └── main.js
├── metadata.json

You can do this on your side and send your friend the .kwinscript file.

  1. Install the script into Plasma, either through the System Settings app (“KWin Scripts” page, then click the “Install from File” button), or with these commands:
kpackagetool6 -t KWin/Script --install "/path/to/myscript.kwinscript"
qdbus-qt6 org.kde.KWin /Scripting unloadScript "myscript"  # here, "myscript" is the Id of the KPlugin in metadata.json
qdbus-qt6 org.kde.KWin /Scripting start

Perhaps you could make a script containing these 3 commands and send that to your friend, if it’s easier than doing the install in the GUI.

3 Likes