SDL  2.0
SDL_android_main.c
Go to the documentation of this file.
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 /* This prototype is needed to prevent a warning about the missing prototype for global function below */
20 JNIEXPORT int JNICALL Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject array);
21 
22 /* Start up the SDL app */
23 JNIEXPORT int JNICALL Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject array)
24 {
25  int i;
26  int argc;
27  int status;
28  int len;
29  char** argv;
30 
31  /* This interface could expand with ABI negotiation, callbacks, etc. */
32  SDL_Android_Init(env, cls);
33 
35 
36  /* Prepare the arguments. */
37 
38  len = (*env)->GetArrayLength(env, array);
39  argv = SDL_stack_alloc(char*, 1 + len + 1);
40  argc = 0;
41  /* Use the name "app_process" so PHYSFS_platformCalcBaseDir() works.
42  https://bitbucket.org/MartinFelis/love-android-sdl2/issue/23/release-build-crash-on-start
43  */
44  argv[argc++] = SDL_strdup("app_process");
45  for (i = 0; i < len; ++i) {
46  const char* utf;
47  char* arg = NULL;
48  jstring string = (*env)->GetObjectArrayElement(env, array, i);
49  if (string) {
50  utf = (*env)->GetStringUTFChars(env, string, 0);
51  if (utf) {
52  arg = SDL_strdup(utf);
53  (*env)->ReleaseStringUTFChars(env, string, utf);
54  }
55  (*env)->DeleteLocalRef(env, string);
56  }
57  if (!arg) {
58  arg = SDL_strdup("");
59  }
60  argv[argc++] = arg;
61  }
62  argv[argc] = NULL;
63 
64 
65  /* Run the application. */
66 
67  status = SDL_main(argc, argv);
68 
69  /* Release the arguments. */
70 
71  for (i = 0; i < argc; ++i) {
72  SDL_free(argv[i]);
73  }
74  SDL_stack_free(argv);
75  /* Do not issue an exit or the whole application will terminate instead of just the SDL thread */
76  /* exit(status); */
77 
78  return status;
79 }
80 
81 #endif /* __ANDROID__ */
82 
83 /* vi: set ts=4 sw=4 expandtab: */
#define SDL_SetMainReady
GLenum GLsizei len
#define SDL_stack_alloc(type, count)
Definition: SDL_stdinc.h:328
void SDL_free(void *mem)
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:143
C_LINKAGE int SDL_main(int argc, char *argv[])
#define SDL_strdup
GLenum array
#define SDL_stack_free(data)
Definition: SDL_stdinc.h:329