Linux下配置触摸板手势操作

警告
本文最后更新于 2020-05-13,文中内容可能已过时。

MacOS的触摸板手势体验没得说,在Linux下也可以借助一些软件达到相近的体验。不过最想要的三指取词还不知到如何实现😠。

首先配置触摸板的驱动程序为libinput,具体操作这里不谈。借助软件libinput-gestures可以拓展触摸板三/四指手势以及缩放操作。

i3桌面下,窗口有两种:平铺以及浮动。工作区的概念类比MacOS中的虚拟桌面,便签区的效果类似窗口最小化

up down left right
三指 切换到上方窗口 切换到下方窗口 切换到左侧窗口 切换到右侧窗口
四指 将浮动窗口移到便签区 循环切换便签区内的窗口 切换到左侧工作区 切换到右侧工作区

如果当前获得焦点的窗口是浏览器,则三指的手势功能为

up down left right
三指 前进 后退 切换到左侧tab 切换到右侧tab

以及另外四种手势

leftup leftdown rightup rightdown
三指 打开最近关闭标签页 关闭当前标签页 左移当前便签页 右移当前标签页

libinput-gestures的配置文件位于~/.config/libinput-gestures.conf。其中涉及到上述功能的配置为

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
 三指手势
gestures swipe left  3 $HOME/Program/bin/move_window_or_tab
gestures swipe right 3 $HOME/Program/bin/move_window_or_tab right
gestures swipe up    3 $HOME/Program/bin/move_window_or_tab up
gestures swipe down  3 $HOME/Program/bin/move_window_or_tab down

 四指手势
gestures swipe left  4 i3-msg workspace prev
gestures swipe right 4 i3-msg workspace next
gestures swipe up    4 $HOME/Program/bin/show_or_hide_scratchpad up
gestures swipe down  4 xdotool key super+shift+minus

 浏览器窗口中三指手势
gestures swipe left_up    3 $HOME/Program/bin/move_window_or_tab left_up
gestures swipe left_down  3 $HOME/Program/bin/move_window_or_tab left_down
gestures swipe right_up   3 $HOME/Program/bin/move_window_or_tab right_up
gestures swipe right_down 3 $HOME/Program/bin/move_window_or_tab right_down

libinput-gestures能捕捉手势,但是将手势与操作绑定需要通过另一个能实现模拟按键操作的软件xdotool。 由于三指手势对应的功能较为复杂,需要区分窗口内是否为浏览器,且涉及的主要功能为切换窗口,因而统一用一个脚本move_window_or_tab实现。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env bash

 获取窗口名称
WM_NAME="$(xdotool getactivewindow getwindowname)"
 获取手势方向
DIRECTION=${1:-left}

if [[ $WM_NAME =~ 'Firefox' ]]; then
    if [[ $DIRECTION == 'left' ]]; then
        xdotool key control+Page_Up
    elif [[ $DIRECTION == 'right' ]]; then
        xdotool key control+Page_Down
     ...
    fi
else
     普通i3窗口
     Key number: H-43, J-44, K-45, L-46, ;-47
    if [[ $DIRECTION == 'left' ]]; then
        xdotool key super+44
    elif [[ $DIRECTION == 'right' ]]; then
        xdotool key super+47
     ...
    fi
fi

四指切换工作区的命令很简单,不需要借助脚本实现。但是涉及到便签区窗口的功能无法用一行命令搞定,因此写了一个脚本show_or_hide_scratchpad

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
 获取手势方向
DIRECTION=${1:-up}

 核心逻辑部分
if [[ $DIRECTION == 'up' ]]; then
    if i3-msg focus floating; then
        i3-msg move scratchpad
    fi
elif [[ $DIRECTION == 'down' ]]; then
    if i3-msg focus floating; then
        i3-msg move scratchpad
        i3-msg move scratchpad
    else
        i3-msg move scratchpad
    fi
    i3-msg [floating] resize set 1280 1200
    i3-msg move position center
fi