4173bcb1d1734efaf130ca44ffb2cac104398533
[platform/upstream/SDL.git] / src / main / android / SDL_android_main.c
1 /*
2     SDL_android_main.c, placed in the public domain by Sam Lantinga  3/13/14
3 */
4 #include "../../SDL_internal.h"
5
6 #ifdef __ANDROID__
7
8 /* Include the SDL main definition header */
9 #include "SDL_main.h"
10
11 /*******************************************************************************
12                  Functions called by JNI
13 *******************************************************************************/
14 #include <jni.h>
15
16 /* Called before SDL_main() to initialize JNI bindings in SDL library */
17 extern void SDL_Android_Init(JNIEnv* env, jclass cls);
18
19 /* Start up the SDL app */
20 JNIEXPORT int JNICALL Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject array)
21 {
22     int i;
23     int argc;
24     int status;
25
26     /* This interface could expand with ABI negotiation, callbacks, etc. */
27     SDL_Android_Init(env, cls);
28
29     SDL_SetMainReady();
30
31     /* Prepare the arguments. */
32
33     int len = (*env)->GetArrayLength(env, array);
34     char* argv[1 + len + 1];
35     argc = 0;
36     /* Use the name "app_process" so PHYSFS_platformCalcBaseDir() works.
37        https://bitbucket.org/MartinFelis/love-android-sdl2/issue/23/release-build-crash-on-start
38      */
39     argv[argc++] = SDL_strdup("app_process");
40     for (i = 0; i < len; ++i) {
41         const char* utf;
42         char* arg = NULL;
43         jstring string = (*env)->GetObjectArrayElement(env, array, i);
44         if (string) {
45             utf = (*env)->GetStringUTFChars(env, string, 0);
46             if (utf) {
47                 arg = SDL_strdup(utf);
48                 (*env)->ReleaseStringUTFChars(env, string, utf);
49             }
50             (*env)->DeleteLocalRef(env, string);
51         }
52         if (!arg) {
53             arg = SDL_strdup("");
54         }
55         argv[argc++] = arg;
56     }
57     argv[argc] = NULL;
58
59
60     /* Run the application. */
61
62     status = SDL_main(argc, argv);
63
64     /* Release the arguments. */
65
66     for (i = 0; i < argc; ++i) {
67         SDL_free(argv[i]);
68     }
69
70     /* Do not issue an exit or the whole application will terminate instead of just the SDL thread */
71     /* exit(status); */
72
73     return status;
74 }
75
76 #endif /* __ANDROID__ */
77
78 /* vi: set ts=4 sw=4 expandtab: */