2 #include <msp/fs/dir.h>
3 #include <msp/fs/path.h>
4 #include <msp/fs/utils.h>
5 #include "application.h"
6 #include "errorlogger.h"
7 #include "mainthread.h"
14 MainThread::MainThread(ANativeActivity *a):
15 asset_manager(a->assetManager),
16 int_data_path(a->internalDataPath),
19 attach_to_activity(a);
24 void MainThread::attach_to_activity(ANativeActivity *a)
27 activity->callbacks->onDestroy = &activity_destroyed;
28 activity->callbacks->onNativeWindowCreated = &native_window_created;
29 activity->callbacks->onNativeWindowResized = &native_window_resized;
30 activity->callbacks->onNativeWindowDestroyed = &native_window_destroyed;
31 activity->callbacks->onInputQueueCreated = &input_queue_created;
32 activity->callbacks->onInputQueueDestroyed = &input_queue_destroyed;
33 activity->instance = this;
36 void MainThread::wait_for_app_created()
38 MutexLock lock(startup_mutex);
41 void MainThread::resume_startup()
46 startup_mutex.unlock();
50 void MainThread::main()
52 /* I have no idea how dependable this is, but it seems to be the only way
53 to get the package name aside from making a Java call through JNI */
54 char *appname = strdup(int_data_path[-2].c_str());
55 char *argv[] = { appname, 0 };
56 Msp::Android::ErrorLogger err_logger;
57 FS::chdir(FS::dirname(int_data_path));
58 Msp::Application::run(1, argv, this, &app_created);
61 ANativeActivity_finish(activity);
64 void MainThread::app_created(void *data)
66 reinterpret_cast<MainThread *>(data)->resume_startup();
69 void MainThread::activity_destroyed(ANativeActivity *activity)
71 reinterpret_cast<MainThread *>(activity->instance)->activity = 0;
74 void MainThread::native_window_created(ANativeActivity *activity, ANativeWindow *window)
76 reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_created.emit(window);
79 void MainThread::native_window_resized(ANativeActivity *activity, ANativeWindow *window)
81 reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_resized.emit(window);
84 void MainThread::native_window_destroyed(ANativeActivity *activity, ANativeWindow *window)
86 reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_destroyed.emit(window);
89 void MainThread::input_queue_created(ANativeActivity *activity, AInputQueue *queue)
91 reinterpret_cast<MainThread *>(activity->instance)->signal_input_queue_created.emit(queue);
94 void MainThread::input_queue_destroyed(ANativeActivity *activity, AInputQueue *queue)
96 reinterpret_cast<MainThread *>(activity->instance)->signal_input_queue_destroyed.emit(queue);
99 } // namespace Android