Debian Codegraph @

src/scripts/linking/find-filter-target.lua

raw
1 -- WirePlumber
2 --
3 -- Copyright © 2023 Collabora Ltd.
4 --
5 -- SPDX-License-Identifier: MIT
6 --
7 -- Check if the target node is a filter target.
8
9 lutils = require ("linking-utils")
10 cutils = require ("common-utils")
11 futils = require ("filter-utils")
12 log = Log.open_topic ("s-linking")
13
14 function findFilterTarget (si, om)
15 local node = si:get_associated_proxy ("node")
16 local link_group = node.properties ["node.link-group"]
17 local target_id = -1
18
19 -- return nil if session item is not a filter node
20 if link_group == nil then
21 return nil, false
22 end
23
24 -- return nil if filter is not smart
25 local direction = cutils.getTargetDirection (si.properties)
26 if not futils.is_filter_smart (direction, link_group) then
27 return nil, false
28 end
29
30 -- get the filter target
31 return futils.get_filter_target (direction, link_group), true
32 end
33
34 SimpleEventHook {
35 name = "linking/find-filter-target",
36 after = "linking/find-audio-group-target",
37 before = "linking/prepare-link",
38 interests = {
39 EventInterest {
40 Constraint { "event.type", "=", "select-target" },
41 },
42 },
43 execute = function (event)
44 local source, om, si, si_props, si_flags, target =
45 lutils:unwrap_select_target_event (event)
46
47 -- bypass the hook if the target is already picked up
48 if target then
49 return
50 end
51
52 local dont_fallback = cutils.parseBool (si_props ["node.dont-fallback"])
53 local target_picked = false
54 local allow_fallback
55
56 log:info (si, string.format ("handling item %d: %s (%s)", si.id,
57 tostring (si_props ["node.name"]), tostring (si_props ["node.id"])))
58
59 target, is_smart_filter = findFilterTarget (si, om)
60
61 local can_passthrough, passthrough_compatible
62 if target then
63 passthrough_compatible, can_passthrough =
64 lutils.checkPassthroughCompatibility (si, target)
65 if lutils.canLink (si_props, target) and passthrough_compatible then
66 target_picked = true
67 end
68 end
69
70 if target_picked and target then
71 log:info (si,
72 string.format ("... filter target picked: %s (%s), can_passthrough:%s",
73 tostring (target.properties ["node.name"]),
74 tostring (target.properties ["node.id"]),
75 tostring (can_passthrough)))
76 si_flags.can_passthrough = can_passthrough
77 event:set_data ("target", target)
78 elseif is_smart_filter and dont_fallback then
79 -- send error to client and destroy node if linger is not set
80 local linger = cutils.parseBool (si_props ["node.linger"])
81 if not linger then
82 local node = si:get_associated_proxy ("node")
83 lutils.sendClientError (event, node, -2, "smart filter defined target not found")
84 node:request_destroy ()
85 log:info(si, "... destroyed node as smart filter defined target was not found")
86 else
87 log:info(si, "... waiting for smart filter defined target as dont-fallback is set")
88 end
89 event:stop_processing ()
90 end
91 end
92 }:register ()