# Control XMMS
# needs XMMS module
# - Josh Armour
use Xmms ();
use Xmms::Remote();
use Xmms::Config();
my $remote =  Xmms::Remote->new;
my $config = Xmms::Config->new(Xmms::Config->file);
$Pid = 0;
sub xmmsusage
{
	run ("/window -h20 -w54 XMMS");
	run ("/print XMMS \n${Cyan}XMMS Usage:\n");
	run ("/print XMMS ${BBlue}==================================================\n${BRed}");
	run ("/print XMMS xmms ${BCyan}play\n");
	run ("/print XMMS ${BRed}xmms ${BCyan}stop\n");
	run ("/print XMMS ${BRed}xmms ${BCyan}pause\n");
	run ("/print XMMS ${BRed}xmms ${BCyan}next\n");
	run ("/print XMMS ${BRed}xmms ${BCyan}prev\n");
	run ("/print XMMS ${BRed}xmms ${BCyan}vol amnt [+10 to raise 10, -10 to lower 10]\n");
	run ("/print XMMS ${BRed}xmms ${BCyan}close (close this window)\n");
	run ("/print XMMS ${Cyan}Playlist Commands\n");
	run ("/print XMMS ${BBlue}==================================================\n${BRed}");
	run ("/print XMMS ${BRed}xmms ${BCyan}add file/dir \(files are separated by commas\)\n");
	run ("/print XMMS ${BRed}xmms ${BCyan}del file/dir \(files are separated by commas\)\n");
	run ("/print XMMS ${BRed}xmms ${BCyan}clear\n");
	run ("/print XMMS ${BRed}xmms ${BCyan}shuffle\n");
	run ("/print XMMS ${BRed}xmms ${BCyan}repeat\n");
	run ("/print XMMS ${BRed}xmms ${BCyan}ls \(lists MP3 dir\)\n");
	run ("/print XMMS ${BBlue}==================================================\n${White}");
}

sub do_playlist
{
        $args = $_[0];
        if($args eq "clear")
        {
                $remote->playlist_clear;
                print "Cleared the playlist\n";  
        }
        if($args =~ /add (.*)/)
        {
                @AddList = split(/,/,$1);
                print "Adding @AddList\n";
                $remote->playlist_add(\@AddList);
        }
        if($args =~ /del (.*)/)
        {
                @AddList = split(/,/,$1);
                print "Deleting @AddList\n";
                $remote->playlist_del(\@AddList);
        }
}

sub cmd_xmms
{
	if($_ eq "")
	{
		xmmsusage;
		return;
	}
	unless($remote->is_running)
	{
		exec "xmms" unless $Pid = fork;
	}
	if($_ eq "next")
	{
		$remote->playlist_next;
	}
	elsif($_ eq "prev")
	{
		$remote->playlist_prev;
	}
	elsif($_ eq "stop")
	{
		$remote->stop;
	}
	elsif($_ eq "play")
	{
		$remote->play;
	}
	elsif($_ eq "pause")
	{
		$remote->pause;
	}
	elsif($_ eq "shuffle")
	{
		$remote->toggle_shuffle;
		print "Shuffle is now "."\n";
	}
	elsif($_ eq "repeat")
	{
		$remote->toggle_repeat;
		print "Repeat is now "."\n";
	}
	elsif(/vol (.*)/)
	{
		$volume = $remote->get_volume;
		$vchange = $1;
		print "Changing the volume by $vchange\n";
		$remote->set_volume($volume+$vchange);
	}
        elsif($_ eq "clear")
        {       
                $remote->playlist_clear;
                print("Cleared the playlist\n");  
        }
        elsif(/add (.*)/)
        {
		do_playlist("add $1");
        }
        elsif(/del (.*)/)
        {
		do_playlist("del $1");
        }
	elsif($_ eq "ls")
	{
		run("/exec ls /MP3");
	}
	elsif($_ eq "close")
	{
		run("/kill XMMS");
	}
	else
	{
		xmmsusage;
	}
}
1;

