Skip to content

Instantly share code, notes, and snippets.

@herbertjones
Created August 26, 2017 15:19
Show Gist options
  • Save herbertjones/fc261743194d84537e0033a07bb51c02 to your computer and use it in GitHub Desktop.
Save herbertjones/fc261743194d84537e0033a07bb51c02 to your computer and use it in GitHub Desktop.
Toggle touchpad on laptop
#!/bin/bash
stderr() { cat <<< "$@" 1>&2; }
toggle_xinput_device()
{
declare -r -a device_filters=("$@")
if [[ ${#device_filters[*]} -eq 0 ]]; then
stderr "No filters provided."
return 1
fi
local xinput_output
if ! xinput_output="$(xinput)"; then
stderr "Failed to get xinput"
return 1
fi
local lines rest="${xinput_output}"
for filter in "${device_filters[@]}"; do
if ! rest="$(grep -i "${filter}" <<< "${rest}")"; then
stderr "Failed find ${filter} in xinput output"
return 1
fi
done
if [[ "$(wc -l <<< "${rest}")" != 1 ]]; then
stderr "Too many devices found"
stderr "${rest}"
return 1
fi
local touchpad_line="${rest}"
declare id
id="$(sed -e 's/^[^=]*id=//' -e 's/[^0-9].*//' <<< "${touchpad_line}")"
if [[ -z $id ]]; then
stderr "Failed find device ID in xinput output"
return 1
fi
local enabled
enabled="$(xinput list-props "${id}" | awk '/Device Enabled/ {print $NF}')"
if [[ $enabled = "1" ]]; then
xinput --disable "${id}"
elif [[ $enabled = "0" ]]; then
xinput --enable "${id}"
else
stderr "Unknown enabled state: $enabled"
return 1
fi
}
main()
{
toggle_xinput_device 'pointer' 'Synaptics TouchPad'
}
[[ "${BASH_SOURCE[0]}" == "${0}" ]] && main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment