aboutsummaryrefslogtreecommitdiff
path: root/src/env.c
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-29 00:45:24 +0100
committerNicholas Rodrigues Lordello <[email protected]>2026-10-29 00:45:24 +0100
commitec6ed6404cdc29fc204eb93fdeb5025deb42f49d (patch)
tree65dc21b389bc716163d9c0ba9a3e5f88ce883d69 /src/env.c
parent05a5c587f6c59aee26038cc4bdf657d9040340e3 (diff)
Support the Shutdown Option
It looks like the `SHUTDOWN` menu option shows up when the `-run` CLI argument is passed, so pass it. There is a catch though, in that PICO-8 does not properly report shutdown requests in those cases, so read Wasm memory directly to detect the shutdown request.
Diffstat (limited to 'src/env.c')
-rw-r--r--src/env.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/env.c b/src/env.c
index e64b3fb..fd67e98 100644
--- a/src/env.c
+++ b/src/env.c
@@ -116,6 +116,33 @@ bool pw_env_link(struct w2c_env *env, struct pw_env_ctx context) {
return true;
}
+bool pw_env_shutdown(struct w2c_env *env) {
+ if (!env) {
+ return SDL_InvalidParamError("env");
+ }
+
+ if (env->data.state.shutdown) {
+ return true;
+ }
+
+ // HERE BE DRAGONS... Taking a look at the Web Assembly module exported
+ // by PICO-8, it calls `env_emscripten_asm_iiiiiiiii(10, ...)` with the
+ // `request_shutdown` flag read from global memory offset `2220668`. For
+ // whatever reason, the `SHUTDOWN` menu flag is setting this location in
+ // memory to a non-zero value, but without calling the ASM const. In order
+ // to correctly support shutting down, we need to not only read the flag
+ // from the ASM const, but also this memory offset. This is tied to the
+ // PICO-8 version, so whenever it gets updated, just:
+ //
+ // grep -B2 'asm_const_iiiiiiiii' build/system/pico8.c
+ //
+ // And check the address that is being loaded for the last parameter to the
+ // state update ASM const call.
+
+ uint32_t value;
+ return wasm_rt_memory_load32_checked(&env->memory, 2220668, &value) && value;
+}
+
/**
* Convenience macro for writing to wasm memory and moving the wasm pointer
* forward for that amount.