mediaplayer 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/perl
  2. # Copyright (C) 2014 Tony Crisci <tony@dubstepdish.com>
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. # Requires playerctl binary to be in your path (except cmus)
  14. # See: https://github.com/acrisci/playerctl
  15. # Set instance=NAME in the i3blocks configuration to specify a music player
  16. # (playerctl will attempt to connect to org.mpris.MediaPlayer2.[NAME] on your
  17. # DBus session).
  18. use Env qw(BLOCK_INSTANCE);
  19. my @metadata = ();
  20. my $player_arg = "";
  21. if ($BLOCK_INSTANCE) {
  22. $player_arg = "--player='$BLOCK_INSTANCE'";
  23. }
  24. if ($ENV{'BLOCK_BUTTON'} == 1) {
  25. system("playerctl $player_arg previous");
  26. } elsif ($ENV{'BLOCK_BUTTON'} == 2) {
  27. system("playerctl $player_arg play-pause");
  28. } elsif ($ENV{'BLOCK_BUTTON'} == 3) {
  29. system("playerctl $player_arg next");
  30. }
  31. if ($player_arg eq '' or $player_arg =~ /cmus$/) {
  32. # try cmus first
  33. my @cmus = split /^/, qx(cmus-remote -Q);
  34. if ($? == 0) {
  35. foreach my $line (@cmus) {
  36. my @data = split /\s/, $line;
  37. if (shift @data eq 'tag') {
  38. my $key = shift @data;
  39. my $value = join ' ', @data;
  40. @metadata[0] = $value if $key eq 'artist';
  41. @metadata[1] = $value if $key eq 'title';
  42. }
  43. }
  44. if (@metadata) {
  45. # metadata found so we are done
  46. print(join ' - ', @metadata);
  47. exit 0;
  48. }
  49. }
  50. # if cmus was given, we are done
  51. exit 0 unless $player_arg eq '';
  52. }
  53. my $xx = `playerctl -l 2>&1`;
  54. if($xx eq 'No players were found'){
  55. exit(0);
  56. }
  57. my $artist = qx(playerctl $player_arg metadata artist);
  58. # exit status will be nonzero when playerctl cannot find your player
  59. exit(0) if $?;
  60. push(@metadata, $artist) if $artist;
  61. my $title = qx(playerctl $player_arg metadata title);
  62. exit(0) if $?;
  63. push(@metadata, $title) if $title;
  64. print(join(" - ", @metadata)) if @metadata && @metadata ne NULL;