use std::env;
use crate::prelude::ClipboardProviderExt;
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum DisplayServer {
X11,
Wayland,
MacOs,
Windows,
Tty,
}
impl DisplayServer {
#[allow(unreachable_code)]
pub fn select() -> DisplayServer {
#[cfg(target_os = "macos")]
return DisplayServer::MacOs;
#[cfg(windows)]
return DisplayServer::Windows;
if is_wayland() {
DisplayServer::Wayland
} else if is_x11() {
DisplayServer::X11
} else if is_tty() {
DisplayServer::Tty
} else {
DisplayServer::X11
}
}
pub fn try_context(self) -> Option<Box<dyn ClipboardProviderExt>> {
match self {
DisplayServer::X11 => {
#[cfg(feature = "x11-fork")]
{
let context = crate::x11_fork::ClipboardContext::new();
if let Ok(context) = context {
return Some(Box::new(context));
}
}
#[cfg(feature = "x11-bin")]
{
let context = crate::x11_bin::ClipboardContext::new();
if let Ok(context) = context {
return Some(Box::new(context));
}
}
#[cfg(all(
unix,
not(any(
target_os = "macos",
target_os = "android",
target_os = "ios",
target_os = "emscripten"
))
))]
{
let context = copypasta::x11_clipboard::X11ClipboardContext::new();
if let Ok(context) = context {
return Some(Box::new(context));
}
}
None
}
DisplayServer::Wayland => {
#[cfg(feature = "wayland-bin")]
{
let context = crate::wayland_bin::ClipboardContext::new();
if let Ok(context) = context {
return Some(Box::new(context));
}
}
copypasta::ClipboardContext::new()
.ok()
.map(|c| -> Box<dyn ClipboardProviderExt> { Box::new(c) })
}
DisplayServer::MacOs | DisplayServer::Windows => copypasta::ClipboardContext::new()
.ok()
.map(|c| -> Box<dyn ClipboardProviderExt> { Box::new(c) }),
DisplayServer::Tty => {
#[cfg(feature = "osc52")]
{
let context = crate::osc52::ClipboardContext::new();
if let Ok(context) = context {
return Some(Box::new(context));
}
}
None
}
}
}
}
pub fn is_x11() -> bool {
if !cfg!(all(unix, not(all(target_os = "macos", target_os = "ios")))) {
return false;
}
match env::var("XDG_SESSION_TYPE").ok().as_deref() {
Some("x11") => true,
Some("wayland") => false,
_ => has_non_empty_env("DISPLAY"),
}
}
pub fn is_wayland() -> bool {
if !cfg!(all(unix, not(all(target_os = "macos", target_os = "ios")))) {
return false;
}
match env::var("XDG_SESSION_TYPE").ok().as_deref() {
Some("wayland") => true,
Some("x11") => false,
_ => has_non_empty_env("WAYLAND_DISPLAY"),
}
}
pub fn is_tty() -> bool {
env::var("XDG_SESSION_TYPE").as_deref() == Ok("tty")
}
#[inline]
fn has_non_empty_env(env: &str) -> bool {
env::var_os(env).map(|v| !v.is_empty()).unwrap_or(false)
}