window-helpers.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. "winstore.app",
  114. "searchapp",
  115. "video.ui",
  116. "searchui",
  117. "lockapp",
  118. "cortana",
  119. "gamebar",
  120. "tabtip",
  121. "time",
  122. NULL,
  123. };
  124. static bool is_microsoft_internal_window_exe(const char *exe)
  125. {
  126. if (!exe)
  127. return false;
  128. for (const char **vals = internal_microsoft_exes; *vals; vals++) {
  129. if (astrcmpi_n(exe, *vals, strlen(*vals)) == 0)
  130. return true;
  131. }
  132. return false;
  133. }
  134. static void add_window(obs_property_t *p, HWND hwnd, add_window_cb callback)
  135. {
  136. struct dstr class = {0};
  137. struct dstr title = {0};
  138. struct dstr exe = {0};
  139. struct dstr encoded = {0};
  140. struct dstr desc = {0};
  141. if (!get_window_exe(&exe, hwnd))
  142. return;
  143. if (is_microsoft_internal_window_exe(exe.array)) {
  144. dstr_free(&exe);
  145. return;
  146. }
  147. get_window_title(&title, hwnd);
  148. if (dstr_cmp(&exe, "explorer.exe") == 0 && dstr_is_empty(&title)) {
  149. dstr_free(&exe);
  150. dstr_free(&title);
  151. return;
  152. }
  153. get_window_class(&class, hwnd);
  154. if (callback && !callback(title.array, class.array, exe.array)) {
  155. dstr_free(&title);
  156. dstr_free(&class);
  157. dstr_free(&exe);
  158. return;
  159. }
  160. dstr_printf(&desc, "[%s]: %s", exe.array, title.array);
  161. encode_dstr(&title);
  162. encode_dstr(&class);
  163. encode_dstr(&exe);
  164. dstr_cat_dstr(&encoded, &title);
  165. dstr_cat(&encoded, ":");
  166. dstr_cat_dstr(&encoded, &class);
  167. dstr_cat(&encoded, ":");
  168. dstr_cat_dstr(&encoded, &exe);
  169. obs_property_list_add_string(p, desc.array, encoded.array);
  170. dstr_free(&encoded);
  171. dstr_free(&desc);
  172. dstr_free(&class);
  173. dstr_free(&title);
  174. dstr_free(&exe);
  175. }
  176. static bool check_window_valid(HWND window, enum window_search_mode mode)
  177. {
  178. DWORD styles, ex_styles;
  179. RECT rect;
  180. if (!IsWindowVisible(window) ||
  181. (mode == EXCLUDE_MINIMIZED && IsIconic(window)))
  182. return false;
  183. GetClientRect(window, &rect);
  184. styles = (DWORD)GetWindowLongPtr(window, GWL_STYLE);
  185. ex_styles = (DWORD)GetWindowLongPtr(window, GWL_EXSTYLE);
  186. if (ex_styles & WS_EX_TOOLWINDOW)
  187. return false;
  188. if (styles & WS_CHILD)
  189. return false;
  190. if (mode == EXCLUDE_MINIMIZED && (rect.bottom == 0 || rect.right == 0))
  191. return false;
  192. return true;
  193. }
  194. bool is_uwp_window(HWND hwnd)
  195. {
  196. wchar_t name[256];
  197. name[0] = 0;
  198. if (!GetClassNameW(hwnd, name, sizeof(name) / sizeof(wchar_t)))
  199. return false;
  200. return wcscmp(name, L"ApplicationFrameWindow") == 0;
  201. }
  202. HWND get_uwp_actual_window(HWND parent)
  203. {
  204. DWORD parent_id = 0;
  205. HWND child;
  206. GetWindowThreadProcessId(parent, &parent_id);
  207. child = FindWindowEx(parent, NULL, NULL, NULL);
  208. while (child) {
  209. DWORD child_id = 0;
  210. GetWindowThreadProcessId(child, &child_id);
  211. if (child_id != parent_id)
  212. return child;
  213. child = FindWindowEx(parent, child, NULL, NULL);
  214. }
  215. return NULL;
  216. }
  217. static HWND next_window(HWND window, enum window_search_mode mode, HWND *parent,
  218. bool use_findwindowex)
  219. {
  220. if (*parent) {
  221. window = *parent;
  222. *parent = NULL;
  223. }
  224. while (true) {
  225. if (use_findwindowex)
  226. window = FindWindowEx(GetDesktopWindow(), window, NULL,
  227. NULL);
  228. else
  229. window = GetNextWindow(window, GW_HWNDNEXT);
  230. if (!window || check_window_valid(window, mode))
  231. break;
  232. }
  233. if (is_uwp_window(window)) {
  234. HWND child = get_uwp_actual_window(window);
  235. if (child) {
  236. *parent = window;
  237. return child;
  238. }
  239. }
  240. return window;
  241. }
  242. static HWND first_window(enum window_search_mode mode, HWND *parent,
  243. bool *use_findwindowex)
  244. {
  245. HWND window = FindWindowEx(GetDesktopWindow(), NULL, NULL, NULL);
  246. if (!window) {
  247. *use_findwindowex = false;
  248. window = GetWindow(GetDesktopWindow(), GW_CHILD);
  249. } else {
  250. *use_findwindowex = true;
  251. }
  252. *parent = NULL;
  253. if (!check_window_valid(window, mode)) {
  254. window = next_window(window, mode, parent, *use_findwindowex);
  255. if (!window && *use_findwindowex) {
  256. *use_findwindowex = false;
  257. window = GetWindow(GetDesktopWindow(), GW_CHILD);
  258. if (!check_window_valid(window, mode))
  259. window = next_window(window, mode, parent,
  260. *use_findwindowex);
  261. }
  262. }
  263. if (is_uwp_window(window)) {
  264. HWND child = get_uwp_actual_window(window);
  265. if (child) {
  266. *parent = window;
  267. return child;
  268. }
  269. }
  270. return window;
  271. }
  272. void fill_window_list(obs_property_t *p, enum window_search_mode mode,
  273. add_window_cb callback)
  274. {
  275. HWND parent;
  276. bool use_findwindowex = false;
  277. HWND window = first_window(mode, &parent, &use_findwindowex);
  278. while (window) {
  279. add_window(p, window, callback);
  280. window = next_window(window, mode, &parent, use_findwindowex);
  281. }
  282. }
  283. static int window_rating(HWND window, enum window_priority priority,
  284. const char *class, const char *title, const char *exe,
  285. bool uwp_window)
  286. {
  287. struct dstr cur_class = {0};
  288. struct dstr cur_title = {0};
  289. struct dstr cur_exe = {0};
  290. int val = 0x7FFFFFFF;
  291. if (!get_window_exe(&cur_exe, window))
  292. return 0x7FFFFFFF;
  293. get_window_title(&cur_title, window);
  294. get_window_class(&cur_class, window);
  295. bool class_matches = dstr_cmpi(&cur_class, class) == 0;
  296. bool exe_matches = dstr_cmpi(&cur_exe, exe) == 0;
  297. int title_val = abs(dstr_cmpi(&cur_title, title));
  298. /* always match by name with UWP windows */
  299. if (uwp_window) {
  300. if (priority == WINDOW_PRIORITY_EXE && !exe_matches)
  301. val = 0x7FFFFFFF;
  302. else
  303. val = title_val == 0 ? 0 : 0x7FFFFFFF;
  304. } else if (priority == WINDOW_PRIORITY_CLASS) {
  305. val = class_matches ? title_val : 0x7FFFFFFF;
  306. if (val != 0x7FFFFFFF && !exe_matches)
  307. val += 0x1000;
  308. } else if (priority == WINDOW_PRIORITY_TITLE) {
  309. val = title_val == 0 ? 0 : 0x7FFFFFFF;
  310. } else if (priority == WINDOW_PRIORITY_EXE) {
  311. val = exe_matches ? title_val : 0x7FFFFFFF;
  312. }
  313. dstr_free(&cur_class);
  314. dstr_free(&cur_title);
  315. dstr_free(&cur_exe);
  316. return val;
  317. }
  318. HWND find_window(enum window_search_mode mode, enum window_priority priority,
  319. const char *class, const char *title, const char *exe)
  320. {
  321. HWND parent;
  322. bool use_findwindowex = false;
  323. HWND window = first_window(mode, &parent, &use_findwindowex);
  324. HWND best_window = NULL;
  325. int best_rating = 0x7FFFFFFF;
  326. if (!class)
  327. return NULL;
  328. bool uwp_window = strcmp(class, "Windows.UI.Core.CoreWindow") == 0;
  329. while (window) {
  330. int rating = window_rating(window, priority, class, title, exe,
  331. uwp_window);
  332. if (rating < best_rating) {
  333. best_rating = rating;
  334. best_window = window;
  335. if (rating == 0)
  336. break;
  337. }
  338. window = next_window(window, mode, &parent, use_findwindowex);
  339. }
  340. return best_window;
  341. }