window-capture.c 6.0 KB

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