Skip to content

Commit

Permalink
win32 webview setup via choc, WIP
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <[email protected]>
  • Loading branch information
falkTX committed Mar 20, 2024
1 parent 46bea66 commit aae3a52
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/plugin/DesktopUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class DesktopUI : public UI,
{
case 1:
if (webview != nullptr)
reloadWebView(webview);
reloadWebView(webview, kPortNumOffset + port * 3 + 2);
break;
case 2:
openUserFilesDir();
Expand Down
6 changes: 6 additions & 0 deletions src/plugin/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ endif

TARGETS = features au clap lv2_sep vst2 vst3

# ifeq ($(WINDOWS),true)
# BUILD_CXX_FLAGS += -DUSING_CHOC
# LINK_FLAGS += -lole32
# $(BUILD_DIR)/WebViewWin32.cpp.o: BUILD_CXX_FLAGS += -std=gnu++17
# endif

# ---------------------------------------------------------------------------------------------------------------------

all: $(TARGETS)
2 changes: 1 addition & 1 deletion src/plugin/WebView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ START_NAMESPACE_DISTRHO

void* addWebView(uintptr_t parentWinId, double scaleFactor, uint port);
void destroyWebView(void* webview);
void reloadWebView(void* webview);
void reloadWebView(void* webview, uint port);
void resizeWebView(void* webview, uint offset, uint width, uint height);

// -----------------------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/WebView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void destroyWebView(void* const webview)
delete impl;
}

void reloadWebView(void* const webview)
void reloadWebView(void* const webview, uint)
{
WebViewImpl* const impl = static_cast<WebViewImpl*>(webview);

Expand Down
49 changes: 48 additions & 1 deletion src/plugin/WebViewWin32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,73 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

#include "WebView.hpp"
#include "DistrhoPluginInfo.h"

#ifdef USING_CHOC
#include "choc/gui/choc_WebView.h"
#endif

START_NAMESPACE_DISTRHO

// -----------------------------------------------------------------------------------------------------------

void* addWebView(const uintptr_t parentWinId, const double scaleFactor, const uint port)
{
#ifdef USING_CHOC
std::unique_ptr<choc::ui::WebView> webview = std::make_unique<choc::ui::WebView>(choc::ui::WebView());
DISTRHO_SAFE_ASSERT_RETURN(webview->loadedOK(), nullptr);

const HWND handle = static_cast<HWND>(webview->getViewHandle());
DISTRHO_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);

char url[32] = {};
std::snprintf(url, 31, "http://127.0.0.1:%u/", port);
webview->navigate(url);

auto flags = GetWindowLongPtr(handle, -16);
flags = (flags & ~(decltype (flags)) WS_POPUP) | (decltype (flags)) WS_CHILD;
SetWindowLongPtr(handle, -16, flags);

SetParent(handle, reinterpret_cast<HWND>(parentWinId));
SetWindowPos(handle, nullptr,
0, kVerticalOffset * scaleFactor,
DISTRHO_UI_DEFAULT_WIDTH * scaleFactor,
(DISTRHO_UI_DEFAULT_HEIGHT - kVerticalOffset) * scaleFactor,
SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
ShowWindow(handle, SW_SHOW);

return webview.release();
#else
return nullptr;
#endif
}

void destroyWebView(void* const webviewptr)
{
#ifdef USING_CHOC
delete static_cast<choc::ui::WebView*>(webviewptr);
#endif
}

void reloadWebView(void* const webviewptr)
void reloadWebView(void* const webviewptr, const uint port)
{
#ifdef USING_CHOC
choc::ui::WebView* const webview = static_cast<choc::ui::WebView*>(webviewptr);

char url[32] = {};
std::snprintf(url, 31, "http://127.0.0.1:%u/", port);
webview->navigate(url);
#endif
}

void resizeWebView(void* const webviewptr, const uint offset, const uint width, const uint height)
{
#ifdef USING_CHOC
choc::ui::WebView* const webview = static_cast<choc::ui::WebView*>(webviewptr);

const HWND handle = static_cast<HWND>(webview->getViewHandle());
SetWindowPos(handle, nullptr, 0, offset, width, height, SWP_NOZORDER | SWP_NOACTIVATE);
#endif
}

// -----------------------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/WebViewX11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void destroyWebView(void* const webviewptr)
delete ipc;
}

void reloadWebView(void* const webviewptr)
void reloadWebView(void* const webviewptr, uint)
{
WebViewX11* const ipc = static_cast<WebViewX11*>(webviewptr);

Expand Down
12 changes: 6 additions & 6 deletions src/plugin/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ static const char* getDataDir()
#ifdef _WIN32
static const wchar_t* getAppDirW()
{
// return L"Z:\\home\\falktx\\Source\\MOD\\mod-app\\build";
static wchar_t appDir[MAX_PATH] = {};

if (appDir[0] == 0)
Expand Down Expand Up @@ -126,7 +127,7 @@ const char* getAppDir()

if (FILE* const f = std::fopen(appDir, "r"))
{
std::memset(appDir, 0, PATH_MAX - 1);
std::memset(appDir, 0, sizeof(appDir));

if (std::fread(appDir, PATH_MAX - 1, 1, f) != 0)
appDir[0] = 0;
Expand Down Expand Up @@ -250,8 +251,7 @@ char* const* getEvironment(const uint portBaseNum)
#endif

// can be null, in case of not found
if (appDir == nullptr)
return nullptr;
DISTRHO_SAFE_ASSERT_RETURN(appDir != nullptr, nullptr);

#ifdef DISTRHO_OS_MAC
const char* const* const* const environptr = _NSGetEnviron();
Expand All @@ -267,14 +267,14 @@ char* const* getEvironment(const uint portBaseNum)

if (wchar_t* const envs = GetEnvironmentStringsW())
{
for (wchar_t* envsi = envs; *envsi != '\0'; envsi += (std::wcslen(envsi) + 1))
for (wchar_t* s = envs; *s != 0; s += std::wcslen(s) + 1)
++envsize;

envpl = new wchar_t*[envsize + 32];

uint i = 0;
for (wchar_t* envsi = envs; *envsi != '\0'; envsi += (std::wcslen(envsi) + 1))
envpl[i] = _wcsdup(envsi);
for (wchar_t* s = envs; *s != 0; s += std::wcslen(s) + 1, ++i)
envpl[i] = _wcsdup(s);

FreeEnvironmentStringsW(envs);
}
Expand Down

0 comments on commit aae3a52

Please sign in to comment.