Openbox - Using feh to Manage you Wallpaper

by Darrel Johnston (djohnston)

Feh is an imlib2 based image viewer. It can also be used to manage your desktop wallpaper. It’s not used for that purpose very often because most desktop environments have their own wallpaper managers. Two exceptions are Fluxbox and Openbox.

In the PCLinuxOS community remaster of Openbox, Melodie has chosen to use the PCManFM file manager to manage the wallpaper. However, if you look in the ~/.config/openbox directory, you will see the file which is run every time you log into an Openbox session. In the file, she has a couple of commented lines for using feh to display a wallpaper. Let’s take a look at that section of the file.

# a random desktop background. There can be one only, it
# works with the same command line. I insist : uncomment ONE LINE ONLY !
# tip : take the second one if you have several wallpapers and want a different
# one at each session.
# feh --bg-scale "$(find ~/.local/wallpapers -type f)" &
# feh --bg-scale "$(find ~/.local/wallpapers -type f |sort -R |tail -1)" &

Let’s examine what each of the two options does. The first example is the simpler one. She uses the find command to find a file in (~ = your home directory) the ~/.local/wallpapers directory. The resulting string value is read by feh. The --bg-scale parameter tells feh to scale the image to the desktop size. Possible parameters are:

Center the file on the background.  If it is too small, it will be surrounded by a black border

Like --bg-scale, but preserves aspect ratio by zooming the image until it fits.  Either a horizontal or a vertical part of the image will be cut off

Like --bg-fill, but scale the image to the maximum size that fits the screen with black borders on one side.

Fit the file into the background without repeating it, cutting off stuff or using borders.  But the aspect ratio is not preserved either

Tile (repeat) the image in case it is too small for the screen

Note that if you have more than one file in the ~/.local/wallpapers directory, you will get an error: “feh ERROR: Couldn’t load image in order to set bg”.

The second option also uses the find command to find a file in the ~/.local/wallpapers directory. But this option is for more than one wallpaper image. The list of files is passed to the sort command, and the -R parameter sorts the list in a random order. The randomly sorted list is then passed to the tail command, which finds the last file in the sorted list. The string value of the resulting file is read by feh, which scales the image to the desktop size. Using this option will give you a randomly selected wallpaper every time you login to an Openbox session.

Suppose you want to cycle the wallpaper every few minutes or hours. To rotate the wallpaper randomly, create a script with the code below (for example, Make the script executable (chmod +x and call it from ~/.xsession. You can also put the source directly in ~/.xsession instead of in a separate file. Change the "15m" delay as you please (see man sleep for options).

while true; do
            feh --bg-scale "$(find ~/.local/wallpapers -type f |sort -R |tail -1)" &
            sleep 15m

Another way of doing it is shown below.

while true; do
            find ~/.wallpaper -type f -name '*.jpg' -o -name '*.png' -print0 |
                    shuf -n1 -z | xargs -0 feh --bg-scale
            sleep 15m

After feh has been run for the first time, it creates the hidden file .fehbg in your home directory. If you wish to use the same wallpaper each time you login, you can add the line sh ~/.fehbg & to your ~/.config/openbox/ file.

Note that if you want to use feh to manage your wallpaper in the Openbox edition, you must comment the line /usr/bin/pcmanfm --desktop & contained in the ~/.xsession file. Most of the instructions I’ve seen say to edit ~/.xinitrc. Commenting out the pcmanfm line in the ~/.xinitrc file did nothing for me. Also note that I’m using the openbox-bonsai-2010.11 version.