AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 980997
Accepted
pa4080
pa4080
Asked: 2017-11-28 23:37:47 +0800 CST2017-11-28 23:37:47 +0800 CST 2017-11-28 23:37:47 +0800 CST

当盖子被扭曲或关闭时,如何禁用触摸板?

  • 772

我有 Ubuntu 16.04 的联想 ThinkPad X230 平板电脑。它有一个可转换的屏幕,当它处于平板电脑模式时,触摸板仍然处于活动状态并弄得一团糟。

我创建了以下脚本并将其绑定到内置按钮之一(通过自定义快捷方式):

#!/bin/bash -e

# Find the TouchPad device ID
ID="$(xinput | grep -ioP 'touchpad.*id=\K[0-9]*')"                                  

if   [ "$(LANG=C xinput --list-props "$ID" | awk 'NR==2{print $4}')" == "0" ]; then 
        # If the device is disabled, then enable it and kill 'onboard' virtual keyboard
        xinput enable "$ID"; killall onboard; xrandr -o normal
elif [ "$(LANG=C xinput --list-props "$ID" | awk 'NR==2{print $4}')" == "1" ]; then
        # If the device is enabled, then disable it and run 'onboard' virtual keyboard
        xinput disable "$ID"; nohup onboard >/dev/null 2>&1 &
fi

该脚本工作正常,但这是一个虚假的解决方案,昨天我花了几个小时来学习如何以正确的方式做到这一点。所以我决定在这里分享这个经验。

command-line
  • 3 3 个回答
  • 2808 Views

