mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-05-28 23:49:12 +00:00
Remove createSDLMainRunnable()
in favour of main()
to fix multiple issues when providing custom main/runnable code (#10434)
This allows managed applications (eg. Java, C#) to override main() to their liking.
This commit is contained in:
parent
2f24e9c2f2
commit
e3cf20e1cc
2 changed files with 46 additions and 26 deletions
|
@ -247,12 +247,19 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method creates a Runnable which invokes SDL_main. The default implementation
|
* The application entry point, called on a dedicated thread (SDLThread).
|
||||||
* uses the getMainSharedObject() and getMainFunction() methods to invoke native
|
* The default implementation uses the getMainSharedObject() and getMainFunction() methods
|
||||||
* code from the specified shared library.
|
* to invoke native code from the specified shared library.
|
||||||
|
* It can be overridden by derived classes.
|
||||||
*/
|
*/
|
||||||
protected Runnable createSDLMainRunnable() {
|
protected void main() {
|
||||||
return new SDLMain();
|
String library = SDLActivity.mSingleton.getMainSharedObject();
|
||||||
|
String function = SDLActivity.mSingleton.getMainFunction();
|
||||||
|
String[] arguments = SDLActivity.mSingleton.getArguments();
|
||||||
|
|
||||||
|
Log.v("SDL", "Running main function " + function + " from library " + library);
|
||||||
|
SDLActivity.nativeRunMain(library, function, arguments);
|
||||||
|
Log.v("SDL", "Finished main function");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -838,7 +845,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
|
||||||
// Start up the C app thread and enable sensor input for the first time
|
// Start up the C app thread and enable sensor input for the first time
|
||||||
// FIXME: Why aren't we enabling sensor input at start?
|
// FIXME: Why aren't we enabling sensor input at start?
|
||||||
|
|
||||||
mSDLThread = new Thread(SDLActivity.mSingleton.createSDLMainRunnable(), "SDLThread");
|
mSDLThread = new Thread(new SDLMain(), "SDLThread");
|
||||||
mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, true);
|
mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, true);
|
||||||
mSDLThread.start();
|
mSDLThread.start();
|
||||||
|
|
||||||
|
@ -1032,6 +1039,8 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
|
||||||
// C functions we call
|
// C functions we call
|
||||||
public static native String nativeGetVersion();
|
public static native String nativeGetVersion();
|
||||||
public static native int nativeSetupJNI();
|
public static native int nativeSetupJNI();
|
||||||
|
public static native void nativeInitMainThread();
|
||||||
|
public static native void nativeCleanupMainThread();
|
||||||
public static native int nativeRunMain(String library, String function, Object arguments);
|
public static native int nativeRunMain(String library, String function, Object arguments);
|
||||||
public static native void nativeLowMemory();
|
public static native void nativeLowMemory();
|
||||||
public static native void nativeSendQuit();
|
public static native void nativeSendQuit();
|
||||||
|
@ -2102,10 +2111,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
|
||||||
class SDLMain implements Runnable {
|
class SDLMain implements Runnable {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
// Runs SDL_main()
|
// Runs SDLActivity.main()
|
||||||
String library = SDLActivity.mSingleton.getMainSharedObject();
|
|
||||||
String function = SDLActivity.mSingleton.getMainFunction();
|
|
||||||
String[] arguments = SDLActivity.mSingleton.getArguments();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_DISPLAY);
|
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_DISPLAY);
|
||||||
|
@ -2113,11 +2119,9 @@ class SDLMain implements Runnable {
|
||||||
Log.v("SDL", "modify thread properties failed " + e.toString());
|
Log.v("SDL", "modify thread properties failed " + e.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.v("SDL", "Running main function " + function + " from library " + library);
|
SDLActivity.nativeInitMainThread();
|
||||||
|
SDLActivity.mSingleton.main();
|
||||||
SDLActivity.nativeRunMain(library, function, arguments);
|
SDLActivity.nativeCleanupMainThread();
|
||||||
|
|
||||||
Log.v("SDL", "Finished main function");
|
|
||||||
|
|
||||||
if (SDLActivity.mSingleton != null && !SDLActivity.mSingleton.isFinishing()) {
|
if (SDLActivity.mSingleton != null && !SDLActivity.mSingleton.isFinishing()) {
|
||||||
// Let's finish the Activity
|
// Let's finish the Activity
|
||||||
|
|
|
@ -64,6 +64,12 @@ JNIEXPORT jstring JNICALL SDL_JAVA_INTERFACE(nativeGetVersion)(
|
||||||
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(
|
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(
|
||||||
JNIEnv *env, jclass cls);
|
JNIEnv *env, jclass cls);
|
||||||
|
|
||||||
|
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeInitSDLThread)(
|
||||||
|
JNIEnv *env, jclass cls);
|
||||||
|
|
||||||
|
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeCleanupSDLThread)(
|
||||||
|
JNIEnv *env, jclass cls);
|
||||||
|
|
||||||
JNIEXPORT int JNICALL SDL_JAVA_INTERFACE(nativeRunMain)(
|
JNIEXPORT int JNICALL SDL_JAVA_INTERFACE(nativeRunMain)(
|
||||||
JNIEnv *env, jclass cls,
|
JNIEnv *env, jclass cls,
|
||||||
jstring library, jstring function, jobject array);
|
jstring library, jstring function, jobject array);
|
||||||
|
@ -188,6 +194,8 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeFileDialog)(
|
||||||
static JNINativeMethod SDLActivity_tab[] = {
|
static JNINativeMethod SDLActivity_tab[] = {
|
||||||
{ "nativeGetVersion", "()Ljava/lang/String;", SDL_JAVA_INTERFACE(nativeGetVersion) },
|
{ "nativeGetVersion", "()Ljava/lang/String;", SDL_JAVA_INTERFACE(nativeGetVersion) },
|
||||||
{ "nativeSetupJNI", "()I", SDL_JAVA_INTERFACE(nativeSetupJNI) },
|
{ "nativeSetupJNI", "()I", SDL_JAVA_INTERFACE(nativeSetupJNI) },
|
||||||
|
{ "nativeInitSDLThread", "()V", SDL_JAVA_INTERFACE(nativeInitSDLThread) },
|
||||||
|
{ "nativeCleanupSDLThread", "()V", SDL_JAVA_INTERFACE(nativeCleanupSDLThread) },
|
||||||
{ "nativeRunMain", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)I", SDL_JAVA_INTERFACE(nativeRunMain) },
|
{ "nativeRunMain", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)I", SDL_JAVA_INTERFACE(nativeRunMain) },
|
||||||
{ "onNativeDropFile", "(Ljava/lang/String;)V", SDL_JAVA_INTERFACE(onNativeDropFile) },
|
{ "onNativeDropFile", "(Ljava/lang/String;)V", SDL_JAVA_INTERFACE(onNativeDropFile) },
|
||||||
{ "nativeSetScreenResolution", "(IIIIFF)V", SDL_JAVA_INTERFACE(nativeSetScreenResolution) },
|
{ "nativeSetScreenResolution", "(IIIIFF)V", SDL_JAVA_INTERFACE(nativeSetScreenResolution) },
|
||||||
|
@ -764,14 +772,10 @@ JNIEXPORT jboolean JNICALL SDL_JAVA_INTERFACE(nativeAllowRecreateActivity)(
|
||||||
return SDL_AtomicGet(&bAllowRecreateActivity);
|
return SDL_AtomicGet(&bAllowRecreateActivity);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Start up the SDL app */
|
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeInitSDLThread)(
|
||||||
JNIEXPORT int JNICALL SDL_JAVA_INTERFACE(nativeRunMain)(JNIEnv *env, jclass cls, jstring library, jstring function, jobject array)
|
JNIEnv *env, jclass jcls)
|
||||||
{
|
{
|
||||||
int status = -1;
|
__android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativeInitSDLThread() %d time", run_count);
|
||||||
const char *library_file;
|
|
||||||
void *library_handle;
|
|
||||||
|
|
||||||
__android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativeRunMain() %d time", run_count);
|
|
||||||
if (run_count == 1) {
|
if (run_count == 1) {
|
||||||
SDL_AddHintCallback(SDL_HINT_ANDROID_ALLOW_RECREATE_ACTIVITY, SDL_AllowRecreateActivityChanged, NULL);
|
SDL_AddHintCallback(SDL_HINT_ANDROID_ALLOW_RECREATE_ACTIVITY, SDL_AllowRecreateActivityChanged, NULL);
|
||||||
}
|
}
|
||||||
|
@ -779,6 +783,22 @@ JNIEXPORT int JNICALL SDL_JAVA_INTERFACE(nativeRunMain)(JNIEnv *env, jclass cls,
|
||||||
|
|
||||||
/* Save JNIEnv of SDLThread */
|
/* Save JNIEnv of SDLThread */
|
||||||
Android_JNI_SetEnv(env);
|
Android_JNI_SetEnv(env);
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeCleanupSDLThread)(
|
||||||
|
JNIEnv *env, jclass jcls)
|
||||||
|
{
|
||||||
|
/* This is a Java thread, it doesn't need to be Detached from the JVM.
|
||||||
|
* Set to mThreadKey value to NULL not to call pthread_create destructor 'Android_JNI_ThreadDestroyed' */
|
||||||
|
Android_JNI_SetEnv(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Start up the SDL app */
|
||||||
|
JNIEXPORT int JNICALL SDL_JAVA_INTERFACE(nativeRunMain)(JNIEnv *env, jclass cls, jstring library, jstring function, jobject array)
|
||||||
|
{
|
||||||
|
int status = -1;
|
||||||
|
const char *library_file;
|
||||||
|
void *library_handle;
|
||||||
|
|
||||||
library_file = (*env)->GetStringUTFChars(env, library, NULL);
|
library_file = (*env)->GetStringUTFChars(env, library, NULL);
|
||||||
library_handle = dlopen(library_file, RTLD_GLOBAL);
|
library_handle = dlopen(library_file, RTLD_GLOBAL);
|
||||||
|
@ -853,10 +873,6 @@ JNIEXPORT int JNICALL SDL_JAVA_INTERFACE(nativeRunMain)(JNIEnv *env, jclass cls,
|
||||||
}
|
}
|
||||||
(*env)->ReleaseStringUTFChars(env, library, library_file);
|
(*env)->ReleaseStringUTFChars(env, library, library_file);
|
||||||
|
|
||||||
/* This is a Java thread, it doesn't need to be Detached from the JVM.
|
|
||||||
* Set to mThreadKey value to NULL not to call pthread_create destructor 'Android_JNI_ThreadDestroyed' */
|
|
||||||
Android_JNI_SetEnv(NULL);
|
|
||||||
|
|
||||||
/* Do not issue an exit or the whole application will terminate instead of just the SDL thread */
|
/* Do not issue an exit or the whole application will terminate instead of just the SDL thread */
|
||||||
/* exit(status); */
|
/* exit(status); */
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue