dotfiles/dot_local/bin/executable_idle-launcher.tmpl

57 lines
1.8 KiB
Bash

#!/bin/sh
if test -n "$XDG_STATE_HOME"; then
log_dir=$XDG_STATE_HOME
else
log_dir=$HOME/.local/state
fi
log_file=$log_dir/idle.log
echo "--- idle-launcher $(date +"%Y-%m-%d %T") ---" > "$log_file"
# kill process if already running
user=$(id -u)
pid=$(ps -o pid,comm -u "$user" | awk '$2=="swayidle" {print $1}')
if [ "$pid" != "" ]; then
echo "$(date +"%Y-%m-%d %T") - idle-launcher: Sending SIGTERM" >> "$log_file"
kill "$pid"
sleep 1
pid=$(ps -o pid,comm -p "$pid" | awk '$2=="swayidle" {print $1}')
if [ "$pid" != "" ]; then
echo "$(date +"%Y-%m-%d %T") - idle-launcher: Sending SIGKILL" >> "$log_file"
kill -9 "$pid"
sleep 1
fi
fi
{{ if eq .chezmoi.hostname "helix" -}}
# dim: 2m, lock: 5m, display: 15m, suspend: 1h
brightness=/sys/class/backlight/amdgpu_bl1/brightness
prev_brightness=$log_dir/prev_brightness
swayidle -dw \
timeout 120 "cat $brightness > $prev_brightness && echo 0 > $brightness" \
resume "cat $prev_brightness > $brightness" \
timeout 300 'waylock-launcher' \
resume "cat $prev_brightness > $brightness" \
timeout 900 "cat $prev_brightness > $brightness && wlr-randr --output eDP-1 --off" \
resume "wlr-randr --output eDP-1 --on && kanshictl reload" \
timeout 3600 "loginctl suspend-then-hibernate" \
resume "wlr-randr --output eDP-1 --on && kanshictl reload" \
lock "waylock-launcher" \
before-sleep "waylock-launcher" \
2>&1 | tee -a "$log_file" >/dev/null &
{{- else if eq .chezmoi.hostname "hydrus" }}
# lock: 5m, display: 15m
swayidle -dw \
timeout 300 'waylock-launcher' \
timeout 900 'kanshictl switch dual-off' \
resume 'kanshictl switch dual' \
lock 'waylock-launcher' \
before-sleep 'waylock-launcher' \
2>&1 | tee -a "$log_file" >/dev/null &
{{- else }}
# lock after 5m
swayidle -dw \
timeout 300 'waylock-launcher' \
2>&1 | tee -a "$log_file" >/dev/null &
{{- end }}