window-helpers.c 11 KB

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