forgewright 0.2.0

Standalone UI automation — CDP for browsers, UIA for Windows desktop apps
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
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
// uia.rs — Windows UI Automation backend
//
// Drives ANY Windows desktop app via the UIA accessibility tree.
// Same action vocabulary as CDP (click, type, query, screenshot, wait-for)
// plus UIA-specific: tree, list-windows, raw-keys, focus
//
// Compiled only on Windows via cfg(target_os = "windows").

#![cfg(target_os = "windows")]

use std::path::Path;
use std::time::{Duration, Instant};
use std::{mem, thread};

use serde_json::{json, Value};

use windows::core::{BSTR, VARIANT};
use windows::Win32::Foundation::{BOOL, HWND, LPARAM, RECT, TRUE};
use windows::Win32::Graphics::Gdi::{
    BitBlt, CreateCompatibleBitmap, CreateCompatibleDC, DeleteDC, DeleteObject, GetDC, GetDIBits,
    ReleaseDC, SelectObject, BITMAPINFO, BITMAPINFOHEADER, BI_RGB, DIB_RGB_COLORS, SRCCOPY,
};
use windows::Win32::System::Com::{
    CoCreateInstance, CoInitializeEx, CLSCTX_ALL, COINIT_MULTITHREADED,
};
use windows::Win32::UI::Accessibility::{
    CUIAutomation, IUIAutomation, IUIAutomationCondition, IUIAutomationElement,
    IUIAutomationExpandCollapsePattern, IUIAutomationInvokePattern,
    IUIAutomationTogglePattern, IUIAutomationValuePattern, TreeScope_Children,
    TreeScope_Descendants, UIA_PATTERN_ID, UIA_PROPERTY_ID, UIA_CONTROLTYPE_ID,
};
use windows::Win32::UI::Input::KeyboardAndMouse::{
    SendInput, INPUT, INPUT_0, INPUT_KEYBOARD, INPUT_MOUSE, KEYBDINPUT, KEYEVENTF_KEYUP,
    KEYEVENTF_UNICODE, MOUSEEVENTF_ABSOLUTE, MOUSEEVENTF_LEFTDOWN, MOUSEEVENTF_LEFTUP,
    MOUSEEVENTF_MOVE, MOUSEINPUT, VIRTUAL_KEY,
};
use windows::Win32::UI::WindowsAndMessaging::{
    EnumWindows, GetClientRect, GetWindowTextW,
    GetWindowThreadProcessId, IsWindowVisible, SetForegroundWindow,
    SM_CXSCREEN, SM_CYSCREEN, GetSystemMetrics,
};
use windows::Win32::Graphics::Gdi::HDC;

const PW_CLIENTONLY: u32 = 0x00000001;
const PW_RENDERFULLCONTENT: u32 = 0x00000002;

#[link(name = "user32")]
extern "system" {
    fn PrintWindow(hwnd: HWND, hdc_blt: HDC, flags: u32) -> BOOL;
}

use crate::selector::{self, Condition, Selector, Step};

// UIA property IDs (wrapped in newtype)
const PROP_NAME: UIA_PROPERTY_ID = UIA_PROPERTY_ID(30005);
const PROP_AUTOMATION_ID: UIA_PROPERTY_ID = UIA_PROPERTY_ID(30011);
const PROP_CLASS_NAME: UIA_PROPERTY_ID = UIA_PROPERTY_ID(30012);
const PROP_CONTROL_TYPE: UIA_PROPERTY_ID = UIA_PROPERTY_ID(30003);

// UIA pattern IDs (wrapped in newtype)
const PAT_INVOKE: UIA_PATTERN_ID = UIA_PATTERN_ID(10000);
const PAT_VALUE: UIA_PATTERN_ID = UIA_PATTERN_ID(10002);
const PAT_TOGGLE: UIA_PATTERN_ID = UIA_PATTERN_ID(10015);
const PAT_EXPAND_COLLAPSE: UIA_PATTERN_ID = UIA_PATTERN_ID(10005);

type UiaResult<T> = Result<T, Box<dyn std::error::Error>>;

pub struct Uia {
    automation: IUIAutomation,
    window: IUIAutomationElement,
    hwnd: HWND,
}

impl Uia {
    /// Connect to a window by spec:
    ///   "MyApp"          → exact title match
    ///   "*MyApp*"        → contains match
    ///   "pid:12345"      → process ID match
    ///   "class:Notepad"  → window class match
    pub fn connect(window_spec: &str) -> UiaResult<Self> {
        unsafe { CoInitializeEx(None, COINIT_MULTITHREADED).ok()? };

        let automation: IUIAutomation =
            unsafe { CoCreateInstance(&CUIAutomation, None, CLSCTX_ALL)? };

        let hwnd = find_window(window_spec)?;

        // Get UIA element for the window
        let window = unsafe { automation.ElementFromHandle(hwnd)? };

        Ok(Uia {
            automation,
            window,
            hwnd,
        })
    }

    /// Click an element. Tries InvokePattern first, falls back to mouse click at center.
    pub fn click(&self, selector_str: &str) -> UiaResult<Value> {
        let element = self.find(selector_str)?;
        let name = get_name(&element);

        // Try InvokePattern (catch invoke errors)
        if let Ok(pattern) = unsafe {
            element.GetCurrentPatternAs::<IUIAutomationInvokePattern>(PAT_INVOKE)
        } {
            if unsafe { pattern.Invoke() }.is_ok() {
                return Ok(json!({
                    "result": "invoked",
                    "name": name,
                }));
            }
        }

        // Try TogglePattern (checkboxes)
        if let Ok(pattern) = unsafe {
            element.GetCurrentPatternAs::<IUIAutomationTogglePattern>(PAT_TOGGLE)
        } {
            if unsafe { pattern.Toggle() }.is_ok() {
                return Ok(json!({
                    "result": "toggled",
                    "name": name,
                }));
            }
        }

        // Fallback: mouse click at element center
        let rect = get_rect(&element)?;
        let cx = (rect.left + rect.right) / 2;
        let cy = (rect.top + rect.bottom) / 2;
        click_at(cx, cy)?;

        Ok(json!({
            "result": "clicked_at",
            "name": name,
            "x": cx,
            "y": cy,
        }))
    }

    /// Type text into an element. Tries ValuePattern first, falls back to SendInput keystrokes.
    pub fn type_text(&self, selector_str: &str, text: &str) -> UiaResult<Value> {
        let element = self.find(selector_str)?;
        let name = get_name(&element);

        // Try ValuePattern
        if let Ok(pattern) = unsafe {
            element.GetCurrentPatternAs::<IUIAutomationValuePattern>(PAT_VALUE)
        } {
            unsafe { pattern.SetValue(&BSTR::from(text))? };
            return Ok(json!({
                "result": "set_value",
                "name": name,
                "text": text,
            }));
        }

        // Fallback: focus + SendInput keystrokes
        unsafe { element.SetFocus()? };
        thread::sleep(Duration::from_millis(50));
        send_string(text)?;

        Ok(json!({
            "result": "typed_keys",
            "name": name,
            "text": text,
        }))
    }

    /// Query element properties
    pub fn query(&self, selector_str: &str) -> UiaResult<Value> {
        let element = self.find(selector_str)?;
        Ok(describe_element(&element))
    }

