window-helpers.c 13 KB

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