How do I disable the touchpad when the lid is twisted or closed?











up vote
2
down vote

favorite












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.










share|improve this question




























    up vote
    2
    down vote

    favorite












    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.










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      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.










      share|improve this question















      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.







      command-line touchpad tablet acpi events






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 30 '17 at 7:51

























      asked Nov 28 '17 at 7:37









      pa4080

      13.2k52561




      13.2k52561






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          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:




          • ArchWiki: acpid - Advanced Configuration and Power Interface event daemon.

          • Ask Ubuntu: How do I disable the touchpad while the lid is down?

          • ThinkWiki: Installing Ubuntu 12.10 on Thinkpad Twist | Thinkpad-acpi | Wacom Tablet Stilus

          • Ubuntu Forums: Touchpad ON/OFF | Why won't this acpi event work?

          • About the parsing read: Programmatically find the current value of DISPLAY using w and awk and Remove particular words from lines using grep -P 'K'.






          share|improve this answer






























            up vote
            1
            down vote













            using the answer of pa4080,



            I had to make this change for it to work in Ubuntu 18.04
            : hard code my user in the script and run the script in the context of my user.



            file: /etc/acpi/events/thinkpad-lid-close



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


            file: /etc/acpi/events/thinkpad-lid-open



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


            and lid.sh.post is



            #! /bin/bash
            # disable the touchpad if the lid is closed
            # 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 two events defined in /etc/acpi/events which call this script
            # user name is hardcoded which is an ugly hack


            export XAUTHORITY=`ls -1 /home/tim/.Xauthority | head -n 1`
            export DISPLAY=":`ls -1 /tmp/.X11-unix/ | sed -e s/^X//g | head -n 1`"
            export TouchPadID=11
            grep -q closed /proc/acpi/button/lid/*/state
            xinput set-int-prop $TouchPadID "Device Enabled" 8 $?





            share|improve this answer























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "89"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f980997%2fhow-do-i-disable-the-touchpad-when-the-lid-is-twisted-or-closed%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote



              accepted










              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:




              • ArchWiki: acpid - Advanced Configuration and Power Interface event daemon.

              • Ask Ubuntu: How do I disable the touchpad while the lid is down?

              • ThinkWiki: Installing Ubuntu 12.10 on Thinkpad Twist | Thinkpad-acpi | Wacom Tablet Stilus

              • Ubuntu Forums: Touchpad ON/OFF | Why won't this acpi event work?

              • About the parsing read: Programmatically find the current value of DISPLAY using w and awk and Remove particular words from lines using grep -P 'K'.






              share|improve this answer



























                up vote
                2
                down vote



                accepted










                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:




                • ArchWiki: acpid - Advanced Configuration and Power Interface event daemon.

                • Ask Ubuntu: How do I disable the touchpad while the lid is down?

                • ThinkWiki: Installing Ubuntu 12.10 on Thinkpad Twist | Thinkpad-acpi | Wacom Tablet Stilus

                • Ubuntu Forums: Touchpad ON/OFF | Why won't this acpi event work?

                • About the parsing read: Programmatically find the current value of DISPLAY using w and awk and Remove particular words from lines using grep -P 'K'.






                share|improve this answer

























                  up vote
                  2
                  down vote



                  accepted







                  up vote
                  2
                  down vote



                  accepted






                  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:




                  • ArchWiki: acpid - Advanced Configuration and Power Interface event daemon.

                  • Ask Ubuntu: How do I disable the touchpad while the lid is down?

                  • ThinkWiki: Installing Ubuntu 12.10 on Thinkpad Twist | Thinkpad-acpi | Wacom Tablet Stilus

                  • Ubuntu Forums: Touchpad ON/OFF | Why won't this acpi event work?

                  • About the parsing read: Programmatically find the current value of DISPLAY using w and awk and Remove particular words from lines using grep -P 'K'.






                  share|improve this 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:




                  • ArchWiki: acpid - Advanced Configuration and Power Interface event daemon.

                  • Ask Ubuntu: How do I disable the touchpad while the lid is down?

                  • ThinkWiki: Installing Ubuntu 12.10 on Thinkpad Twist | Thinkpad-acpi | Wacom Tablet Stilus

                  • Ubuntu Forums: Touchpad ON/OFF | Why won't this acpi event work?

                  • About the parsing read: Programmatically find the current value of DISPLAY using w and awk and Remove particular words from lines using grep -P 'K'.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Oct 25 at 8:13

























                  answered Nov 28 '17 at 7:41









                  pa4080

                  13.2k52561




                  13.2k52561
























                      up vote
                      1
                      down vote













                      using the answer of pa4080,



                      I had to make this change for it to work in Ubuntu 18.04
                      : hard code my user in the script and run the script in the context of my user.



                      file: /etc/acpi/events/thinkpad-lid-close



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


                      file: /etc/acpi/events/thinkpad-lid-open



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


                      and lid.sh.post is



                      #! /bin/bash
                      # disable the touchpad if the lid is closed
                      # 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 two events defined in /etc/acpi/events which call this script
                      # user name is hardcoded which is an ugly hack


                      export XAUTHORITY=`ls -1 /home/tim/.Xauthority | head -n 1`
                      export DISPLAY=":`ls -1 /tmp/.X11-unix/ | sed -e s/^X//g | head -n 1`"
                      export TouchPadID=11
                      grep -q closed /proc/acpi/button/lid/*/state
                      xinput set-int-prop $TouchPadID "Device Enabled" 8 $?





                      share|improve this answer



























                        up vote
                        1
                        down vote













                        using the answer of pa4080,



                        I had to make this change for it to work in Ubuntu 18.04
                        : hard code my user in the script and run the script in the context of my user.



                        file: /etc/acpi/events/thinkpad-lid-close



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


                        file: /etc/acpi/events/thinkpad-lid-open



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


                        and lid.sh.post is



                        #! /bin/bash
                        # disable the touchpad if the lid is closed
                        # 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 two events defined in /etc/acpi/events which call this script
                        # user name is hardcoded which is an ugly hack


                        export XAUTHORITY=`ls -1 /home/tim/.Xauthority | head -n 1`
                        export DISPLAY=":`ls -1 /tmp/.X11-unix/ | sed -e s/^X//g | head -n 1`"
                        export TouchPadID=11
                        grep -q closed /proc/acpi/button/lid/*/state
                        xinput set-int-prop $TouchPadID "Device Enabled" 8 $?





                        share|improve this answer

























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          using the answer of pa4080,



                          I had to make this change for it to work in Ubuntu 18.04
                          : hard code my user in the script and run the script in the context of my user.



                          file: /etc/acpi/events/thinkpad-lid-close



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


                          file: /etc/acpi/events/thinkpad-lid-open



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


                          and lid.sh.post is



                          #! /bin/bash
                          # disable the touchpad if the lid is closed
                          # 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 two events defined in /etc/acpi/events which call this script
                          # user name is hardcoded which is an ugly hack


                          export XAUTHORITY=`ls -1 /home/tim/.Xauthority | head -n 1`
                          export DISPLAY=":`ls -1 /tmp/.X11-unix/ | sed -e s/^X//g | head -n 1`"
                          export TouchPadID=11
                          grep -q closed /proc/acpi/button/lid/*/state
                          xinput set-int-prop $TouchPadID "Device Enabled" 8 $?





                          share|improve this answer














                          using the answer of pa4080,



                          I had to make this change for it to work in Ubuntu 18.04
                          : hard code my user in the script and run the script in the context of my user.



                          file: /etc/acpi/events/thinkpad-lid-close



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


                          file: /etc/acpi/events/thinkpad-lid-open



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


                          and lid.sh.post is



                          #! /bin/bash
                          # disable the touchpad if the lid is closed
                          # 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 two events defined in /etc/acpi/events which call this script
                          # user name is hardcoded which is an ugly hack


                          export XAUTHORITY=`ls -1 /home/tim/.Xauthority | head -n 1`
                          export DISPLAY=":`ls -1 /tmp/.X11-unix/ | sed -e s/^X//g | head -n 1`"
                          export TouchPadID=11
                          grep -q closed /proc/acpi/button/lid/*/state
                          xinput set-int-prop $TouchPadID "Device Enabled" 8 $?






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Dec 1 at 22:42

























                          answered Dec 1 at 6:50









                          Tim Richardson

                          646415




                          646415






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Ask Ubuntu!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f980997%2fhow-do-i-disable-the-touchpad-when-the-lid-is-twisted-or-closed%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Quarter-circle Tiles

                              build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

                              Mont Emei