commit c2938c02994988bd5dde4c4cbba9951709789ee8
parent 614eeaf0ef0390d4956feb991e9038980cf50371
Author: falkTX <falktx@falktx.com>
Date: Tue, 5 Jul 2022 23:35:59 +0100
Enable simd for wasm, early implementation of file dialogs
Signed-off-by: falkTX <falktx@falktx.com>
Diffstat:
6 files changed, 92 insertions(+), 6 deletions(-)
diff --git a/Makefile.base.mk b/Makefile.base.mk
@@ -172,7 +172,7 @@ BASE_OPTS = -O3 -ffast-math -fdata-sections -ffunction-sections
ifeq ($(CPU_I386_OR_X86_64),true)
BASE_OPTS += -mtune=generic
ifeq ($(WASM),true)
-# BASE_OPTS += -msse -msse2 -msse3 -msimd128
+BASE_OPTS += -msse -msse2 -msse3 -msimd128
else
BASE_OPTS += -msse -msse2 -mfpmath=sse
endif
diff --git a/dgl/src/pugl.cpp b/dgl/src/pugl.cpp
@@ -108,6 +108,7 @@
#ifndef DGL_FILE_BROWSER_DISABLED
# define FILE_BROWSER_DIALOG_DGL_NAMESPACE
+# define FILE_BROWSER_DIALOG_NAMESPACE DGL_NAMESPACE
# define DGL_FILE_BROWSER_DIALOG_HPP_INCLUDED
START_NAMESPACE_DGL
# include "../../distrho/extra/FileBrowserDialogImpl.hpp"
diff --git a/distrho/DistrhoUI_macOS.mm b/distrho/DistrhoUI_macOS.mm
@@ -25,6 +25,7 @@
#if DISTRHO_UI_FILE_BROWSER
# define DISTRHO_FILE_BROWSER_DIALOG_HPP_INCLUDED
+# define FILE_BROWSER_DIALOG_NAMESPACE DISTRHO_NAMESPACE
# define FILE_BROWSER_DIALOG_DISTRHO_NAMESPACE
START_NAMESPACE_DISTRHO
# include "extra/FileBrowserDialogImpl.hpp"
diff --git a/distrho/extra/FileBrowserDialogImpl.cpp b/distrho/extra/FileBrowserDialogImpl.cpp
@@ -27,6 +27,9 @@
#ifdef DISTRHO_OS_MAC
# import <Cocoa/Cocoa.h>
#endif
+#ifdef DISTRHO_OS_WASM
+# include <emscripten/emscripten.h>
+#endif
#ifdef DISTRHO_OS_WINDOWS
# include <direct.h>
# include <process.h>
@@ -71,6 +74,52 @@ static constexpr int toHexChar(const char c) noexcept
}
#endif
+#ifdef DISTRHO_OS_WASM
+# define DISTRHO_WASM_NAMESPACE_MACRO_HELPER(NS, SEP, FUNCTION) NS ## SEP ## FUNCTION
+# define DISTRHO_WASM_NAMESPACE_MACRO(NS, FUNCTION) DISTRHO_WASM_NAMESPACE_MACRO_HELPER(NS, _, FUNCTION)
+# define DISTRHO_WASM_NAMESPACE_HELPER(NS) #NS
+# define DISTRHO_WASM_NAMESPACE(NS) DISTRHO_WASM_NAMESPACE_HELPER(NS)
+// FIXME use world class name as prefix
+EM_JS(bool, DISTRHO_WASM_NAMESPACE_MACRO(FILE_BROWSER_DIALOG_NAMESPACE, openWebBrowserFileDialog), (const char* funcname, void* handle), {
+ var canvasFileElem = document.getElementById('canvas_file_open');
+ var jsfuncname = UTF8ToString(funcname);
+ if (canvasFileElem) {
+ canvasFileElem.onchange = function(e) {
+ if (!!canvasFileElem.files) {
+ var file = canvasFileElem.files[0];
+ var filename = '/' + file.name;
+ var reader = new FileReader();
+ reader.onloadend = function(e) {
+ var content = new Uint8Array(reader.result);
+ Module.FS.writeFile(filename, content);
+ Module.ccall(jsfuncname, 'null', ['number', 'string'], [handle, filename]);
+ };
+ reader.readAsArrayBuffer(file);
+ }
+ };
+ canvasFileElem.click();
+ return true;
+ }
+ return false;
+});
+EM_JS(bool, DISTRHO_WASM_NAMESPACE_MACRO(FILE_BROWSER_DIALOG_NAMESPACE, downloadWebBrowserFile), (), {
+ var canvasFileElem = document.getElementById('canvas_file_save');
+ if (canvasFileElem) {
+ // FIXME filename shouldn't change
+ var content = Module.FS.readFile('/download.vcv');
+ canvasFileElem.download = 'download.vcv';
+ canvasFileElem.href = URL.createObjectURL(new Blob([content]));
+ canvasFileElem.click();
+ return true;
+ }
+ return false;
+});
+# define openWebBrowserFileDialogNamespaced DISTRHO_WASM_NAMESPACE_MACRO(FILE_BROWSER_DIALOG_NAMESPACE, openWebBrowserFileDialog)
+# define downloadWebBrowserFileNamespaced DISTRHO_WASM_NAMESPACE_MACRO(FILE_BROWSER_DIALOG_NAMESPACE, downloadWebBrowserFile)
+# define fileBrowserSetPathNamespaced DISTRHO_WASM_NAMESPACE_MACRO(FILE_BROWSER_DIALOG_NAMESPACE, fileBrowserSetPath)
+# define fileBrowserSetPathFuncName DISTRHO_WASM_NAMESPACE(FILE_BROWSER_DIALOG_NAMESPACE) "_fileBrowserSetPath"
+#endif
+
struct FileBrowserData {
const char* selectedFile;
@@ -284,6 +333,19 @@ struct FileBrowserData {
// --------------------------------------------------------------------------------------------------------------------
+#ifdef DISTRHO_OS_WASM
+extern "C" {
+EMSCRIPTEN_KEEPALIVE
+void fileBrowserSetPathNamespaced(FileBrowserHandle handle, const char* filename)
+{
+ handle->free();
+
+ if (filename != nullptr && filename[0] != '\0')
+ handle->selectedFile = strdup(filename);
+}
+}
+#endif
+
FileBrowserHandle fileBrowserCreate(const bool isEmbed,
const uintptr_t windowId,
const double scaleFactor,
@@ -295,17 +357,13 @@ FileBrowserHandle fileBrowserCreate(const bool isEmbed,
{
#ifdef DISTRHO_OS_WINDOWS
if (char* const cwd = _getcwd(nullptr, 0))
- {
- startDir = cwd;
- std::free(cwd);
- }
#else
if (char* const cwd = getcwd(nullptr, 0))
+#endif
{
startDir = cwd;
std::free(cwd);
}
-#endif
}
DISTRHO_SAFE_ASSERT_RETURN(startDir.isNotEmpty(), nullptr);
@@ -375,6 +433,18 @@ FileBrowserHandle fileBrowserCreate(const bool isEmbed,
# endif
#endif
+#ifdef DISTRHO_OS_WASM
+ if (options.saving)
+ {
+ handle->selectedFile = strdup("/download");
+ return handle.release();
+ }
+
+ const char* const funcname = fileBrowserSetPathFuncName;
+ if (openWebBrowserFileDialogNamespaced(funcname, handle.get()))
+ return handle.release();
+#endif
+
#ifdef DISTRHO_OS_WINDOWS
handle->setupAndStart(isEmbed, startDir, windowTitle, windowId, options);
#endif
@@ -663,6 +733,11 @@ bool fileBrowserIdle(const FileBrowserHandle handle)
void fileBrowserClose(const FileBrowserHandle handle)
{
+#ifdef DISTRHO_OS_WASM
+ if (fileBrowserGetPath(handle) != nullptr)
+ downloadWebBrowserFileNamespaced();
+#endif
+
#ifdef HAVE_X11
if (Display* const x11display = handle->x11display)
x_fib_close(x11display);
@@ -693,3 +768,9 @@ END_NAMESPACE_DISTRHO
#undef FILE_BROWSER_DIALOG_DISTRHO_NAMESPACE
#undef FILE_BROWSER_DIALOG_DGL_NAMESPACE
+#undef FILE_BROWSER_DIALOG_NAMESPACE
+
+#undef openWebBrowserFileDialogNamespaced
+#undef downloadWebBrowserFileNamespaced
+#undef fileBrowserSetPathNamespaced
+#undef fileBrowserSetPathFuncName
diff --git a/distrho/src/DistrhoUI.cpp b/distrho/src/DistrhoUI.cpp
@@ -44,6 +44,7 @@
# define x_fib_show DISTRHO_PUGL_NAMESPACE_MACRO(plugin, x_fib_show)
# define x_fib_status DISTRHO_PUGL_NAMESPACE_MACRO(plugin, x_fib_status)
# define DISTRHO_FILE_BROWSER_DIALOG_HPP_INCLUDED
+# define FILE_BROWSER_DIALOG_NAMESPACE DISTRHO_NAMESPACE
# define FILE_BROWSER_DIALOG_DISTRHO_NAMESPACE
START_NAMESPACE_DISTRHO
# include "../extra/FileBrowserDialogImpl.hpp"
diff --git a/distrho/src/DistrhoUIPrivateData.hpp b/distrho/src/DistrhoUIPrivateData.hpp
@@ -481,7 +481,9 @@ inline void PluginWindow::onFileSelected(const char* const filename)
}
#endif
+ puglBackendEnter(pData->view);
ui->uiFileBrowserSelected(filename);
+ puglBackendLeave(pData->view);
}
#endif