src/main/android/SDL_android_main.c
author Ryan C. Gordon
Mon, 05 Jan 2015 01:41:42 -0500
changeset 9306 817656bd36ec
parent 9238 3e53bd4510a2
child 9828 4e0e4f544027
permissions -rw-r--r--
Clang static analysis builds should use C runtime directly.

This is a little macro magic to use malloc() directly instead of SDL_malloc(),
etc, so static analysis tests that know about the C runtime can function
properly, and understand that we are dealing with heap allocations, etc.

This changed our static analysis report from 5 outstanding bugs to 30.

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