From 0c167d27a624f51bfacd31311f0053c8e2a09daf Mon Sep 17 00:00:00 2001 From: Richard Stanway Date: Wed, 10 May 2017 23:47:44 +0200 Subject: [PATCH] win-capture: Hide cursor when in background (window capture) --- plugins/win-capture/dc-capture.c | 2 +- plugins/win-capture/dc-capture.h | 1 + plugins/win-capture/window-capture.c | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/plugins/win-capture/dc-capture.c b/plugins/win-capture/dc-capture.c index d9cb8d20e..a19bb2902 100644 --- a/plugins/win-capture/dc-capture.c +++ b/plugins/win-capture/dc-capture.c @@ -167,7 +167,7 @@ void dc_capture_capture(struct dc_capture *capture, HWND window) ReleaseDC(NULL, hdc_target); - if (capture->cursor_captured) + if (capture->cursor_captured && !capture->cursor_hidden) draw_cursor(capture, hdc, window); dc_capture_release_dc(capture); diff --git a/plugins/win-capture/dc-capture.h b/plugins/win-capture/dc-capture.h index 2574aa35b..c05256737 100644 --- a/plugins/win-capture/dc-capture.h +++ b/plugins/win-capture/dc-capture.h @@ -23,6 +23,7 @@ struct dc_capture { bool capture_cursor; bool cursor_captured; + bool cursor_hidden; CURSORINFO ci; bool valid; diff --git a/plugins/win-capture/window-capture.c b/plugins/win-capture/window-capture.c index 25d3d2672..e478c278f 100644 --- a/plugins/win-capture/window-capture.c +++ b/plugins/win-capture/window-capture.c @@ -26,6 +26,7 @@ struct window_capture { struct dc_capture capture; float resize_timer; + float cursor_check_time; HWND window; RECT last_rect; @@ -134,6 +135,7 @@ static obs_properties_t *wc_properties(void *unused) } #define RESIZE_CHECK_TIME 0.2f +#define CURSOR_CHECK_TIME 0.2f static void wc_tick(void *data, float seconds) { @@ -162,6 +164,25 @@ static void wc_tick(void *data, float seconds) return; } + 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; + } + obs_enter_graphics(); GetClientRect(wc->window, &rect); -- GitLab