#[cfg(not(any(feature = "wayland", feature = "x11")))]
compile_error!("必须启用 'wayland' 或 'x11' 特性之一。");
use std::sync::mpsc;
#[cfg(any(feature = "wayland", feature = "x11"))]
use std::env;
use anyhow::{Result, anyhow};
#[cfg(feature = "x11")]
use x11::X11Capturer;
use crate::{Target, capturer::Options, frame::Frame};
mod error;
#[cfg(feature = "wayland")]
mod wayland;
#[cfg(feature = "x11")]
mod x11;
#[cfg(feature = "wayland")]
use wayland::WaylandCapturer;
pub trait LinuxCapturerImpl {
fn start_capture(&mut self);
fn stop_capture(&mut self);
fn target(&self) -> Option<&Target> {
None
}
}
pub struct LinuxCapturer {
pub imp: Box<dyn LinuxCapturerImpl>, }
type Type = mpsc::Sender<Result<Frame>>;
impl LinuxCapturer {
#[allow(unused_variables)]
pub fn new(options: &Options, tx: Type) -> Result<Self> {
#[cfg(feature = "wayland")]
if env::var("WAYLAND_DISPLAY").is_ok() {
log::debug!("创建新的 Wayland 屏幕捕获器");
return Ok(Self {
imp: Box::new(WaylandCapturer::new(options, tx)?),
});
}
#[cfg(feature = "x11")]
if env::var("DISPLAY").is_ok() {
log::debug!("创建新的 X11 屏幕捕获器");
return Ok(Self {
imp: Box::new(X11Capturer::new(options, tx)?),
});
}
#[cfg(all(feature = "wayland", feature = "x11"))]
let error_msg = "不支持的平台:无法检测到 Wayland 或 X11 显示器";
#[cfg(all(not(feature = "wayland"), feature = "x11"))]
let error_msg =
"不支持的平台:无法检测到 X11 显示器。请启用 'wayland' 特性以支持 Wayland。";
#[cfg(all(feature = "wayland", not(feature = "x11")))]
let error_msg = "不支持的平台:无法检测到 Wayland 显示器。请启用 'x11' 特性以支持 X11。";
#[cfg(not(any(feature = "wayland", feature = "x11")))]
let error_msg = "必须启用 'wayland' 或 'x11' 特性之一。";
Err(anyhow!(error_msg))
}
}
pub fn create_capturer(
options: &Options,
tx: mpsc::Sender<Result<Frame>>,
) -> Result<LinuxCapturer> {
LinuxCapturer::new(options, tx)
}