Friday, November 20, 2009

Reinventing a Wheel: Autostarting Applications

Once reinvented, a wheel is bound to be reinvented again.

I've already described how I use the ~/.xprofile script to launch applications when X starts. One issue with this method is that the applications are launched before the Window Manager is launched, which can and does cause all sorts of weird problems. Some applications need to be started from within the Window Manager.

GNOME, and, persumably, other desktop managers, conform with the Desktop Application Autostart Specification. This means, among other things, that you can autostart applications by placing a file with a .desktop extension under ~/.config/autostart for each of these applications. The contents of the .desktop file has to conform with the Desktop Entry Specification.

All that technical Mumbo Jumbo is usually well hidden under the hood. A normal GNOME user is expected to configure auto-startup applications from the "Startup Applications" control center task. It's easy, clean and sane.

But I use an alternative Window Manager - awesome. It lets me autostart applications in a myriad of ways from the ~/.config/awesome/rc.lua configuration script. None of these conforms with the freedesktop standards, which means that whenever I install/remove a package with an associated autostart application (e.g. tray icon), I need to manually update my startup script.

Well, not anymore. Here's how I handle autostart applications in my rc.lua:
os.execute('grep -ie \'^exec=\' '..
'/etc/xdg/autostart/*.desktop '..
'$HOME/.config/autostart/*.desktop '..
'$HOME/.config/awesome/autostart/*.desktop '..
'| sed -e \'s@.*autostart/@@g\' -e\'s@Exec=@@g\' '..
'| awk -F: \'{e[$1]=$2}END{for(d in e) system(e[d]"&")}\'')
This hack autostarts the applications specified by the .desktop files in ~/.config/autostart and /etc/xdg/autostart, with the former taking precedence over the latter.

Being a hack, it only considers the Exec key in the .desktop file. It does not take into account the working directory set in the Path key and/or whether to start the application in a terminal (as specified by the Terminal key). There are other deficiencies too, but these are the ones that I might be inclined to address if the need arises.

As you may have noted from the code, I added a new autostart directory under ~/.config/awesome, which has the highest priority. It houses .desktop files that correspond to autostart applications, that I only want to start under awesome. I also use it to disable some of the GNOME autostart applications by copying their corresponding .desktop file to this directory, and replacing the command specified by the Exec key within the file, with the command /bin/true.

No comments:

Post a Comment