ashpd 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +66 -0
- data/Cargo.lock +1 -0
- data/ext/ashpd/Cargo.toml +9 -2
- data/ext/ashpd/src/devices.rs +152 -0
- data/ext/ashpd/src/files.rs +211 -0
- data/ext/ashpd/src/lib.rs +124 -6
- data/ext/ashpd/src/screencast.rs +387 -0
- data/ext/ashpd/src/sessions.rs +545 -0
- data/ext/ashpd/src/shell.rs +278 -0
- data/ext/ashpd/src/system.rs +220 -0
- data/lib/ashpd/version.rb +1 -1
- metadata +8 -1
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
//! The portals that are a conversation rather than a question.
|
|
2
|
+
//!
|
|
3
|
+
//! Each of these is created, configured, started, and then lives until it is
|
|
4
|
+
//! closed - so each is a class, and the object has to be kept for as long as
|
|
5
|
+
//! what it granted is wanted. Let it go and the grant goes with it.
|
|
6
|
+
//!
|
|
7
|
+
//! They all follow the shape {crate::screencast} sets out, and for the same
|
|
8
|
+
//! reason: every zbus object is created, used and dropped inside one
|
|
9
|
+
//! `block_on`, because one dropped outside the runtime panics, and a panic
|
|
10
|
+
//! across this boundary takes the VM rather than raising.
|
|
11
|
+
//!
|
|
12
|
+
//! - org.freedesktop.portal.Location
|
|
13
|
+
//! - org.freedesktop.portal.GlobalShortcuts
|
|
14
|
+
//! - org.freedesktop.portal.RemoteDesktop
|
|
15
|
+
|
|
16
|
+
use std::os::fd::IntoRawFd;
|
|
17
|
+
use std::sync::Mutex;
|
|
18
|
+
use std::time::Duration;
|
|
19
|
+
|
|
20
|
+
use futures_util::StreamExt;
|
|
21
|
+
use magnus::{function, method, prelude::*, Error, RArray, RHash, RModule, Ruby, TryConvert};
|
|
22
|
+
|
|
23
|
+
use ashpd_core::desktop::global_shortcuts::{GlobalShortcuts, NewShortcut};
|
|
24
|
+
use ashpd_core::desktop::location::{Accuracy, CreateSessionOptions as LocationOptions, LocationProxy};
|
|
25
|
+
use ashpd_core::desktop::remote_desktop::{
|
|
26
|
+
DeviceType, KeyState, RemoteDesktop, SelectDevicesOptions,
|
|
27
|
+
};
|
|
28
|
+
use ashpd_core::desktop::{PersistMode, Session};
|
|
29
|
+
use ashpd_core::enumflags2::BitFlags;
|
|
30
|
+
|
|
31
|
+
use crate::{block, option_bool, option_name, option_number, rt_err, runtime};
|
|
32
|
+
|
|
33
|
+
/// How long a wait for an event will sit there before giving up, when the
|
|
34
|
+
/// caller named no limit. A wait without a bound is a hung Ruby thread.
|
|
35
|
+
const DEFAULT_TIMEOUT: f64 = 30.0;
|
|
36
|
+
|
|
37
|
+
fn timeout_of(seconds: Option<f64>) -> Duration {
|
|
38
|
+
Duration::from_secs_f64(seconds.unwrap_or(DEFAULT_TIMEOUT).max(0.0))
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// --- org.freedesktop.portal.Location ---------------------------------------
|
|
42
|
+
|
|
43
|
+
/// Where the user is, if they allow it.
|
|
44
|
+
///
|
|
45
|
+
/// The portal does not answer with a position: it starts sending them, and
|
|
46
|
+
/// {next_location} waits for the next one. The first can take a while - the
|
|
47
|
+
/// host may have to turn a radio on - so the timeout matters.
|
|
48
|
+
#[magnus::wrap(class = "Ashpd::Location::Session", free_immediately, size)]
|
|
49
|
+
struct LocationSession {
|
|
50
|
+
parts: Mutex<Option<(LocationProxy, Session<LocationProxy>)>>,
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
fn accuracy_from(name: &str) -> Result<Accuracy, Error> {
|
|
54
|
+
match name {
|
|
55
|
+
"none" => Ok(Accuracy::None),
|
|
56
|
+
"country" => Ok(Accuracy::Country),
|
|
57
|
+
"city" => Ok(Accuracy::City),
|
|
58
|
+
"neighborhood" => Ok(Accuracy::Neighborhood),
|
|
59
|
+
"street" => Ok(Accuracy::Street),
|
|
60
|
+
"exact" => Ok(Accuracy::Exact),
|
|
61
|
+
other => Err(rt_err(format!(
|
|
62
|
+
"unknown accuracy {other:?}, wanted :none, :country, :city, :neighborhood, :street or :exact"
|
|
63
|
+
))),
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
impl LocationSession {
|
|
68
|
+
/// `accuracy:` is what to ask the host for, and asking for less than
|
|
69
|
+
/// `:exact` is the polite default when a city will do.
|
|
70
|
+
fn new(ruby: &Ruby, options: RHash) -> Result<Self, Error> {
|
|
71
|
+
let mut create = LocationOptions::default();
|
|
72
|
+
|
|
73
|
+
if let Some(name) = option_name(ruby, options, "accuracy")? {
|
|
74
|
+
create = create.set_accuracy(accuracy_from(&name)?);
|
|
75
|
+
}
|
|
76
|
+
if let Some(distance) = option_number(ruby, options, "distance_threshold")? {
|
|
77
|
+
create = create.set_distance_threshold(distance as u32);
|
|
78
|
+
}
|
|
79
|
+
if let Some(time) = option_number(ruby, options, "time_threshold")? {
|
|
80
|
+
create = create.set_time_threshold(time as u32);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let parts = block(async {
|
|
84
|
+
let proxy = LocationProxy::new().await?;
|
|
85
|
+
let session = proxy.create_session(create).await?;
|
|
86
|
+
|
|
87
|
+
Ok::<_, ashpd_core::Error>((proxy, session))
|
|
88
|
+
})?
|
|
89
|
+
.map_err(rt_err)?;
|
|
90
|
+
|
|
91
|
+
Ok(Self {
|
|
92
|
+
parts: Mutex::new(Some(parts)),
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/// Start locating, which is where the user is asked.
|
|
97
|
+
fn start(&self) -> Result<(), Error> {
|
|
98
|
+
let guard = self.parts.lock().map_err(rt_err)?;
|
|
99
|
+
let (proxy, session) = guard
|
|
100
|
+
.as_ref()
|
|
101
|
+
.ok_or_else(|| rt_err("the location session is closed"))?;
|
|
102
|
+
|
|
103
|
+
block(async { proxy.start(session, None, Default::default()).await?.response() })?.map_err(rt_err)?;
|
|
104
|
+
|
|
105
|
+
Ok(())
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/// Wait for the next position, as `{"latitude" =>, "longitude" =>,
|
|
109
|
+
/// "altitude" =>, "accuracy" =>, "speed" =>, "heading" =>,
|
|
110
|
+
/// "description" =>}`, or nil if none arrived in time.
|
|
111
|
+
///
|
|
112
|
+
/// The first one can be slow - the host may have to turn a radio on - so
|
|
113
|
+
/// the timeout is worth setting generously.
|
|
114
|
+
fn next_location(ruby: &Ruby, rb_self: &Self, seconds: Option<f64>) -> Result<Option<RHash>, Error> {
|
|
115
|
+
let guard = rb_self.parts.lock().map_err(rt_err)?;
|
|
116
|
+
let (proxy, _session) = guard
|
|
117
|
+
.as_ref()
|
|
118
|
+
.ok_or_else(|| rt_err("the location session is closed"))?;
|
|
119
|
+
|
|
120
|
+
let located = block(async {
|
|
121
|
+
let mut stream = proxy.receive_location_updated().await?;
|
|
122
|
+
|
|
123
|
+
Ok::<_, ashpd_core::Error>(
|
|
124
|
+
tokio::time::timeout(timeout_of(seconds), stream.next())
|
|
125
|
+
.await
|
|
126
|
+
.ok()
|
|
127
|
+
.flatten(),
|
|
128
|
+
)
|
|
129
|
+
})?
|
|
130
|
+
.map_err(rt_err)?;
|
|
131
|
+
|
|
132
|
+
let Some(location) = located else {
|
|
133
|
+
return Ok(None);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
let hash = ruby.hash_new();
|
|
137
|
+
hash.aset("latitude", location.latitude())?;
|
|
138
|
+
hash.aset("longitude", location.longitude())?;
|
|
139
|
+
hash.aset("altitude", location.altitude())?;
|
|
140
|
+
hash.aset("accuracy", location.accuracy())?;
|
|
141
|
+
hash.aset("speed", location.speed())?;
|
|
142
|
+
hash.aset("heading", location.heading())?;
|
|
143
|
+
hash.aset("description", location.description())?;
|
|
144
|
+
|
|
145
|
+
Ok(Some(hash))
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
fn close(&self) -> Result<(), Error> {
|
|
149
|
+
let Some((_proxy, session)) = self.parts.lock().map_err(rt_err)?.take() else {
|
|
150
|
+
return Ok(());
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
block(async {
|
|
154
|
+
let result = session.close().await;
|
|
155
|
+
drop(session);
|
|
156
|
+
result
|
|
157
|
+
})?
|
|
158
|
+
.map_err(rt_err)
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
impl Drop for LocationSession {
|
|
163
|
+
fn drop(&mut self) {
|
|
164
|
+
let Ok(mut guard) = self.parts.lock() else { return };
|
|
165
|
+
let Some(parts) = guard.take() else { return };
|
|
166
|
+
let Ok(runtime) = runtime() else { return };
|
|
167
|
+
|
|
168
|
+
runtime.block_on(async move { drop(parts) });
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// --- org.freedesktop.portal.GlobalShortcuts --------------------------------
|
|
173
|
+
|
|
174
|
+
/// Shortcuts that work whether or not this application has focus.
|
|
175
|
+
///
|
|
176
|
+
/// The user is shown what is being asked for and may change the keys, so what
|
|
177
|
+
/// {bind} returns is what they agreed to, not what was requested.
|
|
178
|
+
#[magnus::wrap(class = "Ashpd::GlobalShortcuts::Session", free_immediately, size)]
|
|
179
|
+
struct ShortcutsSession {
|
|
180
|
+
parts: Mutex<Option<(GlobalShortcuts, Session<GlobalShortcuts>)>>,
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
impl ShortcutsSession {
|
|
184
|
+
fn new() -> Result<Self, Error> {
|
|
185
|
+
let parts = block(async {
|
|
186
|
+
let proxy = GlobalShortcuts::new().await?;
|
|
187
|
+
let session = proxy.create_session(Default::default()).await?;
|
|
188
|
+
|
|
189
|
+
Ok::<_, ashpd_core::Error>((proxy, session))
|
|
190
|
+
})?
|
|
191
|
+
.map_err(rt_err)?;
|
|
192
|
+
|
|
193
|
+
Ok(Self {
|
|
194
|
+
parts: Mutex::new(Some(parts)),
|
|
195
|
+
})
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/// Ask for shortcuts, as `[{id:, description:, preferred_trigger:}]`.
|
|
199
|
+
///
|
|
200
|
+
/// Returns what the user agreed to, as
|
|
201
|
+
/// `[{"id" =>, "description" =>, "trigger_description" =>}]`.
|
|
202
|
+
fn bind(ruby: &Ruby, rb_self: &Self, wanted: RArray) -> Result<RArray, Error> {
|
|
203
|
+
let mut shortcuts = Vec::new();
|
|
204
|
+
|
|
205
|
+
for item in wanted.into_iter() {
|
|
206
|
+
let hash = RHash::try_convert(item)?;
|
|
207
|
+
let id = option_name(ruby, hash, "id")?
|
|
208
|
+
.ok_or_else(|| rt_err("every shortcut needs an :id"))?;
|
|
209
|
+
let description = option_name(ruby, hash, "description")?
|
|
210
|
+
.ok_or_else(|| rt_err("every shortcut needs a :description"))?;
|
|
211
|
+
let mut shortcut = NewShortcut::new(id, description);
|
|
212
|
+
|
|
213
|
+
if let Some(trigger) = option_name(ruby, hash, "preferred_trigger")? {
|
|
214
|
+
shortcut = shortcut.preferred_trigger(Some(trigger.as_str()));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
shortcuts.push(shortcut);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
let guard = rb_self.parts.lock().map_err(rt_err)?;
|
|
221
|
+
let (proxy, session) = guard
|
|
222
|
+
.as_ref()
|
|
223
|
+
.ok_or_else(|| rt_err("the global shortcuts session is closed"))?;
|
|
224
|
+
|
|
225
|
+
let bound = block(async {
|
|
226
|
+
proxy
|
|
227
|
+
.bind_shortcuts(session, &shortcuts, None, Default::default())
|
|
228
|
+
.await?
|
|
229
|
+
.response()
|
|
230
|
+
})?
|
|
231
|
+
.map_err(rt_err)?;
|
|
232
|
+
|
|
233
|
+
let array = ruby.ary_new();
|
|
234
|
+
for shortcut in bound.shortcuts() {
|
|
235
|
+
let hash = ruby.hash_new();
|
|
236
|
+
hash.aset("id", shortcut.id())?;
|
|
237
|
+
hash.aset("description", shortcut.description())?;
|
|
238
|
+
hash.aset("trigger_description", shortcut.trigger_description())?;
|
|
239
|
+
let _ = array.push(hash);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
Ok(array)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/// What is bound now, in the same shape as {bind} returns.
|
|
246
|
+
fn list(ruby: &Ruby, rb_self: &Self) -> Result<RArray, Error> {
|
|
247
|
+
let guard = rb_self.parts.lock().map_err(rt_err)?;
|
|
248
|
+
let (proxy, session) = guard
|
|
249
|
+
.as_ref()
|
|
250
|
+
.ok_or_else(|| rt_err("the global shortcuts session is closed"))?;
|
|
251
|
+
|
|
252
|
+
let listed = block(async { proxy.list_shortcuts(session, Default::default()).await?.response() })?
|
|
253
|
+
.map_err(rt_err)?;
|
|
254
|
+
|
|
255
|
+
let array = ruby.ary_new();
|
|
256
|
+
for shortcut in listed.shortcuts() {
|
|
257
|
+
let hash = ruby.hash_new();
|
|
258
|
+
hash.aset("id", shortcut.id())?;
|
|
259
|
+
hash.aset("description", shortcut.description())?;
|
|
260
|
+
hash.aset("trigger_description", shortcut.trigger_description())?;
|
|
261
|
+
let _ = array.push(hash);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
Ok(array)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/// Wait for the user to press one of them, and say which.
|
|
268
|
+
///
|
|
269
|
+
/// Returns the shortcut's id, or nil if nothing was pressed before the
|
|
270
|
+
/// timeout. Blocks the calling thread, so this belongs on a thread of its
|
|
271
|
+
/// own.
|
|
272
|
+
fn next_activated(rb_self: &Self, seconds: Option<f64>) -> Result<Option<String>, Error> {
|
|
273
|
+
let guard = rb_self.parts.lock().map_err(rt_err)?;
|
|
274
|
+
let (proxy, _session) = guard
|
|
275
|
+
.as_ref()
|
|
276
|
+
.ok_or_else(|| rt_err("the global shortcuts session is closed"))?;
|
|
277
|
+
|
|
278
|
+
let activated = block(async {
|
|
279
|
+
let mut stream = proxy.receive_activated().await?;
|
|
280
|
+
|
|
281
|
+
Ok::<_, ashpd_core::Error>(
|
|
282
|
+
tokio::time::timeout(timeout_of(seconds), stream.next())
|
|
283
|
+
.await
|
|
284
|
+
.ok()
|
|
285
|
+
.flatten(),
|
|
286
|
+
)
|
|
287
|
+
})?
|
|
288
|
+
.map_err(rt_err)?;
|
|
289
|
+
|
|
290
|
+
Ok(activated.map(|event| event.shortcut_id().to_owned()))
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
fn close(&self) -> Result<(), Error> {
|
|
294
|
+
let Some((_proxy, session)) = self.parts.lock().map_err(rt_err)?.take() else {
|
|
295
|
+
return Ok(());
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
block(async {
|
|
299
|
+
let result = session.close().await;
|
|
300
|
+
drop(session);
|
|
301
|
+
result
|
|
302
|
+
})?
|
|
303
|
+
.map_err(rt_err)
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
impl Drop for ShortcutsSession {
|
|
308
|
+
fn drop(&mut self) {
|
|
309
|
+
let Ok(mut guard) = self.parts.lock() else { return };
|
|
310
|
+
let Some(parts) = guard.take() else { return };
|
|
311
|
+
let Ok(runtime) = runtime() else { return };
|
|
312
|
+
|
|
313
|
+
runtime.block_on(async move { drop(parts) });
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// --- org.freedesktop.portal.RemoteDesktop ----------------------------------
|
|
318
|
+
|
|
319
|
+
fn device_type_from(name: &str) -> Result<DeviceType, Error> {
|
|
320
|
+
match name {
|
|
321
|
+
"keyboard" => Ok(DeviceType::Keyboard),
|
|
322
|
+
"pointer" => Ok(DeviceType::Pointer),
|
|
323
|
+
"touchscreen" => Ok(DeviceType::Touchscreen),
|
|
324
|
+
other => Err(rt_err(format!(
|
|
325
|
+
"unknown device type {other:?}, wanted :keyboard, :pointer or :touchscreen"
|
|
326
|
+
))),
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/// Driving someone's desktop: moving their pointer, pressing their keys.
|
|
331
|
+
///
|
|
332
|
+
/// The user is asked once, for the devices named in {select_devices}, and the
|
|
333
|
+
/// notify methods do nothing until {start} has been answered. Every
|
|
334
|
+
/// coordinate is in the stream's own space, not the screen's.
|
|
335
|
+
#[magnus::wrap(class = "Ashpd::RemoteDesktop::Session", free_immediately, size)]
|
|
336
|
+
struct RemoteDesktopSession {
|
|
337
|
+
parts: Mutex<Option<(RemoteDesktop, Session<RemoteDesktop>)>>,
|
|
338
|
+
started: Mutex<Option<String>>,
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
impl RemoteDesktopSession {
|
|
342
|
+
fn new() -> Result<Self, Error> {
|
|
343
|
+
let parts = block(async {
|
|
344
|
+
let proxy = RemoteDesktop::new().await?;
|
|
345
|
+
let session = proxy.create_session(Default::default()).await?;
|
|
346
|
+
|
|
347
|
+
Ok::<_, ashpd_core::Error>((proxy, session))
|
|
348
|
+
})?
|
|
349
|
+
.map_err(rt_err)?;
|
|
350
|
+
|
|
351
|
+
Ok(Self {
|
|
352
|
+
parts: Mutex::new(Some(parts)),
|
|
353
|
+
started: Mutex::new(None),
|
|
354
|
+
})
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/// Say which devices to ask for, before starting.
|
|
358
|
+
fn select_devices(ruby: &Ruby, rb_self: &Self, options: RHash) -> Result<(), Error> {
|
|
359
|
+
let mut selection = SelectDevicesOptions::default();
|
|
360
|
+
|
|
361
|
+
if let Some(devices) = options.get(ruby.to_symbol("devices")) {
|
|
362
|
+
let mut types = BitFlags::<DeviceType>::empty();
|
|
363
|
+
|
|
364
|
+
for device in RArray::try_convert(devices)?.into_iter() {
|
|
365
|
+
types |= device_type_from(&device.to_r_string()?.to_string()?)?;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
selection = selection.set_devices(types);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
if let Some(token) = option_name(ruby, options, "restore_token")? {
|
|
372
|
+
selection = selection.set_restore_token(Some(token.as_str()));
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
if option_bool(ruby, options, "persist").unwrap_or(false) {
|
|
376
|
+
selection = selection.set_persist_mode(PersistMode::ExplicitlyRevoked);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
let guard = rb_self.parts.lock().map_err(rt_err)?;
|
|
380
|
+
let (proxy, session) = guard
|
|
381
|
+
.as_ref()
|
|
382
|
+
.ok_or_else(|| rt_err("the remote desktop session is closed"))?;
|
|
383
|
+
|
|
384
|
+
block(async { proxy.select_devices(session, selection).await?.response() })?
|
|
385
|
+
.map_err(rt_err)?;
|
|
386
|
+
|
|
387
|
+
Ok(())
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/// Start, which is where the user is asked. Returns what they granted, as
|
|
391
|
+
/// `{"devices" =>, "restore_token" =>, "clipboard" =>}`.
|
|
392
|
+
fn start(ruby: &Ruby, rb_self: &Self) -> Result<RHash, Error> {
|
|
393
|
+
let granted = {
|
|
394
|
+
let guard = rb_self.parts.lock().map_err(rt_err)?;
|
|
395
|
+
let (proxy, session) = guard
|
|
396
|
+
.as_ref()
|
|
397
|
+
.ok_or_else(|| rt_err("the remote desktop session is closed"))?;
|
|
398
|
+
|
|
399
|
+
block(async { proxy.start(session, None, Default::default()).await?.response() })?.map_err(rt_err)?
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
let devices = ruby.ary_new();
|
|
403
|
+
for device_type in granted.devices().iter() {
|
|
404
|
+
let _ = devices.push(ruby.to_symbol(match device_type {
|
|
405
|
+
DeviceType::Keyboard => "keyboard",
|
|
406
|
+
DeviceType::Pointer => "pointer",
|
|
407
|
+
DeviceType::Touchscreen => "touchscreen",
|
|
408
|
+
}));
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
if let Some(token) = granted.restore_token() {
|
|
412
|
+
*rb_self.started.lock().map_err(rt_err)? = Some(token.to_owned());
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
let hash = ruby.hash_new();
|
|
416
|
+
hash.aset("devices", devices)?;
|
|
417
|
+
hash.aset("restore_token", granted.restore_token())?;
|
|
418
|
+
hash.aset("clipboard", granted.is_clipboard_enabled())?;
|
|
419
|
+
|
|
420
|
+
Ok(hash)
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/// The token to keep, so a later session can skip the dialog.
|
|
424
|
+
fn restore_token(&self) -> Result<Option<String>, Error> {
|
|
425
|
+
Ok(self.started.lock().map_err(rt_err)?.clone())
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/// Move the pointer by a relative amount.
|
|
429
|
+
fn move_pointer(&self, dx: f64, dy: f64) -> Result<(), Error> {
|
|
430
|
+
let guard = self.parts.lock().map_err(rt_err)?;
|
|
431
|
+
let (proxy, session) = guard
|
|
432
|
+
.as_ref()
|
|
433
|
+
.ok_or_else(|| rt_err("the remote desktop session is closed"))?;
|
|
434
|
+
|
|
435
|
+
block(async { proxy.notify_pointer_motion(session, dx, dy, Default::default()).await })?.map_err(rt_err)
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/// Press or release a pointer button, by its evdev code (BTN_LEFT is
|
|
439
|
+
/// 0x110).
|
|
440
|
+
fn pointer_button(&self, button: i32, pressed: bool) -> Result<(), Error> {
|
|
441
|
+
let guard = self.parts.lock().map_err(rt_err)?;
|
|
442
|
+
let (proxy, session) = guard
|
|
443
|
+
.as_ref()
|
|
444
|
+
.ok_or_else(|| rt_err("the remote desktop session is closed"))?;
|
|
445
|
+
|
|
446
|
+
let state = if pressed {
|
|
447
|
+
KeyState::Pressed
|
|
448
|
+
} else {
|
|
449
|
+
KeyState::Released
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
block(async { proxy.notify_pointer_button(session, button, state, Default::default()).await })?
|
|
453
|
+
.map_err(rt_err)
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/// Press or release a key, by its evdev keycode.
|
|
457
|
+
fn key(&self, keycode: i32, pressed: bool) -> Result<(), Error> {
|
|
458
|
+
let guard = self.parts.lock().map_err(rt_err)?;
|
|
459
|
+
let (proxy, session) = guard
|
|
460
|
+
.as_ref()
|
|
461
|
+
.ok_or_else(|| rt_err("the remote desktop session is closed"))?;
|
|
462
|
+
|
|
463
|
+
let state = if pressed {
|
|
464
|
+
KeyState::Pressed
|
|
465
|
+
} else {
|
|
466
|
+
KeyState::Released
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
block(async { proxy.notify_keyboard_keycode(session, keycode, state, Default::default()).await })?
|
|
470
|
+
.map_err(rt_err)
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/// A descriptor for an EIS socket, for applications that would rather
|
|
474
|
+
/// speak libei than send one event at a time. Ruby owns it once given.
|
|
475
|
+
fn connect_to_eis(&self) -> Result<i32, Error> {
|
|
476
|
+
let guard = self.parts.lock().map_err(rt_err)?;
|
|
477
|
+
let (proxy, session) = guard
|
|
478
|
+
.as_ref()
|
|
479
|
+
.ok_or_else(|| rt_err("the remote desktop session is closed"))?;
|
|
480
|
+
|
|
481
|
+
let fd = block(async { proxy.connect_to_eis(session, Default::default()).await })?.map_err(rt_err)?;
|
|
482
|
+
|
|
483
|
+
Ok(fd.into_raw_fd())
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
fn close(&self) -> Result<(), Error> {
|
|
487
|
+
let Some((_proxy, session)) = self.parts.lock().map_err(rt_err)?.take() else {
|
|
488
|
+
return Ok(());
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
block(async {
|
|
492
|
+
let result = session.close().await;
|
|
493
|
+
drop(session);
|
|
494
|
+
result
|
|
495
|
+
})?
|
|
496
|
+
.map_err(rt_err)
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
impl Drop for RemoteDesktopSession {
|
|
501
|
+
fn drop(&mut self) {
|
|
502
|
+
let Ok(mut guard) = self.parts.lock() else { return };
|
|
503
|
+
let Some(parts) = guard.take() else { return };
|
|
504
|
+
let Ok(runtime) = runtime() else { return };
|
|
505
|
+
|
|
506
|
+
runtime.block_on(async move { drop(parts) });
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
pub fn init(ruby: &Ruby, namespace: RModule) -> Result<(), Error> {
|
|
511
|
+
let shortcuts = namespace.define_module("GlobalShortcuts")?;
|
|
512
|
+
let shortcuts_session = shortcuts.define_class("Session", ruby.class_object())?;
|
|
513
|
+
shortcuts_session.define_singleton_method("new", function!(ShortcutsSession::new, 0))?;
|
|
514
|
+
shortcuts_session.define_method("bind", method!(ShortcutsSession::bind, 1))?;
|
|
515
|
+
shortcuts_session.define_method("list", method!(ShortcutsSession::list, 0))?;
|
|
516
|
+
shortcuts_session
|
|
517
|
+
.define_method("next_activated", method!(ShortcutsSession::next_activated, 1))?;
|
|
518
|
+
shortcuts_session.define_method("close", method!(ShortcutsSession::close, 0))?;
|
|
519
|
+
|
|
520
|
+
let remote = namespace.define_module("RemoteDesktop")?;
|
|
521
|
+
let remote_session = remote.define_class("Session", ruby.class_object())?;
|
|
522
|
+
remote_session.define_singleton_method("new", function!(RemoteDesktopSession::new, 0))?;
|
|
523
|
+
remote_session
|
|
524
|
+
.define_method("select_devices", method!(RemoteDesktopSession::select_devices, 1))?;
|
|
525
|
+
remote_session.define_method("start", method!(RemoteDesktopSession::start, 0))?;
|
|
526
|
+
remote_session
|
|
527
|
+
.define_method("restore_token", method!(RemoteDesktopSession::restore_token, 0))?;
|
|
528
|
+
remote_session.define_method("move_pointer", method!(RemoteDesktopSession::move_pointer, 2))?;
|
|
529
|
+
remote_session
|
|
530
|
+
.define_method("pointer_button", method!(RemoteDesktopSession::pointer_button, 2))?;
|
|
531
|
+
remote_session.define_method("key", method!(RemoteDesktopSession::key, 2))?;
|
|
532
|
+
remote_session
|
|
533
|
+
.define_method("connect_to_eis", method!(RemoteDesktopSession::connect_to_eis, 0))?;
|
|
534
|
+
remote_session.define_method("close", method!(RemoteDesktopSession::close, 0))?;
|
|
535
|
+
|
|
536
|
+
let location = namespace.define_module("Location")?;
|
|
537
|
+
let location_session = location.define_class("Session", ruby.class_object())?;
|
|
538
|
+
location_session.define_singleton_method("new", function!(LocationSession::new, 1))?;
|
|
539
|
+
location_session.define_method("start", method!(LocationSession::start, 0))?;
|
|
540
|
+
location_session
|
|
541
|
+
.define_method("next_location", method!(LocationSession::next_location, 1))?;
|
|
542
|
+
location_session.define_method("close", method!(LocationSession::close, 0))?;
|
|
543
|
+
|
|
544
|
+
Ok(())
|
|
545
|
+
}
|