Android: Don't share the JNI environment across threads, but obtain it the
[maemo-rb.git] / apps / hosted / android / keyboard.c
blob460011c4f72721872a1028825db4747583a8586a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Jonathan Gordon
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
23 #if (CONFIG_PLATFORM&PLATFORM_ANDROID)
24 #include <jni.h>
25 #include <stdbool.h>
26 #include "string-extra.h"
27 #include "kernel.h"
28 #include "lang.h"
29 #include "system.h"
31 static jclass RockboxKeyboardInput_class;
32 static jobject RockboxKeyboardInput_instance;
33 static jmethodID kbd_inputfunc;
34 static struct semaphore kbd_wakeup;
35 static bool accepted;
36 static jstring new_string;
38 JNIEXPORT void JNICALL
39 Java_org_rockbox_RockboxKeyboardInput_put_1result(JNIEnv *env, jobject this,
40 jboolean _accepted,
41 jstring _new_string)
43 (void)env;(void)this;
45 accepted = (bool)_accepted;
46 if (accepted)
48 new_string = _new_string;
49 (*env)->NewGlobalRef(env, new_string); /* prevet GC'ing */
51 semaphore_release(&kbd_wakeup);
54 static void kdb_init(void)
56 JNIEnv *env_ptr = getJavaEnvironment();
57 JNIEnv e = *env_ptr;
59 static jmethodID kbd_is_usable;
60 if (RockboxKeyboardInput_class == NULL)
62 semaphore_init(&kbd_wakeup, 1, 0);
63 /* get the class and its constructor */
64 jclass kbInput_class = e->FindClass(env_ptr,
65 "org/rockbox/RockboxKeyboardInput");
66 RockboxKeyboardInput_class = e->NewGlobalRef(env_ptr, kbInput_class);
67 jmethodID constructor = e->GetMethodID(env_ptr,
68 RockboxKeyboardInput_class,
69 "<init>", "()V");
70 jobject kbInput_instance = e->NewObject(env_ptr,
71 RockboxKeyboardInput_class,
72 constructor);
73 RockboxKeyboardInput_instance = e->NewGlobalRef(env_ptr,
74 kbInput_instance);
75 kbd_inputfunc = e->GetMethodID(env_ptr, RockboxKeyboardInput_class,
76 "kbd_input",
77 "(Ljava/lang/String;"
78 "Ljava/lang/String;"
79 "Ljava/lang/String;)V");
80 kbd_is_usable = e->GetMethodID(env_ptr, RockboxKeyboardInput_class,
81 "is_usable", "()Z");
84 /* need to get it every time incase the activity died/restarted */
85 while (!e->CallBooleanMethod(env_ptr, RockboxKeyboardInput_instance,
86 kbd_is_usable))
87 sleep(HZ/10);
90 int kbd_input(char* text, int buflen)
92 JNIEnv *env_ptr = getJavaEnvironment();
93 JNIEnv e = *env_ptr;
94 jstring str = e->NewStringUTF(env_ptr, text);
95 jstring ok_text = e->NewStringUTF(env_ptr, str(LANG_KBD_OK));
96 jstring cancel_text = e->NewStringUTF(env_ptr, str(LANG_KBD_CANCEL));
97 const char *utf8_string;
98 kdb_init();
100 e->CallVoidMethod(env_ptr, RockboxKeyboardInput_instance,kbd_inputfunc,
101 str, ok_text, cancel_text);
103 semaphore_wait(&kbd_wakeup, TIMEOUT_BLOCK);
105 if (accepted)
107 utf8_string = e->GetStringUTFChars(env_ptr, new_string, 0);
108 strlcpy(text, utf8_string, buflen);
109 e->ReleaseStringUTFChars(env_ptr, new_string, utf8_string);
110 e->DeleteGlobalRef(env_ptr, new_string);
112 e->DeleteLocalRef(env_ptr, str);
113 e->DeleteLocalRef(env_ptr, ok_text);
114 e->DeleteLocalRef(env_ptr, cancel_text);
116 return !accepted; /* return 0 on success */
119 int load_kbd(unsigned char* filename)
121 (void)filename;
122 return 1;
125 #endif