window-helpers.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. #define PSAPI_VERSION 1
  2. #include <obs.h>
  3. #include <util/dstr.h>
  4. #include <windows.h>
  5. #include <psapi.h>
  6. #include "window-helpers.h"
  7. #include "obfuscate.h"
  8. static inline void encode_dstr(struct dstr *str)
  9. {
  10. dstr_replace(str, "#", "#22");
  11. dstr_replace(str, ":", "#3A");
  12. }
  13. static inline char *decode_str(const char *src)
  14. {
  15. struct dstr str = {0};
  16. dstr_copy(&str, src);
  17. dstr_replace(&str, "#3A", ":");
  18. dstr_replace(&str, "#22", "#");
  19. return str.array;
  20. }
  21. extern void build_window_strings(const char *str,
  22. char **class,
  23. char **title,
  24. char **exe)
  25. {
  26. char **strlist;
  27. *class = NULL;
  28. *title = NULL;
  29. *exe = NULL;
  30. if (!str) {
  31. return;
  32. }
  33. strlist = strlist_split(str, ':', true);
  34. if (strlist && strlist[0] && strlist[1] && strlist[2]) {
  35. *title = decode_str(strlist[0]);
  36. *class = decode_str(strlist[1]);
  37. *exe = decode_str(strlist[2]);
  38. }
  39. strlist_free(strlist);
  40. }
  41. static HMODULE kernel32(void)
  42. {
  43. static HMODULE kernel32_handle = NULL;
  44. if (!kernel32_handle)
  45. kernel32_handle = GetModuleHandleA("kernel32");
  46. return kernel32_handle;
  47. }
  48. static inline HANDLE open_process(DWORD desired_access, bool inherit_handle,
  49. DWORD process_id)
  50. {
  51. static HANDLE (WINAPI *open_process_proc)(DWORD, BOOL, DWORD) = NULL;
  52. if (!open_process_proc)
  53. open_process_proc = get_obfuscated_func(kernel32(),
  54. "B}caZyah`~q", 0x2D5BEBAF6DDULL);
  55. return open_process_proc(desired_access, inherit_handle, process_id);
  56. }
  57. bool get_window_exe(struct dstr *name, HWND window)
  58. {
  59. wchar_t wname[MAX_PATH];
  60. struct dstr temp = {0};
  61. bool success = false;
  62. HANDLE process = NULL;
  63. char *slash;
  64. DWORD id;
  65. GetWindowThreadProcessId(window, &id);
  66. if (id == GetCurrentProcessId())
  67. return false;
  68. process = open_process(PROCESS_QUERY_LIMITED_INFORMATION, false, id);
  69. if (!process)
  70. goto fail;
  71. if (!GetProcessImageFileNameW(process, wname, MAX_PATH))
  72. goto fail;
  73. dstr_from_wcs(&temp, wname);
  74. slash = strrchr(temp.array, '\\');
  75. if (!slash)
  76. goto fail;
  77. dstr_copy(name, slash+1);
  78. success = true;
  79. fail:
  80. if (!success)
  81. dstr_copy(name, "unknown");
  82. dstr_free(&temp);
  83. CloseHandle(process);
  84. return true;
  85. }
  86. void get_window_title(struct dstr *name, HWND hwnd)
  87. {
  88. wchar_t *temp;
  89. int len;
  90. len = GetWindowTextLengthW(hwnd);
  91. if (!len)
  92. return;
  93. temp = malloc(sizeof(wchar_t) * (len+1));
  94. if (GetWindowTextW(hwnd, temp, len+1))
  95. dstr_from_wcs(name, temp);
  96. free(temp);
  97. }
  98. void get_window_class(struct dstr *class, HWND hwnd)
  99. {
  100. wchar_t temp[256];
  101. temp[0] = 0;
  102. if (GetClassNameW(hwnd, temp, sizeof(temp) / sizeof(wchar_t)))
  103. dstr_from_wcs(class, temp);
  104. }
  105. /* not capturable or internal windows */
  106. static const char *internal_microsoft_exes[] = {
  107. "applicationframehost",
  108. "shellexperiencehost",
  109. "windowsinternal",
  110. "winstore.app",
  111. "searchui",
  112. "lockapp",
  113. NULL
  114. };
  115. static bool is_microsoft_internal_window_exe(const char *exe)
  116. {
  117. if (!exe)
  118. return false;
  119. for (const char **vals = internal_microsoft_exes; *vals; vals++) {
  120. if (astrcmpi_n(exe, *vals, strlen(*vals)) == 0)
  121. return true;
  122. }
  123. return false;
  124. }
  125. static void add_window(obs_property_t *p, HWND hwnd, add_window_cb callback)
  126. {
  127. struct dstr class = {0};
  128. struct dstr title = {0};
  129. struct dstr exe = {0};
  130. struct dstr encoded = {0};
  131. struct dstr desc = {0};
  132. if (!get_window_exe(&exe, hwnd))
  133. return;
  134. if (is_microsoft_internal_window_exe(exe.array)) {
  135. dstr_free(&exe);
  136. return;
  137. }
  138. get_window_title(&title, hwnd);
  139. if (dstr_cmp(&exe, "explorer.exe") == 0 && dstr_is_empty(&title)) {
  140. dstr_free(&exe);
  141. dstr_free(&title);
  142. return;
  143. }
  144. get_window_class(&class, hwnd);
  145. if (callback && !callback(title.array, class.array, exe.array)) {
  146. dstr_free(&title);
  147. dstr_free(&class);
  148. dstr_free(&exe);
  149. return;
  150. }
  151. dstr_printf(&desc, "[%s]: %s", exe.array, title.array);
  152. encode_dstr(&title);
  153. encode_dstr(&class);
  154. encode_dstr(&exe);
  155. dstr_cat_dstr(&encoded, &title);
  156. dstr_cat(&encoded, ":");
  157. dstr_cat_dstr(&encoded, &class);
  158. dstr_cat(&encoded, ":");
  159. dstr_cat_dstr(&encoded, &exe);
  160. obs_property_list_add_string(p, desc.array, encoded.array);
  161. dstr_free(&encoded);
  162. dstr_free(&desc);
  163. dstr_free(&class);
  164. dstr_free(&title);
  165. dstr_free(&exe);
  166. }
  167. static bool check_window_valid(HWND window, enum window_search_mode mode)
  168. {
  169. DWORD styles, ex_styles;
  170. RECT rect;
  171. if (!IsWindowVisible(window) ||
  172. (mode == EXCLUDE_MINIMIZED && IsIconic(window)))
  173. return false;
  174. GetClientRect(window, &rect);
  175. styles = (DWORD)GetWindowLongPtr(window, GWL_STYLE);
  176. ex_styles = (DWORD)GetWindowLongPtr(window, GWL_EXSTYLE);
  177. if (ex_styles & WS_EX_TOOLWINDOW)
  178. return false;
  179. if (styles & WS_CHILD)
  180. return false;
  181. if (mode == EXCLUDE_MINIMIZED && (rect.bottom == 0 || rect.right == 0))
  182. return false;
  183. return true;
  184. }
  185. bool is_uwp_window(HWND hwnd)
  186. {
  187. wchar_t name[256];
  188. name[0] = 0;
  189. if (!GetClassNameW(hwnd, name, sizeof(name) / sizeof(wchar_t)))
  190. return false;
  191. return wcscmp(name, L"ApplicationFrameWindow") == 0;
  192. }
  193. HWND get_uwp_actual_window(HWND parent)
  194. {
  195. DWORD parent_id = 0;
  196. HWND child;
  197. GetWindowThreadProcessId(parent, &parent_id);
  198. child = FindWindowEx(parent, NULL, NULL, NULL);
  199. while (child) {
  200. DWORD child_id = 0;
  201. GetWindowThreadProcessId(child, &child_id);
  202. if (child_id != parent_id)
  203. return child;
  204. child = FindWindowEx(parent, child, NULL, NULL);
  205. }
  206. return NULL;
  207. }
  208. static HWND next_window(HWND window, enum window_search_mode mode,
  209. HWND *parent, bool use_findwindowex)
  210. {
  211. if (*parent) {
  212. window = *parent;
  213. *parent = NULL;
  214. }
  215. while (true) {
  216. if (use_findwindowex)
  217. window = FindWindowEx(GetDesktopWindow(), window, NULL,
  218. NULL);
  219. else
  220. window = GetNextWindow(window, GW_HWNDNEXT);
  221. if (!window || check_window_valid(window, mode))
  222. break;
  223. }
  224. if (is_uwp_window(window)) {
  225. HWND child = get_uwp_actual_window(window);
  226. if (child) {
  227. *parent = window;
  228. return child;
  229. }
  230. }
  231. return window;
  232. }
  233. static HWND first_window(enum window_search_mode mode, HWND *parent,
  234. bool *use_findwindowex)
  235. {
  236. HWND window = FindWindowEx(GetDesktopWindow(), NULL, NULL, NULL);
  237. if (!window) {
  238. *use_findwindowex = false;
  239. window = GetWindow(GetDesktopWindow(), GW_CHILD);
  240. } else {
  241. *use_findwindowex = true;
  242. }
  243. *parent = NULL;
  244. if (!check_window_valid(window, mode)) {
  245. window = next_window(window, mode, parent, *use_findwindowex);
  246. if (!window && *use_findwindowex) {
  247. *use_findwindowex = false;
  248. window = GetWindow(GetDesktopWindow(), GW_CHILD);
  249. if (!check_window_valid(window, mode))
  250. window = next_window(window, mode, parent,
  251. *use_findwindowex);
  252. }
  253. }
  254. if (is_uwp_window(window)) {
  255. HWND child = get_uwp_actual_window(window);
  256. if (child) {
  257. *parent = window;
  258. return child;
  259. }
  260. }
  261. return window;
  262. }
  263. void fill_window_list(obs_property_t *p, enum window_search_mode mode,
  264. add_window_cb callback)
  265. {
  266. HWND parent;
  267. bool use_findwindowex = false;
  268. HWND window = first_window(mode, &parent, &use_findwindowex);
  269. while (window) {
  270. add_window(p, window, callback);
  271. window = next_window(window, mode, &parent, use_findwindowex);
  272. }
  273. }
  274. static int window_rating(HWND window,
  275. enum window_priority priority,
  276. const char *class,
  277. const char *title,
  278. const char *exe,
  279. bool uwp_window)
  280. {
  281. struct dstr cur_class = {0};
  282. struct dstr cur_title = {0};
  283. struct dstr cur_exe = {0};
  284. int val = 0x7FFFFFFF;
  285. if (!get_window_exe(&cur_exe, window))
  286. return 0x7FFFFFFF;
  287. get_window_title(&cur_title, window);
  288. get_window_class(&cur_class, window);
  289. bool class_matches = dstr_cmpi(&cur_class, class) == 0;
  290. bool exe_matches = dstr_cmpi(&cur_exe, exe) == 0;
  291. int title_val = abs(dstr_cmpi(&cur_title, title));
  292. /* always match by name with UWP windows */
  293. if (uwp_window) {
  294. if (priority == WINDOW_PRIORITY_EXE && !exe_matches)
  295. val = 0x7FFFFFFF;
  296. else
  297. val = title_val == 0 ? 0 : 0x7FFFFFFF;
  298. } else if (priority == WINDOW_PRIORITY_CLASS) {
  299. val = class_matches ? title_val : 0x7FFFFFFF;
  300. if (val != 0x7FFFFFFF && !exe_matches)
  301. val += 0x1000;
  302. } else if (priority == WINDOW_PRIORITY_TITLE) {
  303. val = title_val == 0 ? 0 : 0x7FFFFFFF;
  304. } else if (priority == WINDOW_PRIORITY_EXE) {
  305. val = exe_matches ? title_val : 0x7FFFFFFF;
  306. }
  307. dstr_free(&cur_class);
  308. dstr_free(&cur_title);
  309. dstr_free(&cur_exe);
  310. return val;
  311. }
  312. HWND find_window(enum window_search_mode mode,
  313. enum window_priority priority,
  314. const char *class,
  315. const char *title,
  316. const char *exe)
  317. {
  318. HWND parent;
  319. bool use_findwindowex = false;
  320. HWND window = first_window(mode, &parent, &use_findwindowex);
  321. HWND best_window = NULL;
  322. int best_rating = 0x7FFFFFFF;
  323. if (!class)
  324. return NULL;
  325. bool uwp_window = strcmp(class, "Windows.UI.Core.CoreWindow") == 0;
  326. while (window) {
  327. int rating = window_rating(window, priority, class, title, exe,
  328. uwp_window);
  329. if (rating < best_rating) {
  330. best_rating = rating;
  331. best_window = window;
  332. if (rating == 0)
  333. break;
  334. }
  335. window = next_window(window, mode, &parent, use_findwindowex);
  336. }
  337. return best_window;
  338. }