aboutsummaryrefslogtreecommitdiff
path: root/scripts/media_control.sh
blob: ae883dbbf3032e33f4e6c4b4d7a4c4d9ef6723b8 (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
#!/bin/bash

# To start the correct Player this script uses the file /tmp/media_control_last
f="/tmp/media_control_last"

function spotify_send_command {
        dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.$1
}

# arg parsing
if [ "$1" = "toggle" ]
then
    spotify_cmd="PlayPause"
elif [ "$1" = "next" ]
then
    spotify_cmd="Next"
elif [ "$1" = "prev" ]
then
    spotify_cmd="Previous"
fi

if [ "$spotify_cmd" != "" ]
then
    # check if spotify and mpd are running
    if pgrep spotify &>/dev/null
    then
        spotify_running=true
    else
        spotify_running=false
    fi

    if pgrep mpd &>/dev/null
    then
        mpd_running=true
        if type mpc &>/dev/null
        then
            mpd_controllable=true
        else
            mpd_controllable=false
        fi
    else
        mpd_running=false
    fi

    # both are running -> decide who gets the command
    # the playing program gets the commands 
    if $spotify_running && $mpd_running
    then
        if ! $mpd_controllable
        then
            spotify_send_command $spotify_cmd
            >&2 echo "mpd is running put mpc is not available to control it!"
            exit
        fi

        spotify_playing=$($DOTFILES_LOCATION/scripts/media_is_spotify_playing.sh)
        if mpc | grep "\[playing\]" &> /dev/null
        then
            mpd_playing=true
        else
            mpd_playing=false
        fi

        if $spotify_playing && $mpd_playing
        then
            >&2 echo "mpd and spotify are playing!"
        elif $spotify_playing
        then
            spotify_send_command $spotify_cmd
            echo spotify > $f
        elif $mpd_playing
        then
            mpc $1
            echo mpd > $f
        # nothing is actually running
        # read last running file
        else
            if [ -f $f ]
            then
                last=$(cat $f)

                if [ $last = "spotify" ]
                then
                    spotify_send_command $spotify_cmd
                elif [ $last = "mpd" ]
                then
                    mpc $1
                fi
            fi
        fi

    # only spotify is running
    elif $spotify_running
    then
        spotify_send_command $spotify_cmd
        echo spotify > $f
    # only mpd is running
    elif $mpd_running
    then
        if $mpd_controllable
        then
            mpc $1
            echo mpd > $f
        else
            >&2 echo "mpd is running put mpc is not available to control it!"
        fi
    fi
fi