summaryrefslogtreecommitdiff
path: root/.local/bin/blocks/player
blob: cc17a4eda72ed4af4fa9bf2829a672821bb9ce16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env perl

use Time::HiRes qw(usleep);
use Env qw(BLOCK_INSTANCE);

use constant DELAY => 50; # Delay in ms to let network-based players (spotify) reflect new data.
use constant SPOTIFY_STR => 'spotify';

my $label = $ENV{LABEL} // "";
my @metadata = ();
my $player_arg = "";

if ($BLOCK_INSTANCE) {
    $player_arg = "--player='$BLOCK_INSTANCE'";
}

sub buttons {
    my $method = shift;

    if($method eq 'mpd') {
        if ($ENV{'BLOCK_BUTTON'} == 1) {
            system("mpc prev");
        } elsif ($ENV{'BLOCK_BUTTON'} == 2) {
            system("mpc toggle");
        } elsif ($ENV{'BLOCK_BUTTON'} == 3) {
            system("mpc next");
        } elsif ($ENV{'BLOCK_BUTTON'} == 4) {
            system("mpc volume +10");
        } elsif ($ENV{'BLOCK_BUTTON'} == 5) {
            system("mpc volume -10");
        }
    } elsif ($method eq 'cmus') {
        if ($ENV{'BLOCK_BUTTON'} == 1) {
            system("cmus-remote --prev");
        } elsif ($ENV{'BLOCK_BUTTON'} == 2) {
            system("cmus-remote --pause");
        } elsif ($ENV{'BLOCK_BUTTON'} == 3) {
            system("cmus-remote --next");
        }
    } elsif ($method eq 'playerctl') {
        if ($ENV{'BLOCK_BUTTON'} == 1) {
            system("playerctl $player_arg previous");
            usleep(DELAY * 1000) if $BLOCK_INSTANCE eq SPOTIFY_STR;
        } elsif ($ENV{'BLOCK_BUTTON'} == 2) {
            system("playerctl $player_arg play-pause");
        } elsif ($ENV{'BLOCK_BUTTON'} == 3) {
            system("playerctl $player_arg next");
            usleep(DELAY * 1000) if $BLOCK_INSTANCE eq SPOTIFY_STR;
        } elsif ($ENV{'BLOCK_BUTTON'} == 4) {
            system("playerctl $player_arg volume 0.01+");
        } elsif ($ENV{'BLOCK_BUTTON'} == 5) {
            system("playerctl $player_arg volume 0.01-");
        }
    } elsif ($method eq 'rhythmbox') {
        if ($ENV{'BLOCK_BUTTON'} == 1) {
            system("rhythmbox-client --previous");
        } elsif ($ENV{'BLOCK_BUTTON'} == 2) {
            system("rhythmbox-client --play-pause");
        } elsif ($ENV{'BLOCK_BUTTON'} == 3) {
            system("rhythmbox-client --next");
        }
    }
}

print("$label ");
sub cmus {
    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) {
            buttons('cmus');

            # metadata found so we are done
            print(join ' - ', @metadata);
            exit 0;
        }
    }
}

sub mpd {
    my $data = qx(mpc current);
    if (not $data eq '') {
        buttons("mpd");
        print($data);
        exit 0;
    }
}

sub playerctl {
    buttons('playerctl');

    my $artist = qx(playerctl $player_arg metadata artist);
    chomp $artist;
    # exit status will be nonzero when playerctl cannot find your player
    exit(0) if $? || $artist eq '(null)';

    push(@metadata, $artist) if $artist;

    my $title = qx(playerctl $player_arg metadata title);
    exit(0) if $? || $title eq '(null)';

    push(@metadata, $title) if $title;

    print(join(" - ", @metadata)) if @metadata;
}

sub rhythmbox {
    buttons('rhythmbox');

    my $data = qx(rhythmbox-client --print-playing --no-start);
    print($data);
}

if ($player_arg eq '' or $player_arg =~ /mpd/) {
    mpd;
}
elsif ($player_arg =~ /cmus/) {
    cmus;
}
elsif ($player_arg =~ /rhythmbox/) {
    rhythmbox;
}
else {
    playerctl;
}
print("\n");