pixelcoords-core 0.4.0

Platform-free core of pixelcoords: screen geometry, HiDPI and multi-monitor coordinate spaces, the session.json schema, template relocation, point verdicts, click-point resolution, region diffing, and click-code emitters. Cross-platform (macOS, Windows, Linux), no unsafe
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! Ready-to-paste click snippets from a session — the logic behind
//! `pixelcoords emit`.
//!
//! Each emitter encodes one automation tool's coordinate convention in
//! exactly one place, because that conversion is where hand-written glue
//! gets silently burned: pyautogui speaks logical points on macOS but
//! physical pixels on Windows and X11; cliclick speaks logical points;
//! xdotool speaks physical pixels. Coordinates are the session's own
//! `global_px`, divided by the selection's monitor scale only where the
//! target tool wants logical points. Sessions are machine-local, so a
//! snippet is meant to run on the machine and monitor layout that was
//! captured.

use std::fmt::Write as _;

use thiserror::Error;

use crate::geometry::Point;
use crate::session::{SelectionRecord, SessionFile};
use crate::space::{Resolved, logical_of};

/// The OS the snippet will run on. Only pyautogui branches on it — the
/// other tools each exist on a single platform.
///
/// Re-exported from [`crate::space`], where it now lives: "which OS is
/// this coordinate for" is the same question `--units auto` asks, and one
/// answer serves both.
pub use crate::space::Platform;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EmitFormat {
    Pyautogui,
    Cliclick,
    Xdotool,
}

#[derive(Debug, Error, PartialEq, Eq)]
pub enum EmitError {
    #[error("the session has no selections to emit")]
    NoSelections,
    #[error("no selection is labeled {requested:?}; labels in this session: {available:?}")]
    UnknownLabel {
        requested: String,
        available: Vec<String>,
    },
    #[error(
        "selection {selection} references monitor {monitor}, which the \
         session does not describe"
    )]
    UnknownMonitor { selection: usize, monitor: usize },
}

/// One click the snippet will perform.
struct Target {
    comment: String,
    point: Point,
}

/// Render the session's selections as a ready-to-paste snippet for
/// `format`, ending with a newline.
pub fn emit(
    session: &SessionFile,
    format: EmitFormat,
    platform: Platform,
    label: Option<&str>,
) -> Result<String, EmitError> {
    match format {
        EmitFormat::Pyautogui => pyautogui(session, platform, label),
        EmitFormat::Cliclick => cliclick(session, label),
        EmitFormat::Xdotool => xdotool(session, label),
    }
}

fn pyautogui(
    session: &SessionFile,
    platform: Platform,
    label: Option<&str>,
) -> Result<String, EmitError> {
    let (units, space_note) = match platform {
        Platform::MacOs => (Resolved::Logical, "logical points (macOS)"),
        // pyautogui makes its process DPI-aware on import, so it addresses
        // true physical pixels on Windows.
        Platform::Windows => (Resolved::Physical, "physical pixels (Windows)"),
        Platform::Linux => (Resolved::Physical, "physical pixels (X11)"),
    };
    let targets = click_targets(session, units, label)?;
    let mut out = header("#", session, space_note);
    out.push_str("import pyautogui\n");
    for t in targets {
        // Writing to a String cannot fail.
        let _ = write!(
            out,
            "\n# {}\npyautogui.click({}, {})\n",
            t.comment, t.point.x, t.point.y
        );
    }
    Ok(out)
}

fn cliclick(session: &SessionFile, label: Option<&str>) -> Result<String, EmitError> {
    let targets = click_targets(session, Resolved::Logical, label)?;
    let mut out = header("#", session, "logical points (macOS)");
    for t in targets {
        let _ = writeln!(
            out,
            "cliclick c:{},{}  # {}",
            cliclick_coord(t.point.x),
            cliclick_coord(t.point.y),
            t.comment
        );
    }
    Ok(out)
}

/// cliclick parses a bare leading `-` as an option; its documented escape
/// for negative coordinates is an `=` prefix.
fn cliclick_coord(v: i32) -> String {
    if v < 0 {
        return format!("={v}");
    }
    v.to_string()
}

fn xdotool(session: &SessionFile, label: Option<&str>) -> Result<String, EmitError> {
    let targets = click_targets(session, Resolved::Physical, label)?;
    let mut out = header("#", session, "physical pixels (X11)");
    for t in targets {
        let _ = writeln!(
            out,
            "xdotool mousemove {} {} click 1  # {}",
            t.point.x, t.point.y, t.comment
        );
    }
    Ok(out)
}

fn header(prefix: &str, session: &SessionFile, space_note: &str) -> String {
    format!(
        "{prefix} generated by pixelcoords from a session captured {}\n\
         {prefix} coordinates: {space_note} — run on the machine and \
         monitor layout that was captured\n",
        session.created_utc
    )
}

