- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathinterface.lua
More file actions
Latest commit
234 lines (219 loc) · 8.32 KB
/
Copy pathinterface.lua
File metadata and controls
234 lines (219 loc) · 8.32 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
localdbus=require("lua-dbus.init")
localfunctioncreateinstance(meta, ...)
localinstance=setmetatable({}, meta)
ifinstance.inittheninstance:init(...) end
returninstance
end
localInterface= { new=createinstance }
Interface.__index=Interface
functionInterface:init(opts)
opts=optsor {}
self.bus=opts.busor'session'
self.paths= {}
self.name=opts.name
ifself.namethen
dbus.raw.request_name(self.bus, self.name)
end
end
-- opts: interface, path='/', methods={}, signals={}, properties={}
functionInterface:add(opts)
opts=optsor {}
ifnotopts.interfacethenreturnend
opts.path=opts.pathor'/'
localpath=self.paths[opts.path] or {}
self.paths[opts.path] =path
ifnotpath.introspectionthen
path.introspection=true
self:_introspection(opts)
end
localiface=path[opts.interface] or {}
path[opts.interface] =iface
iface.methods=iface.methodsor {}
formethod, argsinpairs(opts.methodsor {}) do
iface.methods[method] =args
end
iface.signals=iface.signalsor {}
forname, argsinpairs(opts.signalsor {}) do
iface.signals[name] =args
end
localhasproperties=false
iface.properties=iface.propertiesor {}
forname, propertyinpairs(opts.propertiesor {}) do
iface.properties[name] =property
hasproperties=true
end
ifnotpath.propertiesthen
path.properties=true
self:_properties(opts)
end
localiface=self
dbus.on({ bus=self.bus, interface=opts.interface,
handler=function (sig, ...)
ifsig.type~='method_call' thenreturnend
localinterface=iface.paths[opts.path][sig.interface]
ifnotinterfacethenreturnend
localmethod= (interface.methodsor {})[sig.member]
ifmethodandmethod.callbackthen
locali, result, ret=1, {}, {method.callback(sig, ...)}
ifnotmethod.resultthen
result=ret
else
-- prepare callback results with actual interface result types
forj=1,#method.result,2do
table.insert(result, method.result[j]) -- type
table.insert(result, ret[i]) -- value
i=i+1
end
end
returnunpack(result)
end
end,
})
end
localfunctionintrospect(iface, path, header)
localxml=header~=falseand
[[<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">]] or""
ifpath=="/" then
xml=xml.."<node>"
xml=xml..introspect(iface, nil, false)
forfacepath, interfacesinpairs(iface.pathsor {}) do
xml=xml..string.format([[<node name="%s"/>]], string.sub(facepath, 2))
end
xml=xml.."</node>"
returnxml
end
localinterfaces=iface.pathsandiface.paths[pathor"/"]
ifinterfacesthen
ifpaththen
xml=xml..string.format([[<node name="%s">]], path)
end
forname, interfaceinpairs(interfaces) do
ifname~='introspection' andname~='properties' then
xml=xml..string.format([[<interface name="%s">]], name)
formethod, argsinpairs(interface.methods) do
xml=xml..string.format([[<method name="%s">]], method)
fori=1, #args, 2do
xml=xml..string.format(
[[<arg name="%s" type="%s" direction="%s"/>]],
args[i+1], args[i], 'in')
end
ifargs.resultthen
fori=1, #args.result, 2do
xml=xml..string.format(
[[<arg name="%s" type="%s" direction="%s"/>]],
args.result[i+1], args.result[i], 'out')
end
end
xml=xml.."</method>"
end
forname, argsinpairs(interface.signals) do
xml=xml..string.format([[<signal name="%s">]], name)
fori=1, #args, 2do
xml=xml..string.format(
[[<arg name="%s" type="%s"/>]],
args[i+1], args[i])
end
xml=xml.."</signal>"
end
forname, propertyinpairs(interface.properties) do
localaccess='read'
ifproperty.readandproperty.writethen
access='readwrite'
elseifproperty.writethen
access='write'
end
xml=xml..string.format(
[[<property name="%s" type="%s" access="%s"/>]],
name, property.type, access)
end
xml=xml.."</interface>"
end
end
ifpaththen
xml=xml.."</node>"
end
end
returnxml
end
Interface.introspect=introspect
functionInterface:_introspection(opts)
localiface=self
self:add({
interface='org.freedesktop.DBus.Introspectable',
path=opts.path,
methods= {
Introspect= {
result= {'s', "data"},
callback=function (sig) returnintrospect(iface, sig.path) end,
},
},
})
end
functionInterface:_properties(opts)
localiface=self
self:add({
interface='org.freedesktop.DBus.Properties',
path=opts.path,
methods= {
Get= { 's', "interface", 's', "name",
result= {'v', "value"},
callback=function (sig, interface, name)
localinterfaces=iface.pathsandiface.paths[sig.pathor"/"]
localface=interfacesandinterfaces[interface]
iffaceandface.propertiesthen
localproperty=face.properties[name]
ifpropertyandproperty.readthen
returnproperty.read()
end
end
end,
},
GetAll= { 's', "interface",
result= {'a{sv}', "values"},
callback=function (sig, interface)
localinterfaces=iface.pathsandiface.paths[sig.pathor"/"]
localface=interfacesandinterfaces[interface]
localvalues= {}
iffaceandface.propertiesthen
forname, propertyinpairs(face.properties) do
ifproperty.readthen
values[name] =property.read()
end
end
end
returnvalues
end,
},
Set= { 's', "interface", 's', "name", 'v', "value",
callback=function (sig, interface, name, value)
localinterfaces=iface.pathsandiface.paths[sig.pathor"/"]
localface=interfacesandinterfaces[interface]
iffaceandface.propertiesthen
localproperty=face.properties[name]
ifpropertyandproperty.writethen
returnproperty.write(value)
end
end
end,
},
},
signals= {
PropertiesChanged= {
's', "interface", 'a{sv}', "values", 'as', "keys"
},
},
})
end
functionInterface:properties(keys, values, opts)
opts=optsor {}
dbus.raw.emit_signal(self.bus, opts.path,
'org.freedesktop.DBus.Properties', 'PropertiesChanged',
's', opts.interface, 'a{sv}', values, 'as', keys)
end
functionInterface:property(name, value, opts)
localvalues= {}
values[name] =value
self:properties({name}, values, opts)
end
returnInterface