When using multiple monitors, and disconnecting and reconnecting them, I
noticed that Ubuntu 20.04 likes to set the external monitor's brightness
to 0.06, and the laptop's brightness to maximum. This is done without
changing the file at
(/sys/class/backlight/intel_backlight/actual_brightness).

I have modified the above script to work with multiple monitors, and to
change the brightness whenever a discrepancy between the desired and
actual brightnesses of all monitors is detected. The script assumes that
the internal laptop monitor's brightness is the only one to be changed
by brightness keys, and that the laptop's monitor is the first monitor
listed in the command `xrandr --listmonitors`.

Please reply if you have any issues with this script, as I do want to
improve this.

#!/bin/bash
#-----------------------------------------------------------------
# /usr/local/bin/brightness
# be sure this file is executable
#-----------------------------------------------------------------

#   Define a function which finds luminance values in default Ubuntu 
#   location, converts into a factor of maximum brightness
#   Get the max brightness for reference.
#   Get it to 2 decimal places
backlight_path=/sys/class/backlight/intel_backlight
read -r max < "$backlight_path"/max_brightness
luminance() {
    read -r level < "$backlight_path"/actual_brightness
    printf "%.2f" $(bc <<< "scale=10;$level/$max")
}

#   Set up infinite loop
while true
do
    #   Get the current brightness levels from xrandr. Store in array
    mapfile \
        -t xrandrbrightness \
        < <(xrandr --verbose --current \
            | grep Brightness \
            | sed 's/.* //' \
            | xargs printf "%.2f\n")

    #   Get list of monitors and store in array
    mapfile \
        -t monitors \
        < <(xrandr --listmonitors --current \
            | grep -v 'Monitors' \
            | awk '{print $4}')

    #   If the current brightness for monitor 0 != the desired value +/-0.01
    if (( $(echo "${xrandrbrightness[0]} != $(luminance)" | bc -l) ))
    then
        #   Set the brightness of the laptop monitor to the desired value
        echo "${monitors[0]} Current:${xrandrbrightness[0]} 
Desired:$(luminance)"
        xrandr --output "${monitors[0]}" --brightness "$(luminance)"
    fi

    #   Go through the rest of the monitors in the list
    for (( i=1; i<${#monitors[@]}; i++ ))
    do
    
        #   Change the brightness of external monitors to 1 if they aren't
        if (( $(echo "${xrandrbrightness[$i]} != 1.00" | bc -l) ))
        then
            echo "${monitors[$i]} Current:${xrandrbrightness[$i]}"
            xrandr --output "${monitors[$i]}" --brightness 1
        fi
    done


    #   Check every 100ms
    sleep 0.1
done

-- 
You received this bug notification because you are a member of Kernel
Packages, which is subscribed to linux in Ubuntu.
https://bugs.launchpad.net/bugs/1831587

Title:
  brightness control not work with OLED panel

Status in HWE Next:
  New
Status in OEM Priority Project:
  New
Status in linux package in Ubuntu:
  Incomplete

Bug description:
  image: disco 19.04
  kernel: 5.0.0-15-generic
  also verified failed on linux-image-unsigned-5.2.0-050200rc2-generic

  also refer to :
   - 
https://askubuntu.com/questions/824949/lenovo-thinkpad-x1-yoga-oled-brightness
   - https://bugs.freedesktop.org/show_bug.cgi?id=97883

  Linux can not control the brightness on the OLED panel in the traditional way 
[1].
  Although there's a workaround from the community [2] that to execute script 
while brightness hotkey event is triggered. But if we want to upstream it, it's 
still a problem to recognize the OLED panel so that the system knows when to go 
workaround.
   - EDID of my laptop which has OLED: https://paste.ubuntu.com/p/w7rVqJTYDy/
   - EDID of the same model without OLED: https://paste.ubuntu.com/p/P3ChhX9yBJ/

  there's a kernel patch related OLED for reference:
   - https://patchwork.kernel.org/patch/6133471/

  [1] https://wiki.ubuntu.com/Kernel/Debugging/Backlight
  [2] 
https://askubuntu.com/questions/824949/lenovo-thinkpad-x1-yoga-oled-brightness

To manage notifications about this bug go to:
https://bugs.launchpad.net/hwe-next/+bug/1831587/+subscriptions

-- 
Mailing list: https://launchpad.net/~kernel-packages
Post to     : kernel-packages@lists.launchpad.net
Unsubscribe : https://launchpad.net/~kernel-packages
More help   : https://help.launchpad.net/ListHelp

Reply via email to