summaryrefslogtreecommitdiffstats
path: root/input-emulation/src/lib.rs
blob: f9adf3e1604c26519723dc3ca418724be1426885 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use async_trait::async_trait;
use std::fmt::Display;

use input_event::Event;

pub use self::error::{EmulationCreationError, EmulationError, InputEmulationError};

#[cfg(windows)]
pub mod windows;

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

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

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

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

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

/// fallback input emulation (logs events)
pub mod dummy;
pub mod error;

pub type EmulationHandle = u64;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Backend {
    #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
    Wlroots,
    #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
    Libei,
    #[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
    Xdp,
    #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
    X11,
    #[cfg(windows)]
    Windows,
    #[cfg(target_os = "macos")]
    MacOs,
    Dummy,
}

impl Display for Backend {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
            Backend::Wlroots => write!(f, "wlroots"),
            #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
            Backend::Libei => write!(f, "libei"),
            #[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
            Backend::Xdp => write!(f, "xdg-desktop-portal"),
            #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
            Backend::X11 => write!(f, "X11"),
            #[cfg(windows)]
            Backend::Windows => write!(f, "windows"),
            #[cfg(target_os = "macos")]
            Backend::MacOs => write!(f, "macos"),
            Backend::Dummy => write!(f, "dummy"),
        }
    }
}

#[async_trait]
pub trait InputEmulation: Send {
    async fn consume(
        &mut self,
        event: Event,
        handle: EmulationHandle,
    ) -> Result<(), EmulationError>;
    async fn create(&mut self, handle: EmulationHandle);
    async fn destroy(&mut self, handle: EmulationHandle);
    async fn terminate(&mut self);
}

pub async fn create_backend(
    backend: Backend,
) -> Result<Box<dyn InputEmulation>, EmulationCreationError> {
    match backend {
        #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
        Backend::Wlroots => Ok(Box::new(wlroots::WlrootsEmulation::new()?)),
        #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
        Backend::Libei => Ok(Box::new(libei::LibeiEmulation::new().await?)),
        #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
        Backend::X11 => Ok(Box::new(x11::X11Emulation::new()?)),
        #[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
        Backend::Xdp => Ok(Box::new(
            xdg_desktop_portal::DesktopPortalEmulation::new().await?,
        )),
        #[cfg(windows)]
        Backend::Windows => Ok(Box::new(windows::WindowsEmulation::new()?)),
        #[cfg(target_os = "macos")]
        Backend::MacOs => Ok(Box::new(macos::MacOSEmulation::new()?)),
        Backend::Dummy => Ok(Box::new(dummy::DummyEmulation::new())),
    }
}

pub async fn create(
    backend: Option<Backend>,
) -> Result<Box<dyn InputEmulation>, EmulationCreationError> {
    if let Some(backend) = backend {
        let b = create_backend(backend).await;
        if b.is_ok() {
            log::info!("using emulation backend: {backend}");
        }
        return b;
    }

    for backend in [
        #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
        Backend::Wlroots,
        #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
        Backend::Libei,
        #[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
        Backend::Xdp,
        #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
        Backend::X11,
        #[cfg(windows)]
        Backend::Windows,
        #[cfg(target_os = "macos")]
        Backend::MacOs,
        Backend::Dummy,
    ] {
        match create_backend(backend).await {
            Ok(b) => {
                log::info!("using emulation backend: {backend}");
                return Ok(b);
            }
            Err(e) if e.cancelled_by_user() => return Err(e),
            Err(e) => log::warn!("{e}"),
        }
    }

    Err(EmulationCreationError::NoAvailableBackend)
}