From a3699d7e0ae13b3b42aecf390527b207931ced1d Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sun, 1 Mar 2026 00:21:10 +0800 Subject: hyprshutdown: cleanup --- hyprshutdown.sh | 54 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 17 deletions(-) (limited to 'hyprshutdown.sh') diff --git a/hyprshutdown.sh b/hyprshutdown.sh index 77c69d0..3daf7ee 100755 --- a/hyprshutdown.sh +++ b/hyprshutdown.sh @@ -1,36 +1,56 @@ #!/usr/bin/bash WAIT=10 # in seconds -self=$$ -cancel="Cancel" -confirm="Confirm" -DUP_ACTION=kill # or shutdown -DUP_AMOUNT=3 # this only matters if its shutdown +SELF=$$ +CANCEL="Cancel" +CONFIRM="Confirm" +DUP_ACTION=reboot # or shutdown or reboot +DUP_AMOUNT=2 # this only matters if its shutdown or reboot +# maybe add hyprsession --save or whatever thats called to restore prev apps SHUTDOWN="hyprshutdown --post-cmd poweroff" +REBOOT="hyprshutdown --post-cmd reboot" +# default +ACTION=Shutdown other_pids=$(pidof -x -o $$ "$(basename "$0")") dup_count=$(echo "$other_pids" | wc -w) +# kill dialog RAII +trap 'kill -- -$$' EXIT + +function exec_action { + if [[ $ACTION == "Shutdown" ]]; then + $SHUTDOWN &! + elif [[ $ACTION == "Reboot" ]]; then + $REBOOT &! + fi + kill $SELF +} + +function dialog { + ans=$(timeout 1.2 hyprland-dialog --apptitle hyprshutdown --title "$ACTION in $1" --buttons "$CANCEL;$CONFIRM") + if [[ $ans == $CONFIRM ]]; then + exec_action + elif [[ $ans == $CANCEL ]]; then + kill $SELF + fi +} + if [[ $DUP_ACTION == "kill" ]]; then if [ $dup_count -gt 0 ]; then kill $other_pids fi elif [[ $DUP_ACTION == "shutdown" ]]; then - if [ $dup_count -ge DUP_AMOUNT ]; then - $SHUTDOWN &! + if [ $dup_count -ge $DUP_AMOUNT ]; then kill $other_pids + exec_action fi -fi - -function dialog { - ans=$(timeout 1.2 hyprland-dialog --apptitle hyprshutdown --title "Shutting down in $1" --buttons "$cancel;$confirm") - if [[ $ans == $confirm ]]; then - $SHUTDOWN &! - kill $self - elif [[ $ans == $cancel ]]; then - kill $self +elif [[ $DUP_ACTION == "reboot" ]]; then + if [ $dup_count -ge $DUP_AMOUNT ]; then + kill $other_pids + ACTION=Reboot fi -} +fi for ((i=WAIT; i>=0; i--)); do dialog $i & -- cgit v1.2.3