From 074117bdd9ac21aa5b867966edc2953656a55ab8 Mon Sep 17 00:00:00 2001 From: Arti Zirk Date: Tue, 9 Apr 2019 13:35:57 +0300 Subject: [PATCH] Fix playerctl output --- .config/i3blocks/config | 1 + .config/i3blocks/mediaplayer | 78 ++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100755 .config/i3blocks/mediaplayer diff --git a/.config/i3blocks/config b/.config/i3blocks/config index 4df182f..649014b 100644 --- a/.config/i3blocks/config +++ b/.config/i3blocks/config @@ -112,6 +112,7 @@ min_width=CPU: 100.00% # This displays "ARTIST - SONG" if a music is playing. # Supported players are: spotify, vlc, audacious, xmms2, mplayer, and others. [mediaplayer] +command=~/.config/i3blocks/$BLOCK_NAME #instance=spoti interval=5 signal=10 diff --git a/.config/i3blocks/mediaplayer b/.config/i3blocks/mediaplayer new file mode 100755 index 0000000..a2792e6 --- /dev/null +++ b/.config/i3blocks/mediaplayer @@ -0,0 +1,78 @@ +#!/usr/bin/perl +# Copyright (C) 2014 Tony Crisci + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Requires playerctl binary to be in your path (except cmus) +# See: https://github.com/acrisci/playerctl + +# Set instance=NAME in the i3blocks configuration to specify a music player +# (playerctl will attempt to connect to org.mpris.MediaPlayer2.[NAME] on your +# DBus session). + +use Env qw(BLOCK_INSTANCE); + +my @metadata = (); +my $player_arg = ""; + +if ($BLOCK_INSTANCE) { + $player_arg = "--player='$BLOCK_INSTANCE'"; +} + +if ($ENV{'BLOCK_BUTTON'} == 1) { + system("playerctl $player_arg previous"); +} elsif ($ENV{'BLOCK_BUTTON'} == 2) { + system("playerctl $player_arg play-pause"); +} elsif ($ENV{'BLOCK_BUTTON'} == 3) { + system("playerctl $player_arg next"); +} + +if ($player_arg eq '' or $player_arg =~ /cmus$/) { + # try cmus first + my @cmus = split /^/, qx(cmus-remote -Q); + if ($? == 0) { + foreach my $line (@cmus) { + my @data = split /\s/, $line; + if (shift @data eq 'tag') { + my $key = shift @data; + my $value = join ' ', @data; + + @metadata[0] = $value if $key eq 'artist'; + @metadata[1] = $value if $key eq 'title'; + } + } + + if (@metadata) { + # metadata found so we are done + print(join ' - ', @metadata); + exit 0; + } + } + + # if cmus was given, we are done + exit 0 unless $player_arg eq ''; +} + +my $artist = qx(playerctl $player_arg metadata artist); +chomp $artist; +# exit status will be nonzero when playerctl cannot find your player +exit(0) if $?; +push(@metadata, $artist) if $artist; + +my $title = qx(playerctl $player_arg metadata title); +chomp $title; +exit(0) if $?; +push(@metadata, $title) if $title; + +print(join(" - ", @metadata)) if @metadata;