Category Archives: Uncategorized

Underline – A Minimalistic Readline Replacement

(Lib)underline is a small libreadline compatible commandline C library. Command line interface (CLI) may well be the first and arguably one of the most intuitive human-computer interfaces.

I wrote underline when I needed a CLI for an embedded system without operating system and a very limited (and horribly outdated) libc support. My original thought was to port libreadline, the de-facto standard. But in readline’s own words:
“It’s too big and too slow” (man 3 readline – Bugs section).

Underline implements a compatible (although partial) commandline library. The entire implementation is less than 500 lines of code and has some nice features in it:

  • Standard synchronic readline API for basic usage
  • Fully asynchronous API
  • History support (although quite limited)
  • Hotkeys support (for home/end etc.)
  • Basic VT-100 compatibility. Support standard termios library or custom VT implementation.
  • Fits embeeded systems – No memory allocations, small code size

The asynchronous API is suitable for systems requiring high performance. It was tested on Posix AIO, Arduino and RTOS like systems.

Clone the code from the github repo at:


The implementation is, well, minimal. There’s lots to do, especially if you have special needs.

Custom hotkey mapping – At the moment all hotkeys are hardcoded. Should be user override-able.

Command completion – Hitting TAB should call a user defined callback with the partial current word (and the full line).

Dynamic history buffer – The history buffer is static and not optimized. You could change the defines to get more history or implement a custom memory allocator or an arena if you need dynamic history size.

Dynamic prompt – Prompt can be used for fun stuff like showing the current path, Git branch or number of unread emails. Functionality to update the prompt with some helpers is a nice feature.

Terminal width/multiline support – Receive resize events from terminal and support multiline prompt (like bash’s ‘\’ or iPython’s Shift+Enter)

Thread safety – completely not written to be thread safe. The synchrous API has a static context and none of the functions was tested to be reentrant. If you want, make the context struct TLS or protect it and check the functions.

More modular code – History should probably be a module


Leave a comment

Filed under Uncategorized

How to Fix Broken Steam Linux Client With Radeon Graphics Driver (Workaround)

Short Version
If Steam gives out the error: “OpenGL GLX context is not using direct rendering, which may cause performance problems.” and you’re sure the graphics driver is loaded properly try the following script to run Steam:

export LD_PRELOAD='/usr/$LIB/' #Export so all child processes are affected as well
export DISPLAY=:0
#export LIBGL_DEBUG=verbose

Long Version
In the past 6 months my livingroom PC is a Linux box with AMD graphics card. I could never get AMD’s binary driver (FGLRX) to work with Steam reliably but the open source Radeon driver is surprisingly good and keeps improving. The downside is that every so often Steam client updates break for weeks until another update fix it. After going through this several times I decided to actually figure out the root cause and fix it. Here’s a quick walkthrough on how to debug your Steam client setup and how to fix a common problem that may pop up with open source graphics driver.

Running steam from the command line gives out the venerable ‘GLX direct rendering’ error but there’s a hint in the lines before it:

Running Steam on ubuntu 14.10 64-bit
STEAM_RUNTIME is enabled automatically
Installing breakpad exception handler for appid(steam)/version(1420770381)
libGL error: unable to load driver:
libGL error: driver pointer missing
libGL error: failed to load driver: radeonsi
libGL error: unable to load driver:
libGL error: failed to load driver: swrast
Installing breakpad exception handler for appid(steam)/version(1420770381)

Steam can’t open, the shared library responsible for communicating with the graphics driver. To rule out a problem with the driver itself, let’s verify an OpenGL enabled graphics driver is loaded by running:
DISPLAY=:0 glxinfo | grep -i direct
The output should be:

direct rendering: Yes

Next, to debug Steam load run the Steam client from commandline in verbose mode:
DISPLAY=:0 LIBGL_DEBUG=verbose steam