    /// Screenshot the target window to a BMP file (no external crate needed)
    pub fn screenshot(&self, path: &Path) -> UiaResult<()> {
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).ok();
        }

        unsafe {
            let mut rect = RECT::default();
            GetClientRect(self.hwnd, &mut rect)?;
            let width = rect.right - rect.left;
            let height = rect.bottom - rect.top;

            if width <= 0 || height <= 0 {
                return Err("window has zero size".into());
            }

            let hdc_window = GetDC(self.hwnd);
            let hdc_mem = CreateCompatibleDC(hdc_window);
            let hbm = CreateCompatibleBitmap(hdc_window, width, height);
            let old = SelectObject(hdc_mem, hbm);

            // PrintWindow with PW_RENDERFULLCONTENT works on occluded, minimized,
            // or GPU-accelerated windows. BitBlt from window DC returns whatever
            // is composited on top and produces wrong pixels when the target is
            // not foreground.
            let ok = PrintWindow(self.hwnd, hdc_mem, PW_CLIENTONLY | PW_RENDERFULLCONTENT);
            if !ok.as_bool() {
                // Fallback: BitBlt the window DC.
                let _ = BitBlt(hdc_mem, 0, 0, width, height, hdc_window, 0, 0, SRCCOPY);
            }

            // Extract pixel data
            let row_bytes = ((width * 3 + 3) / 4) * 4; // BMP rows are 4-byte aligned
            let data_size = (row_bytes * height) as usize;
            let mut pixels = vec![0u8; data_size];

            let mut bmi = BITMAPINFO {
                bmiHeader: BITMAPINFOHEADER {
                    biSize: mem::size_of::<BITMAPINFOHEADER>() as u32,
                    biWidth: width,
                    biHeight: height, // positive = bottom-up
                    biPlanes: 1,
                    biBitCount: 24,
                    biCompression: BI_RGB.0 as u32,
                    biSizeImage: data_size as u32,
                    ..Default::default()
                },
                ..Default::default()
            };

            GetDIBits(
                hdc_mem,
                hbm,
                0,
                height as u32,
                Some(pixels.as_mut_ptr() as *mut _),
                &mut bmi,
                DIB_RGB_COLORS,
            );

            // Clean up GDI
            SelectObject(hdc_mem, old);
            let _ = DeleteObject(hbm);
            let _ = DeleteDC(hdc_mem);
            ReleaseDC(self.hwnd, hdc_window);

            // Write BMP file
            write_bmp(path, width, height, row_bytes, &pixels)?;
        }

        Ok(())
    }

    /// Dump the UIA element tree
    pub fn tree(&self, selector_str: Option<&str>, depth: u32) -> UiaResult<Value> {
        let root = if let Some(sel) = selector_str {
            self.find(sel)?
        } else {
            self.window.clone()
        };
        Ok(build_tree(&self.automation, &root, depth, 0))
    }

    /// List all visible top-level windows
    pub fn list_windows(&self) -> UiaResult<Value> {
        let windows = enumerate_windows()?;
        Ok(json!(windows))
    }

    /// Wait until an element matching the selector exists
    pub fn wait_for(&self, selector_str: &str, timeout_ms: u64) -> UiaResult<Value> {
        let start = Instant::now();
        let sel = selector::parse(selector_str).map_err(|e| format!("selector: {}", e))?;

        loop {
            if let Ok(element) = self.find_parsed(&sel) {
                let name = get_name(&element);
                return Ok(json!({
                    "found": true,
                    "name": name,
                    "elapsed_ms": start.elapsed().as_millis() as u64,
                }));
            }
            if start.elapsed().as_millis() >= timeout_ms as u128 {
                return Ok(json!({
                    "found": false,
                    "elapsed_ms": start.elapsed().as_millis() as u64,
                    "status": "timeout",
                }));
            }
            thread::sleep(Duration::from_millis(200));
        }
    }

    /// Focus an element
    pub fn focus(&self, selector_str: &str) -> UiaResult<Value> {
        let element = self.find(selector_str)?;
        unsafe { element.SetFocus()? };
        Ok(json!({ "result": "focused", "name": get_name(&element) }))
    }

    /// Send raw keystrokes (for apps without UIA patterns)
    pub fn raw_keys(&self, keys: &str) -> UiaResult<Value> {
        // Bring window to front
        let _ = unsafe { SetForegroundWindow(self.hwnd) };
        thread::sleep(Duration::from_millis(100));
        send_string(keys)?;
        Ok(json!({ "result": "sent", "keys": keys }))
    }

    /// Expand a combo box or tree node
    pub fn expand(&self, selector_str: &str) -> UiaResult<Value> {
        let element = self.find(selector_str)?;
        let pattern = unsafe {
            element
                .GetCurrentPatternAs::<IUIAutomationExpandCollapsePattern>(
                    PAT_EXPAND_COLLAPSE,
                )?
        };
        unsafe { pattern.Expand()? };
        Ok(json!({ "result": "expanded", "name": get_name(&element) }))
    }

    // ── Internal find ────────────────────────────────────────────────────

    fn find(&self, selector_str: &str) -> UiaResult<IUIAutomationElement> {
        let sel = selector::parse(selector_str).map_err(|e| format!("selector: {}", e))?;
        self.find_parsed(&sel)
    }

    fn find_parsed(&self, sel: &Selector) -> UiaResult<IUIAutomationElement> {
        let mut current = self.window.clone();

        for step in &sel.steps {
            let condition = self.build_condition(step)?;
            let scope = if sel.is_single_step() {
                TreeScope_Descendants
            } else {
                TreeScope_Children
            };

            if let Some(idx) = step.index {
                // Find all and pick by index
                let all = unsafe { current.FindAll(scope, &condition)? };
                let len = unsafe { all.Length()? };
                if idx >= len as usize {
                    return Err(format!(
                        "index [{}] out of range (found {} elements)",
                        idx, len
                    )
                    .into());
                }
                current = unsafe { all.GetElement(idx as i32)? };
            } else {
                current = unsafe {
                    current.FindFirst(scope, &condition)?
                };
            }
        }

        Ok(current)
    }

    fn build_condition(&self, step: &Step) -> UiaResult<IUIAutomationCondition> {
        let mut conditions: Vec<IUIAutomationCondition> = Vec::new();

        for cond in &step.conditions {
            let c = match cond {
                Condition::Name(name) => {
                    if name.contains('*') {
                        // Wildcard — strip * and do exact match on the inner text.
                        let clean = name.replace('*', "");
                        unsafe {
                            self.automation.CreatePropertyCondition(
                                PROP_NAME,
                                &VARIANT::from(BSTR::from(clean.as_str())),
                            )?
                        }
                    } else {
                        unsafe {
                            self.automation.CreatePropertyCondition(
                                PROP_NAME,
                                &VARIANT::from(BSTR::from(name.as_str())),
                            )?
                        }
                    }
                }
                Condition::AutomationId(aid) => unsafe {
                    self.automation.CreatePropertyCondition(
                        PROP_AUTOMATION_ID,
                        &VARIANT::from(BSTR::from(aid.as_str())),
                    )?
                },
                Condition::ControlType(type_name) => {
                    let type_id = selector::control_type_id(type_name).ok_or_else(|| {
                        format!("unknown control type: {}", type_name)
                    })?;
                    unsafe {
                        self.automation.CreatePropertyCondition(
                            PROP_CONTROL_TYPE,
                            &VARIANT::from(type_id),
                        )?
                    }
                }
                Condition::ClassName(class) => unsafe {
                    self.automation.CreatePropertyCondition(
                        PROP_CLASS_NAME,
                        &VARIANT::from(BSTR::from(class.as_str())),
                    )?
                },
            };
            conditions.push(c);
        }

        if conditions.len() == 1 {
            Ok(conditions.into_iter().next().unwrap())
        } else {
            // AND all conditions together
            let mut result = conditions[0].clone();
            for c in &conditions[1..] {
                result = unsafe { self.automation.CreateAndCondition(&result, c)? };
            }
            Ok(result)
        }
    }
}

