Skip to content

Commit

Permalink
obs-browser: Add Linux browser panel support
Browse files Browse the repository at this point in the history
This adds browser panel support for Linux.
  • Loading branch information
cg2121 committed Dec 23, 2020
1 parent 751cd04 commit e3695cd
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 36 deletions.
4 changes: 1 addition & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ set(obs-browser_HEADERS
cef-headers.hpp
)

# only allow browser panels on win32 for now -- other operating systems
# need more testing
if(WIN32 AND BROWSER_PANEL_SUPPORT_ENABLED)
if(BROWSER_PANEL_SUPPORT_ENABLED)
list(APPEND obs-browser_SOURCES
panel/browser-panel.cpp
panel/browser-panel-client.cpp
Expand Down
2 changes: 2 additions & 0 deletions panel/browser-panel-internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class QCefWidgetInternal : public QCefWidget {
std::string script;
CefRefPtr<CefRequestContext> rqc;
QTimer timer;
QPointer<QWindow> window;
QPointer<QWidget> container;
bool allowAllPopups_ = false;

virtual void resizeEvent(QResizeEvent *event) override;
Expand Down
87 changes: 64 additions & 23 deletions panel/browser-panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include <util/base.h>
#include <thread>

#if !defined(_WIN32) && !defined(__APPLE__)
#include <X11/Xlib.h>
#endif

extern bool QueueCEFTask(std::function<void()> task);
extern "C" void obs_browser_initialize(void);
extern os_event_t *cef_started_event;
Expand Down Expand Up @@ -80,6 +84,10 @@ struct QCefCookieManagerInternal : QCefCookieManager {
throw "Browser thread not initialized";

BPtr<char> rpath = obs_module_config_path(storage_path.c_str());

if (os_mkdirs(rpath.Get()) != 0)
throw "Failed to create cookie diretory";

BPtr<char> path = os_get_abs_path_ptr(rpath.Get());

#if CHROME_VERSION_BUILD < 3770
Expand Down Expand Up @@ -165,6 +173,9 @@ QCefWidgetInternal::QCefWidgetInternal(QWidget *parent, const std::string &url_,
setAttribute(Qt::WA_NativeWindow);

setFocusPolicy(Qt::ClickFocus);

window = new QWindow();
window->setFlags(Qt::FramelessWindowHint);
}

QCefWidgetInternal::~QCefWidgetInternal()
Expand Down Expand Up @@ -220,28 +231,28 @@ void QCefWidgetInternal::closeBrowser()

void QCefWidgetInternal::Init()
{
WId id = winId();
WId handle = window->winId();

QSize size = this->size();
#ifdef SUPPORTS_FRACTIONAL_SCALING
size *= devicePixelRatioF();
#else
size *= devicePixelRatio();
#endif

bool success = QueueCEFTask([this, id]() {
bool success = QueueCEFTask([this, handle, size]() {
CefWindowInfo windowInfo;

/* Make sure Init isn't called more than once. */
if (cefBrowser)
return;

QSize size = this->size();
#ifdef _WIN32
#ifdef SUPPORTS_FRACTIONAL_SCALING
size *= devicePixelRatioF();
#elif
size *= devicePixelRatio();
#endif
RECT rc = {0, 0, size.width(), size.height()};
windowInfo.SetAsChild((HWND)id, rc);
#elif __APPLE__
windowInfo.SetAsChild((CefWindowHandle)id, 0, 0, size.width(),
size.height());
#else
CefRect rc = {0, 0, size.width(), size.height()};
#endif
windowInfo.SetAsChild((CefWindowHandle)handle, rc);

CefRefPtr<QCefBrowserClient> browserClient =
new QCefBrowserClient(this, script, allowAllPopups_);
Expand All @@ -253,13 +264,19 @@ void QCefWidgetInternal::Init()
CefRefPtr<CefDictionaryValue>(),
#endif
rqc);
#ifdef _WIN32
Resize();
#endif
});

if (success)
if (success) {
timer.stop();

if (!container) {
container =
QWidget::createWindowContainer(window, this);
container->show();
}

Resize();
}
}

void QCefWidgetInternal::resizeEvent(QResizeEvent *event)
Expand All @@ -270,23 +287,47 @@ void QCefWidgetInternal::resizeEvent(QResizeEvent *event)

void QCefWidgetInternal::Resize()
{
#ifdef _WIN32
#ifdef SUPPORTS_FRACTIONAL_SCALING
QSize size = this->size() * devicePixelRatioF();
#elif
#else
QSize size = this->size() * devicePixelRatio();
#endif

QueueCEFTask([this, size]() {
bool success = QueueCEFTask([this, size]() {
if (!cefBrowser)
return;
HWND hwnd = cefBrowser->GetHost()->GetWindowHandle();
SetWindowPos(hwnd, nullptr, 0, 0, size.width(), size.height(),

CefWindowHandle handle =
cefBrowser->GetHost()->GetWindowHandle();

if (!handle)
return;

#ifdef _WIN32
SetWindowPos((HWND)handle, nullptr, 0, 0, size.width(),
size.height(),
SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER);
SendMessage(hwnd, WM_SIZE, 0,
SendMessage((HWND)handle, WM_SIZE, 0,
MAKELPARAM(size.width(), size.height()));
});
#elif __APPLE__
#else
Display *xDisplay = cef_get_xdisplay();

if (!xDisplay)
return;

XWindowChanges changes = {0};
changes.x = 0;
changes.y = 0;
changes.width = size.width();
changes.height = size.height();
XConfigureWindow(xDisplay, (Window)handle,
CWX | CWY | CWHeight | CWWidth, &changes);
#endif
});

if (success && container)
container->resize(size.width(), size.height());
}

void QCefWidgetInternal::showEvent(QShowEvent *event)
Expand Down
23 changes: 13 additions & 10 deletions panel/browser-panel.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <obs-module.h>
#include <util/platform.h>
#include <util/util.hpp>
#include <QWidget>
Expand Down Expand Up @@ -67,13 +68,19 @@ struct QCef {
QObject *obj) = 0;
};

static void *get_browser_lib()
{
obs_module_t *browserModule = obs_get_module_by_name("obs-browser");

if (!browserModule)
return nullptr;

return obs_get_module_lib(browserModule);
}

static inline QCef *obs_browser_init_panel(void)
{
#ifdef _WIN32
void *lib = os_dlopen("obs-browser");
#else
void *lib = os_dlopen("../obs-plugins/obs-browser");
#endif
void *lib = get_browser_lib();
QCef *(*create_qcef)(void) = nullptr;

if (!lib) {
Expand All @@ -90,11 +97,7 @@ static inline QCef *obs_browser_init_panel(void)

static inline int obs_browser_qcef_version(void)
{
#ifdef _WIN32
void *lib = os_dlopen("obs-browser");
#else
void *lib = os_dlopen("../obs-plugins/obs-browser");
#endif
void *lib = get_browser_lib();
int (*qcef_version)(void) = nullptr;

if (!lib) {
Expand Down

0 comments on commit e3695cd

Please sign in to comment.