Files
I38/scripts/screenlock.sh

101 lines
3.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# This file is part of I38.
# I38 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
# I38 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with I38. If not, see <https://www.gnu.org/licenses/>.
screenlockPinHash=""
scriptPath="$(readlink -f "$0")"
scriptDir="${scriptPath%/*}"
i3Path="${scriptDir%/scripts}"
pinFile="${i3Path}/.screenpin"
if [[ -f "$pinFile" ]]; then
read -r screenlockPinHash < "$pinFile"
fi
if [[ -z "$screenlockPinHash" ]]; then
yad --title "I38" --text "Screen lock is not configured. Run i38.sh and set a 4-digit PIN to enable it." --button "Close:0"
exit 1
fi
if ! command -v yad &> /dev/null; then
exit 1
fi
if ! command -v jq &> /dev/null; then
yad --title "I38" --text "Screen lock requires jq to determine the current workspace." --button "Close:0"
exit 1
fi
if ! command -v sha512sum &> /dev/null; then
yad --title "I38" --text "Screen lock requires sha512sum to validate the PIN." --button "Close:0"
exit 1
fi
wmMsg="i3-msg"
if [[ -n "${SWAYSOCK:-}" ]] && command -v swaymsg &> /dev/null; then
wmMsg="swaymsg"
elif [[ -n "${I3SOCK:-}" ]] && command -v i3-msg &> /dev/null; then
wmMsg="i3-msg"
elif command -v swaymsg &> /dev/null; then
wmMsg="swaymsg"
elif command -v i3-msg &> /dev/null; then
wmMsg="i3-msg"
else
yad --title "I38" --text "No i3 or sway command interface was found for screen lock." --button "Close:0"
exit 1
fi
currentWorkspace="$($wmMsg -t get_workspaces | jq -r '.[] | select(.focused==true) | .name')"
lockWorkspace="i38-lock"
if $wmMsg -t get_workspaces | jq -e --arg name "$lockWorkspace" '.[] | select(.name==$name)' &> /dev/null; then
lockWorkspace="i38-lock-$$"
fi
$wmMsg -t command "workspace --no-auto-back-and-forth \"$lockWorkspace\"" &> /dev/null
$wmMsg -t command "mode screenlock" &> /dev/null
attemptCount=0
while : ; do
if [[ $attemptCount -eq 0 ]]; then
promptText="Screen lock is enabled. Enter your 4-digit PIN to unlock."
else
promptText="Incorrect PIN. Enter your 4-digit PIN to unlock."
fi
pinInput="$(yad --entry --hide-text --title "I38" --text "$promptText" --entry-label "Screen lock PIN" --button "Unlock:0" --on-top --sticky --skip-taskbar --fixed --center --undecorated --fullscreen --no-escape)"
yadResult=$?
if [[ $yadResult -ne 0 ]]; then
attemptCount=$((attemptCount + 1))
continue
fi
if [[ ! "$pinInput" =~ ^[0-9]{4}$ ]]; then
attemptCount=$((attemptCount + 1))
continue
fi
pinHash="$(printf "%s" "$pinInput" | sha512sum | awk '{print $1}')"
unset pinInput
if [[ "$pinHash" == "$screenlockPinHash" ]]; then
break
fi
attemptCount=$((attemptCount + 1))
done
$wmMsg -t command "mode default" &> /dev/null
if [[ -n "$currentWorkspace" ]]; then
$wmMsg -t command "workspace \"$currentWorkspace\"" &> /dev/null
fi
exit 0