Page 2 of 6 FirstFirst 1234 ... LastLast
Results 11 to 20 of 51

Thread: Mute and Lock Screen with one command

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

    Re: Mute and Lock Screen with one command

    Hello
    here is a version that unmutes faster, at the cost of more CPU when locked. Introduced lockdelay, the delay between checks for unlocking. Smaller values unmute faster after unlock. The CPU cost for 0.5 secs between checks is modest, but for very small values of lockdelay may become considerable.

    Someone said that line 21 was buggy; I tested this version in console and normally, and it should work. If you get bugs, make sure you copy the code verbatim and your editor does not introduce extra line breaks.

    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
    lockdelay=0.5     # How long wait between unlock checks. Smaller values unmute faster
    # but use more CPU used when locked.
    
    ########################################
    # 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}- >/dev/null
                let vol-=${step}
            done
            ${m} set ${channel} mute >/dev/null
       else
           # first unmute, then fade up
            ${m} set ${channel} unmute >/dev/null
             while [ ${orig} -gt ${vol} ]; do
                sleep ${interval}
                let vol+=${step}
                if [ ${vol} -gt ${orig} ]; then vol=${orig}; fi
                ${m} set ${channel} ${step}+ >/dev/null
            done
            ${m} set ${channel} ${orig} >/dev/null
       fi
    }
    
    # Returns when the screen has been locked and unlocked
    function unlockcheck {
        haslock=
        while true; do
            sleep ${lockdelay}
            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!
    Last edited by Vermind; December 27th, 2009 at 12:13 PM. Reason: Note about line 21.
    Vermind

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

    Re: Mute and Lock Screen with one command

    Thanks for the update. I didn't realise it would cost CPU cycles. I usually keep the computer locked at night but with high CPU cost the noise would become a problem.

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

    Re: Mute and Lock Screen with one command

    I am still getting errors while running it:

    Code:
    ./mute-and-lock.sh: line 23: [: too many arguments
    ./mute-and-lock.sh: line 32: [: too many arguments
    amixer: Invalid command!

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

    Re: Mute and Lock Screen with one command

    please do this:
    Code:
    amixer > amixer-controls.txt
    and post the resulting file. I will check if your amixer has different controls and you need to change some variables.
    Vermind

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

    Re: Mute and Lock Screen with one command

    Thanks for the help

    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 25 [83%] [-9.00dB] [on]
      Front Right: Playback 25 [83%] [-9.00dB] [on]
    Simple mixer control 'PCM',0
      Capabilities: pvolume pswitch
      Playback channels: Front Left - Front Right
      Limits: Playback 0 - 255
      Mono:
      Front Left: Playback 254 [100%] [0.20dB] [on]
      Front Right: Playback 254 [100%] [0.20dB] [on]
    Simple mixer control 'Mic',0
      Capabilities: pvolume pswitch
      Playback channels: Front Left - Front Right
      Limits: Playback 0 - 30
      Mono:
      Front Left: Playback 0 [0%] [-34.50dB] [off]
      Front Right: Playback 0 [0%] [-34.50dB] [off]
    Simple mixer control 'Mic Boost',0
      Capabilities: volume
      Playback channels: Front Left - Front Right
      Capture channels: Front Left - Front Right
      Limits: 0 - 3
      Front Left: 0 [0%]
      Front Right: 0 [0%]
    Simple mixer control 'IEC958',0
      Capabilities: pswitch pswitch-joined
      Playback channels: Mono
      Mono: Playback [off]
    Simple mixer control 'IEC958 Default PCM',0
      Capabilities: pswitch pswitch-joined
      Playback channels: Mono
      Mono: Playback [on]
    Simple mixer control 'Capture',0
      Capabilities: cvolume cswitch
      Capture channels: Front Left - Front Right
      Limits: Capture 0 - 14
      Front Left: Capture 0 [0%] [0.00dB] [on]
      Front Right: Capture 0 [0%] [0.00dB] [on]
    Simple mixer control 'Digital',0
      Capabilities: cvolume
      Capture channels: Front Left - Front Right
      Limits: Capture 0 - 120
      Front Left: Capture 60 [50%] [0.00dB]
      Front Right: Capture 60 [50%] [0.00dB]

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

    Re: Mute and Lock Screen with one command

    Hello, Fixed the script for people with stereo sound cards. It now detects the volume properly.

    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
    lockdelay=0.5     # How long wait between unlock checks. Smaller values unmute faster
    # but use more CPU used when locked.
    
    ########################################
    # Functions                            #
    ########################################
    
    # get current volume
    function getvol {
        vol=$( ${m} sget ${channel} | awk '
    $0 ~ "%" { 
        if ($4 ~ "%") {
            print $3;
        } else if ($5 ~ "%") { 
        print $4;
        exit 0;
        }
    }' )
    }
    
    # fades volume up or down
    function fade {
        # get current volume
        getvol
        if [ "$1" == "down" ]; then
            # fade down:
            while [ ${vol} -gt 0 ]; do
                sleep ${interval}
                ${m} set ${channel} ${step}- >/dev/null
                let vol-=${step}
            done
            ${m} set ${channel} mute >/dev/null
       else
           # first unmute, then fade up
            ${m} set ${channel} unmute >/dev/null
             while [ ${orig} -gt ${vol} ]; do
                sleep ${interval}
                let vol+=${step}
                if [ ${vol} -gt ${orig} ]; then vol=${orig}; fi
                ${m} set ${channel} ${step}+ >/dev/null
            done
            ${m} set ${channel} ${orig} >/dev/null
       fi
    }
    
    # Returns when the screen has been locked and unlocked
    function unlockcheck {
        haslock=
        while true; do
            sleep ${lockdelay}
            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
    getvol
    orig=$vol
    
    ########################################
    # 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!
    Vermind

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

    Re: Mute and Lock Screen with one command

    Works perfectly...no errors!

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

    Re: Mute and Lock Screen with one command

    I was wondering if there was a way to turn of my monitor when the screen is locked?

    The thread here seems to suggest a way, but where should I put it in the script?

    Thanks

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

    Re: Mute and Lock Screen with one command

    Hello,
    here is a new version that turns off the screen.
    The screen is turned off after muting sound, so that it happens around the time that the gnome-screensaver has smoothly turned down the brightness. Also, during the lock period, accidental keystrokes or mouse moves will not leave your screen on until relock, but rather DPMS is used to turn off the screen with 10 seconds of inactivity. The script also restores your pre-lock dpms settings after it is done. Tweak the settings to your liking.

    Just in case something goes wrong, say xset 0 0 0 in a terminal to put dpms on infinite again. The first number is standby, then suspend, then off.


    Code:
    #!/bin/bash
    ########################################
    # mute-and-lock.sh                     #
    # Mutes sound and locks the screen     #
    # using amixer and gnome-screensaver.  #
    # Version 1.0                          #
    # -Added DPMS screen turnoff           #
    #                                      #
    # written by Vermind                   #
    ########################################
    
    ########################################
    # 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
    lockdelay=0.5     # How long wait between unlock checks. Smaller values unmute faster
    # but use more CPU when locked.
    # DPMS settings to use when locked, 0==infinity:
    standby=0 # how many seconds of inactivity to wait before screen standby
    suspend=0 # before screen suspend
    off=10 # before turning the screen off
    
    
    ########################################
    # Functions                            #
    ########################################
    
    # get current volume
    function getvol {
        vol=$( ${m} sget ${channel} | awk '
    $0 ~ "%" { 
        if ($4 ~ "%") {
            print $3;
        } else if ($5 ~ "%") { 
        print $4;
        exit 0;
        }
    }' )
    }
    
    # fades volume up or down
    function fade {
        # get current volume
        getvol
        if [ "$1" == "down" ]; then
            # fade down:
            while [ ${vol} -gt 0 ]; do
                sleep ${interval}
                ${m} set ${channel} ${step}- >/dev/null
                let vol-=${step}
            done
            ${m} set ${channel} mute >/dev/null
       else
           # first unmute, then fade up
            ${m} set ${channel} unmute >/dev/null
             while [ ${orig} -gt ${vol} ]; do
                sleep ${interval}
                let vol+=${step}
                if [ ${vol} -gt ${orig} ]; then vol=${orig}; fi
                ${m} set ${channel} ${step}+ >/dev/null
            done
            ${m} set ${channel} ${orig} >/dev/null
       fi
    }
    
    # Returns when the screen has been locked and unlocked
    function unlockcheck {
        screenlockacc=0;
        haslock=
        while true; do
            sleep ${lockdelay}
            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
    getvol
    orig=$vol
    
    # record original DPMS settings.
    dpms=$( xset dpms q | awk '$0 ~ "Suspend" && $0 ~ "Standby" && $0 ~ "Off" { print $2, $4, $6; }' )
    
    ########################################
    # Procedure                            #
    ########################################
    
    # set specified DPMS settings:
    xset dpms ${standby} ${suspend} ${off}
    gnome-screensaver-command -l # lock screen
    fade down # fade volume down and mute
    xset dpms force off # turn off the screen (directly after lock results in too quick turnoff)
    unlockcheck # wait for unlock
    fade up # fade volume up and unmute
    xset dpms ${dpms} # restore dpms settings
    # That's it!
    Last edited by Vermind; December 31st, 2009 at 01:10 AM. Reason: just in case ...
    Vermind

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

    Re: Mute and Lock Screen with one command

    Works great. But, if I move the mouse or keyboard (while in lock mode) the panel shows up where I can put in my password to unlock. Then if I don't put my password in the computer locks again but the screen does not go to sleep.

    Does that make sense? Slight hiccup

Page 2 of 6 FirstFirst 1234 ... 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
  •