/// Every selection's click point in global coordinates, converted to the
/// requested units via its own monitor's scale — mixed-DPI setups scale
/// each selection independently.
fn click_targets(
    session: &SessionFile,
    units: Resolved,
    label: Option<&str>,
) -> Result<Vec<Target>, EmitError> {
    if session.selections.is_empty() {
        return Err(EmitError::NoSelections);
    }
    let wanted = crate::session::select_by_label(session, label);
    if wanted.is_empty() {
        // Only a label filter can empty a non-empty session.
        return Err(EmitError::UnknownLabel {
            requested: label.unwrap_or_default().to_string(),
            available: crate::session::distinct_labels(session.selections.iter()),
        });
    }
    wanted
        .into_iter()
        .map(|(index, record)| {
            // The click point of the stored global shape. Rect rotation
            // pivots on the bbox center — the click point itself — so
            // `rot_deg` cannot move it; triangles store rotation baked.
            let physical = record.global_px.click_point();
            let point = match units {
                Resolved::Physical => physical,
                Resolved::Logical => to_logical(session, index, record, physical)?,
            };
            Ok(Target {
                comment: describe(index, record),
                point,
            })
        })
        .collect()
}

/// `global_px` through the selection's own monitor scale. The lookup and
/// its error stay here; the arithmetic is `space::logical_of`, shared
/// with every other command that has to answer the same question.
fn to_logical(
    session: &SessionFile,
    index: usize,
    record: &SelectionRecord,
    physical: Point,
) -> Result<Point, EmitError> {
    let monitor = session
        .monitors
        .iter()
        .find(|m| m.index == record.monitor)
        .ok_or(EmitError::UnknownMonitor {
            selection: index,
            monitor: record.monitor,
        })?;
    Ok(logical_of(physical, monitor.scale))
}

