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