From a8a07ffb566dae0057548976f3309c4e8395e9cb Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Sun, 2 Oct 2016 20:37:45 -0400 Subject: [PATCH 1/2] use SDL function to get clipboard text for F5 paste. --- src/sdl2_driver.c | 57 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/src/sdl2_driver.c b/src/sdl2_driver.c index 88f348a..cd942f7 100644 --- a/src/sdl2_driver.c +++ b/src/sdl2_driver.c @@ -33,6 +33,7 @@ #include #include #include +#include #include "defc.h" // BITMASKS @@ -555,9 +556,59 @@ void x_full_screen(int do_full) { } void x_release_kimage(Kimage* kimage_ptr) { } // OG Addding ratio int x_calc_ratio(float x,float y) { return 1; } -// TODO: Add clipboard support -void clipboard_paste(void) { } -int clipboard_get_char(void) { return 0; } + + +static char *g_clipboard = NULL; +static size_t g_clipboard_pos = 0; + +void clipboard_paste(void) { + + char *cp; + + if (g_clipboard) { + free(g_clipboard); + g_clipboard = NULL; + g_clipboard_pos = 0; + } + + cp = SDL_GetClipboardText(); + if (!cp) return; + + g_clipboard = strdup(cp); + g_clipboard_pos = 0; + + SDL_free(cp); +} + +int clipboard_get_char(void) { + char c; + + if (!g_clipboard) + return 0; + + // skip utf-8 characters. + for (;;) { + c = g_clipboard[g_clipboard_pos++]; + if (c & 0x80) { + /* utf8 sequence */ + continue; + } + if (c == '\n') { + continue; + } + break; + } + + if (c == 0) { + free(g_clipboard); + g_clipboard = NULL; + g_clipboard_pos = 0; + return 0; + } + + return c | 0x80; +} + void x_set_mask_and_shift(word32 x_mask, word32 *mask_ptr, int *shift_left_ptr, int *shift_right_ptr) { return; } void x_update_color(int col_num, int red, int green, int blue, word32 rgb) { } void x_update_physical_colormap() { } From ec5a2971874c950f052f98ecd466933a1196d60b Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Mon, 3 Oct 2016 16:48:18 -0400 Subject: [PATCH 2/2] fix up previous code :) for windows, \r\n is pasted as \r. for everyone else, \n is pasted as \r --- src/sdl2_driver.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/sdl2_driver.c b/src/sdl2_driver.c index cd942f7..2beb155 100644 --- a/src/sdl2_driver.c +++ b/src/sdl2_driver.c @@ -586,18 +586,17 @@ int clipboard_get_char(void) { if (!g_clipboard) return 0; - // skip utf-8 characters. - for (;;) { + /* skip utf-8 characters. */ + do { c = g_clipboard[g_clipboard_pos++]; - if (c & 0x80) { - /* utf8 sequence */ - continue; - } - if (c == '\n') { - continue; - } - break; - } + } while (c & 0x80); + + /* windows -- skip the \n in \r\n. */ + if (c == '\r' && g_clipboard[g_clipboard_pos] == '\n') + g_clipboard_pos++; + + /* everybody else -- convert \n to \r */ + if (c == '\n') c = '\r'; if (c == 0) { free(g_clipboard);