#!/usr/bin/perl
#
# xmms2-psipeptune -- updates Psi tune file with data from XMMS2
# Copyright (c)2008 Kacper Gutowski mwgamera at gmail dot com
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
use strict;
use Audio::XMMSClient;
# your $PSIDATADIR/tune here
use constant TUNEPATH => $ENV{HOME}.'/.psi/tune';
our $current = -1; # used to limit subsequent rewrites of tune file
# connect to demon
my $xc = Audio::XMMSClient->new('xmms2-psipeptune');
$xc->connect or die $xc->get_last_error;
my $r;
# publish what's being played now
save_current($xc, TUNEPATH);
# trap changes of status
$r = $xc->broadcast_playback_status or die $xc->get_last_error;
$r->notifier_set( \&save_handler, [$xc, TUNEPATH] );
# trap changes of played entries distinguished by their IDs
$r = $xc->broadcast_playback_current_id or die $xc->get_last_error;
$r->notifier_set( \&save_handler, [$xc, TUNEPATH] );
# trap changes of metadata (streams change titles without changing IDs)
$r = $xc->broadcast_medialib_entry_changed or die $xc->get_last_error;
$r->notifier_set( \&save_handler, [$xc, TUNEPATH] );
# trap server quit event
my $r = $xc->broadcast_quit or die $xc->get_last_error;
$r->notifier_set( sub {
truncate TUNEPATH, 0;
print STDERR"Server quit\n";
exit 0;
});
# run mainloop until server quits
$r->wait;
exit;
# events handler that publishes current tune
sub save_handler($$) {
my ($r,$d) = @_;
warn ${$d}[0]->get_last_error if
save_current(${$d}[0],${$d}[1]);
}
# get current metainfo from given server and publish in given file
sub save_current($$) {
my ($xc,$file) = @_;
my $r = $xc->playback_status or return $!;
$r = $r->wait->value;
# clear when it's stopped (0)
print(STDERR"Cleaning\n"), truncate $file, 0 unless $r;
# do nothing when it's paused (2)
($current = -1), return 0 if $r != 1;
# when playing (1) get id
$r = eval{ $xc->playback_current_id->wait->value } or return $!;
return 0 if $r == $current;
$current = $r;
# get info associated with id
my %info = eval{ %{$xc->medialib_get_info($r)->wait->value} }
or return $!;
# diagnostic
print STDERR"Publishing ID $r\n";
# and publish it to psi
# http://psi-im.org/wiki/Publish_Tune
open F, ">$file" or die $!;
print F "$info{title}\n";
print F "$info{artist}\n";
print F "$info{album}\n";
print F "$info{tracknr}\n";
print F int($info{duration}/1000)."\n";
close F or die $!;
return 0;
}