window-capture.c 9.8 KB

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