| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 29 |
|
| 30 |
|
| 31 |
type CommandList struct { |
| 32 |
client *Client |
| 33 |
cmdQ *list.List |
| 34 |
} |
| 35 |
|
| 36 |
|
| 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 |
|
| 47 |
type PromisedID int |
| 48 |
|
| 49 |
|
| 50 |
|
| 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 |
|
| 59 |
|
| 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 |
|
| 68 |
|
| 69 |
func (c *Client) BeginCommandList() *CommandList { |
| 70 |
return &CommandList{c, list.New()} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
func (cl *CommandList) Ping() { |
| 75 |
cl.cmdQ.PushBack(&command{"ping", nil, cmdNoReturn}) |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
func (cl *CommandList) CurrentSong() *PromisedAttrs { |
| 80 |
pa := newPromisedAttrs() |
| 81 |
cl.cmdQ.PushBack(&command{"currentsong", pa, cmdAttrReturn}) |
| 82 |
return pa |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
func (cl *CommandList) Status() *PromisedAttrs { |
| 87 |
pa := newPromisedAttrs() |
| 88 |
cl.cmdQ.PushBack(&command{"status", pa, cmdAttrReturn}) |
| 89 |
return pa |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
func (cl *CommandList) Next() { |
| 98 |
cl.cmdQ.PushBack(&command{"next", nil, cmdNoReturn}) |
| 99 |
} |
| 100 |
|
| 101 |
|
| 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 |
|
| 111 |
|
| 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 |
|
| 121 |
|
| 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 |
|
| 131 |
func (cl *CommandList) Previous() { |
| 132 |
cl.cmdQ.PushBack(&command{"previous", nil, cmdNoReturn}) |
| 133 |
} |
| 134 |
|
| 135 |
|
| 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 |
|
| 141 |
|
| 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 |
|
| 147 |
func (cl *CommandList) Stop() { |
| 148 |
cl.cmdQ.PushBack(&command{"stop", nil, cmdNoReturn}) |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
func (cl *CommandList) SetVolume(volume int) { |
| 153 |
cl.cmdQ.PushBack(&command{fmt.Sprintf("setvol %d", volume), nil, cmdNoReturn}) |
| 154 |
} |
| 155 |
|
| 156 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 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 |
|
| 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 |
|
| 217 |
|
| 218 |
|
| 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 |
|
| 232 |
func (cl *CommandList) DeleteID(id int) { |
| 233 |
cl.cmdQ.PushBack(&command{fmt.Sprintf("deleteid %d", id), nil, cmdNoReturn}) |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 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 |
|
| 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 |
|
| 256 |
func (cl *CommandList) Add(uri string) { |
| 257 |
cl.cmdQ.PushBack(&command{fmt.Sprintf("add %s", quote(uri)), nil, cmdNoReturn}) |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 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 |
|
| 274 |
func (cl *CommandList) Clear() { |
| 275 |
cl.cmdQ.PushBack(&command{"clear", nil, cmdNoReturn}) |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 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 |
|
| 290 |
|
| 291 |
|
| 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 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 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 |
|
| 311 |
|
| 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 |
|
| 317 |
func (cl *CommandList) PlaylistClear(name string) { |
| 318 |
cl.cmdQ.PushBack(&command{fmt.Sprintf("playlistclear %s", quote(name)), nil, cmdNoReturn}) |
| 319 |
} |
| 320 |
|
| 321 |
|
| 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 |
|
| 327 |
|
| 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 |
|
| 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 |
|
| 338 |
|
| 339 |
func (cl *CommandList) PlaylistRemove(name string) { |
| 340 |
cl.cmdQ.PushBack(&command{fmt.Sprintf("rm %s", quote(name)), nil, cmdNoReturn}) |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
func (cl *CommandList) PlaylistSave(name string) { |
| 345 |
cl.cmdQ.PushBack(&command{fmt.Sprintf("save %s", quote(name)), nil, cmdNoReturn}) |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
func (cl *CommandList) End() error { |
| 350 |
|
| 351 |
|
| 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 |
|
| 360 |
defer cl.cmdQ.Init() |
| 361 |
|
| 362 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 413 |
return cl.client.readOKLine("OK") |
| 414 |
} |