-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote.lua
603 lines (514 loc) · 15.3 KB
/
remote.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
local data = require("data")
local device = require("device")
local ffi = require("ffi")
local fs = libs.fs;
local log = require("log")
local server = require("server")
-----------------------------------------------------------
-- FFI interface
-----------------------------------------------------------
-- POSIX constants
local AF_UNIX = 1
local SOCK_STREAM = 1
local SOCK_NONBLOCK = 2048
local EAGAIN = 11
local POLLIN = 1
ffi.cdef[[
typedef unsigned short int sa_family_t;
typedef unsigned short int nfds_t;
typedef int socklen_t;
typedef int ssize_t;
typedef struct {
sa_family_t sun_family;
char sun_path[108];
} sockaddr_un;
typedef struct {
int fd;
short events;
short revents;
} pollfd;
int socket(int domain, int type, int protocol);
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
ssize_t write(int fd, const void *buf, size_t count);
ssize_t read(int fd, void *buf, size_t count);
int poll(pollfd *fds, nfds_t nfds, int timeout);
char *strerror(int errnum);
int close(int fd);
]]
-- Convert errno to a Lua string.
local function strerror()
return ffi.string(ffi.C.strerror(ffi.errno()))
end
-----------------------------------------------------------
-- IPC interface
-----------------------------------------------------------
local fd = nil
-- Disconnect from ddb, resetting some values.
local function disconnect()
if fd then
ffi.C.close(fd)
fd = nil
end
if tid then
libs.timer.cancel(tid)
tid = nil
end
layout.onoff.icon = "off"
layout.onoff.color = "red"
end
-- Connect to ddb at the given path.
local function connect(path)
-- Make sure we start off in a disconnected state.
disconnect()
requests = {}
next_request_id = 1
listeners = {
["property-change"] = function (tbl)
if tbl.property and observers[tbl.property] then
observers[tbl.property](tbl)
end
end
}
observers = {}
-- Use the supplied path, or the one from the settings.
path = path or settings.input_ipc_server
-- Check that the socket exists.
if not path or path == "" then
device.toast("No ddb IPC path configured.")
return false
elseif not events.detect() then
device.toast("No ddb IPC server at '"..path.."'")
return false
end
fd = ffi.C.socket(AF_UNIX, SOCK_STREAM + SOCK_NONBLOCK, 0)
if fd < 0 then
device.toast("Failed to set up")
log.error("Failed to create socket: "..strerror())
fd = nil
return false
end
local sockaddr = ffi.new("sockaddr_un")
sockaddr.sun_family = AF_UNIX
sockaddr.sun_path = path
if ffi.C.connect(fd, ffi.cast("const struct sockaddr*", sockaddr), ffi.sizeof(sockaddr)) ~= 0 then
device.toast("Failed to connect to ddb at '"..path.."'")
log.warn("Failed to connect to '"..path.."': "..strerror())
disconnect()
return false
end
layout.onoff.icon = "on"
layout.onoff.color = "green"
return true
end
-- Send a command to ddb, registering a callback to handle any responses
local function send_with_callback(callback, command, ...)
if not fd then
return false
end
local message = { command = command}
if ... then
message.args = ...
end
if callback then
requests[next_request_id] = callback
message.request_id = next_request_id
next_request_id = next_request_id + 1
end
local json = data.tojson(message) .. "\n"
local len = #json
local ret = ffi.C.write(fd, json, len)
if ret < 0 then
device.toast("Failed to communicate with ddb")
log.warn("Failed to send: "..strerror())
disconnect()
return false
end
return ret == len
end
-- Read any messages from ddb.
local function read()
if not fd then
return nil
end
local out = ""
local buf = ffi.new("char[4096]")
local pollfds = ffi.new("pollfd[1]")
pollfds[0].fd = fd
pollfds[0].events = POLLIN
ffi.C.poll(pollfds, 1, 50)
if pollfds[0].revents == 0 then
return nil
end
repeat
local ret = ffi.C.read(fd, buf, 4096)
if ret < 0 then
if ffi.errno() == EAGAIN then
break
else
log.warn("Failed to read: "..strerror())
end
disconnect()
return nil
elseif ret > 0 then
out = out .. ffi.string(buf, ret)
end
until ret == 0
if out:len() > 0 then
return out
else
return nil
end
end
-- Send one or more commands to ddb.
local function send ( ... )
send_with_callback(nil, ... )
end
-- Observe a property
local function observe_property(name, callback)
observers[name] = callback
send("observe-property", { property = name } )
end
----------------------------------------------------------
-- UI handlers
----------------------------------------------------------
-- Update the seekbar
local function ui_seek(message)
if message.data then
local pos = 100 * message.data.position/message.data.duration
layout.seek_slider.progress = string.format("%2.0f", pos)
local d_min = math.floor(message.data.duration/60)
local d_s = math.floor(message.data.duration) % 60
local p_min = math.floor(message.data.position/60)
local p_s = math.floor(message.data.position) % 60
layout.seek_slider.text = string.format("%d:%02d / %d:%02d", p_min, p_s, d_min, d_s)
end
end
-- Update the volume bar
local function ui_update_volume(message)
if message.value then
layout.volume_slider.progress = string.format("%2.0f", message.value)
end
end
-- Update mute status
local function ui_update_mute(message)
if message.value == 0 then
layout.volume_slider.color = "green"
else
layout.volume_slider.color = "red"
end
end
-- Set the title
local function ui_set_title(message)
if message["now-playing"] then
layout.media_title.text = message["now-playing"]
end
server.update( {"id = media-title", weight = "wrap" } )
end
-- Update the stop after buttons
local function ui_update_stop_after_current_track(message)
if message.value and message.value ~= 0 then
layout.stop_after_current_track.checked = true
layout.stop_after_current_track.color = "green"
else
layout.stop_after_current_track.checked = false
layout.stop_after_current_track.color = "gray"
end
server.update()
end
local function ui_update_stop_after_current_album(message)
if message.value and message.value ~= 0 then
layout.stop_after_current_album.checked = true
layout.stop_after_current_album.color = "green"
else
layout.stop_after_current_album.checked = false
layout.stop_after_current_album.color = "gray"
end
server.update()
end
local repeats = {"Off", "One", "All"}
local function ui_update_repeats(message)
log.warn("repeat: " .. message.value)
local repeat_children = {}
for k, v in ipairs(repeats) do
local bool = (message.value == string.lower(v))
local item = {
type = "item",
checked = bool,
text = v,
}
table.insert(repeat_children, item)
end
server.update( {id = "repeat_list", children = repeat_children} )
end
local function ui_update_cover_art(message)
if not message["blob"] and not message["filename"] then
return
end
tmpf = fs.temp()
if message["blob"] then
log.info("writing cover from blob")
fs.write(tmpf, data.frombase64(message["blob"]))
end
if message["filename"] then
log.warn("copying cover from file")
fs.copy(message["filename"], tmpf)
end
server.update( {id = "cover_art", image=tmpf} )
end
local shuffles = {"Off", "Tracks", "Albums", "Random"}
local function ui_update_shuffles(message)
log.warn("shuffle: " .. message.value)
local shuffle_children = {}
for k, v in ipairs(shuffles) do
local bool = (message.value == string.lower(v))
local item = {
type = "item",
checked = bool,
text = v,
}
table.insert(shuffle_children, item)
end
server.update( {id = "shuffle_list", children = shuffle_children} )
end
local function ui_populate_playlist(message)
if message.items == nil then
return nil
end
local make_item = function (data)
return { type = "item", checked = false , text = data }
end
local items = {}
for n, i in ipairs(message.items) do
local t = make_item(i)
t.ontap = function (index)
send("play-track-by-idx", { idx = n } )
end
table.insert(items, t)
end
server.update( {id = "track_list", children = items} )
end
local function ui_set_playlist_title(message)
if message.title == nil then
return
end
settings.plt_idx = message.idx
server.update( {id = "playlist_title", text = message.title} )
end
-- Initialize the UI to reflect the current state
local function initialize_ui()
fmt = "%artist% - '['%album% - #%tracknumber%']' %title%"
send_with_callback(ui_set_title, "get-now-playing", { format = fmt })
send_with_callback(ui_update_volume, "get-property", { property = "volume" })
observe_property("volume", ui_update_volume)
send_with_callback(ui_update_mute, "get-property", { property = "mute" })
observe_property("mute", ui_update_mute)
send_with_callback(ui_seek, "get-playpos")
-- poll ddb for progress regularly
local playpos_freq = 100 -- ms
-- uncomment to poll less frequently -- prevents spamming the debug log
-- local playpos_freq = 1000 * 100 -- ms
libs.timer.interval(
function() send_with_callback(ui_seek, "get-playpos") end,
playpos_freq
)
listeners["seek"] = ui_seek
send_with_callback(ui_update_repeats, "get-property", {property = "repeat"} )
send_with_callback(ui_update_shuffles, "get-property", {property = "shuffle"} )
observe_property("repeat", ui_update_repeats)
observe_property("shuffle", ui_update_shuffles)
send_with_callback(ui_update_stop_after_current_track, "get-property", { property = "playlist.stop_after_current" })
send_with_callback(ui_update_stop_after_current_album, "get-property", { property = "playlist.stop_after_album" })
observe_property("playlist.stop_after_current", ui_update_stop_after_current_track)
observe_property("playlist.stop_after_album", ui_update_stop_after_current_album)
send_with_callback(ui_update_cover_art, "request-cover-art", { accept = {"filename", "blob"} } )
listeners["track-changed"] = function (message)
send_with_callback(ui_update_cover_art, "request-cover-art", { accept = {"filename", "blob"} })
send_with_callback(ui_set_title, "get-now-playing", { format = fmt })
end
send_with_callback(ui_set_playlist_title, "get-current-playlist")
listeners["playlist-switched"] = function (message)
settings.plt_idx = message.idx
send_with_callback(ui_set_playlist_title, "get-current-playlist")
send_with_callback(ui_populate_playlist, "get-playlist-contents", { idx = tonumber(settings.plt_idx) })
end
return nil
end
-----------------------------------------------------------
-- Remote events
-----------------------------------------------------------
-- Always allow a few actions, without trying to connect.
local allow = {
onoff = true,
update_ipc = true
}
-- Try to establish the connection before each action.
events.preaction = function(name)
if allow[name] then
return true
end
if not fd and not connect() then
return false
end
return true
end
-- Consume any responses from ddb after each action.
local handle_response = function()
-- Try to get a response from ddb.
local resp = read()
if resp ~= nil and resp:len() > 0 then
-- ddb can send multiple JSON objects on each line.
for msg in resp:gmatch("[^\r\n]+") do
if msg:match("^{") then
local tbl = data.fromjson(msg)
if tbl.error and tbl.error ~= "success" then
device.toast("Command failed")
log.warn("Error from ddb: " .. msg)
end
-- If the message contains a request ID, it is a response to our command.
if tbl.request_id and requests[tbl.request_id] then
requests[tbl.request_id](tbl)
end
-- If the message is an event, let the corresponding listener handle it
if tbl.event and listeners[tbl.event] then
listeners[tbl.event](tbl)
end
-- Flush any other messages/events.
end
end
end
end
-- Set the input field when loading the remote.
events.preload = function()
layout.input_ipc_server.text = settings.input_ipc_server
end
-- Set the input field when the remote gains focus, and try to connect.
-- Apparently some things happen with the internal state of the remote when it loses focus.
events.focus = function()
layout.input_ipc_server.text = settings.input_ipc_server
if connect() then
initialize_ui()
tid = libs.timer.interval(handle_response, 50)
end
end
-- Disconnect from ddb when losing focus.
events.blur = function()
disconnect()
end
-- Disconnect from ddb when the remote is destroyed.
events.destroy = function()
disconnect()
end
-- Detect something.
events.detect = function ()
return fs.exists(settings.input_ipc_server)
end
-----------------------------------------------------------
-- Actions
-----------------------------------------------------------
--@help Pushing the on/off button toggles the connection state.
actions.onoff = function()
if fd then
disconnect()
else
connect()
initialize_ui()
end
end
--@help Changing to the playlist tab requests playlist contents from server
actions.on_tab_changed = function (index)
log.warn(index)
if index == 1 then
send_with_callback(ui_populate_playlist, "get-playlist-contents", { idx = tonumber(settings.plt_idx) })
end
end
--@help Set the input IPC server path.
--@param path:string IPC serevr path
actions.update_ipc = function(path)
settings.input_ipc_server = path
disconnect()
connect()
end
--@help Previous track
actions.previous = function()
send("prev-track")
end
--@help Next track
actions.next = function()
send("next-track")
end
--@help Previous album
actions.previous_album = function()
send("prev-album")
end
--@help Next album
actions.next_album = function()
send("next-album")
end
--@help Toggle play/pause state
actions.play_pause = function()
send("play-pause")
end
--@help Lower volume
actions.volume_down = function()
send("adjust-volume", { adjustment = -2} )
end
--@help Mute volume
actions.volume_mute = function()
send("toggle-mute")
end
--@help Raise volume
actions.volume_up = function()
send("adjust-volume", { adjustment = 2} )
end
--@help Set volume
actions.volume_set = function(value)
send("set-volume", { volume = tonumber(value) } )
end
--@help Seek by percent
actions.seek_percent = function(value)
send("seek", { percent = value} )
end
--@help Toggle Stop after current track
actions.toggle_stop_after_current_track = function()
send("toggle-stop-after-current-track")
end
--@help Toggle Stop after current album
actions.toggle_stop_after_current_album = function()
send("toggle-stop-after-current-album")
end
actions.set_repeat = function(index)
local rep = string.lower(repeats[index+1])
log.warn("Setting repeat " .. rep)
send("set-property",
{
property = "repeat",
value = rep
}
)
end
actions.set_shuffle = function(index)
local shuff = string.lower(shuffles[index+1])
log.warn("Setting shuffle " .. shuff)
send("set-property",
{
property = "shuffle",
value = shuff
}
)
end
local function shift_playlist(by)
local idx = settings.plt_idx + by
if idx < 0 then
return
end
send("set-current-playlist", { idx = idx })
end
actions.playlist_prev = function()
shift_playlist(-1)
end
actions.playlist_next = function()
shift_playlist(1)
end
actions.play_track = function(index)
send("play-num", { idx = index })
end