| 1 |
-- WirePlumber |
| 2 |
-- |
| 3 |
-- This file contains the API that is made available to the Lua scripts |
| 4 |
-- |
| 5 |
-- Copyright © 2020 Collabora Ltd. |
| 6 |
-- @author George Kiagiadakis <[email protected]> |
| 7 |
-- |
| 8 |
-- SPDX-License-Identifier: MIT |
| 9 |
|
| 10 |
local function Constraint (spec) |
| 11 |
assert (type(spec[1]) == "string", "Constraint: expected subject as string"); |
| 12 |
assert (type(spec[2]) == "string", "Constraint: expected verb as string"); |
| 13 |
|
| 14 |
local subject = spec[1] |
| 15 |
local verb = spec[2] |
| 16 |
local verbs = { |
| 17 |
["="] = "equals", |
| 18 |
["!"] = "not-equals", |
| 19 |
["c"] = "in-list", |
| 20 |
["~"] = "in-range", |
| 21 |
["#"] = "matches", |
| 22 |
["+"] = "is-present", |
| 23 |
["-"] = "is-absent" |
| 24 |
} |
| 25 |
|
| 26 |
-- check and convert verb to its short version |
| 27 |
local verb_is_valid = false |
| 28 |
for k, v in pairs(verbs) do |
| 29 |
if verb == k or verb == v then |
| 30 |
verb = k |
| 31 |
spec[2] = k |
| 32 |
verb_is_valid = true |
| 33 |
break |
| 34 |
end |
| 35 |
end |
| 36 |
assert (verb_is_valid, "Constraint: invalid verb '" .. verb .. "'") |
| 37 |
|
| 38 |
-- check and convert type to its integer value |
| 39 |
local type = spec["type"] |
| 40 |
if type then |
| 41 |
local valid_types = { "pw-global", "pw", "gobject" } |
| 42 |
local type_is_valid = false |
| 43 |
|
| 44 |
for i, v in ipairs(valid_types) do |
| 45 |
if type == v then |
| 46 |
spec["type"] = i |
| 47 |
type_is_valid = true |
| 48 |
break |
| 49 |
end |
| 50 |
end |
| 51 |
|
| 52 |
assert(type_is_valid, "Constraint: invalid subject type '" .. type .. "'") |
| 53 |
end |
| 54 |
|
| 55 |
-- check if we got the right amount of values |
| 56 |
if verb == "=" or verb == "!" or verb == "#" then |
| 57 |
assert (spec[3] ~= nil, |
| 58 |
"Constraint: " .. verbs[verb] .. ": expected constraint value") |
| 59 |
elseif verb == "c" then |
| 60 |
assert (spec[3] ~= nil, |
| 61 |
"Constraint: " .. verbs[verb] .. ": expected at least one constraint value") |
| 62 |
elseif verb == "~" then |
| 63 |
assert (spec[3] ~= nil and spec[4] ~= nil, |
| 64 |
"Constraint: " .. verbs[verb] .. ": expected two values") |
| 65 |
else |
| 66 |
assert (spec[3] == nil, |
| 67 |
"Constraint: " .. verbs[verb] .. ": expected no value, but there is one") |
| 68 |
end |
| 69 |
|
| 70 |
return debug.setmetatable(spec, { __name = "Constraint" }) |
| 71 |
end |
| 72 |
|
| 73 |
local function EventInterest(spec) |
| 74 |
spec.type = "event" |
| 75 |
return WpObjectInterest_new(spec) |
| 76 |
end |
| 77 |
|
| 78 |
local function dump_table(t, indent) |
| 79 |
local indent_str = "" |
| 80 |
indent = indent or 1 |
| 81 |
for i = 1, indent, 1 do |
| 82 |
indent_str = indent_str .. "\t" |
| 83 |
end |
| 84 |
|
| 85 |
local kvpairs = {} |
| 86 |
for k, v in pairs(t) do |
| 87 |
table.insert(kvpairs, { k, v }) |
| 88 |
end |
| 89 |
|
| 90 |
table.sort(kvpairs, function (lhs, rhs) |
| 91 |
local left_key, right_key = lhs[1], rhs[1] |
| 92 |
|
| 93 |
-- If the types are different, we sort by the type |
| 94 |
-- in alphabetical order. This means that numbers |
| 95 |
-- come before before strings, etc |
| 96 |
if type(left_key) ~= type(right_key) then |
| 97 |
return type(left_key) < type(right_key) |
| 98 |
end |
| 99 |
|
| 100 |
local key_type = type(left_key) |
| 101 |
|
| 102 |
-- Only numbers and strings have a well-defined order |
| 103 |
-- that's guaranteed to fulfill the requirements of |
| 104 |
-- table.sort (strict weak order) |
| 105 |
if key_type == "number" or key_type == "string" then |
| 106 |
return left_key < right_key |
| 107 |
end |
| 108 |
|
| 109 |
-- At this point, we have no good way to order the objects. |
| 110 |
-- We can't just do `left_key < right_key`, because this may fail |
| 111 |
-- if there's no `__lt` metamethod, and even if there is one, |
| 112 |
-- it might not be a strict weak order. (The Lua reference does |
| 113 |
-- not say what happens if the order is not strict weak, so it's |
| 114 |
-- undefined behaviour) |
| 115 |
|
| 116 |
-- That said, it's always mathematically "permitted" to return `false`, |
| 117 |
-- in which case, since both x < y and y < x are false, the elements |
| 118 |
-- are considered "equivalent" and may appear in any order in relation |
| 119 |
-- to each other. The elements are still sorted in relation to the |
| 120 |
-- *other* keys. |
| 121 |
return false |
| 122 |
|
| 123 |
-- To be a strict weak order, if x and y are equivalent, and y and z |
| 124 |
-- are equivalent, then x and z must be equivalent too. Otherwise the |
| 125 |
-- ordering is only a strict *partial* order. |
| 126 |
|
| 127 |
-- Note that the Lua 5.3 reference states that the order merely has to |
| 128 |
-- be a strict *partial* order, but since all weak orders are partial |
| 129 |
-- orders, this is not a problem. |
| 130 |
end) |
| 131 |
|
| 132 |
for _, pair in ipairs(kvpairs) do |
| 133 |
local k, v = table.unpack(pair) |
| 134 |
|
| 135 |
if (type(v) == "table") then |
| 136 |
print (indent_str .. tostring(k) .. ": ") |
| 137 |
dump_table(v, indent + 1) |
| 138 |
else |
| 139 |
print (indent_str .. tostring(k) .. ": " .. tostring(v)) |
| 140 |
end |
| 141 |
end |
| 142 |
end |
| 143 |
|
| 144 |
local Debug = { |
| 145 |
dump_table = dump_table, |
| 146 |
} |
| 147 |
|
| 148 |
local Id = { |
| 149 |
INVALID = 0xffffffff, |
| 150 |
ANY = 0xffffffff, |
| 151 |
} |
| 152 |
|
| 153 |
local Features = { |
| 154 |
PipewireObject = { |
| 155 |
MINIMAL = 0x11, |
| 156 |
}, |
| 157 |
ALL = 0xffffffff, |
| 158 |
} |
| 159 |
|
| 160 |
local Feature = { |
| 161 |
Proxy = { |
| 162 |
BOUND = 1, |
| 163 |
}, |
| 164 |
PipewireObject = { |
| 165 |
INFO = (1 << 4), |
| 166 |
PARAM_PROPS = (1 << 5), |
| 167 |
PARAM_FORMAT = (1 << 6), |
| 168 |
PARAM_PROFILE = (1 << 7), |
| 169 |
PARAM_PORT_CONFIG = (1 << 8), |
| 170 |
PARAM_ROUTE = (1 << 9), |
| 171 |
}, |
| 172 |
SpaDevice = { |
| 173 |
ENABLED = (1 << 16), |
| 174 |
}, |
| 175 |
Node = { |
| 176 |
PORTS = (1 << 16), |
| 177 |
}, |
| 178 |
Metadata = { |
| 179 |
DATA = (1 << 16), |
| 180 |
}, |
| 181 |
SessionItem = { |
| 182 |
ACTIVE = (1 << 0), |
| 183 |
EXPORTED = (1 << 1), |
| 184 |
}, |
| 185 |
} |
| 186 |
|
| 187 |
-- Allow calling Conf() to instantiate a new WpConf |
| 188 |
WpConf["__new"] = WpConf_new |
| 189 |
|
| 190 |
SANDBOX_EXPORT = { |
| 191 |
Debug = Debug, |
| 192 |
Id = Id, |
| 193 |
Features = Features, |
| 194 |
Feature = Feature, |
| 195 |
GLib = GLib, |
| 196 |
I18n = I18n, |
| 197 |
Log = WpLog, |
| 198 |
Core = WpCore, |
| 199 |
Plugin = WpPlugin, |
| 200 |
EventDispatcher = WpEventDispatcher, |
| 201 |
ObjectManager = WpObjectManager_new, |
| 202 |
Interest = WpObjectInterest_new, |
| 203 |
SessionItem = WpSessionItem_new, |
| 204 |
Constraint = Constraint, |
| 205 |
EventInterest = EventInterest, |
| 206 |
Device = WpDevice_new, |
| 207 |
SpaDevice = WpSpaDevice_new, |
| 208 |
Node = WpNode_new, |
| 209 |
LocalNode = WpImplNode_new, |
| 210 |
Link = WpLink_new, |
| 211 |
Pod = WpSpaPod, |
| 212 |
Json = WpSpaJson, |
| 213 |
State = WpState_new, |
| 214 |
LocalModule = WpImplModule_new, |
| 215 |
ImplMetadata = WpImplMetadata_new, |
| 216 |
Settings = WpSettings, |
| 217 |
Conf = WpConf, |
| 218 |
JsonUtils = JsonUtils, |
| 219 |
ProcUtils = ProcUtils, |
| 220 |
Properties = WpProperties_new, |
| 221 |
SimpleEventHook = WpSimpleEventHook_new, |
| 222 |
AsyncEventHook = WpAsyncEventHook_new, |
| 223 |
} |