There are many ways to adjust the screen backlight of a monitor, laptop or integrated panel (such as the iMac) using software, but depending on hardware and model, sometimes only some options are available.
Examples:
- brightness is controlled by vendor specified hotkey. And there is no
interface for OS to adjust brightness.
- brightness is controlled by OS:
- brightness could be controlled by ACPI
- brightness could be controlled by graphic driver.
There is an excellent article on Wiki summarizing the possible ways of doing this at:
https://wiki.archlinux.org/index.php/Backlight
However if ACPI
, xbacklight
or xcalib
fail to adjust your brightness levels we can use the setpci
command to manipulate the graphics card directly. This is the method we will be using for this tutorial.
Note: This method should only be used if the above mentioned programs fail to accomplish what your wanting to do and is not the preferred method for doing so. However in numerous cases, its the only one that will.
What were going to do is bind the setpci
command to a key combination on your keyboard using xbindkeys,
so that when certain keys are pressed your brightness will adjust up or down.
Ok Let's get Started!!
- First we need to determine the Device Address of your graphics
card, in most cases its going to be
00:02.0
but not always so
here's how to check.
Open up a terminal window and at the command prompt type the following:
lspci |grep -i vga
This will return a line displaying information such as vendor, model, version etc... The part were interested in is at the very beginning and should look something like the following:
00:02.0
Note: Output may vary
This is the Device Address of your graphics card and we will be using this along with the setpci
command to control your brightness levels. So Write It Down!
- Next we need to download
xbindkeys
. This will be the program that
does the actual binding of the setpci
command to the key
combination on your keyboard.
Open a terminal window and type the following at the command prompt:
sudo apt-get install xbindkeys
- Now we are going to create two files in our Home directory that
xbindkeys
will use when it starts up.
Open a terminal window if you haven't already and change into your Home directory. Type the following commands at the command prompt:
touch .xbindkeysrc
This creates a file that we will be typing our configurations into and it is read by xbindkeys
each time it is started
touch .xbindkeys.noauto
This just creates a blank file but it is crucial because when it exists it keeps xbindkeys
from starting automatically at boot. Though we want xbindkeys
to start automatically we want to control it ourselves because the Xserver starts it to early in the boot process. This causes our configurations that we make to sometimes not function properly. We will add a script called xb_start.sh
to our Startup Applications later in this tutorial that will take care of the startup process.
Let's find out what keys do what!!
For time saving purposes I'm going to automatically assume that the Xserver already recognizes the keys you press on your keyboard to change brightness, it just doesn't do anything when you press them. In my case its the Fn
+ Up Arrow
or Down Arrow
But we can't just put the words Fn
,Up Arrow
or Down Arrow
in our config file, we have to find out what the keycodes are.
In most cases they are going to be keycode 232 to adjust the brightness down and keycode 233 to adjust the brightness up but we better make sure.
Open a terminal window and type the following at the command prompt:
xmodmap -pke |grep -i xf86monbrightness
This should return two lines that look similar to the following:
keycode 232 = XF86MonBrightnessDown NoSymbol XF86MonBrightnessDown
keycode 233 = XF86MonBrightnessUp NoSymbol XF86MonBrightnessUp
The info were concerned about are the numbers that come after keycode. In this case it's 232
and 233
. Write whatever numbers it is down for we will be using them later.
Let's start putting it all together!!
- Now we're going to use the information that we've gathered and
written down to add some lines to the
.xbindkeysrc
file you created
earlier in your Home directory.
Using your favorite text editor, open the .xbindkeysrc
file and either copy and paste or type the following lines to it and save it:
Note: If 232
and 233
aren't the same keycodes you wrote down earlier, change them in this file
"/usr/local/bin/blevel_down.sh"
c:232
"/usr/local/bin/blevel_up.sh"
c:233
What were doing here is telling xbindkeys
to run the blevel_down.sh
script when we press the key combo on our keyboard that adjusts brightness down. In my case it's Fn + Down Arrow
. On the next line we're telling it to run the blevel_up.sh
for Fn + Up Arrow
Note: Key combos vary, check the pictures on the keys of your keyboard to determine which ones they are for you.
Lets write the scripts!!
The blevel_up.sh
and blevel_down.sh
scripts are created to incrementally adjust the brightness up or down by passing arguments to the setpci
command. So pretty much when you press whatever keys that do the adjusting, the brightness goes up a little or down a little each time. The xb_start.sh
is used to start xbindkeys
when the computer boots up but allows for other processes to complete before doing so.
Note: If your Device Address of your Graphics card that we determined earlier differs from the one in blevel_up.sh
and blevel_down.sh
you will need to modify accordingly. Most likely they should be the same.
- We will create
blevel_up.sh
first:
Open up your favorite text editor and either copy and paste or type the following into a file and save it as blevel_up.sh in your Home directory
#!/bin/bash
if [ ! -f /home/$USER/.blevel ]; then
touch /home/$USER/.blevel
echo "FF" > /home/$USER/.blevel
state="FF"
else
state=`cat /home/$USER/.blevel`
fi
new_state=$(echo "$[0x$state+0x10]")
if [ "$new_state" -gt 255 ]; then
sudo setpci -s 00:02.0 F4.B=FF
else
state=$(echo "obase=16; $new_state" | bc)
sudo setpci -s 00:02.0 F4.B=$state
echo $state > /home/$USER/.blevel
fi
- Now we will create the
blevel_down.sh
script:
Open up your favorite text editor and either copy and paste or type the following into a file and save it as blevel_down.sh in your Home directory
#!/bin/bash
if [ ! -f /home/$USER/.blevel ]; then
touch /home/$USER/.blevel
echo "FF" > /home/$USER/.blevel
state="FF"
else
state=`cat /home/$USER/.blevel`
fi
new_state=$(echo "$[0x$state-0x10]")
if [ "$new_state" -lt 15 ]; then
sudo setpci -s 00:02.0 F4.B=0F
else
state=$(echo "obase=16; $new_state" | bc)
sudo setpci -s 00:02.0 F4.B=$state
echo $state > /home/$USER/.blevel
fi
- Now we will create the
xb_start.sh
script:
Open up your favorite text editor and either copy and paste or type the following into a file and save it as xb_start.sh in your Home directory
#!/bin/bash
sleep 10
/usr/bin/xbindkeys &
Lets make the scripts Global!!
- Now what we wanna do is move the three scripts we just created from
our Home directory to
/usr/local/bin
and set the permissions
so that no matter who is logged in on the machine they can execute
them.
Open up a terminal window and type the following at the command prompt:
sudo mv blevel_up.sh blevel_down.sh xb_start.sh /usr/local/bin
sudo chmod 755 /usr/local/bin/blevel_up.sh
sudo chmod 755 /usr/local/bin/blevel_down.sh
sudo chmod 755 /usr/local/bin/xb_start.sh
Now to make setpci
be able to run without typing a password each time
Being that setpci
makes changes to certain system files, a regular user would have to prefix setpci
with sudo
eachtime in order to run the command. This would require you to enter a password eachtime you want to adjust the brightness of your screen. If we make a particular entry in the /etc/sudoers
file we can get around this.
Open a terminal window and type the following at the command prompt:
sudo visudo
This will open up the /etc/sudoers
file so that we can make the required changes.
Scroll all the way to the bottom of the file and type the following:
ALL ALL = NOPASSWD:/usr/bin/setpci
Note: File is Case-Sensitive so type exactly how it is here
Save the file and exit
Almost Finished!!
Last thing we need to do is add our xb_start.sh
script to our Startup Applications so that xbindkeys
starts at boot exactly when we want it to.
Reboot and Enjoy!
sudo apt-get install xbacklight
After you have it install type the following at the command prompt:xbacklight -dec 50%
and see if the brightness decreases. If it does typexbacklight -inc 100%
to adjust to full brightness. Report back on your findings.