use super::ScreenModule;
use anyhow::Context;
use serde_json::{json, Value};
use xcap::Monitor;
pub fn tools() -> Vec<Value> {
vec![json!({
"name": "screen_info",
"description": "Get display geometry (resolution, position, scale factor) for all monitors and current mouse cursor position. Call this first to understand the screen layout before taking actions.",
"inputSchema": {
"type": "object",
"properties": {},
"required": []
}
})]
}
pub async fn info(module: &ScreenModule) -> anyhow::Result<Value> {
let monitors = Monitor::all().context("Failed to enumerate monitors")?;
let displays: Vec<Value> = monitors
.iter()
.enumerate()
.map(|(i, m)| {
json!({
"id": i,
"name": m.name(),
"width": m.width(),
"height": m.height(),
"x": m.x(),
"y": m.y(),
"scale_factor": m.scale_factor(),
"is_primary": m.is_primary(),
})
})
.collect();
let cursor = get_cursor_position(module);
Ok(json!({
"session_type": format!("{:?}", module.session_type),
"displays": displays,
"cursor": cursor,
"available_tools": {
"xdotool": module.xdotool_path.is_some(),
"wmctrl": module.wmctrl_path.is_some(),
"ydotool": module.ydotool_path.is_some(),
"wlrctl": module.wlrctl_path.is_some(),
"scrot": module.scrot_path.is_some(),
"grim": module.grim_path.is_some(),
}
}))
}
fn get_cursor_position(module: &ScreenModule) -> Value {
if let Some(ref path) = module.xdotool_path {
if let Ok(output) = std::process::Command::new(path)
.arg("getmouselocation")
.output()
{
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
let mut x = 0i64;
let mut y = 0i64;
for part in stdout.split_whitespace() {
if let Some(val) = part.strip_prefix("x:") {
x = val.parse().unwrap_or(0);
} else if let Some(val) = part.strip_prefix("y:") {
y = val.parse().unwrap_or(0);
}
}
return json!({"x": x, "y": y});
}
}
}
if let Ok(mut enigo) = module.create_enigo() {
if let Ok((x, y)) = enigo::Mouse::location(&mut enigo) {
return json!({"x": x, "y": y});
}
}
json!({"x": null, "y": null, "error": "Could not determine cursor position"})
}