Monday, July 11, 2011

Adding Startup Applications for GDM Window Managers

I ran into a bit of an annoyance today while trying to figure out how to execute custom commands when launching Xmonad, a tiling window manager for Linux. Though Xmonad's documentation promises the execution of commands in .xsession and .Xinitrc, that doesn't seem to be the case on Fedora 14 running Xmonad in a GDM session.

After a bit of tinkering around, I've finally found a solution. GDM's WM selections are defined by files in /usr/share/xsessions. On my system, it looks like this:

[zach@zach-desktop ~]$ ls /usr/share/xsessions/
gnome.desktop xmonad.desktop

The xmonad.desktop file is short and intuitive:

[Desktop Entry]
Encoding=UTF-8
Name=xmonad
Comment=Tiling window manager
Exec=xmonad-start
Terminal=False
TryExec=xmonad-start
[Window Manager]
SessionManaged=true
# vi: encoding=utf-8

You can see that the file defines a couple of environment variables and then executes xmonad-start with the Exec directive.

We could probably add our custom directives to this file, but that isn't very clean, and you probably don't want to cart around xmonad.desktop and overwrite it on new installations. Instead, let's find out where xmonad-start resides:

[zach@zach-desktop ~]$ whereis xmonad-start
xmonad-start: /usr/bin/xmonad-start

The default xmonad-start file is just a simple shell script:

#!/bin/sh
# display the manpage if there is no user configuration
if [ ! -d ~/.xmonad ]; then
xterm -e man xmonad &
fi
exec xmonad

At this point, it's as simple as adding a few new lines:

#!/bin/sh
# display the manpage if there is no user configuration
if [ ! -d ~/.xmonad ]; then
xterm -e man xmonad &
fi
if [ -e ~/.xmonadrc ]; then
. ~/.xmonadrc
fi
exec xmonad

Now you can create a file in your home directory called .xmonadrc- it will be executed every time a new Xmonad session is created.

This will work for other GDM window managers as well; just follow the same steps to find where the startup script is located.