Debian Codegraph @

src/scripts/monitors/bluez-midi.lua

raw
1 -- WirePlumber
2 --
3 -- Copyright © 2022 Pauli Virtanen
4 -- @author Pauli Virtanen
5 --
6 -- SPDX-License-Identifier: MIT
7
8 cutils = require ("common-utils")
9 log = Log.open_topic ("s-monitors")
10
11 defaults = {}
12 defaults.servers = { "bluez_midi.server" }
13
14 config = {}
15 config.seat_monitoring = Core.test_feature ("monitor.bluez.seat-monitoring")
16 config.properties = Conf.get_section_as_properties ("monitor.bluez-midi.properties")
17 config.servers = Conf.get_section_as_array ("monitor.bluez-midi.servers", defaults.servers)
18 config.rules = Conf.get_section_as_json ("monitor.bluez-midi.rules", Json.Array {})
19
20 -- unique device/node name tables
21 node_names_table = nil
22 id_to_name_table = nil
23
24 function setLatencyOffset(node, offset_msec)
25 if not offset_msec then
26 return
27 end
28
29 local props = { "Spa:Pod:Object:Param:Props", "Props" }
30 props.latencyOffsetNsec = tonumber(offset_msec) * 1000000
31
32 local param = Pod.Object(props)
33 log:debug(param, "setting latency offset on " .. tostring(node))
34 node:set_param("Props", param)
35 end
36
37 function createNode(parent, id, type, factory, properties)
38 properties["factory.name"] = factory
39
40 -- set the node description
41 local desc = properties["node.description"]
42 -- sanitize description, replace ':' with ' '
43 properties["node.description"] = desc:gsub("(:)", " ")
44
45 -- set the node name
46 local name =
47 "bluez_midi." .. properties["api.bluez5.address"]
48 -- sanitize name
49 name = name:gsub("([^%w_%-%.])", "_")
50 -- deduplicate nodes with the same name
51 properties["node.name"] = name
52 for counter = 2, 99, 1 do
53 if node_names_table[properties["node.name"]] ~= true then
54 node_names_table[properties["node.name"]] = true
55 break
56 end
57 properties["node.name"] = name .. "." .. counter
58 end
59
60 properties["api.glib.mainloop"] = "true"
61
62 -- apply properties from the rules in the configuration file
63 properties = JsonUtils.match_rules_update_properties (config.rules, properties)
64
65 local latency_offset = properties["node.latency-offset-msec"]
66 properties["node.latency-offset-msec"] = nil
67
68 -- create the node
69 -- it doesn't necessarily need to be a local node,
70 -- the other Bluetooth parts run in the local process,
71 -- so it's consistent to have also this here
72 local node = LocalNode("spa-node-factory", properties)
73 node:activate(Feature.Proxy.BOUND)
74 parent:store_managed_object(id, node)
75 id_to_name_table[id] = properties["node.name"]
76 setLatencyOffset(node, latency_offset)
77 end
78
79 function createMonitor()
80 local monitor_props = {}
81 for k, v in pairs(config.properties or Properties()) do
82 monitor_props[k] = v
83 end
84
85 monitor_props["api.glib.mainloop"] = "true"
86
87 local monitor = SpaDevice("api.bluez5.midi.enum", monitor_props)
88 if monitor then
89 monitor:connect("create-object", createNode)
90 monitor:connect("object-removed", function (parent, id)
91 node_names_table[id_to_name_table[id]] = nil
92 id_to_name_table[id] = nil
93 end)
94 else
95 log:notice("PipeWire's BlueZ MIDI SPA missing or broken. Bluetooth not supported.")
96 return nil
97 end
98
99 -- reset the name tables to make sure names are recycled
100 node_names_table = {}
101 id_to_name_table = {}
102
103 monitor:activate(Feature.SpaDevice.ENABLED)
104 return monitor
105 end
106
107 function createServers()
108 local servers = {}
109 local i = 1
110
111 for k, v in pairs(config.servers) do
112 local node_props = {
113 ["node.name"] = v,
114 ["node.description"] = string.format(I18n.gettext("BLE MIDI %d"), i),
115 ["api.bluez5.role"] = "server",
116 ["factory.name"] = "api.bluez5.midi.node",
117 ["api.glib.mainloop"] = "true",
118 }
119 node_props = JsonUtils.match_rules_update_properties (config.rules, node_props)
120
121 local latency_offset = node_props["node.latency-offset-msec"]
122 node_props["node.latency-offset-msec"] = nil
123
124 local node = LocalNode("spa-node-factory", node_props)
125 if node then
126 node:activate(Feature.Proxy.BOUND)
127 table.insert(servers, node)
128 setLatencyOffset(node, latency_offset)
129 else
130 log:notice("Failed to create BLE MIDI server.")
131 end
132 i = i + 1
133 end
134
135 return servers
136 end
137
138 if config.seat_monitoring then
139 logind_plugin = Plugin.find("logind")
140 end
141 if logind_plugin then
142 -- if logind support is enabled, activate
143 -- the monitor only when the seat is active
144 function startStopMonitor(seat_state)
145 log:info(logind_plugin, "Seat state changed: " .. seat_state)
146
147 if seat_state == "active" then
148 monitor = createMonitor()
149 servers = createServers()
150 elseif monitor then
151 monitor:deactivate(Feature.SpaDevice.ENABLED)
152 monitor = nil
153 servers = nil
154 end
155 end
156
157 logind_plugin:connect("state-changed", function(p, s) startStopMonitor(s) end)
158 startStopMonitor(logind_plugin:call("get-state"))
159 else
160 monitor = createMonitor()
161 servers = createServers()
162 end