Gih's Blog

只言片语

FreeBSD LCD屏幕亮度控制

2011-06-09 by gihnius, tagged as freebsd

在我的笔记本上,可以通过acpi_video.ko来控制屏幕亮度. 写了一个脚本lcd.sh,通过参数"lcd_up","lcd_down"来调节亮度,并用 osd_cat显示百分比信息.

## sysctl MIB of acpi_video
hw.acpi.reset_video: 1
hw.acpi.video.ext0.active: 0
hw.acpi.video.crt0.active: 0
hw.acpi.video.lcd0.active: 0
hw.acpi.video.lcd0.brightness: 70
hw.acpi.video.lcd0.fullpower: 100
hw.acpi.video.lcd0.economy: 90
hw.acpi.video.lcd0.levels: 100 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20
dev.acpi_video.0.%desc: ACPI video extension
dev.acpi_video.0.%driver: acpi_video
dev.acpi_video.0.%parent: vgapci0 

配置sudo,允许wheel组用户通过执行/etc/lcd.sh,调节屏幕亮度.

%wheel     ALL=NOPASSWD:   /etc/lcd.sh

我的FreeBSD LCD屏幕亮度控制脚本: 可能你需要修改脚本的获取和设置lcd信息的 MIB.

#!/bin/sh

action=$1
DISPLAY=:0.0

xosd_bar_font="-adobe-helvetica-bold-*-*-*-34-*-*-*-*-*-*-*"

lcd_down () {
    levels=$(sysctl -n hw.acpi.video.lcd0.levels)
    top=$(echo $levels | sed "s/\(^[0-9]\{2,3\}\).*/\1/")
    bot=$(echo $levels | sed "s/.* \([0-9]\{2,3\}\)$/\1/")
    curr=$(sysctl -n hw.acpi.video.lcd0.brightness)
    down=$curr

    if [ "$curr" -le "$top" ] && [ "$curr" -gt "$bot" ] ; then
        if [ "$down" -ne "$bot" ] ; then
            down=$(echo $levels | sed "s/.*$curr \([0-9]\{2,3\}\).*/\1/")
        fi
    fi

    sysctl hw.acpi.video.lcd0.brightness=$down
    killall osd_cat >/dev/null 2>&1
    osd_cat -p middle -A center -f $xosd_bar_font -c green -O 3 -d 1 -b percentage -P $down -T "LCD: $down %" &
}

lcd_up () {
    levels=$(sysctl -n hw.acpi.video.lcd0.levels)
    top=$(echo $levels | sed "s/\(^[0-9]\{2,3\}\).*/\1/")
    bot=$(echo $levels | sed "s/.* \([0-9]\{2,3\}\)$/\1/")
    curr=$(sysctl -n hw.acpi.video.lcd0.brightness)
    up=$curr

    if [ "$curr" -lt "$top" ] && [ "$curr" -ge "$bot" ] ; then
        up=$(echo $levels | sed "s/^\([0-9]\{2,3\}\) $curr.*/\1/" | sed "s/.*\($curr\).*/\1/")
        if [ "$up" -ne "$top" ] ; then
            up=$(echo $levels | sed "s/.*\([0-9]\{2,3\}\) $curr.*/\1/")
        fi
    fi

    sysctl hw.acpi.video.lcd0.brightness=$up
    killall osd_cat >/dev/null 2>&1
    osd_cat -p middle -A center -f $xosd_bar_font -c green -O 3 -d 1 -b percentage -P $up -T "LCD: $up %" &
}


## do
{
case $action in
    lcd_up)
        lcd_up
        ;;
    lcd_down)
        lcd_down
        ;;
    *)
        ;;
esac
} > /dev/null 

把脚本绑定到快捷键上, 我的fvwm配置:

Key XF86Forward A   A       Exec sudo /etc/lcd.sh lcd_up
Key XF86Back    A   A       Exec sudo /etc/lcd.sh lcd_down