Skip to main content

input_emulation/
error.rs

1#[derive(Debug, Error)]
2pub enum InputEmulationError {
3    #[error("error creating input-emulation: `{0}`")]
4    Create(#[from] EmulationCreationError),
5    #[error("error emulating input: `{0}`")]
6    Emulate(#[from] EmulationError),
7}
8
9#[cfg(any(libei, rdp))]
10use ashpd::{Error::Response, desktop::ResponseError};
11use std::io;
12use thiserror::Error;
13
14#[cfg(wlroots)]
15use wayland_client::{
16    ConnectError, DispatchError,
17    backend::WaylandError,
18    globals::{BindError, GlobalError},
19};
20
21#[derive(Debug, Error)]
22pub enum EmulationError {
23    #[error("event stream closed")]
24    EndOfStream,
25    #[cfg(libei)]
26    #[error("libei error: `{0}`")]
27    Libei(#[from] reis::Error),
28    #[cfg(wlroots)]
29    #[error("wayland error: `{0}`")]
30    Wayland(#[from] wayland_client::backend::WaylandError),
31    #[cfg(any(rdp, libei))]
32    #[error("xdg-desktop-portal: `{0}`")]
33    Ashpd(#[from] ashpd::Error),
34    #[error("io error: `{0}`")]
35    Io(#[from] io::Error),
36}
37
38#[derive(Debug, Error)]
39pub enum EmulationCreationError {
40    #[cfg(wlroots)]
41    #[error("wlroots backend: `{0}`")]
42    Wlroots(#[from] WlrootsEmulationCreationError),
43    #[cfg(libei)]
44    #[error("libei backend: `{0}`")]
45    Libei(#[from] LibeiEmulationCreationError),
46    #[cfg(rdp)]
47    #[error("xdg-desktop-portal: `{0}`")]
48    Xdp(#[from] XdpEmulationCreationError),
49    #[cfg(x11)]
50    #[error("x11: `{0}`")]
51    X11(#[from] X11EmulationCreationError),
52    #[cfg(target_os = "macos")]
53    #[error("macos: `{0}`")]
54    MacOs(#[from] MacOSEmulationCreationError),
55    #[cfg(windows)]
56    #[error("windows: `{0}`")]
57    Windows(#[from] WindowsEmulationCreationError),
58    #[error("capture error")]
59    NoAvailableBackend,
60}
61
62impl EmulationCreationError {
63    /// request was intentionally denied by the user
64    pub(crate) fn cancelled_by_user(&self) -> bool {
65        #[cfg(libei)]
66        if matches!(
67            self,
68            EmulationCreationError::Libei(LibeiEmulationCreationError::Ashpd(Response(
69                ResponseError::Cancelled,
70            )))
71        ) {
72            return true;
73        }
74        #[cfg(rdp)]
75        if matches!(
76            self,
77            EmulationCreationError::Xdp(XdpEmulationCreationError::Ashpd(Response(
78                ResponseError::Cancelled,
79            )))
80        ) {
81            return true;
82        }
83        false
84    }
85}
86
87#[cfg(wlroots)]
88#[derive(Debug, Error)]
89pub enum WlrootsEmulationCreationError {
90    #[error(transparent)]
91    Connect(#[from] ConnectError),
92    #[error(transparent)]
93    Global(#[from] GlobalError),
94    #[error(transparent)]
95    Wayland(#[from] WaylandError),
96    #[error(transparent)]
97    Bind(#[from] WaylandBindError),
98    #[error(transparent)]
99    Dispatch(#[from] DispatchError),
100    #[error(transparent)]
101    Io(#[from] std::io::Error),
102}
103
104#[cfg(wlroots)]
105#[derive(Debug, Error)]
106#[error("wayland protocol \"{protocol}\" not supported: {inner}")]
107pub struct WaylandBindError {
108    inner: BindError,
109    protocol: &'static str,
110}
111
112#[cfg(wlroots)]
113impl WaylandBindError {
114    pub(crate) fn new(inner: BindError, protocol: &'static str) -> Self {
115        Self { inner, protocol }
116    }
117}
118
119#[cfg(libei)]
120#[derive(Debug, Error)]
121pub enum LibeiEmulationCreationError {
122    #[error(transparent)]
123    Ashpd(#[from] ashpd::Error),
124    #[error(transparent)]
125    Io(#[from] std::io::Error),
126    #[error(transparent)]
127    Reis(#[from] reis::Error),
128}
129
130#[cfg(rdp)]
131#[derive(Debug, Error)]
132pub enum XdpEmulationCreationError {
133    #[error(transparent)]
134    Ashpd(#[from] ashpd::Error),
135}
136
137#[cfg(x11)]
138#[derive(Debug, Error)]
139pub enum X11EmulationCreationError {
140    #[error("could not open display")]
141    OpenDisplay,
142}
143
144#[cfg(target_os = "macos")]
145#[derive(Debug, Error)]
146pub enum MacOSEmulationCreationError {
147    #[error("could not create event source")]
148    EventSourceCreation,
149    #[error("accessibility permission is required")]
150    AccessibilityPermission,
151    #[error("input control permission is required")]
152    InputControlPermission,
153}
154
155#[cfg(windows)]
156#[derive(Debug, Error)]
157pub enum WindowsEmulationCreationError {}