mediaplayer 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) 2014 Tony Crisci <tony@dubstepdish.com>
  4. # Copyright (C) 2015 Thiago Perrotta <perrotta dot thiago at poli dot ufrj dot br>
  5. # Copyright (c) 2021 Jesús E. <heckyel@hyperbola.info>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. # Set instance=NAME in the i3blocks configuration to specify a music player.
  20. use Time::HiRes qw(usleep);
  21. use Env qw(BLOCK_INSTANCE);
  22. use constant DELAY => 50; # Delay in ms to let network-based reflect new data.
  23. my @metadata = ();
  24. my $player_arg = "";
  25. if ($BLOCK_INSTANCE) {
  26. $player_arg = "--player='$BLOCK_INSTANCE'";
  27. }
  28. sub buttons {
  29. my $method = shift;
  30. if($method eq 'mpd') {
  31. if ($ENV{'BLOCK_BUTTON'} == 1) {
  32. system("mpc prev");
  33. } elsif ($ENV{'BLOCK_BUTTON'} == 2) {
  34. system("mpc toggle");
  35. } elsif ($ENV{'BLOCK_BUTTON'} == 3) {
  36. system("mpc next");
  37. } elsif ($ENV{'BLOCK_BUTTON'} == 4) {
  38. system("mpc volume +10");
  39. } elsif ($ENV{'BLOCK_BUTTON'} == 5) {
  40. system("mpc volume -10");
  41. }
  42. } elsif ($method eq 'cmus') {
  43. if ($ENV{'BLOCK_BUTTON'} == 1) {
  44. system("cmus-remote --prev");
  45. } elsif ($ENV{'BLOCK_BUTTON'} == 2) {
  46. system("cmus-remote --pause");
  47. } elsif ($ENV{'BLOCK_BUTTON'} == 3) {
  48. system("cmus-remote --next");
  49. }
  50. } elsif ($method eq 'rhythmbox') {
  51. if ($ENV{'BLOCK_BUTTON'} == 1) {
  52. system("rhythmbox-client --previous");
  53. } elsif ($ENV{'BLOCK_BUTTON'} == 2) {
  54. system("rhythmbox-client --play-pause");
  55. } elsif ($ENV{'BLOCK_BUTTON'} == 3) {
  56. system("rhythmbox-client --next");
  57. }
  58. }
  59. }
  60. sub cmus {
  61. my @cmus = split /^/, qx(cmus-remote -Q);
  62. if ($? == 0) {
  63. foreach my $line (@cmus) {
  64. my @data = split /\s/, $line;
  65. if (shift @data eq 'tag') {
  66. my $key = shift @data;
  67. my $value = join ' ', @data;
  68. @metadata[0] = $value if $key eq 'artist';
  69. @metadata[1] = $value if $key eq 'title';
  70. }
  71. }
  72. if (@metadata) {
  73. buttons('cmus');
  74. # metadata found so we are done
  75. print(join ' - ', @metadata);
  76. exit 0;
  77. }
  78. }
  79. }
  80. sub mpd {
  81. my $data = qx(mpc current);
  82. if (not $data eq '') {
  83. buttons("mpd");
  84. print($data);
  85. exit 0;
  86. }
  87. }
  88. sub rhythmbox {
  89. buttons('rhythmbox');
  90. my $data = qx(rhythmbox-client --print-playing --no-start);
  91. print($data);
  92. }
  93. if ($player_arg eq '' or $player_arg =~ /mpd/) {
  94. mpd;
  95. }
  96. elsif ($player_arg =~ /cmus/) {
  97. cmus;
  98. }
  99. elsif ($player_arg =~ /rhythmbox/) {
  100. rhythmbox;
  101. }
  102. else {
  103. print('unknown');
  104. }
  105. print("\n");