diff options
Diffstat (limited to 'src/input.c')
| -rw-r--r-- | src/input.c | 77 |
1 files changed, 73 insertions, 4 deletions
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 || |
