From 0ec90646ba76159f1c5d46e6f8201191bacdfea2 Mon Sep 17 00:00:00 2001
From: Salt <rehashedsalt@cock.li>
Date: Sun, 4 Nov 2018 02:29:16 -0600
Subject: [PATCH] i3lock-custom: Major refactor Polish the imagemagick pipeline
 Be less error-tolerant in subshell Make program flow easier to read And open
 the gates to having a bar on top of it all

---
 i3lock-custom | 59 ++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 40 insertions(+), 19 deletions(-)

diff --git a/i3lock-custom b/i3lock-custom
index 699d6e8..2f16e08 100755
--- a/i3lock-custom
+++ b/i3lock-custom
@@ -6,10 +6,11 @@
 # Distributed under terms of the MIT license.
 #
 
-tmpdir="$(mktemp -d /tmp/i3lock-custom-XXXXXXXX)"
+tmpdir="$(mktemp -d "/tmp/i3lock-custom-$(whoami)-XXXXXXXX")"
 [ -z ${tmpdir+x} ] && exit 2
 cleanup() {
 	rm -rf "$tmpdir"
+	[ -z "$i3lockpid" ] || kill "$i3lockpid"
 }
 trap "cleanup" EXIT
 # Papirus Dark's object-locked.svg, rendererd, gzipped and base64'd
@@ -49,25 +50,45 @@ klg/Tyn9KCMwN3XMDWhrx7stDd1Q4nnRBSHUZn1vrKVXpOqvTAwcKBPunsekzX3q6Z0/Udt6hnii
 EOF
 }
 
-(
-cd "$tmpdir"
-# Take a screenshot
-import -window root stage1.bmp
+step_produce_image() {
+	(
+	# Really wanna make damn sure that we handle this well
+	set +e
+	umask 077
+	cd "$tmpdir"
+	# Grab a screenshot and perform some calculations to make our black bar
+	import -window root scrot.bmp
+	lockimg_gz | base64 -d | gunzip > lock.png
+	rectangle="rectangle $(convert scrot.bmp -print "0,%[fx:h/2+128] %w,%[fx:h/2-128]" /dev/null)"
 
-# Blur out the important bits
-convert stage1.bmp -blur 10x5 stage2.bmp
+	# Do the do
+	# Output is in PNG so i3lock doesn't OOM
+	convert \
+		scrot.bmp \
+		-blur 10x5 \
+		-fill \#282828 \
+		-draw "$rectangle" \
+		lock.png -gravity center -compose Exclusion -composite \
+		end.png
+	)
+	return $?
+}
+step_start_i3lock() {
+	i3lock -i "$tmpdir/end.png" &
+	i3lockpid="$!"
+}
+step_wait() {
+	wait
+	exit 0
+}
 
-# Put a grey bar over it
-rectangle="rectangle $(convert stage2.bmp -print "0,%[fx:h/2+128] %w,%[fx:h/2-128]" /dev/null)"
-convert stage2.bmp -fill \#282828 -draw "$rectangle" stage3.bmp
+main() {
+	step_produce_image || exit 1
+	step_start_i3lock
+	step_wait
+	wait
+	exit 0
+}
 
-# Stick a lock icon on there
-lockimg_gz | base64 -d | gunzip > lock.png
-convert -compose Exclusion -composite -gravity center stage3.bmp lock.png stage4.bmp
-
-# Convert the output to PNG so i3lock doesn't OOM
-convert stage4.bmp end.png
-)
-
-i3lock -i "$tmpdir/end.png"
+main $@