[Pkg-acpi-devel] Bug#849998: replace device-specific ACPI brightness script with a general script

Stephen Gildea stepheng+debian-bts at gildea.com
Mon Jan 2 23:40:12 UTC 2017


Package: acpi-support
Version: 0.142-8
Tags: patch

Please accept this patch updating the brightness-adjusting script.

I noticed that asus-keyboard-backlight.sh did all the things I wanted,
but it had the path of the device class hardwired in, and I have
different hardware.  So I pulled out the path into a second argument.
In the process I enhanced the values you can pass as the first
(up/down) argument to support a wide variety of devices and behavior.

Because the new script is more general, I renamed it from
asus-keyboard-backlight.sh to brightness.sh and changed the callers.

With this change, anyone will be able to add brightness support for a
new device merely by adding new events files.

 < Stephen


diff --git a/events/asus-keyboard-backlight-down b/events/asus-keyboard-backlight-down
index 88bcfc1..892a338 100644
--- a/events/asus-keyboard-backlight-down
+++ b/events/asus-keyboard-backlight-down
@@ -1,7 +1,5 @@
 # /etc/acpi/events/asus-keyboard-backlight-down
-# This is called when the user presses the key brightness 
-# down button and calls /etc/acpi/asus-keyboard-backlight.sh for
-# further processing.
+# This is called when the user presses the key brightness down button.
 
 event=hotkey ATKD 000000c5
-action=/etc/acpi/asus-keyboard-backlight.sh down
+action=/etc/acpi/brightness.sh down leds/asus::kbd_backlight
diff --git a/events/asus-keyboard-backlight-up b/events/asus-keyboard-backlight-up
index 52a3d5a..ba438ac 100644
--- a/events/asus-keyboard-backlight-up
+++ b/events/asus-keyboard-backlight-up
@@ -1,7 +1,5 @@
 # /etc/acpi/events/asus-keyboard-backlight-up
-# This is called when the user presses the key brightness 
-# up button and calls /etc/acpi/asus-keyboard-backlight.sh for
-# further processing.
+# This is called when the user presses the key brightness up button.
 
 event=hotkey ATKD 000000c4
-action=/etc/acpi/asus-keyboard-backlight.sh up
+action=/etc/acpi/brightness.sh up leds/asus::kbd_backlight
diff --git a/brightness.sh b/brightness.sh
new file mode 100755
index 0000000..c47b76c
--- /dev/null
+++ b/brightness.sh
@@ -0,0 +1,98 @@
+#! /bin/sh
+# Generic script to increase or decrease an ACPI brightness level.
+# Can be used for screen backlight, keyboard backlight,
+# and even to turn on and off binary LEDs.
+
+# An example entry in /etc/acpi/events/ might contain these two lines:
+#
+# event=video/brightnessup *
+# action=/etc/acpi/brightness.sh +12% backlight/myvendor_backlight
+#
+# The "event" line gives the event as reported by acpi_listen when
+# that hotkey is pressed.
+# The "action" lines gives the path to this script and its arguments.
+# Here, we are increasing by 12% the brightness of "myvendor_backlight".
+#
+# See comments about the arguments in the code, below.
+
+acpi_basedir=/sys/class
+
+# The amount to change the brightness.  Values can be:
+# +N%  -- up by that percentage, e.g., "+5%"
+# -N%  -- down by that percentage
+# =N%  -- set to that percentage
+# +N   -- up by that raw value
+# -N   -- down by that raw value
+# =N   -- set to that raw value
+# <word> -- there are various aliases defined; see the case statement below.
+
+# In the +/- cases, the amount may be adjusted to make some difference.
+# For example, raising a two-value keyboard backlight by +5% will actually
+# raise it by 50%, because that is the granularity of the value.
+# This behavior is intended to make the "up" and "down" aliases most useful.
+
+adjustment_arg=$1
+case $adjustment_arg in
+  up)   adjustment_arg="+5%" ;;
+  down) adjustment_arg="-5%" ;;
+  on)   adjustment_arg="=100%" ;;
+  off)  adjustment_arg="=0%" ;;
+  +*|-*|=*) ;;
+  *) echo "$0: Unknown adjustment argument '$1'" >&2 ; exit 1 ;;
+esac
+
+# The subdirectory of /sys/class that contains the brightness files
+# This is probably a directory under leds/ or backlight/
+# If unspecified and there is a unique directory under backlight/
+# we use that.
+
+if [ "$2" ]; then
+  # no leading .. allowed in directory names
+  case $2 in ..*|*/..*) exit 1;; esac
+  acpi_dir=$acpi_basedir/$2
+else
+  acpi_dir=$(echo "$acpi_basedir"/backlight/*)
+fi
+
+# Quit if we don't find the directory or the files we expect.
+set -e
+
+cd "$acpi_dir"
+
+read -r max_value < ./max_brightness
+read -r cur_value < ./brightness
+
+case $adjustment_arg in
+  =*)
+    absolute_arg=${adjustment_arg#=}
+    case $absolute_arg in
+      *%) new_value=$((max_value * ${absolute_arg%\%} / 100));;
+      *) new_value=$absolute_arg;;
+    esac
+    ;;
+  *%)
+    step_by=$((max_value * ${adjustment_arg%\%} / 100))
+    if [ "$step_by" = 0 ]; then
+      # This works for keyboard backlights (which often have only 2 levels),
+      # and binary lights with only one level.
+      case $adjustment_arg in
+        -*) step_by=-1 ;;
+        *)  step_by=1 ;;
+      esac
+    fi
+    new_value=$((cur_value + step_by))
+    ;;
+  *)
+    new_value=$((cur_value + adjustment_arg))
+    ;;
+esac
+
+if [ "$new_value" -lt 0 ]; then
+  new_value=0
+elif [ "$new_value" -gt "$max_value" ]; then
+  new_value=$max_value
+fi
+
+if [ "$new_value" != "$cur_value" ]; then
+  echo "$new_value" > ./brightness
+fi
diff --git a/asus-keyboard-backlight.sh b/asus-keyboard-backlight.sh
deleted file mode 100644
index 3fef2ff..0000000
--- a/asus-keyboard-backlight.sh
+++ /dev/null
@@ -1,24 +0,0 @@
-#!/bin/sh
-
-# this directory is a symlink on my machine:
-KEYS_DIR=/sys/class/leds/asus\:\:kbd_backlight
-
-test -d $KEYS_DIR || exit 0
-
-MIN=0
-MAX=$(cat $KEYS_DIR/max_brightness)
-VAL=$(cat $KEYS_DIR/brightness)
-
-if [ "$1" = down ]; then
-	VAL=$((VAL-1))
-else
-	VAL=$((VAL+1))
-fi
-
-if [ "$VAL" -lt $MIN ]; then
-	VAL=$MIN
-elif [ "$VAL" -gt $MAX ]; then
-	VAL=$MAX
-fi
-
-echo $VAL > $KEYS_DIR/brightness



More information about the Pkg-acpi-devel mailing list