rsclaw-platform 0.1.0

Platform crate for RsClaw — internal workspace crate, not for direct use
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
//! Self-owned, **script-based** cross-platform screen capture + window
//! enumeration — the in-tree replacement for the `xcap` crate.
//!
//! Why not xcap: on macOS xcap routes through ScreenCaptureKit, which on every
//! single grab re-enumerates `SCShareableContent` (a window-server round trip)
//! and spins up a fresh capture session — ~1.5s per call, fatal for a tight
//! monitor loop. It also breaks the Linux build (pipewire/libspa chain), so
//! capture was simply disabled there. Shelling out to the OS-native capture
//! tools is both far faster (Apple's `screencapture` is ~0.1s) and makes Linux
//! work again.
//!
//! Backends:
//! - **macOS**: `screencapture` for pixels, `python3`+Quartz for window list /
//!   scale factor (python is already required on macOS for the Vision OCR path).
//! - **Windows**: PowerShell + System.Drawing (capture) and user32 P/Invoke
//!   (window list / per-window capture via PrintWindow).
//! - **Linux**: `grim` (Wayland) → `scrot` → ImageMagick `import` (X11) for
//!   pixels; `wmctrl` for the window list.
//!
//! All coordinates are in **logical** screen points. Capture outputs are native
//! resolution (e.g. 2× on Retina); use [`primary_scale_factor`] when a caller
//! needs to map back to logical pixels.

use anyhow::{anyhow, Result};
use std::path::PathBuf;
use std::process::Command;
use std::sync::atomic::{AtomicU64, Ordering};

/// An on-screen window. `id` is the OS-native window identifier (CGWindowID on
/// macOS, HWND on Windows, X11 window id on Linux). Bounds are logical points.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct WindowInfo {
    pub id: u64,
    pub app: String,
    pub title: String,
    pub x: i32,
    pub y: i32,
    pub w: u32,
    pub h: u32,
    pub minimized: bool,
}

static TMP_SEQ: AtomicU64 = AtomicU64::new(0);

fn tmp_png(tag: &str) -> PathBuf {
    let n = TMP_SEQ.fetch_add(1, Ordering::Relaxed);
    std::env::temp_dir().join(format!("rsclaw_cap_{}_{tag}_{n}.png", std::process::id()))
}

/// Decode PNG bytes into an RGBA image (for pixel scanning).
pub fn png_to_rgba(bytes: &[u8]) -> Result<image::RgbaImage> {
    let img = image::load_from_memory(bytes).map_err(|e| anyhow!("decode PNG: {e}"))?;
    Ok(img.to_rgba8())
}

/// Wrap PNG bytes as a `data:image/png;base64,...` URI.
pub fn png_to_data_uri(bytes: &[u8]) -> String {
    use base64::Engine;
    format!(
        "data:image/png;base64,{}",
        base64::engine::general_purpose::STANDARD.encode(bytes)
    )
}

// ===========================================================================
// macOS
// ===========================================================================
#[cfg(target_os = "macos")]
mod imp {
    use super::*;

    fn run_screencapture(args: &[String], out: &PathBuf) -> Result<Vec<u8>> {
        let status = Command::new("/usr/sbin/screencapture")
            .args(args)
            .arg(out)
            .output()
            .map_err(|e| anyhow!("screencapture spawn failed: {e}"))?;
        if !status.status.success() {
            let _ = std::fs::remove_file(out);
            return Err(anyhow!(
                "screencapture exited {}: {}",
                status.status,
                String::from_utf8_lossy(&status.stderr).trim()
            ));
        }
        let bytes = std::fs::read(out).map_err(|e| anyhow!("read capture: {e}"))?;
        let _ = std::fs::remove_file(out);
        Ok(bytes)
    }

    pub fn capture_region_png(x: i32, y: i32, w: u32, h: u32) -> Result<Vec<u8>> {
        let out = tmp_png("region");
        run_screencapture(
            &["-x".into(), format!("-R{x},{y},{w},{h}")],
            &out,
        )
    }

    pub fn capture_full_png() -> Result<Vec<u8>> {
        let out = tmp_png("full");
        run_screencapture(&["-x".into()], &out)
    }

