-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathapi_queue.go
More file actions
104 lines (93 loc) · 2.98 KB
/
api_queue.go
File metadata and controls
104 lines (93 loc) · 2.98 KB
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
package luaplugin
import lua "github.com/yuin/gopher-lua"
// registerQueueAPI adds cliamp.queue.* to the cliamp table.
//
// Reads (list/count/current) need no permission and pull from the StateProvider.
// Mutators (add/jump/remove/move) require permissions = {"control"} and route
// through the ControlProvider, which dispatches them onto the UI loop.
//
// All indices are 0-based, matching cliamp.queue.current().
func registerQueueAPI(L *lua.LState, cliamp *lua.LTable, state *StateProvider, ctrl *ControlProvider, p *Plugin, logger *pluginLogger) {
tbl := L.NewTable()
// cliamp.queue.list() -> array of {title, artist, album, path, index, queued}
L.SetField(tbl, "list", L.NewFunction(func(L *lua.LState) int {
out := L.NewTable()
if state.QueueList != nil {
for i, e := range state.QueueList() {
row := L.NewTable()
row.RawSetString("title", lua.LString(e.Title))
row.RawSetString("artist", lua.LString(e.Artist))
row.RawSetString("album", lua.LString(e.Album))
row.RawSetString("path", lua.LString(e.Path))
row.RawSetString("index", lua.LNumber(e.Index))
row.RawSetString("queued", lua.LBool(e.Queued))
out.RawSetInt(i+1, row)
}
}
L.Push(out)
return 1
}))
// cliamp.queue.count() -> number of tracks
L.SetField(tbl, "count", L.NewFunction(func(L *lua.LState) int {
n := 0
if state.PlaylistCount != nil {
n = state.PlaylistCount()
}
L.Push(lua.LNumber(n))
return 1
}))
// cliamp.queue.current() -> 0-based index of the current track
L.SetField(tbl, "current", L.NewFunction(func(L *lua.LState) int {
idx := 0
if state.CurrentIndex != nil {
idx = state.CurrentIndex()
}
L.Push(lua.LNumber(idx))
return 1
}))
warned := false
guard := func(name string) bool {
if !p.perms[PermControl] {
if !warned {
logger.log(p.Name, "warn", "queue.%s requires permissions = {\"control\"} — further warnings suppressed", name)
warned = true
}
return false
}
return true
}
// cliamp.queue.add(path) — resolve a file/dir/URL and append to the playlist.
L.SetField(tbl, "add", L.NewFunction(func(L *lua.LState) int {
path := L.CheckString(1)
if guard("add") && ctrl.QueueAdd != nil {
ctrl.QueueAdd(path)
}
return 0
}))
// cliamp.queue.jump(index) — make index the current track and play it.
L.SetField(tbl, "jump", L.NewFunction(func(L *lua.LState) int {
index := L.CheckInt(1)
if guard("jump") && ctrl.QueueJump != nil {
ctrl.QueueJump(index)
}
return 0
}))
// cliamp.queue.remove(index) — remove the track at index.
L.SetField(tbl, "remove", L.NewFunction(func(L *lua.LState) int {
index := L.CheckInt(1)
if guard("remove") && ctrl.QueueRemove != nil {
ctrl.QueueRemove(index)
}
return 0
}))
// cliamp.queue.move(from, to) — reorder a track.
L.SetField(tbl, "move", L.NewFunction(func(L *lua.LState) int {
from := L.CheckInt(1)
to := L.CheckInt(2)
if guard("move") && ctrl.QueueMove != nil {
ctrl.QueueMove(from, to)
}
return 0
}))
L.SetField(cliamp, "queue", tbl)
}