Debian Codegraph @

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

raw
1 -- WirePlumber
2 --
3 -- Copyright © 2022 Collabora Ltd.
4 --
5 -- SPDX-License-Identifier: MIT
6 --
7 -- Traverse through all the possible targets to pick up target node.
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 SimpleEventHook {
15 name = "linking/find-best-target",
16 after = { "linking/find-defined-target",
17 "linking/find-filter-target",
18 "linking/find-media-role-target",
19 "linking/find-default-target" },
20 before = "linking/prepare-link",
21 interests = {
22 EventInterest {
23 Constraint { "event.type", "=", "select-target" },
24 },
25 },
26 execute = function (event)
27 local source, om, si, si_props, si_flags, target =
28 lutils:unwrap_select_target_event (event)
29
30 -- bypass the hook if the target is already picked up
31 if target then
32 return
33 end
34
35 local target_direction = cutils.getTargetDirection (si_props)
36 local target_picked = nil
37 local target_can_passthrough = false
38 local target_priority = 0
39 local target_plugged = 0
40
41 log:info (si, string.format ("handling item: %s (%s)",
42 tostring (si_props ["node.name"]), tostring (si_props ["node.id"])))
43
44 for target in om:iterate {
45 type = "SiLinkable",
46 Constraint { "item.node.type", "=", "device" },
47 Constraint { "item.node.direction", "=", target_direction },
48 Constraint { "media.type", "=", si_props ["media.type"] },
49 } do
50 local target_props = target.properties
51 local target_node_id = target_props ["node.id"]
52 local si_target_node = target:get_associated_proxy ("node")
53 local si_target_link_group = si_target_node.properties ["node.link-group"]
54 local priority = tonumber (target_props ["priority.session"]) or 0
55
56 log:debug (string.format ("Looking at: %s (%s)",
57 tostring (target_props ["node.name"]),
58 tostring (target_node_id)))
59
60 -- Skip smart filters as best target
61 if si_target_link_group ~= nil and
62 futils.is_filter_smart (target_direction, si_target_link_group) then
63 Log.debug ("... ignoring smart filter as best target")
64 goto skip_linkable
65 end
66
67 if not lutils.canLink (si_props, target) then
68 log:debug ("... cannot link, skip linkable")
69 goto skip_linkable
70 end
71
72 if not lutils.haveAvailableRoutes (target_props) then
73 log:debug ("... does not have routes, skip linkable")
74 goto skip_linkable
75 end
76
77 local passthrough_compatible, can_passthrough =
78 lutils.checkPassthroughCompatibility (si, target)
79 if not passthrough_compatible then
80 log:debug ("... passthrough is not compatible, skip linkable")
81 goto skip_linkable
82 end
83
84 local plugged = tonumber (target_props ["item.plugged.usec"]) or 0
85
86 log:debug ("... priority:" .. tostring (priority) .. ", plugged:" .. tostring (plugged))
87
88 -- (target_picked == NULL) --> make sure at least one target is picked.
89 -- (priority > target_priority) --> pick the highest priority linkable(node)
90 -- target.
91 -- (priority == target_priority and plugged > target_plugged) --> pick the
92 -- latest connected/plugged(in time) linkable(node) target.
93 if (target_picked == nil or
94 priority > target_priority or
95 (priority == target_priority and plugged > target_plugged)) then
96 log:debug ("... picked")
97 target_picked = target
98 target_can_passthrough = can_passthrough
99 target_priority = priority
100 target_plugged = plugged
101 end
102 ::skip_linkable::
103 end
104
105 if target_picked then
106 log:info (si,
107 string.format ("... best target picked: %s (%s), can_passthrough:%s",
108 tostring (target_picked.properties ["node.name"]),
109 tostring (target_picked.properties ["node.id"]),
110 tostring (target_can_passthrough)))
111 si_flags.can_passthrough = target_can_passthrough
112 event:set_data ("target", target_picked)
113 end
114 end
115 }:register ()