#!/usr/bin/perl -X
$conf="$ENV{'HOME'}/.mplay/config";
$lastPlayPos="$ENV{'HOME'}/.mplay/playpos";
open(PPL,"$lastPlayPos");
$mplayerOptions="-nocache -quiet -slave -playlist";

if(-f "$ENV{'HOME'}/.mplay/mplayconf")
{
	open(TESTE,"$ENV{'HOME'}/.mplay/mplayconf");
	$found=0;
	while(<TESTE>)
	{
		if($_ =~ /^extensions\=(.*)/){$teste=$1;$found=1;last;}
	}
	$teste="mpg mpeg wma avi asf vob bin fli viv ram yuv mov wmv mp3 ogg ogm mp4 wav" unless $found;
}
else
{
	$teste="mpg mpeg wma avi asf vob bin fli viv ram yuv mov wmv mp3 ogg ogm mp4 wav";
}
@tmp=<PPL>;
$plnr=$tmp[2];chomp $plnr;close PPL;
if($plnr eq '')
{
	$plnr="00";
}
$playlist="$ENV{'HOME'}/.mplay/playlist$plnr";
#print $playlist;exit;
$pid="$ENV{'HOME'}/.mplay/pid";
$remote="$ENV{'HOME'}/.mplay/.remote";
$version='mplay-0.71';
use Term::ReadKey;
if(-f $pid)
{
	open(PID,$pid);
	$tmp=<PID>;
	close PID;
	$in=kill 0,$tmp;
	unless($in)
	{
		unlink "$pid";
		exec($0);
		exit;
	}
	if($ARGV[0] eq '')
	{
		if($in)
		{
			print "mplay: \e[1;34\mmplay\e[0;32\m is already running.\e[0;0m\n";
		}
		exit;

	}
	if($ARGV[0] eq '-h' || $ARGV[0] eq '--help' || $ARGV[0] eq '-help')
	{
		printHelp();exit;
	}
	elsif($ARGV[0] eq '-v' || $ARGV[0] eq '--version' || $ARGV[0] eq '-version' || $ARGV[0] eq '-V')
	{
		print "\e[32\m$version\e[0m\n";exit;
	}
	elsif($ARGV[0] eq '-q' || $ARGV[0] eq '--quit' || $ARGV[0] eq '-quit')
	{
		kill "TERM",$tmp;
#		open(R,">$remote");
#		print R 'q';
#		close R;
		print "\e[32\mmplay closed\e[0m\n";exit;
	}
	elsif($ARGV[0] eq '-n' || $ARGV[0] eq '--next')
	{
		open(R,">$remote");
		print R 'n';
		close R;
		print "\e[32\mnext track\e[0m\n";exit;
	}
	elsif($ARGV[0] eq '-b' || $ARGV[0] eq '--back')
	{
		open(R,">$remote");
		print R 'b';
		close R;
		print "\e[32\mprevious track\e[0m\n";exit;
	}
	elsif($ARGV[0] =~ /^-[0-9]+/||$ARGV[0] =~ /^\+[0-9]+/)
	{
		open(R,">$remote");
		print R $ARGV[0];
		close R;
		print "\e[32\mseek $ARGV[0] seconds\e[0m\n";exit;
	}
	
	@tmp=@ARGV;
	@tmp1=();
	if($ARGV[0] eq '-e')
	{
		print "\e[1;34\mmplay: \e[0;32\mfiles added to playlist\e[0;0m\n";
		shift @tmp;
		open(PL,">>$playlist");
	}
	else
	{
		#unlink $playlist;

		print "\e[1;34\mmplay: \e[0;32\mplaylist replaced\e[0;0m\n";
		open(PL,">$playlist");
	}
	open(PID,$pid);
	$tmp=<PID>;
	close PID;
	$max=scalar(@tmp);
	for($in=0;$in<$max;$in++)
	{
		if($tmp[$in] =~ /^\=/)
		{
			@files=split(/=/,$tmp[$in]);
			open(M,"$files[1]");
			@filesa=<M>;
			$tmp3=scalar(@filesa);
			for($i=0;$i<$tmp3;$i++)
			{
				unless($filesa[$i] =~ /^\#/)
				{
					$filesa[$i] =~ s/\r//g;
					chomp $filesa[$i];
					push(@tmp1, "$filesa[$i]\n");
				}
			}
		}
		else
		{
			if($tmp[$in]=~ /^http\:\/\//)
			{
				push(@tmp1, "$tmp[$in]\n");
			}
			elsif($tmp[$in]=~ /\.m3u$/)
			{
				open(M,"$tmp[$in]");
				@filesa=<M>;
				$tmp3=scalar(@filesa);
				for($i=0;$i<$tmp3;$i++)
				{
					unless($filesa[$i] =~ /^\#/)
					{
						$filesa[$i] =~ s/\r//g;
						chomp $filesa[$i];
						push(@tmp1, "$filesa[$i]\n");
					}
				}
			}
			elsif($tmp[$in]=~ /\.pls$/)
			{
				open(M,"$tmp[$in]");
				@filesa=<M>;
				$tmp3=scalar(@filesa);
				for($i=0;$i<$tmp3;$i++)
				{
					if($filesa[$i] =~ /^File[0-9]+\=(.*)/)
					{
						$tmp=$1;
						$tmp =~ s/\r//g;
						chomp $tmp;
						push(@tmp1, "$tmp\n");
					}
				}
			}
			else
			{
				find( sub {if (-f $_ ){$abs=abs_path("$_") ;push(@tmp1, "$abs\n");}}, "$tmp[$in]" );
			}
		}
	}
	filterOut();
	close PL;
	open(RPL,"$playlist");
	@tmp=<RPL>;
	@filesa=();
	@filesc=();
	close RPL;
	if($tmp[0] =~ /^http\:\/\// && scalar(@tmp)>1)
	{
		$in=0;$ii=0;
		for($i=0;$i<scalar(@tmp);$i++)
		{
			if($tmp[$i] =~ /^http\:\/\//)
			{
				$filesc[$in]=$tmp[$i];
				$in++;
			}
			else
			{
				$filesa[$ii]=$tmp[$i];
				$ii++;
			}
		}
		open(PL,">$playlist");
		print PL @filesa;
		print PL @filesc;
		close PL;
	}
	if($ARGV[0] eq '-e')
	{
		$in=kill 0,$tmp;
		if($in)
		{
			open(R,">$remote");
			print R 'a';
			close R;
		}
		else
		{
			unlink "$pid";
			exec($0);
		}
	}
	else
	{
		$in=kill 0,$tmp;
		if($in)
		{
			#exit;
			open(R,">$remote");
			print R 'r';
			close R;
		}
		else
		{
			unlink "$pid";
			exec($0);
		}
	}
	ReadMode 1;exit;
}
if($ARGV[0] eq '')
{
	open(RPL,"$playlist");
	open(PPL,"$lastPlayPos");
	@files = <RPL>;
	$max=scalar(@files);
	$goBack=1;
}
else
{
	if($ARGV[0] eq '-h' || $ARGV[0] eq '--help' || $ARGV[0] eq '-help')
	{
		printHelp();exit;
	}
	elsif($ARGV[0] eq '-v' || $ARGV[0] eq '--version' || $ARGV[0] eq '-version' || $ARGV[0] eq '-V')
	{
		print "\e[32\m$version\e[0m\n";exit;
	}
	elsif($ARGV[0] =~ /^\-[^-]/)
	{
		print "\e[32\mmplay isn't running\e[0m\n";exit;
	}
	open(PL,">$playlist");
	$max=scalar(@ARGV);
	@tmp1=();
	for($i=0;$i<$max;$i++)
	{
		if ($ARGV[$i] =~ /^\=/)
		{
			@tmp=split( /=/,$ARGV[$i]);
			$files[$i]=$tmp[1];
			print plainfiles($files[$i])."\n";
			open(TMP,"$files[$i]") or die $!;
			@tmp=<TMP>;
			$max=@tmp;
			for($in=0;$in<$max;$in++)
			{
				unless($tmp[$in] =~ /^\#/&&$tmp[$in] =~ /^$/)
				{
					$tmp[$in] =~ s/\r/\n/g;
					chomp $tmp[$in]; 
					push(@tmp1, "$tmp[$in]\n");
				}
			}
			
		}
		elsif($ARGV[$i]=~ /^http\:\/\//)
		{
			push(@tmp1, "$ARGV[$i]\n");
		}
		elsif($ARGV[$i]=~ /\.m3u$/)
		{
			open(M,"$ARGV[$i]");
			@filesa=<M>;
			$tmp3=scalar(@filesa);
			for($i=0;$i<$tmp3;$i++)
			{
				unless($filesa[$i] =~ /^\#/)
				{
					$filesa[$i] =~ s/\r/\n/g;
					chomp $filesa[$i];
					push(@tmp1, "$filesa[$i]\n");
				}
			}
		}
		elsif($ARGV[$i]=~ /pls$/)
		{
			open(M,"$ARGV[$i]");
			@filesa=<M>;
			$tmp3=scalar(@filesa);
			for($i=0;$i<$tmp3;$i++)
			{
				if($filesa[$i] =~ /^File[0-9]+\=(.*)/)
				{
					$tmp=$1;
					$tmp =~ s/\r//g;
					chomp $tmp;
					push(@tmp1, "$tmp\n");
				}
			}
		}
		elsif($ARGV[$i] eq '--mopts')
		{
			$i++;
			$mplayerOptions=$ARGV[$i]." ".$mplayerOptions;
			#$mplayerOptions="-fs -nocache -quiet -slave -playlist";
		}

		else
		{
			find( sub {if (-f $_ ){$abs=abs_path("$_") ;push(@tmp1, "$abs\n") if $abs !~ /^$/}}, "$ARGV[$i]" );
#			$abs="";
		}
		
	}
	filterOut();
	close PL;
}
close PL;
open(PID,">$pid");
print PID $$;
close PID;
open(RPL,"$playlist");
@files = <RPL>;
$max=scalar(@files);
use IO::Handle;
use IO::Socket;
use IPC::Open3 'open3';
use Time::HiRes qw(gettimeofday);
use Time::HiRes qw(sleep);
use List::Util 'shuffle';
use Audio::Mixer;
use MP3::Info;
use Ogg::Vorbis::Header::PurePerl;
use File::Find;
use Cwd qw(abs_path);
use POSIX;
$sigset = POSIX::SigSet->new();
$action = POSIX::SigAction->new('child_handler', $sigset, &POSIX::SA_NODEFER);
POSIX::sigaction(&POSIX::SIGPIPE, $action);
#$SIG{PIPE} = \&child_handler;
$SIG{TERM} = \&parent_handler;
$SIG{HUP} = \&parent_handler;
#$SIG{CHLD} = \&child_handler;
$SIG{CHLD} = 'IGNORE';
$|=1;
if($ENV{'TERM'} eq "linux")
{
	$xterm=0;
}
else
{
	$xterm=1;
}

@colorChange=("progressbar","mp3 tag","info","track info","volume", "playbar","browser","playlist","clock");
@termsize=GetTerminalSize();$termsize[1]-- if $shortView;
if(-f $conf)
{
	open(C,"$conf") or die $!;
	$tmp=<C>;
	close(C);
	@tmp=split(/:/,$tmp);
	$pbarColor=$tmp[0];
	$mp3TagColor=$tmp[1];
	$infoColor=$tmp[2];
	$trackInfoColor=$tmp[3];
	$volumeColor=$tmp[4];
	$playBarColor=$tmp[5];
	$browserColor=$tmp[6];
	$playListColor=$tmp[7];
	$clockColor=$tmp[8];
	$shortView=$tmp[9];
	$nr=$tmp[10];
	$repeat=$tmp[11];
	$dirname=$tmp[12];
	if($shortView)
	{
		$cLine=2;
		$plbLine=3;
	}
	else
	{
		$cLine=10;
		$plbLine=12;
	}

}
else
{
	$pbbart=sprintf "%0.$termsize[0]\s",$pbbar;
	$pbarColor=34;
	$mp3TagColor=32;
	$infoColor=33;
	$trackInfoColor=31;
	$playBarColor=32;
	$volumeColor=31;
	$browserColor=43;
	$playListColor=34;
	$clockColor=37;
	$nr=1;
	$repeat=1;
}
$dirname='/' if $dirname eq '';
if(-d "$ENV{'HOME'}/.mplay/")
{
	;
}
else
{
	mkdir "$ENV{'HOME'}/.mplay/" or die $!;
}
print  "\e[0;0;0m\e[2J\e[$plbLine\H";
@termsize2=@termsize;
ReadMode 4;
if($repeat){$rep=''}else{$rep='|'}
$pbbar="--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";
$e="======================================================================================================================================================================================================================================================================================================================================================================================================================================";
$volbar="";
$progressbar="====";
$tmp6=$termsize[0];
$pbbart=sprintf "%0.$termsize[0]\s",$pbbar;
$dis=sprintf "%0.$tmp6\s",$pbbar;
$tmp=1;
system("setterm -cursor off");
for(@files)
{
	chomp $_;
	if(-e "$_" || $_ =~ /^http\:\/\//)
	{
		$tmp=0; 
		last;
	}
#	print "$_";
}
if($tmp)
{print "\e[31mNo existing file in your playlist!\nentering filebrowser...\e[0m";sleep 0.5;}
#sleep 2;
if($files[0] eq ''||$tmp)
{
	$i=-1;
	$save=$shortView;
	addfile();
}
&open;
sub open
{
	$mpid=open3($W, $R, $E, "mplayer $mplayerOptions $playlist");
}
$tmp=ReadLine -1, $R;
$tmp3=$files[0];
$tmp3 =~ s/[\(\)\-\[\]\<\>\{\}\.\|\^\$\*\+\?\&\\]/\\$&/g;
chomp $tmp3;
chop $tmp3;
chomp $tmp;
while(1)
{
	if($tmp=~ /$tmp3/)
	{
		
		last;
	}
	else
	{
		$tmp=ReadLine -1, $R;
		$tmp2++;
		if($tmp2>2000)
		{last;}
		sleep (0.001);
	}
}
@tmp=split(/\ /,$tmp);
if ($tmp=~ /\.$/)
{
	$chop=1;
}
$playText=$tmp[0];

$ik=0;
$playing=$plbLine;
@fpos=<PPL>;
$i=$fpos[0];
$ii=$i;
$in=$fpos[1];
$change=40;
$look=1;
$check=5;
close(PPL);
@filesa=@files;
@filesc=@files;
$timestat=gettimeofday;
if($goBack)
{
	if($ii>0)
	{
		sleep 0.5;
		syswrite $W, "pt_step $ii\n";
	}
	syswrite $W, "seek $in\n" if $filesc[$ii] =~ /mp3$/;
	$timestat=$timestat-$in;
	$check=4;
}
if($ii<$termsize[1]-$plbLine)
{
	$playing=$ii+$plbLine;
	#refresh();
	$ii-=$playing-$plbLine;
}
else
{
	#refresh();
}
if($actuallist)
{
	$actuallist=0;
}
else
{
	getFileTime();
}
if($shortView){$tmp5=$cLine}else{$tmp5=$cLine+1}
print "\e[$tmp5\H\e[0;$clockColor\m$dis";
printlist($in,$max,$ii);
$ii=$i;
print getPB();
get_time_length();
clearbuffer();
print getVolume("pcm",9).getMP3info();
resetList();
if($i>$termsize[1]-12)
{
	refresh();
}
syswrite $W, "seek -1\n" if $filesc[$ii] =~ /mp3$/;
while(1)
{
	if($test)
	{
		if(waitpid(-1,WNOHANG) > 0)
		{
			&test;
			$test=0;
			$SIG{CHLD}= 'IGNORE';
		}
	}
	if(&termSizeChanged)
	{
		resetList();
		refresh();
	}
	if($key > 96 && $key < 123)
	{;}
	elsif($key eq "")
	{
		$key='?';
	}
	elsif($key eq "Right")
	{
		syswrite $W, "seek 5\n";
		$timestat-=5;
		$check=5;
	}
	elsif($key eq "Left")
	{
		syswrite $W, "seek -5\n";
		if($timeelapsed>=5)
		{
			$timestat+=5;
			$check=5;
		}
		else
		{
			$timestat+=$timeelapsed;
		}
	}
	elsif($key eq ".")
	{
		getVolume(pcm);
		setVolume("pcm",3);
		if($shortView)
		{
			$volume = ($vol[0] + $vol[1]) / 2;
			getVolume("pcm",9);
			print "\e[1\H\e[$volumeColor;1m\e[23\G$volume%\r\e[0;0m";
			$key=readKey(0.3);
		}
		else
		{
			print "\e[1m".getVolume("pcm",9);
		}

	}
	elsif($key eq ",")
	{
		@vol = Audio::Mixer::get_cval('pcm');
		$volume = ($vol[0] + $vol[1]) / 2;
		setVolume("pcm",-3);
		if($shortView)
		{
			getVolume("pcm",9);
			print "\e[1\H\e[$volumeColor;1m\e[23\G$volume%\r\e[0;0m";
			$key=readKey(0.3);
		}
		else
		{
			print "\e[1m".getVolume("pcm",9);
		}

	}
	elsif($key eq 'k')
	{
		syswrite $W, "seek 60 \n";
		$timestat-=60;
	}
	elsif($key eq "j")
	{
		syswrite $W, "seek -60\n";
		if($timeelapsed>=60)
		{
			$timestat+=60;
		}
		else
		{
			$timestat+=$timeelapsed;
		}
	}
	elsif($key=~ /^[0-9]/)
	{
		$tmp1=$key*10;
		syswrite $W, "seek $tmp1 1\n";
		$timestat=gettimeofday;$timestat-=$gt*($key*10/100);
		$check=5;
	}
	elsif($key eq 'h'||$key eq 'k1')
	{
		$save=$shortView;
		help();
	}
	elsif($key eq "Down")
	{
		browse(1);
	}
	elsif($key eq "Up")
	{
		browse(-1);
	}
	elsif($key eq "PgDn")
	{
		for($tmp8=0;$tmp8<=$termsize[1]-$plbLine;$tmp8++)
		{browse(1);}
	}
	elsif($key eq "PgUp")
	{
		for($tmp9=0;$tmp9<=$termsize[1]-$plbLine;$tmp9++)
		{browse(-1);}
	}
	elsif($key eq "n")
	{
		fchange(1,1);
		$check=5;
		$look=1;

	}
	elsif($key eq "b")
	{
		fchange(-1,-1);
		$check=5;
		$look=1;

	}
	elsif($key eq ">")
	{
		fchange(10,10);
		$check=3;
		$look=1;

	}
	elsif($key eq "<")
	{
		$check=3;
		$look=1;
		fchange(-10,-10);
	}
	elsif($key eq "\x06")
	{
		search();
		ret() if $key eq "\n";
	}
	elsif($key eq "\n")
	{
		ret();
	}
	elsif($key eq "p")
	{
		if($shortView){$tmp5=1;}else{$tmp5=$cLine;}
		syswrite $W, "pause\n";
		$tmp6=$termsize[0]-33;
		print "\e[$tmp5\H\e[05;31m\e[$tmp6\G[pause]\r";
		readKey();
		$pausetime=gettimeofday;
		$timestat+=$pausetime-$timenow;
		print "\e[0m\e[$tmp6\G       \r";
		syswrite $W, "pause\n";
		$keyp = "";
	}
	elsif($key eq "m")
	{
		syswrite $W, "mute\n";
		if($shortView){$tmp5=1;}else{$tmp5=$cLine;}
		if($muted == 0)
		{
			print "\e[$tmp5\H\e[5;31m\e[30\G[mute]\e[0m\r";
			$muted=1;
		}
		else
		{
			print "\e[$tmp5\H\e[0m\e[30\G      \r";
			$muted=0;
		}
	}
	elsif($key eq "k2"||$key eq "\x14")
	{
		switchPlayList();
	}
	elsif($key eq "\x0E")
	{
		createNewPlayList();
	}
	elsif($key eq "\t")
	{
		$save=$shortView;
		addfile();
	}
	elsif($key eq "c")
	{
		color();
	}
	elsif($key eq "x")
	{
		mixer();
	}
	elsif($key eq "Del")
	{
		del();
	}
	elsif($key eq 's')
	{
		sort_playlist();
	}
	elsif($key eq "\x12")
	{
		$name='';
		refresh();
	}
	elsif($key eq "e")
	{
		fileInfo();
	}
	elsif($key eq "i")
	{
		if($nr){$nr=0}else{$nr=1}
		refresh(1);
	}
	elsif($key eq "f")
	{
		syswrite $W, "vo_fullscreen\n";
	}
	elsif($key eq "\x04")
	{
		del(1);
	}
	elsif($key eq "\x13")
	{
		saveList();
	}
	elsif($key eq "Pos1")
	{
		while($iib>0){browse(-1)};
#		resetList;
#		$changb=1 if ($termsize[1]-$cLine)<$ii;
#		refresh(1);
	}
	elsif($key eq " ")
	{
		editPlayList();
	}
	elsif($key eq "r")
	{
		$tmp7=int($termsize[0]/2 - 5);
		$in=$plbLine-1;
		if($repeat){$rep='|';$repeat=0;print getINF()."\e[$infoColor;1\m\e[$in\H\e[$tmp7\Grepeat-off";}
		else{$rep="";$repeat=1;print getINF()."\e[$infoColor;1\m\e[$in\H\e[$tmp7\Grepeat-on";}
		readKey(0.5);
		
	}
	elsif($key eq "z")
	{
		switchCompactView();
	}
	elsif($key eq "Alt_Del")
	{
		$actuallist=1;
		print cleanlist();
		unlink "$playlist";
		@files=();@filesa=();@filesc=();
		$i=0;$ib=0;$ik=0;$ikb=0;$ii=0;$iib=0;$playingb=$plbLine;$playing=$plbLine;$max=0;
		unless($plnr eq "00")
		{
			if($plnr eq "01")
			{
				$plnr="00";
			}
			else
			{
				$plnr--;
				$plnr=sprintf "%02d",$plnr;
			}
			$playlist="$ENV{'HOME'}/.mplay/playlist$plnr";
			open(RPL,"$playlist");
			@filesc=();
			@filesc=<RPL>;
			@files=@filesc;@filesa=@filesc;
			$max=scalar(@filesc);
			refresh();
		}
	}
	elsif($key eq 'q' || $key eq "\x03"|| $key eq "Esc")
	{
		quit();
	}
	grepOutput();
	processOutput();
	if($change>0)
	{
		$change--;
	}
	checkRemote();
	calcTime();
	$inf=getINF();
	print $inf.getVolume("pcm",9);
}
sub ret
{
		$name='';
	
		if($actuallist)
		{
			syswrite $W, "loadlist $playlist\n";
			syswrite $W, "pt_step $iib \n" if $iib > 0;
			$timestat=gettimeofday;
			$change=10;
			print "\e[$playing\H".plainfiles($files[$ii],0,$playListColor)."\e[$cLine\H";
			$i=$ib;$ik=$ikb;$ii=$iib;$playing=$playingb;
			$actuallist=0;
			clearbuffer();
		}
		elsif(!$changb)
		{
			print "\e[$playing\H".plainfiles($files[$ii],0,$playListColor,$ii+1)."\e[$cLine\H";
		}
		$changb=0;
		$tmp=$ib-$i;
		if($tmp != 0)
		{
			syswrite $W, "pt_step $tmp \n";
			$timestat=gettimeofday;
			$i=$ib;$ik=$ikb;$ii=$iib;$playing=$playingb;
			$check=5;
			$change=10;
		}
		print "\e[$playingb\H\e[1\m".plainfiles($files[$iib],$playBarColor,$browserColor,$iib+1)."\e[$cLine\H\e[0\m";
		syswrite $W, "seek -1\n" if $filesc[$ii] =~ /mp3$/;
		get_time_length();
		print getMP3info().getPB().getMenuTitle();
}
sub processOutput
{
	if($output ne '')
	{
		if($output=~ /$playText|XMMS/)
		{
			syswrite $W, "seek -1\n" if $filesc[$ii] =~ /mp3$/;
			if(!$change)
			{
				fchange(1);
				$check=5;
			}
			get_time_length();
			correct(1);
		}
		$key = "?";
	}
	else
	{
		check();
	}
}
sub calcTime
{
	$timenow=gettimeofday;
	$timeelapsed=($timenow-$timestat);
	#$timeelapsed=$gt-$timeelapsed;
	$temin=$timeelapsed/60;$temin = sprintf<%02d>,$temin;
	$timenow=gettimeofday;
	$tesec=$timeelapsed%60;$tesec = sprintf <:%02d%s>,$tesec," ";
	$timeremain=$gt-($gt/100*$gpp);$timeremain /= 60;
	$timeremain = sprintf<%3dm>,$timeremain;
	$in=$i+1;chomp $in;
}
sub checkRemote
{
	if(-f "$remote")
	{
		remote();
	}
}
sub getProgress
{
	$progressb=($termsize[0])/100*$gpp;
	$progressb=int($progressb);
	$tmp2=substr($e,0,$progressb);
	return "\e[$termsize[1]\H\e[0;$pbarColor\m$pbbart\r$tmp2\e[1\m>\r";
}
sub getINF
{
	$tmp6=$termsize[0]-5;
	if($shortView)
	{
		$_=$i+1;
		$tmp4=$tmp6-20;
		$tmp3=$tmp6-3;
		$tmp1=int($termsize[0]/2);
		$tmp5=$termsize[1]+1;
		$tmp=$tmp4-6;
		$tmp8=$tmp6-7;
		if($plnr eq '00'){$y='';}else{$y="in $plnr";}
		return getProgress()."\e[1\H\e[$tmp\G\e[0;1;$infoColor\m$temin$tesec\e[$tmp4\G -$gttmp \e[$tmp8\G$rep\e[$tmp3\G$totalTime\e[1\H\e[$tmp1\G\e[0;$clockColor\m\e[0;$trackInfoColor\m\r Track $_/$max $y \e[2\H\e[0;$clockColor\m$dis\e[$cLine\H\e[0;0m\r";
	}
	else
	{
		$tmp6-=3;
		$progressb=($termsize[0])/100*$gpp; 
		$progressb=int($progressb);
		$_=$i+1;
		$tmp2=substr($e,0,$progressb);
		if($gpp > 0){$tmp5="$gpp%"}else{$tmp5="   "}
		return clean($termsize[1]).getProgress()."\e[0;$infoColor\m\e[6\H pos.   elapsed   remain   length     total    \e[1m$rep\n\r $tmp5  \e[10\G$temin$tesec\e[19\G$timeremain\e[27\G$gttmp\e[38\G$totalTime\n\e[0;$trackInfoColor\m\r Track $_ of $max in Playlist $plnr \e[0;$clockColor\m\e[11\H$dis\e[6\H\e[$tmp6\G   $time\e[$cLine\H\e[0;0m\r";
	}
}
sub check
{
	$key=readKey("0.25");
	if($check>4)
	{
		$check=0;
		get_percent_pos();
		($_,$tmp2,$tmp3)=localtime(time);
		$time=sprintf "%.2d:%.2d",$tmp3,$tmp2;
	}
	else
	{
		$check++;
	}
}
sub getPB
{
	if($ii == $iib)
	{
		return "\r\e[$playing\H\e[1m".plainfiles($files[$ii],$playBarColor,$browserColor,$ii+1)."\e[0;0;0m\e[$cLine\H" if !$changb;
	}
	else
	{
		return "\r\e[$playing\H\e[0;0;0m".plainfiles($files[$ii],$playBarColor,1,$ii+1)."\e[0;0;0m\e[$cLine\H" if !$changb;
	}
}
sub getMenuTitle
{
	if($xterm)
	{
			
			if($files[$ii] =~ /^http\:\/\//)
			{
				$tmp=$files[$ii];
				$tmp =~ s/[\x00-\x19]//g;
				$tmp =~ s/\\//g;
				$tmp1='';
			}
			else
			{
				split(/\//,$files[$ii]);
				$tmp=substr($_[scalar(@_)-1],0,length($_[scalar(@_)-1])-5);
				$in=$ii+1;$tmp1="$in/$max - ";
			}
			return chr(27).']0;'."$tmp1$tmp".chr(7);
	}
	else{return '';}
}
sub fchange
{
		$i+=$_[0];$ik+=$_[0];$ii+=$_[0];
		$check=0;
		$name='';
		if($actuallist)
		{
			$tmp=$_[0];
			if($_[0]<0)
			{
				$i-=$_[0];$ik-=$_[0];$ii-=$_[0];
				$tmp=$_[0]*(-1);
				$i+=$tmp;$ik+=$tmp;$ii+=$tmp;
			}
			syswrite $W, "loadlist $playlist\npt_step $ii \n";
			$change=1;
			$ii-=$tmp;
			$playing+=$tmp;
			$playing--;
			$actuallist=0;
			$ik=$ik+$ii-$i;
			$i=$ii;
		}
		elsif($i>=$max)
		{
			print "\33\[$cLine\H\33[31m end of playlist\33[0m\r";
			sleep(0.05);
			print "                                         \r";
			$i-=$_[0];$ik-=$_[0];$ii-=$_[0];
		}
		elsif($i<0)
		{
			$i=0;$ik=0;$ii=0;
		}
		else
		{
			
			if($ik>=$termsize[1]-$plbLine||$changb)
			{
				$tmp4=$termsize[1]-$plbLine;
				$tmp=int($max/$tmp4);
				$tmp2=$tmp;
				$tmp++ unless $max%$tmp4 == 0;
				$tmp1=$tmp*$tmp4;
				if($max-$ii < $tmp4)
				{
#					print "\e[$termsize[1]\H\e[1;31\mtmp: $tmp\ntmp1: $tmp1\ntmp2: $tmp2\ntmp4: $tmp4\n";quit();
					#$tmp1=($termsize[0]-$plbLine)-($max-($ii));
					$tmp1=$max-$ii;#print "\e[$termsize[1]\H\e[1;31\mHALLLo $ii\n\n";quit();
					$ik-=$tmp1;
					$playing-=$tmp1-1;
					$ii=$max-$tmp4;
					#quit();
				}
				else{$playing=$plbLine+1;$playingb=$playing;$ik=1;$ii--}
				$ib=$i;$ikb=$ik;$playingb=$playing;
				print cleanlist();
				#$tmp4=$ii-$tmp1;
				printlist($in,$max,$ii);
				
				#$ii+=$tmp1;
				$ii=$i;
				$iib=$ii;
				$ikb=$ik;
				print "\e[$playing\H\e[1\m".plainfiles($files[$i],$browserColor,$playBarColor,$ii+1)."\e[0m\e[$cLine\H";
				syswrite $W, "pt_step $_[1] \n";
				$change=4;
				$changb=0;
				$timestat=gettimeofday;
			}
			elsif($ik<0)
			{
				$ik=$termsize[1]-($plbLine+1);
				$ii-=$termsize[1]-($plbLine+1);
				$playing=$playing-($plbLine+1)+$termsize[1];
				if ($ii<0)
				{
					$ii=0;
					$playing=$plbLine+$i;
					$ik=$i;
					
				}
				resetList();refresh(1);
				syswrite $W, "pt_step $_[1] \n";
				$change=4;
				
			}
			else
			{
				print "\e[$playing\H".plainfiles($files[$ii-$_[0]],0,$playListColor,$ii-$_[0]+1)."\e[0;0m";
				$playing+=$_[0];
				if($files[$iib+$_[0]] eq $files[$ii])
				{
					$tmp6=$playing-$_[0];
					print "\e[$tmp6\H".plainfiles($files[$ii-$_[0]],$playListColor,$browserColor,$ii-$_[0]+1)."$name\e[0;0m\e[$cLine\H"."\e[$playing\H".plainfiles($files[$ii],$playBarColor,1,$ii+1)."$name\e[0;0m\e[$cLine\H\r";
				}
				elsif($iib == $ii)
				{
					#print "\e[$playing\H\e[1m".plainfiles($files[$ii],$playBarColor,$browserColor,$ii+$_[0])."$name\e[0;0m\e[$cLine\H\r";
				}
				else
				{
					print "\e[$playing\H".plainfiles($files[$ii],$playBarColor,1,$ii+1)."$name\e[0;0m\e[$cLine\H\r";
				}			
				if($_[1] ne '')
				{
					syswrite $W, "pt_step $_[1]\n";
				}
				$change=4;
				$timestat=gettimeofday;
				sleep(0.08);
			}
		}
		#sleep 0.;
		print getMP3info().getMenuTitle();
		print getPB();
}
sub browse
{
		print "\e[$plbLine;$termsize[1]r";
		$ib+=$_[0];$ikb+=$_[0];$iib+=$_[0];
		$check=0;
		$tmp6=int($termsize[0]/2)-3;
		if($ib>=$max)
		{
			$ib-=$_[0];$ikb-=$_[0];$iib-=$_[0];
		}
		elsif($ib<0)
		{
			$ib=0;$ikb=0;$iib=0;
		}
		else
		{
			if($ikb>=$termsize[1]-$plbLine)
			{
				$ikb=$termsize[1]-$plbLine-1;
				#$changb=1;
				#print cleanlist();
				$tmp7=$playingb-$_[0];
				if($ii == $iib)
				{
					$changb=0;$playing=$termsize[1];$ik=$termsize[1]-$plbLine;
					printf "\e[$plbLine\H\e[M\e[$playingb\H\e[1m".plainfiles($files[$ib],$playBarColor,$browserColor,$iib+1)."\e[$tmp7\H".plainfiles($files[$ib-$_[0]],0,$playListColor,$iib-$_[0]+1).getINF().moi();#."\e[0;0m\e[$cLine\H\r";
				}
				elsif($files[$iib-$_[0]] eq $files[$ii])
				{
					printf "\e[$plbLine\H\e[M\e[$playingb\H\e[0m".plainfiles($files[$ib],$playListColor,$browserColor,$iib+1)."\e[$tmp7\H".plainfiles($files[$ib-$_[0]],1,$playBarColor,$iib-$_[0]+1).getINF().moi();#."\e[0;0m\e[$cLine\H\r";
				}
				else
				{
					printf "\e[$plbLine\H\e[M\e[$playingb\H\e[0m".plainfiles($files[$ib],$playListColor,$browserColor,$iib+1)."\e[$tmp7\H".plainfiles($files[$ib-$_[0]],0,$playListColor,$iib-$_[0]+1).getProgress();#."\e[0;0m\e[$cLine\H\r";
				}
				if($playing>$plbLine)
				{
					$ik--;
					$playing--;
					#$changb=1;
				}
				else{$changb=1;}
				#printf ;#.plainfiles($files[$iib-$_[0]],$playListColor,$browserColor,$iib+1)."\e[$cLine\H\e[0;0m\r";
			}
			elsif($ikb<0)
			{
				$inf=getINF();
				if($tesec eq ''||$temin eq ''||$gtsec eq ''||$gtmin eq ''){$inf=getINF();}else{$inf=$tmp17;}
				$vol=getVolume("pcm",9);
				$ikb=0;
				#print cleanlist();
				$tmp7=$playingb-$_[0];
				#$changb=1;
				$playingb=$plbLine;
#				$inf=getINF();
				if($ii == $iib)
				{
					$changb=0;$playing=$plbLine-1;$ik=0;
					printf "\e[$plbLine\H\eM\e[$playingb\H\e[1m".plainfiles($files[$ib],$playBarColor,$browserColor,$iib+1)."\e[$tmp7\H".plainfiles($files[$ib-$_[0]],0,$playListColor,$iib-$_[0]+1).getINF().$vol.moi();#."\e[0;0m\e[$cLine\H\r";
				}
				elsif($files[$iib-$_[0]] eq $files[$ii])
				{
					printf "\e[$plbLine\H\eM\e[$playingb\H\e[0m".plainfiles($files[$ib],$browserColor,$playListColor,$iib+1)."\e[$tmp7\H".plainfiles($files[$ib-$_[0]],1,$playBarColor,$iib-$_[0]+1).getINF().$vol.moi();#."\e[0;0m\e[$cLine\H\r";
				}
				else
				{
					printf "\e[$plbLine\H\eM\e[$playingb\H\e[0m".plainfiles($files[$ib],$playListColor,$browserColor,$iib+1)."\e[$tmp7\H".plainfiles($files[$ib-$_[0]],0,$playListColor,$iib-$_[0]+1)."\e[1\H".getProgress();#."\e[0;0m\e[$cLine\H\r";
				}
				if($playing<$termsize[1]-1)
				{
					$ik++;
					$playing++;
					#$changb=1;
				}
				else{$changb=1;}
			}
			else
			{
				
				if($files[$iib-$_[0]] eq $files[$ii])
				{
					print "\e[$playingb\H".plainfiles($files[$iib-$_[0]],1,$playBarColor,$iib-$_[0]+1)."\e[0m\e[$cLine\H";
				}
				else
				{							
					print "\e[$playingb\H".plainfiles($files[$iib-$_[0]],0,$playListColor,$iib-$_[0]+1)."\e[$cLine\H";
				}
				$playingb+=$_[0];
				$files[$iib]=$filesc[$iib];
				$in=$iib+1;
				if($files[$iib] eq $files[$ii])
				{
					printf "\e\H\e[$playingb\H\e[1\m".plainfiles($files[$iib],$playBarColor,$browserColor,$iib+1)."\e[$cLine\H\e[0;0m\r";
				}
				else
				{
					printf "\e[$playingb\H\e[0m".plainfiles($files[$iib],$playListColor,$browserColor,$iib+1)."\e[$cLine\H\e[0;0m\r";
				}
				
			}
		}
		print "\e[r";
}
sub getMP3info
{
	$filec = $filesc[$ii];
	chomp $filec;
	if($filec =~ /^http\:\/\//)
	{
		print getINF().getVolume("pcm",9);
#		$name=1;
		if($name eq '')
		{
			@tmp=();
			@tmp=split (/http\:\/\//,$filec);
			if($tmp[1] =~ /\:/)
			{
				@tmp=split(/:/,$tmp[1]);
				$port=$tmp[1];
				$port=~ /([0-9]*)(.*)/;
				$port=$1;
				$path=$2;
				if($path =~ /(.*) \#/)
				{
					$path=$1;
				}
				if($path eq '')
				{
					$path='/';
				}
				$ip=$tmp[0];
			}
			else
			{
				$ip=$tmp[1];
				$port=80;
			}
			$socket = IO::Socket::INET->new(PeerAddr=> $ip,PeerPort=> $port,Type=> SOCK_STREAM,Proto=> 'tcp',);
			if($socket eq undef)
			{
				print "\e[$cLine\H\e[0;31m\e  Could not connect .... \e[0;0m\r";
				$key=readKey(2);
				print "\e[$cLine\H\e[2\K\e[11\H\e[2\K\e[$cLine\H\e[0;0\m";
				return;
			}
			else
			{
				print $socket "GET $path HTTP/1.0\r\n" . "Icy-MetaData:1\r\n" . "User-Agent:\r\n\r\n" or last;

				$buf='';$in=0;$tmp='';
				while(1) 
				{
						$socket->recv($tmp, 1024);
						$buf.=$tmp;
						if($buf =~ /icy\-br\:(.*)/) 
						{
							$br=sprintf "%4d",$1;
							$bitrate="kbps\e[11\G$1";
						}
						if($buf =~ /ice\-audio\-info:(.*)/) 
						{
							$bitrate="audio:\e[11\G$1";
						}
						if($buf =~ /ic.\-name\:(.*)/) 
						{
							$name=$1;
						}
						if($buf =~ /ic.\-genre\:(.*)/) 
						{
							$genre=$1;
						}
						if($buf =~ /.*StreamTitle='(.*?)';/)
						{
							$title="$1";
							$title=~/^.*?(\S.*)/;
							$title="$1";
							last;
						}
						if($buf =~ /.*TITLE\=([\x27-\x79\s]*)/i)
						{
							$title=" $1";
							#$title=~/^\s?(.*)/;
							#$title=" $1";
							last;
						}
						$key = readKey(-1);
						if($key eq "Esc")
						{
							last;
						}
						last if $in > 109;
						$in++;
						#print getINF();
				}
			}
			close($socket);
		}
		$tmp2="$ip$\:$port$path # $name";
		if($shortView)
		{
			$tmp=$termsize[0]-4;
			$tmp5=$termsize[1]+1;
			$tmp1=sprintf clean($termsize[1]+1)."\r\e[0;$mp3TagColor\m\e[$tmp5\H%0.$tmp\s\r\e[$tmp\G%s\e[$cLine\H\e[0;0m", "$title - $genre", $br;
		}
		else
		{
			$tmp=$termsize[0]-11;
			$tmp1=sprintf cleanmp3info()."\r\e[0;$mp3TagColor\m\r\e[1H Title:\e[11\G%0.$tmp\s\n\r Station: %0.$tmp\s \n\r Genre:   %0.$tmp\s \n\r %s\e[$playing\H\e[01;$playListColor;$playBarColor\m%0.$termsize[0]\s\e[$cLine\H\e[0;0m", $title, $name, $genre, $bitrate, $tmp2;
		}
		unless($filesc[$ii] =~ /#/)# || $filesc[$ii] =~ /#[\w]*$/)
		{
			$tmp=$name;
			chomp $filesc[$ii];
			$tmp =~ s/[\(\)\-\[\]\<\>\{\}\.\|\^\$\*\+\?\&\\]/\\$&/g;
			$filesc[$ii]="$filesc[$ii] # $tmp\n";
			@filesa=@filesc;@files=@filesc;
		}
		#print getINF().getPB();
	}
	
	elsif($filec =~ /\.mp.$/i && -f $filec)
	{
		$info = get_mp3info($filec);
		$tag=get_mp3tag($filec);
		$artist=$tag->{ARTIST};
		$title=$tag->{TITLE};
		$album=$tag->{ALBUM};
		$bitrate=$info->{BITRATE};
		$rate=$info->{FREQUENCY};
		$tmp1=moi();
	}
	elsif($filec =~ /\.ogg$/i && -f $filec)
	{
		$ogg = Ogg::Vorbis::Header::PurePerl->new("$filec");
		$bitrate = sprintf "%d",${$ogg->info}{bitrate_nominal}/1000;
		$rate = sprintf "%d",${$ogg->info}{rate}/1000;
        	$artist="";$album="";$title="";
		foreach $com ($ogg->comment_tags)
		{
                	if($com eq "artist")
			{
				$artist=sprintf "$_" foreach $ogg->comment($com);
			}
                	elsif($com eq "title")
			{
				$title=sprintf "$_" foreach $ogg->comment($com);
			}
                	elsif($com eq "album")
			{
				$album=sprintf "$_" foreach $ogg->comment($com);
			}
        	}
		$tmp1=moi();

	}
	else
	{
		if($shortView)
		{
			$tmp5=$termsize[1]+1;
			split(/\//,$filec);
			#$tmp2=
			$tmp1="\r\e[0;$mp3TagColor\m\e[$tmp5\H".substr($_[scalar(@_)-1],0,length($_[scalar(@_)-1])-4);
		}
		else
		{
			$tmp=$termsize[0]-9;
			$tmp1=sprintf cleanmp3info()."\r\e[0;$mp3TagColor\m\r\e[1H Title:  %0.$tmp\s\n\r Artist: %0.$tmp\s \n\r Album:  %0.$tmp\s\n\r %d kbps     %d KHz\e[$cLine\H\e[0;0m", substr($_[scalar(@_)-1],0,length($_[scalar(@_)-1])-4), $tag->{ARTIST}, $tag->{ALBUM}, $info->{BITRATE}, $info->{FREQUENCY};
		}
	}
	return $tmp1;
}
sub moi
{
		$tmp=$termsize[0]-9;
		if($shortView)
		{
			$tmp5=$termsize[1]+1;
			$tmp=$termsize[0]-9;
			return sprintf clean($termsize[1]+1)."\r\e[0;$mp3TagColor\m\r\e[$tmp5\H %0.$tmp\s \e[$tmp\G %3d kbps\e[0;0m", $artist." - ".$title." - ".$album,$bitrate;
		}
		else
		{
			return sprintf cleanmp3info()."\r\e[0;$mp3TagColor\m\r\e[1H Title:  %0.$tmp\s\n\r Artist: %0.$tmp\s \n\r Album:  %0.$tmp\s\n\r %d kbps     %d KHz\e[$cLine\H\e[0;0m", $title, $artist, $album, $bitrate, $rate;
		}

}
sub getVolume
{
	@vol = Audio::Mixer::get_cval($_[0]);
	$volume = ($vol[0] + $vol[1]) / 2;
	$volume = "" if $volume > 101;
	$volume=sprintf "%3s",$volume;
	if($shortView)
	{
		#$tmp6=$termsize[1]+1;
		#$tmp5=$tmp6-35;
		$tmp2=sprintf "\e[$volumeColor\m\e[1\H\e[23\G$volume% \e[0;0m\e[$cLine\H\r";
	}
	else
	{
		$tmp2=(28)/100*$volume;
		$tmp1=substr $volbar,0,$tmp2;
		$tmp2=sprintf "\e[$volumeColor\m\e[$_[1]\H\e[39G.\e[1\K\r $_[0]\e[8G| \e[10\G$tmp1 \e[39G| $volume % \e[0;0m\e[$cLine\H\r";
	}
	return $tmp2;
}
sub setVolume
{
	Audio::Mixer::set_cval($_[0],$volume+$_[1],$volume+$_[1]);
}
sub mixer
{
	$save=$shortView;
#	@termsize=GetTerminalSize();
	@tmp1=("vol","pcm","bass","treble");
	$toChange=0;
	$tmp7=$termsize[1]-1;
	$tmp6=$termsize[1]-4;
	$tmp5=$termsize[1]-5;
	$shortView=0;print "\e[$tmp5\H\e[40\m\e[46G\e[1K\r\e[21G\e[4mMIXER\e[0m".getVolume(vol,$tmp7-3)."\e[40m".getVolume("pcm",$tmp7-2)."\e[40m".getVolume(bass,$tmp7-1)."\e[40m".getVolume(treble,$tmp7)."\e[44m".getVolume("$tmp1[$toChange]",$toChange+$tmp7-3);$shortView=$save;
	while(1)
	{
		grepOutput();
		if($output ne '')
		{
			processOutput();
			$tmp5=$termsize[1]-5;$tmp6=$termsize[1]-4;
			$shortView=0;print "\e[$tmp5\H\e[40\m\e[46G\e[1K\r\e[21GMIXER".getVolume(vol,$tmp7-3)."\e[40m".getVolume("pcm",$tmp7-2)."\e[40m".getVolume(bass,$tmp7-1)."\e[40m".getVolume(treble,$tmp7)."\e[44m".getVolume("$tmp1[$toChange]",$toChange+$tmp7-3);$shortView=$save;
		}
		processOutput();
		#check();
		calcTime();
		print getINF().getVolume("pcm",9);
#		$tmp7=$termsize[1]-3;
		if($key eq 'x' || $key eq 'q' || $key eq "Esc")
		{
			$shortView=$save;
			print "\e[2\H\e[2\K".getINF();refresh(1);
			last;
		}
		elsif($key eq 'Down')
		{
			$toChange++;
			if($toChange>3)
			{
				$toChange=0;
			}
		$shortView=0;print "\e[40m".getVolume(vol,$tmp7-3)."\e[40m".getVolume("pcm",$tmp7-2)."\e[40m".getVolume(bass,$tmp7-1)."\e[40m".getVolume(treble,$tmp7)."\e[44m".getVolume("$tmp1[$toChange]",$toChange+$tmp7-3);$shortView=$save;
		}
		elsif($key eq 'Up')
		{
			$toChange--;
			if($toChange<0)
			{
				$toChange=3;
			}
		$shortView=0;print "\e[40m".getVolume(vol,$tmp7-3)."\e[40m".getVolume("pcm",$tmp7-2)."\e[40m".getVolume(bass,$tmp7-1)."\e[40m".getVolume(treble,$tmp7)."\e[44m".getVolume("$tmp1[$toChange]",$toChange+$tmp7-3);$shortView=$save;
		}		
		if($key eq 'Right')
		{
			$shortView=0;
			if($toChange == 0)
			{
				getVolume(vol);
				setVolume(vol,2);
				print "\e[44m".getVolume(vol,$tmp7-3)."\e[0m";
			}
			elsif($toChange == 1)
			{
				getVolume(pcm);
				setVolume("pcm",2);
				print "\e[44m".getVolume("pcm",$tmp7-2)."\e[0m";	
			}
			elsif($toChange == 2)
			{
				getVolume(bass);
				setVolume(bass,2);
				print "\e[44m".getVolume(bass,$tmp7-1)."\e[0m";
			}
			elsif($toChange == 3)
			{
				getVolume(treble);
				setVolume(treble,2);
				print "\e[44m".getVolume(treble,$tmp7)."\e[0m";
			}
			$shortView=$save;
		}
		if($key eq 'Left')
		{
			$shortView=0;
			if($toChange == 0)
			{
				getVolume(vol);
				setVolume(vol,-2);
				print "\e[44m".getVolume(vol,$tmp7-3)."\e[0m";	
			}
			elsif($toChange == 1)
			{
				getVolume(pcm);
				setVolume("pcm",-2);
				print "\e[44m".getVolume("pcm",$tmp7-2)."\e[0m";	
			}
			elsif($toChange == 2)
			{
				getVolume(bass);
				setVolume(bass,-2);
				print "\e[44m".getVolume(bass,$tmp7-1)."\e[0m";
			}
			elsif($toChange == 3)
			{
				getVolume(treble);
				setVolume(treble,-2);
				print "\e[44m".getVolume(treble,$tmp7)."\e[0m";
			}
			$shortView=$save;
		}
	}
	print getMenuTitle();
	
}
sub correct
{
	$name='';
	$tmp1=$output;
	if($tmp1 =~ /^$playText.*/)
	{
		@tmp=split(/$playText\ /,$tmp1);
		$tmp1=$tmp[1];
	}
	
	chomp $tmp1;
	if ($chop)
	{
		chop $tmp1;
	}
	$tmp2=$filesc[$ii];
	chomp $tmp2;
	if($_[0])
	{
		$tmp3=$filesc[0];
		chomp $tmp3;
	}
	else
	{$tmp3='??';}
	unless($tmp1 eq $tmp3)
	{
		if("$tmp1" ne "$tmp2")
		{
			unless($tmp1=~ /.so/)
			{
				$in=-1;
				while($in<$max-1)
				{
					$in++;
					$tmp=$files[$in];
					chomp $tmp;
					if("$tmp1" eq "$tmp")
					{
						$ii=$in;
						$in=$max;
						$docor=1;
					}
				}
				if($docor)
				{
					$docor=0;
					resetList();
					refresh(1);
				}
			}
		}
	}
	print getMenuTitle();$output='';
}
sub refresh
{
	@termsize2=@termsize;
	$tmp6=$termsize[0];
	if($shortView)
	{
		#$tmp6-=14;
		$pbbart=sprintf "%0.$tmp6\s",$pbbar;
		#$tmp6+=9;
		$dis=sprintf "%0.$tmp6\s",$pbbar;
	}
	else
	{
		$pbbart=sprintf "%0.$termsize[0]\s",$pbbar;
		$dis=sprintf "%0.$tmp6\s",$pbbar;
	}
	if($_[0] eq '1')
	{
		print cleanlist()."\e[$plbLine\H";
	}
	else
	{
		print clearscreen()."\e[$plbLine\H";
	}
	if($_[0] eq 'addFile')
	{addfile();return;}
	elsif($_[0] eq 'help')
	{help();return;}
	$iia=$iib-($playingb-$plbLine);
	printlist($in,$max,$iia);
	$ik=$playing-$plbLine;
	
#	if ($changb)
#	{
#		$ikb=$ik;$ib=$i;$iib=$ii;
#		$playingb=$playing;
#		$changb=0;
#	}
	if($ii == $iib)
	{
		print "\e[$cLine\H".getINF().getMP3info().getPB().getVolume("pcm",9).getMenuTitle()."\e[$playingb\H\e[1\m".plainfiles($files[$iib],$playBarColor,$browserColor,$iib+1);
	}
	else
	{
		print "\e[$playingb\H".plainfiles($files[$iib],$playListColor,$browserColor,$iib+1)."\e[$cLine\H".getPB().getINF().getMP3info().getVolume("pcm",9).getMenuTitle();
	}

}
sub resetList
{
	if($termsize[1]-$plbLine > 0)
	{
		$playing=($ii%($termsize[1]-$plbLine))+$plbLine;
		$ik=$playing-$plbLine;
		$i=$ii;$ib=$i;$iib=$i;$ikb=$ik;$playingb=$playing;
	}
}
sub test
{
	$tmp1=0;$tmp2=0;@tmp=();
	for($in=0;$in<$max;$in++)
	{
		unless($filesc[$in] =~ /^http\:\/\//)
		{
			if($time{$filesc[$in]} eq "")
			{
				read OUT, ($tmp),10;
				$time{$filesc[$in]}="$tmp\r";
				chomp $time{$filesc[$in]};
				chop $time{$filesc[$in]} if substr $time{$filesc[$in]},length($time{$filesc[$in]}),1 eq ' ';
			}
			$time{$filesc[$in]} =~ /[0-9]{2,3}\:[0-9]{2}/;
			@tmp=split /:/,$&;
			$tmp1+=$tmp[0];
			$tmp2+=$tmp[1];
		}
	}
	if($tmp1 == 0 && $tmp2 == 0)
	{
		$totalTime="   -";
	}
	else
	{
		$tmp3=int(($tmp1*60+$tmp2)/3600);
		$tmp4=int((($tmp1*60+$tmp2)-($tmp3*3600))/60);
		$tmp5=($tmp1*60+$tmp2)%60;
		$totalTime=sprintf "%2d:%.2d:%.2d",$tmp3,$tmp4,$tmp5;
	}
	if($shortView)
	{
		$tmp6=$termsize[0]-11;
		print "\e[1H\e[0;31\m\e[$tmp6\G             \e[0;0\m";
	}
	else
	{
		$tmp5=$cLine-3;
		$tmp6=37;
		print "\e[$tmp5\H\e[0;31\m\e[$tmp6\G              \e[0;0\m";
	}
	refresh(1);

}
sub getFileTime
{
	if($shortView)
	{
		$tmp5=1;
		$tmp6=$termsize[0]-10;
		$totalTime="\e[$tmp6\G[scanning]";
	}
	else
	{
		$tmp5=$cLine-3;
		$tmp6=37;
		$totalTime="";
	}

	$SIG{CHLD}= '';
	pipe OUT, IN;
	$tmp=fork();
	if($tmp==0)
	{
		sleep 1;
		open(PID,$pid);
		$tmp2=<PID>;
		close PID;
		if($shortView)
		{
			$tmp5=1;
			$tmp6=$termsize[0]-10;
		}
		else
		{
			$tmp5=$cLine-3;
			$tmp6=37;
		}
		if($shortView){print "\e[$tmp5\H\e[0;31\m\e[$tmp6\G[scanning]\e[0;0\m";}
		for($in=0;$in<$max;$in++)
		{
			@termsize=GetTerminalSize();$termsize[1]-- if $shortView;
			$tmp4=kill 0,$tmp2;
			if($tmp4 == 0)
			{
				print "\e[0K";exit;
			}
			else
			{
				unless($filesc[$in] =~ /^http\:\/\//)
				{
					if($time{$filesc[$in]} eq "")
					{
						$tmp=($in/$max*100);
						$tmp4=$cLine+1;
						unless($shortView){printf "\e[$tmp5\H\e[0;31\m\e[$tmp6\G[scanning %d%]\e[0;0\m",$tmp;}
						$tmp3=$filesc[$in];
						chomp $tmp3;
						if ($tmp3 =~ /mp3$/i && -f $tmp3)
						{
							$tmp = get_mp3info($tmp3);
							$tmp1 = sprintf "%s",$tmp->{TIME};
							$tmp1 .="\e[0\m";
						}
						elsif ($tmp3 =~ /ogg$/i && -f $tmp3)
						{
							#$tmp = get_mp3info($tmp3);
							$tmp = Ogg::Vorbis::Header::PurePerl->new("$tmp3");
							$tmp1 = sprintf "%02d:%02d",${$tmp->info}{length}/60,${$tmp->info}{length}%60;
							$tmp1 .="\e[0\m";
						}
						elsif ($tmp3 =~ /wav$/i && -f $tmp3)
						{
							$tmp4='';
							open(READ,$tmp3);
							seek(READ,28,0);
							read(READ, $buffer, 4);
							$length=length($buffer);
							for($tmp1=0;$tmp1<$length;$tmp1++)
							{
								$tmp7=substr($buffer,$tmp1,1);
								$tmp8=sprintf "%x",unpack(C,"$tmp7");#print "\n$a\n";
								$tmp4=$tmp8.$tmp4;
							}
							$tmp1= hex "$tmp4";
							$tmp1=sprintf "%02d:%02d",((-s $tmp3)/$tmp1)/60,((-s $tmp3)/$tmp1)%60;
							$tmp1 .="\e[0\m";
						}
						else
						{
							$tmp1="--:--\e[0\m";
						}
						while(length($tmp1)<10)
						{
							$tmp1=" ".$tmp1;
						}
						$time{$filesc[$in]}="$tmp1";
						sleep 0.02;
						print IN $time{$filesc[$in]};
					}

				}
			}
		}
		exit;
	}
	else
	{
		$test=1;
	}
}
sub plainfiles
{
	$tmp=$_[0];
#	$tmp4='';
	chomp $tmp;
	$tmp =~ s/\\//g;
	if($tmp =~ /^http\:\/\//)
	{
		
		$tmp =~ /http\:\/\/(.*)/;
		$tmp3="$1";
		$tmp5=sprintf "\e[$playListColor;$_[1];$_[2]\m\e[2K%0.$termsize[0]\s\n\r",$1;
		return $tmp5;
	}
	else
	{
		@splitfile=split (/\//,$tmp);
		$tmp3=scalar(@splitfile);
		$tmp5=$termsize[0]-7;
		$tmp=sprintf "%0.$tmp5\s",$splitfile[$tmp3-1];
	}
	if ($_[1] eq "nocolor")
	{
		$tmp6='';
		for($ib=0;$ib<scalar(@splitfile)-1;$ib++)
		{	
			$tmp6.="$splitfile[$ib]/"
		}
		if($_[0] =~ /^http\:\/\//)
		{
			return $_[0];
		}
		else
		{
			return "$tmp#split#$tmp6";
		}
	}
	$tmp4=$termsize[0]-5;
	#$in =$ii;chomp $in;
	if($nr){$tmp1=sprintf "%02d. ",$_[3];}else{$tmp1=''};
	$tmp5=sprintf "\e[$playListColor;$_[1];$_[2]\m\e[2\K$tmp1$tmp\e[$tmp4\G%6s\n\r",$time{$_[0]};
	return $tmp5;
}
sub printlist
{
	print "\e[$plbLine\H";
#	@filesa=();
#	@files=@filesc;
	if (@files == 0)
	{
		print "\e[31\mPlaylist is empty!\n\r\e[0;0m";
	}
	for($_[0]=0;$_[0]<$termsize[1]-$plbLine && $_[2]<$_[1];$_[0]++)
	{
		$tmp6=$filesc[$_[2]];
		print plainfiles($tmp6,0,$playListColor,$_[2]+1);
		$_[2]++;
	}
	print "\e[$cLine\H\e[0;0\m";

}
sub clean
{
	return "\e[$_[1]\m\e[$_[0]\H\e[2K\e[$plbLine\H";
}
sub cleanlist
{
	$tmp1='';print "\e[0;0;0m";
	for($in=$plbLine;$in<$termsize[1];$in++)
	{
		$tmp1.=sprintf clean($in);
	}
	return $tmp1;

}
sub cleanmp3info
{
	return "\e[0;0;0m\e[6\H\e[1J";
}
sub cleaninfo
{
	return "\e[0;0;0m\e[10\H\e[1J";
}
sub clearscreen
{
	return "\e[0;0;0m\e[2J";
}
sub get_percent_pos
{
	grepOutput();
	if($output=~ /$playText|XMMS/)
	{
		if(!$change)
		{
			fchange(1);
			syswrite $W, "seek -1\n" if $filesc[$ii] =~ /mp3$/;
			$check=5;
		}
		correct(1);
	}
 	if($filesc[$ii] =~ /^http\:\/\//)
	{
		$gpp='';
	}
	else
	{
		syswrite $W, "get_percent_pos\n";
		$gpp=ReadLine 0.1, $R;
		$gpp=~/([0-9]{1,2})/;
		$gpp=$1;
	}
}

sub get_time_length
{
	ReadKey 0.3;
	clearbuffer();
	syswrite $W, "get_time_length\n";
	$gttmp=ReadLine 0.1, $R;
	$gttmp=~/([0-9]{1,6})/;
	$gt=$1;
	$gttmp=sprintf "%4.2d:%02d",$1/60,$1%60;
}
sub clearbuffer {
	$line=ReadLine -1 , $R ;
	while ($line) {
		$line=ReadLine -1 , $R  ;
	}
	$R->autoflush() ;
}
sub editPlayList
{
	print getMP3info().getINF().getVolume("pcm",9);
	browse(1);
	$tmp=$playingb-1;
	print "\e[$tmp\H".plainfiles($files[$iib-1],$playListColor,41,$iib)."\e[0m\e[$cLine\H";
	while(1)
	{
#		readKey(0.25);
		grepOutput();
		processOutput();
		calcTime();
		print getINF().getVolume("pcm",9);
		if($key eq 'Esc'||$key eq 'q')
		{
			refresh(1);
			return;
		}
		elsif($key eq "Down")
		{
			browse(1);
		}
		elsif($key eq "Up")
		{
			browse(-1);
		}
	}
}
sub dirlength
{
	if($index < 1)
	{
		$index=1;
	}
	$tmp=$index;
#	if($n>$tts)
#	{
#		if($index+$tts > $n)
#		{
#			print "\e[3H";
#		}
#		else
#		{
#			print "\e[2H";
#		}
#	}
	if($tmp<$tts)
	{
		print "\e[3H";
	}
	else
	{
		print "\e[2H";
	}
#	print "\e[2H";
	$p=3;
	for($in=0;$in<$tts-3;$in++)
	{
	
		if($index<$n)
		{
			yn();
			$tmp3=$termsize[0]-2;
			$tmp1.=sprintf "\n\e[2\K %0.$tmp3\s$slash\r",$f[$index];
			$index++;
		}
	}
	$index=$tmp-1;
	print $tmp1;

}
sub down
{
	unless($index>$n-2)
	{

		if($p>$tts-1)
		{
			$tmp1='';
			$p=$tts-1;
			printf "\e[3\H\e[M\e[$tts\H\e[2K\e[$p\H\e[2K\e[0m %0.$tmp3\s$slash\e[0m\r".getProgress(),$f[$index];
			#print cleanlist()."\e[0H";
			#dirlength();
		}
		$tmp3=$termsize[0]-2;
		printf "\e[$p\H\e[2K\e[0m %0.$tmp3\s$slash\e[0m\r",$f[$index];
		yn(-1);
		$p++;
		$index++;
		$tmp3=$termsize[0]-2;
		printf "\e[$p\H\e[$browserColor;1m\e[2K %0.$tmp3\s$slash\r\e[0;0m\e[0m",$f[$index];
	}
}
sub up
{
	if($index>0)
	{
		if($p<4)
		{
			#$index-=$tts-3;
			$p=4;
			printf "\e[$plbLine\H\eM\e[2K\e[0m %0.$tmp3\s$slash\e[0m\r".getProgress(),$f[$index];
			#printf "\e[$tts\H\eM\e[$termsize[1]\H\e[0;0m\r";
			#print "\e[1H\eM$help[$tmp1-($termsize[1])]\e[$termsize[1]\H\e[0;0m\r";
			#print cleanlist()."\e[0H";
			#dirlength();
			#$index+=$tts-3;
		}
		$tmp3=$termsize[0]-2;
		printf "\e[$p\H\e[2K\e[0m %0.$tmp3\s$slash\e[0m\r",$f[$index];
		yn(1);
		$p--;
		$index--;
		$tmp3=$termsize[0]-2;
		printf "\e[$p\H\e[$browserColor;1m\e[2K %0.$tmp3\s$slash\r\e[0;0m\e[0m",$f[$index];
	}	
}
sub yn
{
	$k=$_[0];
	if(-d "$dirname$f[$index-$k]"){$slash='/'}else{$slash=''}
}
sub dir
{
#	@termsize=GetTerminalSize();
	print cleanlist()."\e[0H";
	@f=readdir(DIR);
	#@s = sort {lc $a cmp lc $b} @a;
	@f=sort {lc $a cmp lc $b} @f;
	$n=scalar(@f);
	$tmp1='';
	$index=0;$p=1;
	dirlength();
	
}
sub addfile
{
	print "\e[r";
	#@termsize=GetTerminalSize();
	unless($shortView)
	{
		print "\e[1H\e[2K\r";
		switchCompactView('silent');
	}
	$cLine=2;
	$plbLine=3;
	$shortView=1;
	print "\e[$plbLine;$termsize[1]r";
	$moi=getMP3info() if $mpid>0;
	$tts=$termsize[1]-1;
	print chr(27)."]0;Add a file/directory with 'a' or a directory recursiv with 'r' or a playlist with 'p' or an url with 'u'".chr(7) if $xterm;
	$dirname='/' if $dirname eq '';
	opendir(DIR,$dirname)or die $!;
	$p=3;
	#$index=2;
	#print "\e[2J";
	dir();
	if ($mpid>0)
	{
		print getMP3info().getINF().getVolume("pcm",9);
	}
	unless($index>$n-2)
	{
		#down();
		#$p++;
		#$index++;
		$tmp3=$termsize[0]-2;
		printf "\e[$p\H\e[$browserColor;1m\e[2K %0.$tmp3\s$slash\r\e[0;0m\e[0m",$f[$index];
	}
	while(1)
	{
		if ($mpid>0)
		{
			grepOutput();
			processOutput();
			calcTime();
		}
		else{$key=readKey(0.25);}
		#@termsize=GetTerminalSize();
		if(&termSizeChanged)
		{
			#help();
			refresh('addFile');
			return;
			
		}
		print getINF().getVolume("pcm",9);
		$tmp3=$termsize[0]+4;
		$dirname=abs_path("$dirname");
		$dirname.="/" unless $dirname eq "/";
		printf "\r\e[1K\e[2\H\e[$playBarColor\m%0.$tmp3\s","$ENV{'USER'}\@mplay:\e[34\m$dirname"."# \e[0;0;0m";
		if($key > 96 && $key < 123)
		{
			$akey=chr($key);
			#$index=0;$p=3;
			for($tmp=$index;$tmp<scalar(@f)+$index;$tmp++)
			{
				if($f[$tmp] =~ /^$akey/i)
				{
					#$tmp=$tmp%scalar(@f);
					#$tmp3=scalar(@f);
					for($tmp4=$index;$tmp4<$tmp;$tmp4++)
					{
						down();
						#sleep 0.1;
					}
				last;
				$key='';
				}
			}
		}
		elsif($key eq 'h')
		{
			help();
		}
		elsif($key eq 'Up')
		{
			up();
		}
		elsif($key eq 'PgUp')
		{
			for($x=0;$x<$tts-3;$x++)
			{
				up();
			}
		}
		elsif($key eq 'Down')
		{
			down();
		}
		elsif($key eq 'PgDn')
		{
			for($x=0;$x<$tts-3;$x++)
			{
				down();
			}
		}
		elsif($key eq 'a')
		{
			@tmp=@filesc;
			if($filesc[0] =~ /^http\:\/\//)
			{
				open(PL,">$playlist");
			}
			else
			{
				@tmp=();
				open(PL,">>$playlist");
			}
			if(-d "$dirname$f[$index]/")
			{
				opendir(DIR,"$dirname$f[$index]/")or die $!;
				@files=();
				@files=readdir (DIR);
				@files=sort @files;
				#@files=grep (/\.mp3$/i|/\.ogg$/i|/\.wav$/i,@files);
				for($in=0;$in<scalar(@files);$in++)
				{
					$tmpfile="$dirname$f[$index]/$files[$in]";
					$tmpfile =~ /(...$)/;
					$ext=$1;
					$ext=~s/[^a-zA-Z0-9]/\\$&/g;
					
					$out.=sprintf "$dirname$f[$index]/$files[$in]\n" if(-f "$dirname$f[$index]/$files[$in]") && $teste =~ /$ext/i;
				}
				print PL $out;
				$changb=1;
			}
			else
			{
				print PL "$dirname$f[$index]\n";
			}
			$tmp4=$termsize[0]-10;
			printf "\e[$p\H\e[$tmp4\G\e[$browserColor;31;1\m %s added ...\e[0\m";#,abs_path("$dirname$f[$index]");
			$out='';
			print PL @tmp;
			close PL;

			$actuallist2=1;
			open(RPL,$playlist);
			@filesa = <RPL>;
			@files=@filesa;
			@filesc=@filesa;
			close RPL;
			$max=scalar(@files);
			$iia=0;
		}
		elsif($key eq 'r')
		{
			if($filesc[0] =~ /^http\:\/\//)
			{
				@tmp=@filesc;
				open(PL,">$playlist");
			}
			else
			{	
				@tmp=();
				open(PL,">>$playlist");
			}
			$tmp4=$termsize[0]-25;
			#print PL @filesa;
			print "\e[$p\H\e[$tmp4\G\e[$browserColor;31;1\m scaning .... please wait\e[0;0\m\r";
			@tmp1=();
			find( sub {if (-f $_ ){$abs=abs_path("$_") ;push(@tmp1,"$abs\n") }}, "$dirname$f[$index]" );
			filterOut();
			close PL;
			$actuallist2=1;
			open(RPL,$playlist);
			@filesa = <RPL>;
			@files=@filesa;
			@filesc=@filesa;
			close RPL;
			$max=scalar(@files);
			$iia=0;
			#nsort();
			print "\e[$p\H\e[$tmp4\G\e[$browserColor;31;1\m added recursive ...\e[0K\e[0;0\m\r";
		}
		elsif($key eq 'p')
		{
			$actuallist2=1;
			open(PL,">>$playlist");
			open(TMP,"$dirname$f[$index]");
			@tmp=<TMP>;
			$tmp3=@tmp;
			for($in=0;$in<$tmp3;$in++)
			{
				unless($tmp[$in] =~ /^\#/)
				{
					print PL "$tmp[$in]";
				}
			}
			close PL;
			close TMP;
			open(RPL,$playlist);
			@filesa = <RPL>;
			@files=@filesa;
			@filesc=@filesa;
			close RPL;
			$max=scalar(@files);
			print "\e[$tts\H\e[2\K\e[32\m playlist $dirname$f[$index] added ...\e[0;0\m\r";
		}
		elsif($key eq 'u')
		{
			$actuallist2=1;
			open(PL,">>$playlist");
			print "\e[$tts\H\e[2\K\e[32\m enter 'url' and press <ENTER>:\e[0;31\m";
			$tmp3='';
			while($key ne "\n")
			{
				$key=readKey();
				if($key eq "Esc")
				{
					refresh();
					$actuallist2=0;
					return;
				}
				elsif($key eq "Backspace")
				{
					chop $tmp3;
				}
				else
				{
					$tmp3.=$key;
				}
				print "\e[33\G\e[0\K$tmp3";
			}
			chop $tmp3;
			if($tmp3 eq '')
			{
				refresh();
				$actuallist2=0;
				return;
			}
			else
			{
				print PL "$tmp3\n";
				close PL;
				open(RPL,$playlist);
				@filesa = <RPL>;
				@files=@filesa;
				@filesc=@filesa;
				close RPL;
				$max=scalar(@files);
				refresh();
				last;
			}
			close PL;
		}
		elsif(-f $remote)
		{
			print "\e[38;38;38\m";
			$termsize[1]-- if $shortView;
			last;
		}
		elsif($key eq "\t" || $key eq 'q' || $key eq "Esc" || -f $remote)
		{
			print "\e[0;0;0\m";
			unless($save)
			{
				switchCompactView('silent');
			}
			print "\e[r";
			refresh();
			$key='';
			last;

		}			
		if($key eq "\n")
		{
			if(-d "$dirname$f[$index]/")
			{
				closedir(DIR);
				$dirname="$dirname$f[$index]/";
				opendir(DIR,$dirname)or die $!;
				$index=0;
				dir();down();
				
				print "\e[$p\H\e[$browserColor;1\m\e[2K $f[$index]$slash\e[0m\r";
			}
		}
		elsif($key eq "Backspace")
		{
				closedir(DIR);
				$dirname="$dirname"."../";
				opendir(DIR,$dirname)or die $!;
				$index=0;
				dir();down();
				print "\e[$p\H\e[$browserColor;1\m\e[2K $f[$index]$slash\e[0m\r";
		
		}
	}
	if($actuallist2)
	{
		getFileTime();
		resetList();
		refresh();
		$actuallist2=0;
		$actuallist=1;
	}
}
sub saveList
{
		print "\e[$cLine\H\e[1;32\m Enter a filename to save playlist (e.g. /tmp/mplay.m3u): ";
		$tmp3='';
		while($key ne "\n")
		{
			$key=readKey();
			if($key eq "Esc")
			{
				$tmp3=$cLine+1;
				print "\e[$cLine\H\e[2\K\e[0;0\m\r";
				return;
			}
			elsif($key eq "Backspace")
			{
				chop $tmp3;
			}
			else
			{
				$tmp3.=$key;
			}
			print "\e[59\G\e[0\K$tmp3";
		}
		open(F,">$tmp3");
		print F "#EXTM3U\n";
		for($in=0;$in<scalar(@filesc);$in++)
		{
			print F $filesc[$in];
		}
		close F;
		print "\e[$cLine\H\e[2\K\e[11\H\e[2\K\e[$cLine\H\e[0;0\m\r";
}
sub search
{
	print "\e[$cLine\H\e[1;32\m Enter search string: \e[31m";
	$expression='';
#	$tmp=fork();
#	if (!$tmp)
#	{
		$start=$iib;
		while($key ne "Esc")
		{
			$key=readKey();
			if($key eq "Esc")
			{
				#$tmp3=$cLine+1;
				last;
			}
			elsif($key eq "Backspace")
			{
				chop $expression;
			}
			elsif($key eq "k3")
			{
				if($found){$start=$iib+1;}
				else{$start=0;}
				#last;
			}
			elsif($key eq "\n")
			{
				last;
			}
			else
			{
				$expression.=$key;
			}
			print "\e[$cLine\H\e[1;32\m Enter search string: \e[31m\e[0\K$expression";
			$expression =~ s/[\(\)\-\[\]\<\>\{\}\.\|\^\$\*\+\?\&\\]/\\$&/g;
			$found=0;
			for($c1=$start;$c1<$max;$c1++)
			{
				$files[$c1] =~ /([^\/]*)$/;
				#$found=$1;
				$ps=$c1-$iib;
				if($ps>0)
				{
					if($1 =~ /$expression/i)
					{
						$found=1;
						for($c2=$iib;$c2<($c1-$browser);$c2++){browse(1);}
						last;
					}
				}
				else
				{
					if($1 =~ /$expression/i)
					{
						$found=1;
						for($c2=$iib;$c2>($c1-$browser);$c2--){browse(-1);}
						last;
					}
				}
				print "\e[$cLine\H\e[1;32\m Enter search string: \e[31m\e[0\K$expression";
			}
			$start=0 unless $found;
		}
		print "\e[$cLine\H\e[2\K\e[0;0\m\r";
		print "\e[$cLine\H\e[0;0\m\r";

}
sub del
{
	$actuallist=1;
	open(RPL,"$playlist");
	@files=<RPL>;
	close(RPL);
	open(PL,">$playlist");
	for($in=0;$in<$max;$in++)
	{
		if($in == $iib)
		{
			if($_[0])
			{
				print "\e[$cLine\H".plainfiles($files[$in],41,1)."\e[0;31\m Press <ENTER> if you really want to delete this file from your Harddisk!!\e[0;0m\b ";
				$key=readKey(20);
				if($key eq "\n")
				{
					chomp $files[$in];
					unlink "$files[$in]" or ${print clean($cLine+1).clean($cline)."\e[$cLine\H\e[00;31\m failed!: \e[1\m$!\e[0;0m\r";$key=readKey(5);}
										
				}
				else
				{
					print PL "$files[$in]";	
					$actuallist=0;
				}
				print clean($cLine+1).clean($cLine);
			}
			if($iib+1 == $max && $ib>0)
			{
				$playingb--;$ikb--;$ib--;$iib--;
			}
			unless($time{$filesc[$iib]} eq undef)
			{
				split(/\:/,$totalTime);
				#print "\e[1;31m0: $_[0]\n1: $_[1]\n2: $_[2]\n";quit();
				$tmp=$_[0]*3600+$_[1]*60+$_[2];
				split(/\:/,$time{$filesc[$iib]});
				$tmp1=$_[0]*60+$_[1];
				$tmp-=$tmp1;
				$tmp2=int($tmp/3600);
				$tmp3=($tmp-$tmp2*3600)/60;
				$tmp4=$tmp%60;
				$totalTime=sprintf "%2d:%.2d:%.2d",$tmp2,$tmp3,$tmp4;
			}
		}
		else
		{
			print PL "$files[$in]";
		}
	}
	if($iib<$ii)
	{
		$ii--;$playing--;$i=$ii;
	}
	$tmp6=$time{$filesc[0]} unless $iib == 0;
	close(PL);
	open(RPL,$playlist);
	@filesa = <RPL>;
	@files=@filesa;
	@filesc=@filesa;
	close RPL;
	$max=scalar(@files);
	$iia=0;
	$iib-=$ikb;
	$time{$filesc[0]}=$tmp6 unless $iib+$ikb == 0;
	print cleanlist();
	printlist($in,$max,$iib);
	$iib=$ib;
	print "\e[$playingb\H".plainfiles($files[$iib],$playListColor,$browserColor,$iib+1)."\e[0m\e[$cLine\H";
	print "\e[$playing\H\e[1m".plainfiles($files[$ii],$playBarColor,1,$ii+1)."\e[0m\e[$cLine\H" unless $changb;
	$tmp3=$iib+1;
#	getFileTime();
}
sub sortMenu
{
	$tmp1=$termsize[1]-8;
	$tmp2=$termsize[1]-7;
	$tmp3=$toChange+$tmp2;
	return "\e[$tmp1\H\e[40G\e[44m\e[1K\r\e[4m\e[33\m Choose and then press \e[31\mEnter\e[33\m to sort\e[0;40m\e[32\m\e[$tmp2\H\e[40G\e[1K\r $tmp1[0]\n\r\e[40G\e[1K\r $tmp1[1]\n\r\e[40G\e[1K\r $tmp1[2]\n\r\e[40G\e[1K\r $tmp1[3]\n\r\e[40G\e[1K\r $tmp1[4]\n\r\e[40G\e[1K\r $tmp1[5]\n\r\e[40G\e[1K\r $tmp1[6]\n\r\e[$tmp3\H\e[7\m $tmp1[$toChange]\e[0;0m";
}
sub sort_playlist
{
	@tmp1=("by Date - ascending","by Date - descending","by Path + Filename ascending","by Path + Filename descending","by Filename ascending","by Filename descending","randomize");
	$toChange=0;
	print sortMenu();
	while(1)
	{
		$tmp3=$toChange+$tmp2;
		print $inf.$vol;#."\e[$tmp1\H\e[40G\e[44m\e[1K\r\e[4m\e[33\m Choose and then press \e[31\mEnter\e[33\m to sort\e[0;40m\e[32\m\e[$tmp2\H\e[40G\e[1K\r $tmp1[0]\n\r\e[40G\e[1K\r $tmp1[1]\n\r\e[40G\e[1K\r $tmp1[2]\n\r\e[40G\e[1K\r $tmp1[3]\n\r\e[40G\e[1K\r $tmp1[4]\n\r\e[40G\e[1K\r $tmp1[5]\n\r\e[40G\e[1K\r $tmp1[6]\n\r\e[$tmp3\H\e[7\m $tmp1[$toChange]\e[0;0m";
		grepOutput();
		processOutput();
		calcTime();
		$inf = getINF();$vol=getVolume("pcm",9);
		if($key eq 's' || $key eq 'q' || $key eq "Esc")
		{
			refresh(1);
			last;
		}
		elsif($key eq "\n")
		{
			print "\e[0;0;0\m";
			$tmp4="$filesc[$ii]";
			if($toChange == 0)
			{
				sortSub(1,1);
			}
			elsif($toChange == 1)
			{
				sortSub(1,0);
			}
			elsif($toChange == 2)
			{
				sortSub(0,1);
			}
			elsif($toChange == 3)
			{
				sortSub(0,0);
			}
			elsif($toChange == 4)
			{
				sortSub(2,1);
			}
			elsif($toChange == 5)
			{
				sortSub(2,0);
			}
			else
			{
				sortSub(3);
			}
			for($in=0;$in<scalar(@filesc);$in++)
			{
				$tmp5=$filesc[$in];
				chomp $tmp5;
				chomp $tmp4;
				if("$tmp5" eq "$tmp4")
				{
					$ii=$in;
					resetList();
					refresh();
					last;
				}
				
			}
			last;
		}
		elsif($key eq 'Down')
		{
			$toChange++;
			if($toChange>6)
			{
				$toChange=0;
			}
			print sortMenu();
		}
		elsif($key eq 'Up')
		{
			$toChange--;
			if($toChange<0)
			{
				$toChange=6;
			}
			print sortMenu();
		}
	}
}
sub sortSub
{
	$output=$filesc[$ii];
	if($_[0] == 1)
	{
		@tmp=@filesc;
		@files=();
		for($in=0;$in<scalar(@tmp);$in++)
		{
			chomp $tmp[$in];
			$tmp1=(stat("$tmp[$in]"))[9];
			$tmp2 = POSIX::strftime("%Y%m%d%H%M", localtime($tmp1));
			$files[$in]="$tmp2 $tmp[$in]";
		}
	}
	elsif($_[0] == 2)
	{
		@tmp=@filesc;
		@files=();
		for($in=0;$in<scalar(@tmp);$in++)
		{
			$files[$in]=plainfiles($filesc[$in],"nocolor");
		}
	}
	@tmp=();
	if($_[1]==1)
	{
		sortInc();
	}
	elsif($_[1]==0)
	{
		sortDec();
	}
	if($_[0]==3)
	{
		@tmp1=@tmp;
		@tmp = shuffle(@tmp1);
	}
	if($_[0] == 2)
	{
		@files=@tmp;
		@tmp=();
		for($in=0;$in<scalar(@files);$in++)
		{
			if($files[$in] =~ /http\:\/\//)
			{
				$files[$in] =~ /http\:\/\/(.*)/;
				$tmp[$in]="http://$1";
			}
			else
			{
				@splitfile=split(/\#split\#/,$files[$in]);
				$tmp[$in]="$splitfile[1]$splitfile[0]";
			}
		}
	}
	for($in=0;$in<scalar(@tmp);$in++)
	{
			$tmp[$in] =~ s/^[0-9]* //g;
			chomp $tmp[$in];

	}
 	while($tmp[0] =~ /^http\:\/\//)
	{
		$tmp6=shift @tmp;
		push @tmp,$tmp6;
	}
	open(PL,">$playlist");
	for($in=0;$in<scalar(@tmp);$in++)
	{
			print PL "$tmp[$in]\n";

	}
	close PL;
	open(RPL,"$playlist");
	@tmp=<RPL>;
	close RPL;
	@filesc=@tmp;
	@files=@filesc;
	@filesa=@filesc;
	correct();
	refresh();
	$actuallist=1;
}
sub sortInc
{
	@tmp=sort {$a cmp $b} @files;
}
sub sortDec
{
	@tmp=sort {$b cmp $a} @files;
}
sub fileInfo
{
	$key = '';
	$filec = $filesc[$iib];
	chomp $filec;
	if($filec =~ /\.mp.$/i)
	{
		@tmp=("TITLE", "ARTIST", "ALBUM", "YEAR", "COMMENT", "GENRE", "Rename");
		$info = get_mp3info($filec);
		$tag=get_mp3tag($filec);
		$tmp=$termsize[0]-9;
		$toChange=0;
		$tmp3=31;
		split(/\//,$filec);
		$tmp1='';
		for($in=0;$in<scalar(@_)-1;$in++)
		{
			$tmp1.="$_[$in]/";
		}
		@tmp1=($tag->{TITLE}, $tag->{ARTIST}, $tag->{ALBUM}, $tag->{YEAR}, $tag->{COMMENT}, $tag->{GENRE}, substr($_[scalar(@_)-1],0,length($_[scalar(@_)-1])-4));
		#@tmp1=($tag->{TITLE}, $tag->{ARTIST}, $tag->{ALBUM}, $tag->{YEAR}, $tag->{COMMENT}, $tag->{GENRE}, $tmp4);
		$tmp5=$info->{MODE};
		if($tmp5 == 0)
		{
			$tmp5="stereo";
		}
		elsif($tmp5 == 1)
		{
			$tmp5="joint stereo";
		}
		elsif($tmp5 == 2)
		{
			$tmp5="dual channel";
		}
		elsif($tmp5 == 3)
		{
			$tmp5="single channel";
		}
		else{$tmp5="Unknow";}
		printf "\e[2J\e[10\HPress \e[32\m[CTRL+S]\e[0;0m to save or \e[31\m[ESC]\e[0;0m to abort\n\nPress \e[31\m[CTRL+D]\e[0;0m to remove ID3-tag \e[9\H\e[55\G\e[1;4\mID3-Info\e[0;0\m\n\n\e[47\GMPEG audio version : %s\n\e[55\GMPEG layer : %s\n\e[53\Gchannel mode : $tmp5\n\e[50\Gboolean for VBR : %s\n\e[58\Gbitrate : %s\n\e[49\Gfrequency in kHz : %s\n\e[52\Gtime in MM:SS : %s\n\e[33\Gboolean for audio is copyrighted : %s\n\e[32\Gboolean for MP3 frames are padded : %s\n\e[37\Gapproximate number of frames : %s\n\e[36\Gapproximate length of a frame : %s\n\e[40\GVBR scale from VBR header : %s\n\e[44\Gbytes in audio stream : %s",$info->{VERSION},$info->{LAYER},$info->{VBR},$info->{BITRATE},$info->{FREQUENCY},$info->{TIME},$info->{COPYRIGHT},$info->{PADDING},$info->{FRAMES},$info->{FRAME_LENGTH},$info->{VBR_SCALE},$info->{SIZE};
		while(1)
		{
			$_=$tmp+8;
			printf "\r\e[0;0\m\r\e[1\H |\e[1;4\m Path \e[0;0\m|\n\e[2\K %0.$_\s\e[3\H  title : %0.$tmp\s\n artist : %0.$tmp\s\n  album : %0.$tmp\s\n   year : %0.$tmp\s\ncomment : %0.$tmp\s\n  genre : %0.$tmp\s\r",$filec,$tmp1[0],$tmp1[1],$tmp1[2],$tmp1[3],$tmp1[4],$tmp1[5];
			$tmp2=$toChange+3;
			printf "\e[$tmp2\H\e[$playListColor;$browserColor\m%7s\e[0;0m : \e[0;$mp3TagColor\m%s\e[0\K\e[0;0m",$tmp[$toChange],$tmp1[$toChange];
			while($key eq undef)
			{
				$key=readKey(1);
				@termsize=GetTerminalSize();
				$termsize[1]-- if $shortView;
				if($termsize[1]+$termsize[0] != $termsize2[1]+$termsize2[0])
				{
					
				}
			}
			if($key eq "Esc")
			{
				refresh();
				last;
			}
			if($key eq 'Down')
			{
				$toChange++;
				if($toChange>5)
				{
					$toChange=-1;
				}
			}
			elsif($key eq 'Up')
			{
				$toChange--;
				if($toChange<-1)
				{
					$toChange=5;
				}
			}
			elsif($key eq "Del")
			{
				$tmp1[$toChange]='';
			}
			elsif($key eq "\x13")
			{
				set_mp3tag("$filec", $tmp1[0], $tmp1[1], $tmp1[2], $tmp1[3], $tmp1[4], $tmp1[5], ) or ${print "\e[11\H\e[31\m ERROR: $!";sleep 2;last};
				if("$filec" ne "$tmp1$tmp1[6]")
				{
					$tmp6=$filesc[$iib];chomp $tmp6;
					rename("$tmp6","$tmp1$tmp1[6].mp3")or ${print "\e[11\H\e[31\m ERROR: $!";sleep 2;last};
					$filesc[$iib]="$tmp1$tmp1[6].mp3\n";$files[$iib]="$tmp1$tmp1[6].mp3\n";
					open(PL,">$playlist");
					for($in=0;$in<$max;$in++)
					{
						print PL "$filesc[$in]";
					}
					$actuallist=1;
				}
				print "\e[11\H\e[32\m Saved succesfully";sleep 1;last;

			}
			elsif($key eq "\x04")
			{
				remove_mp3tag("$filec","ALL") or ${print "\e[11\H\e[31\m ERROR: $!";sleep 2;last};
				print "\e[11\H\e[32\m ID3 tag removed";sleep 1;fileInfo();last;
			}
			else
			{
				if($key eq "End")
				{
					$tmp4=$tmp1[$toChange];
				}
				else{$tmp4=''};
				while(1)
				{
					if($key eq "Esc"||$key eq "\n"||$key eq "Up"||$key eq "Down")
					{
						$tmp1[$toChange]=$tmp4 unless $tmp4 eq '' || $key eq "Esc";
						$filec="$tmp1$tmp1[6].mp3";
						last;
					}
					elsif($key eq "Backspace")
					{
						chop $tmp4;
					}
					elsif($key eq "\x13")
					{
						$tmp1[$toChange]=$tmp4 unless $tmp4 eq '';
						set_mp3tag("$filec", $tmp1[0], $tmp1[1], $tmp1[2], $tmp1[3], $tmp1[4], $tmp1[5], ) or ${print "\e[11\H\e[31\m ERROR: $!";sleep 2;last};
						print "\e[11\H\e[32\m Saved succesfully";sleep 1;refresh();return '';
					}
					else
					{
						$tmp4.=$key;
					}
					printf "\e[$tmp2\H%7s : \e[$playListColor;$browserColor\m%s\e[0\K\e[$mp3TagColor;0\m",$tmp[$toChange],$tmp4;
					$key=readKey(120);
				}
			}
			$key = '';

		}
		refresh();
	}
	elsif($filec =~ /\.ogg$/i)
	{
		$tmp=$termsize[0]-46;
		if($tmp < 0){$tmp=0;}
		print "\e[2J\r\e[0;0\m\r\e[1\H |\e[1;4\m Path \e[0;0\m|\n\e[2\K $filec\e[4H\e[10G\e[1;4m\OGG info\e[45GOGG tag\e[0;0m\e[6H";
		$ogg = Ogg::Vorbis::Header::PurePerl->new("$filec");
		while (my ($k, $v) = each %{$ogg->info})
		{
        		printf "%15s : \e[1;32\m%s\e[0;0\m\n",$k,$v;
		}
		print "\e[6H";
		foreach $com ($ogg->comment_tags)
		{
        		printf "\e[34\G%11s : \e[1;32\m%0.$tmp\s\e[0;0\m\n",$com,$_ foreach $ogg->comment($com);
		}
		
		readKey();
		refresh();
	}
	else
	{
		
		print "\e[2J\r\e[0;0\m\r\e[1\H |\e[1;4\m Path \e[0;0\m|\n\e[2\K $filec";
		readKey();
		refresh();
	}
}
sub createNewPlayList
{
	while(-f "$ENV{'HOME'}/.mplay/playlist$plnr")
	{
		$plnr++;
	}
	$playlist="$ENV{'HOME'}/.mplay/playlist$plnr";
	print cleanlist();
}
sub switchPlayList
{
	$playingInPlaylist{"$plnr"}="$i,$ii,$ik,$playing,$ib,$iib,$ikb,$playingb";
	$plnr++;
	unless(-f "$ENV{'HOME'}/.mplay/playlist$plnr")
	{
		$plnr="00";
		$playlist="$ENV{'HOME'}/.mplay/playlist$plnr";
		refresh(1);
	}
	if(!$playingInPlaylist{"$plnr"})
	{
		$playingInPlaylist{"$plnr"}="0,0,0,$plbLine,0,0,0,$plbLine";
	}
#	else
#	{
		split(/,/,$playingInPlaylist{"$plnr"});
		($i,$ii,$ik,$playing,$ib,$iib,$ikb,$playingb)=@_;
#	}
	$playlist="$ENV{'HOME'}/.mplay/playlist$plnr";
	open(RPL,"$playlist");
	@filesc=();
	@filesc=<RPL>;
	@files=@filesc;@filesa=@filesc;
	$max=@filesc;
	$actuallist=1;
	save();
	getFileTime();
	refresh();
}
sub grepOutput
{
	$output=ReadLine -1, $R;
}
sub readKey
{
#	if($_[0] eq "noclear")
#	{
#		return $key;
#	}
#	else{$key = '';}
	$key = '';
        $x2 = ReadKey $_[0];
	$x=ord($x2);
	#return unless $x;
	if($x == 27)
	{
		$x =ReadKey 0.3;
		$x=ord($x);
		$x2=ReadKey -1;
		$x2=ord($x2);
		$x3=ReadKey -1;
		$x3=ord($x3);
		if($x eq 0||$x eq 27)
		{
			if($x2 == 0)
			{
				$x="Esc";
				return $x;
			}
		}
		if($x == 91)
		{
			if($x2 == 49)
			{
				if($x3 == 49)
				{
					$x="k1";
				}
				elsif($x3 == 50)
				{
					$x="k2";
				}
			}
			elsif($x2 == 52)
			{
				if($x3 == 126)
				{
					$x="End";
				}
			}
			elsif($x2 == 56)
			{
				if($x3 == 126)
				{
					$x="End";
				}
			}
			elsif($x2 == 91)
			{
				if($x3 == 65)
				{
					$x="k1";
				}
				elsif($x3 == 66)
				{
					$x="k2";
				}
				elsif($x3 == 67)
				{
					$x="k3";
				}
				elsif($x3 == 51)
				{
					$x="Alt_Del";
					ReadKey -1;
				}
			}
			elsif($x2 == 51)
			{
					#quit();
				if($x3 == 126)
				{
					$x="Del";
					ReadKey -1;
				}
				elsif($x3 == 59)
				{
					$x="Alt_Del";
					ReadKey -1;
					ReadKey -1;
				}
				else
				{
					#$x3=ReadKey -1;
					#print "\e[34m\e[10H\e[2K\rHALLO: $x,$x2,$x3";quit();
				}
			}
			elsif($x2 == 65)
			{
				$x="Up";
			}
			elsif($x2 == 66)
			{
				$x="Down";
			}
			elsif($x2 == 67)
			{
				$x="Right";
			}
			elsif($x2 == 68)
			{
				$x="Left";
			}
			elsif($x2 == 70)
			{
				$x="End";
			}
			elsif($x2 == 72)
			{
				$x="Pos1";
			}
			elsif($x2 == 53)
			{
				$x="PgUp";
			}
			elsif($x2 == 54)
			{
				$x="PgDn";
			}
			elsif($x2 == 50)
			{
				$x="Ins";
			}
			else{$x='';}
			ReadKey -1;
			ReadKey -1;
		}
		elsif($x == 27)
		{
			if($x2 == 91)
			{
				$x = 'Alt_Del';
				ReadKey -1;
				ReadKey -1;
			}
		}
		elsif($x == 79)
		{
			if($x2 == 80)
			{
				$x="k1";
			}
			elsif($x2 == 81)
			{
				$x="k2";
			}
			elsif($x2 == 82)
			{
				$x="k3";
			}
			else{$x='';}
			ReadKey -1;
			ReadKey -1;
		}
		elsif($x > 96 && $x < 123)
		{
			return sprintf "%d",$x;
		}

	}
	elsif($x == 127||$x == 8)
	{
		$x="Backspace";
	}
	else
	{
		$x=$x2;
	}
	#while(ReadKey -1){;}
	return $x;
}
sub color
{
	if($playingb == 0)
	{
		$ib=$i;$iib=$ii;$ikb=$ik;$playingb=$playing;
	}
	$toChange=0;
	while(1)
	{
		print $inf.$vol."\e[$cLine\H\e[01;31m Press left/rigth to change $colorChange[$toChange] color\e[0K\e[0;0m";
		if($shortView)
		{
			print "\e[2\H\e[$clockColor\m\e[$termsize[0]\G\b\b\b\b$time\e[0;0m\r";
		}
		grepOutput();
		processOutput();
		calcTime();
		$inf=getINF();$vol=getVolume("pcm",9);
		if($key eq 'c' || $key eq 'q'|| $key eq 'Esc')
		{
			print "\e[$cLine\H\e[2K";
			last;
		}
		elsif($key eq 'r')
		{
			$pbarColor=34;
			$mp3TagColor=31;
			$infoColor=29;
			$trackInfoColor=34;
			$playBarColor=44;
			$browserColor=30;
			$playListColor=0;
			refresh();
			last;
		}
		elsif($key eq 'Down')
		{
			$toChange++;
			if($toChange>8)
			{
				$toChange=0;
			}
		}
		elsif($key eq 'Up')
		{
			$toChange--;
			if($toChange<0)
			{
				$toChange=8;
			}
			
		}
		elsif($key eq 'Right')
		{
			if($toChange == 0)
			{
				$pbarColor++;
				if($pbarColor>48)
				{
					$pbarColor=30;
				}
				elsif($pbarColor>37&&$pbarColor<40)
				{
					$pbarColor=40;
				}
				print getINF();
			}
			elsif($toChange == 1)
			{
				$mp3TagColor++;
				if($mp3TagColor>48)
				{
					$mp3TagColor=30;
				}
				elsif($mp3TagColor>37&&$mp3TagColor<40)
				{
					$mp3TagColor=40;
				}
				print getMP3info;
			}
			elsif($toChange == 2)
			{
				$infoColor++;
				if($infoColor>48)
				{
					$infoColor=30;
				}
				elsif($infoColor>37&&$infoColor<40)
				{
					$infoColor=40;
				}
				print getINF();
			}
			elsif($toChange == 3)
			{
				$trackInfoColor++;
				if($trackInfoColor>48)
				{
					$trackInfoColor=30;
				}
				elsif($trackInfoColor>37&&$trackInfoColor<40)
				{
					$trackInfoColor=40;
				}
				print getINF();
			}
			elsif($toChange == 4)
			{
				$volumeColor++;
				if($volumeColor>48)
				{
					$volumeColor=30;
				}
				elsif($volumeColor>37&&$volumeColor<40)
				{
					$volumeColor=40;
				}
				print getVolume("pcm",9);
			}
			elsif($toChange == 5)
			{
				$playBarColor++;
				if($playBarColor>48)
				{
					$playBarColor=30;
				}
				elsif($playBarColor>37&&$playBarColor<40)
				{
					$playBarColor=40;
				}
				if($ii == $iib)
				{print "\e[$playing\H\e[1m".plainfiles($files[$ii],$browserColor,$playBarColor,$ii+1)."\e[0m\e[$cLine\H";}
				else
				{print "\e[$playing\H".plainfiles($files[$ii],$playBarColor,1,$ii+1)."\e[0m\e[$cLine\H";}
			}
			elsif($toChange == 6)
			{
				$browserColor++;
				if($browserColor>48)
				{
					$browserColor=30;
				}
				elsif($browserColor>37&&$browserColor<40)
				{
					$browserColor=40;
				}
				if($ii == $iib)
				{print "\e[$playing\H\e[1m".plainfiles($files[$ii],$playBarColor,$browserColor,$ii+1)."\e[0m\e[$cLine\H";}
				else
				{print "\e[$playingb\H\e[0m".plainfiles($files[$iib],$playListColor,$browserColor,$iib+1)."\e[$playing\H\e[0m\e[$cLine\H";}
			}
			elsif($toChange == 7)
			{
				$playListColor++;
				if($playListColor>48)
				{
					$playListColor=29;
				}
				elsif($playListColor>37&&$playListColor<40)
				{
					$playListColor=40;
				}
				print "\e[$plbLine\H";
				$iia=$ii-($playing-$plbLine);
				print "\e[$plbLine\H";
				$iia=$ii-($playing-$plbLine);
				printlist($in,$max,$iia);
				print "\e[$playingb\H".plainfiles($files[$iib],$playListColor,$browserColor,$iib+1)."\e[0;0m\e[$playing\H".plainfiles($files[$ii],1,$playBarColor,$ii+1)."\e[0m\e[$cLine\H";
			}
			else
			{
				$clockColor++;
				if($clockColor>48)
				{
					$clockColor=30;
				}
				elsif($clockColor>37&&$clockColor<40)
				{
					$clockColor=40;
				}
				print getINF();

			}
		}
		elsif($key eq 'Left')
		{
			if($toChange == 0)
			{
				$pbarColor--;
				if($pbarColor<30)
				{
					$pbarColor=48;
				}
				elsif($pbarColor<40&&$pbarColor>37)
				{
					$pbarColor=37;
				}
				print getINF();
			}
			elsif($toChange == 1)
			{
				$mp3TagColor--;
				if($mp3TagColor<30)
				{
					$mp3TagColor=48;
				}
				elsif($mp3TagColor<40&&$mp3TagColor>37)
				{
					$mp3TagColor=37;
				}
				print getMP3info;
			}
			elsif($toChange == 2)
			{
				$infoColor--;
				if($infoColor<30)
				{
					$infoColor=48;
				}
				elsif($infoColor<40&&$infoColor>37)
				{
					$infoColor=37;
				}
				print getINF();
			}
			elsif($toChange == 3)
			{
				$trackInfoColor--;
				if($trackInfoColor<30)
				{
					$trackInfoColor=48;
				}
				elsif($trackInfoColor<40&&$trackInfoColor>37)
				{
					$trackInfoColor=37;
				}
				print getINF();

			}
			elsif($toChange == 4)
			{
				$volumeColor--;
				if($volumeColor<30)
				{
					$volumeColor=48;
				}
				elsif($volumeColor<40&&$volumeColor>37)
				{
					$volumeColor=37;
				}
				print getVolume("pcm",9);

			}
			elsif($toChange == 5)
			{
				$playBarColor--;
				if($playBarColor<30)
				{
					$playBarColor=48;
				}
				elsif($playBarColor<40&&$playBarColor>37)
				{
					$playBarColor=37;
				}
				if($ii == $iib)
				{print "\e[$playing\H\e[1m".plainfiles($files[$ii],$browserColor,$playBarColor,$ii+1)."\e[0m\e[$cLine\H";}
				else
				{print "\e[$playing\H".plainfiles($files[$ii],$playBarColor,1,$ii+1)."\e[0m\e[$cLine\H";}

			}
			elsif($toChange == 6)
			{
				$browserColor--;
				if($browserColor<30)
				{
					$browserColor=48;
				}
				elsif($browserColor<40&&$browserColor>37)
				{
					$browserColor=37;
				}
				if($ii == $iib)
				{print "\e[$playing\H\e[1m".plainfiles($files[$ii],$playBarColor,$browserColor,$ii+1)."\e[0m\e[$cLine\H";}
				else
				{print "\e[$playingb\H\e[0m".plainfiles($files[$iib],$playListColor,$browserColor,$iib+1)."\e[$playing\H\e[0m\e[$cLine\H";}

			}
			elsif($toChange == 7)
			{
				$playListColor--;
				if($playListColor<29)
				{
					$playListColor=48;
				}
				elsif($playListColor<40&&$playListColor>37)
				{
					$playListColor=37;
				}
				print "\e[$plbLine\H";
				$iia=$ii-($playing-$plbLine);
				print "\e[$plbLine\H";
				$iia=$ii-($playing-$plbLine);
				printlist($in,$max,$iia);
				print "\e[$playingb\H".plainfiles($files[$iib],$playListColor,$browserColor,$iib+1)."\e[0;0m\e[$playing\H".plainfiles($files[$ii],$playBarColor,1,$ii+1)."\e[0m\e[$cLine\H";
			}
			else
			{
				$clockColor--;
				if($clockColor<30)
				{
					$clockColor=48;
				}
				elsif($clockColor<40&&$clockColor>37)
				{
					$clockColor=37;
				}
				print getINF();

			}
		}
	}
	getMenuTitle()
}
sub remote
{
	open(R,"$remote");
	$tmp=<R>;
	close R;
	unlink "$remote";
	chomp $tmp;
	if($tmp eq 'q')
	{
		quit();
	}
	elsif($tmp eq 'a')
	{
		add_list();
	}
	elsif($tmp eq 'r')
	{
		replace_handler();
	}
	elsif($tmp eq 'n')
	{
		fchange(1,1);
		$check=5;
		$look=1;
	}
	elsif($tmp eq 'b')
	{
		fchange(-1,-1);
		$check=5;
		$look=1;
	}
	elsif($tmp =~ /[0-9]?/)
	{
		syswrite $W, "seek $tmp\n";
		$timestat=$timestat-$tmp;
		$check=5;
		if($timestat>$timenow)
		{
			$timestat=$timenow;
		}
	}
	
}
sub save
{
	open(PPL,">$lastPlayPos");
	open(C,">$conf");
	$telapsed=int($timeelapsed);
	chomp $ii;
	print PPL "$ii\n$telapsed\n$plnr";
	print C "$pbarColor:$mp3TagColor:$infoColor:$trackInfoColor:$volumeColor:$playBarColor:$browserColor:$playListColor:$clockColor:$shortView:$nr:$repeat:$dirname";
	close C;close PPL;
}
sub quit
{
	system("setterm -cursor on");
	unlink "$pid";
	save();	
	$key='';
	$i=30;
	$termsize[1]--;
	print "\e[0;0m\e[$termsize[1]\H\n";
	print "\e[0;39\m\n\n\rbye\n\r";kill 15,$mpid;
	wait;
	if($xterm)
	{
		print chr(27).']0;'."xterm".chr(7);
	}
	ReadMode 1;
	exit;
}
sub parent_handler
{
	unlink "$pid";
	save();
	print "\e[$termsize[1]\H\e[1;31m\n\e[2K mplay (pid:$$) received 'SIGTERM' :( \e[0m\n";sleep 0.3;
	print "\e[0;0m\e[$termsize[1]\H\n";kill 15,$mpid; ReadMode 1;wait;
	system("setterm -cursor on");
	exit;
}
sub help
{
	switchCompactView() if $save;
	$tmp2='';
	print clearscreen()."\e[1H\e[0;0m";
	if($ENV{'LANG'} =~ /de/)
	{
		open(HELP,"/usr/share/mplay/help_de") or ${print "\e[31\mKonnte die Hilfedateien nicht finden!\nDu musst \e[32\m./install\e[31\m ausfhren um die Hilfedateien zu kopieren.";readKey();last;}
	}
	else
	{
		open(HELP,"/usr/share/mplay/help_en") or ${print "\e[31\mCould not open help file!\nYou have to run \e[32\m./install\e[31\m to copy the help file.";readKey();last;}
	}
	@help=<HELP>;
	$tmp3=scalar(@help);
#	@termsize=GetTerminalSize();
	for($in=0;$in<$tmp3;$in++)
	{
		chomp $help[$in];
	}
	for($in=0;$in<$termsize[1];$in++)
	{
		$tmp2.=sprintf "\n$help[$in]\r";
	}
	#sleep 3;
	print "$tmp2\e[0;0m";
	$tmp1=$termsize[1];
	while(1)
	{
		if(&termSizeChanged)
		{
			#help();
			refresh('help');
			return;
			
		}
		$key=readKey(0.25);
		if($key eq 'Up'&& $tmp1-$termsize[1]>0)
		{
			$tmp1--;
			print "\e[1;0\m\e[1H\eM$help[$tmp1-($termsize[1])]\e[$termsize[1]\H\e[0;0m\r";
		}
		elsif($key eq 'Down' && $tmp1<$tmp3)
		{
			print "\e[1;0\m\n$help[$tmp1]\e[0;0m\r";
			$tmp1++;
		}
		elsif($key eq 'PgUp')
		{
			for($in=0;$in<$termsize[1]-1&&$tmp1-$termsize[1]>0;$in++)
			{
				$tmp1--;
				print "\e[1;0\m\e[1H\eM$help[$tmp1-($termsize[1])]\e[$termsize[1]\H\e[0;0m\r";
			}
		}
		elsif($key eq 'PgDn')
		{
			for($in=0;$in<$termsize[1]-1&&$tmp1<$tmp3;$in++)
			{
				print "\e[1;0\m\n$help[$tmp1]\e[0;0m\r";
				$tmp1++;
			}
		}
		if($key eq 'h'||$key eq 'q' || $key eq "Esc")
		{
			last;
		}

	}
	close(HELP);
	#$termsize[1]-- if $shortView;
	switchCompactView() if $save;
	refresh();
}
sub switchCompactView
{
	if($shortView)
	{
		$cLine=10;
		$plbLine=12;
		$shortView=0;
		resetList();
		$termsize[1]++;
	}
	else
	{
		$cLine=2;
		$plbLine=3;
		$shortView=1;
		resetList();
		$termsize[1]--;
	}
	refresh() unless $_[0] eq 'silent';
}
sub termSizeChanged
{
	@termsize=GetTerminalSize();
	$termsize[1]-- if $shortView;
	if($termsize[1]+$termsize[0] != $termsize2[1]+$termsize2[0])
	{
		return 1;
	}
	return 0;

}
sub add_list
{
	$actuallist=1;
	open(RPL,$playlist);
	@filesa = <RPL>;
	@files=@filesa;@filesc=@filesa;
	close RPL;
	$max=scalar(@files);
	getFileTime();
	refresh();
}
sub replace_handler
{
	$ii=0;
	$timeelapsed=0;
	$actuallist=0;
	save();
	kill 15,$mpid;
	unlink "$pid";
	wait;
	exec($0);
	ReadMode 1;exit;
}
sub child_handler
{
	save();
	
	if($ii<$max-1)
	{
		unlink "$pid";
		print "\e[0;31mMplayer died\ntry to restart..\e[0m\n";
		$key=readKey(0.1);
		if($key eq 'q' || $key eq "\x03")
		{
			quit();
		}
		wait;

		exec($0);
		exit;
	}
	else
	{
		open(PPL,">$lastPlayPos");
		print PPL "0\n0";
		close(PPL);
		$termsize[1]--;
		print "\e[0;0m\e[$termsize[1]\H\n";
		$tmp=0.5;
		$tmp1=($tmp-0.005)/($termsize[0]-18);
		print "\e[34\m\e[2K".' <End of playlist> '."\e[0\m\r";
		checkRemote();
		$key=readKey(2);
		if($key eq 'q' || $key eq "\x03" || !$repeat)
		{
			quit();
		}
		else
		{
			$i=0;$ib=0;$ii=0;$iib=0;$ik=0;$ikb=0;$playing=$plbLine;$playingb=$plbLine;$telapsed=0;
			&open;
			refresh(1);
			$change=40;
		}
	}
}
sub printHelp
{
	print "\n Usage:\t\e[31\mmplay \e[32\m<options> <path\e[34\m|\e[32\murl>\n\n\e[32\m options \e[0\mfor mplay:\n\n\e[32\m\t-h --help \e[0\m\t\tshow this help\e[34\m\n\e[32\m\t-v --version \e[0\m\t\tshow actual version\e[34\m\n\e[32\m\t--mopts '\e[34mmplayer opts\e[32\m'\e[0\m\tstart mplayer with with specific arguments\n\e[32\m\t-e \e[34\m<path>\e[0\m\t\tto add files to current playlist\n\e[32\m\t-q --quit\e[0\m\t\tto close \e[1\mmplay\e[0\m from outside\n\e[32\m\t-n --next\e[0\m\t\tplay next track\n\e[32\m\t-b --back\e[0\m\t\tplay previous track\n\e[32\m\t+\e[34\msecs\e[0\m\t\t\tseek xx seconds forward\n\e[32\m\t-\e[34msecs\e[0\m\t\t\tseek xx seconds backward\n\nexamples:\e[31\m mplay \e[34\m/music/\e[0;37\m play all files from all sub-directories in /music/\n\t  \e[31\mmplay \e[34\m/path/to/some/music/*.mp3\e[0\m \n\t  \e[31\mmplay \e[34\mhttp://ip:port \e[0;37\mlisten to a stream\n\t  \e[31\mmplay \e[32\m-e \e[34\m/playlists/music.m3u /music/*.mp3 http://11.11.111.1:8000\n\t\e[31\m  mplay \e[34\mplaylist.m3u \e[0;37\mplay m3u playlist\n\t\e[31\m  mplay \e[34\mplaylist.pls \e[0;37\mplay pls playlist\n\t\e[31\m  mplay \e[32\m+\e[34\m33 \e[0;37\mseek 33 seconds forward\n\t\e[31\m  mplay \e[32\m-\e[34\m11 \e[0;37\mseek 11 seconds backward\n\t\e[31\m  mplay \e[32\m--mopts '-fs -ao alsa'\e[34\m /videos/*.avi\e[0;37\m\n\n\e[1;34\mmplay\e[0;34\m saves the position within the track last played before quitting. \nJust start without options to continue playing.\n\nWhen \e[1\mmplay\e[0;34\m is runnig press \e[31\m'h'\e[0;34\m to see the internal help \e[0;0\m\n\nEnjoy ...\n\n";
}
sub filterOut
{
	@tmp=sort @tmp1;
	@tmp1=();
	$ii=0;
	for(@tmp)
	{
		if($_ =~ /^http\:\/\//)
		{
			$tmp1[$ii]=$_;
		}
		else
		{
			$_ =~ /(...$)/;
			$tmp=$1;
			$tmp=~s/[^a-zA-Z0-9]/\\$&/g;
			#print "$tmp\n";quit();
			$tmp1[$ii]=$_ if $teste =~ /$tmp/i;
			$ii++;
		}
	}
	grep ($teste,@tmp);

	print PL @tmp1;
	$ii=0;
}
#sub nonblock 
#{
	#$socket = shift;
#	$flags = fcntl($socket, F_GETFL, 0)or die "Can't get flags for socket: $!\n";
#	fcntl($socket, F_SETFL, $flags | O_NONBLOCK)or die "Can't make socket nonblocking: $!\n";
#}
