use super::{PlatformHandler, Error, WindowInfo};
use anyhow::{Result, Context};
#[cfg(target_os = "linux")]
use crate::platform::linux::{x11, wayland};
#[derive(Debug)]
pub struct PlatformDetector;
impl PlatformDetector {
pub fn detect() -> Result<Box<dyn PlatformHandler>> {
#[cfg(target_os = "linux")]
{
let session_type = std::env::var("XDG_SESSION_TYPE")
.unwrap_or_else(|_| "x11".into())
.to_lowercase();
if session_type.contains("wayland") {
match wayland::LinuxWaylandHandler::new() {
Ok(handler) => return Ok(handler),
Err(e) => tracing::warn!("Wayland detection failed: {}", e),
}
}
match x11::LinuxX11Handler::new() {
Ok(handler) => Ok(handler),
Err(e) => {
tracing::error!("X11 detection failed: {}", e);
Err(Error::PlatformError(
"No supported display server found".into()
).into())
}
}
}
#[cfg(target_os = "windows")]
Err(Error::PlatformError("Windows support not implemented".into()).into())
#[cfg(target_os = "macos")]
Err(Error::PlatformError("macOS support not implemented".into()).into())
#[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
Err(Error::UnsupportedPlatform.into())
}
}
pub fn get_platform_handler() -> Result<Box<dyn PlatformHandler>> {
PlatformDetector::detect().map_err(|e| {
tracing::error!("Platform detection failed: {}", e);
e
})
}