diff --git a/contrib/virtual-desktop/uu-virtual-desktop b/contrib/virtual-desktop/uu-virtual-desktop index 1e3e69f..d162303 100755 --- a/contrib/virtual-desktop/uu-virtual-desktop +++ b/contrib/virtual-desktop/uu-virtual-desktop @@ -26,7 +26,15 @@ case "$display" in :[0-9]*) ;; *) echo "UU_VIRTUAL_DISPLAY must be a local displ number="${display#:}" case "$number" in *[!0-9]*|'') echo "invalid display number" >&2; exit 1 ;; esac socket="/tmp/.X11-unix/X$number" -[ ! -e "$socket" ] || { echo "display already in use" >&2; exit 1; } +# A live server owns the display; a dead one's leftovers (killed Xvfb) are cleaned up by Xvfb itself. +lock="/tmp/.X$number-lock" +if [ -e "$socket" ] || [ -e "$lock" ]; then + owner=$(tr -d ' \n' < "$lock" 2>/dev/null || :) + case "$owner" in ''|*[!0-9]*) owner= ;; esac + if [ -z "$owner" ] || kill -0 "$owner" 2>/dev/null; then echo "display already in use" >&2; exit 1; fi + echo "removing stale display $display left by dead process $owner" >&2 + rm -f "$lock" "$socket" 2>/dev/null || { echo "cannot remove stale display files" >&2; exit 1; } +fi umask 077 export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}" authority="$XDG_RUNTIME_DIR/uu-virtual-$number.Xauthority" diff --git a/src/virtual_audio.rs b/src/virtual_audio.rs index e6fa7b0..f597deb 100644 --- a/src/virtual_audio.rs +++ b/src/virtual_audio.rs @@ -49,7 +49,11 @@ fn persist(route: &Route) -> Result<(), ()> { } fn recover(route: &Route) -> Result<(), ()> { // Do not overwrite a default explicitly changed by the user during remote control. - if pactl(&["get-default-sink"])? == SINK { pactl(&["set-default-sink",&route.previous])?; } + // The previous output may be gone (unplugged USB card): still unload our sink so the server + // picks another default instead of leaving every stream on a null sink. + if pactl(&["get-default-sink"]).is_ok_and(|s| s == SINK) && pactl(&["set-default-sink",&route.previous]).is_err() { + log::warn!("Previous default audio output unavailable; the sound server will choose a new default"); + } move_inputs(SINK,&route.previous); let modules=owned_modules(&pactl(&["list","short","modules"])?); for id in modules { pactl(&["unload-module",&id.to_string()])?; }