# [solved] xbacklight no function

## DerPreis

Hello,

I can't get xbacklight to work. If I use something like

```
xbacklight -dec -20
```

there is no function and no error.

I have an Optimus Laptop. It is possible to change the backlight brightness with 

```
 echo xxx > /sys/class/backlight/intel_backlight/brightness
```

Have someone a solution for this?Last edited by DerPreis on Fri Aug 31, 2018 11:19 pm; edited 1 time in total

----------

## Anon-E-moose

Does the optimus have a function key for adjusting brightness?

----------

## DerPreis

Yes its FN+F11/F12. But they shouldn't be mapped yet, because I use i3 as windows manager without a desktop enviroment and have to map the keys myself

----------

## Anon-E-moose

I saw something while googling brightness and it reference using acpi so I modified the acpi script /etc/acpi/default.sh

under cd) I added

```

        video)

                case $action in

                        displayoff) :;;

                        brightnessdown)

                                    max=$(cat /sys/class/backlight/intel_backlight/max_brightness)

                                    curr=$(cat /sys/class/backlight/intel_backlight/actual_brightness)

                                    div=$((max / 10))

                                    next=$((curr-div))

                                    if [ $next -gt 0 ]; 

                                    then

                                        curr=$next

                                    else

                                        curr=0

                                    fi

                                    echo $curr  > /sys/class/backlight/intel_backlight/brightness

                                    ;;

                        brightnessup)

                                    max=$(cat /sys/class/backlight/intel_backlight/max_brightness)

                                    curr=$(cat /sys/class/backlight/intel_backlight/actual_brightness)

                                    div=$((max / 10))

                                    next=$((curr+div))

                                    if [ $next -lt $max ]; 

                                    then

                                        curr=$next

                                    else

                                        curr=$max

                                    fi

                                    echo $curr  > /sys/class/backlight/intel_backlight/brightness

                                    ;;

                        *) uhd $*;;

                esac

                ;;
```

It allowed my function keys to work perfectly.

Edit to add: I guess you could make a script to just write values to the /sys/*/brightness file but I like using the function keys, values are between 0 and 100.

Using acpi it even works from the console.

----------

## khayyam

DerPreis ...

like the example posted by Anon-E-moose ... though without cluttering up 'default.sh', and the UUoC ;)

```
#!/bin/sh

set -e

backlight_sys_dir="/sys/class/backlight/intel_backlight"

read -r max_brightness < "${backlight_sys_dir}/max_brightness"

read -r curr_brightness < "${backlight_sys_dir}/brightness"

case "$1" in

      up) increment="+ 10" ;;

    down) increment="- 10" ;;

       *) exit 1 ;;

esac 

new_brightness=$(($curr_brightness $increment))

if $((new_brightness < 1)) || $((new_brightness > $max_brightness)); then

    exit 1

else

    echo "$new_brightness" > ${backlight_sys_dir}/brightness 

fi
```

You would then call 'backlight.sh' via 'default.sh' for 'brightnessup' and 'brightnessdown' (which 'acpi_listen' should show correspond to your brightness keys).

```
case "$group" in

[...]

  video)

    case "$action" in 

      brightnessup) /etc/acpi/actions/backlight.sh up ;;

      brightnessdown) /etc/acpi/actions/backlight.sh down ;;

      *) log_unhandled $* ;;

    esac ;;
```

HTH & best ... khay

----------

## DerPreis

First of all thanks for the help. I particually understand the magic of acpi now... 

At the moment state my FN+Brightnessdown is working. The FN+Brightnessup won't. I don't understand why. 

I have mapped the backlight.sh up/down script in i3 to Alt+F11/F12 and its working there. 

I tried to change the default.sh to only brightess up but it still only changes the brightness down... strange...

these are my files...

/etc/acpi/default.sh

```
#!/bin/sh

# /etc/acpi/default.sh

# Default acpi script that takes an entry for all actions

set $*

group=${1%%/*}

action=${1#*/}

device=$2

id=$3

value=$4

