Toggle KWin compositing on and off easily from your Desktop

KWin’s Desktop Effects work well and I usually have them enabled, only disabling compositing temporarily while I play a game (I’m a sucker for old arcade games running in GMAMEUI).

One way to enable/disable compositing in KWin 4.7.0 is via System Settings > Desktop Effects > tick/untick ‘Enable desktop effects at startup’ > Apply. This change only takes effect when you restart KDE. A way to toggle compositing on/off is to use a keyboard shortcut. The default keyboard shortcut for this in KWin is Alt-Shift-F12, but KDE on my machine tells me that Qt does not like the use of the Alt key in this shortcut, so I used System Settings > Desktop Effects to change the shortcut to Ctrl+Shift+U, which does toggle compositing on/off.

A toggle icon on the Desktop is another way of achieving the same thing, and is handy if you are using your mouse at the time. Now, a Plasma widget called Toggle-Compositing exists and is a nice way of enabling/disabling compositing at the click of a mouse. However, it has to be compiled — which some users may feel reluctant to do — and it requires the user to know the $KDEDIR, and I’ll wager some users don’t.

Below I describe one way of adding an icon to your Desktop that you can use to toggle compositing in KWin and that does not require any compiling, so it should be more straightforward to implement by a newcomer to KDE.

Use your preferred text editor to create the following Desktop Configuration File named toggle_compositing_in_KWin in your Desktop directory:

[Desktop Entry]
Comment[en_GB]=toggles compositing in KWin
Comment=toggles compositing in KWin
Exec=sh /home/fitzcarraldo/toggle_compositing_in_KWin.sh
GenericName[en_GB]=toggles compositing in KWin
GenericName=toggles compositing in KWin
Icon=kwin
MimeType=
Name[en_GB]=toggle_compositing_in_KWin
Name=toggle_compositing_in_KWin
Path=
StartupNotify=true
Terminal=true
TerminalOptions=
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=none
X-KDE-SubstituteUID=false
X-KDE-Username=

(Replace “fitzcarraldo” with your user name, of course.)

Then create a text file ~/toggle_compositing_in_KWin.sh containing the following:

#!/bin/bash
echo -n "Current compositing state: "
qdbus org.kde.kwin /KWin compositingActive
echo -n "Changing compositing state..."
qdbus org.kde.kwin /KWin toggleCompositing
echo -n "Current compositing state: "
qdbus org.kde.kwin /KWin compositingActive
echo -n "All done. Press ENTER to close window: "
read ENTRY

Finally, open a Konsole window and enter the following command under your own user account:

chmod +x toggle_compositing_in_KWin.sh

And there you have it. You’ll have an icon on your Desktop that looks something like the following:

KWin icon

KWin icon

All you need to do is double-click (or single-click, if you have configured KDE that way) on the icon toggle_compositing_in_KWin on your Desktop and a window will pop-up telling you:

Current compositing state: true
Changing compositing state...
Current compositing state: false
All done. Press ENTER to close window:

To re-enable compositing, just double-click the icon again. And so on.

And now the final touch…

Actually, it’s not necessary to include the last two lines:

echo -n "All done. Press ENTER to close window: "
read ENTRY

in the Bash script above; I just put them in to display the current compositing state. If you omit those two lines then toggling compositing on and off simply becomes a double-click on the icon on the Desktop (or a single click if you have configured KDE that way). In fact, if you also change Terminal=true to Terminal=false and StartupNotify=true to StartupNotify=false in the Desktop Configuration File, then you won’t even see a Konsole window pop-up briefly: compositing will just be toggled on and off seamlessly, so to speak. Just like the keyboard shortcut, only easier to remember!

About Fitzcarraldo
A Linux user with an interest in all things technical.

3 Responses to Toggle KWin compositing on and off easily from your Desktop

  1. Bart Broek says:

    A little variant, this script fetches the pid of kwin
    #!/bin/bash
    kwinid=`ps -C kwin |awk ‘NR == 2 {print $1}’`
    qdbus org.kde.kwin-$kwinid /KWin toggleCompositing
    exit

  2. Thank you so much for this tip! Works good on Opensuse 13.1.

  3. Fitzcarraldo says:

    In KDE Plasma 5 the commands for suspending and resuming compositing are, respectively, the following:

    qdbus org.kde.KWin /Compositor suspend

    qdbus org.kde.KWin /Compositor resume

    Therefore you will need to change the contents of the Bash script ~/toggle_compositing_in_KWin.sh mentioned in my blog post to the following:

    if [ `qdbus org.kde.KWin /Compositor active` = true ]; then
        qdbus org.kde.KWin /Compositor suspend
    else
        qdbus org.kde.KWin /Compositor resume
    fi
    

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.