2 #include <msp/fs/path.h>
3 #include "application.h"
4 #include "errorlogger.h"
5 #include "mainthread.h"
12 MainThread::MainThread(ANativeActivity *a):
15 attach_to_activity(a);
20 void MainThread::attach_to_activity(ANativeActivity *a)
23 activity->callbacks->onDestroy = &activity_destroyed;
24 activity->callbacks->onNativeWindowCreated = &native_window_created;
25 activity->callbacks->onNativeWindowResized = &native_window_resized;
26 activity->callbacks->onNativeWindowDestroyed = &native_window_destroyed;
27 activity->callbacks->onInputQueueCreated = &input_queue_created;
28 activity->callbacks->onInputQueueDestroyed = &input_queue_destroyed;
29 activity->instance = this;
32 void MainThread::wait_for_app_created()
34 MutexLock lock(startup_mutex);
37 void MainThread::resume_startup()
42 startup_mutex.unlock();
46 void MainThread::main()
48 /* I have no idea how dependable this is, but it seems to be the only way
49 to get the package name aside from making a Java call through JNI */
50 char *appname = strdup(FS::Path(activity->internalDataPath)[-2].c_str());
51 char *argv[] = { appname, 0 };
52 Msp::Android::ErrorLogger err_logger;
53 Msp::Application::run(1, argv, this, &app_created);
56 ANativeActivity_finish(activity);
59 void MainThread::app_created(void *data)
61 reinterpret_cast<MainThread *>(data)->resume_startup();
64 void MainThread::activity_destroyed(ANativeActivity *activity)
66 reinterpret_cast<MainThread *>(activity->instance)->activity = 0;
69 void MainThread::native_window_created(ANativeActivity *activity, ANativeWindow *window)
71 reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_created.emit(window);
74 void MainThread::native_window_resized(ANativeActivity *activity, ANativeWindow *window)
76 reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_resized.emit(window);
79 void MainThread::native_window_destroyed(ANativeActivity *activity, ANativeWindow *window)
81 reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_destroyed.emit(window);
84 void MainThread::input_queue_created(ANativeActivity *activity, AInputQueue *queue)
86 reinterpret_cast<MainThread *>(activity->instance)->signal_input_queue_created.emit(queue);
89 void MainThread::input_queue_destroyed(ANativeActivity *activity, AInputQueue *queue)
91 reinterpret_cast<MainThread *>(activity->instance)->signal_input_queue_destroyed.emit(queue);
94 } // namespace Android