|
1 | 1 | #include <jni.h> |
2 | | -#include <errno.h> |
3 | | -#include <android/log.h> |
4 | 2 | #include <android_native_app_glue.h> |
5 | 3 |
|
6 | | -#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "python", __VA_ARGS__)) |
7 | | -#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "python", __VA_ARGS__)) |
| 4 | +#include "utils.c" |
8 | 5 |
|
9 | | -#define PY_SSIZE_T_CLEAN |
10 | | -#include "Python.h" |
11 | | -#ifndef Py_PYTHON_H |
12 | | -#error Python headers needed to compile C extensions, please install development version of Python. |
13 | | -#endif |
14 | | - |
15 | | -struct android_app *g_state = NULL; |
16 | | - |
17 | | - |
18 | | -static PyObject *androidembed_poll(PyObject *self, PyObject *args) { |
19 | | - int indent; |
20 | | - int events; |
21 | | - struct android_poll_source *source; |
22 | | - int timeout; |
23 | | - |
24 | | - if (!PyArg_ParseTuple(args, "i", &timeout)) { |
25 | | - return NULL; |
26 | | - } |
27 | | - |
28 | | - while ((indent = ALooper_pollAll( |
29 | | - timeout, NULL, &events, (void **)&source)) >= 0) { |
30 | | - |
31 | | - // Process this event |
32 | | - if (source != NULL) { |
33 | | - source->process(g_state, source); |
34 | | - } |
35 | | - |
36 | | - // Check if we are exiting. |
37 | | - if (g_state->destroyRequested != 0) { |
38 | | - Py_RETURN_FALSE; |
39 | | - } |
40 | | - } |
41 | | - |
42 | | - Py_RETURN_TRUE; |
43 | | -} |
44 | | - |
45 | | -static PyObject *androidembed_log(PyObject *self, PyObject *args) { |
46 | | - char *logstr = NULL; |
47 | | - if (!PyArg_ParseTuple(args, "s", &logstr)) { |
48 | | - return NULL; |
49 | | - } |
50 | | - __android_log_print(ANDROID_LOG_INFO, "python", "%s", logstr); |
51 | | - Py_RETURN_NONE; |
52 | | -} |
53 | | - |
54 | | -static PyMethodDef AndroidEmbedMethods[] = { |
55 | | - {"log", androidembed_log, METH_VARARGS, "Log on android platform"}, |
56 | | - {"poll", androidembed_poll, METH_VARARGS, "Poll the android events"}, |
57 | | - {NULL, NULL, 0, NULL} |
58 | | -}; |
59 | | - |
60 | | -PyMODINIT_FUNC initandroidembed(void) { |
61 | | - (void) Py_InitModule("androidembed", AndroidEmbedMethods); |
62 | | -} |
63 | | - |
64 | | -int asset_extract(AAssetManager *am, char *src_file, char *dst_file) { |
65 | | - FILE *fhd = fopen(dst_file, "wb"); |
66 | | - if (fhd == NULL) { |
67 | | - LOGW("Unable to open descriptor for %s (errno=%d:%s)", |
68 | | - dst_file, errno, strerror(errno)); |
69 | | - return -1; |
70 | | - } |
71 | | - |
72 | | - AAsset *asset = AAssetManager_open(am, src_file, AASSET_MODE_BUFFER); |
73 | | - if (asset == NULL) { |
74 | | - LOGW("Unable to open asset %s", src_file); |
75 | | - return -1; |
76 | | - } |
77 | | - |
78 | | - off_t l = AAsset_getLength(asset); |
79 | | - fwrite(AAsset_getBuffer(asset),l, 1, fhd); |
80 | | - fclose(fhd); |
81 | | - AAsset_close(asset); |
82 | | - |
83 | | - return 0; |
84 | | -} |
85 | 6 |
|
86 | 7 | void android_main(struct android_app* state) { |
87 | 8 | app_dummy(); |
@@ -145,6 +66,15 @@ void android_main(struct android_app* state) { |
145 | 66 | return; |
146 | 67 | } |
147 | 68 |
|
| 69 | + // pass a module name as argument: _bootstrap.py use it as the main module |
| 70 | + int argc; |
| 71 | + char * argv[2]; |
| 72 | + argc = 2; |
| 73 | + argv[0] = "_bootstrap.py"; |
| 74 | + argv[1] = "main"; |
| 75 | + |
| 76 | + PySys_SetArgv(argc, argv); |
| 77 | + |
148 | 78 | // run the python bootstrap |
149 | 79 | LOGI("Run _bootstrap.py >>>"); |
150 | 80 | FILE *fhd = fopen(bootstrap_fn, "rb"); |
|
0 commit comments