From aae3a523c1b59c0ea240cb1ddc230051d2b2734c Mon Sep 17 00:00:00 2001 From: falkTX Date: Thu, 21 Mar 2024 00:16:38 +0200 Subject: [PATCH] win32 webview setup via choc, WIP Signed-off-by: falkTX --- src/plugin/DesktopUI.cpp | 2 +- src/plugin/Makefile | 6 +++++ src/plugin/WebView.hpp | 2 +- src/plugin/WebView.mm | 2 +- src/plugin/WebViewWin32.cpp | 49 ++++++++++++++++++++++++++++++++++++- src/plugin/WebViewX11.cpp | 2 +- src/plugin/utils.cpp | 12 ++++----- 7 files changed, 64 insertions(+), 11 deletions(-) diff --git a/src/plugin/DesktopUI.cpp b/src/plugin/DesktopUI.cpp index 7616871..7c663ac 100644 --- a/src/plugin/DesktopUI.cpp +++ b/src/plugin/DesktopUI.cpp @@ -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(); diff --git a/src/plugin/Makefile b/src/plugin/Makefile index 68eb7d6..512f9cc 100644 --- a/src/plugin/Makefile +++ b/src/plugin/Makefile @@ -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) diff --git a/src/plugin/WebView.hpp b/src/plugin/WebView.hpp index 63d2da8..1bd95a9 100644 --- a/src/plugin/WebView.hpp +++ b/src/plugin/WebView.hpp @@ -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); // ----------------------------------------------------------------------------------------------------------- diff --git a/src/plugin/WebView.mm b/src/plugin/WebView.mm index da71c69..1b7bb2f 100644 --- a/src/plugin/WebView.mm +++ b/src/plugin/WebView.mm @@ -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(webview); diff --git a/src/plugin/WebViewWin32.cpp b/src/plugin/WebViewWin32.cpp index c0f74e0..1b6e3e0 100644 --- a/src/plugin/WebViewWin32.cpp +++ b/src/plugin/WebViewWin32.cpp @@ -2,6 +2,11 @@ // 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 @@ -9,19 +14,61 @@ START_NAMESPACE_DISTRHO void* addWebView(const uintptr_t parentWinId, const double scaleFactor, const uint port) { +#ifdef USING_CHOC + std::unique_ptr webview = std::make_unique(choc::ui::WebView()); + DISTRHO_SAFE_ASSERT_RETURN(webview->loadedOK(), nullptr); + + const HWND handle = static_cast(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(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(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(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(webviewptr); + + const HWND handle = static_cast(webview->getViewHandle()); + SetWindowPos(handle, nullptr, 0, offset, width, height, SWP_NOZORDER | SWP_NOACTIVATE); +#endif } // ----------------------------------------------------------------------------------------------------------- diff --git a/src/plugin/WebViewX11.cpp b/src/plugin/WebViewX11.cpp index 9c6959e..2507cac 100644 --- a/src/plugin/WebViewX11.cpp +++ b/src/plugin/WebViewX11.cpp @@ -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(webviewptr); diff --git a/src/plugin/utils.cpp b/src/plugin/utils.cpp index 302812e..1f79064 100644 --- a/src/plugin/utils.cpp +++ b/src/plugin/utils.cpp @@ -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) @@ -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; @@ -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(); @@ -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); }