头图

众所周知,i3WM可以通过在${HOME}/.config/i3/config中配置下述内容并添加light实现亮度调节

# Screen brightness controls
bindsym XF86MonBrightnessUp exec --no-startup-id light -A 5
bindsym XF86MonBrightnessDown exec --no-startup-id light -U 5

可本Arch用户发现...按Fn+F5/F6(屏幕亮度+/-)无反应,把light -A 5light -U 5拿到bash下执行也没反应...单独执行light倒是可以显示当前屏幕亮度(百分数,保留两位小数)。

light本质上是通过更改/sys/class/backlight/intel_backlight/brightness数值实现屏幕亮度更改的。那末,可不可以通过Shell自己写一个呢?
已知屏幕亮度100%时,brightness数值为852,其数值为0时,屏幕熄灭。
通过简单编程,得到以下脚本(将其置于${HOME}/.config/i3/brtctl):

#!/usr/bin/bash
tp11e_brtns="/sys/class/backlight/intel_backlight/brightness"
stp=$2
if [ -z "$stp" ];then
        echo "brtns U|D STEP_NUM"
        echo "brightness: $(cat $tp11e_brtns)/852"
        exit 0
fi
case $1 in
        U)
                upbrt=$(($(cat $tp11e_brtns)+stp))
                if [ $upbrt -lt 852 ];then
                        echo $upbrt > $tp11e_brtns
                else
                        echo 852 > $tp11e_brtns
                        echo "out of range, set the value to 852"
                fi
                ;;
        D)
                dnbrt=$(($(cat $tp11e_brtns)-stp))
                if [ $dnbrt -gt 0 ];then
                        echo $dnbrt > $tp11e_brtns
                else
                        echo 0 > $tp11e_brtns
                        echo "out of range, set the value to 0"
                fi
                ;;
        *)
                echo "brtns U|D STEP_NUM"
                ;;
esac

用法:

$ chmod +x brtctl
# 赋可执行权限
$ ./brtctl U 5
# 亮度在原有基础上+5
$ ./brtctl D 5
# 亮度在原有基础上-5

-5/+5不在0-852范围内则提示超限,并将亮度值定为0或852。

结果并不能改变亮度呢,看下权限罢。

$ ls -lah /sys/class/backlight/intel_backlight/brightness
-rw-r--r-- 1 root root 4.0K Jul 16 08:01 /sys/class/backlight/intel_backlight/brightness

果然是root组root用户呢。

通过sudo chown ${USER} $tp11e_brtnssudo chgrp ${USER} $tp11e_brtns取得权限以后,不光是自己的脚本,light都管用了。原来问题出在权限上,那末我们写一个expect脚本来实现权限提取(事实上并不推荐这样做)。
(文件位于${HOME}/.config/i3/brt_pget.exp)

#!/usr/bin/expect
set username [lindex $argv 0]
set password [lindex $argv 1]
set command [lindex $argv 2]
set sys_bright_conf /sys/class/backlight/intel_backlight/brightness
spawn sudo $command $username $sys_bright_conf
expect "*$username*" {send "$password\r"}
expect eof

用另一个Shell去操作它(文件位于${HOME}/.config/i3/env_init.sh)

#!/usr/bin/bash
i3_root="${HOME}"/.config/i3
brt_pget="$i3_root"/brt_pget.exp
username=$USER
password="PASSWORD"

for command in {chown,chgrp};do
    $brt_pget "$username" "$password" "$command"
done

再通过在${HOME}/.config/i3/config中添加下述内容,实现进入桌面即启动

# Init setup
exec --no-startup-id ${HOME}/.config/i3/env_init.sh

并用自己的brtctl替换掉light

# Screen brightness controls
# bindsym XF86MonBrightnessUp exec --no-startup-id light -A 5
# bindsym XF86MonBrightnessDown exec --no-startup-id light -U 5
bindsym XF86MonBrightnessUp exec --no-startup-id ${HOME}/.config/i3/brtctl U 5
bindsym XF86MonBrightnessDown exec --no-startup-id ${HOME}/.config/i3/brtctl D 5

觉得亮度调节太慢可适当将5替换成更大的数,觉得太快,则替换为更小的数~至此,你获得了相较light更加丝滑的亮度调节方案。

本文基于CC BY-NC-SA 4.0协议开源。


adamlz
1 声望0 粉丝

if[ $(date +%s) == 0 ];then echo "Hello World";fi