Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

mouse-sensitivity.sh (Source)

#!/bin/sh
# File: mouse-sensitivity.sh
# Location: /usr/local/bin
# Author: bgstack15
# Startdate: 2024-02-14-4 14:04
# SPDX-License-Identifier: GPL-3.0-only
# Title: Simple control for mouse sensitivity
# Purpose: provide easy options to slow down my mouse
# History:
# Usage: ./mouse-sensitivity.sh slow
# Reference:
#    https://unix.stackexchange.com/questions/90572/how-can-i-set-mouse-sensitivity-not-just-mouse-acceleration
#    https://shallowsky.com/blog/linux/setting-mouse-speed.html
#    experimentation for my mouse
# Improve:
# Dependencies:
#    x11, notify-send | zenity
# Documentation:
# Functions
warn() {
   #zenity --icon-name mouse --warning --text "${1}"
   notify-send --icon mouse --expire-time=2000 "Mouse sensitivity" "${1}"
}
# Load config
test -f "${XDG_CONFIG_HOME:-~/.config}/mouse-sensitivity.conf" && . "${XDG_CONFIG_HOME:-~/.config}/mouse-sensitivity.conf"
test -z "${DEVICE_NAME}" && DEVICE_NAME="Logitech Wireless Receiver Mouse"
# Load environment/runtime settings
if test -z "${SPEED}" ;
then
   if test -n "${1}" ;
   then
      SPEED="${1}"
   else
      warn "Need SPEED or \$1 of number between -1.0 and 1.0, or slow, normal, fast."
      exit 1
   fi
fi
# determine xinput id
xid="$( xinput list | sed -n -r -e "/${DEVICE_NAME}/{s/.*id=([0-9]+).*/\1/;p;}" )"
# Validate SPEED
if echo "${SPEED}" | grep -qE '^[0-9\.\-]+$' ;
then
   # a decimal number, so running with just that number
   :
elif echo "${SPEED}" | grep -qE '^(slow|normal|fast)' ;
then
   case "${SPEED}" in
      slow) SPEED=-1 ;;
      normal) SPEED=0 ;;
      fast) SPEED=1 ;;
   esac
else
   warn "Need speed of number between -1.0 and 1.0, or slow, normal, fast."
   exit 1
fi
# Run command
printf '%s\n' "Setting device ${xid} speed ${SPEED}" 1>&2
xinput set-prop "${xid}" "libinput Accel Speed" "${SPEED}"