use crate::{
DriverCallback, DriverCategory, DriverContext, DriverError, DriverResult, exec_async,
types::{Driver, DriverParameter},
};
use serde_json::{Value, json};
use std::collections::HashMap;
use tracing::{debug, info};
#[derive(Debug)]
pub struct OsLockDriver;
#[async_trait::async_trait]
impl Driver for OsLockDriver {
fn name(&self) -> &str {
"os_lock"
}
fn description(&self) -> &str {
"Lock the system screen"
}
fn usage_hint(&self) -> &str {
"Use this skill when you need to secure the system without logging out"
}
fn parameters(&self) -> Vec<DriverParameter> {
return vec![];
}
fn example_call(&self) -> DriverResult<Value> {
return Ok(json!({
"action": "os_lock"
}));
}
fn example_output(&self) -> String {
return "Screen locked".to_string();
}
fn category(&self) -> DriverCategory {
return DriverCategory::OperatingSystemBasis;
}
async fn execute(
&self,
_parameters: &HashMap<String, Value>,
_callback: Option<&dyn DriverCallback>,
_context: Option<&DriverContext>,
) -> DriverResult<String> {
debug!("Executing os_lock driver");
#[cfg(target_os = "windows")]
{
debug!("Locking screen on Windows");
exec_async("rundll32.exe", &["user32.dll,LockWorkStation"], None)
.await
.map_err(|e| DriverError::execution(format!("Failed to lock screen on Windows: {}", e)))?;
info!("Screen locked on Windows");
}
#[cfg(target_os = "macos")]
{
debug!("Locking screen on macOS");
let _ =
exec_async("osascript", &["-e", "tell application \"System Events\" to keystroke \"q\" using {command down, control down}"], None)
.await;
info!("Screen locked on macOS");
}
#[cfg(target_os = "linux")]
{
debug!("Locking screen on Linux");
let _ = exec_async("gnome-screensaver-command", &["-l"], None).await;
let _ = exec_async("xdg-screensaver", &["lock"], None).await;
info!("Screen locked on Linux");
}
return Ok("Screen locked".to_string());
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_os_lock_metadata() {
let driver = OsLockDriver;
assert_eq!(driver.name(), "os_lock");
assert_eq!(driver.category(), DriverCategory::OperatingSystemBasis);
}
}