Troubleshooters.Com® and Linux Library Present:
A Touchpad Toggler For Linux
Copyright © 2019 by Steve Litt
See the Troubleshooters.Com Bookstore.
CONTENTS:
The touchpad toggler described on this page supercedes all touchpad togglers that have been described on Troubleshooter.Com's Linux Quick Hacks page. This touchpad toggler version is much more sophisticated and likely to work on a wider variety of hardware.
Not everyone needs a Touchpad Toggler. Some folks use their keyboards so rarely that a wrist colliding with the touchpad isn't an issue. Some don't type from standard home position (touch typing), so there are no wrist collisions. Some have learned to type the right way and still avoid their wrist touching the touchpad.
But some folks are like me. They need to pound out 30 to 100 words per minute of text, and don't want to have to worry about wrist touches knocking their cursor into a different place, changing the window, or even deleting what they've typed. And yet in today's world, everyone needs a touchpad occasionally, and some situations, such as a lack of room forcing your laptop to really be on top of your lap, preclude using a separate mouse.
In those situations, it's great to be able to turn on the mouse with a keysstroke, use the mouse, and then turn it off with a keystroke. The Touchpad Toggler in this document does that.
There are different Touchpad Togglers described all over the Internet. Most are simple affairs that worked with their author's hardware. They're probably not going to work with yours, without a lot of modification.
The Touch Toggler described here uses two different on/off mechanisms:
A lot of modern laptop touchpads respond to one of these two software mechanisms. My limited research tells me that the extra software stimulus does no harm, while still covering both bases.
The synclient works as follows to turn the touchpad first off, then on:
synclient TouchpadOff=1
or
synclient TouchpadOff=0
The xinput method is more complicated, because you have to pick just the right input id's to enable or disable, and those vary between different computers. You can see just how many id's there are to choose from by running the following command:
xinput --list
That's a heck of a lot of id's. It's like finding a needle in a haystack, and if you disable the wrong id's, you can be in a world of hurt. As an experiment, I wrote experimental code that disabled all of them, from 1 to 23. It locked out my touchpad, my external mouse, and my keyboard. The lockout was so tight that I couldn't Ctrl+Alt+F2 to a virtual terminal to undo the damage.The power button was my only salvation.
Imagine some hypothetical computer where touchpad control is via id's 15 and 16. To turn the touchpad first off and then on, you'd perform the following:
xinput --disable 15 xinput --disable 16
Then, to regain touchpad functionality, the following:
xinput --enable 15 xinput --enable 16
By now the architecture is pretty simple. The Touch Toggler uses sed and grep on the output of xinput --list to produce a list of input id's you need to enable or disable, and calls an enable or disable function on each.
There's also a "toggle" function to read a flag file to see whether the touchpad is currently enabled or disabled, and call the proper function with each id. There's a "status" call to interpret the flag file and tell, on the Touchpad Toggler's stdout, whether the touchpad is on or off. Naturally there's a "help" call. The Touchpad Toggler program itself is called touchutil, and it takes a single command as an argument. That command can be one of the following five:
The command must be entirely lowercase. If the command argument is anything but one of the preceding five, and error routine prints the command, says it's bad, displays the help (usage) text, and then exits with a value of 1 to indicate an error.
The code is licensed with the Expat licence, a lax, permissive non-copyleft free software license, compatible with the GNU GPL. The code follows immediately:
#!/bin/sh # Copyright (C) 2019 by Steve Litt, all rights reserved # Licensed under the Expat free software license, # obtainable at # https://directory.fsf.org/wiki/License:Expat tu_touchstatus(){ if test -e /tmp/touchison.flag; then echo "Touchpad is ON." else echo "Touchpad is OFF." fi } tu_touchhelp(){ echo "Usage: touchutil COMMAND" echo "where COMMAND is on, off, toggle, status or help" echo "COMMAND must be lowercase," echo "and there can be only one COMMAND on a line." } tu_touchnoop(){ echo Bad command ::$1::. echo tu_touchhelp } tu_touchon(){ echo Setting touchpad on for input $1 xinput --enable $1 synclient TouchpadOff=0 touch /tmp/touchison.flag } tu_touchoff(){ echo Setting touchpad off for input $1 xinput --disable $1 synclient TouchpadOff=1 rm -f /tmp/touchison.flag } mypgm="tu_touchnoop" if test "$1" = "on"; then mypgm="tu_touchon" elif test "$1" = "off"; then mypgm="tu_touchoff" elif test "$1" = "help"; then tu_touchhelp exit 0 elif test "$1" = "status"; then tu_touchstatus exit 0 elif test "$1" = "toggle"; then if test -e /tmp/touchison.flag; then mypgm="tu_touchoff" else mypgm="tu_touchon" fi else tu_touchnoop "$1" exit 1 fi xinput --list | \ sed -e"s/^..//" -e"s/^ ../ /" | \ grep -vi keyboard | \ grep -i touch | \ sed -e"s/.*id=//" -e"s/\s.*//" | \ while read myline; do $mypgm $myline done
A lot of the work is done by five functions, each starting with tu_. After the functions are defined, a giant if/else construct determines which of those functions to run, and actually runs it in the case of tu_touchhelp, tu_touchstatus, and tu_touchnoop. tu_help. In the cases of tu_touchon, and tu_touchoff, running of the function is saved for later.
The final part of the main program is a long pipeline starting with the output of xinput --list. The list is purged of all lines containing the word "keyboard" case insensitively, or not containingthe word "touch" case insensitively, and then the purged list is whittled down to produce just the list of id's. The final program run in the long pipeline is the function name contained in $mypgm, with the id as its one and only argument.
In real life you'll seldom use the Touch Toggler from the command prompt, because that would take too long and require too much typing. The main reasons to use it from the command prompt are learning, understanding, and troubleshooting.
It would be incredibly inefficient to type a command every time you wanted to turn the touchpad on or off. What's an order of magnitude faster and easier is to have the command touchutil toggle executed by a quick and easy hotkey that keeps your hands on home typing position.
You could also have a two hotkey solution, in which one hotkey runs the command touchutil off and the other runs touchutil on. The two hotkey solution is more likely to do the right thing every single time and never get in a weird state, but you must memorize and consume one more hotkey. I prefer the single hotkey solution.
How to set a hotkey to run a command line command is beyond the scope of this document. Most window managers have ways to accomplish this, and Troubleshooters.Com has detailed how to do it for Openbox, LXDE, IceWM, ctwm, and maybe more. Find a document telling you how, and do it. Knowing how to assign a hotkey to a random command line command is one of the most powerful workflow aides in the Linux world: Use it!
When the Toggler is hooked up to a window manager hotkey, you can't see its stdout, so it's an untroubleshootable black box. So your first task is to run it from your command prompt. Here's a sample session:
[slitt@littlapd ~]$ touchutil status Touchpad is OFF. [slitt@littlapd ~]$ touchutil toggle Setting touchpad on for input 15 Setting touchpad on for input 16 [slitt@littlapd ~]$ touchutil toggle Setting touchpad off for input 15 Setting touchpad off for input 16 [slitt@mydesk ~]$
Your input (id) numbers might vary, but if things are working you should get similar behavior. Be sure to check the functioning of the touchpad before and after each command, to see if it correlates to what the command says it should be. When testing the touchpad, use multifinger gestures, move madly around and twist, because sometimes simple touchpad movements are hard to observe.
If your commands are operating as expected, troubleshoot your hotkey and its connection to the command, and be sure it's connected to the correct command. If your commands at the command prompt are not operating as expected, continue to troubleshoot.
Perform the touchutil on and touchutil off commands. After each, test the touchpad. If these two commands operate as expected, there must be something wrong with the toggle mechanism. Experiment with deleting or making read-only the /tmp/touchison.flag file. If either the touchutil on or touchutil off command doesn't affect the touchpad in the expected way, try the commands they're comprised of individually. Be sure to perform the required xinput and synclient commands, as well as the xinput --list command. Try to make sure the proper input ids are getting toggled.
If you don't have the xinput command or the synclient command, install them.
Try to remember if these commands have ever worked on this computer. If not, perhaps your touchpad uses a different API and isn't affected bythese commands.
Each of us has his or her own way of working. For some, an always-functional touchpad impedes productivity. If so, the Touch Toggler revealed in this document can be connected to a quick and easy hotkey, after which it's almost no trouble at all to turn the touchpad on and off at will.
This Touch Toggler has been designed to work with a lot of modern touchpads, but obviously it can't be aware of every piece of hardware out there. If you have hardware on which this Touch Toggler doesn't work, please email me with the details.