Udev

udev - 連接無線滑鼠時自動禁用觸控板

  • September 26, 2018

我始終將加密狗放在筆記型電腦中,用於無線滑鼠,並希望使用 udev 執行一個腳本,在滑鼠實際打開時禁用我的觸控板。我已經編寫了規則和腳本,但似乎無法讓它們發揮作用。

以下是 OFF/ON 的 UDEV 動作

離開

UDEV  [404.700104] change   /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.2/0003:046D:C52B.0003/0003:046D:4060.0004/power_supply
ACTION=change                                                                                                                     DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.2/0003:046D:C52B.0003/0003:046D:4060.0004/power_supply/hidpp_battery_0    
POWER_SUPPLY_CAPACITY_LEVEL=Unknown                                                                                               
POWER_SUPPLY_MANUFACTURER=Logitech                                                                                                
POWER_SUPPLY_MODEL_NAME=Wireless Mouse MX Master                                                                                  
POWER_SUPPLY_NAME=hidpp_battery_0                                                                                                 
POWER_SUPPLY_ONLINE=0                                                                                                             
POWER_SUPPLY_SCOPE=Device                                                                                                         
POWER_SUPPLY_SERIAL_NUMBER=4060-fb-d9-a0-50                                                                                       
POWER_SUPPLY_STATUS=Unknown                                                                                                       
SEQNUM=3540                                                                                                                       
SUBSYSTEM=power_supply                                                                                                            
USEC_INITIALIZED=404691556                                                                                                        

UDEV  [406.390053] change   /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.2/0003:046D:C52B.0003/0003:046D:4060.0004/power_supply
ACTION=change                                                                                                                     
DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.2/0003:046D:C52B.0003/0003:046D:4060.0004/power_supply/hidpp_battery_0     POWER_SUPPLY_CAPACITY_LEVEL=Normal                                                                                                
POWER_SUPPLY_MANUFACTURER=Logitech                                                                                                
POWER_SUPPLY_MODEL_NAME=Wireless Mouse MX Master                                                                                  
POWER_SUPPLY_NAME=hidpp_battery_0                                                                                                 
POWER_SUPPLY_ONLINE=1                                                                                                             
POWER_SUPPLY_SCOPE=Device                                                                                                         
POWER_SUPPLY_SERIAL_NUMBER=4060-fb-d9-a0-50                                                                                       
POWER_SUPPLY_STATUS=Discharging                                                                                                   
SEQNUM=3541                                                                                                                       
SUBSYSTEM=power_supply                                                                                                            
USEC_INITIALIZED=406373747 

我的規則

ACTION=="change", ATTR{serial_number}=="4060-fb-d9-a0-50", RUN+="/home/sly/bin/mouse-plug.sh $attr{online}"

mouse-plug.sh

#!/bin/sh                                                                                                                         

