win-auto-utils 0.2.6

Universal Windows automation utilities with memory, window, input, and color operations
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
//! Window management module
//!
//! Provides window enumeration, lookup, and manipulation functionality using Windows User32 API.
//!
//! # Features
//! - Find window handles by process ID
//! - Find window handles by title with flexible filtering
//! - Enumerate all windows for a specific process
//! - Advanced window filtering with include/exclude patterns
//!
//! # Window Filtering
//! The module supports flexible window filtering using `(String, u8)` tuples where:
//! - `String`: The pattern to match against window titles
//! - `u8` (mask): Filter mode
//!   - `1`: Window title MUST contain this pattern
//!   - `0`: Window title MUST NOT contain this pattern
//!
//! Multiple filters can be combined, and ALL filters must match for a window to be included.
//!
//! # Example
//! ```no_run
//! use win_auto_utils::window::get_hwnd_list_filtered;
//!
//! // Get windows that contain "Notepad" but don't contain "Untitled"
//! let filters = vec![
//!     ("Notepad".to_string(), 1),    // Must contain "Notepad"
//!     ("Untitled".to_string(), 0),   // Must NOT contain "Untitled"
//! ];
//! let hwnds = get_hwnd_list_filtered(None, Some(&filters));
//! ```

use windows::{
    core::BOOL,
    Win32::{
        Foundation::{HWND, LPARAM},
        UI::WindowsAndMessaging::{
            EnumWindows, GetWindowTextW, GetWindowThreadProcessId, IsWindowVisible,
        },
    },
};

/// Callback function for EnumWindows - collects all window handles
pub extern "system" fn enum_windows_callback(hwnd: HWND, lparam: LPARAM) -> BOOL {
    unsafe {
        let list = lparam.0 as *mut Vec<HWND>;
        (*list).push(hwnd);
        BOOL(1)
    }
}

/// Get window title as UTF-16 String
fn get_window_title(hwnd: HWND) -> String {
    let mut buffer = [0u16; 256];
    let length = unsafe { GetWindowTextW(hwnd, &mut buffer) };

    if length == 0 {
        return String::new();
    }

    String::from_utf16(&buffer[..length as usize]).unwrap_or_default()
}

/// Get process ID from window handle
fn get_pid_by_hwnd(hwnd: HWND) -> u32 {
    let mut pid = 0u32;
    unsafe {
        GetWindowThreadProcessId(hwnd, Some(&mut pid));
    }
    pid
}

/// Get all window handles
///
/// Enumerates all top-level windows on the screen.
///
/// # Returns
/// A vector of all window handles
///
/// # Example
/// ```no_run
/// use win_auto_utils::window::get_hwnd_list;
///
/// let hwnds = get_hwnd_list();
/// println!("Found {} windows", hwnds.len());
/// ```
pub fn get_hwnd_list() -> Vec<HWND> {
    let mut hwnds: Vec<HWND> = Vec::new();
    unsafe {
        let _ = EnumWindows(
            Some(enum_windows_callback),
            LPARAM(&mut hwnds as *mut Vec<HWND> as isize),
        );
    }
    hwnds
}

/// Get the main window handle by process ID
///
/// This function enumerates all windows and returns the first visible window
/// that belongs to the specified process and has a non-empty title.
///
/// # Arguments
/// * `pid` - The process ID to search for
///
/// # Returns
/// * `Some(HWND)` - The window handle if found
/// * `None` - If no window is found for the given PID
///
/// # Example
/// ```no_run
/// use win_auto_utils::snapshot::find_pid_by_name;
/// use win_auto_utils::window::get_hwnd_by_pid;
///
/// if let Some(pid) = find_pid_by_name("notepad.exe") {
///     if let Some(hwnd) = get_hwnd_by_pid(pid) {
///         println!("Found Notepad window: {:?}", hwnd);
///     }
/// }
/// ```
pub fn get_hwnd_by_pid(pid: u32) -> Option<HWND> {
    let hwnd = get_hwnd_list().into_iter().find(|&hwnd| {
        let cur_pid = get_pid_by_hwnd(hwnd);
        let title = get_window_title(hwnd);
        cur_pid == pid && !title.is_empty()
    });

    hwnd.filter(|h| h.0 != std::ptr::null_mut())
}

