2014-04-07 01:20:36 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <util/dstr.h>
|
|
|
|
#include "dc-capture.h"
|
2014-10-17 04:01:18 -07:00
|
|
|
#include "window-helpers.h"
|
2014-04-07 01:20:36 -07:00
|
|
|
|
2014-07-09 22:12:57 -07:00
|
|
|
#define TEXT_WINDOW_CAPTURE obs_module_text("WindowCapture")
|
|
|
|
#define TEXT_WINDOW obs_module_text("WindowCapture.Window")
|
|
|
|
#define TEXT_MATCH_PRIORITY obs_module_text("WindowCapture.Priority")
|
|
|
|
#define TEXT_MATCH_TITLE obs_module_text("WindowCapture.Priority.Title")
|
|
|
|
#define TEXT_MATCH_CLASS obs_module_text("WindowCapture.Priority.Class")
|
|
|
|
#define TEXT_MATCH_EXE obs_module_text("WindowCapture.Priority.Exe")
|
|
|
|
#define TEXT_CAPTURE_CURSOR obs_module_text("CaptureCursor")
|
|
|
|
#define TEXT_COMPATIBILITY obs_module_text("Compatibility")
|
|
|
|
|
2014-04-07 01:20:36 -07:00
|
|
|
struct window_capture {
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_source_t *source;
|
2014-04-07 01:20:36 -07:00
|
|
|
|
|
|
|
char *title;
|
|
|
|
char *class;
|
|
|
|
char *executable;
|
|
|
|
enum window_priority priority;
|
|
|
|
bool cursor;
|
|
|
|
bool compatibility;
|
|
|
|
bool use_wildcards; /* TODO */
|
|
|
|
|
|
|
|
struct dc_capture capture;
|
|
|
|
|
|
|
|
float resize_timer;
|
2017-05-10 14:47:44 -07:00
|
|
|
float cursor_check_time;
|
2014-04-07 01:20:36 -07:00
|
|
|
|
|
|
|
HWND window;
|
|
|
|
RECT last_rect;
|
|
|
|
};
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static void update_settings(struct window_capture *wc, obs_data_t *s)
|
2014-04-07 01:20:36 -07:00
|
|
|
{
|
2014-08-05 11:09:29 -07:00
|
|
|
const char *window = obs_data_get_string(s, "window");
|
|
|
|
int priority = (int)obs_data_get_int(s, "priority");
|
2014-04-07 01:20:36 -07:00
|
|
|
|
|
|
|
bfree(wc->title);
|
|
|
|
bfree(wc->class);
|
|
|
|
bfree(wc->executable);
|
|
|
|
|
2014-10-17 04:01:18 -07:00
|
|
|
build_window_strings(window, &wc->class, &wc->title, &wc->executable);
|
2014-04-07 01:20:36 -07:00
|
|
|
|
2017-05-26 01:20:23 -07:00
|
|
|
if (wc->title != NULL) {
|
|
|
|
blog(LOG_INFO, "[window-capture: '%s'] update settings:\n"
|
|
|
|
"\texecutable: %s",
|
|
|
|
obs_source_get_name(wc->source),
|
|
|
|
wc->executable);
|
|
|
|
blog(LOG_DEBUG, "\tclass: %s", wc->class);
|
|
|
|
}
|
|
|
|
|
2014-04-07 01:20:36 -07:00
|
|
|
wc->priority = (enum window_priority)priority;
|
2014-08-05 11:09:29 -07:00
|
|
|
wc->cursor = obs_data_get_bool(s, "cursor");
|
|
|
|
wc->use_wildcards = obs_data_get_bool(s, "use_wildcards");
|
2015-05-01 01:58:21 -07:00
|
|
|
wc->compatibility = obs_data_get_bool(s, "compatibility");
|
2014-04-07 01:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
2015-09-16 01:30:51 -07:00
|
|
|
static const char *wc_getname(void *unused)
|
2014-04-07 01:20:36 -07:00
|
|
|
{
|
2015-09-16 01:30:51 -07:00
|
|
|
UNUSED_PARAMETER(unused);
|
2014-07-09 22:12:57 -07:00
|
|
|
return TEXT_WINDOW_CAPTURE;
|
2014-04-07 01:20:36 -07:00
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static void *wc_create(obs_data_t *settings, obs_source_t *source)
|
2014-04-07 01:20:36 -07:00
|
|
|
{
|
2015-03-14 00:38:09 -07:00
|
|
|
struct window_capture *wc = bzalloc(sizeof(struct window_capture));
|
|
|
|
wc->source = source;
|
2014-04-07 01:20:36 -07:00
|
|
|
|
|
|
|
update_settings(wc, settings);
|
|
|
|
return wc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wc_destroy(void *data)
|
|
|
|
{
|
|
|
|
struct window_capture *wc = data;
|
|
|
|
|
|
|
|
if (wc) {
|
2015-03-14 00:38:09 -07:00
|
|
|
obs_enter_graphics();
|
2014-04-07 01:20:36 -07:00
|
|
|
dc_capture_free(&wc->capture);
|
2015-03-14 00:38:09 -07:00
|
|
|
obs_leave_graphics();
|
2014-04-07 01:20:36 -07:00
|
|
|
|
|
|
|
bfree(wc->title);
|
|
|
|
bfree(wc->class);
|
|
|
|
bfree(wc->executable);
|
|
|
|
|
|
|
|
bfree(wc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static void wc_update(void *data, obs_data_t *settings)
|
2014-04-07 01:20:36 -07:00
|
|
|
{
|
|
|
|
struct window_capture *wc = data;
|
|
|
|
update_settings(wc, settings);
|
|
|
|
|
|
|
|
/* forces a reset */
|
|
|
|
wc->window = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t wc_width(void *data)
|
|
|
|
{
|
|
|
|
struct window_capture *wc = data;
|
|
|
|
return wc->capture.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t wc_height(void *data)
|
|
|
|
{
|
|
|
|
struct window_capture *wc = data;
|
|
|
|
return wc->capture.height;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static void wc_defaults(obs_data_t *defaults)
|
2014-04-07 01:20:36 -07:00
|
|
|
{
|
2014-08-05 10:58:10 -07:00
|
|
|
obs_data_set_default_bool(defaults, "cursor", true);
|
|
|
|
obs_data_set_default_bool(defaults, "compatibility", false);
|
2014-04-07 01:20:36 -07:00
|
|
|
}
|
|
|
|
|
2014-09-29 08:36:13 -07:00
|
|
|
static obs_properties_t *wc_properties(void *unused)
|
2014-04-07 01:20:36 -07:00
|
|
|
{
|
2014-09-29 08:36:13 -07:00
|
|
|
UNUSED_PARAMETER(unused);
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_properties_t *ppts = obs_properties_create();
|
|
|
|
obs_property_t *p;
|
2014-04-07 01:20:36 -07:00
|
|
|
|
2014-07-09 22:12:57 -07:00
|
|
|
p = obs_properties_add_list(ppts, "window", TEXT_WINDOW,
|
2014-04-07 01:20:36 -07:00
|
|
|
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
|
2016-07-30 12:49:05 -07:00
|
|
|
fill_window_list(p, EXCLUDE_MINIMIZED, NULL);
|
2014-04-07 01:20:36 -07:00
|
|
|
|
2014-07-09 22:12:57 -07:00
|
|
|
p = obs_properties_add_list(ppts, "priority", TEXT_MATCH_PRIORITY,
|
2014-04-07 01:20:36 -07:00
|
|
|
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
|
2014-07-09 22:12:57 -07:00
|
|
|
obs_property_list_add_int(p, TEXT_MATCH_TITLE, WINDOW_PRIORITY_TITLE);
|
|
|
|
obs_property_list_add_int(p, TEXT_MATCH_CLASS, WINDOW_PRIORITY_CLASS);
|
|
|
|
obs_property_list_add_int(p, TEXT_MATCH_EXE, WINDOW_PRIORITY_EXE);
|
2014-04-07 01:20:36 -07:00
|
|
|
|
2014-07-09 22:12:57 -07:00
|
|
|
obs_properties_add_bool(ppts, "cursor", TEXT_CAPTURE_CURSOR);
|
2014-04-07 01:20:36 -07:00
|
|
|
|
2014-07-09 22:12:57 -07:00
|
|
|
obs_properties_add_bool(ppts, "compatibility", TEXT_COMPATIBILITY);
|
2014-04-07 01:20:36 -07:00
|
|
|
|
|
|
|
return ppts;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define RESIZE_CHECK_TIME 0.2f
|
2017-05-10 14:47:44 -07:00
|
|
|
#define CURSOR_CHECK_TIME 0.2f
|
2014-04-07 01:20:36 -07:00
|
|
|
|
|
|
|
static void wc_tick(void *data, float seconds)
|
|
|
|
{
|
|
|
|
struct window_capture *wc = data;
|
|
|
|
RECT rect;
|
|
|
|
bool reset_capture = false;
|
|
|
|
|
2015-01-03 22:39:21 -08:00
|
|
|
if (!obs_source_showing(wc->source))
|
|
|
|
return;
|
|
|
|
|
2014-04-07 01:20:36 -07:00
|
|
|
if (!wc->window || !IsWindow(wc->window)) {
|
|
|
|
if (!wc->title && !wc->class)
|
|
|
|
return;
|
|
|
|
|
2014-10-17 04:01:18 -07:00
|
|
|
wc->window = find_window(EXCLUDE_MINIMIZED, wc->priority,
|
|
|
|
wc->class, wc->title, wc->executable);
|
2016-05-15 04:35:01 -07:00
|
|
|
if (!wc->window) {
|
|
|
|
if (wc->capture.valid)
|
|
|
|
dc_capture_free(&wc->capture);
|
2014-04-07 01:20:36 -07:00
|
|
|
return;
|
2016-05-15 04:35:01 -07:00
|
|
|
}
|
2014-04-07 01:20:36 -07:00
|
|
|
|
|
|
|
reset_capture = true;
|
|
|
|
|
|
|
|
} else if (IsIconic(wc->window)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-10 14:47:44 -07:00
|
|
|
wc->cursor_check_time += seconds;
|
|
|
|
if (wc->cursor_check_time > CURSOR_CHECK_TIME) {
|
|
|
|
DWORD foreground_pid, target_pid;
|
|
|
|
|
|
|
|
// Can't just compare the window handle in case of app with child windows
|
|
|
|
if (!GetWindowThreadProcessId(GetForegroundWindow(), &foreground_pid))
|
|
|
|
foreground_pid = 0;
|
|
|
|
|
|
|
|
if (!GetWindowThreadProcessId(wc->window, &target_pid))
|
|
|
|
target_pid = 0;
|
|
|
|
|
|
|
|
if (foreground_pid && target_pid && foreground_pid != target_pid)
|
|
|
|
wc->capture.cursor_hidden = true;
|
|
|
|
else
|
|
|
|
wc->capture.cursor_hidden = false;
|
|
|
|
|
|
|
|
wc->cursor_check_time = 0.0f;
|
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:58 -07:00
|
|
|
obs_enter_graphics();
|
2014-04-07 01:20:36 -07:00
|
|
|
|
|
|
|
GetClientRect(wc->window, &rect);
|
|
|
|
|
|
|
|
if (!reset_capture) {
|
|
|
|
wc->resize_timer += seconds;
|
|
|
|
|
|
|
|
if (wc->resize_timer >= RESIZE_CHECK_TIME) {
|
|
|
|
if (rect.bottom != wc->last_rect.bottom ||
|
|
|
|
rect.right != wc->last_rect.right)
|
|
|
|
reset_capture = true;
|
|
|
|
|
|
|
|
wc->resize_timer = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reset_capture) {
|
|
|
|
wc->resize_timer = 0.0f;
|
|
|
|
wc->last_rect = rect;
|
|
|
|
dc_capture_free(&wc->capture);
|
|
|
|
dc_capture_init(&wc->capture, 0, 0, rect.right, rect.bottom,
|
|
|
|
wc->cursor, wc->compatibility);
|
|
|
|
}
|
|
|
|
|
|
|
|
dc_capture_capture(&wc->capture, wc->window);
|
2014-08-04 05:48:58 -07:00
|
|
|
obs_leave_graphics();
|
2014-04-07 01:20:36 -07:00
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static void wc_render(void *data, gs_effect_t *effect)
|
2014-04-07 01:20:36 -07:00
|
|
|
{
|
|
|
|
struct window_capture *wc = data;
|
2015-10-16 07:31:52 -07:00
|
|
|
dc_capture_render(&wc->capture, obs_get_base_effect(OBS_EFFECT_OPAQUE));
|
2014-07-12 11:59:07 -07:00
|
|
|
|
|
|
|
UNUSED_PARAMETER(effect);
|
2014-04-07 01:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
struct obs_source_info window_capture_info = {
|
2014-08-04 21:27:52 -07:00
|
|
|
.id = "window_capture",
|
|
|
|
.type = OBS_SOURCE_TYPE_INPUT,
|
|
|
|
.output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW,
|
|
|
|
.get_name = wc_getname,
|
|
|
|
.create = wc_create,
|
|
|
|
.destroy = wc_destroy,
|
|
|
|
.update = wc_update,
|
|
|
|
.video_render = wc_render,
|
|
|
|
.video_tick = wc_tick,
|
|
|
|
.get_width = wc_width,
|
|
|
|
.get_height = wc_height,
|
|
|
|
.get_defaults = wc_defaults,
|
|
|
|
.get_properties = wc_properties
|
2014-04-07 01:20:36 -07:00
|
|
|
};
|