window-helpers.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. static 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. GetWindowTextW(hwnd, temp, len+1);
  95. dstr_from_wcs(name, temp);
  96. free(temp);
  97. }
  98. static void get_window_class(struct dstr *class, HWND hwnd)
  99. {
  100. wchar_t temp[256];
  101. temp[0] = 0;
  102. GetClassNameW(hwnd, temp, sizeof(temp));
  103. dstr_from_wcs(class, temp);
  104. }
  105. static void add_window(obs_property_t *p, HWND hwnd)
  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. dstr_printf(&desc, "[%s]: %s", exe.array, title.array);
  117. encode_dstr(&title);
  118. encode_dstr(&class);
  119. encode_dstr(&exe);
  120. dstr_cat_dstr(&encoded, &title);
  121. dstr_cat(&encoded, ":");
  122. dstr_cat_dstr(&encoded, &class);
  123. dstr_cat(&encoded, ":");
  124. dstr_cat_dstr(&encoded, &exe);
  125. obs_property_list_add_string(p, desc.array, encoded.array);
  126. dstr_free(&encoded);
  127. dstr_free(&desc);
  128. dstr_free(&class);
  129. dstr_free(&title);
  130. dstr_free(&exe);
  131. }
  132. static bool check_window_valid(HWND window, enum window_search_mode mode)
  133. {
  134. DWORD styles, ex_styles;
  135. RECT rect;
  136. if (!IsWindowVisible(window) ||
  137. (mode == EXCLUDE_MINIMIZED && IsIconic(window)))
  138. return false;
  139. GetClientRect(window, &rect);
  140. styles = (DWORD)GetWindowLongPtr(window, GWL_STYLE);
  141. ex_styles = (DWORD)GetWindowLongPtr(window, GWL_EXSTYLE);
  142. if (ex_styles & WS_EX_TOOLWINDOW)
  143. return false;
  144. if (styles & WS_CHILD)
  145. return false;
  146. if (mode == EXCLUDE_MINIMIZED && (rect.bottom == 0 || rect.right == 0))
  147. return false;
  148. return true;
  149. }
  150. static inline HWND next_window(HWND window, enum window_search_mode mode)
  151. {
  152. while (true) {
  153. window = GetNextWindow(window, GW_HWNDNEXT);
  154. if (!window || check_window_valid(window, mode))
  155. break;
  156. }
  157. return window;
  158. }
  159. static inline HWND first_window(enum window_search_mode mode)
  160. {
  161. HWND window = GetWindow(GetDesktopWindow(), GW_CHILD);
  162. if (!check_window_valid(window, mode))
  163. window = next_window(window, mode);
  164. return window;
  165. }
  166. void fill_window_list(obs_property_t *p, enum window_search_mode mode)
  167. {
  168. HWND window = first_window(mode);
  169. while (window) {
  170. add_window(p, window);
  171. window = next_window(window, mode);
  172. }
  173. }
  174. static int window_rating(HWND window,
  175. enum window_priority priority,
  176. const char *class,
  177. const char *title,
  178. const char *exe)
  179. {
  180. struct dstr cur_class = {0};
  181. struct dstr cur_title = {0};
  182. struct dstr cur_exe = {0};
  183. int class_val = 1;
  184. int title_val = 1;
  185. int exe_val = 0;
  186. int total = 0;
  187. if (!get_window_exe(&cur_exe, window))
  188. return 0;
  189. get_window_title(&cur_title, window);
  190. get_window_class(&cur_class, window);
  191. if (priority == WINDOW_PRIORITY_CLASS)
  192. class_val += 3;
  193. else if (priority == WINDOW_PRIORITY_TITLE)
  194. title_val += 3;
  195. else
  196. exe_val += 3;
  197. if (dstr_cmpi(&cur_class, class) == 0)
  198. total += class_val;
  199. if (dstr_cmpi(&cur_title, title) == 0)
  200. total += title_val;
  201. if (dstr_cmpi(&cur_exe, exe) == 0)
  202. total += exe_val;
  203. dstr_free(&cur_class);
  204. dstr_free(&cur_title);
  205. dstr_free(&cur_exe);
  206. return total;
  207. }
  208. HWND find_window(enum window_search_mode mode,
  209. enum window_priority priority,
  210. const char *class,
  211. const char *title,
  212. const char *exe)
  213. {
  214. HWND window = first_window(mode);
  215. HWND best_window = NULL;
  216. int best_rating = 0;
  217. while (window) {
  218. int rating = window_rating(window, priority, class, title, exe);
  219. if (rating > best_rating) {
  220. best_rating = rating;
  221. best_window = window;
  222. }
  223. window = next_window(window, mode);
  224. }
  225. return best_window;
  226. }