Skip to content

Commit 8e2b98e

Browse files
authored
Merge pull request #47 from DDRBoxman/js_events
Frontend event callbacks through js events. Getter support from frontend API.
2 parents 9b1cc2e + 8a2ae40 commit 8e2b98e

28 files changed

+5215
-65
lines changed

CMakeLists.txt

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ set(obs-browser_SOURCES
4646
obs-browser/main.cpp
4747
obs-browser/main-source.cpp
4848
shared/browser-source.cpp
49-
shared/base64.cpp)
49+
shared/base64.cpp
50+
shared/util.cpp)
5051

5152
set(obs-browser_HEADERS
5253
shared/browser-manager.hpp
@@ -56,7 +57,8 @@ set(obs-browser_HEADERS
5657
shared/browser-types.h
5758
shared/base64.hpp
5859
shared/browser-types.h
59-
shared/browser-version.h)
60+
shared/browser-version.h
61+
shared/util.hpp)
6062

6163
if (APPLE)
6264
list(APPEND obs-browser_SOURCES
@@ -94,7 +96,9 @@ else (APPLE)
9496
obs-browser/browser-render-handler.cpp
9597
obs-browser/browser-source-base.cpp
9698
obs-browser/browser-load-handler.cpp
97-
shared/browser-scheme.cpp)
99+
obs-browser/browser-obs-bridge-base.cpp
100+
shared/browser-scheme.cpp
101+
fmt/format.cc)
98102

99103
list(APPEND obs-browser_HEADERS
100104
shared/browser-client.hpp
@@ -105,13 +109,15 @@ else (APPLE)
105109
obs-browser/browser-render-handler.hpp
106110
obs-browser/browser-source-base.hpp
107111
obs-browser/browser-load-handler.hpp
108-
shared/browser-scheme.hpp)
112+
obs-browser/browser-obs-bridge-base.hpp
113+
shared/browser-scheme.hpp
114+
fmt/format.h)
109115

110116
endif(APPLE)
111117

112118
set(obs-browser_LIBRARIES
113-
libobs
114-
obs-frontend-api)
119+
obs-frontend-api
120+
${OBS_JANSSON_IMPORT})
115121

116122
if (APPLE)
117123
list(APPEND obs-browser_LIBRARIES
@@ -128,13 +134,17 @@ target_include_directories(obs-browser PRIVATE
128134
"obs-browser"
129135
"shared")
130136

137+
target_include_directories(obs-browser PUBLIC
138+
${OBS_JANSSON_INCLUDE_DIRS})
139+
131140
if (APPLE)
132141
target_include_directories(obs-browser PUBLIC
133142
"shared-apple")
134-
else(APPLE)
135-
target_include_directories_system(obs-browser PUBLIC ${CEF_INCLUDE_DIR})
143+
else (APPLE)
144+
target_include_directories_system(obs-browser PUBLIC ${CEF_ROOT_DIR})
136145
endif(APPLE)
137146

147+
138148
if (APPLE)
139149
set_xcode_property(obs-browser CLANG_CXX_LIBRARY "libc++")
140150
endif(APPLE)
@@ -160,6 +170,7 @@ if(APPLE)
160170
set(cef-isolation_SOURCES
161171
cef-isolation/main.mm
162172
cef-isolation/cef-isolated-client.mm
173+
cef-isolation/browser-obs-bridge-mac.mm
163174
cef-isolation/browser-handle.mm
164175
cef-isolation/browser-render-handler.mm
165176
cef-isolation/browser-texture-mac.mm
@@ -169,7 +180,8 @@ if(APPLE)
169180
shared/browser-app.cpp
170181
shared/browser-scheme.cpp
171182
shared/base64.cpp
172-
obs-browser/browser-load-handler.cpp)
183+
obs-browser/browser-load-handler.cpp
184+
fmt/format.cc)
173185

174186
set(cef-isolation_HEADERS
175187
shared-apple/cef-logging.h
@@ -179,6 +191,7 @@ if(APPLE)
179191
cef-isolation/browser-render-handler.hpp
180192
cef-isolation/browser-texture-mac.h
181193
cef-isolation/service-connection-delegate.h
194+
cef-isolation/browser-obs-bridge-mac.hpp
182195
shared/browser-texture.hpp
183196
shared/browser-client.hpp
184197
shared/browser-task.hpp
@@ -187,6 +200,7 @@ if(APPLE)
187200
obs-browser/browser-load-handler.hpp
188201
shared/browser-scheme.hpp
189202
shared/browser-types.h
203+
shared/browser-obs-bridge.hpp
190204
shared-apple/browser-bridges.h)
191205

