From e4325bc41d5779c279f0f776ddade9974641074d Mon Sep 17 00:00:00 2001 From: Pauli Virtanen Date: Sun, 27 Feb 2022 23:08:21 +0200 Subject: [PATCH] policy-node: support target.object key Setting node target by id is not safe due to id reuse, and so target.object which specifies the target by its object.serial was introduced, and is used e.g. by pipewire-pulse. Use target.object specifications in policy-node if they are present. --- src/scripts/create-item.lua | 2 ++ src/scripts/policy-node.lua | 41 ++++++++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/scripts/create-item.lua b/src/scripts/create-item.lua index 696ac0b9..3b2cc832 100644 --- a/src/scripts/create-item.lua +++ b/src/scripts/create-item.lua @@ -21,6 +21,8 @@ function configProperties(node) ["node.id"] = node["bound-id"], ["client.id"] = np["client.id"], ["object.path"] = np["object.path"], + ["object.serial"] = np["object.serial"], + ["target.object"] = np["target.object"], ["priority.session"] = np["priority.session"], ["device.id"] = np["device.id"], ["card.profile.device"] = np["card.profile.device"], diff --git a/src/scripts/policy-node.lua b/src/scripts/policy-node.lua index 1688accf..344de20e 100644 --- a/src/scripts/policy-node.lua +++ b/src/scripts/policy-node.lua @@ -266,32 +266,53 @@ end -- that is currently being handled function findDefinedTarget (properties) local metadata = config.move and metadata_om:lookup() - local target_id = metadata - and metadata:find(properties["node.id"], "target.node") - or properties["node.target"] local target_direction = getTargetDirection(properties) + local target_key + local target_value - if target_id and tonumber(target_id) then + if properties["target.object"] ~= nil then + target_value = properties["target.object"] + target_key = "object.serial" + elseif properties["node.target"] ~= nil then + target_value = properties["node.target"] + target_key = "node.id" + end + + if metadata then + local id = metadata:find(properties["node.id"], "target.object") + if id ~= nil then + target_value = id + target_key = "object.serial" + else + id = metadata:find(properties["node.id"], "target.node") + if id ~= nil then + target_value = id + target_key = "node.id" + end + end + end + + if target_value and tonumber(target_value) then local si_target = linkables_om:lookup { - Constraint { "node.id", "=", target_id }, + Constraint { target_key, "=", target_value }, } if si_target and canLink (properties, si_target) then return si_target, true end end - if target_id then + if target_value then for si_target in linkables_om:iterate() do local target_props = si_target.properties - if (target_props["node.name"] == target_id or - target_props["object.path"] == target_id) and + if (target_props["node.name"] == target_value or + target_props["object.path"] == target_value) and target_props["item.node.direction"] == target_direction and canLink (properties, si_target) then return si_target, true end end end - return nil, (target_id and target_id ~= "-1") + return nil, (target_value and target_value ~= "-1") end function parseParam(param, id) @@ -815,7 +836,7 @@ end if config.move then metadata_om:connect("object-added", function (om, metadata) metadata:connect("changed", function (m, subject, key, t, value) - if key == "target.node" then + if key == "target.node" or key == "target.object" then scheduleRescan () end end)