summaryrefslogtreecommitdiffstats
path: root/scripts/xmms.pl
blob: a18c2b9d25d4e156c0fc08766ebd88d692101fc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# 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 Irssi;
use vars qw($VERSION %IRSSI);
$VERSION = "2.0";
%IRSSI = (
    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'
);

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

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

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

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

    if ($args =~ /^help/) {
      print CRAP q{
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
};
      return;
    }

    my ($xf) = Irssi::settings_get_str('xmms_fifo');
    if (!-r $xf) {
        if (!-r '/tmp/xmms-info') {
            Irssi::print "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 (Irssi::settings_get_bool('xmms_print_if_stopped')) {
            $fs{'title'} = sprintf('(%s) %s', $fs{'status'}, $xi{'Title'});
        } else {
            Irssi::print "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") ?
        Irssi::settings_get_str('xmms_format_streaming') :
        Irssi::settings_get_str('xmms_format');
    foreach (keys %fs) {
        $format =~ s/\%$_/$fs{$_}/g;
    }

    # sending it.
    if ($server && $server->{connected} && $witem &&
        ($witem->{type} eq "CHANNEL" || $witem->{type} eq "QUERY")) {
        if ($args eq "") {
            $witem->command("/SAY $format");
        } else {
            $witem->command("/MSG $args $format");
        }
    } else {
        Irssi::print($format);
    }
}

sub format_time {
    my ($m, $s) = split /:/, @_[0], 2;
    my ($format) = Irssi::settings_get_str('xmms_format_time');
    $format =~ s/\%m/$m/g;
    $format =~ s/\%s/$s/g;
    return $format;
}