Why Doesn’t This udev Rule Trigger Upon Device Removal?

mouseudevusbxmodmap

I have two mice, one which has an issue where the middle mouse button and button6 get triggered simultaneously. So I configured my ~/.Xmodmap to disable this button. At the same time, however, I wanted to be able to still use this button with my other mouse. So I wrote a udev rule that called xmodmap to change the button layout depending on which mouse is connected.


Setup

This is my current setup:

/etc/udev/rules.d/logitech-g7.rules:

# deactivate MB 6 when mouse 1 is connected
ACTION=="add", ENV{DEVTYPE}=="usb_device", ENV{ID_VENDOR_ID}=="046d", ENV{ID_MODEL_ID}=="c51a", RUN+="/home/user/.scripts/Troubleshooting/Bugfixes/mouseswitcher_wrapper 1", OWNER="user"
# restore defaults when mouse 1 is disconnected
ACTION=="remove", ENV{DEVTYPE}=="usb_device", ENV{ID_VENDOR_ID}=="046d", ENV{ID_MODEL_ID}=="c51a", RUN+="/home/user/.scripts/Troubleshooting/Bugfixes/mouseswitcher_wrapper 0", OWNER="user"

/home/user/.scripts/Troubleshooting/Bugfixes/mouseswitcher_wrapper:

#!/bin/bash
/home/user/.scripts/Troubleshooting/Bugfixes/mouseswitcher "$1"&

/home/user/.scripts/Troubleshooting/Bugfixes/mouseswitcher

#!/bin/bash
sleep 1
DISPLAY=":0.0"
HOME=/home/user/
XAUTHORITY=$HOME/.Xauthority
export DISPLAY XAUTHORITY HOME

if [[ "$1" == 1 ]] #differentiate between ADD (1) event and REMOVE (0) event
  then
      /usr/bin/xmodmap /home/user/.XmodmapG7 > /dev/null 2>&1
  else
      /usr/bin/xmodmap /home/user/.Xmodmap > /dev/null 2>&1
fi

If this seams a bit overcomplicated it's only because I stumbled on a number of problems whith udev, xmodmap and the DISPLAY variable. My scripts are based on this Q&A.


Current state

The ADD action works fine, the REMOVE action doesn't. Executing either of the RUN commands manually in a terminal works fine.


What I've tried so far

I found this forum post outlining why udev has problems with removal actions and accordingly replaced the ATTRS with ENV variables. But all of this was to no avail, the REMOVE trigger still doesn't work.

There has to be something I'm doing wrong. I would really appreciate it if someone around here more versed in udev and Linux in general could take a look at my case.


Additional information

Output of udevadm monitor --environment --udev when plugging the receiver in:

######ADD#######

monitor will print the received events for:
UDEV - the event which udev sends out after rule processing

UDEV  [21728.392805] add      /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3 (usb)
ACTION=add
BUSNUM=002
DEVNAME=/dev/bus/usb/002/037
DEVNUM=037
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3
DEVTYPE=usb_device
ID_BUS=usb
ID_MODEL=USB_Receiver
ID_MODEL_ENC=USB\x20Receiver
ID_MODEL_ID=c51a
ID_REVISION=4100
ID_SERIAL=Logitech_USB_Receiver
ID_USB_INTERFACES=:030102:030000:
ID_VENDOR=Logitech
ID_VENDOR_ENC=Logitech
ID_VENDOR_ID=046d
MAJOR=189
MINOR=164
PRODUCT=46d/c51a/4100
SEQNUM=3139
SUBSYSTEM=usb
TYPE=0/0/0
UDEV_LOG=3
UPOWER_VENDOR=Logitech, Inc.
USEC_INITIALIZED=21727880859

UDEV  [21728.393864] add      /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0 (usb)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0
DEVTYPE=usb_interface
INTERFACE=3/1/2
MODALIAS=usb:v046DpC51Ad4100dc00dsc00dp00ic03isc01ip02in00
PRODUCT=46d/c51a/4100
SEQNUM=3140
SUBSYSTEM=usb
TYPE=0/0/0
UDEV_LOG=3
USEC_INITIALIZED=21728393779

UDEV  [21728.395536] add      /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1 (usb)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1
DEVTYPE=usb_interface
INTERFACE=3/0/0
MODALIAS=usb:v046DpC51Ad4100dc00dsc00dp00ic03isc00ip00in01
PRODUCT=46d/c51a/4100
SEQNUM=3146
SUBSYSTEM=usb
TYPE=0/0/0
UDEV_LOG=3
USEC_INITIALIZED=21728395426

