1
0

window-helpers.c 5.2 KB

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