window-helpers.c 10 KB

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