log_unhandled() {

   logger "ACPI event unhandled: $*"

}

case "$group" in

   button)

      case "$action" in

         power)

            /etc/acpi/actions/powerbtn.sh

            ;;

         # if your laptop doesnt turn on/off the display via hardware

         # switch and instead just generates an acpi event, you can force

         # X to turn off the display via dpms.  note you will have to run

         # 'xhost +local:0' so root can access the X DISPLAY.

         #lid)

         #   xset dpms force off

         #   ;;

         *)   log_unhandled $* ;;

   video)

      case "$action" in

         brightnessup) /etc/acpi/actions/backlight.sh up ;;

         brightnessdown) /etc/acpi/actions/backlight.sh down ;;

         *)   log_unhandled $* ;;

      esac

      ;;

   ac_adapter)

      case "$value" in

         # Add code here to handle when the system is unplugged

         # (maybe change cpu scaling to powersave mode).  For

         # multicore systems, make sure you set powersave mode

         # for each core!

         #*0)

         #   cpufreq-set -g powersave

         #   ;;

         # Add code here to handle when the system is plugged in

         # (maybe change cpu scaling to performance mode).  For

         # multicore systems, make sure you set performance mode

         # for each core!

         #*1)

         #   cpufreq-set -g performance

         #   ;;

         *)   log_unhandled $* ;;

      esac

      ;;

   *)   log_unhandled $* ;;

esac

```

/etc/acpi/actions/backlight.sh

```
#!/bin/sh

set -e

backlight_sys_dir="/sys/class/backlight/intel_backlight"

read -r max_brightness < "${backlight_sys_dir}/max_brightness"

read -r curr_brightness < "${backlight_sys_dir}/brightness"

echo $max_brightness, $curr_brightness

case "$1" in

   up) increment="+ 375" ;;

   down) increment="- 375" ;;

   *) exit 1 ;;

esac

new_brightness=$(($curr_brightness $increment))

if (($new_brightness < 1)) || (($new_brightness > $max_brightness)); then

   exit 1

else

   echo "$new_brightness" > ${backlight_sys_dir}/brightness

fi

```

acpi_listen

```
video/brightnessup BRTUP 00000086 00000000

video/brightnessdown BRTDN 00000087 00000000

```

I changed the value in brightness to 375 cause of the max of 7500...

----------

## khayyam

DerPreis ... you're missing a closing 'esac' for the 'case $action in power)', it should be:

```
case "$group" in

  button)

    case "$action" in

      power) /etc/acpi/actions/powerbtn.sh ;;

      *) log_unhandled $* ;;

    esac ;;

  video)

    case "$action" in 

      brightnessup) /etc/acpi/actions/backlight.sh up ;;

      brightnessdown) /etc/acpi/actions/backlight.sh down ;;

      *) log_unhandled $* ;;

    esac ;;
```

If correcting that doesn't resolve the issue then post the output of the following:

```
# find /sys/class/backlight/intel_backlight -maxdepth 1 -type f -print -exec cat {} \;

# ls -l /etc/acpi/actions/
```

Also note that "echo $max_brightness, $curr_brightness" is wasted, there is no TTY for that output, if you want that to go somewhere then you will need to use 'logger', eg:

```
logger "ACPI backlight: max_brightness: $max_brightness, curr_brightness: $curr_brightness"
```

best ... khay

----------

## DerPreis

Awesome! It works now!

The missing "esac" was the mistake... the echo [..] was only for testing and is deleted now!

Thank you very much for this lesson, I definitely learned something!

----------

## khayyam

 *DerPreis wrote:*   

> Awesome! It works now!

 

DerPreis ... ok, good. You might also want to add the following under 'button)'

```
mute) /usr/bin/amixer -q set Master toggle ;;

volumeup) /usr/bin/amixer -q set Master 2dB+ unmute ;;

volumedown) /usr/bin/amixer -q set Master 2dB- unmute ;;
```

 *DerPreis wrote:*   

> Thank you very much for this lesson, I definitely learned something!

 

You're welcome & best ... khay

----------

