Debian Codegraph @

src/scripts/monitors/alsa-midi.lua

raw
1 -- WirePlumber
2 --
3 -- Copyright © 2021 Collabora Ltd.
4 -- @author Julian Bouzas <[email protected]>
5 --
6 -- SPDX-License-Identifier: MIT
7
8 cutils = require ("common-utils")
9 log = Log.open_topic ("s-monitors")
10
11 defaults = {}
12 defaults.node_properties = { -- Midi bridge node properties
13 ["factory.name"] = "api.alsa.seq.bridge",
14
15 -- Name set for the node with ALSA MIDI ports
16 ["node.name"] = "Midi-Bridge",
17
18 -- Set priorities so that it can be used as a fallback driver (see pipewire#3562)
19 ["priority.session"] = "100",
20 ["priority.driver"] = "1",
21 }
22
23 config = {}
24 config.monitoring = Core.test_feature ("monitor.alsa-midi.monitoring")
25 config.node_properties = Conf.get_section_as_properties (
26 "monitor.alsa-midi.properties", defaults.node_properties)
27
28 SND_PATH = "/dev/snd"
29 SEQ_NAME = "seq"
30 SND_SEQ_PATH = SND_PATH .. "/" .. SEQ_NAME
31
32 midi_node = nil
33 fm_plugin = nil
34
35 function CreateMidiNode ()
36 -- create the midi node
37 local node = Node("spa-node-factory", config.node_properties)
38 node:activate(Feature.Proxy.BOUND, function (n)
39 log:info ("activated Midi bridge")
40 end)
41
42 return node;
43 end
44
45 if GLib.access (SND_SEQ_PATH, "rw") then
46 midi_node = CreateMidiNode ()
47 elseif config.monitoring then
48 fm_plugin = Plugin.find("file-monitor-api")
49 end
50
51 -- Only monitor the MIDI device if file does not exist and plugin API is loaded
52 if midi_node == nil and fm_plugin ~= nil then
53 -- listen for changed events
54 fm_plugin:connect ("changed", function (o, file, old, evtype)
55 -- files attributes changed
56 if evtype == "attribute-changed" then
57 if file ~= SND_SEQ_PATH then
58 return
59 end
60 if midi_node == nil and GLib.access (SND_SEQ_PATH, "rw") then
61 midi_node = CreateMidiNode ()
62 fm_plugin:call ("remove-watch", SND_PATH)
63 end
64 end
65
66 -- directory is going to be unmounted
67 if evtype == "pre-unmount" then
68 fm_plugin:call ("remove-watch", SND_PATH)
69 end
70 end)
71
72 -- add watch
73 fm_plugin:call ("add-watch", SND_PATH, "m")
74 end