Created
August 17, 2017 13:27
-
-
Save pyokagan/ea4a811d0c777e539cdece5bdf040e26 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Minimal Android/SDL-specific main program | |
* */ | |
#include "Python.h" | |
#include <locale.h> | |
#include "SDL.h" | |
#include <string.h> | |
int | |
main(int argc, char *argv[]) | |
{ | |
wchar_t **argv_copy; | |
/* We need a second copy, as Python might modify the first one. */ | |
wchar_t **argv_copy2; | |
int i, res; | |
char *oldloc; | |
argv_copy = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1)); | |
argv_copy2 = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1)); | |
if (!argv_copy || !argv_copy2) { | |
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "out of memory"); | |
return 1; | |
} | |
for (i = 0; i < argc; i++) { | |
argv_copy[i] = Py_DecodeLocale(argv[i], NULL); | |
if (!argv_copy[i]) { | |
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, | |
"unable to decode the command line argument #%i", i + 1); | |
return 1; | |
} | |
argv_copy2[i] = argv_copy[i]; | |
} | |
argv_copy2[argc] = argv_copy[argc] = NULL; | |
#ifdef __ANDROID__ | |
/* Get prefix (SDL external storage path) */ | |
const char *sdl_prefix = SDL_AndroidGetExternalStoragePath(); | |
/* Get exec prefix (SDL internal storage path) */ | |
const char *sdl_exec_prefix = SDL_AndroidGetInternalStoragePath(); | |
if (sdl_exec_prefix == NULL) { | |
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, | |
"SDL_AndroidGetInternalStoragePath failed"); | |
return 1; | |
} | |
const char *sdl_combined_prefix; | |
char *_blah = NULL; | |
if (sdl_prefix) { | |
_blah = (char*) SDL_calloc(SDL_strlen(sdl_prefix) + SDL_strlen(sdl_exec_prefix) + 2, sizeof(char)); | |
strcat(_blah, sdl_prefix); | |
strcat(_blah, ":"); | |
strcat(_blah, sdl_exec_prefix); | |
sdl_combined_prefix = _blah; | |
} else { | |
sdl_combined_prefix = sdl_exec_prefix; | |
} | |
SDL_Log("Combined prefix is %s", sdl_combined_prefix); | |
/* Decode path into wchar_t */ | |
wchar_t *py_home = Py_DecodeLocale(sdl_combined_prefix, NULL); | |
if (py_home == NULL) { | |
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, | |
"Could not decode SDL_AndroidGetInternalStoragePath"); | |
return 1; | |
} | |
/* Set python home */ | |
Py_SetPythonHome(py_home); | |
#endif | |
/* Run python */ | |
res = Py_Main(argc, argv_copy); | |
for (i = 0; i < argc; i++) { | |
PyMem_RawFree(argv_copy2[i]); | |
} | |
PyMem_RawFree(argv_copy); | |
PyMem_RawFree(argv_copy2); | |
#ifdef __ANDROID__ | |
PyMem_RawFree(py_home); | |
if (_blah) SDL_free(_blah); | |
#endif | |
if (res != 0) { | |
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "cpython returned %d!", res); | |
} | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment