#pragma once

#include "onair/okui/config.h"

#if SCRAPS_ANDROID

#include <SDL2/SDL_main.h>

#include <jni.h>

extern "C" {

/* Called before SDL_main() to initialize JNI bindings in SDL library */
extern void SDL_Android_Init(JNIEnv* env, jclass cls);

/* Start up the SDL app */
JNIEXPORT int JNICALL Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject array)
{
    int i;
    int argc;
    int status;

    /* This interface could expand with ABI negotiation, callbacks, etc. */
    SDL_Android_Init(env, cls);

    SDL_SetMainReady();

    /* Prepare the arguments. */

    int len = env->GetArrayLength((jobjectArray)array);
    char* argv[1 + len + 1];
    argc = 0;
    /* Use the name "app_process" so PHYSFS_platformCalcBaseDir() works.
       https://bitbucket.org/MartinFelis/love-android-sdl2/issue/23/release-build-crash-on-start
     */
    argv[argc++] = SDL_strdup("app_process");
    for (i = 0; i < len; ++i) {
        const char* utf;
        char* arg = NULL;
        jstring string = (jstring)env->GetObjectArrayElement((jobjectArray)array, i);
        if (string) {
            utf = env->GetStringUTFChars(string, 0);
            if (utf) {
                arg = SDL_strdup(utf);
                env->ReleaseStringUTFChars(string, utf);
            }
            env->DeleteLocalRef(string);
        }
        if (!arg) {
            arg = SDL_strdup("");
        }
        argv[argc++] = arg;
    }
    argv[argc] = NULL;


    /* Run the application. */

    status = SDL_main(argc, argv);

    /* Release the arguments. */

    for (i = 0; i < argc; ++i) {
        SDL_free(argv[i]);
    }

    /* Do not issue an exit or the whole application will terminate instead of just the SDL thread */
    /* exit(status); */

    return status;
}

}

#endif

Add android main for sdl
Add android main for sdl

Summary: Needed for the viewer, putting a helper here to be included by the viewer

Test Plan: Run viewer on android

Reviewers: mike.naquin

Reviewed By: mike.naquin

Subscribers: #engineering

Differential Revision: https://phabricator.btlive.tv/D1484

[This commit was only partially migrated from livelib commit 397ac913a235a97fb4bf7f61710a522a0094100e.]