touchpad.bash 920 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/bin/bash
  2. die() { echo "$0: $@" && exit 1 ;}
  3. usage() {
  4. cat <<-USAGE
  5. Usage: touchpad -[edt]
  6. Enable or disable the touchpad.
  7. -e Enbales the touchpad
  8. -d Disables the touchpad
  9. -t Toggles the touchpad state
  10. -h Prints this help message
  11. USAGE
  12. exit $1
  13. }
  14. # Helper function to get the ID for the touchpad
  15. _get_touchpad_id() {
  16. local id=$(xinput | grep -F PS/2)
  17. id="$(echo ${id##*id=})"
  18. id="${id%% *}"
  19. echo $id
  20. }
  21. # Error handling
  22. [[ -d /proc/acpi/button/lid/ ]] || die "system not a laptop"
  23. command -v xinput &>/dev/null || die "xinput not found in system PATH"
  24. ps -e | grep -Fq 'X' || die 'No X server running'
  25. case "$1" in
  26. -e) action=enable ;;
  27. -d) action=disable ;;
  28. -t)
  29. # disabled
  30. if [[ "$(xinput | grep -F floating)" == *id=$TOUCHPADID* ]] ; then
  31. action=enable
  32. # enabled
  33. else action=disable
  34. fi
  35. ;;
  36. -h) usage 0 ;;
  37. *) usage 1 ;;
  38. esac
  39. xinput $action $(_get_touchpad_id)