Register native functions in SDL_main
This commit is contained in:
parent
4db68a3f10
commit
17329a1b29
@ -306,7 +306,7 @@ LOCAL_CFLAGS := -I../lib/angelscript/include \
|
||||
-DANDROID_PACKAGE_NAME=\"$(PACKAGE_NAME)\" \
|
||||
-DANDROID_APP_DIR_NAME=\"$(APP_DIR_NAME)\" \
|
||||
-DSUPERTUXKART_VERSION=\"$(PROJECT_VERSION)\" \
|
||||
-DANDROID_PACKAGE_CALLBACK_NAME=$(PACKAGE_CALLBACK_NAME)
|
||||
-DANDROID_PACKAGE_CLASS_NAME=\"$(PACKAGE_CLASS_NAME)\"
|
||||
LOCAL_CPPFLAGS := -std=gnu++0x
|
||||
|
||||
LOCAL_STATIC_LIBRARIES := irrlicht bullet enet ifaddrs angelscript mcpp SDL2 \
|
||||
|
@ -19,7 +19,7 @@ export COMPILE_SDK_VERSION=29
|
||||
|
||||
export APP_NAME_RELEASE="SuperTuxKart"
|
||||
export PACKAGE_NAME_RELEASE="org.supertuxkart.stk"
|
||||
export PACKAGE_CALLBACK_NAME_RELEASE="org_supertuxkart_stk"
|
||||
export PACKAGE_CLASS_NAME_RELEASE="org/supertuxkart/stk"
|
||||
export APP_DIR_NAME_RELEASE="supertuxkart"
|
||||
export APP_ICON_RELEASE="$DIRNAME/icon.png"
|
||||
export APP_ICON_ADAPTIVE_BG_RELEASE="$DIRNAME/icon_adaptive_bg.png"
|
||||
@ -27,7 +27,7 @@ export APP_ICON_ADAPTIVE_FG_RELEASE="$DIRNAME/icon_adaptive_fg.png"
|
||||
|
||||
export APP_NAME_BETA="SuperTuxKart Beta"
|
||||
export PACKAGE_NAME_BETA="org.supertuxkart.stk_beta"
|
||||
export PACKAGE_CALLBACK_NAME_BETA="org_supertuxkart_stk_1beta"
|
||||
export PACKAGE_CLASS_NAME_BETA="org/supertuxkart/stk_beta"
|
||||
export APP_DIR_NAME_BETA="supertuxkart-beta"
|
||||
export APP_ICON_BETA="$DIRNAME/icon-dbg.png"
|
||||
export APP_ICON_ADAPTIVE_BG_BETA="$DIRNAME/icon_adaptive_bg-dbg.png"
|
||||
@ -35,7 +35,7 @@ export APP_ICON_ADAPTIVE_FG_BETA="$DIRNAME/icon_adaptive_fg-dbg.png"
|
||||
|
||||
export APP_NAME_DEBUG="SuperTuxKart Debug"
|
||||
export PACKAGE_NAME_DEBUG="org.supertuxkart.stk_dbg"
|
||||
export PACKAGE_CALLBACK_NAME_DEBUG="org_supertuxkart_stk_1dbg"
|
||||
export PACKAGE_CLASS_NAME_DEBUG="org/supertuxkart/stk_dbg"
|
||||
export APP_DIR_NAME_DEBUG="supertuxkart-dbg"
|
||||
export APP_ICON_DEBUG="$DIRNAME/icon-dbg.png"
|
||||
export APP_ICON_ADAPTIVE_BG_DEBUG="$DIRNAME/icon_adaptive_bg-dbg.png"
|
||||
@ -89,7 +89,7 @@ if [ "$BUILD_TYPE" = "debug" ] || [ "$BUILD_TYPE" = "Debug" ]; then
|
||||
export IS_DEBUG_BUILD=1
|
||||
export APP_NAME="$APP_NAME_DEBUG"
|
||||
export PACKAGE_NAME="$PACKAGE_NAME_DEBUG"
|
||||
export PACKAGE_CALLBACK_NAME="$PACKAGE_CALLBACK_NAME_DEBUG"
|
||||
export PACKAGE_CLASS_NAME="$PACKAGE_CLASS_NAME_DEBUG"
|
||||
export APP_DIR_NAME="$APP_DIR_NAME_DEBUG"
|
||||
export APP_ICON="$APP_ICON_DEBUG"
|
||||
export APP_ICON_ADAPTIVE_BG="$APP_ICON_ADAPTIVE_BG_DEBUG"
|
||||
@ -99,7 +99,7 @@ elif [ "$BUILD_TYPE" = "release" ] || [ "$BUILD_TYPE" = "Release" ]; then
|
||||
export IS_DEBUG_BUILD=0
|
||||
export APP_NAME="$APP_NAME_RELEASE"
|
||||
export PACKAGE_NAME="$PACKAGE_NAME_RELEASE"
|
||||
export PACKAGE_CALLBACK_NAME="$PACKAGE_CALLBACK_NAME_RELEASE"
|
||||
export PACKAGE_CLASS_NAME="$PACKAGE_CLASS_NAME_RELEASE"
|
||||
export APP_DIR_NAME="$APP_DIR_NAME_RELEASE"
|
||||
export APP_ICON="$APP_ICON_RELEASE"
|
||||
export APP_ICON_ADAPTIVE_BG="$APP_ICON_ADAPTIVE_BG_RELEASE"
|
||||
@ -109,7 +109,7 @@ elif [ "$BUILD_TYPE" = "beta" ] || [ "$BUILD_TYPE" = "Beta" ]; then
|
||||
export IS_DEBUG_BUILD=0
|
||||
export APP_NAME="$APP_NAME_BETA"
|
||||
export PACKAGE_NAME="$PACKAGE_NAME_BETA"
|
||||
export PACKAGE_CALLBACK_NAME="$PACKAGE_CALLBACK_NAME_BETA"
|
||||
export PACKAGE_CLASS_NAME="$PACKAGE_CLASS_NAME_BETA"
|
||||
export APP_DIR_NAME="$APP_DIR_NAME_BETA"
|
||||
export APP_ICON="$APP_ICON_BETA"
|
||||
export APP_ICON_ADAPTIVE_BG="$APP_ICON_ADAPTIVE_BG_BETA"
|
||||
|
@ -51,6 +51,7 @@ import android.widget.TextView;
|
||||
import android.util.DisplayMetrics;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.Set;
|
||||
|
||||
import org.minidns.hla.DnssecResolverApi;
|
||||
@ -70,6 +71,8 @@ public class SuperTuxKartActivity extends SDLActivity
|
||||
private float m_bottom_padding;
|
||||
private float m_left_padding;
|
||||
private float m_right_padding;
|
||||
private AtomicInteger m_keyboard_height;
|
||||
private AtomicInteger m_moved_height;
|
||||
// ------------------------------------------------------------------------
|
||||
public native static void debugMsg(String msg);
|
||||
// ------------------------------------------------------------------------
|
||||
@ -186,6 +189,8 @@ public class SuperTuxKartActivity extends SDLActivity
|
||||
public void onCreate(Bundle instance)
|
||||
{
|
||||
super.onCreate(instance);
|
||||
m_keyboard_height = new AtomicInteger();
|
||||
m_moved_height = new AtomicInteger();
|
||||
m_progress_dialog = null;
|
||||
m_progress_bar = null;
|
||||
m_splash_screen = null;
|
||||
@ -204,12 +209,12 @@ public class SuperTuxKartActivity extends SDLActivity
|
||||
root.getWindowVisibleDisplayFrame(r);
|
||||
int screen_height = root.getRootView().getHeight();
|
||||
int keyboard_height = screen_height - (r.bottom);
|
||||
saveKeyboardHeight(keyboard_height);
|
||||
m_keyboard_height.set(keyboard_height);
|
||||
int moved_height = 0;
|
||||
int margin = screen_height - m_bottom_y;
|
||||
if (keyboard_height > margin)
|
||||
moved_height = -keyboard_height + margin;
|
||||
saveMovedHeight(-moved_height);
|
||||
m_moved_height.set(-moved_height);
|
||||
SDLActivity.moveView(moved_height);
|
||||
}
|
||||
});
|
||||
@ -266,6 +271,14 @@ public class SuperTuxKartActivity extends SDLActivity
|
||||
}
|
||||
// ------------------------------------------------------------------------
|
||||
@Override
|
||||
public void onStart()
|
||||
{
|
||||
super.onStart();
|
||||
m_keyboard_height.set(0);
|
||||
m_moved_height.set(0);
|
||||
}
|
||||
// ------------------------------------------------------------------------
|
||||
@Override
|
||||
public void onPause()
|
||||
{
|
||||
super.onPause();
|
||||
@ -503,5 +516,9 @@ public class SuperTuxKartActivity extends SDLActivity
|
||||
{
|
||||
handlePadding(isInMultiWindowMode);
|
||||
}
|
||||
// ------------------------------------------------------------------------
|
||||
public int getKeyboardHeight() { return m_keyboard_height.get(); }
|
||||
// ------------------------------------------------------------------------
|
||||
public int getMovedHeight() { return m_moved_height.get(); }
|
||||
|
||||
}
|
||||
|
@ -18,17 +18,69 @@ using namespace irr;
|
||||
|
||||
// Call when android keyboard is opened or close, and save its height for
|
||||
// moving screen
|
||||
std::atomic<int> g_keyboard_height(0);
|
||||
std::atomic<int> g_moved_height(0);
|
||||
std::atomic<int> g_disable_padding(0);
|
||||
extern "C" int Android_getKeyboardHeight()
|
||||
{
|
||||
return g_keyboard_height.load();
|
||||
JNIEnv* env = NULL;
|
||||
jobject activity = NULL;
|
||||
jclass class_native_activity = NULL;
|
||||
jmethodID method = NULL;
|
||||
jint keyboard_height = 0;
|
||||
|
||||
env = (JNIEnv*)SDL_AndroidGetJNIEnv();
|
||||
if (!env)
|
||||
goto exit;
|
||||
|
||||
activity = (jobject)SDL_AndroidGetActivity();
|
||||
if (!activity)
|
||||
goto exit;
|
||||
|
||||
class_native_activity = env->GetObjectClass(activity);
|
||||
if (class_native_activity == NULL)
|
||||
goto exit;
|
||||
|
||||
method = env->GetMethodID(class_native_activity, "getKeyboardHeight", "()I");
|
||||
if (method == NULL)
|
||||
goto exit;
|
||||
keyboard_height = env->CallIntMethod(activity, method);
|
||||
exit:
|
||||
if (!env)
|
||||
return 0;
|
||||
env->DeleteLocalRef(class_native_activity);
|
||||
env->DeleteLocalRef(activity);
|
||||
return keyboard_height;
|
||||
}
|
||||
|
||||
extern "C" int Android_getMovedHeight()
|
||||
{
|
||||
return g_moved_height.load();
|
||||
JNIEnv* env = NULL;
|
||||
jobject activity = NULL;
|
||||
jclass class_native_activity = NULL;
|
||||
jmethodID method = NULL;
|
||||
jint moved_height = 0;
|
||||
|
||||
env = (JNIEnv*)SDL_AndroidGetJNIEnv();
|
||||
if (!env)
|
||||
goto exit;
|
||||
|
||||
activity = (jobject)SDL_AndroidGetActivity();
|
||||
if (!activity)
|
||||
goto exit;
|
||||
|
||||
class_native_activity = env->GetObjectClass(activity);
|
||||
if (class_native_activity == NULL)
|
||||
goto exit;
|
||||
|
||||
method = env->GetMethodID(class_native_activity, "getMovedHeight", "()I");
|
||||
if (method == NULL)
|
||||
goto exit;
|
||||
moved_height = env->CallIntMethod(activity, method);
|
||||
exit:
|
||||
if (!env)
|
||||
return 0;
|
||||
env->DeleteLocalRef(class_native_activity);
|
||||
env->DeleteLocalRef(activity);
|
||||
return moved_height;
|
||||
}
|
||||
|
||||
extern "C" int Android_disablePadding()
|
||||
@ -36,11 +88,7 @@ extern "C" int Android_disablePadding()
|
||||
return g_disable_padding.load();
|
||||
}
|
||||
|
||||
#define MAKE_DEBUG_MSG_CALLBACK(x) JNIEXPORT void JNICALL Java_ ## x##_SuperTuxKartActivity_debugMsg(JNIEnv* env, jclass cls, jstring msg)
|
||||
#define ANDROID_DEBUG_MSG_CALLBACK(PKG_NAME) MAKE_DEBUG_MSG_CALLBACK(PKG_NAME)
|
||||
|
||||
extern "C"
|
||||
ANDROID_DEBUG_MSG_CALLBACK(ANDROID_PACKAGE_CALLBACK_NAME)
|
||||
extern "C" JNIEXPORT void JNICALL debugMsg(JNIEnv* env, jclass cls, jstring msg)
|
||||
{
|
||||
if (msg == NULL)
|
||||
return;
|
||||
@ -57,29 +105,7 @@ ANDROID_DEBUG_MSG_CALLBACK(ANDROID_PACKAGE_CALLBACK_NAME)
|
||||
MessageQueue::add(MessageQueue::MT_GENERIC, message);
|
||||
}
|
||||
|
||||
#define MAKE_ANDROID_SAVE_KBD_HEIGHT_CALLBACK(x) JNIEXPORT void JNICALL Java_ ## x##_SuperTuxKartActivity_saveKeyboardHeight(JNIEnv* env, jclass cls, jint height)
|
||||
#define ANDROID_SAVE_KBD_HEIGHT_CALLBACK(PKG_NAME) MAKE_ANDROID_SAVE_KBD_HEIGHT_CALLBACK(PKG_NAME)
|
||||
|
||||
extern "C"
|
||||
ANDROID_SAVE_KBD_HEIGHT_CALLBACK(ANDROID_PACKAGE_CALLBACK_NAME)
|
||||
{
|
||||
g_keyboard_height.store((int)height);
|
||||
}
|
||||
|
||||
#define MAKE_ANDROID_SAVE_MOVED_HEIGHT_CALLBACK(x) JNIEXPORT void JNICALL Java_ ## x##_SuperTuxKartActivity_saveMovedHeight(JNIEnv* env, jclass cls, jint height)
|
||||
#define ANDROID_SAVE_MOVED_HEIGHT_CALLBACK(PKG_NAME) MAKE_ANDROID_SAVE_MOVED_HEIGHT_CALLBACK(PKG_NAME)
|
||||
|
||||
extern "C"
|
||||
ANDROID_SAVE_MOVED_HEIGHT_CALLBACK(ANDROID_PACKAGE_CALLBACK_NAME)
|
||||
{
|
||||
g_moved_height.store((int)height);
|
||||
}
|
||||
|
||||
#define MAKE_ANDROID_HANDLE_PADDING_CALLBACK(x) JNIEXPORT void JNICALL Java_ ## x##_SuperTuxKartActivity_handlePadding(JNIEnv* env, jclass cls, jboolean val)
|
||||
#define ANDROID_HANDLE_PADDING_CALLBACK(PKG_NAME) MAKE_ANDROID_HANDLE_PADDING_CALLBACK(PKG_NAME)
|
||||
|
||||
extern "C"
|
||||
ANDROID_HANDLE_PADDING_CALLBACK(ANDROID_PACKAGE_CALLBACK_NAME)
|
||||
extern "C" JNIEXPORT void JNICALL handlePadding(JNIEnv* env, jclass cls, jboolean val)
|
||||
{
|
||||
g_disable_padding.store((int)val);
|
||||
}
|
||||
|
@ -262,15 +262,7 @@ EventPropagation TextBoxWidget::leftPressed (const int playerID)
|
||||
#ifdef ANDROID
|
||||
#include "jni.h"
|
||||
|
||||
#if !defined(ANDROID_PACKAGE_CALLBACK_NAME)
|
||||
#error
|
||||
#endif
|
||||
|
||||
#define MAKE_EDITTEXT_CALLBACK(x) JNIEXPORT void JNICALL Java_ ## x##_STKEditText_editText2STKEditbox(JNIEnv* env, jclass cls, jint widget_id, jstring text, jint start, jint end, jint composing_start, jint composing_end)
|
||||
#define ANDROID_EDITTEXT_CALLBACK(PKG_NAME) MAKE_EDITTEXT_CALLBACK(PKG_NAME)
|
||||
|
||||
extern "C"
|
||||
ANDROID_EDITTEXT_CALLBACK(ANDROID_PACKAGE_CALLBACK_NAME)
|
||||
extern "C" JNIEXPORT void JNICALL editText2STKEditbox(JNIEnv* env, jclass cls, jint widget_id, jstring text, jint start, jint end, jint composing_start, jint composing_end)
|
||||
{
|
||||
if (text == NULL)
|
||||
return;
|
||||
@ -342,11 +334,7 @@ ANDROID_EDITTEXT_CALLBACK(ANDROID_PACKAGE_CALLBACK_NAME)
|
||||
extern void Android_toggleOnScreenKeyboard(bool show, int type, int y);
|
||||
extern bool Android_isHardwareKeyboardConnected();
|
||||
|
||||
#define MAKE_HANDLE_ACTION_NEXT_CALLBACK(x) JNIEXPORT void JNICALL Java_ ## x##_STKEditText_handleActionNext(JNIEnv* env, jclass cls, jint widget_id)
|
||||
#define ANDROID_HANDLE_ACTION_NEXT_CALLBACK(PKG_NAME) MAKE_HANDLE_ACTION_NEXT_CALLBACK(PKG_NAME)
|
||||
|
||||
extern "C"
|
||||
ANDROID_HANDLE_ACTION_NEXT_CALLBACK(ANDROID_PACKAGE_CALLBACK_NAME)
|
||||
extern "C" JNIEXPORT void JNICALL handleActionNext(JNIEnv* env, jclass cls, jint widget_id)
|
||||
{
|
||||
GUIEngine::addGUIFunctionBeforeRendering([widget_id]()
|
||||
{
|
||||
@ -410,12 +398,7 @@ ANDROID_HANDLE_ACTION_NEXT_CALLBACK(ANDROID_PACKAGE_CALLBACK_NAME)
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
#define MAKE_HANDLE_LEFT_RIGHT_CALLBACK(x) JNIEXPORT void JNICALL Java_ ## x##_STKEditText_handleLeftRight(JNIEnv* env, jclass cls, jboolean left, jint widget_id)
|
||||
#define ANDROID_HANDLE_LEFT_RIGHT_CALLBACK(PKG_NAME) MAKE_HANDLE_LEFT_RIGHT_CALLBACK(PKG_NAME)
|
||||
|
||||
extern "C"
|
||||
ANDROID_HANDLE_LEFT_RIGHT_CALLBACK(ANDROID_PACKAGE_CALLBACK_NAME)
|
||||
extern "C" JNIEXPORT void JNICALL handleLeftRight(JNIEnv* env, jclass cls, jboolean left, jint widget_id)
|
||||
{
|
||||
GUIEngine::addGUIFunctionBeforeRendering([left, widget_id]()
|
||||
{
|
||||
|
@ -23,15 +23,76 @@
|
||||
#include "utils/string_utils.hpp"
|
||||
|
||||
#ifdef ANDROID
|
||||
#include "SDL_stdinc.h"
|
||||
#include "SDL_system.h"
|
||||
#include <jni.h>
|
||||
std::string g_android_main_user_agent;
|
||||
|
||||
extern int android_main(int argc, char *argv[]);
|
||||
|
||||
extern "C" JNIEXPORT void JNICALL debugMsg(JNIEnv* env, jclass cls, jstring msg);
|
||||
extern "C" JNIEXPORT void JNICALL handlePadding(JNIEnv* env, jclass cls, jboolean val);
|
||||
extern "C" JNIEXPORT void JNICALL addDNSSrvRecords(JNIEnv* env, jclass cls, jstring name, jint weight);
|
||||
|
||||
extern "C" JNIEXPORT void JNICALL editText2STKEditbox(JNIEnv* env, jclass cls, jint widget_id, jstring text, jint start, jint end, jint composing_start, jint composing_end);
|
||||
extern "C" JNIEXPORT void JNICALL handleActionNext(JNIEnv* env, jclass cls, jint widget_id);
|
||||
extern "C" JNIEXPORT void JNICALL handleLeftRight(JNIEnv* env, jclass cls, jboolean left, jint widget_id);
|
||||
|
||||
#if !defined(ANDROID_PACKAGE_CLASS_NAME)
|
||||
#error
|
||||
#endif
|
||||
|
||||
void registering_natives()
|
||||
{
|
||||
JNINativeMethod stkactivity_tab[] =
|
||||
{
|
||||
{ "debugMsg", "(Ljava/lang/String;)V", (void*)&debugMsg },
|
||||
{ "handlePadding", "(Z)V", (void*)&handlePadding },
|
||||
{ "addDNSSrvRecords", "(Ljava/lang/String;I)V", (void*)&addDNSSrvRecords }
|
||||
};
|
||||
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
|
||||
assert(env);
|
||||
const char* stkactivity_class = ANDROID_PACKAGE_CLASS_NAME "/SuperTuxKartActivity";
|
||||
jclass clazz = env->FindClass(stkactivity_class);
|
||||
if (clazz == NULL)
|
||||
{
|
||||
Log::error("MainAndroid", "Failed to find class %s.",
|
||||
stkactivity_class);
|
||||
return;
|
||||
}
|
||||
if (env->RegisterNatives(
|
||||
clazz, stkactivity_tab, (int)SDL_arraysize(stkactivity_tab)) < 0)
|
||||
{
|
||||
Log::error("MainAndroid", "Failed to register methods of %s.",
|
||||
stkactivity_class);
|
||||
}
|
||||
|
||||
JNINativeMethod stkeditbox_tab[] =
|
||||
{
|
||||
{ "editText2STKEditbox", "(ILjava/lang/String;IIII)V", (void*)&editText2STKEditbox },
|
||||
{ "handleActionNext", "(I)V", (void*)&handleActionNext },
|
||||
{ "handleLeftRight", "(ZI)V", (void*)&handleLeftRight }
|
||||
};
|
||||
const char* stkeditbox_class = ANDROID_PACKAGE_CLASS_NAME "/STKEditText";
|
||||
clazz = env->FindClass(stkeditbox_class);
|
||||
if (clazz == NULL)
|
||||
{
|
||||
Log::error("MainAndroid", "Failed to find class %s.",
|
||||
stkeditbox_class);
|
||||
return;
|
||||
}
|
||||
if (env->RegisterNatives(
|
||||
clazz, stkeditbox_tab, (int)SDL_arraysize(stkeditbox_tab)) < 0)
|
||||
{
|
||||
Log::error("MainAndroid", "Failed to register methods of %s.",
|
||||
stkeditbox_class);
|
||||
}
|
||||
}
|
||||
|
||||
void override_default_params_for_mobile();
|
||||
extern "C" int SDL_main(int argc, char *argv[])
|
||||
{
|
||||
registering_natives();
|
||||
override_default_params_for_mobile();
|
||||
int result = android_main(argc, argv);
|
||||
// TODO: Irrlicht device is properly waiting for destroy event, but
|
||||
|
@ -69,12 +69,7 @@
|
||||
#include "SDL_system.h"
|
||||
|
||||
std::vector<std::pair<std::string, int> >* g_list = NULL;
|
||||
|
||||
#define MAKE_ADD_DNS_SRV_RECORD_CALLBACK(x) JNIEXPORT void JNICALL Java_ ## x##_SuperTuxKartActivity_addDNSSrvRecords(JNIEnv* env, jclass cls, jstring name, jint weight)
|
||||
#define ANDROID_ADD_DNS_SRV_RECORD_CALLBACK(PKG_NAME) MAKE_ADD_DNS_SRV_RECORD_CALLBACK(PKG_NAME)
|
||||
|
||||
extern "C"
|
||||
ANDROID_ADD_DNS_SRV_RECORD_CALLBACK(ANDROID_PACKAGE_CALLBACK_NAME)
|
||||
extern "C" JNIEXPORT void JNICALL addDNSSrvRecords(JNIEnv* env, jclass cls, jstring name, jint weight)
|
||||
{
|
||||
if (!g_list || name == NULL)
|
||||
return;
|
||||
|
Loading…
x
Reference in New Issue
Block a user