use std::{fmt, str::FromStr};
use serde::{Deserialize, Serialize};
use zbus::zvariant::{SerializeDict, Type};
use super::{HandleToken, Request};
use crate::{proxy::Proxy, Error};
#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct AccessDeviceOptions {
handle_token: HandleToken,
}
#[cfg_attr(feature = "glib", derive(glib::Enum))]
#[cfg_attr(feature = "glib", enum_type(name = "AshpdDevice"))]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Type)]
#[zvariant(signature = "s")]
#[serde(rename_all = "lowercase")]
pub enum Device {
Microphone,
Speakers,
Camera,
}
impl fmt::Display for Device {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Microphone => write!(f, "Microphone"),
Self::Speakers => write!(f, "Speakers"),
Self::Camera => write!(f, "Camera"),
}
}
}
impl AsRef<str> for Device {
fn as_ref(&self) -> &str {
match self {
Self::Microphone => "Microphone",
Self::Speakers => "Speakers",
Self::Camera => "Camera",
}
}
}
impl From<Device> for &'static str {
fn from(d: Device) -> Self {
match d {
Device::Microphone => "Microphone",
Device::Speakers => "Speakers",
Device::Camera => "Camera",
}
}
}
impl FromStr for Device {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"Microphone" | "microphone" => Ok(Device::Microphone),
"Speakers" | "speakers" => Ok(Device::Speakers),
"Camera" | "camera" => Ok(Device::Camera),
_ => Err(Error::ParseError("Failed to parse device, invalid value")),
}
}
}
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.Device")]
pub struct DeviceProxy<'a>(Proxy<'a>);
impl<'a> DeviceProxy<'a> {
pub async fn new() -> Result<DeviceProxy<'a>, Error> {
let proxy = Proxy::new_desktop("org.freedesktop.portal.Device").await?;
Ok(Self(proxy))
}
#[doc(alias = "AccessDevice")]
pub async fn access_device(&self, pid: u32, devices: &[Device]) -> Result<Request<()>, Error> {
let options = AccessDeviceOptions::default();
self.0
.empty_request(
&options.handle_token,
"AccessDevice",
&(pid, devices, &options),
)
.await
}
}
impl<'a> std::ops::Deref for DeviceProxy<'a> {
type Target = zbus::Proxy<'a>;
fn deref(&self) -> &Self::Target {
&self.0
}
}