#[cfg(not(any(windows, target_os = "macos")))]
use crate::error::RdesktopError;
use crate::error::Result;
use crate::hotkeys::Key;
use crate::input::MouseButton;
pub struct InputSimulator {
inner: PlatformSim,
}
enum PlatformSim {
#[cfg(windows)]
Windows(crate::simulate_win::WinSim),
#[cfg(target_os = "macos")]
Macos(crate::simulate_mac::MacSim),
#[cfg(not(any(windows, target_os = "macos")))]
Unsupported,
}
impl InputSimulator {
pub fn new() -> Result<Self> {
Ok(Self {
inner: PlatformSim::new()?,
})
}
pub fn press_key(&self, key: Key) -> Result<()> {
self.inner.press_key(key)
}
pub fn release_key(&self, key: Key) -> Result<()> {
self.inner.release_key(key)
}
pub fn tap_key(&self, key: Key) -> Result<()> {
self.press_key(key)?;
self.release_key(key)
}
pub fn tap_combo(&self, keys: &[Key]) -> Result<()> {
self.inner.tap_combo(keys)
}
pub fn type_text(&self, text: &str) -> Result<()> {
self.inner.type_text(text)
}
pub fn move_mouse(&self, x: i32, y: i32, absolute: bool) -> Result<()> {
self.inner.move_mouse(x, y, absolute)
}
pub fn press_mouse(&self, button: MouseButton) -> Result<()> {
self.inner.press_mouse(button)
}
pub fn release_mouse(&self, button: MouseButton) -> Result<()> {
self.inner.release_mouse(button)
}
pub fn click(&self, button: MouseButton) -> Result<()> {
self.press_mouse(button)?;
self.release_mouse(button)
}
pub fn scroll(&self, delta: i32) -> Result<()> {
self.inner.scroll(delta)
}
}
impl PlatformSim {
fn new() -> Result<Self> {
#[cfg(windows)]
{
Ok(PlatformSim::Windows(crate::simulate_win::WinSim::new()?))
}
#[cfg(target_os = "macos")]
{
Ok(PlatformSim::Macos(crate::simulate_mac::MacSim::new()?))
}
#[cfg(not(any(windows, target_os = "macos")))]
{
Err(RdesktopError::UnsupportedPlatform(
"system-wide input simulation is only supported on Windows and macOS".into(),
))
}
}
fn press_key(&self, key: Key) -> Result<()> {
match self {
#[cfg(windows)]
PlatformSim::Windows(w) => w.press_key(key),
#[cfg(target_os = "macos")]
PlatformSim::Macos(m) => m.press_key(key),
#[cfg(not(any(windows, target_os = "macos")))]
PlatformSim::Unsupported => Err(RdesktopError::UnsupportedPlatform(
"system-wide input simulation is only supported on Windows and macOS".into(),
)),
}
}
fn release_key(&self, key: Key) -> Result<()> {
match self {
#[cfg(windows)]
PlatformSim::Windows(w) => w.release_key(key),
#[cfg(target_os = "macos")]
PlatformSim::Macos(m) => m.release_key(key),
#[cfg(not(any(windows, target_os = "macos")))]
PlatformSim::Unsupported => Err(RdesktopError::UnsupportedPlatform(
"system-wide input simulation is only supported on Windows and macOS".into(),
)),
}
}
fn tap_combo(&self, keys: &[Key]) -> Result<()> {
match self {
#[cfg(windows)]
PlatformSim::Windows(w) => w.tap_combo(keys),
#[cfg(target_os = "macos")]
PlatformSim::Macos(m) => m.tap_combo(keys),
#[cfg(not(any(windows, target_os = "macos")))]
PlatformSim::Unsupported => Err(RdesktopError::UnsupportedPlatform(
"system-wide input simulation is only supported on Windows and macOS".into(),
)),
}
}
fn type_text(&self, text: &str) -> Result<()> {
match self {
#[cfg(windows)]
PlatformSim::Windows(w) => w.type_text(text),
#[cfg(target_os = "macos")]
PlatformSim::Macos(m) => m.type_text(text),
#[cfg(not(any(windows, target_os = "macos")))]
PlatformSim::Unsupported => Err(RdesktopError::UnsupportedPlatform(
"system-wide input simulation is only supported on Windows and macOS".into(),
)),
}
}
fn move_mouse(&self, x: i32, y: i32, absolute: bool) -> Result<()> {
match self {
#[cfg(windows)]
PlatformSim::Windows(w) => w.move_mouse(x, y, absolute),
#[cfg(target_os = "macos")]
PlatformSim::Macos(m) => m.move_mouse(x, y, absolute),
#[cfg(not(any(windows, target_os = "macos")))]
PlatformSim::Unsupported => Err(RdesktopError::UnsupportedPlatform(
"system-wide input simulation is only supported on Windows and macOS".into(),
)),
}
}
fn press_mouse(&self, button: MouseButton) -> Result<()> {
match self {
#[cfg(windows)]
PlatformSim::Windows(w) => w.press_mouse(button),
#[cfg(target_os = "macos")]
PlatformSim::Macos(m) => m.press_mouse(button),
#[cfg(not(any(windows, target_os = "macos")))]
PlatformSim::Unsupported => Err(RdesktopError::UnsupportedPlatform(
"system-wide input simulation is only supported on Windows and macOS".into(),
)),
}
}
fn release_mouse(&self, button: MouseButton) -> Result<()> {
match self {
#[cfg(windows)]
PlatformSim::Windows(w) => w.release_mouse(button),
#[cfg(target_os = "macos")]
PlatformSim::Macos(m) => m.release_mouse(button),
#[cfg(not(any(windows, target_os = "macos")))]
PlatformSim::Unsupported => Err(RdesktopError::UnsupportedPlatform(
"system-wide input simulation is only supported on Windows and macOS".into(),
)),
}
}
fn scroll(&self, delta: i32) -> Result<()> {
match self {
#[cfg(windows)]
PlatformSim::Windows(w) => w.scroll(delta),
#[cfg(target_os = "macos")]
PlatformSim::Macos(m) => m.scroll(delta),
#[cfg(not(any(windows, target_os = "macos")))]
PlatformSim::Unsupported => Err(RdesktopError::UnsupportedPlatform(
"system-wide input simulation is only supported on Windows and macOS".into(),
)),
}
}
}