#!/usr/bin/perl -w
#
#    Copyright 2005, Marcus Thiesen (marcus@thiesen.org)  
#    All rights reserved.
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of either:
#
#    a) the GNU General Public License as published by the Free Software
#    Foundation; either version 1, or (at your option) any later
#       version, or
#
#    b) the "Artistic License" which comes with Perl.
#
#    On Debian GNU/Linux systems, the complete text of the GNU General
#    Public License can be found in `/usr/share/common-licenses/GPL' and
#    the Artistic Licence in `/usr/share/common-licenses/Artistic'.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
#
#########################################################################
#
# Remote control amaroK with a Sony Ericsson mobile using DCOP
# Usage: amarokremote.pl
# 
# Prereq: DCOP, Device::SerialPort, Device::Ericsson::AccessoryMenu
# Version: 0.1
# 
#########################################################################

use strict;
use warnings;

use Device::SerialPort;
use Device::Ericsson::AccessoryMenu;
use DCOP;

my $client = new DCOP;

$client->attach;

my $player = $client->createObject("amarok", "player");
my $playlist = $client->createObject("amarok", "playlist");

sub get_info {
    my $text = "";
    $text .= "Artist: " . $player->artist() . "," if $player->artist;
    $text .= "Title: " . $player->title() . "," if $player->title();
    $text .= "Track: " . $player->track(). "," if $player->track;
    $text .= "Album: " . $player->album(). "," if $player->album;
    $text .= "Rate: " . $player->bitrate(). "," if $player->bitrate;
    $text .= "Total Time: " . $player->totalTime(). "," if $player->totalTime();
    $text .= "Current Time: " . $player->currentTime() if $player->currentTime();
    return $text;
}

my $remote = Device::Ericsson::AccessoryMenu->new;

my $menu = [ 'amaroK' => [ play  => sub { $player->play },
			   info  => \&get_info,

			   pause  => sub { $player->playPause },
			   next   => sub { $player->next(); return get_info },
			   prev   => sub { $player->prev(); return get_info },
			   stop   => sub { $player->stop() },
			   
			   "remove track" => sub { $playlist->removeCurrentTrack },

			   Volume => [ up   => sub { $player->volumeUp },
				       down => sub { $player->volumeDown },
				       ],

			   Random => [ on => sub { $player->enableRandomMode(1) },
				       off => sub { $player->enableRandomMode(0) },
				       ],

			   Seek => [ '5 forward' => sub { $player->seekRelative(5) },
				     '5 backward' => sub { $player->seekRelative(-5) },
				     '10 forward' => sub { $player->seekRelative(10) },
				     '10 backward' => sub { $player->seekRelative(-10) },
				     '30 forward' => sub { $player->seekRelative(30) },
				     '30 backward' => sub { $player->seekRelative(-30) },
				],
		

			   Extra => [ "restart amaroK" => sub { 
			       $client->send("amarok", "MainApplication-Interface", "quit()");
			       sleep 5;
			       system("killall amarok");
			       system("killall amarokapp");
			       system("amarok &");
			       if (fork()) {
				   exit;
			       }
			       do $0;
			   },
				      "exit remote" => sub { exit 0; }, ]
			   
			   ],
	     ];

$remote->menu( $menu );

my $port = Device::SerialPort->new('/dev/rfcomm0')
    or die "couldn't connect to phone";

$remote->port( $port );
$remote->register_menu;

print "Ready.\n";
while (1) {
    $remote->control;
}