// ═══ WINDOW FINDING ══════════════════════════════════════════════════════════

fn find_window(spec: &str) -> UiaResult<HWND> {
    if let Some(pid_str) = spec.strip_prefix("pid:") {
        let target_pid: u32 = pid_str.parse().map_err(|_| format!("invalid pid: {}", pid_str))?;
        let windows = enumerate_windows()?;
        for w in &windows {
            if w.get("pid").and_then(|v| v.as_u64()) == Some(target_pid as u64) {
                let hwnd_val = w.get("hwnd").and_then(|v| v.as_u64()).unwrap_or(0);
                return Ok(HWND(hwnd_val as *mut _));
            }
        }
        return Err(format!("no window for pid {}", target_pid).into());
    }

    if let Some(class) = spec.strip_prefix("class:") {
        let wide: Vec<u16> = class.encode_utf16().chain(std::iter::once(0)).collect();
        let hwnd = unsafe {
            windows::Win32::UI::WindowsAndMessaging::FindWindowW(
                windows::core::PCWSTR(wide.as_ptr()),
                None,
            )?
        };
        if hwnd.0.is_null() {
            return Err(format!("no window with class '{}'", class).into());
        }
        return Ok(hwnd);
    }

    // Title match (exact or wildcard)
    let is_wildcard = spec.contains('*');
    let pattern = spec.replace('*', "");

    let windows = enumerate_windows()?;
    for w in &windows {
        let title = w.get("title").and_then(|v| v.as_str()).unwrap_or("");
        let matches = if is_wildcard {
            title.to_lowercase().contains(&pattern.to_lowercase())
        } else {
            title == spec
        };
        if matches {
            let hwnd_val = w.get("hwnd").and_then(|v| v.as_u64()).unwrap_or(0);
            return Ok(HWND(hwnd_val as *mut _));
        }
    }

    Err(format!("no window matching '{}'", spec).into())
}

