![如何从脚本检测触摸屏设备?](https://linux22.com/image/861198/%E5%A6%82%E4%BD%95%E4%BB%8E%E8%84%9A%E6%9C%AC%E6%A3%80%E6%B5%8B%E8%A7%A6%E6%91%B8%E5%B1%8F%E8%AE%BE%E5%A4%87%EF%BC%9F.png)
我正在寻找一种可靠的方法来通过脚本检测笔记本电脑是否具有触摸屏设备。我知道我可以解析以下内容的输出:
$ xinput --list
⎡ Virtual core pointer id=2 [master pointer (3)]
⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer (2)]
⎜ ↳ ELAN Touchscreen id=10 [slave pointer (2)]
⎜ ↳ SynPS/2 Synaptics TouchPad 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)]
↳ Video Bus id=7 [slave keyboard (3)]
↳ Power Button id=8 [slave keyboard (3)]
↳ TOSHIBA Web Camera - HD id=9 [slave keyboard (3)]
↳ AT Translated Set 2 keyboard id=11 [slave keyboard (3)]
↳ Toshiba input device id=13 [slave keyboard (3)]
但理想情况下,我更愿意不依赖产品名称。实际上,我正在寻找一个标志,明确说明此类设备存在于我的测试系统中。
答案1
udev已经对输入设备进行了分类(参见https://wiki.ubuntu.com/X/InputConfiguration),支持的标志有:
ID_INPUT
所有输入设备都有此标志。
ID_INPUT_MOUSE
触摸屏和桌子也有此标志,因为根据它们可以产生的事件类型,它们可以充当鼠标。
ID_INPUT_TABLET
ID_INPUT_TOUCHSCREEN
ID_INPUT_JOYSTICK
ID _INPUT _KEY
键盘有这个,但也有像盖子开关这样只有几个按钮的东西
ID_INPUT_KEYBOARD
因此,检查被测系统是否具有触摸屏设备的简单方法是解析udevadm info --export-db
以下部分的输出:
P: /devices/pci0000:00/0000:00:14.0/usb2/2-7/2-7:1.0/input/input14
E: ABS=273800000000003
E: DEVPATH=/devices/pci0000:00/0000:00:14.0/usb2/2-7/2-7:1.0/input/input14
E: EV=10000b
E: ID_BUS=usb
E: ID_INPUT=1
E: ID_INPUT_TOUCHSCREEN=1
E: ID_MODEL=Touchscreen
E: ID_MODEL_ENC=Touchscreen
E: ID_MODEL_ID=0100
E: ID_PATH=pci-0000:00:14.0-usb-0:7:1.0
E: ID_PATH_TAG=pci-0000_00_14_0-usb-0_7_1_0
E: ID_REVISION=1110
E: ID_SERIAL=ELAN_Touchscreen
E: ID_TYPE=hid
E: ID_USB_DRIVER=usbhid
E: ID_USB_INTERFACES=:030000:
E: ID_USB_INTERFACE_NUM=00
E: ID_VENDOR=ELAN
E: ID_VENDOR_ENC=ELAN
E: ID_VENDOR_ID=04f3
E: KEY=400 0 0 0 0 0
E: MODALIAS=input:b0003v04F3p0100e0110-e0,1,3,14,k14A,ra0,1,2F,30,31,34,35,36,39,mlsfw
E: NAME="ELAN Touchscreen"
E: PHYS="usb-0000:00:14.0-7/input0"
E: PRODUCT=3/4f3/100/110
E: PROP=2
E: SUBSYSTEM=input
E: UDEV_LOG=3
E: UNIQ=""
E: USEC_INITIALIZED=815199186
最后使用的命令是:
udevadm info --export-db | grep ID_INPUT_TOUCHSCREEN=1
答案2
根据原始答案,我编写了一个小型解析脚本,提取触摸屏的名称。
#!/bin/bash
tmp_dir=$(mktemp -d)
pushd $tmp_dir > /dev/null
# Export whole database
udevadm info --export-db > udevdb.txt
csplit -s udevdb.txt /^$/ {*}
FILES=./xx*
for f in $FILES
do
if [[ ! -z $(grep ID_INPUT_TOUCHSCREEN=1 $f) ]] && [[ ! -z $(grep " NAME=*" $f) ]];
then
# Extract touchscreen name
grep " NAME=*" $f | cut -d "=" -f 2
fi
done
popd > /dev/null
rm -rf $tmp_dir
答案3
建立在Sylvain Pineau 的回答:使用awk
而不是grep
您可以获得所ID_INPUT_TOUCHSCREEN=1
发现的块的所有信息。
$ udevadm info --export-db | awk '/ID_INPUT_TOUCHSCREEN=1/' RS=
此外,如果您想获取触摸屏的设备路径,您可以这样做
$ udevadm info --export-db | awk '/ID_INPUT_TOUCHSCREEN=1/' RS= | grep "^E: DEVPATH="
E: DEVPATH=/devices/pci0000:00/0000:00:15.0/i2c_designware.0/i2c-0/i2c-WCOM486A:00/0018:056A:486A.0002/input/input19
E: DEVPATH=/devices/pci0000:00/0000:00:15.0/i2c_designware.0/i2c-0/i2c-WCOM486A:00/0018:056A:486A.0002/input/input19/event7
E: DEVPATH=/devices/pci0000:00/0000:00:15.0/i2c_designware.0/i2c-0/i2c-WCOM486A:00/0018:056A:486A.0002/input/input19/mouse2
E: DEVPATH=/devices/pci0000:00/0000:00:1d.0/0000:04:00.0/0000:05:04.0/0000:3b:00.0/0000:3c:04.0/0000:3e:00.0/0000:3f:04.0/0000:41:00.0/usb5/5-2/5-2.1/5-2.1:1.0/0003:222A:0001.000D/input/input29
E: DEVPATH=/devices/pci0000:00/0000:00:1d.0/0000:04:00.0/0000:05:04.0/0000:3b:00.0/0000:3c:04.0/0000:3e:00.0/0000:3f:04.0/0000:41:00.0/usb5/5-2/5-2.1/5-2.1:1.0/0003:222A:0001.000D/input/input29/event5
E: DEVPATH=/devices/pci0000:00/0000:00:1d.0/0000:04:00.0/0000:05:04.0/0000:3b:00.0/0000:3c:04.0/0000:3e:00.0/0000:3f:04.0/0000:41:00.0/usb5/5-2/5-2.1/5-2.1:1.0/0003:222A:0001.000D/input/input29/mouse0
或者获取触摸屏的名称:
$ udevadm info --export-db | awk '/ID_INPUT_TOUCHSCREEN=1/' RS= | grep "^E: NAME=" | cut -d '"' -f2
Wacom HID 486A Finger
ILITEK ILITEK-TP
(我在具有两个触摸屏的系统上运行了这些命令。其中一个是外部 USB-C 触摸屏,另一个是内置 PCI(或 i²C?)触摸屏。)