window-capture.c 6.2 KB

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