Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Fixed bug 1614 - SDL for Android does not implement TextInput API
Browse files Browse the repository at this point in the history
Andrey Isakov 2012-10-03 08:30:25 PDT

I've found out in the process of porting one OS project to Android/SDL2 that
there is no support for TextInput events/APIs on Android.
So I implemented some kind of initial support of that feature, and at the very
least it seems to work fine with latin chars input with soft and hardware
keyboards on my Moto Milestone2. I've also tried playing around with more
complex IMEs, like japanese, logging the process and it seemed to work too. I'm
not sure since the app itself I am working on does not have support for
non-latin input.

The main point of the patch is to place a fake input view in the region
specified by SDL_SetTextInputRect and create a custom InputConnection for it.
The reason to make it a separate view is to support Android's pan&scan on input
feature properly. For details please refer to
http://android-developers.blogspot.com/2009/04/updating-applications-for-on-screen.html
Even though the manual states that SetTextInputRect is used to determine the
IME variants position, I thought this would be a proper use for this too.
  • Loading branch information
slouken committed Oct 4, 2012
1 parent 69f4ac7 commit a10ad9e
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 5 deletions.
165 changes: 164 additions & 1 deletion android-project/src/org/libsdl/app/SDLActivity.java
Expand Up @@ -9,7 +9,11 @@
import android.app.*;
import android.content.*;
import android.view.*;
import android.view.inputmethod.BaseInputConnection;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsoluteLayout;
import android.os.*;
import android.util.Log;
import android.graphics.*;
Expand All @@ -33,6 +37,8 @@ public class SDLActivity extends Activity {
// Main components
private static SDLActivity mSingleton;
private static SDLSurface mSurface;
private static View mTextEdit;
private static ViewGroup mLayout;

// This is what SDL runs in. It invokes SDL_main(), eventually
private static Thread mSDLThread;
Expand Down Expand Up @@ -70,7 +76,12 @@ protected void onCreate(Bundle savedInstanceState) {

// Set up the surface
mSurface = new SDLSurface(getApplication());
setContentView(mSurface);

mLayout = new AbsoluteLayout(this);
mLayout.addView(mSurface);

setContentView(mLayout);

SurfaceHolder holder = mSurface.getHolder();
}

Expand Down Expand Up @@ -109,6 +120,7 @@ protected void onDestroy() {
// Messages from the SDLMain thread
static final int COMMAND_CHANGE_TITLE = 1;
static final int COMMAND_KEYBOARD_SHOW = 2;
static final int COMMAND_TEXTEDIT_HIDE = 3;

// Handler for the messages
Handler commandHandler = new Handler() {
Expand All @@ -134,6 +146,14 @@ public void handleMessage(Message msg) {
}
}
break;
case COMMAND_TEXTEDIT_HIDE:
if (mTextEdit != null) {
mTextEdit.setVisibility(View.GONE);

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mTextEdit.getWindowToken(), 0);
}
break;
}
}
};
Expand Down Expand Up @@ -201,6 +221,50 @@ public static void startApp() {
}
}
}

static class ShowTextInputHandler implements Runnable {
/*
* This is used to regulate the pan&scan method to have some offset from
* the bottom edge of the input region and the top edge of an input
* method (soft keyboard)
*/
static final int HEIGHT_PADDING = 15;

public int x, y, w, h;

public ShowTextInputHandler(int x, int y, int w, int h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}

public void run() {
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(
w, h + HEIGHT_PADDING, x, y);

if (mTextEdit == null) {
mTextEdit = new DummyEdit(getContext());

mLayout.addView(mTextEdit, params);
} else {
mTextEdit.setLayoutParams(params);
}

mTextEdit.setVisibility(View.VISIBLE);
mTextEdit.requestFocus();

InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mTextEdit, 0);
}

}

public static void showTextInput(int x, int y, int w, int h) {
// Transfer the task to the main thread as a Runnable
mSingleton.commandHandler.post(new ShowTextInputHandler(x, y, w, h));
}


