LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 05-31-2023, 08:59 PM   #1
Ansen
LQ Newbie
 
Registered: Oct 2022
Posts: 17

Rep: Reputation: 0
what's the way to down/up the backlight by ubuntu


I found that it is different to low down the brightness of a backlight between in debian and ubuntu.

In debian, it's to set the value of /sys/class/backlight/intel_backlight/brightness to change the brightness of a backlight. And the value of /sys/class/backlight/intel_backlight/brightness is same as /sys/class/backlight/intel_backlight/actual_brightness for ever.

But in ubuntu, the value is different between /sys/class/backlight/intel_backlight/brightness and /sys/class/backlight/intel_backlight/actual_brightness when the brightness is low down by os not by a user.

So what is the way in ubuntu?
I have disabled the acpi's by "echo 0 > /sys/module/video/parameters/brightness_switch_enabled". And it doesn't look like work.
 
Old 05-31-2023, 09:24 PM   #2
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,987
Blog Entries: 28

Rep: Reputation: 6371Reputation: 6371Reputation: 6371Reputation: 6371Reputation: 6371Reputation: 6371Reputation: 6371Reputation: 6371Reputation: 6371Reputation: 6371Reputation: 6371
This article might help: https://help.ubuntu.com/community/Backlight
 
1 members found this post helpful.
Old 06-15-2023, 07:36 AM   #3
fraxflax
LQ Newbie
 
Registered: Jan 2018
Location: Sweden
Distribution: Debian
Posts: 24

Rep: Reputation: 5
Quote:
Originally Posted by frankbell View Post
I wrote a similar script, but inspired by xbacklight, using % of MAX-brightness instead of single brightness steps.
Also it does not require to be run as root (no need for the sudo as in the referred to article) instead it allows you to run it once as root to set up permissions for a group to be able manipulate the brightness, and remembers it over reboots by adding a systemd service setting it up at each system startup:

backlight --help
Code:
USAGE:
    backlight [ -g | --get ]
       Get the current brightness value in percent of MAX brightness.

    backlight { -s | --set | -i | --increase | -d | --decrease } percent
       Set the brightness to 'percent' of max brightness or change it
       (increase / decrease) 'percent'. 'percent' is a integer with or
       without a trailing % sign.  Even though -s 0 will usually give
       a darker screen than -s 1, these options will never completely
       turn the brightness off as it might have unexpected
       consequences (see -off).

    backlight { -0 | -o | --off }
       Set the brightness to 0 (on some systems, 'xset dpms force off'
       also sets the brightness to 0 so it may make 'xset dpms force
       on' unexpectantly turn up the brightness under some
       circumstances).

    backlight --install [ groupname ]
       Allow the specified group (or the video group if none
       specified) to set the brighness forth on.

       Must be run by root.

       If systemd is in use 'backlight --install' will also install,
       enable and run a systemd service making 'brightness' writeable
       by the specified group at system startup.

       'backlight --install' will replace the
       /etc/systemd/system/backlightgroup.service if it already exists.
The full script can be found in github: https://github.com/fraxflax/backlight
where I keep it up to date if/when I do changes.

It's on my TODO-list to have it support multiple backlight devices, but right now it only picks the first one it finds, so if you have several folders under /sys/class/backlight you might want to change the "SYS="-line somewhat like this:
Code:
#SYS="/sys/class/backlight/`ls -1 /sys/class/backlight/ | head -1`"
SYS=/sys/class/backlight/intel_backlight
Here's the full script:
Code:
#!/bin/sh
# 
# backlight is a POSIX-compliant (pure bourne shell) script that
# allows you to get and set the brightness of the screen via
# /sys/backlight if supported by your system.
# 
# This is free software created by frax@axnet.nu,
# feel free to use, modify and/or distribute as you see fit.
# 
# TODO:
# - Handle multiple backlight devices 
#   (currently only the first one found is used)
#

# Set LOWEST to 0 to be able to completely turn the backlight off with
# -s and -d or to a higher value (e.g. 1) to prevent turning the
# light completely off as this might be considered turning the screen
# off and "xset dpms force on" on some systems will turn up the light
# if brighness is 0.
LOWEST=1

SYS="/sys/class/backlight/`ls -1 /sys/class/backlight/ | head -1`"