    pub fn capture_window_png(window_id: u64) -> Result<Vec<u8>> {
        let out = tmp_png("win");
        // -o: no window shadow; -l<id>: capture that exact window (occlusion-proof).
        run_screencapture(&["-x".into(), "-o".into(), format!("-l{window_id}")], &out)
    }

    const WINLIST_PY: &str = r#"import json, Quartz
wl = Quartz.CGWindowListCopyWindowInfo(
    Quartz.kCGWindowListOptionOnScreenOnly | Quartz.kCGWindowListExcludeDesktopElements,
    Quartz.kCGNullWindowID)
out = []
for w in wl or []:
    b = w.get('kCGWindowBounds', {})
    out.append({
        'id': int(w.get('kCGWindowNumber', 0)),
        'app': w.get('kCGWindowOwnerName', '') or '',
        'title': w.get('kCGWindowName', '') or '',
        'x': int(b.get('X', 0)), 'y': int(b.get('Y', 0)),
        'w': int(b.get('Width', 0)), 'h': int(b.get('Height', 0)),
        'minimized': not bool(w.get('kCGWindowIsOnscreen', True)),
    })
print(json.dumps(out))
"#;

    pub fn list_windows() -> Result<Vec<WindowInfo>> {
        let out = Command::new("python3")
            .args(["-c", WINLIST_PY])
            .output()
            .map_err(|e| anyhow!("python3 window list spawn failed (need pyobjc Quartz): {e}"))?;
        if !out.status.success() {
            return Err(anyhow!(
                "window list failed: {}",
                String::from_utf8_lossy(&out.stderr).trim()
            ));
        }
        let wins: Vec<WindowInfo> = serde_json::from_slice(&out.stdout)
            .map_err(|e| anyhow!("parse window list: {e}"))?;
        Ok(wins)
    }

    const SCALE_PY: &str = r#"import Quartz
d = Quartz.CGMainDisplayID()
px = Quartz.CGDisplayPixelsWide(d)
b = Quartz.CGDisplayBounds(d)
pts = b.size.width
print(px / pts if pts else 1.0)
"#;

    pub fn primary_scale_factor() -> f32 {
        Command::new("python3")
            .args(["-c", SCALE_PY])
            .output()
            .ok()
            .filter(|o| o.status.success())
            .and_then(|o| String::from_utf8_lossy(&o.stdout).trim().parse::<f32>().ok())
            .filter(|s| *s > 0.0)
            .unwrap_or(2.0)
    }

    /// macOS apps don't destroy their window on close the way WeChat 4.x does on
    /// Windows; the activate path (`open -b`) already handles bringing them up.
    pub fn restore_app_window(_app_name: &str) -> Result<bool> {
        Ok(false)
    }
}

// ===========================================================================
// Windows
// ===========================================================================
#[cfg(target_os = "windows")]
mod imp {
    use super::*;

    fn run_powershell(script: &str, out: &PathBuf) -> Result<Vec<u8>> {
        use std::os::windows::process::CommandExt;
        let status = Command::new("powershell")
            .args(["-NoProfile", "-NonInteractive", "-Command", script])
            // CREATE_NO_WINDOW: no flashing console window per capture.
            .creation_flags(0x08000000)
            .output()
            .map_err(|e| anyhow!("powershell spawn failed: {e}"))?;
        if !status.status.success() {
            let _ = std::fs::remove_file(out);
            return Err(anyhow!(
                "powershell capture failed: {}",
                String::from_utf8_lossy(&status.stderr).trim()
            ));
        }
        let bytes = std::fs::read(out).map_err(|e| anyhow!("read capture: {e}"))?;
        let _ = std::fs::remove_file(out);
        Ok(bytes)
    }

