window-capture.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. #include <stdlib.h>
  2. #include <util/dstr.h>
  3. #include "dc-capture.h"
  4. #include <psapi.h>
  5. #define TEXT_WINDOW_CAPTURE obs_module_text("WindowCapture")
  6. #define TEXT_WINDOW obs_module_text("WindowCapture.Window")
  7. #define TEXT_MATCH_PRIORITY obs_module_text("WindowCapture.Priority")
  8. #define TEXT_MATCH_TITLE obs_module_text("WindowCapture.Priority.Title")
  9. #define TEXT_MATCH_CLASS obs_module_text("WindowCapture.Priority.Class")
  10. #define TEXT_MATCH_EXE obs_module_text("WindowCapture.Priority.Exe")
  11. #define TEXT_CAPTURE_CURSOR obs_module_text("CaptureCursor")
  12. #define TEXT_COMPATIBILITY obs_module_text("Compatibility")
  13. enum window_priority {
  14. WINDOW_PRIORITY_CLASS,
  15. WINDOW_PRIORITY_TITLE,
  16. WINDOW_PRIORITY_EXE,
  17. };
  18. struct window_capture {
  19. obs_source_t source;
  20. char *title;
  21. char *class;
  22. char *executable;
  23. enum window_priority priority;
  24. bool cursor;
  25. bool compatibility;
  26. bool use_wildcards; /* TODO */
  27. struct dc_capture capture;
  28. float resize_timer;
  29. effect_t opaque_effect;
  30. HWND window;
  31. RECT last_rect;
  32. };
  33. void encode_dstr(struct dstr *str)
  34. {
  35. dstr_replace(str, "#", "#22");
  36. dstr_replace(str, ":", "#3A");
  37. }
  38. char *decode_str(const char *src)
  39. {
  40. struct dstr str = {0};
  41. dstr_copy(&str, src);
  42. dstr_replace(&str, "#3A", ":");
  43. dstr_replace(&str, "#22", "#");
  44. return str.array;
  45. }
  46. static void update_settings(struct window_capture *wc, obs_data_t s)
  47. {
  48. const char *window = obs_data_getstring(s, "window");
  49. int priority = (int)obs_data_getint(s, "priority");
  50. bfree(wc->title);
  51. bfree(wc->class);
  52. bfree(wc->executable);
  53. wc->title = NULL;
  54. wc->class = NULL;
  55. wc->executable = NULL;
  56. if (window) {
  57. char **strlist = strlist_split(window, ':', true);
  58. if (strlist && strlist[0] && strlist[1] && strlist[2]) {
  59. wc->title = decode_str(strlist[0]);
  60. wc->class = decode_str(strlist[1]);
  61. wc->executable = decode_str(strlist[2]);
  62. }
  63. strlist_free(strlist);
  64. }
  65. wc->priority = (enum window_priority)priority;
  66. wc->cursor = obs_data_getbool(s, "cursor");
  67. wc->use_wildcards = obs_data_getbool(s, "use_wildcards");
  68. }
  69. static bool get_exe_name(struct dstr *name, HWND window)
  70. {
  71. wchar_t wname[MAX_PATH];
  72. struct dstr temp = {0};
  73. bool success = false;
  74. HANDLE process = NULL;
  75. char *slash;
  76. DWORD id;
  77. GetWindowThreadProcessId(window, &id);
  78. if (id == GetCurrentProcessId())
  79. return false;
  80. process = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, id);
  81. if (!process)
  82. goto fail;
  83. if (!GetProcessImageFileNameW(process, wname, MAX_PATH))
  84. goto fail;
  85. dstr_from_wcs(&temp, wname);
  86. slash = strrchr(temp.array, '\\');
  87. if (!slash)
  88. goto fail;
  89. dstr_copy(name, slash+1);
  90. success = true;
  91. fail:
  92. if (!success)
  93. dstr_copy(name, "unknown");
  94. dstr_free(&temp);
  95. CloseHandle(process);
  96. return true;
  97. }
  98. static void get_window_title(struct dstr *name, HWND hwnd)
  99. {
  100. wchar_t *temp;
  101. int len;
  102. len = GetWindowTextLengthW(hwnd);
  103. if (!len)
  104. return;
  105. temp = malloc(sizeof(wchar_t) * (len+1));
  106. GetWindowTextW(hwnd, temp, len+1);
  107. dstr_from_wcs(name, temp);
  108. free(temp);
  109. }
  110. static void get_window_class(struct dstr *class, HWND hwnd)
  111. {
  112. wchar_t temp[256];
  113. temp[0] = 0;
  114. GetClassNameW(hwnd, temp, sizeof(temp));
  115. dstr_from_wcs(class, temp);
  116. }
  117. static void add_window(obs_property_t p, HWND hwnd,
  118. struct dstr *title,
  119. struct dstr *class,
  120. struct dstr *executable)
  121. {
  122. struct dstr encoded = {0};
  123. struct dstr desc = {0};
  124. if (!get_exe_name(executable, hwnd))
  125. return;
  126. get_window_title(title, hwnd);
  127. get_window_class(class, hwnd);
  128. dstr_printf(&desc, "[%s]: %s", executable->array, title->array);
  129. encode_dstr(title);
  130. encode_dstr(class);
  131. encode_dstr(executable);
  132. dstr_cat_dstr(&encoded, title);
  133. dstr_cat(&encoded, ":");
  134. dstr_cat_dstr(&encoded, class);
  135. dstr_cat(&encoded, ":");
  136. dstr_cat_dstr(&encoded, executable);
  137. obs_property_list_add_string(p, desc.array, encoded.array);
  138. dstr_free(&encoded);
  139. dstr_free(&desc);
  140. }
  141. static bool check_window_valid(HWND window,
  142. struct dstr *title,
  143. struct dstr *class,
  144. struct dstr *executable)
  145. {
  146. DWORD styles, ex_styles;
  147. RECT rect;
  148. if (!IsWindowVisible(window) || IsIconic(window))
  149. return false;
  150. GetClientRect(window, &rect);
  151. styles = (DWORD)GetWindowLongPtr(window, GWL_STYLE);
  152. ex_styles = (DWORD)GetWindowLongPtr(window, GWL_EXSTYLE);
  153. if (ex_styles & WS_EX_TOOLWINDOW)
  154. return false;
  155. if (styles & WS_CHILD)
  156. return false;
  157. if (rect.bottom == 0 || rect.right == 0)
  158. return false;
  159. if (!get_exe_name(executable, window))
  160. return false;
  161. get_window_title(title, window);
  162. get_window_class(class, window);
  163. return true;
  164. }
  165. static inline HWND next_window(HWND window,
  166. struct dstr *title,
  167. struct dstr *class,
  168. struct dstr *exe)
  169. {
  170. while (true) {
  171. window = GetNextWindow(window, GW_HWNDNEXT);
  172. if (!window || check_window_valid(window, title, class, exe))
  173. break;
  174. }
  175. return window;
  176. }
  177. static inline HWND first_window(
  178. struct dstr *title,
  179. struct dstr *class,
  180. struct dstr *executable)
  181. {
  182. HWND window = GetWindow(GetDesktopWindow(), GW_CHILD);
  183. if (!check_window_valid(window, title, class, executable))
  184. window = next_window(window, title, class, executable);
  185. return window;
  186. }
  187. static void fill_window_list(obs_property_t p)
  188. {
  189. struct dstr title = {0};
  190. struct dstr class = {0};
  191. struct dstr executable = {0};
  192. HWND window = first_window(&title, &class, &executable);
  193. while (window) {
  194. add_window(p, window, &title, &class, &executable);
  195. window = next_window(window, &title, &class, &executable);
  196. }
  197. dstr_free(&title);
  198. dstr_free(&class);
  199. dstr_free(&executable);
  200. }
  201. static int window_rating(struct window_capture *wc,
  202. struct dstr *title,
  203. struct dstr *class,
  204. struct dstr *executable)
  205. {
  206. int class_val = 1;
  207. int title_val = 1;
  208. int exe_val = 0;
  209. int total = 0;
  210. if (wc->priority == WINDOW_PRIORITY_CLASS)
  211. class_val += 3;
  212. else if (wc->priority == WINDOW_PRIORITY_TITLE)
  213. title_val += 3;
  214. else
  215. exe_val += 3;
  216. if (dstr_cmpi(class, wc->class) == 0)
  217. total += class_val;
  218. if (dstr_cmpi(title, wc->title) == 0)
  219. total += title_val;
  220. if (dstr_cmpi(executable, wc->executable) == 0)
  221. total += exe_val;
  222. return total;
  223. }
  224. static HWND find_window(struct window_capture *wc)
  225. {
  226. struct dstr title = {0};
  227. struct dstr class = {0};
  228. struct dstr exe = {0};
  229. HWND window = first_window(&title, &class, &exe);
  230. HWND best_window = NULL;
  231. int best_rating = 0;
  232. while (window) {
  233. int rating = window_rating(wc, &title, &class, &exe);
  234. if (rating > best_rating) {
  235. best_rating = rating;
  236. best_window = window;
  237. }
  238. window = next_window(window, &title, &class, &exe);
  239. }
  240. dstr_free(&title);
  241. dstr_free(&class);
  242. dstr_free(&exe);
  243. return best_window;
  244. }
  245. /* ------------------------------------------------------------------------- */
  246. static const char *wc_getname(void)
  247. {
  248. return TEXT_WINDOW_CAPTURE;
  249. }
  250. static void *wc_create(obs_data_t settings, obs_source_t source)
  251. {
  252. struct window_capture *wc;
  253. effect_t opaque_effect = create_opaque_effect();
  254. if (!opaque_effect)
  255. return NULL;
  256. wc = bzalloc(sizeof(struct window_capture));
  257. wc->source = source;
  258. wc->opaque_effect = opaque_effect;
  259. update_settings(wc, settings);
  260. return wc;
  261. }
  262. static void wc_destroy(void *data)
  263. {
  264. struct window_capture *wc = data;
  265. if (wc) {
  266. dc_capture_free(&wc->capture);
  267. bfree(wc->title);
  268. bfree(wc->class);
  269. bfree(wc->executable);
  270. gs_entercontext(obs_graphics());
  271. effect_destroy(wc->opaque_effect);
  272. gs_leavecontext();
  273. bfree(wc);
  274. }
  275. }
  276. static void wc_update(void *data, obs_data_t settings)
  277. {
  278. struct window_capture *wc = data;
  279. update_settings(wc, settings);
  280. /* forces a reset */
  281. wc->window = NULL;
  282. }
  283. static uint32_t wc_width(void *data)
  284. {
  285. struct window_capture *wc = data;
  286. return wc->capture.width;
  287. }
  288. static uint32_t wc_height(void *data)
  289. {
  290. struct window_capture *wc = data;
  291. return wc->capture.height;
  292. }
  293. static void wc_defaults(obs_data_t defaults)
  294. {
  295. obs_data_setbool(defaults, "cursor", true);
  296. obs_data_setbool(defaults, "compatibility", false);
  297. }
  298. static obs_properties_t wc_properties(void)
  299. {
  300. obs_properties_t ppts = obs_properties_create();
  301. obs_property_t p;
  302. p = obs_properties_add_list(ppts, "window", TEXT_WINDOW,
  303. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  304. fill_window_list(p);
  305. p = obs_properties_add_list(ppts, "priority", TEXT_MATCH_PRIORITY,
  306. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  307. obs_property_list_add_int(p, TEXT_MATCH_TITLE, WINDOW_PRIORITY_TITLE);
  308. obs_property_list_add_int(p, TEXT_MATCH_CLASS, WINDOW_PRIORITY_CLASS);
  309. obs_property_list_add_int(p, TEXT_MATCH_EXE, WINDOW_PRIORITY_EXE);
  310. obs_properties_add_bool(ppts, "cursor", TEXT_CAPTURE_CURSOR);
  311. obs_properties_add_bool(ppts, "compatibility", TEXT_COMPATIBILITY);
  312. return ppts;
  313. }
  314. #define RESIZE_CHECK_TIME 0.2f
  315. static void wc_tick(void *data, float seconds)
  316. {
  317. struct window_capture *wc = data;
  318. RECT rect;
  319. bool reset_capture = false;
  320. if (!wc->window || !IsWindow(wc->window)) {
  321. if (!wc->title && !wc->class)
  322. return;
  323. wc->window = find_window(wc);
  324. if (!wc->window)
  325. return;
  326. reset_capture = true;
  327. } else if (IsIconic(wc->window)) {
  328. return;
  329. }
  330. gs_entercontext(obs_graphics());
  331. GetClientRect(wc->window, &rect);
  332. if (!reset_capture) {
  333. wc->resize_timer += seconds;
  334. if (wc->resize_timer >= RESIZE_CHECK_TIME) {
  335. if (rect.bottom != wc->last_rect.bottom ||
  336. rect.right != wc->last_rect.right)
  337. reset_capture = true;
  338. wc->resize_timer = 0.0f;
  339. }
  340. }
  341. if (reset_capture) {
  342. wc->resize_timer = 0.0f;
  343. wc->last_rect = rect;
  344. dc_capture_free(&wc->capture);
  345. dc_capture_init(&wc->capture, 0, 0, rect.right, rect.bottom,
  346. wc->cursor, wc->compatibility);
  347. }
  348. dc_capture_capture(&wc->capture, wc->window);
  349. gs_leavecontext();
  350. }
  351. static void wc_render(void *data, effect_t effect)
  352. {
  353. struct window_capture *wc = data;
  354. dc_capture_render(&wc->capture, wc->opaque_effect);
  355. UNUSED_PARAMETER(effect);
  356. }
  357. struct obs_source_info window_capture_info = {
  358. .id = "window_capture",
  359. .type = OBS_SOURCE_TYPE_INPUT,
  360. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW,
  361. .getname = wc_getname,
  362. .create = wc_create,
  363. .destroy = wc_destroy,
  364. .update = wc_update,
  365. .getwidth = wc_width,
  366. .getheight = wc_height,
  367. .defaults = wc_defaults,
  368. .properties = wc_properties,
  369. .video_render = wc_render,
  370. .video_tick = wc_tick
  371. };