UDEV  [21728.395737] add      /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/0003:046D:C51A.004B (hid)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/0003:046D:C51A.004B
HID_ID=0003:0000046D:0000C51A
HID_NAME=Logitech USB Receiver
HID_PHYS=usb-0000:00:1d.0-1.3/input0
MODALIAS=hid:b0003g0001v0000046Dp0000C51A
SEQNUM=3141
SUBSYSTEM=hid
UDEV_LOG=3
USEC_INITIALIZED=21728395467

UDEV  [21728.398102] add      /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/0003:046D:C51A.004C (hid)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/0003:046D:C51A.004C
HID_ID=0003:0000046D:0000C51A
HID_NAME=Logitech USB Receiver
HID_PHYS=usb-0000:00:1d.0-1.3/input1
MODALIAS=hid:b0003g0001v0000046Dp0000C51A
SEQNUM=3147
SUBSYSTEM=hid
UDEV_LOG=3
USEC_INITIALIZED=21728397927

UDEV  [21728.398650] add      /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/0003:046D:C51A.004B/hidraw/hidraw2 (hidraw)
ACTION=add
DEVNAME=/dev/hidraw2
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/0003:046D:C51A.004B/hidraw/hidraw2
MAJOR=251
MINOR=2
SEQNUM=3145
SUBSYSTEM=hidraw
UDEV_LOG=3
USEC_INITIALIZED=21728398188

UDEV  [21728.399406] add      /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/0003:046D:C51A.004C/hidraw/hidraw3 (hidraw)
ACTION=add
DEVNAME=/dev/hidraw3
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/0003:046D:C51A.004C/hidraw/hidraw3
MAJOR=251
MINOR=3
SEQNUM=3151
SUBSYSTEM=hidraw
UDEV_LOG=3
USEC_INITIALIZED=21728399273

UDEV  [21728.400119] add      /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/usbmisc/hiddev1 (usbmisc)
ACTION=add
DEVNAME=/dev/usb/hiddev1
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/usbmisc/hiddev1
MAJOR=180
MINOR=1
SEQNUM=3150
SUBSYSTEM=usbmisc
UDEV_LOG=3
USEC_INITIALIZED=21727874845

UDEV  [21728.401438] add      /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/input/input73 (input)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/input/input73
EV=17
ID_BUS=usb
ID_INPUT=1
ID_INPUT_MOUSE=1
ID_MODEL=USB_Receiver
ID_MODEL_ENC=USB\x20Receiver
ID_MODEL_ID=c51a
ID_PATH=pci-0000:00:1d.0-usb-0:1.3:1.0
ID_PATH_TAG=pci-0000_00_1d_0-usb-0_1_3_1_0
ID_REVISION=4100
ID_SERIAL=Logitech_USB_Receiver
ID_TYPE=hid
ID_USB_DRIVER=usbhid
ID_USB_INTERFACES=:030102:030000:
ID_USB_INTERFACE_NUM=00
ID_VENDOR=Logitech
ID_VENDOR_ENC=Logitech
ID_VENDOR_ID=046d
KEY=ffff0000 0 0 0 0
MODALIAS=input:b0003v046DpC51Ae0111-e0,1,2,4,k110,111,112,113,114,115,116,117,118,119,11A,11B,11C,11D,11E,11F,r0,1,6,8,am4,lsfw
MSC=10
NAME="Logitech USB Receiver"
PHYS="usb-0000:00:1

Output of udevadm monitor --environment --udev when removing the receiver:

#######REMOVE#######


monitor will print the received events for:
UDEV - the event which udev sends out after rule processing

UDEV  [21800.789239] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/input/input75/mouse1 (input)
ACTION=remove
DEVLINKS=/dev/input/by-id/usb-Logitech_USB_Receiver-mouse /dev/input/by-path/pci-0000:00:1d.0-usb-0:1.3:1.0-mouse
DEVNAME=/dev/input/mouse1
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/input/input75/mouse1
ID_BUS=usb
ID_INPUT=1
ID_INPUT_MOUSE=1
ID_MODEL=USB_Receiver
ID_MODEL_ENC=USB\x20Receiver
ID_MODEL_ID=c51a
ID_PATH=pci-0000:00:1d.0-usb-0:1.3:1.0
ID_PATH_TAG=pci-0000_00_1d_0-usb-0_1_3_1_0
ID_REVISION=4100
ID_SERIAL=Logitech_USB_Receiver
ID_TYPE=hid
ID_USB_DRIVER=usbhid
ID_USB_INTERFACES=:030102:030000:
ID_USB_INTERFACE_NUM=00
ID_VENDOR=Logitech
ID_VENDOR_ENC=Logitech
ID_VENDOR_ID=046d
MAJOR=13
MINOR=33
SEQNUM=3178
SUBSYSTEM=input
UDEV_LOG=3
USEC_INITIALIZED=21797014959