    pub fn capture_region_png(x: i32, y: i32, w: u32, h: u32) -> Result<Vec<u8>> {
        let out = tmp_png("region");
        let p = out.display();
        // DPI-aware so coords/sizes are PHYSICAL pixels (match enigo + full capture).
        let script = format!(
            "Add-Type -MemberDefinition '[DllImport(\"user32.dll\")] public static extern bool SetProcessDPIAware();' -Name DpiR -Namespace Win32 -ErrorAction SilentlyContinue; \
             try {{ [Win32.DpiR]::SetProcessDPIAware() | Out-Null }} catch {{}}; \
             Add-Type -AssemblyName System.Drawing; \
             $bmp = New-Object System.Drawing.Bitmap({w}, {h}); \
             $g = [System.Drawing.Graphics]::FromImage($bmp); \
             $g.CopyFromScreen({x}, {y}, 0, 0, $bmp.Size); \
             $bmp.Save('{p}', [System.Drawing.Imaging.ImageFormat]::Png); \
             $g.Dispose(); $bmp.Dispose()"
        );
        run_powershell(&script, &out)
    }

    pub fn capture_full_png() -> Result<Vec<u8>> {
        let out = tmp_png("full");
        let p = out.display();
        // SetProcessDPIAware FIRST so on a scaled display (e.g. 2560x1440 @150%)
        // we capture PHYSICAL pixels (2560x1440), matching the coordinate space that
        // enigo SendInput uses for clicks. Without it CopyFromScreen captures the
        // logical 1707x960 surface and every OCR→click mapping is off by the DPI
        // scale (clicks land at ~1/1.5 of the intended position).
        let script = format!(
            "Add-Type -MemberDefinition '[DllImport(\"user32.dll\")] public static extern bool SetProcessDPIAware();' -Name DpiA -Namespace Win32 -ErrorAction SilentlyContinue; \
             try {{ [Win32.DpiA]::SetProcessDPIAware() | Out-Null }} catch {{}}; \
             Add-Type -AssemblyName System.Windows.Forms; \
             Add-Type -AssemblyName System.Drawing; \
             $b = [System.Windows.Forms.SystemInformation]::VirtualScreen; \
             $bmp = New-Object System.Drawing.Bitmap($b.Width, $b.Height); \
             $g = [System.Drawing.Graphics]::FromImage($bmp); \
             $g.CopyFromScreen($b.Left, $b.Top, 0, 0, $bmp.Size); \
             $bmp.Save('{p}', [System.Drawing.Imaging.ImageFormat]::Png); \
             $g.Dispose(); $bmp.Dispose()"
        );
        run_powershell(&script, &out)
    }

    pub fn capture_window_png(window_id: u64) -> Result<Vec<u8>> {
        // PrintWindow the HWND (captures even if partially occluded).
        let out = tmp_png("win");
        let p = out.display();
        let script = format!(
            r#"Add-Type -AssemblyName System.Drawing;
$sig = @'
using System;
using System.Runtime.InteropServices;
public class W {{
  [DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr h, out RECT r);
  [DllImport("user32.dll")] public static extern bool PrintWindow(IntPtr h, IntPtr dc, uint f);
  [StructLayout(LayoutKind.Sequential)] public struct RECT {{ public int L,T,R,B; }}
}}
'@;
Add-Type $sig;
$h = [IntPtr]{window_id};
$r = New-Object W+RECT;
[void][W]::GetWindowRect($h, [ref]$r);
$w = $r.R - $r.L; $ht = $r.B - $r.T;
$bmp = New-Object System.Drawing.Bitmap($w, $ht);
$g = [System.Drawing.Graphics]::FromImage($bmp);
$dc = $g.GetHdc();
[void][W]::PrintWindow($h, $dc, 2);
$g.ReleaseHdc($dc);
$bmp.Save('{p}', [System.Drawing.Imaging.ImageFormat]::Png);
$g.Dispose(); $bmp.Dispose()"#
        );
        run_powershell(&script, &out)
    }

