3

I'm trying to set a limit for maximum screen brightness on my Thinkpad X250 running Ubuntu 18.04.

A bit of context : I changed the original screen with a new one (before installing Ubuntu), the brightness controls work perfectly, but I noticed that, over 55-60% brightness, changes are almost unperceivable for human eye, but it drains a lot more battery and it starts running really hot. I'm planning to give the computer to my family with young and unexperienced users, so I would like to set a limit which does not require user action at each startup, and could work for all users without worrying that an update via the software manager could erase the settings.

What I tried : I tried to directly edit /sys/class/backlight/intel_backlight/max_brightness but it refuses to be edited (even after using chmod to change permissions). I saw some anwsers telling to add a command like "echo MAX_VALUE > /sys/class/backlight/intel_backlight/max_brightness" in /etc/rc.local, but I don't have this file. Note that I don't have *acpi_** files or folder in /sys/class/backlight/.

What would be the correct way to set an all-user permanent brightness limit ?

  • Unfortunately, I am unable to edit the max_brightness file with sudo. I tried with gedit, nano, with cat and >, tee... And I get a permission error (despite using sudo). I tried using chmod 777 on the file to be able to temporary edit it, but then I have an "Input/ouput error". – NMZ Jul 1 '18 at 12:59
  • I only habve an integrated intel GPU, and I have no other "max_brightness" files. The test has given me the only file I have as response, which I cannot edit. – NMZ Jul 1 '18 at 13:08
  • No problem. Excuse me for not using properly comments, but I am curious that a software like xbacklight cannont be used. I will investigate this before reposting another topic. – NMZ Jul 1 '18 at 14:00
  • I did already, called in someone better than me and there is no solution to your specific problem except as stated in the answer already... Sorry! ¯\_(ツ)_/¯ – Fabby Jul 1 '18 at 14:02
2

I wrote a script to monitor /sys/class/backlight/*/brightness and redirect it: How to switch between options in `/sys/class/backlight` to solve brightness problem?.

It can be quickly modified to your needs by adding three lines:

    # Modification for: https://askubuntu.com/questions/1051171/lowering-screen-max-brightness-with-ubuntu-18-04
    # If brightness > 50, set to 50
    [[ WatchPer -gt 50 ]] && WatchPer=50

Modified redirected-brightness script

#!/bin/bash

# NAME: redirect-brightness
# PATH: /usr/local/bin
# DESC: Redirect to correct driver when Ubuntu is adjusting the wrong
#       /sys/class/DRIVER_NAME/brightness

# DATE: June 13, 2018. Modified June 14, 2018.

# NOTE: Written for Ubuntu question:
#       https://askubuntu.com/q/1045624/307523

WatchDriver="/sys/class/backlight/intel_backlight"
PatchDriver="/sys/class/backlight/intel_backlight"

# Must be running as sudo
if [[ $(id -u) != 0 ]]; then
    echo >&2 "Root access required. Use: 'sudo redirect-brightness'"
    exit 1
fi

# inotifywait required
type inotifywait >/dev/null 2>&1 || \
    { echo >&2 "'inotifywait' required but it's not installed.  Aborting."; \
      echo >&2 "Use 'sudo apt install inotify-tools' to install it.'"; \
      exit 1; }

# Was right watch driver directory name setup correctly?
if [[ ! -d $WatchDriver ]]; then
    echo >&2 "Watch directory: '$WatchDriver'"; \
    echo >&2 "does not exist. Did you spell it correctly? Aborting.'"; \
    exit 1;
fi

# Was right patch driver directory name setup correctly?
if [[ ! -d $PatchDriver ]]; then
    echo >&2 "Redirect to directory: '$PatchDriver'"; \
    echo >&2 "does not exist. Did you spell it correctly? Aborting.'"; \
    exit 1;
fi

# Get maximum brightness values
WatchMax=$(cat $WatchDriver/max_brightness)
PatchMax=$(cat $PatchDriver/max_brightness)

# PARM: 1="-l" or "--log-file" then write each step to log file.
fLogFile=false
if [[ $1 == "-l" ]] || [[ $1 == "--log-file" ]]; then
    fLogFile=true
    LogFile=/tmp/redirect-brightness.log
    echo redirect-brightness LOG FILE > $LogFile
    echo WatchMax: $WatchMax PatchMax: $PatchMax >> $LogFile
fi

SetBrightness () {
    # Calculate watch current percentage
    WatchAct=$(cat $WatchDriver/actual_brightness)
    WatchPer=$(( WatchAct * 100 / WatchMax ))
    [[ $fLogFile == true ]] && echo WatchAct: $WatchAct WatchPer: $WatchPer >> $LogFile
    # Reverse engineer patch brightness to set
    # Modification for: https://askubuntu.com/questions/1051171/lowering-screen-max-brightness-with-ubuntu-18-04
    # If brightness > 50, set to 50
    [[ WatchPer -gt 50 ]] && WatchPer=50
    PatchAct=$(( PatchMax * WatchPer / 100 ))
    echo $PatchAct | sudo tee $PatchDriver/brightness
    [[ $fLogFile == true ]] && echo PatchAct: $PatchAct >> $LogFile
}

# When machine boots, set brightness to last saved value
SetBrightness

# Wait forever for user to press Fn keys adjusting brightness up/down.
while (true); do
    inotifywait --event modify $WatchDriver/actual_brightness
    [[ $fLogFile == true ]] && \
        echo "Processing modify event in $WatchDriver/actual_brightness" >> $LogFile
    SetBrightness
done
  • Holy Moly! Only saw this now! (After I received an upvote on mine) – Fabby Aug 16 '18 at 21:36
  • I apologize for the very very long delay, thank you kindly for your answer. It seems to fit perfectly ! – NMZ Aug 28 '18 at 14:02
2

This cannot be done the way you want to do this as this is a Linux Kernel Read-Only value:

There is no code that will read the value of max_brightness and act upon it: the kernel just publishes whatever the maximum value for your hardware is.

If you would try to force the kernel into still accepting writes, it will throw an Input/output error.

Maybe there is a way to:

  • Intercept the keypresses individually and then refuse to go higher, but that would mean posting a new question and referring back here as to why you'd want to do that.
  • ask Linus Torvalds himself to attend to your bug, but there's very little chance in him solving this in the near future... ;-)
  • 1
    Holy moly I just saw this now after reading your comment :) – WinEunuuchs2Unix Aug 16 '18 at 21:42

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.