19
19
#include "SDL_opengles2.h"
20
20
21
21
#define ENTRYPOINT_MAXLEN 128
22
- #define LOG (x ) __android_log_write(ANDROID_LOG_INFO, "python", (x))
22
+ #define LOG (n , x ) __android_log_write(ANDROID_LOG_INFO, (n), (x))
23
+ #define LOGP (x ) LOG("python", (x))
23
24
24
25
static PyObject * androidembed_log (PyObject * self , PyObject * args ) {
25
26
char * logstr = NULL ;
26
27
if (!PyArg_ParseTuple (args , "s" , & logstr )) {
27
28
return NULL ;
28
29
}
29
- LOG (logstr );
30
+ LOG (getenv ( "PYTHON_NAME" ), logstr );
30
31
Py_RETURN_NONE ;
31
32
}
32
33
@@ -70,20 +71,27 @@ int main(int argc, char *argv[]) {
70
71
71
72
char * env_argument = NULL ;
72
73
char * env_entrypoint = NULL ;
74
+ char * env_logname = NULL ;
73
75
char entrypoint [ENTRYPOINT_MAXLEN ];
74
76
int ret = 0 ;
75
77
FILE * fd ;
76
78
77
79
/* AND: Several filepaths are hardcoded here, these must be made
78
80
configurable */
79
81
/* AND: P4A uses env vars...not sure what's best */
80
- LOG ("Initialize Python for Android" );
82
+ LOGP ("Initialize Python for Android" );
81
83
env_argument = getenv ("ANDROID_ARGUMENT" );
82
84
setenv ("ANDROID_APP_PATH" , env_argument , 1 );
83
85
env_entrypoint = getenv ("ANDROID_ENTRYPOINT" );
86
+ env_logname = getenv ("PYTHON_NAME" );
87
+
88
+ if (env_logname == NULL ) {
89
+ env_logname = "python" ;
90
+ setenv ("PYTHON_NAME" , "python" , 1 );
91
+ }
84
92
85
- LOG ("Changing directory to the one provided by ANDROID_ARGUMENT" );
86
- LOG (env_argument );
93
+ LOGP ("Changing directory to the one provided by ANDROID_ARGUMENT" );
94
+ LOGP (env_argument );
87
95
chdir (env_argument );
88
96
89
97
Py_SetProgramName (L"android_python" );
@@ -94,31 +102,31 @@ int main(int argc, char *argv[]) {
94
102
PyImport_AppendInittab ("androidembed" , initandroidembed );
95
103
#endif
96
104
97
- LOG ("Preparing to initialize python" );
105
+ LOGP ("Preparing to initialize python" );
98
106
99
107
if (dir_exists ("crystax_python/" )) {
100
- LOG ("crystax_python exists" );
108
+ LOGP ("crystax_python exists" );
101
109
char paths [256 ];
102
110
snprintf (paths , 256 ,
103
111
"%s/crystax_python/stdlib.zip:%s/crystax_python/modules" ,
104
112
env_argument , env_argument );
105
113
/* snprintf(paths, 256, "%s/stdlib.zip:%s/modules", env_argument,
106
114
* env_argument); */
107
- LOG ("calculated paths to be..." );
108
- LOG (paths );
115
+ LOGP ("calculated paths to be..." );
116
+ LOGP (paths );
109
117
110
118
#if PY_MAJOR_VERSION >= 3
111
119
wchar_t * wchar_paths = Py_DecodeLocale (paths , NULL );
112
120
Py_SetPath (wchar_paths );
113
121
#else
114
122
char * wchar_paths = paths ;
115
- LOG ("Can't Py_SetPath in python2, so crystax python2 doesn't work yet" );
123
+ LOGP ("Can't Py_SetPath in python2, so crystax python2 doesn't work yet" );
116
124
exit (1 );
117
125
#endif
118
126
119
- LOG ("set wchar paths..." );
127
+ LOGP ("set wchar paths..." );
120
128
} else {
121
- LOG ("crystax_python does not exist" );
129
+ LOGP ("crystax_python does not exist" );
122
130
}
123
131
124
132
Py_Initialize ();
@@ -127,11 +135,11 @@ int main(int argc, char *argv[]) {
127
135
PySys_SetArgv (argc , argv );
128
136
#endif
129
137
130
- LOG ("Initialized python" );
138
+ LOGP ("Initialized python" );
131
139
132
140
/* ensure threads will work.
133
141
*/
134
- LOG ("AND: Init threads" );
142
+ LOGP ("AND: Init threads" );
135
143
PyEval_InitThreads ();
136
144
137
145
#if PY_MAJOR_VERSION < 3
@@ -147,7 +155,7 @@ int main(int argc, char *argv[]) {
147
155
PyRun_SimpleString ("import sys, posix\n" );
148
156
if (dir_exists ("lib" )) {
149
157
/* If we built our own python, set up the paths correctly */
150
- LOG ("Setting up python from ANDROID_PRIVATE" );
158
+ LOGP ("Setting up python from ANDROID_PRIVATE" );
151
159
PyRun_SimpleString ("private = posix.environ['ANDROID_PRIVATE']\n"
152
160
"argument = posix.environ['ANDROID_ARGUMENT']\n"
153
161
"sys.path[:] = [ \n"
@@ -194,31 +202,31 @@ int main(int argc, char *argv[]) {
194
202
PyRun_SimpleString ("import site; print site.getsitepackages()\n" );
195
203
#endif
196
204
197
- LOG ("AND: Ran string" );
205
+ LOGP ("AND: Ran string" );
198
206
199
207
/* run it !
200
208
*/
201
- LOG ("Run user program, change dir and execute entrypoint" );
209
+ LOGP ("Run user program, change dir and execute entrypoint" );
202
210
203
211
/* Get the entrypoint, search the .pyo then .py
204
212
*/
205
213
char * dot = strrchr (env_entrypoint , '.' );
206
214
if (dot <= 0 ) {
207
- LOG ("Invalid entrypoint, abort." );
215
+ LOGP ("Invalid entrypoint, abort." );
208
216
return -1 ;
209
217
}
210
218
if (strlen (env_entrypoint ) > ENTRYPOINT_MAXLEN - 2 ) {
211
- LOG ("Entrypoint path is too long, try increasing ENTRYPOINT_MAXLEN." );
219
+ LOGP ("Entrypoint path is too long, try increasing ENTRYPOINT_MAXLEN." );
212
220
return -1 ;
213
221
}
214
222
if (!strcmp (dot , ".pyo" )) {
215
223
if (!file_exists (env_entrypoint )) {
216
224
/* fallback on .py */
217
225
strcpy (entrypoint , env_entrypoint );
218
226
entrypoint [strlen (env_entrypoint ) - 1 ] = '\0' ;
219
- LOG (entrypoint );
227
+ LOGP (entrypoint );
220
228
if (!file_exists (entrypoint )) {
221
- LOG ("Entrypoint not found (.pyo, fallback on .py), abort" );
229
+ LOGP ("Entrypoint not found (.pyo, fallback on .py), abort" );
222
230
return -1 ;
223
231
}
224
232
} else {
@@ -232,21 +240,21 @@ int main(int argc, char *argv[]) {
232
240
if (!file_exists (entrypoint )) {
233
241
/* fallback on pure python version */
234
242
if (!file_exists (env_entrypoint )) {
235
- LOG ("Entrypoint not found (.py), abort." );
243
+ LOGP ("Entrypoint not found (.py), abort." );
236
244
return -1 ;
237
245
}
238
246
strcpy (entrypoint , env_entrypoint );
239
247
}
240
248
} else {
241
- LOG ("Entrypoint have an invalid extension (must be .py or .pyo), abort." );
249
+ LOGP ("Entrypoint have an invalid extension (must be .py or .pyo), abort." );
242
250
return -1 ;
243
251
}
244
- // LOG ("Entrypoint is:");
245
- // LOG (entrypoint);
252
+ // LOGP ("Entrypoint is:");
253
+ // LOGP (entrypoint);
246
254
fd = fopen (entrypoint , "r" );
247
255
if (fd == NULL ) {
248
- LOG ("Open the entrypoint failed" );
249
- LOG (entrypoint );
256
+ LOGP ("Open the entrypoint failed" );
257
+ LOGP (entrypoint );
250
258
return -1 ;
251
259
}
252
260
@@ -268,21 +276,24 @@ int main(int argc, char *argv[]) {
268
276
Py_Finalize ();
269
277
fclose (fd );
270
278
271
- LOG ("Python for android ended." );
279
+ LOGP ("Python for android ended." );
272
280
return ret ;
273
281
}
274
282
275
283
JNIEXPORT void JNICALL Java_org_kivy_android_PythonService_nativeStart (
276
284
JNIEnv * env , jobject thiz , jstring j_android_private ,
277
285
jstring j_android_argument , jstring j_service_entrypoint ,
278
- jstring j_python_home , jstring j_python_path , jstring j_arg ) {
286
+ jstring j_python_name , jstring j_python_home , jstring j_python_path ,
287
+ jstring j_arg ) {
279
288
jboolean iscopy ;
280
289
const char * android_private =
281
290
(* env )-> GetStringUTFChars (env , j_android_private , & iscopy );
282
291
const char * android_argument =
283
292
(* env )-> GetStringUTFChars (env , j_android_argument , & iscopy );
284
293
const char * service_entrypoint =
285
294
(* env )-> GetStringUTFChars (env , j_service_entrypoint , & iscopy );
295
+ const char * python_name =
296
+ (* env )-> GetStringUTFChars (env , j_python_name , & iscopy );
286
297
const char * python_home =
287
298
(* env )-> GetStringUTFChars (env , j_python_home , & iscopy );
288
299
const char * python_path =
@@ -293,6 +304,7 @@ JNIEXPORT void JNICALL Java_org_kivy_android_PythonService_nativeStart(
293
304
setenv ("ANDROID_ARGUMENT" , android_argument , 1 );
294
305
setenv ("ANDROID_ENTRYPOINT" , service_entrypoint , 1 );
295
306
setenv ("PYTHONOPTIMIZE" , "2" , 1 );
307
+ setenv ("PYTHON_NAME" , python_name , 1 );
296
308
setenv ("PYTHONHOME" , python_home , 1 );
297
309
setenv ("PYTHONPATH" , python_path , 1 );
298
310
setenv ("PYTHON_SERVICE_ARGUMENT" , arg , 1 );
0 commit comments