thinkpad(T42)热键支持和合盖待机的实现 (peterpannju发表,我稍微完善)

1.在/etc/X11/xorg.conf中加入DPMS


# **********************************************************************


Section "Monitor"

    Identifier "Generic Monitor"

    HorizSync 31.5 - 48.5
    VertRefresh 50-70
    Option    "DPMS"

EndSection

# **********************************************************************


Section "Device"
    Identifier "** ATI Radeon (generic) [radeon]"
    Driver "radeon"
    Option "DPMS" "true"
    Option "AGPSize" "32" # default:8

EndSection

2. rc.conf中启用相关的模块

ac button battery fan acpi_cpufreq cpufreq_ondemand thinkpad_acpi
注意别少了最后一个thinkpad_acpi。

2.5 实现自动降频:(第二步添加 acpi_cpufreq cpufreq_ondemand两个模块后)把一下加入/etc/rc.local
echo "ondemand" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

3. 安装acpid,acpi。

$sudo pacman -S acpid acpi

4. 编辑/etc/modprobe.conf,添加
options ibm_acpi hotkey=enable,0xffff experimental=1
    来指定加载模块时的参数,hotkey=enable,0xffff指明使用尽可能多的快捷键,experimental=1打开一些在测试中的功能,如 控制cpu风扇等,启用hotkey功能后,Fn+F4组合键将不产生sleep的acpi的事件,而是ibm/hotkey事件。acpid这个东西干 什么呢?ThinkWiki介绍说:
    Basically, acpid just executes scripts residing in /etc/acpi/actions. Which script to launch at which event is configured in several files in /etc/acpi/events. All actions are documented in /var/log/acpid.

5.编辑/etc/acpi/文件夹 ,让文件结构如下所示(如果没有则自己创建):
        | - apci
        | | - actions
        | | | - instWiFi.sh
        | | | - lcd_switch.sh
        | | | - sleep.sh
        | | - events
        | | | - anything
        | | - handler.sh

6.将acpi对ibm/hotkey事件的相应加入handler.sh:

ibm/hotkey)
                  case "$2" in
                        HKEY)
                        case "$4" in
                                00001003) #echo "f3" >/dev/tty5

                                        /etc/acpi/actions/lcd_switch.sh
                                        ;;
                                00001004) #echo "f4" >/dev/tty5

                                        /etc/acpi/actions/sleep.sh
                                        ;;
                                00001005) #echo "f7" >/dev/tty5

                                        /etc/acpi/actions/instWiFi.sh
                                        ;;
                                00001007) #echo "f7" >/dev/tty5

                                        echo video_switch > /proc/acpi/ibm/video
                                        ;;
                                00001008) #echo "f8" >/dev/tty5

                                        echo eject > /proc/acpi/ibm/bay
                                        ;;
                                0000100c) #echo "f12" >/dev/tty5

                                        /sbin/halt
                                        ;;
                        esac
                        ;;
                        *) logger "ibm_acpi undefined:$2";;
                 esac
                 ;;

7.编写actions文件夹下相对应的脚本如下:

lcd_switch.sh:
#!/bin/sh

        flag=/tmp/thinkpad_lcd_off
        if [ -e $flag ]; then
            /usr/sbin/radeontool light on
            rm -f $flag
        else
            /usr/sbin/radeontool light off >$flag
        fi

instWiFi.sh
#!/bin/sh
#echo Feel Freedom,using WiFi!

        if lsmod |grep -q 'ipw2200'
        then rmmod ipw2200 ieee80211 ieee80211_crypt;pkill dhcpcd
        else modprobe ipw2200;iwconfig eth1 essid default key 7db579d6f3;dhcpcd eth1
        fi

sleep.sh