USAGE() {
    backlight=`basename $0` || backlight=backlight
    test -n "$1" && /bin/echo -e "\n###\n$@\n###"
    cat>/dev/stderr<<EOF

USAGE:
    $backlight [ -g | --get ]
       Get the current brightness value in percent of MAX brightness.

    $backlight { -s | --set | -i | --increase | -d | --decrease } percent
       Set the brightness to 'percent' of max brightness or change it
       (increase / decrease) 'percent'. 'percent' is a integer with or
       without a trailing % sign.  Even though -s 0 will usually give
       a darker screen than -s 1, these options will never completely
       turn the brightness off as it might have unexpected
       consequences (see -off).

    $backlight { -0 | -o | --off }
       Set the brightness to 0 (on some systems, 'xset dpms force off'
       also sets the brightness to 0 so it may make 'xset dpms force
       on' unexpectantly turn up the brightness under some
       circumstances).

    $backlight --install [ groupname ]
       Allow the specified group (or the video group if none
       specified) to set the brighness forth on.

       Must be run by root.

       If systemd is in use '$backlight --install' will also install,
       enable and run a systemd service making 'brightness' writeable
       by the specified group at system startup.

       '$backlight --install' will replace the
       /etc/systemd/system/backlightgroup.service if it already exists.
EOF
        exit 1
}

[ -e "$SYS/brightness" -a -e "$SYS/max_brightness" ] || USAGE "# No supported backlight system in /sys/class/backlight"

[ "--install" = "$1" ] && {
    test `id -u` -eq 0 || USAGE "# 'backlight -install' must be run as root"
    GRP=video
    test -n "$2" && GRP=$2
    grep -E "^$GRP:" /etc/group >/dev/null || USAGE "# group '$GRP' not found in /etc/group"
    chgrp $GRP /sys/class/backlight/*/brightness ; chmod g+w /sys/class/backlight/*/brightness
    echo
    ls -l /sys/class/backlight/*/brightness
    echo
    test -d /etc/systemd/system && {
        cat>/etc/systemd/system/backlightgroup.service<<EOF
[Unit]
Description=Permission for group $GRP to set backlight brightness
After=syslog.target

[Service]
Type=oneshot
ExecStart=/bin/sh -c '/bin/chgrp $GRP /sys/class/backlight/*/brightness ; /bin/chmod g+w /sys/class/backlight/*/brightness'
RemainAfterExit=no

[Install]
WantedBy=sysinit.target
EOF
        systemctl enable backlightgroup.service || USAGE "# failed to enable the backlightgroup.service"
        systemctl start backlightgroup.service || USAGE "# failed to start the backlightgroup.service"
        systemctl status --no-pager backlightgroup.service
    }
    exit 0
}
MAX=`cat "$SYS/max_brightness"`
CUR=`cat "$SYS/brightness"`

[ "-g" = "$1" -o "--get" = "$1" -o -z "$1" ] && {
    echo $(($CUR*100/$MAX))%
    exit 0
}

if [ "-0" = "$1" -o "-o" = "$1" -o "--off" = "$1" ]; then
    NVAL=0
    LOWEST=0
else 
    VAL=`echo $2 | sed 's/[^0-9]*\([0-9][0-9]*\).*/\1/'` # just keep the first found consecutive digits
    ISNUM=`echo $VAL | sed 's/[0-9]*//'` # if there was no digits at all this will be nonempty
    test -z "$VAL" -o -n "$ISNUM" && USAGE

    OP=$1    
    case "$OP" in
        -s|--set)
            NVAL=$(($VAL*$MAX/100))
            ;;
        -i|--increase)
            NVAL=$(($CUR+$VAL*$MAX/100))
            ;;
        -d|--decrease)
            NVAL=$(($CUR-$VAL*$MAX/100))
            ;;
        *)
            USAGE
            ;;
    esac
fi
test $NVAL -gt $MAX && NVAL=$MAX
test $NVAL -lt $LOWEST && NVAL=$LOWEST 
echo $NVAL > "$SYS/brightness"

#echo $(($NVAL*100/$MAX))"% ($NVAL/$MAX)" >/dev/stderr
exit 0

