aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-29 11:52:30 +0100
committerNicholas Rodrigues Lordello <[email protected]>2026-10-29 11:59:24 +0100
commite51027a576a07ffb41b6e7d2bfc3aade6ae091d7 (patch)
tree1d30aa7156f5a470a20bfab9d9c697829e2aca25
parentf9950fce6f7b7a3743920f0d787fb1857aa05769 (diff)
Minor Fixes
-rw-r--r--src/env.c28
-rw-r--r--src/env.h1
-rw-r--r--src/input.c13
-rw-r--r--src/input.h2
4 files changed, 31 insertions, 13 deletions
diff --git a/src/env.c b/src/env.c
index 0113665..9a41eb6 100644
--- a/src/env.c
+++ b/src/env.c
@@ -205,7 +205,7 @@ bool pw_env_iterate(struct w2c_env *env, bool *shutdown) {
// request instead of a trap.
if (env_shutdown(env)) {
*shutdown = true;
- return true;
+ goto cleanup;
}
return SDL_SetError("PICO-8 iteration execution trapped: %s",
@@ -232,12 +232,13 @@ bool pw_env_iterate(struct w2c_env *env, bool *shutdown) {
goto mixdone;
}
}
-mixdone:
+mixdone:
wasm_rt_trap_unregister();
*shutdown = env_shutdown(env);
+cleanup:;
bool pointerlock;
bool fssync;
if (*shutdown) {
@@ -1021,11 +1022,15 @@ uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env,
return 0;
case 50:
return 0;
- case 51:
- if (!pw_input_clipboard(env->context.input, 0)) {
+ case 51: {
+ char *clipboard = pw_input_clipboard(env->context.input, 0);
+ if (!clipboard) {
SDL_LogWarn(0, "failed to clear clipboard: %s", SDL_GetError());
+ return 0;
}
+ *clipboard = '\0';
return 0;
+ }
case 53:
if (!pw_input_clipboard_commit(env->context.input)) {
SDL_LogWarn(0, "failed to commit to system clipboard: %s",
@@ -1041,11 +1046,12 @@ uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env,
// We don't have a text area, so this is a no-op.
return 0;
case 55: {
- if (!pw_input_clipboard_fetch(env->context.input, &env->data.cliplen)) {
- SDL_LogWarn(0, "failed to fetch to system clipboard: %s", SDL_GetError());
+ size_t len;
+ if (!pw_input_clipboard_fetch(env->context.input, &len)) {
+ SDL_LogWarn(0, "failed to fetch system clipboard: %s", SDL_GetError());
return 0;
}
- return (uint32_t)!!(env->data.cliplen);
+ return (uint32_t)!!len;
}
default:
env_badasm("i", code);
@@ -1116,6 +1122,7 @@ uint32_t w2c_env_0x5Femscripten_asm_const_iii(struct w2c_env *env,
}
wasm_rt_memory_load(&env->memory, a, clipboard, b);
+ clipboard[b] = '\0';
return 0;
}
case 56:
@@ -1123,13 +1130,14 @@ uint32_t w2c_env_0x5Femscripten_asm_const_iii(struct w2c_env *env,
return 0;
}
- uint32_t len = (uint32_t)SDL_min(env->data.cliplen, b - 1);
- char const *clipboard = pw_input_clipboard(env->context.input, len);
+ char const *clipboard = pw_input_clipboard(env->context.input, 0);
if (!clipboard) {
SDL_LogWarn(0, "failed to read clipboard buffer: %s", SDL_GetError());
+ return 0;
}
// Make sure to not split UTF-8 characters.
+ uint32_t len = (uint32_t)SDL_min(SDL_strlen(clipboard), b - 1);
while (len && (clipboard[len] & 0xc0) == 0x80) {
--len;
}
@@ -1203,7 +1211,7 @@ uint32_t w2c_env_0x5Femscripten_asm_const_iiiiiiiii(
case 10:
// Sync PICO-8 state, we only care about requesting pointer lock (so we
// can try to lock the pointer) or that shutdown is requested (so we can
- // exit back to the cart selectio menu.
+ // exit back to the cart selection menu.
env->data.state.pointerlock = f;
env->data.state.shutdown = h;
return 0;
diff --git a/src/env.h b/src/env.h
index 3acd3f2..4beae4d 100644
--- a/src/env.h
+++ b/src/env.h
@@ -64,7 +64,6 @@ struct w2c_env {
} state;
uint32_t btn;
uint8_t gpio[PW_ENV_GPIO_SIZE];
- size_t cliplen;
} data;
};
diff --git a/src/input.c b/src/input.c
index eec088e..8e2ee33 100644
--- a/src/input.c
+++ b/src/input.c
@@ -33,6 +33,7 @@ void pw_input_free(struct pw_input *input) {
return;
}
+ SDL_free(input->clipboard.buffer);
SDL_free(input);
}
@@ -53,7 +54,11 @@ bool pw_input_reset(struct pw_input *input, struct pw_players *players) {
SDL_zeroa(input->players);
clear_queue(&input->keyboard.scancodes);
- clear_queue(&input->keyboard.scancodes);
+ clear_queue(&input->keyboard.text);
+
+ SDL_zerop(&input->mouse.rel);
+
+ *input->clipboard.buffer = '\0';
return true;
}
@@ -584,7 +589,10 @@ bool pw_input_clipboard_commit(struct pw_input *input) {
return SDL_InvalidParamError("input");
}
+ // Just to be sure always make the buffer end with NUL to prevent reads
+ // past the end of the buffer.
input->clipboard.buffer[input->clipboard.size - 1] = '\0';
+
return SDL_SetClipboardText(input->clipboard.buffer);
}
@@ -601,6 +609,9 @@ bool pw_input_clipboard_fetch(struct pw_input *input, size_t *len) {
char *text = SDL_GetClipboardText();
if (!text || !*text) {
+ if (text) {
+ SDL_free(text);
+ }
return false;
}
diff --git a/src/input.h b/src/input.h
index 9ce95b0..1433360 100644
--- a/src/input.h
+++ b/src/input.h
@@ -97,7 +97,7 @@ struct pw_mouse {
};
/**
- * The PICO-8 cliboard buffer state.
+ * The PICO-8 clipboard buffer state.
*/
struct pw_clipboard {
char *buffer;