src/main/android/SDL_android_main.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 17 Jul 2016 21:42:53 +0200
changeset 10198 136b3507f669
parent 10080 426de7645465
permissions -rw-r--r--
Android: Fixed compile warning about a missing prototype (thanks, Sylvain!). Fixes Bugzilla #3351.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8616
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 8093
diff changeset
     1
/*
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 8093
diff changeset
     2
    SDL_android_main.c, placed in the public domain by Sam Lantinga  3/13/14
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 8093
diff changeset
     3
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 7678
diff changeset
     4
#include "../../SDL_internal.h"
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 4999
diff changeset
     5
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 4999
diff changeset
     6
#ifdef __ANDROID__
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     7
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
/* Include the SDL main definition header */
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     9
#include "SDL_main.h"
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    10
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    11
/*******************************************************************************
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
                 Functions called by JNI
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
*******************************************************************************/
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
#include <jni.h>
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7501
diff changeset
    16
/* Called before SDL_main() to initialize JNI bindings in SDL library */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7467
diff changeset
    17
extern void SDL_Android_Init(JNIEnv* env, jclass cls);
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    18
10198
136b3507f669 Android: Fixed compile warning about a missing prototype (thanks, Sylvain!).
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 10080
diff changeset
    19
/* This prototype is needed to prevent a warning about the missing prototype for global function below */
136b3507f669 Android: Fixed compile warning about a missing prototype (thanks, Sylvain!).
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 10080
diff changeset
    20
JNIEXPORT int JNICALL Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject array);
136b3507f669 Android: Fixed compile warning about a missing prototype (thanks, Sylvain!).
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 10080
diff changeset
    21
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7501
diff changeset
    22
/* Start up the SDL app */
9828
4e0e4f544027 Fixed bug 3083 - Android JNIEXPORT and JNICALL missing
Sam Lantinga <slouken@libsdl.org>
parents: 9238
diff changeset
    23
JNIEXPORT int JNICALL Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject array)
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
{
9172
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    25
    int i;
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    26
    int argc;
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    27
    int status;
10080
426de7645465 Android: C89 cleanup to avoid warnings/errors since the default gcc mode on Android is still pre-C99.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 9828
diff changeset
    28
    int len;
426de7645465 Android: C89 cleanup to avoid warnings/errors since the default gcc mode on Android is still pre-C99.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 9828
diff changeset
    29
    char** argv;
9172
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    30
9171
3464a9bda1e2 Fixed typo in source comment.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9006
diff changeset
    31
    /* This interface could expand with ABI negotiation, callbacks, etc. */
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4964
diff changeset
    32
    SDL_Android_Init(env, cls);
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
7281
cd5516b9263d Added some extra protection to notify the developer if they haven't initialized the application properly.
Sam Lantinga <slouken@libsdl.org>
parents: 6354
diff changeset
    34
    SDL_SetMainReady();
cd5516b9263d Added some extra protection to notify the developer if they haven't initialized the application properly.
Sam Lantinga <slouken@libsdl.org>
parents: 6354
diff changeset
    35
9172
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    36
    /* Prepare the arguments. */
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    37
10080
426de7645465 Android: C89 cleanup to avoid warnings/errors since the default gcc mode on Android is still pre-C99.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 9828
diff changeset
    38
    len = (*env)->GetArrayLength(env, array);
426de7645465 Android: C89 cleanup to avoid warnings/errors since the default gcc mode on Android is still pre-C99.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 9828
diff changeset
    39
    argv = SDL_stack_alloc(char*, 1 + len + 1);
9172
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    40
    argc = 0;
9006
98c808d72e20 Fixed bug 2638 - (Signed) Can't create signed apps in Android
Sam Lantinga <slouken@libsdl.org>
parents: 8694
diff changeset
    41
    /* Use the name "app_process" so PHYSFS_platformCalcBaseDir() works.
98c808d72e20 Fixed bug 2638 - (Signed) Can't create signed apps in Android
Sam Lantinga <slouken@libsdl.org>
parents: 8694
diff changeset
    42
       https://bitbucket.org/MartinFelis/love-android-sdl2/issue/23/release-build-crash-on-start
98c808d72e20 Fixed bug 2638 - (Signed) Can't create signed apps in Android
Sam Lantinga <slouken@libsdl.org>
parents: 8694
diff changeset
    43
     */
9172
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    44
    argv[argc++] = SDL_strdup("app_process");
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    45
    for (i = 0; i < len; ++i) {
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    46
        const char* utf;
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    47
        char* arg = NULL;
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    48
        jstring string = (*env)->GetObjectArrayElement(env, array, i);
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    49
        if (string) {
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    50
            utf = (*env)->GetStringUTFChars(env, string, 0);
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    51
            if (utf) {
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    52
                arg = SDL_strdup(utf);
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    53
                (*env)->ReleaseStringUTFChars(env, string, utf);
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    54
            }
9238
3e53bd4510a2 Fixed limitation for number of custom main() arguments on Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9172
diff changeset
    55
            (*env)->DeleteLocalRef(env, string);
9172
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    56
        }
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    57
        if (!arg) {
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    58
            arg = SDL_strdup("");
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    59
        }
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    60
        argv[argc++] = arg;
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    61
    }
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    62
    argv[argc] = NULL;
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    63
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    64
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    65
    /* Run the application. */
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    66
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    67
    status = SDL_main(argc, argv);
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    68
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    69
    /* Release the arguments. */
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    70
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    71
    for (i = 0; i < argc; ++i) {
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    72
        SDL_free(argv[i]);
985481d7f3a1 Fixed bug 2476 - Allow custom main() arguments
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9171
diff changeset
    73
    }
10080
426de7645465 Android: C89 cleanup to avoid warnings/errors since the default gcc mode on Android is still pre-C99.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 9828
diff changeset
    74
    SDL_stack_free(argv);
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
    75
    /* Do not issue an exit or the whole application will terminate instead of just the SDL thread */
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
    76
    /* exit(status); */
8694
244c7784723b Fixed unused local variable warning.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8616
diff changeset
    77
244c7784723b Fixed unused local variable warning.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8616
diff changeset
    78
    return status;
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
}
4999
55fccf89b340 Make sure we shut down the app if SDL_main() returns instead of exiting.
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
    80
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 4999
diff changeset
    81
#endif /* __ANDROID__ */
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 4999
diff changeset
    82
4999
55fccf89b340 Make sure we shut down the app if SDL_main() returns instead of exiting.
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
    83
/* vi: set ts=4 sw=4 expandtab: */