diff --git a/README.md b/README.md index 32fe988..5ab5fd1 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,6 @@ ### TODO - Make rofi power menu work by removing password requirement from shutdown, reboot, etc., which is normally done with `visudo` (see [this Stack Overflow thread](https://askubuntu.com/a/168885) and [Nix `sudo` option](https://search.nixos.org/options?channel=22.05&from=0&size=50&sort=relevance&type=packages&query=sudo). -- Package safe poweroff bash script required by rofi power menu - Theme GRUB - Find out how handling multiple hosts works so both desktop and ThinkPad configurations are buildable from flake - Load in vim configuration diff --git a/rofi/power.nix b/rofi/power.nix index b9882b5..fff5928 100644 --- a/rofi/power.nix +++ b/rofi/power.nix @@ -3,7 +3,7 @@ substituteAll { name = "rofi-power"; src = ./power.sh; - + inherit = wmctrl; dir = "bin"; isExecutable = true; } diff --git a/rofi/power.sh b/rofi/power.sh index 8bde0ef..9b3af73 100755 --- a/rofi/power.sh +++ b/rofi/power.sh @@ -21,13 +21,47 @@ if [[ $sure != $confirm ]]; then fi if [[ $chosen != $SUSPEND ]]; then - sh /home/elnu/scripts/graceful-shutdown/graceful_shutdown.sh + # based on https://github.com/ardadem/graceful-shutdown + + TIMEOUT=10 + + APPLIST_TO_TERMINATE=(Discord firefox) + + close_windows () { + wmctrl -l | awk '{print $1}' | while read -r wId + do + wmctrl -i -c $wId + done + } + + window_timeout_countdown () { + for i in $(seq 1 $TIMEOUT); + do + if [[ -z $(wmctrl -l) ]]; then + return 0 + fi + sleep 1 + done + exit 1 + } + + terminate_processes () { + pidof ${APPLIST_TO_TERMINATE[@]} | tr ' ' '\n' | while read -r pId + do + kill -15 $pId + tail --pid=$pId -f /dev/null # wait until terminate it + done + } + + close_windows + window_timeout_countdown + terminate_processes fi case "$chosen" in - $POWER_OFF) sudo poweroff ;; - $REBOOT) sudo reboot ;; - $SUSPEND) sudo systemctl suspend ;; + $POWER_OFF) poweroff ;; + $REBOOT) reboot ;; + $SUSPEND) systemctl suspend ;; $LOG_OUT) i3-msg exit ;; *) exit 1 ;; esac