Android: Use wakeup objects instead of polling for the dialog results in the keyboard...
[maemo-rb.git] / apps / hosted / keyboard.c
blob6cc14d654f283d39cb84d4eef1c496f425e4b51a
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"
29 extern JNIEnv *env_ptr;
30 static jclass RockboxKeyboardInput_class;
31 static jobject RockboxKeyboardInput_instance;
32 static jmethodID kbd_inputfunc;
33 static struct wakeup kbd_wakeup;
34 static bool accepted;
35 static jstring new_string;
37 JNIEXPORT void JNICALL
38 Java_org_rockbox_RockboxKeyboardInput_put_1result(JNIEnv *env, jobject this,
39 jboolean _accepted,
40 jstring _new_string)
42 (void)env;(void)this;
44 accepted = (bool)_accepted;
45 if (accepted)
47 new_string = _new_string;
48 (*env)->NewGlobalRef(env, new_string); /* prevet GC'ing */
50 wakeup_signal(&kbd_wakeup);
53 static void kdb_init(void)
55 JNIEnv e = *env_ptr;
56 static jmethodID kbd_is_usable;
57 if (RockboxKeyboardInput_class == NULL)
59 wakeup_init(&kbd_wakeup);
60 /* get the class and its constructor */
61 RockboxKeyboardInput_class = e->FindClass(env_ptr,
62 "org/rockbox/RockboxKeyboardInput");
63 jmethodID constructor = e->GetMethodID(env_ptr,
64 RockboxKeyboardInput_class,
65 "<init>", "()V");
66 RockboxKeyboardInput_instance = e->NewObject(env_ptr,
67 RockboxKeyboardInput_class,
68 constructor);
69 kbd_inputfunc = e->GetMethodID(env_ptr, RockboxKeyboardInput_class,
70 "kbd_input", "(Ljava/lang/String;)V");
71 kbd_is_usable = e->GetMethodID(env_ptr, RockboxKeyboardInput_class,
72 "is_usable", "()Z");
75 /* need to get it every time incase the activity died/restarted */
76 while (!e->CallBooleanMethod(env_ptr, RockboxKeyboardInput_instance,
77 kbd_is_usable))
78 sleep(HZ/10);
81 int kbd_input(char* text, int buflen)
83 JNIEnv e = *env_ptr;
84 jstring str = e->NewStringUTF(env_ptr, text);
85 const char *utf8_string;
86 kdb_init();
88 e->CallVoidMethod(env_ptr, RockboxKeyboardInput_instance,kbd_inputfunc,str);
90 wakeup_wait(&kbd_wakeup, TIMEOUT_BLOCK);
92 if (accepted)
94 utf8_string = e->GetStringUTFChars(env_ptr, new_string, 0);
95 strlcpy(text, utf8_string, buflen);
96 e->ReleaseStringUTFChars(env_ptr, new_string, utf8_string);
97 e->DeleteGlobalRef(env_ptr, new_string);
100 return !accepted; /* return 0 on success */
103 int load_kbd(unsigned char* filename)
105 (void)filename;
106 return 1;
109 #endif