Add perlcritic tests and make perlcritic happy
[app-xmms2-notifier.git] / lib / App / XMMS2 / Notifier.pm
CommitLineData
3541057c 1package App::XMMS2::Notifier;
bc8efcf2 2use 5.014000;
3541057c 3use strict;
d53a283b 4use warnings;
3541057c 5our $VERSION = 0.001001;
d53a283b
MG
6
7use Audio::XMMSClient 0.03;
8use Gtk2::Notify 0.05 -init,'xmms2-notifyd';
9
d53a283b
MG
10use constant CONVERSION_SPECIFIERS => qw/bitrate date sample_format url id channels samplerate tracknr genre artist album title/;
11
12##################################################
13
f4385cf2 14my $format;
d53a283b
MG
15my $xmms = Audio::XMMSClient->new('xmms2-notifyd');
16my $notify = Gtk2::Notify->new('');
17
18$notify->set_timeout(3000);
19
20##################################################
21
22sub notify_libnotify{
b51c6114
MG
23 $notify->update($_[0]);
24 $notify->show;
d53a283b
MG
25}
26
27sub notify{
ca539a7a
MG
28 my ($id, $minfo);
29 eval {
30 $id=$xmms->playback_current_id->wait->value or return;
31 $minfo=$xmms->medialib_get_info($id)->wait->value;
32 } or return;
d53a283b 33
57113b20 34 my %metadata = map { $_ => exists $minfo->{$_} ? (values %{$minfo->{$_}})[0] : undef } CONVERSION_SPECIFIERS;
b51c6114 35 my $str=$format;
bc8efcf2 36 $str =~ s/\$$_/$metadata{$_}/gs for keys %metadata;
d53a283b 37
f3286fcd 38 notify_libnotify $str
d53a283b
MG
39}
40
41sub on_playback_current_id {
b51c6114
MG
42 notify;
43 $xmms->broadcast_playback_current_id->notifier_set(\&on_playback_current_id);
d53a283b
MG
44}
45
46sub on_playback_status {
b51c6114
MG
47 notify if $xmms->playback_status->wait->value == 1; # 1 means playing, 2 means paused
48 $xmms->broadcast_playback_status->notifier_set(\&on_playback_status);
d53a283b
MG
49}
50
51sub run {
f4385cf2 52 $format = $_[0];
b51c6114
MG
53 while (1) {
54 last if ($xmms->connect);
55 sleep 1
56 }
57
58 $xmms->broadcast_playback_current_id->notifier_set(\&on_playback_current_id);
59 $xmms->broadcast_playback_status->notifier_set(\&on_playback_status);
60 $xmms->loop
d53a283b
MG
61}
62
bc8efcf2 631;
d53a283b
MG
64__END__
65
66=head1 NAME
67
68App::XMMS2::Notifier - script which notifies you what xmms2 is playing
69
70=head1 SYNOPSIS
71
72 # Shows libnotify notifications e.g. "Silence - Cellule"
73 xmms2-notifier
74
75 # Shows libnotify notifications e.g. "Cellule by Silence (L'autre endroit), year 2005, genre Electro"
76 xmms2-notifier --format="$title by $artist ($album), year $date, genre $genre"
77
78=head1 DESCRIPTION
79
80xmms2-notifier is a script which shows libnotify notifications when
81the song is changed and when the playback is started/resumed.
82
83You can control the notification format with the B<--format> argument.
84The following strings are replaced:
85
86=over
87
88=item $bitrate
89
90The song bitrate, in bits/s. Example: 785104
91
92=item $date
93
94Usually the year the song was published. Example: 2005
95
96=item $sample_format
97
98The format of each sample. Example: S16
99
100=item $url
101
102An URL that points to the song. Example: file:///ext/Music/Silence+-+Cellule.flac
103
104=item $id
105
106The XMMS2 id of the song. Example: 498
107
108=item $channels
109
110The number of channels the song has. Example: 2
111
112=item $samplerate
113
114The sample rate of the song, in Hz. Example: 44100
115
116=item $tracknr
117
118The track number in the album. Example: 1
119
120=item $genre
121
122The genre of the song. Example: Electro
123
124=item $artist
125
126The artist/band. Example: Silence
127
128=item $album
129
130The album the song is from. Example: L'autre endroit
131
132=item $title
133
134The song title. Example: Cellule
135
136=back
137
138=head1 SEE ALSO
139
140L<xmms2(1)>
141
142=head1 AUTHOR
143
144Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
145
146=head1 COPYRIGHT AND LICENSE
147
148Copyright (C) 2013 by Marius Gavrilescu
149
150This library is free software; you can redistribute it and/or modify
151it under the same terms as Perl itself, either Perl version 5.14.2 or,
152at your option, any later version of Perl 5 you may have available.
153
154
155=cut
This page took 0.020659 seconds and 4 git commands to generate.