3 个回答

  • Voted
  1. Best Answer
    pa4080
    2017-11-28T23:41:07+08:002017-11-28T23:41:07+08:00

    要检查设备是否处于平板电脑模式,我们可以读取以下值(0或1):

    /sys/devices/platform/thinkpad_acpi/hotkey_tablet_mode
    

    该值由特定事件切换。我们可以捕获这些事件,并可以使用acpid高级配置和电源接口事件守护程序将脚本绑定到它们。


    1.抓住事件。执行acpi_listen或netcat -U /var/run/acpid.socket,在平板模式下转动盖子,然后将其转回。这是一个示例输出:

    $ acpi_listen
    video/tabletmode TBLT 0000008A 00000001
    video/tabletmode TBLT 0000008A 00000000
    

    请注意,当盖子关闭/打开时,结果会有所不同:

    $ acpi_listen
    button/lid LID close
    button/lid LID open
    

    2.配置acpid识别设备模式改变触发的事件。将以下行作为(单个)命令运行到终端中:

    cat << EOF | sudo tee /etc/acpi/events/thinkpad-tablet-enabled
    # /etc/acpi/events/thinkpad-tablet-enabled
    # This is called when the lid is placed in tablet position on
    # Lenovo ThinkPad X230 Tablet
    
    event=video/tabletmode TBLT 0000008A 00000001
    action=/etc/acpi/thinkpad-touchpad-twist-mode.sh 1
    EOF
    
    cat << EOF | sudo tee /etc/acpi/events/thinkpad-tablet-disabled
    # /etc/acpi/events/thinkpad-tablet-disabled
    # This is called when the lid is placed in normal position on
    # Lenovo ThinkPad X230 Tablet
    
    event=video/tabletmode TBLT 0000008A 00000000
    action=/etc/acpi/thinkpad-touchpad-twist-mode.sh 0
    EOF
    

    上述命令将创建文件:

    • /etc/acpi/events/thinkpad-tablet-enabled
    • /etc/acpi/events/thinkpad-tablet-disabled

    注意:此处不提供盖子打开/关闭的脚本。但它们与上述类似。


    3.重新启动acpid,以便它可以重新读取事件过滤器,包括您刚刚添加的过滤器:

    sudo systemctl restart acpid.service
    

    4./etc/acpi/thinkpad-touchpad-in-twist-mode.sh创建将禁用1和启用0触摸板的脚本(&&使其可执行):

    cat << EOF | sudo tee /etc/acpi/thinkpad-touchpad-twist-mode.sh && sudo chmod +x /etc/acpi/thinkpad-touchpad-twist-mode.sh
    #!/bin/sh
    LANG=C                                                                                                        # Ensure stable parsing
    export DISPLAY="\$(w | awk 'NF > 7 && \$2 ~ /tty[0-9]+/ {print \$3; exit}' 2>/dev/null)"                      # Get and export the current user's \$DISPAY
    export XAUTHORITY="/home/\$(w | awk 'NF > 7 && \$2 ~ /tty[0-9]+/ {print \$1; exit}' 2>/dev/null)/.Xauthority" # Get and export the currentuser's \$XAUTHORITY
    ID="\$(xinput | grep -ioP 'touchpad.*id=\K[0-9]*')"                                                           # Find the TouchPad device ID
    
    if   [ "\${1}" -eq 0 ]; then xinput enable "\$ID"   # Laptop mode or Lid is open
    elif [ "\${1}" -eq 1 ]; then xinput disable "\$ID"  # Tablet mode or Lid is closed
    fi
    EOF
    
    • 该脚本将解析和导出当前用户会话的环境变量$DISPAY和环境变量$XAUTHORITY,以便分别允许root(谁运行acpid进程)访问用户的 X 会话xinput。
    • 然后脚本将解析$ID触摸板的。根据输入变量的值,$1它将启用或禁用触控板。

    注意:美元符号前的反斜杠\$旨在转义命令中的变量(命令替换)扩展cat。因此,如果您复制/粘贴脚本(而不是使用该cat方法),您应该手动删除它们。


    参考:

    • ArchWiki:acpid- 高级配置和电源接口事件守护进程。
    • 问 Ubuntu:如何在盖子关闭时禁用触摸板?
    • ThinkWiki:在 Thinkpad Twist 上安装 Ubuntu 12.10 | Thinkpad-acpi | Wacom 数位板触控笔
    • Ubuntu 论坛:触摸板开/关| 为什么这个 acpi 事件不起作用?
    • 关于解析阅读:以编程方式查找 DISPLAY 的当前值,使用wandawk和Remove specific words from lines usinggrep -P '\K'。
    • 2
  2. Tim Richardson
    2018-12-01T22:50:13+08:002018-12-01T22:50:13+08:00

    使用 pa4080 的答案,我必须对其进行更改才能在 Ubuntu 18.04 中工作:在脚本中硬编码我的用户 ( tim) 并在我的用户上下文中运行脚本。

    文件/etc/acpi/events/thinkpad-lid-event:

    event=button/lid.*
    action=su tim -c '/home/tim/scripts/lid.sh.post'
    

    和lid.sh.post:

    #! /bin/bash
    # toggle touchpad enabled status when lid changes (lid closed,touchpad off)
    # is run in user context
    # 
    # example rule /etc/acpi/events/thinkpad-lid-close
    # event=button/lid.*
    # action=su tim -c '/home/tim/scripts/lid.sh.post'  
    #
    # see https://askubuntu.com/questions/91534/disable-touchpad-while-the-lid-is-down
    # and https://askubuntu.com/questions/980997/how-do-i-disable-the-touchpad-when-the-lid-is-twisted-or-closed/980999#980999
    # this needs an event defined in /etc/acpi/events to call this script when lid status changes
    # these variables need to be set to use xinput properly
    user="tim"
    export XAUTHORITY=$(ls -1 /home/$user/.Xauthority | head -n 1)
    export DISPLAY=":$(ls -1 /tmp/.X11-unix/ | sed -e s/^X//g | head -n 1)"
    
    export TouchPadID=$(xinput | grep 'TouchPad' | sed  -n "s/^.*id=\([[:digit:]]\+\).*$/\1/p")
    grep -q closed /proc/acpi/button/lid/*/state
    LidClosedResult=$?
    xinput set-int-prop  $TouchPadID "Device Enabled" 8 $LidClosedResult
    if [ $? -eq 0 ]; then
      echo "for user: $user xinput device $TouchPadID enabled status changed to $LidClosedResult because of LID ACPI event" | systemd-cat
    else
      echo "failed to change xinput device $TouchPadID enabled status after LID ACPI event" | systemd-cat
    fi
    
    • 1
  3. Bart
    2022-08-09T06:02:49+08:002022-08-09T06:02:49+08:00

    对 'lid' 和 'tabletmode' 进行了另一个变体,只有 2 条规则捕获 ACPI 事件的响应(在操作中使用“%e”,注意引号!):

    #!/bin/bash
    # toggle 'touchpad enabled status' off/on when lid changes (lid closed or tablet mode => touchpad off)
    # 
    # let it act on 2 rules in /etc/acpi/events/: lid-change and tabletmode-change:
    # 
    #     event=button/lid.*
    #     action=/etc/acpi/touchpad-toggle.sh "%e"
    #
    #     event=video/tabletmode.*
    #     action=/etc/acpi/touchpad-toggle.sh "%e"
    #
    
    
    # variables needed to use xinput properly
    user1="$(who | cut -d ' ' -f1)"
    export XAUTHORITY="$(ls -1 /home/$user1/.Xauthority | head -n 1)"
    export DISPLAY=":$(ls -1 /tmp/.X11-unix/ | sed -e s/^X//g | head -n 1)"
    export TPdID="$(xinput | grep -ioP 'touchpad.*id=\K[0-9]*')"
    
    if [ "${@}" == "" ]; then
        echo "ACPI event didn't return trigger values." | systemd-cat
    elif [ "$user1" == "" ]; then
        echo "ACPI event action didn't find current user." | systemd-cat
    else
        false
        if   [ "${@}" == "button/lid LID open" ]; then xinput enable $TPdID
        elif [ "${@}" == "button/lid LID close" ]; then xinput disable $TPdID
        elif [ "${@}" == "video/tabletmode TBLT 0000008A 00000000" ]; then xinput enable $TPdID
        elif [ "${@}" == "video/tabletmode TBLT 0000008A 00000001" ]; then xinput disable $TPdID
        fi
        if [ $? -eq 0 ]; then
            echo "ACPI event: xinput device $TPdID status changed on ${@}." | systemd-cat
        else
            echo "ACPI event failed: xinput device $TPdID status NOT changed on ${@}." | systemd-cat
        fi
    fi
    

    享受使用脚本...

    编辑: $USER 在由 root 使用时并不总是返回结果......所以我用它替换了它: user1="$(who | cut -d ' ' -f1)"

    • 1

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve