From 04cabb0996bd3d753d96fa0a8ee95f29055bef5b Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 23 Dec 2012 13:16:58 +0100 Subject: [PATCH] (Android) refactor JNI code - still can't load ClassLoader --- android/native/jni/android_general.h | 20 +---- android/native/jni/jni_wrapper.c | 106 +++++++++++++++++++++++++++ android/native/jni/jni_wrapper.h | 45 ++++++++++++ android/native/jni/main.c | 52 +------------ console/griffin/griffin.c | 4 + 5 files changed, 160 insertions(+), 67 deletions(-) create mode 100644 android/native/jni/jni_wrapper.c create mode 100644 android/native/jni/jni_wrapper.h diff --git a/android/native/jni/android_general.h b/android/native/jni/android_general.h index 8542921cdf..7a771a2502 100644 --- a/android/native/jni/android_general.h +++ b/android/native/jni/android_general.h @@ -36,29 +36,11 @@ struct droid int32_t last_orient; bool window_ready; float disp_refresh_rate; -}; - -struct jni_params -{ - JavaVM *java_vm; - jobject class_obj; - char class_name[128]; - char method_name[128]; - char method_signature[128]; - char obj_method_name[128]; - char obj_method_signature[128]; -}; - -struct jni_out_params_char -{ - char *out; - size_t out_sizeof; - char in[128]; + jobject class_loader_obj; }; extern struct droid g_android; -void jni_get(void *params, void *out_params, unsigned out_type); bool android_run_events(struct android_app* android_app); #endif diff --git a/android/native/jni/jni_wrapper.c b/android/native/jni/jni_wrapper.c new file mode 100644 index 0000000000..3646608042 --- /dev/null +++ b/android/native/jni/jni_wrapper.c @@ -0,0 +1,106 @@ +/* RetroArch - A frontend for libretro. + * Copyright (C) 2010-2012 - Hans-Kristian Arntzen + * Copyright (C) 2011-2012 - Daniel De Matteis + * + * RetroArch is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with RetroArch. + * If not, see . + */ + +#include +#include +#include "jni_wrapper.h" +#include "../../../retroarch_logger.h" + +jint JNI_OnLoad(JavaVM *vm, void *reserved) +{ + (void)reserved; + + /* + JNIEnv *env; + jclass loader; + + (*vm)->AttachCurrentThread(vm, &env, 0); + + loader = (*env)->FindClass(env, "org/retroarch/browser/ModuleActivity"); + + if (loader == NULL) + { + RARCH_ERR("JNI - Can't find LoaderClass.\n"); + goto do_exit; + } + */ + + RARCH_LOG("JNI - Successfully executed JNI_OnLoad.\n"); + + return JNI_VERSION_1_4; + +do_exit: + (*vm)->DetachCurrentThread(vm); + return -1; +} + +void jni_get(void *params, void *out_params, unsigned out_type) +{ + JNIEnv *env; + + if (!params) + return; + + struct jni_params *in_params = (struct jni_params*)params; + + if (!in_params) + return; + + JavaVM *vm = in_params->java_vm; + jobject obj = NULL; + jmethodID gseid = NULL; + + (*vm)->AttachCurrentThread(vm, &env, 0); + + if (in_params->class_obj) + { + jclass acl = (*env)->GetObjectClass(env, in_params->class_obj); //class pointer + jmethodID giid = (*env)->GetMethodID(env, acl, in_params->method_name, in_params->method_signature); + obj = (*env)->CallObjectMethod(env, in_params->class_obj, giid); //Got our object + } + + if (in_params->obj_method_name && obj) + { + jclass class_obj = (*env)->GetObjectClass(env, obj); //class pointer of object + gseid = (*env)->GetMethodID(env, class_obj, in_params->obj_method_name, + in_params->obj_method_signature); + } + + switch (out_type) + { + case JNI_OUT_CHAR: + if(gseid != NULL) + { + struct jni_out_params_char *out_params_char = (struct jni_out_params_char*)out_params; + + if (!out_params_char) + goto do_exit; + + jstring jsParam1 = (*env)->CallObjectMethod(env, obj, + gseid, (*env)->NewStringUTF(env, out_params_char->in)); + const char *test_argv = (*env)->GetStringUTFChars(env, jsParam1, 0); + strncpy(out_params_char->out, test_argv, out_params_char->out_sizeof); + (*env)->ReleaseStringUTFChars(env, jsParam1, test_argv); + } + break; + case JNI_OUT_NONE: + default: + break; + } + +do_exit: + (*vm)->DetachCurrentThread(vm); +} diff --git a/android/native/jni/jni_wrapper.h b/android/native/jni/jni_wrapper.h new file mode 100644 index 0000000000..c8a87c0fa3 --- /dev/null +++ b/android/native/jni/jni_wrapper.h @@ -0,0 +1,45 @@ +/* RetroArch - A frontend for libretro. + * Copyright (C) 2010-2012 - Hans-Kristian Arntzen + * Copyright (C) 2011-2012 - Daniel De Matteis + * + * RetroArch is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with RetroArch. + * If not, see . + */ + +#ifndef _JNI_WRAPPER_H +#define _JNI_WRAPPER_H + +enum +{ + JNI_OUT_NONE = 0, + JNI_OUT_CHAR +}; + +struct jni_params +{ + JavaVM *java_vm; + jobject class_obj; + char class_name[128]; + char method_name[128]; + char method_signature[128]; + char obj_method_name[128]; + char obj_method_signature[128]; +}; + +struct jni_out_params_char +{ + char *out; + size_t out_sizeof; + char in[128]; +}; + +void jni_get(void *params, void *out_params, unsigned out_type); +#endif diff --git a/android/native/jni/main.c b/android/native/jni/main.c index 1ff89b059d..e6d8dfd444 100644 --- a/android/native/jni/main.c +++ b/android/native/jni/main.c @@ -20,8 +20,8 @@ #include #include -#include #include "android_general.h" +#include "jni_wrapper.h" #include "../../../general.h" #include "../../../performance.h" #include "../../../driver.h" @@ -68,53 +68,6 @@ static void print_cur_config(struct android_app* android_app) AConfiguration_getUiModeNight(android_app->config)); } -#define JNI_OUT_CHAR 0 - -void jni_get(void *params, void *out_params, unsigned out_type) -{ - JNIEnv *env; - struct jni_params *in_params = (struct jni_params*)params; - - JavaVM *vm = in_params->java_vm; - jobject obj = NULL; - jmethodID gseid = NULL; - - (*vm)->AttachCurrentThread(vm, &env, 0); - - if (in_params->class_obj) - { - jclass acl = (*env)->GetObjectClass(env, in_params->class_obj); //class pointer - jmethodID giid = (*env)->GetMethodID(env, acl, in_params->method_name, in_params->method_signature); - obj = (*env)->CallObjectMethod(env, in_params->class_obj, giid); //Got our object - } - - if (in_params->obj_method_name && obj) - { - jclass class_obj = (*env)->GetObjectClass(env, obj); //class pointer of object - gseid = (*env)->GetMethodID(env, class_obj, in_params->obj_method_name, - in_params->obj_method_signature); - } - - switch (out_type) - { - case JNI_OUT_CHAR: - if(gseid != NULL) - { - struct jni_out_params_char *out_params_char = (struct jni_out_params_char*)out_params; - jstring jsParam1 = (*env)->CallObjectMethod(env, obj, - gseid, (*env)->NewStringUTF(env, out_params_char->in)); - const char *test_argv = (*env)->GetStringUTFChars(env, jsParam1, 0); - strncpy(out_params_char->out, test_argv, out_params_char->out_sizeof); - (*env)->ReleaseStringUTFChars(env, jsParam1, test_argv); - } - break; - default: - break; - } - - (*vm)->DetachCurrentThread(vm); -} - /** * Process the next main command. */ @@ -349,11 +302,14 @@ static void* android_app_entry(void* param) char rom_path[512]; char libretro_path[512]; + JNI_OnLoad(g_android.app->activity->vm, NULL); + // Get arguments */ struct jni_params jni_args; jni_args.java_vm = g_android.app->activity->vm; jni_args.class_obj = g_android.app->activity->clazz; + snprintf(jni_args.method_name, sizeof(jni_args.method_name), "getIntent"); snprintf(jni_args.method_signature, sizeof(jni_args.method_signature), "()Landroid/content/Intent;"); snprintf(jni_args.obj_method_name, sizeof(jni_args.obj_method_name), "getStringExtra"); diff --git a/console/griffin/griffin.c b/console/griffin/griffin.c index 06f895e38d..cca6345a94 100644 --- a/console/griffin/griffin.c +++ b/console/griffin/griffin.c @@ -18,6 +18,10 @@ #include "../../msvc/msvc_compat.h" #endif +#ifdef ANDROID +#include "../../android/native/jni/jni_wrapper.c" +#endif + /*============================================================ LOGGERS ============================================================ */