fn enumerate_windows() -> UiaResult<Vec<Value>> {
    let mut results: Vec<Value> = Vec::new();

    unsafe extern "system" fn enum_callback(hwnd: HWND, lparam: LPARAM) -> BOOL {
        let results = &mut *(lparam.0 as *mut Vec<Value>);

        if !IsWindowVisible(hwnd).as_bool() {
            return TRUE;
        }

        let mut title_buf = [0u16; 512];
        let len = GetWindowTextW(hwnd, &mut title_buf);
        if len == 0 {
            return TRUE;
        }
        let title = String::from_utf16_lossy(&title_buf[..len as usize]);

        let mut pid = 0u32;
        GetWindowThreadProcessId(hwnd, Some(&mut pid));

        results.push(json!({
            "hwnd": hwnd.0 as u64,
            "title": title,
            "pid": pid,
        }));

        TRUE
    }

    unsafe {
        EnumWindows(
            Some(enum_callback),
            LPARAM(&mut results as *mut Vec<Value> as isize),
        )?;
    }

    Ok(results)
}

// ═══ ELEMENT HELPERS ═════════════════════════════════════════════════════════

fn get_name(element: &IUIAutomationElement) -> String {
    unsafe {
        element
            .CurrentName()
            .map(|b| b.to_string())
            .unwrap_or_default()
    }
}

fn get_rect(element: &IUIAutomationElement) -> UiaResult<RECT> {
    unsafe {
        let r = element.CurrentBoundingRectangle()?;
        Ok(r)
    }
}

fn describe_element(element: &IUIAutomationElement) -> Value {
    unsafe {
        let name = element.CurrentName().map(|b| b.to_string()).unwrap_or_default();
        let aid = element
            .CurrentAutomationId()
            .map(|b| b.to_string())
            .unwrap_or_default();
        let class = element
            .CurrentClassName()
            .map(|b| b.to_string())
            .unwrap_or_default();
        let control_type = element.CurrentControlType().unwrap_or(UIA_CONTROLTYPE_ID(0));
        let ct_id = control_type.0;
        let enabled = element.CurrentIsEnabled().map(|b| b.as_bool()).unwrap_or(false);
        let focused = element
            .CurrentHasKeyboardFocus()
            .map(|b| b.as_bool())
            .unwrap_or(false);

        let rect = element.CurrentBoundingRectangle().unwrap_or(RECT::default());

        // Try to get current value
        let value = element
            .GetCurrentPatternAs::<IUIAutomationValuePattern>(PAT_VALUE)
            .ok()
            .and_then(|p| p.CurrentValue().ok())
            .map(|b| b.to_string());

        json!({
            "name": name,
            "automationId": aid,
            "className": class,
            "controlType": selector::control_type_name(ct_id),
            "controlTypeId": ct_id,
            "enabled": enabled,
            "focused": focused,
            "value": value,
            "rect": {
                "x": rect.left,
                "y": rect.top,
                "w": rect.right - rect.left,
                "h": rect.bottom - rect.top,
            },
        })
    }
}

fn build_tree(
    automation: &IUIAutomation,
    element: &IUIAutomationElement,
    max_depth: u32,
    current_depth: u32,
) -> Value {
    let mut node = describe_element(element);

    if current_depth < max_depth {
        let true_condition = unsafe { automation.CreateTrueCondition() };
        if let Ok(cond) = true_condition {
            if let Ok(children) = unsafe { element.FindAll(TreeScope_Children, &cond) } {
                let count = unsafe { children.Length().unwrap_or(0) };
                let mut child_nodes = Vec::new();
                for i in 0..count {
                    if let Ok(child) = unsafe { children.GetElement(i) } {
                        child_nodes.push(build_tree(automation, &child, max_depth, current_depth + 1));
                    }
                }
                if !child_nodes.is_empty() {
                    node.as_object_mut()
                        .unwrap()
                        .insert("children".into(), json!(child_nodes));
                }
            }
        }
    }

    node
}

// ═══ INPUT SIMULATION ════════════════════════════════════════════════════════