Last edited by fraxflax; 06-15-2023 at 07:44 AM. Reason: bugfix: checking that grp exists before doing chgrp not after :-)
 
Old 06-15-2023, 05:24 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243
just adjust the script to add a value when you run it. you should check out the files in that directory to find max setting first.

/sys/class/backlight

then figure out your subdir that holds the files.

Code:
#!/usr/bin/env bash
echo 255 | sudo tee /sys/class/backlight/amdgpu_bl0/brightness
or you can use that one in post #3 for sysD

Last edited by BW-userx; 06-15-2023 at 05:26 PM.
 
1 members found this post helpful.
Old 06-24-2023, 12:27 PM   #5
fraxflax
LQ Newbie
 
Registered: Jan 2018
Location: Sweden
Distribution: Debian
Posts: 24

Rep: Reputation: 5
Updated version of the script primarily to handle multiple backlight-devices, but also nicer, more man-pager-like, formatted USAGE output and more ways, to specify the operations (including the ugly '-get' '-set' '-dec' & '-inc' for xbacklight compaility):

backlight --help
Code:
SYNOPSIS:
    backlight [ OPTIONS ] [ -g | --get | -get ]
       Get the current brightness value in percent of MAX brightness.

    backlight [ OPTIONS ] [ -s | --set | -set ] percent
       Set the brightness to 'percent' of max brightness.  'percent' is an integer (anything trailing the first consecutive digits is ignored (e.g. a %-sign),
       allowing you to use the --get output as --set input without modification).

    backlight [ OPTIONS ] { -i | --increase | -inc } step
    backlight +step
       Increase the brightness by step percent units of max brightnes.

    backlight [ OPTIONS ] { -d | --decrease | -dec } step
    backlight -step
       Increase the brightness by step percent units of max brightnes.

    backlight [ OPTIONS ] { -o | --off }
        Set the brightness to 0 (turn the backlight off).
       -s and -d will never completely turn the brightness off, as it might have the consequence that when running 'xset dpms force
       on' the brightness will unexpectedly be increased.


    backlight --install [ groupname ]
       Allow the specified group (or the video group if none specified) to set the brighness forth on.

       If systemd is in use 'backlight --install' will also install, enable and run a systemd service making 'brightness' writeable by the specified
       group at system startup.

       'backlight --install' will replace the /etc/systemd/system/backlightgroup.service if it already exists.

       'backlight --install' must be executed with effective user-id 0 (run as root / with sudo).

OPTIONS:
     --sysfs [ first | /sys/class/backlight/device | all ]

    backlight --sysfs first ...
        Use the first device found in /sys/class/backlight/ . (This is the default.)

    backlight --sysfs /sys/class/backlight/intel_backlight ...
        Use the specified backlight device, e.g. intel_backlight.

    backlight --sysfs all ...
        Do the operation for all backlight devices found in /sys/class/backlight/ .


EXAMPLES:
    Get current backlight brightness:
    backlight --get    

    Set backlight brightness to 75%:
    backlight 75    

    Increase backlight brightness with 5%-units:
    backlight +5%    

    Decrease backlight brightness with 5%-units:
    backlight -d 5    

    Turn screen off (set brightness to 0) for all backlight devices.
    backlight --sysfs all --off    

    Flash the screen, restoring the backlight brightness afterwards:
    B=$(backlight); backlight 0 ; sleep 0.5 ; backlight 100 ; sleep 0.5 ; backlight 0 ; sleep 0.5 ; backlight $B
The source code is available at githup, so check that out for the latest version:
https://github.com/fraxflax/backlight

Here's the full script:
Code:
#!/bin/sh
# 
# https://github.com/fraxflax/backlight
# 
# backlight is a POSIX-compliant (pure bourne shell) script that
# allows you to get and set the brightness of the screen via
# /sys/backlight if supported by your system.
# 
# This is free software created by frax@axnet.nu,
# feel free to use, modify and/or distribute as you see fit.
# 

# Set LOWEST to 0 to be able to completely turn the backlight off with
# -s and -d or to a higher value (e.g. 1) to prevent turning the
# light completely off as this might be considered turning the screen
# off and "xset dpms force on" on some systems will turn up the light
# if brighness is 0.
LOWEST=1

