aboutsummaryrefslogtreecommitdiff
path: root/app/src
diff options
context:
space:
mode:
authorArslaan Pathan <[email protected]>2026-08-17 13:07:44 +1200
committerArslaan Pathan <[email protected]>2026-08-17 13:07:44 +1200
commitcb8cd8dc94bbcf3c0fddff97a7fc8c322bf65de8 (patch)
treee9e899a033dc729e665e40373ef4d4d58a818226 /app/src
parenta01f01f610fccbff5fb555f34c9d192aaeab8140 (diff)
downloadmoku-assistant-main.tar.xz
moku-assistant-main.zip
[fix] please dont crashHEADmain
Diffstat (limited to 'app/src')
-rw-r--r--app/src/main/jni/main.c67
1 files changed, 42 insertions, 25 deletions
diff --git a/app/src/main/jni/main.c b/app/src/main/jni/main.c
index 0fa7be7..b8dd997 100644
--- a/app/src/main/jni/main.c
+++ b/app/src/main/jni/main.c
@@ -24,17 +24,28 @@
#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");
-}
+enum Color {
+ COLOR_RED = 0x000000ff,
+ COLOR_GREEN = 0x0000ff00,
+ COLOR_BLUE = 0x00ff0000
+};
-JNIEXPORT void JNICALL Java_com_arslaancodes_mokuassistant_AssistantService_stopAssistant(JNIEnv *env, jclass clazz) {
- (void)env;
- (void)clazz;
- LOGI("stopAssistant called");
-}
+struct engine {
+ struct android_app *app;
+ int window_initialized;
+ int running;
+ uint64_t last_update;
+ int color;
+ int service_started;
+};
+
+/* DO NOT USE THIS INSTANCE.
+ * USE VARIABLES THAT HAVE BEEN PASSED TO THE FUNCTION(s) IN PARTICULAR
+ * THIS INSTANCE IS FOR JNI FUNCTIONS AND JNI FUNCTIONS ONLY.
+ * IT IS VERY JANK.
+ * GLOBALS ARE A MEMORY LEAK OR UAF WAITING TO HAPPEN.
+ */
+static struct engine *engine_instance = NULL;
void request_mic_permission(struct android_app *app) {
JNIEnv *env;
@@ -80,6 +91,20 @@ void request_notification_permission(struct android_app *app) {
LOGI("Notification permission requested");
}
+JNIEXPORT void JNICALL Java_com_arslaancodes_mokuassistant_AssistantService_startAssistant(JNIEnv *env, jclass clazz) {
+ (void)env;
+ (void)clazz;
+ LOGI("startAssistant called");
+ if (!engine_instance) return;
+ request_mic_permission(engine_instance->app);
+}
+
+JNIEXPORT void JNICALL Java_com_arslaancodes_mokuassistant_AssistantService_stopAssistant(JNIEnv *env, jclass clazz) {
+ (void)env;
+ (void)clazz;
+ LOGI("stopAssistant called");
+}
+
void start_assistant_service(struct android_app *app) {
JNIEnv *env;
JavaVM *jvm = app->activity->vm;
@@ -117,20 +142,6 @@ void start_assistant_service(struct android_app *app) {
LOGI("AssistantService started from C via JNI");
}
-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;
@@ -235,6 +246,10 @@ void engine_attach_window(struct engine *engine) {
}
engine->window_initialized = 1;
engine->last_update = get_timestamp_ns();
+ if (!engine->service_started) {
+ start_assistant_service(state);
+ engine->service_started;
+ }
};
void engine_detach_window(struct engine *engine) {
@@ -292,12 +307,13 @@ void android_main(struct android_app *state) {
LOGE("Failed to create engine!");
return;
}
+ engine_instance = engine;
state->userData = engine;
state->onAppCmd = engine_handle_cmd;
request_notification_permission(state);
- start_assistant_service(state);
+ request_mic_permission(state);
while (!state->destroyRequested) {
if (ALooper_pollAll(-1, NULL, &events, (void**)&source) >= 0) {
@@ -308,5 +324,6 @@ void android_main(struct android_app *state) {
/* more stuff here */
}
+ engine_instance = NULL;
free(engine);
}