#!/bin/sh

        # if launched through a lid event and lid is open, do nothing

        echo "$1" | grep "button/lid" && grep -q open /proc/acpi/button/lid/LID/state && exit 0
        # remove USB 1.1 driver

        rmmod uhci_hcd
        # sync filesystem and clock

        sync
        /sbin/hwclock --systohc
        # switch to console

        FGCONSOLE=`fgconsole`
        chvt 6
        /usr/sbin/radeontool light off
        # go to sleep

        echo -n "mem" > /sys/power/state
        # readjust the clock (it might be off a bit after suspend)

        /sbin/hwclock --adjust
        /sbin/hwclock --hctosys
        # reload USB 1.1 driver

        modprobe uhci_hcd
        # turn on the backlight and switch back to X

        radeontool light on
        chvt $FGCONSOLE

    可以看出,我另添加了三个脚本:
    --lcd_switch.sh提供了关闭屏幕的功能(需要radeontool的支持);
    --sleep.sh提供了休眠的功能,修改后的休眠脚本(需要radeontool的支持);
    --instWiFi.sh提供了无线连接和断开的功能(需要首先配置好无线:安装wireless_tools和ipw2200-fw,并且编译内核时提供了无线支持)。
    记得chmod +x /etc/acpi/actions/*

8.安装radeontool, [url]http://www.thinkwiki.org/wiki/Radeontool[/url]
PKGBUILD:
pkgname=radeontool
pkgver=1.5
pkgrel=2
pkgdesc=
url="http://fdd.com/software/radeon/"
license=GPL
depends=('pciutils')
arch=("i686" "x86_64")
source=( http://fdd.com/software/radeon/$pkgname-$pkgver.tar.gz)
md5sums=(8065eebe5a2b163e43b40461bfe49a56)

build() {
        cd $startdir/src/$pkgname-$pkgver
        make || return 1
        mkdir -p $startdir/pkg/usr/sbin
        cp $pkgname $startdir/pkg/usr/sbin
}

9.$/etc/rc.d/acpid restart,然后就可以看看各个热键是否起作用了。

10.IBM ThinkPad 合上屏幕休眠的设置:
---------------------------------------------
 
    IBM休眠的快捷方式有两个:
        1). 按Fn+F4
        2). 合上机盖
    分别在handler.sh上对应:
        1).    ibm/hotkey HKEY 00000080 00001004
        2).    ibm/hotkey HKEY 00000080 00005001  #ThinkPad对笔记本加上的热键,只有thinkpad会产生
              button/lid LID 00000080 00000007     #正常合上机盖所产生的信号,适合于所有的笔记本
    于是,对于情况2),我们在handler.sh中加入相应的处理方法:

...
        button/lid)
        #echo "LID switched!">/dev/tty5

            /etc/acpi/actions/sleep.sh #添加这句

            ;;
        ...

    对于情况1),就是前面的步骤
           
11.原理:
--------------------------------------
 
    我们来看看acpid的工作原理:
  
    1). 首先,我们按下某热键,此处我们举例Fn+F4 ;
  
    2). acpid监听并截获这个按键组合,我们可以用acpi_listen命令来手动查看不同按键组合产生不同的信号:
        $ acpi_listen
        ibm/hotkey HKEY 00000080 00001004 #这是我的机器上Fn+F4产生的中断信号
  
    3). acpid在/ect/acpi/events/下寻找对应的处理方案,我的该文件夹下面只有一个文件:
        $ cat anything
        event=.*
        action=/etc/acpi/handler.sh %e
    NOTE: 在这里,我偷了一个懒。我将所有的信号(前面acpi_listen所监听到的)一股脑儿抛给了handler.sh,然后在handler.sh里进行分析处理。其实,你可以创建自己的处理方案,比如,
        $ vim /etc/acpid/events/sleep
        event=ibm/hotkey HKEY 00000080 00001004  #ThinkPad Fn+F4产生的中断信号
        action=/etc/acpi/actions/sleep.sh  #遇到这个中断信号则执行该脚本
  
    4). handler.sh接收到处理信号后,首先进行分析:
        set $*
            case "$1" in
                ibm/hotkey)
                    case "$2" in
                        HKEY)
                          case "$4" in
                             00001004) #echo "f4" >/dev/tty5,终于分析完Fn+F4信号了
                                        /etc/acpi/actions/sleep.sh  #激活相应事件处理程序
                                        ;;
        ...
        NOTE: 其实整个事件过程可以在上面第三步用NOTE中的方法代替。但是这样做的好处是将所有的处理方法都放到一起,便于编辑(就是喜欢ArchLinux的这种风格,在系统启动脚本/etc/rc.*上尤其明显)。
 
    5). /etc/acpi/actions/sleep.sh被执行。
  
Links:
-------------------------------------------
 
    1. 笔记本上使用ArchLinux:
    [url]http://www.archlinux.org.cn/main/?q=node/76#sec-4[/url]
    2.  How to configure acpid:
    [url]http://www.thinkwiki.org/wiki/How_to_configure_acpid#Action_Script[/url]
    3. Radeontool:
    [url]http://www.thinkwiki.org/wiki/Randontool[/url]
    4. Debian GNU/Linux on IBM T41P
    [url]http://pthichat.net/index.php/2004/11/01/22-debian-gnulinux-on-ibm-t41p[/url]
    5. Debian + IBM-R40(迅驰)
    [url]http://www.linuxsir.org/bbs/showthread.php?t=238459[/url]

--------------
附上完整的/etc/acpi/handler.sh

#!/bin/sh

# Default acpi script that takes an entry for all actions


# NOTE: This is a 2.6-centric script. If you use 2.4.x, you'll have to

# modify it to not use /sys


minspeed=`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq`
maxspeed=`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq`
setspeed="/sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed"

set $*

case "$1" in
    button/power)
        #echo "PowerButton pressed!">/dev/tty5

        case "$2" in
            PWRF)    logger "PowerButton pressed: $2" ;;
            *) logger "ACPI action undefined: $2" ;;
        esac
        ;;
    button/sleep)
        case "$2" in
            SLPB) echo -n mem >/sys/power/state ;;
            *) logger "ACPI action undefined: $2" ;;
        esac
        ;;
    ac_adapter)
        case "$2" in
            AC)
                case "$4" in
                    00000000)
                        echo -n $minspeed >$setspeed
                        #/etc/laptop-mode/laptop-mode start

                    ;;
                    00000001)
                        echo -n $maxspeed >$setspeed
                        #/etc/laptop-mode/laptop-mode stop

                    ;;
                esac
                ;;
            *) logger "ACPI action undefined: $2" ;;
        esac
        ;;
    battery)
        case "$2" in
            BAT0)
                case "$4" in
                    00000000)    #echo "offline" >/dev/tty5

                    ;;
                    00000001)    #echo "online" >/dev/tty5

                    ;;
                esac
                ;;
            CPU0)    
                ;;
            *) logger "ACPI action undefined: $2" ;;
        esac
        ;;

    button/lid)
        #echo "LID switched!">/dev/tty5

        /etc/acpi/actions/sleep.sh
        ;;

         ibm/hotkey)
                  case "$2" in
                        HKEY)
                        case "$4" in
                                00001003) #echo "f3" >/dev/tty5

                                        /etc/acpi/actions/lcd_switch.sh
                                        ;;
                                00001004) #echo "f4" >/dev/tty5

                                        /etc/acpi/actions/sleep.sh
                                        ;;
                                00001005) #echo "f7" >/dev/tty5

                                        /etc/acpi/actions/instWiFi.sh
                                        ;;
                                00001007) #echo "f7" >/dev/tty5

                                        echo video_switch > /proc/acpi/ibm/video
                                        ;;
                                00001008) #echo "f8" >/dev/tty5

                                        echo eject > /proc/acpi/ibm/bay
                                        ;;
                                0000100c) #echo "f12" >/dev/tty5

                                        /sbin/halt
                                        ;;
                        esac
                        ;;
                        *) logger "ibm_acpi undefined:$2";;
                 esac
                 ;;
    *)
        logger "ACPI group/action undefined: $1 / $2"
        ;;

esac

----完----

peterpannju的原文请查看
[url]http://peterpannju.blogbus.com/logs/7173241.html[/url]

你可能感兴趣的:(职场,ThinkPad,热键,休闲)