Skip to content

Commit

Permalink
Some fixes to text input with SDL 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
pelya committed Feb 23, 2011
1 parent be0552d commit bcd5152
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 38 deletions.
2 changes: 1 addition & 1 deletion project/jni/application/ballfield/AndroidAppSettings.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ SwVideoMode=y
AppUsesMouse=y
AppNeedsTwoButtonMouse=y
AppNeedsArrowKeys=n
AppNeedsTextInput=n
AppNeedsTextInput=y
AppUsesJoystick=n
AppHandlesJoystickSensitivity=n
AppUsesMultitouch=n
Expand Down
2 changes: 2 additions & 0 deletions project/jni/application/ballfield/ballfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <android/log.h>

#include "SDL.h"
#include "SDL_image.h"
Expand Down Expand Up @@ -462,6 +463,7 @@ int main(int argc, char* argv[])
Uint8 *keys = SDL_GetKeyState(&i);
if(keys[SDLK_ESCAPE])
break;
__android_log_print(ANDROID_LOG_INFO, "Ballfield", "SDL key event: state %d key %d mod %d unicode %d", event.key.state, (int)event.key.keysym.sym, (int)event.key.keysym.mod, (int)event.key.keysym.unicode);
}
}

Expand Down
2 changes: 1 addition & 1 deletion project/jni/application/src
7 changes: 5 additions & 2 deletions project/jni/sdl-1.3/include/SDL_screenkeyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ extern DECLSPEC int SDLCALL SDL_ANDROID_GetScreenKeyboardShown();

extern DECLSPEC int SDLCALL SDL_ANDROID_GetScreenKeyboardSize();

/* Show Android on-screen keyboard, and pass entered text back to application when user closes it */
extern DECLSPEC int SDLCALL SDL_ANDROID_ToggleScreenKeyboardTextInput();
/* Show Android on-screen keyboard, and pass entered text back to application as SDL keypress events, previousText may be NULL */
extern DECLSPEC int SDLCALL SDL_ANDROID_ToggleScreenKeyboardTextInput(const char * previousText);

/* Show Android on-screen keyboard, and pass entered text back to application in a buffer, previousText may be NULL */
extern DECLSPEC int SDLCALL SDL_ANDROID_GetScreenKeyboardTextInput(const char * previousText, char * outText, int outTextSize);

/* Whether user redefined on-screen keyboard layout via SDL menu, app should not enforce it's own layout in that case */
extern DECLSPEC int SDLCALL SDL_ANDROID_GetScreenKeyboardRedefinedByUser();
Expand Down
99 changes: 70 additions & 29 deletions project/jni/sdl-1.3/src/video/android/SDL_androidinput.c
Original file line number Diff line number Diff line change
Expand Up @@ -1436,21 +1436,62 @@ extern void SDL_ANDROID_DeferredTextInput()
};
#else

enum { DEFERRED_TEXT_COUNT = 128 };
enum { DEFERRED_TEXT_COUNT = 256 };
static struct { int scancode; int unicode; int down; } deferredText[DEFERRED_TEXT_COUNT];
static int deferredTextIdx1 = 0;
static int deferredTextIdx2 = 0;
static SDL_mutex * deferredTextMutex = NULL;

static SDL_keysym asciiToKeysym(int ascii, int unicode)
{
SDL_keysym keysym;
keysym.scancode = ascii;
keysym.sym = ascii;
keysym.mod = KMOD_NONE;
keysym.unicode = 0;
if ( SDL_TranslateUNICODE )
keysym.unicode = unicode;
return keysym;
}

static int checkShiftRequired( int * sym )
{
switch( *sym )
{
case '!': *sym = '1'; return 1;
case '@': *sym = '2'; return 1;
case '#': *sym = '3'; return 1;
case '$': *sym = '4'; return 1;
case '%': *sym = '5'; return 1;
case '^': *sym = '6'; return 1;
case '&': *sym = '7'; return 1;
case '*': *sym = '8'; return 1;
case '(': *sym = '9'; return 1;
case ')': *sym = '0'; return 1;
case '_': *sym = '-'; return 1;
case '+': *sym = '='; return 1;
case '|': *sym = '\\';return 1;
case '<': *sym = ','; return 1;
case '>': *sym = '.'; return 1;
case '?': *sym = '/'; return 1;
case ':': *sym = ';'; return 1;
case '"': *sym = '\'';return 1;
case '{': *sym = '['; return 1;
case '}': *sym = ']'; return 1;
case '~': *sym = '`'; return 1;
default: if( *sym >= 'A' && *sym <= 'Z' ) { *sym += 'a' - 'A'; return 1; };
}
return 0;
}

