aboutsummaryrefslogtreecommitdiffstats
path: root/native/jni
diff options
context:
space:
mode:
Diffstat (limited to 'native/jni')
-rw-r--r--native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp58
-rw-r--r--native/jni/com_android_inputmethod_keyboard_ProximityInfo.h29
-rw-r--r--native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp257
-rw-r--r--native/jni/com_android_inputmethod_latin_BinaryDictionary.h29
-rw-r--r--native/jni/jni_common.cpp79
-rw-r--r--native/jni/jni_common.h30
6 files changed, 364 insertions, 118 deletions
diff --git a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp
new file mode 100644
index 000000000..f3e2a7e60
--- /dev/null
+++ b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp
@@ -0,0 +1,58 @@
+/*
+**
+** Copyright 2011, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+** http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+
+#define LOG_TAG "LatinIME: jni: ProximityInfo"
+
+#include "com_android_inputmethod_keyboard_ProximityInfo.h"
+#include "jni.h"
+#include "jni_common.h"
+#include "proximity_info.h"
+
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+
+namespace latinime {
+
+static jint latinime_Keyboard_setProximityInfo(JNIEnv *env, jobject object,
+ jint maxProximityCharsSize, jint displayWidth, jint displayHeight, jint gridWidth,
+ jint gridHeight, jintArray proximityCharsArray) {
+ jint* proximityChars = env->GetIntArrayElements(proximityCharsArray, NULL);
+ ProximityInfo *proximityInfo = new ProximityInfo(maxProximityCharsSize, displayWidth,
+ displayHeight, gridWidth, gridHeight, (const uint32_t *)proximityChars);
+ env->ReleaseIntArrayElements(proximityCharsArray, proximityChars, 0);
+ return (jint)proximityInfo;
+}
+
+static void latinime_Keyboard_release(JNIEnv *env, jobject object, jint proximityInfo) {
+ ProximityInfo *pi = (ProximityInfo*)proximityInfo;
+ if (!pi) return;
+ delete pi;
+}
+
+static JNINativeMethod sKeyboardMethods[] = {
+ {"setProximityInfoNative", "(IIIII[I)I", (void*)latinime_Keyboard_setProximityInfo},
+ {"releaseProximityInfoNative", "(I)V", (void*)latinime_Keyboard_release}
+};
+
+int register_ProximityInfo(JNIEnv *env) {
+ const char* const kClassPathName = "com/android/inputmethod/keyboard/ProximityInfo";
+ return registerNativeMethods(env, kClassPathName, sKeyboardMethods,
+ sizeof(sKeyboardMethods) / sizeof(sKeyboardMethods[0]));
+}
+
+} // namespace latinime
diff --git a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.h b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.h
new file mode 100644
index 000000000..4a1e83b09
--- /dev/null
+++ b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.h
@@ -0,0 +1,29 @@
+/*
+**
+** Copyright 2011, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+** http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+
+#ifndef _COM_ANDROID_INPUTMETHOD_KEYBOARD_PROXIMITYINFO_H
+#define _COM_ANDROID_INPUTMETHOD_KEYBOARD_PROXIMITYINFO_H
+
+#include "jni.h"
+
+namespace latinime {
+
+int register_ProximityInfo(JNIEnv *env);
+
+}
+
+#endif // _COM_ANDROID_INPUTMETHOD_KEYBOARD_PROXIMITYINFO_H
diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
index bf7ec0d1a..ce874d8d4 100644
--- a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
+++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
@@ -15,79 +15,135 @@
** limitations under the License.
*/
-#include <stdio.h>
-#include <assert.h>
-#include <unistd.h>
-#include <fcntl.h>
+#define LOG_TAG "LatinIME: jni: BinaryDictionary"
-#include <jni.h>
+#include "com_android_inputmethod_latin_BinaryDictionary.h"
#include "dictionary.h"
+#include "jni.h"
+#include "jni_common.h"
+#include "proximity_info.h"
-// ----------------------------------------------------------------------------
-
-using namespace latinime;
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
-//
-// helper function to throw an exception
-//
-static void throwException(JNIEnv *env, const char* ex, const char* fmt, int data)
-{
- if (jclass cls = env->FindClass(ex)) {
- char msg[1000];
- sprintf(msg, fmt, data);
- env->ThrowNew(cls, msg);
- env->DeleteLocalRef(cls);
+#ifdef USE_MMAP_FOR_DICTIONARY
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#else // USE_MMAP_FOR_DICTIONARY
+#include <stdlib.h>
+#endif // USE_MMAP_FOR_DICTIONARY
+
+namespace latinime {
+
+static jint latinime_BinaryDictionary_open(JNIEnv *env, jobject object,
+ jstring sourceDir, jlong dictOffset, jlong dictSize,
+ jint typedLetterMultiplier, jint fullWordMultiplier, jint maxWordLength, jint maxWords,
+ jint maxAlternatives) {
+ PROF_OPEN;
+ PROF_START(66);
+ const char *sourceDirChars = env->GetStringUTFChars(sourceDir, NULL);
+ if (sourceDirChars == NULL) {
+ LOGE("DICT: Can't get sourceDir string");
+ return 0;
}
-}
+ int fd = 0;
+ void *dictBuf = NULL;
+ int adjust = 0;
+#ifdef USE_MMAP_FOR_DICTIONARY
+ /* mmap version */
+ fd = open(sourceDirChars, O_RDONLY);
+ if (fd < 0) {
+ LOGE("DICT: Can't open sourceDir. sourceDirChars=%s errno=%d", sourceDirChars, errno);
+ return 0;
+ }
+ int pagesize = getpagesize();
+ adjust = dictOffset % pagesize;
+ int adjDictOffset = dictOffset - adjust;
+ int adjDictSize = dictSize + adjust;
+ dictBuf = mmap(NULL, sizeof(char) * adjDictSize, PROT_READ, MAP_PRIVATE, fd, adjDictOffset);
+ if (dictBuf == MAP_FAILED) {
+ LOGE("DICT: Can't mmap dictionary. errno=%d", errno);
+ return 0;
+ }
+ dictBuf = (void *)((char *)dictBuf + adjust);
+#else // USE_MMAP_FOR_DICTIONARY
+ /* malloc version */
+ FILE *file = NULL;
+ file = fopen(sourceDirChars, "rb");
+ if (file == NULL) {
+ LOGE("DICT: Can't fopen sourceDir. sourceDirChars=%s errno=%d", sourceDirChars, errno);
+ return 0;
+ }
+ dictBuf = malloc(sizeof(char) * dictSize);
+ if (!dictBuf) {
+ LOGE("DICT: Can't allocate memory region for dictionary. errno=%d", errno);
+ return 0;
+ }
+ int ret = fseek(file, (long)dictOffset, SEEK_SET);
+ if (ret != 0) {
+ LOGE("DICT: Failure in fseek. ret=%d errno=%d", ret, errno);
+ return 0;
+ }
+ ret = fread(dictBuf, sizeof(char) * dictSize, 1, file);
+ if (ret != 1) {
+ LOGE("DICT: Failure in fread. ret=%d errno=%d", ret, errno);
+ return 0;
+ }
+ ret = fclose(file);
+ if (ret != 0) {
+ LOGE("DICT: Failure in fclose. ret=%d errno=%d", ret, errno);
+ return 0;
+ }
+#endif // USE_MMAP_FOR_DICTIONARY
+ env->ReleaseStringUTFChars(sourceDir, sourceDirChars);
-static jint latinime_BinaryDictionary_open
- (JNIEnv *env, jobject object, jobject dictDirectBuffer,
- jint typedLetterMultiplier, jint fullWordMultiplier)
-{
- void *dict = env->GetDirectBufferAddress(dictDirectBuffer);
- if (dict == NULL) {
- fprintf(stderr, "DICT: Dictionary buffer is null\n");
+ if (!dictBuf) {
+ LOGE("DICT: dictBuf is null");
return 0;
}
- Dictionary *dictionary = new Dictionary(dict, typedLetterMultiplier, fullWordMultiplier);
- return (jint) dictionary;
+ Dictionary *dictionary = new Dictionary(dictBuf, dictSize, fd, adjust, typedLetterMultiplier,
+ fullWordMultiplier, maxWordLength, maxWords, maxAlternatives);
+ PROF_END(66);
+ PROF_CLOSE;
+ return (jint)dictionary;
}
-static int latinime_BinaryDictionary_getSuggestions(
- JNIEnv *env, jobject object, jint dict, jintArray inputArray, jint arraySize,
- jcharArray outputArray, jintArray frequencyArray, jint maxWordLength, jint maxWords,
- jint maxAlternatives, jint skipPos, jintArray nextLettersArray, jint nextLettersSize)
-{
- Dictionary *dictionary = (Dictionary*) dict;
- if (dictionary == NULL) return 0;
+static int latinime_BinaryDictionary_getSuggestions(JNIEnv *env, jobject object, jint dict,
+ jint proximityInfo, jintArray xCoordinatesArray, jintArray yCoordinatesArray,
+ jintArray inputArray, jint arraySize, jint flags,
+ jcharArray outputArray, jintArray frequencyArray) {
+ Dictionary *dictionary = (Dictionary*)dict;
+ if (!dictionary) return 0;
+ ProximityInfo *pInfo = (ProximityInfo*)proximityInfo;
+
+ int *xCoordinates = env->GetIntArrayElements(xCoordinatesArray, NULL);
+ int *yCoordinates = env->GetIntArrayElements(yCoordinatesArray, NULL);
int *frequencies = env->GetIntArrayElements(frequencyArray, NULL);
int *inputCodes = env->GetIntArrayElements(inputArray, NULL);
jchar *outputChars = env->GetCharArrayElements(outputArray, NULL);
- int *nextLetters = nextLettersArray != NULL ? env->GetIntArrayElements(nextLettersArray, NULL)
- : NULL;
- int count = dictionary->getSuggestions(inputCodes, arraySize, (unsigned short*) outputChars,
- frequencies, maxWordLength, maxWords, maxAlternatives, skipPos, nextLetters,
- nextLettersSize);
+ int count = dictionary->getSuggestions(pInfo, xCoordinates, yCoordinates, inputCodes,
+ arraySize, flags, (unsigned short*) outputChars, frequencies);
env->ReleaseIntArrayElements(frequencyArray, frequencies, 0);
env->ReleaseIntArrayElements(inputArray, inputCodes, JNI_ABORT);
+ env->ReleaseIntArrayElements(xCoordinatesArray, xCoordinates, 0);
+ env->ReleaseIntArrayElements(yCoordinatesArray, yCoordinates, 0);
env->ReleaseCharArrayElements(outputArray, outputChars, 0);
- if (nextLetters) {
- env->ReleaseIntArrayElements(nextLettersArray, nextLetters, 0);
- }
return count;
}
-static int latinime_BinaryDictionary_getBigrams
- (JNIEnv *env, jobject object, jint dict, jcharArray prevWordArray, jint prevWordLength,
- jintArray inputArray, jint inputArraySize, jcharArray outputArray,
- jintArray frequencyArray, jint maxWordLength, jint maxBigrams, jint maxAlternatives)
-{
- Dictionary *dictionary = (Dictionary*) dict;
- if (dictionary == NULL) return 0;
+static int latinime_BinaryDictionary_getBigrams(JNIEnv *env, jobject object, jint dict,
+ jcharArray prevWordArray, jint prevWordLength, jintArray inputArray, jint inputArraySize,
+ jcharArray outputArray, jintArray frequencyArray, jint maxWordLength, jint maxBigrams,
+ jint maxAlternatives) {
+ Dictionary *dictionary = (Dictionary*)dict;
+ if (!dictionary) return 0;
jchar *prevWord = env->GetCharArrayElements(prevWordArray, NULL);
int *inputCodes = env->GetIntArrayElements(inputArray, NULL);
@@ -106,12 +162,10 @@ static int latinime_BinaryDictionary_getBigrams
return count;
}
-
-static jboolean latinime_BinaryDictionary_isValidWord
- (JNIEnv *env, jobject object, jint dict, jcharArray wordArray, jint wordLength)
-{
- Dictionary *dictionary = (Dictionary*) dict;
- if (dictionary == NULL) return (jboolean) false;
+static jboolean latinime_BinaryDictionary_isValidWord(JNIEnv *env, jobject object, jint dict,
+ jcharArray wordArray, jint wordLength) {
+ Dictionary *dictionary = (Dictionary*)dict;
+ if (!dictionary) return (jboolean) false;
jchar *word = env->GetCharArrayElements(wordArray, NULL);
jboolean result = dictionary->isValidWord((unsigned short*) word, wordLength);
@@ -120,72 +174,39 @@ static jboolean latinime_BinaryDictionary_isValidWord
return result;
}
-static void latinime_BinaryDictionary_close
- (JNIEnv *env, jobject object, jint dict)
-{
- Dictionary *dictionary = (Dictionary*) dict;
- delete (Dictionary*) dict;
-}
-
-// ----------------------------------------------------------------------------
-
-static JNINativeMethod gMethods[] = {
- {"openNative", "(Ljava/nio/ByteBuffer;II)I",
- (void*)latinime_BinaryDictionary_open},
- {"closeNative", "(I)V", (void*)latinime_BinaryDictionary_close},
- {"getSuggestionsNative", "(I[II[C[IIIII[II)I", (void*)latinime_BinaryDictionary_getSuggestions},
- {"isValidWordNative", "(I[CI)Z", (void*)latinime_BinaryDictionary_isValidWord},
- {"getBigramsNative", "(I[CI[II[C[IIII)I", (void*)latinime_BinaryDictionary_getBigrams}
-};
-
-static int registerNativeMethods(JNIEnv* env, const char* className,
- JNINativeMethod* gMethods, int numMethods)
-{
- jclass clazz;
-
- clazz = env->FindClass(className);
- if (clazz == NULL) {
- fprintf(stderr,
- "Native registration unable to find class '%s'\n", className);
- return JNI_FALSE;
+static void latinime_BinaryDictionary_close(JNIEnv *env, jobject object, jint dict) {
+ Dictionary *dictionary = (Dictionary*)dict;
+ if (!dictionary) return;
+ void *dictBuf = dictionary->getDict();
+ if (!dictBuf) return;
+#ifdef USE_MMAP_FOR_DICTIONARY
+ int ret = munmap((void *)((char *)dictBuf - dictionary->getDictBufAdjust()),
+ dictionary->getDictSize() + dictionary->getDictBufAdjust());
+ if (ret != 0) {
+ LOGE("DICT: Failure in munmap. ret=%d errno=%d", ret, errno);
}
- if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
- fprintf(stderr, "RegisterNatives failed for '%s'\n", className);
- return JNI_FALSE;
+ ret = close(dictionary->getMmapFd());
+ if (ret != 0) {
+ LOGE("DICT: Failure in close. ret=%d errno=%d", ret, errno);
}
-
- return JNI_TRUE;
+#else // USE_MMAP_FOR_DICTIONARY
+ free(dictBuf);
+#endif // USE_MMAP_FOR_DICTIONARY
+ delete dictionary;
}
-static int registerNatives(JNIEnv *env)
-{
+static JNINativeMethod sMethods[] = {
+ {"openNative", "(Ljava/lang/String;JJIIIII)I", (void*)latinime_BinaryDictionary_open},
+ {"closeNative", "(I)V", (void*)latinime_BinaryDictionary_close},
+ {"getSuggestionsNative", "(II[I[I[III[C[I)I", (void*)latinime_BinaryDictionary_getSuggestions},
+ {"isValidWordNative", "(I[CI)Z", (void*)latinime_BinaryDictionary_isValidWord},
+ {"getBigramsNative", "(I[CI[II[C[IIII)I", (void*)latinime_BinaryDictionary_getBigrams}
+};
+
+int register_BinaryDictionary(JNIEnv *env) {
const char* const kClassPathName = "com/android/inputmethod/latin/BinaryDictionary";
- return registerNativeMethods(env,
- kClassPathName, gMethods, sizeof(gMethods) / sizeof(gMethods[0]));
+ return registerNativeMethods(env, kClassPathName, sMethods,
+ sizeof(sMethods) / sizeof(sMethods[0]));
}
-/*
- * Returns the JNI version on success, -1 on failure.
- */
-jint JNI_OnLoad(JavaVM* vm, void* reserved)
-{
- JNIEnv* env = NULL;
- jint result = -1;
-
- if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
- fprintf(stderr, "ERROR: GetEnv failed\n");
- goto bail;
- }
- assert(env != NULL);
-
- if (!registerNatives(env)) {
- fprintf(stderr, "ERROR: BinaryDictionary native registration failed\n");
- goto bail;
- }
-
- /* success -- return valid version number */
- result = JNI_VERSION_1_4;
-
-bail:
- return result;
-}
+} // namespace latinime
diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.h b/native/jni/com_android_inputmethod_latin_BinaryDictionary.h
new file mode 100644
index 000000000..1b1ba7f0f
--- /dev/null
+++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.h
@@ -0,0 +1,29 @@
+/*
+**
+** Copyright 2011, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+** http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+
+#ifndef _COM_ANDROID_INPUTMETHOD_LATIN_BINARYDICTIONARY_H
+#define _COM_ANDROID_INPUTMETHOD_LATIN_BINARYDICTIONARY_H
+
+#include "jni.h"
+
+namespace latinime {
+
+int register_BinaryDictionary(JNIEnv *env);
+
+}
+
+#endif // _COM_ANDROID_INPUTMETHOD_LATIN_BINARYDICTIONARY_H
diff --git a/native/jni/jni_common.cpp b/native/jni/jni_common.cpp
new file mode 100644
index 000000000..8643f723f
--- /dev/null
+++ b/native/jni/jni_common.cpp
@@ -0,0 +1,79 @@
+/*
+**
+** Copyright 2011, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+** http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+
+#define LOG_TAG "LatinIME: jni"
+
+#include "com_android_inputmethod_keyboard_ProximityInfo.h"
+#include "com_android_inputmethod_latin_BinaryDictionary.h"
+#include "jni.h"
+#include "proximity_info.h"
+
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+
+using namespace latinime;
+
+/*
+ * Returns the JNI version on success, -1 on failure.
+ */
+jint JNI_OnLoad(JavaVM* vm, void* reserved) {
+ JNIEnv* env = NULL;
+ jint result = -1;
+
+ if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
+ LOGE("ERROR: GetEnv failed");
+ goto bail;
+ }
+ assert(env != NULL);
+
+ if (!register_BinaryDictionary(env)) {
+ LOGE("ERROR: BinaryDictionary native registration failed");
+ goto bail;
+ }
+
+ if (!register_ProximityInfo(env)) {
+ LOGE("ERROR: ProximityInfo native registration failed");
+ goto bail;
+ }
+
+ /* success -- return valid version number */
+ result = JNI_VERSION_1_4;
+
+bail:
+ return result;
+}
+
+namespace latinime {
+
+int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* methods,
+ int numMethods) {
+ jclass clazz = env->FindClass(className);
+ if (clazz == NULL) {
+ LOGE("Native registration unable to find class '%s'", className);
+ return JNI_FALSE;
+ }
+ if (env->RegisterNatives(clazz, methods, numMethods) < 0) {
+ LOGE("RegisterNatives failed for '%s'", className);
+ env->DeleteLocalRef(clazz);
+ return JNI_FALSE;
+ }
+ env->DeleteLocalRef(clazz);
+ return JNI_TRUE;
+}
+
+} // namespace latinime
diff --git a/native/jni/jni_common.h b/native/jni/jni_common.h
new file mode 100644
index 000000000..c502fa3a8
--- /dev/null
+++ b/native/jni/jni_common.h
@@ -0,0 +1,30 @@
+/*
+**
+** Copyright 2011, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+** http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+
+#ifndef LATINIME_JNI_COMMON_H
+#define LATINIME_JNI_COMMON_H
+
+#include "jni.h"
+
+namespace latinime {
+
+int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* methods,
+ int numMethods);
+
+} // namespace latinime
+
+#endif // LATINIME_JNI_COMMON_H