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
|
dalosp.luafilter = {
default_code = [[
-- available globals:
-- ninputs, noutputs: numbers
-- get_input(ind): handle
-- del_output(ind): nil
-- new_output(ind): nil
-- set_color(c) : nil
function activate()
end
function read(ind, count, userdata, offset)
end
function seek(ind, offset)
end
function input_change(ind)
end
]],
get_settings = function (self)
return { ninputs = self.ninputs, noutputs = self.noutputs, code = self.extra.code }
end,
run_in_localenv = function (self, f, ...)
local localenv = self.extra.localenv
local metatable = getmetatable(_G)
if not metatable then metatable = {} end
local oldni, oldi = metatable.__newindex, metatable.__index
metatable.__newindex = function (table, key, value)
localenv[key] = value
end
metatable.__index = function (table, key)
local l = localenv[key]
if l then return localenv[key] end
return rawget(_G, key)
end
setmetatable(_G, metatable)
if type(f) ~= "function" then f = localenv[f] end
local rets = { true }
if f then rets = { pcall(f, ...) } end
metatable.__newindex, metatable.__index = oldni, oldi
setmetatable(_G, metatable)
if not rets[1] then error(rets[2]) end
table.remove(rets, 1)
return unpack(rets)
end,
load_code = function (self, code)
self.extra.localenv = {
ninputs = self.ninputs + 0,
noutputs = self.noutputs + 0,
get_input = function(ind) return self:get_linked_input(ind) end,
del_output = function(ind) self:set_houtput(nil, ind) end,
new_output = function(ind, size, name)
self:set_houtput(dalos.luahandle{
size = size,
getname = function ()
return name
end,
do_read = function (lh, count, userdata)
return self:run_in_localenv("read", ind, count, userdata, lh.offset)
end,
do_seek = function (lh)
return self:run_in_localenv("seek", ind, lh.offset)
end,
}, ind)
end,
set_color = function(c) self.color = c self:draw() end,
}
if code and code ~= "" then
local f = loadstring(code)
if f then self:run_in_localenv(f) end
end
end,
input_change = function (self, ind)
self:run_in_localenv("input_change", ind)
end,
load_template = function (self)
local dlg = iup.filedlg {
dialogtype = "Open",
filter = "*.dtpl",
}
iup.Popup(dlg)
if dlg.status == -1 then return end
local s, v = pcall(Input, dlg.value)
if not s then error("Problem loading file " .. dlg.value) end
local f = preload(v)
v:destroy()
if not f then error("Syntax error loading file " .. dlg.value) end
local data, otype, tname = f()
if otype ~= "Lua Filter" then error("Wrong template type: " .. otype) end
self.extra.code = data.template.code
end,
configure = function (self)
local okay = false
local text = iup.text { multiline = "Yes", font = "Courier", expand = "Yes", value = self.extra.code }
local bok = iup.button { title = "Ok", action = function () okay = true return iup.CLOSE end }
local bimport = iup.button { title = "Import", action = function() self:load_template() return iup.CLOSE end }
local bexport = iup.button { title = "Export", action = function() self:save_template{ code = self.extra.code } end }
local bcancel = iup.button { title = "Cancel", action = function () okay = false return iup.CLOSE end }
local dlg = iup.dialog { iup.vbox { text, iup.hbox { bok, bimport, bexport, iup.fill{}, bcancel, normalizesize = "Horizontal" } }, title = "Code for " .. self.name, size = "600x300" }
local r = dlg:popup()
-- if r ~= iup.NOERROR then return end
local newcode = text.value
if newcode and okay then
self.extra.code = newcode
self:load_code(newcode)
end
end,
activate = function (self)
self:run_in_localenv "activate"
end,
create = function (d, tab, settings)
tab.ninputs = settings and settings.ninputs
tab.noutputs = settings and settings.noutputs
tab.otype = dalos.objtype.LUA_FILTER
tab.configure = dalosp.luafilter.configure
tab.activate = dalosp.luafilter.activate
tab.input_change = dalosp.luafilter.input_change
tab.default_name = "Lua Filter"
tab.get_settings = dalosp.luafilter.get_settings
tab.ntype = "Lua Filter"
local extra = { localenv = {} }
extra.code = settings and settings.code
if not extra.code or extra.code == "" then extra.code = dalosp.luafilter.default_code end
local s = true
while not s and not tab.ninputs or not tab.noutputs do
s, tab.ninputs, tab.noutputs = iup.GetParam("Lua Filter", nil, "Inputs number: %i\nOutputs number: %i\n", 1, 1)
end
local obj = dalos.object(d, tab, extra)
obj.load_code = dalosp.luafilter.load_code
obj.run_in_localenv = dalosp.luafilter.run_in_localenv
obj.load_template = dalosp.luafilter.load_template
obj:load_code(extra.code)
return obj
end,
}
dalos.luafilter = dalosp.luafilter.create
dalos:register_obj("Lua Filter", dalos.luafilter, "Programmable")
|