Skip to content

Linux: Work around Wayland mouse warp problem#3591

Open
ObserverHerb wants to merge 1 commit intoSFML:masterfrom
ObserverHerb:xwayland_set_mouse_position
Open

Linux: Work around Wayland mouse warp problem#3591
ObserverHerb wants to merge 1 commit intoSFML:masterfrom
ObserverHerb:xwayland_set_mouse_position

Conversation

@ObserverHerb
Copy link

Description

The Blender and SDL teams found a way to get warping the mouse cursor to work under XWayland. This is my attempt at implementing their technique in SFML.

The cursor can be warped under XWayland if it is hidden first. This change detects if the cursor is visible, and if it is, hides it, restoring it after the warp.

The relativeTo reference to the WindowBase is const when it's passed in to sf::Mouse::setPosition(), so I'm using const_cast here to get it working. I know this isn't ideal. I'm sure there's a better way, but I wanted to get the ball rolling on this.

This PR is related to the issue #2750

Tasks

  • Tested on Linux
  • Tested on Windows
  • Tested on macOS
  • Tested on iOS
  • Tested on Android

How to test this PR?

#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
	sf::RenderWindow window(sf::VideoMode({640,480}),"Warp");

	while (window.isOpen())
	{
		while (const std::optional event = window.pollEvent())
		{
			if (event->is<sf::Event::Closed>())
				window.close();
		}

		window.clear();
		window.display();
		auto position=sf::Mouse::getPosition(window);
		std::cout << "X: " << position.x << ", Y: " << position.y << "\n";
		sf::Mouse::setPosition({320,240},window);
	}

	return 0;
}

@ObserverHerb ObserverHerb force-pushed the xwayland_set_mouse_position branch from 5f05e1c to 177c3d9 Compare October 21, 2025 01:29
XWayland does allow warping the mouse if it's hidden, so hide the
cursor before calling setMousePosition then unhide it.
@ChrisThrasher ChrisThrasher force-pushed the xwayland_set_mouse_position branch from 177c3d9 to c2d1854 Compare October 21, 2025 03:04
@ChrisThrasher
Copy link
Member

@ObserverHerb would you please test this out for me one more time after the latest changes I pushed? I expect the behavior will be unchanged but I want to be sure.

@ObserverHerb
Copy link
Author

Tested on Plasma 6, Gnome, Hyprland, and Budgie. When starting with the cursor hidden, it will stay hidden on all four. When starting with the cursor visible, it will flicker while moving the mouse and become hidden when the mouse stops on Plasma, Gnome, and Hyprland. Budgie is the only one that leaves the cursor visible. I'm not sure why it's wrong on the other three.

I was able to confirm this is the case with my code before your change, so I don't think you changed the behavior. I'll look into why the cursor is not being restored properly.

@eXpl0it3r eXpl0it3r removed this from SFML 3.1.0 Jan 20, 2026
@eXpl0it3r eXpl0it3r modified the milestones: 3.1, 3.2 Jan 20, 2026
@github-project-automation github-project-automation bot moved this to Planned in SFML 3.2.0 Jan 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Planned

Development

Successfully merging this pull request may close these issues.

3 participants