1use std::sync::Arc;
2
3use async_trait::async_trait;
4
5use crate::{
6 backend::{
7 request::{Request, RequestImpl},
8 MaybeAppID, MaybeWindowIdentifier, Result,
9 },
10 desktop::{request::Response, usb::UsbDevice, HandleToken},
11 zvariant::{DeserializeDict, OwnedObjectPath, SerializeDict, Type},
12 AppID, WindowIdentifierType,
13};
14
15#[derive(Debug, DeserializeDict, Type)]
16#[zvariant(signature = "dict")]
17pub struct AcquireDevicesOptions {}
18
19#[derive(Debug, SerializeDict, DeserializeDict, Type)]
20#[zvariant(signature = "dict")]
21pub struct AccessOptions {
22 writable: Option<bool>,
23}
24
25impl AccessOptions {
26 pub fn new(is_writable: bool) -> Self {
27 Self {
28 writable: Some(is_writable),
29 }
30 }
31
32 pub fn is_writable(&self) -> Option<bool> {
33 self.writable
34 }
35}
36
37#[async_trait]
38pub trait UsbImpl: RequestImpl {
39 async fn acquire_devices(
40 &self,
41 token: HandleToken,
42 window_identifier: Option<WindowIdentifierType>,
43 app_id: Option<AppID>,
44 devices: Vec<(String, UsbDevice, AccessOptions)>,
45 options: AcquireDevicesOptions,
46 ) -> Result<Vec<(String, AccessOptions)>>;
47}
48
49pub(crate) struct UsbInterface {
50 imp: Arc<dyn UsbImpl>,
51 spawn: Arc<dyn futures_util::task::Spawn + Send + Sync>,
52 cnx: zbus::Connection,
53}
54
55impl UsbInterface {
56 pub fn new(
57 imp: Arc<dyn UsbImpl>,
58 cnx: zbus::Connection,
59 spawn: Arc<dyn futures_util::task::Spawn + Send + Sync>,
60 ) -> Self {
61 Self { imp, cnx, spawn }
62 }
63}
64
65#[zbus::interface(name = "org.freedesktop.impl.portal.Usb")]
66impl UsbInterface {
67 #[zbus(property(emits_changed_signal = "const"), name = "version")]
68 fn version(&self) -> u32 {
69 1
70 }
71
72 #[zbus(name = "AcquireDevices")]
73 #[zbus(out_args("response", "results"))]
74 async fn acquire_devices(
75 &self,
76 handle: OwnedObjectPath,
77 window_identifier: MaybeWindowIdentifier,
78 app_id: MaybeAppID,
79 devices: Vec<(String, UsbDevice, AccessOptions)>,
80 options: AcquireDevicesOptions,
81 ) -> Result<Response<Vec<(String, AccessOptions)>>> {
82 let imp = Arc::clone(&self.imp);
83
84 Request::spawn(
85 "Usb::AcquireDevices",
86 &self.cnx,
87 handle.clone(),
88 Arc::clone(&self.imp),
89 Arc::clone(&self.spawn),
90 async move {
91 imp.acquire_devices(
92 HandleToken::try_from(&handle).unwrap(),
93 window_identifier.inner(),
94 app_id.inner(),
95 devices,
96 options,
97 )
98 .await
99 },
100 )
101 .await
102 }
103}