void SDL_ANDROID_DeferredTextInput()
{
int count = 2;
if( !deferredTextMutex )
deferredTextMutex = SDL_CreateMutex();

SDL_mutexP(deferredTextMutex);

while( deferredTextIdx1 != deferredTextIdx2 && count > 0 )
if( deferredTextIdx1 != deferredTextIdx2 )
{
int nextEvent = getNextEvent();
if( nextEvent == -1 )
Expand All @@ -1466,24 +1507,20 @@ void SDL_ANDROID_DeferredTextInput()

ev->type = SDL_KEYDOWN;
ev->key.state = deferredText[deferredTextIdx1].down;
ev->key.keysym.scancode = deferredText[deferredTextIdx1].scancode;
ev->key.keysym.sym = deferredText[deferredTextIdx1].scancode;
ev->key.keysym.mod = KMOD_NONE;
ev->key.keysym.unicode = 0;
if ( SDL_TranslateUNICODE )
ev->key.keysym.unicode = deferredText[deferredTextIdx1].unicode;
ev->key.keysym = asciiToKeysym( deferredText[deferredTextIdx1].scancode, deferredText[deferredTextIdx1].unicode );

BufferedEventsEnd = nextEvent;
SDL_mutexV(BufferedEventsMutex);
count --;
SDL_ANDROID_MainThreadPushMouseMotion(oldMouseX + (oldMouseX % 2 ? -1 : 1), oldMouseY); // Force screen redraw
}

SDL_mutexV(deferredTextMutex);
};
#endif

extern void SDL_ANDROID_MainThreadPushText( int scancode, int unicode )
extern void SDL_ANDROID_MainThreadPushText( int ascii, int unicode )
{
int shiftRequired;

//__android_log_print(ANDROID_LOG_INFO, "libSDL", "SDL_ANDROID_MainThreadPushText(): %i %i", scancode, unicode);
int nextEvent = getNextEvent();
Expand All @@ -1496,7 +1533,7 @@ extern void SDL_ANDROID_MainThreadPushText( int scancode, int unicode )

// TODO: convert to UTF-8
ev->type = SDL_TEXTINPUT;
ev->text.text[0] = scancode;
ev->text.text[0] = ascii;
ev->text.text[1] = 0;

#else
Expand All @@ -1508,33 +1545,39 @@ extern void SDL_ANDROID_MainThreadPushText( int scancode, int unicode )

ev->type = 0;

if( deferredTextIdx1 == deferredTextIdx2 )
{
ev->type = SDL_KEYDOWN;
ev->key.state = SDL_PRESSED;
ev->key.keysym.scancode = scancode;
ev->key.keysym.sym = scancode;
ev->key.keysym.mod = KMOD_NONE;
ev->key.keysym.unicode = 0;
if ( SDL_TranslateUNICODE )
ev->key.keysym.unicode = unicode;
}
else
shiftRequired = checkShiftRequired(&ascii);

if( shiftRequired )
{
deferredTextIdx2++;
if( deferredTextIdx2 >= DEFERRED_TEXT_COUNT )
deferredTextIdx2 = 0;
deferredText[deferredTextIdx2].down = SDL_PRESSED;
deferredText[deferredTextIdx2].scancode = scancode;
deferredText[deferredTextIdx2].unicode = unicode;
deferredText[deferredTextIdx2].scancode = SDLK_LSHIFT;
deferredText[deferredTextIdx2].unicode = SDLK_LSHIFT;
}
deferredTextIdx2++;
if( deferredTextIdx2 >= DEFERRED_TEXT_COUNT )
deferredTextIdx2 = 0;
deferredText[deferredTextIdx2].down = SDL_PRESSED;
deferredText[deferredTextIdx2].scancode = ascii;
deferredText[deferredTextIdx2].unicode = unicode;

deferredTextIdx2++;
if( deferredTextIdx2 >= DEFERRED_TEXT_COUNT )
deferredTextIdx2 = 0;
deferredText[deferredTextIdx2].down = SDL_RELEASED;
deferredText[deferredTextIdx2].scancode = scancode;
deferredText[deferredTextIdx2].scancode = ascii;
deferredText[deferredTextIdx2].unicode = unicode;
if( shiftRequired )
{
deferredTextIdx2++;
if( deferredTextIdx2 >= DEFERRED_TEXT_COUNT )
deferredTextIdx2 = 0;
deferredText[deferredTextIdx2].down = SDL_RELEASED;
deferredText[deferredTextIdx2].scancode = SDLK_LSHIFT;
deferredText[deferredTextIdx2].unicode = SDLK_LSHIFT;
}

SDL_mutexV(deferredTextMutex);

Expand Down Expand Up @@ -1717,8 +1760,6 @@ JAVA_EXPORT_NAME(Settings_nativeInitKeymap) ( JNIEnv* env, jobject thiz )
int i;
SDLKey * keymap = SDL_android_keymap;

// TODO: keys are mapped rather randomly

for (i=0; i<SDL_arraysize(SDL_android_keymap); ++i)
SDL_android_keymap[i] = SDL_KEY(UNKNOWN);

Expand Down
2 changes: 1 addition & 1 deletion project/jni/sdl-1.3/src/video/android/SDL_androidvideo.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ JAVA_EXPORT_NAME(DemoRenderer_nativeGlContextRecreated) ( JNIEnv* env, jobject
#endif
}

void SDL_ANDROID_CallJavaShowScreenKeyboard()
void SDL_ANDROID_CallJavaShowScreenKeyboard(const char * oldText, int blocking)
{
showScreenKeyboardDeferred = 1;
// Move mouse by 1 pixel to force screen update
Expand Down
2 changes: 1 addition & 1 deletion project/jni/sdl-1.3/src/video/android/SDL_androidvideo.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extern SDL_Rect SDL_ANDROID_ForceClearScreenRect;
extern int SDL_ANDROID_ShowScreenUnderFinger;
extern SDL_Rect SDL_ANDROID_ShowScreenUnderFingerRect, SDL_ANDROID_ShowScreenUnderFingerRectSrc;
extern int SDL_ANDROID_CallJavaSwapBuffers();
extern void SDL_ANDROID_CallJavaShowScreenKeyboard();
extern void SDL_ANDROID_CallJavaShowScreenKeyboard(const char * oldText, int blocking);
extern int SDL_ANDROID_drawTouchscreenKeyboard();
extern void SDL_ANDROID_VideoContextLost();
extern void SDL_ANDROID_VideoContextRecreated();
Expand Down
18 changes: 15 additions & 3 deletions project/jni/sdl-1.3/src/video/android/SDL_touchscreenkeyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ int SDL_ANDROID_processTouchscreenKeyboard(int x, int y, int action, int pointer
{
pointerInButtonRect[i] = pointerId;
if( i == BUTTON_TEXT_INPUT )
SDL_ANDROID_ToggleScreenKeyboardTextInput();
SDL_ANDROID_ToggleScreenKeyboardTextInput(NULL);
else
SDL_ANDROID_MainThreadPushKeyboardKey( SDL_PRESSED, buttonKeysyms[i] );
if( i < AutoFireButtonsNum )
Expand Down Expand Up @@ -786,9 +786,21 @@ int SDL_ANDROID_GetScreenKeyboardSize()
return buttonsize;
};

int SDL_ANDROID_ToggleScreenKeyboardTextInput()
int SDL_ANDROID_ToggleScreenKeyboardTextInput(const char * previousText)
{
SDL_ANDROID_CallJavaShowScreenKeyboard();
const char * textIn = previousText;
if( textIn == NULL )
textIn = "";
SDL_ANDROID_CallJavaShowScreenKeyboard(textIn, 0);
return 1;
};

int SDLCALL SDL_ANDROID_GetScreenKeyboardTextInput(const char * previousText, char * outText, int outTextSize)
{
const char * textIn = previousText;
if( textIn == NULL )
textIn = "";
SDL_ANDROID_CallJavaShowScreenKeyboard(textIn, 1);
return 1;
};

0 comments on commit bcd5152

Please sign in to comment.