diff options
author | Guillermo Ramos | 2021-04-13 18:17:14 +0200 |
---|---|---|
committer | Guillermo Ramos | 2021-04-13 18:17:14 +0200 |
commit | 04446011f364909096cb49f2b66e4df96d713209 (patch) | |
tree | 6c2bbb722d63487a766d91e5ecc9560f01442f44 /multiplayerctl | |
download | cli-04446011f364909096cb49f2b66e4df96d713209.tar.gz |
Initial commit
Diffstat (limited to 'multiplayerctl')
-rwxr-xr-x | multiplayerctl | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/multiplayerctl b/multiplayerctl new file mode 100755 index 0000000..011c7bc --- /dev/null +++ b/multiplayerctl @@ -0,0 +1,57 @@ +#!/usr/bin/env perl + +my $TMPFILE = '/tmp/multiplayerctl'; + +sub players { + my $players = {}; + for my $playerline (`bash -c "paste -d '|' <(playerctl -l) <(playerctl -a status)"`) { + chomp $playerline; + my ($player, $status) = split /\|/, $playerline; + $players->{$status} = [] unless $players->{$status}; + push @{$players->{$status}}, $player; + } + return $players; +} + +sub wrap { + my $player = shift; + + print " player=$player\n"; + + open(my $fd, '>', $TMPFILE) or die $?; + print $fd $player; + + my @args; + push @playerargs, ('-p', $player) if ($player); + exec 'playerctl', @playerargs, @ARGV; +} + +my $players = players(); +my @playing = @{$players->{'Playing'}}; +my @paused = @{$players->{'Paused'}}; + +print "Playing=(@playing) Paused=(@paused)"; + +my $last; +if (open(my $fd, '<', $TMPFILE)) { + $last = <$fd>; +} +print " last=$last"; + +if (grep /^$last$/, @playing) { + # If the last known player is playing, stop it first + wrap($last); +} elsif (@playing) { + # Take the first of the playing players + wrap($playing[0]); +} else { + # Nothing is currently playing + if (grep /^$last$/, @paused) { + # The last known player is paused; take it + print " matched=$last"; + wrap($last); + } elsif (@paused) { + # Take the first of the paused players + wrap($paused[0]); + } +} |