    pub fn list_windows() -> Result<Vec<WindowInfo>> {
        let script = r#"Add-Type -AssemblyName System.Text.Json -ErrorAction SilentlyContinue
$sig = @'
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public class WL {
  [DllImport("user32.dll")] public static extern bool EnumWindows(EnumProc cb, IntPtr p);
  [DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr h);
  [DllImport("user32.dll")] public static extern int GetWindowText(IntPtr h, StringBuilder s, int n);
  [DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr h, out RECT r);
  [DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr h, out uint pid);
  public delegate bool EnumProc(IntPtr h, IntPtr p);
  [StructLayout(LayoutKind.Sequential)] public struct RECT { public int L,T,R,B; }
  public static List<string> Run() {
    var res = new List<string>();
    EnumWindows((h,p) => {
      if (!IsWindowVisible(h)) return true;
      var sb = new StringBuilder(512); GetWindowText(h, sb, 512);
      RECT r; GetWindowRect(h, out r);
      uint pid; GetWindowThreadProcessId(h, out pid);
      string app=""; try { app = System.Diagnostics.Process.GetProcessById((int)pid).ProcessName; } catch {}
      int w=r.R-r.L, ht=r.B-r.T;
      if (w<=0||ht<=0) return true;
      res.Add(((long)h)+"\t"+app+"\t"+sb.ToString().Replace("\t"," ")+"\t"+r.L+"\t"+r.T+"\t"+w+"\t"+ht);
      return true;
    }, IntPtr.Zero);
    return res;
  }
}
'@
Add-Type $sig
[WL]::Run() | ForEach-Object { $_ }"#;
        use std::os::windows::process::CommandExt;
        let out = Command::new("powershell")
            .args(["-NoProfile", "-NonInteractive", "-Command", script])
            // CREATE_NO_WINDOW: no flashing console window when listing windows.
            .creation_flags(0x08000000)
            .output()
            .map_err(|e| anyhow!("powershell window list spawn failed: {e}"))?;
        if !out.status.success() {
            return Err(anyhow!(
                "window list failed: {}",
                String::from_utf8_lossy(&out.stderr).trim()
            ));
        }
        let text = String::from_utf8_lossy(&out.stdout);
        let mut wins = Vec::new();
        for line in text.lines() {
            let f: Vec<&str> = line.split('\t').collect();
            if f.len() < 7 {
                continue;
            }
            wins.push(WindowInfo {
                id: f[0].trim().parse().unwrap_or(0),
                app: f[1].to_string(),
                title: f[2].to_string(),
                x: f[3].trim().parse().unwrap_or(0),
                y: f[4].trim().parse().unwrap_or(0),
                w: f[5].trim().parse().unwrap_or(0),
                h: f[6].trim().parse().unwrap_or(0),
                minimized: false,
            });
        }
        Ok(wins)
    }

    pub fn primary_scale_factor() -> f32 {
        1.0
    }

    /// Restore an app's hidden/tray main window. WeChat 4.x closes to the tray by
    /// destroying its top-level window, so we enumerate ALL top-level windows of
    /// the matching process (including invisible ones) whose title looks like the
    /// main window and SW_RESTORE + foreground them. Returns true if ≥1 restored.
    pub fn restore_app_window(app_name: &str) -> Result<bool> {
        use std::os::windows::process::CommandExt;
        let needle = app_name.to_lowercase();
        // Process-name aliases (WeChat 4.x renamed the process to "Weixin").
        let proc_match = if needle.contains("wechat") || needle.contains("weixin") {
            "weixin wechat".to_string()
        } else {
            needle.clone()
        };
        // Embed the process-name match list; titles are fixed to the known main
        // window captions so we don't surface IME/tray helper windows.
        let script = format!(
            r#"$procs = '{procs}'.Split(' ')
$sig = @'
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public class WR {{
  [DllImport("user32.dll")] public static extern bool EnumWindows(EnumProc cb, IntPtr p);
  [DllImport("user32.dll")] public static extern int GetWindowText(IntPtr h, StringBuilder s, int n);
  [DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr h, out uint pid);
  [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr h, int n);
  [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr h);
  public delegate bool EnumProc(IntPtr h, IntPtr p);
  public static int Restore(string[] procs, string[] titles) {{
    int n = 0;
    EnumWindows((h,p) => {{
      uint pid; GetWindowThreadProcessId(h, out pid);
      string pname = "";
      try {{ pname = System.Diagnostics.Process.GetProcessById((int)pid).ProcessName.ToLower(); }} catch {{ return true; }}
      bool pmatch = false; foreach (var pr in procs) {{ if (pr.Length > 0 && pname.Contains(pr)) pmatch = true; }}
      if (!pmatch) return true;
      var sb = new StringBuilder(512); GetWindowText(h, sb, 512);
      string t = sb.ToString();
      bool tmatch = false; foreach (var tt in titles) {{ if (t == tt) tmatch = true; }}
      if (!tmatch) return true;
      ShowWindow(h, 9); ShowWindow(h, 5); SetForegroundWindow(h); n++;
      return true;
    }}, IntPtr.Zero);
    return n;
  }}
}}
'@
Add-Type $sig
[WR]::Restore($procs, @('微信','Weixin','WeChat'))"#,
            procs = proc_match
        );
        let out = Command::new("powershell")
            .args(["-NoProfile", "-NonInteractive", "-Command", &script])
            // CREATE_NO_WINDOW: no flashing console window.
            .creation_flags(0x08000000)
            .output()
            .map_err(|e| anyhow!("powershell restore spawn failed: {e}"))?;
        if !out.status.success() {
            return Err(anyhow!(
                "restore window failed: {}",
                String::from_utf8_lossy(&out.stderr).trim()
            ));
        }
        let n: i32 = String::from_utf8_lossy(&out.stdout)
            .trim()
            .lines()
            .last()
            .and_then(|l| l.trim().parse().ok())
            .unwrap_or(0);
        Ok(n > 0)
    }
}

