Debian Codegraph @

tests/wplua/scripts/monitor-rules.lua

raw
1 -- WirePlumber
2 --
3 -- Copyright © 2021 Collabora Ltd.
4 -- @author George Kiagiadakis <[email protected]>
5 --
6 -- SPDX-License-Identifier: MIT
7
8 local config = {}
9 config.rules = {
10 {
11 matches = {
12 {
13 { "device.name", "matches", "bluez_card.*" },
14 },
15 },
16 apply_properties = {
17 ["device.nick"] = "My Device",
18 },
19 },
20 {
21 matches = {
22 {
23 { "node.name", "matches", "bluez_input.*" },
24 },
25 {
26 { "node.name", "matches", "bluez_output.*" },
27 },
28 },
29 apply_properties = {
30 ["node.pause-on-idle"] = true,
31 },
32 },
33 }
34
35 for _, r in ipairs(config.rules or {}) do
36 r.interests = {}
37 for _, i in ipairs(r.matches) do
38 local interest_desc = { type = "properties" }
39 for _, c in ipairs(i) do
40 c.type = "pw"
41 table.insert(interest_desc, Constraint(c))
42 end
43 local interest = Interest(interest_desc)
44 table.insert(r.interests, interest)
45 end
46 r.matches = nil
47 end
48
49 function rulesApplyProperties(properties)
50 for _, r in ipairs(config.rules or {}) do
51 if r.apply_properties then
52 for _, interest in ipairs(r.interests) do
53 if interest:matches(properties) then
54 for k, v in pairs(r.apply_properties) do
55 properties[k] = v
56 end
57 end
58 end
59 end
60 end
61 end
62
63 local test1 = {
64 ["node.name"] = "bluez_output.test1"
65 }
66 rulesApplyProperties(test1)
67 assert(test1["node.pause-on-idle"] == true)
68
69 local test2 = {
70 ["device.name"] = "bluez_card.test2"
71 }
72 rulesApplyProperties(test2)
73 assert(test2["device.nick"] == "My Device")
74
75 local test3 = {
76 ["device.name"] = "not_a_match"
77 }
78 rulesApplyProperties(test3)
79 assert(test3["device.nick"] == nil)
80 assert(test3["node.pause-on-idle"] == nil)