192206
add_executable(cef-isolation
@@ -205,21 +219,27 @@ if(APPLE)
205219
"shared"
206220
"shared-apple")
207221

208-
target_include_directories_system(cef-isolation PUBLIC ${CEF_INCLUDE_DIR})
222+
target_include_directories_system(cef-isolation
223+
PUBLIC
224+
${CEF_INCLUDE_DIR}
225+
${OBS_JANSSON_INCLUDE_DIRS})
209226

210227
target_link_libraries(cef-isolation
211228
${CEF_LIBRARIES}
229+
${OBS_JANSSON_IMPORT}
212230
${APPKIT_FRAMEWORK}
213231
${IOSURFACE_FRAMEWORK})
214232

215233
endif(APPLE)
216234

217235
set(cef-bootstrap_SOURCES
218236
cef-bootstrap/main.cpp
219-
shared/browser-app.cpp)
237+
shared/browser-app.cpp
238+
fmt/format.cc)
220239

221240
set(cef-bootstrap_HEADERS
222-
shared/browser-app.hpp)
241+
shared/browser-app.hpp
242+
fmt/format.h)
223243

224244
add_executable(cef-bootstrap
225245
${cef-bootstrap_SOURCES}
@@ -239,26 +259,30 @@ if (APPLE AND XCODE)
239259
endif(APPLE AND XCODE)
240260

241261
target_include_directories(cef-bootstrap PRIVATE "shared")
242-
target_include_directories_system(cef-bootstrap PUBLIC ${CEF_INCLUDE_DIR})
262+
target_include_directories_system(cef-bootstrap
263+
PUBLIC
264+
${CEF_ROOT_DIR}
265+
${OBS_JANSSON_INCLUDE_DIRS})
243266

244267
target_link_libraries(cef-bootstrap
245-
${CEF_LIBRARIES})
268+
${CEF_LIBRARIES}
269+
${OBS_JANSSON_IMPORT})
246270

247271
if (APPLE)
248272

249273
set_target_properties(cef-isolation PROPERTIES
250274
OUTPUT_NAME "CEF"
251275
MACOSX_BUNDLE TRUE
252276
BUILD_WITH_INSTALL_RPATH TRUE
253-
INSTALL_RPATH "@loader_path/../Frameworks"
277+
INSTALL_RPATH "@executable_path/.."
254278
MACOSX_BUNDLE_BUNDLE_NAME "CEF Helper"
255279
MACOSX_BUNDLE_GUI_IDENTIFIER "org.catchexception.cef.cef-isolation")
256280

