| 1 |
-- WirePlumber |
| 2 |
-- |
| 3 |
-- Copyright © 2024 Collabora Ltd. |
| 4 |
-- |
| 5 |
-- SPDX-License-Identifier: MIT |
| 6 |
-- |
| 7 |
-- Select the media role target |
| 8 |
|
| 9 |
cutils = require("common-utils") |
| 10 |
lutils = require("linking-utils") |
| 11 |
log = Log.open_topic("s-linking") |
| 12 |
|
| 13 |
SimpleEventHook { |
| 14 |
name = "linking/find-media-role-target", |
| 15 |
after = { "linking/find-defined-target", |
| 16 |
"linking/find-filter-target" }, |
| 17 |
before = "linking/prepare-link", |
| 18 |
interests = { |
| 19 |
EventInterest { |
| 20 |
Constraint { "event.type", "=", "select-target" }, |
| 21 |
}, |
| 22 |
}, |
| 23 |
execute = function (event) |
| 24 |
local _, om, si, si_props, _, target = |
| 25 |
lutils:unwrap_select_target_event (event) |
| 26 |
|
| 27 |
local target_direction = cutils.getTargetDirection (si_props) |
| 28 |
local media_role = si_props["media.role"] |
| 29 |
|
| 30 |
-- bypass the hook if the target is already picked up or if the role is not |
| 31 |
-- defined |
| 32 |
if target or media_role == nil then |
| 33 |
return |
| 34 |
end |
| 35 |
|
| 36 |
log:info (si, string.format ("handling item %d: %s (%s) role (%s)", si.id, |
| 37 |
tostring (si_props ["node.name"]), tostring (si_props ["node.id"]), media_role)) |
| 38 |
|
| 39 |
for si_target in om:iterate { |
| 40 |
type = "SiLinkable", |
| 41 |
Constraint { "item.node.direction", "=", target_direction }, |
| 42 |
Constraint { "device.intended-roles", "+" }, |
| 43 |
Constraint { "media.type", "=", si_props["media.type"] }, |
| 44 |
} do |
| 45 |
|
| 46 |
local roles_json = si_target.properties["device.intended-roles"] |
| 47 |
local roles_table = Json.Raw(roles_json):parse() |
| 48 |
|
| 49 |
for _, target_role in ipairs(roles_table) do |
| 50 |
if target_role == media_role then |
| 51 |
target = si_target |
| 52 |
break |
| 53 |
end |
| 54 |
end |
| 55 |
|
| 56 |
if target then |
| 57 |
break |
| 58 |
end |
| 59 |
end |
| 60 |
|
| 61 |
-- set target |
| 62 |
if target ~= nil then |
| 63 |
log:info(si, |
| 64 |
string.format("... media role target picked: %s (%s)", |
| 65 |
tostring(target.properties["node.name"]), |
| 66 |
tostring(target.properties["node.id"]))) |
| 67 |
event:set_data("target", target) |
| 68 |
end |
| 69 |
end |
| 70 |
}:register() |