use std::sync::Arc; use async_trait::async_trait; use serde::{Deserialize, Serialize}; use crate::{ MaybeAppID, WindowIdentifierType, backend::{ Result, request::{Request, RequestImpl}, }, desktop::{HandleToken, request::Response, usb::UsbDevice}, zvariant::{Optional, OwnedObjectPath, Type, as_value::optional}, }; #[derive(Debug, Deserialize, Type)] #[zvariant(signature = "dict")] pub struct AcquireDevicesOptions {} #[derive(Debug, Serialize, Deserialize, Type)] #[zvariant(signature = "dict")] pub struct AccessOptions { #[serde(default, with = "optional", skip_serializing_if = "Option::is_none")] writable: Option, } impl AccessOptions { pub fn new(is_writable: bool) -> Self { Self { writable: Some(is_writable), } } pub fn is_writable(&self) -> Option { self.writable } } #[async_trait] pub trait UsbImpl: RequestImpl { #[doc(alias = "AcquireDevices")] async fn acquire_devices( &self, token: HandleToken, window_identifier: Option, app_id: Option, devices: Vec<(String, UsbDevice, AccessOptions)>, options: AcquireDevicesOptions, ) -> Result>; } pub(crate) struct UsbInterface { imp: Arc, spawn: Arc, cnx: zbus::Connection, } impl UsbInterface { pub fn new( imp: Arc, cnx: zbus::Connection, spawn: Arc, ) -> Self { Self { imp, cnx, spawn } } } #[zbus::interface(name = "org.freedesktop.impl.portal.Usb")] impl UsbInterface { #[zbus(property(emits_changed_signal = "const"), name = "version")] fn version(&self) -> u32 { 1 } #[zbus(name = "AcquireDevices")] #[zbus(out_args("response", "results"))] async fn acquire_devices( &self, handle: OwnedObjectPath, window_identifier: Optional, app_id: Optional, devices: Vec<(String, UsbDevice, AccessOptions)>, options: AcquireDevicesOptions, ) -> Result>> { let imp = Arc::clone(&self.imp); Request::spawn( "Usb::AcquireDevices", &self.cnx, handle.clone(), Arc::clone(&self.imp), Arc::clone(&self.spawn), async move { imp.acquire_devices( HandleToken::try_from(&handle).unwrap(), window_identifier.into(), app_id.into(), devices, options, ) .await }, ) .await } }