257281
set_target_properties(cef-bootstrap PROPERTIES
258282
OUTPUT_NAME "CEF Helper"
259283
MACOSX_BUNDLE TRUE
260284
BUILD_WITH_INSTALL_RPATH TRUE
261-
INSTALL_RPATH "@loader_path/../../../"
285+
INSTALL_RPATH "@executable_path/../../../.."
262286
MACOSX_BUNDLE_BUNDLE_NAME "CEF Helper"
263287
MACOSX_BUNDLE_GUI_IDENTIFIER "org.catchexception.cef.cef-bootstrap")
264288

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,23 @@ window.obsstudio.onVisibilityChange = function(visiblity) {
2626

2727
### Register for scene change callbacks
2828
```
29-
/**
30-
* onSceneChange gets callbacks when the scene is changed
31-
*
32-
* @param {string} sceneName - The name of the scene that was changed to.
33-
*/
34-
window.obsstudio.onSceneChange = function(sceneName) {
35-
36-
};
29+
window.addEventListener('obsSceneChanged', function(evt) {
30+
var t = document.createTextNode(evt.detail.name);
31+
document.body.appendChild(t);
32+
});
33+
```
34+
35+
### Get the current scene
36+
```
37+
window.obsstudio.getCurrentScene(function(data) {console.log(data);});
3738
```
3839

3940
## Building on OSX
4041

4142
### Building CEF
4243
#### Getting
4344
* Download CEF Mac 64 from [https://cefbuilds.com/](https://cefbuilds.com/)
44-
* At the time of writing this I used build 2704
45+
* At the time of writing this I used build 2883
4546
* Extract and cd into the folder
4647

4748
#### Setting Up Project
@@ -70,7 +71,7 @@ git clone [email protected]:kc5nra/obs-browser.git ./plugins/obs-browser
7071
echo "add_subdirectory(obs-browser)" >> ./plugins/CMakeLists.txt
7172
mkdir build
7273
cd ./build
73-
cmake -D CMAKE_PREFIX_PATH=/usr/local/opt/qt5/lib/cmake -D CEF_ROOT_DIR=/Users/username/Development/cef_binary_3.2704.1434.gec3e9ed_macosx64 -G Xcode ..
74+
cmake -D CMAKE_PREFIX_PATH=/usr/local/opt/qt5/lib/cmake -D CEF_ROOT_DIR=/Users/username/Development/cef_binary_3.2883.1540.gedbfb20_macosx64 -D BUILD_BROWSER=yes -G Xcode ..
7475
open obs-studio.xcodeproj/
7576
```
7677

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include "cef-isolation.h"
4+
#include "browser-obs-bridge.hpp"
5+
6+
class BrowserOBSBridgeMac : public BrowserOBSBridge
7+
{
8+
public:
9+
BrowserOBSBridgeMac(id<CEFIsolationService> cefIsolationService);
10+
11+
const char* GetCurrentSceneJSONData() override;
12+
13+
private:
14+
id<CEFIsolationService> cefIsolationService;
15+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "browser-obs-bridge-mac.hpp"
2+
3+
BrowserOBSBridgeMac::BrowserOBSBridgeMac(id<CEFIsolationService> cefIsolationService)
4+
: cefIsolationService(cefIsolationService)
5+
{}
6+
7+
const char* BrowserOBSBridgeMac::GetCurrentSceneJSONData()
8+
{
9+
return [cefIsolationService getCurrentSceneJSONData];
10+
}

cef-isolation/browser-render-handler.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class BrowserRenderHandler : public CefRenderHandler
2727

2828
BrowserRenderHandler(std::shared_ptr<BrowserHandle> &browserHandle);
2929

30-
3130
public: /* CefRenderHandler overrides */
3231

3332
virtual bool GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect)

cef-isolation/cef-isolated-client.mm

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
******************************************************************************/
1717

1818
#import <Cocoa/Cocoa.h>
19+
#import <Foundation/Foundation.h>
1920

2021
#include <include/cef_app.h>
2122
#include <include/cef_browser.h>
@@ -35,6 +36,8 @@
3536

3637
#import "cef-isolated-client.h"
3738

39+
#import "browser-obs-bridge-mac.hpp"
40+
3841
@implementation CEFIsolatedClient
3942

4043
- (id)init
@@ -73,6 +76,8 @@ - (int)createBrowser:(BrowserSettingsBridge *)browserSettings
7376
new BrowserHandle(browserSettings.width,
7477
browserSettings.height, _server));
7578

79+
BrowserOBSBridge *browserOBSBridge = new BrowserOBSBridgeMac(_server);
80+
7681
sync_on_cef_ui(^{
7782

7883
CefRefPtr<BrowserRenderHandler> browserRenderHandler =
@@ -82,7 +87,7 @@ new BrowserHandle(browserSettings.width,
8287
new BrowserLoadHandler(std::string([browserSettings.css UTF8String]));
8388

8489
CefRefPtr<BrowserClient> browserClient =
85-
new BrowserClient(browserRenderHandler.get(),loadHandler);
90+
new BrowserClient(browserRenderHandler.get(),loadHandler, browserOBSBridge);
8691

8792
CefWindowInfo windowInfo;
8893
windowInfo.view = nullptr;
@@ -286,13 +291,7 @@ - (void)sendKeyClick:(const int)browserIdentifier
286291
return;
287292
}
288293

289-
host->HandleKeyEventBeforeTextInputClient(nsEvent);
290-
291-
NSTextInputContext *inputContext =
292-
host->GetNSTextInputContext();
293-
[inputContext handleEvent:nsEvent];
294-
295-
host->HandleKeyEventAfterTextInputClient(nsEvent);
294+
host->SendKeyEvent(keyEvent);
296295
}
297296
}];
298297
}
@@ -335,7 +334,20 @@ - (void)refreshPageNoCache:(const int)browserIdentifier
335334
}];
336335
}
337336

337+
-(void)dispatchJSEvent:(const char *)eventName data:(const char *)jsonString
338+
{
339+
[self sendEventToAllBrowsers:^(SharedBrowserHandle browserHandle)
340+
{
341+
CefRefPtr<CefBrowser> browser = browserHandle->GetBrowser();
342+
343+
CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("DispatchJSEvent");
344+
CefRefPtr<CefListValue> args = msg->GetArgumentList();
345+
args->SetString(0, eventName);
346+
args->SetString(1, jsonString);
338347

348+
browser->SendProcessMessage(PID_RENDERER, msg);
349+
}];
350+
}
339351

340352
- (void)destroyBrowser:(const int)browserIdentifier {
341353
if (map.count(browserIdentifier) == 1) {

0 commit comments

Comments
 (0)