window-helpers.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. static void add_window(obs_property_t *p, HWND hwnd, add_window_cb callback)
  106. {
  107. struct dstr class = {0};
  108. struct dstr title = {0};
  109. struct dstr exe = {0};
  110. struct dstr encoded = {0};
  111. struct dstr desc = {0};
  112. if (!get_window_exe(&exe, hwnd))
  113. return;
  114. get_window_title(&title, hwnd);
  115. get_window_class(&class, hwnd);
  116. if (callback && !callback(title.array, class.array, exe.array)) {
  117. dstr_free(&title);
  118. dstr_free(&class);
  119. dstr_free(&exe);
  120. return;
  121. }
  122. dstr_printf(&desc, "[%s]: %s", exe.array, title.array);
  123. encode_dstr(&title);
  124. encode_dstr(&class);
  125. encode_dstr(&exe);
  126. dstr_cat_dstr(&encoded, &title);
  127. dstr_cat(&encoded, ":");
  128. dstr_cat_dstr(&encoded, &class);
  129. dstr_cat(&encoded, ":");
  130. dstr_cat_dstr(&encoded, &exe);
  131. obs_property_list_add_string(p, desc.array, encoded.array);
  132. dstr_free(&encoded);
  133. dstr_free(&desc);
  134. dstr_free(&class);
  135. dstr_free(&title);
  136. dstr_free(&exe);
  137. }
  138. static bool check_window_valid(HWND window, enum window_search_mode mode)
  139. {
  140. DWORD styles, ex_styles;
  141. RECT rect;
  142. if (!IsWindowVisible(window) ||
  143. (mode == EXCLUDE_MINIMIZED && IsIconic(window)))
  144. return false;
  145. GetClientRect(window, &rect);
  146. styles = (DWORD)GetWindowLongPtr(window, GWL_STYLE);
  147. ex_styles = (DWORD)GetWindowLongPtr(window, GWL_EXSTYLE);
  148. if (ex_styles & WS_EX_TOOLWINDOW)
  149. return false;
  150. if (styles & WS_CHILD)
  151. return false;
  152. if (mode == EXCLUDE_MINIMIZED && (rect.bottom == 0 || rect.right == 0))
  153. return false;
  154. return true;
  155. }
  156. bool is_uwp_window(HWND hwnd)
  157. {
  158. wchar_t name[256];
  159. name[0] = 0;
  160. if (!GetClassNameW(hwnd, name, sizeof(name) / sizeof(wchar_t)))
  161. return false;
  162. return wcscmp(name, L"ApplicationFrameWindow") == 0;
  163. }
  164. HWND get_uwp_actual_window(HWND parent)
  165. {
  166. DWORD parent_id = 0;
  167. HWND child;
  168. GetWindowThreadProcessId(parent, &parent_id);
  169. child = GetWindow(parent, GW_CHILD);
  170. while (child) {
  171. DWORD child_id = 0;
  172. GetWindowThreadProcessId(child, &child_id);
  173. if (child_id != parent_id)
  174. return child;
  175. child = GetNextWindow(child, GW_HWNDNEXT);
  176. }
  177. return NULL;
  178. }
  179. static inline HWND next_window(HWND window, enum window_search_mode mode,
  180. HWND *parent)
  181. {
  182. if (*parent) {
  183. window = *parent;
  184. *parent = NULL;
  185. }
  186. while (true) {
  187. window = GetNextWindow(window, GW_HWNDNEXT);
  188. if (!window || check_window_valid(window, mode))
  189. break;
  190. }
  191. if (is_uwp_window(window)) {
  192. HWND child = get_uwp_actual_window(window);
  193. if (child) {
  194. *parent = window;
  195. return child;
  196. }
  197. }
  198. return window;
  199. }
  200. static inline HWND first_window(enum window_search_mode mode, HWND *parent)
  201. {
  202. HWND window = GetWindow(GetDesktopWindow(), GW_CHILD);
  203. *parent = NULL;
  204. if (!check_window_valid(window, mode))
  205. window = next_window(window, mode, parent);
  206. if (is_uwp_window(window)) {
  207. HWND child = get_uwp_actual_window(window);
  208. if (child) {
  209. *parent = window;
  210. return child;
  211. }
  212. }
  213. return window;
  214. }
  215. void fill_window_list(obs_property_t *p, enum window_search_mode mode,
  216. add_window_cb callback)
  217. {
  218. HWND parent;
  219. HWND window = first_window(mode, &parent);
  220. while (window) {
  221. add_window(p, window, callback);
  222. window = next_window(window, mode, &parent);
  223. }
  224. }
  225. static int window_rating(HWND window,
  226. enum window_priority priority,
  227. const char *class,
  228. const char *title,
  229. const char *exe,
  230. bool uwp_window)
  231. {
  232. struct dstr cur_class = {0};
  233. struct dstr cur_title = {0};
  234. struct dstr cur_exe = {0};
  235. int class_val = 1;
  236. int title_val = 1;
  237. int exe_val = 0;
  238. int total = 0;
  239. if (!get_window_exe(&cur_exe, window))
  240. return 0;
  241. get_window_title(&cur_title, window);
  242. get_window_class(&cur_class, window);
  243. if (priority == WINDOW_PRIORITY_CLASS)
  244. class_val += 3;
  245. else if (priority == WINDOW_PRIORITY_TITLE)
  246. title_val += 3;
  247. else
  248. exe_val += 3;
  249. if (uwp_window) {
  250. if (dstr_cmpi(&cur_title, title) == 0 &&
  251. dstr_cmpi(&cur_exe, exe) == 0)
  252. total += exe_val + title_val + class_val;
  253. } else {
  254. if (dstr_cmpi(&cur_class, class) == 0)
  255. total += class_val;
  256. if (dstr_cmpi(&cur_title, title) == 0)
  257. total += title_val;
  258. if (dstr_cmpi(&cur_exe, exe) == 0)
  259. total += exe_val;
  260. }
  261. dstr_free(&cur_class);
  262. dstr_free(&cur_title);
  263. dstr_free(&cur_exe);
  264. return total;
  265. }
  266. HWND find_window(enum window_search_mode mode,
  267. enum window_priority priority,
  268. const char *class,
  269. const char *title,
  270. const char *exe)
  271. {
  272. HWND parent;
  273. HWND window = first_window(mode, &parent);
  274. HWND best_window = NULL;
  275. int best_rating = 0;
  276. bool uwp_window = strcmp(class, "Windows.UI.Core.CoreWindow") == 0;
  277. while (window) {
  278. int rating = window_rating(window, priority, class, title, exe,
  279. uwp_window);
  280. if (rating > best_rating) {
  281. best_rating = rating;
  282. best_window = window;
  283. }
  284. window = next_window(window, mode, &parent);
  285. }
  286. return best_window;
  287. }