Initial commit
This commit is contained in:
50
backends/platform/sdl/emscripten/emscripten-main.cpp
Normal file
50
backends/platform/sdl/emscripten/emscripten-main.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "common/scummsys.h"
|
||||
|
||||
#if defined(__EMSCRIPTEN__)
|
||||
#include "backends/platform/sdl/emscripten/emscripten.h"
|
||||
#include "backends/plugins/sdl/sdl-provider.h"
|
||||
#include "base/main.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
// Create our OSystem instance
|
||||
g_system = new OSystem_Emscripten();
|
||||
assert(g_system);
|
||||
|
||||
// Pre initialize the backend
|
||||
g_system->init();
|
||||
|
||||
#ifdef DYNAMIC_MODULES
|
||||
PluginManager::instance().addPluginProvider(new SDLPluginProvider());
|
||||
#endif
|
||||
|
||||
// Invoke the actual ScummVM main entry point:
|
||||
int res = scummvm_main(argc, argv);
|
||||
|
||||
// Free OSystem
|
||||
g_system->destroy();
|
||||
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
261
backends/platform/sdl/emscripten/emscripten.cpp
Normal file
261
backends/platform/sdl/emscripten/emscripten.cpp
Normal file
@@ -0,0 +1,261 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
|
||||
#define FORBIDDEN_SYMBOL_EXCEPTION_FILE
|
||||
#define FORBIDDEN_SYMBOL_EXCEPTION_getenv
|
||||
#include <emscripten.h>
|
||||
|
||||
#include "backends/events/emscriptensdl/emscriptensdl-events.h"
|
||||
#include "backends/fs/emscripten/emscripten-fs-factory.h"
|
||||
#include "backends/mutex/null/null-mutex.h"
|
||||
#include "backends/fs/emscripten/emscripten-fs-factory.h"
|
||||
#include "backends/platform/sdl/emscripten/emscripten.h"
|
||||
#include "backends/timer/default/default-timer.h"
|
||||
#include "common/file.h"
|
||||
#ifdef USE_TTS
|
||||
#include "backends/text-to-speech/emscripten/emscripten-text-to-speech.h"
|
||||
#endif
|
||||
|
||||
// Inline JavaScript, see https://emscripten.org/docs/api_reference/emscripten.h.html#inline-assembly-javascript for details
|
||||
EM_JS(bool, isFullscreen, (), {
|
||||
return !!document.fullscreenElement;
|
||||
});
|
||||
|
||||
EM_JS(void, toggleFullscreen, (bool enable), {
|
||||
let canvas = document.getElementById('canvas');
|
||||
if (enable && !document.fullscreenElement) {
|
||||
canvas.requestFullscreen();
|
||||
}
|
||||
if (!enable && document.fullscreenElement) {
|
||||
document.exitFullscreen();
|
||||
}
|
||||
});
|
||||
|
||||
EM_JS(void, downloadFile, (const char *filenamePtr, char *dataPtr, int dataSize), {
|
||||
const view = new Uint8Array(Module.HEAPU8.buffer, dataPtr, dataSize);
|
||||
const blob = new Blob([view], {
|
||||
type:
|
||||
'octet/stream'
|
||||
});
|
||||
const filename = UTF8ToString(filenamePtr);
|
||||
setTimeout(() => {
|
||||
const a = document.createElement('a');
|
||||
a.style = 'display:none';
|
||||
document.body.appendChild(a);
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
a.click();
|
||||
window.URL.revokeObjectURL(url);
|
||||
document.body.removeChild(a);
|
||||
}, 0);
|
||||
});
|
||||
|
||||
#ifdef USE_CLOUD
|
||||
/* Listener to feed the activation JSON from the wizard at cloud.scummvm.org back
|
||||
* Usage: Run the following on the final page of the activation flow:
|
||||
* window.opener.postMessage(document.getElementById("json").value,"*")
|
||||
*/
|
||||
EM_JS(bool, cloud_connection_open_oauth_window, (char const *url), {
|
||||
oauth_window = window.open(UTF8ToString(url));
|
||||
window.addEventListener("message", (event) => {
|
||||
Module._cloud_connection_json_callback(stringToNewUTF8( JSON.stringify(event.data)));
|
||||
oauth_window.close()
|
||||
}, {once : true});
|
||||
return true;
|
||||
});
|
||||
#endif
|
||||
|
||||
extern "C" {
|
||||
#ifdef USE_CLOUD
|
||||
void EMSCRIPTEN_KEEPALIVE cloud_connection_json_callback(char *str) {
|
||||
warning("cloud_connection_callback: %s", str);
|
||||
OSystem_Emscripten *emscripten_g_system = dynamic_cast<OSystem_Emscripten *>(g_system);
|
||||
if (emscripten_g_system->_cloudConnectionCallback) {
|
||||
(*emscripten_g_system->_cloudConnectionCallback)(new Common::String(str));
|
||||
} else {
|
||||
warning("No Storage Connection Callback Registered!");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Overridden functions
|
||||
|
||||
void OSystem_Emscripten::initBackend() {
|
||||
#ifdef USE_TTS
|
||||
// Initialize Text to Speech manager
|
||||
_textToSpeechManager = new EmscriptenTextToSpeechManager();
|
||||
#endif
|
||||
|
||||
// SDL Timers don't work in Emscripten unless threads are enabled or Asyncify is disabled.
|
||||
// We can do neither, so we use the DefaultTimerManager instead.
|
||||
_timerManager = new DefaultTimerManager();
|
||||
|
||||
// Event source
|
||||
_eventSource = new EmscriptenSdlEventSource();
|
||||
|
||||
// Invoke parent implementation of this method
|
||||
OSystem_POSIX::initBackend();
|
||||
}
|
||||
|
||||
void OSystem_Emscripten::init() {
|
||||
// Initialze File System Factory
|
||||
EmscriptenFilesystemFactory *fsFactory = new EmscriptenFilesystemFactory();
|
||||
_fsFactory = fsFactory;
|
||||
|
||||
// Invoke parent implementation of this method
|
||||
OSystem_SDL::init();
|
||||
}
|
||||
|
||||
bool OSystem_Emscripten::hasFeature(Feature f) {
|
||||
if (f == kFeatureFullscreenMode)
|
||||
return true;
|
||||
if (f == kFeatureNoQuit)
|
||||
return true;
|
||||
return OSystem_POSIX::hasFeature(f);
|
||||
}
|
||||
|
||||
bool OSystem_Emscripten::getFeatureState(Feature f) {
|
||||
if (f == kFeatureFullscreenMode) {
|
||||
return isFullscreen();
|
||||
} else {
|
||||
return OSystem_POSIX::getFeatureState(f);
|
||||
}
|
||||
}
|
||||
|
||||
void OSystem_Emscripten::setFeatureState(Feature f, bool enable) {
|
||||
if (f == kFeatureFullscreenMode) {
|
||||
toggleFullscreen(enable);
|
||||
} else {
|
||||
OSystem_POSIX::setFeatureState(f, enable);
|
||||
}
|
||||
}
|
||||
|
||||
Common::Path OSystem_Emscripten::getDefaultLogFileName() {
|
||||
return Common::Path("/tmp/scummvm.log");
|
||||
}
|
||||
|
||||
Common::Path OSystem_Emscripten::getDefaultConfigFileName() {
|
||||
return Common::Path(Common::String::format("%s/scummvm.ini", getenv("HOME")));
|
||||
}
|
||||
|
||||
Common::Path OSystem_Emscripten::getScreenshotsPath() {
|
||||
return Common::Path("/tmp/");
|
||||
}
|
||||
|
||||
Common::Path OSystem_Emscripten::getDefaultIconsPath() {
|
||||
return Common::Path(DATA_PATH"/gui-icons/");
|
||||
}
|
||||
|
||||
bool OSystem_Emscripten::displayLogFile() {
|
||||
if (_logFilePath.empty())
|
||||
return false;
|
||||
|
||||
exportFile(_logFilePath);
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef USE_OPENGL
|
||||
OSystem_SDL::GraphicsManagerType OSystem_Emscripten::getDefaultGraphicsManager() const {
|
||||
return GraphicsManagerOpenGL;
|
||||
}
|
||||
#endif
|
||||
|
||||
void OSystem_Emscripten::exportFile(const Common::Path &filename) {
|
||||
Common::File file;
|
||||
Common::FSNode node(filename);
|
||||
file.open(node);
|
||||
if (!file.isOpen()) {
|
||||
warning("Could not open file %s!", filename.toString(Common::Path::kNativeSeparator).c_str());
|
||||
return;
|
||||
}
|
||||
Common::String exportName = filename.getLastComponent().toString(Common::Path::kNativeSeparator);
|
||||
const int32 size = file.size();
|
||||
char *bytes = new char[size + 1];
|
||||
file.read(bytes, size);
|
||||
file.close();
|
||||
downloadFile(exportName.c_str(), bytes, size);
|
||||
delete[] bytes;
|
||||
}
|
||||
|
||||
void OSystem_Emscripten::updateTimers() {
|
||||
// avoid a recursion loop if a timer callback decides to call OSystem::delayMillis()
|
||||
static bool inTimer = false;
|
||||
|
||||
if (!inTimer) {
|
||||
inTimer = true;
|
||||
((DefaultTimerManager *)_timerManager)->checkTimers();
|
||||
inTimer = false;
|
||||
} else {
|
||||
const Common::ConfigManager::Domain *activeDomain = ConfMan.getActiveDomain();
|
||||
assert(activeDomain);
|
||||
|
||||
warning("%s/%s calls update() from timer",
|
||||
activeDomain->getValOrDefault("engineid").c_str(),
|
||||
activeDomain->getValOrDefault("gameid").c_str());
|
||||
}
|
||||
}
|
||||
|
||||
Common::MutexInternal *OSystem_Emscripten::createMutex() {
|
||||
return new NullMutexInternal();
|
||||
}
|
||||
|
||||
void OSystem_Emscripten::addSysArchivesToSearchSet(Common::SearchSet &s, int priority) {
|
||||
// Add the global DATA_PATH (and some sub-folders) to the directory search list
|
||||
// Note: gui-icons folder is added in GuiManager::initIconsSet
|
||||
Common::FSNode dataNode(DATA_PATH);
|
||||
if (dataNode.exists() && dataNode.isDirectory()) {
|
||||
s.addDirectory(dataNode, priority, 2, false);
|
||||
}
|
||||
}
|
||||
|
||||
void OSystem_Emscripten::delayMillis(uint msecs) {
|
||||
static uint32 lastThreshold = 0;
|
||||
const uint32 threshold = getMillis() + msecs;
|
||||
if (msecs == 0 && threshold - lastThreshold < 10) {
|
||||
return;
|
||||
}
|
||||
uint32 pause = 0;
|
||||
do {
|
||||
#ifdef ENABLE_EVENTRECORDER
|
||||
if (!g_eventRec.processDelayMillis())
|
||||
#endif
|
||||
SDL_Delay(pause);
|
||||
updateTimers();
|
||||
pause = getMillis() > threshold ? 0 : threshold - getMillis(); // Avoid negative values
|
||||
pause = pause > 10 ? 10 : pause; // ensure we don't pause for too long
|
||||
} while (pause > 0);
|
||||
lastThreshold = threshold;
|
||||
}
|
||||
|
||||
#ifdef USE_CLOUD
|
||||
bool OSystem_Emscripten::openUrl(const Common::String &url) {
|
||||
if(url.hasPrefix("https://cloud.scummvm.org/")){
|
||||
return cloud_connection_open_oauth_window(url.c_str());
|
||||
}
|
||||
return OSystem_SDL::openUrl(url);
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
74
backends/platform/sdl/emscripten/emscripten.h
Normal file
74
backends/platform/sdl/emscripten/emscripten.h
Normal file
@@ -0,0 +1,74 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef PLATFORM_SDL_EMSCRIPTEN_H
|
||||
#define PLATFORM_SDL_EMSCRIPTEN_H
|
||||
|
||||
#include "backends/platform/sdl/posix/posix.h"
|
||||
#ifdef USE_CLOUD
|
||||
#include "backends/networking/http/request.h"
|
||||
#include "common/ustr.h"
|
||||
typedef Common::BaseCallback<const Common::String *> *CloudConnectionCallback;
|
||||
#endif
|
||||
|
||||
extern "C" {
|
||||
void cloud_connection_json_callback(char *str); // pass cloud storage activation data from JS to setup wizard
|
||||
}
|
||||
class OSystem_Emscripten : public OSystem_POSIX {
|
||||
#ifdef USE_CLOUD
|
||||
friend void ::cloud_connection_json_callback(char *str);
|
||||
#endif
|
||||
protected:
|
||||
#ifdef USE_CLOUD
|
||||
CloudConnectionCallback _cloudConnectionCallback;
|
||||
#endif
|
||||
|
||||
public:
|
||||
void initBackend() override;
|
||||
bool hasFeature(Feature f) override;
|
||||
void setFeatureState(Feature f, bool enable) override;
|
||||
bool getFeatureState(Feature f) override;
|
||||
bool displayLogFile() override;
|
||||
Common::Path getScreenshotsPath() override;
|
||||
Common::Path getDefaultIconsPath() override;
|
||||
#ifdef USE_OPENGL
|
||||
GraphicsManagerType getDefaultGraphicsManager() const override;
|
||||
#endif
|
||||
Common::MutexInternal *createMutex() override;
|
||||
void exportFile(const Common::Path &filename);
|
||||
void delayMillis(uint msecs) override;
|
||||
void init() override;
|
||||
void addSysArchivesToSearchSet(Common::SearchSet &s, int priority) override;
|
||||
|
||||
#ifdef USE_CLOUD
|
||||
void setCloudConnectionCallback(CloudConnectionCallback cb) { _cloudConnectionCallback = cb; }
|
||||
bool openUrl(const Common::String &url) override;
|
||||
#endif // USE_CLOUD
|
||||
|
||||
protected:
|
||||
Common::Path getDefaultConfigFileName() override;
|
||||
Common::Path getDefaultLogFileName() override;
|
||||
|
||||
private:
|
||||
void updateTimers();
|
||||
};
|
||||
|
||||
#endif
|
||||
53
backends/platform/sdl/emscripten/emscripten.mk
Normal file
53
backends/platform/sdl/emscripten/emscripten.mk
Normal file
@@ -0,0 +1,53 @@
|
||||
# use system python as fallback
|
||||
# if EMSDK_PYTHON is not set
|
||||
EMSDK_PYTHON ?= python3
|
||||
|
||||
|
||||
# Special generic target for emscripten static file hosting bundle
|
||||
dist-emscripten: $(EXECUTABLE) $(PLUGINS)
|
||||
mkdir -p ./build-emscripten/
|
||||
mkdir -p ./build-emscripten/data
|
||||
mkdir -p ./build-emscripten/data/games
|
||||
mkdir -p ./build-emscripten/data/gui-icons
|
||||
mkdir -p ./build-emscripten/doc
|
||||
cp $(EXECUTABLE) ./build-emscripten/
|
||||
cp $(EXECUTABLE:html=wasm) ./build-emscripten/
|
||||
cp $(EXECUTABLE:html=js) ./build-emscripten/
|
||||
cp $(DIST_FILES_DOCS) ./build-emscripten/doc
|
||||
cp $(DIST_FILES_THEMES) ./build-emscripten/data
|
||||
zip -d ./build-emscripten/data/shaders.dat anti-aliasing/aa-shader-4.0.glslp anti-aliasing/reverse-aa.glslp cel/MMJ_Cel_Shader_MP.glslp \
|
||||
crt/crt-guest-dr-venom-fast.glslp crt/crt-guest-dr-venom.glslp crt/crt-hyllian-glow.glslp crt/crt-hyllian.glslp \
|
||||
crt/crtsim.glslp crt/gtuv50.glslp crt/yee64.glslp crt/yeetron.glslp cubic/cubic-gamma-correct.glslp \
|
||||
cubic/cubic.glslp denoisers/crt-fast-bilateral-super-xbr.glslp denoisers/fast-bilateral-super-xbr-4p.glslp \
|
||||
denoisers/fast-bilateral-super-xbr-6p.glslp denoisers/fast-bilateral-super-xbr.glslp dithering/bayer-matrix-dithering.glslp \
|
||||
dithering/gendither.glslp hqx/hq2x-halphon.glslp interpolation/bandlimit-pixel.glslp interpolation/controlled_sharpness.glslp \
|
||||
sabr/sabr-hybrid-deposterize.glslp scalefx/scalefx+rAA.glslp scalefx/scalefx-hybrid.glslp scalefx/scalefx.glslp \
|
||||
scalenx/scale2xSFX.glslp scalenx/scale3x.glslp sharpen/adaptive-sharpen-multipass.glslp sharpen/adaptive-sharpen.glslp \
|
||||
sharpen/super-xbr-super-res.glslp xbr/2xBR-lv1-multipass.glslp xbr/super-xbr-2p.glslp xbr/super-xbr-3p-smoother.glslp \
|
||||
xbr/super-xbr-6p-adaptive.glslp xbr/super-xbr-6p-small-details.glslp xbr/super-xbr-6p.glslp xbr/super-xbr-deposterize.glslp \
|
||||
xbr/xbr-hybrid.glslp xbr/xbr-lv2-3d.glslp xbr/xbr-lv2-noblend.glslp xbr/xbr-lv2.glslp xbr/xbr-lv3-multipass.glslp \
|
||||
xbr/xbr-lv3.glslp xbr/xbr-mlv4-multipass.glslp
|
||||
ifdef DIST_FILES_ENGINEDATA
|
||||
cp $(DIST_FILES_ENGINEDATA) ./build-emscripten/data
|
||||
endif
|
||||
ifdef DIST_FILES_NETWORKING
|
||||
cp $(DIST_FILES_NETWORKING) ./build-emscripten/data
|
||||
endif
|
||||
ifdef DIST_FILES_VKEYBD
|
||||
cp $(DIST_FILES_VKEYBD) ./build-emscripten/data
|
||||
endif
|
||||
ifdef DIST_FILES_SOUNDFONTS
|
||||
cp $(DIST_FILES_SOUNDFONTS) ./build-emscripten/data
|
||||
endif
|
||||
ifdef DIST_FILES_SHADERS
|
||||
mkdir -p ./build-emscripten/data/shaders
|
||||
cp $(DIST_FILES_SHADERS) ./build-emscripten/data/shaders
|
||||
endif
|
||||
ifeq ($(DYNAMIC_MODULES),1)
|
||||
mkdir -p ./build-emscripten/data/plugins
|
||||
@for i in $(PLUGINS); do cp $$i ./build-emscripten/data/plugins; done
|
||||
endif
|
||||
$(EMSDK_PYTHON) "$(srcdir)/dists/emscripten/build-make_http_index.py" ./build-emscripten/data
|
||||
cp "$(srcdir)/dists/emscripten/assets/"* ./build-emscripten/
|
||||
cp "$(srcdir)/gui/themes/common-svg/logo.svg" ./build-emscripten/
|
||||
cp "$(srcdir)/icons/scummvm.ico" ./build-emscripten/favicon.ico
|
||||
Reference in New Issue
Block a user