12 #include <freerdp/config.h>
16 #include "android_jni_callback.h"
17 #include "android_freerdp_jni.h"
19 #include <freerdp/log.h>
20 #define TAG CLIENT_TAG("android.callback")
23 static jobject jLibFreeRDPObject;
25 static const char* jLibFreeRDPPath = JAVA_LIBFREERDP_CLASS;
27 static void jni_load_class(JNIEnv* env,
const char* path, jobject* objptr)
32 WLog_DBG(TAG,
"jni_load_class: %s", path);
33 class = (*env)->FindClass(env, path);
37 WLog_ERR(TAG,
"jni_load_class: failed to find class %s", path);
41 method = (*env)->GetMethodID(env,
class,
"<init>",
"()V");
45 WLog_ERR(TAG,
"jni_load_class: failed to find class constructor of %s", path);
49 object = (*env)->NewObject(env,
class, method);
53 WLog_ERR(TAG,
"jni_load_class: failed create new object of %s", path);
57 (*objptr) = (*env)->NewGlobalRef(env,
object);
64 jint init_callback_environment(JavaVM* vm, JNIEnv* env)
67 jni_load_class(env, jLibFreeRDPPath, &jLibFreeRDPObject);
68 return JNI_VERSION_1_6;
72 jboolean jni_attach_thread(JNIEnv** env)
74 if ((*jVM)->GetEnv(jVM, (
void**)env, JNI_VERSION_1_4) != JNI_OK)
76 WLog_DBG(TAG,
"android_java_callback: attaching current thread");
77 (*jVM)->AttachCurrentThread(jVM, env, NULL);
79 if ((*jVM)->GetEnv(jVM, (
void**)env, JNI_VERSION_1_4) != JNI_OK)
81 WLog_ERR(TAG,
"android_java_callback: failed to obtain current JNI environment");
91 void jni_detach_thread()
93 (*jVM)->DetachCurrentThread(jVM);
97 static void java_callback_void(jobject obj,
const char* callback,
const char* signature,
104 WLog_DBG(TAG,
"java_callback: %s (%s)", callback, signature);
105 attached = jni_attach_thread(&env);
106 jObjClass = (*env)->GetObjectClass(env, obj);
110 WLog_ERR(TAG,
"android_java_callback: failed to get class reference");
114 jCallback = (*env)->GetStaticMethodID(env, jObjClass, callback, signature);
118 WLog_ERR(TAG,
"android_java_callback: failed to get method id");
122 (*env)->CallStaticVoidMethodV(env, jObjClass, jCallback, args);
125 if (attached == JNI_TRUE)
130 static jboolean java_callback_bool(jobject obj,
const char* callback,
const char* signature,
136 jboolean res = JNI_FALSE;
138 WLog_DBG(TAG,
"java_callback: %s (%s)", callback, signature);
139 attached = jni_attach_thread(&env);
140 jObjClass = (*env)->GetObjectClass(env, obj);
144 WLog_ERR(TAG,
"android_java_callback: failed to get class reference");
148 jCallback = (*env)->GetStaticMethodID(env, jObjClass, callback, signature);
152 WLog_ERR(TAG,
"android_java_callback: failed to get method id");
156 res = (*env)->CallStaticBooleanMethodV(env, jObjClass, jCallback, args);
159 if (attached == JNI_TRUE)
166 static jint java_callback_int(jobject obj,
const char* callback,
const char* signature,
174 WLog_DBG(TAG,
"java_callback: %s (%s)", callback, signature);
175 attached = jni_attach_thread(&env);
176 jObjClass = (*env)->GetObjectClass(env, obj);
180 WLog_ERR(TAG,
"android_java_callback: failed to get class reference");
184 jCallback = (*env)->GetStaticMethodID(env, jObjClass, callback, signature);
188 WLog_ERR(TAG,
"android_java_callback: failed to get method id");
192 res = (*env)->CallStaticIntMethodV(env, jObjClass, jCallback, args);
195 if (attached == JNI_TRUE)
202 void freerdp_callback(
const char* callback,
const char* signature, ...)
205 va_start(vl, signature);
206 java_callback_void(jLibFreeRDPObject, callback, signature, vl);
210 jboolean freerdp_callback_bool_result(
const char* callback,
const char* signature, ...)
213 va_start(vl, signature);
214 jboolean res = java_callback_bool(jLibFreeRDPObject, callback, signature, vl);
219 jint freerdp_callback_int_result(
const char* callback,
const char* signature, ...)
222 va_start(vl, signature);
223 jint res = java_callback_int(jLibFreeRDPObject, callback, signature, vl);