/
auto-connect-ports.lua
210 lines (176 loc) · 5.79 KB
/
auto-connect-ports.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
-- As explained on: https://bennett.dev/auto-link-pipewire-ports-wireplumber/
--
-- This script keeps my stereo-null-sink connected to whatever output I'm currently using.
-- I do this so Pulseaudio (and Wine) always sees a stereo output plus I can swap the output
-- without needing to reconnect everything.
-- Link two ports together
function link_port(output_port, input_port)
if not input_port or not output_port then
return false
end
local link_args = {
["link.input.node"] = input_port.properties["node.id"],
["link.input.port"] = input_port.properties["object.id"],
["link.output.node"] = output_port.properties["node.id"],
["link.output.port"] = output_port.properties["object.id"],
-- The node never got created if it didn't have this field set to something
["object.id"] = nil,
-- I was running into issues when I didn't have this set
["object.linger"] = true,
["node.description"] = "Link created by auto_connect_ports"
}
local link = Link("link-factory", link_args)
link:activate(1)
return true
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
-- Automatically link ports together by their specific audio channels.
--
-- ┌──────────────────┐ ┌───────────────────┐
-- │ │ │ │
-- │ FL ├────────►│ AUX0 │
-- │ OUTPUT │ │ │
-- │ FR ├────────►│ AUX1 INPUT │
-- │ │ │ │
-- └──────────────────┘ │ AUX2 │
-- │ │
-- └───────────────────┘
--
-- -- Call this method inside a script in global scope
--
-- auto_connect_ports {
--
-- -- A constraint for all the required ports of the output device
-- output = Constraint { "node.name"}
--
-- -- A constraint for all the required ports of the input device
-- input = Constraint { .. }
--
-- -- A mapping of output audio channels to input audio channels
--
-- connections = {
-- ["FL"] = "AUX0"
-- ["FR"] = "AUX1"
-- }
--
-- }
--
function auto_connect_ports(args)
local output_om = ObjectManager {
Interest {
type = "port",
args["output"],
Constraint { "port.direction", "equals", "out" }
}
}
local input_om = ObjectManager {
Interest {
type = "port",
args["input"],
Constraint { "port.direction", "equals", "in" }
}
}
local all_links = ObjectManager {
Interest {
type = "link",
}
}
local unless = nil
if args["unless"] then
unless = ObjectManager {
Interest {
type = "port",
args["unless"],
Constraint { "port.direction", "equals", "in" }
}
}
end
function _connect()
local delete_links = unless and unless:get_n_objects() > 0
print("Delete links", delete_links)
for output_name, input_name in pairs(args.connect) do
local output = output_om:lookup { Constraint { "audio.channel", "equals", output_name } }
local input = input_om:lookup { Constraint { "audio.channel", "equals", input_name } }
if delete_links then
delete_link(all_links, output, input)
else
link_port(output, input)
end
end
end
output_om:connect("object-added", _connect)
input_om:connect("object-added", _connect)
all_links:connect("object-added", _connect)
output_om:activate()
input_om:activate()
all_links:activate()
if unless then
unless:connect("object-added", _connect)
unless:connect("object-removed", _connect)
unless:activate()
end
end
-- Auto connect the stereo null sink to the first two channels of the EVO16
auto_connect_ports {
output = Constraint { "object.path", "matches", "stereo-null-sink:*" },
input = Constraint { "port.alias", "matches", "Audient EVO16:*" },
connect = {
["FL"] = "AUX0",
["FR"] = "AUX1"
}
}
-- Auto connect the stereo null sink to the jack_sink for when the jack server gets started
auto_connect_ports {
output = Constraint { "object.path", "matches", "stereo-null-sink:*" },
input = Constraint { "object.path", "matches", "jack_sink:*" },
connect = {
["FL"] = "FL",
["FR"] = "FR"
}
}
-- Auto connect the stereo null sink to the jack_sink for when the jack server gets started
auto_connect_ports {
output = Constraint { "object.path", "matches", "stereo-null-sink:*" },
input = Constraint { "object.path", "matches", "alsa:pcm:*" },
connect = {
["FL"] = "FL",
["FR"] = "FR"
},
-- Don't connect to speakers if there are bluetooth headphones plugged in
unless = Constraint { "object.path", "matches", "bluez_output.*" }
}
-- Auto connect the stereo null sink to bluetooth headphones
auto_connect_ports {
output = Constraint { "object.path", "matches", "stereo-null-sink:*" },
input = Constraint { "object.path", "matches", "bluez_output.*" },
connect = {
["FL"] = "FL",
["FR"] = "FR"
}
}