如何以编程方式设置笔记本电脑屏幕亮度?

如何以编程方式设置笔记本电脑屏幕亮度?

我目前正在迁移到没有 gnome 会话的 openbox。在 unity 中,我可以使用供应商键来设置屏幕亮度,但在 openbox 中,我只能自己设置。

  • /sys/class/backlight/acpi_video0/brightness 工作正常,问题是我需要 sudo 来设置亮度,而这不适用于键盘映射。
  • xbacklight -get/set 不执行或输出任何操作。
  • 我真的不想使用 xrandr --brightness。

还有其他选项或方法可以解决 xbacklight 或 acpi_video0 的问题吗?

答案1

我的解决方案是添加以下行etc/rc.local

chmod a+w /proc/acpi/video/VID/LCD/brightness

我不知道/sys文件系统中的权限是否也可以更改。

答案2

GNOME 和 Unity 使用的命令是:

pkexec /usr/lib/gnome-settings-daemon/gsd-backlight-helper --set-brightness 5

它不需要运行 GNOME 会话,只需要 PolicyKit – 它几乎总是存在于 Ubuntu 中。


在键绑定中使用普通的sudo;可以通过编辑配置来运行某些命令而无需密码sudoers文件。例如:

zls     ALL=(root) NOPASSWD: /usr/lib/gnome-settings-daemon/gsd-backlight-helper

或者

zls     ALL=(root) NOPASSWD: /usr/local/bin/my-custom-brightness-script

答案3

我刚刚编写了一个符合 POSIX 的脚本来设置背光(很像 xbacklight 但直接操作 /sys/class/backlight/... ),它可以在 X 和控制台中运行。

该脚本有一个 --install 选项,当以 root 身份运行时,会设置 /sys/class/backlight/*/brigtness 的权限,以便指定的组可以对其进行写入,并且为了使其在重新启动后持续安装一个 systemd 服务,该服务会在每次系统启动时设置权限。

promt% backlight --help

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 it if it already exists.

请在 Githup 上查看,如果我做了任何更改,我会及时更新: https://github.com/fraxflax/backlight

现在它只是选择它找到并操作的第一个背光设备,如果你在 sys/class/backlight/ 中有多个文件夹,并且想要操作某个文件夹,你可能需要替换

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

类似于

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

以下是完整脚本:

#!/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 [email protected],
# 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
# -set and -dec 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 it 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
    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 && {
        grep -E "^$GRP:" /etc/group >/dev/null || USAGE "# group '$GRP' not found in /etc/group"
        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

相关内容