You are not logged in.

#1 2020-08-16 00:53:06

silocoder
Member
Registered: 2020-08-16
Posts: 3

Need to be notified when my Magic Mouse connects to Bluetooth

When Magic Mouse bluetooth connects I want to run a script to map B2 to B1. The Magic Mouse doesn't use a dongle and depends on the built in laptop bluetooth (Unlike Logitech)

The way I do it now is straight forward and works but I have to manually run the shell script. In addition the mouse may not be connected until few seconds after login so putting code in /etc/profile doesn't cut it.

I use xinput so here is the shell script I use which finds all mouse then sets xinput set-button-map of the mouse id to 1 1 in effect pointing b2 to b1.

#!/bin/bash
# Map middle button 2 to left button 1 for each mouse pointer in xinput using mouse id
xinput list | grep -i -E "mouse.*pointer" | awk -F= '{ print $2}' | awk '{print $1}' | \
xargs -I{} sh -c 'echo {}; xinput set-button-map {} 1 1;'

Any ideas how to do this?

P.S. Found this article which helps with scrolling https://wiki.archlinux.org/index.php/Bluetooth_mouse

Last edited by silocoder (2020-08-16 01:51:55)

Offline

#2 2020-08-16 00:56:48

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: Need to be notified when my Magic Mouse connects to Bluetooth

Udev rule?


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#3 2020-08-16 01:40:40

silocoder
Member
Registered: 2020-08-16
Posts: 3

Re: Need to be notified when my Magic Mouse connects to Bluetooth

I don't have a product rule because I have no clue how to create one. But this is the info from the device

$ xinput list-props 15
Device 'Apple Wireless Mouse':
	Device Enabled (170):	1
.......
	Device Node (292):	"/dev/input/event16"
	Device Product ID (293):	1452, 781
