libobs_window_helper/
lib.rs

1//! # OBS Window Helper
2//! This crate provides necessary information about windows that could be used
3//! so they can be captured with the `window_capture` or `game_capture` source in OBS.
4//! <br> The function you probably want to use is `get_all_windows` which returns a list of `WindowInfo` structs.
5
6#[cfg(not(windows))]
7compile_error!("This library only supports windows!");
8
9#[cfg(not(target_pointer_width = "64"))]
10compile_error!("compilation is only allowed for 64-bit targets");
11
12mod game;
13mod monitor;
14mod util;
15mod window;
16
17pub use util::*;
18#[cfg(test)]
19mod test;
20
21pub use game::*;
22pub use helper::*;
23use win_iterator::{first_window, next_window};
24use windows::Win32::{Foundation::HWND, System::Console::GetConsoleWindow};
25
26/// Retrieves information about all windows based on the specified search mode and game check flag.
27///
28/// # Arguments
29///
30/// * `mode` - The search mode to use for window enumeration.
31/// * `check_game` - A flag indicating wether a `game_capture` or a `window_capture` is used
32///
33/// # Returns
34///
35/// A `Result` containing a vector of `WindowInfo` structs representing the retrieved window information, or an `anyhow::Error` if an error occurs.
36pub fn get_all_windows(mode: WindowSearchMode) -> anyhow::Result<Vec<WindowInfo>> {
37    let mut use_find_window_ex = false;
38
39    let mut parent = None as Option<HWND>;
40    let window = unsafe { first_window(mode, &mut parent, &mut use_find_window_ex)? };
41    let mut window = Some(window);
42
43    let curr = unsafe { GetConsoleWindow() };
44
45    let mut out = Vec::new();
46    while window.is_some_and(|e| !e.is_invalid()) {
47        let w = window.unwrap();
48        if curr != w {
49            let res = get_window_info(w);
50            if let Ok(info) = res {
51                out.push(info);
52            } else {
53                //eprintln!("Error: {:?}", res.err().unwrap());
54            }
55        }
56
57        unsafe {
58            window = next_window(window, mode, &mut parent, use_find_window_ex)?;
59        }
60    }
61
62    Ok(out)
63}