| 1 | package XMMS::InfoPipe; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.032'; | ||
| 6 | our $PIPE = '/tmp/xmms-info'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | XMMS::InfoPipe - A small module to gather the information produced by | ||
| 11 | the infopipe plugin for XMMS | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use XMMS::InfoPipe; | ||
| 16 | |||
| 17 | my $xmms = XMMS::InfoPipe->new(); | ||
| 18 | |||
| 19 | print "Currently ", $xmms->{info}->{Status}, ": ", $xmms->{info}->{Title}; | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | This module was written to provide a way to snag the information from the | ||
| 24 | file produced by the xmms-infopipe plugin for XMMS. With only a few | ||
| 25 | convenience methods, all of the information that the plugin provides can | ||
| 26 | be obtained from the C<$xmms-E<gt>{info}> hashref. | ||
| 27 | |||
| 28 | B<Nota Bene:> If the XMMS plugin isn't enabled, then this module will NOT | ||
| 29 | return results as expected (if it even works). | ||
| 30 | |||
| 31 | =head1 METHODS | ||
| 32 | |||
| 33 | =head2 new | ||
| 34 | |||
| 35 | my $xmms = XMMS::InfoPipe->new(); | ||
| 36 | |||
| 37 | Creates a new XMMS::InfoPipe instance. By default this parses the file | ||
| 38 | before returning the object. This will undoubtedly cause some initial | ||
| 39 | slowdown (the bottleneck of XMMS::InfoPipe is when it must grab information | ||
| 40 | from the named pipe the XMMS plugin provides), and so you may disable | ||
| 41 | this first parsing by specifying a false value to ForceParse. For example: | ||
| 42 | |||
| 43 | my $xmms = XMMS::InfoPipe->new(ForceParse => 0); | ||
| 44 | |||
| 45 | will create the object and immediately return it, without first populating | ||
| 46 | it with the information from XMMS. This means that before trying to obtain | ||
| 47 | this information, you should first call C<$xmms-E<gt>update_info>. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | |||
| 52 | sub new { | ||
| 53 | my $class = shift; | ||
| 54 | my %args = @_; | ||
| 55 | |||
| 56 | $args{ForceParse} = 1 unless exists $args{ForceParse}; | ||
| 57 | |||
| 58 | my $self = bless { | ||
| 59 | error => '', | ||
| 60 | info => {}, | ||
| 61 | _force_parse=> $args{ForceParse} | ||
| 62 | }, $class; | ||
| 63 | |||
| 64 | $self->{info} = $self->_parse if $self->{_force_parse}; | ||
| 65 | return $self; | ||
| 66 | } | ||
| 67 | |||
| 68 | =head2 is_running | ||
| 69 | |||
| 70 | $xmms->is_running() | ||
| 71 | |||
| 72 | Returns 1 if XMMS is running and 0 if not. This relies on the fact that | ||
| 73 | the named pipe does not exist if XMMS is not running. If the infopipe | ||
| 74 | plugin isn't enabled, this will also return 0. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub is_running { | ||
| 79 | my $self = shift; | ||
| 80 | return $self->{info}->{Status} ne 'Not Running' ? 1 : 0; | ||
| 81 | } | ||
| 82 | |||
| 83 | =head2 is_playing | ||
| 84 | |||
| 85 | $xmms->is_playing() | ||
| 86 | |||
| 87 | Returns 1 if XMMS is playing a song and 0 if not. | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub is_playing { | ||
| 92 | my $self = shift; | ||
| 93 | return $self->{info}->{Status} eq 'Playing' ? 1 : 0; | ||
| 94 | } | ||
| 95 | |||
| 96 | =head2 is_paused | ||
| 97 | |||
| 98 | $xmms->is_paused() | ||
| 99 | |||
| 100 | Returns 1 if XMMS is paused and 0 if not. | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub is_paused { | ||
| 105 | my $self = shift; | ||
| 106 | return $self->{info}->{Status} eq 'Paused' ? 1 : 0; | ||
| 107 | } | ||
| 108 | |||
| 109 | =head2 update_info | ||
| 110 | |||
| 111 | $xmms->update_info() | ||
| 112 | |||
| 113 | Updates C<$xmms-E<gt>{info}> and returns the updated hashref for convenience. | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | sub update_info { | ||
| 118 | my $self = shift; | ||
| 119 | $self->{info} = $self->_parse; | ||
| 120 | return $self->{info}; | ||
| 121 | } | ||
| 122 | |||
| 123 | =head2 _parse | ||
| 124 | |||
| 125 | $xmms->_parse | ||
| 126 | |||
| 127 | Internal function that parses data from the info pipe and returns a hashref. | ||
| 128 | You shouldn't need to use this. | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | sub _parse { | ||
| 133 | open DATA, $PIPE or return { Status => 'Not Running' }; | ||
| 134 | my @data = <DATA>; | ||
| 135 | close DATA; | ||
| 136 | |||
| 137 | my $info = {}; | ||
| 138 | for (@data) { | ||
| 139 | chomp; my ($field, $value) = split /: /, $_, 2; | ||
| 140 | $info->{$field} = $value; | ||
| 141 | } | ||
| 142 | return $info; | ||
| 143 | } | ||
| 144 | |||
| 145 | =head1 VARIABLES | ||
| 146 | |||
| 147 | =head2 $XMMS::InfoPipe::PIPE | ||
| 148 | |||
| 149 | $XMMS::InfoPipe::PIPE = '/tmp/other-name'; | ||
| 150 | |||
| 151 | This variable defaults to C</tmp/xmms-info> which should be a symlink (created | ||
| 152 | by xmms-infopipe) to the real named pipe (something like C</tmp/xmms-info_user.0>). | ||
| 153 | If for whatever reason you need to change it (maybe you have a file generated by | ||
| 154 | something else that follows the same format as xmms-infopipe), just set it before | ||
| 155 | C<update_info> is called (by default that means before C<new> is called) | ||
| 156 | for the right file to be used. | ||
| 157 | |||
| 158 | =head1 INFORMATION AVAILABLE | ||
| 159 | |||
| 160 | As of version 1.3 of the xmms-infopipe plugin, the following information is available: | ||
| 161 | |||
| 162 | XMMS protocol version | ||
| 163 | InfoPipe Plugin version | ||
| 164 | Status | ||
| 165 | Tunes in playlist | ||
| 166 | Currently playing | ||
| 167 | uSecPosition | ||
| 168 | Position | ||
| 169 | uSecTime | ||
| 170 | Time | ||
| 171 | Current bitrate | ||
| 172 | Samping Frequency | ||
| 173 | Channels | ||
| 174 | Title | ||
| 175 | File | ||
| 176 | |||
| 177 | To get this information, just use the corresponding key name above. B<Nota Bene:> | ||
| 178 | "Samping" is not a typo in this doc; it's a typo in the xmms-infopipe plugin. : ) | ||
| 179 | |||
| 180 | =head1 LICENSE | ||
| 181 | |||
| 182 | This module is free software, and may be distributed under the same | ||
| 183 | terms as Perl itself. | ||
| 184 | |||
| 185 | =head1 AUTHOR | ||
| 186 | |||
| 187 | Copyright (C) 2003, Thomas R. Sibley C<tsibley@cpan.org> | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | 1; |