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