#!/usr/bin/ruby

#
#  Remote.rb - Jonathan Woffenden - October 2006
#  Contact:  woff9769@uidaho.edu - user "Almahtar" on the Ubuntu Forums
#
#  Description:
#  This is, essentially, a hacked together specific-purpose script
#  that allows you to control Amarok with your apple remote.
#  It's been tested only on my 1.66 Ghtz Intel Mac Mini running
#  Ubuntu 6.10.  Drop me a line if you get it working on other
#  configurations, or if you have questions/improvements.
#
#  Instructions:
#  Pipe /dev/hiddev0 into this script and you should be good.
#  You have do it with "sudo" to get read access to /dev/hiddev0.
#

$stdin.each_byte { |theByte|

    if (theByte != 0 && theByte != 255 && theByte != 135 && theByte != 238 && theByte != 122)
        puts "I got a #{theByte}\n"
        if (theByte == 3)
            IO.popen("gnome-screensaver-command --query") { |output|
                if (output.read.include? "inactive")
                    system("gnome-screensaver-command --activate")
                else
                    system("gnome-screensaver-command --deactivate")
                end
            }
        end
        if (theByte == 12)
            system("dcop amarok player volumeDown")
        end
        if (theByte == 10)
            system("dcop amarok player volumeUp")
        end
        if (theByte == 5)
            if (!system("dcop amarok player playPause"))
                system("amarok &")
                while (!system("dcop amarok player playPause"))
                    sleep(1)
                    puts "**************waiting for Amarok*********************"
                end
            end
        end
        if (theByte == 9)
            system("dcop amarok player prev")
        end
        if (theByte == 6)
            system("dcop amarok player next")
        end
    end
}
