wdotool 0.4.0

xdotool-compatible automation for Wayland
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
mod cli;
mod diag;

use std::time::Duration;

use clap::Parser;
use tracing_subscriber::EnvFilter;

use regex::Regex;

use wdotool_core::detector::{self, BackendKind, Environment};
use wdotool_core::keysym;
use wdotool_core::{
    Backend, KeyDirection, MouseButton, Result, WdoError, WindowGeometry, WindowId, WindowInfo,
};

use cli::{Cli, Command};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let cli = Cli::parse();
    init_tracing(cli.verbose);

    // Diag has to short-circuit before detector::build so the probes
    // never touch the portal session (which would pop a consent dialog
    // for libei users, exactly the surprise diag is meant to remove).
    if let Command::Diag { json, copy } = cli.command {
        let format = if json {
            diag::DiagFormat::Json
        } else {
            diag::DiagFormat::Markdown
        };
        return diag::run(format, copy);
    }

    let env = Environment::detect();

    let forced = match cli.backend.as_deref() {
        Some(s) => Some(BackendKind::parse(s).ok_or_else(|| {
            WdoError::InvalidArg(format!(
                "unknown backend '{s}' (expected libei, wlroots, kde, gnome, uinput)"
            ))
        })?),
        None => None,
    };

    let backend = detector::build(&env, forced).await?;

    dispatch(&*backend, &env, cli.command).await?;
    Ok(())
}

