Skip to content
Snippets Groups Projects

Mute speakers while GNOME is locked

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Anton Sarukhanov

    Mute your sound output when locking the screen, and un-mute when unlocking.

    • Tested with GNOME 3.30 on Debian 10.
    • Watches org.freedesktop.login1 for LockedHint events via gdbus monitor.
    • Mutes when locking, un-mutes when unlocking.
    • Does not un-mute if mute was enabled prior to locking the screen.
    • Uses amixer (ALSA) to mute/unmute.

    Installation

    1. Save to /usr/local/bin/lockmute.sh.
    2. Make it executable: chmod +x /usr/local/bin/lockmute.sh.

    Test it

    You can test it by running lockmute.sh in a terminal. Play some music, and lock the screen (Super+L).

    Run it automatically

    To run this anytime you're logged in, create an autostart entry.

    ~/.config/autostart/lockmute.desktop

    [Desktop Entry]
    Name=lockmute
    Exec=/usr/local/bin/lockmute.sh
    Type=Application
    NoDisplay=true
    X-GNOME-Autostart-enabled=true
    Edited
    lockmute.sh 869 B
    #!/bin/bash
    
    # lockmute.sh - Mute sound output while GNOME is locked.
    
    STATE_FILE=~/.lockmute_state
    gdbus monitor -y -d org.freedesktop.login1 |
        while read -r DBUS_OUTPUT ; do
            LOCK_STATE=$(echo "$DBUS_OUTPUT" | grep -o "'LockedHint': <\w*>" | awk '{print $2}')
            if [[ "$LOCK_STATE" == "<true>" ]]; then
                AUDIO_STATE=$(amixer get Master | grep -o '\[\(on\|off\)\]')
                if [[ "$AUDIO_STATE" == *"[off]"* ]]; then
                    echo "WAS_MUTED" > "$STATE_FILE"
                else
                    echo "WAS_NOT_MUTED" > "$STATE_FILE"
                fi
                amixer set Master mute > /dev/null
            elif [[ "$LOCK_STATE" == "<false>" ]]; then
                if [[ "$(cat "$STATE_FILE")" == "WAS_NOT_MUTED" ]]; then
                    amixer set Master unmute > /dev/null
                fi
                rm "$STATE_FILE"
            fi
        done
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment