summaryrefslogtreecommitdiff
path: root/.local/bin
diff options
context:
space:
mode:
Diffstat (limited to '.local/bin')
-rwxr-xr-x.local/bin/backlightctl83
1 files changed, 83 insertions, 0 deletions
diff --git a/.local/bin/backlightctl b/.local/bin/backlightctl
new file mode 100755
index 0000000..d3f6280
--- /dev/null
+++ b/.local/bin/backlightctl
@@ -0,0 +1,83 @@
+#!/bin/sh
+
+# Get the correct sub-directory for your backlight device
+root_path="/sys/class/backlight/"
+device="$(find "$root_path" | tail -n 1)"
+
+max="$(cat "$device/max_brightness")"
+
+# What percentage to increase/decrease by when no argument is given
+default_step=10
+# What percentage to set to when no argument is given
+default_set=50
+
+to_percent() {
+ printf "%d" "$((100 * $1 / $max))"
+}
+
+to_value() {
+ printf "%d" "$((max * $1 / 100))"
+}
+
+get_status() {
+ cat "$device/brightness"
+}
+
+get_percent() {
+ to_percent "$(get_status)"
+}
+
+# Add $1 to current brightness. Adjust if out of bounds (0-$max)
+# $1 is converted to a value between 0-$max within this function
+get_total() {
+ value="$(to_value "$1")"
+ printf "%d" "$(($(get_status) + value))"
+}
+
+helpmsg="Usage: backlightctl -[FLAG] [PERCENTAGE]
+ -i, --increase: Increase brightness by n (default 10%)
+ -d, --decrease: Decrease brightness by n (default 10%)
+ -s, --set: Set brightness to specific value (default 50%)
+ -g --get: Get current brightness
+ -q, --quiet: Don't send any notification (for use in dwmblocks)
+ -h, --help: Print this help message
+"
+
+case "$1" in
+
+-i | --increase)
+ [ -z "$2" ] && step="$default_step" || step="$2"
+ get_total "$step" >"$device/brightness"
+ ;;
+
+-d | --decrease)
+ [ -z "$2" ] && step="$default_step" || step="$2"
+ get_total "-$step" >"$device/brightness"
+ ;;
+
+-s | --set)
+ [ -z "$2" ] && set="$default_set" || set="$2"
+ to_value "$set" >"$device/brightness"
+ ;;
+
+-g | --get)
+ printf "󰃠 %s\n" "$(get_status)%"
+ ;;
+
+-h | --help)
+ printf "%s" "$helpmsg"
+ ;;
+*)
+ printf "\033[31mInvalid option: $1\033[0m\n%s" "$helpmsg" && exit 1
+ ;;
+esac
+
+# Send notification
+case "$@" in
+*-q* | *--quiet*)
+ exit 0
+ ;;
+*)
+ notify-send.sh -t 1000 --replace-file /tmp/bl-notif -a "󰃠 Brightness:" "$(get_percent)%"
+ ;;
+esac