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 (or setup autostart of this script on login):

#!/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
}

Explanation: 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.

| improve this answer | |
  • 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
| improve this answer | |
  • 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
0

I used pactl instead of amixer as suggested by nik-gnomic

pactl set-sink-mute @DEFAULT_SINK@ toggle

at Mute all sound when locking device

Here's the entire code for people interested. Its a slight deviation from cliff-stanford's code essentially

Open a terminal window, replace 'user' in the path with your username as applicable

nano /home/'user'/.local/share/applications/MuteLock.sh

then paste the below in the window and hit Ctrl+o (for save) and Ctrl+x (to exit)

#!/bin/bash
# Credit for code:
   #https://unix.stackexchange.com/users/14353/cliff-stanford
   #https://unix.stackexchange.com/users/231160/nik-gnomic
# Post on stackexchange: https://unix.stackexchange.com/questions/467456/how-to-mute-sound-when-xscreensaver-locks-screen/589614#589614

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
                pactl set-sink-mute @DEFAULT_SINK@ toggle
            ;;
            *"<false>"*)
                #amixer -q -D pulse sset Master on
                pactl set-sink-mute @DEFAULT_SINK@ toggle
            ;;
        esac
    done
exit

Make the file executable, replace 'user' in the path with your username as applicable

chmod +x /home/'user'/.local/share/applications/MuteLock.sh

and then execute and do Super+L (lock screen) to check if its working

/home/'user'/.local/share/applications/MuteLock.sh &

Tested under Manjaro 20.01 Gnome 3.36 edition - working on wayland. - works without xscreensaver installed

To start this application in the background on gnome login created a *.desktop file. I followed maxschlepzig's post at Gnome Shell: Running shell script after session starts? for the *.desktop file code

Open a terminal again, replace 'user' in the path with your username as applicable

nano /home/'user'/.local/share/applications/MuteLock.desktop

and paste the following in there, Ctrl+o (save) and Ctrl+x (exit)

[Desktop Entry]
Encoding=UTF-8
Type=Application
Icon=application-x-executable
Name=MuteLock
Exec=/home/'user'/.local/share/applications/MuteLock.sh
Path=/home/'user'/.local/share
Terminal=false
Hidden=false
StartupNotify=false
X-GNOME-Autostart-enabled=true
Comment=Mute on screen-lock and un-mute on screen un-lock
Categories=Settings; System; Utility

In Gnome tweaks under starup applications add the MuteLock.desktop file you just created.

I can confirm that this trick works on all my three linux distros that are natively installed on my Dell Inspiron 7390 2-in-1 laptop

  • Manjaro 20.0.1 kernel v5.6,

  • Ubuntu 20.04 LTS kernel v5.4 LTS,

  • OpenSUSE Tumbleweed kernel v5.6, and

  • with Gnome 3.36, GDM, Xorg, and Wayland

If you have the libplayerctl installed then we can even add the pause and play to the code. Install libplayerctl (libplayerctl2), playerctl from your distro's package manager and modify as follows

case "$line" in
                *"<true>"*)
                    playerctl -a pause #pauses all MPRIS players
                    #amixer -q -D pulse sset Master off
                    pactl set-sink-mute @DEFAULT_SINK@ toggle
                ;;
                *"<false>"*)
                    #amixer -q -D pulse sset Master on
                    pactl set-sink-mute @DEFAULT_SINK@ toggle
                    playerctl play #start playing the default player
                ;;
            esac
| improve this answer | |

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.