2022-03-15 13:29:32 +01:00
/*************************************************************************/
2022-03-23 20:46:05 +01:00
/* java_pandemonium_lib_jni.cpp */
2022-03-15 13:29:32 +01:00
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2022-03-23 20:46:05 +01:00
# include "java_pandemonium_lib_jni.h"
# include "java_pandemonium_io_wrapper.h"
# include "java_pandemonium_wrapper.h"
2022-03-15 13:29:32 +01:00
# include "android/asset_manager_jni.h"
# include "android_input_handler.h"
# include "api/java_class_wrapper.h"
# include "api/jni_singleton.h"
2022-08-17 15:35:28 +02:00
# include "core/config/engine.h"
# include "core/config/project_settings.h"
2022-03-15 13:29:32 +01:00
# include "dir_access_jandroid.h"
# include "file_access_android.h"
2022-07-29 19:24:07 +02:00
# include "file_access_filesystem_jandroid.h"
2022-03-15 13:29:32 +01:00
# include "jni_utils.h"
# include "main/input_default.h"
# include "main/main.h"
# include "net_socket_android.h"
# include "os_android.h"
# include "string_android.h"
# include "thread_jandroid.h"
# include <android/input.h>
# include <unistd.h>
static JavaClassWrapper * java_class_wrapper = NULL ;
static OS_Android * os_android = NULL ;
static AndroidInputHandler * input_handler = NULL ;
2022-03-23 20:46:05 +01:00
static PandemoniumJavaWrapper * pandemonium_java = NULL ;
static PandemoniumIOJavaWrapper * pandemonium_io_java = NULL ;
2022-03-15 13:29:32 +01:00
static bool initialized = false ;
static SafeNumeric < int > step ; // Shared between UI and render threads
static Size2 new_size ;
static Vector3 accelerometer ;
static Vector3 gravity ;
static Vector3 magnetometer ;
static Vector3 gyroscope ;
static void _initialize_java_modules ( ) {
if ( ! ProjectSettings : : get_singleton ( ) - > has_setting ( " android/modules " ) ) {
return ;
}
String modules = ProjectSettings : : get_singleton ( ) - > get ( " android/modules " ) ;
modules = modules . strip_edges ( ) ;
if ( modules = = String ( ) ) {
return ;
}
Vector < String > mods = modules . split ( " , " , false ) ;
if ( mods . size ( ) ) {
2022-03-23 20:46:05 +01:00
jobject cls = pandemonium_java - > get_class_loader ( ) ;
2022-03-15 13:29:32 +01:00
// TODO create wrapper for class loader
JNIEnv * env = get_jni_env ( ) ;
jclass classLoader = env - > FindClass ( " java/lang/ClassLoader " ) ;
jmethodID findClass = env - > GetMethodID ( classLoader , " loadClass " , " (Ljava/lang/String;)Ljava/lang/Class; " ) ;
for ( int i = 0 ; i < mods . size ( ) ; i + + ) {
String m = mods [ i ] ;
2022-03-23 20:46:05 +01:00
// Deprecated in Pandemonium 3.2.2, it's now a plugin to enable in export preset.
2022-03-24 21:39:55 +01:00
if ( m = = " java/src/net/relintai/pandemonium/pandemonium/PandemoniumPaymentV3 " ) {
2022-03-23 20:46:05 +01:00
WARN_PRINT ( " PandemoniumPaymentV3 is deprecated and is replaced by the 'PandemoniumPayment' plugin, which should be enabled in the Android export preset. " ) ;
2022-03-15 13:29:32 +01:00
print_line ( " Skipping Android module: " + m ) ;
continue ;
}
print_line ( " Loading Android module: " + m ) ;
jstring strClassName = env - > NewStringUTF ( m . utf8 ( ) . get_data ( ) ) ;
jclass singletonClass = ( jclass ) env - > CallObjectMethod ( cls , findClass , strClassName ) ;
ERR_CONTINUE_MSG ( ! singletonClass , " Couldn't find singleton for class: " + m + " . " ) ;
2022-03-24 21:39:55 +01:00
jmethodID initialize = env - > GetStaticMethodID ( singletonClass , " initialize " , " (Landroid/app/Activity;)Ljava/src/net/relintai/pandemonium/pandemonium/Pandemonium$SingletonBase; " ) ;
2022-03-23 20:46:05 +01:00
ERR_CONTINUE_MSG ( ! initialize , " Couldn't find proper initialize function 'public static Pandemonium.SingletonBase Class::initialize(Activity p_activity)' initializer for singleton class: " + m + " . " ) ;
2022-03-15 13:29:32 +01:00
2022-03-23 20:46:05 +01:00
jobject obj = env - > CallStaticObjectMethod ( singletonClass , initialize , pandemonium_java - > get_activity ( ) ) ;
2022-03-15 13:29:32 +01:00
env - > NewGlobalRef ( obj ) ;
}
}
}
extern " C " {
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_setVirtualKeyboardHeight ( JNIEnv * env , jclass clazz , jint p_height ) {
if ( pandemonium_io_java ) {
pandemonium_io_java - > set_vk_height ( p_height ) ;
2022-03-15 13:29:32 +01:00
}
}
2022-07-29 19:24:07 +02:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_initialize ( JNIEnv * env , jclass clazz , jobject p_activity , jobject p_pandemonium_instance , jobject p_asset_manager , jobject p_pandemonium_io , jobject p_net_utils , jobject p_directory_access_handler , jobject p_file_access_handler , jboolean p_use_apk_expansion ) {
2022-03-15 13:29:32 +01:00
initialized = true ;
JavaVM * jvm ;
env - > GetJavaVM ( & jvm ) ;
// create our wrapper classes
2022-07-29 19:24:07 +02:00
pandemonium_java = new PandemoniumJavaWrapper ( env , p_activity , p_pandemonium_instance ) ;
pandemonium_io_java = new PandemoniumIOJavaWrapper ( env , p_pandemonium_io ) ;
2022-03-15 13:29:32 +01:00
init_thread_jandroid ( jvm , env ) ;
jobject amgr = env - > NewGlobalRef ( p_asset_manager ) ;
FileAccessAndroid : : asset_manager = AAssetManager_fromJava ( env , amgr ) ;
2022-07-29 21:45:04 +02:00
DirAccessJAndroid : : setup ( p_directory_access_handler ) ;
2022-07-29 19:24:07 +02:00
FileAccessFilesystemJAndroid : : setup ( p_file_access_handler ) ;
NetSocketAndroid : : setup ( p_net_utils ) ;
2022-03-15 13:29:32 +01:00
2022-03-23 20:46:05 +01:00
os_android = new OS_Android ( pandemonium_java , pandemonium_io_java , p_use_apk_expansion ) ;
2022-03-15 13:29:32 +01:00
char wd [ 500 ] ;
getcwd ( wd , 500 ) ;
2022-03-23 20:46:05 +01:00
pandemonium_java - > on_video_init ( env ) ;
2022-03-15 13:29:32 +01:00
}
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_ondestroy ( JNIEnv * env , jclass clazz ) {
2022-03-15 13:29:32 +01:00
// lets cleanup
2022-03-22 19:35:57 +01:00
if ( java_class_wrapper ) {
memdelete ( java_class_wrapper ) ;
}
2022-03-23 20:46:05 +01:00
if ( pandemonium_io_java ) {
delete pandemonium_io_java ;
2022-03-15 13:29:32 +01:00
}
2022-03-23 20:46:05 +01:00
if ( pandemonium_java ) {
pandemonium_java - > destroy_offscreen_gl ( env ) ;
delete pandemonium_java ;
2022-03-15 13:29:32 +01:00
}
if ( input_handler ) {
delete input_handler ;
}
if ( os_android ) {
delete os_android ;
}
}
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_setup ( JNIEnv * env , jclass clazz , jobjectArray p_cmdline ) {
2022-03-15 13:29:32 +01:00
setup_android_thread ( ) ;
const char * * cmdline = NULL ;
jstring * j_cmdline = NULL ;
int cmdlen = 0 ;
if ( p_cmdline ) {
cmdlen = env - > GetArrayLength ( p_cmdline ) ;
if ( cmdlen ) {
cmdline = ( const char * * ) memalloc ( ( cmdlen + 1 ) * sizeof ( const char * ) ) ;
ERR_FAIL_NULL_MSG ( cmdline , " Out of memory. " ) ;
cmdline [ cmdlen ] = NULL ;
j_cmdline = ( jstring * ) memalloc ( cmdlen * sizeof ( jstring ) ) ;
ERR_FAIL_NULL_MSG ( j_cmdline , " Out of memory. " ) ;
for ( int i = 0 ; i < cmdlen ; i + + ) {
jstring string = ( jstring ) env - > GetObjectArrayElement ( p_cmdline , i ) ;
const char * rawString = env - > GetStringUTFChars ( string , 0 ) ;
cmdline [ i ] = rawString ;
j_cmdline [ i ] = string ;
}
}
}
2022-03-22 19:35:57 +01:00
Error err = Main : : setup ( OS_Android : : ANDROID_EXEC_PATH , cmdlen , ( char * * ) cmdline , false ) ;
2022-03-15 13:29:32 +01:00
if ( cmdline ) {
if ( j_cmdline ) {
for ( int i = 0 ; i < cmdlen ; + + i ) {
env - > ReleaseStringUTFChars ( j_cmdline [ i ] , cmdline [ i ] ) ;
}
memfree ( j_cmdline ) ;
}
memfree ( cmdline ) ;
}
2022-07-29 21:45:04 +02:00
// Note: --help and --version return ERR_HELP, but this should be translated to 0 if exit codes are propagated.
2022-03-15 13:29:32 +01:00
if ( err ! = OK ) {
return ; // should exit instead and print the error
}
2022-03-23 20:46:05 +01:00
java_class_wrapper = memnew ( JavaClassWrapper ( pandemonium_java - > get_activity ( ) ) ) ;
2022-03-15 13:29:32 +01:00
ClassDB : : register_class < JNISingleton > ( ) ;
_initialize_java_modules ( ) ;
}
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_resize ( JNIEnv * env , jclass clazz , jint width , jint height ) {
2022-03-15 13:29:32 +01:00
if ( os_android )
os_android - > set_display_size ( Size2 ( width , height ) ) ;
}
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_newcontext ( JNIEnv * env , jclass clazz ) {
2022-03-15 13:29:32 +01:00
if ( os_android ) {
if ( step . get ( ) = = 0 ) {
// During startup
2022-03-23 20:46:05 +01:00
os_android - > set_offscreen_gl_available ( pandemonium_java - > create_offscreen_gl ( env ) ) ;
2022-03-15 13:29:32 +01:00
} else {
// GL context recreated because it was lost; restart app to let it reload everything
step . set ( - 1 ) ; // Ensure no further steps are attempted and no further events are sent
os_android - > main_loop_end ( ) ;
2022-03-23 20:46:05 +01:00
pandemonium_java - > restart ( env ) ;
2022-03-15 13:29:32 +01:00
}
}
}
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_back ( JNIEnv * env , jclass clazz ) {
2022-03-15 13:29:32 +01:00
if ( step . get ( ) = = 0 )
return ;
if ( os_android - > get_main_loop ( ) ) {
os_android - > get_main_loop ( ) - > notification ( MainLoop : : NOTIFICATION_WM_GO_BACK_REQUEST ) ;
}
}
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_step ( JNIEnv * env , jclass clazz ) {
2022-03-15 13:29:32 +01:00
if ( step . get ( ) = = - 1 )
return ;
if ( step . get ( ) = = 0 ) {
2022-03-23 20:46:05 +01:00
// Since Pandemonium is initialized on the UI thread, _main_thread_id was set to that thread's id,
// but for Pandemonium purposes, the main thread is the one running the game loop
2022-03-15 13:29:32 +01:00
Main : : setup2 ( Thread : : get_caller_id ( ) ) ;
input_handler = new AndroidInputHandler ( ) ;
step . increment ( ) ;
return ;
}
if ( step . get ( ) = = 1 ) {
if ( ! Main : : start ( ) ) {
return ; // should exit instead and print the error
}
2022-03-23 20:46:05 +01:00
pandemonium_java - > on_pandemonium_setup_completed ( env ) ;
2022-03-15 13:29:32 +01:00
os_android - > main_loop_begin ( ) ;
2022-03-23 20:46:05 +01:00
pandemonium_java - > on_pandemonium_main_loop_started ( env ) ;
2022-03-15 13:29:32 +01:00
step . increment ( ) ;
}
os_android - > process_accelerometer ( accelerometer ) ;
os_android - > process_gravity ( gravity ) ;
os_android - > process_magnetometer ( magnetometer ) ;
os_android - > process_gyroscope ( gyroscope ) ;
if ( os_android - > main_loop_iterate ( ) ) {
2022-03-23 20:46:05 +01:00
pandemonium_java - > force_quit ( env ) ;
2022-03-15 13:29:32 +01:00
}
}
void touch_preprocessing ( JNIEnv * env , jclass clazz , jint input_device , jint ev , jint pointer , jint pointer_count , jfloatArray positions , jint buttons_mask , jfloat vertical_factor , jfloat horizontal_factor ) {
if ( step . get ( ) < = 0 )
return ;
Vector < AndroidInputHandler : : TouchPos > points ;
for ( int i = 0 ; i < pointer_count ; i + + ) {
jfloat p [ 3 ] ;
env - > GetFloatArrayRegion ( positions , i * 3 , 3 , p ) ;
AndroidInputHandler : : TouchPos tp ;
tp . pos = Point2 ( p [ 1 ] , p [ 2 ] ) ;
tp . id = ( int ) p [ 0 ] ;
points . push_back ( tp ) ;
}
if ( ( input_device & AINPUT_SOURCE_MOUSE ) = = AINPUT_SOURCE_MOUSE ) {
input_handler - > process_mouse_event ( ev , buttons_mask , points [ 0 ] . pos , vertical_factor , horizontal_factor ) ;
} else {
input_handler - > process_touch ( ev , pointer , points ) ;
}
}
// Called on the UI thread
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_touch__IIII_3F ( JNIEnv * env , jclass clazz , jint input_device , jint ev , jint pointer , jint pointer_count , jfloatArray position ) {
2022-03-15 13:29:32 +01:00
touch_preprocessing ( env , clazz , input_device , ev , pointer , pointer_count , position ) ;
}
// Called on the UI thread
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_touch__IIII_3FI ( JNIEnv * env , jclass clazz , jint input_device , jint ev , jint pointer , jint pointer_count , jfloatArray position , jint buttons_mask ) {
2022-03-15 13:29:32 +01:00
touch_preprocessing ( env , clazz , input_device , ev , pointer , pointer_count , position , buttons_mask ) ;
}
// Called on the UI thread
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_touch__IIII_3FIFF ( JNIEnv * env , jclass clazz , jint input_device , jint ev , jint pointer , jint pointer_count , jfloatArray position , jint buttons_mask , jfloat vertical_factor , jfloat horizontal_factor ) {
2022-03-15 13:29:32 +01:00
touch_preprocessing ( env , clazz , input_device , ev , pointer , pointer_count , position , buttons_mask , vertical_factor , horizontal_factor ) ;
}
// Called on the UI thread
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_hover ( JNIEnv * env , jclass clazz , jint p_type , jfloat p_x , jfloat p_y ) {
2022-03-15 13:29:32 +01:00
if ( step . get ( ) < = 0 )
return ;
input_handler - > process_hover ( p_type , Point2 ( p_x , p_y ) ) ;
}
// Called on the UI thread
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_doubleTap ( JNIEnv * env , jclass clazz , jint p_button_mask , jint p_x , jint p_y ) {
2022-03-15 13:29:32 +01:00
if ( step . get ( ) < = 0 )
return ;
input_handler - > process_double_tap ( p_button_mask , Point2 ( p_x , p_y ) ) ;
}
// Called on the UI thread
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_joybutton ( JNIEnv * env , jclass clazz , jint p_device , jint p_button , jboolean p_pressed ) {
2022-03-15 13:29:32 +01:00
if ( step . get ( ) < = 0 )
return ;
AndroidInputHandler : : JoypadEvent jevent ;
jevent . device = p_device ;
jevent . type = AndroidInputHandler : : JOY_EVENT_BUTTON ;
jevent . index = p_button ;
jevent . pressed = p_pressed ;
input_handler - > process_joy_event ( jevent ) ;
}
// Called on the UI thread
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_joyaxis ( JNIEnv * env , jclass clazz , jint p_device , jint p_axis , jfloat p_value ) {
2022-03-15 13:29:32 +01:00
if ( step . get ( ) < = 0 )
return ;
AndroidInputHandler : : JoypadEvent jevent ;
jevent . device = p_device ;
jevent . type = AndroidInputHandler : : JOY_EVENT_AXIS ;
jevent . index = p_axis ;
jevent . value = p_value ;
input_handler - > process_joy_event ( jevent ) ;
}
// Called on the UI thread
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_joyhat ( JNIEnv * env , jclass clazz , jint p_device , jint p_hat_x , jint p_hat_y ) {
2022-03-15 13:29:32 +01:00
if ( step . get ( ) < = 0 )
return ;
AndroidInputHandler : : JoypadEvent jevent ;
jevent . device = p_device ;
jevent . type = AndroidInputHandler : : JOY_EVENT_HAT ;
int hat = 0 ;
if ( p_hat_x ! = 0 ) {
if ( p_hat_x < 0 )
hat | = InputDefault : : HAT_MASK_LEFT ;
else
hat | = InputDefault : : HAT_MASK_RIGHT ;
}
if ( p_hat_y ! = 0 ) {
if ( p_hat_y < 0 )
hat | = InputDefault : : HAT_MASK_UP ;
else
hat | = InputDefault : : HAT_MASK_DOWN ;
}
jevent . hat = hat ;
input_handler - > process_joy_event ( jevent ) ;
}
// Called on the UI thread
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_joyconnectionchanged ( JNIEnv * env , jclass clazz , jint p_device , jboolean p_connected , jstring p_name ) {
2022-03-15 13:29:32 +01:00
if ( step . get ( ) < = 0 )
return ;
String name = jstring_to_string ( p_name , env ) ;
input_handler - > joy_connection_changed ( p_device , p_connected , name ) ;
}
// Called on the UI thread
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_key ( JNIEnv * env , jclass clazz , jint p_keycode , jint p_scancode , jint p_unicode_char , jboolean p_pressed ) {
2022-03-15 13:29:32 +01:00
if ( step . get ( ) < = 0 )
return ;
input_handler - > process_key_event ( p_keycode , p_scancode , p_unicode_char , p_pressed ) ;
}
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_accelerometer ( JNIEnv * env , jclass clazz , jfloat x , jfloat y , jfloat z ) {
2022-03-15 13:29:32 +01:00
accelerometer = Vector3 ( x , y , z ) ;
}
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_gravity ( JNIEnv * env , jclass clazz , jfloat x , jfloat y , jfloat z ) {
2022-03-15 13:29:32 +01:00
gravity = Vector3 ( x , y , z ) ;
}
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_magnetometer ( JNIEnv * env , jclass clazz , jfloat x , jfloat y , jfloat z ) {
2022-03-15 13:29:32 +01:00
magnetometer = Vector3 ( x , y , z ) ;
}
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_gyroscope ( JNIEnv * env , jclass clazz , jfloat x , jfloat y , jfloat z ) {
2022-03-15 13:29:32 +01:00
gyroscope = Vector3 ( x , y , z ) ;
}
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_focusin ( JNIEnv * env , jclass clazz ) {
2022-03-15 13:29:32 +01:00
if ( step . get ( ) < = 0 )
return ;
os_android - > main_loop_focusin ( ) ;
}
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_focusout ( JNIEnv * env , jclass clazz ) {
2022-03-15 13:29:32 +01:00
if ( step . get ( ) < = 0 )
return ;
os_android - > main_loop_focusout ( ) ;
}
2022-03-23 20:46:05 +01:00
JNIEXPORT jstring JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_getGlobal ( JNIEnv * env , jclass clazz , jstring path ) {
2022-03-15 13:29:32 +01:00
String js = jstring_to_string ( path , env ) ;
return env - > NewStringUTF ( ProjectSettings : : get_singleton ( ) - > get ( js ) . operator String ( ) . utf8 ( ) . get_data ( ) ) ;
}
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_callobject ( JNIEnv * env , jclass clazz , jlong ID , jstring method , jobjectArray params ) {
2022-03-15 13:29:32 +01:00
Object * obj = ObjectDB : : get_instance ( ID ) ;
ERR_FAIL_COND ( ! obj ) ;
int res = env - > PushLocalFrame ( 16 ) ;
ERR_FAIL_COND ( res ! = 0 ) ;
String str_method = jstring_to_string ( method , env ) ;
int count = env - > GetArrayLength ( params ) ;
Variant * vlist = ( Variant * ) alloca ( sizeof ( Variant ) * count ) ;
Variant * * vptr = ( Variant * * ) alloca ( sizeof ( Variant * ) * count ) ;
for ( int i = 0 ; i < count ; i + + ) {
jobject obj = env - > GetObjectArrayElement ( params , i ) ;
Variant v ;
if ( obj )
v = _jobject_to_variant ( env , obj ) ;
memnew_placement ( & vlist [ i ] , Variant ) ;
vlist [ i ] = v ;
vptr [ i ] = & vlist [ i ] ;
env - > DeleteLocalRef ( obj ) ;
} ;
Variant : : CallError err ;
obj - > call ( str_method , ( const Variant * * ) vptr , count , err ) ;
// something
env - > PopLocalFrame ( NULL ) ;
}
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_calldeferred ( JNIEnv * env , jclass clazz , jlong ID , jstring method , jobjectArray params ) {
2022-03-15 13:29:32 +01:00
Object * obj = ObjectDB : : get_instance ( ID ) ;
ERR_FAIL_COND ( ! obj ) ;
int res = env - > PushLocalFrame ( 16 ) ;
ERR_FAIL_COND ( res ! = 0 ) ;
String str_method = jstring_to_string ( method , env ) ;
int count = env - > GetArrayLength ( params ) ;
Variant args [ VARIANT_ARG_MAX ] ;
for ( int i = 0 ; i < MIN ( count , VARIANT_ARG_MAX ) ; i + + ) {
jobject obj = env - > GetObjectArrayElement ( params , i ) ;
if ( obj )
args [ i ] = _jobject_to_variant ( env , obj ) ;
env - > DeleteLocalRef ( obj ) ;
} ;
static_assert ( VARIANT_ARG_MAX = = 8 , " This code needs to be updated if VARIANT_ARG_MAX != 8 " ) ;
obj - > call_deferred ( str_method , args [ 0 ] , args [ 1 ] , args [ 2 ] , args [ 3 ] , args [ 4 ] , args [ 5 ] , args [ 6 ] , args [ 7 ] ) ;
// something
env - > PopLocalFrame ( NULL ) ;
}
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_requestPermissionResult ( JNIEnv * env , jclass clazz , jstring p_permission , jboolean p_result ) {
2022-03-15 13:29:32 +01:00
String permission = jstring_to_string ( p_permission , env ) ;
if ( permission = = " android.permission.RECORD_AUDIO " & & p_result ) {
AudioDriver : : get_singleton ( ) - > capture_start ( ) ;
}
if ( os_android - > get_main_loop ( ) ) {
os_android - > get_main_loop ( ) - > emit_signal ( " on_request_permissions_result " , permission , p_result = = JNI_TRUE ) ;
}
}
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_onRendererResumed ( JNIEnv * env , jclass clazz ) {
2022-03-15 13:29:32 +01:00
if ( step . get ( ) < = 0 )
return ;
if ( os_android - > get_main_loop ( ) ) {
os_android - > get_main_loop ( ) - > notification ( MainLoop : : NOTIFICATION_APP_RESUMED ) ;
}
}
2022-03-23 20:46:05 +01:00
JNIEXPORT void JNICALL Java_net_relintai_pandemonium_pandemonium_PandemoniumLib_onRendererPaused ( JNIEnv * env , jclass clazz ) {
2022-03-15 13:29:32 +01:00
if ( step . get ( ) < = 0 )
return ;
if ( os_android - > get_main_loop ( ) ) {
os_android - > get_main_loop ( ) - > notification ( MainLoop : : NOTIFICATION_APP_PAUSED ) ;
}
}
}