Page 3 of 6 FirstFirst 12345 ... LastLast
Results 21 to 30 of 51

Thread: Mute and Lock Screen with one command

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

    Re: Mute and Lock Screen with one command

    You should test if your dpms works in normal mode at all. Perhaps it's disabled or something? Type
    Code:
    xset dpms q
    into a terminal and check what's written after DPMS (Energy Star):
    Also, try to enable dpms screen turnoff without my script, using
    Code:
    xset dpms 0 0 10
    Then wait for 10 seconds without typing or moving the mouse. The screen should turn off. If not, maybe dpms is not working for you? The script can be modified to turn off the screen anyway without dpms in that case.
    Vermind

  2. #22
    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
    You should test if your dpms works in normal mode at all. Perhaps it's disabled or something? Type
    Code:
    xset dpms q
    into a terminal and check what's written after DPMS (Energy Star):
    Also, try to enable dpms screen turnoff without my script, using
    Code:
    xset dpms 0 0 10
    Then wait for 10 seconds without typing or moving the mouse. The screen should turn off. If not, maybe dpms is not working for you? The script can be modified to turn off the screen anyway without dpms in that case.
    Happy New Year! - It's already Jan 1 2010 5:20 am here!

    Anyway back to the matter at hand.

    Code:
    xset dpms q
    I get the following output:
    Code:
    $ xset dpms q
    Keyboard Control:
      auto repeat:  on    key click percent:  0    LED mask:  00000002
      auto repeat delay:  500    repeat rate:  30
      auto repeating keys:  00ffffffdffffbbf
                            fadfffefffedffff
                            9fffffffffffffff
                            fff7ffffffffffff
      bell percent:  0    bell pitch:  400    bell duration:  100
    Pointer Control:
      acceleration:  2/1    threshold:  4
    Screen Saver:
      prefer blanking:  yes    allow exposures:  yes
      timeout:  0    cycle:  0
    Colors:
      default colormap:  0x20    BlackPixel:  0    WhitePixel:  16777215
    Font Path:
      /usr/share/fonts/X11/misc,/usr/share/fonts/X11/100dpi/:unscaled,/usr/share/fonts/X11/75dpi/:unscaled,/usr/share/fonts/X11/Type1,/usr/share/fonts/X11/100dpi,/usr/share/fonts/X11/75dpi,/var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType,built-ins
    DPMS (Energy Star):
      Standby: 0    Suspend: 0    Off: 0
      DPMS is Enabled
      Monitor is On
    So, I guess there is no problem with that

    Code:
    xset dpms 0 0 10
    This seems to turn my monitor off every 10 seconds. So, I will not move my mouse and EVERY 10 seconds my monitor turns off.

    I am currently having two problems using the script:

    1. The monitor turns off when I lock the screen but after 10 seconds the monitor turns back on.

    2. If I move the mouse or hit a key on the keyboard the unlock box shows up and if I don't type anything the monitor still stays on.

    Any thoughts?

    Thanks

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

    Re: Mute and Lock Screen with one command

    Change this in the script:
    Code:
    # set specified DPMS settings:
    xset dpms ${standby} ${suspend} ${off}
    to
    Code:
    # set specified DPMS settings:
    echo "xset dpms ${standby} ${suspend} ${off}"
    xset dpms ${standby} ${suspend} ${off}
    then run it in the console and see what it is setting for dpms.
    Vermind

  4. #24
    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
    Change this in the script:
    Code:
    # set specified DPMS settings:
    xset dpms ${standby} ${suspend} ${off}
    to
    Code:
    # set specified DPMS settings:
    echo "xset dpms ${standby} ${suspend} ${off}"
    xset dpms ${standby} ${suspend} ${off}
    then run it in the console and see what it is setting for dpms.
    Ran it in the console and here is the output:

    Code:
    ./mute-and-lock.sh 
    xset dpms 0 0 10
    The screen switches off, but, after about 10s the screen turns back on and stays on.

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

    Re: Mute and Lock Screen with one command

    Ok, it seems that gnome-screensaver really really wants to show you a pretty blanked screen or some other screensaver. It is turning the screen back on in order to do that. In my opinion, I should really be allowed to choose just locking and not touching the screen after that. Anyway, I was able to hack around it using gnome-screensaver-command -i, which prevents the screensaver from activating. It also blocks, so I have to run it in a separate process. When I run it after locking the screen, the screen will stay locked and dpms will correctly keep the screen turned off like intended, and the 10-second turnoff also functions properly.

    Code:
    #!/bin/bash
    ########################################
    # mute-and-lock.sh                     #
    # Mutes sound and locks the screen     #
    # using amixer and gnome-screensaver.  #
    # Version 1.01                         #
    # -Added DPMS screen turnoff           #
    # -Prevent gnome-screensaver from      #
    #  turning the screen back on          #
    #                                      #
    # 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)
    gnome-screensaver-command -i -a "mute-and-lock" & # Stop screensaver from waking up the screen
    unlockcheck # wait for unlock
    # stop inhibiting screensaver
    kill $( ps -ef | awk '$0 ~ "gnome-screensaver-command -i" && $NF == "mute-and-lock" { print $2; }' )
    fade up # fade volume up and unmute
    xset dpms ${dpms} # restore dpms settings
    # That's it!
    Cheers,
    Vermind

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

    Re: Mute and Lock Screen with one command

    Thanks the update fixed the problem.

    The only thing is when I move my mouse and the box shows up to unlock BUT I don't unlock and click cancel the monitor does not switch of and the screensaver is shown.

    Any thoughts on that?

  7. #27
    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 me, the screensaver also shows in that case; also if I press esc on the unlock screen. However, in several seconds, the screen goes off. If it doesn't for you, I can make this script force it off every 10 seconds with the initial command.
    Vermind

  8. #28
    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,
    Added two things:
    1. It is now possible to force the screen off between $off intervals. Uncomment forceoff in Settings to do this.
    2. The script now ensures a smooth dimdown before turning the screen off by giving the lock + volume fade at least 3 seconds of time.

    Code:
    #!/bin/bash
    ########################################
    # mute-and-lock.sh                     #
    # Mutes sound and locks the screen     #
    # using amixer and gnome-screensaver.  #
    # Version 1.02                         #
    # -Added DPMS screen turnoff           #
    # -Prevent gnome-screensaver from      #
    #  turning the screen back on          #
    # -Workaround for DPMS timing          #
    # -Give screen time to dim down        #
    #                                      #
    # 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
    # Workarounds:
    #forceoff=1  # uncomment to have the screen forced off between the dpms off intervals.
    
    
    
    ########################################
    # 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
    }
    
    function forceoffcheck {
        foacc=$( echo "${lockdelay} +  ${foacc}" | bc ) # lockdelay elapsed
        elapsed=$( echo "${foacc} >= ${off}" | bc ) # off elapsed yet?
        if [ "${elapsed}" == "1" ]; then 
            xset dpms force off;
            foacc=0;
        fi
    }
    
    # Returns when the screen has been locked and unlocked
    function unlockcheck {
        foacc=0; # forceoff time accumulator
        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
            if [ "${forceoff}" == "1" ]; then
                echo "forceoffcheck, forceoff=$forceoff"
                forceoffcheck
            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}
    locktime=$( date +%s )
    gnome-screensaver-command -l # lock screen
    fade down # fade volume down and mute
    let locktime=$( date +%s )-${locktime};
    echo "faded down for ${locktime} seconds";
    if [ ${locktime} -lt 3 ]; then
        let locktime=3-${locktime}
        echo "giving screen fade ${locktime} seconds more time";
        sleep ${locktime};
    fi
    xset dpms force off # turn off the screen (directly after lock results in too quick turnoff)
    gnome-screensaver-command -i -a "mute-and-lock" & # Stop screensaver from waking up the screen
    unlockcheck # wait for unlock
    # stop inhibiting screensaver
    kill $( ps -ef | awk '$0 ~ "gnome-screensaver-command -i" && $NF == "mute-and-lock" { print $2; }' ) 2>/dev/null
    fade up # fade volume up and unmute
    xset dpms ${dpms} # restore dpms settings
    # That's it!
    Last edited by Vermind; January 2nd, 2010 at 03:54 PM. Reason: Fixed a bug with forceoff being always activated
    Vermind

  9. #29
    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 checked and realised that you were right after a while the screen does turn off automatically.

    Thanks again

    EDIT: Annoying little thing (NB: REALLY little thing)....basically when the screen/volume dim-down for a couple of seconds the screensaver shows up. Right now I have set the screensaver to "blank screen", but aesthetically it isn't great. Like I said small thing

  10. #30
    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, try to adjust the step, interval, and dimtime variables.
    If the toning down of your sound is taking longer than the dim,
    increase step or decrease interval. Increased step may mean that you hear the grades when the sound is going down, decreased interval just makes more steps happen in the same time. A lower dimtime adds less time to the clock for dimming, in case volume fade is fast. Try 2 or 1 second.
    Also, for those who do not wish to turn down the volume, but want to turn off the screen, change the value of mute from 1 to 0 (mute=0). Sorry I forgot about that use case. Also, now you can specify nomute as a parameter to the script, so you can have two shortcuts: one with the command
    Code:
    /path/to/mute-and-lock.sh
    and the other with
    Code:
    /path/to/mute-and-lock.sh nomute
    , where the second one does not touch your volume but does turn off and keep off the screen.

    Code:
    #!/bin/bash
    ################################################################################
    # mute-and-lock.sh                                                             #
    # Mutes sound and locks the screen using amixer and gnome screensaver.         #
    # Version 1.03                                                                 #
    #                                                                              #
    # -Added DPMS screen turnoff                                                   #
    # -Prevent gnome-screensaver from turning the screen back on                   #
    # -Workaround for DPMS timing                                                  #
    # -Give screen time to dim (dimtime)                                           #
    # -Allow no muting (nomute parameter and mute setting)                         #
    #                                                                              #
    # written by Vermind                                                           #
    ################################################################################
    
    ########################################
    # Settings                             #
    ########################################
    
    mute=1            # Mute the sound while locked. 0=do not mute. 
                      # Note: nomute as parameter overrides this setting.
    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
    dimtime=3         # whole numbers only. The time given for the initial screen dim.
    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
    # Workarounds:
    #forceoff=1        # uncomment to have the screen forced off between the dpms off intervals.
    
    
    ########################################
    # 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
    }
    
    function forceoffcheck {
        foacc=$( echo "${lockdelay} +  ${foacc}" | bc ) # lockdelay elapsed
        elapsed=$( echo "${foacc} >= ${off}" | bc ) # off elapsed yet?
        if [ "${elapsed}" == "1" ]; then 
            xset dpms force off;
            foacc=0;
        fi
    }
    
    # Returns when the screen has been locked and unlocked
    function unlockcheck {
        foacc=0; # forceoff time accumulator
        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
            if [ "${forceoff}" == "1" ]; then
                echo "forceoffcheck, forceoff=$forceoff"
                forceoffcheck
            fi
        done
    }
    
    function muteandlock {
        locktime=$( date +%s )
        gnome-screensaver-command -l # lock screen
        if [ "${mute}" == "1" ]; then
            fade down # fade volume down and mute
        fi
        if [ ${dimtime} -gt 0 ]; then
            let locktime=$( date +%s )-${locktime};
            echo "faded down for ${locktime} seconds";
            if [ ${locktime} -lt ${dimtime} ]; then
                let locktime=${dimtime}-${locktime}
                echo "giving screen fade ${locktime} seconds more time";
                sleep ${locktime};
            fi
        fi
        xset dpms force off # turn off the screen (directly after lock results in too quick turnoff)
    }
    
    ########################################
    # Startup                              #
    ########################################
    
    if [ -n "${1}" -a "${1}" == "nomute" ]; then
        mute=0;
    fi
    
    if [ "${mute}" == "1" ]; then
        # short mixer command with card
        m="amixer -c ${card}"
        # record original volume value
        getvol
        orig=$vol
    fi
    
    # 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}
    muteandlock # Initial mute and lock of screen
    gnome-screensaver-command -i -a "mute-and-lock" & # Stop screensaver from waking up the screen
    unlockcheck # wait for unlock
    # stop inhibiting screensaver
    kill $( ps -ef | awk '$0 ~ "gnome-screensaver-command -i" && $NF == "mute-and-lock" { print $2; }' ) 2>/dev/null
    if [ "${mute}" == "1" ]; then
        fade up # fade volume up and unmute
    fi
    xset dpms ${dpms} # restore dpms settings
    # That's it!
    Vermind

Page 3 of 6 FirstFirst 12345 ... 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
  •