diff --git a/README.md b/README.md index 4fc07881e..844cec032 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,18 @@ window.obsstudio.onVisibilityChange = function(visiblity) { }; ``` +### Register for active/inactive callbacks +``` +/** + * onActiveChange gets callbacks when the active/inactive state of the browser source changes in OBS + * + * @param {bool} True -> active, False -> inactive + */ +window.obsstudio.onActiveChange = function(active) { + +}; +``` + ### Register for scene change callbacks ``` window.addEventListener('obsSceneChanged', function(evt) { diff --git a/obs-browser/browser-manager-base.cpp b/obs-browser/browser-manager-base.cpp index e157ed3c0..700c018c7 100644 --- a/obs-browser/browser-manager-base.cpp +++ b/obs-browser/browser-manager-base.cpp @@ -81,6 +81,11 @@ void BrowserManager::ExecuteVisiblityJSCallback(int browserIdentifier, bool visi pimpl->ExecuteVisiblityJSCallback(browserIdentifier, visible); } +void BrowserManager::ExecuteActiveJSCallback(int browserIdentifier, bool active) +{ + pimpl->ExecuteActiveJSCallback(browserIdentifier, active); +} + void BrowserManager::ExecuteSceneChangeJSCallback(const char *name) { pimpl->ExecuteSceneChangeJSCallback(name); @@ -336,6 +341,17 @@ void BrowserManager::Impl::ExecuteVisiblityJSCallback(int browserIdentifier, boo }); } +void BrowserManager::Impl::ExecuteActiveJSCallback(int browserIdentifier, bool active) +{ + ExecuteOnBrowser(browserIdentifier, [&](CefRefPtr b) + { + CefRefPtr msg = CefProcessMessage::Create("Active"); + CefRefPtr args = msg->GetArgumentList(); + args->SetBool(0, active); + b->SendProcessMessage(PID_RENDERER, msg); + }); +} + void BrowserManager::Impl::ExecuteSceneChangeJSCallback(const char *name) { ExecuteOnAllBrowsers([&](CefRefPtr b) diff --git a/obs-browser/browser-manager-base.hpp b/obs-browser/browser-manager-base.hpp index 98d796962..5564f1894 100644 --- a/obs-browser/browser-manager-base.hpp +++ b/obs-browser/browser-manager-base.hpp @@ -56,6 +56,8 @@ class BrowserManager::Impl void ExecuteVisiblityJSCallback(int browserIdentifier, bool visible); + void ExecuteActiveJSCallback(int browserIdentifier, bool active); + void ExecuteSceneChangeJSCallback(const char *name); void RefreshPageNoCache(int browserIdentifier); diff --git a/obs-browser/main-source.cpp b/obs-browser/main-source.cpp index af486a0df..8256a4b99 100644 --- a/obs-browser/main-source.cpp +++ b/obs-browser/main-source.cpp @@ -154,11 +154,15 @@ static void browser_source_activate(void *data) if (restart) BrowserManager::Instance()->RefreshPageNoCache(bs->GetBrowserIdentifier()); + + bs->ExecuteActiveJSCallback(true); } static void browser_source_deactivate(void *data) { + BrowserSource *bs = static_cast(data); + bs->ExecuteActiveJSCallback(false); } // Called when the source is visible diff --git a/shared/browser-app.cpp b/shared/browser-app.cpp index 260fef4e7..d5a70dcfa 100644 --- a/shared/browser-app.cpp +++ b/shared/browser-app.cpp @@ -110,6 +110,13 @@ bool BrowserApp::OnProcessMessageReceived(CefRefPtr browser, ExecuteJSFunction(browser, "onVisibilityChange", arguments); return true; } + else if (message->GetName() == "Active") { + CefV8ValueList arguments; + arguments.push_back(CefV8Value::CreateBool(args->GetBool(0))); + + ExecuteJSFunction(browser, "onActiveChange", arguments); + return true; + } else if (message->GetName() == "DispatchJSEvent") { CefRefPtr context = browser->GetMainFrame()->GetV8Context(); @@ -161,7 +168,6 @@ bool BrowserApp::OnProcessMessageReceived(CefRefPtr browser, CefString jsonString = message->GetArgumentList()->GetString(1); CefRefPtr callback = callbackMap[callbackID]; - CefV8ValueList args; args.push_back(CefV8Value::CreateString(jsonString)); diff --git a/shared/browser-manager.hpp b/shared/browser-manager.hpp index fa9c73202..6570c1785 100644 --- a/shared/browser-manager.hpp +++ b/shared/browser-manager.hpp @@ -61,6 +61,8 @@ class BrowserManager { const char *GetModulePath() { return path; } void ExecuteVisiblityJSCallback(int browserIdentifier, bool visible); + + void ExecuteActiveJSCallback(int browserIdentifier, bool active); void ExecuteSceneChangeJSCallback(const char *name); diff --git a/shared/browser-source.cpp b/shared/browser-source.cpp index e1e035a2d..8ea5b0dcb 100644 --- a/shared/browser-source.cpp +++ b/shared/browser-source.cpp @@ -94,3 +94,8 @@ void BrowserSource::ExecuteVisiblityJSCallback(bool visible) { BrowserManager::Instance()->ExecuteVisiblityJSCallback(browserIdentifier, visible); } + +void BrowserSource::ExecuteActiveJSCallback(bool active) +{ + BrowserManager::Instance()->ExecuteActiveJSCallback(browserIdentifier, active); +} diff --git a/shared/browser-source.hpp b/shared/browser-source.hpp index 676bae7ad..7317eb12c 100644 --- a/shared/browser-source.hpp +++ b/shared/browser-source.hpp @@ -69,6 +69,7 @@ class BrowserSource { std::shared_ptr CreateListener(); void ExecuteVisiblityJSCallback(bool visible); + void ExecuteActiveJSCallback(bool active); private: class Impl;