Friday, September 4, 2009

animate your desktop in linux

I've always wanted to have a slideshow of pictures for my desktop instead of just having 1 static image. I now know of 2 ways to do this. One I scripted myself, and another is program I found and learned to utilize which works better and is better.

First I'll detail my script, but be sure to continue to read further down as a better solution exists!

I initially created a script for GNOME that I was able to point to a directory full of images and have it randomly display a picture from the directory of images as my background. I then created a cronjob to execute the script so about every couple hours I would have a new backgroup.

NOTE: This will only work in GNOME 2.x as the script utilizes gconftool-2. I'm sure it can be modified to work with other versions of GNOME as well.
  • Create the script!
    • vi chgBackground.sh
    • #!/bin/sh

      PID=$$

      PHOTO_DIR=$1

      if [[ -z ${PHOTO_DIR} ]]; then
      echo "You must specify a directory that has your photos."
      exit 1
      fi

      if [[ ! -d ${PHOTO_DIR} ]]; then
      echo "Directory '${PHOTO_DIR}' does not exist."
      exit 1
      fi

      # Look for files with extension .jpg or .png
      PIC_COUNT="`find ${PHOTO_DIR} -iregex ".*\.\(jpg\|png\)$" | wc -l`"

      if [[ ${PIC_COUNT} -eq 0 ]]; then
      echo "No pictures found!"
      exit 1
      fi

      find ${PHOTO_DIR} -iregex ".*\.\(jpg\|png\)$" > /tmp/chgBackground.${PID}

      RANDOM=${PID}
      RAN_VAR=`echo $[(${RANDOM} % ${PIC_COUNT}) + 1]`

      BKG_PATH=`head -${RAN_VAR} /tmp/chgBackground.${PID} | tail -1`

      /opt/gnome/bin/gconftool-2 -t string -s /desktop/gnome/background/picture_filename ${BKG_PATH}

      rm -f /tmp/chgBackground.${PID}
  • Set perms to execute the script
    • chmod +x chgBackground.sh
  • Run the script!
    • chgBackground.sh /path/to/pics
  • Schedule in crontab to run automatically
    • crontab -e
    • Add the following to change every hour:
      • 0 * * * * sh -x /path/to/script/chgBackground.sh > /dev/null
I later learned of a program called xwinwrap. This binary allows you to run a program over your desktop. That is to say you could use this program to have VLC playing a video and make that your background. Below are the steps I used to have gslideshow from xscreensaver run as my background, with glslideshow configured to point to a directory full of images.
Now, the command I use is:
  • /path/to/xwinwrap -fs -st -sp -b -ov -- /usr/lib64/xscreensaver/glslideshow -window-id WID -duration 8 -pan 8 -fade 2 &
What the above command does is:
  • -fs : parameter for xwinwrap to run fullscreen
  • -st and -sp : I set these so that when xwinwrap runs it does not show up with a program window in the "Workspace Switcher" applet
  • -b : This has xwinwrap run below GNOME panels
  • -ov : This allows xwinwrap to run in all virtual desktops
  • -- /usr/lib64/xscreensaver/glslideshow -window-id WID -duration 8 -pan 8 -fade 2 : This is an argument passwed to xwinwrap of the command you pass to xwinwrap to be run over your desktop
    • -window-id WID : parameter for glslideshow to run fullscreen in your GNOME window
    • -duration 8 : parameter for glslideshow on how long to have a single photo displayed
    • -pan 8 : how long to pan the image for (if less then duration it will pan the image over 2 cycles for same image)
    • -fade 2 : parameter for glslideshow on how to to fade between images
  • & : allows xwinwrap to run in the background
To set the imageDirectory for glslideshow to use:
  • vi ~/.xscreensaver
    • edit/add the line containing "imageDirectory" to point to the directory of images
      • EXAMPLE:
        imageDirectory: /path/to/images
You can obviously change the argument you pass to xwinwrap to run different image/video applications to have overlayed over your desktop.