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

#TODO
# NOTE:
# if spotify is running toggle is consumed by spotify.


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=$(./media_is_spotify_playing.sh)
        if $(mpc | grep "\[playing\]")
        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
        elif $mpd_playing
        then
            mpc $1
            # nothing is actually running
            # don't know who was playing -> send spotify all commands 
        else
            spotify_send_command $spotify_cmd
        fi

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