megapico.co.uk

Home

Rotating background images in IceWM using a simple PHP script

I couldn't find a simple way to rotate the background image in the Ice Window Manager, so I wrote one.

Instructions

  1. The overall aim is to link different images each time to the filename IceWM uses as its background image. IceWM will never know that the image is different each time.

  2. Tell IceWM that you are going to use a background image. Edit /home/me/.icewm/preferences to include

    DesktopBackgroundImage="/home/me/.icewm/background.png"
    

    where 'me' is your home directory. Make sure that this file does NOT exist.

  3. Store the photos you want to use somewhere such as /home/me/photos/ and insert this directory name when configuring the PHP file below.

  4. Create a PHP file containing the code below, replacing 'me' with your home directory. This file will do the actual image rotation each time you start X. Store it wherever you like, but somewhere you can edit it easily is good, so you can change the photos it will show. The file contains most of these notes as comments for later reference. Make sure the file is executable.

    
    #!/usr/local/bin/php
    <?php
    
    //Rotate the wallpaper displayed by Ice Window Manager through the icewmbg program
    //(c) David Fletcher 2005.
    //See www.megapico.co.uk
    //Contact "linuxdocs at megapico dot co dot uk"
    
    //Run from /home/me/.xinitrc before the icewmbg is started.
    
    //idewmbg displays a background image specified in the /home/me/.icewm/preferences file
    //with the parameter DesktopBackgroundImage="/home/me/.icewm/background.png"
    
    //To rotate the background simply make this file a link to any image before starting the window manager
    
    //Configure the single directory in which the images to be rotated will be stored. Must end in /
    $location = '/home/me/photos/';
    
    //Open the directory and form an array of all the pictures (*.jpg, *.png)
    
    $dir=opendir($location); 
    
    $imgarray = array();
    
    //Build array of possible images
    while($imgfile=readdir($dir)){
      $path_parts = pathinfo($imgfile);
      if ($imgfile != "." && $imgfile!=".." && ($path_parts['extension'] == "jpg" || $path_parts['extension'] == "png")){
        $imgarray[]=$imgfile;
      }
    } 
    closedir($dir);
    
    //Select one at random
    $rand = rand(0,count($imgarray)-1);
    
    //echo $location.$imgarray[$rand];
    
    //Remove any existing symlink
    unlink('/home/me/.icewm/background.png');
    
    //Link the selected file to /home/me/.icewm/background.png. Sometimes JPG gets linked to PNG name
    //but it doesn't seem to matter
    symlink($location.$imgarray[$rand], '/home/me/.icewm/background.png');
    
    ?>
    
  5. Edit the /home/me/.xinitrc file to run the image rotation script before starting the IceWM background program. The .xinitrc will end up containing something like:

    # Start IceWM components :
    # - icewmbg   : handle desktop backgrounds
    # - icewmtray : handle notification icons (gaim, psi, ...)
    # - icewm     : the IceWM window manager
    
    #Rotate the background wallpaper
    /home/me/rotate_wallpaper.php
    
    /usr/bin/icewmbg &
    /usr/bin/icewmtray &
    exec /usr/bin/icewm
    
  6. Close X and restart it - you should get a randomly different background image each time, from those stored in /home/me/photos/