Custom Brightness Controller for Ubuntu
While I love Ubuntu's desktop, the brightness and volume controls are rather annoying as they don't provide a great degree of control. To fix this, I wrote a bash script to control the brightness. Here's what I came up with:
#!/usr/bin/env bash
increment=10
backlight_prefix=/sys/class/backlight/intel_backlight/
echo $1
if [[ "$1" = "decrease" ]]; then
echo decreasing
increment=$(expr -${increment})
fi
cur_brightness=$(cat ${backlight_prefix}brightness)
max_brightness=$(cat ${backlight_prefix}max_brightness)
new_brightness=$(expr ${cur_brightness} + ${increment})
# Permissions changes on brightness:
## change group to sbrl
## add g+w
# Old command:
#gksudo -- bash -c "echo ${new_brightness} >${backlight_prefix}brightness"
echo ${new_brightness} >${backlight_prefix}brightness
####################
### Notification ###
####################
### uncomment the following line to disable the notification
#exit
# Calculate the percentage
new_percent=$(echo "(${new_brightness} / ${max_brightness}) * 100" | bc -l)
new_percent=$(printf "%.1f" "${new_percent}")
echo new_percent: $new_percent
max_bar_length=100
bar_length=$(echo "(${new_percent} / 100) * ${max_bar_length}" | bc -l)
bar_length=$(printf "%.0f" "${bar_length}")
n_bar=$(head -c $bar_length < /dev/zero | tr '\0' '=')
# Kill the previous notification
killall notify-osd
notify-send "Brightness: ${new_percent}%" "${n_bar} (${new_brightness})"
#notify-send "Brightness" "${new_percent}%"
To use the above, you need to do several things. Firstly, you need to find your screen's brightness settings. Open a terminal, and navigate to /sys/class/backlight
, and find your backlight's folder. Mine is intel_backlight
, but yours might acpi_video0
. Once found, you should have a file called brightness
inside it. Change the value of the backlight_prefix
variable on line #3 to equal the path to this folder, not forgetting the trailing slash.
You then need to alter the permissions on the brightness
file in order to allow your user account to change it - otherwise you will get prompted for your password every time you change your brightness! To do this, open a terminal and navigate to the folder we found earlier that contains the brightness
file. Change the user group to be your username with sudo chgrp username brightness
, and then allow write access to group members with sudo chmod g+w brightness
. If this doesn't persist across reboots, you might need to add these commands to your rc.local
or Xsession
files.
It should work now. If you don't want the notification to show every time you change your brightness (or if it doesn't actually work), uncomment line #27.