-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.lua
256 lines (227 loc) · 6.84 KB
/
client.lua
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
local dbus = require("lua-dbus")
local INTERFACE = {
media = 'org.mpris.MediaPlayer2',
track = 'org.mpris.MediaPlayer2.TrackList',
player = 'org.mpris.MediaPlayer2.Player',
playlist = 'org.mpris.MediaPlayer2.PlayLists',
}
local function createinstance(meta, ...)
local instance = setmetatable({}, meta)
if instance.init then instance:init(...) end
return instance
end
local Player = { new = createinstance }
Player.__index = Player
function Player:init(name, opts)
self.id = name:match('^org%.mpris%.MediaPlayer2%.(.*)$') or ''
self.address = opts.address
self.properties = {}
self.name = name
self.path = opts.path
self.bus = opts.bus
if not self.address then
local player = self
dbus.owner(name, function (address)
player.address = address
-- start listing deferred property changes
for _, props in pairs(player.properties) do
for _, prop in ipairs(props) do
dbus.property.on(prop.name, prop.handler, {
interface = INTERFACE[prop.interface],
sender = player.address,
bus = player.bus,
})
end
end
end)
end
end
function Player:call(iface, method, ...)
if self.closed then return end
dbus.call(method, { args = {...},
interface = INTERFACE[iface],
destination = self.name,
path = self.path,
bus = self.bus,
})
end
function Player:get(iface, name, callback)
if self.closed then return end
dbus.property.get(name, callback, {
interface = INTERFACE[iface],
destination = self.name,
path = self.path,
bus = self.bus,
})
end
function Player:set(iface, name, value)
if self.closed then return end
dbus.property.set(name, value, {
interface = INTERFACE[iface],
destination = self.name,
path = self.path,
bus = self.bus,
})
end
function Player:change(iface, name, callback)
if self.closed then return end
local evname = string.format('%s.%s', iface, name)
self.properties[evname] = self.properties[evname] or {}
table.insert(self.properties[evname], {
name = name,
interface = iface,
handler = callback,
})
self:get(iface, name, callback)
if self.address then
dbus.property.on(name, callback, {
interface = INTERFACE[iface],
sender = self.address,
bus = self.bus,
})
end
end
function Player:raise()
self:call('media', 'Raise')
end
function Player:quit()
self:call('media', 'Quit')
end
function Player:play()
self:call('player', 'Play')
end
function Player:pause()
self:call('player', 'Pause')
end
function Player:playpause()
self:call('player', 'PlayPause')
end
function Player:stop()
self:call('player', 'Stop')
end
function Player:previous()
self:call('player', 'Previous')
end
function Player:next()
self:call('player', 'Next')
end
function Player:seek(offset)
self:call('player', 'Seek', 'x', offset)
end
function Player:position(trackid, position)
self:call('player', 'SetPosition', 'o', trackid, 'x', position)
end
function Player:uri(uri)
self:call('player', 'OpenUri', 's', uri)
end
function Player:close()
if self.closed then return end
self.closed = true
for _, prop in pairs(self.properties) do
for _, ev in ipairs(prop) do
dbus.property.off(ev.name, ev.handler, {
interface = INTERFACE[ev.interface],
sender = self.name,
bus = self.bus,
})
end
end
end
local Client = { new = createinstance }
Client.__index = Client
Client.Player = Player
Client.dbus = dbus
function Client:init()
self.bus = 'session'
self.path = '/org/mpris/MediaPlayer2'
end
function Client:getPlayerNames(callback)
return dbus.call('ListNames', function (names)
local players = {}
if type(names) == 'table' then
for _, name in ipairs(names) do
if tostring(name):match('^org%.mpris%.MediaPlayer2%.') then
table.insert(players, name)
end
end
end
return callback(players)
end, {
path = '/',
bus = self.bus,
destination = 'org.freedesktop.DBus',
interface = 'org.freedesktop.DBus',
})
end
function Client:getPlayers(callback)
local client = self
return self:getPlayerNames(function (names)
client.players = {}
for i, name in ipairs(names) do
client.players[name] = Player:new(name, client)
end
return callback(client.players)
end)
end
local function haz(t, value)
for _, v in ipairs(t) do
if v == value then
return true
end
end
return false
end
function Client:updatePlayers(callback)
local client = self
return self:getPlayerNames(function (names)
local playernames = {}
local added, removed, unchanged = {}, {}, {}
for id, player in pairs(client.players) do
table.insert(playernames, player.name)
if haz(names, player.name) then
table.insert(unchanged, player)
else
table.insert(removed, player)
client.players[id] = nil
player:close()
end
end
for _, name in ipairs(names) do
if not haz(playernames, name) then
local player = Player:new(name, client)
client.players[name] = player
table.insert(added, player)
end
end
return callback(added, removed, unchanged)
end)
end
function Client:onPlayer(callback)
local client = self
local opts = setmetatable({}, { __index = client })
return dbus.on('NameOwnerChanged', function (iface, removed, added)
if tostring(iface):match('^org%.mpris%.MediaPlayer2%.') then
local player = (client.players or {})[iface]
if added ~= '' and removed == '' then
if not player then
opts.address = added
player = Player:new(iface, opts)
client.players[iface] = player
end
elseif added == '' and removed ~= '' then
if player and player.address == removed then
client.players[iface] = nil
player:close()
end
elseif added ~= '' and removed ~= '' then
if player and player.address == removed then
player.address = added
end
end
if callback then
return callback(player)
end
end
end, { bus = self.bus, interface = 'org.freedesktop.DBus' })
end
return Client