And now the output is:
Running Steam on ubuntu 14.10 64-bit
STEAM_RUNTIME is enabled automatically
Installing breakpad exception handler for appid(steam)/version(1420770381)
libGL: screen 0 does not appear to be DRI3 capable
libGL: pci id for fd 7: 1002:6798, driver radeonsi
libGL: OpenDriver: trying /usr/lib/i386-linux-gnu/dri/tls/
libGL: OpenDriver: trying /usr/lib/i386-linux-gnu/dri/
libGL: dlopen /usr/lib/i386-linux-gnu/dri/ failed (/home/user/.local/share/Steam/ubuntu12_32/steam-runtime/i386/usr/lib/i386-linux-gnu/ version `GLIBCXX_3.4.20' not found (required by /usr/lib/i386-linux-gnu/dri/
libGL: OpenDriver: trying ${ORIGIN}/dri/tls/
libGL: OpenDriver: trying ${ORIGIN}/dri/

The dlopen error tells us the file exist but there was an error with the LibC6 version the driver requires. Let’s check which LibC6 version is actually behind the that’s loaded:

~$ ls -l /home/user/.local/share/Steam/ubuntu12_32/steam-runtime/i386/usr/lib/i386-linux-gnu/

lrwxrwxrwx 1 user user 19 Jul 19 00:52 /home/user/.local/share/Steam/ubuntu12_32/steam-runtime/i386/usr/lib/i386-linux-gnu/ ->

Let’s check what version is installed on the global /usr/lib path:
~$ ls -l /usr/lib/i386-linux-gnu/

lrwxrwxrwx 1 root root 19 Oct 11 14:58 /usr/lib/i386-linux-gnu/ ->

So Steam loaded LibC6 with ABI version 18 where Radeonsi expects version 20.

Hold on for a second, why does this happen in the first place?

The answer is STEAM_RUNTIME. Steam for Linux packs a set of (standard) shared libraries called STEAM_RUNTIME. This allows the guys at Valve to optimize the client for specific dependencies without fearing the target machine will have different version. This is somewhat similar to static compilation where you compile the program’s dependencies in the main binary.

The problem is that Steam can’t come preloaded with ALL the libraries it needs and has to rely on the OS to supply some. This is exactly the case with This library is in charge of the client’s OpenGL interface with the actual driver. Radeonsi also needs libstdc++ from LibC6 and gets updated independently every once in a while. Whenever an update changes the ABI, the global LibC6 and Steam’s gets out of sync.

So the only thing we need is to get Steam to load the newer version of the library.

Some forums advise you to remove Steam’s version of LibC6 by running:

rm /home/user/.local/share/Steam/ubuntu12_32/steam-runtime/i386/usr/lib/i386-linux-gnu/

etc. This won’t work since Steam checks the integrity of its runtime and will fix the missing files on load.

A more elegant solution would be to have the proper LibC6 version loaded by the OS, by running:
LD_PRELOAD=/usr/lib/i386-linux-gnu/ DISPLAY=:0 steam

This seems to work but puts out a lot of errors in the form of:
ERROR: object '/usr/lib/i386-linux-gnu/' from LD_PRELOAD cannot be preloaded (wrong ELF class: ELFCLASS32): ignored.

This error is an artifact of the fact Steam is a 32-bit binary running on a 64-bit machine. LibC6 has two versions (32-bit and 64-bit). The Steam binary needs the 32-bit version but other parts of the client run as native 64-bit and when they start they will get the LD_PRELOAD environment specifying a library with the wrong format. So how do we tell LD_PRELOAD to take the right one?

LD_PRELOAD='/usr/$LIB/' DISPLAY=:0 steam

The somewhat non-intuitive $LIB parameter gets expanded in to the right path based on the platform of the process being started (man 8 for details).
The quotes around the environment are mandatory. It tells the shell not to try and expand the $LIB but rather pass it as is.

So finally Steam will load cleanly. A more permanent solution can be implemented in a loading script that checks if the global LibC6 version is newer than the one in STEAM_RUNTIME and only then LD_PRELOAD’s. I’ll leave this as an exercise to the reader.

Happy Linux beta gaming!


Filed under Uncategorized

Toggle KWin Composition Based in Power-Profiles on KDE 4.8

One of the changes in KDE 4.8 was KWin’s new composition / effect toggle handling. As a side effect the power management profiles no longer turn off composition and effects when the AC cord is plugged out.

This behaviour may be fixed in the future, in the meantime, below is a simple workaround:

1. Create a shell script something like this:

if [[ $1 == "on" ]]; then
    if [[ "$(qdbus org.kde.kwin /KWin org.kde.KWin.compositingActive)" = "false" ]] ; then
        qdbus org.kde.kwin /KWin org.kde.KWin.toggleCompositing
elif [[ $1 == "off" ]]; then
    if [[ "$(qdbus org.kde.kwin /KWin org.kde.KWin.compositingActive)" = "true" ]] ; then
        qdbus org.kde.kwin /KWin org.kde.KWin.toggleCompositing
    echo "Usage: $0 [on|off]"

2. Make the script executable (chmod +x) and test it from the command line

3. Go to ‘Energy Saving Settings’ and edit the default ‘On AC Power’ profile. Check ‘Run Script’ and type in:
/full path to your script/script_name on


4. Repeat the same for the “On Battery” profile, but this time pass the argument off

5. Finally test the script by disconnecting the AC power and note that KDE notifies that composition has been suspended. Also running the command:
qdbus org.kde.kwin /KWin org.kde.KWin.compositingActive
Should return ‘false’

Now you can enjoy extended battery life.


Filed under Uncategorized

Using Nvidia Optimus on Linux with Bumblebee

If you have an Optimus enabled laptop with Linux on it there are some good news around.

First, some background. Nvidia released laptops with Optimus based switchable graphics over a year ago but Linux support was missing. This means you must choose in the BIOS between battery guzzling dedicated graphics card or limited on-CPU graphics. On other OS users can choose to run specific application on the dedicated card and keeping it powered down when idle.

For some time now there’s been some unofficial work to get the feature working on Linux (see previous post on the topic). One of this efforts evolved to be the Bumblebee project.

Last week the project released version 0.3. The version features automatic stable and persistent power state. Simply put this means that even if your laptop goes to sleep mode and wakes up, the Nvidia card will stay powered down unless you decide to run an application on it. Aside from automatic power-state setting, the project also allows the user to run OpenGL applications on the Nvidia card with a command ‘optirun’.

Installation process has been greatly simplified, follow the link to instructions for your distribution on the release announcement page.

On the official side Nvidia started looking for ways to provide official support but it seems that this won’t happen in the immediate future.

Meanwhile I highly recommend installing the unofficial support so you can enjoy your switchable graphics card under Linux (for example check out Starcraft II under Wine).

Leave a comment

Filed under Uncategorized

Switch to Phonon-VLC for Better Sound in KDE

Short Version

  1. Install vlc (version should be >= 1.1.1):
    apt-get install vlc
  2. Install phonon-backend-vlc:
    apt-get install phonon-backend-vlc
  3. Change backend selection in Phonon: Go to System Settings → Multimedia → Backend
    Choose VLC and click Prefer. Your screen should look like:
    Choosing Phonon backend

Long Version

KDE uses the Phonon framework to play media. Its architecture allows to switch audio backend. This small fact can change the sound quality immensely. Until now, under Linux there was official support for a single backend, namely Xine (and partly official support for GStreamer). About a week ago, the first stable version of VLC backend hit Debian testing and Ubuntu 10.10. If you don’t know VLC, it’s a very popular media player. Check out their website for details:

The backend is the piece of code that does all the playing. It gets the data to play (from file, stream etc.), runs it through codecs, apply equalizer and sends it to the audio server. No wander that the backend effects the quality of the sound. Turns out that VLC does a better job at channel mixing, gain control and more. Practically it just sounds better!

There’s only one thing that I miss at the moment is equalizer support. But even without an equalizer the sound quality is terrific.
A note about stability: After using this backend since April, I can say it is fast and stable, at occasions even better than Xine. Nevertheless remember this is still Beta software, still under development.


Filed under Uncategorized