Pass argument directory to i3lock (#79)

New "--" argument to pass arguments directly to i3lock
String optimizations
This commit is contained in:
Mike Mob 2018-11-04 01:22:53 -04:00 committed by Pavan Jadhaw
parent 533d729a75
commit 5007d00536

View file

@ -6,7 +6,8 @@
# find your resolution so images can be resized to match your screen resolution # find your resolution so images can be resized to match your screen resolution
res=$(xdpyinfo | grep dimensions | sed -r 's/^[^0-9]*([0-9]+x[0-9]+).*$/\1/') res=$(xdpyinfo | grep dimensions | sed -r 's/^[^0-9]*([0-9]+x[0-9]+).*$/\1/')
locktext="Type password to unlock..." locktext='Type password to unlock...'
init_filenames() { init_filenames() {
#$1 resolution #$1 resolution
@ -14,11 +15,10 @@ init_filenames() {
# create folder in ~/.cache/i3lock directory # create folder in ~/.cache/i3lock directory
res_folder="$HOME/.cache/i3lock/$1" res_folder="$HOME/.cache/i3lock/$1"
folder="$HOME/.cache/i3lock/current" folder="$HOME/.cache/i3lock/current"
echo "Got" $@ $res_folder echo "Got" $@ $res_folder
if [ ! -d $folder -o -n "$2" ] if [ ! -d $folder -o -n "$2" ]; then
then rm -f $folder
rm $folder ln -s $res_folder $folder
ln -s $res_folder $folder
fi fi
# ratio for rectangle to be drawn for time background on lockscreen # ratio for rectangle to be drawn for time background on lockscreen
@ -44,12 +44,14 @@ init_filenames() {
init_filenames $res init_filenames $res
prelock() { prelock() {
if [ ! -z "$(pidof dunst)" ] ; then if [ ! -z "$(pidof dunst)" ]; then
pkill -u "$USER" -USR1 dunst pkill -u "$USER" -USR1 dunst
fi fi
} }
lock() { lock() {
#$1 image path #$1 image path
letterEnteredColor=d23c3dff letterEnteredColor=d23c3dff
@ -60,24 +62,26 @@ lock() {
foreground=ffffffff foreground=ffffffff
i3lock \ i3lock \
-t -i "$1" \ -t -i "$1" \
--timepos="x+110:h-70" \ --timepos='x+110:h-70' \
--datepos="x+43:h-45" \ --datepos='x+43:h-45' \
--clock --date-align 1 --datestr "$locktext" \ --clock --date-align 1 --datestr "$locktext" \
--insidecolor=$background --ringcolor=$foreground --line-uses-inside \ --insidecolor=$background --ringcolor=$foreground --line-uses-inside \
--keyhlcolor=$letterEnteredColor --bshlcolor=$letterRemovedColor --separatorcolor=$background \ --keyhlcolor=$letterEnteredColor --bshlcolor=$letterRemovedColor --separatorcolor=$background \
--insidevercolor=$passwordCorrect --insidewrongcolor=$passwordIncorrect \ --insidevercolor=$passwordCorrect --insidewrongcolor=$passwordIncorrect \
--ringvercolor=$foreground --ringwrongcolor=$foreground --indpos="x+280:h-70" \ --ringvercolor=$foreground --ringwrongcolor=$foreground --indpos='x+280:h-70' \
--radius=20 --ring-width=4 --veriftext="" --wrongtext="" \ --radius=20 --ring-width=4 --veriftext='' --wrongtext='' \
--verifcolor="$foreground" --timecolor="$foreground" --datecolor="$foreground" \ --verifcolor="$foreground" --timecolor="$foreground" --datecolor="$foreground" \
--noinputtext="" --force-clock $lockargs --noinputtext='' --force-clock $lockargs
} }
postlock() { postlock() {
if [ ! -z "$(pidof dunst)" ] ; then if [ ! -z "$(pidof dunst)" ] ; then
pkill -u "$USER" -USR2 dunst pkill -u "$USER" -USR2 dunst
fi fi
} }
rec_get_random() { rec_get_random() {
dir="$1" dir="$1"
if [ ! -d "$dir" ]; then if [ ! -d "$dir" ]; then
@ -89,295 +93,301 @@ rec_get_random() {
rec_get_random "$dir" rec_get_random "$dir"
} }
lockselect() {
case "$1" in
"")
# default lockscreen
prelock
lock "$l_resized"
postlock
;;
lockselect() {
prelock
case "$1" in
dim) dim)
# lockscreen with dimmed background # lockscreen with dimmed background
prelock
lock "$l_dim" lock "$l_dim"
postlock
;; ;;
blur) blur)
# set lockscreen with blurred background # set lockscreen with blurred background
prelock
lock "$l_blur" lock "$l_blur"
postlock
;; ;;
dimblur) dimblur)
# set lockscreen with dimmed + blurred background # set lockscreen with dimmed + blurred background
prelock
lock "$l_dimblur" lock "$l_dimblur"
postlock ;;
*)
# default lockscreen
lock "$l_resized"
;;
esac
postlock
}
update() {
# use
background="$1"
# default blur level; fallback to 1
[[ $blur_level ]] || blur_level=1
rectangles=" "
SR=$(xrandr --query | grep ' connected' | grep -o '[0-9][0-9]*x[0-9][0-9]*[^ ]*')
for RES in $SR; do
SRA=(${RES//[x+]/ })
CX=$((${SRA[2]} + 25))
CY=$((${SRA[1]} - 30))
rectangles+="rectangle $CX,$CY $((CX+300)),$((CY-80)) "
done
# User supplied Image
user_image="$folder/user_image.png"
# create folder
if [ ! -d $folder ]; then
echo "Creating '$folder' directory to cache processed images."
mkdir -p "$folder"
fi
# get random file in dir if passed argument is a dir
rec_get_random "$background"
# get user image
cp "$user_input" "$user_image"
if [ ! -f $user_image ]; then
echo 'Please specify the path to the image you would like to use'
exit 1
fi
# replace orignal with user image
cp "$user_image" "$orig_wall"
rm "$user_image"
echo 'Generating alternate images based on the image you specified,'
echo 'please wait this might take few seconds...'
# wallpapers
echo
echo 'Converting provided image to match your resolution...'
# resize image
convert "$orig_wall" -resize "$res""^" -gravity center -extent "$res" "$resized"
echo
echo 'Applying dim and blur effect to resized image'
# dim
convert "$resized" -fill black -colorize 40% "$dim"
# blur
blur_shrink=$(echo "scale=2; 20 / $blur_level" | bc)
blur_sigma=$(echo "scale=2; 0.6 * $blur_level" | bc)
convert "$resized" \
-filter Gaussian \
-resize "$blur_shrink%" \
-define "filter:sigma=$blur_sigma" \
-resize "$res^" -gravity center -extent "$res" \
"$blur"
# dimblur
convert "$dim" \
-filter Gaussian \
-resize "$blur_shrink%" \
-define "filter:sigma=$blur_sigma" \
-resize "$res^" -gravity center -extent "$res" \
"$dimblur"
# lockscreen backgrounds
echo
echo 'Caching images for faster screen locking'
# resized
convert "$resized" -draw "fill rgba(0, 0, 0, 0.4) $rectangles" "$l_resized"
# dim
convert "$dim" -draw "fill rgba(0, 0, 0, 0.4) $rectangles" "$l_dim"
# blur
convert "$blur" -draw "fill rgba(0, 0, 0, 0.4) $rectangles" "$l_blur"
# blur
convert "$dimblur" -draw "fill rgba(0, 0, 0, 0.4) $rectangles" "$l_dimblur"
echo
echo 'All required changes have been applied'
}
wallpaper() {
case "$1" in
'')
# set resized image as wallpaper if no argument is supplied by user
feh --bg-fill $resized
;;
dim)
# set dimmed image as wallpaper
feh --bg-fill $dim
;;
blur)
# set blurred image as wallpaper
feh --bg-fill $blur
;;
dimblur)
# set dimmed + blurred image as wallpaper
feh --bg-fill $dimblur
;; ;;
esac esac
} }
usage() {
echo "Important: Update the image cache (e.g. betterlockscreen -u path/to/image.jpg)." empty() {
echo " Image cache must be updated to initially configure or update wallpaper used" if [ -f $l_dim ]; then
echo -e "\nSeems you haven't provided any arguments. See below for usage details."
else
echo 'Important: Update the image cache (e.g. betterlockscreen -u path/to/image.jpg).'
echo echo
echo echo ' Image cache must be updated to initially configure or update the wallpaper used.'
echo "See: https://github.com/pavanjadhaw/betterlockscreen for additional info..." fi
echo
echo
echo "Options:"
echo
echo " -h --help"
echo " For help (e.g. betterlockscreen -h or betterlockscreen --help)." echo
echo echo 'For other sets of options and help, use the help command.'
echo echo 'e.g. betterlockscreen -h or betterlockscreen --help'
echo " -u --update" echo
echo " to update image cache, you should do this before using any other options" echo 'See: https://github.com/pavanjadhaw/betterlockscreen for additional info...'
echo " E.g: betterlockscreen -u path/to/image.png when image.png is custom background" exit 1
echo " Or you can use betterlockscreen -u path/to/imagedir and a random file will be selected." }
echo
echo
echo " -l --lock" usage() {
echo " to lock screen (e.g. betterlockscreen -l)" echo 'Important: Update the image cache (e.g. betterlockscreen -u path/to/image.jpg).'
echo " you can also use dimmed or blurred background for lockscreen." echo ' Image cache must be updated to initially configure or update wallpaper used'
echo " E.g: betterlockscreen -l dim (for dimmed background)" echo
echo " E.g: betterlockscreen -l blur (for blurred background)" echo
echo " E.g: betterlockscreen -l dimblur (for dimmed + blurred background)" echo 'See: https://github.com/pavanjadhaw/betterlockscreen for additional info...'
echo echo
echo echo
echo " -s --suspend" echo 'Options:'
echo " to suspend system and lock screen (e.g. betterlockscreen -s)" echo
echo " you can also use dimmed or blurred background for lockscreen." echo ' -h --help'
echo " E.g: betterlockscreen -s dim (for dimmed background)" echo ' For help (e.g. betterlockscreen -h or betterlockscreen --help).'
echo " E.g: betterlockscreen -s blur (for blurred background)" echo
echo " E.g: betterlockscreen -s dimblur (for dimmed + blurred background)" echo
echo echo ' -u --update'
echo echo ' to update image cache, you should do this before using any other options'
echo " -w --wall" echo ' E.g: betterlockscreen -u path/to/image.png when image.png is custom background'
echo " you can also set lockscreen background as wallpaper" echo ' Or you can use betterlockscreen -u path/to/imagedir and a random file will be selected.'
echo " to set wallpaper (e.g. betterlockscreen -w or betterlockscreen --wall)" echo
echo " you can also use dimmed or blurred variants." echo
echo " E.g: betterlockscreen -w dim (for dimmed wallpaper)" echo ' -l --lock'
echo " E.g: betterlockscreen -w blur (for blurred wallpaper)" echo ' to lock screen (e.g. betterlockscreen -l)'
echo " E.g: betterlockscreen -w dimblur (for dimmed + blurred wallpaper)" echo ' you can also use dimmed or blurred background for lockscreen.'
echo echo ' E.g: betterlockscreen -l dim (for dimmed background)'
echo echo ' E.g: betterlockscreen -l blur (for blurred background)'
echo " -r --resolution" echo ' E.g: betterlockscreen -l dimblur (for dimmed + blurred background)'
echo " to be used after -u" echo
echo " used to set a custom resolution for the image cache." echo
echo " E.g: betterlockscreen -u path/to/image.png -r 1920x1080" echo ' -s --suspend'
echo " E.g: betterlockscreen -u path/to/image.png --resolution 3840x1080" echo ' to suspend system and lock screen (e.g. betterlockscreen -s)'
echo echo ' you can also use dimmed or blurred background for lockscreen.'
echo echo ' E.g: betterlockscreen -s dim (for dimmed background)'
echo " -b --blur" echo ' E.g: betterlockscreen -s blur (for blurred background)'
echo " to be used after -u" echo ' E.g: betterlockscreen -s dimblur (for dimmed + blurred background)'
echo " used to set blur intensity. Default to 1." echo
echo " E.g: betterlockscreen -u path/to/image.png -b 3" echo
echo " E.g: betterlockscreen -u path/to/image.png --blur 0.5" echo ' -w --wall'
echo echo ' you can also set lockscreen background as wallpaper'
echo echo ' to set wallpaper (e.g. betterlockscreen -w or betterlockscreen --wall)'
echo " -t --text" echo ' you can also use dimmed or blurred variants.'
echo " to set custom lockscreen text (max 31 chars)" echo ' E.g: betterlockscreen -w dim (for dimmed wallpaper)'
echo " E.g: betterlockscreen -l dim -t \"Don't touch my machine!\"" echo ' E.g: betterlockscreen -w blur (for blurred wallpaper)'
echo " E.g: betterlockscreen --text \"Hi, user!\" -s blur" echo ' E.g: betterlockscreen -w dimblur (for dimmed + blurred wallpaper)'
echo
echo
echo ' -r --resolution'
echo ' to be used after -u'
echo ' used to set a custom resolution for the image cache.'
echo ' E.g: betterlockscreen -u path/to/image.png -r 1920x1080'
echo ' E.g: betterlockscreen -u path/to/image.png --resolution 3840x1080'
echo
echo
echo ' -b --blur'
echo ' to be used after -u'
echo ' used to set blur intensity. Default to 1.'
echo ' E.g: betterlockscreen -u path/to/image.png -b 3'
echo ' E.g: betterlockscreen -u path/to/image.png --blur 0.5'
echo
echo
echo ' -t --text'
echo ' to set custom lockscreen text (max 31 chars)'
echo " E.g: betterlockscreen -l dim -t \"Don't touch my machine!\""
echo ' E.g: betterlockscreen --text "Hi, user!" -s blur'
} }
# check if --text argument is given
if [ $# -gt 2 ] ; then
if [ "$1" == "-t" -o "$1" == "--text" ] ; then
locktext="$2"
# delete first 2 arguments and reset numbering ( 3 -> 1, 4 -> 2, ...)
set -- "${@:3}"
elif [ \( "$3" == "-t" -o "$3" == "--text" \) -a $# -gt 3 ] ; then
locktext="$4"
fi
fi
# Options # Options
case "$1" in [[ "$1" = '' ]] && empty
"")
if [ ! -f $l_dim ]; then
echo "Important: Update the image cache (e.g. betterlockscreen -u path/to/image.jpg)."
echo
echo " Image cache must be updated to initially configure or update wallpaper used"
echo
echo "See also: For other set of options and help use help command."
echo "E.g. betterlockscreen -h or betterlockscreen --help"
echo for arg in "$@"; do
echo "See: https://github.com/pavanjadhaw/betterlockscreen for addition info..." [[ "${arg:0:1}" = '-' ]] || continue
exit 1
else
echo
echo "Seems you havent provided any argument, see below for usage info"
echo
echo "See also: For other set of options and help use help command."
echo "E.g. betterlockscreen -h or betterlockscreen --help" case "$1" in
-h | --help)
usage
break
;;
echo -s | --suspend)
echo "See: https://github.com/pavanjadhaw/betterlockscreen for addition info..." runsuspend=true
echo ;&
exit 1
fi
;;
-h | --help) -l | --lock)
runlock=true
[[ $runsuspend ]] || lockargs="$lockargs -n"
[[ ${2:0:1} = '-' ]] && shift 1 || { lockstyle="$2"; shift 2; }
;;
usage -w | --wall | --wallpaper)
;; wallpaper "$2"
shift 2
;;
-l | --lock) -u | --update)
lockargs="-n" runupdate=true
lockselect "$2" imagepath="$2"
;; shift 2
;;
-s | --suspend) -t | --text)
lockselect "$2" locktext="$2"
systemctl suspend shift 2
;; ;;
-w | --wall) -r | --resolution)
case "$2" in res="$2"
"") init_filenames $res force
# set resized image as wallpaper if no argument is supplied by user shift 2
feh --bg-fill $resized ;;
;;
dim) -b | --blur)
# set dimmed image as wallpaper blur_level="$2"
feh --bg-fill $dim shift 2
;; ;;
blur) --)
# set blurred image as wallpaper lockargs="$lockargs ${@:2}"
feh --bg-fill $blur break
;; ;;
dimblur) *)
# set dimmed + blurred image as wallpaper echo "invalid argument: $1"
feh --bg-fill $dimblur break
;; ;;
esac esac
;; done
-u | --update) # Run image generation
background="$2" [[ $runupdate ]] && update "$imagepath"
shift 2
# default blur level # Activate lockscreen
blur_level=1 [[ $runlock ]] && lockselect "$lockstyle" && \
{ [[ $runsuspend ]] && systemctl suspend; }
# parse update arguments exit 0
while [ $# -gt 0 ]; do
case "$1" in
-r | --resolution )
res="$2"
init_filenames $res force
shift 2
;;
-b | --blur )
blur_level="$2"
shift 2
;;
*)
shift ;;
esac
done
rectangles=" "
SR=$(xrandr --query | grep ' connected' | grep -o '[0-9][0-9]*x[0-9][0-9]*[^ ]*')
for RES in $SR; do
SRA=(${RES//[x+]/ })
CX=$((${SRA[2]} + 25))
CY=$((${SRA[1]} - 30))
rectangles+="rectangle $CX,$CY $((CX+300)),$((CY-80)) "
done
# User supplied Image
user_image="$folder/user_image.png"
# create folder
if [ ! -d $folder ]; then
echo "Creating '$folder' directory to cache processed images."
mkdir -p "$folder"
fi
# get random file in dir if passed argument is a dir
rec_get_random "$background"
# get user image
cp "$user_input" "$user_image"
if [ ! -f $user_image ]; then
echo "Please specify the path to the image you would like to use"
exit 1
fi
# replace orignal with user image
cp "$user_image" "$orig_wall"
rm "$user_image"
echo "Generating alternate images based on the image you specified,"
echo "please wait this might take few seconds..."
# wallpapers
echo
echo "Converting provided image to match your resolution..."
# resize image
convert "$orig_wall" -resize "$res""^" -gravity center -extent "$res" "$resized"
echo
echo "Applying dim and blur effect to resized image"
# dim
convert "$resized" -fill black -colorize 40% "$dim"
# blur
blur_shrink=$(echo "scale=2; 20 / $blur_level" | bc)
blur_sigma=$(echo "scale=2; 0.6 * $blur_level" | bc)
convert "$resized" \
-filter Gaussian \
-resize "$blur_shrink%" \
-define "filter:sigma=$blur_sigma" \
-resize "$res^" -gravity center -extent "$res" \
"$blur"
# dimblur
convert "$dim" \
-filter Gaussian \
-resize "$blur_shrink%" \
-define "filter:sigma=$blur_sigma" \
-resize "$res^" -gravity center -extent "$res" \
"$dimblur"
# lockscreen backgrounds
echo
echo "Caching images for faster screen locking"
# resized
convert "$resized" -draw "fill rgba(0, 0, 0, 0.4) $rectangles" "$l_resized"
# dim
convert "$dim" -draw "fill rgba(0, 0, 0, 0.4) $rectangles" "$l_dim"
# blur
convert "$blur" -draw "fill rgba(0, 0, 0, 0.4) $rectangles" "$l_blur"
# blur
convert "$dimblur" -draw "fill rgba(0, 0, 0, 0.4) $rectangles" "$l_dimblur"
echo
echo "All required changes have been applied"
;;
*)
echo "invalid argument"
;;
esac
exit 0