1 /*
2     SDL_android_main.c, placed in the public domain by Sam Lantinga  3/13/14
3 */
4 
5 // Modified by Lasse Oorni and Yao Wei Tjong for Urho3D
6 
7 // Urho3D - use SDK include dir
8 #include <SDL/SDL_internal.h>
9 
10 #ifdef __ANDROID__
11 
12 /* Include the SDL main definition header */ // Urho3D - use SDK include dir
13 #include <SDL/SDL_main.h>
14 
15 /*******************************************************************************
16                  Functions called by JNI
17 *******************************************************************************/
18 #include <jni.h>
19 
20 // Urho3D: added extra filesDir parameter
21 /* Called before SDL_main() to initialize JNI bindings in SDL library */
22 extern void SDL_Android_Init(JNIEnv* env, jclass cls, jstring filesDir);
23 
24 /* This prototype is needed to prevent a warning about the missing prototype for global function below */
25 JNIEXPORT int JNICALL Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject array, jstring filesDir);
26 
27 /* Start up the SDL app */
Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv * env,jclass cls,jobject array,jstring filesDir)28 JNIEXPORT int JNICALL Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject array, jstring filesDir)
29 {
30     int i;
31     int argc;
32     int status;
33     int len;
34     char** argv;
35 
36     /* This interface could expand with ABI negotiation, callbacks, etc. */
37     SDL_Android_Init(env, cls, filesDir);
38 
39     SDL_SetMainReady();
40 
41     /* Prepare the arguments. */
42 
43     len = (*env)->GetArrayLength(env, array);
44     argv = SDL_stack_alloc(char*, len + 1);
45     argc = 0;
46     // Urho3D: avoid hard-coding the "app_process" as the first argument
47     for (i = 0; i < len; ++i) {
48         const char* utf;
49         char* arg = NULL;
50         jstring string = (*env)->GetObjectArrayElement(env, array, i);
51         if (string) {
52             utf = (*env)->GetStringUTFChars(env, string, 0);
53             if (utf) {
54                 arg = SDL_strdup(utf);
55                 (*env)->ReleaseStringUTFChars(env, string, utf);
56             }
57             (*env)->DeleteLocalRef(env, string);
58         }
59         if (!arg) {
60             arg = SDL_strdup("");
61         }
62         argv[argc++] = arg;
63     }
64     argv[argc] = NULL;
65 
66 
67     /* Run the application. */
68 
69     status = SDL_main(argc, argv);
70 
71     /* Release the arguments. */
72 
73     for (i = 0; i < argc; ++i) {
74         SDL_free(argv[i]);
75     }
76     SDL_stack_free(argv);
77     /* Do not issue an exit or the whole application will terminate instead of just the SDL thread */
78     /* exit(status); */
79 
80     return status;
81 }
82 
83 #endif /* __ANDROID__ */
84 
85 /* vi: set ts=4 sw=4 expandtab: */
86