async fn dispatch(backend: &dyn Backend, env: &Environment, cmd: Command) -> Result<()> {
    match cmd {
        Command::Capabilities => {
            let value = wdotool_core::capabilities::report_json(env, backend);
            // pretty for humans tailing the output; consumers parse
            // either form fine.
            let pretty = serde_json::to_string_pretty(&value)
                .map_err(|e| WdoError::InvalidArg(format!("capabilities serialization: {e}")))?;
            println!("{pretty}");
        }
        Command::Info => {
            let caps = backend.capabilities();
            println!("backend:  {}", backend.name());
            println!("desktop:  {:?}", env.desktop);
            println!("session:  {:?}", env.session_type);
            println!("display:  {:?}", env.wayland_display);
            println!("hints:    {:?}", env.compositor_hints);
            println!("wayland:  {}", env.is_wayland());
            println!("capabilities:");
            println!("  key_input:             {}", caps.key_input);
            println!("  text_input:            {}", caps.text_input);
            println!("  pointer_move_absolute: {}", caps.pointer_move_absolute);
            println!("  pointer_move_relative: {}", caps.pointer_move_relative);
            println!("  pointer_button:        {}", caps.pointer_button);
            println!("  scroll:                {}", caps.scroll);
            println!("  list_windows:          {}", caps.list_windows);
            println!("  active_window:         {}", caps.active_window);
            println!("  activate_window:       {}", caps.activate_window);
            println!("  close_window:          {}", caps.close_window);
            println!("  pointer_position:      {}", caps.pointer_position);
            println!("  list_outputs:          {}", caps.list_outputs);
            println!("  window_geometry:       {}", caps.window_geometry);
        }
        Command::Key {
            clearmodifiers,
            chain,
        } => {
            if clearmodifiers {
                clear_modifiers(backend).await;
            }
            run_key(backend, &chain, KeyDirection::PressRelease).await?;
        }
        Command::Keydown {
            clearmodifiers,
            chain,
        } => {
            if clearmodifiers {
                clear_modifiers(backend).await;
            }
            run_key(backend, &chain, KeyDirection::Press).await?;
        }
        Command::Keyup {
            clearmodifiers,
            chain,
        } => {
            if clearmodifiers {
                clear_modifiers(backend).await;
            }
            run_key(backend, &chain, KeyDirection::Release).await?;
        }
        Command::Type {
            delay,
            file,
            clearmodifiers,
            text,
        } => {
            let resolved = resolve_type_input(file, text)?;
            if clearmodifiers {
                clear_modifiers(backend).await;
            }
            backend
                .type_text(&resolved, Duration::from_millis(delay))
                .await?;
        }
        Command::Mousemove {
            relative,
            output,
            x,
            y,
        } => {
            // --output translates output-local coordinates to global
            // before calling mouse_move_absolute. clap already rejects
            // --output combined with --relative.
            let (target_x, target_y) = match output {
                Some(name) => {
                    let outputs = backend.list_outputs().await?;
                    if outputs.is_empty() {
                        return Err(WdoError::InvalidArg(format!(
                            "--output not supported: the {} backend does not enumerate outputs",
                            backend.name()
                        )));
                    }
                    let target = outputs.iter().find(|o| o.name == name).ok_or_else(|| {
                        let available: Vec<&str> =
                            outputs.iter().map(|o| o.name.as_str()).collect();
                        WdoError::InvalidArg(format!(
                            "no output named {name:?}; available: {}",
                            available.join(", ")
                        ))
                    })?;
                    (target.x + x, target.y + y)
                }
                None => (x, y),
            };
            backend.mouse_move(target_x, target_y, !relative).await?;
        }
        Command::Click { button } => {
            backend
                .mouse_button(MouseButton::from_index(button), KeyDirection::PressRelease)
                .await?;
        }
        Command::Mousedown { button } => {
            backend
                .mouse_button(MouseButton::from_index(button), KeyDirection::Press)
                .await?;
        }
        Command::Mouseup { button } => {
            backend
                .mouse_button(MouseButton::from_index(button), KeyDirection::Release)
                .await?;
        }
        Command::Scroll { dx, dy } => {
            backend.scroll(dx, dy).await?;
        }
        Command::Search {
            name,
            class,
            pid,
            regex,
            ignore_case,
            any,
            all: _,
        } => {
            let windows = backend.list_windows().await?;
            let filters = SearchFilters::compile(SearchFlags {
                name: name.as_deref(),
                class: class.as_deref(),
                pid,
                regex,
                ignore_case,
                any,
            })?;
            let mut matched = false;
            for w in windows.iter().filter(|w| filters.matches(w)) {
                println!("{}\t{}", w.id, w.title);
                matched = true;
            }
            // xdotool exits 1 when nothing matched; preserve that for
            // shell scripts that branch on `if wdotool search ...`.
            if !matched {
                std::process::exit(1);
            }
        }
        Command::Getactivewindow => match backend.active_window().await? {
            Some(w) => println!("{}", w.id),
            None => return Err(WdoError::WindowNotFound("active".into())),
        },
        Command::Outputs { json } => {
            let outputs = backend.list_outputs().await?;
            if json {
                let value = serde_json::to_value(
                    outputs
                        .iter()
                        .map(|o| {
                            serde_json::json!({
                                "name": o.name,
                                "x": o.x,
                                "y": o.y,
                                "width": o.width,
                                "height": o.height,
                                "scale": o.scale,
                            })
                        })
                        .collect::<Vec<_>>(),
                )
                .map_err(|e| WdoError::InvalidArg(format!("outputs serialization: {e}")))?;
                let pretty = serde_json::to_string_pretty(&value)
                    .map_err(|e| WdoError::InvalidArg(format!("outputs serialization: {e}")))?;
                println!("{pretty}");
            } else {
                // Tab-separated, header row first. Mirrors `wdotool search`'s
                // shape so shell scripts can `awk -F'\t'` on either.
                println!("name\tx\ty\twidth\theight\tscale");
                for o in &outputs {
                    println!(
                        "{}\t{}\t{}\t{}\t{}\t{}",
                        o.name, o.x, o.y, o.width, o.height, o.scale
                    );
                }
            }
        }
        Command::Getmouselocation => match backend.pointer_position().await? {
            Some((x, y)) => println!("x:{x} y:{y}"),
            None => {
                eprintln!(
                    "wdotool: pointer position is unreadable on the {} backend (Wayland \
                     virtual-pointer protocols are send-only). Use the kde or gnome backend, \
                     or your compositor's IPC (hyprctl cursorpos, swaymsg get_seats).",
                    backend.name()
                );
                std::process::exit(1);
            }
        },
        Command::Windowactivate { id } => backend.activate_window(&WindowId(id)).await?,
        Command::Windowclose { id } => backend.close_window(&WindowId(id)).await?,
        Command::Getwindowname { id } => {
            let w = find_window(backend, &id).await?;
            println!("{}", w.title);
        }
        Command::Getwindowpid { id } => {
            let w = find_window(backend, &id).await?;
            match w.pid {
                Some(pid) => println!("{pid}"),
                None => {
                    eprintln!("wdotool: pid not available for window {id}");
                    std::process::exit(1);
                }
            }
        }
        Command::Getwindowclassname { id } => {
            let w = find_window(backend, &id).await?;
            match w.app_id {
                Some(app_id) => println!("{app_id}"),
                None => {
                    eprintln!("wdotool: classname (app_id) not available for window {id}");
                    std::process::exit(1);
                }
            }
        }
        Command::Getwindowgeometry { id } => {
            // Trait contract:
            //   Ok(Some(geom)) -> backend supports it, found, here it is
            //   Err(WindowNotFound) -> backend supports it, but no
            //                          window with that id
            //   Ok(None) -> backend doesn't support reading geometry
            // The error path bubbles via `?` so we only handle
            // Ok(Some) and Ok(None) explicitly here.
            match backend.window_geometry(&WindowId(id.clone())).await? {
                Some(WindowGeometry {
                    x,
                    y,
                    width,
                    height,
                }) => {
                    // Match xdotool's default format. The "screen"
                    // line xdotool prints doesn't translate to Wayland
                    // (compositors don't expose a stable screen index
                    // that's meaningful to clients), so we drop it.
                    println!("Window {id}");
                    println!("  Position: {x},{y}");
                    println!("  Geometry: {width}x{height}");
                }
                None => {
                    eprintln!(
                        "wdotool: window geometry is unreadable on the {} backend (no Wayland \
                         protocol exposes window geometry to other clients). Use the kde or \
                         gnome backend.",
                        backend.name()
                    );
                    std::process::exit(1);
                }
            }
        }
        Command::Diag { .. } => {
            // Handled in main() before dispatch is called so diag never
            // bootstraps a backend.
            unreachable!("Diag short-circuits before dispatch");
        }
    }
    Ok(())
}

