use crate::features::config::{device_store::DeviceStore, state::DeviceState};
use crate::features::input::scripts::ScriptManager;
use crate::tools::{
macros::print_info,
session::DeviceSession,
types::DeviceIdentifier,
};
use anyhow::{anyhow, Context, Result};
pub enum SwipeMode {
Coords { x1: u16, y1: u16, x2: u16, y2: u16 },
Direction(SwipeDirection),
}
#[derive(Debug)]
pub enum SwipeDirection {
Left,
Right,
Up,
Down,
}
impl SwipeDirection {
pub fn to_script_arg(&self) -> &'static str {
match self {
SwipeDirection::Left => "rl", SwipeDirection::Right => "lr", SwipeDirection::Up => "du", SwipeDirection::Down => "ud", }
}
}
pub async fn execute(mode: SwipeMode) -> Result<()> {
if let SwipeMode::Coords { x1, y1, x2, y2 } = &mode {
for coord in [x1, y1, x2, y2] {
if *coord > 4096 {
return Err(anyhow!("Coordinate out of range: {}. Max: 4096", coord));
}
}
}
let current_host = DeviceState::get_current()?;
let device_id = DeviceIdentifier::Host(current_host);
let device = DeviceStore::find(&device_id)?;
match &mode {
SwipeMode::Coords { x1, y1, x2, y2 } => {
print_info(format!("Swiping from ({},{}) to ({},{}) on device {}",
x1, y1, x2, y2, device.display_name()));
}
SwipeMode::Direction(dir) => {
print_info(format!("Swiping {:?} on device {}", dir, device.display_name()));
}
}
print_info(format!("Connecting to {}:{}...", device.host, device.port));
let mut session = DeviceSession::connect(&device)
.context("Failed to connect to device")?;
ScriptManager::ensure_swipe_script_with_session(&mut session)?;
let script_path = ScriptManager::swipe_script_path();
let swipe_command = match mode {
SwipeMode::Coords { x1, y1, x2, y2 } => {
format!("python3 {} {} {} {} {}", script_path, x1, y1, x2, y2)
}
SwipeMode::Direction(dir) => {
format!("python3 {} {}", script_path, dir.to_script_arg())
}
};
print_info("Executing swipe with devel-su...");
let output = session.exec_as_root(&swipe_command)
.context("Swipe requires root access. Set root password using: audb device add")?;
for line in &output {
if !line.is_empty() {
println!("{}", line);
}
}
print_info("Swipe completed successfully");
Ok(())
}