random compiz wallpaper

Since I have a growing number of pictures of my baby daughter, I wanted to randomly select some to use as desktop wallpaper. I put a bunch of pictures in my ‘wallpaper’ directory and wrote this script to deal with them.

#!/usr/bin/env ruby
 
require 'pathname'
require 'dbus'
 
$wallpaper_dir = Pathname '/home/edward/wallpapers'
$screensize = `xrandr | grep '*' | tr -s ' ' | cut -f 2 -d' '`.chomp!
$sized_wallpaper = $wallpaper_dir.join ".resized#{$screensize}"
$sized_wallpaper.mkpath unless $sized_wallpaper.exist?
 
$wallpaper_dir.entries.select do |f|
    p = f.expand_path($wallpaper_dir)
    p.file? && !$sized_wallpaper.join(p.basename).exist?
end.each do |f|
    p = f.expand_path($wallpaper_dir)
    `convert -geometry #{$screensize} #{p} #{$sized_wallpaper.join(p.basename)}`
end
 
session_bus = DBus::SessionBus.instance
 
compiz = session_bus.service("org.freedesktop.compiz")
hsize = compiz.object('/org/freedesktop/compiz/core/screen0/hsize')
hsize.introspect
ihsize = hsize["org.freedesktop.compiz"]
 
desktops = ihsize.get.first
puts desktops
pictures = $sized_wallpaper.entries.map{|f| f.expand_path($sized_wallpaper)}.select{|p| p.file? }.shuffle.map{|p| p.to_s}
puts pictures
if pictures.size > desktops
    pictures = pictures[0, desktops]
end
 
bgimg = compiz.object('/org/freedesktop/compiz/wallpaper/screen0/bg_image')
bgimg.introspect
ibgimg = bgimg["org.freedesktop.compiz"]
ibgimg.set(pictures)

Some notes:

  • the combination of how the ruby-dbus library and the compiz dbus interfaces work is a little clunky. I’ve started making a library to help cover up some of the weirdness here.
  • I couldn’t find a good way to test the screen size from code, so ended up with the line which calls =xrandr= and parses its output. Not very maintainable.
  • I was going to set up the script to run from cron every hour or so but it causes a noticeable pause on the desktop when Compiz actually swaps the pictures so I just run it occasionally.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*