typedef struct _CGuava7LuaBridgeEvent {
const char * key;
const char * value;
}CGuava7LuaBridgeEvent;
JNIEXPORT void JNICALL Java_com_giderosmobile_android_MyActivity_NativeLuaBridgeDispatchEvent(JNIEnv * env, jobject obj, jstring receiver, jstring func, jobjectArray _keys_values)
{
const char *_func = (*env).GetStringUTFChars(func, 0);
const char *_rec = (*env).GetStringUTFChars(receiver, 0);
int n = 0;
if (_keys_values) n = ((*env).GetArrayLength(_keys_values))>>1;
CGuava7LuaBridgeEvent event[512];
bool p_release[512];
jstring key[512];
jstring value[512];
for (int i = 0; i < n; i++)
{
key[i] = (jstring) (*env).GetObjectArrayElement(_keys_values, i<<1);
value[i] = (jstring) (*env).GetObjectArrayElement(_keys_values, (i<<1) + 1);
if (key[i] && value[i])
{
event[i].key = (*env).GetStringUTFChars(key[i], 0);
event[i].value = (*env).GetStringUTFChars(value[i], 0);
p_release[i] = true;
}
else
{
event[i].key = "null";
event[i].value = "null";
p_release[i] = false;
}
}
gv7_luabridgeDispatchEvent(_rec, _func, n, event);
for (int i = 0; i < n; i++)
{
if (p_release[i])
{
(*env).ReleaseStringUTFChars(key[i], event[i].key);
(*env).ReleaseStringUTFChars(value[i], event[i].value);
}
}
(*env).ReleaseStringUTFChars(func, _func);
(*env).ReleaseStringUTFChars(receiver, _rec);
}
void gv7_luabridgeDispatchEvent(const char* receiver, const char* func, int n, CGuava7LuaBridgeEvent* event)
{
if (L)
{
lua_getglobal(L, receiver);
if (!lua_isnil(L, -1))
{
lua_getfield(L, -1, "dispatchEvent"); // get the dispatchEvent function of receiver
lua_pushvalue(L, -2); // duplicate receiver, this will be the 1st parameter of dispatchEvent
// create an Event object (this will be the 2nd parameter of dispatchEvent)
lua_getglobal(L, "Event"); // get the global Event table
lua_getfield(L, -1, "new"); // get its new function
lua_remove(L, -2); // remove the global Event table
lua_pushstring(L, func);
lua_call(L, 1, 1); // call as Event.new("complete")
if (event)
{
for (int i = 0; i < n; i++)
{
lua_pushstring(L, event->value);
lua_setfield(L, -2, event->key);
}
}
__android_log_print(ANDROID_LOG_INFO, "CGuava7LuaBridge", "DispatchEvent: %s %s %d\n",receiver,func,n);
if (lua_pcall(L, 2, 0, 0) != 0) // call dispatchEvent with receiver and event
{
__android_log_print(ANDROID_LOG_INFO, "CGuava7LuaBridge", "!!! error when DispatchEvent function %s %s: %s\n",receiver, func, lua_tostring(L, -1));
}
}
else
{
__android_log_print(ANDROID_LOG_INFO, "CGuava7LuaBridge", "!!! DispatchEvent receiver NOT FOUND");
}
lua_pop(L, 1); // pop nil or receiver
}
}
Comments
It's sometimes crashing because Lua thread and main UI thread are different. The current way is to use Gideros' internal event queue (which is thread safe).
Lets work on this code collaboratively to make it thread safe and then we (I and Arturs) will write a documentation about it.
Are you using a version control system so that you can give access to me?
Would you please give me skype.
My skype: ricky.ngk
Likes: yarnee
http://www.nightspade.com
Mine is antzhengyi
https://sites.google.com/site/xraystudiogame
Is there a stable version for the android event bridge?
And is there a similar example for ios ?
It could be a nice feature, to have a lua/gideros wrapper for all possible events of a plattform. Cheers, ioannis.giannakakis
And I think its not possible for any arbitrary information, but rather need to have predefined structure or proper serialization/deseralization mechanisms