window-helpers.c 11 KB

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