aboutsummaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rwxr-xr-xbin/backlight99
1 files changed, 99 insertions, 0 deletions
diff --git a/bin/backlight b/bin/backlight
new file mode 100755
index 0000000..ac8a466
--- /dev/null
+++ b/bin/backlight
@@ -0,0 +1,99 @@
+#!/usr/bin/env bash
+# set backlight through sysfs
+
+set -euo pipefail
+
+# brightness steps in percentage
+brightness_step_size=5
+brightness_device=/sys/class/backlight/intel_backlight/brightness
+brightness_max_device=/sys/class/backlight/intel_backlight/max_brightness
+calculated_brightness=0
+amount=0
+
+brightness_steps=$(echo "100/${brightness_step_size}" |bc -l)
+maximum_brightness_raw=$(cat $brightness_max_device)
+current_brightness_raw=$(cat $brightness_device)
+brightness_one_percent_raw=$(echo "${maximum_brightness_raw}/100" |bc -l)
+current_brightness_percentage=$(echo "${current_brightness_raw}/(${maximum_brightness_raw}/100)" |bc -l)
+current_brightness_remainder=$(echo "${current_brightness_percentage}%${brightness_step_size}"|bc -l)
+current_step=$(echo ${current_brightness_percentage}/${brightness_step_size}|bc -l)
+
+check_if_number() {
+ if [[ $1 =~ ^[!\-0-9]+$ ]]; then
+ echo "Not an Integer: $1"
+ exit 1
+ fi
+}
+
+calculate_increment() {
+ check_if_number $1
+ if [ $1 -eq 0 ]; then
+ echo "There's nothing to do."
+ exit 1
+ fi
+ if [ $1 -gt 0 ]; then
+ if [ $(printf '%.0f' $current_brightness_percentage) -eq 100 ]; then
+ echo "Already at 100%"
+ exit 0
+ elif [ $(printf '%.0f' $(echo "${current_brightness_percentage}+$1"| bc)) -gt 100 ]; then
+ calculated_brightness=100
+ else
+ calculated_brightness=$(printf '%.0f' $(echo "${current_brightness_percentage}+$1"| bc))
+ fi
+ else
+ if [ $(printf '%.0f' $current_brightness_percentage) -eq 0 ]; then
+ echo "Already at 0%"
+ exit 0
+ elif [ $(printf '%.0f' $(echo "${current_brightness_percentage}+$1"| bc)) -lt 0 ]; then
+ calculated_brightness=0
+ else
+ calculated_brightness=$(printf '%.0f' $(echo "${current_brightness_percentage}+$1"| bc))
+ fi
+ fi
+}
+
+calculate_percentage() {
+ check_if_number $1
+ if [ $1 -lt 0 ] || [ $1 -gt 100 ]; then
+ echo "Invalid range: $1"
+ exit 1
+ fi
+ calculated_brightness=$1
+}
+
+percentage_to_raw() {
+ printf '%.0f' $(echo "$1*${brightness_one_percent_raw}"|bc -l)
+}
+
+set_brightness() {
+ echo "$(percentage_to_raw $calculated_brightness)" > $brightness_device
+}
+
+print_help() {
+ echo -e "Usage:\n $0 -d <decrement_by_x_percent> \n or $0 -i <increment_by_x_percent>"
+ exit 1
+}
+
+if [ ${#@} -gt 0 ]; then
+ while getopts 'hi:s:' flag; do
+ case "${flag}" in
+ h)
+ print_help
+ ;;
+ i)
+ calculate_increment $OPTARG
+ ;;
+ s)
+ calculate_percentage $OPTARG
+ ;;
+ *)
+ echo "Error! Try '${0} -h'."
+ exit 1
+ ;;
+ esac
+ done
+else
+ print_help
+fi
+
+set_brightness