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!
Bookmarks