summaryrefslogtreecommitdiff
path: root/.local
diff options
context:
space:
mode:
authorsinanmohd <sinan@firemail.cc>2023-06-24 21:10:11 +0530
committersinanmohd <sinan@firemail.cc>2023-06-24 21:56:22 +0530
commit6f9e094ceb48f74e37bcd8f222d8887a602b8af5 (patch)
treeb2c799bfb4daf4debaa696ffbae43c4518a003c2 /.local
parent31cae6809c7fb9274f3812ef5d340683de345532 (diff)
caffeine: keep your computer awake
Diffstat (limited to '.local')
-rwxr-xr-x.local/bin/caffeine118
1 files changed, 118 insertions, 0 deletions
diff --git a/.local/bin/caffeine b/.local/bin/caffeine
new file mode 100755
index 0000000..10b26e0
--- /dev/null
+++ b/.local/bin/caffeine
@@ -0,0 +1,118 @@
+#!/bin/sh
+
+cfpid="${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}}/caffeine.pid"
+
+usage()
+{
+ cat <<- EOF
+ Usage: ${0##*/} [ -s ] [ -t timeout ]
+ Keep your computer awake
+
+ Options:
+ -s silent or quiet mode
+ -t <timeout in minutes>
+
+ EOF
+}
+
+note()
+{
+ command -v notify-send > /dev/null &&
+ notify-send " caffeine" "$1"
+}
+
+die()
+{
+ note "err: $1"
+ printf "\033[31;1merr: %b\033[0m\n" "$1" 1>&2
+ exit "${2:-1}"
+}
+
+dep_check()
+{
+ : "${1:?}"
+
+ for dep; do
+ command -v "$dep" > /dev/null ||
+ die "${dep} not installed"
+ done
+
+ unset dep
+}
+
+set_menu()
+{
+ if [ -n "$WAYLAND_DISPLAY" ]; then
+ menu="wmenu"
+ elif [ -n "$DISPLAY" ]; then
+ menu="dmenu"
+ else
+ die "can't be ran from tty, setup xorg or wayland"
+ fi
+}
+
+caf_time()
+{
+ dep_check "$menu"
+
+ printf "1\n3\n5\n10\n15\n30" | "$menu" -p " sleep after "
+}
+
+drink()
+{
+ # usage: drink <time in mins>
+ : "${1:?}"
+ disp=
+
+ dep_check swayidle wlr-randr brightnessctl swaylock
+
+ [ -s "$cfpid" ] &&
+ kill "$(cat "$cfpid")"
+
+ disp="$(wlr-randr | grep -om1 '^[^ ]*')"
+ swayidle \
+ timeout "$(($1 - 5))" "brightnessctl --save; brightnessctl set 10%-" \
+ resume "brightnessctl --restore" \
+ timeout "$1" "wlr-randr --output ${disp} --off; swaylock" \
+ resume "wlr-randr --output ${disp} --on --adaptive-sync enabled" &
+ echo $! > "$cfpid"
+
+ [ "$silent" != true ] &&
+ note "$(uname -n) will sleep after $(($1 / 60)) m of inactivity"
+}
+
+########
+# MAIN #
+########
+
+timeout=
+silent=
+menu=
+
+set_menu
+
+while getopts "t:sh" f; do
+ case "$f" in
+ t)
+ timeout="$OPTARG"
+ ;;
+ s)
+ silent=true
+ ;;
+ h)
+ usage
+ exit
+ ;;
+ ?)
+ printf "try '%s -h' for more information\n" "${0##*/}"
+ exit 1
+ ;;
+ esac
+done
+
+[ -z "$timeout" ] &&
+ timeout="$(caf_time)"
+[ "$((timeout))" -le 0 ] &&
+ die "invalid input time"
+
+drink "$((timeout * 60))"