1# XMMS-InfoPipe front-end - allow /np [dest]
2#
3#   Thanks to ak for suggestions and even changes.
4#
5#   /set xmms_fifo <dest of xmms-infopipe fifo>
6#   /set xmms_format <format of printed text>
7#   /set xmms_format_streaming <format for streams>
8#   /set xmms_print_if_stopped <ON|OFF>
9#   /set xmms_format_time <time format> - default is %m:%s
10#
11#   xmms_format* takes these arguments:
12#       Variable    Name        Example
13#   ----------------------------------------------------
14#   Song specific:
15#       %status     Status          Playing
16#       %title      Title           Blue Planet Corporation - Open Sea
17#       %file       File            /mp3s/blue planet corporation - open sea.mp3
18#       %length     Length          9:13
19#       %pos        Position        0:08
20#       %bitrate    Bitrate         160kbps
21#       %freq       Sampling freq.  44.1kHz
22#       %pctdone    Percent done    1.4%
23#       %channels   Channels        2
24#   Playlist specific:
25#       %pl_total   Total entries
26#       %pl_current Position in playlist
27#       �pl_pctdone Playlist Percent done
28use strict;
29use Irssi;
30use vars qw($VERSION %IRSSI);
31$VERSION = "2.0";
32%IRSSI = (
33    authors     => 'Simon Shine',
34    contact     => 'simon@blueshell.dk',
35    name        => 'xmms',
36    description => 'XMMS-InfoPipe front-end - allow /np [-help] [dest]',
37    license     => 'Public Domain',
38    changed     => '2004-01-15'
39);
40
41Irssi::settings_add_str('xmms', 'xmms_fifo', '/tmp/xmms-info');
42Irssi::settings_add_str('xmms', 'xmms_format', 'np: %title at %bitrate [%pos of %length]');
43Irssi::settings_add_str('xmms', 'xmms_format_streaming', 'streaming: %title at %bitrate [%file]');
44Irssi::settings_add_str('xmms', 'xmms_format_time', '%m:%s');
45Irssi::settings_add_bool('xmms', 'xmms_print_if_stopped', 'yes');
46
47Irssi::command_bind('np', \&cmd_xmms);
48Irssi::command_bind('xmms', \&cmd_xmms);
49# Tab completition
50Irssi::command_bind('np help', \&cmd_xmms);
51Irssi::command_bind('xmms help', \&cmd_xmms);
52
53sub cmd_xmms {
54    my ($args, $server, $witem) = @_;
55
56    $args =~ s/^\s+//;
57    $args =~ s/\s+$//;
58
59    if ($args =~ /^help/) {
60      print CRAP q{
61Valid format strings for xmms_format and xmms_format_streaming:
62    %%status, %%title, %%file, %%length, %%pos, %%bitrate,
63    %%freq, %%pctdone, %%channels, %%pl_total, %%pl_current
64
65Example: /set xmms_format np: %%title at %%bitrate [%%pctdone]
66
67Valid format string for xmms_format_time:
68    %%m, %%s
69
70Example: /set xmms_format_time %%m minutes, %%s seconds
71};
72      return;
73    }
74
75    my ($xf) = Irssi::settings_get_str('xmms_fifo');
76    if (!-r $xf) {
77        if (!-r '/tmp/xmms-info') {
78            Irssi::print "Couldn't find a valid XMMS-InfoPipe FIFO.";
79            return;
80        }
81        $xf = '/tmp/xmms-info';
82    }
83
84    my %xi;
85
86    open(XMMS, "<", $xf);
87    while (<XMMS>) {
88        chomp;
89        my ($key, $value) = split /: /, $_, 2;
90        $xi{$key} = $value;
91    }
92    close(XMMS);
93
94    my %fs;
95
96    # %status
97    $fs{'status'} = $xi{'Status'};
98    # %title
99    if ($fs{'status'} ne "Playing") {
100        if (Irssi::settings_get_bool('xmms_print_if_stopped')) {
101            $fs{'title'} = sprintf('(%s) %s', $fs{'status'}, $xi{'Title'});
102        } else {
103            Irssi::print "XMMS is currently not playing.";
104            return;
105        }
106    } else {
107        $fs{'title'} = $xi{'Title'};
108    }
109    # %file
110    $fs{'file'} = $xi{'File'};
111    # %length
112    $fs{'length'} = &format_time($xi{'Time'});
113    # %pos
114    $fs{'pos'} = &format_time($xi{'Position'});
115    # %bitrate
116    $fs{'bitrate'} = sprintf("%.0fkbps", $xi{'Current bitrate'} / 1000);
117    # %freq
118    $fs{'freq'} = sprintf("%.1fkHz", $xi{'Samping Frequency'} / 1000);
119    # %pctdone
120    if ($xi{'uSecTime'} > 0) {
121        $fs{'pctdone'} = sprintf("%.1f%%%%", ($xi{'uSecPosition'} / $xi{'uSecTime'}) * 100);
122    } else {
123        $fs{'pctdone'} = "0.0%%";
124    }
125    # %channels
126    $fs{'channels'} = $xi{'Channels'};
127    # %pl_total
128    $fs{'pl_total'} = $xi{'Tunes in playlist'};
129    # %pl_current
130    $fs{'pl_current'} = $xi{'Currently playing'};
131    # %pl_pctdone
132    $fs{'pl_pctdone'} = sprintf("%.1f%%%%", ($fs{'pl_current'} / ($fs{'pl_total'} ? $fs{'pl_total'} : 1)) * 100);
133
134
135    my ($format) = ($xi{'uSecTime'} == "-1") ?
136        Irssi::settings_get_str('xmms_format_streaming') :
137        Irssi::settings_get_str('xmms_format');
138    foreach (keys %fs) {
139        $format =~ s/\%$_/$fs{$_}/g;
140    }
141
142    # sending it.
143    if ($server && $server->{connected} && $witem &&
144        ($witem->{type} eq "CHANNEL" || $witem->{type} eq "QUERY")) {
145        if ($args eq "") {
146            $witem->command("/SAY $format");
147        } else {
148            $witem->command("/MSG $args $format");
149        }
150    } else {
151        Irssi::print($format);
152    }
153}
154
155sub format_time {
156    my ($m, $s) = split /:/, @_[0], 2;
157    my ($format) = Irssi::settings_get_str('xmms_format_time');
158    $format =~ s/\%m/$m/g;
159    $format =~ s/\%s/$s/g;
160    return $format;
161}
162