From 648bc08377386b647f0aa289563e816b7863aa77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Wed, 21 Jun 2023 19:23:05 +0200 Subject: [PATCH] Emscripten: Implement more CRT stuff Namely: - Platform::OpenURI - Platform::DoRestart - Platform::ExecutableName --- src/common/platform/Emscripten.cpp | 32 +++++++++++- src/common/platform/Posix.cpp | 68 ------------------------- src/common/platform/PosixProc.cpp | 79 ++++++++++++++++++++++++++++++ src/common/platform/meson.build | 4 ++ 4 files changed, 113 insertions(+), 70 deletions(-) create mode 100644 src/common/platform/PosixProc.cpp diff --git a/src/common/platform/Emscripten.cpp b/src/common/platform/Emscripten.cpp index 492f45186..14aaa6aad 100644 --- a/src/common/platform/Emscripten.cpp +++ b/src/common/platform/Emscripten.cpp @@ -17,7 +17,16 @@ namespace Platform { void OpenURI(ByteString uri) { - fprintf(stderr, "cannot open URI: not implemented\n"); + EM_ASM({ + open(UTF8ToString($0)); + }, uri.c_str()); +} + +void DoRestart() +{ + EM_ASM({ + location.reload(); + }); } long unsigned int GetTime() @@ -29,7 +38,7 @@ long unsigned int GetTime() ByteString ExecutableNameFirstApprox() { - return ""; + return "powder.wasm"; // bogus } bool CanUpdate() @@ -81,6 +90,25 @@ void MaybeTriggerSyncFs() }); } } + +ByteString ExecutableName() +{ + return DefaultDdir() + "/" + ExecutableNameFirstApprox(); // bogus +} + +bool UpdateStart(const std::vector &data) +{ + return false; +} + +bool UpdateFinish() +{ + return false; +} + +void UpdateCleanup() +{ +} } EMSCRIPTEN_KEEPALIVE extern "C" int MainJs(int argc, char *argv[]) diff --git a/src/common/platform/Posix.cpp b/src/common/platform/Posix.cpp index 2f7c80c3d..faddc0bab 100644 --- a/src/common/platform/Posix.cpp +++ b/src/common/platform/Posix.cpp @@ -150,72 +150,4 @@ std::vector DirectoryList(ByteString directory) closedir(directoryHandle); return directoryList; } - -ByteString ExecutableName() -{ - auto firstApproximation = ExecutableNameFirstApprox(); - auto rp = std::unique_ptr(realpath(&firstApproximation[0], NULL), std::free); - if (!rp) - { - std::cerr << "realpath: " << errno << std::endl; - return ""; - } - return rp.get(); -} - -void DoRestart() -{ - ByteString exename = ExecutableName(); - if (exename.length()) - { - execl(exename.c_str(), exename.c_str(), NULL); - int ret = errno; - fprintf(stderr, "cannot restart: execl(...) failed: code %i\n", ret); - } - else - { - fprintf(stderr, "cannot restart: no executable name???\n"); - } - Exit(-1); -} - -bool UpdateStart(const std::vector &data) -{ - ByteString exeName = Platform::ExecutableName(); - - if (!exeName.length()) - return false; - - auto updName = exeName + "-update"; - - if (!WriteFile(data, updName)) - { - RemoveFile(updName); - return false; - } - - if (chmod(updName.c_str(), 0755)) - { - RemoveFile(updName); - return false; - } - - if (!RenameFile(updName, exeName, true)) - { - RemoveFile(updName); - return false; - } - - execl(exeName.c_str(), "powder-update", NULL); - return false; // execl returned, we failed -} - -bool UpdateFinish() -{ - return true; -} - -void UpdateCleanup() -{ -} } diff --git a/src/common/platform/PosixProc.cpp b/src/common/platform/PosixProc.cpp new file mode 100644 index 000000000..15eda60a8 --- /dev/null +++ b/src/common/platform/PosixProc.cpp @@ -0,0 +1,79 @@ +#include "Platform.h" +#include +#include +#include +#include +#include +#include +#include + +namespace Platform +{ +void DoRestart() +{ + ByteString exename = ExecutableName(); + if (exename.length()) + { + execl(exename.c_str(), exename.c_str(), NULL); + int ret = errno; + fprintf(stderr, "cannot restart: execl(...) failed: code %i\n", ret); + } + else + { + fprintf(stderr, "cannot restart: no executable name???\n"); + } + Exit(-1); +} + +ByteString ExecutableName() +{ + auto firstApproximation = ExecutableNameFirstApprox(); + auto rp = std::unique_ptr(realpath(&firstApproximation[0], NULL), std::free); + if (!rp) + { + std::cerr << "realpath: " << errno << std::endl; + return ""; + } + return rp.get(); +} + +bool UpdateStart(const std::vector &data) +{ + ByteString exeName = Platform::ExecutableName(); + + if (!exeName.length()) + return false; + + auto updName = exeName + "-update"; + + if (!WriteFile(data, updName)) + { + RemoveFile(updName); + return false; + } + + if (chmod(updName.c_str(), 0755)) + { + RemoveFile(updName); + return false; + } + + if (!RenameFile(updName, exeName, true)) + { + RemoveFile(updName); + return false; + } + + execl(exeName.c_str(), "powder-update", NULL); + return false; // execl returned, we failed +} + +bool UpdateFinish() +{ + return true; +} + +void UpdateCleanup() +{ +} +} diff --git a/src/common/platform/meson.build b/src/common/platform/meson.build index 48cb92330..a95de984c 100644 --- a/src/common/platform/meson.build +++ b/src/common/platform/meson.build @@ -21,6 +21,7 @@ elif host_platform == 'darwin' common_files += files( 'Darwin.cpp', 'Posix.cpp', + 'PosixProc.cpp', 'ExitCommon.cpp', ) powder_files += files( @@ -32,6 +33,7 @@ elif host_platform == 'android' common_files += files( 'Android.cpp', 'Posix.cpp', + 'PosixProc.cpp', 'ExitCommon.cpp', ) powder_files += files( @@ -51,6 +53,7 @@ elif host_platform == 'linux' common_files += files( 'Linux.cpp', 'Posix.cpp', + 'PosixProc.cpp', 'ExitCommon.cpp', ) powder_files += files( @@ -62,6 +65,7 @@ else common_files += files( 'Null.cpp', 'Posix.cpp', + 'PosixProc.cpp', 'ExitCommon.cpp', ) powder_files += files(