LWN.net Logo

Akademy: KWin scripting

By Jake Edge
July 11, 2012

An effort to make KWin, KDE's window manager, more flexible, goes back more than two years, but it is now bearing fruit. Adding scripting support so that less technical users can make changes to the behavior and appearance of KDE without having to resort to C++ is now possible. KWin hacker Martin Gräßlin talked about the history and status of KWin scripting on July 1 at Akademy.

Some history

[Martin Gräßlin]

The idea of KWin scripting goes back to the Tokamak IV sprint in February 2010. The intent is to make the window rules more flexible. At that time, KWin rules were static and there was no way to say "I want all GIMP windows to go to a certain virtual desktop", for example. Gräßlin discussed the idea with Nuno Pinheiro, who leads the Oxygen theme project, at Akademy 2010, with Pinheiro making it clear that not requiring C++ was important.

A 2010 Google Summer of Code project added scripting support to KWin, but it suffered from a number of drawbacks. The API was hand-crafted and the documentation was hand-written. In addition, it "interweaved" the scripting and KWin core components in undesirable ways. It was, Gräßlin said, a prototype and one that should never have been merged.

One thing that was needed to support scripting is a generic animation effect. Previously, code was copied into many places to do the same kinds of effects for window state changes (e.g. window close or minimize). That meant that bugs got copied as well. A base implementation for window effects was added to react on state changes. That implementation is now made available for use in scripts.

Supporting a touch-based interface like KDE's Plasma Active was a "completely new world to us as a window manager", he said. KWin was highly mouse and keyboard-driven, which is not suitable for touch interfaces. Also, Plasma Active needs windows to always start maximized, which was a big difference. KWin started off supporting Plasma Active using ifdefs, but that became unwieldy. What was needed was a way to put a new interface on top of KWin without changing the core.

KWin changes

The window switcher is one place where scripting has come into play. Window switching is "surprisingly complicated" because there can't be one UI that fits all users, Gräßlin said. If there are just a few windows, there can be a simpler UI, but for ten windows the UI can't be the same. Since KDE 4.8, window switching can be scripted using QML, a JavaScript-based scripting language. There are methods to render thumbnails of the active windows and different use cases can use different layouts.

The same goes for desktop switching. It uses the same framework as the window switching support and will be able to be scripted using QML in KDE 4.9. Right now there is only layout available, as it is "not a focus" for the KWin project, but more could be added.

Support for QtScript, another JavaScript-based language, has been added for KWin as well. Those scripts can access all of the KWin options and client programs are exported to the scripts. Using D-Bus, scripts can be loaded and unloaded at runtime. Scripts can also call D-Bus methods and have their own configuration values.

Effects scripts can be written based on the AnimationEffect class. The scripts are written in QtScript, with an API that is similar to that of the KWin scripts. Instead of accessing clients, though, the effects scripts access windows. There is "no impact on performance", Gräßlin said, because no JavaScript is executed while rendering. In addition, window decorations in KWin have been rewritten in QML. That gives better performance, while allowing things like interactive previews of themes.

The KWin team likes to eat its own dog food, but that was difficult with the prototype scripting implementation. Most of the team did not know how to use the prototype. The newer scripting support is better understood, so the team is using it to find and fix bugs early on.

For KDE 4.9, several effects were ported from C++ to QML, including the "fade" and "fade desktop" effects. There are more effects to be done, and that is worth doing because 300 lines of C++ can be replaced with 20 lines of JavaScript, he said. He encourages other developers to work on this porting effort.

KWin is preparing to support Wayland by adding client properties to its API. These QProperties describe the client interface so that non-X clients can be handled. That means there will be one code base that can handle both window manager and compositing modes.

Multi-screen handling has also been changed substantially. There used to be lots of options governing the behavior of windows on multiple screens for historic reasons, but those have all been replaced. Now there is a single script for the only sensible choice: a video wall spanning all the screens. Other use cases could be scripted if needed, he said.

Scripting much of the functionality that used to be done in C++ has had a dramatic effect on the code size. KWin lost 5000 lines of C++, which was replaced with a few hundred lines of QML, Gräßlin said.

Third parties

Various third parties have started using KWin scripting. Get Hot New Stuff, the freedesktop.org project for sharing desktop resources, is using KWin scripts. A project to do window tiling for KWin exists as well. A tiling window manager struck some in the audience as rather amusing, but the implementation is working quite well and is not something that could have been done with C++, he said.

The "most impressive" user of KWin scripting is the Arctos Dashboard. It gives an overview of the open windows, has a KRunner interface, and an activity switcher. The developer has been collaborating with the KWin team and it has been a nice experience to have someone outside the project using and testing the scripting support, he said.

There may be other projects interested in KWin, beyond just KDE's Plasma. Razor-qt is interested, and Unity may want KWin because there are problems with its window manager (Compiz), he said. KWin has lost its static Plasma dependencies as they have been moved to runtime. There are still a few dependencies on kdelibs, but KWin could move to Qt-only in the future now that some KDE functionality is moving down into Qt.

Down the road, there are plans for integrating scripting with PlasMate, a development environment for creating Plasma widgets, themes, and more. There is a GSoC project underway to do that. Support for desktop thumbnails (e.g. for desktop switching) is already working though there are a few "glitches" with OpenGL rendering that need to be worked out.

Adding the ability to do unit testing with scripts is also in the works. Unit testing window managers has traditionally been difficult, so being able to inject test scripts simulating user interaction into a running KWin instance will be quite useful. Gräßlin said he is "really looking forward to that" as it will lead to a better tested code base for KDE 4.10 and 4.11.

The KWin team is looking for more people to try out the scripting additions and report on problems that they find. In addition, they are interested in hearing about use cases that they haven't thought about. It is "really easy to add things" and the team is open to new ideas, he said, so "give it a try". Scripting support has simplified KWin while also providing new capabilities; more input and testing can only help to make it better.

[ The author would like to thank KDE e.V. for travel assistance to Tallinn for Akademy. ]


(Log in to post comments)

Copyright © 2012, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds