[email protected]

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:

CodeKey
1Esc
59F1
60F2
61F3
62F4
63F5
64F6
65F7
66F8
67F9
68F10
87F11
88F12
99PrintScrn
70Scroll Lock
119Pause
41`
21
32
43
54
65
76
87
98
109
110
12-
13=
14Backspace
110Insert
102Home
104Page Up
69Num Lock
98KP /
55KP *
74KP -
15Tab
16Q
17W
18E
19R
20T
21Y
22U
23I
24O
25P
26[
27]
28Return
111Delete
107End
109Page Down
71KP 7
72KP 8
73KP 9
78KP +
58Caps Lock
30A
31S
32D
33F
34G
35H
36J
37K
38L
39;
40
75KP 4
76KP 5
77KP 6
42Shift Left
86International
44Z
45X
46C
47V
48B
49N
50M
51,
52.
53/
54Shift Right
43\
103Cursor Up
79KP 1
80KP 2
81KP 3
96KP Enter
29Ctrl Left
125Logo Left (-> Option)
56Alt Left (-> Command)
57Space
100Alt Right (-> Command)
126Logo Right (-> Option)
97Ctrl Right
105Cursor Left
108Cursor Down
106Cursor Right
82KP 0
83KP .

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.