backlight=`basename $0` || backlight=backlight
USAGE() {
    COLS=72; ___=''; __=''; which tput>/dev/null && { COLS=`tput cols`; ___=`tput bold`; __=`tput sgr0`; }
    FMT=cat;which fmt>/dev/null && FMT="fmt -w $COLS"
    [ -n "$1" ] && /bin/echo -e "\n###\n$@\n###" >/dev/stderrr
$FMT>/dev/stderr<<EOF

${___}SYNOPSIS:
    $backlight$__ [ OPTIONS ] [ $___-g$__ | $___--get$__ | $___-get$__ ]
       Get the current brightness value in percent of MAX brightness.
$___
    $backlight$__ [ OPTIONS ] [ $___-s$__ | $___--set$__ | $___-set$__ ] percent
       Set the brightness to 'percent' of max brightness.  'percent'
       is an integer (anything trailing the first consecutive digits
       is ignored (e.g. a %-sign), allowing you to use the --get
       output as --set input without modification).
$___
    $backlight$__ [ OPTIONS ] { $___-i$__ | $___--increase$__ | $___-inc$__ } step
${___}    $backlight$__ +step
       Increase the brightness by step percent units of max brightnes.
$___
    $backlight$__ [ OPTIONS ] { $___-d$__ | $___--decrease$__ | $___-dec$__ } step
${___}    $backlight$__ -step
       Increase the brightness by step percent units of max brightnes.
$___
    $backlight$__ [ OPTIONS ] { $___-o$__ | $___--off$__ }
        Set the brightness to 0 (turn the backlight off).
       $___-s$__ and $___-d$__ will never completely turn the brightness
       off, as it might have the consequence that when running '${___}xset dpms force on$__' the brightness will unexpectedly be increased.

$___
    $backlight --install$__ [ groupname ]
       Allow the specified group (or the video group if none
       specified) to set the brighness forth on.

       If systemd is in use '$___$backlight --install$__' will also install,
       enable and run a systemd service making 'brightness' writeable
       by the specified group at system startup.

       '$___$backlight --install$__' will replace the
       /etc/systemd/system/backlightgroup.service if it already exists.

       '$___$backlight --install$__' must be executed with effective user-id 0 (run as root / with sudo).

${___}OPTIONS:
     --sysfs$__ [ ${___}first$__ | /sys/class/backlight/device | ${___}all$__ ]

${___}    backlight --sysfs first$__ ...
        Use the first device found in /sys/class/backlight/ . (This is the default.)

${___}    backlight --sysfs /sys/class/backlight/intel_backlight$__ ...
        Use the specified backlight device, e.g. intel_backlight.

${___}    backlight --sysfs all$__ ...
        Do the operation for all backlight devices found in /sys/class/backlight/ .


${___}EXAMPLES:$__
        Get current backlight brightness:
${___}    $backlight --get    $__

        Set backlight brightness to 75%:
${___}    $backlight 75    $__

        Increase backlight brightness with 5%-units:
${___}    $backlight +5%    $__

        Decrease backlight brightness with 5%-units:
${___}    $backlight -d 5    $__

        Turn screen off (set brightness to 0) for all backlight devices.
${___}    $backlight --sysfs all --off    $__

        Flash the screen, restoring the backlight brightness afterwards:
${___}    B=\$($backlight); $backlight 0 ; sleep 0.5 ; $backlight 100 ; sleep 0.5 ; $backlight 0 ; sleep 0.5 ; $backlight \$B     $__

EOF
        exit 1
}


if [ "--sysfs" = "$1" ]; then
    if [ "all" = "$2" ]; then
        sysfs="`echo /sys/class/backlight/*`"
    elif [ -d "$2" -a -e "$2/brightness" -a -e "$2/max_brightness" ]; then
        sysfs="$2"
    elif ["first" = "$2" ]; then
        sysfs="/sys/class/backlight/`ls -1 /sys/class/backlight/ | head -1`"
    else
        USAGE "# unsupported --sysfs device"
    fi
    OP=$3
    VAL=$4
else
    OP=$1
    VAL=$2
    sysfs="/sys/class/backlight/`ls -1 /sys/class/backlight/ | head -1`"
    [ -e "$sysfs/brightness" -a -e "$sysfs/max_brightness" ] || USAGE "# No supported backlight system in /sys/class/backlight"
