#!/usr/bin/perl -X
$mp3Version=0;
$speed=1;
$version='mplay-0.80';
$conf="$ENV{'HOME'}/.mplay/config";
$lastPlayPos="$ENV{'HOME'}/.mplay/playpos";
open(PPL,"$lastPlayPos");
$playlistTime="$ENV{'HOME'}/.mplay/pltime";
$playlistTag="$ENV{'HOME'}/.mplay/pltag";
$mplayconf="$ENV{'HOME'}/.mplay/mplayconf";
if(-f $mplayconf)
{
	open(MPC,$mplayconf);
	$found=0;
	while(<MPC>)
	{
		if($_ =~ /^extensions\=(.*)/){$extensions=$1;$found=1;}
		if($_ =~ /^cache\=(.*)/){$cache=$1;}
		if($_ =~ /^normalize\=on/){$normalize=1;}
		if($_ =~ /^extrastereo\=on/){$extraStereo=1;}
		#if($_ =~ /^cache-min\=(.*)/){$cache_min=$1;}
		if($_ =~ /^cache-prefill\=(.*)/){$cache_prefill=$1;}
	}
	$extensions="mpg mpeg wma avi asf vob bin fli viv ram yuv mov wmv mp3 ogg ogm mp4 wav" unless $found;
}
else
{
	$extensions="mpg mpeg wma avi asf vob bin fli viv ram yuv mov wmv mp3 ogg ogm mp4 wav";
}
$cache=2048 if $cache eq '';
$cache_prefill=18 if $cache_prefill eq '';
$cache_min=0;
close MPC;
$mplayerDefOptions="-cache $cache -cache-min $cache_min -cache-prefill $cache_prefill -quiet -slave -playlist";
$videoExtensions=$extensions;$videoExtensions =~ s/ogg//g;$videoExtensions =~ s/mp3//g;$videoExtensions =~ s/wav//g;
@tmp=<PPL>;
$plnr=$tmp[2];chomp $plnr;close PPL;
if($plnr == 5){$plnrText="bookmarks";}else{$plnrText=$plnr;}

if($plnr<1||$plnr>5)
{
	$plnr=1;
}
$playlist="$ENV{'HOME'}/.mplay/playlist$plnr";

$pid="$ENV{'HOME'}/.mplay/pid";
$remote="$ENV{'HOME'}/.mplay/.remote";
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] eq '-p' || $ARGV[0] eq '--pause')
	{
		open(R,">$remote");
		print R 'p';
		close R;
		print "\e[32\mpause\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;
	}
	if(!checkIfExist())
	{
		print "\e[1;34\mmplay: \e[0;32\mtarged doesn't exist -> nothing added \e[0;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";
		#print "$playlist\n";quit();
		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 '-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;
}


sub checkIfExist
{
	$max=scalar(@ARGV);$tmp=0;$tmp1=0;
	for($i=0;$i<$max;$i++)
	{
		if(-e $ARGV[$i])
		{
			$tmp=1;
			last if $tmp1;
		}
		elsif($ARGV[$i] eq '--mopts')
		{
			$mplayerCmdOptions=$ARGV[$i+1];
			last if $tmp;
			$tmp1=1;
		}
		elsif($ARGV[$i] =~ /^http\:\/\//)
		{$tmp=1;}
	}
	$shoutcast=0 if $tmp;
	return $tmp;
}
$tmp=checkIfExist();
if($tmp == 0)
{
	open(RPL,"$playlist");
	open(PPL,"$lastPlayPos");
	@files = <RPL>;
	close RPL;
	$max=scalar(@files);
	open(PLT,"$playlistTime");
	@tmp=<PLT>;
	for($in=0;$in<scalar(@tmp);$in++)
	{
		$time{$files[$in]}=$tmp[$in];
		chomp $time{$files[$in]};
	}
	close PLT;
	open(TAG,"$playlistTag");
	@tmp=<TAG>;
	for($in=0;$in<scalar(@tmp);$in++)
	{
		$tag{$files[$in]}=$tmp[$in];
		chomp $tag{$files[$in]};
	}
	close TAG;
	$goBack=1;
}
else
{
	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//;
					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");
				}
			}
		}
		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>;
close 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 Video::Info;
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} = \&reap;
#$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","separator");
@termsize=GetTerminalSize();$termsize[1]-- if $shortView;
$separatorColor=33;
$curVolume="pcm";
$dirname="/";
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];
	chomp $tmp[13];
	@eq=split(/\+/,$tmp[13]);
	$eq=$tmp[14];
	$shoutcast=$tmp[15];
	$shoutcastGenre=$tmp[16];
	$sel=$tmp[17];
	$showTagPlaylist=$tmp[18];
	$curVolume=$tmp[19];
	$sortShoutcast=$tmp[20];
	$timeSwitch=$tmp[21];
	#$shoutcastGenreSelect=$tmp[18];
	setMplayerEq();
	#print $mplayerOptions;exit;

	

}
else
{
	setDefColor();
	$shortView=0;
	$showTagPlaylist=1;
	$nr=1;
	$repeat=1;
	$eq=0;
	@eq=("0","0","0","0","0","0","0","0","0","0");
}
if($shortView)
{
	$cLine=2;
	$plbLine=3;
}
else
{
	$cLine=10;
	$plbLine=12;
}
$dirname='/' if $dirname eq '';
if(-d "$ENV{'HOME'}/.mplay/")
{
	;
}
else
{
	mkdir "$ENV{'HOME'}/.mplay/" or die "$!\nPlease be sure that you have write permissions in your home directory.\n";
}
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();
}
openMplayer();
sub openMplayer
{
	$mplayerOptions="$mplayerEqOptions $mplayerCmdOptions $mplayerDefOptions";
	$mpid=open3($W, $R, $E, "mplayer $mplayerOptions $playlist");
	if($shoutcast && scalar(@pls)){$tmp1="http://www.shoutcast.com$pls[$iib]";syswrite $W, "loadlist $tmp1\n";}
	elsif($_[0] eq "fchange"){syswrite $W, "pt_step $ii\n";}
	
}
$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 0.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;
$change=40;
$look=1;
$check=5;
close(PPL);
@filesa=@files;
@filesc=@files;
$timestat=gettimeofday;
sub goBack
{
	if($ii>0)
	{
		#sleep 0.5;
		syswrite $W, "pt_step $ii\n";
	}
	#sleep 3;
	syswrite $W, "seek $_[0]\n";
	$timestat=$timestat-$_[1];
	$check=4;
}
# clearbuffer(0.1);
goBack($fpos[1],$fpos[1]) if $goBack;
if($ii<$termsize[1]-$plbLine)
{
	$playing=$ii+$plbLine;
	#print refresh();
	$ii-=$playing-$plbLine;
}
else
{
	#print refresh();
}
if($actuallist)
{
	$actuallist=0;
}
else
{
	getFileTime();
}
if($shortView){$tmp5=$cLine}else{$tmp5=$cLine+1}
print "\e[$tmp5\H\e[0;$clockColor\m$dis";
print list($in,$max,$ii) unless $shoutcast;
$ii=$i;
print getPB() unless $shoutcast;
#get_time_length();
# clearbuffer(-1);
print getVolume("$curVolume",9).getMP3info();
resetList();
if($i>$termsize[1]-12)
{
	print refresh();
}
#syswrite $W, "seek -1\n" if $filesc[$ii] =~ /mp3$/;
#$SIG{CHLD}= \&reap;
$SIG{CHLD}= 'IGNORE';
sub reap
{
	if($test)
	{
#		if(waitpid(-1,WNOHANG) > 0)
#		{
			&test;
			$test=0;
#		}
	}
	if($test2)
	{
#		if(waitpid(-1,WNOHANG) > 0)
#		{
			&test2;
			$test2=0;
#		}
	}
	#while(waitpid(-1,WNOHANG) > 0){sleep 0.1;}
	#$pid = waitpid(-1, &WNOHANG);
#	$SIG{CHLD}= \&reap;
	#wait;
}
if($shoutcast){$termsize[1]--;shoutcast();}
get_time_length();
while(1)
{
	if(&termSizeChanged)
	{
		resetList();
		print refresh();
	}
	if($key eq "a")
	{
		$key=menu();
	}
	#if($key > 96 && $key < 123)
	#{;}
	if($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 ".")
	{
		raiseVolume();
	}
	elsif($key eq ",")
	{
		reduceVolume();
	}
	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 'H')
	{
		$save=$shortView;
		help("help");
	}
	elsif($key eq "Down")
	{
		print browse(1);
	}
	elsif($key eq "Up")
	{
		print browse(-1);
	}
	elsif($key eq "PgDn")
	{
		PgDn();
	}
	elsif($key eq "PgUp")
	{
		PgUp();
	}
	elsif($key eq "n")
	{
		#print browse(1);ret();
		fchange(1,1);
		#$check=5;
		#$look=1;
	}
	elsif($key eq "b")
	{
		#print browse(-1);ret();
 		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")
	{
		pause();
	}
	elsif($key eq "m")
	{
		mute();
	}
	elsif($key eq "k1"||$key eq "k2"||$key eq "k3"||$key eq "k4"||$key eq "k5")
	{
		$key=~s/k//g;
		switchPlayList($key);
	}
	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 "d")
	{
		mplayerEqualizer();
		print refresh();
	}
	elsif($key eq "Del")
	{
		del();
	}
	elsif($key eq 's')
	{
		sort_playlist();
	}
	elsif($key eq "\x12")
	{
		$name='';
		print refresh();
	}
	elsif($key eq "e")
	{
		fileInfo();
	}
	elsif($key eq "i")
	{
		if($nr){$nr=0}else{$nr=1}
		print 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 "A")
	{
		aspect();
	}
	elsif($key eq "Pos1")
	{
		while($iib>0){browse(-1)};
		print refresh(1);
	}
	elsif($key eq "End")
	{
		while($iib<($max-1)){browse(1)};
		print refresh(1);
	}
	elsif($key eq " ")
	{
		editPlayList();
	}
	elsif($key eq "R")
	{
		switchRepeatList();
		#readKey(0.5);
		
	}
	elsif($key eq "r")
	{
		switchRepeatOne();
#		$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";}
		
	}
	elsif($key eq "z")
	{
		switchCompactView();
	}
	elsif($key eq "t")
	{
		switchTagPlaylist();
	}
	elsif($key eq "o")
	{
		$playingInPlaylist{"$plnr"}="$i,$ii,$ik,$playing,$ib,$iib,$ikb,$playingb";
		if($playingInPlaylist{"6"})
		{
			split(/,/,$playingInPlaylist{"6"});
			($i,$ii,$ik,$playing,$ib,$iib,$ikb,$playingb)=@_;
		}
		else{$i=0;$ii=0;$ik=0;$playing=$plbLine;$ib=0;$iib=0;$ikb=0;$playingb=$plbLine;}
		shoutcast();
	}
	elsif($key eq "+")
	{
		syswrite $W, "speed_set $speed\n";
		$speed+=0.01;printInfo("speed=$speed");
		#readKey(1);
	}
	elsif($key eq "-")
	{
		syswrite $W, "speed_set $speed\n";
		$speed-=0.01;printInfo("speed=$speed");
		#readKey(1);
	}
	elsif($key eq "@")
	{
		
		open(TMP,"|(xterm -e streamripper $files[$ii])&");
		close TMP;
		$tmp=$plbLine-1;
		$tmp1="\e[$tmp\H\e[31mstart streamripper\e[0m";
		open(TMP,"|(echo $tmp1;sleep 2;echo $dis)&");
		close TMP;
	}
	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;
	}
	elsif($key eq "Alt_b")
	{
		open(BM,">>$ENV{'HOME'}/.mplay/playlist5");
		print BM $files[$iib];
		close BM;
		printInfo("file added to bookmarks");
	}
	elsif($key eq "Alt_p")
	{
		preferences();
	}
	elsif($key eq 'q' || $key eq "\x03"|| $key eq "Esc")
	{
		quit();
	}
	grepOutput();
	#processOutput();
	if($change>0)
	{
		$change--;
	}
	checkRemote();
	calcTime();
	print getINF().getVolume("$curVolume",9);
}
sub ret
{
		$name='';
		$tmp=kill 0,$mpid;openMplayer("fchange") unless $tmp;
		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(-1);
		}
		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 mp3Workaround();
