|
25 | 25 | #include "SDL_android.h"
|
26 | 26 |
|
27 | 27 | #include "../../events/SDL_events_c.h"
|
| 28 | +#include "../../main/SDL_runapp.h" |
28 | 29 | #include "../../video/android/SDL_androidkeyboard.h"
|
29 | 30 | #include "../../video/android/SDL_androidmouse.h"
|
30 | 31 | #include "../../video/android/SDL_androidtouch.h"
|
@@ -817,41 +818,49 @@ JNIEXPORT int JNICALL SDL_JAVA_INTERFACE(nativeRunMain)(JNIEnv *env, jclass cls,
|
817 | 818 | char **argv;
|
818 | 819 | bool isstack;
|
819 | 820 |
|
| 821 | + // Always use the name "app_process" for argv[0] so PHYSFS_platformCalcBaseDir() works. |
| 822 | + // https://github.com/love2d/love-android/issues/24 |
| 823 | + |
820 | 824 | // Prepare the arguments.
|
821 | 825 | len = (*env)->GetArrayLength(env, array);
|
822 |
| - argv = SDL_small_alloc(char *, 1 + len + 1, &isstack); // !!! FIXME: check for NULL |
823 |
| - argc = 0; |
824 |
| - /* Use the name "app_process" so PHYSFS_platformCalcBaseDir() works. |
825 |
| - https://github.com/love2d/love-android/issues/24 |
826 |
| - */ |
827 |
| - argv[argc++] = SDL_strdup("app_process"); |
828 |
| - for (i = 0; i < len; ++i) { |
829 |
| - char *arg = NULL; |
830 |
| - jstring string = (*env)->GetObjectArrayElement(env, array, i); |
831 |
| - if (string) { |
832 |
| - const char *utf = (*env)->GetStringUTFChars(env, string, 0); |
833 |
| - if (utf) { |
834 |
| - arg = SDL_strdup(utf); |
835 |
| - (*env)->ReleaseStringUTFChars(env, string, utf); |
| 826 | + argv = SDL_small_alloc(char *, 1 + len + 1, &isstack); |
| 827 | + if (!argv) { |
| 828 | + // Failed to allocate the argv (out of memory?). Use a dummy argv instead. |
| 829 | + char dummyargv0[] = { 'a', 'p', 'p', '_', 'p', 'r', 'o', 'c', 'e', 's', 's', '\0' }; |
| 830 | + char *dummyargv[2] = { dummyargv0, NULL }; |
| 831 | + |
| 832 | + // Run the application (without arguments). |
| 833 | + status = SDL_main(1, dummyargv); |
| 834 | + } else { |
| 835 | + argc = 0; |
| 836 | + argv[argc++] = SDL_strdup("app_process"); |
| 837 | + for (i = 0; i < len; ++i) { |
| 838 | + char *arg = NULL; |
| 839 | + jstring string = (*env)->GetObjectArrayElement(env, array, i); |
| 840 | + if (string) { |
| 841 | + const char *utf = (*env)->GetStringUTFChars(env, string, 0); |
| 842 | + if (utf) { |
| 843 | + arg = SDL_strdup(utf); |
| 844 | + (*env)->ReleaseStringUTFChars(env, string, utf); |
| 845 | + } |
| 846 | + (*env)->DeleteLocalRef(env, string); |
836 | 847 | }
|
837 |
| - (*env)->DeleteLocalRef(env, string); |
838 |
| - } |
839 |
| - if (arg == NULL) { |
840 |
| - arg = SDL_strdup(""); |
| 848 | + if (arg == NULL) { |
| 849 | + arg = SDL_strdup(""); |
| 850 | + } |
| 851 | + argv[argc++] = arg; |
841 | 852 | }
|
842 |
| - argv[argc++] = arg; |
843 |
| - } |
844 |
| - argv[argc] = NULL; |
| 853 | + argv[argc] = NULL; |
845 | 854 |
|
846 |
| - // Run the application. |
847 |
| - status = SDL_main(argc, argv); |
| 855 | + // Run the application. |
| 856 | + status = SDL_main(argc, argv); |
848 | 857 |
|
849 |
| - // Release the arguments. |
850 |
| - for (i = 0; i < argc; ++i) { |
851 |
| - SDL_free(argv[i]); |
| 858 | + // Release the arguments. |
| 859 | + for (i = 0; i < argc; ++i) { |
| 860 | + SDL_free(argv[i]); |
| 861 | + } |
| 862 | + SDL_small_free(argv, isstack); |
852 | 863 | }
|
853 |
| - SDL_small_free(argv, isstack); |
854 |
| - |
855 | 864 | } else {
|
856 | 865 | __android_log_print(ANDROID_LOG_ERROR, "SDL", "nativeRunMain(): Couldn't find function %s in library %s", function_name, library_file);
|
857 | 866 | }
|
|
0 commit comments