I have an Apple Magic Mouse paired with a Dell Latitude E7440 (running Ubuntu 16.04 LTS) that has a weird habit of disconnecting [and I have to manually reconnect it] from time to time (or being disconnected, still not sure of whom is the culprit).
And not satisfied only with this, each time the mouse is reconnected, the mouse [pointer] speed settings are gone! I have to [manually] reapply the xinput
configs every single time the mouse reconnects.
Ubuntu's Mouse&Touchpad settings does not have any effect: I've slide the acceleration all the way down but it is still way too fast.
The question: Is there anyway to have those settings [applied using the xinput --set-prop
] at each time the mouse reconnects? (The way it is - having to apply those settings every time the mouse reconnects - is not acceptable, since the mouse disconnects and reconnects several times a day!]
Long, contextualized version:
I've found many proposals for solving this intermittent disconnection (here and here) but they failed to resolve the issue completely. The disconnection still happens from time to time, perhaps a tad less frequently (but it might be a biased perception).
The scrolling speed [was too slow, need to speed it up] was fixed following this. To make it definitive (to persist after a reboot), a one-liner modprobe config file solves it:
/etc/modprobe.d/hid_magicmouse.conf:
options hid_magicmouse scroll-speed=40 scroll-acceleration=1
Another issue was the mouse pointer moving too fast; the solution was found here. But this is not a definitive solution, as each time the computer is restarted, the configs are gone, and the configs need to be reapplied - manually.
xinput --set-prop 'Apple Magic Mouse' "Device Accel Constant Deceleration" 2
xinput --set-prop 'Apple Magic Mouse' "Device Accel Velocity Scaling" 1
However each time the mouse disconnects, the settings are gone, and need to be manually reapplied once more.
I'd like to make it more definitive i.e. to be reapplied automatically every time the mouse reconnects.
The udev framework/mechanism looked like a perfect fit. Wiring this script to an udev rule (and therefore the settings will be reapplied every time the mouse is connected).
(the udev rule)
/etc/udev/rules.d/71-applemagicmouse.rules
SUBSYSTEMS=="input", ATTRS{name}=="Apple Magic Mouse", ACTION=="add", ENV{DISPLAY}=":0.0", ENV{XAUTHORITY}="/home/cfeng01/.Xauthority", RUN+="/bin/su cfeng01 -c /home/cfeng01/bin/set_apple_mouse_speed.sh"
(and the script)
/home/cfeng01/bin/set_apple_mouse_speed.sh
#!/bin/sh
# references:
# * about udev and running scripts after the device is plugged in [or recognized by Linux]
# https://askubuntu.com/questions/686144/run-a-shell-command-after-a-bluetooth-input-device-is-detected
#
# * about how the need of XDISPLAY and XAUTHORITY env variables
# https://ubuntuforums.org/showthread.php?t=2334004&page=2
# for debugging purposes
#set -x
while [ ! "$(/usr/bin/hcitool info 1C:1A:C0:D6:74:10 2>&1 > /dev/null; echo $?)" ]; do
sleep 0.1
done
# the xinput requires X [to acessible] and since this script will be triggered by udev,
# there won't be these X-related variables;
# However, exporting here (in the script) doesn't work!
#
# export XDISPLAY=:0
# export XAUTHORITY=/home/cfeng01/.Xauthority
#
# It only worked when I added in the udev rule:
#
# SUBSYSTEMS=="input", ATTRS{name}=="Apple Magic Mouse", ACTION=="add", ENV{DISPLAY}=":0.0", ENV{XAUTHORITY}="/home/cfeng01/.Xauthority", RUN+="/bin/su cfeng01 -c /home/cfeng01/bin/set_apple_mouse_speed.sh"
while [ ! "$(xinput query-state 'Apple Magic Mouse' 2>&1 > /dev/null; echo $?)" ]; do
sleep 0.1
done
MOUSE_ID=`xinput --list | grep "Apple Magic Mouse" | awk 'BEGIN {FS = " "}; { print $6}' | awk 'BEGIN {FS = "="}; { print $2}'`
if [ "${MOUSE_ID}" != "" ]; then
logger $0 Magic Mouse found, updating the mouse speed settings.
# para desacelerar o Apple Magic Mouse
xinput --set-prop ${MOUSE_ID} "Device Accel Constant Deceleration" 2
xinput --set-prop ${MOUSE_ID} "Device Accel Velocity Scaling" 1
else
logger $0 Magic Mouse not found, skipping.
fi
However, cycling over and over the "disable bluetooth > enable bluetooth > activate the mouse" didn't made any effect. Analysing the /var/log/syslog
[and comparing a "timed" xinput list
output] looks like the mouse is only "attached" to X after the udev finishes running scripts [while the udev script is not finished, the Magic Mouse doesn't show up in the xinput list
output].
I reason that, before the device is actually attached to the X, the xinput cannot do anything with it.
Therefore, need to have some mechanism to workaround this. Perhaps some sort of callback?
Can anyone shed a light?