Interacting with kwin but not in a KWin script

Do you know if it’s possible to access the Kwin scripting API from anything else than a KWin script? The reason I ask is I would like to

  • choose my preferred language rather than use JS

  • access the file system (to save preferences, macros, etc)

  • develop and debug the application directly in Qt Creator rather than have to deal with the rather clunky loading/unloading of kwin scripts

  • In other words, I’d like to manipulate KWin’s windows from a full-fledged application, rather than from a script

Some methods of KWin, which are declared in classes with the Q_CLASSINFO(“D-Bus Interface”, “org.kde.kwin.***”) entry, are directly accessible through qdbus, but most are not. For instance the indispensable methods in Kwin::WorkspaceWrapper or Kwin::Window are not.

Do you know a way around that?

Looks like you will need to make the script and your application communicate using DBus

Or depending on what you’re trying do do you could try GitHub - jinliu/kdotool: xdotool-like for KDE Wayland · GitHub

1 Like

Since KWin scripts can be written in QML you could have a C++ plugin that exposes DBus methods, which you would call from your application, then from the script you either could talk back to the application via DBusCall qml type or from the C++ plugin.

1 Like

The easiest option is, as @luisbocanegra says, probably to use a KWin script and let it communicate with the app using D-Bus.

I think there are even topics here on Discuss which have examples on doing this with Python being used on the app side.

Alternatively there are various C++ plugin APIs in Kwin which could be used to load something that communicates with the app via a different mechanism.

Ultimately it should also be possible, just more work, to integrate a different scripting system.

JavaScript is just the easiest because Qt already has that as a module (via QtDeclarative)

Thank you for your replies.
From what I understand, you guys are saying that all I need is to have a KWin script act as some sort of gateway between its owner (KWin) and my application? That would be great: the script would simply forward calls back and forth.
The simplest way to do that would be through a pure javascript script.
But, the truth is I had thought about that. But I can’t find a way to access DBus from Javascript code. Web searches typically yield results such as ‘gjs’, ‘Node.js’ or ‘Cockpit’. Would any of these solutions be adequate?
As for using a C++ plugin (rather than JS), I’m open to that as well. Would you happen to have pointers as to how to achieve that? I can’t find any documentation or examples…

1 Like

JS callDBus > python D-Bus service Determine when monitor is turned on or off via python dbus - #9 by supagu

For the plugin check Creating C++ Plugins for QML | Qt Qml | Qt 6.10.2

Here is an example of a QML script that has a C++ plugin C++ plugin GitHub - luisbocanegra/kwin-minimize2tray: Hide windows to the system tray, similar to KDocker but in the form of a KWin Script that works on Wayland · GitHub

D-Bus

2 Likes

And there are plenty of KWin plugins in its repository even an example

1 Like

Thank you @luisbocanegra and @krake , your insight was very helpful.

I had not noticed in Kwin API’s documentation the existence of the callDBus function. Thanks to @luisbocanegra’s link to @supagu’s thread, I am now able to get going!

I appreciate your help.