use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use auv_driver_common::{Driver, DriverDescriptor, DriverResult, DriverSession};
use crate::descriptor::{LinuxDriverDescriptor, linux_driver_descriptor};
use crate::native::portal::{ClipboardSession, RestoreTokenStore, ScreenCastSession};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum InputBackend {
#[default]
Portal,
Uinput,
}
#[derive(Clone, Debug, Default)]
pub struct LinuxDriver {
input_backend: InputBackend,
portal_state_root: Option<PathBuf>,
#[cfg(target_os = "linux")]
portal_app_id: Option<ashpd::AppID>,
#[cfg(not(target_os = "linux"))]
portal_app_id: Option<String>,
}
impl LinuxDriver {
pub fn new() -> Self {
Self::default()
}
pub fn with_input_backend(mut self, backend: InputBackend) -> Self {
self.input_backend = backend;
self
}
pub fn with_portal_state_root(mut self, root: PathBuf) -> Self {
self.portal_state_root = Some(root);
self
}
pub fn with_portal_app_id(mut self, app_id: String) -> auv_driver_common::DriverResult<Self> {
#[cfg(target_os = "linux")]
let app_id = app_id.parse::<ashpd::AppID>().map_err(|error| crate::error::invalid_input(error.to_string()))?;
self.portal_app_id = Some(app_id);
Ok(self)
}
pub fn linux_descriptor(&self) -> LinuxDriverDescriptor {
linux_driver_descriptor()
}
}
#[derive(Clone, Debug)]
pub struct LinuxDriverSession {
pub(crate) state: Arc<Mutex<LinuxDriverSessionState>>,
}
#[derive(Debug, Default)]
pub(crate) struct LinuxDriverSessionState {
pub(crate) clipboard_session: Option<ClipboardSession>,
pub(crate) input_session: Option<Arc<Mutex<crate::input::InputSession>>>,
pub(crate) input_backend: InputBackend,
pub(crate) screencast_session: Option<ScreenCastSession>,
pub(crate) restore_tokens: Option<RestoreTokenStore>,
#[cfg(target_os = "linux")]
pub(crate) portal_app_id: Option<ashpd::AppID>,
#[cfg(not(target_os = "linux"))]
pub(crate) portal_app_id: Option<String>,
}
impl LinuxDriverSession {
pub fn linux_descriptor(&self) -> LinuxDriverDescriptor {
linux_driver_descriptor()
}
}
impl Driver for LinuxDriver {
type Session = LinuxDriverSession;
fn descriptor(&self) -> DriverDescriptor {
self.linux_descriptor().as_driver_descriptor()
}
fn open_local(&self) -> DriverResult<Self::Session> {
Ok(LinuxDriverSession {
state: Arc::new(Mutex::new(LinuxDriverSessionState {
input_backend: self.input_backend,
restore_tokens: self.portal_state_root.clone().map(RestoreTokenStore::new),
portal_app_id: self.portal_app_id.clone(),
..Default::default()
})),
})
}
}
impl DriverSession for LinuxDriverSession {
fn descriptor(&self) -> DriverDescriptor {
self.linux_descriptor().as_driver_descriptor()
}
}
#[cfg(test)]
#[path = "driver_test.rs"]
mod tests;