UDEV  [21800.792866] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/input/input75/event6 (input)
ACTION=remove
DEVLINKS=/dev/input/by-id/usb-Logitech_USB_Receiver-event-mouse /dev/input/by-path/pci-0000:00:1d.0-usb-0:1.3:1.0-event-mouse
DEVNAME=/dev/input/event6
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/input/input75/event6
ID_BUS=usb
ID_INPUT=1
ID_INPUT_MOUSE=1
ID_MODEL=USB_Receiver
ID_MODEL_ENC=USB\x20Receiver
ID_MODEL_ID=c51a
ID_PATH=pci-0000:00:1d.0-usb-0:1.3:1.0
ID_PATH_TAG=pci-0000_00_1d_0-usb-0_1_3_1_0
ID_REVISION=4100
ID_SERIAL=Logitech_USB_Receiver
ID_TYPE=hid
ID_USB_DRIVER=usbhid
ID_USB_INTERFACES=:030102:030000:
ID_USB_INTERFACE_NUM=00
ID_VENDOR=Logitech
ID_VENDOR_ENC=Logitech
ID_VENDOR_ID=046d
MAJOR=13
MINOR=70
SEQNUM=3179
SUBSYSTEM=input
UDEV_LOG=3
USEC_INITIALIZED=21797013896

UDEV  [21800.797061] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/input/input75 (input)
ACTION=remove
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/input/input75
EV=17
ID_BUS=usb
ID_INPUT=1
ID_INPUT_MOUSE=1
ID_MODEL=USB_Receiver
ID_MODEL_ENC=USB\x20Receiver
ID_MODEL_ID=c51a
ID_PATH=pci-0000:00:1d.0-usb-0:1.3:1.0
ID_PATH_TAG=pci-0000_00_1d_0-usb-0_1_3_1_0
ID_REVISION=4100
ID_SERIAL=Logitech_USB_Receiver
ID_TYPE=hid
ID_USB_DRIVER=usbhid
ID_USB_INTERFACES=:030102:030000:
ID_USB_INTERFACE_NUM=00
ID_VENDOR=Logitech
ID_VENDOR_ENC=Logitech
ID_VENDOR_ID=046d
KEY=ffff0000 0 0 0 0 0 0 0 0
MODALIAS=input:b0003v046DpC51Ae0111-e0,1,2,4,k110,111,112,113,114,115,116,117,118,119,11A,11B,11C,11D,11E,11F,r0,1,6,8,am4,lsfw
MSC=10
NAME="Logitech USB Receiver"
PHYS="usb-0000:00:1d.0-1.3/input0"
PRODUCT=3/46d/c51a/111
PROP=0
REL=143
SEQNUM=3180
SUBSYSTEM=input
UDEV_LOG=3
UNIQ=""
USEC_INITIALIZED=21796479085

UDEV  [21800.797132] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/0003:046D:C51A.004D/hidraw/hidraw2 (hidraw)
ACTION=remove
DEVNAME=/dev/hidraw2
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/0003:046D:C51A.004D/hidraw/hidraw2
MAJOR=251
MINOR=2
SEQNUM=3181
SUBSYSTEM=hidraw
UDEV_LOG=3
USEC_INITIALIZED=21800796369

UDEV  [21800.797172] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/0003:046D:C51A.004D (hid)
ACTION=remove
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/0003:046D:C51A.004D
HID_ID=0003:0000046D:0000C51A
HID_NAME=Logitech USB Receiver
HID_PHYS=usb-0000:00:1d.0-1.3/input0
MODALIAS=hid:b0003g0001v0000046Dp0000C51A
SEQNUM=3182
SUBSYSTEM=hid
UDEV_LOG=3
USEC_INITIALIZED=21800796501

UDEV  [21800.798537] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0 (usb)
ACTION=remove
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0
DEVTYPE=usb_interface
INTERFACE=3/1/2
MODALIAS=usb:v046DpC51Ad4100dc00dsc00dp00ic03isc01ip02in00
PRODUCT=46d/c51a/4100
SEQNUM=3183
SUBSYSTEM=usb
TYPE=0/0/0
UDEV_LOG=3
USEC_INITIALIZED=21800796538