/// Look up a window by its id string. Used by the `getwindow*` commands
/// which all need to resolve an id to a `WindowInfo` before reading a
/// single field. Returns `WindowNotFound` if no window in the current
/// list has that id, which xdotool also signals via non-zero exit.
async fn find_window(backend: &dyn Backend, id: &str) -> Result<WindowInfo> {
    let windows = backend.list_windows().await?;
    windows
        .into_iter()
        .find(|w| w.id.0 == id)
        .ok_or_else(|| WdoError::WindowNotFound(id.to_string()))
}

/// Approximates xdotool's --clearmodifiers. Wayland doesn't let a normal
/// client query the compositor's current modifier state, so we can't do the
/// "save + restore" dance xdotool does. Best effort: release every standard
/// modifier unconditionally, ignoring backend errors per-key (a modifier
/// that isn't in the keymap is a no-op, not a user-visible failure).
async fn clear_modifiers(backend: &dyn Backend) {
    const STANDARD_MODIFIERS: &[&str] = &[
        "Control_L",
        "Control_R",
        "Shift_L",
        "Shift_R",
        "Alt_L",
        "Alt_R",
        "Super_L",
        "Super_R",
        "ISO_Level3_Shift",
    ];
    for sym in STANDARD_MODIFIERS {
        let _ = backend.key(sym, KeyDirection::Release).await;
    }
}

/// Resolve the text to type: from --file (path or `-` for stdin) or the
/// positional argument. clap enforces mutual exclusion; this function just
/// dispatches and errors if neither source is present.
fn resolve_type_input(file: Option<String>, text: Option<String>) -> Result<String> {
    use std::io::Read;
    match (file, text) {
        (Some(path), _) => {
            if path == "-" {
                let mut buf = String::new();
                std::io::stdin()
                    .read_to_string(&mut buf)
                    .map_err(|e| WdoError::InvalidArg(format!("failed to read stdin: {e}")))?;
                Ok(buf)
            } else {
                std::fs::read_to_string(&path)
                    .map_err(|e| WdoError::InvalidArg(format!("failed to read {path}: {e}")))
            }
        }
        (None, Some(t)) => Ok(t),
        (None, None) => Err(WdoError::InvalidArg(
            "type requires either --file <path> or a positional text argument".into(),
        )),
    }
}

