I have made a small example script that can aid on using the function
keys to change the brightness. This script assumes that the system is
using Nvidia GPU/graphics, so change it accordingly. Look for "End of
file" to see where this script ends.

In order for everything to work nicely, we need to place the script
inside "/etc/acpi/", and this place is generally meant to be used by
administrators of the system. That said, in this example we'll be using
GNU nano to edit the script (if you want to use other text editor, and
if it's a graphical one, use "gksudo" instead of "sudo"), so open a
terminal and do (you can remove the "$ ", it's just a way to tell you
what must be done):

Note: You can change the name of the script
("acpi-to-nouveau-backlight-brightness.sh") to anything you want, just
remember the name because we'll need it later on.

$ sudo nano "/etc/acpi/acpi-to-nouveau-backlight-brightness.sh"

"sudo" and "gksudo" will ask for your user's password, and you'll
probably not be able to see it being typed.

Then, once your password is correct, GNU nano should show up. Then, copy
and paste the script bellow. If you're reading this in a terminal: use
Ctrl + Shift + C, instead of Ctrl + C, to copy the text; and use Ctrl +
Shift + V, instead of Ctrl + V to paste the text.

# Begin of file. Remove this line.
#!/bin/bash
# The line above starts the script and tells the system to use GNU Bash
to read the commands.

# Associative arrays must be created with "declare -A".
declare -A acpi_video
declare -A nv_backlight

acpi_video[path]="/sys/class/backlight/acpi_video0"
nv_backlight[path]="/sys/class/backlight/nv_backlight"

# If both directories don't exist...
if [[ ! -d "${acpi_video[path]}" || ! -d "${nv_backlight[path]}" ]]
then
        # Do nothing and exit with status "1".
        exit 1
fi

# Sets minimum brightness.
min_brightness=0
# Takes maximum brightness value from ACPI's video and Nvidia GPU.
acpi_video[max_brightness]="$(cat "${acpi_video[path]}/max_brightness")"
nv_backlight[max_brightness]="$(cat
"${nv_backlight[path]}/max_brightness")"
# Takes the current brightness value from ACPI video and Nvidia GPU.
acpi_video[brightness]="$(cat "${acpi_video[path]}/brightness")"
nv_backlight[brightness]="$(cat "${nv_backlight[path]}/brightness")"

# Needed so that we don't have to watch the ACPI backlight brightness 
# every time, and so that the Nouveau backlight brightness matches the 
# ACPI one.
# We accomplish this by doing proportionality calculation.
nv_backlight[brightness]="$(( ( ${acpi_video[brightness]} *
${nv_backlight[max_brightness]} ) / ${acpi_video[max_brightness]} ))"

# A "step" in this script is done every time the ACPI brightness is
changed. ACPI backlight brightness has a fixed step of 1. This means
that everytime you press the brightness key, you are modifying the
brightness by value "1", either negative or positive.
# Needed to convert an ACPI backlight brightness step to a Nouveau
backlight 
# brightness step.
acpi_video_to_nv_backlight_step="$(( ( 1 *
${nv_backlight[max_brightness]} ) / ${acpi_video[max_brightness]} ))"

# If the script receives the word "down"...
if [[ "${1}" == "down" ]]
then
        # Steps down on the Nvidia backlight brightness.
        nv_backlight[brightness]="$(( ( ( ${acpi_video[brightness]} *
${nv_backlight[max_brightness]} ) / ${acpi_video[max_brightness]} ) -
acpi_video_to_nv_backlight_step ))"
# Else if the word received is "up"...
elif [[ "${1}" == "up" ]]
then
        # Steps up on the Nvidia backlight brightness.
        nv_backlight[brightness]="$(( ( ( ${acpi_video[brightness]} *
${nv_backlight[max_brightness]} ) / ${acpi_video[max_brightness]} ) +
acpi_video_to_nv_backlight_step ))"
fi

# Let's make sure that the Nvidia brightness is not negative.
if [[ "${nv_backlight[brightness]}" -lt "${min_brightness}" ]]
then
        nv_backlight[brightness]="${min_brightness}"
# And not way beyond what the maximum brightness is supposed to be.
elif [[ "${nv_backlight[brightness]}" -gt
"${nv_backlight[max_brightness]}" ]]
then
        nv_backlight[brightness]="${nv_backlight[max_brightness]}"
fi

# Finally, let's make things work...
# Sets the Nvidia GPU brightness to the one calculated by this script.
echo "${nv_backlight[brightness]}" > "${nv_backlight[path]}/brightness"
# End of file. Remove this line.

Inside GNU nano, press Ctrl + X (labeled as "^X" on the bottom) to quit.
You'll be asked if you want to save the file, do so.

Now, we'll set the permissions of the file, by doing:

Note: If you have changed the name of the file, do so here too.

$ sudo chmod "+rx" "/etc/acpi/acpi-to-nouveau-backlight-brightnes"

Now we set which user and group are the owners of the file:

Note: If you have changed the name of the file, do so here too.

$ sudo chown "root:root" "/etc/acpi/acpi-to-nouveau-backlight-brightnes"

After doing this, we must now find out what the software responsible for
ACPI control is seeing when you press the brightness keys, because,
apparently we didn't tell how to recognize the key yet.

To have a chance to see which key is seen by the ACPI controller, one
can do:

$ acpi_listen

From now on until you press Ctrl + C (terminate the process),
acpi_listen will print what it sees each time you press a function key.

In my case, since I'm using a Sony laptop, every time I press the
"brightness up" key, acpi_listen prints three lines, each starting with:

video/brightnessup
sony/hotkey
sony/hotkey

If, like in my case, you see "video/brightnessup" in one of the lines,
you can take note of "video/brightnessup" (make sure that acpi_listen
sees "video/brightnessdown" every time you press the "brightness down"
function key too) and, in this case, you don't need to take note of the
letters or numbers that appear after that.

As I said earlier, and once your notes are taken, press Ctrl + C to
quit/terminate acpi_listen.

Now we'll begin telling the ACPI controller what it must do every time
we press the keys. So, for the "brightness down" function key, do:

Note: You can change the name of the files.

$ sudo nano "/etc/acpi/events/acpi-to-nouveau-backlight-brightness-down"

And write this inside:

Note: If you have changed the name of the very first script, do so here
too.

# Begin of file. Remove this line.
event=video/brightnessdown
action=/etc/acpi/acpi-to-nouveau-backlight-brightness.sh down
# End of file. Remove this line.

Now, save and exit GNU nano.

Now, we do the same for the "brightness up" function key:

Note: You can change the name of the files.

$ sudo nano "/etc/acpi/events/acpi-to-nouveau-backlight-brightness-up"

And so we write the following text inside:

Note: If you have changed the name of the very first script, do so here
too.

# Begin of file. Remove this line.
event=video/brightnessup
action=/etc/acpi/acpi-to-nouveau-backlight-brightness.sh up
# End of file. Remove this line.

Finally, we do:

$ sudo service acpid stop
$ sudo service acpid start

And we're done!

I wish you success on all your free software activism!

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to