// ===========================================================================
// Linux
// ===========================================================================
#[cfg(target_os = "linux")]
mod imp {
    use super::*;

    fn have(cmd: &str) -> bool {
        which::which(cmd).is_ok()
    }

    fn read_consume(out: &PathBuf) -> Result<Vec<u8>> {
        let bytes = std::fs::read(out).map_err(|e| anyhow!("read capture: {e}"))?;
        let _ = std::fs::remove_file(out);
        Ok(bytes)
    }

    pub fn capture_region_png(x: i32, y: i32, w: u32, h: u32) -> Result<Vec<u8>> {
        let out = tmp_png("region");
        // Wayland: grim -g "X,Y WxH". X11: ImageMagick import / scrot -a.
        if have("grim") {
            let st = Command::new("grim")
                .args(["-g", &format!("{x},{y} {w}x{h}")])
                .arg(&out)
                .status();
            if matches!(st, Ok(s) if s.success()) {
                return read_consume(&out);
            }
        }
        if have("import") {
            let st = Command::new("import")
                .args(["-window", "root", "-crop", &format!("{w}x{h}+{x}+{y}")])
                .arg(&out)
                .status();
            if matches!(st, Ok(s) if s.success()) {
                return read_consume(&out);
            }
        }
        if have("scrot") {
            let st = Command::new("scrot")
                .args(["-a", &format!("{x},{y},{w},{h}")])
                .arg(&out)
                .status();
            if matches!(st, Ok(s) if s.success()) {
                return read_consume(&out);
            }
        }
        Err(anyhow!(
            "no Linux screenshot tool found (install one of: grim, imagemagick, scrot)"
        ))
    }

    pub fn capture_full_png() -> Result<Vec<u8>> {
        let out = tmp_png("full");
        if have("grim") {
            let st = Command::new("grim").arg(&out).status();
            if matches!(st, Ok(s) if s.success()) {
                return read_consume(&out);
            }
        }
        if have("scrot") {
            let st = Command::new("scrot").arg(&out).status();
            if matches!(st, Ok(s) if s.success()) {
                return read_consume(&out);
            }
        }
        if have("import") {
            let st = Command::new("import").args(["-window", "root"]).arg(&out).status();
            if matches!(st, Ok(s) if s.success()) {
                return read_consume(&out);
            }
        }
        Err(anyhow!(
            "no Linux screenshot tool found (install one of: grim, scrot, imagemagick)"
        ))
    }

    pub fn capture_window_png(window_id: u64) -> Result<Vec<u8>> {
        let out = tmp_png("win");
        // X11 only: capture the specific window by id via ImageMagick import.
        if have("import") {
            let st = Command::new("import")
                .args(["-window", &window_id.to_string()])
                .arg(&out)
                .status();
            if matches!(st, Ok(s) if s.success()) {
                return read_consume(&out);
            }
        }
        // Fallback: capture the window's region from its bounds.
        if let Ok(wins) = list_windows() {
            if let Some(w) = wins.iter().find(|w| w.id == window_id) {
                return capture_region_png(w.x, w.y, w.w, w.h);
            }
        }
        Err(anyhow!(
            "window capture needs ImageMagick `import` (X11) or a resolvable window region"
        ))
    }

