Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 246 lines (208 sloc) 5.672 kb
#!/usr/bin/perl
our $VERSION = 0.3;
=mplayerd
mplayerd - mplayer daemon
Control it by issuing commands through the named pipe
Get now playing info by executing mplayerd when the daemon's already running
=cut
use strict;
use Mplayer::NowPlaying qw($np_log np);
use Carp;
use Data::Dumper;
use Getopt::Long;
use Cwd qw(abs_path getcwd);
use File::Copy;
my $destdir = "$ENV{HOME}/ToTransfer";
my $fifo = "$ENV{HOME}/.mplayer/mplayer.fifo";
my $log = "$ENV{HOME}/.mplayer/mplayerd.log";
my $lock = '/tmp/mplayerd.pid'; # /var/run/
$np_log = $log;
my $PID; # for child
my %allowed_cmds = (
next => 'pt_step 1',
prev => 'pt_step -1',
toggle => 'pause',
pause => 'pause',
stop => 'stop',
fs => 'vo_fullscreen 1',
);
if(!@ARGV) {
if(!-e $lock) {
yell("Mplayerd is not running\n");
usage();
}
}
GetOptions(
'cmd:s' => \&cmd_in,
'kill' => \&killchild,
'cp' => \&cp,
'help' => \&usage,
'list' => \&listcmds,
'play:s{,}' => \&loadfiles,
);
#map($_ = "'".abs_path($_)."'", @ARGV); # not needed in the list form of exec()
for(@ARGV) {
if($_ =~ /(?:http|mms):\/\//) {
next;
}
$_ = abs_path($_);
}
my @files = @ARGV;
unless(-f $log) {
open(my $fh, '>', $log);
close($fh);
}
unless(-p $fifo) {
use POSIX;
mkfifo($fifo, 0666) or die "Cant mkfifo $fifo: $!";
}
if(!-e $lock) {
daemonize();
# system() will fork. So will exec(), unless used in list mode.
# From here, mplayer will take over our process (the grandchild)
exec('mplayer', '-quiet','-identify', '-slave', "-input", "file=$fifo", @files);
}
else {
if(@files) {
print @files;
cmd_in("FIXTHIS loadfile @files");
}
printf("\t\033[38;5;197m\033[1mMPLAYERD\033[0m\033[38;5;178m\033[0m\n");
my $artist = np('artist');
my $album = np('album');
my $title = np('title');
my $year = np('year');
my $file = np('file');
my $track = np('track');
my $genre = np('genre');
my $comment = np('comment');
my $codec = np('codec');
my $format = np('format');
my $length = np('length');
my $channels= np('channels');
my $bitrate = sprintf("%d", np('bitrate'));
printf("\033[38;5;033m\033[1m%10s\033[0m: %s\n", 'Artist', $artist)
unless !$artist;
printf("\033[38;5;032m\033[1m%10s\033[0m: %s\n", 'Album', $album)
unless !$album;
printf("\033[38;5;031m\033[1m%10s\033[0m: %s\n", 'Song', $title)
unless !$title;
printf("\033[38;5;030m\033[1m%10s\033[0m: %s\n", 'Year', $year)
unless !$year;
printf("\033[38;5;029m\033[1m%10s\033[0m: %s\n", 'File', $file)
unless !$file;
printf("\033[38;5;035m\033[1m%10s\033[0m: %s\n", 'Track', $track)
unless !$track;
printf("\033[38;5;036m\033[1m%10s\033[0m: %s\n", 'Genre', $genre)
unless !$genre;
printf("\033[38;5;037m\033[1m%10s\033[0m: \033[38;5;240m%s\033[0m\n", 'Comment',$comment)
unless !$comment;
printf("\033[38;5;050m\033[1m%10s\033[0m: %s\n", 'Codec', $codec)
unless !$codec;
printf("\033[38;5;049m\033[1m%10s\033[0m: %s\n", 'Format', $format)
unless !$format;
printf("\033[38;5;048m\033[1m%10s\033[0m: %s\n", 'Length', $length)
unless !$length;
printf("\033[38;5;047m\033[1m%10s\033[0m: %s\n", 'Channels', $channels)
unless !$channels;
yell("\nmplayerd is running.\n");
}
sub cp {
my $file = np('file');
if(copy($file, "$destdir/")) {
print "\033[38;5;148m$file\033[0m > \033[1m $destdir\033[0m\n";
}
else {
print $destdir; yell(": $!\n");
}
exit(0);
}
sub loadfiles {
shift;
#map{abs_path($_)} @_;
my @files = @_;
open(my $fh, '>', $fifo) or croak("Cant open FIFO $fifo for writing: $!");
print $fh "loadfile @files\n";
close($fh);
exit(0);
}
sub listcmds {
yell("Valid commands:\n");
for my $k(sort(keys(%allowed_cmds))) {
print " $k\n";
}
print "\nExample: mplayerd -cmd next\n";
print "\nIssue 'mplayer -input cmdlist' to see all supported commands\n";
exit 0;
}
sub cmd_in {
shift;
my @cmd = @_;
@cmd = $allowed_cmds{@cmd} // @_;
#if(!defined($allowed_cmds{$cmd})) {
# print "Arbitary command! Dont blame me if it doesnt work...\n";
#}
open(my $fh, '>', $fifo) or die "Cant open $fifo for writing: $!";
print $fh @cmd, "\n";
close($fh);
exit 0;
}
sub daemonize {
use POSIX 'setsid';
$PID = fork();
exit 0 if $PID; # parent
exit 1 if !defined($PID); # out of resources
setsid();
$PID = fork();
exit 1 if !defined($PID); # out of resources
if($PID) { # parent
waitpid($PID, 0);
unlink($lock); #fail silently
exit 0;
}
elsif($PID == 0) { #child
open(my $fh, '>', $lock) or die "Cant open $lock: $!";
print $fh $$;
close($fh);
open(STDOUT, '>', $log);
open(STDERR, '>', '/dev/null');
open(STDIN, '<', '/dev/null');
}
}
sub killchild {
open(my $fh, '<', $lock) or yell("Mplayerd is not running\n") and exit(1);
my $tokill = <$fh>;
close($fh);
if(kill(9, $tokill)) {
yell("mplayerd: @{[error($tokill)]} killed\n");
}
else {
yell("Could not kill $tokill\n");
}
unlink($lock);
exit 0;
}
sub yell {
my @text = @_;
map{$_ = "\033[1m$_\033[0m"} @text;
print @text;
}
sub error {
my @text = @_;
map{$_ = "\033[31;1m$_\033[0m"} @text;
return @text;
}
sub usage {
print << "USAGE";
mplayerd $VERSION
Usage: mplayerd [OPTIONS] (ARGS)
OPTIONS:
--cmd send a command to mplayer
-l, --list list valid commands
-p, --play play FILE or URI (change playlist)
--cp copy playing track to \$destdir
--kill kill the playing process
--help this help
USAGE
exit 0;
}
Jump to Line
Something went wrong with that request. Please try again.