UDEV  [21800.814181] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/input/input76/event7 (input)
ACTION=remove
DEVLINKS=/dev/input/by-id/usb-Logitech_USB_Receiver-event-if01 /dev/input/by-path/pci-0000:00:1d.0-usb-0:1.3:1.1-event
DEVNAME=/dev/input/event7
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/input/input76/event7
ID_BUS=usb
ID_INPUT=1
ID_INPUT_KEY=1
ID_MODEL=USB_Receiver
ID_MODEL_ENC=USB\x20Receiver
ID_MODEL_ID=c51a
ID_PATH=pci-0000:00:1d.0-usb-0:1.3:1.1
ID_PATH_TAG=pci-0000_00_1d_0-usb-0_1_3_1_1
ID_REVISION=4100
ID_SERIAL=Logitech_USB_Receiver
ID_TYPE=hid
ID_USB_DRIVER=usbhid
ID_USB_INTERFACES=:030102:030000:
ID_USB_INTERFACE_NUM=01
ID_VENDOR=Logitech
ID_VENDOR_ENC=Logitech
ID_VENDOR_ID=046d
MAJOR=13
MINOR=71
SEQNUM=3184
SUBSYSTEM=input
UDEV_LOG=3
USEC_INITIALIZED=21797013933
XKBLAYOUT=de
XKBMODEL=pc105

UDEV  [21800.816765] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/input/input76 (input)
ABS=1 0
ACTION=remove
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/input/input76
EV=1f
ID_BUS=usb
ID_INPUT=1
ID_INPUT_KEY=1
ID_MODEL=USB_Receiver
ID_MODEL_ENC=USB\x20Receiver
ID_MODEL_ID=c51a
ID_PATH=pci-0000:00:1d.0-usb-0:1.3:1.1
ID_PATH_TAG=pci-0000_00_1d_0-usb-0_1_3_1_1
ID_REVISION=4100
ID_SERIAL=Logitech_USB_Receiver
ID_TYPE=hid
ID_USB_DRIVER=usbhid
ID_USB_INTERFACES=:030102:030000:
ID_USB_INTERFACE_NUM=01
ID_VENDOR=Logitech
ID_VENDOR_ENC=Logitech
ID_VENDOR_ID=046d
KEY=4837fff 72ff32d bf544446 0 0 1 20f90 8b17c000 677bfa d9415fed 9ed680 4400 0 10000002
MODALIAS=input:b0003v046DpC51Ae0111-e0,1,2,3,4,k71,72,73,74,77,80,82,83,85,86,87,88,89,8A,8B,8C,8E,90,96,98,9B,9C,9E,9F,A1,A3,A4,A5,A6,A7,A8,A9,AB,AC,AD,AE,B0,B1,B2,B5,B6,CE,CF,D0,D1,D2,D4,D8,D9,DB,DF,E4,E7,E8,E9,EA,EB,F1,100,161,162,166,16A,16E,172,174,176,178,179,17A,17B,17C,17D,17F,180,182,183,185,188,189,18C,18D,18E,18F,190,191,192,193,195,198,199,19A,1A0,1A1,1A2,1A3,1A4,1A5,1A6,1A7,1A8,1A9,1AA,1AB,1AC,1AD,1AE,1B0,1B1,1B7,1BA,r6,a20,m4,lsfw
MSC=10
NAME="Logitech USB Receiver"
PHYS="usb-0000:00:1d.0-1.3/input1"
PRODUCT=3/46d/c51a/111
PROP=0
REL=40
SEQNUM=3185
SUBSYSTEM=input
UDEV_LOG=3
UNIQ=""
USEC_INITIALIZED=21796482192

UDEV  [21800.817249] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/usbmisc/hiddev1 (usbmisc)
ACTION=remove
DEVNAME=/dev/usb/hiddev1
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/usbmisc/hiddev1
MAJOR=180
MINOR=1
SEQNUM=3186
SUBSYSTEM=usbmisc
UDEV_LOG=3
USEC_INITIALIZED=21800816392

UDEV  [21800.818490] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/0003:046D:C51A.004E/hidraw/hidraw3 (hidraw)
ACTION=remove
DEVNAME=/dev/hidraw3
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/0003:046D:C51A.004E/hidraw/hidraw3
MAJOR=251
MINOR=3
SEQNUM=3187
SUBSYSTEM=hidraw
UDEV_LOG=3
USEC_INITIALIZED=21800816472

UDEV  [21800.818536] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/0003:046D:C51A.004E (hid)
ACTION=remove
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1/0003:046D:C51A.004E
HID_ID=0003:0000046D:0000C51A
HID_NAME=Logitech USB Receiver
HID_PHYS=usb-0000:00:1d.0-1.3/input1
MODALIAS=hid:b0003g0001v0000046Dp0000C51A
SEQNUM=3188
SUBSYSTEM=hid
UDEV_LOG=3
USEC_INITIALIZED=21800816908

