Rewrite brightness script

This commit is contained in:
ItsDrike 2022-06-19 15:35:15 +02:00
parent 5c2f1eed17
commit 3f1d2d31b0
No known key found for this signature in database
GPG key ID: B014E761034AF742
2 changed files with 137 additions and 47 deletions

View file

@ -197,13 +197,13 @@ myKeys =
, ("<XF86AudioMute>", spawn "pulsemixer --toggle-mute") , ("<XF86AudioMute>", spawn "pulsemixer --toggle-mute")
, ("<XF86AudioLowerVolume>", spawn "pulsemixer --change-volume -5") , ("<XF86AudioLowerVolume>", spawn "pulsemixer --change-volume -5")
, ("<XF86AudioRaiseVolume>", spawn "pulsemixer --change-volume +5") , ("<XF86AudioRaiseVolume>", spawn "pulsemixer --change-volume +5")
, ("<XF86MonBrightnessUp>", spawn "brightness + 5 %") , ("<XF86MonBrightnessUp>", spawn "brightness -i 5%")
, ("<XF86MonBrightnessDown>", spawn "brightness - 5 %") , ("<XF86MonBrightnessDown>", spawn "brightness -d 5%")
-- Map media keys to meta + arrows for keyboards without special keys -- Map media keys to meta + arrows for keyboards without special keys
, ("M-<Down>", spawn "pulsemixer --change-volume -5") , ("M-<Down>", spawn "pulsemixer --change-volume -5")
, ("M-<Up>", spawn "pulsemixer --change-volume +5") , ("M-<Up>", spawn "pulsemixer --change-volume +5")
, ("M-<Right>", spawn "brightness + 5 %") , ("M-<Right>", spawn "brightness -i 5%")
, ("M-<Left>", spawn "brightness - 5 %") , ("M-<Left>", spawn "brightness -d 5%")
] ]
where nonNSP = WSIs (return (\ws -> W.tag ws /= "NSP")) where nonNSP = WSIs (return (\ws -> W.tag ws /= "NSP"))
nonEmptyNonNSP = WSIs (return (\ws -> isJust (W.stack ws) && W.tag ws /= "NSP")) nonEmptyNonNSP = WSIs (return (\ws -> isJust (W.stack ws) && W.tag ws /= "NSP"))

View file

