Ubuntu – How to disable the touchpad when the lid is twisted or closed

acpicommand lineeventstablettouchpad

I have Lenovo ThinkPad X230 Tablet with Ubuntu 16.04. It has a convertible screen and when it is in tablet mode the touchpad is still active and make a mess.

I've created the following script and bound it to one of the built in buttons (by a custom shortcut):

#!/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

The script works properly, but this is a fake solution and yesterday I spent few hours to learn how to do that in a proper way. So I decided to share this experience here.

Best Answer

To check whether the device is in tablet mode or not we could read the value (0 or 1) of:

/sys/devices/platform/thinkpad_acpi/hotkey_tablet_mode

This value is switched by specific events. We can catch these events and could bind scripts to them by using acpid - Advanced Configuration and Power Interface event daemon.


1. Catch the events. Execute acpi_listen or netcat -U /var/run/acpid.socket, turn the lid in tablet mode, then turn it back. Here is an example output:

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

Please note when the lid is close/open the result is different:

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

2. Configure acpid to recognize the events triggered by the device mode change. Run the following lines into a terminal as (single) commands:

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

The above commands will create the files:

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

Note: The scripts for lid open/close aren't provided here. But they are similar as the above.


3. Restart acpid so it can re-read the event filters, including the ones you just added:

sudo systemctl restart acpid.service

4. Create the script /etc/acpi/thinkpad-touchpad-in-twist-mode.sh that will disable 1 and enable 0 the touchpad (&& make it executable):

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
  • The script will parse and export the environment variables $DISPAY and $XAUTHORITY of the current user's session, in order to allow root (who runs the acpid process) to access the user's X session, respectively xinput.
  • Then the script will parse the $ID of the touchpad. And depending on the value of the input variable $1 it will enable or disable the touckpad.

Note: The backslashes before the dollar signs \$ are intended to escape the variable (command substitution) expansion within the cat command. So if you copy/paste the script (instead using of the cat approach) you should remove them manually.


References:

Related Question