#!/bin/sh # Start the session's sound server with no systemd user manager: pipewire, # then wireplumber and pipewire-pulse once its socket is up, each in the # background under the caller's XDG_RUNTIME_DIR and session bus. Prints one # line: the three started and the PulseAudio socket up, or that this runtime # directory's server already runs (exit 0); otherwise why not (exit 1). The # GNOME session runs it once the shell answers, weston as its autolaunch # program, the X session from XDG autostart. AUDIO_TIMEOUT=120 [ -n "$XDG_RUNTIME_DIR" ] && [ -d "$XDG_RUNTIME_DIR/" ] || { echo "wabuntu-session-audio: no XDG_RUNTIME_DIR"; exit 1; } PIDS=$XDG_RUNTIME_DIR/.wabuntu-session-audio PW_SOCK=$XDG_RUNTIME_DIR/pipewire-0 PULSE_SOCK=$XDG_RUNTIME_DIR/pulse/native LOG=/var/log [ -d "$LOG/" ] && [ -w "$LOG/" ] || LOG=$XDG_RUNTIME_DIR say() { echo "wabuntu-session-audio: $*" echo "wabuntu-session-audio: $*" >> "$LOG/wabuntu-session-audio.log" } # alive PID [NAME]: PID is a live process, named NAME when one is given. # Builtins only: the waits below poll it. alive() { _l= { read -r _l < "/proc/$1/stat"; } 2>/dev/null || return 1 case $_l in *") "[ZX]" "*) return 1 ;; esac [ -z "$2" ] || case $_l in *" ($2) "*) ;; *) return 1 ;; esac } if [ -r "$PIDS" ]; then while read -r n p; do [ "$n" = pipewire ] && alive "$p" pipewire && { say "already running: pipewire $p on $PW_SOCK"; exit 0; } done < "$PIDS" fi uptime_s() { read -r _u _ < /proc/uptime; echo "${_u%.*}"; } t0=$(uptime_s) : > "$PIDS" # start PROGRAM: run it in the background, its output in its own log. start() { "$1" "$LOG/$1.log" 2>&1 & started=$! echo "$1 $started" >> "$PIDS" } # wait_sock SOCKET PID PROGRAM: 0 once SOCKET exists; 1, with the reason on # stdout, when PROGRAM exits first or the time runs out. wait_sock() { while [ ! -S "$1" ]; do alive "$2" || { echo "$3 exited: $(tail -n 1 "$LOG/$3.log" 2>/dev/null)"; return 1; } [ $(( $(uptime_s) - t0 )) -lt "$AUDIO_TIMEOUT" ] || { echo "no $1 after ${AUDIO_TIMEOUT}s"; return 1; } sleep 1 done } # wireplumber and pipewire-pulse started before the daemon's socket find no # daemon and exit. start pipewire pw=$started r=$(wait_sock "$PW_SOCK" "$pw" pipewire) || { say "$r"; exit 1; } start wireplumber wp=$started start pipewire-pulse pp=$started r=$(wait_sock "$PULSE_SOCK" "$pp" pipewire-pulse) || { say "$r"; exit 1; } alive "$wp" || { say "wireplumber exited: $(tail -n 1 "$LOG/wireplumber.log" 2>/dev/null)"; exit 1; } say "pipewire $pw, wireplumber $wp, pipewire-pulse $pp; $PULSE_SOCK up in $(( $(uptime_s) - t0 ))s"