forked from moai/moai-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMOAIKeyboardAndroid.cpp
More file actions
234 lines (181 loc) · 6.99 KB
/
Copy pathMOAIKeyboardAndroid.cpp
File metadata and controls
234 lines (181 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright (c) 2012 allaboutapps.at. All Rights Reserved.
//
// Contributed to the MOAI project by Jay Vaughan, allaboutapps.at
//
// This file implements the MOAIKeyboardAndroid object
// for user-input from the soft keyboard on Android
//
// http://getmoai.com
//
#include "moai-core/pch.h"
#include "moai-sim/pch.h"
#include <jni.h>
#include <moai-core/headers.h>
#include <moai-android/JniUtils.h>
#include <moai-android/MOAIKeyboardAndroid.h>
extern JavaVM* jvm;
//================================================================//
// MOAIKeyboardAndroid
//================================================================//
//----------------------------------------------------------------//
// The listeners need to be called on the event
extern "C" JNIEXPORT void JNICALL Java_com_moaisdk_core_MoaiKeyboard_AKUNotifyKeyEvent ( JNIEnv* env, jclass cls ) {
MOAIKeyboardAndroid::Get ().NotifyKeyEvent();
}
extern "C" JNIEXPORT void JNICALL Java_com_moaisdk_core_MoaiKeyboard_AKUNotifyTextDone ( JNIEnv* env, jclass cls ) {
MOAIKeyboardAndroid::Get ().NotifyTextDone();
}
//----------------------------------------------------------------//
int MOAIKeyboardAndroid::_hideKeyboard ( lua_State* L ) {
JNI_GET_ENV ( jvm, env );
MOAILuaState state ( L );
jclass moai = env->FindClass ( "com/moaisdk/core/MoaiKeyboard" );
if ( moai ) {
jmethodID hideSoftKeyboard = env->GetStaticMethodID ( moai, "hideKeyboard", "()V" );
if ( hideSoftKeyboard ) {
env->CallStaticVoidMethod ( moai, hideSoftKeyboard );
// !hiq-max - [email protected] !: pushing "false", because old code returned the value from call to nonexisting
// function "bool hideKeyboard()", signature is "void hideKeyboard()"
// void was casted to bool and returned to the lua vm
// this worked up until now, wrong function lookup and call caused segfault
lua_pushboolean ( state, false );
return 1;
}
}
MOAIKeyboardAndroid::Get ().Finish ();
return 0;
}
// Listeners use getText to retrieve the input
//----------------------------------------------------------------//
int MOAIKeyboardAndroid::_getText ( lua_State* L ) {
MOAILuaState state ( L );
MOAIKeyboardAndroid::Get ().PushText ( state );
return 1;
}
//----------------------------------------------------------------//
int MOAIKeyboardAndroid::_setListener ( lua_State* L ) {
MOAILuaState state ( L );
u32 idx = state.GetValue < u32 >( 1, TOTAL );
if ( idx < TOTAL ) {
MOAIKeyboardAndroid::Get ().mListeners [ idx ].SetRef ( state, 2 );
}
return 0;
}
int MOAIKeyboardAndroid::_setText ( lua_State* L ) {
MOAILuaState state ( L );
cc8* text = lua_tostring ( state, 1 );
JNI_GET_ENV ( jvm, env );
MOAIJString jtext = JNI_GET_JSTRING ( text );
jclass moai = env->FindClass ( "com/moaisdk/core/MoaiKeyboard" );
if ( moai ) {
jmethodID setText = env->GetStaticMethodID ( moai, "setText", "(Ljava/lang/String;)V" );
if ( setText ) {
env->CallStaticVoidMethod ( moai, setText, ( jstring )jtext );
return 1;
}
}
return 0;
}
int MOAIKeyboardAndroid::_showKeyboard ( lua_State* L ) {
return _showTextKeyboard( L );
}
int MOAIKeyboardAndroid::_showTextKeyboard( lua_State* L ) {
return _showKeyboardHelper( "showTextKeyboard" );
}
int MOAIKeyboardAndroid::_showNumberKeyboard( lua_State* L ) {
return _showKeyboardHelper( "showNumberKeyboard" );
}
int MOAIKeyboardAndroid::_showDateTimeKeyboard( lua_State* L ) {
return _showKeyboardHelper( "showDateTimeKeyboard" );
}
int MOAIKeyboardAndroid::_showPhoneKeyboard( lua_State* L ) {
return _showKeyboardHelper( "showPhoneKeyboard" );
}
int MOAIKeyboardAndroid::_showKeyboardHelper( const char* j_func ) {
JNI_GET_ENV ( jvm, env );
jclass t_class = env->FindClass ( "com/moaisdk/core/MoaiKeyboard" );
if ( t_class ) {
jmethodID t_func = env->GetStaticMethodID(t_class, j_func, "()V");
if ( t_func ) {
env->CallStaticVoidMethod ( t_class, t_func );
}
}
return 0;
}
//================================================================//
// MOAIKeyboardAndroid
//================================================================//
//----------------------------------------------------------------//
void MOAIKeyboardAndroid::Finish () {
}
//----------------------------------------------------------------//
MOAIKeyboardAndroid::MOAIKeyboardAndroid () {
RTTI_SINGLE ( MOAILuaObject )
}
//----------------------------------------------------------------//
MOAIKeyboardAndroid::~MOAIKeyboardAndroid () {
this->Finish ();
}
//----------------------------------------------------------------//
void MOAIKeyboardAndroid::NotifyKeyEvent () {
JNI_GET_ENV ( jvm, env );
MOAILuaRef& callback = this->mListeners [ EVENT_INPUT ];
if ( callback ) {
jclass moai = env->FindClass ( "com/moaisdk/core/MoaiKeyboard" );
if ( moai ) {
jmethodID getString = env->GetStaticMethodID ( moai, "getString", "()Ljava/lang/String;" );
if ( getString ) {
MOAIJString jkeystring = ( jstring )env->CallStaticObjectMethod ( moai, getString );
JNI_GET_CSTRING ( jkeystring, ckeystring );
MOAIScopedLuaState state = callback.GetSelf ();
// push the start, length and string
state.Push ( 0 );
state.Push (( u32 )strlen ( ckeystring ));
state.Push ( ckeystring );
state.DebugCall ( 3, 0 );
JNI_RELEASE_CSTRING ( jkeystring, ckeystring );
}
}
}
}
//----------------------------------------------------------------//
void MOAIKeyboardAndroid::NotifyTextDone () {
JNI_GET_ENV ( jvm, env );
MOAILuaRef& callback = this->mListeners [ EVENT_RETURN ];
if (callback) {
MOAIScopedLuaState state = callback.GetSelf ();
state.DebugCall ( 0, 0 );
}
}
//----------------------------------------------------------------//
void MOAIKeyboardAndroid::PushText ( MOAILuaState& state ) {
JNI_GET_ENV ( jvm, env );
jclass moai = env->FindClass ( "com/moaisdk/core/MoaiKeyboard" );
if ( moai ) {
jmethodID getString = env->GetStaticMethodID ( moai, "getString", "()Ljava/lang/String;" );
if ( getString ) {
MOAIJString jkeystring = ( jstring )env->CallStaticObjectMethod ( moai, getString );
JNI_GET_CSTRING ( jkeystring, keystring );
state.Push(keystring);
JNI_RELEASE_CSTRING ( jkeystring, keystring );
}
}
}
//----------------------------------------------------------------//
void MOAIKeyboardAndroid::RegisterLuaClass ( MOAILuaState& state ) {
state.SetField ( -1, "EVENT_INPUT", ( u32 )EVENT_INPUT );
state.SetField ( -1, "EVENT_RETURN", ( u32 )EVENT_RETURN );
luaL_Reg regTable [] = {
{ "getText", _getText },
{ "setListener", _setListener },
{ "showKeyboard", _showKeyboard },
{ "showTextKeyboard", _showTextKeyboard },
{ "showNumberKeyboard", _showNumberKeyboard },
{ "showDateTimeKeyboard", _showDateTimeKeyboard },
{ "showPhoneKeyboard", _showPhoneKeyboard },
{ "hideKeyboard", _hideKeyboard },
{ "setText", _setText },
{ NULL, NULL }
};
luaL_register ( state, 0, regTable );
}