From b9f5d20e9308e4f86bc4ec26652931c9d00f9b6a Mon Sep 17 00:00:00 2001 From: Daniel Gibson Date: Fri, 2 Jan 2015 03:53:05 +0100 Subject: [PATCH] improved SDL2 textinput handling strdup() and free() aren't really the right tool if the size of the buffer is known anyway (and quite small, currently 32 chars) while at it, I renamed s and s_pos to str and str_pos for better readability --- neo/sys/sdl/sdl_events.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/neo/sys/sdl/sdl_events.cpp b/neo/sys/sdl/sdl_events.cpp index c45fc7c6..ed876df2 100644 --- a/neo/sys/sdl/sdl_events.cpp +++ b/neo/sys/sdl/sdl_events.cpp @@ -772,20 +772,19 @@ sysEvent_t Sys_GetEvent() static int previous_hat_state = SDL_HAT_CENTERED; #if SDL_VERSION_ATLEAST(2, 0, 0) - static char* s = NULL; - static size_t s_pos = 0; + static char str[SDL_TEXTINPUTEVENT_TEXT_SIZE] = {0}; + static size_t str_pos = 0; - if( s ) + if( str_pos != 0 ) { res.evType = SE_CHAR; - res.evValue = s[s_pos]; + res.evValue = str[str_pos]; - s_pos++; - if( !s[s_pos] ) + ++str_pos; + if( !str[str_pos] ) { - free( s ); - s = NULL; - s_pos = 0; + memset( str, 0, sizeof( str ) ); + str_pos = 0; } return res; @@ -1012,12 +1011,12 @@ sysEvent_t Sys_GetEvent() #if SDL_VERSION_ATLEAST(2, 0, 0) case SDL_TEXTINPUT: - if( ev.text.text && *ev.text.text ) + if( ev.text.text[0] != '\0' ) { if( !ev.text.text[1] ) c = *ev.text.text; else - s = strdup( ev.text.text ); + idStr::Copynz( str, ev.text.text, sizeof( str ) ); } return res_none;