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 Location #350

Open
matt24smith opened this issue Oct 29, 2024 · 1 comment
Open

Wayland Mouse Location #350

matt24smith opened this issue Oct 29, 2024 · 1 comment
Labels

Comments

@matt24smith
Copy link

Hi, thanks for creating enigo! It's been a great help to me.

I'm a wayland user and noticed in the wayland.rs source code:

fn location(&self) -> InputResult<(i32, i32)> {
    // TODO Implement this
    error!("You tried to get the mouse location. I don't know how this is possible under Wayland. Let me know if there is a new protocol");
    Err(InputError::Simulate("Not implemented yet"))
}

So I wanted to add some discussion on this. It appears that Slurp is able to get the cursor position from the wayland compositor, so looking at the Slurp source code may yield some clues on how it could be implemented.

In the meantime, for anybody that needs this functionality, this hacky workaround might work for you, using a subprocess to get the location from slurp.

/// terrible hack to get current mouse position
fn slurp_mouse_position(enigo: &mut Enigo) -> Result<(i32, i32), Box<dyn std::error::Error>> {
    let cmd = std::process::Command::new("slurp")
        .arg("-p")
        .arg("-f")
        .arg("%x %y")
        .arg("-b")
        .arg("#00000000")
        .stdout(std::process::Stdio::piped())
        .spawn()?;

    std::thread::sleep(std::time::Duration::from_millis(100));

    enigo
        .button(enigo::Button::Left, enigo::Direction::Click)
        .expect("mouse click");

    let cmd_output = cmd.wait_with_output()?.stdout;
    let output_string = String::from_utf8(cmd_output)?;
    let coords: Vec<&str> = output_string.split(' ').collect();
    if coords.len() != 2 {
        return Err("can't parse slurp output".into());
    }
    let x: i32 = coords[0].parse()?;
    let y: i32 = coords[1].parse()?;
    Ok((x, y))
}
@pentamassiv
Copy link
Collaborator

Thank you for your research. I won't have time to look at the source code for a while but once I do, it will be helpful to have some code as a reference :)

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

No branches or pull requests

2 participants