libobs_window_helper\util/
helper.rs1use anyhow::{anyhow, Result as AnyResult};
2use windows::{
3 core::{Error, Result as WinResult},
4 Win32::{Foundation::HWND, UI::WindowsAndMessaging::GetWindowThreadProcessId},
5};
6
7use crate::{
8 is_blacklisted_window,
9 monitor::get_monitor_id,
10 validators::is_microsoft_internal_exe,
11 window::{
12 get_command_line_args, get_exe, get_product_name, get_title, get_window_class,
13 hwnd_to_monitor, intersects_with_multiple_monitors,
14 },
15};
16
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18#[cfg_attr(feature = "specta", derive(specta::Type))]
19#[derive(Debug, Clone)]
20pub struct WindowInfo {
22 pub full_exe: String,
24 pub obs_id: String,
26 #[cfg(all(not(feature = "serde"), not(feature = "specta")))]
27 pub handle: HWND,
29 pub pid: u32,
31 pub title: Option<String>,
33 pub class: Option<String>,
35 pub product_name: Option<String>,
37 pub monitor: Option<String>,
39 pub intersects: Option<bool>,
41 pub cmd_line: Option<String>,
43 pub is_game: bool,
45}
46
47fn encode_string(s: &str) -> String {
48 s.replace("#", "#22").replace(":", "#3A")
49}
50
51pub fn get_window_info(wnd: HWND) -> AnyResult<WindowInfo> {
66 let (proc_id, full_exe) = get_exe(wnd)?;
67 let exe = full_exe
68 .file_name()
69 .ok_or(anyhow!("Failed to get file name"))?;
70 let exe = exe.to_str().ok_or(anyhow!("Failed to convert to str"))?;
71 let exe = exe.to_string();
72
73 if is_microsoft_internal_exe(&exe) {
74 return Err(anyhow!("Handle is a Microsoft internal exe"));
75 }
76
77 if exe == "obs64.exe" {
78 return Err(anyhow!("Handle is obs64.exe"));
79 }
80
81 let is_game = !is_blacklisted_window(&exe);
82
83 let title = get_title(wnd).ok();
84 let class = get_window_class(wnd).ok();
85
86 let product_name = get_product_name(&full_exe).ok();
87 let monitor = Some(hwnd_to_monitor(wnd)?);
88 let intersects = intersects_with_multiple_monitors(wnd).ok();
89 let cmd_line = get_command_line_args(wnd).ok();
90 let monitor_id = monitor.and_then(|e| get_monitor_id(e).ok());
91
92 let title_o = title.as_ref().map_or("", |v| v);
93 let class_o = class.as_ref().map_or("", |v| v);
94
95 let obs_id: Vec<String> = vec![title_o, class_o, &exe]
96 .into_iter()
97 .map(encode_string)
98 .collect();
99
100 let obs_id = obs_id.join(":");
101 Ok(WindowInfo {
102 full_exe: full_exe.to_string_lossy().to_string(),
103 obs_id,
104 #[cfg(all(not(feature = "serde"), not(feature = "specta")))]
105 handle: wnd,
106 pid: proc_id,
107 title,
108 class,
109 product_name,
110 monitor: monitor_id,
111 intersects,
112 cmd_line,
113 is_game,
114 })
115}
116
117pub struct ProcessInfo {
118 pub process_id: u32,
119 pub thread_id: u32,
120}
121
122pub fn get_thread_proc_id(wnd: HWND) -> WinResult<ProcessInfo> {
123 let mut proc_id = 0u32;
124
125 let thread_id = unsafe { GetWindowThreadProcessId(wnd, Some(&mut proc_id)) };
126 if thread_id == 0 {
127 return Err(Error::from_win32());
128 }
129
130 Ok(ProcessInfo {
131 process_id: proc_id,
132 thread_id,
133 })
134}