# 		get_time_length();
		print getMP3info().getPB().getMenuTitle();
}
sub PgDn
{
	for($tmp8=0;$tmp8<=$termsize[1]-$plbLine;$tmp8++)
	{browse(1);}
	print refresh(1);
}
sub PgUp	
{
	for($tmp9=0;$tmp9<=$termsize[1]-$plbLine;$tmp9++)
	{browse(-1);}
	print refresh(1);
}

sub processOutput
{
# 	if($output ne '')
# 	{
# 		if($output=~ /$playText|XMMS/g)
# 		{
# 			#syswrite $W, "seek -1\n" if mp3Workaround();
# 			if(!$change)
# 			{
# 				if($ignoreOutput){$timestat=gettimeofday}
# 				else{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," ";
#	if(!$shortView)
#	{
		$tmp3=$gt-$timeelapsed;
		$tmp=$gt-($gt/100*$gpp);#$tmp3 = int($tmp/60);$tmp1=$tmp%60;
		$tmp4=$tmp/$tmp3;
		if($tmp4 < 0.8||$tmp4 > 1.2&&$gpp<92){$timestat+=($tmp-$tmp3) ;$tmp3=$tmp;}
		$timeremain = sprintf "%3.2d:%02d",$tmp3/60,$tmp3%60;
#	}
	#$timeremain = sprintf "%02d:%02d",$timeremain,$tmp1;
	#$timeremain=int($gt-$timeelapsed)."/$timeremain";
	#$in=$i+1;chomp $in;
}
sub checkRemote
{
	if(-f "$remote")
	{
		remote();
	}
}
sub getProgress
{
	if($shoutcast){$tmp7=sprintf "\e[$termsize[1]\H\e[0;0;$separatorColor\m%0.$termsize[0]\s\e[0m",$pbbar;return $tmp7}
	else
	{
		$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 raiseVolume
{
	if($muted){$unmuteVolume=4;mute();}
	getVolume($curVolume);
	setVolume($curVolume,3);
	print "\e[1m".getVolume($curVolume,9)."\e[0m";
}
sub reduceVolume
{
	@vol = Audio::Mixer::get_cval($curVolume);
	$volume = ($vol[0] + $vol[1]) / 2;
	setVolume($curVolume,-3);
	print "\e[1m".getVolume($curVolume,9)."\e[0m";
}

sub printInfo
{
		if($_[1] eq undef){$st=1;}else{$st=$_[1];}
		$echoPid=999999999 if $echoPid eq '';
		kill 15,$echoPid;
		$tmp=$plbLine-1;
		$tmp7=int($termsize[0]/2 - 10);
		$tmp1="\e[$tmp\H\e[31m\e[$tmp7\G$_[0]\e[0m";
		$echoPid=open(TMP,"|(echo $tmp1;sleep $st;echo $dis)&");
		#system("(echo $tmp1;sleep 1;echo $dis)&");
		#print $echoPid;quit();
		close TMP;

#		$tmp7=int($termsize[0]/2 - 10);
#		$in=$plbLine-1;
#		print getINF()."\e[$infoColor;1\m\e[$in\H\e[$tmp7\G$_[0]";
}
sub getINF
{
	return if $shoutcast;
	$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 == 1){$y='';}else{$y="in $plnrText";}
		return "\e[1\H\e[$tmp1\G\e[0;$clockColor\m\e[0;$trackInfoColor\m\r $_/$max $y \e[2\H\e[$cLine\H\e[0;0m\r" if $shoutcast;
		return getProgress()."\e[1\H\e[$tmp\G\e[0;1;$infoColor\m$timeremain\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 $_/$max $y \e[2\H\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 $plnrText \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();
		if($timeSwitch){($_,$tmp2,$tmp3)=localtime(time);$time=sprintf "%.2d:%.2d",$tmp3,$tmp2;}else{$time=''}
	}
	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
{
	$tmp=kill 0,$mpid;openMplayer("fchange") unless $tmp;
	$fchangeOut='';
	$gpp=0;
		$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)
		{
			$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)
				{
#					$fchangeOut.= "\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;#$fchangeOut.= "\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;
				$fchangeOut.= cleanlist();
				#$tmp4=$ii-$tmp1;
				$fchangeOut .= list($in,$max,$ii);
				
				#$ii+=$tmp1;
				$ii=$i;
				$iib=$ii;
				$ikb=$ik;
				$fchangeOut.= "\e[$playing\H\e[1\m".plainfiles($files[$i],$browserColor,$playBarColor,$ii+1)."\e[0m\e[$cLine\H";
				syswrite $W, "pt_step $_[1] \n" if $_[1];
				$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();print refresh(1);
				syswrite $W, "pt_step $_[1] \n" if $_[1];
				$change=4;
				
			}
			else
			{
				$fchangeOut.= "\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];
					$fchangeOut.= "\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)
				{
					#$fchangeOut.= "\e[$playing\H\e[1m".plainfiles($files[$ii],$playBarColor,$browserColor,$ii+$_[0])."$name\e[0;0m\e[$cLine\H\r";
				}
				else
				{
					$fchangeOut.= "\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.;
		if($shoutcast){return $fchangeOut}
		else{	unless($addfile){print $fchangeOut.getMP3info().getMenuTitle().getPB();sleep(0.08);}}
}
sub browse
{
		$browseOut='';
		$browseOut .= "\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;
					$browseOut .= sprintf "\e[$plbLine\H\e[M\e[$playingb\H\e[1m\e[2K".plainfiles($files[$ib],$playBarColor,$browserColor,$iib+1)."\e[$tmp7\H".plainfiles($files[$iib-$_[0]],0,$playListColor,$iib-$_[0]+1).getProgress();#."\e[0;0m\e[$cLine\H\r";
				}
				elsif($iib-$_[0] == $ii)
				{
					$browseOut .= sprintf "\e[$plbLine\H\e[M\e[$playingb\H\e[0m\e[2K".plainfiles($files[$ib],$playListColor,$browserColor,$iib+1)."\e[0;0;0m\e[$tmp7\H".plainfiles($files[$ib-$_[0]],1,$playBarColor,$iib-$_[0]+1).getProgress();#."\e[0;0m\e[$cLine\H\r";
				}
				else
				{
					$browseOut .= sprintf "\e[$plbLine\H\e[M\e[$playingb\H\e[0m\e[2K".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("$curVolume",9);
				$ikb=0;
				#print cleanlist();
				$tmp7=$playingb-$_[0];
				#$changb=1;
				$playingb=$plbLine;
#				$inf=getINF();
				if($ii == $iib)
				{
					$changb=0;$playing=$plbLine-1;$ik=0;
					$browseOut .=sprintf "\e[$plbLine\H\eM\e[$playingb\H\e[0;0;1m\e[2K".plainfiles($files[$iib],$playBarColor,$browserColor,$iib+1)."\e[$tmp7\H".plainfiles($files[$iib-$_[0]],0,$playListColor,$iib-$_[0]+1).getProgress();#."\e[0;0m\e[$cLine\H\r";
				}
				elsif($iib-$_[0] == $ii)
				{
					$browseOut .= sprintf "\e[$plbLine\H\eM\e[$playingb\H\e[0;0;0m\e[2K".plainfiles($files[$iib],$browserColor,$playListColor,$iib+1)."\e[0;0;0m\e[$tmp7\H".plainfiles($files[$iib-$_[0]],1,$playBarColor,$iib-$_[0]+1).getProgress();#."\e[0;0m\e[$cLine\H\r";
				}
				else
				{
					$browseOut .= sprintf "\e[$plbLine\H\eM\e[$playingb\H\e[0m\e[2K".plainfiles($files[$iib],$playListColor,$browserColor,$iib+1)."\e[$tmp7\H".plainfiles($files[$iib-$_[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])
				{
					$browseOut .= "\e[$playingb\H".plainfiles($files[$iib-$_[0]],1,$playBarColor,$iib-$_[0]+1)."\e[0m\e[$cLine\H";
				}
				else
				{							
					$browseOut .= "\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])
				{
					$browseOut .= sprintf "\e\H\e[$playingb\H\e[1\m".plainfiles($files[$iib],$playBarColor,$browserColor,$iib+1)."\e[$cLine\H\e[0;0m\r";
				}
				else
				{
					$browseOut .= sprintf "\e[$playingb\H\e[0m".plainfiles($files[$iib],$playListColor,$browserColor,$iib+1)."\e[$cLine\H\e[0;0m\r";
				}
				
			}
		}
		$browseOut .=  "\e[r";
		return $browseOut;
}
#sub mp3Workaround
#{
#	if(!$noWorkaround && $filesc[$ii] =~ /mp3$/)
#	{return 1}else{return 0}
#}
sub getMP3info
{
	return if $shoutcast;
	$filec = $filesc[$ii];
	chomp $filec;
	$filec =~ /\.(.{2,4})$/;$ves=$1;
	if($filec =~ /^http\:\/\//)
	{
		#print getINF().getVolume("$curVolume",9);
#		$name=1;
		if($name eq '')
		{
			$title='';$genre='';$bitrate='';
			#print getHttpInfo("....");
			#printInfo("reading info...");
			$test2=1;
			pipe HOUT,HIN;
			$httpChildPid=fork();
			return if($httpChildPid !=0);
			
			@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\:(.*)/) 
						{
							$bitrate=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);
			$tmp1="$title ++ $name ++ $genre ++ $bitrate ++ $ip ++ $port";
			$tmp6=sprintf "%04d",length($tmp1);
			#print HIN "$title##$name##$genre##$bitrate##$tmp2\n";
			print HIN "$tmp6$tmp1 ";
			open(R,">$remote");
			print R "t";
			close R;
			exit;
		}
		else
		{
			return getHttpInfo();
		}
		#$tmp1=getHttpInfo();
		#print getINF().getPB();
		#$tmp3="title=$title name=$name genre=$genre bitrate=$bitrate PP";
#		$tmp6=sprintf "%04d",length($tmp1);
		#print HIN "$title##$name##$genre##$bitrate##$tmp2\n";
#		print HIN "$tmp6$tmp1";
	}
	
	elsif($filec =~ /\.mp3$/i && -f $filec)
	{
		$info = get_mp3info($filec);
		$tag=get_mp3tag($filec,$mp3Version);
		$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();

	}
	elsif($ves !~ /mpg/i && $videoExtensions =~ /$ves/i && -f $filec)
	{
		$video=Video::Info->new(-file=>$filec);
		if (defined $video)
		{
			$filec =~ /^.*\/(.*?)\.[a-zA-Z]{2,4}$/;
			if($shortView)
			{
				$tmp5=$termsize[1]+1;
				$tmp=$termsize[0];
				$tmp1=sprintf "\r\e[0;$mp3TagColor\m\r\e[$tmp5\H%0.$tmp\s",$1;
			}
			else
			{
				$tmp=$termsize[0]-9;
				$tmp1=sprintf cleanmp3info()."\r\e[0;$mp3TagColor\m\r\e[1H Title:  %0.$tmp\s\n Codec:  %s\nAspect:  %sx%s",$1,$video->vcodec(),$video->width,$video->height();
			}
		}
	}
	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 getHttpInfo
{
		$bitrate =~ s/ //g;
		#$tmp2="$ip$\:$port$path # $name";
		if($_[0]){$title="$_[0]"}
		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", $bitrate;
		}
		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 kbps     %s\e[0;0m", $title, $name, $genre, $bitrate;
		}
		if($filesc[$ii] !~ /#/ && $_[0] eq '')
		{
			$tmp=$name;
			chomp $filesc[$ii];
			$tmp =~ s/[\(\)\-\[\]\<\>\{\}\.\|\^\$\*\+\?\&\\]/\\$&/g;
			$filesc[$ii]="$filesc[$ii] # $tmp\n";
			@filesa=@filesc;@files=@filesc;
		}
		return $tmp1;
}
sub getVolume
{
	@vol = Audio::Mixer::get_cval($_[0]);
	$volume = ($vol[0] + $vol[1]) / 2;
	return $volume if $_[1] eq "noPrint";
	$volume = "" if $volume > 101;
	$volume=sprintf "%3s",$volume;
	if($shortView)
	{
		$tmp6=$termsize[0]-40;
		#$tmp5=$tmp6-35;
		$tmp2=sprintf "\e[$volumeColor\m\e[1\H\e[$tmp6\G$volume% \e[0;0m\e[$cLine\H\r";
	}
	else
	{
		$tmp2=(28)/100*$volume;
		$tmp1=substr $volbar,0,$tmp2;
		$tmp2=sprintf "\e[0;0;$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 mute
{
		if($shortView){$tmp5=1;}else{$tmp5=$cLine;}
		$tmp6=$termsize[0]-46;
		if($muted == 0)
		{
			$unmuteVolume=$volume;
			setVolume($curVolume,-$volume);
			print "\e[$tmp5\H\e[5;31m\e[$tmp6\G[mute]\e[0m\r";
			$muted=1;
		}
		else
		{
			setVolume($curVolume,$unmuteVolume);
			print "\e[$tmp5\H\e[0m\e[$tmp6\G      \r";
			$muted=0;
		}

}
sub mixer
{
	$save=$shortView;
#	@termsize=GetTerminalSize();
	@tmp1=();
	@tmp=Audio::Mixer::get_mixer_params();
	for(@tmp){$tmp=getVolume($_,"noPrint");push(@tmp1,"$_") unless $tmp > 100}
	$toChange=0;
	$shortView=$save;
	$shortView=0;
	$tmp6=$termsize[1]-4;
	$tmp5=$termsize[1]-5;
	while(1)
	{
		$tmp7=$plbLine-1;
		$out='';for($in=0;$in<scalar(@tmp1);$in++){$out.="\e[$tmp7"."H"."\e[47;34;1m                   Mixer                      "."\e[40;1m". getVolume("$tmp1[$in]",($plbLine+$in));};$out.="\e[42;1m".getVolume("$tmp1[$toChange]",$plbLine+$toChange)."\e[0m";print $out;
		grepOutput();
#		if($output ne '')
#		{
#			processOutput();
#		}
		#processOutput();
		#check();
		calcTime();
		print getINF().getVolume("$curVolume",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++;
			$toChange=$toChange % scalar(@tmp1);
		}
		elsif($key eq 'Up')
		{
			$toChange--;
			$toChange=$toChange % scalar(@tmp1);
		}		
		if($key eq 'Right')
		{
			getVolume("$tmp1[$toChange]");
			setVolume("$tmp1[$toChange]",2);
		}
		elsif($key eq 'Left')
		{
			getVolume("$tmp1[$toChange]");
			setVolume("$tmp1[$toChange]",-2);
		}
		elsif($key eq "\n"){$curVolume="$tmp1[$toChange]"}
	}
	print getMenuTitle();
	
}
sub shoutcast
{
	$save=$shortView;
	switchCompactView() unless $shortView;
	$SIG{PIPE} = 'IGNORE';
	#$shoutcastGenre="";
#	$shoutcastGenreBegin=0;
	@tmp2=@files;
	if(scalar(@streams))
	{
		$shoutcast=1;
		printShoutcast();
	}
	else
	{
		doShoutcast();
	}
	while(1)
	{
			if(&termSizeChanged)
		{
			printShoutcast();
		}

		$key=readKey(0.25);
		print getVolume($curVolume,1);
		if($key eq "a")
		{
			$key=menu();
		}
		if($key eq "Up")
		{
			print browse(-1);
		}
		elsif($key eq "Down")
		{
			print browse(1);
		}
		elsif($key eq "Right")
		{
			;
		}
		elsif($key eq "Left")
		{
			;
		}
		elsif($key eq "PgUp")
		{
			PgUp();
		}
		elsif($key eq "PgDn")
		{
			PgDn();
		}
		elsif($key eq ".")
		{
			raiseVolume();
		}
		elsif($key eq ",")
		{
			reduceVolume();
		}
		elsif($key eq "\x06")
		{
			search();printShoutcast();
			if($key eq "\n"){retShoutcast();}
			
		}
		elsif($key eq "\x12")
		{
			doShoutcast();
		}
		elsif($key eq "m")
		{
			mute();
		}
		elsif($key eq "r")
		{
			$tmp=kill 0,$mpid;$tmp1="http://www.shoutcast.com$pls[$iib]";
			$mpid=open3($W, $R, $E, "mplayer $mplayerOptions $tmp1");
		}
		elsif($key eq "\n")
		{
			retShoutcast();
		}
		elsif($key eq "e")
		{
			print "\e[2JTarget: http://www.shoutcast.com$pls[$iib]\n";
			readKey();
			printShoutcast();
		}
		elsif($key eq "\t")
		{
			changeGenre();
		}
		elsif($key eq "d")
		{
			mplayerEqualizer();
		}
		elsif($key eq "x")
		{
			mixer();
		}
		elsif($key eq "c")
		{
			color();
		}
		elsif($key eq "s")
		{
			sortShoutcast()
		}
		elsif($key eq "Alt_p")
		{
			preferences()
		}
		elsif($key eq "h")
		{
			help(help_shoutcast);
			printShoutcast();
		}
		elsif($key eq "q")
		{
			@files=@tmp2;@filesc=@tmp2;
			$max=@files;
			quit();
			#print refresh();
			#last;;
		}
		elsif($key eq "o")
		{
			$playingInPlaylist{"6"}="$i,$ii,$ik,$playing,$ib,$iib,$ikb,$playingb";
			split(/,/,$playingInPlaylist{"$plnr"});
			($i,$ii,$ik,$playing,$ib,$iib,$ikb,$playingb)=@_;
			$key='';
			@files=@tmp2;@filesc=@tmp2;
			$max=@files;
			$shoutcast=0;
			$actuallist=1;
			switchCompactView() unless $save;
			resetList();
			print refresh();
			last;;
		}
	}
}
sub retShoutcast
{
	$sTmp=$iib-$i;$shoutcastOut='';
	if($sTmp<0){$sTmp*=-1;$sTmp2=-1}else{$sTmp2=1;}for($sTmp1=0;$sTmp1<$sTmp;$sTmp1++){fchange($sTmp2);}
	resetList();
	$tmp=kill 0,$mpid;openMplayer() unless $tmp;
	$tmp1="http://www.shoutcast.com$pls[$iib]";
	clearbuffer(-1);
	syswrite $W, "loadlist $tmp1\n";
	clearbuffer(0.1);
	printShoutcast();
}
sub changeGenre
{
	$curShoutSort='';
	$subSel=1;
	return unless(scalar(@shoutcastGenre));
	while(1)
	{
	
#		print "\e[$tmp2\H\e[7\m $genre[$shoutcastGenreBegin]";
		$sel=$sel%(scalar(@shoutcastGenre));
		$subSel=$subSel % scalar(@{$shoutcastGenre[$sel]});
		#if($shoutcastGenreBegin ne ''){$shoutcastGenreBegin=$shoutcastGenreBegin % scalar(@shoutcastGenre);}
		$tmp2=1;
		#$in=0;
		#printf "\e[$tmp2\H \e[1;30;47\m%-13s\e[0;0m",$tmp[$in];$tmp2--;
		$Print='';
		for($in=1;$in<16;$in++){printf "\e[$in\H\e[17G\e[0;0;0\m%+15s",""};
		for($in=0;$in<scalar(@shoutcastGenre);$in++)
		{
			$Print.=sprintf "\e[$tmp2\H \e[0;30;47\m%-15s\e[0;0m",$shoutcastGenre[$in][0];
			$tmp2++;
		}
		$tmp2=$sel+1;
		if($tmp2 - 2 > (scalar(@shoutcastGenre) - scalar(@{$shoutcastGenre[$sel]})))
		{$tmp2=(scalar(@shoutcastGenre) - scalar(@{$shoutcastGenre[$sel]})) + 2 }
		$subGenreLine=$tmp2-1;
		for($in=1;$in<scalar(@{$shoutcastGenre[$sel]});$in++)
		{
			$Print.=sprintf "\e[$tmp2\H\e[17\G\e[0;30;47\m%-15s\e[0;0m",$shoutcastGenre[$sel][$in];
			$tmp2++;
		}
		if($subGenre){$tmp3=$subSel+$subGenreLine;$Print.=sprintf "\e[$tmp3\H\e[17\G\e[0;33;47\m\e[7\m%-14s\e[0m",$shoutcastGenre[$sel][$subSel];}
		else{$tmp3=$sel+1;$Print.=sprintf "\e[$tmp3\H \e[0;33;47\m\e[7\m%+13s >\e[0m",$shoutcastGenre[$sel][0];}
		print $Print;
		$key=readKey();
		#printShoutcast();
		if($key eq "\t" || $key eq "q"|| $key eq "Esc")
		{
			printShoutcast();
			return '';
		}
		elsif($key eq "\n")
		{
			if($shoutcastGenre[$sel][$subSel] eq "Search")
			{
				print "\e[$cLine\H\e[20\G\e[1;32\m Enter search string: \e[31m";
				$expression='';
				while($key ne "Esc")
				{
					$key=readKey();
					if($key eq "Esc")
					{
						printShoutcast();
						return;
					}
					elsif($key eq "Backspace")
					{
						chop $expression;
					}
					elsif($key eq "\n")
					{
						last;
					}
					elsif($key =~ /[a-zA-Z]{2,5}/){;}
					else
					{
						$expression.=$key;
					}
					print "\e[$cLine\H\e[20\G\e[1;32\m Enter search string: \e[0\K$expression";
					$expression =~ s/[\(\)\-\[\]\<\>\{\}\.\|\^\$\*\+\?\&\\]/\\$&/g;
				}
			}
			printShoutcast();
			$curGenre="$shoutcastGenre[$sel][0]\:\:$shoutcastGenre[$sel][$subSel]";
			$i=0;$ii=0;$ik=0;$playing=$plbLine;$ib=0;$iib=0;$ikb=0;$playingb=$plbLine;
			if($subGenre){$shoutcastGenre=$shoutcastGenre[$sel][$subSel];}else{$shoutcastGenre=$shoutcastGenre[$sel][0];}
			$subGenre=0;
			%hash=();
			#while($iib>0){browse(-1)};
			doShoutcast();
			return;
		}
		elsif($key eq 'Down')
		{
			if($subGenre){$subSel++;}else{$sel++;}
		}
		elsif($key eq 'Up')
		{
			if($subGenre){$subSel--;}else{$sel--;}
		}
		elsif($key eq 'Right')
		{
			$subSel=1;$subGenre=1;
		}
		elsif($key eq 'Left')
		{
			$subGenre=0;
		}
		#print "\e[$plbLine\H";
		#printf "\e[$tmp2\H\e[32;47\m %-15s",$tmp[$in];
	#	$tmp2='';
	#	for($in=0;$in<scalar(@tmp);$in++)
	#	{
	#		$tmp2.=sprintf "\e[7m%17s  \e[0m  \n",$tmp[$in];
		#}
		#$tmp=$plbLine+$toChangeMenu+1;
		#printf "%-17s  \e[0;0\m\n$tmp2\e[2\G%20s\e[$tmp\H\e[1;33;40\m%17s  \e[0;0;0m\n","$mTitle"," ",$tmp[$toChangeMenu];
		#sleep 2;
	#}
	}
	print getMenuTitle();
	
}
sub doShoutcast
{
	$shoutcast=1;
	$startat=0;
	$ip="www.shoutcast.com";
	$port="80";
	print "\e[$termsize[1]\H\e[5;44m\e[2Kreceiving\e[0;44m streams from genre: $shoutcastGenre ";
	$loop=1;
	@content=request();
	@streams=();@pls=();@shoutcastGenre=();
	$done=0;
	for($in=0;$in<scalar(@content);$in++)
	{
		if($content[$in]=~ /(\/sbin\/shout.*filename\.pls)/){$pls=$1;}
		if($content[$in]=~ /scurl.*\"\>(.*?)\</){$rs=$1;}
		
		if($content[$in]=~ /\>([0-9]{2,3})\<\/font\>\<\/td\>/){$br=$1;}
		if($content[$in]=~ /.*Now\ Playing\:\<\/font\> (.*?)\</){$nowPlaying=$1;}
		if($content[$in]=~ /\-\=\[Top 25 Streams\]\=\-/ && $done == 0)
		{
			#push(@shoutcastGenre,"Top25\@\@");
			$c1=0;$c2=0;
			$shoutcastGenre[0][0]="Top25";
			$in++;
			while($content[$in]=~ /\<OPTION VALUE\=\"(.*)\"\>(.*)$/)
			{
				$tmp1=$1;
				$tmp1=~ s/\%20/ /g;$tmp1=~ s/\%26/\&/g;
				#if($2 !~ / \-.*/){push(@shoutcastGenre,"$tmp\n")if $tmp ne '';$tmp=$tmp1;}
				if($2 !~ / \-.*/){$c1++;$c2=0;$shoutcastGenre[$c1][$c2]="$tmp1";}
				else{$c2++;$shoutcastGenre[$c1][$c2]=$tmp1;}
				$in++;
			}
			$shoutcastGenre[$c1+1][0]="Search";
			#print "\n".scalar(@shoutcastGenre);
			#for(@shoutcastGenre){print "\n".scalar(@$_)." ";for(@$_){print "$_ - "}};
			#$ic=0;
			#for($cc=0;$cc<14;$cc++){print $shoutcastGenre[$cc][0]};
			#quit();
			$done=1;
		}
		unless($rs eq ''||$pls eq ''||$br eq ''){$loop=0 if defined $hash{"$rs \@\@ $nowPlaying \@\@ $br"};$hash{"$rs \@\@ $nowPlaying \@\@ $br \@\@$pls"}="$pls"; $rs='';$br='';}
	}
	while(($tmp1,$tmp2) = each(%hash))
	{
		$tmp1 =~ /(.*\@\@.*\@\@.*)\@\@/;
		$tmp1=$1;
		push(@streams,"$tmp1");
		push(@pls,"$tmp2");
	}
	#@streams=sort(@streams);
	#$sortShoutcast=1;
	sortShoutcast($sortShoutcast);
	#printShoutcast();
}
sub sortShoutcast
{
	while(1)
	{
		$tmp=int($shoutcastNameRow/2)-4;$tmp1=$shoutcastNameRow + int(($shoutcastBitrateRow-$shoutcastNameRow)/2)-7;$tmp4=$shoutcastBitrateRow;
		print  "\e[0;0;0m\e[2\H$dis"."\e[2H\e[$tmp\G\e[0;41;37\m1\e[0;7;$separatorColor\mname\e[0;41;37\m|\e[$tmp1\G2\e[0;7;$separatorColor\mnow playing\e[0;41;37\m|\e[$tmp4\G3\e[0;7;$separatorColor\mkbps\e[0;41;37\m|";
		if(scalar(@_)){$sortShoutcast=$_[0];}else{$sortShoutcast=readKey();}
		if($sortShoutcast eq "1")
		{
			if($curShoutSort eq "name"){@tmp=sort {uc($b) cmp uc($a)} keys %hash;@streams=();@pls=();$curShoutSort="nameDesc";}
			else{	@tmp=sort {uc($a) cmp uc($b)} keys %hash;@streams=();@pls=();$curShoutSort="name";}
			for(@tmp)
			{
				$_ =~ /(.*\@\@.*\@\@.*\@\@)(.*)$/;
				push(@streams,"$1");
				push(@pls,"$2");
			}
			printShoutcast();return;
		}
		elsif($sortShoutcast eq "2")
		{
			@tmp=keys %hash;@streams=();@pls=();
			for(@tmp)
			{
				$_ =~ /(.*\@\@)(.*\@\@)(.*\@\@)(.*)$/;
				push(@streams,"$2$1$3$4");
			}
			if($curShoutSort eq "playing"){@tmp=sort {uc($b) cmp uc($a)} (@streams);$curShoutSort="playingDesc";}
			else{@tmp=sort {uc($a) cmp uc($b)} (@streams);$curShoutSort="playing";}
			@streams=();
			for(@tmp)
			{
				$_ =~ /(.*\@\@)(.*\@\@)(.*\@\@)(.*)$/;
				push(@streams,"$2$1$3");
				push(@pls,"$4");
			}
			printShoutcast();return;
			
		}
		elsif($sortShoutcast eq "3")
		{
			@tmp=keys %hash;@streams=();@pls=();
			for(@tmp)
			{
				$_ =~ /(.*\@\@)(.*\@\@)(.*\@\@)(.*)$/;
				push(@streams,"$3$2$1$4");
			}
			if($curShoutSort eq "kbps"){@tmp=sort {uc($a) <=> uc($b)} (@streams);$curShoutSort="kbpsAsc";}
			else{@tmp=sort {uc($b) <=> uc($a)} (@streams);$curShoutSort="kbps";}
			@streams=();
			for(@tmp)
			{
				$_ =~ /(.*\@\@)(.*\@\@)(.*\@\@)(.*)$/;
				push(@streams,"$3$2$1");
				push(@pls,"$4");
			}
			printShoutcast();return;
		}
		else{printShoutcast();return}

	}
}
sub printShoutcast
{
	$shoutcastGenreBar='';
	$tmp3=scalar(@shoutcastGenre);
	@files=@streams;@filesc=@streams;
	$max=@files;
	$shoutcastOut = refresh("shoutcast");$tmp=int($shoutcastNameRow/2)-4;$tmp1=$shoutcastNameRow + int(($shoutcastBitrateRow-$shoutcastNameRow)/2)-7;$tmp4=$shoutcastBitrateRow;
	$shoutcastOut .=  "\e[1H\e[0;0;$infoColor\m$curGenre\e[0;0;0m\e[2\H$dis"."\e[2H\e[$tmp\G\e[0;0;$separatorColor\m|\e[0;4;$separatorColor\mname\e[0;0;$separatorColor\m|\e[$tmp1\G|\e[0;4;$separatorColor\mnow playing\e[0;0;$separatorColor\m|\e[$tmp4\G|\e[0;4;$separatorColor\mkbps\e[0;0;$separatorColor\m|".shoutcastMP3();
	$shoutcastOut .= sprintf "\e[$termsize[1]\H\e[0;0;$separatorColor\m%0.$termsize[0]\s\e[0m",$pbbar;
	$shoutcastOut .= getPB();
	print $shoutcastOut;
	;
	#print list($in,$max,$iia);
	#$i=0;$ii=0;$ik=0;$playing=$plbLine;$ib=0;$iib=0;$ikb=0;$playingb=$plbLine;
}
sub shoutcastMP3
{
	$tmp1=$termsize[1]+1;
	$tmp4=$termsize[0] - 4;
	@tmp1=split(/\@\@/,$filesc[$ii]);
	$tmp2= "\e[$tmp4\G$tmp1[2]";
	$tmp7= sprintf "\e[0;$mp3TagColor\m\e[$tmp1\H\e[2K\r%0.$tmp4\s$tmp2","$tmp1[0] - $tmp1[1]";
	return $tmp7;
}
sub request
{
	$more=1;
	$out='';
	while($more)
	{
		if($shoutcastGenre[$sel][$subSel] eq "Search")
		{
			$expression =~ s/ /\+/g;
			$path="\/directory\/\?s\=$expression\&numresult\=100\&startat\=$startat";
		}
		else
		{
			#$plnrText=$shoutcastGenre;
			$shoutcastGenre =~ s/ /\%20/g;$shoutcastGenre =~ s/\&/\%26/g;
			$path="\/directory\/\?sgenre\=$shoutcastGenre\&numresult\=100\&startat\=$startat";
		}
		$curGenre=$shoutcastGenre unless $curGenre;
		quote($curGenre);
# 		#print "\e[5m..";
		#$path='/directory/index.phtml?startat=100';
		$socket = IO::Socket::INET->new(PeerAddr=> $ip,PeerPort=> $port,Type=> SOCK_STREAM,Proto=> 'tcp',);
		if(defined $socket){print $socket "GET $path HTTP/1.0\r\n\r\n";}else{return;}
		while(1)
		{
			$socket->recv($tmp, 8192);
			last unless $tmp;
			$tmp1.= $tmp;
		}
		#close $socket;
		$out.= $tmp1;
		unless($tmp1 =~ /startat.*\[ Next \]/g){$more=0;}
		$tmp1='';
		$startat+=100;
	}
	close $socket;
	return split(/\n/,$out);
}
sub menu
{
	if($shoutcast){setMenuShoutcast();}else{if($setMenuRight){setMenuRight();}else{setMenuLeft();}}
	while(1)
	{
		grepOutput();
# 		processOutput();
		#check();
		calcTime();
		print getINF().getVolume("$curVolume",9);
#		$tmp7=$termsize[1]-3;
		if($key eq 'a' || $key eq 'q' || $key eq "Esc")
		{
			#print "\e[2\H\e[2\K".getINF();print refresh(1)if $eq[$toChangeMenu]> -12;
			print refresh(1);
			return '';
		}
		elsif($key eq "\n")
		{
			print refresh(1);
			return $tmp[$toChangeMenu];
		}
		elsif($key eq 'Down')
		{
			$toChangeMenu++;
			if($toChangeMenu>scalar(@tmp1)-1)
			{
				$toChangeMenu=0;
			}
		}
		elsif($key eq 'Up')
		{
			$toChangeMenu--;
			if($toChangeMenu<0)
			{
				$toChangeMenu=scalar(@tmp1)-1;
			}
		}
		elsif($key eq 'Right')
		{
			$setMenuRight=1;
			#print refresh(1);
			setMenuRight();
		}
		elsif($key eq 'Left')
		{
			$setMenuRight=0;
			#print refresh(1);
			setMenuLeft();
		}
		print "\e[$plbLine\H";
		$tmp2='';
		for($in=0;$in<scalar(@tmp1);$in++)
		{
			$tmp2.=sprintf "\e[7m%17s  \e[0m  \n",$tmp1[$in];
		}
		$tmp=$plbLine+$toChangeMenu+1;
		printf "%-17s  \e[0;0\m\n$tmp2\e[2\G%20s\e[$tmp\H\e[1;33;40\m%17s  \e[0;0;0m\n","$mTitle"," ",$tmp1[$toChangeMenu];
		#sleep 2;
	}
	print getMenuTitle();
	
}
sub setMenuLeft
{
	return if $shoutcast;
	$mTitle="\e[1;32;44\m   Menu\e[0;0;44\m     Tools";
	@tmp1=("file info","add files","search","internet radio","fullscreen(video)","save playlist","help","quit");
	@tmp=("e","\t","\x06","o","f","\x13","h","q");
}
sub setMenuRight
{
	return if $shoutcast;
	$mTitle="\e[0;0;44m   Menu     \e[1;32;44\mTools\e[0;44\m";
	@tmp1=("add to bookmarks","sort playlist","change colors","mixer","equalizer","remove file","preferences","clear playlist");
	@tmp=("Alt_b","s","c","x","d","Del","Alt_p","Alt_Del");
}
sub setMenuShoutcast
{
	$mTitle="\e[0;0;44m   Menu          \e[0;44\m";
	@tmp1=("change genre","sort","mixer","search","equalizer","change colors","leave shoutcast","help","quit");
	@tmp=("\t","s","x","\x06","d","c","o","h","q");
}
sub prefOnOff
{
	if($_[0]){return "\e[23\G\e[0;30;42\m ON  \e[0;0;0\m"}else{return "\e[23\G\e[0;30;41\m OFF \e[0;0;0\m"}
}
sub prefVal
{
	if($shoutcast)
	{
		@tmp=("cache","normalize","extrastereo");
		@tmp1=("\e[23\G$cache",prefOnOff($normalize),prefOnOff($extraStereo));
	}
	else
	{
		@tmp=("cache","normalize","extrastereo","show tag in pls","tag definition","compact mode","show pls numbers","repeat track","repeat list","clock visible");
	
		@tmp1=("\e[23\G$cache ",prefOnOff($normalize),prefOnOff($extraStereo),prefOnOff($showTagPlaylist), "",prefOnOff($shortView),prefOnOff($nr),prefOnOff($repeatOne),prefOnOff($repeat),prefOnOff($timeSwitch));
	}
}
sub preferences
{
	#"$pbarColor:$mp3TagColor:$infoColor:$trackInfoColor:$volumeColor:$playBarColor:$browserColor:$playListColor:$clockColor:$shortView:$nr:$repeat:$dirname:$equalizer:$eq:$shoutcast:$shoutcastGenre:$sel:$showTagPlaylist:$curVolume:$sortShoutcast";
	$saveCache=$cache;
	$toChangePref=0;
	$prefOut='';

	$prefColor="\e[0;0;42m                   \e[0;0;44m           \e[0;30;42m   \r";
	prefVal();
	while(1)
	{
		$prefOut .= "\e[$plbLine\H\e[1;34;47m         Preferences             \n";
		for($in=0;$in<scalar(@tmp);$in++){$prefOut.= "$prefColor $tmp[$in]$tmp1[$in]\n";}
		$toChangePref=$toChangePref % scalar(@tmp);
		$tmp=($plbLine + $toChangePref + 1);
		$prefOut .= "\e[0;31;42m\e[$tmp\H $tmp[$toChangePref]";
		print $prefOut;
		grepOutput();
		calcTime();$prefOut = getINF().getVolume($curVolume,9);
#		print $prefOut;
		if($key eq "q" || $key eq "Esc")
		{
			if($cache != $saveCache){changeCache()}
			if($shoutcast){printShoutcast()}else{print refresh();}return;
		}
		elsif($key eq "Up"){$toChangePref--}
		elsif($key eq "Down"){$toChangePref++}
		elsif($key eq "\x20")
		{
			if($toChangePref == 1){switchSoundEffect($normalize,"normalize")}
			elsif($toChangePref == 2){switchSoundEffect($extraStereo,"extrastereo")}
			elsif($toChangePref == 3){switchTagPlaylist();}
			elsif($toChangePref == 5){switchCompactView();}
			elsif($toChangePref == 6){if($nr){$nr=0}else{$nr=1}print refresh(1);}
			elsif($toChangePref == 7){switchRepeatOne();}
			elsif($toChangePref == 8){switchRepeatList();}
			elsif($toChangePref == 9){switchClock()}
			prefVal();
		}
		if($toChangePref == 0)
		{
			if($key eq "Left"){$cache-- if $cache > 4}
			elsif($key eq "Right"){$cache++ if $cache < 4096}
			elsif($key eq "PgDn"){$cache-=32 if $cache > 4}
			elsif($key eq "PgUp"){$cache+=32 if $cache < 4096}
			prefVal();
		}
	}
	readKey();
	print refresh();
}
sub mplayerEqualizer
{
	$pp=":";
	@tmp1=(" 31 Hz $pp"," 62 Hz $pp","125 Hz $pp","250 Hz $pp","500 Hz $pp"," 1 kHz $pp"," 2 kHz $pp"," 4 kHz $pp"," 8 kHz $pp","16 kHz $pp","preAmp ");
	getMEQ();	
	print "$ppt";
	while(1)
	{
		grepOutput();
# 		processOutput();
		#check();
		calcTime();
		print getINF().getVolume($curVolume,9);
#		$tmp7=$termsize[1]-3;
		if($key eq 'd' || $key eq 'q' || $key eq "Esc")
		{
			if($shoutcast){printShoutcast();}
			else{print "\e[2\H\e[2\K".getINF().print refresh(1)if $eq[$toChangeEq]> -12;}
			last;
		}
		if($key eq 'r'){restart();}
		if($key eq 'Right')
		{
			if($toChangeEq==10)
			{$eq[$toChangeEq]++if $eq[$toChangeEq] < 60;}
			else{$eq[$toChangeEq]++ if $eq[$toChangeEq] < 12;}
			#restart();$key='';
#			$remRestart=1;
		}		
		elsif($key eq 'Left')
		{
			if($toChangeEq==10)
			{$eq[$toChangeEq]--if $eq[$toChangeEq]> -200;}
			else{$eq[$toChangeEq]-- if $eq[$toChangeEq]> -12;}
			#restart();$key='';
#			$remRestart=1;
		}		
		elsif($key eq 'Down')
		{
			$toChangeEq++;
			if($toChangeEq>10)
			{
				$toChangeEq=0;
			}
		}
		elsif($key eq 'Up')
		{
			$toChangeEq--;
			if($toChangeEq<0)
			{
				$toChangeEq=10;
			}
		}		
		elsif($key eq "\x20"||$key eq "\n"||$key eq "\t"){if($eq){$eq=0}else{$eq=1}$remRestart=1;}
		$line=$toChangeEq+$plbLine;
		if($toChangeEq == 10){$row='';}else{$row=21+$eq[$toChangeEq];$row="\e[$row\GX";}
		getMEQ();
		$tmp2=sprintf "%4d",$eq[$toChangeEq];
	 	print "$ppt\e[$line\H \e[32;7\m$tmp1[$toChangeEq]$tmp2$row\e[0;0m";
		if($remRestart){restart();$remRestart=0;}
	}
# 	print getMenuTitle();
	
}
sub getMEQ
{
	if($eq){$eqStat="\e[32m  ON  ";}else{$eqStat="\e[31m  OFF ";}
	$tmp7=$plbLine-1;$ppt="\e[$tmp7\H\e[7m         Mplayer euqalizer       $eqStat\e[0m\n";
	$eq[10]=22 if $eq[10] eq '';
	for($in=0;$in<11;$in++)
	{
		$eq[$in]=0 if $eq[$in] eq '';
		$tmp=21+$eq[$in];
		$tmp2=sprintf "%3d",$eq[$in];
		$tmp3=sprintf "%4d",$eq[$in];
		if($in == 10){$ppt.=" $tmp1[$in]$tmp3     (-200 - +60 db)        \n";}
		else{$ppt.=" $tmp1[$in] $tmp2  \e[$tmp\GX\n";}
	}
	$ppt.="\e[1;30mPress 'r' to take effect >rest. Mplayer. Space to switch on/off\e[0;0m";
}
sub correct
{
	$name='';
	$tmp1=$curTrack;
	#print "\nTMP1: $curTrack\n";quit();
	if($tmp1 =~ /^$playText.*/g)
	{
		@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();
					print refresh(1);
					#printInfo("repeat-one-activeted");
				}
			}
		}
	}
	print getMenuTitle();$output='';
}
sub refresh
{
	$refreshOut='';
	@termsize2=@termsize;
	$tmp6=$termsize[0];
	$tmp=$plbLine-1;
	if($shortView)
	{
		#$tmp6-=14;
		$pbbart=sprintf "%0.$tmp6\s",$pbbar;
		#$tmp6+=9;
		$dis=sprintf "\e[$tmp\H\e[$separatorColor\m%0.$tmp6\s\e[0m",$pbbar;
	}
	else
	{
		$pbbart=sprintf "%0.$termsize[0]\s",$pbbar;
		$dis=sprintf "\e[$tmp\H\e[$separatorColor\m%0.$tmp6\s\e[0m",$pbbar;
	}
	if($_[0] eq '1')
	{
		$refreshOut .= cleanlist();
	}
	elsif($_[0] eq '2')
	{
		$refreshOut .= cleanlist();
	}
	else
	{
		$refreshOut .= clearscreen();
	}
	$refreshOut .= "$dis\e[$plbLine\H" unless $shoutcast;
	if($_[0] eq 'addFile')
	{addfile();return;}
	elsif($_[0] eq 'help')
	{help();return;}
	$iia=$iib-($playingb-$plbLine);
	$refreshOut .= list($in,$max,$iia);
	$ik=$playing-$plbLine;
	if($_[0] eq 'shoutcast')
	{
		if($ii == $iib)
		{
			$refreshOut .=  "\e[2\H$dis\e[$termsize[1]\H$dis\e[$cLine\H".getPB().getVolume($curVolume,2).getMenuTitle()."\e[$playingb\H\e[1\m".plainfiles($files[$iib],$playBarColor,$browserColor,$iib+1);
		}
		else
		{
			$refreshOut .=  "\e[2\H$dis\e[$termsize[1]\H$dis\e[$playingb\H".plainfiles($files[$iib],$playListColor,$browserColor,$iib+1)."\e[$cLine\H".getPB()..getVolume($curVolume,2).getMenuTitle();
		}
		return $refreshOut;
	}
#	if ($changb)
#	{
#		$ikb=$ik;$ib=$i;$iib=$ii;
#		$playingb=$playing;
#		$changb=0;
#	}
	if($ii == $iib)
	{
		$refreshOut .= "\e[$cLine\H".getINF().getMP3info().getPB().getVolume($curVolume,9).getMenuTitle()."\e[$playingb\H\e[1\m".plainfiles($files[$iib],$playBarColor,$browserColor,$iib+1);
	}
	else
	{
		$refreshOut .= "\e[$playingb\H".plainfiles($files[$iib],$playListColor,$browserColor,$iib+1)."\e[$cLine\H".getPB().getINF().getMP3info().getVolume($curVolume,9).getMenuTitle();
	}
	return $refreshOut;

}
sub resetList
{
	if($termsize[1]-$plbLine > 0)
	{
		$playing=($ii%($termsize[1]-$plbLine))+$plbLine;
# 		if( ($termsize[1] - $playing) > ($max - $playing) && $max > $termsize[1]){ print "\e[$termsize[1]\H\n $termsize[1] - $playing -- $max - $playing";quit();}
		$ik=$playing-$plbLine;
		$i=$ii;$ib=$i;$iib=$i;$ikb=$ik;$playingb=$playing;
	}
}
sub test2
{
	read HOUT, $tmp1,4;
	read HOUT, $tmp2,$tmp1;
	@tmp=();
	@tmp=split(/ \+\+ /,$tmp2);
	$title=$tmp[0];
	$name=$tmp[1];
	$genre=$tmp[2];
	$bitrate=$tmp[3];
	$ip=$tmp[4];
	$port=$tmp[5];
	$tmp=getHttpInfo();
	#print $tmp;
	if($ii == $iib){printf "$tmp\e[$playing\H\e[1m\e[$browserColor;$playListColor;$playBarColor\m\e[2K%0.$termsize[0]\s\e[$cLine\H","\<$ip\:$port\> $name";}
	else	{printf "$tmp\e[$playing\H\e[1;$playListColor;$playBarColor\m\e[2K%0.$termsize[0]\s\e[$cLine\H","\<$ip\:$port\> $name";}

	#printf "$tmp\e[$playing\H\e[1m\e[$browserColor;$playListColor;$playBarColor\m\e[2K%0.$termsize[0]\s\e[$cLine\H","$ip\:$port \# $name";
	
	#quit();
}
sub test
{
	$tmp1=0;$tmp2=0;@tmp=();
	open(PLT,"$playlistTime");
	open(TAG,"$playlistTag");
	@tmp1=<PLT>;
	@tmp2=<TAG>;
	for($in=0;$in<$max;$in++)
	{
		unless($filesc[$in] =~ /^http\:\/\//)
		{
			if($time{$files[$in]} eq "")
			{
				#read PLT, ($tmp),10;
				chomp $tmp1[$in];chomp $tmp2[$in];
				$time{$files[$in]}="$tmp1[$in]";
				$tag{$files[$in]}="$tmp2[$in]";
				#$time{$files[$in]}=" ".$time{$files[$in]};
				#chomp $time{$files[$in]};
				#chop $time{$files[$in]} if substr $time{$files[$in]},length($time{$files[$in]}),1 eq ' ';
				#chop $time{$files[$in]} if($time{$files[$in]} =~ / $/)
			}
			#$time{$files[$in]} =~ /[0-9]{2,3}\:[0-9]{2}/;
			@tmp=split /:/,$time{$files[$in]};
			$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";
	}
	print 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;
	$getTimeChildPid=fork();
	if($getTimeChildPid==0)
	{
		sleep 1;
		
		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;
			unless($filesc[$in] =~ /^http\:\/\//)
			{
				if($time{$files[$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];
					$tmp3 =~ /\.(.{2,4})$/;
					$ves=$1;
					#print "\n$ves\n$videoExtensions\n";quit();
					chomp $tmp3;
					if ($tmp3 =~ /mp3$/i && -f $tmp3)
					{
						$tmp = get_mp3info($tmp3);
						$tmp2=get_mp3tag($tmp3,$mp3Version);
						$tmp1 = sprintf "%s\e[0\m",$tmp->{TIME};
						if($showTagPlaylist || $tmp2->{ARTIST} || $tmp2->{TITLE} || $tmp2->{ALBUM})
						{
							$tag{$files[$in]}=sprintf "%s - %s - %s",$tmp2->{ARTIST},$tmp2->{TITLE},$tmp2->{ALBUM};
						}
						#$tmp1 .="";
					}
					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";
					}
					#elsif ($tmp3 =~ /avi$/i || $tmp3=~ /mp[^0-9]{1,2}$/i ||$tmp3 =~ /wmv$/i && -f $tmp3)
					elsif($ves !~ /mpg/i && $videoExtensions =~ /$ves/i && -f $tmp3)
					{
						$tmp=Video::Info->new(-file=>$tmp3);
						$tmp1=sprintf "%02d:%02d",($tmp->duration())/60,($tmp->duration())%60;
						$tmp1 .="\e[0\m";
#						quit();
					}
					else
					{
						$tmp1="--:--\e[0\m";
					}
					$time{$files[$in]}="$tmp1";
					sleep 0.02;
				}

			}
		}
		savePlaylistTime();
		savePlaylistTag();
		open(R,">$remote");
		print R "t
		close R;";
		exit;
	}
	else
	{
		$test=1;
	}
}
sub plainfiles
{
	$tmp=$_[0];
#	$tmp4='';
	chomp $tmp;
	$tmp =~ s/\\//g;
	if($shoutcast)
	{
		@tmp1=split(/\@\@/,$tmp);
		#return sprintf "\e[$playListColor;$_[1];$_[2]\m\e[2K\%0.$termsize[0]\s\n\r","Name: $tmp[0] Playing: $tmp[1] BR: $tmp[2]";
		$shoutcastNameRow=int(($termsize[0]-5)*0.62);$shoutcastPlayingRow=int(($termsize[0]-5)*0.38);$shoutcastBitrateRow=$termsize[0]-5;
		return sprintf "\e[$playListColor;$_[1];$_[2]\m\e[2K\%0.$shoutcastNameRow\s\e[$shoutcastNameRow\G\e[$separatorColor\m|\e[$playListColor;$_[1];$_[2]\m%0.$shoutcastPlayingRow\s\e[$shoutcastBitrateRow\G\e[$separatorColor\m|\e[$playListColor;$_[1];$_[2]\m%-0.5\s\n\r", "$tmp1[0]","$tmp1[1]","$tmp1[2]";
	}
	if($tmp =~ /^http\:\/\//)
	{
		
		$tmp =~ /http\:\/\/(.*)/;
		$tmp3="$1";
		unquote($tmp3);
		$tmp5=sprintf "\e[$playListColor;$_[1];$_[2]\m\e[2K\<%0.$termsize[0]\s\n\r",$tmp3;
		$tmp5 =~ s/ \#/\>/g;

		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]-length($time{$_[0]})+5;
	#$in =$ii;chomp $in;
	if($nr){$tmp1=sprintf "%02d. ",$_[3];}else{$tmp1=''};
	if($showTagPlaylist && $tag{$_[0]}){$tmp=$tag{$_[0]};}
	$tmp3=$tmp4-2;
 	$tmp5=sprintf "\e[$playListColor;$_[1];$_[2]\m\e[2K%0.$tmp3\s\e[$tmp4\G%s\n\r","$tmp1$tmp",$time{$_[0]};
	return $tmp5;
}
sub quote
{
	$_[0] =~ s/ /\%20/g;
	$_[0] =~ s/\&/\%26/g;
	$_[0] =~ s/\`/\%60/g;
	$_[0] =~ s/\/\%B4/g;
}
sub unquote
{
	$_[0] =~ s/\%20/ /g;
	$_[0] =~ s/\%26/\&/g;
	$_[0] =~ s/\%60/\`/g;
	$_[0] =~ s/\%B4/\/g;
}
sub list
{
	#$w="\e[$_[3]\G";
	$listOut= "\e[$plbLine\H";
#	@filesa=();
#	@files=@filesc;
	if (@files == 0)
	{
		$listOut .= "\e[31\mPlaylist is empty!\n\r\e[0;0m";
	}
	for($_[0]=0;$_[0]<$termsize[1]-$plbLine && $_[2]<$_[1];$_[0]++)
	{
		$tmp6=$filesc[$_[2]];
		$listOut .= plainfiles($tmp6,0,$playListColor,$_[2]+1);
		$_[2]++;
	}
	$listOut .= "\e[$cLine\H\e[0;0\m";
	return $listOut;

}
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();
	syswrite $W, "get_percent_pos\n";
	$_=ReadLine 0.25, $R;
	$_=~/([0-9]{1,2})/;
	$gpp=$1;
}

sub get_time_length
{
	$gt=$filesc[$ii];chomp $gt;
	$tmp = get_mp3info($gt);
	$gt = sprintf "%s\e[0\m",int($tmp->{SECS});
	$gttmp=sprintf "%4.2d:%02d",$gt/60,$gt%60;
}
sub clearbuffer {
 	$line=ReadLine $_[0] , $R ;
 	while ($line) {
 		$line=ReadLine $_[0] , $R  ;
 	}
	$R->autoflush() ;
}
sub editPlayList
{
	print getMP3info().getINF().getVolume("$curVolume",9);
	@tmp=();
	$tmp[$iib]=1;
	print browse(1);
	printMarked($iib-1);
	printInfo("edit mode",8);
	while(1)
	{
#		readKey(0.25);
		grepOutput();
# 		processOutput();
		calcTime();
		print getINF().getVolume("$curVolume",9);
		if($key eq 'Esc'||$key eq 'q')
		{
			print refresh(1);
			return;
		}
		elsif($key eq "Down")
		{
			print browse(1);
			printMarked($iib-1);
		}
		elsif($key eq "Up")
		{
			print browse(-1);
			printMarked($iib+1);
			#print "\e[$tmp\H".plainfiles($files[$iib-1],$playListColor,41,$iib)."\e[0m\e[$cLine\H" if($ii == $iib-1);
			#printMarked(($iib));
		}
		elsif($key eq "\x20")
		{
			if($tmp[$iib]){$tmp[$iib]=0;}else{$tmp[$iib]=1;}
			print browse(1);
			printMarked($iib-1);
		}
		elsif($key eq "Del")
		{
			$actuallist=1;
			#open(RPL,"$playlist");
			#@files=<RPL>;
			#$max=scalar(@files);
			#close(RPL);
			open(PL,">$playlist");
			$tmp6=0;
			for($in=0;$in<$max;$in++)
			{
				if($tmp[$in] != 1){print PL $files[$in];}else{$tmp6++;}
			}
			close PL;
			#$playing-=$tmp6;
			#$playingb-=$tmp6;
			open(RPL,"$playlist");
			@files=<RPL>;
			$max=scalar(@files);
			close(RPL);
			@filesc=@files;
			$max=@files;
			last;
		}
	}
			resetList();
			print refresh();
}
sub printMarked
{
	$tmp5=$_[0];
	if($tmp[$tmp5])
	{
		$tmp=$playingb-($iib-$tmp5);
		print "\e[$tmp\H\e[0m".plainfiles($files[$tmp5],$playListColor,41,$iib)."\e[0m\e[$cLine\H";
	}
}
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
{
	$addfile=1;
	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 ${print $!;sleep 1;$dirname='/';};
	$p=3;
	#$index=2;
	#print "\e[2J";
	dir();
	if ($mpid>0)
	{
		print getMP3info().getINF().getVolume("$curVolume",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();
			print refresh('addFile');
			return;
			
		}
		print getINF().getVolume("$curVolume",9);
		$tmp3=$termsize[0]+4;
		$dirname=abs_path("$dirname");
		$dirname.="/" unless $dirname eq "/";
		printf "\r\e[2\H\e[1;$playBarColor\m%0.$tmp3\s","$ENV{'USER'}\@mplay:\e[34\m$dirname"."# \e[0K\e[0;0;0m";
		if($key =~ /^Alt_([a-z])/)
		{
			$akey=$1;
			#$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 print $!;
				@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]") && $extensions =~ /$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")
				{
					print 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 '')
			{
				print 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);
				print 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";
			print refresh();
			$key='';
			$addfile=0;
			last;

		}			
		if($key eq "\n")
		{
			if(-d "$dirname$f[$index]/")
			{
				closedir(DIR);
				$tmp2=$dirname;
				$dirname="$dirname$f[$index]/";
				if(opendir(DIR,$dirname))
				{
					$index=0;
					dir();down();
				}
				else{print $!;$dirname=$tmp2;}
				
				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 print $!;
				$index=0;
				dir();down();
				print "\e[$p\H\e[$browserColor;1\m\e[2K $f[$index]$slash\e[0m\r";
		
		}
	}
	if($actuallist2)
	{
		getFileTime();
		resetList();
		print refresh();
		$actuallist2=0;
		$actuallist=1;
	}
}
sub saveList
{
		print "\e[$cLine\H\e[1;32\m 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;
			}
			elsif($key =~ /[a-zA-Z]{2,5}/){;}
			else
			{
				$tmp3.=$key;
			}
			print "\e[39\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 aspect
{
		print "\e[$cLine\H\e[1;32\m Enter new aspect ratio (float): ";
		$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[34\G\e[0\K$tmp3";
		}
		syswrite $W, "switch_ratio $tmp3\n";
		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;
		$found=0;
		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;
			}
			elsif($key =~ /[a-zA-Z]{2,5}/){;}
			else
			{
				$expression.=$key;
			}
			print "\e[$cLine\H\e[1;32\m Enter search string: \e[$foundColor\m\e[0\K$expression";
			$expression =~ s/[\(\)\-\[\]\<\>\{\}\.\|\^\$\*\+\?\&\\]/\\$&/g;
			$found=0;
			for($c1=$start;$c1<$max;$c1++)
			{
				$tmp=$files[$c1];
				unless($shoutcast)
				{
					$files[$c1] =~ /([^\/]*)$/;
					$tmp=$1;
				}
				#$found=$1;
				$ps=$c1-$iib;
				if($ps>0)
				{
					if($tmp =~ /$expression/i)
					{
						$found=1;
						for($c2=$iib;$c2<($c1-$browser);$c2++){browse(1);}
						print refresh(2);last;
					}
				}
				else
				{
					if($tmp =~ /$expression/i)
					{
						$found=1;
						for($c2=$iib;$c2>($c1-$browser);$c2--){browse(-1);}
						print refresh(1);last;
					}
				}
			}
			if(!$found){$start=0;$foundColor=31;}else{$foundColor=32}
			print "\e[$cLine\H\e[1;32\m Enter search string: \e[$foundColor\m\e[0\K$expression";
		}
		print "\e[$cLine\H\e[2\K\e[0;0\m\r\e[$cLine\H\e[0;0\m\r$dis";

}
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);
			}
			print browse(-1) if($iib+1 == $max && $ib>0);
			unless($time{$filesc[$iib]} eq undef)
			{
				split(/\:/,$totalTime);
				$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();
	print list($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("$curVolume",9);
		if($key eq 's' || $key eq 'q' || $key eq "Esc")
		{
			print 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();
					print 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();
	print refresh();
	$actuallist=1;
}
sub sortInc
{
	@tmp=sort {uc($a) cmp uc($b)} @files;
}
sub sortDec
{
	@tmp=sort {uc($b) cmp uc($a)} @files;
}
sub fileInfo
{
	$key = '';
	$filec = $filesc[$iib];
	chomp $filec;
	$filec =~ /\.(.{2,4})$/;$ves=$1;
	if($filec =~ /\.mp3$/i)
	{
		@tmp=("TITLE", "ARTIST", "ALBUM", "YEAR", "COMMENT", "GENRE", "Rename");
		$info = get_mp3info($filec);
		$tag=get_mp3tag($filec,$mp3Version);
		$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")
			{
				print 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
			{
				$tmp5='';$tmp4='';
				if($key eq "End"){$tmp4=$tmp1[$toChange];$key='';}
				if($key eq "Pos1"){$tmp5=$tmp1[$toChange];$key='';}
				while(1)
				{
					if($key eq "End"){$tmp4="$tmp4$tmp5";$tmp5='';$key='';}
					elsif($key eq "Pos1"){$tmp5="$tmp4$tmp5";$tmp4='';$key='';}
					elsif($key eq "Right"){$tmp4="$tmp4$tmp6";$tmp5=$tmp7;$key='';}
					elsif($key eq "Left"){$tmp5=substr($tmp4,length($tmp4)-1,1)."$tmp5";$tmp4=substr($tmp4,0,length($tmp4)-1);$key='';$tmp6='';}
					if($key eq "Esc"||$key eq "\n"||$key eq "Up"||$key eq "Down")
					{
						$tmp4="$tmp4$tmp5";
						$tmp1[$toChange]=$tmp4 unless $tmp4 eq '' || $key eq "Esc";
						$filec="$tmp1$tmp1[6].mp3";
						last;
					}
					elsif($key eq "Backspace")
					{
						chop $tmp4;
					}
					elsif($key eq "Del")
					{
						$key='';$tmp5="$tmp7";
					}
					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;print refresh();return '';
					}
					else
					{
						$tmp4.=$key;
					}
					if($tmp5 ne ''){$tmp6=substr($tmp5,0,1);$tmp7=substr($tmp5,1);}else{$tmp6='';$tmp7='';}
					printf "\e[$tmp2\H%7s :\e[0\K \e[1;$browserColor\m%s\e[7;$browserColor\m%s\e[0;$browserColor\m%s\e[0;0m",$tmp[$toChange],$tmp4,$tmp6,$tmp7;
					$key=readKey(120);
				}
			}
			$key = '';

		}
		print 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();
		print refresh();
	}
	elsif($ves !~ /mpg/i && $videoExtensions =~ /$ves/i && -f $filec)
	{
		$video=Video::Info->new(-file=>$filec);
		if (defined $video)
		{
			printf "%s\t  Time : %02d:%02d\nFrames per sec : %s\n  Video aspect : %sx%s\n   Video codec : %s\n    Video type : %s\n     File size : %.2f MB\n    Audio freq : %s Hz\n   Audio codec : %s\n Audio bitrate : %.2f kbps\nAudio channels : %s","\e[2J\r\e[1;34\m\r\e[1\H |\e[1;4\m Path \e[0;1;34\m|\n\e[2\K $filec\e[0;32\m\n\n",($video->duration())/60,($video->duration())%60,$video->fps(),$video->width(),$video->height(),$video->vcodec(),$video->type(),($video->filesize())/1024/1024,$video->afrequency(),$video->acodec(),$video->arate()/1000,$video->achans();
		}
		readKey();
		print 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();
		print refresh();
	}
}
sub createNewPlayList
{
#	if($plnr<4)
#	{
#		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=$_[0];
	if($plnr == 5){$plnrText="bookmarks";}else{$plnrText=$plnr;}
#	unless(-f "$ENV{'HOME'}/.mplay/playlist$plnr")
#	{
#		$plnr=0;
#		$playlist="$ENV{'HOME'}/.mplay/playlist$plnr";
#		print 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") if -f $playlist;
	@filesc=();
	@filesc=<RPL>;
	@files=@filesc;@filesa=@filesc;
	$max=@filesc;
	$actuallist=1;
	save();
	getFileTime();
	print refresh();
}
sub switchTagPlaylist
{
	if($showTagPlaylist){$showTagPlaylist=0}else{$showTagPlaylist=1}
	print refresh();
}
sub grepOutput
{
	$line=1;$output='';
	while ($line) {
		$line=ReadLine 0.01 , $R  ;$output.=$line;
		if($line =~ /^Playing (.*)$/){$curTrack=$1;}
	}
	if($output ne '')
	{
		if($output=~ /$playText|XMMS/g)
		{
			#syswrite $W, "seek -1\n" if mp3Workaround();
			if(!$change)
			{
				if($ignoreOutput){$timestat=gettimeofday}
				else{fchange(1)}
				$check=5;
			}
# 			clearbuffer(0.3);
			#syswrite $W, "get_time_length\n";
			get_time_length();
			#print "\e[31m HAAALLO $gt\n";sleep 3;
			#$gttmp=ReadLine 0.25, $R;
			correct(1);
		}
		$key = "?";
	}
	else
	{
		check();
	}

#	$output=ReadLine -1, $R;
}
sub readKey
{
#	if($_[0] eq "noclear")
#	{
#		return $key;
#	}
#	else{$key = '';}
	$key = '';
        $x2 = ReadKey $_[0];
	$x=ord($x2);
	return unless $x;
	$check=2;
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($x3 == 53)
				{
					$x="k5";
				}
			}
			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 == 68)
				{
					$x="k4";
				}
				elsif($x3 == 69)
				{
					$x="k5";
				}
				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 == 32)
		{
			$x = 'Alt_Space';
		}
		elsif($x == 79)
		{
			if($x2 == 80)
			{
				$x="k1";
			}
			elsif($x2 == 81)
			{
				$x="k2";
			}
			elsif($x2 == 82)
			{
				$x="k3";
			}
			elsif($x2 == 83)
			{
				$x="k4";
			}
			else{$x='';}
			ReadKey -1;
			ReadKey -1;
		}
		elsif($x > 96 && $x < 123)
		{
			return sprintf "Alt_%c",$x;
		}

	}
	elsif($x == 127||$x == 8)
	{
		$x="Backspace";
	}
	elsif($x > 224 && $x < 251)
	{
		$x-=128;
		return sprintf "Alt_%c",$x;
	}
	else
	{
		$x=$x2;
	}
	#while(ReadKey -1){;}
	return $x;
}
sub colorChange
{
	if($key eq 'Right')
	{
		$_[0]++;
		if($_[0]>48)
		{
			$_[0]=30;
		}
		elsif($_[0]>37&&$_[0]<40)
		{
			$_[0]=40;
		}
	}
	elsif($key eq 'Left')
	{	
		$_[0]--;
		if($_[0]<30)
		{
			$_[0]=48;
		}
		elsif($_[0]<40&&$_[0]>37)
		{
			$_[0]=37;
		}
	}
}
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";
		}
		if($shoutcast){$key=readKey(0.25);}
		else{
		grepOutput();
		calcTime();}
# 		processOutput();
		$inf=getINF();$vol=getVolume("$curVolume",9);
		if($key eq 'c' || $key eq 'q'|| $key eq 'Esc')
		{
			print "\e[$cLine\H\e[2K";
			last;
		}
		elsif($key eq 'r')
		{
			setDefColor();
			print refresh();
			last;
		}
		elsif($key eq 'Down')
		{
			$toChange++;
			if($toChange>8)
			{
				$toChange=0;
			}
		}
		elsif($key eq 'Up')
		{
			$toChange--;
			if($toChange<0)
			{
				$toChange=8;
			}
			
		}
		if($toChange == 0)
		{
			colorChange($pbarColor);
			print getINF();
		}
		elsif($toChange == 1)
		{
			colorChange($mp3TagColor);
			print getMP3info;
		}
		elsif($toChange == 2)
		{
			colorChange($infoColor);
			print getINF();
		}
		elsif($toChange == 3)
		{
			colorChange($trackInfoColor);
			print getINF();
		}
		elsif($toChange == 4)
		{
			colorChange($volumeColor);
			print getVolume("$curVolume",9);
		}
		elsif($toChange == 5)
		{
			colorChange($playBarColor);
			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)
		{
			colorChange($browserColor);
			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)
		{
			colorChange($playListColor);
			print "\e[$plbLine\H";
			$iia=$ii-($playing-$plbLine);
			print "\e[$plbLine\H";
			$iia=$ii-($playing-$plbLine);
			print list($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";
		}
		elsif($toChange == 8)
		{
			colorChange($separatorColor);$tmp=$plbLine-1;
			if($shoutcast){printShoutcast()}
			else{printf "\e[$tmp\H\e[$separatorColor\m%0.$termsize[0]\s\e[0m",$pbbar;}
			
		}
	}
	getMenuTitle();
	if($shoutcast){print refresh(shoutcast);}else{print refresh();}
}
sub pause
{
		if($shortView){$tmp5=1;}else{$tmp5=$cLine;}
		syswrite $W, "pause\n";
		$tmp6=$termsize[0]-32;
		print "\e[$tmp5\H\e[05;31m\e[$tmp6\G[pause]\r";
		$key='';$tmp='';
		while($key eq '')
		{
			open(R,"$remote");
			$tmp=<R>;
			close R;
			if($tmp eq 'p'){unlink "$remote";last}
			$key=readKey(0.5);
			if($key eq ' ')
			{syswrite $W, "frame_step\n";$key ='';}
		}
		$pausetime=gettimeofday;
		$timestat+=$pausetime-$timenow;
		print "\e[0m\e[$tmp6\G       \r";
		syswrite $W, "pause\n";
		$keyp = "";$key = '';
}
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 eq 'p')
	{
		pause();
	}
	elsif($tmp eq 't')
	{
		reap();
	}
	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");
	savePlaylistTime();
	$equalizer="$eq[0]+$eq[1]+$eq[2]+$eq[3]+$eq[4]+$eq[5]+$eq[6]+$eq[7]+$eq[8]+$eq[9]+$eq[10]";
	$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:$equalizer:$eq:$shoutcast:$shoutcastGenre:$sel:$showTagPlaylist:$curVolume:$sortShoutcast:$timeSwitch";
	close C;close PPL;
}
sub savePlaylistTime
{
	open(PLT,">$playlistTime");
	for($in=0;$in<scalar(@filesc);$in++)
	{
		print PLT "$time{$files[$in]}\n";
	}
	close PLT;

}
sub savePlaylistTag
{
	open(TAG,">$playlistTag");
	for($in=0;$in<scalar(@filesc);$in++)
	{
		print TAG "$tag{$files[$in]}\n";
	}
	close TAG;

}
sub restart
{
	#$timestat=gettimeofday;
	kill 15,$mpid;
	wait;
	setMplayerEq();
	openMplayer();
	#syswrite $W, "pt_step $ii \n";
	#unlink "$pid";
# 	clearbuffer(0.1);
	goBack(int($timeelapsed),0);
	#$key=readKey(0.5);
# 	clearbuffer(0.5);
}
sub quit
{
	$SIG{CHLD}= 'IGNORE';
	$termsize[1]++ if $shortView;
	print "\e[0;0m\e[$termsize[1]\H\e[0;39\m\n\n\rsaving ...\r";
	unlink "$pid";
	save();	
	$key='';
	$i=30;
	$termsize[1]--;
	$tmp4=kill 0,$getTimeChildPid;
	unless($tmp4 == 0)
	{
		kill SIGKILL,$getTimeChildPid;
	}
	print "\e[0;0m\e[$termsize[1]\H\n";
	print "\e[0;0m\e[$termsize[1]\H\e[0;39\m\n\rclosing MPlayer ...\r";
	kill 15,$mpid;
	wait;
	system("setterm -cursor on");
	print "\e[0;39\m\rbye                     \n\r";
	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/local/share/mplay/$_[0]_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/local/share/mplay/$_[0]_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();
			print 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;
	print refresh();
}
sub switchCompactView
{
	if($shortView)
	{
		$cLine=10;
		$plbLine=12;
		$shortView=0;
		resetList();
		$termsize[1]++;
	}
	else
	{
		$cLine=2;
		$plbLine=3;
		$shortView=1;
		resetList();
		$termsize[1]--;
	}
	print refresh() unless $_[0] eq 'silent';
}
sub switchRepeatList
{
	if($repeat){$rep='|';$repeat=0;printInfo("repeat-all-off");}
	else{$rep="";$repeat=1;printInfo("repeat-all-on");}
}
sub changeCache
{
	open(MPC,$mplayconf);
	$tmp='';
	while(<MPC>)
	{
		if($_ =~ /^cache\=/){$_='cache='."$cache\n";}
		$tmp.=$_;
	}
	close MPC;
	open(MPC,">$mplayconf");
	print MPC $tmp;
	close MPC;
}
sub switchSoundEffect
{
	$_[0]=$_[0]?0:1;
	open(MPC,$mplayconf);
	$tmp='';
	while(<MPC>)
	{
		if($_ =~ /^$_[1]\=on/){$_="$_[1]".'=off'."\n";}
		elsif($_ =~ /^$_[1]\=off/){$_="$_[1]".'=on'."\n";}
		$tmp.=$_;
	}
	close MPC;
	open(MPC,">$mplayconf");
	print MPC $tmp;
	close MPC;
	restart();
}
sub switchClock
{
	$timeSwitch=$timeSwitch?0:1;
}
sub switchRepeatOne
{
	if($repeatOne)
	{
		$repeatOne=0;
		$rep=$repSave;printInfo("repeat-one-deactiveted");
		$ignoreOutput=0;
		restart();
	}
	else
	{
		save();kill 15,$mpid;
		$tmp=$playlist.".loop";
		open(W,">$tmp");print W $files[$ii];close W;
		$mpid=open3($W, $R, $E, "mplayer $mplayerOptions $tmp -loop 0");
		goBack(int($timeelapsed),0);
		$ignoreOutput=1;
		#$check=40;
		$repeatOne=1;
		$repSave=$rep;
		$rep='O';printInfo("repeat-one-activeted");
		#readKey(1);
# 		clearbuffer(0.5);
	}
}
sub termSizeChanged
{
	@termsize=GetTerminalSize();
	$termsize[1]-- if $shortView;
	if($termsize[1]+$termsize[0] != $termsize2[1]+$termsize2[0])
	{
		return 1;
	}
	return 0;

}
sub setMplayerEq
{
	if($eq)
	{
		$tmp='';
		if($normalize){$tmp .='volnorm,'}
		if($extraStereo){$tmp .='extrastereo,'}
		$tmp .= 'equalizer';
		$mplayerEqOptions="-af $tmp=$eq[0]:$eq[1]:$eq[2]:$eq[3]:$eq[4]:$eq[5]:$eq[6]:$eq[7]:$eq[8]:$eq[9],volume=$eq[10]";
	}
	else
	{
		$mplayerEqOptions="";
		if($normalize || $extraStereo)
		{
			$mplayerEqOptions="-af ";
			$mplayerEqOptions .= "volnorm," if $normalize;
			$mplayerEqOptions .= "extrastereo" if $extraStereo;
		}
	}
}

sub setDefColor
{
	$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;
}
sub add_list
{
	$actuallist=1;
	open(RPL,$playlist);
	@filesa = <RPL>;
	@files=@filesa;@filesc=@filesa;
	close RPL;
	$max=scalar(@files);
	getFileTime();
	print refresh();
}
sub replace_handler
{
	#$SIG{CHLD} = 'IGNORE';
	$timestat=gettimeofday;
	$timeelapsed=0;
	open(RPL,"$playlist");
	@files=<RPL>;
	@filesa=@files;
	@filesc=@files;
	close RPL;
	$name='';
	$max=scalar(@files);
	$i=0;$ii=0;$ik=0;$playing=$plbLine;$ib=0;$iib=0;$ikb=0;$playingb=$plbLine;$timestat+=$timeelapsed;
	restart();
	#$actuallist=1;
	print refresh();
	getFileTime();
}
sub child_handler
{
	save();
	#$check=-100;
	if($diedTime){$diedTimeNow=time;}else{$diedTime=time;}
	
	if($diedTimeNow)
	{
		if(($diedTimeNow-$diedTime)<3)
		{print "\e[0;31mMplayer died to fast consecutively .. wait for keystroke\e[0m\n";$key=readKey();return}
		$diedTimeNow='';$diedTime='';
	}
		#$diedTimeNow='';$diedTime='';
		$child_handler_count=1;
		if($ii<$max-1)
		{
			print "\e[0;31mMplayer died ... try to restart..\e[0m\n";
			restart();
			$key=readKey(0.2);
			if($key eq 'q' || $key eq "\x03")
			{
				quit();
			}
			print refresh();
		}
		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;
				openMplayer();
				print 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-p --pause\e[0\m\t\tpause on/off \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 specifying files 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=();
	#print @tmp;quit();
	$ii=0;
	for(@tmp)
	{
		if($_ =~ /^[a-zA-Z]{2,4}\:\/\//)
		{
			$tmp1[$ii]=$_;
		}
		else
		{
			$_ =~ /(...$)/;
			$tmp=$1;
			$tmp=~s/[^a-zA-Z0-9]/\\$&/g;
			#print "$tmp\n";quit();
			$tmp1[$ii]=$_ if $extensions =~ /$tmp/i;
		}
		$ii++;
	}
	grep ($extensions,@tmp);

	print PL @tmp1;
	$ii=0;
}