fn click_at(x: i32, y: i32) -> UiaResult<()> {
    // Convert to absolute coordinates (0-65535 range)
    let screen_w = unsafe { GetSystemMetrics(SM_CXSCREEN) };
    let screen_h = unsafe { GetSystemMetrics(SM_CYSCREEN) };

    let abs_x = (x * 65535) / screen_w;
    let abs_y = (y * 65535) / screen_h;

    let inputs = [
        INPUT {
            r#type: INPUT_MOUSE,
            Anonymous: INPUT_0 {
                mi: MOUSEINPUT {
                    dx: abs_x,
                    dy: abs_y,
                    mouseData: 0,
                    dwFlags: MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
                    time: 0,
                    dwExtraInfo: 0,
                },
            },
        },
        INPUT {
            r#type: INPUT_MOUSE,
            Anonymous: INPUT_0 {
                mi: MOUSEINPUT {
                    dx: abs_x,
                    dy: abs_y,
                    mouseData: 0,
                    dwFlags: MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_ABSOLUTE,
                    time: 0,
                    dwExtraInfo: 0,
                },
            },
        },
        INPUT {
            r#type: INPUT_MOUSE,
            Anonymous: INPUT_0 {
                mi: MOUSEINPUT {
                    dx: abs_x,
                    dy: abs_y,
                    mouseData: 0,
                    dwFlags: MOUSEEVENTF_LEFTUP | MOUSEEVENTF_ABSOLUTE,
                    time: 0,
                    dwExtraInfo: 0,
                },
            },
        },
    ];

    unsafe {
        SendInput(&inputs, mem::size_of::<INPUT>() as i32);
    }
    thread::sleep(Duration::from_millis(50));
    Ok(())
}

fn send_string(text: &str) -> UiaResult<()> {
    for c in text.chars() {
        let scan = c as u16;
        let inputs = [
            INPUT {
                r#type: INPUT_KEYBOARD,
                Anonymous: INPUT_0 {
                    ki: KEYBDINPUT {
                        wVk: VIRTUAL_KEY(0),
                        wScan: scan,
                        dwFlags: KEYEVENTF_UNICODE,
                        time: 0,
                        dwExtraInfo: 0,
                    },
                },
            },
            INPUT {
                r#type: INPUT_KEYBOARD,
                Anonymous: INPUT_0 {
                    ki: KEYBDINPUT {
                        wVk: VIRTUAL_KEY(0),
                        wScan: scan,
                        dwFlags: KEYEVENTF_UNICODE | KEYEVENTF_KEYUP,
                        time: 0,
                        dwExtraInfo: 0,
                    },
                },
            },
        ];
        unsafe {
            SendInput(&inputs, mem::size_of::<INPUT>() as i32);
        }
        thread::sleep(Duration::from_millis(10));
    }
    Ok(())
}

// ═══ BMP WRITER ══════════════════════════════════════════════════════════════
// Minimal BMP writer — no external crate. 24-bit BGR, bottom-up.

fn write_bmp(path: &Path, width: i32, height: i32, _row_bytes: i32, pixels: &[u8]) -> UiaResult<()> {
    let data_size = pixels.len() as u32;
    let file_size = 14 + 40 + data_size;

    let mut buf = Vec::with_capacity(file_size as usize);

    // BMP file header (14 bytes)
    buf.extend_from_slice(b"BM");
    buf.extend_from_slice(&file_size.to_le_bytes());
    buf.extend_from_slice(&[0u8; 4]); // reserved
    buf.extend_from_slice(&54u32.to_le_bytes()); // pixel data offset

    // DIB header (40 bytes — BITMAPINFOHEADER)
    buf.extend_from_slice(&40u32.to_le_bytes());
    buf.extend_from_slice(&width.to_le_bytes());
    buf.extend_from_slice(&height.to_le_bytes());
    buf.extend_from_slice(&1u16.to_le_bytes()); // planes
    buf.extend_from_slice(&24u16.to_le_bytes()); // bits per pixel
    buf.extend_from_slice(&0u32.to_le_bytes()); // compression (BI_RGB)
    buf.extend_from_slice(&data_size.to_le_bytes());
    buf.extend_from_slice(&2835u32.to_le_bytes()); // h pixels/meter
    buf.extend_from_slice(&2835u32.to_le_bytes()); // v pixels/meter
    buf.extend_from_slice(&0u32.to_le_bytes()); // colors used
    buf.extend_from_slice(&0u32.to_le_bytes()); // important colors

    // Pixel data (already bottom-up BGR from GetDIBits)
    buf.extend_from_slice(pixels);

    std::fs::write(path, buf)?;
    Ok(())
}