......
~$ udevadm info --attribute-walk --path=$(udevadm info --query=path --name=/dev/input/event16)

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/usb3/3-11/3-11:1.0/bluetooth/hci0/hci0:21/0005:05AC:030D.0007/input/input28/event16':
    KERNEL=="event16"
    SUBSYSTEM=="input"
    DRIVER==""

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb3/3-11/3-11:1.0/bluetooth/hci0/hci0:21/0005:05AC:030D.0007/input/input28':
    KERNELS=="input28"
    SUBSYSTEMS=="input"
    DRIVERS==""
    ATTRS{uniq}=="d8:30:62:44:bd:7d"
    ATTRS{name}=="Apple Wireless Mouse"
    ATTRS{properties}=="0"
    ATTRS{phys}=="80:56:f2:a5:ba:0c"

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb3/3-11/3-11:1.0/bluetooth/hci0/hci0:21/0005:05AC:030D.0007':
    KERNELS=="0005:05AC:030D.0007"
    SUBSYSTEMS=="hid"
    DRIVERS=="magicmouse"
    ATTRS{country}=="00"

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb3/3-11/3-11:1.0/bluetooth/hci0/hci0:21':
    KERNELS=="hci0:21"
    SUBSYSTEMS=="bluetooth"
    DRIVERS==""

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb3/3-11/3-11:1.0/bluetooth/hci0':
    KERNELS=="hci0"
    SUBSYSTEMS=="bluetooth"
    DRIVERS==""

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb3/3-11/3-11:1.0':
    KERNELS=="3-11:1.0"
    SUBSYSTEMS=="usb"
    DRIVERS=="btusb"
    ATTRS{bInterfaceNumber}=="00"
    ATTRS{supports_autosuspend}=="1"
    ATTRS{bInterfaceClass}=="e0"
    ATTRS{authorized}=="1"
    ATTRS{bNumEndpoints}=="03"
    ATTRS{bAlternateSetting}==" 0"
    ATTRS{bInterfaceSubClass}=="01"
    ATTRS{bInterfaceProtocol}=="01"

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb3/3-11':
    KERNELS=="3-11"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{version}==" 1.10"
    ATTRS{bMaxPower}=="100mA"
    ATTRS{speed}=="12"
    ATTRS{idVendor}=="0cf3"
    ATTRS{maxchild}=="0"
    ATTRS{quirks}=="0x0"
    ATTRS{bNumInterfaces}==" 2"
    ATTRS{bMaxPacketSize0}=="64"
    ATTRS{bDeviceProtocol}=="01"
    ATTRS{busnum}=="3"
    ATTRS{configuration}==""
    ATTRS{ltm_capable}=="no"
    ATTRS{devpath}=="11"
    ATTRS{tx_lanes}=="1"
    ATTRS{rx_lanes}=="1"
    ATTRS{bcdDevice}=="0002"
    ATTRS{bDeviceSubClass}=="01"
    ATTRS{devnum}=="4"
    ATTRS{bDeviceClass}=="e0"
    ATTRS{urbnum}=="177048"
    ATTRS{removable}=="fixed"
    ATTRS{authorized}=="1"
    ATTRS{bmAttributes}=="e0"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{idProduct}=="3004"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{avoid_reset_quirk}=="0"

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb3':
    KERNELS=="usb3"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{maxchild}=="14"
    ATTRS{serial}=="0000:00:14.0"
    ATTRS{quirks}=="0x0"
    ATTRS{devpath}=="0"
    ATTRS{bDeviceClass}=="09"
    ATTRS{removable}=="unknown"
    ATTRS{urbnum}=="120"
    ATTRS{busnum}=="3"
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{devnum}=="1"
    ATTRS{bcdDevice}=="0504"
    ATTRS{configuration}==""
    ATTRS{manufacturer}=="Linux 5.4.0-42-generic xhci-hcd"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bmAttributes}=="e0"
    ATTRS{authorized}=="1"
    ATTRS{tx_lanes}=="1"
    ATTRS{bDeviceProtocol}=="01"
    ATTRS{ltm_capable}=="no"
    ATTRS{speed}=="480"
    ATTRS{avoid_reset_quirk}=="0"
    ATTRS{product}=="xHCI Host Controller"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{bMaxPower}=="0mA"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{bMaxPacketSize0}=="64"
    ATTRS{idVendor}=="1d6b"
    ATTRS{rx_lanes}=="1"
    ATTRS{authorized_default}=="1"
    ATTRS{version}==" 2.00"
    ATTRS{interface_authorized_default}=="1"
    ATTRS{idProduct}=="0002"

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

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

Offline

#4 2020-08-16 05:37:01

d_fajardo
Member
Registered: 2017-07-28
Posts: 1,687

Re: Need to be notified when my Magic Mouse connects to Bluetooth

You already have the shell script you that does what you need and you have the attributes of the device so you can then create a udev rule to call the script when the device is attached.
Again refer to the udev wiki.

Offline

#5 2020-08-17 01:09:23

silocoder
Member
Registered: 2020-08-16
Posts: 3

Re: Need to be notified when my Magic Mouse connects to Bluetooth

So I read the udev rules and this seems to work with only one problem. The script runs twice.

ACTION=="add", ATTRS{name}=="Apple Wireless Mouse", ATTRS{uniq}=="d8:30:62:44:bd:7d", \
    RUN+="/bin/bash -c '/home/mytest/playsound.sh'"

These are the first couple of lines from udevadm monitor. Maybe I am missing something on the rules?

KERNEL[11276.305538] add      /devices/pci0000:00/0000:00:14.0/usb3/3-11/3-11:1.0/bluetooth/hci0/hci0:22 (bluetooth)
UDEV  [11276.307874] add      /devices/pci0000:00/0000:00:14.0/usb3/3-11/3-11:1.0/bluetooth/hci0/hci0:22 (bluetooth)
KERNEL[11276.413211] add      /devices/pci0000:00/0000:00:14.0/usb3/3-11/3-11:1.0/bluetooth/hci0/hci0:22/0005:05AC:030D.0025 (hid)

PS: Well I fixed it by adding some code in my script to check for environment variables the first time, that don't exists the other times.  In this case was $NAME

Last edited by silocoder (2020-08-17 02:23:09)

Offline

Board footer

Powered by FluxBB