Commit dbb0a2aa74d2dadd19256e67cc3cb98fd428099b

Sam Lantinga 2017-08-28T14:34:15

Removed the need for libSDL2main.a on Android, and separated JNI initialization out for other integrations

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
diff --git a/android-project/src/org/libsdl/app/SDLActivity.java b/android-project/src/org/libsdl/app/SDLActivity.java
index b1bbf1e..79a371d 100644
--- a/android-project/src/org/libsdl/app/SDLActivity.java
+++ b/android-project/src/org/libsdl/app/SDLActivity.java
@@ -136,10 +136,11 @@ public class SDLActivity extends Activity {
     protected void onCreate(Bundle savedInstanceState) {
         Log.v(TAG, "Device: " + android.os.Build.DEVICE);
         Log.v(TAG, "Model: " + android.os.Build.MODEL);
-        Log.v(TAG, "onCreate(): " + mSingleton);
+        Log.v(TAG, "onCreate()");
         super.onCreate(savedInstanceState);
 
         SDLActivity.initialize();
+
         // So we can call stuff from static callbacks
         mSingleton = this;
 
@@ -179,18 +180,16 @@ public class SDLActivity extends Activity {
            return;
         }
 
-        // Set up the surface
-        mSurface = new SDLSurface(getApplication());
-
+        // Set up JNI
+        SDLActivity.nativeSetupJNI();
 
-        if(Build.VERSION.SDK_INT >= 16) {
+        if (Build.VERSION.SDK_INT >= 16) {
             mJoystickHandler = new SDLJoystickHandler_API16();
-        } else if(Build.VERSION.SDK_INT >= 12) {
+        } else if (Build.VERSION.SDK_INT >= 12) {
             mJoystickHandler = new SDLJoystickHandler_API12();
         } else {
             mJoystickHandler = new SDLJoystickHandler();
         }
-
         mHapticHandler = new SDLHapticHandler();
 
         if (Build.VERSION.SDK_INT >= 11) {
@@ -200,6 +199,9 @@ public class SDLActivity extends Activity {
             mClipboardHandler = new SDLClipboardHandler_Old();
         }
 
+        // Set up the surface
+        mSurface = new SDLSurface(getApplication());
+
         mLayout = new RelativeLayout(this);
         mLayout.addView(mSurface);
 
@@ -207,7 +209,6 @@ public class SDLActivity extends Activity {
         
         // Get filename from "Open with" of another application
         Intent intent = getIntent();
-
         if (intent != null && intent.getData() != null) {
             String filename = intent.getData().getPath();
             if (filename != null) {
@@ -309,6 +310,7 @@ public class SDLActivity extends Activity {
         }
 
         super.onDestroy();
+
         // Reset everything in case the user re opens the app
         SDLActivity.initialize();
     }
@@ -358,43 +360,42 @@ public class SDLActivity extends Activity {
 
         // Try a transition to resumed state
         if (mNextNativeState == NativeState.RESUMED) {
+            if (mIsSurfaceReady && mHasFocus && mIsResumedCalled) {
+                if (mSDLThread == null) {
+                    // This is the entry point to the C app.
+                    // Start up the C app thread and enable sensor input for the first time
+                    // FIXME: Why aren't we enabling sensor input at start?
+
+                    final Thread sdlThread = new Thread(new SDLMain(), "SDLThread");
+                    mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, true);
+                    sdlThread.start();
+
+                    // Set up a listener thread to catch when the native thread ends
+                    mSDLThread = new Thread(new Runnable() {
+                        @Override
+                        public void run() {
+                            try {
+                                sdlThread.join();
+                            } catch (Exception e) {
+                                // Ignore any exception
+                            } finally {
+                                // Native thread has finished
+                                if (!mExitCalledFromJava) {
+                                    handleNativeExit();
+                                }
+                            }
+                        }
+                    }, "SDLThreadListener");
 
-           if (mIsSurfaceReady && mHasFocus && mIsResumedCalled) {
-
-              if (mSDLThread == null) {
-                  // This is the entry point to the C app.
-                  // Start up the C app thread and enable sensor input for the first time
-
-                  final Thread sdlThread = new Thread(new SDLMain(), "SDLThread");
-                  mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, true);
-                  sdlThread.start();
-
-                  // Set up a listener thread to catch when the native thread ends
-                  mSDLThread = new Thread(new Runnable(){
-                      @Override
-                      public void run(){
-                          try {
-                              sdlThread.join();
-                          }
-                          catch(Exception e){}
-                          finally{
-                              // Native thread has finished
-                              if (! mExitCalledFromJava) {
-                                  handleNativeExit();
-                              }
-                          }
-                      }
-                  }, "SDLThreadListener");
-                  mSDLThread.start();
-              }
-
+                    mSDLThread.start();
+                }
 
-              nativeResume();
-              mSurface.handleResume();
-              mCurrentNativeState = mNextNativeState;
-          }
-          return;
-       }
+                nativeResume();
+                mSurface.handleResume();
+                mCurrentNativeState = mNextNativeState;
+            }
+            return;
+        }
     }
 
     /* The native thread has finished */
@@ -460,12 +461,14 @@ public class SDLActivity extends Activity {
                 break;
             case COMMAND_SET_KEEP_SCREEN_ON:
             {
-                Window window = ((Activity) context).getWindow();
-                if (window != null) {
-                    if ((msg.obj instanceof Integer) && (((Integer) msg.obj).intValue() != 0)) {
-                        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
-                    } else {
-                        window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
+                if (context instanceof Activity) {
+                    Window window = ((Activity) context).getWindow();
+                    if (window != null) {
+                        if ((msg.obj instanceof Integer) && (((Integer) msg.obj).intValue() != 0)) {
+                            window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
+                        } else {
+                            window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
+                        }
                     }
                 }
                 break;
@@ -490,7 +493,8 @@ public class SDLActivity extends Activity {
     }
 
     // C functions we call
-    public static native int nativeInit(Object arguments);
+    public static native int nativeSetupJNI();
+    public static native int nativeRunMain(String library, String function, Object arguments);
     public static native void nativeLowMemory();
     public static native void nativeQuit();
     public static native void nativePause();
@@ -1208,15 +1212,25 @@ public class SDLActivity extends Activity {
 }
 
 /**
-    Simple nativeInit() runnable
+    Simple runnable to start the SDL application
 */
 class SDLMain implements Runnable {
     @Override
     public void run() {
         // Runs SDL_main()
-        SDLActivity.nativeInit(SDLActivity.mSingleton.getArguments());
+        String library;
+        String[] libraries = SDLActivity.mSingleton.getLibraries();
+        if (libraries.length > 0) {
+            library = "lib" + libraries[libraries.length - 1] + ".so";
+        } else {
+            library = "libmain.so";
+        }
+        String function = "SDL_main";
+
+        Log.v("SDL", "Running main function " + function + " from library " + library);
+        SDLActivity.nativeRunMain(library, function, SDLActivity.mSingleton.getArguments());
 
-        //Log.v("SDL", "SDL thread terminated");
+        Log.v("SDL", "Finished main function");
     }
 }
 
@@ -1251,7 +1265,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
         mDisplay = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
         mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
 
-        if(Build.VERSION.SDK_INT >= 12) {
+        if (Build.VERSION.SDK_INT >= 12) {
             setOnGenericMotionListener(new SDLGenericMotionListener_API12());
         }
 
@@ -1964,8 +1978,8 @@ class SDLHapticHandler {
             SDLHaptic haptic = getHaptic(deviceIds[i]);
             if (haptic == null) {
                 InputDevice device = InputDevice.getDevice(deviceIds[i]);
-                Vibrator vib = device.getVibrator ();
-                if(vib.hasVibrator ()) {
+                Vibrator vib = device.getVibrator();
+                if (vib.hasVibrator()) {
                     haptic = new SDLHaptic();
                     haptic.device_id = deviceIds[i];
                     haptic.name = device.getName();
diff --git a/configure b/configure
index 8011d50..f15e262 100755
--- a/configure
+++ b/configure
@@ -1616,7 +1616,7 @@ Optional Features:
                           include OpenGL ES 1.1 support [[default=yes]]
   --enable-video-opengles2
                           include OpenGL ES 2.0 support [[default=yes]]
-  --enable-video-vulkan   include Vulkan surface support [[default=yes]]
+  --enable-video-vulkan   include Vulkan support [[default=yes]]
   --enable-libudev        enable libudev support [[default=yes]]
   --enable-dbus           enable D-Bus support [[default=yes]]
   --enable-ime            enable IME support [[default=yes]]
@@ -23641,7 +23641,6 @@ case "$host" in
                 ANDROID_CFLAGS="-DGL_GLEXT_PROTOTYPES"
                 CFLAGS="$CFLAGS $ANDROID_CFLAGS"
                 SDL_CFLAGS="$SDL_CFLAGS $ANDROID_CFLAGS"
-                SDL_LIBS="$SDL_LIBS -lSDL2main -Wl,-u,SDL_main_dummy"
                 EXTRA_CFLAGS="$EXTRA_CFLAGS $ANDROID_CFLAGS"
                 EXTRA_LDFLAGS="$EXTRA_LDFLAGS -ldl -lGLESv1_CM -lGLESv2 -llog -landroid"
                 SDLMAIN_SOURCES="$srcdir/src/main/android/*.c"
diff --git a/configure.in b/configure.in
index ca3307e..6e0c354 100644
--- a/configure.in
+++ b/configure.in
@@ -3201,7 +3201,6 @@ case "$host" in
                 ANDROID_CFLAGS="-DGL_GLEXT_PROTOTYPES"
                 CFLAGS="$CFLAGS $ANDROID_CFLAGS"
                 SDL_CFLAGS="$SDL_CFLAGS $ANDROID_CFLAGS"
-                SDL_LIBS="$SDL_LIBS -lSDL2main -Wl,-u,SDL_main_dummy"
                 EXTRA_CFLAGS="$EXTRA_CFLAGS $ANDROID_CFLAGS"
                 EXTRA_LDFLAGS="$EXTRA_LDFLAGS -ldl -lGLESv1_CM -lGLESv2 -llog -landroid"
                 SDLMAIN_SOURCES="$srcdir/src/main/android/*.c"
diff --git a/include/SDL_main.h b/include/SDL_main.h
index 24eef5e..2af3236 100644
--- a/include/SDL_main.h
+++ b/include/SDL_main.h
@@ -107,7 +107,7 @@
 /**
  *  The prototype for the application's main() function
  */
-extern C_LINKAGE int SDL_main(int argc, char *argv[]);
+extern C_LINKAGE DECLSPEC int SDL_main(int argc, char *argv[]);
 
 
 #include "begin_code.h"
diff --git a/src/core/android/SDL_android.c b/src/core/android/SDL_android.c
index 926b684..fabd8bf 100644
--- a/src/core/android/SDL_android.c
+++ b/src/core/android/SDL_android.c
@@ -43,6 +43,7 @@
 #include <pthread.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <dlfcn.h>
 /* #define LOG_TAG "SDL_android" */
 /* #define LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) */
 /* #define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) */
@@ -58,6 +59,13 @@
 
 
 /* Java class SDLActivity */
+JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(
+        JNIEnv* mEnv, jclass cls);
+
+JNIEXPORT int JNICALL SDL_JAVA_INTERFACE(nativeRunMain)(
+        JNIEnv* env, jclass cls,
+        jstring library, jstring function, jobject array);
+
 JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeDropFile)(
         JNIEnv* env, jclass jcls,
         jstring filename);
@@ -237,9 +245,9 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
 }
 
 /* Called before SDL_main() to initialize JNI bindings */
-JNIEXPORT void JNICALL SDL_Android_Init(JNIEnv* mEnv, jclass cls)
+JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv* mEnv, jclass cls)
 {
-    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
+    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativeSetupJNI()");
 
     Android_JNI_SetupThread();
 
@@ -299,16 +307,95 @@ JNIEXPORT void JNICALL SDL_Android_Init(JNIEnv* mEnv, jclass cls)
        !midSetActivityTitle || !midSetOrientation || !midGetContext || !midInputGetInputDeviceIds ||
        !midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown || 
        !midClipboardSetText || !midClipboardGetText || !midClipboardHasText) {
-        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
+        __android_log_print(ANDROID_LOG_WARN, "SDL", "Missing some Java callbacks, do you have the latest version of SDLActivity.java?");
     }
 
     fidSeparateMouseAndTouch = (*mEnv)->GetStaticFieldID(mEnv, mActivityClass, "mSeparateMouseAndTouch", "Z");
 
     if (!fidSeparateMouseAndTouch) {
-        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java static fields, check that they're named and typed correctly");
+        __android_log_print(ANDROID_LOG_WARN, "SDL", "Missing some Java static fields, do you have the latest version of SDLActivity.java?");
     }
 
-    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init() finished!");
+    SDL_SetMainReady();
+}
+
+/* SDL main function prototype */
+typedef int (*SDL_main_func)(int argc, char *argv[]);
+
+/* 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_handle = dlopen(library_file, RTLD_GLOBAL);
+    if (library_handle) {
+        const char *function_name;
+        SDL_main_func SDL_main;
+
+        function_name = (*env)->GetStringUTFChars(env, function, NULL);
+        SDL_main = (SDL_main_func)dlsym(library_handle, function_name);
+        if (SDL_main) {
+            int i;
+            int argc;
+            int len;
+            char **argv;
+
+            /* Prepare the arguments. */
+            len = (*env)->GetArrayLength(env, array);
+            argv = SDL_stack_alloc(char*, 1 + len + 1);
+            argc = 0;
+            /* Use the name "app_process" so PHYSFS_platformCalcBaseDir() works.
+               https://bitbucket.org/MartinFelis/love-android-sdl2/issue/23/release-build-crash-on-start
+             */
+            argv[argc++] = SDL_strdup("app_process");
+            for (i = 0; i < len; ++i) {
+                const char* utf;
+                char* arg = NULL;
+                jstring string = (*env)->GetObjectArrayElement(env, array, i);
+                if (string) {
+                    utf = (*env)->GetStringUTFChars(env, string, 0);
+                    if (utf) {
+                        arg = SDL_strdup(utf);
+                        (*env)->ReleaseStringUTFChars(env, string, utf);
+                    }
+                    (*env)->DeleteLocalRef(env, string);
+                }
+                if (!arg) {
+                    arg = SDL_strdup("");
+                }
+                argv[argc++] = arg;
+            }
+            argv[argc] = NULL;
+
+
+            /* Run the application. */
+            status = SDL_main(argc, argv);
+
+            /* Release the arguments. */
+            for (i = 0; i < argc; ++i) {
+                SDL_free(argv[i]);
+            }
+            SDL_stack_free(argv);
+
+        } else {
+            __android_log_print(ANDROID_LOG_ERROR, "SDL", "nativeRunMain(): Couldn't find function %s in library %s", function_name, library_file);
+        }
+        (*env)->ReleaseStringUTFChars(env, function, function_name);
+
+        dlclose(library_handle);
+
+    } else {
+        __android_log_print(ANDROID_LOG_ERROR, "SDL", "nativeRunMain(): Couldn't load library %s", library_file);
+    }
+    (*env)->ReleaseStringUTFChars(env, library, library_file);
+
+    /* Do not issue an exit or the whole application will terminate instead of just the SDL thread */
+    /* exit(status); */
+
+    return status;
 }
 
 /* Drop file */
@@ -548,6 +635,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativePause)(
                                     JNIEnv* env, jclass cls)
 {
     __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativePause()");
+
     if (Android_Window) {
         SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
         SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
diff --git a/src/main/android/SDL_android_main.c b/src/main/android/SDL_android_main.c
index db6ae4f..054738a 100644
--- a/src/main/android/SDL_android_main.c
+++ b/src/main/android/SDL_android_main.c
@@ -1,88 +1,7 @@
 /*
     SDL_android_main.c, placed in the public domain by Sam Lantinga  3/13/14
-*/
-
-#include "SDL_config.h"
-#include "SDL.h"
-
-#ifdef __ANDROID__
-
-/* Include the SDL main definition header */
-#include "SDL_main.h"
-
-/*******************************************************************************
-                 Functions called by JNI
-*******************************************************************************/
-#include <jni.h>
-
-/* Dummy symbol so '-u SDL_main_dummy' forces ld to link in libSDL2main.a */
-JNIEXPORT int SDL_main_dummy;
-
-/* Called before SDL_main() to initialize JNI bindings in SDL library */
-extern void SDL_Android_Init(JNIEnv* env, jclass cls);
-
-/* This prototype is needed to prevent a warning about the missing prototype for global function below */
-JNIEXPORT int JNICALL Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject array);
-
-/* Start up the SDL app */
-JNIEXPORT int JNICALL Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject array)
-{
-    int i;
-    int argc;
-    int status;
-    int len;
-    char** argv;
-
-    /* This interface could expand with ABI negotiation, callbacks, etc. */
-    SDL_Android_Init(env, cls);
 
-    SDL_SetMainReady();
-
-    /* Prepare the arguments. */
-
-    len = (*env)->GetArrayLength(env, array);
-    argv = SDL_stack_alloc(char*, 1 + len + 1);
-    argc = 0;
-    /* Use the name "app_process" so PHYSFS_platformCalcBaseDir() works.
-       https://bitbucket.org/MartinFelis/love-android-sdl2/issue/23/release-build-crash-on-start
-     */
-    argv[argc++] = SDL_strdup("app_process");
-    for (i = 0; i < len; ++i) {
-        const char* utf;
-        char* arg = NULL;
-        jstring string = (*env)->GetObjectArrayElement(env, array, i);
-        if (string) {
-            utf = (*env)->GetStringUTFChars(env, string, 0);
-            if (utf) {
-                arg = SDL_strdup(utf);
-                (*env)->ReleaseStringUTFChars(env, string, utf);
-            }
-            (*env)->DeleteLocalRef(env, string);
-        }
-        if (!arg) {
-            arg = SDL_strdup("");
-        }
-        argv[argc++] = arg;
-    }
-    argv[argc] = NULL;
-
-
-    /* Run the application. */
-
-    status = SDL_main(argc, argv);
-
-    /* Release the arguments. */
-
-    for (i = 0; i < argc; ++i) {
-        SDL_free(argv[i]);
-    }
-    SDL_stack_free(argv);
-    /* Do not issue an exit or the whole application will terminate instead of just the SDL thread */
-    /* exit(status); */
-
-    return status;
-}
-
-#endif /* __ANDROID__ */
+    As of SDL 2.0.6 this file is no longer necessary.
+*/
 
 /* vi: set ts=4 sw=4 expandtab: */