Gestures in Linux - 03/04/2024
A guide to using gestures on your trackpad.
A Story First
Like many others, I started with a Windows PC. I can’t complain, I was fortunate enough to have a computer at home from a young age, which allowed me to learn how to use it, not to be afraid of touching some things at the risk of breaking it. I would have loved to have internet access at that time, but nothing is perfect. That curiosity was enough for me to become an engineer in electronics and telecommunications and, therefore, a technology enthusiast.
The year 2020 was running, I was locked up at home due to the Covid pandemic, and I remembered some failed experiments I had done years ago trying to run macOS on a laptop. At that moment, I told myself: “Why not try again”. I had a lot of free time and a desktop PC that perfectly met the hardware requirements to run macOS. After a little research, within a few hours I already had macOS running on my PC.
It was an incredible experience, after years of using Windows and occasionally Linux, I had found a system that strikes a balance between a kernel with a powerful command console and a friendly and attractive graphical environment with support for certain proprietary software. Within days, macOS became my primary operating system, and at the risk of wasting time due to a possible failure or data loss, I used it for work.
I used it for more than a year, I even renewed the PC to have a better experience, I already mastered the dual-boot with Windows so I could play on it too, until suddenly a BIOS update broke everything. I used to do it since it was a Ryzen 5 3600 and it was customary for AMD to improve performance with BIOS updates. I wasn’t too worried, since I had a backup of everything, but I didn’t have time to fix it.
Against my instincts about money management, I went to a store and bought a MacBook Air M1. I had been conquered by Apple. For a while I understood the Apple fanboys, when you have the precise hardware macOS works well, except for the workspace transition, the animation is still a mess today. But otherwise, what a beautiful thing. Battery that lasted all day, incredible charging times, a keyboard I loved, an operating system that allowed me to do everything I needed, and a trackpad that made me fall in love.
And it’s on this last point that I want to get to, the motivation for telling this story and why I’ve invested time in making this tutorial. The gestures in macOS are incredible, and also mandatory. You can’t use macOS well without gestures, window management is horrible without them. As for Windows, Microsoft has been good at interpreting the market and has implemented gestures, laptop manufacturers are including better trackpads (well, where the budget allows it) and gestures are becoming increasingly common. But in Linux, it’s different.
Why Linux?
After several years working in web development, I began to feel burnout, if there’s something that knowledge in electronics contributes, it’s the desire to understand how things work physically. And that’s why I wanted to return a bit to embedded systems programming. But I couldn’t do it with my MacBook, those two USB-C ports didn’t allow me to connect practically anything without adding a cumbersome hub. So I decided to go back to Linux, and this time with a laptop that would be a worthy successor to my MacBook. Surprise, I was able to do it and with a much lower budget. I found a seller with a batch of 6th generation Lenovo Thinkpad X1 Carbon laptops; a thin laptop, with good battery life, an incredible keyboard and a trackpad that wasn’t bad.
With the right machine, it was time to choose the distro. This is one of the headaches for any Linux beginner: there are so many alternatives, so many opinions, so many variables to consider. I already had an idea, I was sure to use Arch Linux, but I didn’t want to waste time configuring everything. I had an old acquaintance in mind, Manjaro, a distro that allows you to choose your desktop environment and some pre-installed applications. In my case, I chose Sway, a window manager inspired by i3, but with support for Wayland.
Where are the gestures?
(Here the tutorial begins)
The position of the Windows key on the Thinkpad is not comfortable for me to switch between workspaces, and the trackpad does not have gesture support. So I set out to investigate and found that there is a tool called libinput-gestures
. It’s a tool that allows you to configure gestures in Linux, and it’s very easy to install and configure.
First, we need to install libinput-gestures
. In my case, I use Manjaro, so the command is:
sudo pacman -S libinput-gestures
Once installed, we need to enable the service and add it to the list of services that start on boot. To do this, we run:
libinput-gestures-setup autostart
And that’s it? Well no, now we have to configure the gestures. To do this, we edit the libinput-gestures
configuration file:
nano ~/.config/libinput-gestures.conf
And this is where it gets interesting. You can use gestures to execute any command you want. In my case, I wanted to execute the Sway shortcuts to switch between workspaces. But, surprise, there are no default shortcuts in Manjaro for that. Nor does xdotool work, a tool that allows sending keyboard and mouse events to X11. So I had to look for an alternative when using Wayland.
Among all the options I found, ‘ydotool’ convinced me, practically a clone of xdotool but for Wayland. Except for a couple of details that I’ll explain later. To install it, you can go to the GitHub repository and follow the instructions, or you can use the following command:
sudo pacman -S ydotool
But for some reason ydotool does not come ready to work, so I had to do some additional configurations. First, you need to define the service with systemd. To do this, create the file /etc/systemd/system/ydotool.service
with the following content:
[Unit]
Description=Starts ydotoold user service
[Service]
Type=simple
Restart=always
RestartSec=3
ExecStartPre=/bin/sleep 2
ExecStart=/usr/bin/ydotoold
ExecReload=/usr/bin/kill -HUP $MAINPID
KillMode=process
TimeoutSec=180
[Install]
WantedBy=basic.target
Now, you need to enable the service:
systemctl enable ydotool.service
And finally, you need to start the service:
systemctl start ydotool.service
At this point, a socket is created at /tmp/.ydotool_socket
, so for ydotool to work, you need to define the YDOTOOL_SOCKET
environment variable by modifying the /etc/environment
file:
YDOTOOL_SOCKET=/tmp/.ydotool_socket
And reboot the system. Now, you can use ydotool to send keyboard and mouse events to Wayland. To test that everything is working, you can run:
ydotool type "Hola, mundo!"
If you see Hello, world! written in the active terminal, then everything is working correctly.
Now I needed to add the Sway shortcuts to switch between workspaces. To do this, I added the following lines to the .config/sway/config
configuration file:
bindsym $mod+Ctrl+Left workspace prev
bindsym $mod+Ctrl+Right workspace next
And now, I needed to configure the gestures. Let’s go back to libinput-gestures.conf
. In my case, I wanted to execute commands with three and four fingers. So my configuration file looked like this:
# Full screen gesture
gesture swipe up 3 ydotool key 126:1 33:1 33:00 126:0
# Stack to vertical
gesture swipe up 4 ydotool key 126:1 42:1 35:1 35:0 42:0 126:0
# Stack to horizontal
gesture swipe down 4 ydotool key 126:1 42:1 36:1 36:0 42:0 126:0
# New workspace
gesture swipe down 3 ydotool key 126:1 49:1 49:0 126:0
# Workspace Switch next
gesture swipe right ydotool key 126:1 97:1 106:1 106:0 97:0 126:0
# Workspace Switch prev
gesture swipe left ydotool key 126:1 97:1 105:1 105:0 97:0 126:0
But what do those numbers mean? They are the key codes that ydotool sends. The ones and zeros indicate whether the key is pressed or not. It is important that you respect the order of events, first you press the key, then you release it. In my case, code 126 is the Super key and 33 is the F key, so the first line executes the command Super+F
when you swipe up with three fingers. Be careful with the codes, if you forget to release a key, it will be pressed and you will have to restart the system to release it.
This is the list of codes:
Code | Key |
---|---|
1 | Esc |
59 | F1 |
60 | F2 |
61 | F3 |
62 | F4 |
63 | F5 |
64 | F6 |
65 | F7 |
66 | F8 |
67 | F9 |
68 | F10 |
87 | F11 |
88 | F12 |
99 | PrintScrn |
70 | Scroll Lock |
119 | Pause |
41 | ` |
2 | 1 |
3 | 2 |
4 | 3 |
5 | 4 |
6 | 5 |
7 | 6 |
8 | 7 |
9 | 8 |
10 | 9 |
11 | 0 |
12 | - |
13 | = |
14 | Backspace |
110 | Insert |
102 | Home |
104 | Page Up |
69 | Num Lock |
98 | KP / |
55 | KP * |
74 | KP - |
15 | Tab |
16 | Q |
17 | W |
18 | E |
19 | R |
20 | T |
21 | Y |
22 | U |
23 | I |
24 | O |
25 | P |
26 | [ |
27 | ] |
28 | Return |
111 | Delete |
107 | End |
109 | Page Down |
71 | KP 7 |
72 | KP 8 |
73 | KP 9 |
78 | KP + |
58 | Caps Lock |
30 | A |
31 | S |
32 | D |
33 | F |
34 | G |
35 | H |
36 | J |
37 | K |
38 | L |
39 | ; |
40 | ’ |
75 | KP 4 |
76 | KP 5 |
77 | KP 6 |
42 | Shift Left |
86 | International |
44 | Z |
45 | X |
46 | C |
47 | V |
48 | B |
49 | N |
50 | M |
51 | , |
52 | . |
53 | / |
54 | Shift Right |
43 | \ |
103 | Cursor Up |
79 | KP 1 |
80 | KP 2 |
81 | KP 3 |
96 | KP Enter |
29 | Ctrl Left |
125 | Logo Left (-> Option) |
56 | Alt Left (-> Command) |
57 | Space |
100 | Alt Right (-> Command) |
126 | Logo Right (-> Option) |
97 | Ctrl Right |
105 | Cursor Left |
108 | Cursor Down |
106 | Cursor Right |
82 | KP 0 |
83 | KP . |
And now, apply the changes by restarting the service:
libinput-gestures-setup restart
Remember that if you just configured Sway shortcuts, you need to restart the Sway service for the changes to take effect.
And that’s it, now you can enjoy gestures in Linux. I hope this tutorial has been useful to you, and if you have any questions, don’t hesitate to ask.