Page 1 of 6 123 ... LastLast
Results 1 to 10 of 51

Thread: Mute and Lock Screen with one command

  1. #1
    Join Date
    Apr 2006
    Location
    Coventry
    Beans
    1,379
    Distro
    Ubuntu 9.10 Karmic Koala

    Mute and Lock Screen with one command

    I usually press CTRL+ALT+L to lock the screen. However, I would like to know if I could set it up so that when I lock the screen my speakers also get muted. I sometimes forget to mute the speakers when I lock my screen and step away from my computer and if someone calls me on skype the ringtone is really loud and disturbs others.

  2. #2
    Join Date
    Mar 2007
    Location
    Helsinki, Finland, Earth
    Beans
    234
    Distro
    Ubuntu 18.04 Bionic Beaver

    Re: Mute and Lock Screen with one command

    For this, you need to create a script with the two commands in it.
    Code:
    gnome-screensaver-command -l
    locks the screen.
    I am not sure about the mute command. If you have not changed the karmic default pulseaudio install, you can get a list of sound sinks with
    Code:
    list-sink-inputs
    and mute them with
    Code:
    set-sink-input-mute
    . I am more familiar with alsa though. For alsa, the mute command is
    Code:
    amixer set Master mute
    .

    To create the script, you can for example
    Code:
    gedit mute-and-lock.sh
    and then insert this text into the file:
    Code:
    #!/bin/bash
    # for pulse:
    set-sink-input-mute xx 1 # xx is what you get from list-sink-inputs. Should be master or something. Not sure about this
    # Lock the screen:
    gnome-screensaver-command -l
    Then save and quit gedit. After this, right-click the file you created and choose properties, and on the permissions tab "allow executing as program". Now you can try the script, by double clicking on it and choosing run or run in terminal. If it locks and mutes, congrats!

    Next we make a shortcut for it. In the System menu, Preferences, there is Keyboard Shortcuts. Click the Add button there. The name can be for example "mute and lock" and the command should be the file you created; you can drag it to the command text box or write
    Code:
    /home/youruser/mute-and-lock.sh
    into it. Press ok, and find the new command in the shortcut list. It will be in the last Custom Shortcuts category. Click the right side to assign a key combo for it. Then press whatever combo you want to assign, for example ctrl-alt-L.
    Vermind

  3. #3
    Join Date
    Apr 2006
    Location
    Coventry
    Beans
    1,379
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Mute and Lock Screen with one command

    Thanks your solution was PERFECT!

    Incidentally, in Karmic (with default sound settings) the command to mute is:
    Code:
    amixer set Master mute
    So, my script was:
    Code:
    #!/bin/bash
    amixer set Master mute
    gnome-screensaver-command -l
    Thanks again!

  4. #4
    Join Date
    May 2009
    Location
    Fareham, UK
    Beans
    1,524
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Mute and Lock Screen with one command

    what about when you unlock, this wont unmute will it. i guess thats not so bad,
    Catch me on Freenode - imark

  5. #5
    Join Date
    Apr 2006
    Location
    Coventry
    Beans
    1,379
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Mute and Lock Screen with one command

    The problem with unmuting is that when you unlock you unlock with ANY button. Therefore, it is hard to bind a particular key to unmute.

  6. #6
    Join Date
    Mar 2007
    Location
    Helsinki, Finland, Earth
    Beans
    234
    Distro
    Ubuntu 18.04 Bionic Beaver

    Re: Mute and Lock Screen with one command

    For automatic unmute, have the script changed to this:

    Code:
    #!/bin/bash
    amixer set Master mute
    gnome-screensaver-command -l
    haslock=;
    while true; do
        sleep 2;
        locked=$( gnome-screensaver-command -q | grep " active" );
        if [ -n "$haslock" ]; then
            # lock has happened before. Check unlock and break if unlocked
            if [ -z "$locked" ]; then break; fi
        fi
        if [ -n "$locked" ]; then
            haslock="true";
        fi
    done
    # we only exit the loop after an unlock, so
    # now unmute:
    amixer set Master unmute

    Run this in the console first to see if it works for you. I just tested it on this machine, looks good.
    Cheers,
    Vermind

  7. #7
    Join Date
    Apr 2006
    Location
    Coventry
    Beans
    1,379
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Mute and Lock Screen with one command

    That's really cool! Thanks a lot.

  8. #8
    Join Date
    Mar 2007
    Location
    Helsinki, Finland, Earth
    Beans
    234
    Distro
    Ubuntu 18.04 Bionic Beaver

    Re: Mute and Lock Screen with one command

    Hi,
    nobody likes sharp edges, so I thought up a smooth fade of volume into the mute and lock script. It now remembers your pre-lock volume and fades it down to zero prior to muting. When unlocking, it fades the volume up again. I also made it modular and less platform-dependent by providing some settings for choosing the sound card and channel.

    Code:
    #!/bin/bash
    ########################################
    # Settings                             #
    ########################################
    
    card=0;           # Sound card. 0 == default, first card.
    channel="Master"; # Which volume to control. Examples: Master, PCM, Headphone
    step=3;           # Fade step: The volume increment/decrement amount
    interval=0.2      # Seconds between increments/decrements when fading
    
    ########################################
    # Functions                            #
    ########################################
    
    # fades volume up or down
    function fade {
        # get current volume
        vol=$( ${m} sget ${channel} | awk '$0 ~ "%" { print $3 }' )
        if [ "$1" == "down" ]; then
            # fade down:
            while [ ${vol} -gt 0 ]; do
                sleep ${interval}
                ${m} set ${channel} ${step}-
                let vol-=${step}
            done
            ${m} set ${channel} mute
       else
           # first unmute, then fade up
            ${m} set ${channel} unmute
             while [ ${orig} -gt ${vol} ]; do
                sleep ${interval}
                let vol+=${step}
                if [ ${vol} -gt ${orig} ]; then vol=${orig}; fi
                ${m} set ${channel} ${step}+
            done
            ${m} set ${channel} ${orig}
       fi
    }
    
    # Returns when the screen has been locked and unlocked
    function unlockcheck {
        haslock=
        while true; do
            sleep 2
            locked=$( gnome-screensaver-command -q | grep " active" )
            if [ -n "${haslock}" ]; then
                # lock has happened before. Check unlock and break if unlocked
                if [ -z "${locked}" ]; then break; fi
            fi
            if [ -n "${locked}" ]; then
                haslock="true"
            fi
        done
    }
    
    ########################################
    # Startup                              #
    ########################################
    
    # short mixer command with card
    m="amixer -c ${card}";
    # record original volume value
    orig=$( ${m} sget ${channel} | awk '$0 ~ "%" { print $3 }' )
    
    ########################################
    # Procedure                            #
    ########################################
    
    gnome-screensaver-command -l # lock screen
    fade down # fade volume down and mute
    unlockcheck # wait for unlock
    fade up # fade volume up and unmute
    # That's it!
    Ok, I better do some work...
    Vermind

  9. #9
    Join Date
    Apr 2006
    Location
    Coventry
    Beans
    1,379
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Mute and Lock Screen with one command

    since we're tweaking like mad...

    The initial mute and lock is fine. However, when it comes out of the lock screen it takes a second for it to unmute. Can it unmute either at the same time as it unlocks or even before it unlocks. Presumably when you are there to unlock it you want it to mute. The one second wait makes everything feel slow.

    Thanks again!

  10. #10
    Join Date
    Apr 2006
    Location
    Coventry
    Beans
    1,379
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Mute and Lock Screen with one command

    Quote Originally Posted by Vermind View Post
    Hi,
    nobody likes sharp edges, so I thought up a smooth fade of volume into the mute and lock script. It now remembers your pre-lock volume and fades it down to zero prior to muting. When unlocking, it fades the volume up again. I also made it modular and less platform-dependent by providing some settings for choosing the sound card and channel.

    Code:
    #!/bin/bash
    ########################################
    # Settings                             #
    ########################################
    
    card=0;           # Sound card. 0 == default, first card.
    channel="Master"; # Which volume to control. Examples: Master, PCM, Headphone
    step=3;           # Fade step: The volume increment/decrement amount
    interval=0.2      # Seconds between increments/decrements when fading
    
    ########################################
    # Functions                            #
    ########################################
    
    # fades volume up or down
    function fade {
        # get current volume
        vol=$( ${m} sget ${channel} | awk '$0 ~ "%" { print $3 }' )
        if [ "$1" == "down" ]; then
            # fade down:
            while [ ${vol} -gt 0 ]; do
                sleep ${interval}
                ${m} set ${channel} ${step}-
                let vol-=${step}
            done
            ${m} set ${channel} mute
       else
           # first unmute, then fade up
            ${m} set ${channel} unmute
             while [ ${orig} -gt ${vol} ]; do
                sleep ${interval}
                let vol+=${step}
                if [ ${vol} -gt ${orig} ]; then vol=${orig}; fi
                ${m} set ${channel} ${step}+
            done
            ${m} set ${channel} ${orig}
       fi
    }
    
    # Returns when the screen has been locked and unlocked
    function unlockcheck {
        haslock=
        while true; do
            sleep 2
            locked=$( gnome-screensaver-command -q | grep " active" )
            if [ -n "${haslock}" ]; then
                # lock has happened before. Check unlock and break if unlocked
                if [ -z "${locked}" ]; then break; fi
            fi
            if [ -n "${locked}" ]; then
                haslock="true"
            fi
        done
    }
    
    ########################################
    # Startup                              #
    ########################################
    
    # short mixer command with card
    m="amixer -c ${card}";
    # record original volume value
    orig=$( ${m} sget ${channel} | awk '$0 ~ "%" { print $3 }' )
    
    ########################################
    # Procedure                            #
    ########################################
    
    gnome-screensaver-command -l # lock screen
    fade down # fade volume down and mute
    unlockcheck # wait for unlock
    fade up # fade volume up and unmute
    # That's it!
    Ok, I better do some work...
    Running this new script in the terminal gives me the following:

    Code:
    ./mute-and-lock.sh: line 21: [: too many arguments
    Simple mixer control 'Master',0
      Capabilities: pvolume pswitch pswitch-joined
      Playback channels: Front Left - Front Right
      Limits: Playback 0 - 30
      Mono:
      Front Left: Playback 24 [80%] [-10.50dB] [off]
      Front Right: Playback 24 [80%] [-10.50dB] [off]
    Simple mixer control 'Master',0
      Capabilities: pvolume pswitch pswitch-joined
      Playback channels: Front Left - Front Right
      Limits: Playback 0 - 30
      Mono:
      Front Left: Playback 24 [80%] [-10.50dB] [on]
      Front Right: Playback 24 [80%] [-10.50dB] [on]
    ./mute-and-lock.sh: line 30: [: too many arguments
    amixer: Invalid command!
    Seems to be a problem somewhere!

    Also (and I think this may be related to the error) after it unlocks the volume unmutes after about 1-2 seconds.

    The old script gives the following output:
    Code:
    Simple mixer control 'Master',0
      Capabilities: pvolume pswitch pswitch-joined
      Playback channels: Front Left - Front Right
      Limits: Playback 0 - 30
      Mono:
      Front Left: Playback 24 [80%] [-10.50dB] [off]
      Front Right: Playback 24 [80%] [-10.50dB] [off]
    Simple mixer control 'Master',0
      Capabilities: pvolume pswitch pswitch-joined
      Playback channels: Front Left - Front Right
      Limits: Playback 0 - 30
      Mono:
      Front Left: Playback 24 [80%] [-10.50dB] [on]
      Front Right: Playback 24 [80%] [-10.50dB] [on]
    But, even using this script there is a 1-2 second delay after unlock for the volume to unmute.

Page 1 of 6 123 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •