window-helpers.c 11 KB

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