window-helpers.c 8.7 KB

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