Write Standard User Scenerio Scripts to measure energy usage of software with KEcoLab
A guide to use ydotool and kdotool
KDE Eco is an ongoing initiative taken by the KDE community to support the development and adoption of sustainable Free & Open Source Software worldwide and I am happy to be able to contribute to this mission as part of Season of KDE 2026.
As part of this initiative, KDE aims to measure the energy consumption of its software and eco-certify applications with the Blue Angel ecolabel. To make this possible, KEcoLab a dedicated lab based in Berlin provides remote access to energy-measurement hardware via a GitLab CI/CD pipeline to measure the energy usage of software following the guide documented in KDE Eco Handbook
To measure energy usage of a software we need to prepare three scripts baseline.sh , idle.sh and sus.sh and push them via MR to this repository, SUS or Standard User Scenario script emulates a standard user in a automated manner without human intervention, more on how to prepare SUS can be found here.
Currently, these scripts rely on xdotool to simulate user interactions. However, xdotool does not work on Wayland. Since the KEcoLab computer have recently migrated to Fedora 43, which uses Wayland by default, the existing scripts no longer work.
To solve this problem,
I am working with my mentors Joseph, Aakarsh, and Karanjot to port the existing test scripts from xdotool to ydotool and kdotool, which are compatible with Wayland. As part of this effort, I’ve written a guide explaining how to use ydotool and kdotool more easily.
Setup ydotool and kdotool
#
To test the scripts you have written it’s recommended to setup locally.
Note: Some features changes and some are removed with updates so it’s recommended to build them from source mentioned here
ydotool #
- Clone the repository and navigate into it
git clone -b feat/setup-script https://invent.kde.org/neogg/ydotool.git
cd ydotool - Run the setup script
chmod +x setup.sh
./setup.sh install- Give
ydotooldpermission to uinput so it can run without root
sudo touch /etc/udev/rules.d/99-uinput.rules
echo 'KERNEL=="uinput", MODE="0660", GROUP="input"' | sudo tee /etc/udev/rules.d/99-uinput.rules > /dev/null
sudo usermod -aG input "$USER"
sudo udevadm control --reload-rules
sudo udevadm triggerNote: A reboot (or full logout/login) is required for the
inputgroup change to take effect.
- Reboot your computer and run
systemctl --user start ydotooldVerify that you can use ydotool by ydotool type "Hello" you will see Hello typed if installation was successful.
kdotool #
- Clone the repository and navigate into it
git clone -b feat/setup-script https://invent.kde.org/neogg/kdotool.git
cd kdotool - Run the setup script
chmod +x setup.sh
./setup.sh installVerify that you can use kdotool by kdotool -h
How to use ydotool and kdotool to perform various actions #
Press keys #
For pressing keys we use ydotool , the syntax for pressing keys is a little confusing when you look at it :
ydotool key KEY_CODE:ACTIONKEY_CODE
Linux uses a file named input-event-codes.h that defines numeric codes for everything an input device can generate in Linux. Simply speaking for every type of keypress there is a numerical representation. For example, the LEFTCTRL key is represented by 29, and the letter T is represented by 20.
How to find the code for the key you want to press , You can read the original .h file by nano /usr/include/linux/input-event-codes.h if you are in a linux system ( i hope you are using one). or inspect the file here
I use grep to find the code for the key i want since the header file is too long grep KEY_LEFTCTRL /usr/include/linux/input-event-codes.h and so on.
ACTION
- 1 : key is pressed
- 0 : key is released
So to press and release Ctrl you can use the following command
ydotool key 29:1 29:0Press key combinations #
Key combinations is the same as above , you just need to keep all the keys pressed.
So to press Ctrl+Alt+T which opens up a terminal you can use the following command
ydotool key 29:1 56:1 20:1 29:0 56:0 20:0This translates to Ctrl(pressed) Alt(pressed) T(pressed) Ctrl(released) Alt(released) T(released)
This will hopefully open up a terminal.
Click ( left, right, double) #
For performing mouse clicks we can use the click command in ydotool , If you are do not like codes unlike me,I am sorry but again we have codes…
ydotool click OPTIONS BUTTON_CODEOPTIONS
--repeat=N : can be used to press a button N times
--P : can be used to press and release a button as that's what we mostly doBUTTON_CODE
This is a hexadecimal value that represents different mouse buttons (left, right, middle, etc.).
Yes, it looks ugly at first, but you can always open up my blog.
ydotool click 0xC0 # left click
ydotool click 0xC1 # right click
ydotool click 0xC2 # middle button click (scroll button)
ydotool click 0x40 # left button down (press)
ydotool click 0x80 # left button up (release)
ydotool click 0x41 # right button down (press)
ydotool click 0x81 # right button up (release)And some combinations This can be useful to select text, drag and drop etc..
ydotool click 0x40 # left down
ydotool mousemove 300 0 # drag right
ydotool mousemove 0 200 # drag down
ydotool click 0x80 # left upsame as above but more reliable with delays to simulate a real user
ydotool click 0x40
ydotool mousemove -D 20 300 0
ydotool mousemove -D 20 0 200
ydotool click 0x80Type text #
Typing text is pretty simple using ydotool
ydotool type "string-you-want-to-type"Focus a specific app #
kdotool search --name "Window Title" windowactivate
kdotool search --class appname windowactivate
kdotool search --classname org.kde.app windowactivate ( for kde apps )Send mouse and keyboard commands to a specific window #
Focus the window using kdotool than execute commands using ydotool since commands are always executed in the active window.
Get current mouse location #
We can use kdotool for getting the current mouse location which also returns the windowID along with x and y coordinate of current mouse location
kdotool getmouselocationThe output :
x:42 y:96 screen:0 window:{e8aeec46-5c45-42cc-9839-8ad2edcb7f4f}If you only want to extract the x and y value since thats what’s more important you can do that using regex
# Read mouse location
loc="$(kdotool getmouselocation)"
# Extract x and y
x=$(echo "$loc" | sed -n 's/.*x:\([0-9-]*\).*/\1/p')
y=$(echo "$loc" | sed -n 's/.*y:\([0-9-]*\).*/\1/p')Move mouse to a specific coordinate #
Note : We can use ydotool to move the mouse but that may not give us any visual feedback if you are using it in a VM, that is you cannot see the actual mouse cursor move when the commands are executed but you can use the
kdotool getmouselocationto see how the mouse location gets updated.
There is a command in ydotool
ydotool mousemove --absolute -x x-value -y y-valuebut that does not work in Wayland properly.
We have an another command that moves the mouse relative to current mouse location so we can use that.
ydotool mousemove -x x-value -y y-value- mousemove -9999 -9999 ( moves the mouse to top left 0,0)
- mousemove x y ( now we move relative to 0,0 so that’s like absolute mousemove)
Move mouse inside a specific window #
Since we cannot directly send mouse events to a window on Wayland, we rely on window geometry that we can get using kdotool and do relative mouse movement using ydotool.
The steps are:
- Focus the window
- Get the window geometry
- Reset the mouse to
(0,0) - Move the mouse relatively using the window coordinates
# First, focus the window:
kdotool search --class firefox windowactivate
# Get the window geometry:
kdotool search --class firefox getwindowgeometry
# Example output:
# Window {a24d3d34-85f6-4915-821b-54a71a959f6a}
# Position: 340.23748404968967,68.49159882293117
# Geometry: 768x864
#Reset the mouse position to the top-left corner of the screen:
ydotool mousemove -x -9999 -y -9999
# Move the mouse to the Position you got from above command (top left corner of the window )
# Now move the mouse inside the window (top-left corner of the window):
ydotool mousemove -x 120 -y 80
#At this point, the mouse cursor is inside the target window. so you can now move inside the window , but only once than again repeat the above steps once again ( i will think of a script to automate this)Move mouse to the center of a window #
Moving to the center of a window uses the same idea, but instead of moving to x and y, we add half the width and height.
From the geometry we extract : x=120 y=80 width=1280 height=800
Calculate the center:
center_x = x + width / 2
center_y = y + height / 2Reset the mouse again:
ydotool mousemove -x -9999 -y -9999Move the mouse to the center of the window:
ydotool mousemove -x center_x -y center_yThese completes all the basic actions that are required to automate user actions , We may need to combine many of above to do tasks that simulate real user.