window-capture.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include <stdlib.h>
  2. #include <util/dstr.h>
  3. #include "dc-capture.h"
  4. #include "window-helpers.h"
  5. /* clang-format off */
  6. #define TEXT_WINDOW_CAPTURE obs_module_text("WindowCapture")
  7. #define TEXT_WINDOW obs_module_text("WindowCapture.Window")
  8. #define TEXT_MATCH_PRIORITY obs_module_text("WindowCapture.Priority")
  9. #define TEXT_MATCH_TITLE obs_module_text("WindowCapture.Priority.Title")
  10. #define TEXT_MATCH_CLASS obs_module_text("WindowCapture.Priority.Class")
  11. #define TEXT_MATCH_EXE obs_module_text("WindowCapture.Priority.Exe")
  12. #define TEXT_CAPTURE_CURSOR obs_module_text("CaptureCursor")
  13. #define TEXT_COMPATIBILITY obs_module_text("Compatibility")
  14. /* clang-format on */
  15. struct window_capture {
  16. obs_source_t *source;
  17. char *title;
  18. char *class;
  19. char *executable;
  20. enum window_priority priority;
  21. bool cursor;
  22. bool compatibility;
  23. bool use_wildcards; /* TODO */
  24. struct dc_capture capture;
  25. float resize_timer;
  26. float check_window_timer;
  27. float cursor_check_time;
  28. HWND window;
  29. RECT last_rect;
  30. };
  31. static void update_settings(struct window_capture *wc, obs_data_t *s)
  32. {
  33. const char *window = obs_data_get_string(s, "window");
  34. int priority = (int)obs_data_get_int(s, "priority");
  35. bfree(wc->title);
  36. bfree(wc->class);
  37. bfree(wc->executable);
  38. build_window_strings(window, &wc->class, &wc->title, &wc->executable);
  39. if (wc->title != NULL) {
  40. blog(LOG_INFO,
  41. "[window-capture: '%s'] update settings:\n"
  42. "\texecutable: %s",
  43. obs_source_get_name(wc->source), wc->executable);
  44. blog(LOG_DEBUG, "\tclass: %s", wc->class);
  45. }
  46. wc->priority = (enum window_priority)priority;
  47. wc->cursor = obs_data_get_bool(s, "cursor");
  48. wc->use_wildcards = obs_data_get_bool(s, "use_wildcards");
  49. wc->compatibility = obs_data_get_bool(s, "compatibility");
  50. }
  51. /* ------------------------------------------------------------------------- */
  52. static const char *wc_getname(void *unused)
  53. {
  54. UNUSED_PARAMETER(unused);
  55. return TEXT_WINDOW_CAPTURE;
  56. }
  57. static void *wc_create(obs_data_t *settings, obs_source_t *source)
  58. {
  59. struct window_capture *wc = bzalloc(sizeof(struct window_capture));
  60. wc->source = source;
  61. update_settings(wc, settings);
  62. return wc;
  63. }
  64. static void wc_destroy(void *data)
  65. {
  66. struct window_capture *wc = data;
  67. if (wc) {
  68. obs_enter_graphics();
  69. dc_capture_free(&wc->capture);
  70. obs_leave_graphics();
  71. bfree(wc->title);
  72. bfree(wc->class);
  73. bfree(wc->executable);
  74. bfree(wc);
  75. }
  76. }
  77. static void wc_update(void *data, obs_data_t *settings)
  78. {
  79. struct window_capture *wc = data;
  80. update_settings(wc, settings);
  81. /* forces a reset */
  82. wc->window = NULL;
  83. }
  84. static uint32_t wc_width(void *data)
  85. {
  86. struct window_capture *wc = data;
  87. return wc->capture.width;
  88. }
  89. static uint32_t wc_height(void *data)
  90. {
  91. struct window_capture *wc = data;
  92. return wc->capture.height;
  93. }
  94. static void wc_defaults(obs_data_t *defaults)
  95. {
  96. obs_data_set_default_bool(defaults, "cursor", true);
  97. obs_data_set_default_bool(defaults, "compatibility", false);
  98. }
  99. static obs_properties_t *wc_properties(void *unused)
  100. {
  101. UNUSED_PARAMETER(unused);
  102. obs_properties_t *ppts = obs_properties_create();
  103. obs_property_t *p;
  104. p = obs_properties_add_list(ppts, "window", TEXT_WINDOW,
  105. OBS_COMBO_TYPE_LIST,
  106. OBS_COMBO_FORMAT_STRING);
  107. fill_window_list(p, EXCLUDE_MINIMIZED, NULL);
  108. p = obs_properties_add_list(ppts, "priority", TEXT_MATCH_PRIORITY,
  109. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  110. obs_property_list_add_int(p, TEXT_MATCH_TITLE, WINDOW_PRIORITY_TITLE);
  111. obs_property_list_add_int(p, TEXT_MATCH_CLASS, WINDOW_PRIORITY_CLASS);
  112. obs_property_list_add_int(p, TEXT_MATCH_EXE, WINDOW_PRIORITY_EXE);
  113. obs_properties_add_bool(ppts, "cursor", TEXT_CAPTURE_CURSOR);
  114. obs_properties_add_bool(ppts, "compatibility", TEXT_COMPATIBILITY);
  115. return ppts;
  116. }
  117. #define RESIZE_CHECK_TIME 0.2f
  118. #define CURSOR_CHECK_TIME 0.2f
  119. static void wc_tick(void *data, float seconds)
  120. {
  121. struct window_capture *wc = data;
  122. RECT rect;
  123. bool reset_capture = false;
  124. if (!obs_source_showing(wc->source))
  125. return;
  126. if (!wc->window || !IsWindow(wc->window)) {
  127. if (!wc->title && !wc->class)
  128. return;
  129. wc->check_window_timer += seconds;
  130. if (wc->check_window_timer < 1.0f) {
  131. if (wc->capture.valid)
  132. dc_capture_free(&wc->capture);
  133. return;
  134. }
  135. wc->check_window_timer = 0.0f;
  136. wc->window = find_window(EXCLUDE_MINIMIZED, wc->priority,
  137. wc->class, wc->title, wc->executable);
  138. if (!wc->window) {
  139. if (wc->capture.valid)
  140. dc_capture_free(&wc->capture);
  141. return;
  142. }
  143. reset_capture = true;
  144. } else if (IsIconic(wc->window)) {
  145. return;
  146. }
  147. wc->cursor_check_time += seconds;
  148. if (wc->cursor_check_time > CURSOR_CHECK_TIME) {
  149. DWORD foreground_pid, target_pid;
  150. // Can't just compare the window handle in case of app with child windows
  151. if (!GetWindowThreadProcessId(GetForegroundWindow(),
  152. &foreground_pid))
  153. foreground_pid = 0;
  154. if (!GetWindowThreadProcessId(wc->window, &target_pid))
  155. target_pid = 0;
  156. if (foreground_pid && target_pid &&
  157. foreground_pid != target_pid)
  158. wc->capture.cursor_hidden = true;
  159. else
  160. wc->capture.cursor_hidden = false;
  161. wc->cursor_check_time = 0.0f;
  162. }
  163. obs_enter_graphics();
  164. GetClientRect(wc->window, &rect);
  165. if (!reset_capture) {
  166. wc->resize_timer += seconds;
  167. if (wc->resize_timer >= RESIZE_CHECK_TIME) {
  168. if (rect.bottom != wc->last_rect.bottom ||
  169. rect.right != wc->last_rect.right)
  170. reset_capture = true;
  171. wc->resize_timer = 0.0f;
  172. }
  173. }
  174. if (reset_capture) {
  175. wc->resize_timer = 0.0f;
  176. wc->last_rect = rect;
  177. dc_capture_free(&wc->capture);
  178. dc_capture_init(&wc->capture, 0, 0, rect.right, rect.bottom,
  179. wc->cursor, wc->compatibility);
  180. }
  181. dc_capture_capture(&wc->capture, wc->window);
  182. obs_leave_graphics();
  183. }
  184. static void wc_render(void *data, gs_effect_t *effect)
  185. {
  186. struct window_capture *wc = data;
  187. dc_capture_render(&wc->capture, obs_get_base_effect(OBS_EFFECT_OPAQUE));
  188. UNUSED_PARAMETER(effect);
  189. }
  190. struct obs_source_info window_capture_info = {
  191. .id = "window_capture",
  192. .type = OBS_SOURCE_TYPE_INPUT,
  193. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW,
  194. .get_name = wc_getname,
  195. .create = wc_create,
  196. .destroy = wc_destroy,
  197. .update = wc_update,
  198. .video_render = wc_render,
  199. .video_tick = wc_tick,
  200. .get_width = wc_width,
  201. .get_height = wc_height,
  202. .get_defaults = wc_defaults,
  203. .get_properties = wc_properties,
  204. .icon_type = OBS_ICON_TYPE_WINDOW_CAPTURE,
  205. };