-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEnhancedDigitalOutputApplet.lua
More file actions
650 lines (543 loc) · 17.4 KB
/
EnhancedDigitalOutputApplet.lua
File metadata and controls
650 lines (543 loc) · 17.4 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
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
--[[
Enhanced Digital Output Applet - support of usb audio and extended digital output capabilties via addon kernel
(c) 2012, Adrian Smith, triode1@btinternet.com
--]]
local oo = require("loop.simple")
local io = require("io")
local os = require("os")
local Applet = require("jive.Applet")
local System = require("jive.System")
local Framework = require("jive.ui.Framework")
local SimpleMenu = require("jive.ui.SimpleMenu")
local Textarea = require("jive.ui.Textarea")
local Label = require("jive.ui.Label")
local Popup = require("jive.ui.Popup")
local Checkbox = require("jive.ui.Checkbox")
local RadioGroup = require("jive.ui.RadioGroup")
local RadioButton = require("jive.ui.RadioButton")
local Slider = require("jive.ui.Slider")
local Surface = require("jive.ui.Surface")
local Task = require("jive.ui.Task")
local Timer = require("jive.ui.Timer")
local Icon = require("jive.ui.Icon")
local Window = require("jive.ui.Window")
local debug = require("jive.utils.debug")
local JIVE_VERSION = jive.JIVE_VERSION
local appletManager = appletManager
local STOP_SERVER_TIMEOUT = 10
local string, ipairs, tonumber, tostring, require, type = string, ipairs, tonumber, tostring, require, type
module(..., Framework.constants)
oo.class(_M, Applet)
function deviceMenu(self, menuItem, firstUse)
local window = Window("text_list", self:string("SELECT_OUTPUT"))
local menu = SimpleMenu("menu")
window:addWidget(menu)
-- refreshing menu to detect hotplugging of usb dacs
local timer
local updateMenu = function()
local info = self:_parseCards()
local curr = self:getSettings()["playbackDevice"]
local items = {}
if curr == "default" then
if not firstUse then
items[#items+1] = {
text = tostring(self:string("ANALOG_DIGITAL")) .. tostring(self:string("CURRENT")),
}
end
else
items[#items+1] = {
text = self:string("ANALOG_DIGITAL"),
sound = "WINDOWSHOW",
callback = function(event, menuItem)
timer:stop()
self:_setCardAndReboot("default", false)
end,
}
end
for num, card in ipairs(info) do
if curr != card.id then
items[#items+1] = {
text = card.desc,
sound = "WINDOWSHOW",
callback = function(event, menuItem)
timer:stop()
if card.needshub then
-- this is a usb1 async dac without a hub - ask whether to use embeddedTTHack
local window = Window("text_list", menuItem.text)
local menu = SimpleMenu("menu")
menu:setHeaderWidget(Textarea("help_text", self:string("HELP_USB1_DAC")))
menu:addItem({
text = self:string("USB1_DAC_NO_HUB"),
style = 'item_choice',
callback = function(event, menuItem)
self:_setCardAndReboot(card.id, true)
end,
})
menu:addItem({
text = self:string("USB1_DAC_HUB"),
style = 'item_choice',
callback = function(event, menuItem)
self:_setCardAndReboot(card.id, false)
end,
})
window:addWidget(menu)
window:show()
else
self:_setCardAndReboot(card.id, false)
end
end,
}
elseif card.id == "TXRX" then
items[#items+1] = {
text = card.desc .. tostring(self:string("CURRENT")),
}
else
items[#items+1] = {
text = card.desc .. tostring(self:string("INFO")),
sound = "WINDOWSHOW",
callback = function(event, menuItem)
timer:stop()
self:_showStats(card.id, card.desc)
end,
}
end
end
if firstUse then
menu:setHeaderWidget(Textarea("help_text", self:string("HELP_FIRST_USE")))
end
menu:setItems(items, #items)
end
-- update on a timer
timer = Timer(1000, function() updateMenu() end, false)
timer:start()
-- initial display
updateMenu()
-- cancel timer when window is hidden (e.g. screensaver)
window:addListener(EVENT_WINDOW_ACTIVE | EVENT_HIDE,
function(event)
local type = event:getType()
if type == EVENT_WINDOW_ACTIVE then
timer:restart()
else
timer:stop()
end
return EVENT_UNUSED
end,
true
)
self:tieAndShowWindow(window)
return window
end
function optionsMenu(self, menuItem)
local window = Window("text_list", menuItem.text)
local menu = SimpleMenu("menu")
window:addWidget(menu)
menu:addItem({
text = self:string("SELECT_OUTPUT"),
sound = "WINDOWSHOW",
callback = function(event, menuItem)
self:deviceMenu(menuItem, false)
end
})
menu:addItem({
text = self:string("KERNEL_UPDATE"),
sound = "WINDOWSHOW",
callback = function(event, menuItem)
local window = Window("text_list", menuItem.text)
local menu = SimpleMenu("menu")
menu:setHeaderWidget(Textarea("help_text", self:string("HELP_KERNEL_UPDATE")))
menu:addItem({
text = self:string("KERNEL_UPDATE_AUTO"),
style = 'item_choice',
check = Checkbox("checkbox",
function(object, isSelected)
self:getSettings()["autoKernelUpdate"] = isSelected
self:storeSettings()
end,
self:getSettings()["autoKernelUpdate"]
),
})
menu:addItem({
text = self:string("KERNEL_UPDATE_NOW"),
style = 'item_choice',
callback = function(event, menuItem)
self:_kernelUpdate()
end,
})
window:addWidget(menu)
window:show()
end,
})
menu:addItem({
text = self:string("NO_HUB_OPTION"),
sound = "WINDOWSHOW",
callback = function(event, menuItem)
local window = Window("text_list", menuItem.text)
local menu = SimpleMenu("menu")
menu:setHeaderWidget(Textarea("help_text", self:string("HELP_NO_HUB_OPTION")))
menu:addItem({
text = self:string("NO_HUB_SETTING"),
style = 'item_choice',
check = Checkbox("checkbox",
function(object, isSelected)
self:getSettings()["embeddedTTHack"] = isSelected
self:storeSettings()
self:_restart()
end,
self:getSettings()["embeddedTTHack"]
),
})
window:addWidget(menu)
window:show()
end,
})
menu:addItem({
text = self:string("KERNEL_IDLE_OPTION"),
sound = "WINDOWSHOW",
callback = function(event, menuItem)
local window = Window("text_list", menuItem.text)
local menu = SimpleMenu("menu")
menu:setHeaderWidget(Textarea("help_text", self:string("HELP_KERNEL_IDLE_OPTION")))
menu:addItem({
text = self:string("KERNEL_IDLE_SETTING"),
style = 'item_choice',
check = Checkbox("checkbox",
function(object, isSelected)
self:getSettings()["cpuIdleFullspeed"] = isSelected
self:storeSettings()
self.cpuIdle() -- cb in meta to potentially update state
end,
self:getSettings()["cpuIdleFullspeed"]
),
})
window:addWidget(menu)
window:show()
end,
})
menu:addItem({
text = self:string("BUFFER_TUNING"),
sound = "WINDOWSHOW",
callback = function(event, menuItem)
local options = {
{ desc = "BUFFER_DEFAULT", time = 20000, count = 2 },
{ desc = "BUFFER_LARGE", time = 100000, count = 4 },
{ desc = "BUFFER_SMALL", time = 4000, count = 2 },
{ desc = "BUFFER_RAND", time = 100000, count = 104 },
{ desc = "BUFFER_VLARGE", time =4000000, count = 4 },
{ desc = "BUFFER_VLRAND", time =4000000, count = 104 },
}
local window = Window("text_list", menuItem.text)
local menu = SimpleMenu("menu")
local group = RadioGroup()
local settings = self:getSettings()
menu:setHeaderWidget(Textarea("help_text", self:string("HELP_BUFFER_TUNING")))
for _, opt in ipairs(options) do
menu:addItem({
text = self:string(opt.desc),
style = 'item_choice',
check = RadioButton("radio", group,
function(event, menuItem)
settings.bufferTime = opt.time
settings.periodCount = opt.count
self:storeSettings()
self:_restart()
end,
settings.bufferTime == opt.time and settings.periodCount == opt.count)
})
end
window:addWidget(menu)
window:show()
end,
})
self:tieAndShowWindow(window)
return window
end
function _parseCards(self)
local t = {}
local cards = io.open("/proc/asound/cards", "r")
if cards == nil then
log:error("/proc/asound/cards could not be opened")
return
end
-- internal cards, put first in list
t[1] = { id = "TXRX", desc = "Digital Only" }
-- read and parse entries
for line in cards:lines() do
local num, id, desc = string.match(line, "(%d+)%s+%[(.-)%s*%]:%s+(.*)")
if id and id != "TXRX" and id != "fab4" and id != "fab4_1" then
-- usb card - get bitdepth info
local info = self:_parseStreamInfo(id)
t[#t+1] = { id = id, desc = desc, needshub = info.needshub }
end
end
cards:close()
return t
end
function _parseStreamInfo(self, card)
local bits, needhub, async
local t = {}
local cards = io.open("/proc/asound/" .. card .. "/stream0", "r")
if cards == nil then
log:error("/proc/asound/" .. card .. "/stream0 could not be opened")
return t
end
-- parsing helper functions
local last
local parse = function(regexp, opt)
local tmp = last or cards:read()
if tmp == nil then
return
end
local r1, r2, r3 = string.match(tmp, regexp)
if opt and r1 == nil and r2 == nil and r3 == nil then
last = tmp
else
last = nil
end
return r1, r2, r3
end
local skip = function(number)
if last and number > 0 then
last = nil
number = number - 1
end
while number > 0 do
cards:read()
number = number - 1
end
end
local eof = function()
if last then return false end
last = cards:read()
return last == nil
end
-- detect full speed async devices without external hub
t.id, t.speed = parse("usb%-fsl%-ehci%.(.-),%s(%w+)%sspeed%s:")
t.hub = (t.id != "0-1")
skip(2)
-- detect status
t.status = parse(" Status: (%w+)")
if t.status == "Running" then
t.interface = parse(" Interface = (%d+)")
t.altset = parse(" Altset = (%d+)")
skip(2)
t.momfreq = parse(" Momentary freq = (%d+) Hz")
t.feedbkfmt = parse(" Feedback Format = (.*)", true)
end
local fmts = {}
while not eof() do
local intf = parse(" Interface (%d+)")
local alt = parse(" Altset (%d+)")
local fmt = parse(" Format: (.*)")
local chan = parse(" Channels: (%w+)")
local type = parse(" Endpoint: %d+ %w+ %((%w+)%)")
local rate = parse(" Rates: (.*)")
local int = parse(" Data packet interval: (.*)")
skip(2)
fmts[#fmts+1] = { intf = intf, alt = alt, fmt = fmt, chan = chan, type = type, rate = rate, int = int }
if t.interface == intf and t.altset == alt then
t.fmt = fmts[#fmts]
end
-- touch needs a hub in this case only due to imx35 embedded TT limitations
if type == "ASYNC" and t.speed == "full" and not t.hub then
t.needshub = true
end
end
t.fmts = fmts
cards:close()
return t
end
function _setCardAndReboot(self, card, embeddedTTHack)
local s = self:getSettings()
s.playbackDevice = card
s.embeddedTTHack = embeddedTTHack
self:storeSettings()
self:_restart()
end
function _showStats(self, card, desc)
-- check we can open the /proc file
local info = io.open("/proc/asound/" .. card .. "/stream0", "r")
if info == nil then
log:error("/proc/asound/" .. card .. "/stream0" .. " could not be opened")
return
end
info:close()
local window = Window("text_list", desc)
local menu = SimpleMenu("menu")
window:addWidget(menu)
self:tieAndShowWindow(window)
-- refreshing menu to detect hotplugging of usb dacs
local timer
local display = function()
log:debug("fetching info...")
local info = self:_parseStreamInfo(card)
local items = {}
local entry = function(text)
items[#items+1] = {
text = text,
style = "item_no_arrow",
}
end
entry("Status: " .. info.status)
entry("Speed: " .. (info.speed == "full" and "Full" or "High"))
entry("Connection: " .. (info.hub and "via Hub" or "Direct"))
if info.status == "Running" then
local first, rest = string.match(info.fmt.type, "(%w)(%w+)")
entry("Type: " .. string.upper(first) .. string.lower(rest))
entry("Frequency: " .. info.momfreq .. " Hz")
entry("Format: " .. info.fmt.fmt)
entry("Rates: " .. info.fmt.rate)
entry("Feedback Format: " .. ((info.feedbkfmt == "10.14" and "Full (10.14)") or
(info.feedbkfmt == "16.16" and "High (16.16)") or "None"))
entry("Interval: " .. info.fmt.int)
else
local i = 1
while info.fmts[i] do
local fmt = info.fmts[i]
local cnt = #info.fmts > 1 and (" [" .. i .. "]: ") or ": "
local first, rest = string.match(fmt.type, "(%w)(%w+)")
entry("Type" .. cnt .. string.upper(first) .. string.lower(rest))
entry("Format" .. cnt .. fmt.fmt)
entry("Rates" .. cnt .. fmt.rate)
i = i + 1
end
end
menu:setItems(items, #items)
end
-- initial display
display()
-- update on a timer
local timer = Timer(1000, function() display() end, false)
timer:start()
-- cancel timer when window is hidden (e.g. screensaver)
window:addListener(EVENT_WINDOW_ACTIVE | EVENT_HIDE,
function(event)
local type = event:getType()
if type == EVENT_WINDOW_ACTIVE then
timer:restart()
else
timer:stop()
end
return EVENT_UNUSED
end,
true
)
end
function _restart(self)
self.popup = Popup("update_popup")
self.popup:addWidget(Icon("icon_restart"))
self.popup:addWidget(Label("text", self:string("REBOOTING")))
self:tieAndShowWindow(self.popup)
self.timer = Timer(3000,
function()
log:info("rebooting...")
appletManager:callService("reboot")
end,
true)
self.timer:start()
end
--------------------------------------------------------------------------------------------------------------------------------
-- following is a modified form of SetupFirmwareUpgrade
function _kernelUpdate(self, kernel)
if kernel then
self.kernel = kernel
end
self.popup = Popup("update_popup")
self.icon = Icon("icon_software_update")
self.popup:addWidget(self.icon)
self.text = Label("text", self:string("DOWNLOADING"), "")
self.counter = Label("subtext", "")
self.progress = Slider("progress", 1, 100, 1)
self.popup:addWidget(self.text)
self.popup:addWidget(self.counter)
self.popup:addWidget(self.progress)
self.popup:focusWidget(self.text)
-- make sure this popup remains on screen
self.popup:setAllowScreensaver(false)
self.popup:setAlwaysOnTop(true)
self.popup:setAutoHide(false)
self.popup:setTransparent(false)
-- no way to exit this popup
self.upgradeListener =
Framework:addListener(EVENT_ALL_INPUT,
function()
Framework.wakeup()
return EVENT_CONSUME
end,
true)
-- disconnect from SqueezeCenter, we don't want to up
-- interrupted during the firmware upgrade.
appletManager:callService("disconnectPlayer")
-- stop memory hungry services before upgrading
if (System:getMachine() == "fab4") then
appletManager:callService("stopSqueezeCenter")
appletManager:callService("stopFileSharing")
-- start the upgrade once SBS is shut down or timed out
local timeout = 0
self.serverStopTimer = self.popup:addTimer(1000, function()
timeout = timeout + 1
if timeout <= STOP_SERVER_TIMEOUT and appletManager:callService("isBuiltInSCRunning") then
return
end
Task("upgrade", self, _doUpgrade, _upgradeFailed):addTask()
self.popup:removeTimer(self.serverStopTimer)
end)
else
Task("upgrade", self, _doUpgrade, _upgradeFailed):addTask()
end
self:tieAndShowWindow(self.popup)
return window
end
function _doUpgrade(self)
Task:yield(true)
-- EN only messages
local str = { UPDATE_DOWNLOAD = self:string("KERNEL_DOWNLOAD"), UPDATE_VERIFY = self:string("KERNEL_VERIFY"),
UPDATE_REBOOT = self:string("REBOOTING") }
local KernelUpgrade = require("applets.EnhancedDigitalOutput.KernelUpgrade")
local t, err = KernelUpgrade():start(self.kernel.url, self.kernel.md5,
function (done, msg, count)
if type(count) == "number" then
if count >= 100 then
count = 100
end
self.counter:setValue(count .. "%")
self.progress:setRange(1, 100, count)
else
self.counter:setValue("")
end
self.text:setValue(str[msg] or msg)
if done then
self.icon:setStyle("icon_restart")
end
end
)
if not t then
log:error("Upgrade failed: ", err)
self:_upgradeFailed()
if self.popup then
self.popup:hide()
self.popup = nil
end
end
end
function _upgradeFailed(self)
-- unblock keys
Framework:removeListener(self.upgradeListener)
self.upgradeListener = nil
-- reconnect to server
appletManager:callService("connectPlayer")
local window = Window("help_list", self:string("KERNEL_UPDATE_FAILED"))
local menu = SimpleMenu("menu")
window:addWidget(menu)
menu:addItem({
text = self:string("CANCEL"),
sound = "WINDOWHIDE",
callback = function()
window:hide()
end,
})
-- turn off auto update
self:getSettings()["autoKernelUpdate"] = false
self:storeSettings()
self:tieAndShowWindow(window)
end