window-capture.c 6.4 KB

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