# XMMS-InfoPipe front-end - allow /np [dest]
#
#   Thanks to ak for suggestions and even changes.
#
#   /set xmms_fifo <dest of xmms-infopipe fifo>
#   /set xmms_format <format of printed text>
#   /set xmms_format_streaming <format for streams>
#   /set xmms_print_if_stopped <ON|OFF>
#   /set xmms_format_time <time format> - default is %m:%s
# 
#   xmms_format* takes these arguments:
#       Variable    Name        Example
#   ----------------------------------------------------
#   Song specific:
#       %status     Status          Playing
#       %title      Title           Blue Planet Corporation - Open Sea
#       %file       File            /mp3s/blue planet corporation - open sea.mp3
#       %length     Length          9:13
#       %pos        Position        0:08
#       %bitrate    Bitrate         160kbps
#       %freq       Sampling freq.  44.1kHz
#       %pctdone    Percent done    1.4%
#       %channels   Channels        2
#   Playlist specific:
#       %pl_total   Total entries
#       %pl_current Position in playlist
#       ¤pl_pctdone Playlist Percent done

use strict;
use Ekg2;

our $VERSION = "2.0";
our %EKG2 = (
    authors     => 'Simon Shine',
    contact     => 'simon@blueshell.dk',
    name        => 'xmms',
    description => 'XMMS-InfoPipe front-end - allow /np [-help] [dest]',
    license     => 'Public Domain',
    changed     => '2004-01-15'
);

# rewriten to ekg2 perl script by darkjames @ 2005-08-28

Ekg2::variable_add('xmms_fifo', '/tmp/xmms-info');
Ekg2::variable_add('xmms_format', 'np: %title at %bitrate [%pos of %length]');
Ekg2::variable_add('xmms_format_streaming', 'streaming: %title at %bitrate [%file]');
Ekg2::variable_add('xmms_format_time', '%m:%s');
Ekg2::variable_add('xmms_print_if_stopped', '1');

Ekg2::command_bind('np', 'cmd_xmms');
# Ekg2::command_bind('xmms', 'cmd_xmms');
# Tab completition
Ekg2::command_bind('np help', 'cmd_xmms');
## Ekg2::command_bind('xmms help', \&cmd_xmms);

sub cmd_xmms {
    my ($name, $args) = @_;

    $args =~ s/^\s+//;
    $args =~ s/\s+$//;

    if ($args =~ /^help/) {
      Ekg2::echo("
Valid format strings for xmms_format and xmms_format_streaming:
    %%status, %%title, %%file, %%length, %%pos, %%bitrate,
    %%freq, %%pctdone, %%channels, %%pl_total, %%pl_current

Example: /set xmms_format np: %%title at %%bitrate [%%pctdone]

Valid format string for xmms_format_time:
    %%m, %%s

Example: /set xmms_format_time %%m minutes, %%s seconds\n");
      return;
    }

    my ($xf) = Ekg2::variable_find('xmms_fifo')->{value};
    if (!-r $xf) {
        if (!-r '/tmp/xmms-info') {
            Ekg2::echo("Couldn't find a valid XMMS-InfoPipe FIFO.");
            return;
        }
        $xf = '/tmp/xmms-info';
    }

    my %xi;

    open(XMMS, $xf);
    while (<XMMS>) {
        chomp;
        my ($key, $value) = split /: /, $_, 2;
        $xi{$key} = $value;
    }
    close(XMMS);

    my %fs;

    # %status
    $fs{'status'} = $xi{'Status'};
    # %title
    if ($fs{'status'} ne "Playing") {
        if (Ekg2::variable_find('xmms_print_if_stopped')->{value} > 0) {
            $fs{'title'} = sprintf('(%s) %s', $fs{'status'}, $xi{'Title'});
        } else {
            Ekg2::echo("XMMS is currently not playing.");
            return;
        }
    } else {
        $fs{'title'} = $xi{'Title'};
    }
    # %file
    $fs{'file'} = $xi{'File'};
    # %length
    $fs{'length'} = &format_time($xi{'Time'});
    # %pos
    $fs{'pos'} = &format_time($xi{'Position'});
    # %bitrate
    $fs{'bitrate'} = sprintf("%.0fkbps", $xi{'Current bitrate'} / 1000);
    # %freq
    $fs{'freq'} = sprintf("%.1fkHz", $xi{'Samping Frequency'} / 1000);
    # %pctdone
    if ($xi{'uSecTime'} > 0) {
        $fs{'pctdone'} = sprintf("%.1f%%%%", ($xi{'uSecPosition'} / $xi{'uSecTime'}) * 100);
    } else {
        $fs{'pctdone'} = "0.0%%";
    }
    # %channels
    $fs{'channels'} = $xi{'Channels'};
    # %pl_total
    $fs{'pl_total'} = $xi{'Tunes in playlist'};
    # %pl_current
    $fs{'pl_current'} = $xi{'Currently playing'};
    # %pl_pctdone
    $fs{'pl_pctdone'} = sprintf("%.1f%%%%", ($fs{'pl_current'} / ($fs{'pl_total'} ? $fs{'pl_total'} : 1)) * 100);


    my ($format) = ($xi{'uSecTime'} == "-1") ?
        Ekg2::variable_find('xmms_format_streaming')->{value} :
        Ekg2::variable_find('xmms_format')->{value};
    foreach (keys %fs) {
        $format =~ s/\%$_/$fs{$_}/g;
    }

    # sending it.
    my ($sess) = Ekg2::session_current;
    my ($wind) = Ekg2::window_current;
    
    if ($sess && $sess->{connected} && $wind->{id} > 1) {
        if ($args eq "") {
	    Ekg2::command("/msg $wind->{target} $format");
        } else {
	    Ekg2::command("/msg $args $format");
       }
    } else {
        Ekg2::echo($format);
    }
}

sub format_time {
    my ($m, $s) = split /:/, @_[0], 2;
    my ($format) = Ekg2::variable_find('xmms_format_time')->{value};
    $format =~ s/\%m/$m/g;
    $format =~ s/\%s/$s/g;
    return $format;
}
1;