window-helpers.c 8.7 KB

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