window-helpers.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #define inline __inline
  9. static inline void encode_dstr(struct dstr *str)
  10. {
  11. dstr_replace(str, "#", "#22");
  12. dstr_replace(str, ":", "#3A");
  13. }
  14. static inline char *decode_str(const char *src)
  15. {
  16. struct dstr str = {0};
  17. dstr_copy(&str, src);
  18. dstr_replace(&str, "#3A", ":");
  19. dstr_replace(&str, "#22", "#");
  20. return str.array;
  21. }
  22. extern void build_window_strings(const char *str,
  23. char **class,
  24. char **title,
  25. char **exe)
  26. {
  27. char **strlist;
  28. *class = NULL;
  29. *title = NULL;
  30. *exe = NULL;
  31. if (!str) {
  32. return;
  33. }
  34. strlist = strlist_split(str, ':', true);
  35. if (strlist && strlist[0] && strlist[1] && strlist[2]) {
  36. *title = decode_str(strlist[0]);
  37. *class = decode_str(strlist[1]);
  38. *exe = decode_str(strlist[2]);
  39. }
  40. strlist_free(strlist);
  41. }
  42. static HMODULE kernel32(void)
  43. {
  44. static HMODULE kernel32_handle = NULL;
  45. if (!kernel32_handle)
  46. kernel32_handle = GetModuleHandleA("kernel32");
  47. return kernel32_handle;
  48. }
  49. static inline HANDLE open_process(DWORD desired_access, bool inherit_handle,
  50. DWORD process_id)
  51. {
  52. static HANDLE (WINAPI *open_process_proc)(DWORD, BOOL, DWORD) = NULL;
  53. if (!open_process_proc)
  54. open_process_proc = get_obfuscated_func(kernel32(),
  55. "B}caZyah`~q", 0x2D5BEBAF6DDULL);
  56. return open_process_proc(desired_access, inherit_handle, process_id);
  57. }
  58. bool get_window_exe(struct dstr *name, HWND window)
  59. {
  60. wchar_t wname[MAX_PATH];
  61. struct dstr temp = {0};
  62. bool success = false;
  63. HANDLE process = NULL;
  64. char *slash;
  65. DWORD id;
  66. GetWindowThreadProcessId(window, &id);
  67. if (id == GetCurrentProcessId())
  68. return false;
  69. process = open_process(PROCESS_QUERY_LIMITED_INFORMATION, false, id);
  70. if (!process)
  71. goto fail;
  72. if (!GetProcessImageFileNameW(process, wname, MAX_PATH))
  73. goto fail;
  74. dstr_from_wcs(&temp, wname);
  75. slash = strrchr(temp.array, '\\');
  76. if (!slash)
  77. goto fail;
  78. dstr_copy(name, slash+1);
  79. success = true;
  80. fail:
  81. if (!success)
  82. dstr_copy(name, "unknown");
  83. dstr_free(&temp);
  84. CloseHandle(process);
  85. return true;
  86. }
  87. static void get_window_title(struct dstr *name, HWND hwnd)
  88. {
  89. wchar_t *temp;
  90. int len;
  91. len = GetWindowTextLengthW(hwnd);
  92. if (!len)
  93. return;
  94. temp = malloc(sizeof(wchar_t) * (len+1));
  95. GetWindowTextW(hwnd, temp, len+1);
  96. dstr_from_wcs(name, temp);
  97. free(temp);
  98. }
  99. static void get_window_class(struct dstr *class, HWND hwnd)
  100. {
  101. wchar_t temp[256];
  102. temp[0] = 0;
  103. GetClassNameW(hwnd, temp, sizeof(temp));
  104. dstr_from_wcs(class, temp);
  105. }
  106. static void add_window(obs_property_t *p, HWND hwnd)
  107. {
  108. struct dstr class = {0};
  109. struct dstr title = {0};
  110. struct dstr exe = {0};
  111. struct dstr encoded = {0};
  112. struct dstr desc = {0};
  113. if (!get_window_exe(&exe, hwnd))
  114. return;
  115. get_window_title(&title, hwnd);
  116. get_window_class(&class, hwnd);
  117. dstr_printf(&desc, "[%s]: %s", exe.array, title.array);
  118. encode_dstr(&title);
  119. encode_dstr(&class);
  120. encode_dstr(&exe);
  121. dstr_cat_dstr(&encoded, &title);
  122. dstr_cat(&encoded, ":");
  123. dstr_cat_dstr(&encoded, &class);
  124. dstr_cat(&encoded, ":");
  125. dstr_cat_dstr(&encoded, &exe);
  126. obs_property_list_add_string(p, desc.array, encoded.array);
  127. dstr_free(&encoded);
  128. dstr_free(&desc);
  129. dstr_free(&class);
  130. dstr_free(&title);
  131. dstr_free(&exe);
  132. }
  133. static bool check_window_valid(HWND window, enum window_search_mode mode)
  134. {
  135. DWORD styles, ex_styles;
  136. RECT rect;
  137. if (!IsWindowVisible(window) ||
  138. (mode == EXCLUDE_MINIMIZED && IsIconic(window)))
  139. return false;
  140. GetClientRect(window, &rect);
  141. styles = (DWORD)GetWindowLongPtr(window, GWL_STYLE);
  142. ex_styles = (DWORD)GetWindowLongPtr(window, GWL_EXSTYLE);
  143. if (ex_styles & WS_EX_TOOLWINDOW)
  144. return false;
  145. if (styles & WS_CHILD)
  146. return false;
  147. if (mode == EXCLUDE_MINIMIZED && (rect.bottom == 0 || rect.right == 0))
  148. return false;
  149. return true;
  150. }
  151. static inline HWND next_window(HWND window, enum window_search_mode mode)
  152. {
  153. while (true) {
  154. window = GetNextWindow(window, GW_HWNDNEXT);
  155. if (!window || check_window_valid(window, mode))
  156. break;
  157. }
  158. return window;
  159. }
  160. static inline HWND first_window(enum window_search_mode mode)
  161. {
  162. HWND window = GetWindow(GetDesktopWindow(), GW_CHILD);
  163. if (!check_window_valid(window, mode))
  164. window = next_window(window, mode);
  165. return window;
  166. }
  167. void fill_window_list(obs_property_t *p, enum window_search_mode mode)
  168. {
  169. HWND window = first_window(mode);
  170. while (window) {
  171. add_window(p, window);
  172. window = next_window(window, mode);
  173. }
  174. }
  175. static int window_rating(HWND window,
  176. enum window_priority priority,
  177. const char *class,
  178. const char *title,
  179. const char *exe)
  180. {
  181. struct dstr cur_class = {0};
  182. struct dstr cur_title = {0};
  183. struct dstr cur_exe = {0};
  184. int class_val = 1;
  185. int title_val = 1;
  186. int exe_val = 0;
  187. int total = 0;
  188. if (!get_window_exe(&cur_exe, window))
  189. return 0;
  190. get_window_title(&cur_title, window);
  191. get_window_class(&cur_class, window);
  192. if (priority == WINDOW_PRIORITY_CLASS)
  193. class_val += 3;
  194. else if (priority == WINDOW_PRIORITY_TITLE)
  195. title_val += 3;
  196. else
  197. exe_val += 3;
  198. if (dstr_cmpi(&cur_class, class) == 0)
  199. total += class_val;
  200. if (dstr_cmpi(&cur_title, title) == 0)
  201. total += title_val;
  202. if (dstr_cmpi(&cur_exe, exe) == 0)
  203. total += exe_val;
  204. dstr_free(&cur_class);
  205. dstr_free(&cur_title);
  206. dstr_free(&cur_exe);
  207. return total;
  208. }
  209. HWND find_window(enum window_search_mode mode,
  210. enum window_priority priority,
  211. const char *class,
  212. const char *title,
  213. const char *exe)
  214. {
  215. HWND window = first_window(mode);
  216. HWND best_window = NULL;
  217. int best_rating = 0;
  218. while (window) {
  219. int rating = window_rating(window, priority, class, title, exe);
  220. if (rating > best_rating) {
  221. best_rating = rating;
  222. best_window = window;
  223. }
  224. window = next_window(window, mode);
  225. }
  226. return best_window;
  227. }