Skip to content

Commit 413de2c

Browse files
committed
bootstrap: add a method to poll android event in a python loop.
1 parent e44804d commit 413de2c

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

bootstrap/minimal/jni/main.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,36 @@
1212
#error Python headers needed to compile C extensions, please install development version of Python.
1313
#endif
1414

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+
1545
static PyObject *androidembed_log(PyObject *self, PyObject *args) {
1646
char *logstr = NULL;
1747
if (!PyArg_ParseTuple(args, "s", &logstr)) {
@@ -22,8 +52,8 @@ static PyObject *androidembed_log(PyObject *self, PyObject *args) {
2252
}
2353

2454
static PyMethodDef AndroidEmbedMethods[] = {
25-
{"log", androidembed_log, METH_VARARGS,
26-
"Log on android platform"},
55+
{"log", androidembed_log, METH_VARARGS, "Log on android platform"},
56+
{"poll", androidembed_poll, METH_VARARGS, "Poll the android events"},
2757
{NULL, NULL, 0, NULL}
2858
};
2959

@@ -55,7 +85,9 @@ int asset_extract(AAssetManager *am, char *src_file, char *dst_file) {
5585

5686
void android_main(struct android_app* state) {
5787
app_dummy();
88+
5889
LOGI("Starting minimal bootstrap.");
90+
g_state = state;
5991

6092
char *env_argument = NULL;
6193
int fd = -1;

0 commit comments

Comments
 (0)