1use std::{fmt, str::FromStr};
20
21use serde::{Deserialize, Serialize};
22use zbus::zvariant::{SerializeDict, Type};
23
24use super::{HandleToken, Request};
25use crate::{proxy::Proxy, Error, Pid};
26
27#[derive(SerializeDict, Type, Debug, Default)]
28#[zvariant(signature = "dict")]
30struct AccessDeviceOptions {
31 handle_token: HandleToken,
33}
34
35#[cfg_attr(feature = "glib", derive(glib::Enum))]
36#[cfg_attr(feature = "glib", enum_type(name = "AshpdDevice"))]
37#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Type)]
38#[zvariant(signature = "s")]
39#[serde(rename_all = "lowercase")]
40pub enum Device {
42 Microphone,
44 Speakers,
46 Camera,
48}
49
50impl fmt::Display for Device {
51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52 match self {
53 Self::Microphone => write!(f, "Microphone"),
54 Self::Speakers => write!(f, "Speakers"),
55 Self::Camera => write!(f, "Camera"),
56 }
57 }
58}
59
60impl AsRef<str> for Device {
61 fn as_ref(&self) -> &str {
62 match self {
63 Self::Microphone => "Microphone",
64 Self::Speakers => "Speakers",
65 Self::Camera => "Camera",
66 }
67 }
68}
69
70impl From<Device> for &'static str {
71 fn from(d: Device) -> Self {
72 match d {
73 Device::Microphone => "Microphone",
74 Device::Speakers => "Speakers",
75 Device::Camera => "Camera",
76 }
77 }
78}
79
80impl FromStr for Device {
81 type Err = Error;
82
83 fn from_str(s: &str) -> Result<Self, Self::Err> {
84 match s {
85 "Microphone" | "microphone" => Ok(Device::Microphone),
86 "Speakers" | "speakers" => Ok(Device::Speakers),
87 "Camera" | "camera" => Ok(Device::Camera),
88 _ => Err(Error::ParseError("Failed to parse device, invalid value")),
89 }
90 }
91}
92
93#[derive(Debug)]
102#[doc(alias = "org.freedesktop.portal.Device")]
103pub struct DeviceProxy<'a>(Proxy<'a>);
104
105impl<'a> DeviceProxy<'a> {
106 pub async fn new() -> Result<DeviceProxy<'a>, Error> {
108 let proxy = Proxy::new_desktop("org.freedesktop.portal.Device").await?;
109 Ok(Self(proxy))
110 }
111
112 #[doc(alias = "AccessDevice")]
127 pub async fn access_device(&self, pid: Pid, devices: &[Device]) -> Result<Request<()>, Error> {
128 let options = AccessDeviceOptions::default();
129 self.0
130 .empty_request(
131 &options.handle_token,
132 "AccessDevice",
133 &(pid, devices, &options),
134 )
135 .await
136 }
137}
138
139impl<'a> std::ops::Deref for DeviceProxy<'a> {
140 type Target = zbus::Proxy<'a>;
141
142 fn deref(&self) -> &Self::Target {
143 &self.0
144 }
145}