Port JNI class from Lime legacy

This commit is contained in:
Joshua Granick
2015-08-11 15:39:41 -07:00
parent f1b17dfb8d
commit dd8ca69353
11 changed files with 2523 additions and 62 deletions

View File

@@ -85,6 +85,7 @@ https://github.com/haxenme/nme
project/include/utils/QuickVec.h
project/src/graphics/format/
project/src/graphics/opengl/
project/src/system/JNI.cpp
project/src/text/Font.cpp
project/src/utils/ByteArray.cpp
project/src/utils/LZMA.cpp

371
lime/system/JNI.hx Normal file
View File

@@ -0,0 +1,371 @@
package lime.system;
class JNI {
private static var alreadyCreated = new Map<String, Bool> ();
private static var initialized = false;
public static function callMember (method:Dynamic, jobject:Dynamic, a:Array<Dynamic>):Dynamic {
switch (a.length) {
case 0: return method (jobject);
case 1: return method (jobject, a[0]);
case 2: return method (jobject, a[0], a[1]);
case 3: return method (jobject, a[0], a[1], a[2]);
case 4: return method (jobject, a[0], a[1], a[2], a[3]);
case 5: return method (jobject, a[0], a[1], a[2], a[3], a[4]);
case 6: return method (jobject, a[0], a[1], a[2], a[3], a[4], a[5]);
case 7: return method (jobject, a[0], a[1], a[2], a[3], a[4], a[5], a[6]);
default: return null;
}
}
public static function callStatic (method:Dynamic, a:Array<Dynamic>):Dynamic {
switch (a.length) {
case 0: return method ();
case 1: return method (a[0]);
case 2: return method (a[0], a[1]);
case 3: return method (a[0], a[1], a[2]);
case 4: return method (a[0], a[1], a[2], a[3]);
case 5: return method (a[0], a[1], a[2], a[3], a[4]);
case 6: return method (a[0], a[1], a[2], a[3], a[4], a[5]);
case 7: return method (a[0], a[1], a[2], a[3], a[4], a[5], a[6]);
default: return null;
}
}
public static function createMemberField (className:String, memberName:String, signature:String):JNIMemberField {
init ();
#if android
return new JNIMemberField (lime_jni_create_field (className, memberName, signature, false));
#else
return null;
#end
}
public static function createMemberMethod (className:String, memberName:String, signature:String, useArray:Bool = false, quietFail:Bool = false):Dynamic {
init ();
#if android
className = className.split (".").join ("/");
var handle = lime_jni_create_method (className, memberName, signature, false, quietFail);
if (handle == null) {
if (quietFail) {
return null;
}
throw "Could not find member function \"" + memberName + "\"";
}
var method = new JNIMethod (handle);
return method.getMemberMethod (useArray);
#else
return null;
#end
}
public static function createStaticField (className:String, memberName:String, signature:String):JNIStaticField {
init ();
#if android
return new JNIStaticField (lime_jni_create_field (className, memberName, signature, true));
#else
return null;
#end
}
public static function createStaticMethod (className:String, memberName:String, signature:String, useArray:Bool = false, quietFail:Bool = false):Dynamic {
init ();
#if android
className = className.split (".").join ("/");
var handle = lime_jni_create_method (className, memberName, signature, true, quietFail);
if (handle == null) {
if (quietFail) {
return null;
}
throw "Could not find static function \"" + memberName + "\"";
}
var method = new JNIMethod (handle);
return method.getStaticMethod (useArray);
#else
return null;
#end
}
public static function getEnv ():Dynamic {
init ();
return lime_jni_get_env ();
}
private static function init ():Void {
if (!initialized) {
initialized = true;
#if android
var method = System.load ("lime", "lime_jni_init_callback", 1);
method (onCallback);
#end
}
}
private static function onCallback (object:Dynamic, method:Dynamic, args:Dynamic):Dynamic {
var field = Reflect.field (object, method);
if (field != null) {
return Reflect.callMethod (object, field, args);
}
trace ("onCallback - unknown field " + method);
return null;
}
// Native Methods
#if (cpp || neko || nodejs)
private static var lime_jni_create_field = System.load ("lime", "lime_jni_create_field", 4);
private static var lime_jni_create_method = System.load ("lime", "lime_jni_create_method", 5);
private static var lime_jni_get_env = System.load ("lime", "lime_jni_get_env", 0);
private static var lime_jni_call_member = System.load ("lime", "lime_jni_call_member", 3);
private static var lime_jni_call_static = System.load ("lime", "lime_jni_call_static", 2);
#end
}
class JNIMemberField {
private var field:Dynamic;
public function new (field:Dynamic) {
this.field = field;
}
public function get (jobject:Dynamic):Dynamic {
#if android
return lime_jni_get_member (field, jobject);
#else
return null;
#end
}
public function set (jobject:Dynamic, value:Dynamic):Dynamic {
#if android
lime_jni_set_member (field, jobject, value);
#end
return value;
}
// Native Methods
#if (cpp || neko || nodejs)
private static var lime_jni_get_member = System.load ("lime", "lime_jni_get_member", 2);
private static var lime_jni_set_member = System.load ("lime", "lime_jni_set_member", 3);
#end
}
class JNIStaticField {
private var field:Dynamic;
public function new (field:Dynamic) {
this.field = field;
}
public function get ():Dynamic {
#if android
return lime_jni_get_static (field);
#else
return null;
#end
}
public function set (value:Dynamic):Dynamic {
#if android
lime_jni_set_static (field, value);
#end
return value;
}
// Native Methods
#if (cpp || neko || nodejs)
private static var lime_jni_get_static = System.load ("lime", "lime_jni_get_static", 1);
private static var lime_jni_set_static = System.load ("lime", "lime_jni_set_static", 2);
#end
}
class JNIMethod {
private var method:Dynamic;
public function new (method:Dynamic) {
this.method = method;
}
public function callMember (args:Array<Dynamic>):Dynamic {
#if android
var jobject = args.shift ();
return lime_jni_call_member (method, jobject, args);
#else
return null;
#end
}
public function callStatic (args:Array<Dynamic>):Dynamic {
#if android
return lime_jni_call_static (method, args);
#else
return null;
#end
}
public function getMemberMethod (useArray:Bool):Dynamic {
if (useArray) {
return callMember;
} else {
return Reflect.makeVarArgs (callMember);
}
}
public function getStaticMethod (useArray:Bool):Dynamic {
if (useArray) {
return callStatic;
} else {
return Reflect.makeVarArgs (callStatic);
}
}
// Native Methods
#if (cpp || neko || nodejs)
private static var lime_jni_call_member = System.load ("lime", "lime_jni_call_member", 3);
private static var lime_jni_call_static = System.load ("lime", "lime_jni_call_static", 2);
#end
}

