3 #include <android/window.h>
4 #include <msp/fs/dir.h>
5 #include <msp/fs/path.h>
6 #include <msp/fs/utils.h>
7 #include "application.h"
8 #include "errorlogger.h"
9 #include "mainthread.h"
16 MainThread::MainThread(ANativeActivity *a):
17 asset_manager(a->assetManager),
18 int_data_path(a->internalDataPath)
20 attach_to_activity(a);
25 void MainThread::attach_to_activity(ANativeActivity *a)
28 activity->callbacks->onDestroy = &activity_destroyed;
29 activity->callbacks->onNativeWindowCreated = &native_window_created;
30 activity->callbacks->onNativeWindowResized = &native_window_resized;
31 activity->callbacks->onNativeWindowDestroyed = &native_window_destroyed;
32 activity->callbacks->onInputQueueCreated = &input_queue_created;
33 activity->callbacks->onInputQueueDestroyed = &input_queue_destroyed;
34 activity->instance = this;
36 set_window_flags(AWINDOW_FLAG_FULLSCREEN, 0);
39 void MainThread::wait_for_app_created()
41 MutexLock lock(startup_mutex);
44 void MainThread::resume_startup()
49 startup_mutex.unlock();
53 JavaVM *MainThread::get_java_vm() const
61 void MainThread::set_window_flags(unsigned set, unsigned clear)
63 ANativeActivity_setWindowFlags(activity, set, clear);
66 void MainThread::main()
68 /* I have no idea how dependable this is, but it seems to be the only way
69 to get the package name aside from making a Java call through JNI */
70 char *appname = strdup(int_data_path[-2].c_str());
71 char *argv[] = { appname, nullptr };
72 Msp::Android::ErrorLogger err_logger;
73 FS::chdir(FS::dirname(int_data_path));
74 Msp::Application::run(1, argv, this, &app_created);
78 activity->callbacks->onDestroy = &end_process;
79 ANativeActivity_finish(activity);
85 void MainThread::app_created(void *data)
87 reinterpret_cast<MainThread *>(data)->resume_startup();
90 void MainThread::activity_destroyed(ANativeActivity *activity)
92 reinterpret_cast<MainThread *>(activity->instance)->activity = 0;
95 void MainThread::native_window_created(ANativeActivity *activity, ANativeWindow *window)
97 reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_created.emit(window);
100 void MainThread::native_window_resized(ANativeActivity *activity, ANativeWindow *window)
102 reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_resized.emit(window);
105 void MainThread::native_window_destroyed(ANativeActivity *activity, ANativeWindow *window)
107 reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_destroyed.emit(window);
110 void MainThread::input_queue_created(ANativeActivity *activity, AInputQueue *queue)
112 reinterpret_cast<MainThread *>(activity->instance)->signal_input_queue_created.emit(queue);
115 void MainThread::input_queue_destroyed(ANativeActivity *activity, AInputQueue *queue)
117 reinterpret_cast<MainThread *>(activity->instance)->signal_input_queue_destroyed.emit(queue);
120 void MainThread::end_process(ANativeActivity *)
125 } // namespace Android