3 #include <msp/fs/dir.h>
4 #include <msp/fs/path.h>
5 #include <msp/fs/utils.h>
6 #include "application.h"
7 #include "errorlogger.h"
8 #include "mainthread.h"
15 MainThread::MainThread(ANativeActivity *a):
16 asset_manager(a->assetManager),
17 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;
37 void MainThread::wait_for_app_created()
39 MutexLock lock(startup_mutex);
42 void MainThread::resume_startup()
47 startup_mutex.unlock();
51 void MainThread::main()
53 /* I have no idea how dependable this is, but it seems to be the only way
54 to get the package name aside from making a Java call through JNI */
55 char *appname = strdup(int_data_path[-2].c_str());
56 char *argv[] = { appname, 0 };
57 Msp::Android::ErrorLogger err_logger;
58 FS::chdir(FS::dirname(int_data_path));
59 Msp::Application::run(1, argv, this, &app_created);
63 activity->callbacks->onDestroy = &end_process;
64 ANativeActivity_finish(activity);
70 void MainThread::app_created(void *data)
72 reinterpret_cast<MainThread *>(data)->resume_startup();
75 void MainThread::activity_destroyed(ANativeActivity *activity)
77 reinterpret_cast<MainThread *>(activity->instance)->activity = 0;
80 void MainThread::native_window_created(ANativeActivity *activity, ANativeWindow *window)
82 reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_created.emit(window);
85 void MainThread::native_window_resized(ANativeActivity *activity, ANativeWindow *window)
87 reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_resized.emit(window);
90 void MainThread::native_window_destroyed(ANativeActivity *activity, ANativeWindow *window)
92 reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_destroyed.emit(window);
95 void MainThread::input_queue_created(ANativeActivity *activity, AInputQueue *queue)
97 reinterpret_cast<MainThread *>(activity->instance)->signal_input_queue_created.emit(queue);
100 void MainThread::input_queue_destroyed(ANativeActivity *activity, AInputQueue *queue)
102 reinterpret_cast<MainThread *>(activity->instance)->signal_input_queue_destroyed.emit(queue);
105 void MainThread::end_process(ANativeActivity *)
110 } // namespace Android