View File

@@ -1,33 +0,0 @@
package lime.utils;
import lime.system.System;
class JNI {
public static function getEnv ():Dynamic {
#if android
return lime_jni_getenv ();
#else
return null;
#end
}
// Native Methods
#if (cpp || neko || nodejs)
private static var lime_jni_getenv = System.load ("lime", "lime_jni_getenv", 0);
#end
}

View File

@@ -180,7 +180,6 @@
<file name="src/backend/sdl/SDLGamepad.cpp" />
<file name="src/backend/sdl/SDLMouse.cpp" />
<file name="src/backend/sdl/SDLSystem.cpp" />
<file name="src/backend/sdl/SDLJNI.cpp" />
</section>
@@ -203,6 +202,7 @@
<file name="src/math/Matrix3.cpp" />
<file name="src/math/Rectangle.cpp" />
<file name="src/math/Vector2.cpp" />
<file name="src/system/JNI.cpp" if="android" />
<file name="src/ui/GamepadEvent.cpp" />
<file name="src/ui/KeyEvent.cpp" />
<file name="src/ui/MouseEvent.cpp" />

View File

@@ -1,5 +1,5 @@
#ifndef LIME_UTILS_JNI_H
#define LIME_UTILS_JNI_H
#ifndef LIME_SYSTEM_JNI_H
#define LIME_SYSTEM_JNI_H
namespace lime {

View File

@@ -8,10 +8,6 @@ namespace lime {
class Object {
public:
int ref_count;
protected:
virtual ~Object () {}
@@ -28,6 +24,25 @@ namespace lime {
}
Object *IncRef () {
ref_count++;
return this;
}
void DecRef () {
ref_count--;
if (ref_count <= 0) {
delete this;
}
}
void drop () {
ref_count--;
@@ -37,6 +52,8 @@ namespace lime {
}
}
int ref_count;
};

View File

@@ -20,6 +20,7 @@
#include <graphics/ImageBuffer.h>
#include <graphics/Renderer.h>
#include <graphics/RenderEvent.h>
#include <system/JNI.h>
#include <system/System.h>
#include <text/Font.h>
#include <text/TextLayout.h>
@@ -33,7 +34,6 @@
#include <ui/TouchEvent.h>
#include <ui/Window.h>
#include <ui/WindowEvent.h>
#include <utils/JNI.h>
#include <utils/LZMA.h>
#include <vm/NekoVM.h>

View File

@@ -1,21 +0,0 @@
#ifdef ANDROID
#include <utils/JNI.h>
#include <SDL_system.h>
namespace lime {
void *JNI::GetEnv () {
return SDL_AndroidGetJNIEnv ();
}
}
#endif

View File

@@ -1,5 +1,6 @@
#include <graphics/PixelFormat.h>
#include <math/Rectangle.h>
#include <system/JNI.h>
#include <system/System.h>
#ifdef HX_MACOS
@@ -46,6 +47,17 @@ namespace lime {
static bool init = false;
void *JNI::GetEnv () {
#ifdef ANDROID
return SDL_AndroidGetJNIEnv ();
#else
return 0;
#endif
}
const char* System::GetDirectory (SystemDirectory type, const char* company, const char* title) {
switch (type) {

2099
project/src/system/JNI.cpp Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -24,6 +24,21 @@ public class GameActivity extends SDLActivity {
public Handler handler;
public static void postUICallback (final long handle) {
Extension.callbackHandler.post (new Runnable () {
@Override public void run () {
Lime.onCallback (handle);
}
});
}
@Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {
for (Extension extension : extensions) {