Debian Codegraph @

mpd/commandlist.go

raw symbols SCIP
1 // Copyright 2013 The GoMPD Authors. All rights reserved.
2 // Use of this source code is governed by the MIT
3 // license that can be found in the LICENSE file.
4
5 package mpd
6
7 import (
8 "container/list"
9 "errors"
10 "fmt"
11 "strconv"
12 )
13
14 type cmdType uint
15
16 const (
17 cmdNoReturn cmdType = iota
18 cmdAttrReturn
19 cmdIDReturn
20 )
21
22 type command struct {
23 cmd string
24 promise interface{}
25 typeOf cmdType
26 }
27
28 // CommandList is for batch/mass MPD commands.
29 // See http://www.musicpd.org/doc/protocol/command_lists.html
30 // for more details.
31 type CommandList struct {
32 client *Client
33 cmdQ *list.List
34 }
35
36 // PromisedAttrs is a set of promised attributes (to be) returned by MPD.
37 type PromisedAttrs struct {
38 attrs Attrs
39 computed bool
40 }
41
42 func newPromisedAttrs() *PromisedAttrs {
43 return &PromisedAttrs{attrs: make(Attrs), computed: false}
44 }
45
46 // PromisedID is a promised identifier (to be) returned by MPD.
47 type PromisedID int
48
49 // Value returns the Attrs that were computed when CommandList.End was
50 // called. Returns an error if CommandList.End has not yet been called.
51 func (pa *PromisedAttrs) Value() (Attrs, error) {
52 if !pa.computed {
53 return nil, errors.New("value has not been computed yet")
54 }
55 return pa.attrs, nil
56 }
57
58 // Value returns the ID that was computed when CommandList.End was
59 // called. Returns an error if CommandList.End has not yet been called.
60 func (pi *PromisedID) Value() (int, error) {
61 if *pi == -1 {
62 return -1, errors.New("value has not been computed yet")
63 }
64 return (int)(*pi), nil
65 }
66
67 // BeginCommandList creates a new CommandList structure using
68 // this connection.
69 func (c *Client) BeginCommandList() *CommandList {
70 return &CommandList{c, list.New()}
71 }
72
73 // Ping sends a no-op message to MPD. It's useful for keeping the connection alive.
74 func (cl *CommandList) Ping() {
75 cl.cmdQ.PushBack(&command{"ping", nil, cmdNoReturn})
76 }
77
78 // CurrentSong returns information about the current song in the playlist.
79 func (cl *CommandList) CurrentSong() *PromisedAttrs {
80 pa := newPromisedAttrs()
81 cl.cmdQ.PushBack(&command{"currentsong", pa, cmdAttrReturn})
82 return pa
83 }
84
85 // Status returns information about the current status of MPD.
86 func (cl *CommandList) Status() *PromisedAttrs {
87 pa := newPromisedAttrs()
88 cl.cmdQ.PushBack(&command{"status", pa, cmdAttrReturn})
89 return pa
90 }
91
92 //
93 // Playback control
94 //
95
96 // Next plays next song in the playlist.
97 func (cl *CommandList) Next() {
98 cl.cmdQ.PushBack(&command{"next", nil, cmdNoReturn})
99 }
100
101 // Pause pauses playback if pause is true; resumes playback otherwise.
102 func (cl *CommandList) Pause(pause bool) {
103 if pause {
104 cl.cmdQ.PushBack(&command{"pause 1", nil, cmdNoReturn})
105 } else {
106 cl.cmdQ.PushBack(&command{"pause 0", nil, cmdNoReturn})
107 }
108 }
109
110 // Play starts playing the song at playlist position pos. If pos is negative,
111 // start playing at the current position in the playlist.
112 func (cl *CommandList) Play(pos int) {
113 if pos < 0 {
114 cl.cmdQ.PushBack(&command{"play", nil, cmdNoReturn})
115 } else {
116 cl.cmdQ.PushBack(&command{fmt.Sprintf("play %d", pos), nil, cmdNoReturn})
117 }
118 }
119
120 // PlayID plays the song identified by id. If id is negative, start playing
121 // at the currect position in playlist.
122 func (cl *CommandList) PlayID(id int) {
123 if id < 0 {
124 cl.cmdQ.PushBack(&command{"playid", nil, cmdNoReturn})
125 } else {
126 cl.cmdQ.PushBack(&command{fmt.Sprintf("playid %d", id), nil, cmdNoReturn})
127 }
128 }
129
130 // Previous plays previous song in the playlist.
131 func (cl *CommandList) Previous() {
132 cl.cmdQ.PushBack(&command{"previous", nil, cmdNoReturn})
133 }
134
135 // Seek seeks to the position time (in seconds) of the song at playlist position pos.
136 func (cl *CommandList) Seek(pos, time int) {
137 cl.cmdQ.PushBack(&command{fmt.Sprintf("seek %d %d", pos, time), nil, cmdNoReturn})
138 }
139
140 // SeekID is identical to Seek except the song is identified by it's id
141 // (not position in playlist).
142 func (cl *CommandList) SeekID(id, time int) {
143 cl.cmdQ.PushBack(&command{fmt.Sprintf("seekid %d %d", id, time), nil, cmdNoReturn})
144 }
145
146 // Stop stops playback.
147 func (cl *CommandList) Stop() {
148 cl.cmdQ.PushBack(&command{"stop", nil, cmdNoReturn})
149 }
150
151 // SetVolume sets the MPD volume level.
152 func (cl *CommandList) SetVolume(volume int) {
153 cl.cmdQ.PushBack(&command{fmt.Sprintf("setvol %d", volume), nil, cmdNoReturn})
154 }
155
156 // Random enables random playback, if random is true, disables it otherwise.
157 func (cl *CommandList) Random(random bool) {
158 if random {
159 cl.cmdQ.PushBack(&command{"random 1", nil, cmdNoReturn})
160 } else {
161 cl.cmdQ.PushBack(&command{"random 0", nil, cmdNoReturn})
162 }
163 }
164
165 // Repeat enables repeat mode, if repeat is true, disables it otherwise.
166 func (cl *CommandList) Repeat(repeat bool) {
167 if repeat {
168 cl.cmdQ.PushBack(&command{"repeat 1", nil, cmdNoReturn})
169 } else {
170 cl.cmdQ.PushBack(&command{"repeat 0", nil, cmdNoReturn})
171 }
172 }
173
174 // Single enables single song mode, if single is true, disables it otherwise.
175 func (cl *CommandList) Single(single bool) {
176 if single {
177 cl.cmdQ.PushBack(&command{"single 1", nil, cmdNoReturn})
178 } else {
179 cl.cmdQ.PushBack(&command{"single 0", nil, cmdNoReturn})
180 }
181 }
182
183 // Consume enables consume mode, if consume is true, disables it otherwise.
184 func (cl *CommandList) Consume(consume bool) {
185 if consume {
186 cl.cmdQ.PushBack(&command{"consume 1", nil, cmdNoReturn})
187 } else {
188 cl.cmdQ.PushBack(&command{"consume 0", nil, cmdNoReturn})
189 }
190 }
191
192 //
193 // Playlist related functions
194 //
195
196 // SetPriority sets the priority for songs in the playlist. If both start and
197 // end are non-negative, it updates those at positions in range [start, end).
198 // If end is negative, it updates the song at position start.
199 func (cl *CommandList) SetPriority(priority, start, end int) error {
200 if start < 0 {
201 return errors.New("negative start index")
202 }
203 if end < 0 {
204 cl.cmdQ.PushBack(&command{fmt.Sprintf("prio %d %d", priority, start), nil, cmdNoReturn})
205 } else {
206 cl.cmdQ.PushBack(&command{fmt.Sprintf("prio %d %d:%d", priority, start, end), nil, cmdNoReturn})
207 }
208 return nil
209 }
210
211 // SetPriorityID sets the priority for the song identified by id.
212 func (cl *CommandList) SetPriorityID(priority, id int) {
213 cl.cmdQ.PushBack(&command{fmt.Sprintf("prioid %d %d", priority, id), nil, cmdNoReturn})
214 }
215
216 // Delete deletes songs from playlist. If both start and end are positive,
217 // it deletes those at positions in range [start, end). If end is negative,
218 // it deletes the song at position start.
219 func (cl *CommandList) Delete(start, end int) error {
220 if start < 0 {
221 return errors.New("negative start index")
222 }
223 if end < 0 {
224 cl.cmdQ.PushBack(&command{fmt.Sprintf("delete %d", start), nil, cmdNoReturn})
225 } else {
226 cl.cmdQ.PushBack(&command{fmt.Sprintf("delete %d:%d", start, end), nil, cmdNoReturn})
227 }
228 return nil
229 }
230
231 // DeleteID deletes the song identified by id.
232 func (cl *CommandList) DeleteID(id int) {
233 cl.cmdQ.PushBack(&command{fmt.Sprintf("deleteid %d", id), nil, cmdNoReturn})
234 }
235
236 // Move moves the songs between the positions start and end to the new position
237 // position. If end is negative, only the song at position start is moved.
238 func (cl *CommandList) Move(start, end, position int) error {
239 if start < 0 {
240 return errors.New("negative start index")
241 }
242 if end < 0 {
243 cl.cmdQ.PushBack(&command{fmt.Sprintf("move %d %d", start, position), nil, cmdNoReturn})
244 } else {
245 cl.cmdQ.PushBack(&command{fmt.Sprintf("move %d:%d %d", start, end, position), nil, cmdNoReturn})
246 }
247 return nil
248 }
249
250 // MoveID moves songid to position on the playlist.
251 func (cl *CommandList) MoveID(songid, position int) {
252 cl.cmdQ.PushBack(&command{fmt.Sprintf("moveid %d %d", songid, position), nil, cmdNoReturn})
253 }
254
255 // Add adds the file/directory uri to playlist. Directories add recursively.
256 func (cl *CommandList) Add(uri string) {
257 cl.cmdQ.PushBack(&command{fmt.Sprintf("add %s", quote(uri)), nil, cmdNoReturn})
258 }
259
260 // AddID adds the file/directory uri to playlist and returns the identity
261 // id of the song added. If pos is positive, the song is added to position
262 // pos.
263 func (cl *CommandList) AddID(uri string, pos int) *PromisedID {
264 var id PromisedID = -1
265 if pos >= 0 {
266 cl.cmdQ.PushBack(&command{fmt.Sprintf("addid %s %d", quote(uri), pos), &id, cmdIDReturn})
267 } else {
268 cl.cmdQ.PushBack(&command{fmt.Sprintf("addid %s", quote(uri)), &id, cmdIDReturn})
269 }
270 return &id
271 }
272
273 // Clear clears the current playlist.
274 func (cl *CommandList) Clear() {
275 cl.cmdQ.PushBack(&command{"clear", nil, cmdNoReturn})
276 }
277
278 // Shuffle shuffles the tracks from position start to position end in the
279 // current playlist. If start or end is negative, the whole playlist is
280 // shuffled.
281 func (cl *CommandList) Shuffle(start, end int) {
282 if start < 0 || end < 0 {
283 cl.cmdQ.PushBack(&command{"shuffle", nil, cmdNoReturn})
284 return
285 }
286 cl.cmdQ.PushBack(&command{fmt.Sprintf("shuffle %d:%d", start, end), nil, cmdNoReturn})
287 }
288
289 // Update updates MPD's database: find new files, remove deleted files, update
290 // modified files. uri is a particular directory or file to update. If it is an
291 // empty string, everything is updated.
292 func (cl *CommandList) Update(uri string) (attrs *PromisedAttrs) {
293 attrs = newPromisedAttrs()
294 cl.cmdQ.PushBack(&command{fmt.Sprintf("update %s", quote(uri)), attrs, cmdAttrReturn})
295 return
296 }
297
298 // Stored playlists related commands.
299
300 // PlaylistLoad loads the specfied playlist into the current queue.
301 // If start and end are non-negative, only songs in this range are loaded.
302 func (cl *CommandList) PlaylistLoad(name string, start, end int) {
303 if start < 0 || end < 0 {
304 cl.cmdQ.PushBack(&command{fmt.Sprintf("load %s", quote(name)), nil, cmdNoReturn})
305 } else {
306 cl.cmdQ.PushBack(&command{fmt.Sprintf("load %s %d:%d", quote(name), start, end), nil, cmdNoReturn})
307 }
308 }
309
310 // PlaylistAdd adds a song identified by uri to a stored playlist identified
311 // by name.
312 func (cl *CommandList) PlaylistAdd(name string, uri string) {
313 cl.cmdQ.PushBack(&command{fmt.Sprintf("playlistadd %s %s", quote(name), quote(uri)), nil, cmdNoReturn})
314 }
315
316 // PlaylistClear clears the specified playlist.
317 func (cl *CommandList) PlaylistClear(name string) {
318 cl.cmdQ.PushBack(&command{fmt.Sprintf("playlistclear %s", quote(name)), nil, cmdNoReturn})
319 }
320
321 // PlaylistDelete deletes the song at position pos from the specified playlist.
322 func (cl *CommandList) PlaylistDelete(name string, pos int) {
323 cl.cmdQ.PushBack(&command{fmt.Sprintf("playlistdelete %s %d", quote(name), pos), nil, cmdNoReturn})
324 }
325
326 // PlaylistMove moves a song identified by id in a playlist identified by name
327 // to the position pos.
328 func (cl *CommandList) PlaylistMove(name string, id, pos int) {
329 cl.cmdQ.PushBack(&command{fmt.Sprintf("playlistmove %s %d %d", quote(name), id, pos), nil, cmdNoReturn})
330 }
331
332 // PlaylistRename renames the playlist identified by name to newName.
333 func (cl *CommandList) PlaylistRename(name, newName string) {
334 cl.cmdQ.PushBack(&command{fmt.Sprintf("rename %s %s", quote(name), quote(newName)), nil, cmdNoReturn})
335 }
336
337 // PlaylistRemove removes the playlist identified by name from the playlist
338 // directory.
339 func (cl *CommandList) PlaylistRemove(name string) {
340 cl.cmdQ.PushBack(&command{fmt.Sprintf("rm %s", quote(name)), nil, cmdNoReturn})
341 }
342
343 // PlaylistSave saves the current playlist as name in the playlist directory.
344 func (cl *CommandList) PlaylistSave(name string) {
345 cl.cmdQ.PushBack(&command{fmt.Sprintf("save %s", quote(name)), nil, cmdNoReturn})
346 }
347
348 // End executes the command list.
349 func (cl *CommandList) End() error {
350
351 // Tell MPD to start an OK command list:
352 beginID, beginErr := cl.client.cmd("command_list_ok_begin")
353 if beginErr != nil {
354 return beginErr
355 }
356 cl.client.text.StartResponse(beginID)
357 cl.client.text.EndResponse(beginID)
358
359 // Ensure the queue is cleared regardless.
360 defer cl.cmdQ.Init()
361
362 // Issue all of the queued up commands in the list:
363 for e := cl.cmdQ.Front(); e != nil; e = e.Next() {
364 cmdID, cmdErr := cl.client.cmd(e.Value.(*command).cmd)
365 if cmdErr != nil {
366 return cmdErr
367 }
368 cl.client.text.StartResponse(cmdID)
369 cl.client.text.EndResponse(cmdID)
370 }
371
372 // Tell MPD to end the command list and do the operations.
373 endID, endErr := cl.client.cmd("command_list_end")
374 if endErr != nil {
375 return endErr
376 }
377 cl.client.text.StartResponse(endID)
378 defer cl.client.text.EndResponse(endID)
379
380 // Get the responses back and check for errors:
381 for e := cl.cmdQ.Front(); e != nil; e = e.Next() {
382 switch e.Value.(*command).typeOf {
383
384 case cmdNoReturn:
385 if err := cl.client.readOKLine("list_OK"); err != nil {
386 return err
387 }
388
389 case cmdAttrReturn:
390 a, aErr := cl.client.readAttrs("list_OK")
391 if aErr != nil {
392 return aErr
393 }
394 pa := e.Value.(*command).promise.(*PromisedAttrs)
395 pa.attrs = a
396 pa.computed = true
397
398 case cmdIDReturn:
399 a, aErr := cl.client.readAttrs("list_OK")
400 if aErr != nil {
401 return aErr
402 }
403 rid, ridErr := strconv.Atoi(a["Id"])
404 if ridErr != nil {
405 return ridErr
406 }
407 *(e.Value.(*command).promise.(*PromisedID)) = PromisedID(rid)
408
409 }
410 }
411
412 // Finalize the command list with the last OK:
413 return cl.client.readOKLine("OK")
414 }