You are not logged in.

#1 2022-03-16 09:00:30

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,420

[SOLVED] udev rule to set mouse speed via xinput executed too early.

Hi,
I'd like to run a script to set some properties to my mouse:

koko@Gozer# cat /home/koko/scripts/set_mouse.sh 
#!/bin/bash
xinput list
xinput set-ptr-feedback "USB Optical Mouse" 30 12 10
xinput set-prop "USB Optical Mouse" "Device Accel Constant Deceleration" 1.25

It works if i launch by terminal with my user and if i launch it from root this way:

XAUTHORITY=/home/koko/.Xauthority DISPLAY=:0.0 /home/koko/set_mouse.sh

Given that the mouse idVendor and idProduct are correct , i made the following rule:

# cat /etc/udev/rules.d/20-flackymouse.rules 
#Profilo velocità e accelerazione per il mouse dell'ufficio che ogni tanto si disconnette e quindi perde le impostazioni.
ACTION=="add", ATTRS{idVendor}=="0461", ATTRS{idProduct}=="4d64", ENV{DISPLAY}=":0.0", ENV{XAUTHORITY}="/home/koko/.Xauthority", ENV{ID_CLASS}="mouse", RUN+="/usr/bin/sh -c 'sleep 0.1 ; /home/koko/scripts/set_mouse.sh &>/tmp/out.txt'"

I reloaded the rules:
sudo udevadm control --reload-rules

I simulated the device (dis)connection:
echo 2-1.3 > /sys/bus/usb/drivers/usb/unbind ; echo 2-1.3 > /sys/bus/usb/drivers/usb/bind

...but no properties were applied.

By inspecting /tmp/out.txt, i discovered the following:

⎡ Virtual core pointer                          id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎣ Virtual core keyboard                         id=3    [master keyboard (2)]
    ↳ Virtual core XTEST keyboard               id=5    [slave  keyboard (3)]
    ↳ Power Button                              id=6    [slave  keyboard (3)]
    ↳ Video Bus                                 id=7    [slave  keyboard (3)]
    ↳ Power Button                              id=8    [slave  keyboard (3)]
    ↳ LITEON Technology USB Multimedia Keyboard id=10   [slave  keyboard (3)]
    ↳ Eee PC WMI hotkeys                        id=11   [slave  keyboard (3)]
unable to find device 'USB Optical Mouse'
unable to find device USB Optical Mouse

It seems that by the moment the rule was executed xinput was not aware of the mouse.
I tried to insert a  dumb sleep (you can see it in the code posted) before the script execution, but it just blocked the whole usb reconnection process.

Maybe the rule has to be triggered later somehow?

Thanks.

Last edited by kokoko3k (2022-03-16 09:36:34)


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Offline

#2 2022-03-16 09:25:13

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,420

Re: [SOLVED] udev rule to set mouse speed via xinput executed too early.

And solved, exactly my issue:
https://unix.stackexchange.com/question … connection

[..]it appears that the connected device is not yet visible to xinput when the udev add rule is triggered.
[..]
I had the same problem. My solution was to trigger the udev rule with ACTION=="bind" instead of ACTION=="add" (Because I saw with udevadm monitor that it was last action when I connected a mouse)

-EDIT-
So the working rule is:

#Set speed parameters for my flacky mouse which disconnects and reconnects by its owns.

#Notes:
#       use ACTION="bind" instead of "add", because xinput cannot see the mouse otherwise.
#       use ENV{DEVTYPE}=="usb_device" to avoid executing the rule multiple times

#Refs:
#       https://unix.stackexchange.com/questions/439478/how-to-set-xinput-properties-triggered-by-udev-device-connection
#       https://bbs.archlinux.org/viewtopic.php?pid=2027114#p2027114

ENV{DEVTYPE}=="usb_device" ACTION=="bind", ATTRS{idVendor}=="0461", ATTRS{idProduct}=="4d64", ENV{DISPLAY}=":0.0", ENV{XAUTHORITY}="/home/koko/.Xauthority", ENV{ID_CLASS}="mouse", RUN+="/home/koko/scripts/set_mouse.sh"

#Same with debug:
#ENV{DEVTYPE}=="usb_device" ACTION=="bind", ATTRS{idVendor}=="0461", ATTRS{idProduct}=="4d64", ENV{DISPLAY}=":0.0", ENV{XAUTHORITY}="/home/koko/.Xauthority", ENV{ID_CLASS}="mouse", RUN+="/usr/bin/sh -c '/home/koko/scripts/set_mouse.sh &>>/tmp/prova.out'"

Last edited by kokoko3k (2022-03-16 09:38:19)


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Offline

Board footer

Powered by FluxBB