4

The goal is to mute sound whenever screen lock happens.

Ideally restore sound after unlocking as well.

Sound system is Pulseaudio.

5 Answers 5

3

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.

1
  • Note that pamixer will not be installed by default on many systems, while pacmd/pactrl will be.
    – dirkt
    Commented Sep 8, 2018 at 13:25
3

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
1

On Xubuntu with XFCE the solutions didn't work for me, because org.freedesktop.login1 doesn't seem to get any events. (@VasyaNovikov seems to have the same issue.)

What did work for me was to look for org.xfce.ScreenSaver events.

#!/bin/bash
gdbus  monitor  --session  --dest org.xfce.ScreenSaver  |  grep "ActiveChanged"  --line-buffered  |
    while  read  line
    do
        case  "$line"  in
            *"(true,"*)
                pactl  set-sink-mute  @DEFAULT_SINK@  on
            ;;
            *"(false,"*)
                pactl  set-sink-mute  @DEFAULT_SINK@  off
            ;;
        esac
    done
exit
1

Don't know if it can be useful but I've adapted the script to mute the microphone. I find it very useful when you have open conversations (eg. on Discord) and you forget to mute the mic when you're away from your PC.

#!/bin/bash

gdbus monitor -y -d org.freedesktop.login1 |
  grep --line-buffered LockedHint |
  while read -r line; do
  case "$line" in
    *"<true>"*)
      pactl set-source-mute 0 1
      ;;
    *"<false>"*)
      # pactl set-source-mute 0 0
      ;;
  esac
  done
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
3
  • 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 Commented Sep 23, 2018 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? Commented Sep 23, 2018 at 14:10
  • I'm running XFCE + xscreensaver daemon. Commented Sep 23, 2018 at 18:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .