aboutsummaryrefslogtreecommitdiffstats
path: root/_bin/music-player.sh
blob: e1e69792ef6a0c8e29f9f27c47596ffb978700c2 (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
137
138
139
140
141
142
143
144
145
#!/bin/sh
#
# Music player control wrapper to support different players.
#
# Aaron LI
# Created: 2016-01-07
# Updated: 2016-01-08
#

PLAYERS="mpd mocp"
#PLAYERS="mocp mpd"


if [ $# -ne 1 ]; then
    echo "Usage:"
    echo "    `basename $0` < state | info | play | pause | toggle | stop | previous | next >"
    exit 1
fi


escape() {
    echo "$1" | sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g;'
}


notify() {
    title="$1"
    text=`escape "$2"`
    icon="$3"
    notify-send -t 5000 --hint=int:transient:1 -i "${icon}" "${title}" "${text}"
}

# mpd/mpc
player_mpd() {
    case "$1" in
        state)
            status=`mpc status`
            if echo "${status}" | grep -q '^\[playing\]'; then
                echo "playing"
            elif echo "${status}" | grep -q '^\[paused\]'; then
                echo "paused"
            else
                echo "stopped"
            fi
            ;;
        info)
            state=`player_mpd state`
            #echo "state: ${state}"
            if [ "${state}" = "stopped" ]; then
                notify "MPD" "stopped"
            else
                artist=`mpc current -f "%artist%"`
                album=`mpc current -f "[%date% - ]%album%"`
                title=`mpc current -f "%title%"`
                notify "${title}" "${artist}\n${album}"
            fi
            ;;
        play)
            mpc play
            ;;
        pause)
            mpc pause
            ;;
        toggle)
            mpc toggle
            ;;
        stop)
            mpc stop
            ;;
        previous)
            mpc prev
            ;;
        next)
            mpc next
            ;;
        *)
            echo "ERROR: mpd/mpc: unknown command!"
            exit 11
            ;;
    esac
}


# moc/mocp
player_mocp() {
    case "$1" in
        state)
            state=`mocp -Q "%state"`
            [ "${state}" = "PLAY"  ] && echo "playing"
            [ "${state}" = "STOP"  ] && echo "stopped"
            [ "${state}" = "PAUSE" ] && echo "paused"
            ;;
        info)
            state=`mocp -Q "%state"`
            if [ "${state}" = "STOP" ]; then
                notify "MOC" "stopped"
            else
                artist=`mocp -Q "%artist"`
                album=`mocp -Q "%album"`
                title=`mocp -Q "%song"`
                notify "${title}" "${artist}\n${album}"
            fi
            ;;
        play)
            state=`mocp -Q "%state"`
            [ "${state}" = "STOP" ] && mocp -p || mocp -U
            ;;
        pause)
            mocp -P
            ;;
        toggle)
            mocp -G
            ;;
        stop)
            mocp -s
            ;;
        previous)
            mocp -r
            ;;
        next)
            mocp -f
            ;;
        *)
            echo "ERROR: mocp: unknown command!"
            exit 21
            ;;
    esac
}


for player in ${PLAYERS}; do
    if pgrep -x ${player} >/dev/null 2>&1; then
        player_cmd="player_${player}"
        #echo "Found player: ${player}"
        break
    fi
done

if [ -n "${player_cmd}" ]; then
    eval ${player_cmd} "$1"
else
    echo "ERROR: no known music player running/found"
    exit 31
fi