(Android) Refactor JNI code

This commit is contained in:
twinaphex 2012-12-13 15:32:35 +01:00
parent 54017c88b9
commit c5bda29ce3
4 changed files with 123 additions and 32 deletions

View File

@ -0,0 +1,43 @@
/* 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 <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stddef.h>
#include "jni_utils.h"
void jni_get_char_argv(struct jni_params *params, struct jni_out_params_char *out_params)
{
JNIEnv *env;
JavaVM *vm = params->java_vm;
(*vm)->AttachCurrentThread(vm, &env, 0);
jclass acl = (*env)->GetObjectClass(env, params->class_obj); //class pointer
jmethodID giid = (*env)->GetMethodID(env, acl, params->method_name, params->method_signature);
jobject obj = (*env)->CallObjectMethod(env, params->class_obj, giid); //Got our object
jclass class_obj = (*env)->GetObjectClass(env, obj); //class pointer of object
jmethodID gseid = (*env)->GetMethodID(env, class_obj, params->obj_method_name, params->obj_method_signature);
jstring jsParam1 = (*env)->CallObjectMethod(env, obj, gseid, (*env)->NewStringUTF(env, out_params->in));
const char *test_argv = (*env)->GetStringUTFChars(env, jsParam1, 0);
strncpy(out_params->out, test_argv, out_params->out_sizeof);
(*env)->ReleaseStringUTFChars(env, jsParam1, test_argv);
(*vm)->DetachCurrentThread(vm);
}

View File

@ -0,0 +1,43 @@
/* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef _RARCH_JNI_UTILS_H
#define _RARCH_JNI_UTILS_H
#include <jni.h>
#include "android_glue.h"
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_char_argv(struct jni_params *params, struct jni_out_params_char *out_params);
#endif

View File

@ -15,12 +15,12 @@
*/
#include <stdio.h>
#include <jni.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/resource.h>
#include "jni_utils.h"
#include "android_general.h"
#include "../../../general.h"
#include "../../../performance.h"
@ -73,32 +73,6 @@ int android_get_sdk_version(void)
return AConfiguration_getSdkVersion(g_android.app->config);
}
static void android_get_char_argv(char *argv, size_t sizeof_argv, const char *arg_name)
{
JNIEnv *env;
JavaVM *rarch_vm = g_android.app->activity->vm;
(*rarch_vm)->AttachCurrentThread(rarch_vm, &env, 0);
jobject me = g_android.app->activity->clazz;
jclass acl = (*env)->GetObjectClass(env, me); //class pointer of NativeActivity
jmethodID giid = (*env)->GetMethodID(env, acl, "getIntent", "()Landroid/content/Intent;");
jobject intent = (*env)->CallObjectMethod(env, me, giid); //Got our intent
jclass icl = (*env)->GetObjectClass(env, intent); //class pointer of Intent
jmethodID gseid = (*env)->GetMethodID(env, icl, "getStringExtra", "(Ljava/lang/String;)Ljava/lang/String;");
jstring jsParam1 = (*env)->CallObjectMethod(env, intent, gseid, (*env)->NewStringUTF(env, arg_name));
const char *test_argv = (*env)->GetStringUTFChars(env, jsParam1, 0);
strncpy(argv, test_argv, sizeof_argv);
(*env)->ReleaseStringUTFChars(env, jsParam1, test_argv);
(*rarch_vm)->DetachCurrentThread(rarch_vm);
}
/**
* Process the next main command.
*/
@ -334,12 +308,37 @@ static void* android_app_entry(void* param)
char libretro_path[512];
// Get arguments */
android_get_char_argv(rom_path, sizeof(rom_path), "ROM");
android_get_char_argv(libretro_path, sizeof(libretro_path), "LIBRETRO");
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");
snprintf(jni_args.obj_method_signature, sizeof(jni_args.obj_method_signature), "(Ljava/lang/String;)Ljava/lang/String;");
struct jni_out_params_char out_args;
out_args.out = rom_path;
out_args.out_sizeof = sizeof(rom_path);
snprintf(out_args.in, sizeof(out_args.in), "ROM");
jni_get_char_argv(&jni_args, &out_args);
out_args.out = libretro_path;
out_args.out_sizeof = sizeof(libretro_path);
snprintf(out_args.in, sizeof(out_args.in), "LIBRETRO");
jni_get_char_argv(&jni_args, &out_args);
char refreshrate_char[128];
float refreshrate;
out_args.out = refreshrate_char;
out_args.out_sizeof = sizeof(refreshrate_char);
snprintf(out_args.in, sizeof(out_args.in), "REFRESHRATE");
android_get_char_argv(refreshrate_char,sizeof(refreshrate_char), "REFRESHRATE");
jni_get_char_argv(&jni_args, &out_args);
refreshrate = (float)strtod(refreshrate_char, NULL);

View File

@ -28,11 +28,18 @@ LOGGERS
#include "../logger/logger.c"
#endif
#ifdef RARCH_CONSOLE
/*============================================================
JNI UTILS
============================================================ */
#ifdef ANDROID
#include "../../android/native/jni/jni_utils.c"
#endif
/*============================================================
CONSOLE EXTENSIONS
============================================================ */
#ifdef RARCH_CONSOLE
#include "../rarch_console.h"
@ -76,7 +83,6 @@ default_paths_t default_paths;
#include "../rarch_console_libretro_mgmt.c"
#endif
#endif
/*============================================================