-
Notifications
You must be signed in to change notification settings - Fork 3
/
mpd.go
323 lines (299 loc) · 7.27 KB
/
mpd.go
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package main
import (
"bufio"
"fmt"
"net"
"strconv"
"strings"
)
// TODO: Timeouts for network stuff.
// FIXME: Should I look for tags case-insensitively? The documentation
// doesn't say anything about this.
type mpdState int
const (
playMPDState mpdState = iota
stopMPDState
pauseMPDState
)
func handleMpdEvents(events chan event) {
for {
_, err := executeMPDCommand("idle playlist player")
if err != nil {
return
}
events <- updateStateEvent
}
}
func getState() (state state, err error) {
status, err := executeMPDCommand("status")
if err != nil {
return
}
if state.mpdState, err = getMPDState(status); err != nil {
return
}
if state.elapsed, err = getElapsed(status); err != nil {
return
}
if state.songID, err = getSongID(status); err != nil {
return
}
if err = fillQueue(&state); err != nil {
return
}
return
}
func fillQueue(state *state) error {
info, err := executeMPDCommand("playlistinfo")
if err != nil {
return err
}
var s song
var file, title, artist, album string
var track *int
for _, line := range strings.Split(info, "\n") {
split := strings.SplitN(line, ": ", 2)
switch split[0] {
case "file":
if len(file) > 0 {
s.displayName = composeDisplayName(file, title, artist, album, track)
state.queue = append(state.queue, s)
// Reset before parsing the next song:
file = ""
title = ""
artist = ""
album = ""
track = nil
}
if len(split) < 2 {
return fmt.Errorf("encountered empty URI")
}
file = split[1]
case "Id":
if len(split) > 1 {
if s.songID, err = strconv.Atoi(split[1]); err != nil {
return fmt.Errorf("could not parse songid: %s", err.Error())
}
}
case "duration":
if len(split) > 1 {
f, err := strconv.ParseFloat(split[1], 32)
if err != nil {
return fmt.Errorf("could not parse duration: %s", err.Error())
}
s.duration = float32(f)
}
case "Title":
if len(split) > 1 {
title = split[1]
}
case "Artist":
if len(split) > 1 {
artist = split[1]
}
case "Album":
if len(split) > 1 {
album = split[1]
}
case "Track":
if len(split) > 1 {
i, err := strconv.Atoi(split[1])
if err != nil {
return fmt.Errorf("could not parse track: %s", err.Error())
}
track = &i
}
}
}
if len(file) > 0 {
s.displayName = composeDisplayName(file, title, artist, album, track)
state.queue = append(state.queue, s) // add last song to queue
}
return nil
}
func composeDisplayName(file, title, artist, album string, track *int) string {
if title == "" {
return fmt.Sprintf("%s", file)
} else if artist == "" {
return fmt.Sprintf("%s", title)
} else if track == nil || album == "" {
return fmt.Sprintf("%s - %s", artist, title)
}
return fmt.Sprintf("[#%02d of %s] %s - %s", *track, album, artist, title)
}
func getMPDState(status string) (mpdState mpdState, err error) {
for _, line := range strings.Split(status, "\n") {
if strings.HasPrefix(line, "state: ") && len(line) > 7 {
switch line[7:] {
case "play":
return playMPDState, nil
case "stop":
return stopMPDState, nil
case "pause":
return pauseMPDState, nil
}
}
}
err = fmt.Errorf("mpdState not found")
return
}
func getElapsed(status string) (*float32, error) {
for _, line := range strings.Split(status, "\n") {
if strings.HasPrefix(line, "elapsed: ") && len(line) > 9 {
s, err := strconv.ParseFloat(line[9:], 32)
s32 := float32(s)
return &s32, err
}
}
return nil, nil
}
func getSongID(status string) (*int, error) {
for _, line := range strings.Split(status, "\n") {
if strings.HasPrefix(line, "songid: ") && len(line) > 8 {
i, err := strconv.Atoi(line[8:])
return &i, err
}
}
return nil, nil
}
func executeMPDCommand(command string) (resp string, err error) {
conn, err := initiateMPDConnection()
if conn != nil {
defer conn.Close()
}
if err != nil {
return
}
fmt.Fprintf(conn, "%s\n", command)
var respBuilder strings.Builder
var line string
connReader := bufio.NewReader(conn)
for {
if line, err = connReader.ReadString('\n'); err != nil {
return
}
if line == "OK\n" {
break
} else if strings.HasPrefix(line, "ACK ") {
msg := "received mpd error '%s' while executing '%s'"
err = fmt.Errorf(msg, strings.TrimSpace(line), command)
break
} else {
respBuilder.WriteString(line)
}
}
return respBuilder.String(), err
}
func initiateMPDConnection() (conn *net.TCPConn, err error) {
tcpAddr, err := net.ResolveTCPAddr("tcp", mpdAddr)
if err != nil {
return
}
conn, err = net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
return
}
var line string
connReader := bufio.NewReader(conn)
if line, err = connReader.ReadString('\n'); err != nil {
return
}
if !strings.HasPrefix(line, "OK MPD ") {
err = fmt.Errorf("no mpd server found")
}
return
}
func playHighlighted(state state) error {
if len(state.queue) == 0 {
return nil
}
song := state.queue[state.highlighted]
_, err := executeMPDCommand(fmt.Sprintf("playid %d", song.songID))
return err
}
func togglePause(state state) error {
switch state.mpdState {
case playMPDState:
_, err := executeMPDCommand("pause 1")
return err
case pauseMPDState:
_, err := executeMPDCommand("pause 0")
return err
}
return nil
}
func deleteHighlighted(state state) error {
if len(state.queue) == 0 {
return nil
}
song := state.queue[state.highlighted]
_, err := executeMPDCommand(fmt.Sprintf("deleteid %d", song.songID))
if err != nil && strings.Contains(err.Error(), "No such song") {
// This usually happens when pressing the delete button too quickly.
return nil
}
return err
}
func clear(state state) error {
_, err := executeMPDCommand("clear")
return err
}
func moveHighlightedUpwards(state *state) error {
if len(state.queue) == 0 {
return nil
}
if state.highlighted == 0 {
// Can't move over the top. Just ignore.
return nil
}
cmd := fmt.Sprintf("move %d %d", state.highlighted, state.highlighted-1)
state.highlighted -= 1
_, err := executeMPDCommand(cmd)
return err
}
func moveHighlightedDownwards(state *state) error {
if len(state.queue) == 0 {
return nil
}
if state.highlighted >= len(state.queue)-1 {
// Can't move below the bottom. Just ignore.
return nil
}
cmd := fmt.Sprintf("move %d %d", state.highlighted, state.highlighted+1)
state.highlighted += 1
_, err := executeMPDCommand(cmd)
return err
}
func seekBackwards(state state, seconds int) error {
if state.mpdState == stopMPDState {
return nil
}
_, err := executeMPDCommand(fmt.Sprintf("seekcur -%d", seconds))
return err
}
func seekForwards(state state, seconds int) error {
if state.mpdState == stopMPDState {
return nil
}
// Some logic is required here, because mpd behaves weirdly, when
// trying to seek across the end of a song:
song, err := getCurrentSong(state)
if err != nil {
return err
}
remaining := song.duration - *state.elapsed
if remaining <= 0.4 {
// No need to seek, if the end is almost reached.
return nil
} else if remaining-0.4 < float32(seconds) {
_, err = executeMPDCommand(fmt.Sprintf("seekcur +%f", remaining-0.3))
} else {
_, err = executeMPDCommand(fmt.Sprintf("seekcur +%d", seconds))
}
if err != nil && strings.Contains(err.Error(), "Decoder failed to seek") {
// This seems to happen, when trying to seek shortly after the song
// changed.
return nil
}
return err
}