    pub fn list_windows() -> Result<Vec<WindowInfo>> {
        if !have("wmctrl") {
            return Err(anyhow!("window list needs `wmctrl` on Linux"));
        }
        // `wmctrl -lG`: id desktop x y w h host title ; `-lx` adds WM_CLASS for app.
        let geo = Command::new("wmctrl")
            .args(["-lG"])
            .output()
            .map_err(|e| anyhow!("wmctrl spawn failed: {e}"))?;
        if !geo.status.success() {
            return Err(anyhow!(
                "wmctrl failed: {}",
                String::from_utf8_lossy(&geo.stderr).trim()
            ));
        }
        let class = Command::new("wmctrl").args(["-lx"]).output().ok();
        let class_txt = class
            .as_ref()
            .map(|o| String::from_utf8_lossy(&o.stdout).to_string())
            .unwrap_or_default();
        let mut app_by_id = std::collections::HashMap::new();
        for line in class_txt.lines() {
            let f: Vec<&str> = line.split_whitespace().collect();
            if f.len() >= 3 {
                if let Ok(id) = u64::from_str_radix(f[0].trim_start_matches("0x"), 16) {
                    app_by_id.insert(id, f[2].to_string());
                }
            }
        }
        let text = String::from_utf8_lossy(&geo.stdout);
        let mut wins = Vec::new();
        for line in text.lines() {
            let f: Vec<&str> = line.splitn(8, char::is_whitespace).collect();
            if f.len() < 8 {
                continue;
            }
            let id = u64::from_str_radix(f[0].trim_start_matches("0x"), 16).unwrap_or(0);
            wins.push(WindowInfo {
                id,
                app: app_by_id.get(&id).cloned().unwrap_or_default(),
                title: f[7].to_string(),
                x: f[2].parse().unwrap_or(0),
                y: f[3].parse().unwrap_or(0),
                w: f[4].parse().unwrap_or(0),
                h: f[5].parse().unwrap_or(0),
                minimized: false,
            });
        }
        Ok(wins)
    }

    pub fn primary_scale_factor() -> f32 {
        1.0
    }

    pub fn restore_app_window(_app_name: &str) -> Result<bool> {
        Ok(false)
    }
}

// ===========================================================================
// Public API (delegates to the platform module)
// ===========================================================================

/// Capture a region (logical points) of the screen → PNG bytes.
pub fn capture_region_png(x: i32, y: i32, w: u32, h: u32) -> Result<Vec<u8>> {
    imp::capture_region_png(x, y, w, h)
}

/// Capture the full (primary/virtual) screen → PNG bytes.
pub fn capture_full_png() -> Result<Vec<u8>> {
    imp::capture_full_png()
}

/// Capture a specific window by its OS-native id → PNG bytes.
pub fn capture_window_png(window_id: u64) -> Result<Vec<u8>> {
    imp::capture_window_png(window_id)
}

/// Enumerate on-screen windows (front-to-back where the OS provides ordering).
pub fn list_windows() -> Result<Vec<WindowInfo>> {
    imp::list_windows()
}

/// Primary-display scale factor (2.0 on macOS Retina, 1.0 elsewhere by default).
pub fn primary_scale_factor() -> f32 {
    imp::primary_scale_factor()
}

/// Restore an app's main window from the system tray / hidden state so it can be
/// captured. `app_name` is matched case-insensitively against the process name
/// (e.g. "WeChat"/"Weixin"). Returns `Ok(true)` if at least one window was
/// restored. No-op (returns `Ok(false)`) on platforms where it isn't needed.
///
/// Motivation: WeChat 4.x closes to the tray by destroying its top-level window
/// (MainWindowHandle becomes 0), so window enumeration finds nothing and OCR has
/// no surface. This brings the hidden main window back before capture.
pub fn restore_app_window(app_name: &str) -> Result<bool> {
    imp::restore_app_window(app_name)
}

/// Convenience: capture a region directly as an RGBA image.
pub fn capture_region_rgba(x: i32, y: i32, w: u32, h: u32) -> Result<image::RgbaImage> {
    png_to_rgba(&capture_region_png(x, y, w, h)?)
}