fi


[ "--install" = "$OP" ] && {
    [ `id -u` -eq 0 ] || USAGE "# '$backlight --install' must be run as root"
    GRP=video
    [ -n "$VAL" ] && GRP=$VAL
    grep -E "^$GRP:" /etc/group >/dev/null || USAGE "# group '$GRP' not found in /etc/group"
    chgrp $GRP /sys/class/backlight/*/brightness ; chmod g+w /sys/class/backlight/*/brightness
    echo
    ls -l /sys/class/backlight/*/brightness
    echo
    [ -d /etc/systemd/system ] || { echo "WARNING '/etc/systemd/system/' does not exists, systemd backlightgroup.service not installed."; exit 0;}
    cat>/etc/systemd/system/backlightgroup.service<<EOF
[Unit]
Description=Permission for group $GRP to set backlight brightness
After=syslog.target

[Service]
Type=oneshot
ExecStart=/bin/sh -c '/bin/chgrp $GRP /sys/class/backlight/*/brightness ; /bin/chmod g+w /sys/class/backlight/*/brightness'
RemainAfterExit=no

[Install]
WantedBy=sysinit.target
EOF
    systemctl enable backlightgroup.service || USAGE "# failed to enable the backlightgroup.service"
    systemctl start backlightgroup.service || USAGE "# failed to start the backlightgroup.service"
    systemctl status --no-pager backlightgroup.service
    exit 0
}


for SYS in $sysfs; do
    #echo "SYS:$sysfs" >/dev/stderr
    [ -e "$SYS/brightness" -a -e "$SYS/max_brightness" ] && {
        MAX=`cat "$SYS/max_brightness"`
        CUR=`cat "$SYS/brightness"`

        [ "-g" = "$OP" -o "--get" = "$1" -o "-get" = "$1" -o -z "$1" ] && {
            echo $(($CUR*100/$MAX))%
            exit 0
        }

        [ "-o" = "$OP" -o "--off" = "$1" ] && {
            echo 0 > "$SYS/brightness"
            exit 0;
        }

        [ -z "$VAL" ] && {
            VAL=$OP
            echo $OP | grep -E '^(-|\+)' >/dev/null || OP="-s"
        }
        VAL=`echo $VAL | sed -E 's/([^0-9]*)([0-9][0-9]*).*/\2/'` # just keep the first found consecutive digits
        ISNUM=`echo $VAL | sed 's/[0-9]*//'` # if there was no digits at all this will be nonempty

        [ -z "$VAL" -o -n "$ISNUM" ] && USAGE
        #echo "OP:$OP VAL:$VAL ISNUM:$ISNUM" >/dev/stderr
    
        case "$OP" in
            -s|--set|-set)
                NVAL=$(($VAL*$MAX/100))
                ;;
            -i|--increase|-inc|+[0-9]*)
                NVAL=$(($CUR+$VAL*$MAX/100))
                ;;
            -d|--decrease|-dec|-[0-9]*)
                NVAL=$(($CUR-$VAL*$MAX/100))
                ;;
            *)
                USAGE
                ;;
        esac
        
        [ $NVAL -gt $MAX ] && NVAL=$MAX
        [ $NVAL -lt $LOWEST ] && NVAL=$LOWEST 
        echo $NVAL > "$SYS/brightness"
        
        #echo $(($NVAL*100/$MAX))"% ($NVAL/$MAX)" >/dev/stderr
    }
done
exit 0

Last edited by fraxflax; 06-24-2023 at 01:20 PM. Reason: usage typo
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get LCD monitor backlight OFF with xlock jonr Mandriva 6 06-24-2009 10:59 AM
ACPI: edit lid.sh to blank screen, turn backlight off, no-suspend ????? Outabux Debian 5 11-15-2004 08:11 AM
[ibook G4] Screen backlight always on kemkem Linux - Laptop and Netbook 1 11-14-2004 12:49 PM
Backlight turns off, Suspend/Resume fixes but looses USB nickI-S Linux - Laptop and Netbook 0 09-04-2004 06:28 AM
LCD backlight does not turn off on console (acer travelmate 291LMi) zen_guerrilla Linux - Laptop and Netbook 0 07-16-2004 11:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 07:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration