From 6cb12f50fcbe091de56461100b5d527294405dd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Wed, 31 Jan 2018 17:11:54 +0100 Subject: Add utf-8 support to the sdl_clipboard functions --- README.asciidoc | 5 +++-- c_src/sdl_clipboard.c | 21 ++++++++++----------- src/sdl_clipboard.erl | 4 ++-- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/README.asciidoc b/README.asciidoc index 510407c..9a91b4f 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -10,6 +10,7 @@ corresponding to the public headers. * 'SDL.h' * 'SDL_blendmode.h' +* 'SDL_clipboard.h' * 'SDL_cpuinfo.h' * 'SDL_filesystem.h' * 'SDL_keyboard.h' @@ -22,7 +23,6 @@ corresponding to the public headers. == Partially implemented -* 'SDL_clipboard.h': We currently do not support UTF-8. We should probably switch to binaries as input/output to support it. * 'SDL_events.h': The following events and functions are missing: ** `SDL_SYSWMEVENT` ** `SDL_TEXTEDITING` @@ -78,7 +78,7 @@ corresponding to the public headers. ** `SDL_GetRenderTarget` (renderer) ** `SDL_RenderIsClipEnabled` (renderer) ** `SDL_RenderReadPixels` (renderer) -* 'SDL_stdinc.h': SDL_bool is implemented in 'sdl_bool.c'. Do we need anything else? +* 'SDL_stdinc.h': Erlang does not come with the functions `copysign` and `scalbn`. * 'SDL_surface.h': Only surface creation (via `IMG_Load`) and destruction is implemented. Might be better to move IMG_* functions in their own space. * 'SDL_version.h': `SDL_GetRevisionNumber` must be implemented. The macros may also be useful. * 'SDL_video.h': The following elements are missing: @@ -156,6 +156,7 @@ These don't make a lot of sense for Erlang. * 'SDL_main.h' * 'SDL_mutex.h' * 'SDL_quit.h' (only necessary when using `SDL_Main`?) +* 'SDL_stdinc.h': only a few functions are implemented, others are not interesting. * 'SDL_thread.h' * 'SDL_video.h': the functions `SDL_CreateWindowFrom`, `SDL_SetWindowData` and `SDL_GetWindowData` take external data as argument. diff --git a/c_src/sdl_clipboard.c b/c_src/sdl_clipboard.c index c5c32b6..2d776a9 100644 --- a/c_src/sdl_clipboard.c +++ b/c_src/sdl_clipboard.c @@ -18,21 +18,22 @@ NIF_FUNCTION(get_clipboard_text) { + ErlNifBinary bin; char* text; - ERL_NIF_TERM term; text = SDL_GetClipboardText(); if (!text) return sdl_error_tuple(env); - term = enif_make_string(env, text, ERL_NIF_LATIN1); + enif_alloc_binary(strlen(text), &bin); + memcpy(bin.data, text, bin.size); SDL_free(text); return enif_make_tuple2(env, atom_ok, - term + enif_make_binary(env, &bin) ); } @@ -50,21 +51,19 @@ NIF_FUNCTION(has_clipboard_text) NIF_FUNCTION(set_clipboard_text) { - unsigned int len; + ErlNifBinary bin; char* text; int ret; - BADARG_IF(!enif_get_list_length(env, argv[0], &len)); - text = (char*)enif_alloc(len + 1); + BADARG_IF(!enif_inspect_binary(env, argv[0], &bin)); - if (!enif_get_string(env, argv[0], text, len + 1, ERL_NIF_LATIN1)) { - enif_free(text); - return enif_make_badarg(env); - } + text = malloc(bin.size + 1); + memcpy(text, bin.data, bin.size); + text[bin.size] = '\0'; ret = SDL_SetClipboardText(text); - enif_free(text); + free(text); if (ret != 0) return sdl_error_tuple(env); diff --git a/src/sdl_clipboard.erl b/src/sdl_clipboard.erl index 8577206..f694e6f 100644 --- a/src/sdl_clipboard.erl +++ b/src/sdl_clipboard.erl @@ -18,7 +18,7 @@ -export([has_text/0]). -export([set_text/1]). --spec get_text() -> {ok, string()} | sdl:error(). +-spec get_text() -> {ok, binary()} | sdl:error(). get_text() -> esdl2:get_clipboard_text(). @@ -26,6 +26,6 @@ get_text() -> has_text() -> esdl2:has_clipboard_text(). --spec set_text(string()) -> ok | sdl:error(). +-spec set_text(binary()) -> ok | sdl:error(). set_text(Text) -> esdl2:set_clipboard_text(Text). -- cgit v1.2.3