aboutsummaryrefslogtreecommitdiffstats
path: root/input-emulation/src/xdg_desktop_portal.rs
blob: 7c2d133cea0489a192702043b45fd70d46577979 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use ashpd::{
    desktop::{
        PersistMode, Session,
        remote_desktop::{
            Axis, DeviceType, KeyState, NotifyPointerAxisOptions, RemoteDesktop,
            SelectDevicesOptions,
        },
    },
    zbus::AsyncDrop,
};
use async_trait::async_trait;

use futures::FutureExt;
use input_event::{
    Event::{Keyboard, Pointer},
    KeyboardEvent, PointerEvent,
};

use crate::error::EmulationError;

use super::{Emulation, EmulationHandle, error::XdpEmulationCreationError};

pub(crate) struct DesktopPortalEmulation {
    proxy: RemoteDesktop,
    session: Session<RemoteDesktop>,
}

impl DesktopPortalEmulation {
    pub(crate) async fn new() -> Result<DesktopPortalEmulation, XdpEmulationCreationError> {
        log::debug!("connecting to org.freedesktop.portal.RemoteDesktop portal ...");
        let proxy = RemoteDesktop::new().await?;

        // retry when user presses the cancel button
        log::debug!("creating session ...");
        let session = proxy.create_session(Default::default()).await?;

        log::debug!("selecting devices ...");
        let options = SelectDevicesOptions::default()
            .set_devices(DeviceType::Keyboard | DeviceType::Pointer)
            .set_persist_mode(PersistMode::ExplicitlyRevoked);
        proxy.select_devices(&session, options).await?;

        log::info!("requesting permission for input emulation");
        let _devices = proxy
            .start(&session, None, Default::default())
            .await?
            .response()?;

        log::debug!("started session");
        let session = session;

        Ok(Self { proxy, session })
    }
}

#[async_trait]
impl Emulation for DesktopPortalEmulation {
    async fn consume(
        &mut self,
        event: input_event::Event,
        _client: EmulationHandle,
    ) -> Result<(), EmulationError> {
        match event {
            Pointer(p) => match p {
                PointerEvent::Motion { time: _, dx, dy } => {
                    self.proxy
                        .notify_pointer_motion(&self.session, dx, dy, Default::default())
                        .await?;
                }
                PointerEvent::Button {
                    time: _,
                    button,
                    state,
                } => {
                    let state = match state {
                        0 => KeyState::Released,
                        _ => KeyState::Pressed,
                    };
                    self.proxy
                        .notify_pointer_button(
                            &self.session,
                            button as i32,
                            state,
                            Default::default(),
                        )
                        .await?;
                }
                PointerEvent::AxisDiscrete120 { axis, value } => {
                    let axis = match axis {
                        0 => Axis::Vertical,
                        _ => Axis::Horizontal,
                    };
                    self.proxy
                        .notify_pointer_axis_discrete(
                            &self.session,
                            axis,
                            value / 120,
                            Default::default(),
                        )
                        .await?;
                }
                PointerEvent::Axis {
                    time: _,
                    axis,
                    value,
                } => {
                    let axis = match axis {
                        0 => Axis::Vertical,
                        _ => Axis::Horizontal,
                    };
                    let (dx, dy) = match axis {
                        Axis::Vertical => (0., value),
                        Axis::Horizontal => (value, 0.),
                    };
                    self.proxy
                        .notify_pointer_axis(
                            &self.session,
                            dx,
                            dy,
                            NotifyPointerAxisOptions::default().set_finish(true),
                        )
                        .await?;
                }
            },
            Keyboard(k) => {
                match k {
                    KeyboardEvent::Key {
                        time: _,
                        key,
                        state,
                    } => {
                        let state = match state {
                            0 => KeyState::Released,
                            _ => KeyState::Pressed,
                        };
                        self.proxy
                            .notify_keyboard_keycode(
                                &self.session,
                                key as i32,
                                state,
                                Default::default(),
                            )
                            .await?;
                    }
                    KeyboardEvent::Modifiers { .. } => {
                        // ignore
                    }
                }
            }
        }
        Ok(())
    }

    async fn create(&mut self, _client: EmulationHandle) {}
    async fn destroy(&mut self, _client: EmulationHandle) {}
    async fn terminate(&mut self) {
        if let Err(e) = self.session.close().await {
            log::warn!("session.close(): {e}");
        };
        if let Err(e) = self.session.receive_closed().await {
            log::warn!("session.receive_closed(): {e}");
        };
    }
}

impl AsyncDrop for DesktopPortalEmulation {
    #[doc = r" Perform the async cleanup."]
    #[allow(clippy::type_complexity, clippy::type_repetition_in_bounds)]
    fn async_drop<'async_trait>(
        self,
    ) -> ::core::pin::Pin<
        Box<dyn ::core::future::Future<Output = ()> + ::core::marker::Send + 'async_trait>,
    >
    where
        Self: 'async_trait,
    {
        async move {
            let _ = self.session.close().await;
        }
        .boxed()
    }
}