x11_input_mirror/
mouse.rs

1use std::process::Command;
2use std::str;
3use std::sync::mpsc::{channel, Receiver};
4use std::thread::{sleep, spawn};
5use std::time::Duration;
6
7#[derive(Debug)]
8pub struct Event {
9    pub x: u16,
10    pub y: u16,
11}
12
13pub fn spawn_thread(interval_ms: u64) -> Receiver<Event> {
14    let interval = Duration::from_millis(interval_ms);
15    let (tx, rx) = channel();
16    spawn(move || loop {
17        tx.send(get_current_mouse_location()).unwrap();
18        sleep(interval);
19    });
20    rx
21}
22
23pub fn get_current_mouse_location() -> Event {
24    let r = Command::new("xdotool")
25        .arg("getmouselocation")
26        .output()
27        .unwrap();
28    let stdout = r.stdout;
29    let stdout = unsafe { str::from_utf8_unchecked(&stdout) };
30    let coords = stdout.split(' ').take(2);
31    let coords = coords.map(|s| s[2..].parse::<u16>().unwrap());
32    let coords = coords.collect::<Vec<_>>();
33    Event {
34        x: coords[0],
35        y: coords[1],
36    }
37}