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












2















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





























    2















    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



























      2












      2








      2








      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







      pa4080

















      asked Nov 28 '17 at 7:37









      pa4080pa4080

      14.3k52668




      14.3k52668






















          2 Answers
          2






          active

          oldest

          votes


















          2














          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

































            1














            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-event



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


            and lid.sh.post is



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


            ~






            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',
              autoActivateHeartbeat: false,
              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









              2














              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






























                2














                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




























                  2












                  2








                  2







                  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 '18 at 8:13

























                  answered Nov 28 '17 at 7:41









                  pa4080pa4080

                  14.3k52668




                  14.3k52668

























                      1














                      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-event



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


                      and lid.sh.post is



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


                      ~






                      share|improve this answer






























                        1














                        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-event



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


                        and lid.sh.post is



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


                        ~






                        share|improve this answer




























                          1












                          1








                          1







                          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-event



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


                          and lid.sh.post is



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


                          ~






                          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-event



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


                          and lid.sh.post is



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


                          ~







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jan 29 at 11:34

























                          answered Dec 1 '18 at 6:50









                          Tim RichardsonTim Richardson

                          691415




                          691415






























                              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.




                              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

                              How to make a Squid Proxy server?

                              Is this a new Fibonacci Identity?

                              19世紀