// EGL functions
public static boolean initEGL(int majorVersion, int minorVersion) {
Expand Down Expand Up @@ -623,5 +687,104 @@ public void onSensorChanged(SensorEvent event) {
event.values[2] / SensorManager.GRAVITY_EARTH);
}
}

}

/* This is a fake invisible editor view that receives the input and defines the
* pan&scan region
*/
class DummyEdit extends View implements View.OnKeyListener {
InputConnection ic;

public DummyEdit(Context context) {
super(context);
setFocusableInTouchMode(true);
setFocusable(true);
setOnKeyListener(this);
}

@Override
public boolean onCheckIsTextEditor() {
return true;
}

public boolean onKey(View v, int keyCode, KeyEvent event) {

// This handles the hardware keyboard input
if (event.isPrintingKey()) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
ic.commitText(String.valueOf((char) event.getUnicodeChar()), 1);
}
return true;
}

if (event.getAction() == KeyEvent.ACTION_DOWN) {
SDLActivity.onNativeKeyDown(keyCode);
return true;
} else if (event.getAction() == KeyEvent.ACTION_UP) {
SDLActivity.onNativeKeyUp(keyCode);
return true;
}

return false;
}

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
ic = new SDLInputConnection(this, true);

outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI
| EditorInfo.IME_FLAG_NO_FULLSCREEN;

return ic;
}
}

class SDLInputConnection extends BaseInputConnection {

public SDLInputConnection(View targetView, boolean fullEditor) {
super(targetView, fullEditor);

}

@Override
public boolean sendKeyEvent(KeyEvent event) {

/*
* This handles the keycodes from soft keyboard (and IME-translated
* input from hardkeyboard)
*/
int keyCode = event.getKeyCode();
if (event.getAction() == KeyEvent.ACTION_DOWN) {

SDLActivity.onNativeKeyDown(keyCode);
return true;
} else if (event.getAction() == KeyEvent.ACTION_UP) {

SDLActivity.onNativeKeyUp(keyCode);
return true;
}
return super.sendKeyEvent(event);
}

@Override
public boolean commitText(CharSequence text, int newCursorPosition) {

nativeCommitText(text.toString(), newCursorPosition);

return super.commitText(text, newCursorPosition);
}

@Override
public boolean setComposingText(CharSequence text, int newCursorPosition) {

nativeSetComposingText(text.toString(), newCursorPosition);

return super.setComposingText(text, newCursorPosition);
}

public native void nativeCommitText(String text, int newCursorPosition);

public native void nativeSetComposingText(String text, int newCursorPosition);

}
60 changes: 60 additions & 0 deletions src/core/android/SDL_android.cpp
Expand Up @@ -218,6 +218,30 @@ extern "C" void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(
Android_RunAudioThread();
}

extern "C" void Java_org_libsdl_app_SDLInputConnection_nativeCommitText(
JNIEnv* env, jclass cls,
jstring text, jint newCursorPosition)
{
const char *utftext = env->GetStringUTFChars(text, NULL);

SDL_SendKeyboardText(utftext);

env->ReleaseStringUTFChars(text, utftext);
}

extern "C" void Java_org_libsdl_app_SDLInputConnection_nativeSetComposingText(
JNIEnv* env, jclass cls,
jstring text, jint newCursorPosition)
{
const char *utftext = env->GetStringUTFChars(text, NULL);

SDL_SendEditingText(utftext, 0, 0);

env->ReleaseStringUTFChars(text, utftext);
}




/*******************************************************************************
Functions called by SDL into Java
Expand Down Expand Up @@ -918,6 +942,42 @@ extern "C" int Android_JNI_SendMessage(int command, int param)
return 0;
}

extern "C" int Android_JNI_ShowTextInput(SDL_Rect *inputRect)
{
JNIEnv *env = Android_JNI_GetEnv();
if (!env) {
return -1;
}

jmethodID mid = env->GetStaticMethodID(mActivityClass, "showTextInput", "(IIII)V");
if (!mid) {
return -1;
}
env->CallStaticVoidMethod( mActivityClass, mid,
inputRect->x,
inputRect->y,
inputRect->w,
inputRect->h );
return 0;
}

/*extern "C" int Android_JNI_HideTextInput()
{
JNIEnv *env = Android_JNI_GetEnv();
if (!env) {
return -1;
}
jmethodID mid = env->GetStaticMethodID(mActivityClass, "hideTextInput", "()V");
if (!mid) {
return -1;
}
env->CallStaticVoidMethod(mActivityClass, mid);
return 0;
}*/

#endif /* __ANDROID__ */

/* vi: set ts=4 sw=4 expandtab: */
3 changes: 3 additions & 0 deletions src/core/android/SDL_android.h
Expand Up @@ -27,11 +27,14 @@ extern "C" {
/* *INDENT-ON* */
#endif

#include "SDL_rect.h"

/* Interface from the SDL library into the Android Java activity */
extern SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion);
extern void Android_JNI_SwapWindow();
extern void Android_JNI_SetActivityTitle(const char *title);
extern SDL_bool Android_JNI_GetAccelerometerValues(float values[3]);
extern int Android_JNI_ShowTextInput(SDL_Rect *inputRect);

// Audio support
extern int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames);
Expand Down
21 changes: 21 additions & 0 deletions src/video/android/SDL_androidkeyboard.c
Expand Up @@ -321,6 +321,27 @@ Android_IsScreenKeyboardShown(_THIS, SDL_Window * window)
return SDL_FALSE;
}

void
Android_StartTextInput(_THIS)
{
SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
Android_JNI_ShowTextInput(&videodata->textRect);
}

#define COMMAND_TEXTEDIT_HIDE 3
void
Android_StopTextInput(_THIS)
{
Android_JNI_SendMessage(COMMAND_TEXTEDIT_HIDE, 0);
}

void
Android_SetTextInputRect(_THIS, SDL_Rect *rect)
{
SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
videodata->textRect = *rect;
}

#endif /* SDL_VIDEO_DRIVER_ANDROID */

/* vi: set ts=4 sw=4 expandtab: */
4 changes: 4 additions & 0 deletions src/video/android/SDL_androidkeyboard.h
Expand Up @@ -32,4 +32,8 @@ extern int Android_HideScreenKeyboard(_THIS, SDL_Window * window);
extern int Android_ToggleScreenKeyboard(_THIS, SDL_Window * window);
extern SDL_bool Android_IsScreenKeyboardShown(_THIS, SDL_Window * window);

extern void Android_StartTextInput(_THIS);
extern void Android_StopTextInput(_THIS);
extern void Android_SetTextInputRect(_THIS, SDL_Rect *rect);

/* vi: set ts=4 sw=4 expandtab: */
20 changes: 16 additions & 4 deletions src/video/android/SDL_androidvideo.c
Expand Up @@ -87,17 +87,24 @@ Android_CreateDevice(int devindex)
{
printf("Creating video device\n");
SDL_VideoDevice *device;
SDL_VideoData *data;

/* Initialize all variables that we clean on shutdown */
device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
if (!device) {
SDL_OutOfMemory();
if (device) {
SDL_free(device);
}
return (0);
return NULL;
}

data = (SDL_VideoData*) SDL_calloc(1, sizeof(SDL_VideoData));
if (!data) {
SDL_OutOfMemory();
SDL_free(device);
return NULL;
}

device->driverdata = data;

/* Set the function pointers */
device->VideoInit = Android_VideoInit;
device->VideoQuit = Android_VideoQuit;
Expand Down Expand Up @@ -132,6 +139,11 @@ Android_CreateDevice(int devindex)
device->GetClipboardText = Android_GetClipboardText;
device->HasClipboardText = Android_HasClipboardText;

/* Text input */
device->StartTextInput = Android_StartTextInput;
device->StopTextInput = Android_StopTextInput;
device->SetTextInputRect = Android_SetTextInputRect;

return device;
}

Expand Down

0 comments on commit a10ad9e

Please sign in to comment.