Gnome and Random Desktop Wallpapers

People say that the only preference that most users ever change is their desktop wallpaper. Your typical Linux distribution comes with a large selection of attractive wallpapers to choose from, not to mention some that are just plain bizarre. It seems a shame that all but one of these wallpapers should go to waste, so why not pick one at random?

The old fashioned way

Unix people seem to like the word root. Just like the directory / is called the root directory, and user number 0 is called the root user, the background of a screen under the X Window System is called the root window.

X11 comes with a program called xsetroot that manipulates the root window. Unfortunately, it only knows how to work with the black and white bitmaps of the kind you'll find lurking in /usr/X11R6/include/X11/bitmaps/. This might have been fine in the days when most screens were monochrome and memory was at a premium, but nowadays people expect something a bit flashier.

A range of third-party programs, including xsri, xloadimage and xv, can all place colourful images in the root window. The following example uses the xsri program supplied by Redhat:

xsri --tile=/usr/share/backgrounds/tiles/Won-Ton-Soup-3.jpg

You can now use your favourite programming language to select a random image. Here's a Perl one-liner that picks a file from the directory /usr/share/backgrounds/tiles/:

perl -e '@images = glob "/usr/share/backgrounds/tiles/*"; print $images[int rand $#images+1], "\n"'

Putting it all together, we have:

xsri --tile=`perl -e '@images = glob "/usr/share/backgrounds/tiles/*"; print $images[int rand $#images+1], "\n"'`

I've tried that, but...

The above example takes a random image and places it in the root window. There's a catch. Gnome's file manager, Nautilus, creates it's own large, borderless window and places it over the root window. It uses this window to hold the icons that you see on your desktop. The result of this is that Gnome users never actually see the root window.

So, if we want to change the wallpapers for Gnome users, we're going to have to persuade Nautilus to draw the wallpaper in it's own background window

GConf to the rescue

At about the same time as Gnome introduced the Nautilus file manager, they also introduced the GConf configuration system. This replaced a directory ~/.gnome/ full of plain-text files with a directory ~/.gconf/ full of XML, and a daemon, gconfd-2, to read and write these XML files.

At first glance, it looks as if this might make it more awkward for the hacker to go in and fiddle with the configuration of Gnome programs. Not so. It has some useful features that will actually help us:

From gconf-editor you can find all the usual settings available in Gnome programs, plus extra settings that developers have hidden from their preference dialogues so as not to clutter their interfaces. We can use it to poke around and see exactly what's available to be changed.

As we can see, settings are organised in a tree, similar to files on a file system. The wallpaper settings can be found in /desktop/gnome/background. The entries of interest are /desktop/gnome/background/picture_filename and /desktop/gnome/background/picture_options. As the name suggests, the former is simply the filename of an image to place on the desktop. The latter sets how the image should be drawn; the possible values are wallpaper, which tiles the image across the screen, stretched, which enlarges the image fill the screen, scaled, which resizes the image whilst preserving it's aspect ratio, and centred. Here's how you might set these on the command-line:

gconftool-2 --type string --set /desktop/gnome/background/picture_filename /usr/share/backgrounds/tiles/Won-Ton-Soup-3.jpg --set /desktop/gnome/background/picture_options wallpaper

Finishing off

We now know everything we need to select a random wallpaper under Gnome. Create a file called, say, ~/random-wallpaper.sh that contains the following:

#!/bin/sh

gconftool-2 --type string --set /desktop/gnome/background/picture_filename `perl -e '@images = glob "/usr/share/backgrounds/tiles/*"; print $images[int rand $#images+1], "\n"'` --set /desktop/gnome/background/picture_options wallpaper

Make this script executable with chmod a+rx ~/random-wallpaper.sh. Now, go into the Gnome Sessions preferences, and add ./random-wallpaper.sh to the list of startup programs. You'll get a different wallpaper each time you log in.

Perhaps more interestingly, we also know how to change the settings of most Gnome programs on the fly. As well as being useful for support teams in a corporate environment, this could form the basis of many a desktop hack.

Links

Last updated: 2005-02-14
Back to home