// Press modifiers, then the key, then release in reverse — matches xdotool
// ordering so scripts relying on this behaviour continue to work.
async fn run_key(backend: &dyn Backend, chain: &str, dir: KeyDirection) -> Result<()> {
    let parsed = keysym::parse_chain(chain)?;
    match dir {
        KeyDirection::Press => {
            for m in &parsed.modifiers {
                backend.key(m, KeyDirection::Press).await?;
            }
            backend.key(&parsed.key, KeyDirection::Press).await?;
        }
        KeyDirection::Release => {
            backend.key(&parsed.key, KeyDirection::Release).await?;
            for m in parsed.modifiers.iter().rev() {
                backend.key(m, KeyDirection::Release).await?;
            }
        }
        KeyDirection::PressRelease => {
            for m in &parsed.modifiers {
                backend.key(m, KeyDirection::Press).await?;
            }
            backend.key(&parsed.key, KeyDirection::PressRelease).await?;
            for m in parsed.modifiers.iter().rev() {
                backend.key(m, KeyDirection::Release).await?;
            }
        }
    }
    Ok(())
}

fn init_tracing(verbose: bool) {
    let default = if verbose {
        "wdotool=debug"
    } else {
        "wdotool=info,warn"
    };
    let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(default));
    let _ = tracing_subscriber::fmt()
        .with_env_filter(filter)
        .with_target(false)
        .with_writer(std::io::stderr)
        .try_init();
}

/// Inputs to [`SearchFilters::compile`]; mirrors the CLI flags so the
/// filter compilation is testable without round-tripping through clap.
struct SearchFlags<'a> {
    name: Option<&'a str>,
    class: Option<&'a str>,
    pid: Option<u32>,
    regex: bool,
    ignore_case: bool,
    any: bool,
}

/// Compiled search predicates. Built once per `wdotool search` call and
/// then applied to each window. Holding the regex(es) here avoids
/// recompiling per-window.
struct SearchFilters {
    name: Option<Regex>,
    class: Option<Regex>,
    pid: Option<u32>,
    /// True when `--any` was passed: matching switches from AND to OR.
    any: bool,
}

impl SearchFilters {
    fn compile(flags: SearchFlags<'_>) -> Result<Self> {
        let make_regex = |pat: &str, field: &'static str| -> Result<Regex> {
            // Without --regex, escape so substring patterns are
            // taken literally. With --ignore-case, prefix with the
            // (?i) inline flag; works in both modes uniformly.
            let body = if flags.regex {
                pat.to_string()
            } else {
                regex::escape(pat)
            };
            let full = if flags.ignore_case {
                format!("(?i){body}")
            } else {
                body
            };
            Regex::new(&full)
                .map_err(|e| WdoError::InvalidArg(format!("invalid {field} pattern {pat:?}: {e}")))
        };
        Ok(Self {
            name: flags.name.map(|p| make_regex(p, "--name")).transpose()?,
            class: flags.class.map(|p| make_regex(p, "--class")).transpose()?,
            pid: flags.pid,
            any: flags.any,
        })
    }

    fn matches(&self, w: &WindowInfo) -> bool {
        if self.any {
            self.matches_any(w)
        } else {
            self.matches_all(w)
        }
    }

    /// AND semantics (default): every set filter must match.
    fn matches_all(&self, w: &WindowInfo) -> bool {
        if let Some(re) = &self.name {
            if !re.is_match(&w.title) {
                return false;
            }
        }
        if let Some(re) = &self.class {
            // app_id is the Wayland equivalent of WM_CLASS. Backends
            // that don't expose it (uinput, bare libei) can't match
            // here at all, which is correct.
            match w.app_id.as_deref() {
                Some(a) if re.is_match(a) => {}
                _ => return false,
            }
        }
        if let Some(p) = self.pid {
            if w.pid != Some(p) {
                return false;
            }
        }
        true
    }

    /// OR semantics (`--any`): at least one set filter must match.
    /// With zero set filters, falls back to "match everything" so that
    /// `wdotool search --any` (no filters) lists all windows, same as
    /// `wdotool search` does.
    fn matches_any(&self, w: &WindowInfo) -> bool {
        let any_set = self.name.is_some() || self.class.is_some() || self.pid.is_some();
        if !any_set {
            return true;
        }
        if let Some(re) = &self.name {
            if re.is_match(&w.title) {
                return true;
            }
        }
        if let Some(re) = &self.class {
            if let Some(a) = w.app_id.as_deref() {
                if re.is_match(a) {
                    return true;
                }
            }
        }
        if let Some(p) = self.pid {
            if w.pid == Some(p) {
                return true;
            }
        }
        false
    }
}

