Audio-MPD

 view release on metacpan or  search on metacpan

lib/Audio/MPD.pm  view on Meta::CPAN

# This method is meant to factorize this code, and will parse the raw output
# of _send_command() in a cooked hash.
#
sub _cooked_command_as_kv {
    my ($self, $command) = @_;
    my %hash =
        map { split(/:\s/, $_, 2) }
        $self->_send_command($command);
    return %hash;
}

#
# my @list = $mpd->_cooked_command_strip_first_field( $command );
#
# Lots of Audio::MPD methods are using _send_command() and then parse the
# output to remove the first field (with the colon ":" acting as separator).
# This method is meant to factorize this code, and will parse the raw output
# of _send_command() in a cooked list of strings.
#
sub _cooked_command_strip_first_field {
    my ($self, $command) = @_;

    my @list =
        map { ( split(/:\s+/, $_, 2) )[1] }
        $self->_send_command($command);
    return @list;
}


#--
# Public methods

# -- MPD interaction: general commands


sub ping {
    my ($self) = @_;
    $self->_send_command( "ping\n" );
}




# sub version {} # implemented as an accessor.




sub kill {
    my ($self) = @_;
    $self->_send_command("kill\n");
}




# implemented by password trigger (from moose)



sub updatedb {
    my ($self, $path) = @_;
    $path ||= '';
    $self->_send_command("update $path\n");
}



sub urlhandlers {
    my ($self) = @_;
    return $self->_cooked_command_strip_first_field("urlhandlers\n");
}


# -- MPD interaction: handling volume & output


sub volume {
    my ($self, $volume) = @_;

    if ($volume =~ /^(-|\+)(\d+)/ )  {
        my $current = $self->status->volume;
        $volume = $1 eq '+' ? $current + $2 : $current - $2;
    }
    $self->_send_command("setvol $volume\n");
}



sub outputs {
    my ($self) = @_;

    my @lines = $self->_send_command("outputs\n");
    my (@outputs, %param);

    # parse lines in reverse order since "id" lines come first
    foreach my $line (reverse @lines) {
        my ($k,$v) = split /:\s/, $line, 2;
        $k =~ s/^output//;
        $param{$k} = $v;
        next unless $k eq 'id'; # last output param
        unshift @outputs, Audio::MPD::Common::Output->new(%param);
        %param = ();
    }

    return @outputs;
}



sub output_enable {
    my ($self, $output) = @_;
    $self->_send_command("enableoutput $output\n");
}



sub output_disable {
    my ($self, $output) = @_;
    $self->_send_command("disableoutput $output\n");
}



( run in 0.807 second using v1.01-cache-2.11-cpan-39bf76dae61 )