Fix wireplumber auto connect

This commit is contained in:
Christian Colglazier 2025-01-26 11:15:35 -05:00
parent e253055758
commit 75e898b8f7
4 changed files with 71 additions and 59 deletions

View File

@ -1,14 +0,0 @@
wireplumber.components = [
{
name = /home/aqua/.config/wireplumber/scripts/auto-connect-ports.lua,
type = script/lua
provides = custom.my-script
}
]
wireplumber.profiles = {
main = {
custom.my-script = required
}
}

View File

@ -0,0 +1,13 @@
wireplumber.components = [
{
name = auto-connect-ports.lua, type = script/lua
provides = custom.auto-connect-ports
}
]
wireplumber.profiles = {
main = {
custom.auto-connect-ports = required
}
}

View File

@ -7,7 +7,7 @@
-- Link two ports together -- Link two ports together
function link_port(output_port, input_port) function link_port(output_port, input_port)
if not input_port or not output_port then if not input_port or not output_port then
return false return nil
end end
local link_args = { local link_args = {
@ -29,42 +29,7 @@ function link_port(output_port, input_port)
local link = Link("link-factory", link_args) local link = Link("link-factory", link_args)
link:activate(1) link:activate(1)
return true return link
end
function delete_link(link_om, output_port, input_port)
print("Trying to delete")
if not input_port or not output_port then
print("No ports")
return false
end
local link = link_om:lookup {
Constraint {
"link.input.node", "equals", input_port.properties["node.id"]
},
Constraint {
"link.input.port", "equals", input_port.properties["object.id"],
},
Constraint {
"link.output.node", "equals", output_port.properties["node.id"],
},
Constraint {
"link.output.port", "equals", output_port.properties["object.id"],
}
}
if not link then
print("No link!")
return
end
print("Deleting link!")
link:request_destroy()
end end
-- Automatically link ports together by their specific audio channels. -- Automatically link ports together by their specific audio channels.
@ -107,6 +72,8 @@ function auto_connect_ports(args)
} }
} }
local links = {}
local input_om = ObjectManager { local input_om = ObjectManager {
Interest { Interest {
type = "port", type = "port",
@ -131,22 +98,40 @@ function auto_connect_ports(args)
Constraint { "port.direction", "equals", "in" } Constraint { "port.direction", "equals", "in" }
} }
} }
end end
function _connect() function _connect()
local delete_links = unless and unless:get_n_objects() > 0 local delete_links = unless and unless:get_n_objects() > 0
print("Delete links", delete_links) if delete_links then
for _i, link in pairs(links) do
link:request_destroy()
end
for output_name, input_name in pairs(args.connect) do links = {}
local output = output_om:lookup { Constraint { "audio.channel", "equals", output_name } }
local input = input_om:lookup { Constraint { "audio.channel", "equals", input_name } } return
end
for output_name, input_names in pairs(args.connect) do
local input_names = input_names[1] == nil and { input_names } or input_names
if delete_links then if delete_links then
delete_link(all_links, output, input)
else else
link_port(output, input) -- Iterate through all the output ports with the correct channel name
for output in output_om:iterate { Constraint { "audio.channel", "equals", output_name } } do
for _i, input_name in pairs(input_names) do
-- Iterate through all the input ports with the correct channel name
for input in input_om:iterate { Constraint { "audio.channel", "equals", input_name } } do
-- Link all the nodes
local link = link_port(output, input)
if link then
table.insert(links, link)
end
end
end
end
end end
end end
end end
@ -166,10 +151,12 @@ function auto_connect_ports(args)
end end
end end
-- pw-cli list-objects | grep object.path
-- Connect to speakers -- Connect to speakers
auto_connect_ports { auto_connect_ports {
output = Constraint { "object.path", "matches", "speakers:*" }, output = Constraint { "object.path", "matches", "speakers:*" },
input = Constraint { "object.path", "matches", "alsa:pcm:2:hw:2,0:playback:*" }, input = Constraint { "object.path", "matches", "alsa:acp:C8Pre:0:playback:*" },
connect = { connect = {
["FL"] = "AUX0", ["FL"] = "AUX0",
["FR"] = "AUX1" ["FR"] = "AUX1"

View File

@ -0,0 +1,26 @@
-- Dump all Wireplumber ports
function dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ',\n'
end
return s .. '} '
else
return tostring(o)
end
end
local port_om = ObjectManager {
Interest {
type = "port",
}
}
port_om:connect("object-added", function (om, port)
print(dump(port.properties) .. '\n\n')
end)
port_om:activate()