diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-29 11:30:06 +0100 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-29 11:30:06 +0100 |
| commit | 668594a0bedec468e976a38bbf9eac871e1353de (patch) | |
| tree | 6c2cef3aac24ce34e3b04a40341f972ec6968149 /src | |
| parent | ec6ed6404cdc29fc204eb93fdeb5025deb42f49d (diff) | |
Add Clipboard Support
Diffstat (limited to 'src')
| -rw-r--r-- | src/env.c | 55 | ||||
| -rw-r--r-- | src/env.h | 1 | ||||
| -rw-r--r-- | src/input.c | 77 | ||||
| -rw-r--r-- | src/input.h | 24 |
4 files changed, 149 insertions, 8 deletions
@@ -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); @@ -64,6 +64,7 @@ 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 fcedb1f..5e0ee27 100644 --- a/src/input.c +++ b/src/input.c @@ -1,3 +1,4 @@ +#include <SDL3/SDL_clipboard.h> #include <SDL3/SDL_log.h> #include <SDL3/SDL_mouse.h> @@ -9,14 +10,20 @@ struct pw_input *pw_input_init(SDL_Window *window) { return NULL; } - struct pw_input *input = SDL_malloc(sizeof(*input)); + struct pw_input *input = SDL_calloc(1, sizeof(*input)); if (!input) { return NULL; } - SDL_zerop(input); - input->window = window; + input->clipboard.size = 1024; + input->clipboard.buffer = + SDL_calloc(input->clipboard.size, sizeof(*input->clipboard.buffer)); + if (!input->clipboard.buffer) { + SDL_free(input); + return NULL; + } + input->window = window; return input; } @@ -540,12 +547,74 @@ void pw_input_wheel(struct pw_input *input, int32_t *dx, int32_t *dy) { } } -bool pw_input_poll(struct pw_input *input, union pw_input_event *event) { +char *pw_input_clipboard(struct pw_input *input, size_t len) { if (!input) { SDL_InvalidParamError("input"); + return NULL; + } + if (len == SIZE_MAX) { + SDL_OutOfMemory(); + return NULL; + } + + size_t size = len + 1; + if (size > input->clipboard.size) { + char *buffer = SDL_realloc(input->clipboard.buffer, size); + if (!buffer) { + return NULL; + } + + SDL_memset(buffer + input->clipboard.size, 0, size - input->clipboard.size); + input->clipboard.buffer = buffer; + input->clipboard.size = size; + } + + return input->clipboard.buffer; +} + +bool pw_input_clipboard_commit(struct pw_input *input) { + if (!input) { + return SDL_InvalidParamError("input"); + } + + input->clipboard.buffer[input->clipboard.size - 1] = '\0'; + return SDL_SetClipboardText(input->clipboard.buffer); +} + +bool pw_input_clipboard_fetch(struct pw_input *input, size_t *len) { + if (!input) { + return SDL_InvalidParamError("input"); + } + + if (!SDL_HasClipboardText()) { + *input->clipboard.buffer = '\0'; + *len = 0; + return true; + } + + char *text = SDL_GetClipboardText(); + if (!text || !*text) { return false; } + *len = SDL_strlen(text); + if (*len >= input->clipboard.size) { + SDL_free(input->clipboard.buffer); + input->clipboard.buffer = text; + input->clipboard.size = *len + 1; + } else { + SDL_memcpy(input->clipboard.buffer, text, *len + 1); + SDL_free(text); + } + + return true; +} + +bool pw_input_poll(struct pw_input *input, union pw_input_event *event) { + if (!input) { + return SDL_InvalidParamError("input"); + } + if (!event) { // Just check whether or not there is queued event and return. return input->keyboard.scancodes.start != input->keyboard.scancodes.end || diff --git a/src/input.h b/src/input.h index d3b996a..9ce95b0 100644 --- a/src/input.h +++ b/src/input.h @@ -97,6 +97,14 @@ struct pw_mouse { }; /** + * The PICO-8 cliboard buffer state. + */ +struct pw_clipboard { + char *buffer; + size_t size; +}; + +/** * A PICO-8 input event. * * This is only a subset of SDL events that the PICO-8 engine uses. @@ -134,6 +142,7 @@ struct pw_input { enum pw_button players[PW_PLAYERS_MAX]; struct pw_keyboard keyboard; struct pw_mouse mouse; + struct pw_clipboard clipboard; }; /** @@ -202,6 +211,21 @@ enum pw_mouse_button pw_input_mouse(struct pw_input *input, void pw_input_wheel(struct pw_input *input, int32_t *dx, int32_t *dy); /** + * Gets a writable pointer to the clipboard buffer. + */ +char *pw_input_clipboard(struct pw_input *input, size_t size); + +/** + * Commits the clipboard buffer to the system clipboard. + */ +bool pw_input_clipboard_commit(struct pw_input *input); + +/** + * Fetches the system clipboard into our clipboard buffer. + */ +bool pw_input_clipboard_fetch(struct pw_input *input, size_t *len); + +/** * Polls for the next input event for the PICO-8 engine. */ bool pw_input_poll(struct pw_input *input, union pw_input_event *event); |
