From d4bb1a775f569df94b5c27100eddd0a48f94fb45 Mon Sep 17 00:00:00 2001 From: Ookiineko Date: Sat, 13 Apr 2024 03:35:39 +0900 Subject: [PATCH 01/72] fixup: README: fixup emsdk link style Signed-off-by: Ookiineko --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3330645..c1061a0 100644 --- a/README.md +++ b/README.md @@ -192,7 +192,7 @@ On arch-cygwin, set `CYGWIN_CC` and `CYGWIN_CXX` to `x86_64-pc-cygwin-clang` and Please read the [Cross compiling](#cross-compiling) instructions first. -Install the [Emscripten][Emscripten] SDK and also a Rust compiler with Emscripten target (probably via [rustup][rustup]). +Install the [Emscripten SDK][emsdk] and also a Rust compiler with Emscripten target (probably via [rustup][rustup]). > **Warning** > @@ -409,6 +409,6 @@ For more details about these licenses, please see [LICENSE](LICENSE) and [LICENS [fedora-cygwin]: https://copr.fedorainfracloud.org/coprs/yselkowitz/cygwin/ [cmake-toolchains]: https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html [vcpkg]: https://vcpkg.io/ -[Emscripten]: https://github.com/emscripten-core/emsdk +[emsdk]: https://github.com/emscripten-core/emsdk [NodeJS]: https://nodejs.org/ [ONDK]: https://github.com/topjohnwu/ondk From aa95adb7bff29c343188cd929b8c4b55d5d1f99e Mon Sep 17 00:00:00 2001 From: Ookiineko Date: Sat, 13 Apr 2024 03:37:28 +0900 Subject: [PATCH 02/72] CI: rename NodeJS build artifacts Signed-off-by: Ookiineko --- .github/__release_template.md | 4 ---- .github/workflows/ci.yaml | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/.github/__release_template.md b/.github/__release_template.md index b60e162..e71691c 100644 --- a/.github/__release_template.md +++ b/.github/__release_template.md @@ -24,8 +24,4 @@ For debug builds, please download from [here](../../releases/last-debug-ci) inst OS release number is minimal required version to run the binary, binaries from the previous OS release(s) should work. -#### About Web builds - -- `noderawfs`: using NodeJS's host filesystem API (requires NodeJS to run, see the *WebAssembly*->*Emscripten* section in [README.md](README.md#emscripten) for more details), build with this tag is intended for running on desktop OS that is not yet supported by magiskboot_build - [Magisk]: https://github.com/topjohnwu/Magisk/releases diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ba86490..1d6fc89 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -785,7 +785,7 @@ jobs: emsdk_ver: 3.1.37 emsdk_cache_dir: "emsdk-cache" rust_target: "wasm32-unknown-emscripten" - upload_suffix: "emscripten-wasm32-noderawfs-static" + upload_suffix: "node-emscripten-wasm32-static" steps: - name: Prepare (Host) From bcd1a393232752e9a2a59d4ebbbcc0717e8efc6a Mon Sep 17 00:00:00 2001 From: Ookiineko Date: Sat, 13 Apr 2024 20:52:05 +0900 Subject: [PATCH 03/72] CI: emscripten: upgrade mymindstorm/setup-emsdk to v14 - this includes a bugfix for having multiple jobs that tries to use emsdk Signed-off-by: Ookiineko --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1d6fc89..9788f70 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -802,7 +802,7 @@ jobs: key: emsdk-${{ env.emsdk_ver }}-${{ runner.os }} - name: Prepare (Emscripten) - uses: mymindstorm/setup-emsdk@v13 + uses: mymindstorm/setup-emsdk@v14 with: version: ${{ env.emsdk_ver }} actions-cache-folder: ${{ env.emsdk_cache_dir }} From 90f73b60c3f40683b34687e29bd2fb1be71fe9d0 Mon Sep 17 00:00:00 2001 From: Ookiineko Date: Sat, 13 Apr 2024 03:22:44 +0900 Subject: [PATCH 04/72] web: start working on a frontend wrapper for Wasm build - this will allow running magiskboot in browsers in the future Signed-off-by: Ookiineko --- .github/workflows/ci.yaml | 35 ++++++++-- CMakeLists.stub.txt | 12 ++++ README.md | 12 +--- src/web-shims/script.js.in | 128 ++++++++++++++++++++++++++++++++++++ src/web-shims/shell.html.in | 13 ++++ 5 files changed, 185 insertions(+), 15 deletions(-) create mode 100644 src/web-shims/script.js.in create mode 100644 src/web-shims/shell.html.in diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 9788f70..3360d63 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -780,12 +780,23 @@ jobs: permissions: actions: write needs: [vars] + strategy: + matrix: + include: + - upload_prefix: "node" + ldflags: "-sNODERAWFS" + check: YES + + - upload_prefix: "web" + keep_shell_file: YES + opt_size: YES + env: vcpkg_triplet: "wasm32-emscripten" emsdk_ver: 3.1.37 emsdk_cache_dir: "emsdk-cache" rust_target: "wasm32-unknown-emscripten" - upload_suffix: "node-emscripten-wasm32-static" + upload_suffix: "emscripten-wasm32-static" steps: - name: Prepare (Host) @@ -829,6 +840,17 @@ jobs: . "$HOME/.cargo/env" rustup target add ${{ env.rust_target }} + - name: Determine Build type + id: detect_build_type + run: | + cmake_build_type=${{ needs.vars.outputs.cmake_build_type }} + if [ "${{ matrix.opt_size }}" == "YES" ]; then + if [ "${{ needs.vars.outputs.cmake_build_type }}" == "Release" ]; then + cmake_build_type=MinSizeRel + fi + fi + echo "cmake_build_type=${cmake_build_type}" | tee -a $GITHUB_OUTPUT + - name: Synchronize (source) uses: yogeshlonkar/wait-for-jobs@v0.2.1 with: @@ -848,9 +870,9 @@ jobs: echo -e "\n### configure ###\n" . "$HOME/.cargo/env" - emcmake cmake -G Ninja -B build -DCMAKE_EXE_LINKER_FLAGS=" ${{ needs.vars.outputs.lto_ldflags }} -sNODERAWFS" \ + emcmake cmake -G Ninja -B build -DCMAKE_EXE_LINKER_FLAGS=" ${{ needs.vars.outputs.lto_ldflags }} ${{ matrix.ldflags }}" \ -DRust_CARGO_TARGET=${{ env.rust_target }} -DFULL_RUST_LTO=${{ needs.vars.outputs.full_lto }} \ - -DCMAKE_BUILD_TYPE=${{ needs.vars.outputs.cmake_build_type }} -DPREFER_STATIC_LINKING=ON \ + -DCMAKE_BUILD_TYPE=${{ steps.detect_build_type.outputs.cmake_build_type }} -DPREFER_STATIC_LINKING=ON \ -DCMAKE_TOOLCHAIN_FILE=$VCPKG_INSTALLATION_ROOT/scripts/buildsystems/vcpkg.cmake \ -DVCPKG_TARGET_TRIPLET=${{ env.vcpkg_triplet }} \ -DVCPKG_CHAINLOAD_TOOLCHAIN_FILE="$(dirname $(which emcc))/cmake/Modules/Platform/Emscripten.cmake" @@ -859,6 +881,10 @@ jobs: cmake --build build -j $(nproc) -v emstrip build/magiskboot*.wasm + if [[ "${{ matrix.keep_shell_file }}" != 'YES' ]]; then + rm -f build/magiskboot*.html + fi + file build/magiskboot* $EMSDK_NODE ./build/magiskboot*.js || true rm -rf $GITHUB_WORKSPACE/${{ env.progname }}-out && mkdir -p $GITHUB_WORKSPACE/${{ env.progname }}-out @@ -868,10 +894,11 @@ jobs: uses: actions/upload-artifact@v4 with: if-no-files-found: error - name: ${{ env.progname }}-${{ needs.vars.outputs.short_sha }}-${{ needs.vars.outputs.build_type }}-${{ env.upload_suffix }} + name: ${{ env.progname }}-${{ needs.vars.outputs.short_sha }}-${{ needs.vars.outputs.build_type }}-${{ matrix.upload_prefix }}-${{ env.upload_suffix }} path: ${{ github.workspace }}/${{ env.progname }}-out/magiskboot* - name: Check + if: matrix.check == 'YES' run: | cd magiskboot_*-src/ EMULATOR=$EMSDK_NODE ./scripts/magiskboot_test.sh $GITHUB_WORKSPACE/${{ env.progname }}-out/magiskboot*.js diff --git a/CMakeLists.stub.txt b/CMakeLists.stub.txt index 5fada59..5971673 100644 --- a/CMakeLists.stub.txt +++ b/CMakeLists.stub.txt @@ -104,6 +104,18 @@ if (WIN32) endif() if (EMSCRIPTEN) + message(STATUS "Using custom HTML wrapper") + + set(CMAKE_EXECUTABLE_SUFFIX ".html") + get_target_property(MAGISKBOOT_WASM_NAME magiskboot_exe OUTPUT_NAME) + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/web-shims/script.js.in ${CMAKE_CURRENT_BINARY_DIR}/___script.js @ONLY) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -sEXPORTED_RUNTIME_METHODS=ENV,FS,TTY,callMain") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -sFORCE_FILESYSTEM -sEXIT_RUNTIME=0") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --pre-js=${CMAKE_CURRENT_BINARY_DIR}/___script.js") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --shell-file=${CMAKE_CURRENT_SOURCE_DIR}/src/web-shims/shell.html.in") + set_target_properties(magiskboot_exe PROPERTIES LINK_DEPENDS + "${CMAKE_CURRENT_BINARY_DIR}/___script.js;${CMAKE_CURRENT_SOURCE_DIR}/src/web-shims/shell.html.in") + message(STATUS "Using Emscripten mmap stubs") target_sources(platform_stubs PRIVATE diff --git a/README.md b/README.md index c1061a0..c1f406d 100644 --- a/README.md +++ b/README.md @@ -182,14 +182,6 @@ On arch-cygwin, set `CYGWIN_CC` and `CYGWIN_CXX` to `x86_64-pc-cygwin-clang` and #### Emscripten -> **Note** -> -> Currently this port only support running with NodeJS. -> -> A web frontend wrapper must be written to handle the argument passing and file system management in browsers. -> -> This should be implemented soon in the future ([#19](../../issues/19)), but unfortunately I am not a web developer, any help on this will be welcome QwQ - Please read the [Cross compiling](#cross-compiling) instructions first. Install the [Emscripten SDK][emsdk] and also a Rust compiler with Emscripten target (probably via [rustup][rustup]). @@ -202,9 +194,7 @@ Use [vcpkg][vcpkg] to install the [depended libraries](#requirements), the tripl When configuring, use `emcmake cmake` instead of `cmake` (but don't use it for `cmake --build` and other CMake commands) , and use `/path/to/your/emsdk/emscripten/cmake/Modules/Platform/Emscripten.cmake` as the toolchain file for vcpkg. -For NodeJS, make sure to set `CMAKE_EXE_LINKER_FLAGS` to `-sNODERAWFS` to allow using the host filesystem. - -finally, you can run the result with [NodeJS][NodeJS] using: `node magiskboot.js` +For NodeJS, make sure to set `CMAKE_EXE_LINKER_FLAGS` to `-sNODERAWFS` to allow using the host filesystem, then you will be able to run the end result with [NodeJS][NodeJS] like this: `node magiskboot.js` diff --git a/src/web-shims/script.js.in b/src/web-shims/script.js.in new file mode 100644 index 0000000..e71cb00 --- /dev/null +++ b/src/web-shims/script.js.in @@ -0,0 +1,128 @@ +if (typeof window !== 'undefined') { + var Module = { + 'noInitialRun': true, // prevent calling main() on page load + 'instantiateWasm': (imps, cb) => { + var _progress_label = document.createElement('label'); + var _progress_show = document.createElement('span'); + + _progress_label.textContent = 'Fetching: '; + _progress_show.textContent = '-- % (N/A)'; + + _progress_label.appendChild(_progress_show); + document.body.insertBefore(_progress_label, document.body.lastChild); + + // fetch wasm + + var xhr = new XMLHttpRequest(); + + xhr.open('GET', '@MAGISKBOOT_WASM_NAME@.wasm', true); + xhr.responseType = 'arraybuffer'; + xhr.onprogress = (ev) => { + if (ev.lengthComputable) { + const pct = Math.floor(100 * (ev.loaded / ev.total)); + _progress_show.textContent = `${pct} % (${ev.loaded} / ${ev.total})`; + } else { + _progress_show.textContent = `-- % (${ev.loaded} / ?)` + } + }; + xhr.onload = async () => { + var succeed = false; + var wasm = null; + + if (xhr.status == 200) { + _progress_show.textContent = 'Take longer on slow devices'; + _progress_label.textContent = 'Compiling: '; + _progress_label.appendChild(_progress_show); + + try { + wasm = await WebAssembly.instantiate(xhr.response, imps); + succeed = true; + } catch (exc) { + _progress_show.textContent = exc.message; + _progress_label.textContent = 'WebAssembly Error: '; + _progress_label.appendChild(_progress_show); + } + } else { + _progress_show.textContent = xhr.statusText; + _progress_label.textContent = 'Network Error: '; + _progress_label.appendChild(_progress_show); + } + + xhr = null; + + if (succeed) { + // free + _progress_label.remove(); + _progress_label = undefined; + _progress_show = undefined; + + cb(wasm.instance); + } + }; + xhr.send(); + + return {}; + }, + 'preInit': () => { + // bind stdout + + const _conout = document.createElement('textarea'); + const _dec = new TextDecoder(); + + _conout.readOnly = true; + _conout.style.width = '1024px'; + _conout.style.height = '768px'; + + Module.TTY.stream_ops.write = (_, buff, off, len) => { + const arr = buff.subarray(off, off + len); + _conout.value += _dec.decode(arr); + _conout.scrollTop = _conout.scrollHeight; + + return len; + } + document.body.insertBefore(_conout, document.body.firstChild); + + // exec ctrl + + const _status_label = document.createElement('label'); + const _status_show = document.createElement('span'); + window.onerror = (_) => { + _status_show.textContent = 'Crashed'; + }; + + const _cmdline_edit = document.createElement('input'); + const _clear_btn = document.createElement('button'); + const _exec_btn = document.createElement('button'); + + _clear_btn.textContent = 'Clear'; + _clear_btn.addEventListener('click', () => { + _status_show.textContent = 'Ready'; + _conout.value = ''; + }); + + _exec_btn.disabled = true; + Module.onRuntimeInitialized = () => { + // we can call main() now + _status_show.textContent = 'Ready'; + _status_label.textContent = 'Status: '; + _status_label.appendChild(_status_show); + _exec_btn.disabled = false; + }; + _exec_btn.textContent = 'Run'; + _exec_btn.addEventListener('click', () => { + const args = _cmdline_edit.value.split(/\s+/); // ws seperated args + _conout.value += `\n### Arguments: ${_cmdline_edit.value} ###\n\n`; + _cmdline_edit.value = ''; + + _status_show.textContent = 'Running'; + const ex = Module.callMain(args); + _status_show.textContent = `Exited (code ${ex})`; + }); + + document.body.insertBefore(_cmdline_edit, document.body.firstChild); + document.body.insertBefore(_clear_btn, document.body.firstChild); + document.body.insertBefore(_exec_btn, document.body.firstChild); + document.body.insertBefore(_status_label, _conout.nextSibling); + }, + }; +} diff --git a/src/web-shims/shell.html.in b/src/web-shims/shell.html.in new file mode 100644 index 0000000..225cb70 --- /dev/null +++ b/src/web-shims/shell.html.in @@ -0,0 +1,13 @@ + + + + + + magiskboot + + + + + {{{ SCRIPT }}} + + From 4493bc66b901e500d45030c3d1661d9fdc05927d Mon Sep 17 00:00:00 2001 From: Ookiineko Date: Sat, 13 Apr 2024 22:39:12 +0900 Subject: [PATCH 05/72] web-shims: add hint texts for argument input box Signed-off-by: Ookiineko --- src/web-shims/script.js.in | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/web-shims/script.js.in b/src/web-shims/script.js.in index e71cb00..ff4b63d 100644 --- a/src/web-shims/script.js.in +++ b/src/web-shims/script.js.in @@ -91,6 +91,8 @@ if (typeof window !== 'undefined') { }; const _cmdline_edit = document.createElement('input'); + _cmdline_edit.placeholder = 'Arguments…'; + const _clear_btn = document.createElement('button'); const _exec_btn = document.createElement('button'); From 73159dc68e1867fb21f50a93cfb3f83b23dbc47c Mon Sep 17 00:00:00 2001 From: Ookiineko Date: Sat, 13 Apr 2024 22:45:08 +0900 Subject: [PATCH 06/72] web-shims: use pico.css theme - this adds dark theme capability with just one line personally i think this dependency is definitely worth it Signed-off-by: Ookiineko --- src/web-shims/shell.html.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/web-shims/shell.html.in b/src/web-shims/shell.html.in index 225cb70..234e1ca 100644 --- a/src/web-shims/shell.html.in +++ b/src/web-shims/shell.html.in @@ -1,11 +1,11 @@ - magiskboot + {{{ SCRIPT }}} From eb0e84837cbf8f2018da7bc65ded72764e4d73e4 Mon Sep 17 00:00:00 2001 From: Ookiineko Date: Sat, 13 Apr 2024 22:56:07 +0900 Subject: [PATCH 07/72] web-shims: fix monospace font Signed-off-by: Ookiineko --- src/web-shims/script.js.in | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/web-shims/script.js.in b/src/web-shims/script.js.in index ff4b63d..b08dc11 100644 --- a/src/web-shims/script.js.in +++ b/src/web-shims/script.js.in @@ -70,6 +70,7 @@ if (typeof window !== 'undefined') { const _dec = new TextDecoder(); _conout.readOnly = true; + _conout.style.fontFamily = 'monospace'; _conout.style.width = '1024px'; _conout.style.height = '768px'; @@ -91,6 +92,7 @@ if (typeof window !== 'undefined') { }; const _cmdline_edit = document.createElement('input'); + _cmdline_edit.style.fontFamily = 'monospace'; _cmdline_edit.placeholder = 'Arguments…'; const _clear_btn = document.createElement('button'); From cc3de9f9e1a88195378a83f782fbdba941d9004e Mon Sep 17 00:00:00 2001 From: Ookiineko Date: Sat, 13 Apr 2024 23:19:06 +0900 Subject: [PATCH 08/72] web-shims: rename script - more friendly to IDEs Signed-off-by: Ookiineko --- CMakeLists.stub.txt | 2 +- src/web-shims/{script.js.in => script.in.js} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/web-shims/{script.js.in => script.in.js} (100%) diff --git a/CMakeLists.stub.txt b/CMakeLists.stub.txt index 5971673..cd38e90 100644 --- a/CMakeLists.stub.txt +++ b/CMakeLists.stub.txt @@ -108,7 +108,7 @@ if (EMSCRIPTEN) set(CMAKE_EXECUTABLE_SUFFIX ".html") get_target_property(MAGISKBOOT_WASM_NAME magiskboot_exe OUTPUT_NAME) - configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/web-shims/script.js.in ${CMAKE_CURRENT_BINARY_DIR}/___script.js @ONLY) + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/web-shims/script.in.js ${CMAKE_CURRENT_BINARY_DIR}/___script.js @ONLY) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -sEXPORTED_RUNTIME_METHODS=ENV,FS,TTY,callMain") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -sFORCE_FILESYSTEM -sEXIT_RUNTIME=0") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --pre-js=${CMAKE_CURRENT_BINARY_DIR}/___script.js") diff --git a/src/web-shims/script.js.in b/src/web-shims/script.in.js similarity index 100% rename from src/web-shims/script.js.in rename to src/web-shims/script.in.js From 2ba793d5a7a046f381655d58b581074c5c5ce09f Mon Sep 17 00:00:00 2001 From: Ookiineko Date: Sun, 14 Apr 2024 00:51:50 +0900 Subject: [PATCH 09/72] web-shims: fix mobile browsers - migrates dynamic UI declarations to static HTML - display correctly on more devices Signed-off-by: Ookiineko --- CMakeLists.stub.txt | 4 +- src/web-shims/script.in.js | 99 +++++++++++++------------------------ src/web-shims/shell.html.in | 13 ----- src/web-shims/shell.in.html | 43 ++++++++++++++++ 4 files changed, 79 insertions(+), 80 deletions(-) delete mode 100644 src/web-shims/shell.html.in create mode 100644 src/web-shims/shell.in.html diff --git a/CMakeLists.stub.txt b/CMakeLists.stub.txt index cd38e90..0cc2d53 100644 --- a/CMakeLists.stub.txt +++ b/CMakeLists.stub.txt @@ -112,9 +112,9 @@ if (EMSCRIPTEN) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -sEXPORTED_RUNTIME_METHODS=ENV,FS,TTY,callMain") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -sFORCE_FILESYSTEM -sEXIT_RUNTIME=0") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --pre-js=${CMAKE_CURRENT_BINARY_DIR}/___script.js") - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --shell-file=${CMAKE_CURRENT_SOURCE_DIR}/src/web-shims/shell.html.in") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --shell-file=${CMAKE_CURRENT_SOURCE_DIR}/src/web-shims/shell.in.html") set_target_properties(magiskboot_exe PROPERTIES LINK_DEPENDS - "${CMAKE_CURRENT_BINARY_DIR}/___script.js;${CMAKE_CURRENT_SOURCE_DIR}/src/web-shims/shell.html.in") + "${CMAKE_CURRENT_BINARY_DIR}/___script.js;${CMAKE_CURRENT_SOURCE_DIR}/src/web-shims/shell.in.html") message(STATUS "Using Emscripten mmap stubs") diff --git a/src/web-shims/script.in.js b/src/web-shims/script.in.js index b08dc11..2524ca7 100644 --- a/src/web-shims/script.in.js +++ b/src/web-shims/script.in.js @@ -2,14 +2,11 @@ if (typeof window !== 'undefined') { var Module = { 'noInitialRun': true, // prevent calling main() on page load 'instantiateWasm': (imps, cb) => { - var _progress_label = document.createElement('label'); - var _progress_show = document.createElement('span'); + var status_label = document.getElementById('status_label').childNodes[0]; + var status_show = document.getElementById('status_show'); - _progress_label.textContent = 'Fetching: '; - _progress_show.textContent = '-- % (N/A)'; - - _progress_label.appendChild(_progress_show); - document.body.insertBefore(_progress_label, document.body.lastChild); + status_label.textContent = 'Fetching: '; + status_show.textContent = '-- % (N/A)'; // fetch wasm @@ -20,9 +17,9 @@ if (typeof window !== 'undefined') { xhr.onprogress = (ev) => { if (ev.lengthComputable) { const pct = Math.floor(100 * (ev.loaded / ev.total)); - _progress_show.textContent = `${pct} % (${ev.loaded} / ${ev.total})`; + status_show.textContent = `${pct} % (${ev.loaded} / ${ev.total})`; } else { - _progress_show.textContent = `-- % (${ev.loaded} / ?)` + status_show.textContent = `-- % (${ev.loaded} / ?)` } }; xhr.onload = async () => { @@ -30,34 +27,25 @@ if (typeof window !== 'undefined') { var wasm = null; if (xhr.status == 200) { - _progress_show.textContent = 'Take longer on slow devices'; - _progress_label.textContent = 'Compiling: '; - _progress_label.appendChild(_progress_show); + status_show.textContent = 'Take longer on slow devices'; + status_label.textContent = 'Compiling: '; try { wasm = await WebAssembly.instantiate(xhr.response, imps); succeed = true; } catch (exc) { - _progress_show.textContent = exc.message; - _progress_label.textContent = 'WebAssembly Error: '; - _progress_label.appendChild(_progress_show); + status_show.textContent = exc.message; + status_label.textContent = 'WebAssembly Error: '; } } else { - _progress_show.textContent = xhr.statusText; - _progress_label.textContent = 'Network Error: '; - _progress_label.appendChild(_progress_show); + status_show.textContent = xhr.statusText; + status_label.textContent = 'Network Error: '; } xhr = null; - if (succeed) { - // free - _progress_label.remove(); - _progress_label = undefined; - _progress_show = undefined; - + if (succeed) cb(wasm.instance); - } }; xhr.send(); @@ -66,67 +54,48 @@ if (typeof window !== 'undefined') { 'preInit': () => { // bind stdout - const _conout = document.createElement('textarea'); + const conout = document.getElementById('conout'); const _dec = new TextDecoder(); - _conout.readOnly = true; - _conout.style.fontFamily = 'monospace'; - _conout.style.width = '1024px'; - _conout.style.height = '768px'; - Module.TTY.stream_ops.write = (_, buff, off, len) => { const arr = buff.subarray(off, off + len); - _conout.value += _dec.decode(arr); - _conout.scrollTop = _conout.scrollHeight; + conout.value += _dec.decode(arr); + conout.scrollTop = conout.scrollHeight; return len; } - document.body.insertBefore(_conout, document.body.firstChild); // exec ctrl - const _status_label = document.createElement('label'); - const _status_show = document.createElement('span'); + const status_label = document.getElementById('status_label').childNodes[0];; + const status_show = document.getElementById('status_show'); window.onerror = (_) => { - _status_show.textContent = 'Crashed'; + status_show.textContent = 'Crashed'; }; - const _cmdline_edit = document.createElement('input'); - _cmdline_edit.style.fontFamily = 'monospace'; - _cmdline_edit.placeholder = 'Arguments…'; + const cmdline_edit = document.getElementById('cmdline_edit'); + const clear_btn = document.getElementById('clear_btn'); + const exec_btn = document.getElementById('exec_btn'); - const _clear_btn = document.createElement('button'); - const _exec_btn = document.createElement('button'); - - _clear_btn.textContent = 'Clear'; - _clear_btn.addEventListener('click', () => { - _status_show.textContent = 'Ready'; - _conout.value = ''; + clear_btn.addEventListener('click', () => { + status_show.textContent = 'Ready'; + conout.value = ''; }); - - _exec_btn.disabled = true; Module.onRuntimeInitialized = () => { // we can call main() now - _status_show.textContent = 'Ready'; - _status_label.textContent = 'Status: '; - _status_label.appendChild(_status_show); - _exec_btn.disabled = false; + status_show.textContent = 'Ready'; + status_label.textContent = 'Status: '; + exec_btn.disabled = false; }; - _exec_btn.textContent = 'Run'; - _exec_btn.addEventListener('click', () => { - const args = _cmdline_edit.value.split(/\s+/); // ws seperated args - _conout.value += `\n### Arguments: ${_cmdline_edit.value} ###\n\n`; - _cmdline_edit.value = ''; + exec_btn.addEventListener('click', () => { + const args = cmdline_edit.value.split(/\s+/); // ws seperated args + conout.value += `\n### Arguments: ${cmdline_edit.value} ###\n\n`; + cmdline_edit.value = ''; - _status_show.textContent = 'Running'; + status_show.textContent = 'Running'; const ex = Module.callMain(args); - _status_show.textContent = `Exited (code ${ex})`; + status_show.textContent = `Exited (code ${ex})`; }); - - document.body.insertBefore(_cmdline_edit, document.body.firstChild); - document.body.insertBefore(_clear_btn, document.body.firstChild); - document.body.insertBefore(_exec_btn, document.body.firstChild); - document.body.insertBefore(_status_label, _conout.nextSibling); }, }; } diff --git a/src/web-shims/shell.html.in b/src/web-shims/shell.html.in deleted file mode 100644 index 234e1ca..0000000 --- a/src/web-shims/shell.html.in +++ /dev/null @@ -1,13 +0,0 @@ - - - - - magiskboot - - - - - - {{{ SCRIPT }}} - - diff --git a/src/web-shims/shell.in.html b/src/web-shims/shell.in.html new file mode 100644 index 0000000..c4cd38f --- /dev/null +++ b/src/web-shims/shell.in.html @@ -0,0 +1,43 @@ + + + + + + magiskboot + + + + + + +
+ + +
+ + + + {{{ SCRIPT }}} + + From f2b1905f42bec6a81880c2f81b3b35a5199738d0 Mon Sep 17 00:00:00 2001 From: Ookiineko Date: Sun, 14 Apr 2024 12:12:20 +0900 Subject: [PATCH 10/72] web-shims: fix old browsers Signed-off-by: Ookiineko --- src/web-shims/shell.in.html | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/web-shims/shell.in.html b/src/web-shims/shell.in.html index c4cd38f..5d52dd8 100644 --- a/src/web-shims/shell.in.html +++ b/src/web-shims/shell.in.html @@ -17,7 +17,16 @@ margin: 0; display: flex; flex-direction: column; - height: 100dvh; + /* fallback */ + position: fixed; + height: 100%; + height: 100dvh; /* require new browsers */ + } + @supports(-webkit-touch-callout: none) { + /* safari only */ + body { + height: -webkit-fill-available; + } } #cmdline_edit, #conout { font-family: monospace; From b165a86b2826030ea98e16a3931513c35a055cbe Mon Sep 17 00:00:00 2001 From: Ookiineko Date: Sun, 14 Apr 2024 12:18:05 +0900 Subject: [PATCH 11/72] web-shims: tweak default status text for unsupported browsers Signed-off-by: Ookiineko --- src/web-shims/shell.in.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/web-shims/shell.in.html b/src/web-shims/shell.in.html index 5d52dd8..cbbc405 100644 --- a/src/web-shims/shell.in.html +++ b/src/web-shims/shell.in.html @@ -44,8 +44,8 @@ -