2

The goal is to mute sound whenever screen lock happens.

Ideally restore sound after unlocking as well.

Sound system is Pulseaudio.

2

Launch this script once when starting your X graphical session:

#!/bin/bash -euET
{
set -o pipefail

export DBUS_SESSION_BUS_ADDRESS="${DBUS_SESSION_BUS_ADDRESS:-unix:path=/run/user/$(id -u)/bus}"
export DISPLAY="${DISPLAY:-:0}"

xscreensaver-command -watch | while read -r line ; do
    echo "handling event: $line"

    if [[ $line = LOCK* ]]; then
      volume=$(pamixer --get-volume)
      echo "current volume is $volume"
      pamixer --set-volume 0
    fi

    if [[ $line = UNBLANK* ]]; then
      echo "setting volume to $volume"
      pamixer --set-volume "$volume"
    fi
done

exit
}

The "daemon" part of this script xscreensaver-command -watch will hang and wait for any events from xscreensaver. Whenever "LOCK" or "UNBLANK" event occurs, the sound volume will be zeroed and restored respectively.

  • Note that pamixer will not be installed by default on many systems, while pacmd/pactrl will be. – dirkt Sep 8 '18 at 13:25
0

I have found that the simplest way is a shell script which is run on startup. This should work whatever screen-saver is in use (if any).

#!/bin/bash

gdbus monitor -y -d org.freedesktop.login1 | grep LockedHint --line-buffered |
    while read line
    do
        case "$line" in
            *"<true>"*)
                amixer -q -D pulse sset Master off
            ;;
            *"<false>"*)
                amixer -q -D pulse sset Master on
            ;;
        esac
    done
  • Strange, this doesn't work for me. I do get the "Session" and "Seat" events when I switch between tty2<->tty7. But I do not get anything on lock-unlock. I'm running the following command while doing the tests: gdbus monitor -y -d org.freedesktop.login1 – VasyaNovikov Sep 23 '18 at 12:50
  • That's odd, @VasyaNovikov. I get a slew of messages when I log out. Are you running Gnome3 or the Ubuntu login? – Cliff Stanford Sep 23 '18 at 14:10
  • I'm running XFCE + xscreensaver daemon. – VasyaNovikov Sep 23 '18 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.