aboutsummaryrefslogtreecommitdiff
path: root/src/env.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/env.c')
-rw-r--r--src/env.c55
1 files changed, 51 insertions, 4 deletions
diff --git a/src/env.c b/src/env.c
index fd67e98..de2bf9d 100644
--- a/src/env.c
+++ b/src/env.c
@@ -902,11 +902,31 @@ uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env,
case 50:
return 0;
case 51:
+ if (!pw_input_clipboard(env->context.input, 0)) {
+ SDL_LogWarn(0, "failed to clear clipboard: %s", SDL_GetError());
+ }
+ return 0;
case 53:
+ if (!pw_input_clipboard_commit(env->context.input)) {
+ SDL_LogWarn(0, "failed to commit to system clipboard: %s",
+ SDL_GetError());
+ }
+
+ // In the browser, an additional `Ctrl+C` is required to copy from the
+ // selected text area, but not here since we write directly to the system
+ // clipboard. Tell PICO-8 "there is no text area" so it does not print
+ // the misleading "PRESS CTRL-C TO COMPLETE COPY" message.
+ return 1;
case 54:
- case 55:
- // TODO(nlordell): Handle clipboard text area.
+ // 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());
+ return 0;
+ }
+ return (uint32_t)!!(env->data.cliplen);
+ }
default:
env_badasm("i", code);
}
@@ -968,9 +988,36 @@ uint32_t w2c_env_0x5Femscripten_asm_const_iii(struct w2c_env *env,
env->data.gpio[a] = (uint8_t)(b & 0xff);
}
return 0;
- case 52:
+ case 52: {
+ char *clipboard = pw_input_clipboard(env->context.input, b);
+ if (!clipboard) {
+ SDL_LogWarn(0, "failed to write clipboard buffer: %s", SDL_GetError());
+ return 0;
+ }
+
+ wasm_rt_memory_load(&env->memory, a, clipboard, b);
+ return 0;
+ }
case 56:
- // TODO(nlordell): Handle clipboard text area.
+ if (!b) {
+ return 0;
+ }
+
+ uint32_t len = (uint32_t)SDL_min(env->data.cliplen, b - 1);
+ char const *clipboard = pw_input_clipboard(env->context.input, len);
+ if (!clipboard) {
+ SDL_LogWarn(0, "failed to read clipboard buffer: %s", SDL_GetError());
+ }
+
+ // Make sure to not split UTF-8 characters.
+ while (len && (clipboard[len] & 0xc0) == 0x80) {
+ --len;
+ }
+
+ if (len) {
+ wasm_rt_memory_store(&env->memory, a, clipboard, len);
+ wasm_rt_memory_store8(&env->memory, a + len, 0);
+ }
return 0;
default:
env_badasm("iii", code);