#!/usr/bin/ruby
#
#
# ==Description
# Remote control for XMMS using the Griffin PowerMate
# rotation::  change volume
# click::  skip to next track
# >0.5s press::  pause / de-pause
#
#
# ==Revision History
#
# v1.0  - Initial Public release (Feb 4, 2002)
#
# ==TODO
#
# none known
#
# =Licence
#
# (c) Oliver M. Bolzer <oliver@fakeroot.net>, 2002 
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. Neither the name of the author nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
###########################################################################

require "PowerMate"  # PowerMate support            
require "xmms"       # XMMS support

## open PowerMate
pmate = nil
begin
  pmate = PowerMate.new
rescue RuntimeError => e
  $stderr << "Error: " + e.to_s + "\n"
  exit(1)
end

## check for running xmms
## maybe not needed but let's be on the safe side
xmms = nil
begin
  xmms = XMMS.new
rescue Errno::ENOENT, Errno::ECONNREFUSED
  $stderr << "Error: xmms not running\n"
  exit(1)
end

## register event-handlers  
pmate.on_button_press{ 
  pmate.brightness = 0
  $btn_pressed = Time.now
}

pmate.on_button_release{
  pmate.brightness = 0
  if $paused or Time.now - $btn_pressed > 0.5 then
    $paused = !$paused 
    xmms.pause
  else 
    begin
      xmms.next_track
    rescue Errno::ENOENT, Errno::ECONNREFUSED
      # don't care if XMMS not runnging, let's hope it runs one day again
    end
  end
} 


pmate.on_rotate{|event|
    volume = xmms.volume
    if event.value >0 then      # increase volume
      lvol = volume.left + 1
      rvol = volume.right + 1
    else                        # decrease volume
      lvol = volume.left - 1
      rvol = volume.right - 1
    end

    begin
      xmms.volume = XMMS::Volume.new(lvol, rvol)  # set volume
    rescue Errno::ENOENT, Errno::ECONNREFUSED
      # don't care if XMMS not runnging, let's hope it runs one day again
    end
}

## run until interrupted
begin
 Thread.new{ pmate.each_event{} }.join
rescue Interrupt
  exit(0)
end

