poly_mcp/modules/screen/
mod.rs1mod capture;
2mod elements;
3mod extras;
4mod info;
5mod input;
6mod ocr;
7mod windows;
8
9use serde_json::Value;
10use std::path::PathBuf;
11use std::sync::atomic::{AtomicU64, Ordering};
12
13static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);
14
15#[derive(Debug, Clone, Copy, PartialEq)]
17pub enum SessionType {
18 X11,
19 Wayland,
20 Unknown,
21}
22
23pub struct ScreenModule {
25 pub(crate) session_type: SessionType,
26 pub(crate) xdotool_path: Option<PathBuf>,
27 pub(crate) wmctrl_path: Option<PathBuf>,
28 pub(crate) ydotool_path: Option<PathBuf>,
29 pub(crate) wlrctl_path: Option<PathBuf>,
30 pub(crate) scrot_path: Option<PathBuf>,
31 pub(crate) grim_path: Option<PathBuf>,
32}
33
34impl ScreenModule {
35 pub fn new() -> Self {
36 let session_type = detect_session_type();
37
38 Self {
39 session_type,
40 xdotool_path: which("xdotool"),
41 wmctrl_path: which("wmctrl"),
42 ydotool_path: which("ydotool"),
43 wlrctl_path: which("wlrctl"),
44 scrot_path: which("scrot"),
45 grim_path: which("grim"),
46 }
47 }
48
49 pub(crate) fn create_enigo(&self) -> anyhow::Result<enigo::Enigo> {
52 let settings = enigo::Settings::default();
53 enigo::Enigo::new(&settings)
54 .map_err(|e| anyhow::anyhow!("Failed to initialize input simulation: {}. Ensure a display server (X11/Wayland) is running.", e))
55 }
56
57 pub fn get_tools(&self) -> Vec<Value> {
58 let mut tools = Vec::new();
59 tools.extend(capture::tools());
60 tools.extend(input::tools());
61 tools.extend(info::tools());
62 tools.extend(windows::tools());
63 tools.extend(ocr::tools());
64 tools.extend(elements::tools());
65 tools.extend(extras::tools());
66 tools
67 }
68
69 pub async fn capture(&self, args: Value) -> anyhow::Result<Value> {
72 capture::capture(self, args).await
73 }
74
75 pub async fn click(&self, args: Value) -> anyhow::Result<Value> {
76 input::click(self, args).await
77 }
78
79 pub async fn type_text(&self, args: Value) -> anyhow::Result<Value> {
80 input::type_text(self, args).await
81 }
82
83 pub async fn key(&self, args: Value) -> anyhow::Result<Value> {
84 input::key(self, args).await
85 }
86
87 pub async fn move_mouse(&self, args: Value) -> anyhow::Result<Value> {
88 input::move_mouse(self, args).await
89 }
90
91 pub async fn scroll(&self, args: Value) -> anyhow::Result<Value> {
92 input::scroll(self, args).await
93 }
94
95 pub async fn drag(&self, args: Value) -> anyhow::Result<Value> {
96 input::drag(self, args).await
97 }
98
99 pub async fn info(&self, _args: Value) -> anyhow::Result<Value> {
100 info::info(self).await
101 }
102
103 pub async fn windows(&self, args: Value) -> anyhow::Result<Value> {
104 windows::list_windows(self, args).await
105 }
106
107 pub async fn focus(&self, args: Value) -> anyhow::Result<Value> {
108 windows::focus_window(self, args).await
109 }
110
111 pub async fn wait(&self, args: Value) -> anyhow::Result<Value> {
112 windows::wait(self, args).await
113 }
114
115 pub async fn ocr(&self, args: Value) -> anyhow::Result<Value> {
118 ocr::ocr(self, args).await
119 }
120
121 pub async fn find_text(&self, args: Value) -> anyhow::Result<Value> {
122 ocr::find(self, args).await
123 }
124
125 pub async fn elements(&self, args: Value) -> anyhow::Result<Value> {
126 elements::elements(args).await
127 }
128
129 pub async fn clipboard(&self, args: Value) -> anyhow::Result<Value> {
130 extras::clipboard(self, args).await
131 }
132
133 pub async fn pixel(&self, args: Value) -> anyhow::Result<Value> {
134 extras::pixel(self, args).await
135 }
136
137 pub async fn diff(&self, args: Value) -> anyhow::Result<Value> {
138 extras::diff(self, args).await
139 }
140
141 pub async fn process(&self, args: Value) -> anyhow::Result<Value> {
142 extras::process(args).await
143 }
144}
145
146fn detect_session_type() -> SessionType {
149 if let Ok(session) = std::env::var("XDG_SESSION_TYPE") {
150 match session.to_lowercase().as_str() {
151 "x11" => return SessionType::X11,
152 "wayland" => return SessionType::Wayland,
153 _ => {}
154 }
155 }
156 if std::env::var("WAYLAND_DISPLAY").is_ok() {
157 return SessionType::Wayland;
158 }
159 if std::env::var("DISPLAY").is_ok() {
160 return SessionType::X11;
161 }
162 SessionType::Unknown
163}
164
165pub(crate) fn shm_temp_path(prefix: &str, ext: &str) -> String {
168 let base = if std::path::Path::new("/dev/shm").exists() {
169 "/dev/shm"
170 } else {
171 "/tmp"
172 };
173 let id = TEMP_COUNTER.fetch_add(1, Ordering::Relaxed);
174 format!("{}/poly_mcp_{}_{}_{}.{}", base, prefix, std::process::id(), id, ext)
175}
176
177fn which(name: &str) -> Option<PathBuf> {
178 std::process::Command::new("which")
179 .arg(name)
180 .output()
181 .ok()
182 .filter(|o| o.status.success())
183 .map(|o| PathBuf::from(String::from_utf8_lossy(&o.stdout).trim()))
184}