| 1 |
-- WirePlumber |
| 2 |
-- |
| 3 |
-- Copyright © 2022 Collabora Ltd. |
| 4 |
-- |
| 5 |
-- SPDX-License-Identifier: MIT |
| 6 |
-- |
| 7 |
-- Check if the target node is defined explicitly. |
| 8 |
-- This definition can be done in two ways. |
| 9 |
-- 1. "node.target"/"target.object" in the node properties |
| 10 |
-- 2. "target.node"/"target.object" in the default metadata |
| 11 |
|
| 12 |
lutils = require ("linking-utils") |
| 13 |
cutils = require ("common-utils") |
| 14 |
log = Log.open_topic ("s-linking") |
| 15 |
|
| 16 |
SimpleEventHook { |
| 17 |
name = "linking/find-defined-target", |
| 18 |
before = "linking/prepare-link", |
| 19 |
interests = { |
| 20 |
EventInterest { |
| 21 |
Constraint { "event.type", "=", "select-target" }, |
| 22 |
}, |
| 23 |
}, |
| 24 |
execute = function (event) |
| 25 |
local source, om, si, si_props, si_flags, target = |
| 26 |
lutils:unwrap_select_target_event (event) |
| 27 |
|
| 28 |
-- bypass the hook if the target is already picked up |
| 29 |
if target then |
| 30 |
return |
| 31 |
end |
| 32 |
|
| 33 |
log:info (si, string.format ("handling item %d: %s (%s)", si.id, |
| 34 |
tostring (si_props ["node.name"]), tostring (si_props ["node.id"]))) |
| 35 |
|
| 36 |
local metadata = Settings.get_boolean ("linking.allow-moving-streams") and |
| 37 |
cutils.get_default_metadata_object () |
| 38 |
local dont_fallback = cutils.parseBool (si_props ["node.dont-fallback"]) |
| 39 |
local dont_move = cutils.parseBool (si_props ["node.dont-move"]) |
| 40 |
local target_key |
| 41 |
local target_value = nil |
| 42 |
local node_defined = false |
| 43 |
local target_picked = nil |
| 44 |
|
| 45 |
if si_props ["target.object"] ~= nil then |
| 46 |
target_value = si_props ["target.object"] |
| 47 |
target_key = "object.serial" |
| 48 |
node_defined = true |
| 49 |
elseif si_props ["node.target"] ~= nil then |
| 50 |
target_value = si_props ["node.target"] |
| 51 |
target_key = "node.id" |
| 52 |
node_defined = true |
| 53 |
end |
| 54 |
|
| 55 |
if metadata and not dont_move then |
| 56 |
local id = metadata:find (si_props ["node.id"], "target.object") |
| 57 |
if id ~= nil then |
| 58 |
target_value = id |
| 59 |
target_key = "object.serial" |
| 60 |
node_defined = false |
| 61 |
else |
| 62 |
id = metadata:find (si_props ["node.id"], "target.node") |
| 63 |
if id ~= nil then |
| 64 |
target_value = id |
| 65 |
target_key = "node.id" |
| 66 |
node_defined = false |
| 67 |
end |
| 68 |
end |
| 69 |
end |
| 70 |
|
| 71 |
if target_value == "-1" then |
| 72 |
target_picked = false |
| 73 |
target = nil |
| 74 |
elseif target_value and tonumber (target_value) then |
| 75 |
target = om:lookup { |
| 76 |
type = "SiLinkable", |
| 77 |
Constraint { target_key, "=", target_value }, |
| 78 |
} |
| 79 |
if target and lutils.canLink (si_props, target) then |
| 80 |
target_picked = true |
| 81 |
end |
| 82 |
elseif target_value then |
| 83 |
for lnkbl in om:iterate { type = "SiLinkable" } do |
| 84 |
local target_props = lnkbl.properties |
| 85 |
if (target_props ["node.name"] == target_value or |
| 86 |
target_props ["object.path"] == target_value) and |
| 87 |
target_props ["item.node.direction"] == cutils.getTargetDirection (si_props) and |
| 88 |
lutils.canLink (si_props, lnkbl) then |
| 89 |
target_picked = true |
| 90 |
target = lnkbl |
| 91 |
break |
| 92 |
end |
| 93 |
end |
| 94 |
end |
| 95 |
|
| 96 |
local can_passthrough, passthrough_compatible |
| 97 |
if target then |
| 98 |
passthrough_compatible, can_passthrough = |
| 99 |
lutils.checkPassthroughCompatibility (si, target) |
| 100 |
if not passthrough_compatible then |
| 101 |
target = nil |
| 102 |
end |
| 103 |
end |
| 104 |
|
| 105 |
si_flags.has_defined_target = false |
| 106 |
if target_picked and target then |
| 107 |
log:info (si, |
| 108 |
string.format ("... defined target picked: %s (%s), can_passthrough:%s", |
| 109 |
tostring (target.properties ["node.name"]), |
| 110 |
tostring (target.properties ["node.id"]), |
| 111 |
tostring (can_passthrough))) |
| 112 |
si_flags.has_node_defined_target = node_defined |
| 113 |
si_flags.can_passthrough = can_passthrough |
| 114 |
si_flags.has_defined_target = true |
| 115 |
event:set_data ("target", target) |
| 116 |
elseif target_value and dont_fallback then |
| 117 |
-- send error to client and destroy node if linger is not set |
| 118 |
local linger = cutils.parseBool (si_props ["node.linger"]) |
| 119 |
if not linger then |
| 120 |
local node = si:get_associated_proxy ("node") |
| 121 |
lutils.sendClientError (event, node, -2, "defined target not found") |
| 122 |
node:request_destroy () |
| 123 |
log:info(si, "... destroyed node as defined target was not found") |
| 124 |
else |
| 125 |
log:info(si, "... waiting for defined target as dont-fallback is set") |
| 126 |
end |
| 127 |
event:stop_processing () |
| 128 |
end |
| 129 |
|
| 130 |
end |
| 131 |
}:register () |