30

I'm looking for a solution working in Wayland to get mouse position, move it and click inside a bash script, all things xdotool can do for X server.

xdotool getmouselocation

is still working, but

xdotool mousemove 
xdotool click 

have no impact on the mouse cursor in GNOME Wayland session.

dessert
  • 39,392
  • 12
  • 115
  • 163
mxdsp
  • 3,798
  • 2
  • 29
  • 53
  • 2
    See also https://superuser.com/questions/1032270/wayland-alternative-for-xorgs-xdotool and https://unix.stackexchange.com/questions/381831/keyboard-emulation-in-wayland – Ben Creasy Feb 18 '18 at 09:32
  • If anyone has functional Python tools to do this stuff too, that would be useful and those should be mentioned here too. So far I'm really struggling though: [Stack Overflow: `pynput` library not working as expected in Python to press Windows + D key](https://stackoverflow.com/q/76399361/4561887) – Gabriel Staples Jun 04 '23 at 08:19

4 Answers4

9

There is the ydotool package for Wayland:

Package ydotool

Generic Linux command-line automation tool (no X!)

https://github.com/ReimuNotMoe/ydotool

Performs some of the functions of xdotool(1) without requiring X11 - however, it generally requires root permission (to open /dev/uinput)

Currently implemented command(s):

  • type - Type a string
  • key - Press keys
  • mousemove - Move mouse pointer to absolute position
  • click - Click on mouse buttons

N.B. optionally, you can enable and start the ydotoold daemon with:

  1. systemctl enable ydotool
  2. systemctl start ydotool
Pablo Bianchi
  • 14,308
  • 4
  • 74
  • 117
WinEunuuchs2Unix
  • 99,709
  • 34
  • 237
  • 401
  • Great answer. Important to note is that `ydotool` is not just for Wayland. From [the official github repo](https://github.com/ReimuNotMoe/ydotool): "**`ydotool` is not limited to Wayland.** You can use it on anything as long as it accepts keyboard/mouse/whatever input. For example, X11, text console, "RetroArch OS", fbdev apps (fbterm/mplayer/SDL1/LittleVGL/Qt Embedded), etc." I've tested it on Ubuntu 22.04 with the X11 window manager, for instance, to press the `Super` + `D` keys to show the desktop, and it works great. – Gabriel Staples Jun 05 '23 at 01:57
  • I wrote a tutorial here ([How can I write a program to press keys, such as Windows + D, in Wayland? (replace `xdotool` in Wayland)](https://askubuntu.com/a/1473061/327339)) and on my website here ([Tutorial: Getting started with ydotool to automate key presses (or mouse movements) in Linux](https://gabrielstaples.com/ydotool-tutorial/#gsc.tab=0)) for anyone wanting to get started with `ydotool`. It was tricky and took me a long time to write this tutorial, so hopefully this helps a lot of people. – Gabriel Staples Jun 24 '23 at 01:31
9

This is because such features have been explicitly removed from Wayland for security reasons. The major concerns were reading other programs input and allowing fake input to be sent to other programs which would allow different attack vectors.

Some window-managers might implement some sort of macro feature in the future but as of now there is no such feature that I know of.

Implementing this would mean to implement it for each of the different window managers which surely will take still a while.

I suggest using Xorg instead of Wayland for now if you need to use such features.

dessert
  • 39,392
  • 12
  • 115
  • 163
Videonauth
  • 33,045
  • 16
  • 104
  • 120
  • 3
    I don't know if you are aware of any of the discussions, but there was a RFC on a mailing list: [RFC: Interface for injection of input events](https://lists.freedesktop.org/archives/wayland-devel/2017-March/033676.html) and [Add an API for taking screenshots and recording screencasts](https://bugs.freedesktop.org/show_bug.cgi?id=98894). It sounds like the challenges could be addressed with some additional hardening work. – Ben Creasy Feb 18 '18 at 09:36
  • 1
    Is this still up to date? If no click/type automation is possible, it would be an absolute blocker against Wayland for me. (Also, it's never great when someone tells the user what they're allowed to do on their own computer. This is not Windows.) – Fabian Röling Jul 27 '21 at 09:43
  • 1
    So, re `ydotool`, for security reasons, automation on wayland now requires a daemon with root privileges that affects all users, instead of the fake input tool being contained to a specific x11-session of a specific user. The issue for taking screenshots is still open 5 years later: https://gitlab.freedesktop.org/wayland/wayland/-/issues/32 – Arne Babenhauserheide Apr 19 '22 at 11:12
2

evemu from the evemu-tools package can emulate devices like mouse, touchpads and keyboards in wayland. It cannot "read" what is happening on the screen but can easily move & click and "blindly" interact with the session.

sudo evemu-describe # list devices

if your mouse is /dev/input/event5 from the output of above command the following will move mouse 50 pixels xy then right-press & let go of button:

sudo evemu-event /dev/input/event5 --type EV_REL --code REL_X --value 50
sudo evemu-event /dev/input/event5 --type EV_REL --code REL_Y --value 50 
sudo evemu-event /dev/input/event5 --type EV_KEY --code BTN_RIGHT --value 1 
sudo evemu-event /dev/input/event5 --type EV_KEY --code BTN_RIGHT --value 0

You can also record a particular sequence and re-play it:

# press ctrl-c to stop recording
sudo evemu-record /dev/input/event5 /tmp/mouse-sequence

# to replay session
sudo evemu-play /tmp/mouse-sequence
Pablo Bianchi
  • 14,308
  • 4
  • 74
  • 117
tomodachi
  • 14,542
  • 4
  • 31
  • 50
2

There is the wtype package for Wayland. It's available on apt repos on Ubuntu 22.04.

Seems a simpler approach (no need for ydotoold/systemd service), but for the time being, mutter (GNOME compositor) doesn't work with it.

Check also Evemu, a freedesktop project that records and replays device descriptions and events.

Pablo Bianchi
  • 14,308
  • 4
  • 74
  • 117