window-helpers.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. #include "window-helpers.h"
  2. #include <util/dstr.h>
  3. #include <util/windows/obfuscate.h>
  4. #include <dwmapi.h>
  5. #include <psapi.h>
  6. static inline void encode_dstr(struct dstr *str)
  7. {
  8. dstr_replace(str, "#", "#22");
  9. dstr_replace(str, ":", "#3A");
  10. }
  11. static inline char *decode_str(const char *src)
  12. {
  13. struct dstr str = {0};
  14. dstr_copy(&str, src);
  15. dstr_replace(&str, "#3A", ":");
  16. dstr_replace(&str, "#22", "#");
  17. return str.array;
  18. }
  19. void ms_build_window_strings(const char *str, char **class, char **title,
  20. char **exe)
  21. {
  22. char **strlist;
  23. *class = NULL;
  24. *title = NULL;
  25. *exe = NULL;
  26. if (!str) {
  27. return;
  28. }
  29. strlist = strlist_split(str, ':', true);
  30. if (strlist && strlist[0] && strlist[1] && strlist[2]) {
  31. *title = decode_str(strlist[0]);
  32. *class = decode_str(strlist[1]);
  33. *exe = decode_str(strlist[2]);
  34. }
  35. strlist_free(strlist);
  36. }
  37. static void insert_preserved_val(obs_property_t *p, const char *val, size_t idx)
  38. {
  39. char *window_class = NULL;
  40. char *title = NULL;
  41. char *executable = NULL;
  42. struct dstr desc = {0};
  43. ms_build_window_strings(val, &window_class, &title, &executable);
  44. dstr_printf(&desc, "[%s]: %s", executable, title);
  45. obs_property_list_insert_string(p, idx, desc.array, val);
  46. obs_property_list_item_disable(p, idx, true);
  47. dstr_free(&desc);
  48. bfree(window_class);
  49. bfree(title);
  50. bfree(executable);
  51. }
  52. bool ms_check_window_property_setting(obs_properties_t *ppts, obs_property_t *p,
  53. obs_data_t *settings, const char *val,
  54. size_t idx)
  55. {
  56. const char *cur_val;
  57. bool match = false;
  58. size_t i = 0;
  59. cur_val = obs_data_get_string(settings, val);
  60. if (!cur_val) {
  61. return false;
  62. }
  63. for (;;) {
  64. const char *val = obs_property_list_item_string(p, i++);
  65. if (!val)
  66. break;
  67. if (strcmp(val, cur_val) == 0) {
  68. match = true;
  69. break;
  70. }
  71. }
  72. if (cur_val && *cur_val && !match) {
  73. insert_preserved_val(p, cur_val, idx);
  74. return true;
  75. }
  76. UNUSED_PARAMETER(ppts);
  77. return false;
  78. }
  79. static HMODULE kernel32(void)
  80. {
  81. static HMODULE kernel32_handle = NULL;
  82. if (!kernel32_handle)
  83. kernel32_handle = GetModuleHandleA("kernel32");
  84. return kernel32_handle;
  85. }
  86. static inline HANDLE open_process(DWORD desired_access, bool inherit_handle,
  87. DWORD process_id)
  88. {
  89. typedef HANDLE(WINAPI * PFN_OpenProcess)(DWORD, BOOL, DWORD);
  90. static PFN_OpenProcess open_process_proc = NULL;
  91. if (!open_process_proc)
  92. open_process_proc = (PFN_OpenProcess)ms_get_obfuscated_func(
  93. kernel32(), "B}caZyah`~q", 0x2D5BEBAF6DDULL);
  94. return open_process_proc(desired_access, inherit_handle, process_id);
  95. }
  96. bool ms_get_window_exe(struct dstr *name, HWND window)
  97. {
  98. wchar_t wname[MAX_PATH];
  99. struct dstr temp = {0};
  100. bool success = false;
  101. HANDLE process = NULL;
  102. char *slash;
  103. DWORD id;
  104. GetWindowThreadProcessId(window, &id);
  105. if (id == GetCurrentProcessId())
  106. return false;
  107. process = open_process(PROCESS_QUERY_LIMITED_INFORMATION, false, id);
  108. if (!process)
  109. goto fail;
  110. if (!GetProcessImageFileNameW(process, wname, MAX_PATH))
  111. goto fail;
  112. dstr_from_wcs(&temp, wname);
  113. slash = strrchr(temp.array, '\\');
  114. if (!slash)
  115. goto fail;
  116. dstr_copy(name, slash + 1);
  117. success = true;
  118. fail:
  119. if (!success)
  120. dstr_copy(name, "unknown");
  121. dstr_free(&temp);
  122. CloseHandle(process);
  123. return true;
  124. }
  125. void ms_get_window_title(struct dstr *name, HWND hwnd)
  126. {
  127. int len;
  128. len = GetWindowTextLengthW(hwnd);
  129. if (!len)
  130. return;
  131. if (len > 1024) {
  132. wchar_t *temp;
  133. temp = malloc(sizeof(wchar_t) * (len + 1));
  134. if (!temp)
  135. return;
  136. if (GetWindowTextW(hwnd, temp, len + 1))
  137. dstr_from_wcs(name, temp);
  138. free(temp);
  139. } else {
  140. wchar_t temp[1024 + 1];
  141. if (GetWindowTextW(hwnd, temp, len + 1))
  142. dstr_from_wcs(name, temp);
  143. }
  144. }
  145. void ms_get_window_class(struct dstr *class, HWND hwnd)
  146. {
  147. wchar_t temp[256];
  148. temp[0] = 0;
  149. if (GetClassNameW(hwnd, temp, sizeof(temp) / sizeof(wchar_t)))
  150. dstr_from_wcs(class, temp);
  151. }
  152. /* not capturable or internal windows, exact executable names */
  153. static const char *internal_microsoft_exes_exact[] = {
  154. "startmenuexperiencehost.exe",
  155. "applicationframehost.exe",
  156. "peopleexperiencehost.exe",
  157. "shellexperiencehost.exe",
  158. "microsoft.notes.exe",
  159. "systemsettings.exe",
  160. "textinputhost.exe",
  161. "searchapp.exe",
  162. "video.ui.exe",
  163. "searchui.exe",
  164. "lockapp.exe",
  165. "cortana.exe",
  166. "gamebar.exe",
  167. "tabtip.exe",
  168. "time.exe",
  169. NULL,
  170. };
  171. /* partial matches start from the beginning of the executable name */
  172. static const char *internal_microsoft_exes_partial[] = {
  173. "windowsinternal",
  174. NULL,
  175. };
  176. static bool is_microsoft_internal_window_exe(const char *exe)
  177. {
  178. if (!exe)
  179. return false;
  180. for (const char **vals = internal_microsoft_exes_exact; *vals; vals++) {
  181. if (astrcmpi(exe, *vals) == 0)
  182. return true;
  183. }
  184. for (const char **vals = internal_microsoft_exes_partial; *vals;
  185. vals++) {
  186. if (astrcmpi_n(exe, *vals, strlen(*vals)) == 0)
  187. return true;
  188. }
  189. return false;
  190. }
  191. static void add_window(obs_property_t *p, HWND hwnd, add_window_cb callback)
  192. {
  193. struct dstr class = {0};
  194. struct dstr title = {0};
  195. struct dstr exe = {0};
  196. struct dstr encoded = {0};
  197. struct dstr desc = {0};
  198. if (!ms_get_window_exe(&exe, hwnd))
  199. return;
  200. if (is_microsoft_internal_window_exe(exe.array)) {
  201. dstr_free(&exe);
  202. return;
  203. }
  204. ms_get_window_title(&title, hwnd);
  205. if (dstr_cmp(&exe, "explorer.exe") == 0 && dstr_is_empty(&title)) {
  206. dstr_free(&exe);
  207. dstr_free(&title);
  208. return;
  209. }
  210. ms_get_window_class(&class, hwnd);
  211. if (callback && !callback(title.array, class.array, exe.array)) {
  212. dstr_free(&title);
  213. dstr_free(&class);
  214. dstr_free(&exe);
  215. return;
  216. }
  217. dstr_printf(&desc, "[%s]: %s", exe.array, title.array);
  218. encode_dstr(&title);
  219. encode_dstr(&class);
  220. encode_dstr(&exe);
  221. dstr_cat_dstr(&encoded, &title);
  222. dstr_cat(&encoded, ":");
  223. dstr_cat_dstr(&encoded, &class);
  224. dstr_cat(&encoded, ":");
  225. dstr_cat_dstr(&encoded, &exe);
  226. obs_property_list_add_string(p, desc.array, encoded.array);
  227. dstr_free(&encoded);
  228. dstr_free(&desc);
  229. dstr_free(&class);
  230. dstr_free(&title);
  231. dstr_free(&exe);
  232. }
  233. static inline bool IsWindowCloaked(HWND window)
  234. {
  235. DWORD cloaked;
  236. HRESULT hr = DwmGetWindowAttribute(window, DWMWA_CLOAKED, &cloaked,
  237. sizeof(cloaked));
  238. return SUCCEEDED(hr) && cloaked;
  239. }
  240. static bool check_window_valid(HWND window, enum window_search_mode mode)
  241. {
  242. DWORD styles, ex_styles;
  243. RECT rect;
  244. if (!IsWindowVisible(window) ||
  245. (mode == EXCLUDE_MINIMIZED &&
  246. (IsIconic(window) || IsWindowCloaked(window))))
  247. return false;
  248. GetClientRect(window, &rect);
  249. styles = (DWORD)GetWindowLongPtr(window, GWL_STYLE);
  250. ex_styles = (DWORD)GetWindowLongPtr(window, GWL_EXSTYLE);
  251. if (ex_styles & WS_EX_TOOLWINDOW)
  252. return false;
  253. if (styles & WS_CHILD)
  254. return false;
  255. if (mode == EXCLUDE_MINIMIZED && (rect.bottom == 0 || rect.right == 0))
  256. return false;
  257. return true;
  258. }
  259. bool ms_is_uwp_window(HWND hwnd)
  260. {
  261. wchar_t name[256];
  262. name[0] = 0;
  263. if (!GetClassNameW(hwnd, name, sizeof(name) / sizeof(wchar_t)))
  264. return false;
  265. return wcscmp(name, L"ApplicationFrameWindow") == 0;
  266. }
  267. HWND ms_get_uwp_actual_window(HWND parent)
  268. {
  269. DWORD parent_id = 0;
  270. HWND child;
  271. GetWindowThreadProcessId(parent, &parent_id);
  272. child = FindWindowEx(parent, NULL, NULL, NULL);
  273. while (child) {
  274. DWORD child_id = 0;
  275. GetWindowThreadProcessId(child, &child_id);
  276. if (child_id != parent_id)
  277. return child;
  278. child = FindWindowEx(parent, child, NULL, NULL);
  279. }
  280. return NULL;
  281. }
  282. static HWND next_window(HWND window, enum window_search_mode mode, HWND *parent,
  283. bool use_findwindowex)
  284. {
  285. if (*parent) {
  286. window = *parent;
  287. *parent = NULL;
  288. }
  289. while (true) {
  290. if (use_findwindowex)
  291. window = FindWindowEx(GetDesktopWindow(), window, NULL,
  292. NULL);
  293. else
  294. window = GetNextWindow(window, GW_HWNDNEXT);
  295. if (!window || check_window_valid(window, mode))
  296. break;
  297. }
  298. if (ms_is_uwp_window(window)) {
  299. HWND child = ms_get_uwp_actual_window(window);
  300. if (child) {
  301. *parent = window;
  302. return child;
  303. }
  304. }
  305. return window;
  306. }
  307. static HWND first_window(enum window_search_mode mode, HWND *parent,
  308. bool *use_findwindowex)
  309. {
  310. HWND window = FindWindowEx(GetDesktopWindow(), NULL, NULL, NULL);
  311. if (!window) {
  312. *use_findwindowex = false;
  313. window = GetWindow(GetDesktopWindow(), GW_CHILD);
  314. } else {
  315. *use_findwindowex = true;
  316. }
  317. *parent = NULL;
  318. if (!check_window_valid(window, mode)) {
  319. window = next_window(window, mode, parent, *use_findwindowex);
  320. if (!window && *use_findwindowex) {
  321. *use_findwindowex = false;
  322. window = GetWindow(GetDesktopWindow(), GW_CHILD);
  323. if (!check_window_valid(window, mode))
  324. window = next_window(window, mode, parent,
  325. *use_findwindowex);
  326. }
  327. }
  328. if (ms_is_uwp_window(window)) {
  329. HWND child = ms_get_uwp_actual_window(window);
  330. if (child) {
  331. *parent = window;
  332. return child;
  333. }
  334. }
  335. return window;
  336. }
  337. void ms_fill_window_list(obs_property_t *p, enum window_search_mode mode,
  338. add_window_cb callback)
  339. {
  340. HWND parent;
  341. bool use_findwindowex = false;
  342. HWND window = first_window(mode, &parent, &use_findwindowex);
  343. while (window) {
  344. add_window(p, window, callback);
  345. window = next_window(window, mode, &parent, use_findwindowex);
  346. }
  347. }
  348. static int window_rating(HWND window, enum window_priority priority,
  349. const char *class, const char *title, const char *exe,
  350. bool uwp_window, bool generic_class)
  351. {
  352. struct dstr cur_class = {0};
  353. struct dstr cur_title = {0};
  354. struct dstr cur_exe = {0};
  355. int val = 0x7FFFFFFF;
  356. if (!ms_get_window_exe(&cur_exe, window))
  357. return 0x7FFFFFFF;
  358. ms_get_window_title(&cur_title, window);
  359. ms_get_window_class(&cur_class, window);
  360. bool class_matches = dstr_cmpi(&cur_class, class) == 0;
  361. bool exe_matches = dstr_cmpi(&cur_exe, exe) == 0;
  362. int title_val = abs(dstr_cmpi(&cur_title, title));
  363. if (generic_class && (priority == WINDOW_PRIORITY_CLASS))
  364. priority = WINDOW_PRIORITY_TITLE;
  365. /* always match by name with UWP windows */
  366. if (uwp_window) {
  367. if (priority == WINDOW_PRIORITY_EXE && !exe_matches)
  368. val = 0x7FFFFFFF;
  369. else
  370. val = title_val == 0 ? 0 : 0x7FFFFFFF;
  371. } else if (priority == WINDOW_PRIORITY_CLASS) {
  372. val = class_matches ? title_val : 0x7FFFFFFF;
  373. if (val != 0x7FFFFFFF && !exe_matches)
  374. val += 0x1000;
  375. } else if (priority == WINDOW_PRIORITY_TITLE) {
  376. val = title_val == 0 ? 0 : 0x7FFFFFFF;
  377. } else if (priority == WINDOW_PRIORITY_EXE) {
  378. val = exe_matches ? title_val : 0x7FFFFFFF;
  379. }
  380. dstr_free(&cur_class);
  381. dstr_free(&cur_title);
  382. dstr_free(&cur_exe);
  383. return val;
  384. }
  385. static const char *generic_class_substrings[] = {
  386. "Chrome",
  387. NULL,
  388. };
  389. static bool is_generic_class(const char *current_class)
  390. {
  391. const char **class = generic_class_substrings;
  392. while (*class) {
  393. if (astrstri(current_class, *class) != NULL) {
  394. return true;
  395. }
  396. class ++;
  397. }
  398. return false;
  399. }
  400. static bool is_uwp_class(const char *window_class)
  401. {
  402. return strcmp(window_class, "Windows.UI.Core.CoreWindow") == 0;
  403. }
  404. HWND ms_find_window(enum window_search_mode mode, enum window_priority priority,
  405. const char *class, const char *title, const char *exe)
  406. {
  407. HWND parent;
  408. bool use_findwindowex = false;
  409. HWND window = first_window(mode, &parent, &use_findwindowex);
  410. HWND best_window = NULL;
  411. int best_rating = 0x7FFFFFFF;
  412. if (!class)
  413. return NULL;
  414. const bool uwp_window = is_uwp_class(class);
  415. const bool generic_class = is_generic_class(class);
  416. while (window) {
  417. int rating = window_rating(window, priority, class, title, exe,
  418. uwp_window, generic_class);
  419. if (rating < best_rating) {
  420. best_rating = rating;
  421. best_window = window;
  422. if (rating == 0)
  423. break;
  424. }
  425. window = next_window(window, mode, &parent, use_findwindowex);
  426. }
  427. return best_window;
  428. }
  429. struct top_level_enum_data {
  430. enum window_search_mode mode;
  431. enum window_priority priority;
  432. const char *class;
  433. const char *title;
  434. const char *exe;
  435. bool uwp_window;
  436. bool generic_class;
  437. HWND best_window;
  438. int best_rating;
  439. };
  440. BOOL CALLBACK enum_windows_proc(HWND window, LPARAM lParam)
  441. {
  442. struct top_level_enum_data *data = (struct top_level_enum_data *)lParam;
  443. if (!check_window_valid(window, data->mode))
  444. return TRUE;
  445. if (IsWindowCloaked(window))
  446. return TRUE;
  447. const int rating = window_rating(window, data->priority, data->class,
  448. data->title, data->exe,
  449. data->uwp_window, data->generic_class);
  450. if (rating < data->best_rating) {
  451. data->best_rating = rating;
  452. data->best_window = window;
  453. }
  454. return rating > 0;
  455. }
  456. HWND ms_find_window_top_level(enum window_search_mode mode,
  457. enum window_priority priority, const char *class,
  458. const char *title, const char *exe)
  459. {
  460. if (!class)
  461. return NULL;
  462. struct top_level_enum_data data;
  463. data.mode = mode;
  464. data.priority = priority;
  465. data.class = class;
  466. data.title = title;
  467. data.exe = exe;
  468. data.uwp_window = is_uwp_class(class);
  469. data.generic_class = is_generic_class(class);
  470. data.best_window = NULL;
  471. data.best_rating = 0x7FFFFFFF;
  472. EnumWindows(enum_windows_proc, (LPARAM)&data);
  473. return data.best_window;
  474. }