Quick KDE KWin hack: Ignore global shortcuts except alt-tab

From time to time a pet peeve can reach a critical level of annoyance at which it starts to irritate. You google it, hoping to find a solution or at least some allies, but if none turns up and you’re out of time, you simply give up and hope a solution will magically manifest itself one other day. But what if that day never comes? Luckily for us, open source enthousiasts, we can do something about it ourselves 🙂

Context

I’m quite an avid shortcut user. Linux/KDE is an environment where this type of user feels right at home. Not only do most actions in a software have a shortcut, you can also define lots of system-wide or global shortcuts. Kwin, KDE’s window manager defines quite a few out of the box.

Problem

This is all fine and dandy until you want to use this cross-platform software which you got used to in a different environment (i.e. windows at work) and you want to start using it at home. When the application is not (KDE) shortcut friendly or you are so used to the windows keymap that it’s a pain to learn a new scheme, you start to feel frustrated. Learning a new shortcut scheme seems the best solution but this doesn’t work well when you have to maintain 2 shortcut schemes in your head, depending on your environment (home vs work, your computer at work versus a colleague’s computer).

A typical example here would be some IDE, for example Intellij IDEA. Although the IDE does provide a KDE keymap by now, I started using it ages ago when there simply was no alternative keymap (nor a community edition or even official linux version, I guess). Also, I used linux only occasionally back then. These shortcuts are hard-wired by now and it is already hard enough to keep in mind whether I’m using Intellij or Visual Studio.

Another possibility is that the application really needs that much shortcuts that it just can’t bother with the shortcuts which may or may not be defined by the environment or workspace. One example of such complex application is Blender. Try it out and you’ll see what I mean. 😉

KDE, being the configurable desktop environment, does offer some configuration settings. For each window, Kwin allows you to define rules which match certain windows / applications which creates a config. Using this config, you can configure special window attributes. One setting is to block all global shortcuts.

This allows you to use your favorite application with its favorite native shortcuts. Of course, most people use more than 1 application at the same time. However, since all global shortcuts are blocked, the alt+tab shortcut to switch windows is blocked too. Alt+tab is not a special shortcut to KDE, it simply is a shortcut that happens to be assigned globally to the “Walk through windows” action exposed by the KWin component to the System Settings. Lacking any window switching shortcut, you adapt and start using the mouse to switch applications. There are times you cope just fine. But there are other times it is just one thing too much that annoys you. 🙂

Solution

So how to fix this? Just like any other problem you have with an open source application: fetch the source, fix and build! 😉

Since I just wanted a quick fix (I thought, let’s check this out for 30 mins, which turned out to be a lot longer of course), I chose to patch the behavior of the “Ignore global shortcuts” setting to exclude the Alt+Tab shortcut (more precisely, the shortcut currently assigned to the “Walk through windows” action). I briefly investigated KWin’s scripting abilities but it turns out the required functionality is not exposed.

I will now document the process, which took place on opensuse leap 42.3. You may want to tune out now if you don’t care about the gritty tech details. 😉

Where is the source?

I thought it had to be kwin, right? So I checked out the source from github and started searching for “Ignore global shortcuts”. It turned out to just call something in kglobalaccel which is not part of kwin.

Getting my hands dirty, I first fired up a VM I had lying around in order not to f*ck up my currently running system. Next I installed Qt Creator to easily navigate the code. Also, never forget you have to checkout the tag for the version you are currently running! (can be a treasure hunt by itself)

Browsing the source, I finally find a possible tweaking point in the src/runtime/component.cpp -> deactivateShortcuts method. It disables all shortcuts when a window is entered which has a “Ignore global shortcuts” rule. So I simply added an exclusion for the “switch windows” action.

Building a patched rpm

Time to try it out! In order to build it, you will need to install its dependencies. On an rpm based system, the easiest way to go forward is to do a “sourceinstall” of the rpm in case. For that, we first have to know which rpm contains the affected shared object (.so file).

You can take different routes to pin point the rpm. I checked the CMakeLists.txt to see what the output targets were. When you build, you can also see the class pass by and check into which library it gets linked into. Using that .so file, you can find the rpm using “rpm -qf <.so>”

An alternative way would be to find a kde related rpm matching “global” and then checking its contents (using “rpm -q –filesbypkg <rpm>”) for the .so file.

Once you know the rpm, you can easily install all required build dependencies by issueing a zypper sourceinstall command: “zypper si <rpm>”

The hard part: logging

I wanted to add some debug output to see if I was looking at the right place in the source base and how frequently the code got called. Now things got messy. You’d think patching the code was the hard part, guess what.. 😉

I had played with logging before in KDE, but of course things have changed with KDE 5. Nowadays, KDE uses Qt’s default logging mechanism. It meant figuring out the logging category used by kglobalaccel and finding a way to activate it. The logging category was nicely tucked away in logging_p.h: “kglobalaccel-runtime”. Activating it seemed to be trickier. According to the Qt documentation, you can alter the log level through an env var QT_LOGGING_RULES. Since I was adapting a core component, I needed to set the env var before logging in. On opensuse you can create a file /etc/profile.local which gets sourced by /etc/profile.

