Skip to content

Instantly share code, notes, and snippets.

@brunomsantiago
Created March 25, 2021 23:40
Show Gist options
  • Save brunomsantiago/75d49443568680e52a29bd464f1d35f0 to your computer and use it in GitHub Desktop.
Save brunomsantiago/75d49443568680e52a29bd464f1d35f0 to your computer and use it in GitHub Desktop.
Toogle (enable/disable) Thinkpad 440p touchpad on Linux
#!/home/bms/miniconda3/bin/python3
# This script toggles status (enabled/disabled) of my notebook touchpad. My
# notebook is a Thinkpad 440p and its touchpad is a Synaptics TM2722-001. The
# system is Linux Mint. I map it to a keyboard shortcut (alt+F11 in my case).
#
# I tried just using: xinput disable 14
# ... but the device id keeps changing after boot.
#
# I tried just using: xinput disable "Synaptics TM2722-001"
# ... but the device string keeps changing after system updates
#
# I hope this script with partial matching of multiple strings will be
# robust against such changes. If not, I hope it will be easy to fix.
#
# It should be easy to adapt to other notebook models, just change strings
# on toggle_touchpad() function.
from subprocess import run, PIPE
def read_devices():
result = run(['xinput'], stdout=PIPE)
return result.stdout.decode('utf-8')
def find_line(text, looking_for=[]):
for line in text.split('\n'):
for string in looking_for:
if string in line:
return line
def find_id(line):
loc = line.find('id=')
return line[loc+3:loc+5]
def read_properties(device_id):
result = run(['xinput', 'list-props', device_id], stdout=PIPE)
return result.stdout.decode('utf-8')
def toggle_touchpad():
device_strings = ['Synaptics', 'TM2722-001']
device_line = find_line(read_devices(), device_strings)
device_id = find_id(device_line)
enabled_line = find_line(read_properties(device_id), ['Device Enabled'])
enabled = bool(int(enabled_line[-1]))
command = 'disable' if enabled else 'enable'
run(['xinput', command, device_id], stdout=PIPE)
return device_id
if __name__ == "__main__":
toggle_touchpad()
@brunomsantiago
Copy link
Author

Using it for 14 days and still works

@brunomsantiago
Copy link
Author

9 months later and still working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment