Skip to content

Instantly share code, notes, and snippets.

@tafaust
Created February 21, 2021 07:14
Show Gist options
  • Save tafaust/bc45a06412c93a8f02ae066b55b59068 to your computer and use it in GitHub Desktop.
Save tafaust/bc45a06412c93a8f02ae066b55b59068 to your computer and use it in GitHub Desktop.
Bash script to toggle touchpad state on X server
#!/bin/bash
# set your device name here (can be found through `xinput`)
touchpadDevice='SynPS/2 Synaptics TouchPad'
if [ ! -z "$1" ]; then
# set the device state via argument (must be 0 or 1)
devEnabled=$1
else
# toggle the device state
id=$(xinput --list --id-only "$touchpadDevice")
devEnabled=$(xinput --list-props $id | awk '/Device Enabled/{print !$NF}')
fi
# alter the device state and subsequently print the state via wm dbus
if [ -n "$DISPLAY" ] ; then
export DISPLAY=:1
export touchpadDevice
export devEnabled
/usr/bin/su $USER -c '/usr/bin/xinput --set-prop "$touchpadDevice" "Device Enabled" "$devEnabled"'
/usr/bin/su $USER /usr/bin/notify-send -t 2000 -i "input-touchpad" "$touchpadDevice" "TouchPad device is $([[ $devEnabled -eq 0 ]] && echo -ne "disabled" || echo -ne "enabled")"
else
/usr/bin/xinput --set-prop "$touchpadDevice" 'Device Enabled' "$devEnabled"
/usr/bin/notify-send -t 2000 -i "input-touchpad" "$touchpadDevice" "TouchPad device is $([[ $devEnabled -eq 0 ]] && echo -ne "disabled" || echo -ne "enabled")"
fi
unset devEnabled
unset touchpadDevice
unset id
@tafaust
Copy link
Author

tafaust commented Feb 21, 2021

Check https://bbs.archlinux.org/viewtopic.php?id=92896 if you want to call this script via udev rather than manually invocation upon usb mouse plug-in/out.

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