为了我的手腕健康
鼠标用多了,手腕和小臂总会感觉不舒服
换成数位板之后,就没有问题了
但是最近数位板的内置电池似乎开始鼓包了(有点担心…),加上我已经眼馋 Magic Tackpad 2 许久
为了自己的身体健康,花重金购入了果家的 Magic Tackpad 2
简单开箱,比想象中要小,蛮精致的小板子
蓝牙连接电脑,Windows 和 Linux 下可以直接用,但只限于模拟鼠标左键和右键单击,不支持 Windows 的三指切换任务等手势
所以要进一步配置一下这个小板子
Windows 配置
Windows 下配置非常简单,GitHub 上有人做好驱动了,直接安装就可以
项目地址:Github - imbushuo/mac-precision-touchpad
按照说明装好之后 Trackpad 就可以支持多指手势,和笔记本自带的触控板操作没有区别
Linux 配置
Linux 下配置稍微麻烦一点,配置的过程中踩了一些许多坑,记录一下
第一次配置,失败
Manjaro 设置里面有触控板的各种配置,但只限于基本功能,不能设置多指手势
而且不知道为什么,自带的设置很容易失效,从睡眠中唤醒电脑之后,触控板会恢复默认设置,比如指针速度、滚动方向之类
在设置中再次更改也不会生效,奇怪
没办法,只能放弃自带的设置中心,自己动手配置
首先在网上找一篇教程:Configure Apple Magic Trackpad 2 Gestures on Manjaro KDE
似乎刚好符合我的需求,大概浏览一下,开始按照他的方法配置
-
sudo pacman -S libinput-gestures
安装libinput-gestures这个包,可以让我们根据自己的需要配置手势 -
sudo gpasswd -a $USER input
把自己的用户添加到input用户组里面 -
然后就可以启动
libinput-gestures服务了libinput-gestures-setup start启动libinput-gestures-setup stop停止- 还有
restart|autostart|status这几个比较常用
-
启动似乎没问题,接下来要设置手势
配置文件可以放 2 个地方
/etc/libinput-gestures.conf是系统级的配置文件,有一些默认的配置
~/.config/libinput-gestures.conf是各个用户自己的配置,需要自己写
一般就在~/.config/libinput-gestures.conf里面,根据自己的需要配置就行 -
写好之后,重启一下服务
libinput-gestures-setup restart
但是我按照他的步骤设置好之后,发现任何手势都没有反应
用 libinput-gestures -d 开 debug 看,不管在触控板上做什么操作,libinput-gestures 都没有输出,似乎没有收到触控板的 event
折腾了一会儿,无果,暂时作罢(一度以为我折腾不好这个板子了
第二次折腾,找到原因了
过了两周,偶然翻到 libinput 这个包,想到 libinput-gestures 可能就是从 libinput 接收触控板事件,再根据配置文件响应手势
之前看 libinput-gestures 没有读到触控板操作,那么 libinput 呢?
-
先看看
libinput怎么操作Terminal window $ libinput -hUsage: libinput [--help|--version] <command> [<args>]Global options:--help ...... show this help and exit--version ... show version information and exitCommands:list-devicesList all devices with their default configuration optionsdebug-eventsPrint events to stdoutdebug-guiDisplay a simple GUI to visualize libinput's events.measure <feature>Measure various device properties. See the man page for more infoanalyze <feature>Analyze device events. See the man page for more inforecordRecord event stream from a device node. See the man page for more inforeplayReplay a previously recorded event stream. See the man page for more info -
看了看,我应该先找一下
libinput能不能识别到 Trackpad
libinput list-devices
列出来一堆设备,从里面找到我的小板子,输出类似这样Terminal window Device: Apple Inc. Magic Trackpad 2Kernel: /dev/input/event11Group: 4Seat: seat0, defaultSize: 162x115mmCapabilities: pointer gestureTap-to-click: disabledTap-and-drag: enabledTap drag lock: disabledLeft-handed: disabledNat.scrolling: disabledMiddle emulation: disabledCalibration: n/aScroll methods: *two-finger edgeClick methods: button-areas *clickfingerDisable-w-typing: n/aDisable-w-trackpointing: n/aAccel profiles: flat *adaptive customRotation: n/aDevice: Apple Inc. Magic Trackpad 2Kernel: /dev/input/event13Group: 4Seat: seat0, defaultSize: 162x115mmCapabilities: pointer gestureTap-to-click: disabledTap-and-drag: enabledTap drag lock: disabledLeft-handed: disabledNat.scrolling: disabledMiddle emulation: disabledCalibration: n/aScroll methods: *two-finger edgeClick methods: button-areas *clickfingerDisable-w-typing: n/aDisable-w-trackpointing: n/aAccel profiles: flat *adaptive customRotation: n/a但是奇怪的是,他找到 2 个 Trackpad,明明我只连接了一个板子
回头看,这个就是罪魁祸首
-
接下来看
libinput能不能接收触控板的事件
libinput debug-events /dev/input/event11然后随便操作一下触控板Terminal window event11 DEVICE_ADDED Apple Inc. Magic Trackpad 2 seat0 default group1 cap:pg size 162x115mm tap(dl off)event11 POINTER_MOTION +0.012s 0.00/ 1.04 ( +0.00/ +5.34)event11 POINTER_MOTION +0.022s 0.00/ 0.72 ( +0.00/ +3.20)event11 POINTER_MOTION +0.034s 0.00/ 1.20 ( +0.00/ +5.34)event11 GESTURE_HOLD_BEGIN +0.040s 1event11 POINTER_MOTION +0.045s -0.22/ 1.44 ( -1.00/ +6.41)event11 POINTER_MOTION +0.056s -0.90/ 1.67 ( -4.00/ +7.48)event11 GESTURE_HOLD_END +0.056s 1 cancelledevent11 POINTER_MOTION +0.067s -1.79/ 2.39 ( -8.00/+10.68)event11 POINTER_MOTION +0.079s -2.69/ 2.39 (-12.00/+10.68)event11 POINTER_MOTION +0.090s -3.36/ 2.87 (-15.00/+12.82)event11 POINTER_MOTION +0.101s -6.49/ 5.74 (-29.00/+25.64)event11 POINTER_MOTION +0.112s -7.16/ 4.78 (-32.00/+21.36)event11 POINTER_MOTION +0.124s -8.51/ 5.26 (-38.00/+23.50)event11 POINTER_MOTION +0.135s -9.18/ 5.02 (-41.00/+22.43)event11 POINTER_MOTION +0.146s -6.72/ 2.87 (-30.00/+12.82)event11 POINTER_MOTION +0.157s -9.63/ 4.31 (-43.00/+19.23)event11 POINTER_MOTION +0.169s -9.40/ 3.35 (-42.00/+14.95)event11 POINTER_MOTION +0.180s -8.51/ 2.87 (-38.00/+12.82)event11 POINTER_MOTION +0.191s -4.70/ 0.72 (-21.00/ +3.20)...很顺利,
libinput收到了一堆触控板事件 -
所以,现在的问题就是
libinput可以读到触控板事件,但libinput-gestures读不到
接着找原因,libinput-gestures --help看看libinput-gestures怎么用Terminal window $ libinput-gestures --helpusage: libinput-gestures [-h] [-c CONFFILE] [-v] [-d] [-r] [-l] [--device DEVICE]Read gestures from libinput touchpad and action shell commands.options:-h, --help show this help message and exit-c CONFFILE, --conffile CONFFILEalternative configuration file-v, --verbose output diagnostic messages-d, --debug output diagnostic messages only, do not action gestures-r, --raw output raw libinput debug-event messages only, do not action gestures-l, --list just list out environment and configuration--device DEVICE explicit device name to use (or path if starts with /)这些选项的意思大概是
-d可以看设置的手势有没有被触发
-r显示从libinput接收的事件
现在要看libinput-gestures有没有收到libinput传过来的事件,用-r比较好
(不过这些经验都是设置好之后,回过头来看才发现的,当时摸索的时候逮到哪个用哪个,都试了一下libinput-gestures-setup stop先停止现在的libinput-gestures服务
libinput-gestures -r --device /dev/input/event11看libinput-gestures能不能读到event11的事件Terminal window $ libinput-gestures -r --device /dev/input/event11libinput-gestures: session KDE+x11 on Linux-6.1.**Hash: **device /dev/input/event11libinput-gestures: device /dev/input/event11: Apple Inc. Magic Trackpad 2-event11 DEVICE_ADDED Apple Inc. Magic Trackpad 2 seat0 default group1 cap:pg size 162x115mm tap(dl off)event11 POINTER_MOTION +0.867s 2.59/ 0.00 (+13.00/ +0.00)event11 POINTER_MOTION +0.878s 3.58/ 0.24 (+16.00/ +1.07)event11 POINTER_MOTION +0.889s 3.81/ 0.24 (+17.00/ +1.07)event11 POINTER_MOTION +0.900s 3.81/ 0.72 (+17.00/ +3.20)event11 POINTER_MOTION +0.912s 3.58/ 0.48 (+16.00/ +2.14)event11 POINTER_MOTION +0.923s 3.13/ 0.48 (+14.00/ +2.14)event11 POINTER_MOTION +0.934s 2.69/ 0.00 (+12.00/ +0.00)event11 POINTER_MOTION +0.945s 2.24/ 0.24 (+10.00/ +1.07)event11 POINTER_MOTION +0.957s 1.79/ 0.00 ( +8.00/ +0.00)event11 POINTER_MOTION +0.968s 2.46/ 0.24 (+11.00/ +1.07)libinput-gestures跟libinput一样,收到了一堆触控板事件,看来是没问题的libinput-gestures -r --device /dev/input/event13看看另一个触控板设备是什么情况Terminal window $ libinput-gestures -r --device /dev/input/event13libinput-gestures: session KDE+x11 on Linux-6.1.**Hash: **device /dev/input/event13libinput-gestures: device /dev/input/event13: Apple Inc. Magic Trackpad 2-event13 DEVICE_ADDED Apple Inc. Magic Trackpad 2 seat0 default group1 cap:pg size 162x115mm tap(dl off)不管在触控板上怎么操作,这个设备都读不到任何事件
-
目前的情况是,电脑上不知道为什么出现了两个触控板设备,
/dev/input/event11和/dev/input/event13
前一个可以正常使用,后一个没反应libinput-gestures -d看了看Terminal window $ libinput-gestures -dlibinput-gestures: session KDE+x11 on Linux-6.1.**Hash: **Gestures configured in ~/.config/libinput-gestures.conf:swipe up 4 xdotool key ctrl+super+Downswipe down 4 xdotool key ctrl+super+Upswipe left 4 xdotool key ctrl+super+Rightswipe right 4 xdotool key ctrl+super+Leftlibinput-gestures: device /dev/input/event13: Apple Inc. Magic Trackpad 2
破案了,原因是 libinput-gestures 默认使用了 /dev/input/event13 这个读不到触控板事件的设备
至于为什么电脑识别到 2 个触控板,并且第二个触控板没有反应
猜测是我测试触控板的时候,先测试了有线连接,用 Lightning 线连接到电脑,感觉没问题,之后又测试了蓝牙连接
但不知道什么原因,之前有线连接的触控板设备并没有自动删除,还留在电脑里
之后libinput-gestures又恰好用了这台设备…悲
让 libinput-gestures 使用特定设备
这样的话,解决办法就是指定 libinput-gestures 用能读到触控板事件的 /dev/input/event11
至于怎么设置,记得似乎在 /etc/libinput-gestures.conf 系统级的配置文件里,瞄到过设置方法
找到这个文件,仔细看了一下
############################################################################### This application normally determines your touchpad device automatically. Some users may have multiple touchpads but by default we use only the first one found. However, you can choose to specify the explicit device name to use. Run "libinput list-devices" to work out the name of your device (from the "Device:" field). Then add a device line specifying that name, e.g:# device DLL0665:01 06CB:76AD Touchpad# If the device name starts with a '/' then it is instead considered as the explicit device path although since device paths can change through reboots this is best to be a symlink. E.g. instead of specifying /dev/input/event12, you should use the corresponding full path link under /dev/input/by-path/ or /dev/input/by-id/.# You can choose to use ALL touchpad devices by setting the device name to "all". E.g. Do this if you have multiple touchpads which you want to use in parallel. This reduces performance slightly so only set this if you have to.# device all原来开发者在配置文件里已经说的清清楚楚了,libinput-gestures 默认使用发现的第一个触控板设备
如果是多设备用户,可以指定使用某个设备
device /dev/input/event11device /dev/input/by-path/pci-0000:00:****-event-mousedevice /dev/input/by-id/usb-Apple_Inc._Magic_Trackpad_2_****-event-mouse试验了一下,上面 3 种方式都可以指定使用某一个设备
但是根据开发者的说明,event11 这个号码不是固定的,每次启动电脑都会改变
所以最好通过 by-path 或 by-id 指定完整的路径
当然,还有一个简单暴力的方案,就是
device all同时使用所有的触控板设备(代价是损失一些性能
考虑了一下,决定还是用 device all
因为有时候要把触控板连电脑上充电,或者蓝牙不稳定时换有线
电脑会把有线和蓝牙当作 2 个设备,如果通过 by-path 或 by-id 指定了蓝牙触控板,换到有线时 libinput-gestures 的配置都会失效
所以还是无脑 device all 吧
xinput 命令行更改触控板设置
libinput-gestures 的问题解决了,触控板现在可以识别多指手势
接下来就要设置一下触控板的设置,比如指针速度,滚动方向
其实这 2 个设置都可以在图形界面的设置中心里更改
但正如开头所说,电脑从休眠里唤醒之后,这些设置有时会失效,变回默认设置,并且无法再次更改
于是在网上找了找怎么用命令行设置触控板
看了一圈,似乎 xinput 是最简便的了
使用方法可以参考 ArchWiki - xinput,就是
xinput list找对应的设备xinput list-props device看设备有哪些设置可以更改xinput set-prop device property values想改哪个改哪个
在这里我遇到了同样的问题,xinput 也识别到了 2 个触控板
$ xinput list⎡ Virtual core pointer id=2 [master pointer (3)]⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer (2)]⎜ ↳ Logi K855 Keyboard id=11 [slave pointer (2)]⎜ ↳ Apple Inc. Magic Trackpad 2 id=9 [slave pointer (2)]⎜ ↳ Apple Inc. Magic Trackpad 2 id=13 [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)] ↳ CSCTEK USB Audio and HID id=10 [slave keyboard (3)] ↳ Logi K855 Keyboard id=12 [slave keyboard (3)]想了想,决定和 libinput-gestures 一样,既然我不知道哪个是正确的设备,那就把 2 个触控板都设置一下就行了
去网上找了找设置方法,参考了
Stack Overflow - ‘xinput list’ shows same device twice & device IDs change: how to use ‘set-prop’ in a script?
Stack Overflow - How to make a program that finds id’s of xinput devices and sets xinput some settings
最后缝合出来一段能用的脚本
# Setup Apple Magic Trackpad 2
get all trackpad devices idids=$(xinput --list | grep "Trackpad" | cut -d '=' -f 2 | cut -f 1)
for id in $idsdo xinput set-prop $id "libinput Accel Speed" 0.5 xinput set-prop $id "libinput Natural Scrolling Enabled" 1done
libinput-gestures-setup restartsystemctl --user restart libinput-gestures.service加到 .bashrc 里,只要发现触控板不对劲,Ctrl+Alt+T 开一个 Terminal 窗口就能再次设置,让触控板顺滑如初
脚本最后 2 行是因为
libinput-gestures服务有时会挂掉,需要偶尔重启一下
可以用libinput-gestures-setup restart重启
也可以把libinput-gestures注册到systemd里,用脚本里的命令重启
设置systemd可以参考官方文档 Github - bulletmark/libinput-gestures - SYSTEMD USER SERVICE
总结
所以这次的问题就是,libinput 识别到 2 个触控板,libinput-gestures 默认使用那个不存在的触控板,导致所有配置都不起作用
查了一圈,最后发现解决方法就写在文档里,清清楚楚,只是我刚开始没仔细看…悲,花了好多时间
以后还是应该仔细看文档,不能图省事
Trackpad 2 使用体验
总体还是不错的,蓝牙连接电脑,使用一周大概掉电 20% 的样子,或者更少(没有仔细计算,好像是这个水平),电池还是比较耐用的
蓝牙连接的延迟似乎会高一些,而且不如有线稳定,偶尔会没有反应,要等几秒重新连
除此之外,还是很满意的,钱没白花
托盘图标消失
使用触控板一段时间之后,发现有时托盘图标会莫名少几个,键盘 layout 变回默认的 US 样式,没办法更改,不知道和触控板掉线重连有没有关系
在网上找了找重启托盘图标的命令,在 .bashrc 里加一个别名
alias tray_restart='kded5 --replace > /dev/null 2>&1 &'托盘图标不见的时候 tray_restart 就好了