Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wayland: mouse moves to 0, 0 no matter where you move it to when using abs cords #396

Open
Zai-Kun opened this issue Feb 26, 2025 · 11 comments

Comments

@Zai-Kun
Copy link

Zai-Kun commented Feb 26, 2025

Describe the bug
Pretty much as the title says. No matter what x,y cords you set, it just moves to 0, 0. However, relative seems to work.

To Reproduce

use enigo::{
    Enigo, Mouse, Settings,
    Coordinate::Abs
};

fn main() {
    let mut enigo = Enigo::new(&Settings::default()).unwrap();
    enigo.move_mouse(100 200, Abs).unwrap();
}

Expected behavior
Mouse cursor should move to the defined x,y cords.

Environment (please complete the following information):

  • OS: linux, wayland, sway
  • Rust rustc 1.84.0
  • Library Version enigo 0.3.0

Additional context
None

recorded_video.mp4
@Zai-Kun Zai-Kun changed the title Wayland: moves to 0, 0 no matter where you move it to when using abs cords Wayland: mouse moves to 0, 0 no matter where you move it to when using abs cords Feb 26, 2025
@pentamassiv
Copy link
Collaborator

Hi,
thank you for the report.

Could you please try again with the current main branch? I fixed a few Wayland issues recently. Also, can you tell me more about your environment? You say that you are using sway with Wayland. Which feature are you using to do that? wayland, libei or just the default (then you are using XWayland)? Does everything else work (e.g left click with the mouse)?

@Zai-Kun
Copy link
Author

Zai-Kun commented Feb 26, 2025

I cloned the main repo using git clone https://github.com/enigo-rs/enigo, and specified the cloned dir in cargo.toml. Here is what it looks like:

[package]
name = "lol"
version = "0.1.0"
edition = "2024"

[dependencies]
enigo = { path = "/tmp/enigo", features = ["wayland"] }

That did not solve the issue but introduced another issue. A weird one at that. It doesn't take effect until i switch my workspace. Please see the attached video:

recorded_video.mp4

After tinkering around with the source code I bit, I discovered that changing the arguments in the

                vp.motion_absolute(
                    time,
                    x,
                    y,
                    u32::MAX, // TODO: Check what would be the correct value here
                    u32::MAX, // TODO: Check what would be the correct value here
                );

function from u32:MAX to my width and height of the screen somehow fished the first issue.

recorded_video.mp4

About the question earlier, mouse button clicks seem to work just fine.

@Zai-Kun
Copy link
Author

Zai-Kun commented Feb 26, 2025

Also, before, writing text using enigo.text wasn't working. But after switching to the latest version, from the main branch, it seems to work now but with the same new issue: doesn't do anything until I switch my workspace. It's so weird.

@pentamassiv
Copy link
Collaborator

Nice find, now we need to find out how to get the correct screen dimensions dynamically

Regarding your videos, I don't understand what I am seeing in them. Maybe it is because I never used sway, but I can't tell when you change workspaces and if it is intentional or not. I also don't get what enigo is supposed to do. Is it executing the same code as before (i.e it is just supposed to move the mouse)?

@Zai-Kun
Copy link
Author

Zai-Kun commented Feb 26, 2025

I'm sorry for not being clear. What I wanted to show was, that no matter how long I keep the program running nothing happens, not until I switch my workspace. The code used is the same as before:

use enigo::{
    Enigo, Mouse, Settings,
    Coordinate::Abs
};

fn main() {
    let mut enigo = Enigo::new(&Settings::default()).unwrap();
    enigo.move_mouse(500, 500, Abs).unwrap();
}

Focus on my mouse pointer; it suddenly moves when I switch a workspace, and then the program exits. Now, another weird part about it is, that it only happens when I move to a workspace with a window, and not with an empty one.

recorded_video.mp4

Those numbers at the top indicate my workspaces. 3rd workspace is where I was running the program 2nd workspace is where I had another terminal open. As you can see, when I switch to an empty workspace, workspace 4 for example, nothing happens. But when I do the same with a workspace with a window on it, my cursor moves and the program exists. I hope that explained it.

pentamassiv added a commit to pentamassiv/enigo that referenced this issue Feb 26, 2025
On sway, the roundtrip never finished until e.g the workspace was switched. The move_mouse and scroll functions would not finish otherwise. Changing this to flush fixes the issue (see enigo-rs#396 for more details)
@pentamassiv
Copy link
Collaborator

pentamassiv commented Feb 26, 2025

No problem, I understand it now.

I have a suspicion why the program does not terminate unless the workspace is switched. Could you please try out the fix from #398?

You should be able to just change your Cargo.toml to include

enigo = { git = "https://github.com/pentamassiv/enigo.git", branch = "wayland_sway_roundtrip", features = ["wayland"] }

I haven't tested it myself yet, but it should fix the issue of the program not terminating. It might still only successfully simulate the mouse move after you change workspaces. Please let me know what happens :-)

@Zai-Kun
Copy link
Author

Zai-Kun commented Feb 26, 2025

Unfortunately, that did not seem to fix the issue :(

@pentamassiv
Copy link
Collaborator

What happened? Is the program still not terminating before switching workspaces?

Then it must be waiting for a previous Wayland message. That makes sense, as the changes I recently merged were touching the initialization of the connection. Could you please change your Cargo.toml file to this:

[package]
name = "lol"
version = "0.1.0"
edition = "2024"

[dependencies]
enigo = { git = "https://github.com/pentamassiv/enigo.git", branch = "wayland_sway_roundtrip", features = ["wayland"] }
env_logger = "0.11"

and please change your code to this:

use enigo::{
    Enigo, Mouse, Settings,
    Coordinate::Abs
};

fn main() {
    env_logger::try_init().ok();
    let mut enigo = Enigo::new(&Settings::default()).unwrap();
    enigo.move_mouse(100 200, Abs).unwrap();
}

Please start the program by running WAYLAND_DEBUG=1 RUST_LOG=debug cargo run (or adapt your script to add the environment flags). This will print the sent and received Wayland messages (it's a long output, so you probably want to pipe it to a file). Please paste the output of a successful run (you switched workspaces for it to finish) and paste the output of the program running until it is supposed to simulate the mouse move (press CTRL+C before switching the workspace). This should help us determine why it hangs.

@Zai-Kun
Copy link
Author

Zai-Kun commented Feb 27, 2025

Yes, the problem persists.
Here is the successful run output:

successful_run.txt

Here is the interrupted one:

interrupted_run.txt

@pentamassiv
Copy link
Collaborator

Awesome, thank you. I now know what the issue is.

The code gets stuck here. We are waiting for the compositor to send an event, but it never comes until for example the workspace gets switched.

I know how to fix this, but I will only have the time to work on it starting mid march because I have exams coming up that I need to prepare for.

@Zai-Kun
Copy link
Author

Zai-Kun commented Feb 27, 2025

Perfect! Goodluck with your exams :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants