Skip to content

Commit

Permalink
Add active/inactive JS callback
Browse files Browse the repository at this point in the history
  • Loading branch information
OsirisNL committed Jul 18, 2017
1 parent d252aee commit 6f1d458
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions obs-browser/browser-manager-base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -336,6 +341,17 @@ void BrowserManager::Impl::ExecuteVisiblityJSCallback(int browserIdentifier, boo
});
}

void BrowserManager::Impl::ExecuteActiveJSCallback(int browserIdentifier, bool active)
{
ExecuteOnBrowser(browserIdentifier, [&](CefRefPtr<CefBrowser> b)
{
CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("Active");
CefRefPtr<CefListValue> args = msg->GetArgumentList();
args->SetBool(0, active);
b->SendProcessMessage(PID_RENDERER, msg);
});
}

void BrowserManager::Impl::ExecuteSceneChangeJSCallback(const char *name)
{
ExecuteOnAllBrowsers([&](CefRefPtr<CefBrowser> b)
Expand Down
2 changes: 2 additions & 0 deletions obs-browser/browser-manager-base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions obs-browser/main-source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<BrowserSource *>(data);

bs->ExecuteActiveJSCallback(false);
}

// Called when the source is visible
Expand Down
8 changes: 7 additions & 1 deletion shared/browser-app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ bool BrowserApp::OnProcessMessageReceived(CefRefPtr<CefBrowser> 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<CefV8Context> context = browser->GetMainFrame()->GetV8Context();

Expand Down Expand Up @@ -161,7 +168,6 @@ bool BrowserApp::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
CefString jsonString = message->GetArgumentList()->GetString(1);

CefRefPtr<CefV8Value> callback = callbackMap[callbackID];

CefV8ValueList args;
args.push_back(CefV8Value::CreateString(jsonString));

Expand Down
2 changes: 2 additions & 0 deletions shared/browser-manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 5 additions & 0 deletions shared/browser-source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,8 @@ void BrowserSource::ExecuteVisiblityJSCallback(bool visible)
{
BrowserManager::Instance()->ExecuteVisiblityJSCallback(browserIdentifier, visible);
}

void BrowserSource::ExecuteActiveJSCallback(bool active)
{
BrowserManager::Instance()->ExecuteActiveJSCallback(browserIdentifier, active);
}
1 change: 1 addition & 0 deletions shared/browser-source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class BrowserSource {
std::shared_ptr<BrowserListener> CreateListener();

void ExecuteVisiblityJSCallback(bool visible);
void ExecuteActiveJSCallback(bool active);

private:
class Impl;
Expand Down

0 comments on commit 6f1d458

Please sign in to comment.