UDEV  [21800.818580] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1 (usb)
ACTION=remove
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.1
DEVTYPE=usb_interface
INTERFACE=3/0/0
MODALIAS=usb:v046DpC51Ad4100dc00dsc00dp00ic03isc00ip00in01
PRODUCT=46d/c51a/4100
SEQNUM=3189
SUBSYSTEM=usb
TYPE=0/0/0
UDEV_LOG=3
USEC_INITIALIZED=21800816945

UDEV  [21800.818645] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3 (usb)
ACTION=remove
BUSNUM=002
DEVNAME=/dev/bus/usb/002/038
DEVNUM=038
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3
DEVTYPE=usb_device
ID_BUS=usb
ID_MODEL=USB_Receiver
ID_MODEL_ENC=USB\x20Receiver
ID_MODEL_ID=c51a
ID_REVISION=4100
ID_SERIAL=Logitech_USB_Receiver
ID_USB_INTERFACES=:030102:030000:
ID_VENDOR=Logitech
ID_VENDOR_ENC=Logitech
ID_VENDOR_ID=046d
MAJOR=189
MINOR=165
PRODUCT=46d/c51a/4100
SEQNUM=3190
SUBSYSTEM=usb
TYPE=0/0/0
UDEV_LOG=3
UPOWER_VENDOR=Logitech, Inc.
USEC_INITIALIZED=21796488421

Best Answer

Alternative solution

After finishing writing the answer bellow, I realized that what you're trying to achieve could be accomplished much more elegantly with the help of xinput or even using Xorg's config. Make sure to read the documentation about controlling input devices in Xorg.

Using udev (an answer to your question)

According to my tests there are two problems with your rules:

  1. At least on my Ubuntu GNU/Linux 12.04, no checks for ENV{DEVTYPE} ever match (even though it is reported properly by udevadm info and udevadm monitor). This is the reason you don't even see the add rule matching.
  2. You should remove the OWNER assignment from the remove rule. It doesn't make sense and udev ignores the rule altogether.

Please try with the following two rules and see if they solve the problem.

ACTION=="add", ENV{ID_MODEL}=="USB_Receiver", ENV{ID_VENDOR_ID}=="046d", ENV{ID_MODEL_ID}=="c51a", RUN+="/home/user/.scripts/Troubleshooting/Bugfixes/mouseswitcher_wrapper 1", OWNER="user"
ACTION=="remove", ENV{ID_MODEL}=="USB_Receiver", ENV{ID_VENDOR_ID}=="046d", ENV{ID_MODEL_ID}=="c51a", RUN+="/home/user/.scripts/Troubleshooting/Bugfixes/mouseswitcher_wrapper 0"

Additional considerations

Several things to consider:

  1. it's much cleaner to use ATTR{idVendor} and ATTR{idProduct} for classification of the devices. You can safely change the add rule to use them, instead of ENV, but I left them like this for the sake of simplicity. Currently add and remove rules are almost identical.
  2. Consider the security implications of running a script as root in a directory that is writable by other users. In your particular case, it's not a serious issue, but I wouldn't call it a good security practice. IMO it would be much better putting the script in /usr/local/bin/, making it owned by root.root and mode 0755.
  3. Make sure that you want the mouse device to be owned by your user, there is really no need for that, PolicyKit and Xorg should be able to handle root-owned devices without any problems.

If you don't need to change the owner of the device and your setup works with a root-owned device, then you could simplify your two udev rules to this one:

ACTION=="add|remove", ENV{ID_MODEL}=="USB_Receiver", ENV{ID_VENDOR_ID}=="046d", ENV{ID_MODEL_ID}=="c51a", RUN+="/home/user/.scripts/Troubleshooting/Bugfixes/mouseswitcher_wrapper $env{ACTION}"

This will call your script with the corresponding action - remove or add, so you will need to modify your script to handle these arguments.

To prevent the rule to match (and the script to run) more than once you need to make the rule more specific: the rule matches for each "input" (buttons, etc) of the mouse. That's why it's executed repeatedly. Try adding ENV{ID_TYPE}!="hid" or ENV{ID_USB_DRIVER}!="*hid*" and see if it works as there is only one device that is not HID - the top usb_device.

PS: If you want to make your mouseswitcher script more flexible, and are running a system with ConsoleKit, you could use ck-list-sessions to get the user that is currently logged in and use that info for setting the XAUTHORITY variable.

Related Question