@ -1,63 +1,153 @@
#!/bin/sh #!/bin/sh
BRIGHTNESS_FILE="/sys/class/backlight/intel_backlight/brightness"
BRIGHTNESS_MAX=937
function change_brightness() { # Parse arguments
BRIGHTNESS=$(cat $BRIGHTNESS_FILE) # ------------------------------------------------------------------------------------
BRIGHTNESS_DIR="/sys/class/backlight/*"
SEND_NOTIFICATION=0
URGENCY="normal"
INCREASE=0
DECREASE=0
SET=0
BRIGHTNESS=0
change_value=$2 while [ "$1" ]; do
case "$1" in
-h | --help)
cat << EOF
brightness is a cli tool that for displaying or modifying screen brightness.
# If we're dealing with percentages, convert to absolutes Options:
if [ $3 == "%" ]; then -h | --help: Display this message
change_value=$((($BRIGHTNESS_MAX / 100) * $change_value)) -n | --notification: Produce a desktop notification with brightness info
elif [ $3 == "#" ]; then -N | --no-notification: Don't produce a desktop notification with brightness info
change_value=$change_value -u | --urgency [URGENCY]: Pass over notify-send urgency attribute (default: normal)
else -i | --increase [BRIGHTNESS]: Increase the brightness by given amount
echo "Invalid unit, options: [% - percent, # - absolute], default: %" -d | --decrease [BRIGHTNESS]: Decrease the brightness by given amount
-s | --set [BRIGHTNESS]: Set new brightness level
-p | --path [DIR_PATH]: Path to brightness directory (default: /sys/class/backlight/*)
Valid values:
URGENCY: low, normal, critical
DIR_PATH: Valid path to a directory
BRIGHTNESS:
specific value - Example: 10
percentage value - Example: 10%
EOF
exit 0
;;
-n | --notification)
SEND_NOTIFICATION=1
;;
-N | --no-notification)
SEND_NOTIFICATION=0
;;
-u | --urgency)
URGENCY="$2"
shift
;;
-i | --increase)
INCREASE=1
BRIGHTNESS="$2"
shift
;;
-d | --decrease)
DECREASE=1
BRIGHTNESS="$2"
shift
;;
-s | --set)
SET=1
BRIGHTNESS="$2"
shift
;;
-p | --path)
BRIGHTNESS_DIR="$2"
shift
;;
* )
echo "Unknown argument '$1', use -h or --help for help"
exit 1 exit 1
;;
esac
shift
done
# Define constants based on parsed arguments
# ------------------------------------------------------------------------------------
BRIGHTNESS_FILE="$BRIGHTNESS_DIR/brightness"
BRIGHTNESS_MAX="$(cat $BRIGHTNESS_DIR/max_brightness)"
# Helper functins
# ------------------------------------------------------------------------------------
# Send brightness level desktop notification, showing the given brightness level
# as progress bar, along with given message.
# $1 - brightness level (number 0-100)
# $2 - message (notification body)
send_brightness_notify() {
percent_brightness="$1"
msg="$2"
notify-send \
--app-name=brightness \
--urgency="$URGENCY" \
-h int:value:$percent_brightness \
-h string:synchronous:brightness \
"brightness" "$msg"
}
# Set brightness to given absolute value
# $1 - brightness absolute value
set_brightness() {
# there should be sudo config allowing this command without password
echo "$1" | sudo tee $BRIGHTNESS_FILE >/dev/null
}
# Main Logic
# ------------------------------------------------------------------------------------
# Determine the absolute new brightness level
if [ $INCREASE -eq 1 ] || [ $DECREASE -eq 1 ] || [ $SET -eq 1 ]; then
# If we're dealing with percentages, change to absolutes
if echo "$BRIGHTNESS" | grep -qE '%$'; then
numeric=$(echo "$BRIGHTNESS" | sed 's/.$//')
absolute=$(echo "($BRIGHTNESS_MAX / 100) * $numeric" | bc -l)
BRIGHTNESS=$(printf "%.0f" $absolute)
fi fi
# Increment or decrement based on first arg # Get the new requested absolute brightness
if [ $1 == "+" ]; then if [ $SET -eq 1 ]; then
new_brightness=$(($BRIGHTNESS + $change_value)) new_brightness=$BRIGHTNESS
elif [ $1 == "-" ]; then elif [ $DECREASE -eq 1 ]; then
new_brightness=$(($BRIGHTNESS - $change_value)) cur_brightness=$(cat $BRIGHTNESS_FILE)
new_brightness=$(($cur_brightness - $BRIGHTNESS))
else else
echo "Invalid operator, options: [+, -]" cur_brightness=$(cat $BRIGHTNESS_FILE)
exit 1 new_brightness=$(($cur_brightness + $BRIGHTNESS))
fi fi
# Make sure we respect min/max boundaries # Ensure we respect max/min boundaries
if [ $new_brightness -lt 0 ]; then if [ $new_brightness -lt 0 ]; then
new_brightness=0 new_brightness=0
elif [ $new_brightness -gt $BRIGHTNESS_MAX ]; then elif [ $new_brightness -gt $BRIGHTNESS_MAX ]; then
new_brightness=$BRIGHTNESS_MAX new_brightness=$BRIGHTNESS_MAX
fi fi
# Write the brightness (sudo shouldn't require password here) # Update the brightness
echo $new_brightness | sudo tee $BRIGHTNESS_FILE set_brightness $new_brightness
}
if [ $# -ge 1 ] && [ "$1" == "+" ] || [ "$1" == "-" ]; then
if [ $# -lt 2 ]; then
change_value=5 # Default to 5%
else
change_value=$2
fi fi
if [ $# -lt 3 ]; then cur_brightness=$(cat $BRIGHTNESS_FILE)
unit="%" percent_brightness=$(echo "($cur_brightness / $BRIGHTNESS_MAX) * 100" | bc -l)
else percent_brightness_2f=$(printf "%.2f" $percent_brightness)
unit=$3 percent_brightness_rounded=$(printf "%.0f" $percent_brightness)
fi
change_brightness $1 $change_value $unit > /dev/null if [ $SEND_NOTIFICATION -eq 1 ]; then
send_brightness_notify "$percent_brightness_rounded" "Level: $percent_brightness_rounded"
fi fi
# Display new brightness echo "Brightness: ${percent_brightness_2f}% (absolute: $cur_brightness)"
BRIGHTNESS=$(cat $BRIGHTNESS_FILE)
BRIGHTNESS_PERCENT=$(echo "($BRIGHTNESS / $BRIGHTNESS_MAX) * 100" | bc -l)
BRIGHTNESS_PERCENT=$(printf "%.2f" $BRIGHTNESS_PERCENT)
echo "Brightness: ${BRIGHTNESS_PERCENT}% (absolute: $BRIGHTNESS)"