Last active
October 17, 2016 18:55
-
-
Save joona/6ec1081a51bb3ee262ac967f3540c8b7 to your computer and use it in GitHub Desktop.
Backlight control helper for laptops running intel and NVIDIA cards.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| PRIME_MODE=`prime-select query` | |
| IS_NVIDIA=false | |
| if [ "$PRIME_MODE" == "nvidia" ]; then | |
| IS_NVIDIA=true | |
| fi | |
| NVIDIA_BACKLIGHT=/sys/class/backlight/intel_backlight/brightness | |
| BRIGHTNESS=100 | |
| MAX_BRIGHTNESS=100 | |
| SINGLE_STEP=1 | |
| STEP=$(echo "$SINGLE_STEP * 5" | bc) | |
| check_nvidia() { | |
| if [[ $IS_NVIDIA -ne false ]]; then | |
| echo "NVIDIA GPU enabled, can't proceed!" | |
| exit 1 | |
| fi | |
| } | |
| get_brightness() { | |
| if [[ $IS_NVIDIA ]]; then | |
| REAL_BRIGHTNESS=$(cat /sys/class/backlight/intel_backlight/brightness) | |
| MAX_BRIGHTNESS=$(cat /sys/class/backlight/intel_backlight/max_brightness) | |
| SINGLE_STEP=$(echo "$MAX_BRIGHTNESS/100" | bc | cut -f1 -d ".") | |
| BRIGHTNESS=$(echo "$REAL_BRIGHTNESS/$SINGLE_STEP" | bc) | |
| else | |
| REAL_BRIGHTNESS=$(xbacklight | awk '{print int($1+0.5)}') | |
| BRIGHTNESS=$REAL_BRIGHTNESS | |
| fi | |
| } | |
| get_brightness | |
| if [ $BRIGHTNESS -lt 50 ]; then | |
| STEP=3 | |
| fi | |
| if [ $BRIGHTNESS -lt 5 ]; then | |
| STEP=1 | |
| fi | |
| case "$1" in | |
| get) | |
| echo $BRIGHTNESS | |
| ;; | |
| set) | |
| if [[ $IS_NVIDIA ]]; then | |
| echo $(echo $SINGLE_STEP * $2 | bc) | sudo tee $NVIDIA_BACKLIGHT | |
| else | |
| xbacklight -set $2 | |
| fi | |
| get_brightness | |
| echo $BRIGHTNESS | |
| ;; | |
| real) | |
| echo $REAL_BRIGHTNESS | |
| ;; | |
| up) | |
| if [[ $IS_NVIDIA ]]; then | |
| NEW_VALUE=$(echo "$REAL_BRIGHTNESS + ($SINGLE_STEP * $STEP)" | bc) | |
| echo $NEW_VALUE | sudo tee $NVIDIA_BACKLIGHT | |
| else | |
| xbacklight -inc $STEP | |
| fi | |
| get_brightness | |
| echo $BRIGHTNESS | |
| ;; | |
| down) | |
| if [[ $IS_NVIDIA ]]; then | |
| NEW_VALUE=$(echo "$REAL_BRIGHTNESS - ($SINGLE_STEP * $STEP)" | bc) | |
| if [[ "$NEW_VALUE" -lt 1 ]]; then | |
| echo 1 | sudo tee $NVIDIA_BACKLIGHT | |
| else | |
| echo $NEW_VALUE | sudo tee $NVIDIA_BACKLIGHT | |
| fi | |
| else | |
| xbacklight -dec $STEP | |
| fi | |
| get_brightness | |
| echo $BRIGHTNESS | |
| ;; | |
| reset) | |
| if [[ $IS_NVIDIA ]]; then | |
| echo $(echo "$SINGLE_STEP * 20" | bc) | sudo tee $NVIDIA_BACKLIGHT | |
| else | |
| xbacklight -set 20 | |
| fi | |
| get_brightness | |
| echo $BRIGHTNESS | |
| ;; | |
| *) | |
| echo "./brightnessctl.sh <get|up|down>" | |
| exit 1 | |
| ;; | |
| esac | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On my Dell XPS 15, running ubuntu Ubuntu 16.04, xbacklight doesn't do the right thing when nvidia card is selected. Then again, when running on intel, xbacklight works, but /sys/class/backlight/intel_backlight/brightness doesn't. So I wrote this helper that I use this with i3wm, commands up / down mapped to hardware brightness control keys, and i3 toolbar is reading outputs of get -command.
Works at least on 4.4.0-43-generic kernel.