-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
570 lines (494 loc) · 14.6 KB
/
init.lua
File metadata and controls
570 lines (494 loc) · 14.6 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
---@diagnostic disable: lowercase-global
--[[ Utils ]]--
function find_in(t,v)
for k,val in pairs(t) do
if(v == val) then return k; end;
end;
return nil;
end;
function is_in(t,v)
return find_in(t,v) ~= nil;
end;
function deep_copy(orig, copies)
copies = copies or {}
local orig_type = type(orig)
local copy
if orig_type == 'table' then
if copies[orig] then
copy = copies[orig]
else
copy = {}
copies[orig] = copy
for orig_key, orig_value in next, orig, nil do
copy[deep_copy(orig_key, copies)] = deep_copy(orig_value, copies)
end
setmetatable(copy, deep_copy(getmetatable(orig), copies))
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end
--[[ Pattern-based Controls Iterator ]]--
function ctls(pattern)
local k,v;
return function()
while true do
k,v = next(Controls,k);
if(not k) then return nil; end;
local match = {k:match(pattern)}
if(#match > 0) then
return v, table.unpack(match), k;
end;
end;
end
end;
--[[ Controls table constructor ]]--
Ctls = function()
_G.Ctls = {};
for k, ctl in pairs(Controls) do
local parts = {};
for part in k:gmatch('([^_]+)') do
table.insert(parts, part);
end;
local current = Ctls;
for i, part in ipairs(parts) do
if type(current) ~= 'table' then
local parts_so_far = table.concat(parts, '_', 1, i-1);
error('Cannot add control "' .. k .. '" to non-table "' .. parts_so_far .. '".');
end;
if(i == #parts) then
-- Convert numbers to integers
if(tonumber(part)) then
part = tonumber(part);
end;
current[part] = ctl;
else
current[part] = current[part] or {};
current = current[part];
end;
end;
end;
end;
--[[ Selector setter ]]--
function set_selector(control, text)
for _, json_string in ipairs(control.Choices) do
local ok, data = pcall(require('json').decode, json_string);
if(ok and data.Text == text) then
control.String = json_string;
end;
end;
end;
--[[ Interlock object creator ]]--
function interlock(pattern_or_table, options)
if(not options) then options = {}; end;
local current_value;
local controls = {};
if type(pattern_or_table) == 'string' then
for ctl, value in ctls(pattern_or_table) do
controls[ctl] = value;
end
elseif type(pattern_or_table) == 'table' then
for value, ctl in pairs(pattern_or_table) do
controls[ctl] = value;
end
else
error('Interlock expects a string or table as first argument, got ' .. type(pattern_or_table));
end
local function set(value, prevent_cb)
current_value = value;
for c,v in pairs(controls) do
c.Boolean = (current_value == v);
end;
if(options.callback and not prevent_cb) then
options.callback(current_value);
end;
end;
local function reset(prevent_cb)
if(options.default) then
set(options.default, prevent_cb);
else
local _, value = next(controls, nil);
set(value, prevent_cb);
end;
end;
for ctl, value in pairs(controls) do
ctl.EventHandler = function()
set(value);
end;
end
reset(true); -- prevent auto callback
if(options.delayed_init_callback) then
init(function() options.callback(current_value) end);
elseif(options.callback and not options.no_startup) then
options.callback(current_value);
end;
return setmetatable({},{
__index = {
set = set,
reset = reset
},
__call = function()
return current_value;
end
});
end;
--[[ Clock object creator]]--
function clock(ctl, format, trim)
if(not _G._locimation_lib_data.ClockTimer) then
_G._locimation_lib_data.ClockTimer = {};
end
_G._locimation_lib_data.ClockTimer[ctl] = Timer.New();
_G._locimation_lib_data.ClockTimer[ctl].EventHandler = function()
local time = os.date(format);
if(trim) then time = time:gsub('^0',''); end;
ctl.String = time;
if(ctl.Legend) then ctl.Legend = time; end;
end;
_G._locimation_lib_data.ClockTimer[ctl]:Start(0.2);
end;
--[[ Press + Hold object creator ]]
function presshold(ctl, options)
options = options or {};
if options.Threshold then options.threshold = options.Threshold; end;
if not options.threshold then options.threshold = 2; end;
-- Initialise globals
if(not _G._locimation_lib_data.PressHold) then
_G._locimation_lib_data.PressHold = {
Timer = Timer.New(),
State = {},
Press = {},
Hold = {}
}
_G._locimation_lib_data.PressHold.Timer.EventHandler = function()
local now = Timer.Now();
for ctl,time in pairs(_G._locimation_lib_data.PressHold.State) do
if(time and now > time) then
ctl.Boolean = false;
if(_G._locimation_lib_data.PressHold.Hold[ctl]) then
_G._locimation_lib_data.PressHold.Hold[ctl](ctl);
end;
_G._locimation_lib_data.PressHold.State[ctl] = nil;
end;
end;
end;
_G._locimation_lib_data.PressHold.Timer:Start(0.2);
end
-- Event Handlers
if(options.Hold) then
if(type(options.Hold) ~= 'function') then error('PressHold.Hold expects a function.'); end;
_G._locimation_lib_data.PressHold.Hold[ctl] = options.Hold;
end;
if(options.Press) then
if(type(options.Press) ~= 'function') then error('PressHold.Press expects a function.'); end;
_G._locimation_lib_data.PressHold.Press[ctl] = options.Press;
end;
ctl.EventHandler = function()
if(ctl.Boolean) then
_G._locimation_lib_data.PressHold.State[ctl] = Timer.Now() + options.threshold;
else
if(_G._locimation_lib_data.PressHold.Press[ctl]) then
_G._locimation_lib_data.PressHold.Press[ctl](ctl);
end;
_G._locimation_lib_data.PressHold.State[ctl] = nil;
end;
end;
return setmetatable(options, {
__index = function() return nil; end,
__newindex = function(t,k,v)
if(k == 'Threshold') then
if(type(v) ~= 'number') then error('PressHold.Threshold expects a number.'); end;
rawset(t, 'threshold', v);
elseif(k == 'Hold') then
if(type(v) ~= 'function') then error('PressHold.Hold expects a function.'); end;
_G._locimation_lib_data.PressHold.Hold[ctl] = v;
elseif(k == 'Press') then
if(type(v) ~= 'function') then error('PressHold.Press expects a function.'); end;
_G._locimation_lib_data.PressHold.Press[ctl] = v;
else
error('Property "' .. k .. '" does not exist on PressHold.');
end
end
});
end;
--[[ Initialisation helper ]]--
function init(fn)
_G._locimation_lib_data.init_functions
= _G._locimation_lib_data.init_functions or {};
if(fn) then
table.insert(_G._locimation_lib_data.init_functions, fn);
else
for _,fn in ipairs(_G._locimation_lib_data.init_functions) do
fn();
end
end;
end;
--[[ Volume control object creator ]]--
function volume(ctl, options)
local is_k = false;
options = options or {};
if(not options.RepeatDelay) then options.RepeatDelay = 0.35; end;
if(not options.RepeatInterval) then options.RepeatInterval = 0.1; end;
if(not options.Increment) then options.Increment = 0.05; end;
if(not options.Min) then options.Min = 0; end;
if(not options.Max) then options.Max = 1; end;
if((options.Prefix or options.Suffix) and not options.Up) then
local name = (options.Prefix or '') .. 'Up' .. (options.Suffix or '');
options.Up = Controls[name];
if(not options.Up) then error('Missing volume up control: ' .. name); end;
end;
if((options.Prefix or options.Suffix) and not options.Down) then
local name = (options.Prefix or '') .. 'Down' .. (options.Suffix or '');
options.Down = Controls[name];
if(not options.Down) then error('Missing volume down control: ' .. name); end;
end;
if(type(ctl) ~= 'userdata' and type(ctl) ~= 'control') then
if K and type(ctl) == 'string' then
is_k = true;
else
error('bad argument #1 to volume (expected control or userdata, got ' .. type(ctl)..')');
end;
end;
for _,option in pairs({'RepeatDelay', 'RepeatInterval', 'Increment', 'Min', 'Max'}) do
if(type(options[option]) ~= 'number') then
error('Volume.'..option..' expects number, got ' .. type(options[option]));
end;
end;
if(options.Up and type(options.Up) ~= 'userdata') then
error('Volume.Up expects control, got ' .. type(options.Up));
end;
if(options.Down and type(options.Down) ~= 'userdata') then
error('Volume.Down expects control, got ' .. type(options.Down));
end;
if(options.Change and type(options.Change) ~= 'function') then
error('Volume.Change expects function, got ' .. type(options.Change));
end;
if(not _G._locimation_lib_data.Volume) then
_G._locimation_lib_data.Volume = {};
end;
local function nudge()
local delta = 0;
if(options.Up) then delta = delta + options.Up.Value; end;
if(options.Down) then delta = delta - options.Down.Value; end;
delta = delta * options.Increment;
local oldPosition;
if is_k then
oldPosition = K.get(ctl);
if type(oldPosition) == 'nil' then
oldPosition = 0.5;
end
else
oldPosition = ctl.Position;
end
local newPosition = oldPosition + delta;
if(options.Max and newPosition > options.Max) then newPosition = options.Max; end;
if(options.Min and newPosition < options.Min) then newPosition = options.Min; end;
if(oldPosition ~= newPosition) then
if is_k then
K.set(ctl, newPosition);
else
ctl.Position = newPosition;
end;
if(options.Change) then
if is_k then
options.Change(newPosition);
else
options.Change(ctl);
end;
end;
end;
end;
_G._locimation_lib_data.Volume[ctl] = Timer.New();
_G._locimation_lib_data.Volume[ctl].EventHandler = function()
_G._locimation_lib_data.Volume[ctl]:Stop();
_G._locimation_lib_data.Volume[ctl]:Start(options.RepeatInterval);
nudge();
end;
local function upDownHandler(c)
if(c.Boolean) then
nudge();
_G._locimation_lib_data.Volume[ctl]:Start(options.RepeatDelay);
else
_G._locimation_lib_data.Volume[ctl]:Stop();
end;
end;
options.Up.EventHandler = upDownHandler;
options.Down.EventHandler = upDownHandler;
if(options.Change) then
if is_k then
K.on(ctl, options.Change);
else
ctl.EventHandler = options.Change;
end
if is_k then
options.Change(K.get(ctl));
else
options.Change(ctl);
end
end;
return {
set = function(position)
if is_k then
K.set(ctl, position);
else
ctl.Position = position;
end;
if(options.Change) then
if is_k then
options.Change(K.get(ctl));
else
options.Change(ctl);
end
end;
end;
}
end;
-- [[ fn() helper ]] --
function fn(fn, ...)
local args = {...};
return function()
fn(table.unpack(args));
end;
end;
-- [[ link creator ]] --
function link(...)
local args = {...};
local method;
if type(args[#args]) == 'string' then
method = table.remove(args);
else
method = 'String';
end;
local function update_from(c)
for _, ctl in ipairs(args) do
if c ~= ctl then
ctl[method] = c[method];
end;
end;
end;
for _,c in ipairs(args) do
c.EventHandler = update_from;
end;
update_from(args[1]);
end;
-- [[ navigator ]] --
function navigator(page_name, layers)
local current_page;
local history = {};
local history_skip = {};
local history_reset_point;
local transitions = {};
local hooks = {};
local function hook(fn)
table.insert(hooks, fn);
end;
local warn = true;
local function layer(layer_name, state, transition)
if not transition then
transition = transitions[layer_name];
end;
local ok = pcall(Uci.SetLayerVisibility, page_name, layer_name, state, transition);
if warn and not ok then
print('WARNING: Layer "' .. layer_name .. '" not found by navigator.');
end;
end;
local function go(page)
-- Store current page
current_page = page;
-- Update history
if(history_reset_point) then
table.insert(history, page);
end;
-- Show / hide known layers
for _, layer_name in ipairs(layers) do
layer(layer_name, layer_name == page);
end;
-- Execute hooks
for _,hook in ipairs(hooks) do
hook(page);
end;
end;
-- Dependent layers
local dependencies = {};
hook(function(page)
for layer_name, parent_layers in pairs(dependencies) do
layer(layer_name, is_in(parent_layers, page));
end;
end);
local function back()
if not history_reset_point then
error('History reset point not set. Use navigator.history_from(\'Home\') to init, for example.');
end;
if(#history > 0) then
table.remove(history); -- remove current page
local page = table.remove(history);
while is_in(history_skip, page) do
page = table.remove(history);
end;
go(page);
end;
end;
return {
go = go,
goFn = function(page) return fn(go, page); end,
back = back,
history_from = function(page)
history_reset_point = page;
end,
history_skip = function(page)
table.insert(history_skip, page);
end,
transition = function(layer_name, transition)
transitions[layer_name] = transition;
end,
depend = function(layer_name, parent_layers)
dependencies[layer_name] = parent_layers;
end,
layer = layer,
warn = function(w) warn = w; end,
hook = hook,
on = function(page, fn)
hook(function(p)
if(p == page) then
fn();
end;
end);
end,
get = function() return current_page; end
}
end;
-- [[ popupper ]] --
function popupper(page_name, popup_layers)
local navigator = navigator(page_name, popup_layers);
navigator.go(''); -- hide all, allowing warnings for missing popup layers
navigator.warn(false); -- disable warnings
local function popup(layer_name)
if not is_in(popup_layers, layer_name) then
error('Popup "' .. layer_name .. '" not found in popupper.');
end;
navigator.go(layer_name);
end;
local function close()
navigator.go(''); -- hide all
end;
return {
popup = popup,
popupFn = function(layer_name) popup(layer_name); end,
close = close,
toggleFn = function(layer_name)
return function(c)
if(c.Boolean) then
popup(layer_name);
else
close();
end;
end;
end,
hook = navigator.hook,
on = navigator.on,
layer = navigator.layer,
depend = navigator.depend,
get = navigator.get
}
end;
_G._locimation_lib_data = {};