Debian Codegraph @

tools/configure_pipewire.sh

raw
1 #!/bin/bash
2
3 # This script configures pipewire to work both in the graphical environment and in the console with root apps.
4 # Supports both legacy WirePlumber (Lua) and modern WirePlumber (0.5+ config format)
5
6 # Detect WirePlumber version
7 detect_wireplumber_version() {
8 if command -v wireplumber >/dev/null 2>&1; then
9 local version
10 version=$(wireplumber --version 2>/dev/null | grep -oP 'wireplumber \K[0-9]+\.[0-9]+' | head -n1)
11 if [ -n "$version" ]; then
12 echo "$version"
13 return 0
14 fi
15 fi
16 # Default to legacy if detection fails
17 echo "0.4"
18 return 1
19 }
20
21 # Compare versions (returns 0 if version1 >= version2)
22 version_ge() {
23 [ "$(printf '%s\n' "$1" "$2" | sort -V | head -n1)" = "$2" ]
24 }
25
26 if [[ $(whoami) != "root" ]]; then
27 # Track overall success
28 CONFIG_SUCCESS=true
29
30 # Get the current user's XDG_HOME
31 xdgPath="${XDG_CONFIG_HOME:-$HOME/.config}"
32 mkdir -p "$xdgPath/pipewire" || CONFIG_SUCCESS=false
33
34 # Detect WirePlumber version
35 WP_VERSION=$(detect_wireplumber_version)
36 echo "Detected WirePlumber version: $WP_VERSION"
37
38 # Determine configuration format
39 if version_ge "$WP_VERSION" "0.5"; then
40 echo "Using modern WirePlumber configuration format (0.5+)"
41 USE_MODERN_CONFIG=true
42 mkdir -p "$xdgPath/wireplumber/wireplumber.conf.d"
43 mkdir -p "$xdgPath/wireplumber/main.conf.d"
44 mkdir -p "$xdgPath/wireplumber/bluetooth.conf.d"
45 else
46 echo "Using legacy WirePlumber Lua configuration format"
47 USE_MODERN_CONFIG=false
48 mkdir -p "$xdgPath/wireplumber/main.lua.d"
49 mkdir -p "$xdgPath/wireplumber/bluetooth.lua.d"
50 fi
51
52 # Create drop-in configuration for PipeWire-Pulse console access
53 mkdir -p "$xdgPath/pipewire/pipewire-pulse.conf.d"
54 # Warn user if we are going to overwrite an existing fenrir console config
55 if [ -f "$xdgPath/pipewire/pipewire-pulse.conf.d/50-fenrir-console.conf" ]; then
56 read -r -p "This will replace the current file located at $xdgPath/pipewire/pipewire-pulse.conf.d/50-fenrir-console.conf, press enter to continue or control+c to abort. "
57 fi
58 cat << "EOF" > "$xdgPath/pipewire/pipewire-pulse.conf.d/50-fenrir-console.conf" || CONFIG_SUCCESS=false
59 # Fenrir console audio support
60 # Adds secondary socket for console applications like Fenrir
61
62 pulse.properties = {
63 # the addresses this server listens on
64 server.address = [
65 "unix:native"
66 "unix:/tmp/pulse.sock" # console access socket
67 ]
68 }
69
70 # client/stream specific properties
71 pulse.rules = [
72 {
73 # speech dispatcher asks for too small latency and then underruns.
74 matches = [ { application.name = "~speech-dispatcher*" } ]
75 actions = {
76 update-props = {
77 pulse.min.req = 1024/48000 # 21ms
78 pulse.min.quantum = 1024/48000 # 21ms
79 }
80 }
81 }
82 ]
83 EOF
84
85 # Create WirePlumber configuration to prevent audio device suspension on console switch
86 if [ "$USE_MODERN_CONFIG" = true ]; then
87 # Modern WirePlumber 0.5+ configuration format
88 CONFIG_FILE="$xdgPath/wireplumber/main.conf.d/50-fenrir-no-suspend.conf"
89 if [ -f "$CONFIG_FILE" ]; then
90 read -r -p "This will replace the current file located at $CONFIG_FILE, press enter to continue or control+c to abort. "
91 fi
92 cat << "EOF" > "$CONFIG_FILE" || CONFIG_SUCCESS=false
93 # Fenrir console audio support
94 # Prevents audio device suspension when switching to TTY console
95
96 monitor.alsa.rules = [
97 {
98 matches = [
99 {
100 device.name = "~alsa_card.*"
101 }
102 ]
103 actions = {
104 update-props = {
105 api.alsa.use-acp = true
106 api.acp.auto-profile = false
107 api.acp.auto-port = false
108 session.suspend-timeout-seconds = 0
109 }
110 }
111 }
112 {
113 matches = [
114 {
115 node.name = "~alsa_input.*"
116 }
117 {
118 node.name = "~alsa_output.*"
119 }
120 ]
121 actions = {
122 update-props = {
123 session.suspend-timeout-seconds = 0
124 }
125 }
126 }
127 ]
128 EOF
129 else
130 # Legacy Lua configuration format
131 CONFIG_FILE="$xdgPath/wireplumber/main.lua.d/50-fenrir-no-suspend.lua"
132 if [ -f "$CONFIG_FILE" ]; then
133 read -r -p "This will replace the current file located at $CONFIG_FILE, press enter to continue or control+c to abort. "
134 fi
135 cat << "EOF" > "$CONFIG_FILE" || CONFIG_SUCCESS=false
136 -- Fenrir console audio support
137 -- Prevents audio device suspension when switching to TTY console
138
139 alsa_monitor.rules = {
140 {
141 matches = {
142 {
143 { "device.name", "matches", "alsa_card.*" },
144 },
145 },
146 apply_properties = {
147 ["api.alsa.use-acp"] = true,
148 ["api.acp.auto-profile"] = false,
149 ["api.acp.auto-port"] = false,
150 ["session.suspend-timeout-seconds"] = 0
151 },
152 },
153 {
154 matches = {
155 {
156 { "node.name", "matches", "alsa_input.*" },
157 },
158 {
159 { "node.name", "matches", "alsa_output.*" },
160 },
161 },
162 apply_properties = {
163 ["session.suspend-timeout-seconds"] = 0
164 },
165 },
166 }
167 EOF
168 fi
169
170 # Create WirePlumber bluetooth configuration to prevent disconnection on TTY switch
171 if [ "$USE_MODERN_CONFIG" = true ]; then
172 # Modern WirePlumber 0.5+ configuration format
173 CONFIG_FILE="$xdgPath/wireplumber/bluetooth.conf.d/50-fenrir-bluez.conf"
174 if [ -f "$CONFIG_FILE" ]; then
175 read -r -p "This will replace the current file located at $CONFIG_FILE, press enter to continue or control+c to abort. "
176 fi
177 cat << "EOF" > "$CONFIG_FILE" || CONFIG_SUCCESS=false
178 # Fenrir console audio support
179 # Prevents bluetooth disconnection when switching TTY
180
181 monitor.bluez.properties = {
182 # Disable logind integration to prevent bluetooth device suspension on TTY switch
183 bluez5.enable-sbc-xq = true
184 bluez5.enable-msbc = true
185 bluez5.enable-hw-volume = true
186 }
187
188 monitor.bluez.rules = [
189 {
190 matches = [
191 {
192 device.name = "~bluez_card.*"
193 }
194 ]
195 actions = {
196 update-props = {
197 session.suspend-timeout-seconds = 0
198 }
199 }
200 }
201 ]
202 EOF
203 else
204 # Legacy Lua configuration format
205 CONFIG_FILE="$xdgPath/wireplumber/bluetooth.lua.d/30-fenrir-bluez.lua"
206 if [ -f "$CONFIG_FILE" ]; then
207 read -r -p "This will replace the current file located at $CONFIG_FILE, press enter to continue or control+c to abort. "
208 fi
209 cat << "EOF" > "$CONFIG_FILE" || CONFIG_SUCCESS=false
210 -- Fenrir console audio support
211 -- Disables logind module to prevent bluetooth disconnection when switching TTY
212
213 bluez_monitor = {}
214 bluez_monitor.properties = {}
215 bluez_monitor.rules = {}
216
217 function bluez_monitor.enable()
218 load_monitor("bluez", {
219 properties = bluez_monitor.properties,
220 rules = bluez_monitor.rules,
221 })
222 end
223 EOF
224 fi
225
226 echo ""
227 echo "Configuration files created successfully."
228 if [ "$USE_MODERN_CONFIG" = true ]; then
229 echo "Using modern WirePlumber 0.5+ configuration format (.conf files)"
230 else
231 echo "Using legacy WirePlumber configuration format (.lua files)"
232 fi
233 echo ""
234 echo "Please ensure that your user is added to the audio group."
235 echo "If you have not yet done so, please run this script as root to write the client.conf file."
236 else
237 # This section does the root part:
238 CONFIG_SUCCESS=true
239 xdgPath="/root/.config"
240 mkdir -p "$xdgPath/pulse" || CONFIG_SUCCESS=false
241
242 # Warn user if we are going to overwrite an existing client.conf
243 if [ -f "$xdgPath/pulse/client.conf" ]; then
244 read -r -p "This will replace the current file located at $xdgPath/pulse/client.conf, press enter to continue or control+c to abort. "
245 fi
246
247 cat << "EOF" > "$xdgPath/pulse/client.conf" || CONFIG_SUCCESS=false
248 # This file is part of PulseAudio.
249 #
250 # PulseAudio is free software; you can redistribute it and/or modify
251 # it under the terms of the GNU Lesser General Public License as published by
252 # the Free Software Foundation; either version 2 of the License, or
253 # (at your option) any later version.
254 #
255 # PulseAudio is distributed in the hope that it will be useful, but
256 # WITHOUT ANY WARRANTY; without even the implied warranty of
257 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
258 # General Public License for more details.
259 #
260 # You should have received a copy of the GNU Lesser General Public License
261 # along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
262
263 ## Configuration file for PulseAudio clients. See pulse-client.conf(5) for
264 ## more information. Default values are commented out. Use either ; or # for
265 ## commenting.
266
267 ; default-sink =
268 ; default-source =
269 default-server = unix:/tmp/pulse.sock
270 ; default-dbus-server =
271
272 autospawn = no
273 ; autospawn = yes
274 ; daemon-binary = /usr/bin/pulseaudio
275 ; extra-arguments = --log-target=syslog
276
277 ; cookie-file =
278
279 ; enable-shm = yes
280 ; shm-size-bytes = 0 # setting this 0 will use the system-default, usually 64 MiB
281
282 ; auto-connect-localhost = no
283 ; auto-connect-display = no
284 EOF
285 echo "If you have not yet done so, run this script as your normal user to write the user configs"
286 fi
287
288 # If there were no errors tell user to restart, else warn them errors happened.
289 if [ "$CONFIG_SUCCESS" = true ]; then
290 echo "Configuration created successfully, please restart both Pipewire-pulseaudio and Wireplumber or your system, for changes to take affect."
291 exit 0
292 else
293 echo "Errors were encountered whilst writing the configuration, please correct them manually."
294 exit 1
295 fi