TOUCHPADDEVICE=$(($(xinput list | grep -i touchpad | cut -d= -f2 | cut -d[ -f1)+0))                                               

if [ $1 -eq 1 ]; then                                                                                                             
 xinput disable $TOUCHPADDEVICE                                                                                                  
else                                                                                                                              
 xinput enable $TOUCHPADDEVICE                                                                                                   
fi

設備的udev資訊

$ udevadm info -a -p /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.2/0003:046D:C52B.0003/0003:046D:4060.0004/power_supply/hidpp_battery_0

Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

 looking at device '/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.2/0003:046D:C52B.0003/0003:046D:4060.0004/power_supply/hidpp_battery_0':
   KERNEL=="hidpp_battery_0"
   SUBSYSTEM=="power_supply"
   DRIVER==""
   ATTR{capacity_level}=="Normal"
   ATTR{manufacturer}=="Logitech"
   ATTR{model_name}=="Wireless Mouse MX Master"
   ATTR{online}=="1"
   ATTR{scope}=="Device"
   ATTR{serial_number}=="4060-fb-d9-a0-50"
   ATTR{status}=="Discharging"
   ATTR{type}=="Battery"

 looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.2/0003:046D:C52B.0003/0003:046D:4060.0004':
   KERNELS=="0003:046D:4060.0004"
   SUBSYSTEMS=="hid"
   DRIVERS=="logitech-hidpp-device"
   ATTRS{country}=="00"

 looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.2/0003:046D:C52B.0003':
   KERNELS=="0003:046D:C52B.0003"
   SUBSYSTEMS=="hid"
   DRIVERS=="logitech-djreceiver"
   ATTRS{country}=="00"

 looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.2':
   KERNELS=="1-2:1.2"
   SUBSYSTEMS=="usb"
   DRIVERS=="usbhid"
   ATTRS{authorized}=="1"
   ATTRS{bAlternateSetting}==" 0"
   ATTRS{bInterfaceClass}=="03"
   ATTRS{bInterfaceNumber}=="02"
   ATTRS{bInterfaceProtocol}=="00"
   ATTRS{bInterfaceSubClass}=="00"
   ATTRS{bNumEndpoints}=="01"
   ATTRS{supports_autosuspend}=="1"

 looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-2':
   KERNELS=="1-2"
   SUBSYSTEMS=="usb"
   DRIVERS=="usb"
   ATTRS{authorized}=="1"
   ATTRS{avoid_reset_quirk}=="0"
   ATTRS{bConfigurationValue}=="1"
   ATTRS{bDeviceClass}=="00"
   ATTRS{bDeviceProtocol}=="00"
   ATTRS{bDeviceSubClass}=="00"
   ATTRS{bMaxPacketSize0}=="8"
   ATTRS{bMaxPower}=="98mA"
   ATTRS{bNumConfigurations}=="1"
   ATTRS{bNumInterfaces}==" 3"
   ATTRS{bcdDevice}=="1207"
   ATTRS{bmAttributes}=="a0"
   ATTRS{busnum}=="1"
   ATTRS{configuration}=="RQR12.07_B0029"
   ATTRS{devnum}=="2"
   ATTRS{devpath}=="2"
   ATTRS{devspec}=="          (null)"
   ATTRS{idProduct}=="c52b"
   ATTRS{idVendor}=="046d"
   ATTRS{ltm_capable}=="no"
   ATTRS{manufacturer}=="Logitech"
   ATTRS{maxchild}=="0"
   ATTRS{product}=="USB Receiver"
   ATTRS{quirks}=="0x0"
   ATTRS{removable}=="removable"
   ATTRS{rx_lanes}=="1"
   ATTRS{speed}=="12"
   ATTRS{tx_lanes}=="1"
   ATTRS{urbnum}=="69831"
   ATTRS{version}==" 2.00"

 looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1':
   KERNELS=="usb1"
   SUBSYSTEMS=="usb"
   DRIVERS=="usb"
   ATTRS{authorized}=="1"
   ATTRS{authorized_default}=="1"
   ATTRS{avoid_reset_quirk}=="0"
   ATTRS{bConfigurationValue}=="1"
   ATTRS{bDeviceClass}=="09"
   ATTRS{bDeviceProtocol}=="01"
   ATTRS{bDeviceSubClass}=="00"
   ATTRS{bMaxPacketSize0}=="64"
   ATTRS{bMaxPower}=="0mA"
   ATTRS{bNumConfigurations}=="1"
   ATTRS{bNumInterfaces}==" 1"
   ATTRS{bcdDevice}=="0418"
   ATTRS{bmAttributes}=="e0"
   ATTRS{busnum}=="1"
   ATTRS{configuration}==""
   ATTRS{devnum}=="1"
   ATTRS{devpath}=="0"
   ATTRS{devspec}=="          (null)"
   ATTRS{idProduct}=="0002"
   ATTRS{idVendor}=="1d6b"
   ATTRS{interface_authorized_default}=="1"
   ATTRS{ltm_capable}=="no"
   ATTRS{manufacturer}=="Linux 4.18.5-arch1-1-ARCH xhci-hcd"
   ATTRS{maxchild}=="16"
   ATTRS{product}=="xHCI Host Controller"
   ATTRS{quirks}=="0x0"
   ATTRS{removable}=="unknown"
   ATTRS{rx_lanes}=="1"
   ATTRS{serial}=="0000:00:14.0"
   ATTRS{speed}=="480"
   ATTRS{tx_lanes}=="1"
   ATTRS{urbnum}=="279"
   ATTRS{version}==" 2.00"

 looking at parent device '/devices/pci0000:00/0000:00:14.0':
   KERNELS=="0000:00:14.0"
   SUBSYSTEMS=="pci"
   DRIVERS=="xhci_hcd"
   ATTRS{ari_enabled}=="0"
   ATTRS{broken_parity_status}=="0"
   ATTRS{class}=="0x0c0330"
   ATTRS{consistent_dma_mask_bits}=="64"
   ATTRS{d3cold_allowed}=="1"
   ATTRS{device}=="0xa12f"
   ATTRS{devspec}==""
   ATTRS{dma_mask_bits}=="64"
   ATTRS{driver_override}=="(null)"
   ATTRS{enable}=="1"
   ATTRS{irq}=="128"
   ATTRS{local_cpulist}=="0-7"
   ATTRS{local_cpus}=="ff"
   ATTRS{msi_bus}=="1"
   ATTRS{numa_node}=="-1"
   ATTRS{revision}=="0x31"
   ATTRS{subsystem_device}=="0x07be"
   ATTRS{subsystem_vendor}=="0x1028"
   ATTRS{vendor}=="0x8086"

 looking at parent device '/devices/pci0000:00':
   KERNELS=="pci0000:00"
   SUBSYSTEMS==""
   DRIVERS==""

mouse-plug.sh 會按預期啟用和禁用觸摸板,但不會在 UDEV 事件時觸發。

mouse-plug 需要修改為

#!/bin/sh                                                                                                                         

export DISPLAY=:0
export XAUTHORITY="/home/sly/.Xauthority"
TOUCHPADDEVICE=$(($(xinput list | grep -i touchpad | cut -d= -f2 | cut -d[ -f1)+0))                                               

if [ $1 -eq 1 ]; then                                                                                                             
 xinput disable $TOUCHPADDEVICE                                                                                                  
else                                                                                                                              
 xinput enable $TOUCHPADDEVICE                                                                                                   
fi

這可以通過傳遞使用者名並通過 udev 動態顯示來改善,但對於單使用者筆記型電腦,它可以像這樣正常工作。

引用自:https://unix.stackexchange.com/questions/471447