目录

利用libinput-gestures让触控板手势更丰富

关于libinput-gestures

Arch Wiki对于libinput-gesture的说明

libinput-gestures的Github主页

libinput-gestures是为linux下触控板提供自定义手势配置的程序。

libinput已经对触控板的基本使用手势有了支持,有部分DE和WM已经支持定义一些手势(比如GnomeKDE),但是并不一定能够覆盖所有,并且一般都是已经预设好的,不支持自定义。

所以可以通过使用libinput-gestures程序来扩展触控板的使用。

安装

Arch Linux可以通过AUR仓库安装:

1
$ yay -S libinput-gestures

libinput-gestures还可以搭配wmctrlxdotool两个工具来扩展手势定义的动作:

1
$ pacman -S wmctrl xdotool

启动

libinput-gestures的启动方式有两种,systemd service方式和desktop application方式,通过libinput-gestures-setup命令来指定启动方式或者是否自启动。

我们使用systemd方式作为user服务来启动:libinput-gestures-setup service

这里可能会出现“Systemd not available, can not run as service.”的错误提示,这是因为libinput-gestures.service中的[Install]graphical-session.target,而我们在user环境下使用的是default.targetlibinput-gestures-setup脚本无法检测到对应的target,所以设置失败。

我的解决方法是:

  1. libinput-gestures.service中的所有graphical-session.target修改为default.target

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    
    # /home/dylan/.config/systemd/user/libinput-gestures.service
    [Unit]
    Description=Actions gestures on your touchpad using libinput
    Documentation=https://github.com/bulletmark/libinput-gestures
    PartOf=default.target  # <-- 修改
    After=default.target   # <-- 修改
    
    [Service]
    Type=simple
    ExecStart=/usr/bin/libinput-gestures
    
    [Install]
    WantedBy=default.target  # <-- 修改
    
  2. libinput-gestures-setup脚本中关于HAS_SYSD的检测也改为default.target

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    # /usr/bin/libinput-gestures-setup
    ...
    	# Test if systemd is installed
        if type systemctl &>/dev/null; then
            #HAS_SYSD=$(sysd_prop graphical-session.target ActiveState active)  # <-- 注释,改为下面这行
            HAS_SYSD=$(sysd_prop default.target ActiveState active)
        else
            HAS_SYSD=0
        fi
    ...
    

    这不是最好的办法,可能在libinput-gestures更新后被覆盖

设置为service方式之后,设置为自动启动:libinput-gestures-setup autostart

立即开启libinput-gestures-setup start

配置

软件默认的配置文件为/etc/libinput-gestures.conf,可以通过~/.config/libinput-gestures.conf来创建用户配置。

因为我使用XFCE,在窗口管理器中配置好快捷键,之后可以通过xdotool映射到gestures。

下面以我的配置文件为例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 3 finger swipe
gesture swipe up			3 xdotool key control+1 # 切换到工作区1
gesture swipe down			3 xdotool key control+2 # 切换到工作区2
gesture swipe left_up		3 xdotool key alt+Left  # 后退操作
gesture swipe right_up		3 xdotool key alt+Right # 前进操作
gesture swipe right			3 xdotool key control+Tab        # 上一个Tab
gesture swipe left			3 xdotool key control+shift+Tab  # 下一个Tab

# 4 finger swipe
gesture swipe up			4 xdotool key super+Up       # 窗口最大化
gesture swipe down			4 xdotool key super+Down     # 窗口最小化
gesture swipe left			4 xdotool key super+Left     # 窗口置左
gesture swipe right			4 xdotool key super+Right    # 窗口置右
gesture swipe left_up		4 xdotool key super+Page_Up  # 窗口置左上
gesture swipe left_down		4 xdotool key super+Home     # 窗口置左下
gesture swipe right_up		4 xdotool key super+Page_Down# 窗口置右上
gesture swipe right_down	4 xdotool key super+End      # 窗口置右下

# 2 finger pinch
gesture pinch clockwise		2 xdotool key alt+Tab        # 顺时针向前切换窗口
gesture pinch anticlockwise	2 xdotool key alt+shift+Tab  # 逆时针向后切换窗口

# hold on gestures
gesture hold on				4 xdotool key control+F4  # 四指按住关闭当前页
gesture hold on				3 xdotool key alt+F8      # 三指按住调整窗口大小
#gesture hold on				2 xdotool key alt+F7

# pinch in/out
gesture pinch in			xdotool key ctrl+KP_Subtract # 两指捏缩小
gesture pinch out			xdotool key ctrl+KP_Add      # 两指扩放大

# device
device all   # 所有设备均应用

关于device选项这里做个记录:

先前我没有仔细查看libinput-gestures.conf对于设备选择的说明,只对手势定义做了配置;

实际使用时笔记本和外接触控板同时只能有一个对手势生效,如果我的外接触控板断开,内置的触控板不会接替生效,必须重启服务;

于是我准备在udev rule中对触控板的连接与断开事件指定一个重启服务的操作;

但无奈如果使用desktop方式启动libinput-gestures,udev rule在执行RUN脚本时会出现很多问题(比如X环境变量、执行用户等),并且也无法直接利用restart命令;

最后我换成service启动,并且在rule中配置了对服务的依赖,才勉强实现了切换;

实际上libinput-gestures可以指定某个device,甚至是监听所有的devices;

没有仔细查看文档导致绕了这么一个大弯路,谨此作为教训。

调试

使用libinput-gestures -d对已定义好的手势进行调试。

注意要将已经运行的libinput-gestures程序关闭。