/// Get window handle by window title
///
/// Searches for a window whose title contains the specified string.
/// The search is case-insensitive.
///
/// # Arguments
/// * `title` - The window title to search for (partial match supported)
///
/// # Returns
/// * `Some(HWND)` - The window handle if found
/// * `None` - If no matching window is found
///
/// # Example
/// ```no_run
/// use win_auto_utils::window::get_hwnd_by_title;
///
/// // Find Notepad window by title
/// if let Some(hwnd) = get_hwnd_by_title("Notepad") {
///     println!("Found window: {:?}", hwnd);
/// }
///
/// // Find window with exact title
/// if let Some(hwnd) = get_hwnd_by_title("Untitled - Notepad") {
///     println!("Found specific window: {:?}", hwnd);
/// }
/// ```
pub fn get_hwnd_by_title(title: &str) -> Option<HWND> {
    let title_lower = title.to_lowercase();

    let hwnd = get_hwnd_list().into_iter().find(|&hwnd| {
        let window_title = get_window_title(hwnd);
        window_title.to_lowercase().contains(&title_lower)
    });

    hwnd.filter(|h| h.0 != std::ptr::null_mut())
}

/// Get all window handles for a specific process
///
/// Enumerates all windows belonging to the specified process.
/// This includes both visible and invisible windows.
///
/// # Arguments
/// * `pid` - The process ID to enumerate windows for
///
/// # Returns
/// A vector of window handles. Empty if no windows found.
///
/// # Example
/// ```no_run
/// use win_auto_utils::snapshot::find_pid_by_name;
/// use win_auto_utils::window::get_hwnd_list_by_pid;
///
/// if let Some(pid) = find_pid_by_name("chrome.exe") {
///     let hwnds = get_hwnd_list_by_pid(pid);
///     println!("Chrome has {} windows", hwnds.len());
///     
///     for (i, hwnd) in hwnds.iter().enumerate() {
///         println!("  Window {}: {:?}", i, hwnd);
///     }
/// }
/// ```
pub fn get_hwnd_list_by_pid(pid: u32) -> Vec<HWND> {
    get_hwnd_list()
        .into_iter()
        .filter(|&hwnd| get_pid_by_hwnd(hwnd) == pid)
        .collect()
}

/// Get window handle by PID with flexible title filtering
///
/// Finds the first window belonging to the specified process that matches
/// all the provided title filters. Filters use an include/exclude mechanism:
/// - Mask 1: Window title MUST contain the pattern
/// - Mask 0: Window title MUST NOT contain the pattern
/// All filters must match for a window to be selected.
///
/// # Arguments
/// * `pid` - The process ID to search for
/// * `filters` - Vector of (pattern, mask) tuples for title filtering
///
/// # Returns
/// * `Some(HWND)` - The first matching window handle
/// * `None` - If no matching window is found
///
/// # Example
/// ```no_run
/// use win_auto_utils::snapshot::find_pid;
/// use win_auto_utils::window::get_hwnd_by_pid_and_filter;
///
/// if let Some(pid) = find_pid("chrome.exe") {
///     // Find Chrome windows containing "Google" but not "Incognito"
///     let filters = vec![
///         ("Google".to_string(), 1),      // Must contain "Google"
///         ("Incognito".to_string(), 0),   // Must NOT contain "Incognito"
///     ];
///     
///     if let Some(hwnd) = get_hwnd_by_pid_and_filter(pid, &filters) {
///         println!("Found filtered window: {:?}", hwnd);
///     }
/// }
/// ```
pub fn get_hwnd_by_pid_and_filter(pid: u32, filters: &Vec<WindowTitleFilter>) -> Option<HWND> {
    get_hwnd_list()
        .into_iter()
        .find(|&hwnd| {
            // First check if this window belongs to the target process
            if get_pid_by_hwnd(hwnd) != pid {
                return false;
            }

            // Then apply all title filters
            let window_title_lower = get_window_title(hwnd).to_lowercase();

            // All filters must match
            for (pattern, mask) in filters {
                let pattern_lower = pattern.to_lowercase();
                let contains_pattern = window_title_lower.contains(&pattern_lower);

                // Mask 1: must contain, Mask 0: must NOT contain
                let matches = if *mask == 1 {
                    contains_pattern
                } else {
                    !contains_pattern
                };

                if !matches {
                    return false;
                }
            }

            true
        })
        .filter(|h| h.0 != std::ptr::null_mut())
}

/// Type alias for window title filter
/// - String: The pattern to match
/// - u8: Filter mask (1 = must contain, 0 = must NOT contain)
pub type WindowTitleFilter = (String, u8);