fn describe(index: usize, record: &SelectionRecord) -> String {
    let shape = match record.shape {
        crate::geometry::ToolKind::Rect => "rect",
        crate::geometry::ToolKind::Circle => "circle",
        crate::geometry::ToolKind::Ellipse => "ellipse",
        crate::geometry::ToolKind::Polygon
        | crate::geometry::ToolKind::Freehand
        | crate::geometry::ToolKind::Poly => "poly",
        crate::geometry::ToolKind::Triangle => "triangle",
    };
    if record.label.is_empty() {
        return format!("selection {index}{shape} on monitor {}", record.monitor);
    }
    format!("{}{shape} on monitor {}", record.label, record.monitor)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::geometry::{Rect, Shape, Size};
    use crate::selection::Selection;
    use crate::session::MonitorRecord;

    fn monitor(index: usize, ox: i32, oy: i32, scale: f64) -> MonitorRecord {
        MonitorRecord {
            index,
            name: format!("Display {index}"),
            primary: index == 0,
            origin_px: Point::new(ox, oy),
            size_px: Size::new(1920, 1080),
            scale,
        }
    }

    fn labeled(shape: Shape, monitor: usize, label: &str) -> Selection {
        let mut sel = Selection::new(shape, monitor);
        sel.label = label.into();
        sel
    }

    fn session(monitors: Vec<MonitorRecord>, selections: &[Selection]) -> SessionFile {
        let crops: Vec<String> = (0..selections.len()).map(|i| format!("c{i}.png")).collect();
        SessionFile::build(
            "test",
            "2026-07-27T11:35:42Z".into(),
            monitors,
            selections,
            &crops,
            None,
        )
    }

    #[test]
    fn pyautogui_on_macos_emits_logical_points() {
        // Rect center at physical (100, 60) on a 2x monitor -> (50, 30).
        let file = session(
            vec![monitor(0, 0, 0, 2.0)],
            &[labeled(Shape::Rect(Rect::new(80, 40, 40, 40)), 0, "submit")],
        );
        let out = emit(&file, EmitFormat::Pyautogui, Platform::MacOs, None).unwrap();
        assert_eq!(
            out,
            "# generated by pixelcoords from a session captured 2026-07-27T11:35:42Z\n\
             # coordinates: logical points (macOS) — run on the machine and \
             monitor layout that was captured\n\
             import pyautogui\n\
             \n\
             # submit — rect on monitor 0\n\
             pyautogui.click(50, 30)\n"
        );
    }

    #[test]
    fn pyautogui_elsewhere_emits_physical_pixels() {
        let file = session(
            vec![monitor(0, 0, 0, 2.0)],
            &[labeled(Shape::Rect(Rect::new(80, 40, 40, 40)), 0, "submit")],
        );
        for (platform, note) in [
            (Platform::Windows, "physical pixels (Windows)"),
            (Platform::Linux, "physical pixels (X11)"),
        ] {
            let out = emit(&file, EmitFormat::Pyautogui, platform, None).unwrap();
            assert!(out.contains("pyautogui.click(100, 60)"), "got: {out}");
            assert!(out.contains(note), "got: {out}");
        }
    }

    #[test]
    fn cliclick_escapes_negative_logical_coordinates() {
        // A monitor left of the primary: global physical (-1800, 40) at
        // scale 2 -> logical (-900, 20), with cliclick's `=` escape.
        let file = session(
            vec![monitor(0, -3840, 0, 2.0)],
            &[labeled(Shape::Rect(Rect::new(2020, 20, 40, 40)), 0, "back")],
        );
        let out = emit(&file, EmitFormat::Cliclick, Platform::MacOs, None).unwrap();
        assert!(
            out.contains("cliclick c:=-900,20  # back — rect on monitor 0"),
            "got: {out}"
        );
    }

    #[test]
    fn xdotool_emits_physical_pixels_untouched() {
        let file = session(
            vec![monitor(0, 0, 0, 2.0)],
            &[labeled(
                Shape::Circle {
                    cx: 500,
                    cy: 300,
                    r: 25,
                },
                0,
                "dot",
            )],
        );
        let out = emit(&file, EmitFormat::Xdotool, Platform::Linux, None).unwrap();
        assert!(
            out.contains("xdotool mousemove 500 300 click 1  # dot — circle on monitor 0"),
            "got: {out}"
        );
    }

    #[test]
    fn mixed_dpi_scales_each_selection_by_its_own_monitor() {
        let file = session(
            vec![monitor(0, 0, 0, 1.0), monitor(1, 1920, 0, 2.0)],
            &[
                labeled(Shape::Rect(Rect::new(100, 100, 20, 20)), 0, "left"),
                labeled(Shape::Rect(Rect::new(100, 100, 20, 20)), 1, "right"),
            ],
        );
        let out = emit(&file, EmitFormat::Pyautogui, Platform::MacOs, None).unwrap();
        // Monitor 0 at scale 1: physical (110, 110) stays put. Monitor 1 at
        // scale 2: global physical (2030, 110) -> logical (1015, 55).
        assert!(out.contains("pyautogui.click(110, 110)"), "got: {out}");
        assert!(out.contains("pyautogui.click(1015, 55)"), "got: {out}");
    }

    #[test]
    fn triangles_click_their_centroid_and_unlabeled_selections_get_names() {
        let file = session(
            vec![monitor(0, 0, 0, 1.0)],
            &[labeled(
                Shape::Triangle {
                    ax: 30,
                    ay: 0,
                    bx: 0,
                    by: 60,
                    cx: 60,
                    cy: 60,
                },
                0,
                "",
            )],
        );
        let out = emit(&file, EmitFormat::Xdotool, Platform::Linux, None).unwrap();
        assert!(
            out.contains("xdotool mousemove 30 40 click 1  # selection 0 — triangle on monitor 0"),
            "got: {out}"
        );
    }

    #[test]
    fn a_rotated_rect_clicks_its_pivot() {
        let mut sel = Selection::new(Shape::Rect(Rect::new(10, 10, 40, 10)), 0);
        sel.rot_deg = 90;
        let file = session(vec![monitor(0, 0, 0, 1.0)], &[sel]);
        let out = emit(&file, EmitFormat::Xdotool, Platform::Linux, None).unwrap();
        // The pivot (30, 15) is rotation-invariant, so the click lands
        // inside the silhouette at any angle.
        assert!(
            out.contains("xdotool mousemove 30 15 click 1"),
            "got: {out}"
        );
    }

    #[test]
    fn a_label_filter_emits_only_matching_selections() {
        let file = session(
            vec![monitor(0, 0, 0, 1.0)],
            &[
                labeled(Shape::Rect(Rect::new(0, 0, 10, 10)), 0, "Cancel"),
                labeled(Shape::Rect(Rect::new(100, 100, 10, 10)), 0, "Submit"),
            ],
        );
        let out = emit(&file, EmitFormat::Xdotool, Platform::Linux, Some("submit")).unwrap();
        assert!(out.contains("xdotool mousemove 105 105"), "got: {out}");
        assert!(!out.contains("mousemove 5 5"), "got: {out}");
        // The comment keeps the selection's original session index.
        assert!(out.contains("Submit — rect on monitor 0"), "got: {out}");

        let err = emit(&file, EmitFormat::Xdotool, Platform::Linux, Some("send")).unwrap_err();
        assert_eq!(
            err,
            EmitError::UnknownLabel {
                requested: "send".into(),
                available: vec!["Cancel".into(), "Submit".into()],
            }
        );
    }

    #[test]
    fn an_empty_session_is_an_error() {
        let file = session(vec![monitor(0, 0, 0, 1.0)], &[]);
        let err = emit(&file, EmitFormat::Xdotool, Platform::Linux, None).unwrap_err();
        assert_eq!(err, EmitError::NoSelections);
    }

    #[test]
    fn a_selection_on_an_undescribed_monitor_is_an_error_for_logical_units() {
        let file = session(
            vec![monitor(0, 0, 0, 2.0)],
            &[labeled(Shape::Rect(Rect::new(0, 0, 10, 10)), 3, "orphan")],
        );
        let err = emit(&file, EmitFormat::Cliclick, Platform::MacOs, None).unwrap_err();
        assert_eq!(
            err,
            EmitError::UnknownMonitor {
                selection: 0,
                monitor: 3,
            }
        );
        // Physical units never look the monitor up, so the same session
        // still emits for xdotool.
        assert!(emit(&file, EmitFormat::Xdotool, Platform::Linux, None).is_ok());
    }
}