summaryrefslogtreecommitdiffstats
path: root/src/capture.rs
blob: d3b99db2fcffd0e7aba43366062052cbd37c5d50 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use std::io;

use futures_core::Stream;

use crate::{
    client::{ClientEvent, ClientHandle},
    event::Event,
};

#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
pub mod libei;

#[cfg(target_os = "macos")]
pub mod macos;

#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
pub mod wayland;

#[cfg(windows)]
pub mod windows;

#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
pub mod x11;

/// fallback input capture (does not produce events)
pub mod dummy;

pub async fn create() -> Box<dyn InputCapture<Item = io::Result<(ClientHandle, Event)>>> {
    #[cfg(target_os = "macos")]
    match macos::MacOSInputCapture::new() {
        Ok(p) => return Box::new(p),
        Err(e) => log::info!("macos input capture not available: {e}"),
    }

    #[cfg(windows)]
    match windows::WindowsInputCapture::new() {
        Ok(p) => return Box::new(p),
        Err(e) => log::info!("windows input capture not available: {e}"),
    }

    #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
    match libei::LibeiInputCapture::new().await {
        Ok(p) => {
            log::info!("using libei input capture");
            return Box::new(p);
        }
        Err(e) => log::info!("libei input capture not available: {e}"),
    }

    #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
    match wayland::WaylandInputCapture::new() {
        Ok(p) => {
            log::info!("using layer-shell input capture");
            return Box::new(p);
        }
        Err(e) => log::info!("layer_shell input capture not available: {e}"),
    }

    #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
    match x11::X11InputCapture::new() {
        Ok(p) => {
            log::info!("using x11 input capture");
            return Box::new(p);
        }
        Err(e) => log::info!("x11 input capture not available: {e}"),
    }

    log::error!("falling back to dummy input capture");
    Box::new(dummy::DummyInputCapture::new())
}

pub trait InputCapture: Stream<Item = io::Result<(ClientHandle, Event)>> + Unpin {
    /// notify input capture of configuration changes
    fn notify(&mut self, event: ClientEvent) -> io::Result<()>;

    /// release mouse
    fn release(&mut self) -> io::Result<()>;
}