#[cfg(test)]
mod search_tests {
    use super::*;
    use wdotool_core::WindowId;

    fn win(id: &str, title: &str, app_id: Option<&str>, pid: Option<u32>) -> WindowInfo {
        WindowInfo {
            id: WindowId(id.into()),
            title: title.into(),
            app_id: app_id.map(str::to_string),
            pid,
        }
    }

    fn flags<'a>(
        name: Option<&'a str>,
        class: Option<&'a str>,
        pid: Option<u32>,
    ) -> SearchFlags<'a> {
        SearchFlags {
            name,
            class,
            pid,
            regex: false,
            ignore_case: false,
            any: false,
        }
    }

    #[test]
    fn substring_name_match_is_default() {
        let f = SearchFilters::compile(flags(Some("fox"), None, None)).unwrap();
        assert!(f.matches(&win("1", "Firefox", None, None)));
        assert!(!f.matches(&win("2", "Chromium", None, None)));
    }

    #[test]
    fn dot_in_pattern_is_escaped_without_regex_flag() {
        // With --regex off, `Fire.fox` only matches the literal string,
        // not `Fire?fox` (which a regex would match).
        let f = SearchFilters::compile(flags(Some("Fire.fox"), None, None)).unwrap();
        assert!(!f.matches(&win("1", "Firefox", None, None)));
        assert!(f.matches(&win("2", "Fire.fox Browser", None, None)));
    }

    #[test]
    fn regex_flag_enables_pattern_semantics() {
        let f = SearchFilters::compile(SearchFlags {
            name: Some("Fire.*x"),
            class: None,
            pid: None,
            regex: true,
            ignore_case: false,
            any: false,
        })
        .unwrap();
        assert!(f.matches(&win("1", "Firefox", None, None)));
        assert!(!f.matches(&win("2", "Chromium", None, None)));
    }

    #[test]
    fn ignore_case_works_in_substring_mode() {
        let f = SearchFilters::compile(SearchFlags {
            name: Some("FIREFOX"),
            class: None,
            pid: None,
            regex: false,
            ignore_case: true,
            any: false,
        })
        .unwrap();
        assert!(f.matches(&win("1", "Mozilla Firefox", None, None)));
    }

    #[test]
    fn ignore_case_works_in_regex_mode() {
        let f = SearchFilters::compile(SearchFlags {
            name: Some("FIRE.*X"),
            class: None,
            pid: None,
            regex: true,
            ignore_case: true,
            any: false,
        })
        .unwrap();
        assert!(f.matches(&win("1", "Mozilla Firefox", None, None)));
    }

    #[test]
    fn class_filter_matches_app_id() {
        let f = SearchFilters::compile(flags(None, Some("firefox"), None)).unwrap();
        assert!(f.matches(&win("1", "Some Page", Some("org.mozilla.firefox"), None)));
        assert!(!f.matches(&win("2", "kitty", Some("kitty"), None)));
    }

    #[test]
    fn class_filter_skips_windows_without_app_id() {
        let f = SearchFilters::compile(flags(None, Some("anything"), None)).unwrap();
        // app_id None means the backend doesn't expose it (uinput,
        // bare libei). Such windows can never satisfy a class filter.
        assert!(!f.matches(&win("1", "Some Page", None, None)));
    }

    #[test]
    fn pid_filter_requires_exact_match() {
        let f = SearchFilters::compile(flags(None, None, Some(1234))).unwrap();
        assert!(f.matches(&win("1", "Firefox", None, Some(1234))));
        assert!(!f.matches(&win("2", "Firefox", None, Some(5678))));
        // Backends that don't populate pid never match a pid filter.
        assert!(!f.matches(&win("3", "Firefox", None, None)));
    }

    #[test]
    fn filters_are_anded_together() {
        let f =
            SearchFilters::compile(flags(Some("Firefox"), Some("mozilla"), Some(1234))).unwrap();
        assert!(f.matches(&win(
            "1",
            "Firefox - Wikipedia",
            Some("org.mozilla.firefox"),
            Some(1234)
        )));
        // Right title + class but wrong pid: rejected.
        assert!(!f.matches(&win(
            "2",
            "Firefox - Wikipedia",
            Some("org.mozilla.firefox"),
            Some(5678)
        )));
    }

    #[test]
    fn no_filters_matches_everything() {
        let f = SearchFilters::compile(flags(None, None, None)).unwrap();
        assert!(f.matches(&win("1", "Anything", None, None)));
        assert!(f.matches(&win("2", "Else", Some("kitty"), Some(99))));
    }

    fn flags_any<'a>(
        name: Option<&'a str>,
        class: Option<&'a str>,
        pid: Option<u32>,
    ) -> SearchFlags<'a> {
        SearchFlags {
            name,
            class,
            pid,
            regex: false,
            ignore_case: false,
            any: true,
        }
    }

    #[test]
    fn any_matches_when_only_name_matches() {
        let f = SearchFilters::compile(flags_any(Some("Firefox"), Some("nope"), Some(99))).unwrap();
        // Title matches, class and pid don't. With AND this would be
        // rejected; with --any, name alone is enough.
        assert!(f.matches(&win(
            "1",
            "Firefox - Wikipedia",
            Some("org.mozilla.firefox"),
            Some(1234)
        )));
    }

    #[test]
    fn any_matches_when_only_class_matches() {
        let f = SearchFilters::compile(flags_any(Some("nope"), Some("firefox"), Some(99))).unwrap();
        assert!(f.matches(&win(
            "1",
            "Wikipedia",
            Some("org.mozilla.firefox"),
            Some(1234)
        )));
    }

    #[test]
    fn any_matches_when_only_pid_matches() {
        let f = SearchFilters::compile(flags_any(Some("nope"), Some("nope"), Some(1234))).unwrap();
        assert!(f.matches(&win(
            "1",
            "Wikipedia",
            Some("org.mozilla.firefox"),
            Some(1234)
        )));
    }

    #[test]
    fn any_rejects_when_no_filter_matches() {
        let f = SearchFilters::compile(flags_any(Some("nope"), Some("nope"), Some(99))).unwrap();
        assert!(!f.matches(&win(
            "1",
            "Wikipedia",
            Some("org.mozilla.firefox"),
            Some(1234)
        )));
    }

    #[test]
    fn any_with_no_filters_matches_everything() {
        // Same fall-through as default: zero filters lists all windows
        // regardless of which combinator was chosen.
        let f = SearchFilters::compile(flags_any(None, None, None)).unwrap();
        assert!(f.matches(&win("1", "Anything", None, None)));
        assert!(f.matches(&win("2", "Else", Some("kitty"), Some(99))));
    }

    #[test]
    fn any_with_class_filter_skips_window_without_app_id() {
        // app_id None can't satisfy a class regex; with --any and only
        // a class filter set, that means the window doesn't match.
        let f = SearchFilters::compile(flags_any(None, Some("anything"), None)).unwrap();
        assert!(!f.matches(&win("1", "Some Page", None, None)));
    }

    #[test]
    fn invalid_regex_pattern_returns_invalid_arg() {
        let result = SearchFilters::compile(SearchFlags {
            name: Some("[unclosed"),
            class: None,
            pid: None,
            regex: true,
            ignore_case: false,
            any: false,
        });
        match result {
            Err(WdoError::InvalidArg(msg)) => assert!(msg.contains("--name")),
            Err(other) => panic!("expected InvalidArg, got {other:?}"),
            Ok(_) => panic!("expected InvalidArg, got Ok"),
        }
    }
}