aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/jni/main.c
blob: 44f4a40840b6ebe7847eb23d2389e7ae76ea35af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <vosk_api.h>
#include <aaudio/AAudio.h>
#include <android_native_app_glue.h>
#include <android/log.h>
#include <android/sensor.h>
#include <stdint.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <android/native_window.h>
#include <android/set_abort_message.h>
#include <android/choreographer.h>
#include <android/hardware_buffer.h>
#include <time.h>
#include <jni.h>

#define LOG_TAG "moku"

#define _LOG(priority, fmt, ...)                    \
  ((void)__android_log_print((priority), (LOG_TAG), \
                             (fmt)__VA_OPT__(, ) __VA_ARGS__))

#define LOGE(fmt, ...) _LOG(ANDROID_LOG_ERROR, (fmt)__VA_OPT__(, ) __VA_ARGS__)
#define LOGW(fmt, ...) _LOG(ANDROID_LOG_WARN, (fmt)__VA_OPT__(, ) __VA_ARGS__)
#define LOGI(fmt, ...) _LOG(ANDROID_LOG_INFO, (fmt)__VA_OPT__(, ) __VA_ARGS__)

JNIEXPORT void JNICALL Java_com_arslaancodes_mokuassistant_AssistantService_startAssistant(JNIEnv *env, jclass clazz) {
	(void)env;
	(void)clazz;
	LOGI("startAssistant called");
}

JNIEXPORT void JNICALL Java_com_arslaancodes_mokuassistant_AssistantService_stopAssistant(JNIEnv *env, jclass clazz) {
	(void)env;
	(void)clazz;
	LOGI("stopAssistant called");
}

enum Color {
	COLOR_RED = 0x000000ff,
	COLOR_GREEN = 0x0000ff00,
	COLOR_BLUE = 0x00ff0000
};

struct engine {
	struct android_app *app;
	int window_initialized;
	int running;
	uint64_t last_update;
	int color;
};

struct engine *engine_new(struct android_app* app) {
	struct engine *engine;

	engine = malloc(sizeof(struct engine));
	if (!engine) {
		LOGE("Failed to allocate engine!");
		return NULL;
	}

	engine->window_initialized = 0;
	engine->app = app;
	engine->running = 0;
	engine->color = COLOR_RED;
	engine->last_update = (uint64_t)0;

	return engine;
}

uint64_t get_timestamp_ns() {
	struct timespec ts;
	clock_gettime(CLOCK_REALTIME, &ts);
	return (uint64_t)ts.tv_nsec + (uint64_t)ts.tv_sec * 1000000000;
}

static void engine_schedule_tick(struct engine *engine);

static void engine_draw_frame(struct engine *engine) {
	ANativeWindow_Buffer buffer;
	int x, y;
	size_t pixel_idx;

	if (!engine->app->window) return;

	if (ANativeWindow_lock(engine->app->window, &buffer, NULL) < 0) {
		LOGE("Unable to lock window buffer");
		return;
	}

	if (!engine->window_initialized) {
		return;
	}

	for (y = 0; y < buffer.height; y++) {
		for (x = 0; x < buffer.width; x++) {
			pixel_idx = y * buffer.stride + x;
			((uint32_t*)(buffer.bits))[pixel_idx] = (uint32_t)engine->color;
		}
	}

	ANativeWindow_unlockAndPost(engine->app->window);
}

static void engine_update(struct engine *engine) {
	(void)engine;
	/* uint64_t now = get_timestamp_ns();
	if (now - engine->last_update > 1000000000LL) {
		switch (engine->color) {
			case COLOR_RED:
				engine->color = COLOR_GREEN;
				break;
			case COLOR_GREEN:
				engine->color = COLOR_BLUE;
				break;
			case COLOR_BLUE:
				engine->color = COLOR_RED;
				break;
			default:
				LOGE("unexpected color %08x", (uint32_t)engine->color);
		}
		engine->last_update = now;
	} */
}

static void engine_real_tick(struct engine *engine) {
	if (!engine->running) return;

	// LOGI("tick");
	engine_schedule_tick(engine);
	engine_update(engine);
	engine_draw_frame(engine);
}

static void engine_tick(long frameTimeNanos, void *data) {
	(void)frameTimeNanos;
	struct engine *engine;

	if (!data) return;
	engine = (struct engine*)data;

	engine_real_tick(engine);
}

static void engine_schedule_tick(struct engine *engine) {
	AChoreographer_postFrameCallback(AChoreographer_getInstance(), engine_tick, (void*)engine);
}

/* TODO implement these stubs */
void engine_attach_window(struct engine *engine) {
	if (ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM) < 0) {
		engine->window_initialized = 0;
		return;
	}
	engine->window_initialized = 1;	
	engine->last_update = get_timestamp_ns();
};

void engine_detach_window(struct engine *engine) {
	/* Called when native window for app is destroyed. 
	 * Doesn't mean app is being killed, also happens on rotate
	 * Normally, with a traditional OpenGL app, do cleanup here
	 * At the moment, it's fine to just set a flag to disable the rendering loop
	 */
	engine->window_initialized = 0;
};

void engine_resume(struct engine *engine) { 
	if (!engine->running) {
		engine->running = 1;
		engine_schedule_tick(engine);
		LOGI("engine running");
	}
};


void engine_pause(struct engine *engine) {	
	engine->running = 0;
	LOGI("engine paused");
};

static void engine_handle_cmd(struct android_app *app, int32_t cmd) {
	struct engine *engine;

	engine = (struct engine *)app->userData;

	switch (cmd) {
		case APP_CMD_INIT_WINDOW:
			engine_attach_window(engine);
			break;
		case APP_CMD_TERM_WINDOW:
			engine_detach_window(engine);
			break;
		case APP_CMD_GAINED_FOCUS:
			engine_resume(engine);
			break;
		case APP_CMD_LOST_FOCUS:
			engine_pause(engine);
			break;
	}
}

void android_main(struct android_app *state) {
	/* Declarations */
	struct android_poll_source *source;
	struct engine *engine;
	int events;

	engine = engine_new(state);
	if (!engine) {
		LOGE("Failed to create engine!");
		return;
	}

	state->userData = engine;
	state->onAppCmd = engine_handle_cmd;

	/* do stuff here */
	
	while (!state->destroyRequested) {
		if (ALooper_pollAll(-1, NULL, &events, (void**)&source) >= 0) {
			if (source) {
				source->process(state, source);
			}
		}
		/* more stuff here */
	}

	free(engine);
}