目录
查看当前系统中的输入设备
查看设备
查看事件
Linux 系统中事件处理流程
总体流程
app获取事件
Xorg到app的事件抓取
xinput的使用
xev命令
Libinput的使用
Linux input模块
参考资料
问题:采用无线鼠标后,虽然内核事件上报正常,但“APP获取事件”的代码不能获取到事件。
查看当前系统中的输入设备
查看设备
cat /proc/bus/input/devices
I: Bus=0003 Vendor=093a Product=2510 Version=0111
N: Name="PixA琀 USB Optical Mouse"
P: Phys=usb-0000:00:14.0-2/input0
S: Sysfs=/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/0003:093A:2510.000F/input/input35
U: Uniq=
H: Handlers=mouse0 event3
B: PROP=0
B: EV=17
B: KEY=70000 0 0 0 0
B: REL=103
B: MSC=10
I: Bus=0003 Vendor=1c4f Product=0002 Version=0110
N: Name="SIGMACHIP USB Keyboard"
P: Phys=usb-0000:00:14.0-1/input0
S: Sysfs=/devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/0003:1C4F:0002.0010/input/input36
U: Uniq=
H: Handlers=sysrq kbd leds event4
B: PROP=0
B: EV=120013
B: KEY=1000000000007 ff800000000007ff febeffdff3cfffff fffffffffffffffe
B: MSC=10
B: LED=7
无线鼠标
I: Bus=0003 Vendor=24ae Product=2013 Version=0110
N: Name="RAPOO Rapoo 2.4G Wireless Device"
P: Phys=usb-0000:00:14.0-3/input0
S: Sysfs=/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/0003:24AE:2013.0004/input/input18
U: Uniq=
H: Handlers=mouse1 event15
B: PROP=0
B: EV=17
B: KEY=1f0000 0 0 0 0
B: REL=103
B: MSC=10
查看事件
sudo hexdump /dev/input/event3
0000000 9554 60b5 0000 0000
0000010 0004 0004 0001 0009 9554 60b5 0000 0000
0000020 ee6f 0008 0000 0000 0001 0110 0001 0000
0000030 9554 60b5 0000 0000 ee6f 0008 0000 0000
0000040 0000 0000 0000 0000 9554 60b5 0000 0000
如上这些二进制数据有什么解析工具?
Linux 系统中事件处理流程
总体流程
在基于X11的系统中,以鼠标按键为例子,事件从硬件流转到应用经历的各个模块。
Generally, input events live through two stages:
Event generation and event processing.
In the event generation stage, input is gathered from the connected devices and transformed into abstract input events, the so-called InternalEvents.
In the processing stage, these InternalEvents events are converted to protocol events, depending on the event masks of the windows. An InternalEvent may be converted into a core event, an XI 1.x event or an XI2 event. More events such as enter and leave events are generated during the processing stage as well.
The event generation stage is part of the interrupt handling. The event processing stage is part of the processing loop (Dispatch()).
第一个阶段是在内核中实现的。第二个阶段是在xorg里面实现。
app获取事件
Xorg到app的事件抓取
通过工具xdebug
首先在问题复现后,在终端输入命令 xdebug -a --event=ButtonPress -p `pgrep term`
此时,点击鼠标左键,一直没有反应。而后恢复环境为正常状态,再点击鼠标左键,开始也没反应,但是经过几秒后,像是从某个阻塞状态回来了,上述xdebug可以抓到鼠标事件了。
xinput的使用
可以查看鼠标是否被禁用
xinput list
⎡ Virtual core pointer id=2 [master pointer (3)]
⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer (2)]
⎜ ↳ SIGMACHIP USB Keyboard Consumer Control id=10 [slave pointer (2)]
⎜ ↳ PixA琀 USB Optical Mouse id=12 [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)]
↳ Power Button id=7 [slave keyboard (3)]
↳ Sleep Button id=8 [slave keyboard (3)]
↳ SIGMACHIP USB Keyboard id=9 [slave keyboard (3)]
↳ SIGMACHIP USB Keyboard System Control id=11 [slave keyboard (3)]
↳ SIGMACHIP USB Keyboard Consumer Control id=13 [slave keyboard (3)]
xinput --list-props 12
Device 'PixA琀 USB Optical Mouse':
Device Enabled (155): 1
Coordinate Transformation Matrix (157): 1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000
libinput Natural Scrolling Enabled (291): 0
libinput Natural Scrolling Enabled Default (292): 0
libinput Scroll Methods Available (295): 0, 0, 1
libinput Scroll Method Enabled (296): 0, 0, 0
libinput Scroll Method Enabled Default (297): 0, 0, 0
libinput Button Scrolling Button (298): 2
libinput Button Scrolling Button Default (299): 2
libinput Middle Emulation Enabled (300): 0
libinput Middle Emulation Enabled Default (301): 0
libinput Accel Speed (302): 0.333333
libinput Accel Speed Default (303): 0.000000
libinput Accel Profiles Available (304): 1, 1
libinput Accel Profile Enabled (305): 1, 0
libinput Accel Profile Enabled Default (306): 1, 0
libinput Left Handed Enabled (307): 0
libinput Left Handed Enabled Default (308): 0
libinput Send Events Modes Available (276): 1, 0
libinput Send Events Mode Enabled (277): 0, 0
libinput Send Events Mode Enabled Default (278): 0, 0
Device Node (279): "/dev/input/event6"
Device Product ID (280): 2362, 9488
libinput Drag Lock Buttons (293): <no items>
libinput Horizontal Scroll Enabled (294): 1
grep -e "Using input driver 'libinput'" /var/log/Xorg.0.log
[ 24.313] (II) Using input driver 'libinput' for 'Power Button'
[ 24.353] (II) Using input driver 'libinput' for 'Power Button'
[ 24.367] (II) Using input driver 'libinput' for 'Sleep Button'
[ 24.384] (II) Using input driver 'libinput' for 'SIGMACHIP USB Keyboard'
[ 24.399] (II) Using input driver 'libinput' for 'SIGMACHIP USB Keyboard Consumer Control'
[ 24.419] (II) Using input driver 'libinput' for 'SIGMACHIP USB Keyboard System Control'
[ 24.439] (II) Using input driver 'libinput' for 'PixA琀 USB Optical Mouse'
[ 24.467] (II) Using input driver 'libinput' for 'SIGMACHIP USB Keyboard Consumer Control'
[ 1378.887] (II) Using input driver 'libinput' for 'RAPOO Rapoo 2.4G Wireless Device'
[ 1379.307] (II) Using input driver 'libinput' for 'RAPOO Rapoo 2.4G Wireless Device'
[ 1379.413] (II) Using input driver 'libinput' for 'RAPOO Rapoo 2.4G Wireless Device System Control'
[ 1379.440] (II) Using input driver 'libinput' for 'RAPOO Rapoo 2.4G Wireless Device Consumer Control'
[ 1379.460] (II) Using input driver 'libinput' for 'RAPOO Rapoo 2.4G Wireless Device Consumer Control'
xev命令
Libinput的使用
采用的版本为 :https://lists.freedesktop.org/archives/wayland-devel/2020-August/041578.html
1.16.0版本。
编译:
进入libinput-1.16.0/builddir目录,即可看到调试工具 ./libinput-debug-events
源代码如下:
https://gitlab.freedesktop.org/libinput/libinput/-/blob/main/tools/libinput-debug-events.c
使用,在故障鼠标上进行测试,按住鼠标左键,然后将无线鼠标的电源关闭,此时可以看到只有pressed事件,没有release事件。
将接收器拔掉后,可以看到有release事件产生。
通过usbmon 模块跟踪usb事件,也可以看到无释放按键URB,那么拔出接收器时,为什么会有事件产生呢?
Linux input模块
鼠标驱动
无线鼠标接收器拔出和插入的日志信息
[ 491.041401] usb 1-3: USB disconnect, device number 2
[ 507.751132] usb 1-3: new full-speed USB device number 5 using xhci_hcd
[ 507.902005] usb 1-3: New USB device found, idVendor=24ae, idProduct=1100, bcdDevice= 1.10
[ 507.902010] usb 1-3: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 507.902013] usb 1-3: Product: Rapoo 2.4G Wireless Device
[ 507.902016] usb 1-3: Manufacturer: RAPOO
[ 507.905686] input: RAPOO Rapoo 2.4G Wireless Device as /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/0003:24AE:1100.0004/input/input22
[ 507.906214] hid-generic 0003:24AE:1100.0004: input,hidraw1: USB HID v1.10 Mouse [RAPOO Rapoo 2.4G Wireless Device] on usb-0000:00:14.0-3/input0
[ 507.911130] hid-generic 0003:24AE:1100.0005: hiddev0,hidraw2: USB HID v1.10 Device [RAPOO Rapoo 2.4G Wireless Device] on usb-0000:00:14.0-3/input1
插入时,依次为usb->input->hid驱动
参考资料
Xorg处理事件的流程 https://www.x.org/wiki/Development/Documentation/InputEventProcessing/
X11实现的解析鼠标事件 https://stackoverflow.com/questions/14553435/how-to-listen-for-mouse-events-in-linux
xlib库获取事件的说明 https://tronche.com/gui/x/xlib/
xinput的使用 https://linuxhint.com/change_mouse_touchpad_settings_xinput_linux/
xev 命令的使用 https://bbs.archlinux.org/viewtopic.php?id=261138