2012-03-08 18:05:47 +01:00
|
|
|
//
|
|
|
|
// main.m
|
2015-02-14 13:05:54 +01:00
|
|
|
// {{ cookiecutter.project_name }}
|
2012-03-08 18:05:47 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#import <UIKit/UIKit.h>
|
2018-11-09 18:48:08 +01:00
|
|
|
{%- if cookiecutter.python_major == 2 %}
|
|
|
|
#include "{{ cookiecutter.kivy_dir }}/dist/root/python2/include/python2.7/Python.h"
|
|
|
|
{%- else %}
|
|
|
|
#include "{{ cookiecutter.kivy_dir }}/dist/root/python3/include/python3.7m/Python.h"
|
|
|
|
{%- endif %}
|
2016-11-20 09:50:19 +01:00
|
|
|
#include "{{ cookiecutter.kivy_dir }}/dist/include/common/sdl2/SDL_main.h"
|
2012-03-08 18:05:47 +01:00
|
|
|
#include <dlfcn.h>
|
|
|
|
|
2012-10-15 12:40:01 +02:00
|
|
|
void export_orientation();
|
2013-01-23 02:03:57 +01:00
|
|
|
void load_custom_builtin_importer();
|
2012-10-15 12:40:01 +02:00
|
|
|
|
2012-03-08 18:05:47 +01:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
int ret = 0;
|
2017-05-14 17:54:14 +02:00
|
|
|
|
2012-03-08 18:05:47 +01:00
|
|
|
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
|
|
|
// Change the executing path to YourApp
|
|
|
|
chdir("YourApp");
|
2017-05-14 17:54:14 +02:00
|
|
|
|
2012-03-08 18:05:47 +01:00
|
|
|
// Special environment to prefer .pyo, and don't write bytecode if .py are found
|
2016-11-19 06:01:27 +01:00
|
|
|
// because the process will not have a write attribute on the device.
|
2012-03-08 18:05:47 +01:00
|
|
|
putenv("PYTHONDONTWRITEBYTECODE=1");
|
|
|
|
putenv("PYTHONNOUSERSITE=1");
|
2012-03-11 17:22:45 +01:00
|
|
|
putenv("PYTHONPATH=.");
|
2018-11-09 18:48:08 +01:00
|
|
|
putenv("PYTHONUNBUFFERED=1");
|
|
|
|
// putenv("PYTHONVERBOSE=1");
|
|
|
|
// putenv("PYOBJUS_DEBUG=1");
|
2017-05-14 17:54:14 +02:00
|
|
|
|
2016-11-19 06:01:27 +01:00
|
|
|
// Kivy environment to prefer some implementation on iOS platform
|
2012-03-08 18:05:47 +01:00
|
|
|
putenv("KIVY_BUILD=ios");
|
|
|
|
putenv("KIVY_NO_CONFIG=1");
|
|
|
|
putenv("KIVY_NO_FILELOG=1");
|
2015-02-14 13:05:54 +01:00
|
|
|
putenv("KIVY_WINDOW=sdl2");
|
2013-03-08 00:07:55 +01:00
|
|
|
putenv("KIVY_IMAGE=imageio,tex");
|
2015-02-15 16:19:12 +01:00
|
|
|
putenv("KIVY_AUDIO=sdl2");
|
2017-05-14 17:54:14 +02:00
|
|
|
putenv("KIVY_GL_BACKEND=sdl2");
|
2013-04-15 15:28:02 +02:00
|
|
|
#ifndef DEBUG
|
2013-03-08 00:07:55 +01:00
|
|
|
putenv("KIVY_NO_CONSOLELOG=1");
|
|
|
|
#endif
|
2017-05-14 17:54:14 +02:00
|
|
|
|
2013-04-26 12:13:23 +02:00
|
|
|
// Export orientation preferences for Kivy
|
|
|
|
export_orientation();
|
2012-10-15 12:40:01 +02:00
|
|
|
|
2012-03-08 18:05:47 +01:00
|
|
|
NSString * resourcePath = [[NSBundle mainBundle] resourcePath];
|
2018-11-09 18:48:08 +01:00
|
|
|
#if PY_MAJOR_VERSION == 2
|
2012-03-08 18:05:47 +01:00
|
|
|
NSLog(@"PythonHome is: %s", (char *)[resourcePath UTF8String]);
|
|
|
|
Py_SetPythonHome((char *)[resourcePath UTF8String]);
|
2018-11-09 18:48:08 +01:00
|
|
|
#else
|
|
|
|
NSString *python_home = [NSString stringWithFormat:@"PYTHONHOME=%@", resourcePath, nil];
|
|
|
|
putenv((char *)[python_home UTF8String]);
|
|
|
|
|
|
|
|
NSString *python_path = [NSString stringWithFormat:@"PYTHONPATH=%@:%@/lib/python3.7/:%@/lib/python3.7/site-packages", resourcePath, resourcePath, resourcePath, nil];
|
|
|
|
putenv((char *)[python_path UTF8String]);
|
|
|
|
|
|
|
|
NSString *tmp_path = [NSString stringWithFormat:@"TMP=%@/tmp", resourcePath, nil];
|
|
|
|
putenv((char *)[tmp_path UTF8String]);
|
|
|
|
#endif
|
2012-03-08 18:05:47 +01:00
|
|
|
|
|
|
|
NSLog(@"Initializing python");
|
2017-05-14 17:54:14 +02:00
|
|
|
Py_Initialize();
|
2018-11-09 18:48:08 +01:00
|
|
|
|
|
|
|
#if PY_MAJOR_VERSION == 2
|
2012-03-08 18:05:47 +01:00
|
|
|
PySys_SetArgv(argc, argv);
|
2018-11-09 18:48:08 +01:00
|
|
|
#else
|
|
|
|
wchar_t** python_argv = PyMem_RawMalloc(sizeof(wchar_t *) *argc);
|
|
|
|
for (int i = 0; i < argc; i++)
|
|
|
|
python_argv[i] = Py_DecodeLocale(argv[i], NULL);
|
|
|
|
PySys_SetArgv(argc, python_argv);
|
|
|
|
#endif
|
2012-03-08 18:05:47 +01:00
|
|
|
|
2016-11-19 06:01:27 +01:00
|
|
|
// If other modules are using the thread, we need to initialize them before.
|
2012-03-08 18:05:47 +01:00
|
|
|
PyEval_InitThreads();
|
|
|
|
|
2013-04-26 12:13:23 +02:00
|
|
|
// Add an importer for builtin modules
|
|
|
|
load_custom_builtin_importer();
|
2013-01-05 12:17:52 +01:00
|
|
|
|
2012-03-08 18:05:47 +01:00
|
|
|
// Search and start main.py
|
2018-11-09 18:48:08 +01:00
|
|
|
#if PY_MAJOR_VERSION == 2
|
|
|
|
#define MAIN_EXT @"pyo"
|
|
|
|
#else
|
|
|
|
#define MAIN_EXT @"pyc"
|
|
|
|
#endif
|
|
|
|
|
2012-03-08 18:05:47 +01:00
|
|
|
const char * prog = [
|
2018-11-09 18:48:08 +01:00
|
|
|
[[NSBundle mainBundle] pathForResource:@"YourApp/main" ofType:MAIN_EXT] cStringUsingEncoding:
|
2012-03-08 18:05:47 +01:00
|
|
|
NSUTF8StringEncoding];
|
2018-11-09 18:48:08 +01:00
|
|
|
NSLog(@"Running main.py: %s", prog);
|
2012-03-08 18:05:47 +01:00
|
|
|
FILE* fd = fopen(prog, "r");
|
|
|
|
if ( fd == NULL ) {
|
|
|
|
ret = 1;
|
2018-11-09 18:48:08 +01:00
|
|
|
NSLog(@"Unable to open main.py, abort.");
|
2012-03-08 18:05:47 +01:00
|
|
|
} else {
|
|
|
|
ret = PyRun_SimpleFileEx(fd, prog, 1);
|
|
|
|
if (ret != 0)
|
|
|
|
NSLog(@"Application quit abnormally!");
|
|
|
|
}
|
2017-05-14 17:54:14 +02:00
|
|
|
|
2012-03-08 18:05:47 +01:00
|
|
|
Py_Finalize();
|
|
|
|
NSLog(@"Leaving");
|
2017-05-14 17:54:14 +02:00
|
|
|
|
2012-03-08 18:05:47 +01:00
|
|
|
[pool release];
|
2017-05-14 17:54:14 +02:00
|
|
|
|
2016-11-19 06:01:27 +01:00
|
|
|
// Look like the app still runs even when we left here.
|
2012-03-08 18:05:47 +01:00
|
|
|
exit(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2012-10-15 12:40:01 +02:00
|
|
|
|
2016-06-24 11:56:00 +02:00
|
|
|
// This method reads the available orientations from the Info.plist file and
|
|
|
|
// shares them via an environment variable. Kivy will automatically set the
|
|
|
|
// orientation according to this environment value, if it exists. To restrict
|
|
|
|
// the allowed orientation, please see the comments inside.
|
2012-10-15 12:40:01 +02:00
|
|
|
void export_orientation() {
|
2013-04-26 12:13:23 +02:00
|
|
|
NSDictionary *info = [[NSBundle mainBundle] infoDictionary];
|
|
|
|
NSArray *orientations = [info objectForKey:@"UISupportedInterfaceOrientations"];
|
2017-05-14 17:54:14 +02:00
|
|
|
|
2016-06-24 11:56:00 +02:00
|
|
|
// Orientation restrictions
|
|
|
|
// ========================
|
|
|
|
// Comment or uncomment blocks 1-3 in order the limit orientation support
|
2017-05-14 17:54:14 +02:00
|
|
|
|
2016-06-24 11:56:00 +02:00
|
|
|
// 1. Landscape only
|
|
|
|
// NSString *result = [[NSString alloc] initWithString:@"KIVY_ORIENTATION=LandscapeLeft LandscapeRight"];
|
2017-05-14 17:54:14 +02:00
|
|
|
|
2016-06-24 11:56:00 +02:00
|
|
|
// 2. Portrait only
|
|
|
|
// NSString *result = [[NSString alloc] initWithString:@"KIVY_ORIENTATION=Portrait PortraitUpsideDown"];
|
2017-05-14 17:54:14 +02:00
|
|
|
|
2016-06-24 11:56:00 +02:00
|
|
|
// 3. All orientations
|
2012-10-15 12:40:01 +02:00
|
|
|
NSString *result = [[NSString alloc] initWithString:@"KIVY_ORIENTATION="];
|
|
|
|
for (int i = 0; i < [orientations count]; i++) {
|
|
|
|
NSString *item = [orientations objectAtIndex:i];
|
|
|
|
item = [item substringFromIndex:22];
|
|
|
|
if (i > 0)
|
|
|
|
result = [result stringByAppendingString:@" "];
|
|
|
|
result = [result stringByAppendingString:item];
|
|
|
|
}
|
2016-06-24 11:56:00 +02:00
|
|
|
// ========================
|
2012-10-15 12:40:01 +02:00
|
|
|
|
2013-02-11 21:34:03 +01:00
|
|
|
putenv((char *)[result UTF8String]);
|
2015-02-14 13:05:54 +01:00
|
|
|
NSLog(@"Available orientation: %@", result);
|
2012-10-15 12:40:01 +02:00
|
|
|
}
|
|
|
|
|
2013-01-05 12:17:52 +01:00
|
|
|
void load_custom_builtin_importer() {
|
2013-04-26 12:13:23 +02:00
|
|
|
static const char *custom_builtin_importer = \
|
2018-11-09 18:48:08 +01:00
|
|
|
"import sys, imp, types\n" \
|
2013-04-26 12:13:23 +02:00
|
|
|
"from os import environ\n" \
|
|
|
|
"from os.path import exists, join\n" \
|
2018-11-09 18:48:08 +01:00
|
|
|
"try:\n" \
|
|
|
|
" # python 3\n"
|
|
|
|
" import _imp\n" \
|
|
|
|
" EXTS = _imp.extension_suffixes()\n" \
|
|
|
|
" sys.modules['subprocess'] = types.ModuleType(name='subprocess')\n" \
|
|
|
|
"except ImportError:\n" \
|
|
|
|
" EXTS = ['.so']\n"
|
2016-11-06 13:46:31 +01:00
|
|
|
"# Fake redirection to supress console output\n" \
|
|
|
|
"if environ.get('KIVY_NO_CONSOLE', '0') == '1':\n" \
|
|
|
|
" class fakestd(object):\n" \
|
|
|
|
" def write(self, *args, **kw): pass\n" \
|
|
|
|
" def flush(self, *args, **kw): pass\n" \
|
|
|
|
" sys.stdout = fakestd()\n" \
|
|
|
|
" sys.stderr = fakestd()\n" \
|
2013-04-26 12:13:23 +02:00
|
|
|
"# Custom builtin importer for precompiled modules\n" \
|
|
|
|
"class CustomBuiltinImporter(object):\n" \
|
|
|
|
" def find_module(self, fullname, mpath=None):\n" \
|
2018-11-09 18:48:08 +01:00
|
|
|
" # print(f'find_module() fullname={fullname} mpath={mpath}')\n" \
|
2013-04-26 12:13:23 +02:00
|
|
|
" if '.' not in fullname:\n" \
|
|
|
|
" return\n" \
|
2016-05-02 22:00:36 +02:00
|
|
|
" if not mpath:\n" \
|
2013-04-26 12:13:23 +02:00
|
|
|
" return\n" \
|
|
|
|
" part = fullname.rsplit('.')[-1]\n" \
|
2018-11-09 18:48:08 +01:00
|
|
|
" for ext in EXTS:\n" \
|
|
|
|
" fn = join(list(mpath)[0], '{}{}'.format(part, ext))\n" \
|
|
|
|
" # print('find_module() {}'.format(fn))\n" \
|
|
|
|
" if exists(fn):\n" \
|
|
|
|
" return self\n" \
|
2013-04-26 12:13:23 +02:00
|
|
|
" return\n" \
|
|
|
|
" def load_module(self, fullname):\n" \
|
|
|
|
" f = fullname.replace('.', '_')\n" \
|
|
|
|
" mod = sys.modules.get(f)\n" \
|
|
|
|
" if mod is None:\n" \
|
2018-11-09 18:48:08 +01:00
|
|
|
" # print('LOAD DYNAMIC', f, sys.modules.keys())\n" \
|
2013-04-26 12:13:23 +02:00
|
|
|
" try:\n" \
|
|
|
|
" mod = imp.load_dynamic(f, f)\n" \
|
|
|
|
" except ImportError:\n" \
|
2018-11-09 18:48:08 +01:00
|
|
|
" import traceback; traceback.print_exc();\n" \
|
|
|
|
" # print('LOAD DYNAMIC FALLBACK', fullname)\n" \
|
2013-04-26 12:13:23 +02:00
|
|
|
" mod = imp.load_dynamic(fullname, fullname)\n" \
|
2017-05-14 17:54:14 +02:00
|
|
|
" sys.modules[fullname] = mod\n" \
|
2013-04-26 12:13:23 +02:00
|
|
|
" return mod\n" \
|
|
|
|
" return mod\n" \
|
2018-11-09 18:48:08 +01:00
|
|
|
"sys.meta_path.insert(0, CustomBuiltinImporter())";
|
2013-04-26 12:13:23 +02:00
|
|
|
PyRun_SimpleString(custom_builtin_importer);
|
2013-01-05 12:17:52 +01:00
|
|
|
}
|