I added the env var and it seemed to work (=showed up), unfortunately still no logging. More precisely, nothing showed up in ~/.xsession-errors-:0. I started adding log statements all over the place, analysing the .so file to check whether my modified method was actually included in it (readelf -Ws libKF5GlobalAccelPrivate.so.5.32.0  | grep deactivate), etc.. Until I realised I didn’t check the journal (journalctl -b -0) yet, there it was. 🙂 Bonus points: the code change had its intended effect!

End result

So there I had it, a working patch WITH logging ⇒ pure SATISFACTION 🙂

Feel free to share this satisfaction by downloading the .so and installing it in /usr/lib64 (use at own risk). I’ve been using it for 2 months now without problems. It’s a good hack until a more fundamental solution is in place. First steps have been taken! 😉

Using KDE systray notifications from bash scripts

I just improved my Extract audio script to show a system notification when it’s finished. In the meantime I also have a “convert to mp3” script which proves useful if you don’t care about the original audio stream and/or you target a platform with limited decoding abilities:

ffmpeg -i %u -acodec libmp3lame -ar 44100 -ab 160k %u.mp3

To popup a KDE4 system tray notification at the end of the encoding process, simply add the following:

kdialog --passivepopup 'Finished converting %u' 3

The last value simply specifies the timeout in seconds. To chain the commands together, use a semicolon, so it also executes when the encode fails.

Mouse locating kwin effects

Ever felt lost on your desktop? Or more precisely: unable to find your mouse pointer within a (few) second(s)? I know I have, and nowadays on 2 monitors or more (or a huge 48″ tv), this isn’t an exception.

In KDE, we always had the track mouse effect which draws circling yellow stars around your pointer. Unfortunately, this is not adequate anymore. That’s why I thought it would be fun to write my own effect which would do a better job. The key to success seems to me that it should be a full screen effect drawing the attention to the right spot no matter where at the screen you’re looking.

For now, I’ve devised 3 effects:

  1. Looney tunes zoom: fancy term for describing an effect which darkens the background and zooms in on the cursor with a spotlight. I somehow associated that with intro or outro from looney tunes, not sure if that makes sense. :p
  2. Sunray: animates some kind of rays around the mouse position.
    kwin sunray mouse locate effect
  3. Radial texture animation: least fancy name, as it is so generic. 🙂 It just animates a texture from outer to inner. The concrete application is to show arrows pointing to the mouse.
    kwin arrow mouse locate effect

For the last 2 effects, I don’t have screencasts yet, don’t ask, I’ve been through hell to create the first one. Also, for the last one, I have to fix the texture uv map generation, or use a better mesh (or maybe a different technique 🙂 ).

Configuring numlock indicator in KDE4

My brother has this really neat wireless keyboard with built-in trackball for his mediacenter. There is only one downside: it doesn’t have a numerical keypad and you never really know if the numlock is on or off. I think it is off at boot, but after turning it on and logging on, it is off again, unless you did a reboot.. or something like that.. Anyway, since it can be quite a challenge to enter passwords correctly, I started looking for a numlock plasmoidish solution.

First thing that turned up in the searches was the Keyboard Status applet. However, in the comments someone rightfully asks why it doesn’t use the keystate DataEngine. Great idea, because this is already packaged with vanilla kde. So I started searching for a general LED plasmoid which I could connect any datasource to using some mapping. No such luck. But then I wondered: why would someone write a plasma DataEngine and then not use it? 🙂 There must be some existing plasmoid! It turned out to be the Key State plasmoid which is currently hosted at gitorious.

Since it is a javascript plasmoid, you can install it as follows:

git clone git://gitorious.org/keystate/keystate.git
plasmapkg -i keystate

Done! It should now be in the list when adding plasmoids under the name “Key State”. You can configure a custom color for each special key. There are some advanced options as well which I didn’t try out. Here is what it looks like:

Key State plasmoid

A simple rectangle shows my numlock is active. I know, nothing fancy but it’s all we need and it does the job! Thank you Martin Blumenstingl. 🙂

Extract audio from AV container service menu for KDE4

Nowadays, we live in a KDE4 world for real, so it’s time to update my “dump audio from video file” service menu 😉 It should work both in Konqueror and Dolphin.

Create a file with the following contents and put it in one of your service directories (which you can find out by running “kde4-config –path services” , typically you will have something like $HOME/.kde4/share/kde4/services )


[Desktop Entry]
Type=Service
Encoding=UTF-8
ServiceTypes=KonqPopupMenu/Plugin,video/*
Actions=extractaudio

[Desktop Action extractaudio]
Name=Extract audio
Name[en-GB]=Extract audio
Exec=mplayer -dumpaudio %u -dumpfile %u.dump
Icon=speaker