/// Get all window handles with optional filtering
///
/// Advanced function that allows filtering by both PID and multiple title patterns.
/// Title filters use a flexible include/exclude mechanism:
/// - Mask 1: Window title MUST contain the pattern
/// - Mask 0: Window title MUST NOT contain the pattern
/// All filters must match for a window to be included.
///
/// # Arguments
/// * `pid` - Optional process ID filter (None means all processes)
/// * `title_filters` - Optional vector of (pattern, mask) tuples for title filtering
///
/// # Returns
/// A vector of window handles matching all criteria
///
/// # Example
/// ```no_run
/// use win_auto_utils::window::get_hwnd_list_filtered;
///
/// // Get windows that contain "Notepad" but don't contain "Untitled"
/// let filters = vec![
///     ("Notepad".to_string(), 1),    // Must contain "Notepad"
///     ("Untitled".to_string(), 0),   // Must NOT contain "Untitled"
/// ];
/// let hwnds = get_hwnd_list_filtered(None, Some(&filters));
///
/// // Get all windows from a specific process
/// let all_process_windows = get_hwnd_list_filtered(Some(12345), None);
///
/// // Complex filtering: Chrome windows with "Google" but not "Incognito"
/// let chrome_filters = vec![
///     ("Google".to_string(), 1),
///     ("Incognito".to_string(), 0),
/// ];
/// let filtered_chrome = get_hwnd_list_filtered(Some(chrome_pid), Some(&chrome_filters));
/// ```
pub fn get_hwnd_list_filtered(
    pid: Option<u32>,
    title_filters: Option<&Vec<WindowTitleFilter>>,
) -> Vec<HWND> {
    get_hwnd_list()
        .into_iter()
        .filter(|&hwnd| {
            // Filter by PID if specified
            if let Some(target_pid) = pid {
                if get_pid_by_hwnd(hwnd) != target_pid {
                    return false;
                }
            }

            // Filter by title patterns if specified
            if let Some(filters) = title_filters {
                let window_title_lower = get_window_title(hwnd).to_lowercase();

                // All filters must match
                for (pattern, mask) in filters {
                    let pattern_lower = pattern.to_lowercase();
                    let contains_pattern = window_title_lower.contains(&pattern_lower);

                    // Mask 1: must contain, Mask 0: must NOT contain
                    let matches = if *mask == 1 {
                        contains_pattern
                    } else {
                        !contains_pattern
                    };

                    if !matches {
                        return false;
                    }
                }
            }

            true
        })
        .collect()
}

/// Check if a window is valid and exists
///
/// # Arguments
/// * `hwnd` - The window handle to validate
///
/// # Returns
/// `true` if the window exists and is valid
pub fn is_valid_window(hwnd: HWND) -> bool {
    hwnd.0 != std::ptr::null_mut()
}

/// Check if a window is visible
///
/// # Arguments
/// * `hwnd` - The window handle to check
///
/// # Returns
/// `true` if the window is visible
pub fn is_window_visible(hwnd: HWND) -> bool {
    if !is_valid_window(hwnd) {
        return false;
    }

    unsafe { IsWindowVisible(hwnd).as_bool() }
}

/// Get window title (for testing purposes)
#[cfg(test)]
pub fn get_window_title_for_test(hwnd: HWND) -> String {
    let mut buffer = [0u16; 256];
    let length = unsafe { GetWindowTextW(hwnd, &mut buffer) };

    if length == 0 {
        return String::new();
    }

    String::from_utf16(&buffer[..length as usize]).unwrap_or_default()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_get_hwnd_by_title_nonexistent() {
        // Search for a window title that definitely doesn't exist
        let result = get_hwnd_by_title("definitely_not_existing_window_xyz_12345");
        assert!(result.is_none());
    }

    #[test]
    fn test_is_valid_window() {
        // Invalid HWND
        assert!(!is_valid_window(HWND::default()));
        assert!(!is_valid_window(HWND(std::ptr::null_mut())));

        // Note: We can't easily test valid HWNDs without an actual window
    }

    #[test]
    fn test_get_hwnd_list_not_empty() {
        // Get all windows (should have at least some)
        let hwnds = get_hwnd_list();

        // There should be at least some windows on a running system
        // (taskbar, desktop, etc.)
        println!("Total windows found: {}", hwnds.len());
        assert!(!hwnds.is_empty(), "Should find at least some windows");
    }

    #[test]
    fn test_get_hwnd_list_by_nonexistent_pid() {
        // Use a PID that's unlikely to exist
        let hwnds = get_hwnd_list_by_pid(999999);
        assert!(
            hwnds.is_empty(),
            "Should not find windows for non-existent PID"
        );
    }

    #[test]
    fn test_window_title_retrieval() {
        // Get some windows and check that we can retrieve their titles
        let hwnds = get_hwnd_list();

        for hwnd in hwnds.iter().take(5) {
            let title = get_window_title(*hwnd);
            let pid = get_pid_by_hwnd(*hwnd);
            // Title might be empty for some windows, that's okay
            println!("PID: {}, Window {:?}: '{}'", pid, hwnd, title);
        }
    }

    #[test]
    fn test_get_hwnd_list_filtered_no_filter() {
        // Get all windows with no filters
        let hwnds = get_hwnd_list_filtered(None, None);
        assert!(!hwnds.is_empty());
    }
}