summaryrefslogtreecommitdiffstats
path: root/src/emulate/xdg_desktop_portal.rs
blob: e0ff6c4a7e9e7a8742fb3ea4181109a3fbb91eef (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
use anyhow::Result;
use ashpd::{
    desktop::{
        remote_desktop::{Axis, DeviceType, KeyState, RemoteDesktop},
        ResponseError, Session,
    },
    WindowIdentifier,
};
use async_trait::async_trait;

use crate::{
    client::ClientEvent,
    emulate::InputEmulation,
    event::{
        Event::{Keyboard, Pointer},
        KeyboardEvent, PointerEvent,
    },
};

pub struct DesktopPortalEmulation<'a> {
    proxy: RemoteDesktop<'a>,
    session: Session<'a>,
}

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

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

            log::debug!("selecting devices ...");
            proxy
                .select_devices(&session, DeviceType::Keyboard | DeviceType::Pointer)
                .await?;

            log::info!("requesting permission for input emulation");
            match proxy
                .start(&session, &WindowIdentifier::default())
                .await?
                .response()
            {
                Ok(d) => break (session, d),
                Err(ashpd::Error::Response(ResponseError::Cancelled)) => {
                    log::warn!("request cancelled!");
                    continue;
                }
                e => e?,
            };
        };

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

        Ok(Self { proxy, session })
    }
}

#[async_trait]
impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
    async fn consume(&mut self, event: crate::event::Event, _client: crate::client::ClientHandle) {
        match event {
            Pointer(p) => match p {
                PointerEvent::Motion {
                    time: _,
                    relative_x,
                    relative_y,
                } => {
                    if let Err(e) = self
                        .proxy
                        .notify_pointer_motion(&self.session, relative_x, relative_y)
                        .await
                    {
                        log::warn!("{e}");
                    }
                }
                PointerEvent::Button {
                    time: _,
                    button,
                    state,
                } => {
                    let state = match state {
                        0 => KeyState::Released,
                        _ => KeyState::Pressed,
                    };
                    if let Err(e) = self
                        .proxy
                        .notify_pointer_button(&self.session, button as i32, state)
                        .await
                    {
                        log::warn!("{e}");
                    }
                }
                PointerEvent::AxisDiscrete120 { axis, value } => {
                    let axis = match axis {
                        0 => Axis::Vertical,
                        _ => Axis::Horizontal,
                    };
                    if let Err(e) = self
                        .proxy
                        .notify_pointer_axis_discrete(&self.session, axis, value)
                        .await
                    {
                        log::warn!("{e}");
                    }
                }
                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.),
                    };
                    if let Err(e) = self
                        .proxy
                        .notify_pointer_axis(&self.session, dx, dy, true)
                        .await
                    {
                        log::warn!("{e}");
                    }
                }
                PointerEvent::Frame {} => {}
            },
            Keyboard(k) => {
                match k {
                    KeyboardEvent::Key {
                        time: _,
                        key,
                        state,
                    } => {
                        let state = match state {
                            0 => KeyState::Released,
                            _ => KeyState::Pressed,
                        };
                        if let Err(e) = self
                            .proxy
                            .notify_keyboard_keycode(&self.session, key as i32, state)
                            .await
                        {
                            log::warn!("{e}");
                        }
                    }
                    KeyboardEvent::Modifiers { .. } => {
                        // ignore
                    }
                }
            }
            _ => {}
        }
    }

    async fn notify(&mut self, _client: ClientEvent) {}

    async fn destroy(&mut self) {
        log::debug!("closing remote desktop session");
        if let Err(e) = self.session.close().await {
            log::error!("failed to close remote desktop session: {e}");
        }
    }
}